Windows环境崩溃问题(dump)可根据vs调试或windbg工具查看.linux环境崩溃文件为core文件,可以使用gdb进行调试分析。
前提:都是都是用了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文件。
建立一个 /data/coredump 文件夹,在根目录data里的coredump文件夹。
调用如下命令
echo /data/coredump/core.%e.%p > /proc/sys/kernel/core_pattern
将更改core文件生成路径,自动放在这个/data/coredump文件夹里。
对应参数意义说明:
%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 www
b.运行可执行程序。会出现核心已转储,表明已生成core文件。注意我只有使用root权限用户才出现核心已转储(即才生成core文件)
root@gxrong-virtual-machine:/home/gxrong/qt# g++ -g w.cpp -o www
root@gxrong-virtual-machine:/home/gxrong/qt# ./www
段错误 (核心已转储)
4.使用gdb调试core文件
a. 执行 gdb 可执行程序exe
root@gxrong-virtual-machine:/home/gxrong/qt# gdb www
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 www...done.
b. 执行 core-file core的名字
(gdb) core-file coredump
[New LWP 2199]
Core was generated by `./www'.
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)
完整的过程
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
其他的步骤和一般的linux程序一样。都是 设置ulimit -c unlimited 、更改core文件、运行可执行程序生成core文件。
linux + qt 环境下调试core文件
第一种办法:和一般linux程序一样,通过gdb调试。
第二种办法:使用qt creator
1、Debug->Start Debugging->Load Core File
2、在弹出的窗口选择对应的文件
3、点击OK,开始调试,会看到code里的信息