如何在 linux 中使用链接寄存器(如 backtrace_symbol)从函数地址中检索函数名 [英] How to retrieve function name from function address using link register (like backtrace_symbol) in linux

查看:22
本文介绍了如何在 linux 中使用链接寄存器(如 backtrace_symbol)从函数地址中检索函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写 c 程序(如 backtrace) 我正在获取函数的地址,但我不知道如何将这些地址转换为符号(函数名).请帮帮我

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.一些操作系统甚至在内核空间中执行这种魔法.但是你显然想要 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/self/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

如果您想要比这更多的详细信息,您将需要解析包含调试信息的部分(如果存在) - 并且可能会将其移动到单独的文件以允许 apt 拥有那些不错的 -dbg 包,但是可能是很多工作的方式,并且更容易破解 gdb 或从 valgrind 等项目中提取代码.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