Redhat Linux下如何產生core dump檔案 在Redhat Linux系統中預設是不產生core dump檔案的,這是因為在/etc/profile檔案中有這樣一行 ulimit -S -c 0 > /dev/null 2>&1 如何開啟core dump呢?最簡單的方法是使用者在自己的~/.bash_profile中加入ulimit -S -c unlimited > /dev/null 2>&1,這樣設定後允許目前使用者產生沒有大小限制的core dump檔案。此外還有兩種系統級修改產生core dump的方法。 第一種方法是修改/etc/profile,把ulimit那一行改為 ulimit -S -c unlimited > /dev/null 2>&1 這樣設定後系統允許所有使用者產生沒有大小限制的core dump檔案。這樣做的優點是不需要重起系統,缺點是無法控制只讓某些使用者產生core dump檔案。 第二種方法是修改/etc/security/limits.conf檔案。很多系統上限都可以通過修改這個檔案改變,如最大子進程個數,最大開啟檔案數等等。這個檔案有詳細的注釋,對如何修改這個檔案做了說明。如果想對所有使用者開啟core dump,可以加入一行 * soft core 0 如果只想對某些使用者或使用者組開啟core dump,可以加入 user soft core 0或@group soft core 0 注意如果通過修改/etc/security/limits.conf檔案開啟core dump,還需要注釋掉/etc/profile中的ulmit那一行 #ulimit -S -c 0 > /dev/null 2>&1 這樣修改的優點是可以針對特定使用者或特定組開啟core dump檔案,缺點是需要重起系統。 最後說一下產生core dump檔案的位置,預設位置與可執行程式在同一目錄下,檔案名稱是core.***,其中***是一個數字。core dump檔案名稱的模式儲存在/proc/sys/kernel/core_pattern中,預設值是core。通過以下命令可以更改core dump檔案的位置(如希望產生到/tmp/cores目錄下) echo "/tmp/cores/core" > /proc/sys/kernel/core_pattern core dump(核心轉儲) 的使用本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chaoi/archive/2007/07/16/1693149.aspx在Unix系統下,應用程式崩潰,一般會產生core檔案,如何根據core檔案尋找問題的所在,並做相應的分析和調試,是非常重要的,本文對此做簡單介紹。 例如,一個程式cmm_test_tool在啟動並執行時候發生了錯誤,並產生了一個core檔案,如下: -rw-r–r– 1 root cmm_test_tool.c-rw-r–r– 1 root cmm_test_tool.o-rwxr-xr-x 1 root cmm_test_tool-rw——- 1 root core.19344-rw——- 1 root core.19351-rw-r–r– 1 root cmm_test_tool.cfg-rw-r–r– 1 root cmm_test_tool.res-rw-r–r– 1 root cmm_test_tool.log[root@AUTOTEST_SIM2 mam2cm]# 就可以利用命令gdb進行尋找,參數一是應用程式的名稱,參數二是core檔案,運行gdb cmm_test_tool core.19344結果如下: [root@AUTOTEST_SIM2 mam2cm]# gdb cmm_test_tool core.19344GNU gdb Red Hat Linux (5.2.1-4)Copyright 2002 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type “show copying” to see the conditions.There is absolutely no warranty for GDB. Type “show warranty” for details.This GDB was configured as “i386-redhat-linux”…Core was generated by `./cmm_test_tool’.Program terminated with signal 11, Segmentation fault.Reading symbols from /lib/i686/libpthread.so.0…done.Loaded symbols for /lib/i686/libpthread.so.0Reading symbols from /lib/i686/libm.so.6…done.Loaded symbols for /lib/i686/libm.so.6Reading symbols from /usr/lib/libz.so.1…done.Loaded symbols for /usr/lib/libz.so.1Reading symbols from /usr/lib/libstdc++.so.5…done.Loaded symbols for /usr/lib/libstdc++.so.5Reading symbols from /lib/i686/libc.so.6…done.Loaded symbols for /lib/i686/libc.so.6Reading symbols from /lib/libgcc_s.so.1…done.Loaded symbols for /lib/libgcc_s.so.1Reading symbols from /lib/ld-linux.so.2…done.Loaded symbols for /lib/ld-linux.so.2Reading symbols from /lib/libnss_files.so.2…done.Loaded symbols for /lib/libnss_files.so.2#0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6(gdb) 進入gdb提示符,輸入where,找到錯誤發生的位置和堆棧,如下: (gdb) where#0 0×4202cec1 in __strtoul_internal () from /lib/i686/libc.so.6#1 0×4202d4e7 in strtoul () from /lib/i686/libc.so.6#2 0×0804b4da in GetMaxIDFromDB (get_type=2, max_id=0×806fd20) at cmm_test_tool.c:788#3 0×0804b9d7 in ConstrctVODProgram (vod_program=0×40345bdc) at cmm_test_tool.c:946#4 0×0804a2f4 in TVRequestThread (arg=0×0) at cmm_test_tool.c:372#5 0×40021941 in pthread_start_thread () from /lib/i686/libpthread.so.0(gdb) 至此,可以看出檔案出錯的位置是函數 GetMaxIDFromDB ,兩個參數分別是2和0×806fd20,這個函數位於原始碼的788行,基於此,我們就可以有針對性的找到問題的根源,並加以解決。 linux c Segmentation faultTo debug segmentation fault, please following steps: > ulimit –c unlimited # run test image > ./a.out # Segmenation fault happened. You will see the file “core.xxxx” (xxxx is pid) in the directory > gdb a.out core.xxxx > (gdb) bt # Then you will see the segmentation fault point at the File xxxx.cpp : Line xxxx. 舉個例子,我在fedora 下面運行這個代碼的, 檔案名稱叫做test.c #include int main() { char *p = NULL; *p = 10; } 1.編譯一下 gcc -ggdb test.c 2.輸入命令 ulimit -c unlimited 3.運行檔案 ./a.out 發生segmentation falut,同時會產生一個檔案core.xxxx(xxxx means pid) 4.gdb a.out core.xxxx 5.gdb > bt 馬上就會輸出錯誤碼所在的檔案和行數,同時還列印出這句錯誤的語句。Cited from:http://blog.tianya.cn/blogger/post_show.asp?BlogID=420361&PostID=7613747 backtrace函數與堆棧[b]linux[/b][b]下追蹤函數呼叫堆疊[/b]一般察看函數運行時堆棧的方法是使用GDB之類的外部調試器,但是,有些時候為了剖析器的BUG,(主要針對長時間運行程式的分析),在程式出錯時列印出函數的呼叫堆疊是非常有用的。在標頭檔"execinfo.h"中聲明了三個函數用於擷取當前線程的函數呼叫堆疊Function: int backtrace(void **buffer,int size)該函數用與擷取當前線程的呼叫堆疊,擷取的資訊將會被存放在buffer中,它是一個指標列表。參數 size 用來指定buffer中可以儲存多少個void* 元素。函數傳回值是實際擷取的指標個數,最大不超過size大小在buffer中的指標實際是從堆棧中擷取的返回地址,每一個堆棧架構有一個返回地址注意某些編譯器的最佳化選項對擷取正確的呼叫堆疊有幹擾,另外內嵌函式沒有堆棧架構;刪除架構指標也會使無法正確解析堆棧內容Function: char ** backtrace_symbols (void *const *buffer, int size)backtrace_symbols將從backtrace函數擷取的資訊轉化為一個字串數組. 參數buffer應該是從backtrace函數擷取的數組指標,size是該數組中的元素個數(backtrace的傳回值) 函數傳回值是一個指向字串數組的指標,它的大小同buffer相同.每個字串包含了一個相對於buffer中對應元素的可列印資訊.它包括函數名,函數的位移地址,和實際的返回地址現在,只有使用ELF二進位格式的程式和苦衷才能擷取函數名稱和位移地址.在其他系統,只有16進位的返回地址能被擷取.另外,你可能需要傳遞相應的標誌給連結器,以能支援函數名功能(比如,在使用GNU ld的系統中,你需要傳遞(-rdynamic))該函數的傳回值是通過malloc函數申請的空間,因此調用這必須使用free函數來釋放指標.注意:如果不能為字串擷取足夠的空間函數的傳回值將會為NULLFunction:void backtrace_symbols_fd (void *const *buffer, int size, int fd)backtrace_symbols_fd 與backtrace_symbols 函數具有相同的功能,不同的是它不會給調用者返回字串數組,而是將結果寫入檔案描述符為fd的檔案中,每個函數對應一行.它不需要調用malloc函 數,因此適用於有可能調用該函數會失敗的情況下面的例子顯示了這三個函數的用法 #include #include #include /* Obtain a backtrace and print it to stdout. */voidprint_trace (void){void *array[10];size_t size;char **strings;size_t i;size = backtrace (array, 10);strings = backtrace_symbols (array, size);printf ("Obtained %zd stack frames.\n", size);for (i = 0; i [i]);free (strings);}/* A dummy function to make the backtrace more interesting. */voiddummy_function (void){print_trace ();}intmain (void){dummy_function ();return 0;}[/i][i]備忘:void *const *buffer -- buffer指向char類型的常量指標的指標(很是拗口)[/i][b]善用[/b][b]backtrace[/b][b]解決大問題[/b][b]([/b][b]轉[/b][b])[/b]程式在得到一個Segmentation fault這樣的錯誤資訊毫無保留地就跳出來了,遇到這樣的問題讓人很痛苦,尋找問題不亞於你N多天辛苦勞累編寫代碼的難度。那麼有沒有更好的方法可以在產生SIGSEGV訊號的時候得到調試可用的資訊呢?看看下面的常式吧!sigsegv.h#ifndef __sigsegv_h__#define __sigsegv_h__#ifdef __cplusplusextern "C" {#endif int setup_sigsegv();#ifdef __cplusplus}#endif#endif /* __sigsegv_h__ */sigsegv.c#define _GNU_SOURCE#include #include #include #include #include #include #include #ifndef NO_CPP_DEMANGLE#include #endif#if defined(REG_RIP)# define SIGSEGV_STACK_IA64# define REGFORMAT "%016lx"#elif defined(REG_EIP)# define SIGSEGV_STACK_X86# define REGFORMAT "%08x"#else# define SIGSEGV_STACK_GENERIC# define REGFORMAT "%x"#endifstatic void signal_segv(int signum, siginfo_t* info, void*ptr) { static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"}; size_t i; ucontext_t *ucontext = (ucontext_t*)ptr;#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64) int f = 0; Dl_info dlinfo; void **bp = 0; void *ip = 0;#else void *bt[20]; char **strings; size_t sz;#endif fprintf(stderr, "Segmentation Fault!\n"); fprintf(stderr, "info.si_signo = %d\n", signum); fprintf(stderr, "info.si_errno = %d\n", info->si_errno); fprintf(stderr, "info.si_code = %d (%s)\n", info->si_code, si_codes[info->si_code]); fprintf(stderr, "info.si_addr = %p\n", info->si_addr); for(i = 0; i fprintf(stderr, "reg[%02d] = 0x" REGFORMAT "\n", i, ucontext->uc_mcontext.gregs[i]);#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)# if defined(SIGSEGV_STACK_IA64) ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP]; bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];# elif defined(SIGSEGV_STACK_X86) ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP]; bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];# endif fprintf(stderr, "Stack trace:\n"); while(bp && ip) { if(!dladdr(ip, &dlinfo)) break; const char *symname = dlinfo.dli_sname;#ifndef NO_CPP_DEMANGLE int status; char *tmp = __cxa_demangle(symname, NULL, 0, &status); if(status == 0 && tmp) symname = tmp;#endif fprintf(stderr, "% 2d: %p (%s)\n", ++f, ip, symname, (unsigned)(ip - dlinfo.dli_saddr), dlinfo.dli_fname);#ifndef NO_CPP_DEMANGLE if(tmp) free(tmp);#endif if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main")) break; ip = bp[1]; bp = (void**)bp[0]; }#else fprintf(stderr, "Stack trace (non-dedicated):\n"); sz = backtrace(bt, 20); strings = backtrace_symbols(bt, sz); for(i = 0; i fprintf(stderr, "%s\n", strings[i]);#endif fprintf(stderr, "End of stack trace\n"); exit (-1);}int setup_sigsegv() { struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_sigaction = signal_segv; action.sa_flags = SA_SIGINFO; if(sigaction(SIGSEGV, &action, NULL) perror("sigaction"); return 0; } return 1;}#ifndef SIGSEGV_NO_AUTO_INITstatic void __attribute((constructor)) init(void) { setup_sigsegv();}#endifmain.c#include "sigsegv.h"#include int die() { char *err = NULL; strcpy(err, "gonner"); return 0;}int main() { return die();}下面來編譯上面的main.c程式看看將會產生什麼樣的資訊呢,不過要注意的就是如果要在你的程式裡引用sigsegv.h、sigsegv.c得到堆棧資訊的話記得加上-rdynamic -ldl參數。/data/codes/c/test/backtraces $ gcc -o test -rdynamic -ldl -ggdb -g sigsegv.c main.c/data/codes/c/test/backtraces $ ./testSegmentation Fault!info.si_signo = 11info.si_errno = 0info.si_code = 1 (SEGV_MAPERR)info.si_addr = (nil)reg[00] = 0x00000033reg[01] = 0x00000000reg[02] = 0xc010007breg[03] = 0x0000007breg[04] = 0x00000000reg[05] = 0xb7fc8ca0reg[06] = 0xbff04c2creg[07] = 0xbff04c1creg[08] = 0xb7f8cff4reg[09] = 0x00000001reg[10] = 0xbff04c50reg[11] = 0x00000000reg[12] = 0x0000000ereg[13] = 0x00000006reg[14] = 0x080489ecreg[15] = 0x00000073reg[16] = 0x00010282reg[17] = 0xbff04c1creg[18] = 0x0000007bStack trace:1: 0x80489ec (/data/codes/c/test/backtraces/test)2: 0x8048a16 (/data/codes/c/test/backtraces/test)End of stack trace/data/codes/c/test/backtraces $下面用gdb來看看出錯的地方左右的代碼:/data/codes/c/test/backtraces $ gdb ./testgdb> disassemble die+16Dump of assembler code for function die:0x080489dc : push %ebp0x080489dd : mov %esp,%ebp0x080489df : sub $0x10,%esp0x080489e2 : movl $0x0,0xfffffffc(%ebp)0x080489e9 : mov 0xfffffffc(%ebp),%eax0x080489ec : movl $0x6e6e6f67,(%eax)0x080489f2 : movw $0x7265,0x4(%eax)0x080489f8 : movb $0x0,0x6(%eax)0x080489fc : mov $0x0,%eax0x08048a01 : leave 0x08048a02 : ret End of assembler dump.gdb>也可以直接break *die+16進行調試,看看在出錯之前的堆棧情況,那麼下面我們再來看看代碼問題到底出在什麼地方了。/data/codes/c/test/backtraces $ gdb ./testgdb> break *die+16Breakpoint 1 at 0x80489f2: file main.c, line 6.gdb> list *die+160x80489f2 is in die (main.c:6).1 #include "sigsegv.h"2 #include 3 4 int die() {5 char *err = NULL;6 strcpy(err, "gonner");7 return 0;8 }9 10 int main() {gdb>現 在看看定位錯誤將會多麼方便,上面的調試指令中list之前break不是必須的,只是讓你可以看到break其實就已經指出了哪一行代碼導致 Segmentation fault了。如果你要發布你的程式你一般會為了減少體積不會附帶調試資訊的(也就是不加-ggdb -g參數),不過沒關係,你一樣可以得到上面stack-trace資訊,然後你調試之前只要加上調試資訊即可。