gdb snippets

Setting LD_LIBRARY_PATH when using the Gnu Project Debugger (gdb)

Tagged ld_library_path, gdb  Languages bash

If you get this:

ImportError: lpthread.so: cannot open shared object file: No such file or directory

Do this:

set environment LD_LIBRARY_PATH=directory where lpthread.so is

The reason for this problem is that gdb doesn't use the same settings as your session so specifying LD_LIBRARY_PATH in your bash session won't work.

Inspecting a runaway Ruby process with GDB

Tagged ruby, gdb, debugging  Languages ruby

Here's how to inspect a live Ruby process that is eating 100% CPU:

First:

sudo apt-get install gdb

Then ~/.gdbinit:

define eval
  call(rb_p(rb_eval_string_protect($arg0,(int*)0)))
end

define redirect_stdout
  call rb_eval_string("$_old_stdout, $stdout = $stdout, File.open('/tmp/ruby-debug.' + Process.pid.to_s, 'a'); $stdout.sync = true")
end

Now:

$ sudo gdb attach <PID>
(gdb) redirect_stdout
(gdb) eval("Kernel.caller")
(gdb) eval "ObjectSpace.each_object {|o| puts \"#{o.class.name}, #{o.inspect} -- #{o.object_id}\" unless o.is_a?(String) }; puts '----'"

Thanks: http://rrn.dk/running-ruby-process-callstack http://www.coffeepowered.net/2010/08/23/mongomapper-development-mode-and-memory-leaks/