<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Linux下發(fā)生段錯誤時(shí)如何產(chǎn)生core文件

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

作者: 時(shí)間:2010-02-06 來(lái)源:網(wǎng)絡(luò ) 收藏
  下的C程序常常會(huì )因為內存訪(fǎng)問(wèn)錯誤等原因造成segment fault(段錯誤),此時(shí)如果系統core dump功能是打開(kāi)的,那么將會(huì )有內存映像轉儲到硬盤(pán)上來(lái),之后可以用gdb對進(jìn)行分析,還原系統發(fā)生段錯誤時(shí)刻的堆棧情況。這對于我們發(fā)現程序bug很有幫助。

  使用ulimit -a可以查看系統的大小限制;使用ulimit -c [kbytes]可以設置系統允許生成的大小,例如

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

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

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

  先看一段會(huì )造成段錯誤的程序:

  #include

  int main()

  {

  char *ptr=linuxers.cn;

  *ptr=0;

  }

  編譯運行后結果如下:

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

  [leconte@localhost test]$ ./test

  段錯誤

  此時(shí)并沒(méi)有產(chǎn)生core文件,接下來(lái)使用ulimit -c設置core文件大小為無(wú)限制,再執行./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

  可見(jiàn)core文件已經(jīng)生成,接下來(lái)可以用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

  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;

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

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

  除此之外,還可以在/proc/sys/kernel/core_pattern里設置core文件的文件名模板,詳情請看core的官方man手冊。 (發(fā)布者:chiying)
linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)


關(guān)鍵詞: core文件 Linux

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>