7

.NET 5 程序高级调试-WinDbg

 3 years ago
source link: http://www.cnblogs.com/tianqing/p/14289170.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

上周和大家分享了.NET 5开源工作流框架elsa,程序跑起来后,想看一下后台线程的执行情况。抓了个进程Dump后,使用WinDbg调试,加载SOS调试器扩展,结果无法正常使用了:

0:000> .loadby sos clr

Unable to find module 'clr'

这引起了个人的兴趣,必须要重新掌握.NET 5 / .NET Core 下WinDbg调试技能。那么,我们就开始吧:

一、先安装WinDbg

推荐的下载链接(老版本的WinDbg): https://raw.githubusercontent.com/EasyDarwin/Tools/master/Windbg_x86_x64/dbg_amd64.msi

如果各位想尝鲜,也可以从Windows Store下载 WingDbg Preview版本

amYVr2b.jpg!mobile

下载后,一步一步安装即可。

JZ7rAvR.jpg!mobile

启动后的界面:

zuumam.jpg!mobile

二、安装最新版本的dotnet-sos

使用SOS调试器扩展,可以使用本地调试器(WinDbg、lldb)调试.NET Core 程序。

推荐大家详细学习参考这篇文档: dotnet-sos install

关于SOS调试器扩展,推荐大家看这篇链接: SOS调试器扩展

我们使用dotnet global tool 下载安装最新的 dotnet-sos Nuget包

NV73qaB.jpg!mobile

dotnet tool install --global dotnet-sos

2Q3IJbn.jpg!mobile

安装成功后,我们需要继续安装dotnet-sos

EjmYJrA.jpg!mobile

dotnet-sos install [--architecture <arch>]

架构有以下选项:

  • Arm
  • Arm64
  • X86
  • X64

bERr6zu.jpg!mobile

安装完成后,有这么一条提示:

Execute ' .load C:\Users\zhougq\.dotnet\sos\sos.dll ' to load SOS in your Windows debugger.

总结以下:WinDbg or cdb by running .load %USERPROFILE%\.dotnet\sos\sos.dll in the debugger.

原先我们使用.load by sos,在.NET Core 或者 .NET 5中需要直接按指定目录加载SOS调试器扩展了。

三、新建.NET 5应用,运行起来抓Dump

调试环境ready后,我们启动.NET 5 WinDbg调试了

首先我们找个.NET 5 Console应用(大家可以自己新建一个),这里我使用了上次研究elsa的测试工程了:

u2Yz6fZ.jpg!mobile

测试代码:

 1 using Microsoft.Extensions.DependencyInjection;
 2 using Microsoft.Extensions.Hosting;
 3 using Microsoft.Extensions.Logging;
 4 using System;
 5 using System.Threading.Tasks;
 6 using Elsa.Activities.Console.Activities;
 7 using Elsa.Activities.Console.Extensions;
 8 using Elsa.Activities.Timers.Extensions;
 9 using Elsa.Expressions;
10 using Elsa.Extensions;
11 using Elsa.Services;
12 using NodaTime;
13 
14 namespace ElsaRecurringTaskWorkflow
15 {
16     using Elsa.Activities.Console.Extensions;
17 
18     class Program
19     {        
20         static async Task Main(string[] args)
21         {
22             var host = new HostBuilder()
23                 .ConfigureServices(ConfigureServices)
24                 .ConfigureLogging(logging => logging.AddConsole())
25                 .UseConsoleLifetime()
26                 .Build();
27 
28             using (host)
29             {
30                 await host.StartAsync();
31                 await host.WaitForShutdownAsync();
32             }
33         }
34 
35         private static void ConfigureServices(IServiceCollection services)
36         {            
37             services             
38                 .AddElsaCore()
39                 .AddConsoleActivities()             
40                 .AddTimerActivities(options => options.Configure(x => x.SweepInterval = Duration.FromSeconds(1)))
41                 .AddWorkflow<RecurringTaskWorkflow>();
42         }
43     }
44 }

Run 跑起来:

3q22E3Q.jpg!mobile

在Windows 任务管理器中抓个Dump

rABV7zQ.jpg!mobile

四、使用WinDbg调试.NET 5 应用

在上一步中,我们抓了一个Dump文件:C:\Users\zhougq\AppData\Local\Temp\ElsaRecurringTaskWorkflow.DMP

我们打开Windbg

2eEFvu2.jpg!mobile

然后打开我们刚才抓的Dump文件:Open Dump File

a2Y7n2y.jpg!mobile

首次打开会比较慢,WinDbg会尝试下载所需要的pdb调试符号,稍等一会即可。

ZjiM7be.jpg!mobile

下载复制完成后,我们就可以开始调试了:

首先,加载SOS扩展:

.load C:\Users\zhougq\.dotnet\sos\sos.dll

ruEFn22.jpg!mobile

接下来,大家可以根据需要去不同的调试指令了,例如!runaway  !threadpool !syncblk等:

V3AzQ3m.jpg!mobile

详细的WinDbg调试交差大家可以参考:

https://www.cnblogs.com/tianqing/p/11307049.html

[置顶] Windbg程序调试系列-索引篇

以上是使用WinDbg调试.NET 5的技术分享,下一篇将给大家继续分享Linux抓Dump分享的技能。

推荐几个不错的链接:

dotnet-sos install

SOS调试器扩展

周国庆

2021/1/17


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK