如何在 Linux 中使用 addr2line 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7648642/
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 use the addr2line command in Linux?
提问by Prak
I am trying to use addr2line command in Unix but everytime it is giving the same output as ??:0. I am giving command as addr2line -e a.out 0x4005BDC
. I got this address while running this a.out executable with valgrind
tool to find the memory leakage. I also compiled the source code with -g
option.
我试图在 Unix 中使用 addr2line 命令,但每次它都给出与 ??:0 相同的输出。我正在发出命令addr2line -e a.out 0x4005BDC
。我在使用valgrind
工具运行这个 a.out 可执行文件以查找内存泄漏时得到了这个地址。我还编译了带有-g
选项的源代码。
回答by Mat
That's exactly how you use it. There is a possibility that the address you have does not correspond to something directly in your source code though.
这正是您使用它的方式。但是,您拥有的地址可能与源代码中的某些内容不直接对应。
For example:
例如:
$ cat t.c
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
$ gcc -g t.c
$ addr2line -e a.out 0x400534
/tmp/t.c:3
$ addr2line -e a.out 0x400550
??:0
0x400534
is the address of main
in my case. 0x400408
is also a valid function address in a.out
, but it's a piece of code generated/imported by GCC, that has no debug info. (In this case, __libc_csu_init
. You can see the layout of your executable with readelf -a your_exe
.)
0x400534
main
在我的情况下是地址。0x400408
也是 中的有效函数地址a.out
,但它是由 GCC 生成/导入的一段代码,没有调试信息。(在这种情况下,__libc_csu_init
。您可以使用readelf -a your_exe
.查看可执行文件的布局。)
Other times when addr2line
will fail is if you're including a library that has no debug information.
其他时候addr2line
会失败是如果你包含一个没有调试信息的库。
回答by ks1322
You can also use gdb instead of addr2line to examine memory address. Load executable file in gdb and print the name of a symbol which is stored at the address. 16 Examining the Symbol Table.
您还可以使用 gdb 代替 addr2line 来检查内存地址。在 gdb 中加载可执行文件并打印存储在该地址的符号的名称。16 检查符号表。
(gdb) info symbol 0x4005BDC
回答by tdenniston
You need to specify an offsetto addr2line, not a virtual address (VA). Presumably if you had address space randomization turned off, you could use a full VA, but in most modern OSes, address spaces are randomized for a new process.
您需要指定addr2line的偏移量,而不是虚拟地址 (VA)。据推测,如果您关闭了地址空间随机化,则可以使用完整的 VA,但在大多数现代操作系统中,地址空间是为新进程随机化的。
Given the VA 0x4005BDC
by valgrind, find the base address of your process or library in memory. Do this by examining the /proc/<PID>/maps
file while your program is running. The line of interest is the text
segment of your process, which is identifiable by the permissions r-xp
and the name of your program or library.
给定0x4005BDC
valgrind的 VA ,在内存中找到进程或库的基地址。通过/proc/<PID>/maps
在程序运行时检查文件来完成此操作。感兴趣的线是text
您的进程段,它可以通过权限r-xp
和程序或库的名称来识别。
Let's say that the base VA is 0x0x4005000
. Then you would find the difference between the valgrind supplied VA and the base VA: 0xbdc
. Then, supply that to add2line:
假设基础 VA 是0x0x4005000
。然后,你会发现Valgrind的供应VA和基础VA之间的区别:0xbdc
。然后,将其提供给 add2line:
addr2line -e a.out -j .text 0xbdc
And see if that gets you your line number.
看看这是否能让你得到你的行号。
回答by Penghe Geng
Try adding the -f
option to show the function names :
尝试添加-f
选项以显示函数名称:
addr2line -f -e a.out 0x4005BDC