一种Linux中快速定位段错误的方法

2019-07-13 01:57发布

    在做嵌入式Linux开发的时候,程序很容易出现段错误。段错误一般是内存操作指针出错或是内存溢出等问题,有的时候系统会有一点错误提示,但有的时候就直接提示个Segmentation fault (core dumped) 。如果程序是单线程,那很好处理,编译的时候添加参数-g  ,直接使用gdb 单步调试就可以直接定位到问题点在哪了。但是对于多线程,情况就不一样了。多线程进行单步调试不好处理,并且时候程序需要运行很久才出来段错误。这样直接使用gdb单步调试就不合适了。这里提供一种快速定位段错误的方式。 1.ulimit 命令设置core文件的最大值
1.1执行ulimit -a, 查看core文件的最大值,一般默认值是0
biao@ubuntu:~/test/FWH/9th_DiskTest$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3712
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) 3712
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited


1.2执行命令:ulimit -c unlimited
biao@ubuntu:~/test/FWH/9th_DiskTest$ ulimit -c unlimited
biao@ubuntu:~/test/FWH/9th_DiskTest$ 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) 3712
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) 3712
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
biao@ubuntu:~/test/FWH/9th_DiskTest$ 


可以看到core file size  已经被修改为不受限制,运行程序的时候会在当前目录生成一个core文件
biao@ubuntu:~/test/FWH/9th_DiskTest$ ls
core  Diskmanage  EncodeReadWrite  IndexManage  main.cpp  main.o  Makefile  sharestruct.h  test


2.使用gdb调试:
biao@ubuntu:~/test/FWH/9th_DiskTest$ sudo 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
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:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
[New LWP 7445]
[New LWP 7440]
[New LWP 7441]
[New LWP 7443]
[New LWP 7444]
[New LWP 7442]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004046c0 in StorageService::VideoWriteThread (this=0x71e470, arg=0x7fffac5cbccc)
    at EncodeReadWrite/StorageService.cpp:627
627                             l_s32Ret = l_pAudioQue[i]->ReadQueue(i,(void*)l_pAudioDataOut[i]);
[Current thread is 1 (Thread 0x7f32e0fa5700 (LWP 7445))]
(gdb) printf i
Bad format string, missing '"'.
(gdb) print i 
$1 = 6

(gdb) q
biao@ubuntu:~/test/FWH/9th_DiskTest$ 

上面的test 为我的可执行程序。
这样可以精确的定位到出现段错误的函数行,并可以查看各参数的值,这样定位起来就很快了。


注意在一个shell 线程中,core file size 值只能被修改一次,如果取消之后再修改会出现错误:
-bash: ulimit: core file size: cannot modify limit: Operation not permitted
解决这一问题的方法是exit退出该shell,重启一个shell 就可以了。