Debugging in studio works but not in VSCode

I have made a working blink sketch in mbed studio, which can be debugged. After exporting it with the following command: mbed export -i vscode_gcc_arm -m nucleo_f303re --profile debug, I got a working project in vscode which can upload to the designated target. The problem I’m facing however is that I can’t debug the executing code.

My launch.json is as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/cmake_build/NUCLEO_F303RE/debug/GCC_ARM/${workspaceRootFolderName}.elf",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "targetArchitecture": "arm",
            "externalConsole": false,
            "debugServerArgs": "",
            "serverLaunchTimeout": 20000,
            "filterStderr": true,
            "filterStdout": false,
            "serverStarted": "GDB\\ server\\ started",
            "preLaunchTask": "mbed",
            "setupCommands": [
                {
                    "text": "-target-select remote localhost:3333",
                    "description": "connect to target",
                    "ignoreFailures": false
                },
                {
                    "text": "-file-exec-and-symbols ${workspaceRoot}/cmake_build/NUCLEO_F303RE/debug/GCC_ARM/${workspaceRootFolderName}.elf",
                    "description": "load file",
                    "ignoreFailures": false
                },
                {
                    "text": "-interpreter-exec console \"monitor endian little\"",
                    "ignoreFailures": false
                },
                {
                    "text": "-interpreter-exec console \"monitor reset\"",
                    "ignoreFailures": false
                },
                {
                    "text": "-interpreter-exec console \"monitor halt\"",
                    "ignoreFailures": false
                },
                {
                    "text": "-interpreter-exec console \"monitor arm semihosting enable\"",
                    "ignoreFailures": false
                },
                {
                    "text": "-target-download",
                    "description": "flash target",
                    "ignoreFailures": false
                }
            ],
            "logging": {
                "moduleLoad": true,
                "trace": true,
                "engineLogging": true,
                "programOutput": true,
                "exceptions": true
            },
            "linux": {
                "MIMode": "gdb",
                "MIDebuggerPath": "arm-none-eabi-gdb",
                "debugServerPath": "pyocd-gdbserver"
            },
            "osx": {
                "MIMode": "gdb",
                "MIDebuggerPath": "arm-none-eabi-gdb",
                "debugServerPath": "pyocd-gdbserver"
            },
            "windows": {
                "preLaunchTask": "mbedtools",
                "MIMode": "gdb",
                "MIDebuggerPath": "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.10\\bin\\arm-none-eabi-gdb.exe",
                "debugServerPath": "C:\\Code\\C++\\mbed6venv\\Scripts\\pyocd-gdbserver.exe",
                "setupCommands": [
                    {
                        "text": "-environment-cd ${workspaceRoot}/cmake_build/NUCLEO_F303RE/debug/GCC_ARM"
                    },
                    {
                        "text": "-target-select remote localhost:3333",
                        "description": "connect to target",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-file-exec-and-symbols ${workspaceRootFolderName}.elf",
                        "description": "load file",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-interpreter-exec console \"monitor endian little\"",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-interpreter-exec console \"monitor reset\"",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-interpreter-exec console \"monitor halt\"",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-interpreter-exec console \"monitor arm semihosting enable\"",
                        "ignoreFailures": false
                    },
                    {
                        "text": "-target-download",
                        "description": "flash target",
                        "ignoreFailures": false
                    }
                ]
            }
        },
        {
            "name": "Cortex",
            "cwd": "${workspaceRoot}",
            "executable": "./cmake_build/NUCLEO_F303RE/debug/GCC_ARM/${workspaceRootFolderName}.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "stlink",
            "serverArgs": [
                "-l 1",
                "-s",
                "-i 066EFF3035344E5043142257",
                "-v"
            ],
            "interface": "swd",
            "device": "nucleo_f303re",
        }
    ]
}

This launch.json uses the following tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "name": "mbedtools",
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}/mbed-os"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "args": [],
    "linux": {
        "command": "mbedtools"
    },
    "osx": {
        "command": "mbedtools"
    },
    "windows": {
        "command": "mbedtools"
    },
    "tasks": [
        {
            "label": "mbedtools",
            "type": "shell",
            "args": [
                "compile", "--profile=debug", "-t", "GCC_ARM", "-m", "nucleo_f303re"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}/mbed-os"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "group": {
                "_id": "build",
                "isDefault": false
            }
        }
    ]
}

The only major change I made is that I changed the build system to the mbed build system instead of make.

Target is a pcb based on the nucleo_f303re

You should install Cortex-Debug extension from the VsCode extensions tab. Then in launch.json click on Add Configuration on the bottom right. Select one of these from the list: st-link, st-util, py-ocd, open-ocd. I was using Py-ocd. Then when I created a new virtual environment with the latest pyton and pip versions I could not get it work again. Now I use st-link. I find Open-ocd not so handy. Open-ocd requires you to modify the config files and tell the debugger where it can find them. The added configurations should look like this for stlink:

{
    "cwd": "${workspaceFolder}",
    "executable": "${workspaceRoot}/BUILD/DISCO_F407VG/GCC_ARM-DEVELOP/${workspaceRootFolderName}.elf",
    "name": "Debug with ST-Link",
    "request": "launch",
    "type": "cortex-debug",
    "runToEntryPoint": "main",
    "showDevDebugOutput": "none",
    "servertype": "stlink"
},

You will also need to set the arm toolchain path in VsCode settings. To do this go to extensions again and select Cortex-Debug. Then click on ‘Manage’ next to the ‘Uninstall’ button and select ‘Extension Settings’. There you have to select the corresponding ‘Arm Toolchain Path’ for your os. Mine looks like this:

"cortex-debug.armToolchainPath.windows": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.10/bin",

In the end do not forget to select the correct debugger configuration before clicking on ‘Start Debugging’.

I do not kow which debug server is the best. I would be happy to hear if someone has experienced any difference between them.

I’m using it the same way. The stlink gdbserver is very good, as well as jlink when you have a jlink adapter. I have also used BMP, there is a lot of refactoring at the moment and I haven`t upgraded to the latest version yet.
For the stlink-gdbserver, a path to the executable must be also set. Don’t know if the autodetection is working now. The excecutable is deeply hidden in the ST installation.

1 Like

I’ve tried using those debuggers before and none of them seemed to work, the cortex config can be seen in the launch.json.

I’ve now resorted to using the mbed ce and their debug setup. This has seemed to partially work. I can debug a very small piece of code but once the code expands the hardware seems to crash. Making me think it might be a hardware issue.

but the config is not using the VSC extension ‘cortex-debug’. You are using the generic gdb, cortex-debug is better adapted to embedded debugging.
The mbed export to vscode is very old and was not long maintained, cortex-debug came later and better.
I still had no time to check the Mbed CE, it would be great to integrate the cortex-debug if not already existent. Apart from the debug server/probe settings, it needs only to know where to find the .elf and eventually the target mcu.

As I’ve said I’ve already tried the cortex-debug with the extension, with no succes. That’s why I switched to mbed ce which has been the most succesfull this far.

@JojoS Cortex-Debug is one of requirements for working with Mbed OS with Mbed-CE build system under VS Code.

Project Setup: VS Code · mbed-ce/mbed-os Wiki (github.com)

The launch.json is prepared automatically by the Mbed-CE cmake, according to user’s setup from cmake-variants.yaml file.

BR, Jan