core dump 相關保存(轉自http://blog.sina.com.cn/s/blog_602f87700100ew04.html)

1. 什麼是Core:

Sam之前一直以爲Core Dump中Core是 Linux Kernel的意思. 今天才發現在這裏,Core是另一種意思:

在使用半導體作爲內存的材料前,人類是利用線圈當作內存的材料(發明者爲王安),線圈就叫作 core ,用線圈做的內存就叫作 core memory。如今 ,半導體工業澎勃發展,已經沒有人用 core memory 了,不過,在許多情況下, 人們還是把記憶體叫作 core 。

 

2. 什麼是Core Dump:

我們在開發(或使用)一個程序時,最怕的就是程序莫明其妙地當掉。雖然系統沒事,但我們下次仍可能遇到相同的問題。於是這時操作系統就會把程序當掉 時的內存內容 dump 出來(現在通常是寫在一個叫 core 的 file 裏面),讓 我們或是 debugger 做爲參考。這個動作就叫作 core dump。

 

3. Core Dump時會生成何種文件:

Core Dump時,會生成諸如 core.進程號 的文件。

 

4. 爲何有時程序Down了,卻沒生成 Core文件。

Linux下,有一些設置,標明瞭resources available to the shell and to processes。 可以使用

#ulimit -a 來看這些設置。 (ulimit是bash built-in Command)

              -a     All current limits are reported
              -c     The maximum size of core files created
              -d     The maximum size of a process鈥檚 data segment
              -e     The maximum scheduling priority ("nice")
              -f     The maximum size of files written by the shell and its children
              -i     The maximum number of pending signals
              -l     The maximum size that may be locked into memory
              -m     The maximum resident set size (has no effect on Linux)
              -n     The maximum number of open file descriptors (most systems do not allow this value to be set)
              -p     The pipe size in 512-byte blocks (this may not be set)
              -q     The maximum number of bytes in POSIX message queues
              -r     The maximum real-time scheduling priority
              -s     The maximum stack size
              -t     The maximum amount of cpu time in seconds
              -u     The maximum number of processes available to a single user
              -v     The maximum amount of virtual memory available to the shell
              -x     The maximum number of file locks

 從這裏可以看出,如果 -c是顯示:core file size          (blocks, -c)

如果這個值爲0,則無法生成core文件。所以可以使用:

#ulimit -c 1024  或者 #ulimit -c unlimited  來使能 core文件。

如果程序出錯時生成Core 文件,則會顯示Segmentation fault (core dumped)

 

5. Core Dump的核心轉儲文件目錄和命名規則:
/proc/sys/kernel/core_uses_pid可以控制產生的core文件的文件名中是否添加pid作爲擴展,如果添加則文件內容爲1,否則爲0

 

 

6. 如何使用Core文件:

在Linux下,使用:

#gdb -c core.pid program_name

就可以進入gdb模式。

 

輸入where,就可以指出是在哪一行被Down掉,哪個function內,由誰調用等等。

(gdb) where

 

或者輸入 bt。

(gdb) bt

 

7. 如何讓一個正常的程序down:

#kill -s SIGSEGV pid

 

 

8. 察看Core文件輸出在何處:

存放Coredump的目錄即進程的當前目錄,一般就是當初發出命令啓動該進程時所在的目錄。但如果是通過腳本啓動,則腳本可能會修改當前目錄,這時進程真正的當前目錄就會與當初執行腳本所在目錄不同。這時可以查看”/proc/<進程pid>/cwd“符號鏈接的目標來確定進程真正的當前目錄地址。通過系統服務啓動的進程也可通過這一方法查看。

 

9. 嵌入式設備下如何使用Core dump:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章