• <ul id="mayc0"></ul>
    <ul id="mayc0"><center id="mayc0"></center></ul>
    <strike id="mayc0"><input id="mayc0"></input></strike>
    <ul id="mayc0"></ul>
  • 始創(chuàng)于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊有禮 登錄
    • 掛牌上市企業(yè)
    • 60秒人工響應
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補償
    全部產(chǎn)品
    您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

    Linux下發(fā)生段錯誤時如何產(chǎn)生core文件

    發(fā)布時間:  2012/8/10 10:27:45
     Linux下的C程序常常會因為內(nèi)存訪問錯誤等原因造成segment fault(段錯誤),此時如果系統(tǒng)core dump功能是打開的,那么將會有內(nèi)存映像轉(zhuǎn)儲到硬盤上來,之后可以用gdb對core文件進行分析,還原系統(tǒng)發(fā)生段錯誤時刻的堆棧情況。這對于我們發(fā)現(xiàn)程序bug很有幫助。

      使用ulimit -a可以查看系統(tǒng)core文件的大小限制;使用ulimit -c [kbytes]可以設置系統(tǒng)允許生成的core文件大小,例如

      ulimit -c 0 不產(chǎn)生core文件

      ulimit -c 100 設置core文件最大為100k

      ulimit -c unlimited 不限制core文件大小

      先看一段會造成段錯誤的程序:

    #include <stdio.h>

    int main()

    {

    char *ptr="linuxers.cn";

    *ptr=0;

    }

    編譯運行后結果如下:

    [leconte@localhost test]$ gcc -g -o test a.c

    [leconte@localhost test]$ ./test

    段錯誤

    此時并沒有產(chǎn)生core文件,接下來使用ulimit -c設置core文件大小為無限制,再執(zhí)行./test程序,結果如下:

    [leconte@localhost ~]$ ulimit -a

    core file size (blocks, -c) 0

    [leconte@localhost test]$ ulimit -c unlimited

    [leconte@localhost test]$ ulimit -a

    core file size (blocks, -c) unlimited

    [leconte@localhost test]$ ./test

    段錯誤 (core dumped)

    [leconte@localhost test]$ ls -al core.*

    -rw------- 1 leconte leconte 139264 01-06 22:31 core.2065

    可見core文件已經(jīng)生成,接下來可以用gdb分析,查看堆棧情況:

    [leconte@localhost test]$ gdb ./test core.2065

    GNU gdb Fedora (6.8-27.el5)

    Copyright (C) 2008 Free Software Foundation, Inc.

    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

    This is free software: you are free to change and redistribute it.

    There is NO WARRANTY, to the extent permitted by law. Type "show copying"

    and "show warranty" for details.

    This GDB was configured as "i386-redhat-linux-gnu"...

    warning: exec file is newer than core file.

    warning: Can't read pathname for load map: Input/output error.

    Reading symbols from /lib/libc.so.6...done.

    Loaded symbols for /lib/libc.so.6

    Reading symbols from /lib/ld-linux.so.2...done.

    Loaded symbols for /lib/ld-linux.so.2

    Core was generated by `./test'.

    Program terminated with signal 11, Segmentation fault.

    [New process 2065]

    #0 0x0804836f in main () at a.c:6

    6 *ptr=0;

      從上述輸出可以清楚的看到,段錯誤出現(xiàn)在a.c的第6行,問題已經(jīng)清晰地定位到了。

      很多系統(tǒng)默認的core文件大小都是0,我們可以通過在shell的啟動腳本/etc/bashrc或者~/.bashrc等地方來加入 ulimit -c 命令來指定core文件大小,從而確保core文件能夠生成。

      除此之外,還可以在/proc/sys/kernel/core_pattern里設置core文件的文件名模板,詳情請看core的官方man手冊


    本文出自:億恩科技【www.vbseamall.com】

    服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區(qū)翠竹街1號總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號
      0
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線