在涉及自己创建的函数指针,为了方便调试,希望将函数指针当前指向的函数名打印出来。直接打印不可能,所以需要在定义指针时添加上指示符。
例如stackoverflow上的一段代码样例
http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c// Define it like this typedef struct { char *dec_text; #ifdef _DEBUG_FUNC void (*action)(char); #endif } func_Struct; // Initialize it like this func_Struct func[3]= { #ifdef _DEBUG_FUNC {"my_Set(char input)",&my_Set}}; {"my_Get(char input)",&my_Get}}; {"my_Clr(char input)",&my_Clr}}; #else {&my_Set}}; {&my_Get}}; {&my_Clr}}; #endif // And finally you can use it like this func[0].action( 0x45 ); #ifdef _DEBUG_FUNC printf("%s",func.dec_text);
#endif
linux下可以通过backtrace、backtrace_symbols、backtrace_symbols_fd这样的函数,结合-rdynamic