Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until remote .NET debugger attached

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"?

like image 530
Clinton Pierce Avatar asked Dec 11 '08 21:12

Clinton Pierce


People also ask

How do I wait for debugger?

Go to Settings > Developer options > Select debug app and choose your app from the list, then click Wait for debugger. Wait for the app to load and a dialog to appear telling you the app is waiting for a debugger.

How do I connect to Visual Studio Remote debugger?

To perform remote debugging using Visual Studio: On the remote computer, in Visual Studio, choose Connect to Remote Debugger from the Tools menu. In the Connect to Remote Debugger dialog box, enter a connection string, and click Connect.

How do I turn off remote debugging?

Go to your android settings and clear app data and cache and reload the app remote debugging will be turned off.


1 Answers

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");         }     } }
like image 105
Daniel Richardson Avatar answered Sep 21 '22 04:09

Daniel Richardson