简体   繁体   English

远程进程的源代码名称和行号

[英]source code name and line number of the remote process

Hi I am trying to create some sort of a debugger that looks at system calls to detect them, but from an usability standpoint I wish to be able to get the source code name and line number where the error is detected.您好我正在尝试创建某种调试器来查看系统调用以检测它们,但从可用性的角度来看,我希望能够获取检测到错误的源代码名称和行号。 I have been using library called libunwind, but it gives me function name, offset from the function, and program counter in the address space of the executable.我一直在使用名为 libunwind 的库,但它给了我 function 名称,与 function 的偏移量,以及可执行文件地址空间中的程序计数器。 However, if you look at valgrind or gdb, it gives you the line number and the source code name when it was compiled with -g flag.但是,如果您查看 valgrind 或 gdb,它会在使用 -g 标志编译时为您提供行号和源代码名称。 How can I do it?我该怎么做?

Unfortunately, libunwind is only good at fetching addresses from the call stack.不幸的是,libunwind 只擅长从调用堆栈中获取地址。 The library you are looking for is libbacktrace by Ian Lance Taylor .您要查找的库是Ian Lance Taylor 的 libbacktrace

It parses DWARF debugging information.它解析 DWARF 调试信息。 As a result, it is able to generate a backtrace with the names of inlined functions, source file names and line numbers.因此,它能够生成带有内联函数名称、源文件名和行号的回溯。

Edit: another option is backward-cpp .编辑:另一个选项是back-cpp I have no experience with this one, though, while I can certify that libbacktrace rocks!不过,我没有这方面的经验,但我可以证明 libbacktrace 非常棒!

For interactive viewing, you can use the addr2line command line utility.对于交互式查看,您可以使用addr2line命令行实用程序。 It takes the executable name and the address (the program counter), and outputs the source file and line.它采用可执行名称和地址(程序计数器),并输出源文件和行。 If you create a programmatic parser, you can use that to check yourself.如果您创建了一个程序化解析器,您可以使用它来检查自己。

For resolving the address to a line from a program , you'd have to parse the DWARF information in the executable, specifically the line number section.为了将地址解析为程序中的一行,您必须解析可执行文件中的 DWARF 信息,特别是行号部分。 From a cursory look at the libunwind API, it doesn't seem like it supports retrieving that kind of information.粗略地看一下libunwind API,它似乎不支持检索那种信息。 There's another library called libdwarf that supports general purpose DWARF parsing.还有一个名为libdwarf的库,它支持通用 DWARF 解析。 It would be quite an involved project though, the way line numbers are stored in DWARF is not straightforward.不过,这将是一个相当复杂的项目,行号存储在 DWARF 中的方式并不简单。

If you don't want to go all the way with libdwarf , you can use the readelf --debug-dump=decodedline command line to translate the line number section into a (relatively) easy to parse text file, and parse/interpret that from your C program.如果您不想 go 一直使用libdwarf ,您可以使用readelf --debug-dump=decodedline命令行将行号部分转换为(相对)易于解析的文本文件,并解析/解释从您的 C 程序。 It will give you the correspondence between program counter values (ranges) and source line numbers.它将为您提供程序计数器值(范围)和源代码行号之间的对应关系。

Another way to dump the line info from the command line is dwarfdump -l .从命令行转储行信息的另一种方法是dwarfdump -l That one is more low level IIRC.那是更底层的IIRC。

Keep in mind inlined functions.请记住内联函数。 The same PC value may correspond to more than one source line.相同的 PC 值可能对应多个源行。

To see the DWARF information in an executable interactively, you can use the DWARF Explorer GUI tool (full disclosure: I wrote the GUI).要以交互方式查看可执行文件中的 DWARF 信息,您可以使用DWARF Explorer GUI 工具(完全公开:我编写了 GUI)。 The line numbers will be under the top item of a source file, under stmt_list .行号将位于源文件的顶部项下,在stmt_list下。 The DWARF parser behind that tool is a Python one , not a C one.该工具背后的 DWARF 解析器是 Python 一个,而不是 C 一个。

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

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