Debugging with Mbed Studio
This section presents the debugger mode and debugging tools available in Mbed Studio. You can step debug an Mbed program on any connected Mbed Enabled board that supports pyOCD. The debugger mode provides different ways of checking what your code is doing while it runs. You can step through your code, examine the execution path of your code, or look at the values stored in variables.
To start debugging:
Set the program you want to debug as the active program.
Connect a board to your computer with a USB adapter and ensure it is the build target for your program (Mbed Studio should autodetect the connect board).
Select the Debug profile.
Click the debug icon.
Mbed Studio automatically builds and flashes your program to the connected board.
The Debug pane is displayed and the debug session is started.
Setting up a debug session
Setting breakpoints and debugging
Breakpoints are useful when you know which part of your code needs to be examined. You can set one or more breakpoints to suspend your running code in order to take a look at variables' values, or see whether or not a block of code is getting executed.
Tip: By deafult, Mbed Studio stops on the first line of main().
To set a breakpoint, click in the margin to the left of a line of code in the editor pane. A red dot is displayed. Click the Continue button to start debugging. The debugger runs to the first breakpoint it encounters and stops. A yellow arrow is displayed next to the statement on which the debugger paused. The statement is highlighted in yellow.
Debug session with breakpoint on line 17
Using the step buttons to navigate your code
There are several options available to help you navigate your code more quickly.
- Step Over : Skip over functions or methods and go straight to the parts of code you are more interested in.
- Step Into : Advance the debugger into each function. The debugger then breaks on the first statement that gets executed in the function.
- Step Out : Advances the debugger until the current function returns (in other words, advances all the way through the current function).
Restarting or stopping the debugger
When you click the Restart button (), the debugger starts debugging the code from the beginning. This is faster than stopping the debugger and initiating a new debug session from the files pane.
If you do want to stop the debugger and get back into the editor pane, click the Stop button.
Examining threads and the call stack
You can examine and work with threads in the code that you are debugging. Working with threads is useful for debugging multithreaded applications. Each thread appears on a separate row in the THREADS list and has its own call stack.
When using the Mbed OS full profile (unlike the bare metal profile), you'll see at least three active threads in the THREADS list because Mbed OS uses three system threads. You may also see other drivers, such as networking drivers, spin their own threads.
When you select a thread, its call stack is displayed in the CALL STACK list. The call stack shows the order in which methods and functions are called.
You can inspect variables and check whether they are storing the values they should contain. If you find a wrong value, find out where it was set (you might need to restart the debugger, look at the call stack or both).
While paused in the debugger, hover over an object with the mouse, and you will see its current property value. You can also view variable values in the VARIABLES list.
Using GDB through the debug console
If you are an advanced user, you can use the debug console to interact with the debugger through GNU Debugger (GDB) commands.
To access the debug console, open the View menu and select Debug Console (or press Ctrl+Shift+Y (Windows) or Cmd+Shift+Y (macOS)).
The debug console provides functionality the UI does not expose, including the ability to inspect register values, break when a memory address is accessed and see where a variable is stored in memory.
For example, to view the contents of all registers while debugging:
Wait for your program to stop at a breakpoint.
Open the debug console (View > Debug Console).
In the debug console, type:
info registerscommand prints the information saved in all general-purpose CPU registers.
For more details about all of the GDB commands you can use, see the GDB Command Reference.