core dump

1啓用core dump

ulimit –c size
size以kb爲單位;或者
ulimit -c unlimited
2設置code dump文件輸出目錄及格式
echo "1" > /proc/sys/kernel/core-user-pid
使core文件名加上pid號,還可以用
mkdir -p /root/corefile
echo "/root/corefile/core-%e-%p-%t" > /proc/sys/kernel/core-pattern
控制core文件保存位置和文件名格式。
 
以下是參數列表:
    %p - insert pid into filename 添加pid
    %u - insert current uid into filename 添加當前uid
    %g - insert current gid into filename 添加當前gid
    %s - insert signal that caused the coredump into the filename 添加導致產生core的信號
    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成時的unix時間
    %h - insert hostname where the coredump happened into filename 添加主機名
    %e - insert coredumping executable name into filename 添加命令名
3用gdb查看core文件
下面我們可以在發生運行時信號引起的錯誤時發生core dump了.編譯時加上-g
發生core dump之後, 用gdb進行查看core文件的內容, 以定位文件中引發core dump的行。
gdb [exec file] [core file]
如:
gdb ./test test.core
在進入gdb後, 用bt命令查看backtrace以檢查發生程序運行到哪裏, 來定位core dump的文件行。
4給個例子
源代碼文件test.c
void a()
{
   char *p = NULL;
   printf("%d\n", *p);
}
int main()
{
    a();
    return 0;
}
編寫makefile
test:test.c
        gcc -g -o $@ $^
 
clean:
        rm -f test *.gdb *~
運行
make
運行
./test
輸出
Segmentation fault (core dumped)
運行
gdb test /root/corefile/core-test-1956-1302505135
輸出
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/jason/tmp/coredump/test...done.
[New Thread 1956]
 
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0 0x080483d4 in a () at test.c:9
9          printf("%d\n", *p);
(gdb)
查看線程堆棧,定位錯誤
(gdb) info thread
* 1 Thread 1956 0x080483d4 in a () at test.c:9
(gdb) bt  
#0 0x080483d4 in a () at test.c:9
#1 0x080483f8 in main () at test.c:17
(gdb) frame 1
#1 0x080483f8 in main () at test.c:17
17          a();
(gdb) frame 0
#0 0x080483d4 in a () at test.c:9
9          printf("%d\n", *p);
(gdb) l
4
5       {
6
7          char *p = NULL;
8
9          printf("%d\n", *p);
10
11      }
12
13      int main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章