简体   繁体   English

addr2line如何找到源文件和代码行?

[英]How addr2line can locate the source file and the line of code?

addr2line translates addresses into file names and line numbers. addr2line将地址转换为文件名和行号。 I am still beginner in debugging, and have some questions about addr2line. 我仍然是调试的初学者,并对addr2line有一些疑问。

  1. If am debugging a certain .so (binary) file, how the tool can locate its source code file (from where can get it!), what if the source doesn't exist? 如果要调试某个.so(二进制)文件,该工具如何定位其源代码文件(从哪里可以找到它!),如果源代码不存在怎么办?

  2. What is the relation between the address in a binary and the line number in its source, so addr2line can do this kind of mapping? 二进制文件中的地址与其源文件中的行号之间是什么关系,因此addr2line可以进行这种映射?

In general, addr2line works best on ELF executables or shared libraries with debug information. 通常, addr2line最适用于带有调试信息的ELF可执行文件或共享库。 That debug information is emitted by the compiler when you pass -g (or -g2 , etc...) to GCC . 当您-g (或-g2等)传递给GCC时,编译器会发出调试信息。 It notably provides a mapping between source code location (name of source file, line number, column number) and functions, variable names, call stack frame organization, etc etc... The debug information is today in DWARF format (and is also processed by the gdb debugger , the libbacktrace library , etc etc...). 尤其是,它提供了源代码位置(源文件的名称,行号,列号)与函数,变量名, 调用堆栈框架组织等之间的映射。当今,调试信息采用DWARF格式(并且也进行了处理)通过gdb调试器libbacktrace等)。 Notice that the debug information contains source file paths (not the source file itself). 请注意,调试信息包含源文件路径(而不是源文件本身)。

In practice, you can (and often should) pass the -g (or -g2 ) debugging option to GCC even with optimization flags like -O2 . 实际上,即使具有-O2类的优化标志,您也可以(通常应该)将-g (或-g2 )调试选项传递给GCC In that case, the debug information is slightly less precise but still practically usable. 在这种情况下,调试信息的精度会稍差一些,但仍可实际使用。 In some cases, stack frames may disappear (inlined function calls, tail call optimizations, ....). 在某些情况下,堆栈框架可能会消失(内联函数调用,尾部调用优化等)。

You could use the strip(1) utility to remove debug information (and other symbol tables, etc) from some ELF executable . 您可以使用strip(1)实用程序从某些ELF 可执行文件中删除调试信息(以及其他符号表等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM