如何获取C函数指针的函数名

发布于 2024-09-16 10:57:46 字数 406 浏览 16 评论 0原文

我遇到以下问题:当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-09-23 10:57:46

根据您引用的典型地址的实质性差异进行猜测,一个来自实际的共享库,另一个来自您的主可执行文件。阅读 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 by dlopen(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.

往事随风而去 2024-09-23 10:57:46

addr2line(1) 可能正是您正在寻找的东西。

addr2line(1) may be just the thing you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文