C ++ programs on remote debugging in Visual Studio Windows in several ways

background

Remote debugging ( Remote Debugging principles) can see below:
remote-debugger-client-apps

We can see, remote debugging is to communicate (using TCP protocol) will run remote programs and information through the network Visual Studio control information pass each other over the network. If some special BUG occurred in the absence of network conditions, the remote debugging on no use of.

Secondly, the need to deploy remote debugging a program (on the target machine being debugged Remote Tools ), this program msvsmon.exe version must be and Visual Studio versions of the same.

Remote tools can be downloaded https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging?view=vs-2019#download-and-install-the-remote-tools , but the installation of Visual Studio 's when this thing is integrated into the installation directory, you can refer https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging?view=vs-2019#fileshare_msvsmon .

With these prerequisites can be remotely debugging.

Remote debugging scenarios

Case1

Program running on the target machine abnormalities (such as stuck, and expected operating results contrary), would like to see attached directly to the target program situation.

Case2

Program on the target machine once it has started abnormal behavior trying to debug the target program in the startup process.

Case3

I do not want to run the target program directly on the development machine (this is quite common, because the development of the program will often do a mess of their own computer), so the target program to run on other machines, remote debugging target program.

msvsmon.exe settings

Preferably before starting off the firewall (running firewall.cplcan open the firewall configuration), to ensure the development and destination machines can each ping on.

Here is best to use an administrator starts msvsmon.exe , the start will see the following interface

Here Insert Picture Description

Here Insert Picture Description

Here you can see msvsmon.exe default TCP port is 4024, do not change; for simple authentication mode, select No Authentication and tick allow any user to debug; longest idle time is 900 seconds by default, here a few more zeros otherwise it will free up the debugger off.

Demo environment

IP address of my development machine is 172.18.165.25; my target machine is a virtual machine, IP address 192.168.174.130, the virtual machine network settings here for NAT mode, it will appear in the IP 192, you can choose bridge mode, so that developers and machine the target machine's IP in the same network segment.

Additional program

Additional programs are used for remote debugging is still running and the program in question, the operation is relatively simple. In Visual Studio 2019 in 调试 -> 附加到进程. The following settings:

Here Insert Picture Description

连接类型选择远程(无身份验证);连接目标填入目标机器的IP地址;勾选显示所有用户进程;点击刷新,就可以看到目标机器的所有进程了,这里要调试的进程是ConsoleApplication4.exe。因此选择这个进程附加。这里要注意,源码调试进程需要对应的源代码和PDB,这里PDB要么存在本地,要么存在远端服务器上,这里要配置好。

注意,应用程序和PDB必须一一对应,否则无法调试;但是源代码和应用程序不一样也是可以调试的(比如对应源代码新增了几行或者删除了几行,只要改动不大,一般都不影响调试),调试 -> 选项 -> 调试 -> 常规中取消勾选要求源文件与原始版本完全匹配

远程启动

远程启动要在项目的属性页中的调试中配置目标程序路径等一系列信息:

Here Insert Picture Description

这里说一下各个选项的注意事项:

  • 远程命令:填目标机器上的目标程序所在位置的绝对路径
  • 远程命令参数:同程序运行所需额外参数一样
  • 工作目录:除非要指定进程的工作目录,否者就默认填目标程序所在目录
  • 远程服务器名称:填目标机器的IP地址
  • 连接:选择不带身份验证的远程访问
  • 部署目录:这个下面重点讲一下,如果应用场景是Case3,请忽略这个选项
  • 其他要部署的文件:这个下面重点讲一下,如果应用场景是Case3,请忽略这个选项

这样就可以远程启动调试程序了。其实要配置这么多有点麻烦,刚刚说了,附加程序调试是最简单的,因此这里可以利用IsDebuggerPresent和DebugBreak在Windows项目中的应用来简化这个过程。

部署目录

部署目录这个选项专门应用于Case3。你想把自己的程序放在别的机器上运行,并且远程调试代码。

最直接的方法:在目标机器上编译链接程序,然后拷贝到目标机器上去,每编译一次,就拷贝一次,这样可以达到目的。

最直接的方法效率是很低下的,能不能编译、拷贝、运行全链路一键运行呢,是可以的,要利用这个部署目录。

Here Insert Picture Description

这是Visual Studio中的原话,意思就是会把你的项目输出直接复制到目标机器上,注意哦,假设你的项目输出是exe或者dll,就只会复制exe或者dll过去,至于输出项目的其他文件就要自己处理了。如果文件不多,就可以利用其他要部署的文件这个选项了。

只要填了部署目录这个选项,就可以在项目的右键属性上出现部署这个选项,点这个部署就可以在本地先输出程序,然后拷贝到目标机器上;

如果你想一键编译、部署,运行,那么就需要在项目的配置管理器中勾选部署这个选项。

Here Insert Picture Description

直接运行,就直接远程调试了:

Here Insert Picture Description

其他方法

上面的东西基本上可以满足绝大部分调试需求了。但是部署的时候只能部署exe或者dll。因此项目输出的其他内容可以在第一次全部拷贝到目标机器上,因此这些东西基本上是不变的,每次改动编译只会影响到exe,把exe拷贝过去其实就可以了;

加入到Visual Studio的文件也是可以作为部署内容拷贝到目标机器上的,文件的属性 -> 常规 -> 内容选择是,就可以把文件部署到目标机器的指定目录中;

由于我一般使用虚拟机作为目标机器,虚拟机上有一个共享文件夹的功能:

Here Insert Picture Description

勾选在Windows客户机中映射为网络驱动器,就可以在目标机器上的我的电脑中看到这个网络磁盘

Here Insert Picture Description

点进去可以看到你共享的Visual Studio输出目录。这一步间接地把文件在开发机和目标机同步了,因此,直接远程调试就行,不用再部署;

还有一种方法,就是利用Windows的网络发现,把目标机的一个文件夹共享出去,然后Visual Studio输出目录选择这个网络共享文件夹,这样也可以不用部署就能同步文件了。

总结

调试的方法千千万万种,要根据实际情况变通,本篇文章只是简单介绍了下远程调试的用法。

参考

Published 299 original articles · won praise 353 · Views 450,000 +

Guess you like

Origin blog.csdn.net/FlushHip/article/details/104022999