Created
May 6, 2009 10:03
-
-
Save fujin/107466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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