让程序崩溃时产生coredump

Core Dump
[ 2010-01-06 13:38:30 | 作者:  yuhen ]

在 Windows 下我们已经习惯了用 Windbg 之类的工具调试 dump 文件,从而分析并排除程序运行时错误。在 Linux 下我们同样可以完成类似的工作 —— Core Dump。


我们先看看相关的设置。
$ ulimit -a

core file size       (blocks, -c) 0
data seg size        (kbytes, -d) unlimited
scheduling priority          (-e) 20
file size            (blocks, -f) unlimited
pending signals              (-i) 16382
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) 8192
cpu time            (seconds, -t) unlimited
max user processes           (-u) unlimited
virtual memory       (kbytes, -v) unlimited
file locks                   (-x) unlimited

"core file size  (blocks, -c) 0" 意味着在程序崩溃时不会生成 core dump 文件,我们需要修改一下设置。如果你和我一样懒得修改配置文件,那么就输入下面这样命令吧。
$ sudo  sh -c "ulimit -c unlimited; ./test" # test 是可执行文件名。

等等…… 我们还是先准备个测试目标。
#include <stdio.h>
#include <stdlib.h>

void test()
{
    char* s = "abc";
    *s = 'x';
}

int main(int argc, char** argv)
{
    test();
    return (EXIT_SUCCESS);
}

很显然,我们在 test() 里面写了一个不该写的东东,这无疑会很严重。生成可执行文件后,执行上面的命令。
$ sudo sh -c "ulimit -c unlimited; ./test"

Segmentation fault (core dumped)
$ ls -l

total 96
-rw------- 1 root  root  167936 2010-01-06 13:30 core
-rwxr-xr-x 1 yuhen yuhen   9166 2010-01-06 13:16 test

这个 core 文件就是被系统 dump 出来的,我们分析目标就是它了。
$ sudo gdb test core

GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.

Reading symbols from .../dist/Debug/test...done.

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/tls/i686/cmov/libpthread.so.0... ...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/tls/i686/cmov/libpthread.so.0
Reading symbols from /lib/tls/i686/cmov/libc.so.6... ...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/tls/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2... ...done.
(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  0x080483f4 in test () at main.c:16

warning: Source file is more recent than executable.
16        *s = 'x';

最后这几行提示已经告诉我们错误的原因和代码位置,接下来如何调试就是 gdb 的技巧了,可以先输入 "where" 看看调用堆栈。
(gdb) where

#0  0x080483f4 in test () at main.c:16
#1  0x08048401 in main (argc=1, argv=0xbfd53e44) at main.c:22
(gdb) p s

$1 = 0x80484d0 "abc"
(gdb) info files

Symbols from ".../dist/Debug/test".
Local core dump file:

Local exec file:
    `.../dist/Debug/test', file type elf32-i386.
    Entry point: 0x8048330
    0x08048134 - 0x08048147 is .interp
    ... ...
    0x08048330 - 0x080484ac is .text
    0x080484ac - 0x080484c8 is .fini
    0x080484c8 - 0x080484d4 is .rodata

很显然 "abc" 属于 .rodata,严禁调戏。

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

附:如果你调试的是 Release (-O2) 版本,而且删除(strip)了符号表,那还是老老实实数汇编代码吧。可见用 Debug 版本试运行是很重要滴!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Windows 平台上,应用程序崩溃可能会产生 Core Dump 文件,通常以 .dmp 后缀结尾。这个文件包含了应用程序崩溃的内存状态,可以帮助开发者定位和解决问题。以下是处理 Windows 应用程序崩溃产生 Core Dump 的一些方法: 1. 启用 Windows Core Dump 在 Windows 上,默认情况下是不启用 Core Dump 的。使用以下命令可以启用 Core Dump: ``` reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpType /t REG_DWORD /d 2 /f ``` 2. 使用 Visual Studio 调试 Core Dump Visual Studio 支持使用 Core Dump 进行调试。可以使用 Visual Studio 打开 Core Dump 文件,并分析崩溃的原因。具体的操作步骤可以参考 Microsoft 官方文档。 3. 使用 WinDbg 调试 Core Dump WinDbg 是一款微软开发的用于 Windows 平台上的调试器,支持分析 Core Dump 文件。可以使用 WinDbg 打开 Core Dump 文件,并分析崩溃的原因。具体的操作步骤可以参考微软官方文档。 4. 使用第三方工具分析 Core Dump 除了使用 Visual Studio 和 WinDbg,还有一些第三方工具可以用来分析 Core Dump 文件,比如 Process Explorer、GDB 等。这些工具都有各自的优缺点,开发者可以根据自己的需要选择合适的工具。 总的来说,处理 Windows 应用程序崩溃产生 Core Dump 的过程比较复杂,需要开发者具备一定的调试经验。建议在处理 Core Dump 之前先了解一下相关的调试工具和技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值