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

时间:2022-12-07 16:00:37

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).

我有以下问题:当我使用backtrace(3)函数在C中获得回溯时,返回的符号可以使用dwarf库和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).

问题是,如果我有一个简单的函数指针(例如通过传递它和函数),那么dladdr + dwarf函数就无济于事。似乎指针与backtrace(3)返回的指针不同(这并不奇怪,因为回溯直接从堆栈中获取这些函数指针)。

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

指针之间的区别是非常重要的:我得到的回溯是:0x8048ca4直接指针版本:0x3ba838

Seems to me that the second one needs an offset.

在我看来,第二个需要一个偏移量。

2 个解决方案

#1


1  

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.

根据您引用的典型地址的实质差异进行猜测,一个来自实际的共享库,另一个来自您的主要可执行文件。在dladdr(3)的手册页之间读取,可能是这样的情况:如果符号不在dlopen(3)加载的模块中,那么它可能无法重建匹配文件和符号名称。

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.

我假设您没有从这里关注的任何模块中删除符号,或者所有投注都已关闭。如果可执行文件包含符号,则应该可以在其中查找与任何可命名函数的地址完全匹配的符号。毕竟,指向函数的指针就是这样。

#2


0  

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

addr2line(1)可能就是你要找的东西。

#1


1  

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.

根据您引用的典型地址的实质差异进行猜测,一个来自实际的共享库,另一个来自您的主要可执行文件。在dladdr(3)的手册页之间读取,可能是这样的情况:如果符号不在dlopen(3)加载的模块中,那么它可能无法重建匹配文件和符号名称。

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.

我假设您没有从这里关注的任何模块中删除符号,或者所有投注都已关闭。如果可执行文件包含符号,则应该可以在其中查找与任何可命名函数的地址完全匹配的符号。毕竟,指向函数的指针就是这样。

#2


0  

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

addr2line(1)可能就是你要找的东西。