首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

学会用core dump调试程序异常

2012-07-22 
学会用core dump调试程序错误转载自:http://blog.csdn.net/wen0006/archive/2009/02/28/3945845.aspx?mkdi

学会用core dump调试程序错误

转载自:

http://blog.csdn.net/wen0006/archive/2009/02/28/3945845.aspx

?

mkdir -p /root/corefile

echo "/root/corefile/core-%e-%p-%t" > /proc/sys/kernel/core-pattern控制core文件保存位置和文件名格式。

以下是参数列表:
? ? %p - insert pid into filename 添加pid
? ? %u - insert current uid into filename 添加当前uid
? ? %g - insert current gid into filename 添加当前gid
? ? %s - insert signal that caused the coredump into the filename 添加导致产生core的信号
? ? %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
? ? %h - insert hostname where the coredump happened into filename 添加主机名
? ? %e - insert coredumping executable name into filename 添加命令名

4. 用gdb查看core文件:
下面我们可以在发生运行时信号引起的错误时发生core dump了.编译时加上-g
发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.
gdb [exec file] [core file]
如:
gdb ./test test.core
在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件行.

还有一种方法是:

gdb -c test.core

然后输入where,幸运的话(yum --disablerepo='*' --enablerepo='*-debuginfo'安装了debug包)直接就可以定位了。

5. 给个例子

test.c

void a()

{

?? char *p = NULL;

?? printf("%d\n", *p);

}

int main()

{

??? a();

??? return 0;

}

编译 gcc -g -o test test.c

运行 ./test

报segmentation fault(core dump)

gdb ./test test.core如果生成的是test.core.

?

?

另外一个用得比较多的功能是GDB执行用户命令脚本。我们组无施同学有一个例子:Oceanbase系统有一个ObGetParam的类,是一个数组,里面的每个元素是一个ObCellInfo,ObGetParam中可能包含成百上千个ObCellInfo,现在需要在GDB调试的时候输出数组中所有的ObCellInfo对象信息。脚本如下:

define dumpGetParamset $cell_list = ($arg0)set $cell_num = ($arg1)set $cell_idx = (0)while ($cell_idx < $cell_num)??printf "cell_idx:%d,table_id:%llu,column_id:%llu\n", $cell_idx,????$cell_list[$cell_idx].table_id_, $cell_list[$cell_idx].column_id??set $cell_idx = $cell_idx + 1endend

上面的代码定义了一个命令叫dumpGetParam,其第一个参数$arg0是cell数组的地址,第二个参数$arg1是数组大小,代码的功能就是打印所有cell的信息。
把上面的代码写入一个文本文件dump_get_param.txt,在gdb中执行source dump_get_param.txt,然后就可以使用dumpGetParam命令了。

热点排行