日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 嵌入式 > 嵌入式軟件
[導(dǎo)讀]Linux下的C程序常常會(huì)因?yàn)閮?nèi)存訪問錯(cuò)誤等原因造成segment fault(段錯(cuò)誤),此時(shí)如果系統(tǒng)core dump功能是打開的,那么將會(huì)有內(nèi)存映像轉(zhuǎn)儲(chǔ)到硬盤上來,之后可以用gdb對(duì)core文件進(jìn)行分析,還原系統(tǒng)發(fā)生段錯(cuò)誤時(shí)刻的堆棧情況。這對(duì)于我們發(fā)現(xiàn)程序bug很有幫助。

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

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

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

ulimit -c 100 設(shè)置core文件最大為100k

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

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

#include

int main()

{

char *ptr="linuxers.cn";

*ptr=0;

}

編譯運(yùn)行后結(jié)果如下:

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

[leconte@localhost test]$ ./test

段錯(cuò)誤

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

[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

段錯(cuò)誤 (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

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;

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

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

除此之外,還可以在/proc/sys/kernel/core_pattern里設(shè)置core文件的文件名模板,詳情請(qǐng)看core的官方man手冊(cè)。 (發(fā)布者:chiying)

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