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

    在做嵌入式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 <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.
[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 就可以了。

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

li_wen01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值