如何等到远程 .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) { }
让进程保持活动状态,然后我可以用调试器设置下一个语句",但这看起来很尴尬和粗鲁.
Kept the process alive, and then I could "set next statement" with the debugger, but it seemed awkward and rude.
Console.ReadLine();
输入似乎很奇怪,因为实际上没有控制台供我按 enter 在.(它也不起作用.设置下一条语句,然后运行将带您回到 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屋!