如何等待远程 .NET 调试器连接

发布于 2024-07-09 12:24:38 字数 464 浏览 10 评论 0原文

今天我遇到了一个问题,我需要远程调试程序。 该程序是从另一个系统启动的,所以我真的没有机会在命令行上与它交互。 不过我可以很容易地改变它的来源。

我需要做的是让程序正常启动,然后等待我用调试器附加到它。 我想不出一个让我快乐的方法。 我确实发现了这个错误,但没有调试器的帮助。

while(true) { }

保持进程处于活动状态,然后我可以使用调试器“设置下一个语句”,但这看起来很尴尬和粗鲁。

Console.ReadLine();

打字看起来很奇怪,因为实际上没有控制台供我按输入。 (它也不起作用。设置下一个语句,然后运行会将您带回 ReadLine() 等待。)

那么我可以在 .NET/CLR/C# 程序中插入什么样的代码来表示“在这里等待,直到我可以附加调试器”吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧城空念 2024-07-16 12:24:39

您可以使用 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");
        }
    }
}

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");
        }
    }
}
¢蛋碎的人ぎ生 2024-07-16 12:24:39

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

Debugger.Launch();

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

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

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

零度° 2024-07-16 12:24:39

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

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.

爱要勇敢去追 2024-07-16 12:24:39

连接远程调试器的工作方式与使用本地调试器完全相同。

首先,执行通常的操作:

System.Diagnostics.Debugger.Launch();

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

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.

十二 2024-07-16 12:24:39
Debug.Assert(true);

我想也应该有效。 顺便说一句,我有时也会遇到这个问题,而且我确实

MessageBox.Show() 

:P :P

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

红焚 2024-07-16 12:24:39

设置一个超时,让您有时间附加调试器。

Thread.Sleep(30000);

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

Thread.Sleep(30000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文