Linux环境下经常遇到某个进程挂掉而找不到原因,我们可以通过生成core file文件加上gdb来定位。

(1)首先 在makefile中要增加编译调试选项 -g,才可以利用下面的gdb来调试
 gcc udp_server.c -o udp_server.elf -g -lpthread
 
 -g选项的作用是在可执行文件中加入源代码的信息,比如可执行文件中第几条机器指令对应源代码的第几行,
 但并不是把整个源文件嵌入到可执行文件中,所以在调试时必须保证gdb能找到源文件。
 
(2)如何产生core file?
我们可以使用ulimit这条命令对core file文件的大小进行设定。
在用这个命令的时候主要是为了产生core文件,就是程序运行发行段错误时的文件.
一般默认情况下,core file的大小被设置为了0,这样系统就不dump出core file了。
这时用如下命令进行设置:
ulimit -c unlimited
这样便把core file的大小设置为了无限大,同时也可以使用数字来替代unlimited,对core file的上限值做更精确的设定。
  
(3)调试段错误 实际例子
lijun@ubuntu-server:~/workspace/test/udp_server$ 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) 63156
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) 63156
virtual memory          (kbytes, -v) unlimited
lijun@ubuntu-server:~/workspace/test/udp_server$ ulimit -c unlimited   (把core file的大小设置为了无限大)
lijun@ubuntu-server:~/workspace/test/udp_server$ ./udp_server.elf     (可执行程序测试)
wait client...
start recv data1111...
start recvfrom... 255
Segmentation fault (core dumped)
lijun@ubuntu-server:~/workspace/test/udp_server$ gdb udp_server.elf core    (gdb结合core文件调试)
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/old_service/lijun/workspace/test/udp_server/udp_server.elf...done.
[New LWP 25812]

warning: Can't read pathname for load map: Input/output error.
[Thread debugging using libthread_db enabled]
Core was generated by `./udp_server.elf'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007f8ac0a6c1a4 in vfprintf () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0  0x00007f8ac0a6c1a4 in vfprintf () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007f8ac0a74f79 in printf () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00000000004009d8 in main () at ./udp_server.c:72   (定位到72行了)

参考:
段错误bug的调试 (挺经典的)
http://blog.csdn.net/ab198604/article/details/6164110

Linux下segment fault的调试
http://www.docin.com/p-390840896.html
http://wenku.baidu.com/view/310242e69b89680203d82599.html###

ulimit -c unlimited  (比较详细地介绍了ulimit命令)
http://www.cnblogs.com/qq78292959/archive/2012/05/08/2490443.html

GDB让调试变简单 (也写的很经典的)
http://dev.jizhiinfo.net/?post=25

gdb结合coredump定位崩溃进程 (很实用的)
http://lazycat.is-programmer.com/posts/31925.html

gdb core调试
http://www.cnblogs.com/wangkangluo1/archive/2012/06/28/2566575.html

gdb core文件调试
http://www.cppblog.com/isware/archive/2011/07/29/152046.aspx

LINUX下GDB调试
http://blog.csdn.net/yeyuangen/article/details/6825542

最新文章

  1. Android总结之Gzip/Zip压缩
  2. 用margin还是padding
  3. error C2259: 'CException' : cannot instantiate abstract class
  4. JAVA Hashmap不能用基本的数据类型
  5. android基础学习之布局
  6. Android中通过WebView控件实现与JavaScript方法相互调用的地图应用
  7. Codeforces Round #299 (Div. 1)
  8. 在fragment中调用SharedPreferences
  9. jQuery延迟加载(懒加载)插件 – jquery.lazyload.js-Web前端(W3Cways.com) - Web前端学习之路
  10. 汽车Vin码识别技术的由来到发展
  11. Springboot 系列(十三)使用邮件服务
  12. centos6.9系列LNMP环境的安装
  13. 玩转Python图片处理 (OpenCV-Python )
  14. Nignx添加proxy_pass可能造成DNS解析超时的问题解决
  15. nrm 安装与npm镜像切换
  16. Effective Java 第三版——52. 明智而审慎地使用重载
  17. 网络运营商名称显示&amp;amp;SIM名称显示
  18. vue2.0分页组件,
  19. 用 Python 3 的 async / await 做异步编程
  20. JDK 8 - Method Reference 分析

热门文章

  1. Linux---cp命令学习
  2. 两级宏&amp;&amp;字符串化宏
  3. windows下安装win7虚拟机并安装oracle
  4. vue项目跨域问题
  5. Zabbix监控告警Lack of free swap space on Zabbix server解决办法
  6. Class:向传统类模式转变的构造函数
  7. 网络编程-osi七层
  8. 常用模块之 re shutil configparser hashlib xldt和xlwd
  9. PAT basic 1087
  10. Django templates(模板)