Skip to content

Instantly share code, notes, and snippets.

@fujin
Created May 6, 2009 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fujin/107466 to your computer and use it in GitHub Desktop.
Save fujin/107466 to your computer and use it in GitHub Desktop.
Attaching to the process with GDB
Populate your .gdbinit and .gdb directories
In ~/.gdbinit:
define session-ruby
source ~/.gdb/ruby
end
Next, you need to download http://eigenclass.org/hiki.rb?c=plugin;plugin=attach_download;p=ruby+live+process+introspection;file_name=ruby
And place it in ~/.gdb/ruby.
$ mkdir -p ~/.gdb
$ wget 'http://eigenclass.org/hiki.rb?c=plugin;plugin=attach_download;p=ruby+live+process+introspection;file_name=ruby'
$ mv ruby ~/.gdb
Attach to the process, load the ruby helpers, and redirect stdout:
$ ps ax | grep ruby
845 s000 S+ 0:00.91 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /usr/bin/chef-indexer -l debug
932 s001 R+ 0:00.00 grep ruby
842 s002 S+ 0:00.44 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /usr/bin/stompserver
The important part here is the very first segment of the line (the PID).
$ gdb /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby 845
The first argument to gdb should be the full path to the ruby executable that is running the process.
Start your debug session
From above, you should see a bunch of output like this:
...
Loaded symbols for /srv/ruby/1.8.6/lib/ruby/1.8/i686-linux/nkf.so
0xbfffe402 in +kernel_vsyscall ()
And then you'll wind up at...
(gdb)
First things first, load up the ruby helpers.
(gdb) session-ruby
Then, redirect stdout to a file, so we can capture the output for later.
(gdb) redirect_stdout
Open up a second terminal, and you can watch the output from your debug session:
$ tail -f /tmp/ruby-debug.PID
Where PID above is equal to the PID you attached to with gdb.
Now that you are watching, flip back to your gdb session and..
Get some data
(gdb) eval "caller"
(gdb) rb_object_counts
(gdb) eval "total = ObjectSpace.each_object {|x| if x.class.to_s =~ /Puppet/; puts '---'; puts x.inspect; end }; puts \"---\nTotal Objects: #{total}\""
Or, for arrays only:
(gdb) eval "total = ObjectSpace.each_object(Array){|x| puts '---'; puts x.inspect }; puts \"---\nTotal Arrays: #{total}\""
You should see a stack trace, a long count of the number of objects in the system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment