使用gdb和core dump迅速定位段错误

来源:互联网 发布:淘宝不给发票怎么应对 编辑:程序博客网 时间:2024/05/22 17:22
一、什么是core dump
    core:内存、核心的意思;
    dump:抛出,扔出;
    core dump:前提:当某程序崩溃的一瞬间,内核会抛出当时该程序进程的内存详细情况,存储在一个名叫core.xxx(xxx为一个数字,比如core.699)的文件中。
二、更改生成的core文件的大小限制
    可见,core文件是内核生成的,那某一个进程因为段错误而崩溃的时候的内存映像很大,那必然会生成一个很大的core文件,所以我们可以通过ulimit命令来设置生成core文件的大小,例如$ulimit -c unlimited,这里就是设置生成的core文件无大小限制。
三、生成core文件
    当第二步完成了,就运行一次那个有问题的程序,然后自然就会因为段错误而崩溃,在当前目录下就生成了core.xxx文件。
四、分析core文件

    使用命令$gdb 程序名 core.xxx,然后再输入where就可以看到产生段错误的地方。


cat /proc/sys/kernel/core_pattern
看看core文件的设置


例如,一个程序cmm_test_tool在运行的时候发生了错误,并生成了一个core文件,如下:

-rw-r–r– 1 root cmm_test_tool.c-rw-r–r– 1 root cmm_test_tool.o-rwxr-xr-x 1 root cmm_test_tool-rw——- 1 root core.19344-rw——- 1 root core.19351-rw-r–r– 1 root cmm_test_tool.cfg-rw-r–r– 1 root cmm_test_tool.res-rw-r–r– 1 root cmm_test_tool.log[root@AUTOTEST_SIM2 mam2cm]#

就可以利用命令gdb进行查找,参数一是应用程序的名称,参数二是core文件,运行
gdb cmm_test_tool core.19344结果如下:

[root@AUTOTEST_SIM2 mam2cm]# gdb cmm_test_tool core.19344GNU gdb Red Hat Linux (5.2.1-4)Copyright 2002 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type “show copying” to see the conditions.There is absolutely no warranty for GDB. Type “show warranty” for details.This GDB was configured as “i386-redhat-linux”…Core was generated by `./cmm_test_tool’.Program terminated with signal 11, Segmentation fault.Reading symbols from /lib/i686/libpthread.so.0…done.Loaded symbols for /lib/i686/libpthread.so.0Reading symbols from /lib/i686/libm.so.6…done.Loaded symbols for /lib/i686/libm.so.6Reading symbols from /usr/lib/libz.so.1…done.Loaded symbols for /usr/lib/libz.so.1Reading symbols from /usr/lib/libstdc++.so.5…done.Loaded symbols for /usr/lib/libstdc++.so.5Reading symbols from /lib/i686/libc.so.6…done.Loaded symbols for /lib/i686/libc.so.6Reading symbols from /lib/libgcc_s.so.1…done.Loaded symbols for /lib/libgcc_s.so.1Reading symbols from /lib/ld-linux.so.2…done.Loaded symbols for /lib/ld-linux.so.2Reading symbols from /lib/libnss_files.so.2…done.Loaded symbols for /lib/libnss_files.so.2#0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6(gdb)

进入gdb提示符,输入where,找到错误发生的位置和堆栈,如下:

(gdb) where#0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6#1 0×4202d4e7 in strtoul () from /lib/i686/libc.so.6#2 0×0804b4da in GetMaxIDFromDB (get_type=2, max_id=0×806fd20) at cmm_test_tool.c:788#3 0×0804b9d7 in ConstrctVODProgram (vod_program=0×40345bdc) at cmm_test_tool.c:946#4 0×0804a2f4 in TVRequestThread (arg=0×0) at cmm_test_tool.c:372#5 0×40021941 in pthread_start_thread () from /lib/i686/libpthread.so.0(gdb)

至此,可以看出文件出错的位置是函数 GetMaxIDFromDB ,两个参数分别是2和0×806fd20,这个函数位于源代码的788行,基于此,我们就可以有针对性的找到问题的根源,并加以解决。


0 0
原创粉丝点击