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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:14:14  来源:igfitidea点击:

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

linuxgdbshared-libraries

提问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 gzopenand print dlopenwork (i.e. they find the respective symbols). But print myfoodoesn't work, where myfoois a function loaded by the process from an .sofile using dlopen. Here is what I get:

我想使用 GDB 调试在 Linux 2.6 上运行的进程。attach PID(其中,PID是进程ID), ,,和工作(即,他们发现各个符号)。但不起作用,进程使用. 这是我得到的:print mainprint sinprint gzopenprint dlopenprint myfoomyfoo.sodlopen

(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 myfoodoes 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 directoryerror message printed by the attach $PIDcommand of GDB. Apparently GDB was started in the wrong directory. Doing the proper cdbefore starting GDB fixed the problem, and print myfoostarted working.

原来是GDBmydir/mylib.so: No such file or directoryattach $PID命令打印出了错误信息。显然 GDB 是在错误的目录中启动的。cd在启动 GDB 之前做正确的事情解决了问题,并print myfoo开始工作。

I'd like to automate this: I want GDB figure out where my .sofiles (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_PATHand doing a set solib-search-path /tmp/parentdidn't work (ls -l /tmp/parent/mydir/myfoo.sodoes 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/mapscontains 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 .sofiles 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 上有效——我不知道它在其他操作系统上的表现如何。