原文地址:
http://www.cppblog.com/sandy/archive/2007/03/13/19723.htmlStep1 下载最新的Debugging Tools for windows
http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx最新版本是6.6.7.5
http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.6.07.5.exe(BTW,如果使用老版本可能出现"The call to LoadLibrary(ext) failed, Win32 error 127. The specified procedure could not be found."这样的错误信息)
Step2
设置好symbol server的环境变量
Set _NT_SYMBOL_PATH=srv*c:\symbols*http://msdl.microsoft.com/download/symbols
创建好c:\symbols目录
(BTW,如果要设置http代理set _NT_SYMBOL_PROXY=proxy_server:port)
Step3
把WinDBG(CDB)安装路径加到path中(默认是C:\Program Files\Debugging Tools for Windows)
设置CDB为默认的JIT debugger
C:\>cdb -iae
Step4使用vc6创建一个控制台程序Crash,主程序为crash.cpp
1 // Crash.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5
6 int main(int argc, char* argv[])
7 {
8 for(int i=0;i<10;++i)
9 printf("Hello World!%s\n",i);
10 return 0;
11 }
可以看到该程序就第九行有一个错误,%s应该写成%d
Step 5
编译生成release版本(这里我主要针对Release程序)
注意设置
最后会生成Crash.exe和Crash.pdb和其他一些中间文件
Step 6
运行Release版本的crash.exe会出现crash,这时CDB会自动attach到该进程,开始调试
出现如下画面
Hello World!(null)
Microsoft (R) Windows Debugger Version 6.6.0007.5
Copyright (c) Microsoft Corporation. All rights reserved.
*** wait with pending attach
Symbol search path is: srv*c:\symbols*http://msdl.microsoft.com/download/symbols
Executable search path is:
ModLoad: 00400000 0040c000 D:\Test\Crash\Release\Crash.exe
ModLoad: 77f80000 77ffc000 C:\WINNT\system32\ntdll.dll
ModLoad: 7c570000 7c624000 C:\WINNT\system32\KERNEL32.dll
(e04.f18): Access violation - code c0000005 (!!! second chance !!!)
eax=00000001 ebx=7ffdf073 ecx=00000001 edx=7fffffff esi=7ffffffe edi=00000800
eip=004016dd esp=0012fcf8 ebp=0012ff50 iopl=0 nv up ei pl nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000 efl=00000206
*** WARNING: Unable to verify checksum for D:\Test\Crash\Release\Crash.exe
Crash!_output+0x49a:
004016dd 803800 cmp byte ptr [eax],0 ds:0023:00000001=??
0:000>
输入.lines load source code information
输入k命令查看堆栈
0:000> .lines
Line number information will be loaded
0:000> k
ChildEBP RetAddr
0012ff50 0040103f Crash!_output+0x49a
0012ff70 0040100e Crash!printf+0x1f
0012ff80 00401105 Crash!main+0xe [D:\Test\Crash\Crash.cpp @ 9]
0012ffc0 7c5989a5 Crash!mainCRTStartup+0xb4
0012fff0 00000000 KERNEL32!BaseProcessStart+0x3d
可以发现程序在Crash.cpp第9行crash
输入q命令退出cdb,结束调试。