Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the function name of a C function pointer

Tags:

c

linux

elf

dwarf

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.

like image 510
terminus Avatar asked Aug 27 '10 19:08

terminus


People also ask

Is the name of a function a pointer in C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

Is the name of a function a pointer?

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.

What is the syntax of function pointer?

Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.

Which operator is used to refer to FN using pointer?

The syntax looks like you are preceding the dereferenced pointer with an object member selection (the “dot” operator) or object pointer selection (the “arrow” operator). Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments);


1 Answers

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.

like image 140
RBerteig Avatar answered Oct 04 '22 23:10

RBerteig