Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach to remote gdb with vscode?

I want to do remote C/C++ gdb debug with vscode. I use "Native Debug" extension where I do configuration. Here is my launch.json configuration

{
        "type": "gdb",
        "request": "launch",
        "name": "Launch Program (SSH)",
        "target": "./hello",
        "cwd": "/home/root/test1/",
        "ssh": {
            "host": "192.168.15.130",
            "cwd": "/home/root/test1/",
            "password": "",
            "user": "root"
} 

And at target I run

gdbserver localhost:2000 ./hello

Unfortunately after I can't sill connect with remote device to debug. Is there someone with experience on configuring of this?

like image 318
user3428154 Avatar asked Nov 28 '18 12:11

user3428154


People also ask

How do I connect to a remote server in VS Code?

In VS Code, select Remote-SSH: Connect to Host... from the Command Palette (F1, Ctrl+Shift+P) and use the same user@hostname as in step 1. If VS Code cannot automatically detect the type of server you are connecting to, you will be asked to select the type manually.

How do I add GDB code to Visual Studio?

Configuring VS CodeOpen the Debug panel ( CTRL + SHIFT + D ) and select “Add Configuration > GDB” through the top left dropdown arrow. Create a GDB configuration in launch. json and add the following. Note: Change the paths in “target”, “gdbpath”, and “autorun” to the correct locations.

Can you use GDB in VS Code?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)

How do I connect to Visual Studio remote debugger?

To perform remote debugging using Visual Studio: On the remote computer, in Visual Studio, choose Connect to Remote Debugger from the Tools menu. In the Connect to Remote Debugger dialog box, enter a connection string, and click Connect.


2 Answers

I found the answer of this question here: Is it possible to attach to a remote gdb target with vscode?

Summary:

First you need to install the Native Debug extension for VS Code.

Then edit launch.json file and add:

{
    "type": "gdb",
    "request": "attach",
    "name": "Attach to gdbserver",
    "executable": "<path to the elf/exe file relative to workspace root in order to load the symbols>",
    "target": "X.X.X.X:9999",
    "remote": true,
    "cwd": "${workspaceRoot}", 
    "gdbpath": "path/to/your/gdb",
    "autorun": [
            "any gdb commands to initiate your environment, if it is needed"
        ]
}

Then you can restart the VS Code and start debugging. Make sure there is no other client is connected to the gdb-server otherwise you will get time-out error.

like image 101
HmT Avatar answered Oct 18 '22 00:10

HmT


You have to install gdbserver on the remote machine. E.g.

apt-get install gdbserver

start the gdbserver on the remote machine

gdbserver :1234 ./mybinary

pick any port you like - here 1234

test the connection to gdbserver from your local machine by typing

gdb
(gdb) target remote myremotehostname:1234

trigger any gdb command to check whether it works - e.g. c (or continue) to continue running mybinary.

Put the following into your .vscode/launch.json

    {
      "name": "C++ Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/mybinary",
      "miDebuggerServerAddress": "myremotehostname:1234",
      "cwd": "${workspaceRoot}",
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb"
      }
    }

Or use the "type": "gdb" launch config as given in the other answer(s).

In order to get code browsing and stuff working, it's important to have the source directories in sync on the local and remote side. Use a network filesystem, manual copying, git triggers or anything like that to set this up.

like image 40
Martin Gerhardy Avatar answered Oct 18 '22 01:10

Martin Gerhardy