Cortex-M debugging: Real-time variable watch using SWV/SWO

Posted by Magnus Unemyr on Mar 25, 2015 3:06:00 PM

In this blog article, I will provide the first of three articles on what capabilities are available to Cortex-M developers in terms of real-time variable value visualization and memory access history analysis, using Serial Wire Viewer tracing. Using H/W support in Cortex-M devices (including STM32, Kinetis, EFM32, etc.), advanced debuggers can provide a lot of useful information to developers in pain trying to find that particularly hard to find “million-dollar” bug.


In particular, capabilities highlighted in the three articles in this blog post series are the possibility to watch variable values in real-time as the application runs at full execution speed (no need to stop on a breakpoint to check the value of a variable), the possibility to analyze the variable/memory access history in detail, as well as providing a graphical real-time plot of variable values in “oscilloscope style” as the application runs at full speed.

In previous blog articles, I have provided a general introduction to Serial Wire Viewer (SWV) real-time event- and data- tracing, as well as single-channel and multi-channel printf() redirection using the instrumentation Trace Macrocell which is part of the ARM CoreSight debugger architecture in general, and the Serial Wire Viewer trace module in particular. These blog articles provides basic background information on which this article extends upon. You may thus want to read those for a better understanding of this article.

As outlined in the previous blog articles, you need to configure the Serial Wire Viewer trace behavior, to make it emit the debugging data needed. By enabling all capabilities at the same time, you overload the 1-pin SWO interface with lost data packages as a result. Therefore it is recommended to only enable the SWV tracing data you need at a specific point in time, in order to avoid problem with buffer overruns due to bandwidth problems.

The Cortex-M core provides a limited set of H/W comparators that can emit data to the debugger whenever a memory address (and therefore a variable) changes its value. The H/W comparators monitor up to four 8-, 16- or 32-bit memory locations at the same time; and trigger on a data read, a data write or both. To optimize the SWO trace bandwidth usage, the Cortex-M core can send just the new data value, or the new data value plus the program counter (PC) address that caused the modification of the value, as needed.

To enable data tracing, open the SWV trace configuration dialog box in your debugger. In Atollic TrueSTUDIO, this toolbar button in any of the SWV debugger views opens the SWV configruation dialog box:


The SWV configuration dialog box in Atollic TrueSTUDIO looks like this:

To minimize risk of bandwidth problems, first disable all checkboxes/trace capabilities.

Then enable comparator 0 to watch one variable/memory location. In the comparator 0 group, enter the memory address or variable name to monitor in the “Address” field. Usually there is no need to monitor when a variable is read, and if this is the case set the “Access” dropdown field to only trigger a trace event when the variable is written to. In the size field, select if the memory location you monitor is 8, 16 or 32 bits. If you entered a variable name in the “Address” field, the size is configured automatically by Atollic TrueSTUDIO. In the “Generate” field, you can configure if you only want to record what new variable value that is written to this location, or if you also want to know what machine code instruction (program counter, PC) did the value change.

Additionally, you may want to enable the checkbox to instrument the trace packages with “Timestamps” as this will be needed in a later blog post to create a real-time “oscilloscope style” graphical plot of the variable value over time.

Optionally, configure comparator 1-3 in a similar manner to monitor also more variables. Please note that since the Cortex-M core can only monitor memory locations of 8-, 16- or 32-bit widths, the same limitation applies to watching variables – it is not possible to watch variable values of variables of larger size than 32 bits, or complex data structures, for this reason. Atollic TrueSTUDIO offers a different method to do that though, which is outlined in this blog article.

Having configured SWV to monitor memory accesses using comparators 0-3 according to what is outlined above, close the configuration dialog box by clicking OK and enable SWV trace recording by making sure the red “record” button is enabled:


Then open the SWV Data trace debugger view and start execution using the “play” toolbar button:


The SWV Data trace view now shows the current value (being updated in real-time as the application executes at full speed) in the top part of the view. As can be seen here, the “Watch” part lists the variable name or memory address being monitored on the enabled comparators, along with their real-time updated values.

Using the data trace capabilities of SWV available in Cortex-M devices, developers can get an increased insight into the dynamics of the system running at full execution speed. Upcoming blog articles will cover using the SWV Data Trace history log for memory access analysis, as well as how to plot an “oscilloscope style” real-time graph of variable values. This enables even better debugging capabilities.

Serial Wire Viewer event- and data- tracing works in Atollic TrueSTUDIO using both the SEGGER J-Link and ST-LINK debug probes. Please note Serial Wire Viewer event- and data- tracing is not supported by standard Eclipse and GDB solutions and is a proprietary extension made by Atollic in the TrueSTUDIO IDE.

For more information on Serial Wire Viewer event- and data- tracing, read this white paper:

Read our SWV event and data tracing whitepaper!



Topics: ARM Cortex, GNU tools (GCC/GDB), Debugging, Atollic TrueSTUDIO, SEGGER J-Link, ST-LINK