Linux下分析进程segment fault的方法

原博客地址:
https://blog.csdn.net/weixin_42263483/article/details/80868034
http://blog.itpub.net/29785807/viewspace-2154535/

长时间不用,命令容易忘,记录一下,以备忘.

1. 程序在编译时,记得加 -g 选项,以生成带符号表的目标程序.

2. 执行:

ulimit -c unlimited

问题现象
[chenjch@chen cjc]$ ulimit -c unlimited
-bash: ulimit: core file size: cannot modify limit: Operation not permitted

问题原因
chenjch用户没有权限
[chenjch@chen cjc]$ ulimit -a
core file size (blocks, -c) 0

解决方案:
[root@chen cjc]$ vi /etc/security/limits.conf
chenjch hard core unlimited
chenjch soft core unlimited

[chenjch@chen cjc]$ ulimit -c unlimited

3. 段错误发生后,执行:

gdb ./程序名 ./core
 
 

示例:


 
 
  1. Queue is empty: TRUE
  2. Queue is full: FALSE
  3. Element number in Queue: 0
  4. We are going to add 5 elements into queue:
  5. enqueue ok: q->data[ 0]=The first element
  6. enqueue ok: q->data[ 1]= 2:aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  7. enqueue ok: q->data[ 2]= 3:The third element
  8. enqueue ok: q->data[ 3]= 4:Bejing Beijing
  9. enqueue ok: q->data[ 4]= 5:The fivth element
  10. Queue is empty: FALSE
  11. Queue is full: FALSE
  12. Element number in Queue: 5
  13. We are going to remove 2 elements from queue:
  14. 段错误 (核心已转储)

执行gdb ./Queue ./core


 
 
  1. $ gdb ./Queue ./core
  2. GNU gdb (Ubuntu 7.11 .1 -0ubuntu1~ 16.5) 7.11 .1
  3. Copyright (C) 2016 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http: //gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "x86_64-linux-gnu".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http: //www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http: //www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from ./Queue...done.
  17. warning: exec file is newer than core file.
  18. [New LWP 5377]
  19. Core was generated by `./Queue '.
  20. Program terminated with signal SIGSEGV, Segmentation fault.
  21. #0 strlen () at ../sysdeps/x86_64/strlen.S:106
  22. 106 ../sysdeps/x86_64/strlen.S: 没有那个文件或目录.
strlen () at ../sysdeps/x86_64/strlen.S:106 106 ../sysdeps/x86_64/strlen.S: 没有那个文件或目录.

strlen()出错,这种一般因为入参为空指针,顺着代码排查即可.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值