开发者

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

开发者 https://www.devze.com 2023-01-30 03:35 出处:网络
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). B

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.开发者_如何学JAVA

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?


"info target" command in gdb will show a list of all sections in all loaded shared objects (including dlopen()ed libraries). At least this works on Linux -- I don't know how it behaves on other operating systems.


I maintain a program that loads a shared library via dlopen() and have successfully accessed symbols in the shared library using GDB. This will only work, however, if the shared library has a symbol table.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消