2

I have an executable that dynamically loads a shared object library, let say foo.so. I do not build foo.so and it is given to me as a library to use in my code. foo.cpp compiles to give foo.so. I do not have access to foo.cpp but have foo.so. I want to set a breakpoint when my executable calls a function inside this shared library foo.so (that is when function inside foo.cpp is called). Is their a way in gdb to do so? I have tried

gdb funcname 

but it does not seem to work. Any pointers how to break gdb at that point?

Deepti Jain
  • 1,901
  • 4
  • 21
  • 29
  • Could you be more specific on the operating system and version of gdb you are using? – razeh Aug 14 '12 at 02:00
  • I am using CentOS 5.5 and gdb 7.2. – Deepti Jain Aug 14 '12 at 02:08
  • @DeeptiJain Was the shared object library compiled with the appropriate debug flags? –  Aug 14 '12 at 04:32
  • @Michael GDB does *not* need the library to be compiled with debug flags to set breakpoints in it. – Employed Russian Aug 14 '12 at 05:52
  • @EmployedRussian It's my [understanding](http://stackoverflow.com/a/322015/849425) that without the symbol table (no debug flags) that you can only do ASM level debugging. –  Aug 15 '12 at 01:26
  • @Michael Your understanding is *incorrect* for all UNIX machines I know of. Debug flags *do not affect* symbol table (except on ancient UNIX systems that still use `COFF`) to begin with. And when you are doing ASM level debugging, it still helps a lot to know what function you are in, and *that* does not require debug info; just the symbol table. – Employed Russian Aug 15 '12 at 03:37

1 Answers1

3

Is their a way in gdb to do so?

Yes.

I have tried gdb funcname

That wouldn't work. You need to run gdb exename, then break funcname at the (gdb) prompt.

At that point, GDB will likely inform you that funcname does not yet exist (since you haven't yet dynamically loaded foo.so), and will ask you whether you want to create a deferred breakpoint. You should answer yes, and GDB will retry setting this breakpoint every time a new shared library is loaded. Eventually this will succeed, and you should get the breakpoint set (automatically and silently), and when you call the funcname later, that breakpoint will fire and GDB will stop (which is exactly what you want).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362