Remote Debug Go Code with VSCode without Remote Development

At some point, it begins to be required to debug a program that cannot be debugged on a working computer. In my case, I needed to debug a program that communicates via D-Bus with iwd , a daemon that manages Wi-Fi connections, on a laptop.



VSCode has a Remote Development add-on specifically designed for such cases. He did not suit me for several reasons:



  1. Auto-signing GnuPG commits from VSCode didn't work.
  2. The SSH agent did not work (probably due to disabled agent forwarding).
  3. It would seem that the opening of the local directory on the remote machine, which seemed to exist in RD, did not work (some of the necessary files were not included in version control, and I did not want to do manual copying over the network every time).


I write in Go, so the hack I'm going to describe is for the Delve debugger . The approach itself changes little regardless of the programming language; similar can be done for VSCode used in Python ptvsd and any other debugger allowing remote connections.



TL; DR post
  1. , , SCP Delve.
  2. VSCode , .
  3. VSCode , .1 .


Scripting Delve Build and Run



Delve can work in debug server mode, allowing clients to connect over the network.



// dlv bash- Makefile, Taskfile, Taskfile.yml, shell-:



version: '2'

tasks:
  killall:
    cmds:
      #  Delve   ,
      #      ,
      #    
      - ssh target_machine killall dlv || true
  push:
    deps:
      - killall
    cmds:
      #    
      - go build -gcflags="all=-N -l" -o ./build/debug_binary ./cmd/program
      #     
      - scp ./build/debug_binary target_machine:/home/tdemin/Desktop/debug_binary
  delve:
    deps:
      - push
    cmds:
      #  dlv         64001;
      #   tmux    ,    
      #  dlv,  &  nohup   
      #  
      - ssh target_machine '(cd ~ && chmod +x Desktop/debug_binary && tmux new -d dlv --headless -l \[::\]:64001 exec ./Desktop/debug_binary)'


task delve; Taskfile.yml Delve ( SCP, scp dlv ), / Delve .





.vscode/launch.json, , :



{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to target",
            // dlv  API v1  ,
            //     --api-version
            "apiVersion": 1,
            "type": "go",
            "request": "attach",
            "mode": "remote",
            //       ; , 
            //  ,      ,
            //         
            "remotePath": "${workspaceFolder}",
            //   ,     
            //   
            "preLaunchTask": "Run Delve on target",
            "port": 64001,
            "host": "target_machine"
        }
    ]
}


.vscode/tasks.json :



{
    "version": "2.0.0",
    "tasks": [
        {
            //      preLaunchTask  launch.json
            "label": "Run Delve on target",
            "type": "shell",
            //  Taskfile
            "command": "task delve",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "presentation": {
                //       
                // ,   
                "reveal": "silent"
            }
        }
    ]
}




After everything is configured, you can press F5, a debugging session will start:



Debugging process in VSCode



This method works, but it has one big limitation: the terminal built into VSCode does not show the standard I / O of the process being debugged. If they are needed, after starting debugging, you can SSH to the tmux session in which the program is running in the background.



Links






All Articles