WinDbg调试托管程序环境问题总结

基本环境搭建及安装

安装

有2个方式可以安装WinDbg。

  1. 新版 安装WinDbg Preview
    在商店里搜WinDbg直接就可以安装,这里安装的版本是x64版本。x64版本的WinDbg其实是可以调试x86版本的程序,直接附加到进程就可以了,这也是我们推荐的方式。
  2. 旧版 安装windows sdk

这里建议新旧两个版本都安装,实际调试时候都可能需要用到。

配置与调试

符号文件配置

sympath: WinDbg 查找 symbols的地址.

symbols 能优化我们的调试体验,我们可以通过如下命令查看 sympath :

.sympath

一般通过两种种方式设置sympath。参考Symbol path for Windows debuggers.

  • 系统环境变量

    增加系统环境变量: _NT_SYMBOL_PATH 对应的值为:
    SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols

  • 调试会话中设置

    可以再调试种如下加载symbols :

.sympath C:\symbols\local;srv*C:\symbols\microsoft*https://msdl.microsoft.com/download/symbols
.reload

加载SOS.dll

SOS是在ntsd或windbag下调试.Net程序的扩展。有两种方式可以加载。
- .loadby
一般使用.loadby sos clr (针对.net framework 4.x)没有报错就代表正确加载了。实际上根据情况有如下几种选择:

    .loadby sos mscorsvr
    .loadby sos mscorwks
    .loadby sos clr
    .loadby sos coreclr
    .loadby sos <somethingelse>

如何选择参考链接: .net - Cannot .loadby sos mscorwks or .loadby sos clr - Stack Overflow

 - `.load`

对第一种方式的不能正确加载的情况,可以自行加载,使用方式如下:

.load C:\Windows\Microsoft.NET\Framework64\v4.0.30319\sos.dll

接下来就能愉快的使用!eeheap等命令调试了。

注意:不要在.net clr还没有加载的时候就尝试加载sos 键入g直到clr加载后再中断重新加载sos.dll。

特殊情况的处理

如果只调试自己电脑上的程序那么一般情况比较简单,但调试客户的程序会遇到各种各样的情况。例如如下两种最常见的场景。

1. 32位兼容程序的调试

有时我们在生成项目时,选择了优先32位。生成32的程序在我们现在的64位环境下,会运行在wow64下。
这个时候我们是可以直接使用x64版本的windbg直接附加到进程调试的,但是对客户的机器上的程序,我们很多时候都需要生成dump来分析当时的问题。直接用默认的64位任务管理器生成的dump并不能直接分析,给bug的分析带来很多困难。我们可能会看到如下提示信息。

  0:000> !eeheap
  SOS does not support the current target architecture.

或者,

Failed to load data access DLL, 0x80004005 
Verify that 1) you have a recent build of the debugger (6.2.14 or newer) 
            2) the file mscordacwks.dll that matches your version of mscorwks.dll is 
                in the version directory 
            3) or, if you are debugging a dump file, verify that the file 
                mscordacwks_<arch>_<arch>_<version>.dll is on your symbol path. 
            4) you are debugging on the same architecture as the dump file. 
                For example, an IA64 dump file must be debugged on an IA64 
                machine.

You can also run the debugger command .cordll to control the debugger's 
load of mscordacwks.dll.  .cordll -ve -u -l will do a verbose reload. 
If that succeeds, the SOS command should work on retry.

If you are debugging a minidump, you need to make sure that your executable 
path is pointing to mscorwks.dll as well.

按照提示输入.cordll -ve -u -l后有如下提示。

  .cordll -ve -u -l 
  CLR DLL status: No load attempts

使用windbg preview时可以在堆栈中看到wow64等字样就是遇到了这种问题。

我们可以通过如下方式解决:

    1. 使用32位的任务管理器重新抓dump。
      32位任务管理器位于 C:\Windows\SysWOW64\Taskmgr.exe下。参考链接:.Net Dump 的分析 I | 非洲海星的深海大鳳梨 (wordpress.com)
    1. 使用soswow64加载sos
      这里一定要使用旧版的windbg x86版本并下载soswow64放到指定目录下。
      使用 .load soswow64 加载后就能使用sos中的扩展命令。github中的readme说的非常清楚。

参考链接:poizan42/soswow64: windbg/dbgeng extension for debugging 64-bit dumps of 32-bit .NET processes. (github.com)

2. clr运行环境不符的调试

有时调试的目标dump的clr版本与本地并不相符,使用 .cordll -ve -u -l 命令有如下类似提示。
unable to find mscordacwks_x86_x86_4.7.3110.00.dll by mscorwks search
可以按提示拷贝目标机器上的相应dll,重命名后放到windbg的目录下。
参考:
Windbg调试SOS.DLL和CLR 不匹配问题_SpringDou的博客-CSDN博客

3. 调试其他机器上的内存转储文件

参考原文地址

需要目标计算机上的如下DLLs:

  • mscordacwks.dll
  • SOS.dll

他们位于当前的.NET framework中: C:\Windows\Microsoft.NETDebugging Managed Code Using the Windows Debugger .

你需要把他们加载到WinDbg:

  • SOS.load C:\path-to-dll\SOS.dll

  • mscordacwks.cordll -lp C:\directory-in-which-mscordacwks-is-located

    • 路径不要带上mscordacwks.dll (例如,如果路径是 C:\dlls\mscordacwks.dll ,则使用如下命令 .cordll -lp C:\dlls)

4. 其他常见问题

参考nuggets/README.md at main · gabrielweyer/nuggets (github.com)

Q&A

其他参考链接

  1. dotnet core下的配置。教你配置windows上的windbg,linux上的lldb,打入clr内部这一篇就够了 - 掘金 (juejin.cn)
  2. windbg - Failed to load data access DLL, 0x80004005 - Stack Overflow
  3. "Failed to load data access DLL, 0x80004005" when debugging a live process on local machine (microsoft.com)
  4. How to use Windbg to debug a dump of a 32bit .NET app running on a x64 machine - PKI Extensions (sysadmins.lv)
  5. visual studio 2010 - Debugging dump of 32-bit process captured on 64-bit machine - Stack Overflow
  6. 调试运行在Wow64子系统下的程序----x64版windbg调试win32程序_lixiangminghate的专栏-CSDN博客
posted @ 2021-11-28 13:18  时风拖拉机  阅读(311)  评论(0编辑  收藏  举报