.NET 5 程序高级调试-WinDbg
上周和大家分享了.NET 5開(kāi)源工作流框架elsa,程序跑起來(lái)后,想看一下后臺(tái)線程的執(zhí)行情況。抓了個(gè)進(jìn)程Dump后,使用WinDbg調(diào)試,加載SOS調(diào)試器擴(kuò)展,結(jié)果無(wú)法正常使用了:
0:000> .loadby sos clr
Unable to find module 'clr'
這引起了個(gè)人的興趣,必須要重新掌握.NET 5 / .NET Core 下WinDbg調(diào)試技能。那么,我們就開(kāi)始吧:
一、先安裝WinDbg
推薦的下載鏈接(老版本的WinDbg):https://raw.githubusercontent.com/EasyDarwin/Tools/master/Windbg_x86_x64/dbg_amd64.msi
如果各位想嘗鮮,也可以從Windows Store下載 WingDbg Preview版本
?下載后,一步一步安裝即可。
?
?啟動(dòng)后的界面:
?
二、安裝最新版本的dotnet-sos
? 使用SOS調(diào)試器擴(kuò)展,可以使用本地調(diào)試器(WinDbg、lldb)調(diào)試.NET Core 程序。
? 推薦大家詳細(xì)學(xué)習(xí)參考這篇文檔:dotnet-sos install
? 關(guān)于SOS調(diào)試器擴(kuò)展,推薦大家看這篇鏈接:SOS調(diào)試器擴(kuò)展
? 我們使用dotnet global tool 下載安裝最新的dotnet-sos Nuget包
??
? ? dotnet tool install --global dotnet-sos
? ?
? ?安裝成功后,我們需要繼續(xù)安裝dotnet-sos?
? ?
? ?dotnet-sos install [--architecture <arch>]
? ? 架構(gòu)有以下選項(xiàng):
Arm
Arm64
X86
X64
? ?
? ?安裝完成后,有這么一條提示:
? ?Execute '.load C:\Users\zhougq\.dotnet\sos\sos.dll' to load SOS in your Windows debugger.
? ?總結(jié)以下:WinDbg or cdb by running .load %USERPROFILE%\.dotnet\sos\sos.dll in the debugger.
? ?原先我們使用.load by sos,在.NET Core 或者 .NET 5中需要直接按指定目錄加載SOS調(diào)試器擴(kuò)展了。
三、新建.NET 5應(yīng)用,運(yùn)行起來(lái)抓Dump
? ?調(diào)試環(huán)境ready后,我們啟動(dòng).NET 5 WinDbg調(diào)試了
? ?首先我們找個(gè).NET 5 Console應(yīng)用(大家可以自己新建一個(gè)),這里我使用了上次研究elsa的測(cè)試工程了:
? ?
? ?測(cè)試代碼:
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 跑起來(lái):
?
?在Windows 任務(wù)管理器中抓個(gè)Dump
?
四、使用WinDbg調(diào)試.NET 5 應(yīng)用
? ?在上一步中,我們抓了一個(gè)Dump文件:C:\Users\zhougq\AppData\Local\Temp\ElsaRecurringTaskWorkflow.DMP
? ?我們打開(kāi)Windbg
? ?
? ?然后打開(kāi)我們剛才抓的Dump文件:Open Dump File
? ?
首次打開(kāi)會(huì)比較慢,WinDbg會(huì)嘗試下載所需要的pdb調(diào)試符號(hào),稍等一會(huì)即可。
?
下載復(fù)制完成后,我們就可以開(kāi)始調(diào)試了:
首先,加載SOS擴(kuò)展:
.load C:\Users\zhougq\.dotnet\sos\sos.dll
?
接下來(lái),大家可以根據(jù)需要去不同的調(diào)試指令了,例如!runaway ?!threadpool !syncblk等:
詳細(xì)的WinDbg調(diào)試交差大家可以參考:
https://www.cnblogs.com/tianqing/p/11307049.html
[置頂]?Windbg程序調(diào)試系列-索引篇
?
以上是使用WinDbg調(diào)試.NET 5的技術(shù)分享,下一篇將給大家繼續(xù)分享Linux抓Dump分享的技能。
推薦幾個(gè)不錯(cuò)的鏈接:
? ? ? ??dotnet-sos install
? ? ? ??SOS調(diào)試器擴(kuò)展
總結(jié)
以上是生活随笔為你收集整理的.NET 5 程序高级调试-WinDbg的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何在 C# 8 中使用 模式匹配
- 下一篇: .Net在线编辑工具.NET Fiddl