【debug】段错误

段错误

参考:

https://blog.csdn.net/chen1415886044/article/details/108175581

https://blog.csdn.net/chen1415886044/article/details/108118966 #coredump

原理分析:

常用解决方法:

coredump分析段错误详细步骤:

  1. 创建储存coredump空间

    1
    如,mkdir /home/tests/corefile
  2. 设置coredump文件大小

    1
    ulimit -c unlimited
  3. 设置文件格式

    1
    2
    echo “1” > /proc/sys/kernel/core_uses_pid //将1写入到该文件里
    echo "/home/tests/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
  4. 修改配置文件/etc/profile,

    1
    2
    添加 `ulimit -S -c unlimited > /dev/null 2>&1`
    后执行source etc/profile
  5. 在配置文件/etc/rc.local中最后面添加信息(机器重启时会自动加载该命令)

    1
    2
    rm -rf /home/tests/corefile/*
    机器重启时清空该文件夹,由于产生的coredump文件很大,若不清空的话时间长了会将硬盘占满;
  6. 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    1.test.c源文件
    #include<stdio.h>
    int main()
    {
    int *p = NULL;
    *p = 3;

    return 0;
    }


    2.Makefile文件
    make:
    gcc -g test.c -o test #加g 才能gdb调试
    clean:
    rm test
    ~

    详细过程:

    1).执行make,编译完成后,运行二级制文件,生成corefile

    1
    2
    3
    [root@hik c]# ll /home/tests/corefile/
    total 220
    -rw-------. 1 root root 307200 Jun 24 22:21 core-test-58552-1656080504

    2)readelf 查看是否为core文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    readelf -h /home/tests/corefile/core-test-58552-1656080504
    ELF Header:
    Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
    Class: ELF64
    Data: 2's complement, little endian
    Version: 1 (current)
    OS/ABI: UNIX - System V
    ABI Version: 0
    Type: CORE (Core file)
    Machine: Advanced Micro Devices X86-64
    Version: 0x1
    Entry point address: 0x0
    Start of program headers: 64 (bytes into file)
    Start of section headers: 0 (bytes into file)
    Flags: 0x0
    Size of this header: 64 (bytes)
    Size of program headers: 56 (bytes)
    Number of program headers: 24
    Size of section headers: 0 (bytes)
    Number of section headers: 0
    Section header string table index: 0

  7. 运行gdb阅读core文件,格式,gdb 二进制文件 core文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    [root@hik c]# gdb test /home/tests/corefile/core-test-58552-1656080504
    GNU gdb (GDB) Red Hat Enterprise Linux 10.2-8.el9
    Copyright (C) 2021 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-redhat-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <https://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...
    [New LWP 58552]
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".
    Core was generated by `./test'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0 0x0000000000401116 in main () at test.c:5
    5 *p = 3;
    (gdb)
  8. 进一步调试:断点调试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    (gdb) l #--用l(list)显示我们的源代码
    1 #include<stdio.h>
    2 int main()
    3 {
    4 int *p = NULL;
    5 *p = 3;
    6
    7 return 0;
    8 }
    (gdb) b 5 # --用b(break)设置断点
    Breakpoint 1 at 0x401112: file test.c, line 5.
    (gdb) p p # --用p(print)打印变量i的值[看到没,这里p的值是0哦]
    $1 = (int *) 0x0
    (gdb) r #--用r(run)运行,直到断点处
    Starting program: /home/tests/c/test
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib64/libthread_db.so.1".

    Breakpoint 1, main () at test.c:5
    5 *p = 3; #--[试图往地址0处写进一个值]
    (gdb) n #-用n(next)执行下一步

    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000401116 in main () at test.c:5
    5 *p = 3;
    (gdb) c #--在上面我们接收到了SIGSEGV,然后用c(continue)继续执
    Continuing.

    Program terminated with signal SIGSEGV, Segmentation fault.
    The program no longer exists.
    (gdb) quit #--退出gdb

    undefined reference to

    依次排查(后续顺序会有调整):

    case1:

    函数定义时使用的是static型,它的作用域限于包含它的文件中,但是我却在别的文件中引用这个函数,编译结果必然会出现“函数未定义的使用”。

    case2: