- 环境和工具: Qt5.6.2 MinGW 、 Red Hat
1. 首先配置Core文件的生成环境
步骤一:
首先通过 ulimit –c 查看 若为0,则不会产生对应的coredump,需要进行修改和设置。
ulimit -c unlimited (可以产生coredump且不受大小限制),这种设置仅对当前生效;
Tisp:如果想永久生效,在/etc/profile中加入以下一行,这将允许生成coredump文件
ulimit -c unlimited
步骤二:
vi /etc/sysctl.conf
添加下面两行;
kernel.core_uses_pid = 1
kernel.core_pattern=/tmp/core-%e-%p
Tips (kernel.core_pattern 的设置规则如下):
{
%p -insert pid into filename
%u - insertcurrent uid into filename
%g - insert current gidinto filename
%s - insert signal thatcaused the coredump into the filename
%t - insert UNIX time thatthe coredump occurred into filename
%h - insert hostname wherethe coredump happened into filename
%e -insert coredumping executable name into filename
}
最后:
sysctl -p /etc/sysctl.conf
将修改的配置生效
2. 打开Qt5.6.2 创建一个工程 TestExceptionDump
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::a()
{
qDebug()<<"this is c";
}
void MainWindow::b()
{
qDebug()<<"this is b";
}
void MainWindow::c()
{
// 异常位置
int *i = NULL;
*i = 100;
}
void MainWindow::on_pushButton_clicked()
{
int kkk = 1000;
qDebug()<<"this is button " << kkk;
a();
b();
c();
}
3. 运行
4.程序崩溃
查看core配置路径/tmp文件夹发现生成了一个core文件
将这个文件copy到工程执行程序的目录下
执行命令:gdb TestExeceptionDump core-TestExceptionDu-4138
然后在执行命令:bt (back trace 堆栈查询)
从上图中可以看出最后一个执行的位置在c()函数中,从而分析错误的位置;