如何等到远程.NET调试器连接 [英] 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) { }
保留该进程,然后我可以使用调试器设置下一个语句,但它似乎很尴尬和粗鲁。 p>
Kept the process alive, and then I could "set next statement" with the debugger, but it seemed awkward and rude.
Console.ReadLine();
看起来很奇怪,因为实际上没有一个控制台可以按输入在。 (它没有工作,或者,设置下一个语句,然后运行会让你回到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.)
那么我可以插入什么样的代码一个.NET / CLR / C#程序,说等待这里直到我可以附加一个调试器?
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"?
推荐答案
你可以使用 System.Diagnostics.Debugger.IsAttached 属性检查调试器是否附加到进程。此应用程序将等待直到调试器被附加:
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");
}
}
}
这篇关于如何等到远程.NET调试器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!