.NET 5 程序高级调试-WinDbg
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版本
下载后,一步一步安装即可。
启动后的界面:
二、安装最新版本的dotnet-sos
使用SOS调试器扩展,可以使用本地调试器(WinDbg、lldb)调试.NET Core 程序。
推荐大家详细学习参考这篇文档: dotnet-sos install
关于SOS调试器扩展,推荐大家看这篇链接: SOS调试器扩展
我们使用dotnet global tool 下载安装最新的 dotnet-sos Nuget包
dotnet tool install --global dotnet-sos
安装成功后,我们需要继续安装dotnet-sos
dotnet-sos install [--architecture <arch>]
架构有以下选项:
- Arm
- Arm64
- X86
- X64
安装完成后,有这么一条提示:
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的测试工程了:
测试代码:
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
四、使用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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK