利用WinDbg找出程序崩溃的代码行号

摘要:
我之前在论坛上见过几个朋友,说程序不时崩溃,xxoo看不懂!分析-v。几秒钟后,将打印一条错误消息。函数调用堆栈如下:MicrosoftWindowsDebuggerVersion6.11.0001.404X86版权所有Microsoft Corporation。保留所有权利。LoadingDumpFile[C:Test Release Log 2012-05-29160059.dmp]UserMiniDumpFile:仅寄存器、堆栈和部分内存可用Symbolsearchpath:C:\Test\ReleaseExecutable搜索路径:C:\Test\ ReleaseWindowsXPVersion2600MPFreex86compatible产品:WinNt,suite:SingleUserTSMachineName:Debugssessiontime:TueMay2916:00:59.0002012系统正常运行时间:notavailableProcessUptime:0days0:00:01.000访问冲突-codec0000005eax=00a80000ebx=00157ea8ecx=0000000 7edx=7c92e514esi=00157e80edi=00157ed8eip=7c91e514ESp=0012e830ebp=0010e840iopl=0nvupeipzrnapenccs=001bss=0023ds=0023es=0023fs=003bgs=0000efl=00000246***错误:无法找到符号。默认情况下,导出符号forntdll.dll-ntdll!
之前碰到论坛里有几个好友,说程序不时的崩溃,什么xxoo不能read的!
如果光要是这个内存地址,估计你会疯掉~~
所以分享一下基本的调试技巧,需要准备的工具有WinDbg + VC6.0,
下面是自己整理的一份自动生成DUMP文件的源代码,只需要添加到工程即可,源代码如下:
MiniDump.h
MiniDump.cpp

<具体请参考附件SRC中,太大就不贴了>
1、在CXXDlg::OnInitDialog()中添加这样一段:
  1. BOOL CTestDlg::OnInitDialog()
  2. {
  3. CDialog::OnInitDialog();

  4. // ......
  5. SetUnhandledExceptionFilter(CrashReportEx);
  6. HMODULE hKernel32;

  7. // Try to get MiniDumpWriteDump() address.
  8. hDbgHelp = LoadLibrary("DBGHELP.DLL");
  9. MiniDumpWriteDump_ = (MINIDUMP_WRITE_DUMP)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
  10. // d("hDbgHelp=%X, MiniDumpWriteDump_=%X", hDbgHelp, MiniDumpWriteDump_);

  11. // Try to get Tool Help library functions.
  12. hKernel32 = GetModuleHandle("KERNEL32");
  13. CreateToolhelp32Snapshot_ = (CREATE_TOOL_HELP32_SNAPSHOT)GetProcAddress(hKernel32, "CreateToolhelp32Snapshot");
  14. Module32First_ = (MODULE32_FIRST)GetProcAddress(hKernel32, "Module32First");
  15. Module32Next_ = (MODULE32_NEST)GetProcAddress(hKernel32, "Module32Next");
  16. }
复制代码
下面是工程中的测试代码:
  1. class CTestDlg : public CDialog
  2. {
  3. // Construction
  4. public:
  5. CTestDlg(CWnd* pParent = NULL); // standard constructor

  6. void Fun1(char *pszBuffer);
  7. void Fun2(char *pszBuffer);
  8. void Fun3(char *pszBuffer);
  9. };
复制代码
  1. void CTestDlg::Fun1(char *pszBuffer)
  2. {
  3. Fun2(pszBuffer);
  4. }

  5. void CTestDlg::Fun2(char *pszBuffer)
  6. {
  7. Fun3(pszBuffer);
  8. }

  9. void CTestDlg::Fun3(char *pszBuffer)
  10. {
  11. pszBuffer[1] = 0x00;
  12. }
复制代码
我们在双击确定按钮时的响应代码如下:
  1. void CTestDlg::OnOK()
  2. {
  3. // TODO: Add extra validation here
  4. Fun1(NULL);
  5. }
复制代码

2、设置VC编译选项,勾选生成MAP和Debug Info、Progma Datebase:

1.jpg
2011-9-5 09:19 上传
下载附件(55.12 KB)

1.JPG
2012-5-29 16:03 上传
下载附件(82.96 KB)

3、将编译生成的Release目录中的pdb、map文件保存起来,以后调试会用到:
2.jpg
2011-9-5 09:19 上传
下载附件(9.85 KB)

