tmm1 (owner)

Forks

Revisions

gist: 209839 Download_button fork
public
Description:
debug a process using strace/gdb
Public Clone URL: git://gist.github.com/209839.git
Embed All Files: show embed
debug.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# collect information about a running process
# ruby debug.rb <pid>
 
begin
  raise ArgumentError unless pid = ARGV[0]
  pid = pid.to_i
  raise ArgumentError unless pid > 0
  Process.kill(0,pid)
rescue TypeError, ArgumentError
  raise 'pid required'
rescue Errno::ESRCH
  raise 'invalid pid'
rescue Errno::EPERM
  raise 'could not signal process (run as root)'
end
 
require 'fileutils'
dir = FileUtils.mkdir_p "/tmp/debug-#{pid}"
puts "Debugging process #{pid}, results in #{dir}."
 
File.open("#{dir}/proc-status.txt",'w'){ |f| f.write File.read("/proc/#{pid}/status") }
 
10.times do |i|
  out = File.open("#{dir}/gdb-backtrace-#{i}.txt",'w')
  r,w = IO.pipe
  child = fork{ $stdin.reopen(r); $stdout.reopen(out); $stderr.reopen(out); exec "gdb /proc/#{pid}/exe" }
  w.puts "attach #{pid}"
  w.puts "set height 0"
  w.puts "backtrace"
  w.puts "detach"
  w.puts "quit"
  Process.wait
  out.close
 
  sleep 1
end
 
out = File.open("#{dir}/lsof.txt",'w')
child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "lsof -nPp #{pid}" }
Process.wait
out.close
 
out = File.open("#{dir}/strace-summary.txt",'w')
child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -cp #{pid}" }
sleep 5
Process.kill 'HUP', child
Process.wait
out.close
 
out = File.open("#{dir}/strace-log.txt",'w')
child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -ttTp #{pid}" }
sleep 5
Process.kill 'HUP', child
Process.wait
out.close