ubuntu core文件

一.相关知识

1)何为core文件?

        核心文件(core file),也称核心转储(core dump),是操作系统在进程收到某些信号而终止运行时,将此时进程地址空间的内容以及有关进程状态的其他信息写出的一个磁盘文件。这种信息往往用于调试。[摘自百度百科]

        A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.

On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase "to dump core" has come to mean, in many cases, any fatal error, regardless of whether a record of the program memory is created.[摘自维基百科]    


二.示例 ubuntu下core文件的产生

1)查看相关环境,如下图中

查看相关环境

注:core file size 为 0,core file产生还没打开

2)写一个简单的存在问题的c++程序并运行

运行出错

注:运行出错,提示段错误(访问空指针),由于没有打开core文件的开关,故没有产生core文件的信息

3)打开core文件产生的”开关“

打开core文件产生的开关

注:设置core file size来开否core文件的产生,这里的1024是喜好值,可以根据实值情况调整

4)运生程序出错并生成core文件

查看产生的core文件

注:默认在当前工作目录产生core文件,而文件名为core

[更改core文件产生的格式]

更新core文件的名称

注:修改/proc/sys/kernel/core_pattern的内容为core-%p-%e-%t,原来的内容为core

proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
echo "core-%e-%p-%t" > /proc/sys/kernel/core_pattern/core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
    %p - insert pid into filename 添加pid
    %u - insert current uid into filename 添加当前uid
    %g - insert current gid into filename 添加当前gid
    %s - insert signal that caused the coredump into the filename 添加导致产生core的信号
    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
    %h - insert hostname where the coredump happened into filename 添加主机名
    %e - insert coredumping executable name into filename 添加命令名

5)使用gdb调试core文件

使用gdb

core文件的查看
 core文件需要使用gdb来查看。
 gdb ./a.out
 core-file core.xxxx
 使用bt命令即可看到程序出错的地方。 
以下两种命令方式具有相同的效果,但是在有些环境下不生效,所以推荐使用上面的命令。 
1)gdb -core=core.xxxx
file ./a.out
bt 
2)gdb -c core.xxxx
file ./a.out
bt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值