打印函数指针的名字


转 https://blog.csdn.net/iefswang/article/details/25225973 

在内核调试时,我们需要跟踪函数调用过程,在这时,我们经常会碰到函数指针的情况,那么,我们怎么跟踪到具体的函数处呢?如何打印出函数指针的函数名?

     这里有两种方法。先说比较笨重的方法:

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);

 

详细了解printk后,发现还有更简便的方法。

%p:打印裸指针(raw pointer)

%pF可打印函数指针的函数名和偏移地址

%pf只打印函数指针的函数名,不打印偏移地址。


printk("%pf",func[0]->action); 结果:
my_Set

%pM打印冒号分隔的MAC地址

%pm打印MAC地址的16进制无分隔



printk("%pM %pm\n", mac, mac) willprint:

2c:00:1d:00:1b:00 2c001d001b00

%I4打印无前导0的IPv4地址,%i4打印冒号分隔的IPv4地址
%I6打印无前导0的IPv6地址,%i6打印冒号分隔的IPv6地址

printk("%pI4 %pi4\n", ip, ip) will print:
127.0.0.1 127:0:0:1

其它的特殊格式字符参见
http://lxr.linux.no/#linux+v2.6.34/lib/vsprintf.c#L930

---------------------
作者:iefswang
来源:CSDN
原文:https://blog.csdn.net/iefswang/article/details/25225973
版权声明:本文为博主原创文章,转载请附上博文链接!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM