Debugging an application

The main recommended tool for debugging in the nRF Connect SDK is nRF Debug of the nRF Connect for VS Code. The tool uses Microsoft’s debug adaptor and integrates custom debugging features specific for the nRF Connect SDK.

Use nRF Debug after adding the required Kconfig options to the prj.conf file. For details, see the How to debug an application section in the nRF Connect for VS Code documentation.

Read also the Debugging overview and other guides in the debugging section of the extension documentation for more information about debugging in the nRF Connect for VS Code, for example testing and debugging with custom options.

Note

For hands-on tutorials on debugging and troubleshooting applications in the nRF Connect SDK, see Lesson 2 - Debugging and troubleshooting in the nRF Connect SDK Intermediate course in the Nordic Developer Academy.

Debug configuration

When you are following the How to debug an application process in the nRF Connect for VS Code, the following Kconfig options are set to y when you add the build configuration in the Add Build Configuration page:

  • When you select the Optimize for debugging optimization level, the corresponding CONFIG_DEBUG_OPTIMIZATIONS Kconfig option is set to y. This option limits the optimizations made by the compiler to only those that do not impact debugging.

  • If you also enable the Include debug thread information checkbox in addition to Optimize for debugging, the corresponding CONFIG_DEBUG_THREAD_INFO Kconfig option is set to y. This option adds additional information to the thread object, so that the debugger can discover the threads. This will work for any debugger.

You can also set these options to y manually. There are many more Kconfig options for debugging that are specific to different modules. For details, see the respective documentation pages of the modules.

Debug suffixes and build types

Some applications and samples provide a specific configuration that enables additional debug functionalities, either as file suffixes or Custom build types. You can select custom configurations when you are configuring the build settings.

Debug logging

You can use logging system to get more information about the state of your application. Logs are integrated to many various modules and subsystems in the nRF Connect SDK and Zephyr. These logs are visible once you configure logger for your application.

You can also configure log level per logger module, for example to get more information about a given subsystem. See Logging in nRF Connect SDK for details on how to enable and configure logs.

Debug logging for the Bluetooth stack

To enable debug logging for the Bluetooth stack in your prj.conf file when using the nRF Connect SDK, set the CONFIG_BT_LOG_LEVEL_DBG Kconfig option to y. This enables debug-level logging for the Bluetooth subsystem, allowing you to see detailed logs from the Bluetooth stack during development and troubleshooting. For example, a minimal configuration might look like this:

CONFIG_BT=y
CONFIG_BT_LOG_LEVEL_DBG=y

If you want even more detailed debugging (such as stack traces or assertion failures), you can also enable the following Kconfig options:

  • CONFIG_ASSERT=y

  • CONFIG_BT_LOG_LEVEL_INF=y

  • CONFIG_EXCEPTION_STACK_TRACE=y

For more detailed logs from specific parts of the Bluetooth stack, you can add more specific configurations. To enable debug logs from a module of your interest, use a module-specific logging configuration string and add the _DBG prefix to it. For example:

To ensure that the logger module is enabled and you can see the logs, set the CONFIG_LOG Kconfig option to y.

For more advanced debug purposes, see the following:

  • Logging HCI packets for logs with a detailed view of what happens both inside and between Bluetooth devices.

  • The CONFIG_MPSL_PIN_DEBUG Kconfig option to figure out when the radio is used. On multi-core SoCs, set this option on the core that runs the radio firmware.

Debug libraries

The nRF Connect SDK also provides several libraries and drivers for debugging different components. For example:

  • You can use Debug libraries for any of your applications, for example to measure CPU load or trace hardware peripheral events on pins.

  • You can use nRF Profiler to measure performance and debug applications without introducing big performance overhead. This option requires introducing additional code changes: your application must register profiler events and log their occurrences.

  • When working with the SEGGER J-Link with the RTT feature, you can use the Ethernet over RTT driver, which is useful for handling data transfer.

  • The Thread protocol implementation offers pre-built libraries with debug symbols.

  • The Zigbee protocol implementation offers Zigbee OSIF and Shell libraries with custom Kconfig options that you can set for debugging.

Debugging stack overflows

One of the potential root causes of fatal errors in an application are stack overflows. Read the Stack Overflows section on the Fatal Errors page in the Zephyr documentation to learn about stack overflows and how to debug them.

You can also use a separate module to make sure that the stack sizes used by your application are big enough to avoid stack overflows. One of such modules is for example Zephyr’s Thread analyzer.

Debugging multi-core SoCs

If you use a multi-core SoC, for example from the nRF53 Series, and you only wish to debug the application core firmware, a single debug session is sufficient. To debug the firmware running on the network core, you need to set up two separate debug sessions: one for the network core and one for the application core. When debugging the network core, the application core debug session runs in the background and you can debug both cores if needed.

Complete the following steps to start debugging the network core:

  1. Set up sessions for the application core and network core as mentioned in the How to debug applications for a multi-core System on Chip section in the nRF Connect for VS Code documentation.

  2. Select the appropriate CPU for debugging in each session, corresponding to the application core and the network core of your SoC, respectively.

  3. Once both sessions are established, execute the code on the application core.

    The startup code releases the NETWORK.FORCEOFF signal to start the network core and allocates the necessary GPIO pins for it.

  4. Start code execution on the network core in the other debug session.

If you want to reset the network core while debugging, make sure to first reset the application core and execute the code.

Debugging secure and non-secure firmware

When using a board target with CMSE enabled (*/ns variant), by default you can only debug firmware in the non-secure environment of the application core firmware.

To debug firmware running in the secure environment, you need to build Trusted Firmware-M with debug symbols enabled and load the symbols during the debugging session. To build Trusted Firmware-M with debug symbols, set the CONFIG_TFM_CMAKE_BUILD_TYPE_RELWITHDEBINFO Kconfig option.

nRF Debug in the nRF Connect for VS Code automatically loads the Trusted Firmware-M debug symbols.

Enabling non-halting debugging with Cortex-M Debug Monitor

The debugging process can run in two modes. The halt-mode debugging stops the CPU when a debug request occurs. The monitor-mode debugging lets a CPU debug parts of an application while crucial functions continue. Unlike halt-mode, the monitor-mode is useful for scenarios like PWM motor control or Bluetooth, where halting the entire application is risky. The CPU takes debug interrupts, running a monitor code for J-Link communication and user-defined functions.

Use the following steps to enable monitor-mode debugging in the nRF Connect SDK:

  1. In the application configuration file, set the Kconfig options CONFIG_CORTEX_M_DEBUG_MONITOR_HOOK and CONFIG_SEGGER_DEBUGMON.

  2. Attach the debugger to the application.

  3. Depending on debugger you are using, enable monitor-mode debugging:

    • For nRF Debug in the nRF Connect for VS Code, the monitor-mode is automatically enabled when you set the CONFIG_SEGGER_DEBUGMON Kconfig option and build your application (-exec monitor exec SetMonModeDebug=1 is automatically passed to the debug console).

    • For debugging using Ozone, enter Exec.Command("SetMonModeDebug = 1"); in the console.

For more information about monitor-mode debugging, see Zephyr’s Cortex-M Debug Monitor documentation and SEGGER’s Monitor-mode Debugging documentation.

Remote debugging using Memfault

The nRF Connect SDK provides remote observability support to maintain optimal device performance and reliability in ever-changing device ecosystems. As you start to rollout a fleet, you can debug and monitor devices remotely.

This functionality allows you to efficiently collect crash information and get the same data from remote devices as you would when using a debugger.

For more information on enabling remote debugging with the nRF Connect SDK see nRF Cloud powered by Memfault integration.

Debugging tools

In addition to nRF Debug, you can use several other standalone tools to debug the nRF Connect SDK applications.

The following debugging tools are most commonly used in different areas of the nRF Connect SDK:

Most common debugging tools in the nRF Connect SDK

Tool

Purpose

Area

Bluetooth Low Energy app

Configure and test Bluetooth Low Energy devices. Available from nRF Connect for Desktop.

Bluetooth

Cellular Monitor app

Capture and analyze modem traces to evaluate communication and view network parameters. Available from nRF Connect for Desktop.

Cellular

Direct Test Mode app

Perform RF PHY checks of Bluetooth Low Energy devices using a GUI for the Bluetooth-specified Direct Test Mode. Available from nRF Connect for Desktop.

Bluetooth

RSSI Viewer app

Visualize RSSI and frequency in the 2.4-GHz band in real time. Available from nRF Connect for Desktop.

Bluetooth

Power Profiler app

Measure the real-time power consumption of your designs. Available from nRF Connect for Desktop.

Bluetooth, Cellular, Matter, Thread, Wi-Fi, Zigbee

Online Power Profiler (OPP)

Calculate the current consumption online.

Bluetooth, Cellular, Wi-Fi

nRF Sniffer for 802.15.4

Debug the communication over networking protocols by look into data exchanged over-the-air between devices.

Bluetooth Mesh, Matter, Thread, Zigbee

nRF Sniffer for Bluetooth LE

Debug Bluetooth packets that are sent between a selected Bluetooth Low Energy device and the device it is communicating with, even when the connection is encrypted. Available from nRF Util.

Bluetooth, Matter

nRF Thread Topology Monitor

Visualize Thread mesh network topology in real time.

Thread