简体   繁体   中英

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. 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. How can I do it?

Unfortunately, libunwind is only good at fetching addresses from the call stack. The library you are looking for is libbacktrace by Ian Lance Taylor .

It parses DWARF debugging information. 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 . I have no experience with this one, though, while I can certify that libbacktrace rocks!

For interactive viewing, you can use the addr2line command line utility. 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. From a cursory look at the libunwind API, it doesn't seem like it supports retrieving that kind of information. There's another library called libdwarf that supports general purpose DWARF parsing. It would be quite an involved project though, the way line numbers are stored in DWARF is not straightforward.

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. 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 . That one is more low level IIRC.

Keep in mind inlined functions. The same PC value may correspond to more than one source line.

To see the DWARF information in an executable interactively, you can use the DWARF Explorer GUI tool (full disclosure: I wrote the GUI). The line numbers will be under the top item of a source file, under stmt_list . The DWARF parser behind that tool is a Python one , not a C one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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