zoukankan      html  css  js  c++  java
  • linux下调试core的命令

    在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数。

    1.core文件的生成开关和大小限制
      1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。
      2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。


    2.core文件的名称和生成路径
    core文件生成路径:
    输入可执行文件运行命令的同一路径下。
    若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

      1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
    可通过以下命令修改此文件:echo "1" > /proc/sys/kernel/core_uses_pid

      2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
    可通过以下命令修改此文件:
    echo "/corefile/core-%e-%p-%t" > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
    以下是参数列表:
        %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 添加命令名


    3.core文件的查看
     core文件需要使用gdb来查看。
     gdb ./a.out
     core-file core.xxxx
     使用bt命令即可看到程序出错的地方。 
    以下两种命令方式具有相同的效果,但是在有些环境下不生效,所以推荐使用上面的命令。 
    1)gdb -core=core.xxxx
    file ./a.out
    bt 
    2)gdb -c core.xxxx
    file ./a.out
    bt

    3)gdb ./bin/game ./core.9317

    例子:

    首先看看默认的一些core的参数,注意core file size是个0,程序出错时不会产生core文件了。

    $ ulimit -a
    core file size (blocks, -c) 0
    data seg size (kbytes, -d) unlimited
    file size (blocks, -f) unlimited
    max locked memory (kbytes, -l) 4
    max memory size (kbytes, -m) unlimited
    open files (-n) 2048
    pipe size (512 bytes, -p) 8
    stack size (kbytes, -s) 10240
    cpu time (seconds, -t) unlimited
    max user processes (-u) 7168
    virtual memory (kbytes, -v) unlimited

    写个简单的程序,看看core文件是不是会被产生。

    $ more foo.c

    #include <stdio.h>

    static void sub(void);

    int main(void)
    {
        sub();
        return 0;
    }

    static void sub(void)
    {
        int *p = NULL;

        /* derefernce a null pointer, expect core dump. */
        printf("%d", *p);
    }

    $ gcc -Wall -g foo.c
    $ ./a.out
    Segmentation fault

    $ ls -l core.*
    ls: core.*: No such file or directory

    没有找到core文件,我们改改ulimit的设置,让它产生。1024是随便取的,要是core文件大于1024个块,就产生不出来了。

    $ ulimit -c 1024 (转者注: 使用-c unlimited不限制core文件大小)
     

    $ ulimit -a
    core file size (blocks, -c) 1024
    data seg size (kbytes, -d) unlimited
    file size (blocks, -f) unlimited
    max locked memory (kbytes, -l) 4
    max memory size (kbytes, -m) unlimited
    open files (-n) 2048
    pipe size (512 bytes, -p) 8
    stack size (kbytes, -s) 10240
    cpu time (seconds, -t) unlimited
    max user processes (-u) 7168
    virtual memory (kbytes, -v) unlimited

    $ ./a.out
    Segmentation fault (core dumped)
    $ ls -l core.*
    -rw------- 1 uniware uniware 53248 Jun 30 17:10 core.9128

    注意看上述的输出信息,多了个(core dumped)。确实产生了一个core文件,9128是该进程的PID。我们用GDB来看看这个core。

    (转者注:默认生成的文件就叫core,不带PID,如果要带PID需要设置,通过echo "1" > /proc/sys/kernel/core_uses_pid能够设置pid)

    gdb --core=core.9128
    GNU gdb Asianux (6.0post-0.20040223.17.1AX)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome 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-asianux-linux-gnu".
    Core was generated by `./a.out'.
    Program terminated with signal 11, Segmentation fault.
    #0 0x08048373 in ?? ()
    (gdb) bt
    #0 0x08048373 in ?? ()
    #1 0xbfffd8f8 in ?? ()
    #2 0x0804839e in ?? ()
    #3 0xb74cc6b3 in ?? ()
    #4 0x00000000 in ?? ()

    此时用bt看不到backtrace,也就是调用堆栈,原来GDB还不知道符号信息在哪里。我们告诉它一下:

    (gdb) file ./a.out
    Reading symbols from ./a.out...done.
    Using host libthread_db library "/lib/tls/libthread_db.so.1".
    (gdb) bt
    #0 0x08048373 in sub () at foo.c:17
    #1 0x08048359 in main () at foo.c:8

    此时backtrace出来了。

    (gdb) l
    8         sub();
    9         return 0;
    10     }
    11
    12     static void sub(void)
    13     {
    14         int *p = NULL;
    15
    16         /* derefernce a null pointer, expect core dump. */
    17         printf("%d", *p);

  • 相关阅读:
    sql学习2----函数
    网络相关命令
    网络相关概念
    linux学习(2)--指令
    linux学习(1)--基本知识
    Linux学习技巧
    VMware安装Linux
    接口测试
    app测试--DDMS的使用
    app测试--性能测试DDMS
  • 原文地址:https://www.cnblogs.com/DswCnblog/p/5669692.html
Copyright © 2011-2022 走看看