转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289

源自:http://andyniu.iteye.com/blog/1965571

core dump的概念:

core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.

On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase "to dump core" has come to mean, in many cases, any fatal error, regardless of whether a record of the program memory is created.

在linux平台下,设置core dump文件生成的方法:

linux coredump调试
 
1 )如何生成 coredump 文件 ?
登陆 LINUX 服务器,任意位置键入
       echo "ulimit -c 1024" >> /etc/profile
退出 LINUX 重新登陆 LINUX
键入 ulimit -c
如果显示 1024 那么说明 coredump 已经被开启。
1024 限制产生的 core 文件的大小不能超过 1024kb,可以使用参数unlimited,取消该限制
ulimit -c unlimited
2 ) . core 文件的简单介绍
在一个程序崩溃时,它一般会在指定目录下生成一个 core 文件。 core 文件仅仅是一个内存映象 ( 同时加上调试信息 ) ,主要是用来调试的。
3 ) . 开启或关闭 core 文件的生成
用以下命令来阻止系统生成 core 文件 :
ulimit -c 0
下面的命令可以检查生成 core 文件的选项是否打开 :
ulimit -a
该命令将显示所有的用户定制,其中选项 -a 代表“ all ”。
也可以修改系统文件来调整 core 选项
在 /etc/profile 通常会有这样一句话来禁止产生 core 文件,通常这种设置是合理的 :
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开 core 文件产生的设置。
在用户的 ~/.bash_profile 里加上 ulimit -c unlimited 来让特定的用户可以产生 core 文件。
如果 ulimit -c 0 则也是禁止产生 core 文件,而 ulimit -c 1024 则限制产生的 core 文件的大小不能超过 1024kb
4 ) . 设置 Core Dump 的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid 可以控制产生的 core 文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0
proc/sys/kernel/core_pattern 可以设置格式化的 core 文件保存位置或文件名 ,比如原来文件内容是 core-%e
可以这样修改 :
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 添加命令名
6 ) . 一个小方法来测试产生 core 文件
直接输入指令 :
kill -s SIGSEGV $$

如何产生Core Dump

发生doredump一般都是在进程收到某个信号的时候,Linux上现在大概有60多个信号,可以使用 kill -l 命令全部列出来。

sagi@sagi-laptop:~$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

针对特定的信号,应用程序可以写对应的信号处理函数。如果不指定,则采取默认的处理方式, 默认处理是coredump的信号如下:

3)SIGQUIT   4)SIGILL    6)SIGABRT   8)SIGFPE    11)SIGSEGV    7)SIGBUS    31)SIGSYS
5)SIGTRAP 24)SIGXCPU 25)SIGXFSZ 29)SIGIOT

我们看到SIGSEGV在其中,一般数组越界或是访问空指针都会产生这个信号。另外虽然默认是这样的,但是你也可以写自己的信号处理函数改变默认行为,更多信号相关可以看参考链接33

上述内容只是产生coredump的必要条件,而非充分条件。要产生core文件还依赖于程序运行的shell,可以通过ulimit -a命令查看,输出内容大致如下:

sagi@sagi-laptop:~$ 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,这个值用来限制产生的core文件大小,超过这个值就不会保存了。我这里输出是0,也就是不会保存core文件,即使产生了,也保存不下来==! 要改变这个设置,可以使用ulimit -c unlimited。

OK, 现在万事具备,只缺一个能产生Core的程序了,介个对C程序员来说太容易了。

  1. <span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">#include <stdio.h>;</span>
  2. <span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">#include <stdlib.h>;</span>
  3. <span style="margin:0px;padding:0px;border:0px;color:rgb(153,51,51);vertical-align:baseline;background-color:transparent;">int</span> crash<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">)</span>
  4. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">{</span>
  5. <span style="margin:0px;padding:0px;border:0px;color:rgb(153,51,51);vertical-align:baseline;background-color:transparent;">char</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">*</span>xxx <span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">=</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(255,0,0);vertical-align:baseline;background-color:transparent;">"crash!!"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">;</span>
  6. xxx<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">[</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,0,221);vertical-align:baseline;background-color:transparent;">1</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">]</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">=</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(255,0,0);vertical-align:baseline;background-color:transparent;">'D'</span><span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">;</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(102,102,102);font-style:italic;vertical-align:baseline;background-color:transparent;">// 写只读存储区!</span>
  7. <span style="margin:0px;padding:0px;border:0px;color:rgb(177,177,0);vertical-align:baseline;background-color:transparent;">return</span> <span style="margin:0px;padding:0px;border:0px;color:rgb(0,0,221);vertical-align:baseline;background-color:transparent;">2</span><span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">;</span>
  8. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">}</span>
  9. <span style="margin:0px;padding:0px;border:0px;color:rgb(153,51,51);vertical-align:baseline;background-color:transparent;">int</span> foo<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">)</span>
  10. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">{</span>
  11. <span style="margin:0px;padding:0px;border:0px;color:rgb(177,177,0);vertical-align:baseline;background-color:transparent;">return</span> crash<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">;</span>
  12. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">}</span>
  13. <span style="margin:0px;padding:0px;border:0px;color:rgb(153,51,51);vertical-align:baseline;background-color:transparent;">int</span> main<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">)</span>
  14. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">{</span>
  15. <span style="margin:0px;padding:0px;border:0px;color:rgb(177,177,0);vertical-align:baseline;background-color:transparent;">return</span> foo<span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(51,153,51);vertical-align:baseline;background-color:transparent;">;</span>
  16. <span style="margin:0px;padding:0px;border:0px;color:rgb(0,153,0);vertical-align:baseline;background-color:transparent;">}</span>

