Linux调试之(五)gdb调试coredump

文章目录

    • 1.什么是core文件?
    • 2.怎样配置生成 core 文件
        • (1)core文件开关
        • (2)core文件命名和保存路径
    • 3.调试core文件
        • (1)test.c
        • (2)编译
        • (3)gdb调试
          • ①gdb [exec file] [core file] 然后执行bt看堆栈信息:
          • ① gdb -c [core file],然后 file [exec file],最后再使用 bt 查看错误位置

1.什么是core文件?

有问题的程序运行后,产生“段错误 (核心已转储)”时生成的具有堆栈信息和调试信息的文件。


2.怎样配置生成 core 文件

(1)core文件开关

①使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件;

②使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件;

③使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法;

④如果需要开机就执行,则需要将这句命令写到 /etc/profile 等文件。

echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

Linux调试之(五)gdb调试coredump_第1张图片


(2)core文件命名和保存路径

core文件有默认的名称和路径,但为了方便我们通常会自己命名和指定保存路径方法如下:

echo "/corefile/core-%e-%p-%t" > core_pattern

命名的参数列表:

    %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文件

(1)test.c

#include 
#include 
#include 
#include 

struct{
   int rlim_cur;
   int rlim_max;
}rlimt;

#define CORE_SIZE   1024 * 1024 * 500
int main()
{
    struct rlimit rlmt;
    printf("Before set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);

    rlmt.rlim_cur = (rlim_t)CORE_SIZE;
    rlmt.rlim_max  = (rlim_t)CORE_SIZE;

    printf("After set rlimit CORE dump current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max);

    /*测试非法内存,产生core文件*/
    int *ptr = NULL;
    *ptr = 10;

    return 0;
}

(2)编译

编译时需要加选项g使程序生成调试信息:

gcc -g test.c -o test

(3)gdb调试

①gdb [exec file] [core file] 然后执行bt看堆栈信息:
ubuntu:~$     gcc -g test.c -o test
ubuntu:~$ ./test 
Before set rlimit CORE dump current is:1824195920, max is:0
After set rlimit CORE dump current is:524288000, max is:524288000
Segmentation fault (core dumped)
xuliu@ubuntu:~$ gdb test core
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.

warning: exec file is newer than core file.
[New LWP 51918]
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400580 in main () at test.c:25
25          *ptr = 10; 
(gdb) bt
#0  0x0000000000400580 in main () at test.c:25
① gdb -c [core file],然后 file [exec file],最后再使用 bt 查看错误位置

Linux调试之(五)gdb调试coredump_第2张图片

你可能感兴趣的:(LINUX,内核驱动)