85

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90

6 Answers6

157

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");
        }
    }
}
Daniel Richardson
  • 5,114
  • 2
  • 22
  • 20
  • 1
    Thanks for this answer. I knew there had to be a way to check from code if a debugger was attached, and this was the first/best hit I got from searching. – David Hay Sep 30 '09 at 21:28
  • 1
    Replacing the while with an if and the sleep with an assert might be a better solution, because it will allow you to continue easily if you don't want to attach a debugger everytime. – Luke Kim Aug 13 '11 at 15:30
  • Is it possible to use some kind of an event from `Debugger` and wait on some `AutoResetEvent` till `IsAttached` event is raised? – Egor Okhterov Aug 27 '15 at 11:59
28

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

Martin Sherburn
  • 5,958
  • 2
  • 23
  • 17
  • 12
    This is absolutely the answer you want if you're not doing remote debugging. So long as the code you want to debug is on the local machine, this line of code will open a pop-up that lets you debug it with Visual Studio. (If you deploy this code to a test or production box though, the window will appear there, and your application might appear to hang) – kbaribeau Oct 04 '10 at 19:50
  • 7
    have to secon kbaribeau: This only works if you want to attach a local debugger. OP asked for a remote debugger and in this case, Debugger.Launch() will not help as you can't select a remote debugger in the popup and as it will terminate the exe if you select to not attach a debugger – buddybubble Nov 15 '13 at 11:52
  • Not working for remote debugging. Caused my VS and the program to crash. – user3772108 Aug 21 '19 at 07:29
  • @kbaribeau I don't know if this was true in 2010, but today I was able to debug using this solution. When the popup comes, attach yourself with the remote debugger. Click yes on the popup, then click yes. When the option to choose a Visual Studio instance comes up, just click cancel. The program will continue with the remote debugger attached. – NickLokarno Jun 22 '23 at 18:33
6

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
5

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.

ya23
  • 14,226
  • 9
  • 46
  • 43
  • 3
    I tested that and selecting "no" will terminate the program even if a remote debugger is already attached – buddybubble Nov 15 '13 at 11:53
  • @buddybubble What I did was wait for that window to pop up, then attach the remote debugger from the other machine. Then select yes, then when the prompt to open a new instance of Visual Studio pops up, click Cancel, and the program continues with the remote debugger attached. – NickLokarno Jun 22 '23 at 15:07
1
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

-2

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

Thread.Sleep(30000);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Arron S
  • 5,511
  • 7
  • 50
  • 57