为什么在带有 SOS 和 .Net Core 3.1 的 Windbg 中使用 !bpmd 没有触发我的断点?

Why is my break point not triggered using !bpmd in Windbg with SOS and .Net Core 3.1?

最近了解了用sos在Windbg中调试托管代码的过程。我查看了许多示例,从我所看到的来看,这应该可行。但事实并非如此。这是一个 .net 核心 3.1 应用程序。

我只是想打破下面非常简单的 Main 方法的内部。

namespace ConsoleAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Console App!");
            ThisPrints();
        }

        static void ThisPrints()
        {
            Console.WriteLine("This is a message");
        }
    }
}

我的过程如下

  1. 附加到 Windbg 中的 ConsoleAppTest.exe
  2. 执行sxe ld clrjit
  3. g 这些步骤产生以下输出:
0:000> g
ModLoad: 00007ffe`a0900000 00007ffe`a0930000   C:\Windows\System32\IMM32.DLL
ModLoad: 00007ffe`7df70000 00007ffe`7e004000   C:\Program Files\dotnet\host\fxr.1.8\hostfxr.dll
ModLoad: 00007ffe`64ef0000 00007ffe`64f82000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\hostpolicy.dll
ModLoad: 00007ffe`3c600000 00007ffe`3cb6f000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\coreclr.dll
ModLoad: 00007ffe`9fbb0000 00007ffe`9fcda000   C:\Windows\System32\ole32.dll
ModLoad: 00007ffe`9f690000 00007ffe`9f9e5000   C:\Windows\System32\combase.dll
ModLoad: 00007ffe`a0830000 00007ffe`a08fd000   C:\Windows\System32\OLEAUT32.dll
ModLoad: 00007ffe`9edb0000 00007ffe`9ee05000   C:\Windows\System32\SHLWAPI.dll
ModLoad: 00007ffe`9e840000 00007ffe`9e867000   C:\Windows\System32\bcrypt.dll
(5b4.40e8): Unknown exception - code 04242420 (first chance)
ModLoad: 00007ffe`32430000 00007ffe`32d4d000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Private.CoreLib.dll
ModLoad: 00007ffe`9c000000 00007ffe`9c012000   C:\Windows\SYSTEM32\kernel.appcore.dll
ModLoad: 00007ffe`9e520000 00007ffe`9e59f000   C:\Windows\System32\bcryptPrimitives.dll
ModLoad: 00007ffe`4ad30000 00007ffe`4ae72000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\clrjit.dll
ntdll!NtMapViewOfSection+0x14:
00007ffe`a0a0c2a4 c3              ret
  1. 接下来我执行!bpmd ConsoleAppTest Program.Main
  2. g

问题发生在第5步之后,即使加载了clrjit.dll,在第4步设置后断点也没有命中。我得到的最终输出是:

0:000> !bpmd ConsoleAppTest Program.Main
Adding pending breakpoints...
0:000> g
ModLoad: 00000221`90e50000 00000221`90e58000   C:\Users\Baruc\source\repos\ConsoleAppTest\ConsoleAppTest\bin\Release\netcoreapp3.1\ConsoleAppTest.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
Failed to set code notification
ModLoad: 00007ffe`98f60000 00007ffe`98f6e000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Runtime.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffe`7d360000 00007ffe`7d385000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Console.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffe`82050000 00007ffe`82063000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Threading.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffe`72720000 00007ffe`72753000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Runtime.Extensions.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffe`822b0000 00007ffe`822b3000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.8\System.Text.Encoding.Extensions.dll
(5b4.40e8): CLR notification exception - code e0444143 (first chance)
ntdll!NtFsControlFile+0x14:
00007ffe`a0a0c4c4 c3              ret

任何指导将不胜感激。谢谢!

可以通过显式加载 sos.dll 的正确版本来解决该问题。这需要匹配目标的运行时版本和位数。

请注意,由于 .NET 5.0 现已发布,如果您未明确指定版本,则很可能会获得 .NET 5.0 版本。

这将安装 .NET 5.0 sos:

dotnet tool install -g dotnet-sos

这将安装 .NET 3.1 sos:

dotnet tool install -g dotnet-sos --version 3.1.141901

您还需要显式加载 sos(以确保它与目标的 .NET 版本相匹配 + x86/x64)。我不记得 windbg 将默认使用哪个版本的 sos(如果有)。 (您需要修改下面显示的路径以反映正确的用户名):

.load C:\Users\<username>\.dotnet\tools\.store\dotnet-sos.1.141901\dotnet-sos.1.141901\tools\netcoreapp2.1\any\win-x64\sos.dll

完整跟踪:

0:000> .load C:\Users\<username>\.dotnet\tools\.store\dotnet-sos.1.141901\dotnet-sos.1.141901\tools\netcoreapp2.1\any\win-x64\sos.dll

0:000> sxe ld clrjit
0:000> g
ModLoad: 00007ffd`ae750000 00007ffd`ae7ce000   C:\Program Files\dotnet\host\fxr.0.1\hostfxr.dll
ModLoad: 00007ffd`ddaf0000 00007ffd`ddb93000   C:\Windows\System32\ADVAPI32.dll
ModLoad: 00007ffd`dca30000 00007ffd`dcace000   C:\Windows\System32\msvcrt.dll
ModLoad: 00007ffd`ddd80000 00007ffd`dde1e000   C:\Windows\System32\sechost.dll
ModLoad: 00007ffd`ddba0000 00007ffd`ddcc2000   C:\Windows\System32\RPCRT4.dll
ModLoad: 00007ffd`ab5b0000 00007ffd`ab642000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\hostpolicy.dll
ModLoad: 00007ffd`919d0000 00007ffd`91f3f000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\coreclr.dll
ModLoad: 00007ffd`dc770000 00007ffd`dc8c6000   C:\Windows\System32\ole32.dll
ModLoad: 00007ffd`dcc40000 00007ffd`dcf6d000   C:\Windows\System32\combase.dll
ModLoad: 00007ffd`db760000 00007ffd`db7de000   C:\Windows\System32\bcryptPrimitives.dll
ModLoad: 00007ffd`dd250000 00007ffd`dd279000   C:\Windows\System32\GDI32.dll
ModLoad: 00007ffd`dc3f0000 00007ffd`dc58c000   C:\Windows\System32\gdi32full.dll
ModLoad: 00007ffd`db8e0000 00007ffd`db980000   C:\Windows\System32\msvcp_win.dll
ModLoad: 00007ffd`dd950000 00007ffd`ddae7000   C:\Windows\System32\USER32.dll
ModLoad: 00007ffd`db740000 00007ffd`db760000   C:\Windows\System32\win32u.dll
ModLoad: 00007ffd`dd2e0000 00007ffd`dd3a4000   C:\Windows\System32\OLEAUT32.dll
ModLoad: 00007ffd`dd280000 00007ffd`dd2d2000   C:\Windows\System32\SHLWAPI.dll
ModLoad: 00007ffd`db4d0000 00007ffd`db4f6000   C:\Windows\System32\bcrypt.dll
ModLoad: 00007ffd`ddcd0000 00007ffd`ddcfe000   C:\Windows\System32\IMM32.DLL
(37ec.25dc): Unknown exception - code 04242420 (first chance)
ModLoad: 00007ffd`910b0000 00007ffd`919cd000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\System.Private.CoreLib.dll
ModLoad: 00007ffd`db490000 00007ffd`db4a1000   C:\Windows\System32\kernel.appcore.dll
ModLoad: 00007ffd`a9b80000 00007ffd`a9cc2000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\clrjit.dll
ntdll!NtMapViewOfSection+0x14:
00007ffd`df4bfc64 c3              ret

0:000> !bpmd ConsoleAppTest Program.Main
Adding pending breakpoints...
0:000> g
ModLoad: 00000179`2e410000 00000179`2e418000   C:\Users\<username>\Documents\!My Stuff\Repos\Test Projects\ConsoleAppTest\ConsoleAppTest\bin\Debug\netcoreapp3.1\ConsoleAppTest.dll
(37ec.25dc): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffd`d08e0000 00007ffd`d08ee000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\System.Runtime.dll
(37ec.25dc): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffd`c8d10000 00007ffd`c8d35000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App.1.10\System.Console.dll
(37ec.25dc): CLR notification exception - code e0444143 (first chance)
(37ec.25dc): CLR notification exception - code e0444143 (first chance)
JITTED ConsoleAppTest!ConsoleAppTest.Program.Main(System.String[])
Setting breakpoint: bp 00007FFD32010FB0 [ConsoleAppTest.Program.Main(System.String[])]
Breakpoint 0 hit
00007ffd`32010fb0 55              push    rbp