轉 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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!