1. linux内核堆栈打印方法

在需要打印堆栈的函数内部加入函数dump_stack()或 __backtrace();

例:在下面gsmld_open(...)函数内部加入dump_stack():

static int gsmld_open(struct tty_struct *tty)
{
	struct gsm_mux *gsm;

	dump_stack();
	
	if (tty->ops->write == NULL)
		return -EINVAL;

	/* Attach our ldisc data */
	gsm = gsm_alloc_mux();
	if (gsm == NULL)
		return -ENOMEM;

	tty->disc_data = gsm;
	tty->receive_room = 65536;

	/* Attach the initial passive connection */
	gsm->encoding = 1;
	return gsmld_attach_gsm(tty, gsm);
}

堆栈信息:

Backtrace: 
[<c0012df8>] (dump_backtrace+0x0/0x10c) from [<c0012f1c>] (show_stack+0x18/0x1c)
 r7:c2c9b0c0 r6:c2ca8a00 r5:c2c9b060 r4:c2ca8a00
[<c0012f04>] (show_stack+0x0/0x1c) from [<c030df3c>] (dump_stack+0x20/0x2c)
[<c030df1c>] (dump_stack+0x0/0x2c) from [<bf00e420>] (gsmld_open+0x1c/0x130 [n_gsm])
[<bf00e404>] (gsmld_open+0x0/0x130 [n_gsm]) from [<c01d7228>] (tty_ldisc_open+0x54/0x94)
 r9:c2ca8a18 r8:c2c9b060 r7:c2c9b0c0 r6:c2ca8a00 r5:c2c9b060
r4:c2ca8a00
[<c01d71d4>] (tty_ldisc_open+0x0/0x94) from [<c01d7fc0>] (tty_set_ldisc+0x1b8/0x7d0)
 r5:00000000 r4:c2ca8af0
[<c01d7e08>] (tty_set_ldisc+0x0/0x7d0) from [<c01d1038>] (tty_ioctl+0x56c/0xcb0)
[<c01d0acc>] (tty_ioctl+0x0/0xcb0) from [<c00d631c>] (do_vfs_ioctl+0x88/0x638)
[<c00d6294>] (do_vfs_ioctl+0x0/0x638) from [<c00d6910>] (SyS_ioctl+0x44/0x68)
[<c00d68cc>] (SyS_ioctl+0x0/0x68) from [<c000f7c0>] (ret_fast_syscall+0x0/0x2c)
 r8:c000f948 r7:00000036 r6:00000000 r5:00001002 r4:b6e32510

2. linux应用层堆栈打印方法

在需要打印堆栈的函数内部加入函数backtrace(...)、backtrace_symbols(...):

 void *bt[20]; 
 char **strings; 
 size_t sz;
 sz = backtrace(bt, 20); 
 strings = backtrace_symbols(bt, sz); 
    for(i = 0; i < sz; ++i) 
        fprintf(stderr, "%s/n", strings[i]);


GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