Linux環境下常常遇到某個進程掛掉而找不到緣由,咱們能夠經過生成core file文件加上gdb來定位。html
(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]linux
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行了)redis
參考:
段錯誤bug的調試 (挺經典的)
http://blog.csdn.net/ab198604/article/details/6164110ubuntu
Linux下segment fault的調試
http://www.docin.com/p-390840896.html
http://wenku.baidu.com/view/310242e69b89680203d82599.html###post
ulimit -c unlimited (比較詳細地介紹了ulimit命令)
http://www.cnblogs.com/qq78292959/archive/2012/05/08/2490443.html測試
GDB讓調試變簡單 (也寫的很經典的)
http://dev.jizhiinfo.net/?post=25spa
gdb結合coredump定位崩潰進程 (很實用的)
http://lazycat.is-programmer.com/posts/31925.html.net
gdb core調試
http://www.cnblogs.com/wangkangluo1/archive/2012/06/28/2566575.htmldebug
gdb core文件調試
http://www.cppblog.com/isware/archive/2011/07/29/152046.aspx3d
LINUX下GDB調試
http://blog.csdn.net/yeyuangen/article/details/6825542