上手调试

测试如下代码
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
 
int func(int
*p)
{

        *p = 0;
}
 

int main()
{

        func(NULL);

        return
0;
}
 
生成可执行文件并运行
gcc -o main a.c
root@ubuntu:~# ./main
Segmentation fault (core dumped) 
<-----这里出现段错误并生成core文件了。
在/tmp目录下发现文件core-main-10815 
 
如何查看进程挂在哪里了?
我们可以用
gdb main /tmp/core-main-10815 
查看信息,发现能定位到函数了
Program terminated with signal 11, Segmentation fault.
#0  0x080483ba in func ()
 

如何定位到行?

在编译的时候开启-g调试开关就可以了
gcc -o main -g a.c
gdb
main /tmp/core-main-10815 
最终看到的结果如下,好棒。
Program terminated with signal 11, Segmentation fault.
#0  0x080483ba in func (p=0x0) at a.c:5
5          *p = 0;
 
总结一下,需要定位进程挂在哪一行我们只需要4个操作,
ulimit
-c unlimited
echo
"/tmp/core-%e-%p" > /proc/sys/kernel/core_pattern
gcc -o main -g a.c
gdb main /tmp/core-main-10815 
就可以啦。

上边的程序编译的时候有一点需要注意,需要带上参数-g,
这样生成的可执行程序中会带上足够的调试信息。编译运行之后你就应该能看见期待已久的“Segment Fault(core
dumped)”或是“段错误
(核心已转储)”之类的字眼了。看看当前目录下是不是有个core或是core.xxx的文件。祭出linux下经典的调试器GDB,首先带着core文件载入程序:gdb
exefile core,这里需要注意的这个core文件必须是exefile产生的,否则符号表会对不上。载入之后大概是这个样子的:

sagi@sagi-laptop:~$ gdb coredump core
Core was generated by ./coredump'.
Program terminated with signal 11, Segmentation fault.
#0 0x080483a7 in crash () at coredump.c:8
8 xxx[1] = 'D';
(gdb)

我们看到已经能直接定位到出core的地方了,在第8行写了一个只读的内存区域导致触发Segment Fault信号。在载入core的时候有个小技巧,如果你事先不知道这个core文件是由哪个程序产生的,你可以先随便找个代替一下,比如/usr/bin/w就是不错的选择。比如我们采用这种方法载入上边产生的core,gdb会有类似的输出:

sagi@sagi-laptop:~$ gdb /usr/bin/w core
Core was generated by ./coredump'.
Program terminated with signal 11, Segmentation fault.
#0 0x080483a7 in ?? ()
(gdb)

可以看到GDB已经提示你了,这个core是由哪个程序产生的。

GDB 常用操作

上边的程序比较简单,不需要另外的操作就能直接找到问题所在。现实却不是这样的,常常需要进行单步跟踪,设置断点之类的操作才能顺利定位问题。下边列出了GDB一些常用的操作。

  • 启动程序:run
  • 设置断点:b 行号|函数名
  • 删除断点:delete 断点编号
  • 禁用断点:disable 断点编号
  • 启用断点:enable 断点编号
  • 单步跟踪:next 也可以简写 n
  • 单步跟踪:step 也可以简写 s
  • 打印变量:print 变量名字
  • 设置变量:set var=value
  • 查看变量类型:ptype var
  • 顺序执行到结束:cont
  • 顺序执行到某一行: util lineno
  • 打印堆栈信息:bt

