linux 下崩溃文件coredump 生成及调试分析

20 篇文章 0 订阅
13 篇文章 1 订阅

Windows环境崩溃问题(dump)可根据vs调试或windbg工具查看.

linux环境崩溃文件为core文件,可以使用gdb进行调试分析。

至于gdb调试的介绍见:linux gdb使用_小飞侠hello的博客-CSDN博客

前提:都是都是用了root权限的用户操作。

1.生成core文件的前提

产生coredump的条件,首先需要确认当前会话的ulimit –c,若为0,则不会产生对应的coredump,需要进行修改和设置。 

临时修改

在当前终端中输入  ulimit  -c unlimited  (可以产生coredump且不受大小限制),这种设置仅对当前生效。

永久生效

需要在/etc/profile中加入以下一行。具体操作: vim /etc/profile,然后进入编辑模式,在profile文件中加入ulimit -c unlimited   保存退出,重启服务器,改文件就长久生效,或者#source /etc/profile,不重启服务器,使用source使文件马上生效。

2.更改core dump生成路径

因为core dump默认会生成在程序的工作目录,但是有些程序存在切换目录的情况,导致core dump生成的路径没有规律,所以最好是自己建立一个文件夹,存放生成的core文件。

在程序同级目录下建立一个 coredump文件夹,该coredump文件夹存放coredump崩溃文件

临时有效:

调用如下命令

echo ./coredump/core-%e-%t > /proc/sys/kernel/core_patter

将更改core文件生成路径,自动放在当前程序的coredump文件夹里。

永久有效

打开该文件:
sudo vi /etc/sysctl.conf
文件尾部增加:
kernel.core_pattern =./coredump/core-%e-%t

 对应参数意义说明: 
%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文件生成时的时间 
%h - insert hostname where the coredump happened into filename 插入主机名 
%e - insert coredumping executable name into filename 插入程序名
 

3测试生成core文件

#include <stdio.h>
int main()
{
    int* p = NULL;
    *p = 0;
    return 0;
}

a. 编译代码生成可执行程序。 编译的时候带上-g选项,这样才能用gdb调试core

g++ -g w.cpp -o w

b.运行可执行程序。会出现核心已转储,表明已生成core文件。注意我只有使用root权限用户才出现核心已转储(即才生成core文件)

root@gxrong-virtual-machine:/home/gxrong/qt# g++ -g w.cpp -o w 
root@gxrong-virtual-machine:/home/gxrong/qt# ./w
段错误 (核心已转储)

4.使用gdb调试core文件

前提条件:cpp文件需要和exe在同一目录

a. 执行 gdb 可执行程序exe

root@gxrong-virtual-machine:/home/gxrong/qt#  gdb w
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from w...done.

b. 执行 core-file  core的名字

(gdb) core-file coredump
[New LWP 2199]
Core was generated by `./w'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00005604710ed60a in main () at w.cpp:5
5	    *p = 0;
(gdb) 

注意:如果是简单的程序,其实可以把上面的a、b步骤合并成一步执行即可。

gdb 程序名  core文件名

gdb w /data/coredump/core.w.2893

c.敲bt命令,这是gdb查看back trace的命令

(gdb) bt
#0  0x00005604710ed60a in main () at w.cpp:5
(gdb) 

完整的过程

//w.cpp 文件
#include <stdio.h>
int main()
{
    int* p = NULL;
    *p = 0;
    return 0;
}
gxrong@gxrong-virtual-machine:~/qt$ su root
密码: 
root@gxrong-virtual-machine:/home/gxrong/qt# ulimit -c
0
root@gxrong-virtual-machine:/home/gxrong/qt# ulimit -c unlimited 
root@gxrong-virtual-machine:/home/gxrong/qt# g++ -g w.cpp -o w
root@gxrong-virtual-machine:/home/gxrong/qt# ./w
段错误 (核心已转储)
root@gxrong-virtual-machine:/home/gxrong/qt# gdb w /data/coredump/core.w.2893
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from w...done.
[New LWP 2893]
Core was generated by `./w'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000055570ac5c60a in main () at w.cpp:5
5	    *p = 0;
(gdb) bt
#0  0x000055570ac5c60a in main () at w.cpp:5
(gdb) 

延伸:

linux + qt 环境下生成core文件

1.在pro 配置添加 (好像不用配置也可以)
QMAKE_CC += -g
QMAKE_CXX += -g
QMAKE_LINK += -g
 2.其他的步骤和一般的linux程序一样。都是 设置ulimit -c unlimited 、更改core文件、运行可执行程序生成core文件。

linux + qt 环境下调试core文件

前提条件:注意代码存放的目录位置,要和生成core文件目录时保持一致。

第一种办法:和一般linux程序一样,通过gdb调试。

调用  gdb 程序名  core文件名

或者分2步走 

 先gdb  可执行程序    再core-file  core的名字

第二种办法:使用qt creator    

1、Debug->Start Debugging->Load Core File

2、在弹出的窗口选择对应的文件
3、点击OK,开始调试,会看到code里的信息

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
了解,可以为您提供一些关于Linux core dump文件分析的信息。首先,core dump文件是在程序崩溃或异常终止时生成的,它包含了程序崩溃时的内存映像和其他相关信息。分析core dump文件可以帮助我们定位程序崩溃的原因。 在Linux中,您可以使用以下工具来分析core dump文件: 1. GDB (GNU调试器): GDB是一个功能强大的调试器,可以用于分析core dump文件。您可以使用以下命令来加载core dump文件并进行分析: ``` gdb <可执行文件> <core dump文件> ``` 这将启动GDB并加载core dump文件。您可以使用GDB的各种命令来查看内存状态、寄存器值等,以帮助定位问题。 2. Crash: Crash是一个用于分析core dump文件的命令行工具,它提供了一些有用的命令和脚本来帮助您分析崩溃。您可以使用以下命令来使用crash工具: ``` crash <vmlinux> <core dump文件> ``` 其中,`vmlinux` 是内核符号文件,您可以在`/usr/lib/debug/boot`目录下找到。 3. SystemTap: SystemTap是一个功能强大的系统跟踪工具,可以用于分析各种系统问题,包括core dump文件。您可以编写SystemTap脚本来分析core dump文件中的各种信息。例如,您可以编写一个脚本来检查程序崩溃时的堆栈跟踪信息。 这些工具都有很多功能和选项,可以根据您的具体需求进行深入的分析。请注意,对于大型和复杂的core dump文件分析可能需要一些时间和经验。 希望这些信息对您有所帮助!如果您有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值