天天看點

linux下生成core dump檔案方法及設定源自:http://andyniu.iteye.com/blog/1965571

源自: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檔案生成的方法:

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 指令全部列出來。

[email protected]:~$ 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指令檢視,輸出内容大緻如下:

[email protected]:~$ 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程式員來說太容易了。

[c] view plain copy

  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

[email protected]:~# ./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産生的,否則符号表會對不上。載入之後大概是這個樣子的:

[email protected]:~$ 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會有類似的輸出:

[email protected]:~$ 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

參考:https://blog.csdn.net/mrjy1475726263/article/details/44116289/

繼續閱讀