'VSCode - Cortex-Debug - GDB Launch Commands

Im using vscode with the cortex-debug/jlink extension to debug my cortex m target.

I need the capability to issue GDB commands during launch (attach to debug and start session).

In this specific case, i wanna issue the command "directory C:...\BUILD" to GDB to replace absolute paths within the output file (*.elf).

However i have no clue how i can issue GDB commands in the VSCode launch.json file.

Example:

{
  // extension: cortex debug
  "version": "0.2.0",
  "configurations": [
    {
      "type": "cortex-debug",
      "request": "launch",
      "name": "Debug J-Link",
      "cwd": "${workspaceRoot}",
      "executable": "${workspaceRoot}/BUILD/output.elf",
      "serverpath": "/usr/bin/JLinkGDBServerExe",
      "servertype": "jlink",
      "device": "...",
      "interface": "swd",
      "serialNumber": "",
      "jlinkscript": "",
      "runToMain": true,
      "svdFile": ""
    },
    ...

How can i add GDB commands to this?



Solution 1:[1]

You can use the "postStartSessionCommands" and "postAttachCommands" parameters. Full list of all supported parameters that can be placed in a launch.json is available on Cortex-debug github here : https://github.com/Marus/cortex-debug/blob/master/package.json. Remember that those accept arrays of strings, so an example would be:

    {
        "cwd": "${workspaceRoot}",
        "executable": "./build/zephyr/zephyr.signed.confirmed.hex",
        "postStartSessionCommands": [
            "file /x/y/z/zephyr.elf"
        ],
        "breakAfterReset": true,
        (...)
    }

Solution 2:[2]

Below is my launch config for ARM Cortex-M4 board. I just use the cppdbg type. But note the mi* attributes:

{
    "name": "reel_board",
    "type": "cppdbg",
    "request": "launch",
    "program": "your.elf",
    "args": ["-ex", "load"],
    "stopAtEntry": false,
    "cwd": "${fileDirname}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "/your/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb",
    "miDebuggerServerAddress": "localhost:<your gdbserver port>",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        }
    ]
}

Once your gdbserver is started, you press F5 in VSC to launch the gdb client.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iwasz
Solution 2 smwikipedia