如何从函数地址在Linux中使用链接寄存器(如backtrace_symbol)检索函数名 [英] How to retrieve function name from function address using link register (like backtrace_symbol) in linux
问题描述
我想写C程序(如回溯)我得到函数的地址,但我不知道如何将这些地址转换为符号(函数名)。请帮我
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
推荐答案
第一个答案是,符号的处理是一个内部ABI隐藏起来。有的甚至OS执行这个魔术在内核空间。但是,你显然希望ARM + Linux操作系统。
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.
首先需要的信息是映射地址回原籍。这个映射你可以从这里获取:的/ proc /自/ STAT
First information needed is to map addresses back to their origin. This mapping you can retrieve from here: /proc/self/stat
接下来的部分是更靠谱,扭转了这些文件这些偏移成符号。对于您将需要解析ELF文件。如果你不想分析二进制数据,你可以欺骗和objdump的使用和解析ASCII格式的数据,而不是
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
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
如果你想比这个更详细的信息,你需要分析包含如果present调试信息的部分 - 它可能会被移动到一个单独的文件,以允许容易有那些好的-dbg包,但是这可能是办法不多的工作,也更容易破解,而不是GDB或类似的valgrind。项目提取code
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:如果你的用例是进行调试/诊断出问题时,我会建议使用的valgrind
PS: If your use-case is to perform debug/diagnostics when things go wrong, I will recommend to use valgrind
这篇关于如何从函数地址在Linux中使用链接寄存器(如backtrace_symbol)检索函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!