Let's take a look at the problem of VScode remote debugging Linux program. The specific content is as follows, let's take a look! I've recently been debugging a program on Linux, but I'm still at the entry-level stage of using gdb, mainly because it doesn't have an intuitive graphical interface. I searched online and found two options. One is through the remote debugging function of VisualStudio2019. I haven't tried it because I have been using VScode recently. I can try it when I have time. Another solution is to perform remote debugging through the Remote Development plug-in of VScode (officially provided by Microsoft). This article introduces this solution. Required software and plugins First of all, you must install gdb and gdbserver. You can install them according to the type of your remote system. I use Ubuntu, which is already installed by default. The command is as follows: sudo apt install gdb sudo apt install gdbserver Secondly, you need to install the Remote Development plug-in for VScode, the official C/C++ plug-in. After this C/C++ plug-in is remotely connected to Linux, it also needs to be installed on the remote Linux. You can see in the screenshot below that there is a "Extension enabled on SSH: xxxx" next to the plugin's uninstall button, which means it has already been installed. The installation method will be introduced later after the connection is successful. remote connection After installing the Remote Development plug-in, you can remotely connect to Linux. There are two ways to connect via ssh. One is with an account and password. Another type is public-private key connection. It is recommended to use public and private keys to connect here, because the remote debugging process will be connected to multiple places later, and it is troublesome to enter the password multiple times. If you use public and private keys, you only need to configure it once, which is very convenient. If you still choose to connect with an account and password, you can skip this step. The ssh remote configuration method is relatively simple, but there is a big problem on Windows. First, generate a public and private key pair on the remote Linux: # Execute the following command and generate a public and private key pair according to the prompts. ssh-keygen -t rsa # The public key is saved directly in the generated path and then transferred to authorized_keys # Store in the user's .ssh directory. Generally, when generating, the default path is the user's .ssh directory. # Assume that the generated public key is "vscode_rsa.pub". Finally, pay attention to the permission settings. No change is required by default. cat /home/user/.ssh/vscode_rsa.pub >> /home/user/.ssh/authorized_keys chmod 644 /root/.ssh/authorized_keys # Download the private key to the Windows machine # Assume the path is "D:/.ssh/vscode_rsa" There are no problems up to here. Now you need to configure the connection in VScode. Host xxxx HostName xxxx User username IdentityFile D:/.ssh/vscode_rsa Then you can open the remote folder in the original file browsing interface. But when the connection is configured, the VScode terminal reports an error:
The root cause is the permission problem of the private key. If this is done in Linux, you can directly use chmod to change it to 644, but in Windows, it is a little more troublesome. Solution: Right-click on the private key and select Properties, then select the [Security] tab, then click the [Advanced] button below, then click [Disable Inheritance] at the bottom of the new pop-up window, and then click the [Add] button above the inheritance button to re-set the current window login user as the owner of the private key and check all permissions. Finally, it should be the same as below: At this time, open the VScode remote connection again and there will be no problem. Remote debugging VScode settings First, you need to install the C/C++ plug-in mentioned above on the remote Linux. The installation method is simple. Click the plug-in. You can see that some plug-ins have a green prompt [In SSH: IP] in the installed plug-ins. Find the C/C++ plug-in, click the green prompt, and install it on the remote Linux. After installation, restart VScode. It is best to restart the remote Linux as well, because I did not start it. When I operated later, VScode prompted that it could not find the selected debugger type, and it would not automatically generate a launch.json file based on the debugger you selected. But if you can succeed without rebooting, that would be best. Then select Run->Add Configuration in the menu bar, and a prompt will pop up to select the debugging environment. Select [C++ GDB/LLDB] to automatically generate the launch.json file. as follows: { // Use IntelliSense to learn about related properties. // Hover to see descriptions of existing properties. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Start", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/program", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } If it is not automatically generated, it means that VScode does not recognize the environment and the plug-in you installed has not taken effect, so you need to restart VScode and the remote Linux. Remote Linux starts gdbserver Start gdbserver on the remote Linux as follows: #gdbserver localhost:<port> <program> <args> gdbserver localhost:2333 /path/to/myprogram arg1 arg2 Be careful not to change the port number, as this is the port number used by default when VScode connects. Then just press F5 in VScode to debug, gdb will automatically view the source code, so it is best to use the debug version of the program to be debugged. Reference articles: https://warmgrid.github.io/2019/05/21/remote-debug-in-vscode-insiders.html https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open This is the end of this article about solving the problem of VScode configuration remote debugging Linux program. For more relevant VScode remote debugging Linux program content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: JavaScript to achieve simple drag effect
>>: How to create a view in MySQL
This article introduces how to install the system...
Table of contents 1. Install Docker 2. Pull the J...
Download the Windows version of Nginx from the Ng...
Preview versions of Safari (Technology Preview 10...
1. Introduction MySQL locks can be divided into g...
I have been in contact with MGR for some time. Wi...
1. Indexing principle Indexes are used to quickly...
The tutorial for installing OpenStack Ussuri with...
Recently, I started upgrading my blog. In the proc...
Table of contents First Look Index The concept of...
tcpdump is a flexible and powerful packet capture...
One demand Generally speaking, a company has mult...
Preface This article uses the new features of MyS...
This article shares with you the installation and...
Use HTML to write a dynamic web clock. The code i...