November 8th, 2007

  • evan

tracing calls to a library

Say you have a complicated library whose functionality you're trying to use, but while you can run their sample apps, your implementation based on those apps fails. How can you diagnose it?

Here's a trick: first, build the library in debug mode (Debian of course will have you covered: apt-get install libgnutls13-dbg) so that you can get all its symbols. Then run gdb on the working binary. Set a breakpoint on main: b main; run it: r; and then when the breakpoint hits (and your target shared library symbols are loaded), tell gdb to break on all calls to the library, via the new-to-me rbreak command, which is documented like this: "Set a breakpoint for all functions matching REGEXP."

So rbreak ^gnutls_ and then c to continue, and from then on each time you hit enter it'll show the next gnutls function being called along with the exact arguments it's receiving. You can even do the same thing to your own program in another gdb session to watch them run side by side.

PS: There's gotta be some way to tell gdb to load all the shared library symbols without this breakpoint hack, but for all my digging I couldn't find it. gdb is pretty mysterious to me -- rbreak often prints out "Junk at end of arguments." for opaque reasons.