Visual Studio remote debugging on application startup

放肆的年华 提交于 2020-08-24 05:04:22

问题


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


回答1:


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.




回答2:


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.




回答3:


On the target machine set up the Visual Studio Remote Debugger that matches the year of Visual Studio on your local machine.

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.

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.

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.

Now when you press the start button from you local machine it will start the program on the target machine with the debugger attached.




回答4:


With Visual Studio Pro 2010 building a .NET 4 application, this doesn't work for me.

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

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:

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.

Ugly but functional.




回答5:


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.

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


来源:https://stackoverflow.com/questions/9896857/visual-studio-remote-debugging-on-application-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!