I want to write c program (like backtrace) I am getting the address of functions but I don't know how to convert those address to symbols (function name ). Please help me
-
4The debug version of the executable has the symbol names in it. So you need to get the documentation that describes the format of the executable file. – user3386109 Apr 18 '16 at 05:29
-
1I tried this once. I ended up logging the adresses on stack and manually searching for them in assembler output. Very slow procedure. I did find the bug though. – Andreas Apr 18 '16 at 05:45
-
Checkout addr2line, it does the similar thing as you want. – fluter Apr 18 '16 at 06:45
-
1Possible duplicate of [print stack trace in arm-linux](http://stackoverflow.com/questions/10877377/print-stack-trace-in-arm-linux) – artless noise Apr 18 '16 at 15:23
-
An answer will depend on your tool suite, libc and possibly kernel. It is possible if you are willing to change things. Given some arbitrarily old tools, libc and kernel it maybe too much effort for some people. – artless noise Apr 18 '16 at 16:24
1 Answers
The first answer is that the symbol handling is an internal ABI hidden away. Some OS even perform this magic in kernel space. But you obviously want ARM + linux.
First information needed is to map addresses back to their origin. This mapping you can retrieve from here: /proc/self/stat
Next part is more tricky, reversing those offsets from those files into symbols. For that you will need to parse the ELF files. If you do not want to parse the binary data, you can cheat and use objdump and parse the ASCII formatted data instead
http://man7.org/linux/man-pages/man5/elf.5.html
objdump -t -T -r -R /lib/x86_64-linux-gnu/libnss_files-2.19.so
If you want even more details information than this, you will need to parse the section that contains the debug information if present - and it might be moved to a separate file to allow apt to have those nice -dbg packages, but that is probably way to much work, and easier to hack gdb instead or extract code from projects like valgrind.
PS: If your use-case is to perform debug/diagnostics when things go wrong, I will recommend to use valgrind
- 2,277
- 1
- 9
- 19
-
If you are lucky, your libc has backtrace and backtrace_symbols calls. Check your manpages – Stian Skjelstad Apr 18 '16 at 07:08