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:
- Ensure the program that you want to debug is the active program.
- Connect a board to your computer with a USB adapter and ensure it is the target platform for your program.
- Ensure the Debug profile is selected.
- Click the debug icon. If required, Mbed Studio automatically builds and flashes your program to the connected board.
- The Debug pane is displayed and the debug session is started.
Setting breakpoints and start debugging
Breakpoints are essential to debugging. They 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 the values of variables, or see whether or not a branch of code is getting executed.
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.
Using the step buttons to navigate your code
There are several options available to help you navigate your code more quickly.
- Step Over: With this button, you can skip over functions or methods and go straight to the parts of code you are more interested in.
- Step Into: This button advances the debugger into each function. The debugger then breaks on the first statement that gets executed in the function.
- Step Out: This button advances the debugger until the current function returns. Use this button if you want to advance the debugger 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. It saves time compared with stopping the debugger and restarting it with the debug icon 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 multi-threaded applications. Each thread appears on a separate row in the THREADS list and has its own call stack. 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 that they should contain. If you find a bad value, you should find out where the bad value was set (to find where the value was set, you might need to either 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 default property value.
Using the debug console
If you are an advanced user and want to use GDB commands to do debugging, the debug console is available. Open the View menu and select Debug Console. The debug console opens in a new tab. Check this GDB quick reference sheet for more details on the commands you can use.