visual-studio-codegdbnios

How to set and use a random number in launch.json


TL;DR

I'm looking for a way to set and use a random environment variable each time I launch the debugger. Specifically, I'd like to be able to use a random port number for the GDB server and client. My configuration currently looks like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "app",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/app/app.elf",
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "MIMode": "gdb",
      "miDebuggerServerAddress": "localhost:3333",
      "miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
      "debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
      "debugServerArgs": "--tcpport 3333 --reset-target --tcptimeout 5",
      }
   ]
}

Context

I'm using the Nios ii Embedded Design Suite via Visual Studio Code in order to avoid using Eclipse. Everything works quite nicely via vscode including debugging, however, there's an annoying bug in nios2-gdb-server; when it exits, it doesn't always kill the TCP connection. So, if you want to start a new debug session shortly thereafter, it will fail as the port is still in use (the port eventually closes after a few minutes). The Eclipse side of the tools avoids this by always using a random port for each debug session. I'm trying to find a convenient way to replicate that in vscode.


Solution

  • You can use extension Command Variable v1.42.1

    Use the commands: extension.commandvariable.number, and extension.commandvariable.remember

    In the example I used a random port number that is different from the 10 previous numbers.

    Depending on which string is evaluated first by VSC you might need to swap the 2 ${input} variables.

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "app",
          "type": "cppdbg",
          "request": "launch",
          "program": "${workspaceFolder}/app/app.elf",
          "stopAtEntry": true,
          "cwd": "${workspaceFolder}",
          "MIMode": "gdb",
          "miDebuggerServerAddress": "localhost:${input:randomPort}",
          "miDebuggerPath": "/home/me/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
          "debugServerPath": "/home/me/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
          "debugServerArgs": "--tcpport ${input:rememberRandomPort} --reset-target --tcptimeout 5",
        }
      ],
      "inputs": [
        {
          "id": "randomPort",
          "type": "command",
          "command": "extension.commandvariable.number",
          "args": {
            "name": "randomPort",
            "range": [1500, 60000],
            "random": true,
            "uniqueCount": 10
          }
        },
        {
          "id": "rememberRandomPort",
          "type": "command",
          "command": "extension.commandvariable.remember",
          "args": { "key": "number-randomPort" }
        }
      ]
    }