在實驗室環(huán)境中經(jīng)常會遇到的問題就是進(jìn)程down了,這種問題沒有g(shù)db這種調(diào)試工具的話很難定位到具體有問題的點。首先要用core文件調(diào)試,需要在進(jìn)程down掉的時候生成core文件,下面我們首先用root做好相應(yīng)的設(shè)置。
查看ulimit設(shè)置,
omu1:/opt/y00249743 # ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 194775
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 2048
cpu time (seconds, -t) unlimited
max user processes (-u) 194775
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
現(xiàn)在core file size是0,這樣進(jìn)程down掉的時候是不會生成core file的。設(shè)置ulimit
ulimit -c unlimited查看修改后的設(shè)置
omu1:/opt/y00249743 # ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 194775
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 2048
cpu time (seconds, -t) unlimited
max user processes (-u) 194775
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited設(shè)置core文件生成的路徑
omu1:/opt/y00249743 # echo core > /proc/sys/kernel/core_pattern有問題的代碼
#include <stdio.h> void core_here(char *ptr) { *ptr = 0; } void test() { char *ptr = NULL; core_here(ptr); } int main() { test(); return 0; }編譯代碼
omu1:/opt/y00249743 # gcc -g test.c -o test執(zhí)行后將會在本目錄下生成core文件
omu1:/opt/y00249743 # ./test
Segmentation fault (core dumped)用gdb調(diào)試core文件
omu1:/opt/y00249743 # gdb test core
GNU gdb 6.6
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i586-suse-linux"...
Using host libthread_db library "/lib/libthread_db.so.1".
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0 0x0804838a in core_here (ptr=0x0) at test.c:5
5 ptr = 0;
雖然這里一眼就可以看出是在是在test.c的第5行,ptr = 0執(zhí)行導(dǎo)致進(jìn)程down掉,我們在實驗室環(huán)境中調(diào)試代碼可能有時候需要看下一些變量的值以及調(diào)用棧,所以我們繼續(xù)看下。查看調(diào)用棧信息
(gdb) bt
#0 0x0804838a in core_here (ptr=0x0) at test.c:5
#1 0x080483a7 in test () at test.c:11
#2 0x080483bc in main () at test.c:16
這里可以看到調(diào)用棧里有三個函數(shù)。查看具體變量值
(gdb) frame 0
#0 0x0804838a in core_here (ptr=0x0) at test.c:5
5 *ptr = 0;
確定代碼塊
(gdb) l
1 #include <stdio.h>
2
3 void core_here(char *ptr)
4 {
5 *ptr = 0;
6 }
7
8 void test()
9 {
10 char *ptr = NULL;
(gdb) p ptr
$3 = 0x0