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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章