How to wait until remote .NET debugger attached

.NetDebuggingBreakpointsRemote Debugging

.Net Problem Overview


Today I ran into a problem were I needed to remote-debug a program. The program was launched from another system, so I really don't have an opportunity to interact with it on the command line. I could change its source easily though.

What I needed to happen was for the program to start normally, and then wait for me to attach to it with a debugger. I couldn't come up with a way to do it that made me happy. I did find the bug, but without the help of the debugger.

while(true) { }

Kept the process alive, and then I could "set next statement" with the debugger, but it seemed awkward and rude.

Console.ReadLine();

Seemed odd to type since there wasn't actually a Console for me to press enter at. (It didn't work, either. Set next statement and then run takes you back into the ReadLine() wait.)

So what kind of code can I insert into a .NET/CLR/C# program that says "wait here until I can attach with a debugger"?

.Net Solutions


Solution 1 - .Net

You can use the System.Diagnostics.Debugger.IsAttached property to check if a debugger is attached to the process. This application will wait until a debugger has been attached:

using System;
using System.Diagnostics;
using System.Threading;




namespace DebugApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Waiting for debugger to attach");
while (!Debugger.IsAttached)
{
Thread.Sleep(100);
}
Console.WriteLine("Debugger attached");
}
}
}

namespace DebugApp { class Program { static void Main(string[] args) { Console.WriteLine("Waiting for debugger to attach"); while (!Debugger.IsAttached) { Thread.Sleep(100); } Console.WriteLine("Debugger attached"); } } }

Solution 2 - .Net

This sounds like exactly what you need:

Debugger.Launch();

http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx

"Launches and attaches a debugger to the process."

Solution 3 - .Net

I don't know, since I've never tried it but I wonder if you could use System.Diagnostics.Debugger.Break() in order to have it hit a breakpoint and then wait for a debugger to attach. I assume a remote debugger would work, but I do not know for sure and currently do not have access to my home environment where I could easily mock it up and test my theory. There's an MSDN article talking about using it in an ASP.Net application so I imagine it would work.

Solution 4 - .Net

Attaching remote debugger works exactly the same as using local debugger.

First, do the usual:

System.Diagnostics.Debugger.Launch();

You will see a prompt to chose debugger. At this point the execution is suspended, so so you can attach remote debugger and select "No" from the prompt.

Solution 5 - .Net

Debug.Assert(true);

should also work I guess. By the way, I also face this proble at times and I do

MessageBox.Show() 

:P :P

Solution 6 - .Net

Set a timeout that gives you time to attach the debugger.

Thread.Sleep(30000);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClinton PierceView Question on Stackoverflow
Solution 1 - .NetDaniel RichardsonView Answer on Stackoverflow
Solution 2 - .NetMartin SherburnView Answer on Stackoverflow
Solution 3 - .NetSteven BehnkeView Answer on Stackoverflow
Solution 4 - .Netya23View Answer on Stackoverflow
Solution 5 - .NetSudarsan SrinivasanView Answer on Stackoverflow
Solution 6 - .NetArron SView Answer on Stackoverflow