Solve the problem of VScode configuration remote debugging Linux program

Solve the problem of VScode configuration remote debugging Linux program

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.
Although there are other articles introducing this process online, they all describe successful situations, without describing the problems encountered during the process, and some parts are not very clear. So I thought I'd write one. Also, please note that this document introduces remote debugging, not remote compilation . Remote debugging of VScode is also supported, but I don't need it at the moment. I will configure it later if needed. Moreover, my project needs to be compiled using cmake and make, not directly with g++, so I haven't started the configuration yet.
VScode's remote debugging is performed using the gdbserver mechanism. The general principle is to use VScode on Windows or other graphical systems, use the Remote Development plug-in to ssh to connect to the remote Linux, and then perform remote debugging through the connection provided by gdbserver. The following describes the specific configuration method.

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 developmentC++

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.
After installing the Remote Development plug-in, there is a remote resource manager icon on the far left of VScode, as shown in the figure below. Then select SSH Targets, click the plus sign, add it in the format of user@ip, and then you will see the configuration file of the remote connection according to the prompts. Or you can directly open the configuration file by clicking the gear next to the plus sign on the interface below, add it according to the following format, and add the path of the private key after IdentityFile:

insert image description here

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:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'vscode_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "vscode_rsa": bad permissions

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:

insert image description here

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 open the VScode resource manager, which is the one on the top left that browses files. You will be prompted to open the remote folder. At this time, just follow the prompts to open the folder where the program to be debugged is located.

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.
The only thing that needs to be modified in the generated launch.json file is the program field. ${workspaceFolder} refers to the remote folder you just opened. You only need to specify the name of the program to be debugged. The stopAtEntry field defaults to false, which refers to whether the breakpoint is at the main function when debugging starts, so change it to true. For other settings, just use the default ones and don’t need to add anything.

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:
  • Implementation of remote Linux development using vscode
  • Use VSCode's Remote-SSH to connect to Linux for remote development
  • Solution to the error problem of Vscode remotely connecting to Ubuntu

<<:  JavaScript to achieve simple drag effect

>>:  How to create a view in MySQL

Recommend

JS function call, apply and bind super detailed method

Table of contents JS function call, apply and bin...

Common errors and solutions for connecting Navicat to virtual machine MySQL

Question 1 solve Start the service: service mysql...

MySQL InnoDB row_id boundary overflow verification method steps

background I talked to my classmates about a boun...

Implement full screen and monitor exit full screen in Vue

Table of contents Preface: Implementation steps: ...

MySQL server 5.7.20 installation and configuration method graphic tutorial

This article records the installation and configu...

Detailed explanation of Linux rpm and yum commands and usage

RPM package management A packaging and installati...

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...

Detailed explanation of top command output in Linux

Preface I believe everyone has used the top comma...

Definition and usage of MySQL cursor

Creating a Cursor First, create a data table in M...

3 Tips You Must Know When Learning JavaScript

Table of contents 1. The magical extension operat...

Detailed explanation of tcpdump command examples in Linux

Preface To put it simply, tcpdump is a packet ana...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...

The best solution for resetting the root password of MySQL 8.0.23

This method was edited on February 7, 2021. The v...

Implementation of Docker deployment of MySQL cluster

Disadvantages of single-node database Large-scale...

MySQL online log library migration example

Let me tell you about a recent case. A game log l...