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.)
看起来很奇怪,因为实际上没有一个控制台供我输入。 (它也没有用。设置下一个语句然后运行会带你回到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#程序中插入什么样的代码来说“在这里等到我可以附加调试器”?
6 个解决方案
#1
85
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:
您可以使用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");
}
}
}
#2
12
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."
“启动并附加调试程序。”
#3
5
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.
我不知道,因为我从来没有尝试过,但我想知道你是否可以使用System.Diagnostics.Debugger.Break()以使其达到断点然后等待调试器附加。我假设一个远程调试器可以工作,但我不确定,目前无法访问我的家庭环境,我可以轻松地模拟它并测试我的理论。有一篇MSDN文章讨论在ASP.Net应用程序中使用它,所以我想它会起作用。
#4
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
:P:P
#5
0
Set a timeout that gives you time to attach the debugger.
设置超时,为您提供连接调试器的时间。
Thread.Sleep(30000);
#6
0
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.
您将看到选择调试器的提示。此时执行被暂停,因此您可以附加远程调试器并从提示中选择“否”。
#1
85
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:
您可以使用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");
}
}
}
#2
12
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."
“启动并附加调试程序。”
#3
5
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.
我不知道,因为我从来没有尝试过,但我想知道你是否可以使用System.Diagnostics.Debugger.Break()以使其达到断点然后等待调试器附加。我假设一个远程调试器可以工作,但我不确定,目前无法访问我的家庭环境,我可以轻松地模拟它并测试我的理论。有一篇MSDN文章讨论在ASP.Net应用程序中使用它,所以我想它会起作用。
#4
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
:P:P
#5
0
Set a timeout that gives you time to attach the debugger.
设置超时,为您提供连接调试器的时间。
Thread.Sleep(30000);
#6
0
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.
您将看到选择调试器的提示。此时执行被暂停,因此您可以附加远程调试器并从提示中选择“否”。