Visual Studio remote debugging on application star

2020-08-09 05:38发布

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.

5条回答
迷人小祖宗
2楼-- · 2020-08-09 06:18

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.

查看更多
别忘想泡老子
3楼-- · 2020-08-09 06:23

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.

查看更多
Lonely孤独者°
4楼-- · 2020-08-09 06:24

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.

查看更多
smile是对你的礼貌
5楼-- · 2020-08-09 06:30

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.

查看更多
迷人小祖宗
6楼-- · 2020-08-09 06:33

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);
        }
查看更多
登录 后发表回答