如何等到远程 .NET 调试器附加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/361077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:48:32  来源:igfitidea点击:

How to wait until remote .NET debugger attached

.netdebuggingbreakpointsremote-debugging

提问by Clinton Pierce

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 enterat. (It didn't work, either. Set next statement and then run takes you back into the ReadLine() wait.)

输入似乎很奇怪,因为实际上没有控制台供我按Enter键。(它也不起作用。设置下一条语句,然后运行将带您回到 ReadLine() 等待。)

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/CLR/C# 程序中插入什么类型的代码,该程序说“请等待,直到我可以附加调试器”?

回答by Daniel Richardson

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

您可以使用System.Diagnostics.Debugger.IsAttached属性来检查调试器是否附加到进程。此应用程序将等待调试器已附加:

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");
        }
    }
}

回答by Martin Sherburn

This sounds like exactly what you need:

这听起来正是您所需要的:

Debugger.Launch();

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

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

"Launches and attaches a debugger to the process."

“启动并将调试器附加到进程中。”

回答by Steven Behnke

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 articletalking about using it in an ASP.Net application so I imagine it would work.

我不知道,因为我从未尝试过,但我想知道您是否可以使用System.Diagnostics.Debugger.Break()它来让它达到断点,然后等待调试器附加。我假设远程调试器可以工作,但我不确定,目前无法访问我的家庭环境,我可以轻松地模拟它并测试我的理论。有一篇MSDN 文章谈到在 ASP.Net 应用程序中使用它,所以我想它会起作用。

回答by ya23

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.

您将看到选择调试器的提示。此时执行已暂停,因此您可以附加远程调试器并从提示中选择“否”。

回答by ya23

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

:P :P

回答by Arron S

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

设置一个超时时间,让您有时间连接调试器。

Thread.Sleep(30000);