开发者

How to get the function name of a C function pointer

开发者 https://www.devze.com 2023-01-13 23:55 出处:网络
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).

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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消