How to make gdb print symbols in shared libraries loaded with dlopen?

gdb, linux, shared-libraries

Solution

It looks like there is no easy way to automate finding finding `.so` files in GDB.

Problem

I want to debug a process running on Linux 2.6 using GDB. `attach PID` (where PID is the process ID), `print main`, `print sin`, `print gzopen` and `print dlopen` work (i.e. they find the respective symbols). But `print myfoo` doesn't work, where `myfoo` is a function loaded by the process from an `.so` file using `dlopen`. Here is what I get: ``` (gdb) print main $3 = {int (int, char **)} 0x805ba90 <main> (gdb) print sin $4 = {<text variable, no debug info>} 0xb7701230 <sin> (gdb) print gzopen $5 = {<text variable, no debug info>} 0xb720df50 <gzopen> (gdb) print dlopen $6 = {<text variable, no debug info>} 0xb77248e0 <__dlopen_nocheck> (gdb) print myfoo No symbol "myfoo" in current context. ``` How do I get GDB to find `myfoo`? The function `myfoo` does indeed exist, because in the program I managed to get its address using `dlsym` (after `dlopen`), and I managed to call it. Only after that I attached GDB to the process. It turned out that there was a `mydir/mylib.so: No such file or directory` error message printed by the `attach $PID` command of GDB. Apparently GDB was started in the wrong directory. Doing the proper `cd` before starting GDB fixed the problem, and `print myfoo` started working. I'd like to automate this: I want GDB figure out where my `.so` files (loaded with `dlopen`) are. An approximation I can think of is examining `/proc/$PID/maps` (on Linux), finding possible directories, and adding all of them to the GDB library search path before starting GDB. Extending `LD_LIBRARY_PATH` and doing a `set solib-search-path /tmp/parent` didn't work (`ls -l /tmp/parent/mydir/myfoo.so` does work), GDB still reported the `No such file or directory`. How do I tell GDB where to look for `mydir/myfoo.so`? My other question is how do I get the list of possible directories? On Linux, `/proc/$PID/maps` contains them -- but what about other operating systems like FreeBSD and the Mac OS X?

Original source