Linux 如何在使用 dlopen 加载的共享库中制作 gdb 打印符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4392550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make gdb print symbols in shared libraries loaded with dlopen?
提问by pts
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 调试在 Linux 2.6 上运行的进程。attach PID
(其中,PID是进程ID), ,,和工作(即,他们发现各个符号)。但不起作用,进程使用. 这是我得到的:print main
print sin
print gzopen
print dlopen
print myfoo
myfoo
.so
dlopen
(gdb) print main
= {int (int, char **)} 0x805ba90 <main>
(gdb) print sin
= {<text variable, no debug info>} 0xb7701230 <sin>
(gdb) print gzopen
= {<text variable, no debug info>} 0xb720df50 <gzopen>
(gdb) print dlopen
= {<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
?
我如何让 GDB 找到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.
该函数myfoo
确实存在,因为在程序中我设法使用dlsym
(after dlopen
)获取了它的地址,并且我设法调用了它。在那之后,我才将 GDB 附加到该过程中。
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.
原来是GDBmydir/mylib.so: No such file or directory
的attach $PID
命令打印出了错误信息。显然 GDB 是在错误的目录中启动的。cd
在启动 GDB 之前做正确的事情解决了问题,并print myfoo
开始工作。
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
?
我想自动执行此操作:我希望 GDB 找出我的.so
文件(加载了dlopen
)的位置。我能想到的一个近似方法是检查/proc/$PID/maps
(在 Linux 上),找到可能的目录,然后在启动 GDB 之前将它们全部添加到 GDB 库搜索路径中。扩展LD_LIBRARY_PATH
和做一个set solib-search-path /tmp/parent
没有用(ls -l /tmp/parent/mydir/myfoo.so
确实有效),GDB 仍然报告No such file or directory
. 我如何告诉 GDB 在哪里寻找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?
我的另一个问题是如何获取可能的目录列表?在 Linux 上,/proc/$PID/maps
包含它们——但其他操作系统如 FreeBSD 和 Mac OS X 呢?
采纳答案by pts
It looks like there is no easy way to automate finding finding .so
files in GDB.
似乎没有简单的方法可以.so
在 GDB 中自动查找文件。
回答by Jon Trauntvein
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.
我维护了一个通过 dlopen() 加载共享库的程序,并使用 GDB 成功访问了共享库中的符号。但是,这仅在共享库具有符号表时才有效。
回答by Martin ünsal
"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.
gdb 中的“info target”命令将显示所有加载的共享对象(包括 dlopen()ed 库)中所有部分的列表。至少这在 Linux 上有效——我不知道它在其他操作系统上的表现如何。