Skip to content
Advertisement

gdb: how to learn which shared library loaded a shared library in question

I need to get the list of shared libraries used by an app in runtime. Most of them can be listed by ldd, but some can be seen only with gdb -p <pid> and by running the gdb command info sharedlib.

It would really help, if I could learn in some way: for a chosen library (in the list, output by info sharedlib), which library (of the same list) had caused to load it. Is there any way to learn it in gdb or in some other way? Because sometimes, I see a loaded library in the list and cannot get why it is there and which (probably, previously loaded in memory) library loaded it.

UPDATE:

I attach a screen shot of gdb showing the info that I need. I used breakpoint on dlopen, as it was suggested in comments and in the answer. The command x/s $rdi prints out the first argument of dlopen, as by Linux System V ABI, i.e. it prints the name if the library, about which I want to learn who loaded it (libdebuginfod.so.1). I put it here just for those who are curious. In my case, it can be seen, that the libdebuginfod.so.1 was loaded by libdw.so.1 (as shown by bt command).

enter image description here

Advertisement

Answer

Is there any way to learn it in gdb or in some other way?

There are a few ways.

You can run the program with env LD_DEBUG=files /path/to/exe.

This will produce output similar to:

 LD_DEBUG=files /bin/date
     76042:
     76042:     file=libc.so.6 [0];  needed by /bin/date [0]

It is the needed by part that you most care about.

You could also use GDB and use set set stop-on-solib-events 1. This will produce output similar to:

Stopped due to shared library event:
  Inferior loaded /lib/x86_64-linux-gnu/libc.so.6

At that point, you could execute where command and observe which dlopen() call caused the new library to be loaded.

You could also set a breakpoint on dlopen() and do the same.

The stop-on-solib-events may be better if your executable repeatedly dlopen()s the same library — the library set will not change when you dlopen() the same library again, and you’ll stop when you don’t care to stop. Setting stop-on-solib-events avoids such unnecessary stops.

Advertisement