how to set breakpoint on function in a shared library which has not been loaded in gdb

44,382

Solution 1

Actually gdb should tell you that it's able to resolve the symbol in the future, when new libraries are loaded:

(gdb) b test
Function "test" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (test) pending.
(gdb) r

And later on once the .so object is loaded, it will resolve the breakpoint, e.g.:

Reading symbols for shared libraries . done
Breakpoint 1 at 0xcafebebe
Pending breakpoint 1 - "test" resolved

Solution 2

Actually, this method won't always work.

Suppose I have several shared libraries which each have a function named "Init". If I have loaded a different library then "b Init" is going to set the breakpoint to the wrong instance of the function "Init". So I have to specify the breakpoint like this:

(gdb) b object5.c:66

No source file named object5.c.

Solution 3

Another way is to specify the filename and der function, e.g.:

b object5.c:test

This should be unique. Maybe you want also to specify the path to the source code (as already suggested) by:

set directories path_of_object5.c

Solution 4

how to set a breakpoint on a shared lib.

It's quite common to have a breakpoint inside a shared library. Shared libraries can be loaded and unloaded explicitly, and possibly repeatedly, as the program is executed. To support this use case, gdb updates breakpoint locations whenever any shared library is loaded or unloaded. Typically, you would set a breakpoint in a shared library at the beginning of your debugging session, when the library is not loaded, and when the symbols from the library are not available. When you try to set breakpoint, gdb will ask you if you want to set a so called pending breakpoint—breakpoint whose address is not yet resolved.

quote from https://sourceware.org/gdb/onlinedocs/gdb/Set-Breaks.html

(gdb) b object5.c:66 No source file named object5.c.

maybe you can use "set directory the_location_of_object5.c_file" to fix it.

Share:
44,382
pierrotlefou
Author by

pierrotlefou

Software Developer

Updated on January 25, 2020

Comments

  • pierrotlefou
    pierrotlefou over 4 years

    I have a shared library libtest.so which will be loaded into the the main program using dlopen. Function test() reside in libtest.so and will be called in the main program through dlsym. Is there any way I could set up a break point on test?

    Please note that the main programm has not been linked to libtest.so during linking time. Otherwise , I should be able to set the break point although it is a pending action. In my case, when I do b test, gdb will tell me Function "test" not defined.

  • cardiff space man
    cardiff space man almost 11 years
    On the GDB I'm using, even if I "set breakpoint pending on" it will NOT ask me if I want the pending behavior if the symbol is undefined. It just tells me "Can't find member bla bla Hint try tab bla bla"
  • Fernando Gonzalez Sanchez
    Fernando Gonzalez Sanchez over 9 years
    In my case the problem I found I have to compile with debug symbols (to put breakpoints in a custom .so).