Skip to content

Advanced debugging techniques

This page describes advanced debugging techniques. It is recommended to first become familiar with the features and concepts described on the Debugging concepts page before reading this page.

Changing the default debugger

If you want to use Cortex-Debug, you can change the debugging settings in the Extension Settings. When the Cortex-Debug option is selected, the Debug button in the Actions View will use this tool to debug applications.

To enable Cortex-Debug:

  1. Click on the Extensions icon from the Activity Bar or press Ctrl+Shift+X.

  2. Find the nRF Connect for VS Code and click on the Settings icon on the bottom right corner of the extension.

  3. Click on Extension Settings.

  4. Scroll down to Nrf-connect > Debugging: Backend.

  5. In the drop-down menu, click on Cortex-Debug.

Change default debugger

Custom launch and debug configurations

Similar to the build and flash task configuration, you can customize and bind specific actions when launching the debugger in VS Code. You can tweak the debugging configuration for your specific setup by opening and editing a launch.json file. This allows you to launch the debugger with custom debugging setup details.

You can read more about the general uses of launch configurations in VS Code's documentation.

There are two ways to open a launch.json file and add custom nRF Debug configurations:

  • From the Actions View:

    1. Hover your cursor over Debug.
    2. Click the cogwheel icon called Configure debugging.
  • From the top-level menu bar:

    1. Go to Run > Add configuration.... A list of debuggers appears.
    2. Choose nRF Connect.

The following example shows a launch.json file using the nrf-connect debug type.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "nrf-connect",
            "request": "launch",
            "name": "Launch active build configuration",
            "config": "${activeConfig}",
            "runToEntryPoint": "main",
            "preLaunchTask": "nRF Connect: Build active configuration"
        },
        {
            "name": "Launch build_nrf52840dk_nrf52840",
            "type": "nrf-connect",
            "request": "launch",
            "config": "c:\\Users\\joe\\workspace\\apps\\hello_world\\build_nrf52840",
            "runToEntryPoint": "main"
        }
    ]
}

Additional configuration

You can also add additional configuration fields in the launch.json for debugging with the extension's nRF Debug. These configuration fields allow you to do the following actions:

  • Launch against a server that already runs.
  • Target non-localhost servers.
  • Use servers other than SEGGER's (for example, west debug).
  • Run without a configured toolchain.

See the Debugging UI and settings reference page for a detailed description of available configuration fields and the variables that they accept.

For example, the following configuration lets you set up a debug session against a remote GDB server that's already running on IP address 10.0.0.84, port 2331, using the serverAddress and flash configuration fields:

launch.json
{
    "configurations": [
        {
            "type": "nrf-connect",
            "request": "Remote debug",
            "config": "${activeConfig}",
            "serverpath": null,
            "serverAddress": "10.0.0.84:2331",
            "flash": false
        }
    ]
}

Custom section in debugger Memory Viewer

You can add a custom memory region to nRF Debug's Memory Viewer. This displays the defined device's memory address within its own region tab. This is useful if you want to view a specific memory address on your device, rather than viewing all memory addresses within the flash and RAM.

Complete the following steps to add a custom memory region tab:

  1. Click on the Configure debugging button (the cog wheel) that is to the right of Debug in the extension's Actions View. This will open the launch.json in your current workspace.

  2. Extend the configuration by adding a memorySections array to it. The following example shows how to add a 1-KB custom section that starts at the address 0xCAFE0000:

    Add custom memory section
        "memorySections": [
            { "name": "Custom section", "start": "0xCAFE0000", "size": 1024 }
    
    • The start property defines the start address of the section.
    • The size property defines the number of bytes from the start address that the property should span.
    • Either integers or hex strings can be used for start, and only integers are valid for size.

    A validation check is run against these values and a warning message will be shown if they are invalid.

    The launch.json file should now include a memorySections array and look like the following:

    Full configuration
    {
        "configurations": [
            {
                "type": "nrf-connect",
                "request": "launch",
                "config": "${activeConfig}",
                "name": "Launch nRF Connect application",
                "memorySections": [
                    { "name": "Custom section", "start": "0xCAFE0000", "size": 1024 }
                ]
            }
        ]
    }
    
  3. Save the file.

  4. Click on Debug from the Actions View.

  5. Once the Debug toolbar appears in the editor, click on the Pause button.

  6. Open the nRF Debug Panel View from the Panel Ctrl+J.

You can now see the custom region tab in the Memory Viewer.