Determines which function the pointer points to C?

I have a pointer to a function, I suppose any signature. And I have 5 different functions with the same signature.

At run time, one of them is assigned to a pointer, and this function is called.

Without inserting any print statement into these functions, how can I find out the name of the function that the pointer points to?

+53
c function-pointers
Oct 19 '15 at 13:12
source share
9 answers

You will need to check which of the 5 functions your pointer indicates:

if (func_ptr == my_function1) { puts("func_ptr points to my_function1"); } else if (func_ptr == my_function2) { puts("func_ptr points to my_function2"); } else if (func_ptr == my_function3) { puts("func_ptr points to my_function3"); } ... 

If this is the general template you need, use the structure table instead of the function pointer:

 typedef void (*my_func)(int); struct Function { my_func func; const char *func_name; }; #define FUNC_ENTRY(function) {function, #function} const Function func_table[] = { FUNC_ENTRY(function1), FUNC_ENTRY(function2), FUNC_ENTRY(function3), FUNC_ENTRY(function4), FUNC_ENTRY(function5) } struct Function *func = &func_table[3]; //instead of func_ptr = function4; printf("Calling function %s\n", func->func_name); func ->func(44); //instead of func_ptr(44); 
+65
Oct 19 '15 at 13:21
source share

Typically, in C, such things are not available to the programmer.

There may be systemic ways of accessing there using debugging symbols, etc., but you probably do not want to depend on their availability for the program to function properly.

But you can, of course, compare the value of the pointer with another value, for example.

 if (ptr_to_function == some_function) printf("Function pointer now points to some_function!\n"); 
+27
Oct 19 '15 at 13:17
source share

Function names will not be available at run time.

C is not a reflective language.

Either save the function pointer table using the name, or provide a call mode for each function that returns the name.

+16
Oct 19 '15 at 13:17
source share

The debugger can tell you that (for example, the name of the function, given its address).

The ELF executable symbol table can also help. See nm (1) , objdump (1) , readelf (1)

Another Linux GNU / libc approach might be to use the dladdr (3) function at run time. Assuming that your program is beautifully and dynamically connected (for example, with -rdynamic ), it can find the name of the symbol and the path to the general object, taking into account some address (a function named globally).

Of course, if you have only five functions of this signature, you can compare your address (with five addresses from them).

Note that some functions do not have ((globally visible) names, such as static functions.

And some functions can be dlopen -ed and dlsym -ed (for example, inside plugins). Or their code is synthesized at runtime by some JIT-ing structure ( libjit , gccjit , LLVM , asmjit ). And the optimizing compiler can (and does!) Inline functions, clone them, tail-call them, etc., so your question may not make any sense at all ...

See also backtrace (3) and Ian Taylor libbacktrace inside GCC.

But in general, your quest is impossible. If you really need such reflective information in a reliable way, manage it yourself (look at the Pitrat CAIA system as an example, or somehow my MELT ), possibly generating some code at build time.

+15
Oct 19 '15 at 13:41
source share

To find out where the function pointer points, you will need to track your program. The most common is to declare an array of function pointers and use the int variable as the index of this array.

Currently, you can also say at runtime the function that is currently being executed using the __func__ identifier:

 #include <stdio.h> typedef const char* func_t (void); const char* foo (void) { // do foo stuff return __func__; } const char* bar (void) { // do bar stuff return __func__; } int main (void) { func_t* fptr; fptr = foo; printf("%s executed\n", fptr()); fptr = bar; printf("%s executed\n", fptr()); return 0; } 

Output:

 foo executed bar executed 
+13
Oct 19 '15 at 13:22
source share

Not at all - the symbolic name of the function disappears after compilation. Unlike a reflexive language, C does not know how its syntax elements were called by the programmer; especially, there is no "function search" by name after compilation.

Of course, you may have a "database" (for example, an array) of function pointers with which you can compare your current pointer.

+10
Oct 19 '15 at 13:16
source share

This is completely terrible and not tolerable, but subject to:

  • You are on Linux or some similar ELF-based system.
  • You are using dynamic linking.
  • The function is in the shared library or you used -rdynamic when linking.
  • There are probably many other assumptions that you should not make ...

You can get the function name by passing its address to the non-standard function dladdr .

+8
Oct. 20 '15 at 2:59
source share
  • set your linker to output the MAP file.
  • pause program
  • check the address in the index.
  • Find the address in the MAP file to find out which function is pointing to.
+3
Oct 19 '15 at 21:22
source share

A pointer to a function C is an address like any pointer. You can get the value from the debugger. You can overlay any integer type with enough bits to express it completely, and print it. Any compilation unit that can use a pointer, i.e. It has a function name in scope, can print pointer values ​​or compare them with a run-time variable without affecting anything inside the functions themselves.

0
Oct 21 '15 at 20:21
source share



All Articles