偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

Linux下的C程序常常會(huì)因?yàn)閮?nèi)存訪問(wèn)錯(cuò)誤等原因造成segment fault(段錯(cuò)誤),此時(shí)如果系統(tǒng)core dump功能是打開的,那么將會(huì)有內(nèi)存映像轉(zhuǎn)儲(chǔ)到硬盤上來(lái),之后可以用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 <stdio.h>

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

可見(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 <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;

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

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

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

【編輯推薦】

  1. 安裝Fedora Core 4配置文件--Linux系統(tǒng)
  2. 講解介紹Fedora Core 3安裝相關(guān)的問(wèn)題
  3. Fedora Core 3系統(tǒng)硬件要求與軟件說(shuō)明
責(zé)任編輯:趙寧寧 來(lái)源: chinaitlab
相關(guān)推薦

2021-09-14 07:26:25

雪花算法ID

2020-09-24 09:43:59

Http協(xié)議options請(qǐng)求

2010-06-01 15:58:14

SVN錯(cuò)誤

2018-07-16 08:40:08

Linux段錯(cuò)誤C++

2019-03-04 11:11:53

CPU調(diào)試程序MIPS

2019-03-04 14:40:46

Linux文件系統(tǒng)修復(fù)

2024-05-31 12:54:37

.NET CoreLinux語(yǔ)言

2021-07-14 07:55:47

Linux CPULinux 系統(tǒng)

2016-12-13 15:18:02

代碼

2010-01-12 16:53:23

Fedora Core

2022-08-14 19:27:16

LinuxmacOS

2025-06-30 01:45:00

Netty輪詢HTTP 協(xié)議

2021-04-12 10:57:28

區(qū)塊鏈信任銀行

2011-06-16 17:05:54

CSS

2011-03-21 15:25:52

MySQL系統(tǒng)錯(cuò)誤

2022-09-16 15:10:12

模型AI

2021-01-18 14:04:49

java監(jiān)控操作

2019-01-21 09:55:25

LinuxNET Core SD命令

2022-01-20 08:04:41

FSCKLinux磁盤

2022-11-07 12:52:25

Linuxfsck命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)