Windows进程崩溃问题的定位方法
内容摘要
Linux上进程崩溃通常会生成core文件,用gdb打开后执行bt命令即可查看堆栈。而在Windows平台上,我们通常会采用MiniDumpWriteDump来进行堆栈转储,而这需要对系统Api有一定的了解
文章正文
Linux上进程崩溃通常会生成core文件,用gdb打开后执行bt命令即可查看堆栈。而在Windows平台上,我们通常会采用MiniDumpWriteDump来进行堆栈转储,而这需要对系统Api有一定的了解和编写一些代码。本文就结合实际项目经验,总结了一种无需编码即可记录进程崩溃堆栈的方法。
原理简介:使用nstd工具进行进程崩溃时内存和堆栈转储。
编译Release版本时打开调试选项,将exe和pdb文件一起发布。
2.使用批处理命令设置Windows系统在进程崩溃时调用的调试器为ntsd
这里下载。
#include <stdio.h>
Opened log file 'D:\Dump\dump_22d4_2014-09-30_15-15-33-062.txt'
Debug session time: Tue Sep 30 15:15:33.063 2014 (GMT+8)
System Uptime: 2 days 3:35:54.545
Process Uptime: 0 days 0:00:00.923
Kernel time: 0 days 0:00:00.015
User time: 0 days 0:00:00.000
Process Status:
. 0 id: 3854 attach name: D:\xcb\20140808\test\CoreDump\DumpExampleNormalStack.exe
Thread Status:
. 0 Id: 3854.3138 Suspend: 1 Teb: 7ffdf000 Unfrozen
Stack Status:
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\SYSTEM32\ntdll.dll -
# ChildEBP RetAddr
002dfb2c 01321038 DumpExampleNormalStack!test2(void)+0x18
002dfb34 01321048 DumpExampleNormalStack!test1(void)+0x8
002dfb3c 01321159 DumpExampleNormalStack!main(int argc = 1, char ** argv = 0x003ea488)+0x8
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\system32\kernel32.dll -
002dfb84 76e2ee1c DumpExampleNormalStack!__tmainCRTStartup(void)+0xfe
WARNING: Stack unwind information not available. Following frames may be wrong.
002dfb90 77ba37eb kernel32!BaseThreadInitThunk+0x12
002dfbd0 77ba37be ntdll!RtlInitializeExceptionChain+0xef
002dfbe8 00000000 ntdll!RtlInitializeExceptionChain+0xc2
Closing open log file D:\Dump\dump_22d4_2014-09-30_15-15-33-062.txt
3.下面通过一个实例来演示下效果:
复制代码 代码如下:
#include <stdio.h>
void test2()
{
int a = 1;
int b = 0;
int c = a/b;
}
void test1()
{
test2();
}
int main(int argc, char** argv)
{
test1();
return 0;
}
我们通过除0错误来构造一次崩溃,test1和test2是为了演示调用堆栈。
通过本方法抓取的堆栈文本如下:
复制代码 代码如下:
Opened log file 'D:\Dump\dump_22d4_2014-09-30_15-15-33-062.txt'
Debug session time: Tue Sep 30 15:15:33.063 2014 (GMT+8)
System Uptime: 2 days 3:35:54.545
Process Uptime: 0 days 0:00:00.923
Kernel time: 0 days 0:00:00.015
User time: 0 days 0:00:00.000
Process Status:
. 0 id: 3854 attach name: D:\xcb\20140808\test\CoreDump\DumpExampleNormalStack.exe
Thread Status:
. 0 Id: 3854.3138 Suspend: 1 Teb: 7ffdf000 Unfrozen
Stack Status:
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\SYSTEM32\ntdll.dll -
# ChildEBP RetAddr
002dfb2c 01321038 DumpExampleNormalStack!test2(void)+0x18
002dfb34 01321048 DumpExampleNormalStack!test1(void)+0x8
002dfb3c 01321159 DumpExampleNormalStack!main(int argc = 1, char ** argv = 0x003ea488)+0x8
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\system32\kernel32.dll -
002dfb84 76e2ee1c DumpExampleNormalStack!__tmainCRTStartup(void)+0xfe
WARNING: Stack unwind information not available. Following frames may be wrong.
002dfb90 77ba37eb kernel32!BaseThreadInitThunk+0x12
002dfbd0 77ba37be ntdll!RtlInitializeExceptionChain+0xef
002dfbe8 00000000 ntdll!RtlInitializeExceptionChain+0xc2
Closing open log file D:\Dump\dump_22d4_2014-09-30_15-15-33-062.txt
切记:在Release版本中需要把调试选项打开,而且生成的pdb文件和exe要放在同一目录下。
完~
代码注释