如何使用 WinDbg 分析 VC++ 应用程序的故障转储? [英] How to use WinDbg to analyze the crash dump for VC++ application?

查看:38
本文介绍了如何使用 WinDbg 分析 VC++ 应用程序的故障转储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

以下是一些可助您顺利进行的一般步骤:

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

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

既然您已经从客户那里收到转储文件,您就知道创建转储的应用程序的确切版本,并且您已经找到了此构建的 PDB 文件.

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

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

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

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

要使用 Visual Studio,您要做的就是像打开项目一样打开转储文件.打开后,运行"转储文件(默认为 F5),如果所有路径都设置正确,它将带您直接进入崩溃的代码,为您提供调用堆栈等.>

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

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

片刻之后,如果一切配置正确,WinDbg 将带您到崩溃的位置.在这一点上,您有数百万个选项可以深入挖掘应用程序的内存空间、临界区的状态、窗口等.但这超出了本文的范围.

祝你好运!

How do I use WinDbg for analyzing a dump file?

解决方案

Here are some general steps that will get you on your way:

First, you must change your compiler's settings so that it creates PDB files, even for release builds. Later versions of the Visual C++ compiler do this by default, but in many versions of Visual C++ you must do this yourself. Create program database files, and then keep an archive of those files along with each build of your application. It is critical that every build of your applications has its own set of PDBs. You can't just reuse the same ones you made with build 10 to examining the dumps generated by build 15, for example. Over the life of your project, you will end up with a ton of PDBs, so be prepared for that.

Next, you need to be able to identify the exact version of your application which generated the dump file. If you are creating your own MiniDumps (by calling MiniDumpWriteDump() for example), probably the easiest way to do this is to simply make part of the filename of the MiniDump the complete version number of your application. You'll need to have a reasonable version numbering scheme in place for this to work. In my shop, we increment the build number across all branches by one every time the autobuilder creates a build.

Now that you have received the dump file from the customer, you know the precise version of the application that created the dump, and you have found the PDB files for this build.

Now you need to go through your source control's history and find the source code for this exact version of the software. The best way to do this is to apply 'labels' to your branches every time you make a build. Set the value of the label to the exact version number, and it becomes easy to find in the history.

You're almost ready to fire up WinDbg/Visual C++:

  1. Get the complete source tree for that version of your application. Put it in a separate place on your hard drive, say c:app_build_1.0.100 for application version 1.0 build #100.
  2. Get the binaries for that exact version of your application and put them somewhere on your hard drive. It might be easiest simply to install that version of your application to get the binaries.
  3. Put the PDB files in the same location as the binaries in step 2.

Now you have two options for viewing the dump file. You can use Visual Studio or WinDbg. Using Visual Studio is easier, but WinDbg is much more powerful. Most of the time the functionality in Visual Studio will suffice.

To use Visual Studio, all you have to do is open the dump file like it is a project. Once opened, "run" the dump file (F5 by default) and if all the paths are set correctly it will take you right to the code that crashed, give you a call stack, etc.

To use WinDbg, you have to jump through a couple of hoops:

  1. Start WinDbg
  2. Open the dump file. (Ctrl + D by default)
  3. Tell WinDbg to go get the correct MicroSoft symbol files. Type .symfix. This may take a few moments as it will pull a ton of stuff down from the Internet.
  4. Tell WinDbg where the symbols (PDB files) are. Type .sympath+ c:pdblocation, substituting wherever you put the PDB files for the pathname. Make sure you get the plus sign in there with no whitespace between .sympath and the + sign or else you'll screw up step 3.
  5. Tell WinDbg where the source code is. Type .srcpath c:app_build_1.0.100 substituting the path where you got code from source control for this version of the software.
  6. Tell WinDbg to analyze the dump file. Type !analyze -v

After a few moments, if everything is configured correctly, WinDbg will take you right to the location of your crash. At this point you have a million options for digging deep into your application's memory space, the state of critical sections, windows, etc. But that is way beyond the scope of this post.

Good luck!

这篇关于如何使用 WinDbg 分析 VC++ 应用程序的故障转储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