Remote debugging of applications with Visual Studio Code (VSC)

This section describes how to use your Windows-based development environment to compile and debug applications on TwinCAT/BSD target systems.

The basic idea is that you keep all your source code on the Windows development computer. The source code editing is done on Windows and the debugging front end also runs on your normal Windows development computer. An SSH connection is used for compiling and debugging on TwinCAT/BSD. This process can be divided into the following steps:

  1. Synchronize the source code from the Windows development computer to the TwinCAT/BSD target system via SSH using rsync.
  2. Compile the application on the target system via an SSH connection.
  3. Debug using VSC via an SSH connection to gdb, which runs on the target system.

Requirements:

(Get-Content -Encoding UTF8 $home\.ssh\id_ed25519.pub).Replace("`r`n", "`n") | ssh Administrator@172.17.66.111 'cat >> ~/.ssh/authorized_keys'
(CTRL+p) ext install ms-vscode.cpptools

VSC settings:

Now cwrsync and ssh are available, and you can continue to set up VSC for remote debugging.

1. Start by synchronizing the source code. Make sure you have a .vscode folder in your project directory. Then select the following from the VSC menu:
Terminal->Configure Tasks...->Create tasks.json from template->Others
2. Customize the following template according to your configuration to get a task like syn-src-to-remote.
{
  "version": "2.0.0",
  "windows": {
    "options": {
      "shell": {
        "executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
        "args": [
          "-NoProfile",
          "-ExecutionPolicy",
          "Bypass",
          "-Command"
        ]
      }
    }
  },
  "tasks": [
    {
      "label": "sync-src-to-remote",
      "type": "shell",
      "args": [
        "-aP",
        "--exclude",
        ".git",
        "--exclude",
        "This folder should be ignored",
        "-e", "c:/''Program Files''/cwrsync/bin/ssh.exe -i ${userHome}/.ssh/id_ed25519 -o ''StrictHostKeyChecking no''",
        ".",
        "Administrator@172.17.66.111:~/${workspaceFolderBasename}/"
      ],
      "command": "c:/Program Files/cwrsync/bin/rsync.exe",
      "problemMatcher": []
    }
  ]
}
3. You can test the new task as follows:
Menu->Terminal->Run Task->sync-src-to-remote
4. Now you should see something in the terminal output window and the source files should appear on your target system. If you see "permission denied" errors, try these additional rsync arguments:
"--chmod=u=rwX --chmod=go=rX"
5. If you use another SSH port, add it with -p <Port>:
"c:/''Program Files''/cwrsync/bin/ssh.exe -p 22222 -i ${userHome}/.ssh/id_ed25519 -o ''StrictHostKeyChecking no''"
6. The next step is to build your application remotely on your target system. Add another task build-on-remote to your tasks.json.
{
  "version": "2.0.0",
  "windows": {
    "options": {
      "shell": {
        "executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
        "args": [
          "-NoProfile",
          "-ExecutionPolicy",
          "Bypass",
          "-Command"
        ]
      }
    }
  },
  "tasks": [
    {
      "label": "sync-src-to-remote",
      "type": "shell",
      "args": [
        "-aP",
        "--exclude",
        ".git",
        "--exclude",
        "This folder should be ignored",
        "-e", "c:/''Program Files''/cwrsync/bin/ssh.exe -i ${userHome}/.ssh/id_ed25519 -o ''StrictHostKeyChecking no''",
        ".",
        "Administrator@172.17.66.111:~/${workspaceFolderBasename}/"
      ],
      "command": "c:/Program Files/cwrsync/bin/rsync.exe",
      "problemMatcher": []
    },
    {
      "dependsOn":"sync-src-to-remote" ,
      "label": "build-on-remote",
      "group" : "build",
      "type" : "shell",
      "args": [
         "Administrator@172.17.66.111",
         "cd ${workspaceFolderBasename}; clang++ -g -Wall -pedantic main.cpp -o main. bin"
      ],
      "command": "ssh",
      "problemMatcher": []
    }
    ]
}
7. This time you can use STRG+SHIFT+B to run the new build-on-remote task because we added it to the build group.
8. Add a launch configuration to debug your application remotely. Select the following in the menu:
Run->Add configuration
9. Edit the launch.json template to fit your requirements:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "debug-on-remote",
      "type": "cppdbg",
      "request": "launch",
      "cwd": "/home/Administrator/${workspaceFolderBasename}",
      "preLaunchTask": "build-on-remote",
      "program": "main",
      "stopAtEntry": false,
      "externalConsole": false,
      "sourceFileMap": {
        "/home/Administrator/${workspaceFolderBasename}": "${workspaceFolder}"
      },
      "pipeTransport": {
        "debuggerPath": "/usr/bin/gdb",
        "pipeProgram": "ssh",
        "pipeArgs": [
          "Administrator@172.17.66.111"
        ]
      },
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}
10. Adjust a few parameters to your requirements:
program: should be the binary on your remote machine you want to debug
pipeTransport->pipeArgs :username and ip address for your remote machine

Now you have a launch.json file. F5 will run a debugger connected to your program on the target computer.
Notice : Ensure that you compile with debug symbols enabled ("-g").