如何获取C函数指针的函数名
我遇到以下问题:当我使用 backtrace(3) 函数在 C 中获得回溯时,返回的符号可以使用 dwarf 库和 dladdr(3) 轻松确定函数名称。
问题是,如果我有一个简单的函数指针(例如通过传递它 &function),则 dladdr + dwarf 函数无济于事。看起来该指针与 backtrace(3) 返回的指针不同(这并不奇怪,因为 backtrace 直接从堆栈获取这些函数指针)。
我的问题是是否也有某种方法可以解析这些名称?另外,我想确切地知道这两个指针之间有什么区别。
谢谢!
更新:
指针之间的差异非常显着:
我通过回溯得到的是:0x8048ca4
直接指针版本:0x3ba838
在我看来,第二个需要一个偏移量。
I have the following problem: when I get a backtrace in C using the backtrace(3) function the symbols returned the name of the functions is easily determinable with the dwarf library and dladdr(3).
The problem is that if I have a simple function pointer (e.g. by passing it &function) the dladdr + dwarf functions can't help. It seems that the pointer is different from the one returned by backtrace(3) (it's not strange, since backtrace gets these function pointers straight from the stack).
My question is whether there is some method for resolving these names too? Also, I'd like to know exactly what is the difference between the two pointers.
Thanks!
UPDATE:
The difference between the pointers is quite significant:
The one I get with backtrace is: 0x8048ca4
The direct pointer version: 0x3ba838
Seems to me that the second one needs an offset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您引用的典型地址的实质性差异进行猜测,一个来自实际的共享库,另一个来自您的主可执行文件。阅读
dladdr(3)
手册页的字里行间,可能的情况是,如果符号未位于由 dlopen(3) 加载的模块中,则可能无法重建匹配的文件和符号名称。我假设您还没有从您关心的任何模块中删除符号,或者所有的赌注都失败了。如果可执行文件具有符号,那么应该可以在其中查找与任何可命名函数的地址完全匹配的符号。毕竟,指向函数的指针就是这样。
Making a guess from the substantial differences in the typical addresses you cited, one is from an actual shared library, and the other from your main executable. Reading between the lines of a man page for
dladdr(3)
, it could be the case that if the symbol isn't located in a module that was loaded bydlopen(3)
then it might not be able to reconstruct the matching file and symbol names.I assume you haven't stripped the symbols off any module you care about here, or all bets are off. If the executable has symbols, then it should be possible to look in them for one that is an exact match for the address of any nameable function. A pointer to a function is just that, after all.
addr2line(1) 可能正是您正在寻找的东西。
addr2line(1) may be just the thing you're looking for.