runtime icon indicating copy to clipboard operation
runtime copied to clipboard

dotnet.exe host should have a --debug switch

Open eerhardt opened this issue 10 years ago • 37 comments

Steps to reproduce

It is too hard to debug applications when they aren't launched from VS. Today, you need to add some "Console.ReadLine()" calls in order to have the process wait for you to attach to the process.

Expected behavior

There should be an easy way to say "launch this .net core app and attach a debugger" or "wait for a debugger to be attached".

Actual behavior

This isn't possible today.

Notes

I think even if we enabled something simple in the host like:

if --debug is passed before the assembly to run, print out a message like: "DEBUG MODE: attach a debugger to process <process id> and then hit ENTER to continue". That waits for the ENTER, and then your entry method is invoked.

/cc @schellap @gkhanna79 @piotrpMSFT @blackdwarf @brthor @ericstj @weshaggard

eerhardt avatar Jun 30 '16 16:06 eerhardt

@eerhardt Are you not able to use VS debugger do the same by pointing it to a command line?

gkhanna79 avatar Jun 30 '16 16:06 gkhanna79

Are you not able to use VS debugger do the same by pointing it to a command line?

That approach is less than ideal because:

  1. I need to create a dummy project so I can tell VS to "point to a command line"
  2. I need to ensure I created a .NET Core dummy project.
    • If I use a Windows Desktop C# console app I get an error:
---------------------------
Microsoft Visual Studio
---------------------------
A fatal error has occurred and debugging needs to be terminated. For more details, please see the Microsoft Help and Support web site. HRESULT=0x8000ffff. ErrorCode=0x0.
---------------------------
OK   
---------------------------
  1. Maybe I don't have VS, and I only have WinDBG, or some other debugger. When I move my app into "production" or off of my dev box, I don't want to install full VS just to debug the app.
  2. Maybe my intent isn't to do traditional VS debugging, but instead I want to get the process ID and do some other monitoring while the app runs.

eerhardt avatar Jun 30 '16 17:06 eerhardt

@eerhardt so, in summary, the proposal is to, at least, have an easy way for people to get a PID of the host running?

thesmithee avatar Jun 30 '16 17:06 thesmithee

BTW - without this 'feature', folks have been inventing it themselves by using DebugHelper.HandleDebugSwitch and copying that file around.

See https://github.com/search?p=3&q=org%3Adotnet+DebugHelper&type=Code for all the places it is used in our 'dotnet' repos today.

eerhardt avatar Jun 30 '16 17:06 eerhardt

so, in summary, the proposal is to, at least, have an easy way for people to get a PID of the host running?

AND pause the app until I say "GO"

eerhardt avatar Jun 30 '16 17:06 eerhardt

@eerhardt got it. The pause part is actually the more useful one, as getting the PID can be done in different ways. I have to say, this could be useful actually, from a user's POV.

thesmithee avatar Jun 30 '16 17:06 thesmithee

@eerhardt trying to understand this ask... how did desktop framework work without this feature?

AFAIK, no program suspends on its own for a potential debugger attach, so why should a managed program host do this?

schellap avatar Jun 30 '16 17:06 schellap

no program suspends on its own for a potential debugger attach

See my link above. That shows at least 3 programs that we wrote in the CLI and in core-setup.

Also, CoreRun.exe does this today:

C:\cli>\core-setup\pkg\Tools\CoreRun.exe /d
Runs executables on CoreCLR

USAGE: coreRun [/d] [/v] Managed.exe

  where Managed.exe is a managed executable built for CoreCLR
        /v causes verbose output to be written to the console
        /d causes coreRun to wait for a debugger to attach before
         launching Managed.exe

  CoreCLR is searched for in %core_root%, then in the directory
  that coreRun.exe is in, then finally in %windir%\system32\.

I won't go searching for all the programs in the world that do this, but suffice it to say, this seems valuable to a lot of people if we have that DebugHelper class, and CoreRun.exe implemented it. And the only reason I knew about CoreRun.exe is because @rainersigwald pointed it out to me, because he uses it. So we have users outside of the .NET team who use this switch in CoreRun.

