Segment fault及LINUX core dump详解
Posted 莫水千流
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Segment fault及LINUX core dump详解相关的知识,希望对你有一定的参考价值。
源自:http://andyniu.iteye.com/blog/1965571
core dump的概念:
A 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文件生成的方法:
如何产生Core Dump
发生doredump一般都是在进程收到某个信号的时候,Linux上现在大概有60多个信号,可以使用 kill -l 命令全部列出来。
)SIGQUIT )SIGILL )SIGABRT )SIGFPE )SIGSEGV )SIGBUS )SIGSYS
)SIGTRAP )SIGXCPU )SIGXFSZ )SIGIOT
我们看到SIGSEGV在其中,一般数组越界或是访问空指针都会产生这个信号。另外虽然默认是这样的,但是你也可以写自己的信号处理函数改变默认行为,更多信号相关可以看参考链接33。
上述内容只是产生coredump的必要条件,而非充分条件。要产生core文件还依赖于程序运行的shell,可以通过ulimit -a命令查看,输出内容大致如下:
看到第一行了吧,core file size,这个值用来限制产生的core文件大小,超过这个值就不会保存了。我这里输出是0,也就是不会保存core文件,即使产生了,也保存不下来==! 要改变这个设置,可以使用ulimit -c unlimited。
OK, 现在万事具备,只缺一个能产生Core的程序了,介个对C程序员来说太容易了。
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">#include <stdlib.h>;</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(153,51,51); vertical-align:baseline; background-color:transparent">int</span> crash<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">(</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">)</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">{</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(153,51,51); vertical-align:baseline; background-color:transparent">char</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">*</span>xxx <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">=</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(255,0,0); vertical-align:baseline; background-color:transparent">"crash!!"</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">;</span>
- xxx<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">[</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,0,221); vertical-align:baseline; background-color:transparent">1</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">]</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">=</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(255,0,0); vertical-align:baseline; background-color:transparent">‘D‘</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">;</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(102,102,102); font-style:italic; vertical-align:baseline; background-color:transparent">// 写只读存储区!</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(177,177,0); vertical-align:baseline; background-color:transparent">return</span> <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,0,221); vertical-align:baseline; background-color:transparent">2</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">;</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">}</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(153,51,51); vertical-align:baseline; background-color:transparent">int</span> foo<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">(</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">)</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">{</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(177,177,0); vertical-align:baseline; background-color:transparent">return</span> crash<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">(</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">)</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">;</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">}</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(153,51,51); vertical-align:baseline; background-color:transparent">int</span> main<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">(</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">)</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">{</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(177,177,0); vertical-align:baseline; background-color:transparent">return</span> foo<span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">(</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">)</span><span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(51,153,51); vertical-align:baseline; background-color:transparent">;</span>
- <span style="margin:0px; padding:0px; border:0px currentColor; color:rgb(0,153,0); vertical-align:baseline; background-color:transparent">}</span>
上手调试
2
4
6
8
10
12
|
#include <stdio.h> { *p = 0;
{ func(NULL);
return
0; } |
Segmentation fault (core dumped)
#0 0x080483ba in func ()
如何定位到行?
#0 0x080483ba in func (p=0x0) at a.c:5
5 *p = 0;
上边的程序编译的时候有一点需要注意,需要带上参数-g, 这样生成的可执行程序中会带上足够的调试信息。编译运行之后你就应该能看见期待已久的“Segment Fault(core dumped)”或是“段错误 (核心已转储)”之类的字眼了。看看当前目录下是不是有个core或是core.xxx的文件。祭出linux下经典的调试器GDB,首先带着core文件载入程序:gdb exefile core,这里需要注意的这个core文件必须是exefile产生的,否则符号表会对不上。载入之后大概是这个样子的:
我们看到已经能直接定位到出core的地方了,在第8行写了一个只读的内存区域导致触发Segment Fault信号。在载入core的时候有个小技巧,如果你事先不知道这个core文件是由哪个程序产生的,你可以先随便找个代替一下,比如/usr/bin/w就是不错的选择。比如我们采用这种方法载入上边产生的core,gdb会有类似的输出:
-
-
-
-
-
-
-
-
-
-
-
- 打印堆栈信息:bt
以上是关于Segment fault及LINUX core dump详解的主要内容,如果未能解决你的问题,请参考以下文章
Z段错误Segment Fault定位,即core dump文件与gdb定位
你好,请问linux下程序编译遇到的那种segment fault你怎么解决的啊 看他们说的那个#objdump -D XXX >debug