如何等待远程 .NET 调试器连接
今天我遇到了一个问题,我需要远程调试程序。 该程序是从另一个系统启动的,所以我真的没有机会在命令行上与它交互。 不过我可以很容易地改变它的来源。
我需要做的是让程序正常启动,然后等待我用调试器附加到它。 我想不出一个让我快乐的方法。 我确实发现了这个错误,但没有调试器的帮助。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 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:
这听起来正是您所需要的:
http://msdn .microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx
“启动调试器并将其附加到进程。”
This sounds like exactly what you need:
http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx
"Launches and attaches a debugger to the process."
我不知道,因为我从未尝试过,但我想知道您是否可以使用 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.连接远程调试器的工作方式与使用本地调试器完全相同。
首先,执行通常的操作:
您将看到选择调试器的提示。 此时执行已暂停,因此您可以连接远程调试器并从提示中选择“否”。
Attaching remote debugger works exactly the same as using local debugger.
First, do the usual:
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.
我想也应该有效。 顺便说一句,我有时也会遇到这个问题,而且我确实
:P :P
should also work I guess. By the way, I also face this proble at times and I do
:P :P
设置一个超时,让您有时间附加调试器。
Set a timeout that gives you time to attach the debugger.