Thursday, October 22, 2009

gdb: incredibly useful stuff!

For ages, I'd wanted gdb to search for the source file recursively inside a given "dir". Now I finally figured out how to accomplish this. Not by a 'recursive dir search', but by setting a 'substitute path' in gdb. This makes life so much easier now! Woohoo! :D

Much much thanks to this source for setting substitute-paths for gdb:

You know, by using a set substitute-path <library-source-path>
installed-source-path
in the $HOME/.gdbinit file it is possible to
install the library source somewhere and have gdb replace the path
where the library's debug info says the source should be with where
you've installed the library's source.

On my system, for example, by using the 'strings' command, I'm able
to see that glibmm's debug info says that the source should be found
in /build/buildd/glibmm2.4-2.14.2/. I actually installed glibmm's
source in /usr/src/glibmm2.4-2.14.2. So by including the line set
substitute-path /build/buildd /usr/src
in ~/.gdbinit, gdb has no
problems finding glibmm's source any more.

This works with all debuggers (even nemiver) as long as ~/.gdbinit is
processed by gdb when it starts up. :-)


So this is my current ~/.gdbinit file:
set substitute-path /sandbox/builds/appframework_dev /data/source/branches/appframework_dev
And I tested it on piidentity.net right away. IT'S AWESOME! Works right outta da box!

You can now automatically "list" code immedidately after attaching gdb to an httpd process - totally painlessly. This is so much better than having to set each "dir" in gdb for each source directory you wanted to "list" as you debugged it. I love "set substitute-path" - it rocks!


Some more cool gdb stuff I learnt today:
  • interpreter-exec mi "-file-list-exec-source-files"
    (which is equivalent to "info sources")
  • interpreter-exec mi "-file-list-exec-source-file"
    (which is equivalent to "info source")
  • GDB has a ncurses-based GUI called TUI mode! Whoa!
    http://sources.redhat.com/gdb/current/onlinedocs/gdb_26.html

No comments: