如何使用 WinDbg 分析 VC++ 应用程序的故障转储?

Posted

技术标签:

【中文标题】如何使用 WinDbg 分析 VC++ 应用程序的故障转储?【英文标题】:How to use WinDbg to analyze the crash dump for VC++ application? 【发布时间】:2010-10-18 13:39:55 【问题描述】:

如何使用WinDbg 分析转储文件?

【问题讨论】:

【参考方案1】:

以下是一些通用步骤,可帮助您顺利上路:

首先,您必须更改编译器的设置,以便它创建 PDB 文件,即使对于发布版本也是如此。 Visual C++ 编译器的更高版本默认执行此操作,但在 Visual C++ 的许多版本中,您必须自己执行此操作。创建程序数据库文件,然后将这些文件与应用程序的每个构建一起保存。至关重要的是,您的应用程序的每个构建都有自己的一组 PDB。例如,您不能只重用您在构建 10 中创建的相同内容来检查构建 15 生成的转储。在您的项目生命周期中,您最终会得到大量 PDB,因此请做好准备。

接下来,您需要能够识别生成转储文件的应用程序的确切版本。如果您正在创建自己的 MiniDump(例如,通过调用 MiniDumpWriteDump()),最简单的方法可能是简单地将 MiniDump 的文件名的一部分作为应用程序的完整版本号。您需要有一个合理的版本编号方案才能正常工作。在我的商店中,每次自动构建器创建构建时,我们都会将所有分支的构建号加一。

现在您已收到客户的转储文件,您知道创建转储的应用程序的确切版本,并且您已找到此构建的 PDB 文件。

现在您需要查看源代码管理的历史并找到该软件版本的源代码。最好的方法是在每次构建时将“标签”应用于分支。将标签的值设置为准确的版本号,在历史中就很容易找到了。

您几乎已经准备好启动 WinDbg/Visual C++:

    获取该版本应用程序的完整源代码树。将其放在硬盘驱动器上的单独位置,例如 c:\app_build_1.0.100 用于应用程序版本 1.0 build #100。 为您的应用程序的确切版本获取二进制文件并将它们放在硬盘上的某个位置。安装该版本的应用程序以获取二进制文件可能是最简单的方法。 将 PDB 文件放在与步骤 2 中的二进制文件相同的位置。

现在您有两种查看转储文件的选项。您可以使用Visual Studio 或 WinDbg。使用 Visual Studio 更容易,但 WinDbg 更强大。大多数情况下,Visual Studio 中的功能就足够了。

要使用 Visual Studio,您只需像打开项目一样打开转储文件。打开后,“运行”转储文件(默认为 F5),如果所有路径设置正确,它将带您直接找到崩溃的代码,为您提供调用堆栈等。

要使用 WinDbg,您必须跳过几个环节:

    启动 WinDbg 打开转储文件。 (Ctrl + D 默认) 告诉 WinDbg 去获取正确的 MicroSoft 符号文件。输入.symfix。这可能需要一些时间,因为它会从 Internet 上下载大量内容。 告诉 WinDbg 符号(PDB 文件)在哪里。键入.sympath+ c:\pdblocation,用放置 PDB 文件的位置替换路径名。确保你在那里得到了加号,.sympath+ 之间没有空格,否则你会搞砸第 3 步。 告诉 WinDbg 源代码在哪里。输入 .srcpath c:\app_build_1.0.100 替换您从源代码控制获取此版本软件的代码的路径。 告诉 WinDbg 分析转储文件。输入!analyze -v

片刻之后,如果一切配置正确,WinDbg 会直接将您带到崩溃的位置。此时,您有上百万个选项可以深入挖掘应用程序的内存空间、关键部分的状态、窗口等。但这方式超出了本文的范围。

祝你好运!

【讨论】:

非常感谢您提供详细信息。我会尝试你的步骤,Is .symfix 会给出任何输出。我们怎么知道命令执行完成与否? 状态栏中有一个 BUSY 指示器,它会告诉您 WinDbg 何时正忙于处理您的命令。除此之外,除非您收到错误消息,否则您不会收到说明命令已完成的响应。但是请注意 !analyze -v 的输出。如果您没有任何模块的正确 PDB,它会抱怨。 使用符号索引和源索引将简化定位匹配 PDB 和源代码的工作。此外,索引同时支持 Visual Studio 和 WinDbg。 @Thomson:我不确定我知道你在说什么,但如果你能通过答案进行详细说明,我敢打赌这会大大增强这个问题。这个问题得到了很多关注。 是否可以在不同时向二进制文件中添加大量调试信息的情况下创建 .PDB 文件?【参考方案2】:

(请参阅下面的“转储”部分)

WinDbg使用基础教程及演示

Installing and Configuring WinDbg (Windows Debug Tools) Mike Taulty - A word for WinDBG WinDbg Tutorials Windows Debuggers: Part 1: A WinDbg Tutorial

“启动”/附加 WinDBG 的不同方式

Start Debugging with Windbg (includes how to debug an .msi) How to debug a Windows service Setting up Windows Debugging

工作区

了解工作区的工作原理...

Pimp up your debugger: Creating a custom workspace for windbg debugging Uncovering How Workspaces Work in WinDbg

命令树

“cmdtree”允许您定义调试器命令的“菜单”,以便轻松访问常用命令,而无需记住简洁的命令名称。

您不必将所有命令定义放入同一个 cmdtree 文本文件中……如果您愿意,可以将它们分开并加载多个(然后它们有自己的窗口)。

Amazing helper .cmdtree How do I make a cmdtree window dock at startup in WinDBG Making it easier to debug .net dumps in windbg using .cmdtree Microshaoft Cmdtree Special Command—Execute Commands from a Customized User Interface with .cmdtree

启动脚本

您可以在命令行中使用 -c 选项在启动 WinDBG 时自动运行 WinDBG 脚本。

提供了打开 DML(调试器标记语言)模式、加载特定扩展、设置 .NET 异常断点、设置内核标志(例如,在内核调试时您可能需要更改 DbgPrint 掩码以便查看跟踪信息... .ed nt!Kd_DEFAULT_Mask 0xffffffff),加载cmdtrees等

http://yeilho.blogspot.co.uk/2012/10/windbg-init-script.html Take Control of WinDBG

一个示例脚本:

$$ Include a directory to search for extensions
$$ (point to a source controlled or UNC common directory so that all developers get access)
.extpath+"c:\svn\DevTools\WinDBG\Extensions"
$$ When debugging a driver written with the Windows Driver Framework/KMDF
$$ load this extension that comes from the WinDDK.
!load C:\WinDDK\7600.16385.1\bin\x86\wdfkd.dll
!wdftmffile C:\WinDDK\7600.16385.1\tools\tracing\i386\wdf01009.tmf
$$ load some extensions
.load msec.dll
.load byakugan.dll
.load odbgext.dll
.load sosex
.load psscor4
$$ Make commands that support DML (Debugger Markup Language) use it
.prefer_dml 1
.dml_start
$$ Show NTSTATUS codes in hex by default
.enable_long_status 1
$$ Set default extension
.setdll psscor4
$$ Show all loaded extensions
.chain /D
$$ Load some command trees
.cmdtree c:\svn\DevTools\WinDBG\cmdtree\cmdtree1.txt
.cmdtree c:\svn\DevTools\WinDBG\cmdtree\cmdtree2.txt
$$ Show some help for the extensions
!wdfkd.help
!psscor4.help
.help /D

命令备忘单

Crash Dump Analysis Poster v3.0 SOS Cheat Sheet (.NET 2.0/3.0/3.5) WinDbg cheat sheet (Art of Dev) WinDbg Kernel-Mode Extension Commands Flashcards

扩展

“扩展”允许您扩展 WinDBG 中支持的命令/功能范围。

bigLasagne (bldbgexts & blwdbgue)- 汇编语法高亮和驱动映射工具) BigLib Number Reader Byakugan- 检测反调试方法、vista 堆可视化/仿真、跟踪内存中的缓冲区 Call Flow Analyzer + KnExt CmdHist- 记录您在调试会话中执行的每个命令,以便您可以轻松地重新执行 Core Analyzer- 检查堆结构是否损坏,检测线程共享的对象等 dom WinDBG Extension- (!stlpvector, !idt, !unhex, !grep 等) dumppe- 从内存中转储 PE 文件 Image Viewer Extension (Vladimir Vukićević) Intel UEFI Development Kit Debugger Tool- 调试 UEFI 固件 leaktrap- GDI/USER 句柄跟踪器以帮助进行泄漏检测 Mona(需要 PyKD)- 帮助高级分析/发现漏洞利用的命令集 MSEC- 提供自动崩溃分析和安全风险评估 narly- 列出有关已加载模块的信息,例如是否使用 SafeSEH、ASLR、DEP、/GS(缓冲区安全检查) netext (Rodney Viana)- (!wservice - 列出 WCF 服务对象,!wconfig - 显示 .config 行,!whttp - 列出 HttpContexts,!wselect/!wfrom - 支持类似 SQL 的数组查询) ODbgExt- 打开调试器扩展 OllyMigrate- 将调试对象传递给另一个调试器而不重新启动 Psscor2- SOS 的超集,用于协助调试 .NET 2.0 托管代码 Psscor4- SOS 的超集,用于协助调试 .NET 4 托管代码 PyDBGExt- 允许使用 python 脚本 PyKD- 允许使用 Python 编写 WinDBG 脚本 sdbgext (Nynaeve)-(!valloc, !vallocrwx, !heapalloc, !heapfree, !remotecall, !remotecall64, !loaddll, !unloaddll, !close, !killthread, !adjpriv, !ret) SieExtPub-legacy 扩展...现在内置在 ext.dll 中的 WinDBG SOSEX- 更多帮助调试托管 NET 2.0 或 4.0 代码的命令 SPT/SDBGExt2 (Steve Niemitz)- (!DumpHttpContext, !DumpASPNetRequests, !DumpSqlConnectionPools, !DumpThreadPool 等) Uniqstack- 调试器扩展的源代码(需要 OSR 在线帐户才能访问它) viscope- 代码覆盖率图 Wait Chain Traversal/wct.dll (Codeplex Debugging Extensions- 显示应用程序线程的等待链(帮助查找 deadlocks) windbgshark- 集成 Wireshark 协议分析器以启用 VM 流量操纵和分析 WinDBG Extensions (Sasha Goldstein)- Tracer, WCT, heap_stat, bkb, traverse_map, traverse_vector) WinDBG Highlight (ColorWindbg.dll) [使用谷歌翻译翻译链接]- asm 语法高亮

编写你自己的扩展

Tools of the Trade: Part IV - Developing WinDbg Extension DLLs The Basics of Debugger Extensions: Short Term Effort, Long Term Gain

使用 WinDBG 调试托管代码

Breaking on an Exception Breaking on specific CLR Exception Debugging .Net framework source code within Windbg Debugging exceptions in managed code using Windbg Debugging managed code using WinDbg and SOS.dll Debugging with WinDbg. Deadlocks in Applications. MANAGED DEBUGGING with WINDBG. Introduction and Index Setting .NET breakpoints in Windbg for applications that crash on startup

脚本(C#、PS、Python、WinDBG)

KDAR (Kernel Debugger Anti Rootkit)- WinDBG 脚本集合 Sysnative BSOD Scripts/Processing Apps WinDBG Script library- WinDBG 脚本集合 Scripting MDbg and DbgHostLib- 允许托管代码编写托管调试器 (MDBG) 和 DbgEng ExtCS- 允许通过 C# 脚本控制 WinDBG PowerDBG- 允许通过 Powershell 脚本控制 WinDBG Pykd - 允许通过 Python 脚本控制 WinDBG windbglib - 围绕 WinDBG 的 pykd 扩展的 python 包装库,模仿 immlib(因此您可以使用最初为 Immunity Debugger 编写的脚本)

使用 dbgeng.dll API/WinDBG 工具的调试器/工具

A Simple Dbgeng Based User Mode Debugger Acorns.Debugging NET Deadlock Detector(使用 cdb.exe)(download) CLR Managed Debugger (MDBG) DbgHost - How to control a debugging engine Debug Diagnostic Tool v1.2 (DebugDiag), Ver 2.0 + DebugDiag Blog Dynamorio - 可以与 WinDBG 交互的动态二进制检测工具 IDA + WinDBG plugin GUI WinDBG LeakShell(查找托管漏洞) mdbglib - Managed Debug API PyDbgEng- Windows 调试引擎的 python 包装器 SOSNET - 专注于使用 SOS 扩展并支持 C# 脚本的 WinDBG Fork/替代外壳 SOSNET O2 fork - 将 Rosyln 用于 C# REPL(read-eval-print-loop)脚本引擎的 SOSNET 的分支 VDB/Vivisect (kenshoto) - 提供基于 WinDBG 的跨平台调试 API WinAppDbg + Heappie-WinAppDbg Writing a basic Windows debugger

为事后分析生成故障转储文件的不同方法

DebugDiag 2.0 Dump Cheat Sheet- 包括如何从 Hyper-V、VMWare ESX 和 XenServer 虚拟机生成转储。 Citrix SystemDump Keyboard Keypress Combination MiniDumpWriteDump-(通过应用程序内的 WIN32 API 调用)。 (Example for C# applications) NMI Switch 或 (here)(基于硬件的功能来生成 NMI...通常在高端服务器上找到,例如 HP 或者您可以获得附加 PCI 卡 "Universal PCI Dump Switch")。微软 NMI 技术background。 Procdump System|Advanced System Settings|Startup and Recovery(registry info), (how to configure a Complete (Full) Memory Dump),(how to enable Complete Memory Dump),(how to enable Complete Memory Dump on Windows 7 when PC has lots of memory...normally not available when more than 2GB of memory) Task Manager "Create Dump File" UserDump, instructions(很老的工具) UserModeProcessDumper, instructions Visual Studio "Save Dump As…" WER (Windows Error Reporting....local dumps) WinDBG

转储分析工具

BlueScreenView - 查找在 BSOD 后由 Windows 保存的 minidump .dmp 文件,并提取有关导致崩溃的原因的信息 Debug.Analyzer(可以分析转储文件,插件可以用.NET编写) SAD - Simple After Dump(事后分析器) Volatility - 用于分析转储文件中记录的“内存”的框架 (cheat sheet)

转储相关工具

Citrix dumpcheck - 检查转储文件的一致性(看起来它已被废弃 link + link) dumpchk(调试工具的一部分)- 检查转储文件的一致性 MoonSols Windows Memory Toolkit(以前的 windd) - 将各种原始内存转储文件转换为 WinDBG 兼容的 dmp 文件 vm2dmp - Microsoft Hyper-V VM 状态到内存转储转换器 vmss2core - 将 VMWare 快照文件转换为核心转储文件 (download), (instructions)

内核调试虚拟机

VMKD - 虚拟机 KD 扩展 VirtualKD - (对托管在 VMWare/VirtualBox 中的操作系统的内核调试器支持)

视频

.NET Cracking 101 #2 - WinDbg basics .NET Debugging for the Production Environment (Channel9) dotnetConf - Advanced Debugging with WinDbg and SOS David Truxall "Debugging with WinDBG" Mike Taulty Debugging Memory Leaks oredev 2009 Session: Debugging .NET Applications with WinDbg Pluralsight Advanced Windows Debugging(以及 Pluralsight 的各种其他人) Tess Ferrandez WinDBG (Channel9)

博客

一些博客(混合了本地和托管代码调试)。

Advanced .NET Debugging All Your Base Are Belong To Us(萨沙·戈德施泰因) Analyze-v ASP.NET Debugging Cyberiafreak(线程和高级 Windows 编程和调试) Debug Analyzer.NET Debug and Beyond Debugging Experts Magazine Online Debugging Toolbox(Windbg 脚本、调试和故障排除工具和技术,可帮助您隔离软件问题。) Decrypt my World greggm's WebLog Junfeng Zhang's Windows Programming Notes Kristoffer's tidbits Mark Russinovich's Blog Mike Stalls .NET Debugging Blog Naveen's Blog Never Doubt Thy Debugger (Carlo) Notes from a Dark Corner Ntdebugging Blog(微软全球升级服务团队) Nynaeve. Adventures in Windows debugging and reverse engineering PFE Developer Notes for the Field Visual Studio Debugger Team WinDbg by Volker von Einem

高级文章和教程资源

Advanced Debugging Techniques in WinDbg Debugging Applications for MS.Net and Windows (Powerpoint Slides) Debugging STL Containers with WinDbg Debug Tutorials 1-7 (CodeProject-Toby Opferman) Debugging.tv Developmentor WinDBG Tagged articles Dr Fu's Security Blog - Malware Analysis Tutorials - Reverse Engineering Approach Exploit writing tutorial part 5 : How debugger modules & plugins can speed up basic exploit development Hunting Rootkits Remote Microsoft Windows Server OS Kernel Debugging Using Dell Windows Debugger Utility (DWDU) (DELL(TM) Windows(R) Debugger Utility 1.1 README)

替代调试器

Bokken - (Inguma)(雷达图形用户界面) BugDbg Debug++(尚未发布) Debuggy Discoloured Ring 0 Debugger (download) edb (Linux) FDBG GoBug Hades (Ring 3 debugger with anti debugger detection strategy) Hopper(Linux、OSX 和 Windows)(当前未实现 Windows 调试) Hyperdbg IDA Debugger ImmunityDebugger Nanomite Obsidian (non-intrusive debugger) OllyDBG PEBrowse RaceVB6(VB6 P 代码调试器) radare radare2ui(雷达图形用户界面) Rasta Ring 0 Debugger (RR0D) Syser Kernel Debugger TRW 2000(非常旧的调试器,大约 W9x)+dions plugin archive VisualDux Debugger Wintruder(可扩展调试器) WKTVDebugger(Visual Basic P-Code 的调试器)(download) x64_dbg Zeta Debugger

其他链接

Collaborative RCE Tool Library- 大量的调试器和系统级工具 cr4zyserb- 大量插件和其他调试工具 How to Write a Windows Debugger References (Devon Straw)- 大量链接为您提供详细信息,如果您想编写自己的调试器,例如PDB 文件格式、.DMP 文件格式、PE 文件结构、如何记录堆栈跟踪等。 Tuts4You- 解压器、IDA、OllyDBG、Immunity Debugger 插件等

【讨论】:

我刚刚将此页面添加为书签以供您回答..!! :)【参考方案3】:

这是一个非常广泛的问题。

    第一步是将转储文件加载到 WinDbg 实例中。 接下来,您需要确保已设置符号。 最后,您可以运行命令!analyze -v 对其进行基本分析。您需要为您的代码提供可用的符号信息,以使转储文件有价值。

网站 Memory Dump, Software Trace, Debugging, Malware, Victimware and Intelligence Analysis Portal 对我来说非常有用。我也非常喜欢这本书,Advanced Windows Debugging Mario Hewardt 和 Daniel Pravat。

【讨论】:

【参考方案4】:

Tess Ferrandez 有 a great set of basic tutorials and labs 开始使用 Windbg。我强烈推荐他们。

【讨论】:

以上是关于如何使用 WinDbg 分析 VC++ 应用程序的故障转储?的主要内容,如果未能解决你的问题,请参考以下文章

如果使用WINDBG来调试C语言程序.

使用Windbg分析多线程临界区死锁问题分享

使用Windbg分析多线程临界区死锁问题分享

VC++ 6.0 - WinDbg 堆栈跟踪显示 malloc 处的崩溃点

如何分析windbg中的<unclassified>内存使用情况

调试技巧 —— 如何利用windbg + dump + map分析程序异常