Linux下调试段错误 (gdb,core,ulimit)

时间:2023-03-09 00:34:32
Linux下调试段错误 (gdb,core,ulimit)

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.****.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.****.net/yeyuangen/article/details/6825542