Search code examples
.net-coreconsole-applicationwindbg

How do I debug a .NET Core console app with WinDbg by "Launch executable"?


I am trying to debug a simple .NET Core console app with WinDbg in live (by "Launch executable" in WinDbg), but an exception about "Access violation" has been thrown.

Environment info:

  • OS: Windows 10 22H2 19045.3448
  • .NET runtime: .NET 7
  • Build tool: Visual Studio 2022 (64-bit) v17.7.4
  • Build options: Debug, Any CPU
  • Debug tool: WinDbg Preview 1.2308.2002.0

Code:

namespace Simplicity01
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            TestDebugging01();
        }

        static void TestDebugging01()
        { 
            T01 t01 = new T01();
            t01.Print();
        }
    }

    internal class T01 
    {
        public void Print()
        {
            int i = 3;
            string str = "abc";
            Console.WriteLine(i);
            Console.WriteLine(str); 
        }
    }
}

The entered command steps for the debug are as follows in the order:

  • Open WinDbg and click "Launch executable" to load the Simplicity01.exe file.

    sxe ld:Simplicity01 g !bpmd Simplicity01 Simplicity01.Program.TestDebugging01 g g

Here are the step details in WinDbg:

************* Preparing the environment for Debugger Extensions Gallery repositories **************
   ExtensionRepository : Implicit
   UseExperimentalFeatureForNugetShare : false
   AllowNugetExeUpdate : false
   AllowNugetMSCredentialProviderInstall : false
   AllowParallelInitializationOfLocalRepositories : true

   -- Configuring repositories
      ----> Repository : LocalInstalled, Enabled: true
      ----> Repository : UserExtensions, Enabled: true

>>>>>>>>>>>>> Preparing the environment for Debugger Extensions Gallery repositories completed, duration 0.000 seconds

************* Waiting for Debugger Extensions Gallery to Initialize **************

>>>>>>>>>>>>> Waiting for Debugger Extensions Gallery to Initialize completed, duration 0.031 seconds
   ----> Repository : UserExtensions, Enabled: true, Packages count: 0
   ----> Repository : LocalInstalled, Enabled: true, Packages count: 36

Microsoft (R) Windows Debugger Version 10.0.25921.1001 AMD64
Copyright (c) Microsoft Corporation. All rights reserved.

CommandLine: F:\Debug\Simplicity01.exe

************* Path validation summary **************
Response                         Time (ms)     Location
Deferred                                       cache*F:\Project\Symbol\Windbg
Deferred                                       srv*F:\Project\Symbol\Windbg*https://msdl.microsoft.com/download/symbols
Symbol search path is: cache*F:\Project\Symbol\Windbg;srv*F:\Project\Symbol\Windbg*https://msdl.microsoft.com/download/symbols
Executable search path is: 
ModLoad: 00007ff7`e82d0000 00007ff7`e82fb000   apphost.exe
ModLoad: 00007fff`86a90000 00007fff`86c88000   ntdll.dll
ModLoad: 00007fff`85a30000 00007fff`85aed000   C:\Windows\System32\KERNEL32.DLL
ModLoad: 00007fff`84290000 00007fff`84586000   C:\Windows\System32\KERNELBASE.dll
ModLoad: 00007fff`81930000 00007fff`819c1000   C:\Windows\SYSTEM32\apphelp.dll
ModLoad: 00007fff`84d00000 00007fff`84e9e000   C:\Windows\System32\USER32.dll
ModLoad: 00007fff`84ac0000 00007fff`84ae2000   C:\Windows\System32\win32u.dll
ModLoad: 00007fff`86420000 00007fff`8644c000   C:\Windows\System32\GDI32.dll
ModLoad: 00007fff`848a0000 00007fff`849ba000   C:\Windows\System32\gdi32full.dll
ModLoad: 00007fff`845e0000 00007fff`8467d000   C:\Windows\System32\msvcp_win.dll
ModLoad: 00007fff`849c0000 00007fff`84ac0000   C:\Windows\System32\ucrtbase.dll
ModLoad: 00007fff`85af0000 00007fff`86235000   C:\Windows\System32\SHELL32.dll
ModLoad: 00007fff`866a0000 00007fff`8674e000   C:\Windows\System32\ADVAPI32.dll
ModLoad: 00007fff`84ea0000 00007fff`84f3e000   C:\Windows\System32\msvcrt.dll
ModLoad: 00007fff`86530000 00007fff`865cc000   C:\Windows\System32\sechost.dll
ModLoad: 00007fff`86750000 00007fff`86876000   C:\Windows\System32\RPCRT4.dll
(5070.5034): Break instruction exception - code 80000003 (first chance)
ntdll!LdrpDoDebuggerBreak+0x30:
00007fff`86b60750 cc              int     3
0:000> sxe ld:Simplicity01
0:000> g
ModLoad: 00007fff`84b10000 00007fff`84b40000   C:\Windows\System32\IMM32.DLL
ModLoad: 00007fff`71920000 00007fff`71982000   C:\Program Files\dotnet\host\fxr\7.0.11\hostfxr.dll
ModLoad: 00007fff`059a0000 00007fff`05a03000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\hostpolicy.dll
ModLoad: 00007ffe`b25b0000 00007ffe`b2aaa000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\coreclr.dll
ModLoad: 00007fff`84fd0000 00007fff`850fa000   C:\Windows\System32\ole32.dll
ModLoad: 00007fff`85660000 00007fff`859b4000   C:\Windows\System32\combase.dll
ModLoad: 00007fff`862a0000 00007fff`8636d000   C:\Windows\System32\OLEAUT32.dll
ModLoad: 00007fff`84810000 00007fff`84892000   C:\Windows\System32\bcryptPrimitives.dll
(5070.5034): Unknown exception - code 04242420 (first chance)
ModLoad: 00007ffe`b15d0000 00007ffe`b20eb000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Private.CoreLib.dll
ModLoad: 00007ffe`b1450000 00007ffe`b15cb000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\clrjit.dll
ModLoad: 00007fff`82000000 00007fff`82012000   C:\Windows\SYSTEM32\kernel.appcore.dll
ModLoad: 000001f9`3d350000 000001f9`3d358000   F:\Debug\Simplicity01.dll
ntdll!NtMapViewOfSection+0x14:
00007fff`86b2d354 c3              ret
0:000> !bpmd Simplicity01 Simplicity01.Program.TestDebugging01
Adding pending breakpoints...
0:000> g
(5070.5034): CLR notification exception - code e0444143 (first chance)
ModLoad: 000001f9`3ed50000 000001f9`3ed5e000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Runtime.dll
(5070.5034): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007ffe`b5500000 00007ffe`b552a000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Console.dll
(5070.5034): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007fff`2bb90000 00007fff`2bba3000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Threading.dll
(5070.5034): CLR notification exception - code e0444143 (first chance)
ModLoad: 000001f9`3d3d0000 000001f9`3d3d8000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Text.Encoding.Extensions.dll
(5070.5034): CLR notification exception - code e0444143 (first chance)
ModLoad: 00007fff`541f0000 00007fff`541fd000   C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.11\System.Runtime.InteropServices.dll
(5070.5034): CLR notification exception - code e0444143 (first chance)
(5070.5034): CLR notification exception - code e0444143 (first chance)
JITTED Simplicity01!Simplicity01.Program.TestDebugging01()
Setting breakpoint: bp 00007FFE52C10B00 [Simplicity01.Program.TestDebugging01()]
Breakpoint 0 hit
Simplicity01!COM+_Entry_Point:
00007ffe`52c10b00 55              push    rbp
0:000> g
(5070.5034): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
00007ffe`52c10b60 0000            add     byte ptr [rax],al ds:00007ffe`52d445b8=ff

Now, the "Access violation" exception is thrown after executing "a" command.

I don't know why it cause this "Access violation" exception when debugging.

How do I address this problem?


Solution

  • Doing some small investigation and asking around on X/twitter, appears this issue is related with W^X (write & execute) setting that was introduced in .NET 7 (or maybe only the default value was change in .NET7). The new value is problematic for debuggers and profiling related tooling.

    You can control this by setting the environment variable DOTNET_EnableWriteXorExecute to 0 with setx DOTNET_EnableWriteXorExecute 0. With that value set, the scenario will pass.

    Knowing the flag name, searching shows some projects (like Benchmark.NET here) overriding this value to avoid problems that this new default value generates for them.

    There's an open issue to fix this problem for bpmd command.