how did desktop framework work without this feature

desktop framework was very "VS centric". .NET Core runs on platforms VS isn't capable of running on.

eerhardt avatar Jun 30 '16 19:06 eerhardt

@schellap The problem is that it's easy to vsjitdebugger.exe MyProgram.exe or windbg.exe MyProgram.exe or set ImageFileExecutionOptions to break into the start of a desktop executable. Those are much less desirable here because I don't want to step into the CLR host--I want to step into my program.

Also, every program I maintain has a clause like this:

if (Environment.GetEnvironmentVariable("{something}DEBUGONSTART") == 1)
{
    Debugger.Launch();
}

Sure would be nice if it were built in.

rainersigwald avatar Jun 30 '16 19:06 rainersigwald

@rainersigwald and @eerhardt, thanks. We can bake this in with some switch that doesn't conflict with dotnet.dll.

@eerhardt, sorry... I wanted to call out that most programs native or managed (incl. Desktop apps) don't have this support, for these handful of programs you quoted. They are being debugged fine... did not ask to list what programs do this today.

schellap avatar Jun 30 '16 19:06 schellap

I guess I still have more questions to understand this better. If the goal is to attach the debugger before any managed application is launched, then doing "windbg dotnet <app.dll>" is equivalent to what you are requesting for.

@rainersigwald Can you please explain your workflow while using the switch? Is it the case that you do not own the process that launches the application?

gkhanna79 avatar Jul 05 '16 16:07 gkhanna79

@gkhanna79 I use this all the time to debug unit tests with xunit: corerun /d xunit.console.netcore.exe ....

I like being able to use VS to debug using the session I'm using to develop, with persistent breakpoints and mental state built up over multiple runs of the program under test. But when the entry-point application is a netcore app that's not my code, it's really inconvenient to set VS up to launch it.

Maybe there's a better way, but the only way I know is to create a new bogus xproj and configure it to launch the application. My mental cost/benefit calculation always makes it seem like it's cheaper to launch with the debug flag and manually attach from my VS.

Just prefixing the command line with vsjitdebugger.exe almost works, but it launches in native-debugging mode and doesn't hit managed breakpoints.

If there's an actually-easy way to get a running instance of VS to attach to a short-lived program via command line, I'd be happy. This seems like a way to get that.

rainersigwald avatar Jul 05 '16 16:07 rainersigwald

@gkhanna79 this sounds like a feature, and we're past feature complete..

danmoseley avatar Apr 28 '17 06:04 danmoseley

Yeah, this nice to have and can move out.

gkhanna79 avatar Apr 28 '17 14:04 gkhanna79

I would also like to have this option for so I can debug during dotnet test.

jamesqo avatar Jun 10 '17 14:06 jamesqo

If you think about implementing that, please make sure that the implementation does not print to console.

If you write a .net core console application that is used to stream out some data into stdout and you want to pipe that to another console application, any extra console output will mess that up.

gingters avatar Feb 22 '18 06:02 gingters

Moving this out again; feature complete is next week and this won't make it

steveharter avatar Feb 22 '18 16:02 steveharter

Moving to 3.0

cc @jeffschwMSFT

Summary:

  1. Corerun already does this coreclr's corerun /d switch
  2. Add the PID to the message to make attaching easier
  3. To prevent interferrence of the running application, do not display a message to stdout\stderr by default, or display by default but provide a way to turn that off. Consider using the existing COREHOST_TRACE and -v options (need to discuss)

steveharter avatar Jun 05 '18 19:06 steveharter

See https://twitter.com/migueldeicaza/status/1013061980284997632?s=21 for others talking about this same thing. It would be great if this was just built in to the platform. (Note the link to the tweet about forgetting to remove Thread.Sleep(30s) from the code, causing slow startup)

eerhardt avatar Jun 30 '18 15:06 eerhardt

I'd love to see this make it into 3.0. If this is accurate (https://github.com/OmniSharp/omnisharp-vscode/issues/1059), getting this feature would be very helpful.

gholt-aws avatar Oct 03 '18 01:10 gholt-aws