4、运行程序,单击确定按钮出现异常后自动重启,并创建一个Log文件夹,里面生成dump文件:
3.jpg
2011-9-5 09:19 上传
下载附件(8.92 KB)

5、我们打开WinDbg,设置一下相关路径
A、
设置pdb路径(File \ Symbol File Path)
PDBPath.jpg
2012-5-29 16:06 上传
下载附件(12.4 KB)

B、设置源代码路径( File \ Source File Path )
SourcePath.jpg
2012-5-29 16:06 上传
下载附件(11.05 KB)

C、设置Exe路径( File \ Image File Path )
ExePath.jpg
2012-5-29 16:06 上传
下载附件(12.84 KB)


6、用WiinDbg打开dump文件(File \ Open Crash Dump)
5.jpg
2011-9-5 09:19 上传
下载附件(112.02 KB)

7、输入命令!analyze -v,等待几秒后会打印出错误信息,函数调用栈如下图:

  1. Microsoft (R) Windows Debugger Version 6.11.0001.404 X86
  2. Copyright (c) Microsoft Corporation. All rights reserved.


  3. Loading Dump File [C:\Test\Release\Log\2012-05-29 160059.dmp]
  4. User Mini Dump File: Only registers, stack and portions of memory are available

  5. Symbol search path is: C:\Test\Release
  6. Executable search path is: C:\Test\Release
  7. Windows XP Version 2600 (Service Pack 3) MP (4 procs) Free x86 compatible
  8. Product: WinNt, suite: SingleUserTS
  9. Machine Name:
  10. Debug session time: Tue May 29 16:00:59.000 2012 (GMT+8)
  11. System Uptime: not available
  12. Process Uptime: 0 days 0:00:01.000
  13. ...................................
  14. This dump file has an exception of interest stored in it.
  15. The stored exception information can be accessed via .ecxr.
  16. (1710.1450): Access violation - code c0000005 (first/second chance not available)
  17. eax=00a80000 ebx=00157ea8 ecx=00000007 edx=7c92e514 esi=00157e80 edi=00157ed8
  18. eip=7c92e514 esp=0012e830 ebp=0012e840 iopl=0 nv up ei pl zr na pe nc
  19. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
  20. *** ERROR: Symbol file could not be found. Defaulted to export symbols for ntdll.dll -
  21. ntdll!KiFastSystemCallRet:
  22. 7c92e514 c3 ret
  23. 0:000> !analyze -v
  24. *******************************************************************************
  25. * *
  26. * Exception Analysis *
  27. * *
  28. *******************************************************************************

  29. *** ERROR: Symbol file could not be found. Defaulted to export symbols for mfc42.dll -
  30. *** ERROR: Symbol file could not be found. Defaulted to export symbols for user32.dll -
  31. ***** OS symbols are WRONG. Please fix symbols to do analysis.

  32. *** ERROR: Symbol file could not be found. Defaulted to export symbols for kernel32.dll -
  33. *************************************************************************
  34. *** ***
  35. *** ***
  36. *** Your debugger is not using the correct symbols ***
  37. *** ***
  38. *** In order for this command to work properly, your symbol path ***
  39. *** must point to .pdb files that have full type information. ***
  40. *** ***
  41. *** Certain .pdb files (such as the public OS symbols) do not ***
  42. *** contain the required information. Contact the group that ***
  43. *** provided you with these symbols if you need this command to ***
  44. *** work. ***
  45. *** ***
  46. *** Type referenced: IMAGE_NT_HEADERS32 ***
  47. *** ***
  48. *************************************************************************
  49. *** ERROR: Symbol file could not be found. Defaulted to export symbols for ole32.dll -
  50. *** ERROR: Symbol file could not be found. Defaulted to export symbols for advapi32.dll -
  51. *************************************************************************
  52. *** ***
  53. *** ***
  54. *** Your debugger is not using the correct symbols ***
  55. *** ***
  56. *** In order for this command to work properly, your symbol path ***
  57. *** must point to .pdb files that have full type information. ***
  58. *** ***
  59. *** Certain .pdb files (such as the public OS symbols) do not ***
  60. *** contain the required information. Contact the group that ***
  61. *** provided you with these symbols if you need this command to ***
  62. *** work. ***
  63. *** ***
  64. *** Type referenced: kernel32!pNlsUserInfo ***
  65. *** ***
  66. *************************************************************************
  67. *************************************************************************
  68. *** ***
  69. *** ***
  70. *** Your debugger is not using the correct symbols ***
  71. *** ***
  72. *** In order for this command to work properly, your symbol path ***
  73. *** must point to .pdb files that have full type information. ***
  74. *** ***
  75. *** Certain .pdb files (such as the public OS symbols) do not ***
  76. *** contain the required information. Contact the group that ***
  77. *** provided you with these symbols if you need this command to ***
  78. *** work. ***
  79. *** ***
  80. *** Type referenced: kernel32!pNlsUserInfo ***
  81. *** ***
  82. *************************************************************************

  83. FAULTING_IP:
  84. Test!CTestDlg::Fun3+6 [C:\Test\TestDlg.cpp @ 141]
  85. 00401ca6 c6400100 mov byte ptr [eax+1],0

  86. EXCEPTION_RECORD: ffffffff -- (.exr 0xffffffffffffffff)
  87. ExceptionAddress: 00401ca6 (Test!CTestDlg::Fun3+0x00000006)
  88. ExceptionCode: c0000005 (Access violation)
  89. ExceptionFlags: 00000000
  90. NumberParameters: 2
  91. Parameter[0]: 00000001
  92. Parameter[1]: 00000001
  93. Attempt to write to address 00000001

  94. PROCESS_NAME: Test.exe

  95. ADDITIONAL_DEBUG_TEXT:
  96. Use '!findthebuild' command to search for the target build information.
  97. If the build information is available, run '!findthebuild -s ; .reload' to set symbol path and load symbols.

  98. MODULE_NAME: Test

  99. FAULTING_MODULE: 7c920000 ntdll

  100. DEBUG_FLR_IMAGE_TIMESTAMP: 4fc48236

  101. ERROR_CODE: (NTSTATUS) 0xc0000005 - "0x%08lx"

  102. EXCEPTION_CODE: (NTSTATUS) 0xc0000005 - "0x%08lx"

  103. EXCEPTION_PARAMETER1: 00000001

  104. EXCEPTION_PARAMETER2: 00000001

  105. WRITE_ADDRESS: 00000001

  106. FOLLOWUP_IP:
  107. Test!CTestDlg::Fun3+6 [C:\Test\TestDlg.cpp @ 141]
  108. 00401ca6 c6400100 mov byte ptr [eax+1],0

  109. FAULTING_THREAD: 00001450

  110. BUGCHECK_STR: APPLICATION_FAULT_NULL_CLASS_PTR_DEREFERENCE_INVALID_POINTER_WRITE_WRONG_SYMBOLS

  111. PRIMARY_PROBLEM_CLASS: NULL_CLASS_PTR_DEREFERENCE

  112. DEFAULT_BUCKET_ID: NULL_CLASS_PTR_DEREFERENCE

  113. LAST_CONTROL_TRANSFER: from 00401c9c to 00401ca6

  114. STACK_TEXT:
  115. 0012f89c 00401c9c 00000000 0012f8b4 00401c8c Test!CTestDlg::Fun3+0x6 [C:\Test\TestDlg.cpp @ 141]
  116. 0012f8a8 00401c8c 00000000 0012f8cc 00401f27 Test!CTestDlg::Fun2+0xc [C:\Test\TestDlg.cpp @ 137]
  117. 0012f8b4 00401f27 00000000 73d323eb 73dcf07c Test!CTestDlg::Fun1+0xc [C:\Test\TestDlg.cpp @ 132]
  118. 0012f8bc 73d323eb 73dcf07c 00000111 0012f8fc Test!CTestDlg::OnOK+0x7 [C:\Test\TestDlg.cpp @ 242]
  119. WARNING: Stack unwind information not available. Following frames may be wrong.
  120. 0012f8cc 73d322fd 0012fe94 00000001 00000000 mfc42!Ordinal567+0xa2
  121. 0012f8fc 73d976e5 00000001 00000000 00000000 mfc42!Ordinal4424+0x108
  122. 0012f920 73d33094 00000001 00000000 00000000 mfc42!Ordinal4431+0x1b
  123. 0012f970 73d31b58 00000000 0014120e 0012fe94 mfc42!Ordinal4441+0x51
  124. 0012f9f0 73d31b07 00000111 00000001 0014120e mfc42!Ordinal5163+0x2f
  125. 0012fa10 73d31a78 00000111 00000001 0014120e mfc42!Ordinal6374+0x22
  126. 0012fa70 73d319d0 0012fe94 00000000 00000111 mfc42!Ordinal1109+0x91
  127. 0012fa90 73dbe47c 0018124c 00000111 00000001 mfc42!Ordinal1578+0x34
  128. 0012fabc 77d18734 0018124c 00000111 00000001 mfc42!Ordinal1579+0x39
  129. 0012fae8 77d18816 73dbe443 0018124c 00000111 user32!GetDC+0x6d
  130. 0012fb50 77d2927b 00000000 73dbe443 0018124c user32!GetDC+0x14f
  131. 0012fb8c 77d292e3 006d5120 007101c8 00000001 user32!GetParent+0x16c
  132. 0012fbac 77d4ff7d 0018124c 00000111 00000001 user32!SendMessageW+0x49
  133. 0012fbc4 77d465d2 007156c0 00000000 007156c0 user32!CreateMDIWindowA+0x1bd
  134. 0012fbe0 77d25e94 001530ec 00000001 00000000 user32!DeregisterShellHookWindow+0x6312
  135. 0012fc64 77d3b082 007156c0 00000202 00000000 user32!IsDlgButtonChecked+0x109a
  136. 0012fc84 77d18734 0014120e 00000202 00000000 user32!SoftModalMessageBox+0xda3
  137. 0012fcb0 77d18816 77d3b036 0014120e 00000202 user32!GetDC+0x6d
  138. 0012fd18 77d189cd 00000000 77d3b036 0014120e user32!GetDC+0x14f
  139. 0012fd78 77d18a10 00404314 00000000 0012fdac user32!GetWindowLongW+0x127
  140. 0012fd88 77d274ff 00404314 00404314 0040431c user32!DispatchMessageW+0xf
  141. 0012fdac 77d3c6d3 0018124c 007156c0 00404314 user32!IsDialogMessageW+0xdb
  142. 0012fdcc 73d45202 0018124c 00404314 0012fe94 user32!IsDialogMessage+0x4a
  143. 0012fddc 73d39be0 00404314 73d451ce 00404314 mfc42!Ordinal4047+0x2f
  144. 0012ff00 73d3c1cf 006f0072 00142373 00000000 mfc42!Ordinal5278+0x29
  145. 004034c0 00401c20 004019f0 00401a00 00401a10 mfc42!Ordinal1576+0x47
  146. 004034c4 004019ef 00401a00 00401a10 00402130 Test!CTestDlg::`scalar deleting destructor'
  147. 004034c8 004019ff 00401a10 00402130 0040212a Test!CTestDlg::~CTestDlg+0xf
  148. 004034cc 00401a0f 00402130 0040212a 0040203a Test!CObject::Serialize+0xf
  149. 004034d0 00402130 0040212a 0040203a 00402034 Test!CObject::AssertValid+0xf
  150. 004034d4 0040212a 0040203a 00402034 0040202e Test!CDialog::OnCmdMsg
  151. 004034d8 0040203a 00402034 0040202e 00402028 Test!CWnd::OnFinalRelease
  152. 004034dc 00402034 0040202e 00402028 00402022 Test!CCmdTarget::IsInvokeAllowed
  153. 004034e0 0040202e 00402028 00402022 00401c70 Test!CCmdTarget::GetDispatchIID
  154. 004034e4 00402028 00402022 00401c70 0040201c Test!CCmdTarget::GetTypeInfoCount
  155. 004034e8 00402022 00401c70 0040201c 00402016 Test!CCmdTarget::GetTypeLibCache
  156. 004034ec 00401c6f 0040201c 00402016 00402010 Test!CCmdTarget::GetTypeLib
  157. 004034f0 0040201c 00402016 00402010 0040200a Test!CTestDlg::_GetBaseMessageMap+0xf
  158. 004034f4 00402016 00402010 0040200a 00402004 Test!CCmdTarget::GetCommandMap
  159. 004034f8 00402010 0040200a 00402004 00401ffe Test!CCmdTarget::GetDispatchMap
  160. 004034fc 0040200a 00402004 00401ffe 00401ff8 Test!CCmdTarget::GetConnectionMap
  161. 00403500 00402004 00401ffe 00401ff8 00401ff2 Test!CCmdTarget::GetInterfaceMap
  162. 00403504 00401ffe 00401ff8 00401ff2 00401fec Test!CCmdTarget::GetEventSinkMap
  163. 00403508 00401ff8 00401ff2 00401fec 00402124 Test!CCmdTarget::OnCreateAggregates
  164. 00403608 004022fc 00402310 00000000 19930520 Test!CCmdTarget::GetInterfaceHook
  165. 0040360c 00402310 00000000 19930520 00000008 Test!WinMainCRTStartup+0x13e
  166. 00403610 00000000 19930520 00000008 00403638 Test!WinMainCRTStartup+0x152


  167. STACK_COMMAND: ~0s; .ecxr ; kb

  168. FAULTING_SOURCE_CODE:
  169. 137: }
  170. 138:
  171. 139: void CTestDlg::Fun3(char *pszBuffer)
  172. 140: {
  173. > 141: pszBuffer[1] = 0x00;
  174. 142: }
  175. 143:
  176. 144: BOOL CTestDlg::OnInitDialog()
  177. 145: {
  178. 146: CDialog::OnInitDialog();


  179. SYMBOL_STACK_INDEX: 0

  180. SYMBOL_NAME: Test!CTestDlg::Fun3+6

  181. FOLLOWUP_NAME: MachineOwner

  182. IMAGE_NAME: Test.exe

  183. BUCKET_ID: WRONG_SYMBOLS

  184. FAILURE_BUCKET_ID: NULL_CLASS_PTR_DEREFERENCE_c0000005_Test.exe!CTestDlg::Fun3

  185. WATSON_STAGEONE_URL: http://watson.microsoft.com/StageOne/Test_exe/1_0_0_1/4fc48236/Test_exe/1_0_0_1/4fc48236/c0000005/00001ca6.htm?Retriage=1

  186. Followup: MachineOwner
  187. ---------
复制代码
OK ,这样我们就能在发布版本的程序中,准确的定位到哪个函数出了问题,所以发布程序时,一定要记得生成pdb、map文件,不然客户运行出错的话,你不死也残!
测试工程下载地址:

利用WinDbg找出程序崩溃的代码行号第9张DumpTest.rar
原文链接:
http://blog.csdn.net/wangningyu/article/details/6748138

免责声明:文章转载自《利用WinDbg找出程序崩溃的代码行号》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Sql Server查询性能优化之走出索引的误区SQL Server 2016 CTP2.2 的关键特性下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

shell脚本编写

一、shell script概念 可以将shell终端解释器作为人与计算机硬件之间的“翻译官”,作为用户与Linux系统内部的通信媒介。 shell脚本命令的工作方式: 1.交互式(Interactive):用户每输入一条命令就立刻执行。 2.批处理(Batch):由用户事先编写好一个完整的shell脚本,脚本会一次性执行完所有的命令。 在shell sc...

Python-单元测试 unittest &amp;amp; HTMLTestRunner模块产生的测试报告

 1、单元测试: ——开发程序的人测已经已经写好的代码。 unittest框架,执行的顺序是按照方法名的字母来排序的 setUpClass方法是最开始执行的,只会执行一次 tearDownClass是最后执行的,只会执行一次 setUp方法是在每个测试用例执行前会执行 tearDown方法是在每个用例执行后会执行 import unittest de...

Visual Studio 2008 中程序路径配置 .

Visual Studio 2008 环境变量的配置(改为:Visual Studio 2008 中程序路径配置 更合理) 在调试 Visual Studio 2008 程序时,经常有一些动态链接库(即 dll 文件)需要加载到工程里,这样才能依赖第三方库进行程序调试。 这些动态链接库,往往都是测试版本或是开发中的版本,或者会有若干个版本;这个时候,...

Pytest自动化测试

Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。 1、严重性 如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个类。 它以 allure.severity_level 枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),...

Linux命令集合

批量改名文件 for var in `ls *.bak`;do mv -f "$var" `echo "$var"|sed 's/....$//'`;done //将去除文件名字后面的 .bak for var in `ls *.bak`;do mv -f "$var" `echo "$var"|sed 's/...$/xxx/'`;done /...

sysbench对自装MySQL数据库进行基准测试

一、 安装sysbench wget https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh chmod +x script.rpm.sh ./script.rpm.sh yum install -y sysbench 二、准备测试表 sysbench //...