Vscode remote debugging and gdbserver configuration

If you are like me and prefer to use a GUI rather than the command line to set breakpoints, step through code, and inspect values ​​while the program is running, then you can set up VSCode and gdbserver to edit locally at runtime and Debug the code it's on the remote server.

Note: I'm using macOS Sierra locally and the remote machine is running Ubuntu 14.04, but this guide will apply to any Unix system. (Sorry, Windows users).

Note: Commands to be run on the remote computer are prefixed remote$, and local commands are prefixed local$.

1. Install gdbserver on the remote machine

Installation varies by system. On Debian/Ubuntu you can do the following:

remote$ apt-get install gdbserver

I installed it into my user folder using Linuxbrew :

remote$ brew install gdbserver

2. Install gdb on your local machine

On macOS Sierra, I use Homebrew to install gdb:

local$ brew install gdb --with-all-targets

NOTE: --with-all-targetsThe option is important; without it, you will not be able to debug on a remote computer that has a different operating system or architecture than the local computer.

3. Test gdb

At this point, you should be able to run gdbserver on the remote machine and connect to it from the local gdb CLI. I will forward the local port 9091 connection to the remote port 9091 using the ssh option:-L

local$ ssh -L9091:localhost:9091 user@remote
remote$ cd ./myproject/ && make
remote$ gdbserver :9091 ./myprogram

(Port 9091 is arbitrary; use any port number you like)

Let the command run in a terminal window; it will wait until gdb connects before running it ./myprogram.

In another terminal window on your local computer, run gdb:

local$ gdb
GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
...
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)

Then connect to the gdbserver instance:

(gdb) target remote localhost:9091
Remote debugging using localhost:9091
...
(gdb)

To verify that everything is working properly, you can run various gdb commands, for example , info sourcesor set breakpoints using breakUse continueto run ./myprogram.

4. Collaborative design gdb

VSCode will prevent you from running gdb unless it is signed

5. Synchronize local and remote file systems

You may have noticed that for basic functionality, the gdb CLI on your local machine does not need to provide any information about the program or its source code, other than the host and port on which gdbserver is running. But there are two important reasons why you want to keep your local and remote project directories in sync:

  • View the source code in the gdb CLI (ie list).
  • The VSCode  C/C++ extension requires you to provide the path to the compiled executable to start gdb. ( "program"Field in launch.json).

I chose to use sshfs because it requires minimal server-side setup, but you can use NFS, rsync, or other alternatives.

Using sshfs, mount the remote project folder locally:

local$ mkdir ./myproject
local$ sshfs user@remote:myproject ./myproject

Note: On macOS, you can uninstall this directory later using umount ./myproject. On Linux, use fusermount -u ./myproject.

6. Configure Visual Studio Code

./myproject/Open the newly installed project in VSCode and open .vscode/launch.jsonor create it if it doesn't exist:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C++ Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/myprogram",
      "miDebuggerServerAddress": "localhost:9091",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb"
      },
      "osx": {
        "MIMode": "gdb"
      },
      "windows": {
        "MIMode": "gdb"
      }
    }
  ]
}

This configuration will cause clicking "C++ Launch" to run gdb similar to:

local$ gdb ./myprogram
...
(gdb) target remote localhost:9091

7. Write a script to compile the program and start gdbserver

Ideally, you want to be able to run a single command or click a single button to compile and debug your program.

This could probably be done with VSCode tasks and preLaunchTaskoptions in launch.json , but I can't put together a simple solution using these tasks.

Instead, I wrote a quick and dirty shell script prepare_remote_debug.sh:

# Kill gdbserver if it's running
ssh user@remote killall gdbserver &> /dev/null
# Compile myprogram and launch gdbserver, listening on port 9091
ssh \
  -L9091:localhost:9091 \
  user@remote \
  "zsh -l -c 'cd myproject && make && gdbserver :9091 ./myprogram'"

8. Start debugging

This is your new workflow:

  1. Edit some code.
  2. ./prepare_remote_debug.shRun in a terminal window. The output of your program will appear here.
  3. Set some breakpoints.
  4. Run "C++ Startup".
  5. Step through code in VSCode's debugger.
  6. repeat.

Guess you like

Origin blog.csdn.net/qq_41929396/article/details/132878790