简体   繁体   English

应用程序启动时Visual Studio远程调试

[英]Visual Studio remote debugging on application startup

As I understand it now, the only way to use the remote debugger is to start the target application, and then attach to it through Visual Studio. 据我所知,使用远程调试器的唯一方法是启动目标应用程序,然后通过Visual Studio连接到它。 Is there a way to capture all of the breakpoints from the very beginning of the program? 有没有办法从程序的最开始捕获所有断点?

There is code within my program that I need to debug, and I can never get the debugger attached fast enough to capture that executing code. 我需要调试程序中的代码,我永远无法快速连接调试器以捕获执行代码。

If you can change the code, try injecting this line of code in the starting point of your app: 如果您可以更改代码,请尝试在应用的起点注入以下代码:

System.Diagnostics.Debugger.Launch();

When this line is hit it will prompt you to attach a debugger, effectively waiting for you to respond. 当该行被命中时,它将提示您附加调试器,有效地等待您的响应。 Since you are using a remote debugger you should be able to attach at that point and then just cancel the dialog. 由于您使用的是远程调试器,因此您应该能够在该点附加,然后取消对话框。 Hope this helps. 希望这可以帮助。

The solution 解决方案

System.Diagnostics.Debugger.Launch 

didn't work for me either. 也不适合我。 However, I managed to solve my problem writing in my application start up the following: 但是,我设法在我的应用程序中解决了我的问题,启动了以下内容:

while (!System.Diagnostics.Debugger.IsAttached)
    System.Threading.Thread.Sleep(100);

That way the application will be waiting until a debugger gets attached. 这样,应用程序将等待调试器连接。

With Visual Studio Pro 2010 building a .NET 4 application, this doesn't work for me. 使用Visual Studio Pro 2010构建.NET 4应用程序,这对我不起作用。

Apparently this is a known bug: 显然这是一个已知的错误:

https://connect.microsoft.com/VisualStudio/feedback/details/611486/debugger-launch-is-now-crashing-my-net-application-after-upgrading-to-net-4-0 https://connect.microsoft.com/VisualStudio/feedback/details/611486/debugger-launch-is-now-crashing-my-net-application-after-upgrading-to-net-4-0

A (somewhat hacky) workaround for the moment which is working for me is just to have the app throw up a MessageBox() right at the start of main window initialisation: 暂时(稍微有些hacky)解决方法对我来说只是让应用程序在主窗口初始化开始时抛出一个MessageBox():

public partial class MainWindow : Form
{
    public MainWindow()
    {
        // To allow you time to attach a remote debugger ...
        MessageBox.Show("Please attach debugger");

        InitializeComponent();
        ...

Now you can attach the VS remote debugger at your leisure, and then hit OK on the message box. 现在,您可以随意附加VS远程调试器,然后在消息框中单击“确定”。

Ugly but functional. 丑陋但功能齐全。

On the target machine set up the Visual Studio Remote Debugger that matches the year of Visual Studio on your local machine. 在目标计算机上设置Visual Studio远程调试程序,该程序与本地计算机上的Visual Studio年份匹配。

Note the line that gives you the server name. 请注意为您提供服务器名称的行。

On your local machine in visual studio, open the properties of your startup project and then open the debug section. 在Visual Studio中的本地计算机上,打开启动项目的属性,然后打开调试部分。

Check the box for "use remote machine" and then enter into the text field the server name you got from the Visual Studio Remote Debugger. 选中“使用远程计算机”复选框,然后在文本字段中输入从Visual Studio远程调试程序获取的服务器名称。

Under "Start Action", select "Start External Program". 在“开始操作”下,选择“启动外部程序”。 Then place into the field the path to the .exe you wish to start on your target machine. 然后将您希望在目标计算机上启动的.exe的路径放入该字段。

Now when you press the start button from you local machine it will start the program on the target machine with the debugger attached. 现在当您从本地机器按下启动按钮时,它将在目标机器上启动程序并附带调试器。

The correct solution for me was a combination of the answers. 对我来说,正确的解决方案是答案的组合。

The while loop will check if the debugger is attached from Visual Studio and exit the loop when it is attached. while循环将检查调试器是否从Visual Studio连接并在连接时退出循环。

        System.Diagnostics.Debugger.Launch();
        while (!System.Diagnostics.Debugger.IsAttached)
        {
            System.Threading.Thread.Sleep(100);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM