新聞中心
1. core文件的簡(jiǎn)單介紹

成都創(chuàng)新互聯(lián)自2013年起,先為連云等服務(wù)建站,連云等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為連云企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
我們經(jīng)常聽(tīng)到大家說(shuō)到程序core掉了,需要定位解決,這里說(shuō)的大部分是指對(duì)應(yīng)程序由于各種異?;蛘遙ug導(dǎo)致在運(yùn)行過(guò)程中異常退出或者中止,并且在滿(mǎn)足一定條件下(這里為什么說(shuō)需要滿(mǎn)足一定的條件呢?下面會(huì)分析)會(huì)產(chǎn)生一個(gè)叫做core的文件。
通常情況下,core文件會(huì)包含了程序運(yùn)行時(shí)的內(nèi)存,寄存器狀態(tài),堆棧指針,內(nèi)存管理信息還有各種函數(shù)調(diào)用堆棧信息等,我們可以理解為是程序工作當(dāng)前狀態(tài)存儲(chǔ)生成第一個(gè)文件,許多的程序出錯(cuò)的時(shí)候都會(huì)產(chǎn)生一個(gè)core文件,通過(guò)工具分析這個(gè)文件,我們可以定位到程序異常退出的時(shí)候?qū)?yīng)的堆棧調(diào)用等信息,找出問(wèn)題所在并進(jìn)行及時(shí)解決。
2. 開(kāi)啟或關(guān)閉core文件的生成用以下命令來(lái)阻止系統(tǒng)生成core文件:ulimit -c 0下面的命令可以檢查生成core文件的選項(xiàng)是否打開(kāi):ulimit -a該命令將顯示所有的用戶(hù)定制,其中選項(xiàng)-a代表“all”。
也可以修改系統(tǒng)文件來(lái)調(diào)整core選項(xiàng)在/etc/profile通常會(huì)有這樣一句話(huà)來(lái)禁止產(chǎn)生core文件,通常這種設(shè)置是合理的:# No core files by defaultulimit -S -c 0 > /dev/null 2>&1但是在開(kāi)發(fā)過(guò)程中有時(shí)為了調(diào)試問(wèn)題,還是需要在特定的用戶(hù)環(huán)境下打開(kāi)core文件產(chǎn)生的設(shè)置在用戶(hù)的~/.bash_profile里加上ulimit -c unlimited來(lái)讓特定的用戶(hù)可以產(chǎn)生core文件如果ulimit -c 0 則也是禁止產(chǎn)生core文件,而ulimit -c 1024則限制產(chǎn)生的core文件的大小不能超過(guò)1024kb
3. 設(shè)置Core Dump的核心轉(zhuǎn)儲(chǔ)文件目錄和命名規(guī)則/proc/sys/kernel/core_uses_pid可以控制產(chǎn)生的core文件的文件名中是否添加pid作為擴(kuò)展,如果添加則文件內(nèi)容為1,否則為0/proc/sys/kernel/core_pattern可以設(shè)置格式化的core文件保存位置或文件名,比如原來(lái)文件內(nèi)容是core-%e可以這樣修改:echo “/corefile/core-%e-%p-%t” > /proc/sys/kernel/core_pattern將會(huì)控制所產(chǎn)生的core文件會(huì)存放到/corefile目錄下,產(chǎn)生的文件名為core-命令名-pid-時(shí)間戳以下是參數(shù)列表: %p – insert pid into filename 添加pid %u – insert current uid into filename 添加當(dāng)前uid %g – insert current gid into filename 添加當(dāng)前gid %s – insert signal that caused the coredump into the filename 添加導(dǎo)致產(chǎn)生core的信號(hào) %t – insert UNIX time that the coredump occurred into filename 添加core文件生成時(shí)的unix時(shí)間 %h – insert hostname where the coredump happened into filename 添加主機(jī)名 %e – insert coredumping executable name into filename 添加命令名
4. 使用core文件在core文件所在目錄下鍵入:gdb -c core它會(huì)啟動(dòng)GNU的調(diào)試器,來(lái)調(diào)試core文件,并且會(huì)顯示生成此core文件的程序名,中止此程序的信號(hào)等等如果你已經(jīng)知道是由什么程序生成此core文件的,比如MyServer崩潰了生成core.12345,那么用此指令調(diào)試:gdb -c core MyServer以下怎么辦就該去學(xué)習(xí)gdb的使用了
5. 一個(gè)小方法來(lái)測(cè)試產(chǎn)生core文件直接輸入指令:kill -s SIGSEGV $$
6. **為何有時(shí)程序**Down了,卻沒(méi)生成 **Core**文件。
Linux下,有一些設(shè)置,標(biāo)明了resources available to the shell and to processes。 可以使用
#ulimit -a 來(lái)看這些設(shè)置。 (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)
如果這個(gè)值為0,則無(wú)法生成core文件。所以可以使用:
#ulimit -c 1024 或者 #ulimit -c unlimited 來(lái)使能 core文件。
如果程序出錯(cuò)時(shí)生成Core 文件,則會(huì)顯示Segmentation fault (core dumped)。
**7. Core Dump**的核心轉(zhuǎn)儲(chǔ)文件目錄和命名規(guī)則:/proc/sys/kernel/core_uses_pid可以控制產(chǎn)生的core文件的文件名中是否添加pid作為擴(kuò)展,如果添加則文件內(nèi)容為1,否則為0
本文標(biāo)題:詳解Linuxcore文件
鏈接地址:http://fisionsoft.com.cn/article/dphoije.html


咨詢(xún)
建站咨詢(xún)
