版权声明:本文为CSDN博主「yanlaifan」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:dump_stack()使用方法

对于大型驱动,想要知道某个回调函数由谁调用,非常困难。到底有没有办法知道呢?回答是肯定的,通过内核提供的接口dump_stack()可以满足要求。当内核发生panic时候,也会主动调用该接口,我们也可以在调试过程中主动调用该接口来进行测试。

Demo如下:

1
2
3
4
5
6
#if defined(CONFIG_FB)
data->fb_notif.notifier_call = fb_notifier_callback;//回调函数在啥时候调用呢?
err = fb_register_client(&data->fb_notif);
if (err)
dev_err(&client->dev, "Unable to register fb_notifier: %d\n",err);
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#if defined(CONFIG_FB)
static int fb_notifier_callback(struct notifier_block *self,
unsigned long event, void *data)
{
struct fb_event *evdata = data;
int *blank;
struct ft5x06_ts_data *ft5x06_data =
container_of(self, struct ft5x06_ts_data, fb_notif);

printk(KERN_ALERT"--------------[fyl] dump_stack start----------------");
dump_stack();
printk(KERN_ALERT"--------------[fyl] dump_stack end----------------");

...

return 0;
}
#endif

测试抓取log:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<1>[  950.351269] --------------[fyl] dump_stack start----------------
<6>[ 950.351270] CPU: 2 PID: 295 Comm: surfaceflinger Tainted: G W O 3.10.49-gc92b51d-dirty #4
<6>[ 950.351288] [<c010be4c>] (unwind_backtrace+0x0/0x11c) from [<c0109a08>] (show_stack+0x10/0x14)
<6>[ 950.351300] [<c0109a08>] (show_stack+0x10/0x14) from [<c05ca74c>] (fb_notifier_callback+0x20/0x9c)//定位到该函数
<6>[ 950.351310] [<c05ca74c>] (fb_notifier_callback+0x20/0x9c) from [<c0a7b098>] (notifier_call_chain+0x40/0x68) //notifier_call_chain调用了fb_notifier_callback
<6>[ 950.351321] [<c0a7b098>] (notifier_call_chain+0x40/0x68) from [<c01459e8>] (__blocking_notifier_call_chain+0x40/0x58)
<6>[ 950.351333] [<c01459e8>] (__blocking_notifier_call_chain+0x40/0x58) from [<c0145a14>] (blocking_notifier_call_chain+0x14/0x1c)
<6>[ 950.351344] [<c0145a14>] (blocking_notifier_call_chain+0x14/0x1c) from [<c0374ba0>] (fb_blank+0x34/0x88)
<6>[ 950.351353] [<c0374ba0>] (fb_blank+0x34/0x88) from [<c03755cc>] (do_fb_ioctl+0x51c/0x598)
<6>[ 950.351363] [<c03755cc>] (do_fb_ioctl+0x51c/0x598) from [<c021039c>] (vfs_ioctl+0x28/0x3c)
<6>[ 950.351372] [<c021039c>] (vfs_ioctl+0x28/0x3c) from [<c0210e44>] (do_vfs_ioctl+0x4d8/0x588)
<6>[ 950.351380] [<c0210e44>] (do_vfs_ioctl+0x4d8/0x588) from [<c0210f40>] (SyS_ioctl+0x4c/0x78)
<6>[ 950.351389] [<c0210f40>] (SyS_ioctl+0x4c/0x78) from [<c0105f60>] (ret_fast_syscall+0x0/0x30)
<1>[ 950.642678] --------------[fyl] dump_stack end----------------

从log中可以看出,是notifier_call_chain调用了fb_notifier_callback。整个函数调用栈可以从log中获取。