Linux系統開啟core dump的配置,以及用最短程式拋出core

來源:互聯網
上載者:User
什麼是core dump

core dump又叫核心轉儲, 當程式運行過程中發生異常, 程式異常退出時, 由作業系統把程式當前的記憶體狀況儲存在一個core檔案中, 叫core dump。core dump在應用crash掉之後對問題的診斷是很有協助的。而在預設安裝的時候core dump是關閉狀態的。

如何查看系統是否開啟了core dump

使用【ulimit -c】查看core dump是否開啟。如果結果為0,則表示此功能處於關閉狀態,不會產生core檔案

如何開啟core dump

方法一:命令列方式【ulimit -c 1024】,在這個例子中開啟了core dump 同時限制檔案大小為1024k,現在的程式佔用記憶體都比較兇猛,以前寫C程式需要計算記憶體的時代已經過去了。如果不加限制,可能一個core檔案,幾個G就出去了~,當然沒有限制的方式還是有的【ulimit -c unlimited】

方法二:配置profile檔案,開啟/etc/profile檔案,在裡面可以找到【ulimit -S -c 0 > /dev/null 2>&1】,將它改成【ulimit -S -c unlimited > /dev/null 2>&1】
方法三:修改/etc/security/limits.conf檔案,添加【* soft core 0】,這個方法可以針對指定使用者或使用者組開啟core dump【user soft core 0或@group soft core 0】。不過要使用這個方法一定要將方法二提到的那行注釋掉,不可同時存在

如何查看core檔案的儲存路徑和檔案名稱格式

預設情況下,在開啟core後,如果應用發生crash,那麼會在應用所在位置,產生一個core.【應用pid】的檔案,檔案名稱的可讀性不高,管理也不方便。
查看正在使用的core檔案路徑和格式【more /proc/sys/kernel/core_pattern】
後面自動添加pid的配置是在【more /proc/sys/kernel/core_uses_pid】裡面配置的,如果為1就是自動添加

如何修改core檔案的儲存路徑和檔案名稱格式

修改/etc/sysctl.conf檔案【vi /etc/sysctl.conf】,添加需要儲存的路徑【kernel.core_pattern = /tmp/corefile/core.%e.%t】,需要注意的是該路徑必須應用有寫的許可權,不然core檔案是不會產生的。再執行命令【sysctl -p】即可生效。關於core_users_pid預設在sysctl檔案裡面已經存在,不需要更改,pid還是很重要的資訊。

附上core檔案支援的格式列表:
%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 【應用的名字】

如何使用測試程式產生的core

#include <iostream>using namespace std;int main() {    *(char *)1=1;    return 0;}  

 

自:  http://blog.csdn.net/shijun_zhang/article/details/6577604#

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.