上周和你们分享了.NET 5开源工做流框架elsa,程序跑起来后,想看一下后台线程的执行状况。抓了个进程Dump后,使用WinDbg调试,加载SOS调试器扩展,结果没法正常使用了:html
0:000> .loadby sos clr
Unable to find module 'clr'git
这引发了我的的兴趣,必需要从新掌握.NET 5 / .NET Core 下WinDbg调试技能。那么,咱们就开始吧:github
1、先安装WinDbg架构
推荐的下载连接(老版本的WinDbg):https://raw.githubusercontent.com/EasyDarwin/Tools/master/Windbg_x86_x64/dbg_amd64.msi框架
若是各位想尝鲜,也能够从Windows Store下载 WingDbg Preview版本async
下载后,一步一步安装便可。post
启动后的界面:学习
2、安装最新版本的dotnet-sos测试
使用SOS调试器扩展,可使用本地调试器(WinDbg、lldb)调试.NET Core 程序。ui
推荐你们详细学习参考这篇文档:dotnet-sos install
关于SOS调试器扩展,推荐你们看这篇连接:SOS调试器扩展
咱们使用dotnet global tool 下载安装最新的dotnet-sos Nuget包
dotnet tool install --global dotnet-sos
安装成功后,咱们须要继续安装dotnet-sos
dotnet-sos install [--architecture <arch>]
架构有如下选项:
安装完成后,有这么一条提示:
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调试器扩展了。
3、新建.NET 5应用,运行起来抓Dump
调试环境ready后,咱们启动.NET 5 WinDbg调试了
首先咱们找个.NET 5 Console应用(你们能够本身新建一个),这里我使用了上次研究elsa的测试工程了:
测试代码:
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 跑起来:
在Windows 任务管理器中抓个Dump
4、使用WinDbg调试.NET 5 应用
在上一步中,咱们抓了一个Dump文件:C:\Users\zhougq\AppData\Local\Temp\ElsaRecurringTaskWorkflow.DMP
咱们打开Windbg
而后打开咱们刚才抓的Dump文件:Open Dump File
首次打开会比较慢,WinDbg会尝试下载所须要的pdb调试符号,稍等一会便可。
下载复制完成后,咱们就能够开始调试了:
首先,加载SOS扩展:
.load C:\Users\zhougq\.dotnet\sos\sos.dll
接下来,你们能够根据须要去不一样的调试指令了,例如!runaway !threadpool !syncblk等:
详细的WinDbg调试交差你们能够参考:
https://www.cnblogs.com/tianqing/p/11307049.html
以上是使用WinDbg调试.NET 5的技术分享,下一篇将给你们继续分享Linux抓Dump分享的技能。
推荐几个不错的连接:
周国庆
2021/1/17