runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Debugger.Launch doesn't work correctly on coreclr on Windows

Open stephentoub opened this issue 9 years ago • 32 comments

Repro:

using System;
using System.Diagnostics;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("1");
        Console.WriteLine("2");
        Debugger.Launch();
        Console.WriteLine("3");
        Console.WriteLine("4");
    }
}

With .NET Framework, I can compile this, run it from the console, and the Debugger.Launch pops the attach dialog asking which VS I want to use and which debugging engine to employ. I'm then in the debugger broken into the app on the Launch line, and I can successfully F10 to step to the WriteLine for "3" and "4".

With .NET Core 2.0 (built with the latest source), the same thing happens, up until the debugger breaking in. VS opens, but I have to manually hit the pause button to break into the app. Then I find myself on the Launch line, but neither F5 nor F10 work successfully; instead, they start running the app again, but no forward progress is made, and breaking in again reveals being on the same line, with this stack:

 	ntdll.dll!NtWaitForMultipleObjects()	Unknown
 	KernelBase.dll!WaitForMultipleObjectsEx()	Unknown
 	[Managed to Native Transition]	
>	tmpapp.dll!Test.Main() Line 10	C#
 	[Native to Managed Transition]	
 	CoreRun.exe!00007ff68c9f2fa3()	Unknown
 	CoreRun.exe!00007ff68c9f33dc()	Unknown
 	CoreRun.exe!00007ff68c9fc149()	Unknown
 	kernel32.dll!BaseThreadInitThunk()	Unknown
 	ntdll.dll!RtlUserThreadStart()	Unknown

That's the case with both VS2015 and VS2017, and with selecting the managed CoreCLR debug engine.

Am I missing some component that's supposed to make this work?

stephentoub avatar Jun 03 '17 22:06 stephentoub

@mikem8361 @gregg-miskelly : any ideas?

lt72 avatar Jun 05 '17 17:06 lt72

@lt72 there is no real Just-In-Time debugging support for CoreCLR. To make this work we would need to work out a contract between the Just-In-Time debugger and CoreCLR so that we could know how to know that a Just-In-Time launch is for CoreCLR debugging, and how to tell the CoreCLR to resume execution.

gregg-miskelly avatar Jun 05 '17 17:06 gregg-miskelly

Yes, this feature has never been implemented (the interaction with the JIT debugger). It looks like coreclr does initialize debugging and should be attachable. It is just that the JIT debugger is NOT launched automatically to do the attach.

mikem8361 avatar Jun 05 '17 17:06 mikem8361

@stephentoub: Steve, did you inquired because you found this accidentally or do you need this feature for any reason?

lt72 avatar Jun 05 '17 19:06 lt72

Found it while trying to use it for my own debugging purposes.

stephentoub avatar Jun 05 '17 19:06 stephentoub

@lt72 what's the prospects of getting this implemented? It would certainly help folks working on CoreFX, as well as customers.

danmoseley avatar Jun 26 '17 02:06 danmoseley

@noahfalk FYI We will put this work item on the 2.1 planning and prioritize according to other needs. We should be able to make a call by end of July, when planning is finalized.

lt72 avatar Jun 26 '17 17:06 lt72

@danmosemsft You are asking for only Windows AND only full VS, correct? I don't see how we could make the full end-to-end work in VS Code, or in some sort of remote VS case.

gregg-miskelly avatar Jun 26 '17 18:06 gregg-miskelly

@gregg-miskelly yes, most people debug most of the time on Windows with VS. This would make it easier, particularly when a test spawns a child process.

danmoseley avatar Jun 26 '17 18:06 danmoseley

If making it work requires changes to it anyway, shouldn't it be removed it since it doesn't work?

ghost avatar Sep 08 '17 16:09 ghost

Any update on this? If this worked, a lot of one-off debugging problems would be a bit simpler for me.

If making it work requires changes to it anyway, shouldn't it be removed it since it doesn't work?

That's not the worst option, really. This method's current behavior is very misleading -- probably doubly-so for a regular .NET Core user who doesn't understand that it's broken.

mellinoe avatar Sep 11 '17 22:09 mellinoe

This would significantly help devs working on the BCL libraries.

danmoseley avatar Sep 11 '17 22:09 danmoseley

We will not be able to get to this anytime soon. We can try and see that this gets some traction sometimes before the end of October.

lt72 avatar Sep 12 '17 16:09 lt72

We can try and see that this gets some traction sometimes before the end of October.

@lt72, any luck?

stephentoub avatar Nov 29 '17 00:11 stephentoub

@mikem8361: can you please take a look to this one?

lt72 avatar Nov 29 '17 20:11 lt72

.NET Core really doesn't use the registry so I purpose that coreclr uses an environment variable like COMPlus_AeDebug that works like the AeDebug registry key so you could set it to something like ""C:\WINDOWS\system32\vsjitdebugger.exe" -p %ld -e %ld" and end up with similar behavior to the desktop.

The default if the environment variable doesn't exists is the current behavior.

I worry about the security problems with launching something from an environment variable.

/cc @jkotas

mikem8361 avatar Nov 30 '17 01:11 mikem8361

I don't think you want to use an environment variable for this -- this is a system setting, so it should really be in the registry on Windows, and as a configuration file on other OSs.

I would like to suggest that we:

  • Use a registry key on Windows
  • Do NOT use AeDebug\Debugger, so that we can use a different command line format from native exceptions and so we aren't just launching a native debugger
  • Come up with a command line contract that makes it easy for the JIT debugger to understand the basic details of the exception (I think the exception type and maybe message?) without needing to load up any other dll (ex: these things can either be passed on the command line, or the address of some descriptor struct is passed on the command line)

gregg-miskelly avatar Nov 30 '17 02:11 gregg-miskelly

If you invent a new mechanism I hope there is a way to make it work with existing VS and native debuggers...that should technically work I assume? It is sometimes useful to launch a native debugger possibly to use SOS.

danmoseley avatar Nov 30 '17 04:11 danmoseley

Perhaps if the new key is not set it can fall back to AeDebug?

danmoseley avatar Nov 30 '17 04:11 danmoseley

No. VS will need to do work to support any .NET Core JIT debugging scheme.

Windbg could do something, but I think it would be a poor trade-off to fall back AeDebug when the 99% case is that it will not work. Just write the new key with the Windbg command line if you want Windbg.

gregg-miskelly avatar Nov 30 '17 05:11 gregg-miskelly