Linux生成core使用

来源:互联网 发布:c语言流程图while 编辑:程序博客网 时间:2024/06/10 10:10

Linux下产生并调试core文件 先看看我用的是个什么机器:

[root@localhost work]# uname -a

Linux localhost.localdomain 2.6.32-279.el6.i686 #1 SMP Wed Jun 13 18:23:32 EDT 2012 i686 i686 i386 GNU/Linux

 

再看看默认的一些参数,注意core file size是个0,程序出错时不会产生core文件了。

[root@localhost work]# 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) 15029

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) 10240

cpu time               (seconds, -t) unlimited

max user processes              (-u) 1024

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited

 

写个简单的程序,看看core文件是不是会被产生。

[root@localhost work]# cat main.c

#include <stdio.h>

 

int main(void)

{

char *str = "hello";

str[0] = 'h';

return 0;

}

 

[root@localhost work]# gcc main.c -g -o main

[root@localhost work]# ./main

Segmentation fault (core dumped)

[root@localhost work]#  ls -l core.*

ls: cannot access core.*: No such file or directory

 

没有找到core文件,我们改改ulimit的设置,让它产生。1024是随便取的,要是core文件大于1024个块,就产生不出来了。

 

[root@localhost work]# ulimit -c 1024

[root@localhost work]# ulimit -a

core file size          (blocks, -c) 1024

data seg size           (kbytes, -d) unlimited

scheduling priority             (-e) 0

file size               (blocks, -f) unlimited

pending signals                 (-i) 15029

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) 10240

cpu time               (seconds, -t) unlimited

max user processes              (-u) 1024

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited

 

[root@localhost work]# ./main

Segmentation fault (core dumped)

[root@localhost work]#  ls -l core.*

-rw------- 1 root root 155648 Jul  2 20:56 core.5860

 

注意看上述的输出信息,多了个(core dumped)。确实产生了一个core文件,5860是该进程的PID。我们用GDB来看看这个core

 

[root@localhost work]#  gdb --core=core.5860

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6)

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-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

Missing separate debuginfo for the main executable file

Try: yum --disablerepo='*' --enablerepo='*-debug*' install /usr/lib/debug/.build-id/b9/b3b663d93d2e0baab612b9ac21bdb91402dbeb

[New Thread 5860]

Core was generated by `./main'.

Program terminated with signal 11, Segmentation fault.

#0  0x080483a4 in ?? ()

(gdb) bt

#0  0x080483a4 in ?? ()

#1  0x003f6ce6 in ?? ()

#2  0x00000001 in ?? ()

#3  0xbf86f724 in ?? ()

#4  0x08048301 in ?? ()

(gdb)

 

此时用bt看不到backtrace,也就是调用堆栈,原来GDB还不知道符号信息在哪里。我们告诉它一下:

(gdb) file ./main

Reading symbols from /work/main...done.

(gdb) bt

#0  0x080483a4 in main () at main.c:6

 

此时backtrace出来了。

(gdb) l

1#include <stdio.h>

2

3int main(void)

4{

5char *str = "hello";

6str[0] = 'h';

7return 0;

8}

 

在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数。

1.core文件的生成开关和大小限制
---------------------------------
1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。
2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。

2.core文件的名称和生成路径

----------------------------

core文件生成路径:

输入可执行文件运行命令的同一路径下。

若系统生成的core文件不带其他任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。如下:

[root@localhost work]# ls

1.txt  core.5860  main  main.c  main.map

 

1/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core

可通过以下命令修改此文件:

echo "1" > /proc/sys/kernel/core_uses_pid

2proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。

可通过以下命令修改此文件:

echo "/corefile/core-%e-%p-%t" > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳

以下是参数列表:

%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.core文件的查看

-----------------

core文件需要使用gdb来查看。

gdb ./a.out

core-file core.xxxx

使用bt命令即可看到程序出错的地方。

以下两种命令方式具有相同的效果,但是在有些环境下不生效,所以推荐使用上面的命令。

1gdb -core=core.xxxx

file ./a.out

bt

2gdb -c core.xxxx

file ./a.out

bt

 

4.开发板上使用core文件调试

-----------------------------

如果开发板的操作系统也是linuxcore调试方法依然适用。如果开发板上不支持gdb,可将开发板的环境(依赖库)、可执行文件和core文件拷贝到PClinux下。

PC上调试开发板上产生的core文件,需要使用交叉编译器自带的gdb,并且需要在gdb中指定solib-absolute-prefixsolib-search-path两个变量以保证gdb能够找到可执行程序的依赖库路径。有一种建立配置文件的方法,不需要每次启动gdb都配置以上变量,即:在待运行gdb的路径下建立.gdbinit

配置文件内容:

set solib-absolute-prefix YOUR_CROSS_COMPILE_PATH

set solib-search-path YOUR_CROSS_COMPILE_PATH

set solib-search-path YOUR_DEVELOPER_TOOLS_LIB_PATH

handle SIG32 nostop noprint pass