【C】C调试段错误方法一(Linux下core dump 和 gdp的使用)

步骤一:kernel默认是关闭产生core文件,通过ulimit -c 命令查看是否开启,如果是0,则需要使用ulimit -c unlimited  进行开启 (unlimited是无限制大小,也可以自行设置core文件大小)

    需要注意的是,如果使用unlimit -c 100 设置后大小后,后面再修改core文件大小时只能比100小,否则会报错。

    还可以通过ulimit -a 查看对应的 core file size 为 unlimited则开启成功。

ubuntu@ubuntu:~$ ulimit -c
0
ubuntu@ubuntu:~$ ulimit -c unlimited
ubuntu@ubuntu:~$ ulimit -c
unlimited
ubuntu@ubuntu:~$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7647
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7647
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

步骤二:编写测试代码

  1 #include <stdio.h>
  2 
  3 
  4 int main(int argc,const char *argv[])
  5 {
  6 
  7     char *ptr = "111111";
  8     *ptr = 0;
  9 
 10     return 0;
 11 }

步骤三:编译,gcc test_segfault.c -g    加上-g是为了后面gdb调试使用,可以看见~目录下产生了一个core文件。

ubuntu@ubuntu:~$ gcc test_segfault.c -g
ubuntu@ubuntu:~$ 
ubuntu@ubuntu:~$ 
ubuntu@ubuntu:~$ ./a.out 
段错误 (核心已转储)
ubuntu@ubuntu:~$ ls
a.out             test_segfault.c                    公共的  图片  音乐
core              VMwareTools-10.3.2-9925305.tar.gz  模板    文档  桌面
examples.desktop  vmware-tools-distrib               视频    下载
ubuntu@ubuntu:~$ 

通过readelf -h core可以查看core所属文件类型

ubuntu@ubuntu:~$ readelf -h core  
ELF 头:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  类别:                              ELF64
  数据:                              2 补码,小端序 (little endian)
  版本:                              1 (current)
  OS/ABI:                            UNIX - System V
  ABI 版本:                          0
  类型:                              CORE (Core 文件)
  系统架构:                          Advanced Micro Devices X86-64
  版本:                              0x1
  入口点地址:               0x0
  程序头起点:          64 (bytes into file)
  Start of section headers:          0 (bytes into file)
  标志:             0x0
  本头的大小:       64 (字节)
  程序头大小:       56 (字节)
  Number of program headers:         18
  节头大小:         0 (字节)
  节头数量:         0
  字符串表索引节头: 0

接下来就是最后一步,通过gdb和core文件定位段错误的位置。

 gdb ./a.out ./core

可以看见收到了一个SIGSEGV信号,并且打印出段错误的地址为0x00000000004004ed,gdb是直接指出了错误的行数和位置。我们也可以通过addr2line命令和该地址定位出段错误的具体位置。

ubuntu@ubuntu:~$ gdb ./a.out ./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 ./a.out...done.
[New LWP 53315]

warning: Unexpected size of section `.reg-xstate/53315' in core file.
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.

warning: Unexpected size of section `.reg-xstate/53315' in core file.
#0  0x00000000004004ed in main (argc=1, argv=0x7ffd0bbb2458)
    at test_segfault.c:8
8               *ptr = 0;

 

posted @ 2020-06-02 17:29  一半丶  阅读(440)  评论(0编辑  收藏  举报