Windows程序crash定位,首先需要有dump文件,dump文件是C++程序发生异常时,保存当时程序运行状态的文件, 是调试异常程序重要的方法。
windows系统默认不产生程序的dmp文件,需要手动设置才能生成dmp文件。
1. dump文件生成方式:
修改注册表,使用管理员权限,执行以下脚本内容,运行后: 任何程序崩溃都会在C:\xxx 产生dmp文件(full dmp),bat脚本示例:
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps"
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpFolder /t REG_EXPAND_SZ /d "C:\xxx" /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpType /t REG_DWORD /d 2 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpCount /t REG_DWORD /d 10 /f
pause
2. dump文件分析(WinDbg工具)
WinDbg工具安装:
模拟崩溃程序 ,新建空项目,属性中配置成生成动态链接dll库:
utils.h:
#pragma once
#include <iostream>
using namespace std;
extern "C" __declspec(dllexport) int add_fun(int a, int b);
utils.cpp:
# include "utils.h"
int add_fun(int a, int b)
{
int *arr = NULL;
int c = arr[a] + arr[b];
return c;
}
重新生成解决方案,即可生成dll及lib文件。
新建空项目调上述dll库,默认生成exe程序:
#include "utils.h"
#pragma comment(lib, "dll_test.lib")
int main()
{
int c = add_fun(0, 3);
cout << c << endl;
}
运行后程序崩溃,因为dll库中的add_fun函数里使用了空指针,此时在上述配置目录下(上"C:\xxx")会生成dmp文件:
打开WinDbg工具,首先点击文件,打开dmp文件:
其次设置Default source path和Default symbal path:
命令行执行如下指令进行自动分析,分析结果如下,不仅可以看到dll中报错函数代码,而且可以具体到具体报错行数:
Windows下生成dump文件的三种方式
dump后,如何用Windbg进行分析呢?