linux下生成core dump文件方法及设置【转】的更多相关文章

  1. linux下生成core dump文件方法及设置

    linux下生成core dump文件方法及设置    from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...

  2. linux下生成core dump文件方法

    core 文件的简单介绍 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成“核心转储”).我们可以认为 co ...

  3. 解决linux下不生成core dump文件

    core dump的概念: A core dump is the recorded state of the working memory of a computer program at a spe ...

  4. Ubuntu16.04下写的Qt程序,调试时没问题,运行时偶现崩溃 (需要在运行时生成core dump文件,QMAKE_CC += -g)

    记录一下 Ubuntu16.04下写的Qt程序,调试时没问题,运行时偶现崩溃 需要在运行时生成core dump文件 首先在pro结尾里加入 QMAKE_CC += -g QMAKE_CXX += - ...

  5. Linux中生成Core Dump系统异常信息记录文件的教程

    Linux中生成Core Dump系统异常信息记录文件的教程 http://www.jb51.net/LINUXjishu/473351.html

  6. linux下恢复误删除的文件方法(ext2及ext3)

     linux下恢复误删除的文件方法(ext2及ext3) 2009-12-19 15:23:47 分类: LINUX 如果是ext2文件系统的,直接用debugfs是可以恢复出来的,但对于ext3,d ...

  7. core dump使用方法、设置、测试用例

    core dump使用方法.设置.测试用例 http://blog.csdn.net/liuzhuchen/article/details/21975227

  8. dotnet core调试docker下生成的dump文件

    最近公司预生产环境.net core应用的docker容器经常出现内存暴涨现象,有时会突然吃掉几个G,触发监控预警,造成容器重启. 分析了各种可能原因,修复了可能发生的内存泄露,经测试本地正常,但是发 ...

  9. vs 自动生成core dump文件

    一直以来觉着core dump这个东西很神奇,在初步学习的时候也没有个大方向,最近项目需要记录程序崩溃时的日志信息,因此在网上寻找相关的信息,此时core dump也成为了我重点关注的东西. 说说我的 ...

随机推荐

  1. 【bzoj4542】[Hnoi2016]大数 莫队算法

    题目描述 给出一个数字串,多次询问一段区间有多少个子区间对应的数为P的倍数.其中P为质数. 输入 第一行一个整数:P.第二行一个串:S.第三行一个整数:M.接下来M行,每行两个整数 fr,to,表示对 ...

  2. 2018九省联考(SHOI2018)

    听说在退役前还能有去外省的机会QAQ D1 9点T1,T2过拍,感觉自己稳得一批,然后边看T3边幻想AK 事实证明我是多么菜多么无知多么傻逼 想T3时太浮躁,最后也没想出来 T2根本没有想过去怀疑自己 ...

  3. Mac连接HDMI后没有声音

    Mac连接HDMI后,会自动切换到HDMI设备进行发声,若HDMI设备没有声音,则不会发声.必须切换回内置扬声器才能有声音,或者拔出HDMI设备. 系统偏好设置 -  声音 - 输出 - 选择内置扬声 ...

  4. git使用经验(一)

    在使用Git Push代码到数据仓库时,提示如下错误: [remote rejected] master -> master (branch is currently checked out) ...

  5. 《剑指offer》— JavaScript(8)跳台阶

    跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 实现代码 function jumpFloor(number) { if (number& ...

  6. 《JavaScript高级程序设计(第三版)》-3

    相等操作符 相等和不相等 在转换不同的数据类型时,相等和不想等操作符遵循下面基本规则: 如果有一个操作符数是布尔值,则在比较相等性之前先将其转换为数值——false转换为0,而true转换为1: 如果 ...

  7. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C 并查集

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. 如何将下载的web工程导入到eclipse中使用

    如果你是喜欢编程的,在你的开发工具中一定有许多项目,就像小编一样(PS:小编只想默默地装一X):   我们选中其中的一个项目,然后[Ctrl + C]复制,再[Ctrl + V]粘贴到桌面:   那么 ...

  9. ECONNRESET和WSAECONNRESET怎么产生的以及如何避免

    ECONNRESET是linux环境网络编程产生的错误,错误码为104, WSAECONNRESET是windows环境网络编程产生的错误,错误码为10054 两者产生的原因都一样,分以下几种情况: ...

  10. JSTL获取当日时间与数据时间比较

    <jsp:useBean id="now" class="java.util.Date" /> <fmt:formatDate value=& ...