在分析 DUMP 进行自动化调试的时候,很多时候只能通过 WinDbg 和命令行调用的方式,这样的方式很难做到灵活。同时编写各个命令行的难度也特别高,这在需要对命令行的输出进行不同的分支的判断时候,难度会更大。于是找到了 Microsoft.Diagnostics.Runtime 库,这个库提供了简单的方式,可以在 C# 里面用代码写分析 DUMP 的代码

需要先在 NuGet 上添加一个私有的源才能使用这个库,添加私有源的方式请看 VisualStudio 给项目添加特殊的 Nuget 的链接

本文这里就添加了NuGet.config文件,详细内容请看github代码

<?xml version="1.0" encoding="utf-8"?>
<!-- This file should be kept in sync across https://www.github.com/dotnet/wpf and dotnet-wpf-int repos. -->
<configuration>
  <packageSources>
    <clear />
    <add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
    <add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
    <add key="dotnet5-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5-transport/nuget/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
  </packageSources>
</configuration>

右击项目管理 NuGet 然后输入 Microsoft.Diagnostics.Runtime 然后切换到 dotnet-tools 源

找到一个有趣的项目,让他跑起来,然后炸掉他,这样就能拿到一个 DUMP 文件了。当然不炸掉他也可以,在任务管理器右击创建转储文件

假设放在 f:\lindexi\test\dotnet.exe.52348.dmp 文件,可以使用下面代码进行分析

 using (DataTarget dataTarget = DataTarget.LoadCrashDump(@"f:\lindexi\test\dotnet.exe.52348.dmp"))

此时就可以拿到 dataTarget 的 Clr 版本,请看代码

               foreach (ClrInfo version in dataTarget.ClrVersions)
                {
                    Console.WriteLine("Found CLR Version: " + version.Version);

                    // This is the data needed to request the dac from the symbol server:
                    ModuleInfo moduleInfo = version.ModuleInfo;

                    Console.WriteLine("Filesize:  {0:X}", moduleInfo.IndexFileSize);
                    Console.WriteLine("Timestamp: {0:X}", moduleInfo.IndexTimeStamp);
                    Console.WriteLine("Dac File:  {0}", moduleInfo.FileName);

                    // If we just happen to have the correct dac file installed on the machine,
                    // the "LocalMatchingDac" property will return its location on disk:
                    string dacLocation = version.LocalMatchingDac;
                    if (!string.IsNullOrEmpty(dacLocation))
                        Console.WriteLine("Local dac location: " + dacLocation);

                    // You may also download the dac from the symbol server, which is covered
                    // in a later section of this tutorial.
                }

更多的方法就请小伙伴自己去找找 API 了

本文只是一个简单的入门的博客,告诉大家有这个技术,因为这方向的调试技术需要涉及的技术特别多,也不是本渣能一篇博客能说明白的

本文代码放在github欢迎小伙伴访问


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-%E7%94%A8-Microsoft.Diagnostics.Runtime-%E5%BA%93%E5%86%99%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90-DUMP-%E6%96%87%E4%BB%B6.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利