Skip to content

Instantly share code, notes, and snippets.

@3rdman
Forked from ice799/core_helper.rb
Created September 25, 2010 00:39
Show Gist options
  • Select an option

  • Save 3rdman/596303 to your computer and use it in GitHub Desktop.

Select an option

Save 3rdman/596303 to your computer and use it in GitHub Desktop.

Revisions

  1. @ice799 ice799 revised this gist Sep 20, 2010. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions core_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    #!/custom/ree/bin/ruby

    # USAGE:
    #
    # echo "|/path/to/core_helper.rb %p %s %u %g" > /proc/sys/kernel/core_pattern
    #

    require 'etc'
    require 'net/smtp'

  2. @ice799 ice799 created this gist Sep 20, 2010.
    65 changes: 65 additions & 0 deletions core_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/custom/ree/bin/ruby

    require 'etc'
    require 'net/smtp'

    CORE_DIR = "/cores"
    TO_EMAIL = "[email protected]"
    FROM_EMAIL = "[email protected]"
    MAIL_SERVER = "blah"

    class CoreHelper
    def self.start!

    time = Time.now.strftime("%Y-%m-%d-%H:%M")
    corefile = "#{CORE_DIR}/core-#{time}.core"
    cmdline = File.read("/proc/#{ARGV[0]}/cmdline")

    f = File.new("#{CORE_DIR}/core-#{time}-metadata", "w")
    f.puts "PID: #{ARGV[0]}"
    f.puts "Signal: #{ARGV[1]}"
    f.puts "UID: #{Etc.getpwuid(ARGV[2].to_i)[:name]}/#{ARGV[2]}, GID: #{Etc.getgrgid(ARGV[3].to_i)[:name]}/#{ARGV[3]}"
    f.puts "Command line: #{cmdline}"
    f.puts
    f.puts
    f.flush

    core = File.new(corefile, "w")
    begin
    while (chunk = STDIN.read)
    core.write chunk
    break if chunk == nil || chunk.length == 0
    end
    rescue EOFError
    core.close
    end
    core.close

    r,w = IO.pipe
    child = fork{ $stdin.reopen(r); $stdout.reopen(f); $stderr.reopen(f); exec "/usr/bin/gdb -q -c #{corefile}" }
    w.puts "set height 0"
    w.puts "backtrace"
    w.puts "info registers"
    w.puts "x/5i $rip"
    w.puts "quit"
    Process.wait

    msg = <<MESSAGE
    From: #{FROM_EMAIL}
    To: #{TO_EMAIL}
    Subject: Core dump on #{`hostname`}
    #{File.read("#{CORE_DIR}/core-#{time}-metadata")}
    MESSAGE

    Net::SMTP.start(MAIL_SERVER) do |smtp|
    smtp.send_message msg, FROM_EMAIL, TO_EMAIL
    end

    f.close

    exit 0
    end
    end

    CoreHelper.start!