Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging/tracing inside a shared library during runtime?

I'm trying to understand how a certain library works. I've compiled it with my added prinfts and everything is great. Now I want to stop the example program during runtime to look at the call stack, but I can't quite figure out how to do it with gdb. The function I want to break on, is inside a shared library. I've reviewed a previous question here on SO, but the approach doesn't work for me. The language in question is C++. I've attempted to provide the filename and line number, but gdb refuses to understand that, it only lists the source files from the demo app.

Any suggestions?

like image 634
EightyEight Avatar asked Aug 26 '09 21:08

EightyEight


2 Answers

You can do "break main" first. By the time you hit that, the shared library should be loaded, and you can then set a breakpoint in any of its routines.

like image 82
Jim Lewis Avatar answered Oct 21 '22 06:10

Jim Lewis


There are two cases to consider (and your question doesn't make it clear which case you have):
- your executable is linked with the shared library directly:
this means that GDB will "see" the symbols (and sources) from shared library when you stop on main
- your executable dynamically loads the shared library (e.g. via dlopen):
in that case, GDB will not "see" your shared library until after dlopen completes.

Since you can't see the symbols when you stop at main, I am guessing you have the second case. You can do "set stop-on-solib-events 1" at the (gdb) prompt, and GDB will stop every time a new shared library is loaded (or unloaded).

You can see which libraries GDB "knows" about via info shared command.
Just wait until you see your target library in that list, before attempting to set breakpoints in it.

like image 41
Employed Russian Avatar answered Oct 21 '22 05:10

Employed Russian