Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a pointer to the current function in C (gcc)?

is there a magic variable in gcc holding a pointer to the current function ?

I would like to have a kind of table containing for each function pointer a set of information.

I know there's a __func__ variable containing the name of the current function as a string but not as a function pointer.

This is not to call the function then but just to be used as an index.

EDIT Basically what i would like to do is being able to run nested functions just before the execution of the current function (and also capturing the return to perform some things.) Basically, this is like __cyg_profile_func_enter and __cyg_profile_func_exit (the instrumentation functions)... But the problem is that these instrumentation functions are global and not function-dedicated.

EDIT In the linux kernel, you can use unsigned long kallsyms_lookup_name(const char *name) from include/linux/kallsyms.h ... Note that the CONFIG_KALLSYMS option must be activated.

like image 534
LB40 Avatar asked Jan 28 '10 13:01

LB40


People also ask

How do you use a pointer to a function in C?

You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions. For z/OS® XL C/C++, use the __cdecl keyword to declare a pointer to a function as a C linkage.

Can a pointer be passed to a function?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

How do you assign a pointer to a function?

int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);

What is __ Pretty_function __?

The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...


1 Answers

void f() {
   void (*fpointer)() = &f;
}
like image 184
David Rodríguez - dribeas Avatar answered Oct 12 '22 05:10

David Rodríguez - dribeas