【转载】使用core文件调试程序

本文详细解释了Linux环境下程序崩溃时如何生成核心文件,并提供了如何配置系统以生成核心文件的方法,以及如何使用GDB调试核心文件。重点讨论了核心文件的位置、创建条件及如何检查和创建核心文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

看下面的例子:

/*core_dump_test.c*/

      1 #include <stdio.h>

      2

      3 const char *str = "test";

      4

      5 void core_test()

      6 {

      7     str[1] = 'T';

      8 }

      9

     10 int main()

     11 {

     12     core_test();

     13

     14     return 0;

     15 }

 

编译:

[zhanghua@localhost core_dump]$ gcc –g core_dump_test.c -o core_dump_test

 

如果需要调试程序的话,使用gcc编译时加上-g选项,这样调试core文件的时候比较容易找到错误的地方。

 

 

执行:

 

[zhanghua@localhost core_dump]$ ./core_dump_test

段错误

 

运行core_dump_test程序出现了“段错误”,但没有产生core文件。这是因为系统默认core文件的大小为0,所以没有创建。可以用ulimit命令查看和修改core文件的大小。

 

[zhanghua@localhost core_dump]$ ulimit -c

0

[zhanghua@localhost core_dump]$ ulimit -c 1000

[zhanghua@localhost core_dump]$ ulimit -c

1000

 

-c 指定修改core文件的大小,1000指定了core文件大小。也可以对core文件的大小不做限制,如:

 

[zhanghua@localhost daemon]# ulimit -c unlimited

[zhanghua@localhost daemon]# ulimit -c

unlimited

 

如果想让修改永久生效,则需要修改配置文件,如 .bash_profile/etc/profile/etc/security/limits.conf

 

再次执行:

[zhanghua@localhost core_dump]$ ./core_dump_test

段错误 (core dumped)

[zhanghua@localhost core_dump]$ ls core.*

core.6133

 

可以看到已经创建了一个core.6133的文件.6133core_dump_test程序运行的进程ID

 

调式core文件

       core文件是个二进制文件,需要用相应的工具来分析程序崩溃时的内存映像。

 

[zhanghua@localhost core_dump]$ file core.6133

core.6133: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from 'core_dump_test'

 

Linux下可以用GDB来调试core文件。

 

[zhanghua@localhost core_dump]$ gdb core_dump_test core.6133

GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)

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

Core was generated by `./core_dump_test'.

Program terminated with signal 11, Segmentation fault.

Reading symbols from /lib/tls/libc.so.6...done.

Loaded symbols for /lib/tls/libc.so.6

Reading symbols from /lib/ld-linux.so.2...done.

Loaded symbols for /lib/ld-linux.so.2

#0  0x080482fd in core_test () at core_dump_test.c:7

7           str[1] = 'T';

(gdb) where

#0  0x080482fd in core_test () at core_dump_test.c:7

#1  0x08048317 in main () at core_dump_test.c:12

#2  0x42015574 in __libc_start_main () from /lib/tls/libc.so.6

 

GDB中键入where,就会看到程序崩溃时堆栈信息(当前函数之前的所有已调用函数的列表(包括当前函数),gdb只显示最近几个),我们很容易找到我们的程序在最后崩溃的时候调用了core_dump_test.c 7行的代码,导致程序崩溃。注意:在编译程序的时候要加入选项-g。您也可以试试其他命令, 如 framlist等。更详细的用法,请查阅GDB文档。

 

core文件创建在什么位置

       在进程当前工作目录的下创建。通常与程序在相同的路径下。但如果程序中调用了chdir函数,则有可能改变了当前工作目录。这时core文件创建在chdir指定的路径下。有好多程序崩溃了,我们却找不到core文件放在什么位置。和chdir函数就有关系。当然程序崩溃了不一定都产生core文件。

 

什么时候不产生core文件

在下列条件下不产生core文件:

( a )进程是设置-用户-ID,而且当前用户并非程序文件的所有者;

( b )进程是设置-组-ID,而且当前用户并非该程序文件的组所有者;

( c )用户没有写当前工作目录的许可权;

( d )文件太大。core文件的许可权(假定该文件在此之前并不存在)通常是用户读/写,组读和其他读。

转载于:https://www.cnblogs.com/chenkai1990/archive/2012/09/09/2678110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值