Debugging using the IAR C-SPY Debugger
Debugging the application
Starting the debugger
Organizing the windows
Inspecting source statements
Inspecting variables
Setting and monitoring breakpoints
Debugging in disassembly mode
Monitoring memory
Viewing terminal I/O
Reaching program exit
* This tutorial continues the development cycle started in the previous tutorial and explores the basic features of C-SPY®.
* Note that, depending on what IAR Systems product package you have installed, C-SPY might or might not be included. The tutorials assume that you are using the C-SPY Simulator.
Debugging the application
The project1.out application, created in the previous tutorial, is now ready to be run in C-SPY where you can watch variables, set breakpoints, view code in disassembly mode, monitor registers and memory, and print the program output in the Terminal I/O window, etc.
Starting the debugger
Before starting C-SPY, you must set a few options.
1
Choose Project>Options and then the Debugger category. On the Setup page, make sure that you have chosen Simulator from the Driver drop-down list and that Run to main is selected. Click OK.
2
Choose Project>Download and Debug. Alternatively, click the Download and Debug button in the toolbar. C-SPY starts with the project1.out application loaded. In addition to the windows already opened in the IDE, a set of C-SPY-specific windows are now available.
Organizing the windows
In the IDE, you can dock windows at specific places, and organize them in tab groups. You can also make a window floating, which means it is always on top of other windows. If you change the size or position of a floating window, other currently open windows are not affected.
The status bar, located at the bottom of the the IAR Embedded Workbench® IDE main window, contains useful help about how to arrange windows. For further details, see the IDE Project Management and Building Guide for ARM®.
Make sure the following windows and window contents are open and visible on the screen: the Workspace window with the active build configuration tutorials – project1, the editor window with the source files Tutor.c and Utilities.c, and the Debug Log window.
Inspecting source statements
1
2
With the file Tutor.c displayed in the editor window, first step over with the Debug>Step Over command.
Alternatively, click the Step Over button on the toolbar.
Step until the call to the InitFib function.
3
Choose Debug>Step Into to step into the function InitFib.
Alternatively, click the Step Into button on the toolbar.
At source level, the Step Over and Step Into commands allow you to execute your application a statement at a time. Step Into continues stepping inside function or subroutine calls, whereas Step Over executes each function call in a single step. For further details, see the C-SPY® Debugging Guide for ARM®.
When Step Into is executed you will notice that the active window changes to Utilities.c as the function InitFib is located in this file.
4
Use the Step Into command until you reach the for loop.
5
Use Step Over until you are back in the header of the for loop. Notice that the step points are on a function call level, not on a statement level.
You can also step on a statement level. Choose Debug>Next statement to execute one statement at a time. Alternatively, click the Next statement button on the toolbar.
Notice how this command differs from the Step Over and the Step Into commands.
Inspecting variables
C-SPY allows you to watch variables or expressions in the source code, so that you can keep track of their values as you execute your application. You can look at a variable in several ways. For example, point at it in the source window with the mouse pointer, or open one of the Auto, Locals, Live Watch, Statics, or Watch windows. In this tutorial, we will look into some of these methods.For more information about inspecting variables, see the C-SPY® Debugging Guide for ARM®.
Note: When optimization level None is used, all non-static variables will live during their entire scope and thus, the variables are fully debuggable. When higher levels of optimizations are used, variables might not be fully debuggable.
Using the Auto window
1
Choose View>Auto to open the Auto window.
The Auto window will show the current value of recently modified expressions.
2
Setting a watchpoint
Next you will use the Watch window to inspect variables.
3
Choose View>Watch to open the Watch window. Notice that it is, by default, grouped together with the currently open Auto window; the windows are located as a tab group.
4
Set a watchpoint on the variable i using this procedure: Click the dotted rectangle in the Watch window. In the entry field that appears, type i and press the Enter key.
You can also drag a variable from the editor window to the Watch window.
5
Select the Fib array in the InitFib function, then drag it to the Watch window.
The Watch window will show the current value of i and Fib. You can expand the Fib array to watch it in more detail.
6
7
Setting and monitoring breakpoints
C-SPY contains a powerful breakpoint system with many features. For detailed information about breakpoints, see the C-SPY® Debugging Guide for ARM®.
The most convenient way is usually to set breakpoints interactively, simply by positioning the insertion point in or near a statement and then choosing the Toggle Breakpoint command.
1
Set a breakpoint on the function call GetFib(i) using this procedure: First, click the Utilities.c tab in the editor window and click in the statement to position the insertion point. Then choose Edit>Toggle Breakpoint.
Alternatively, click the Toggle Breakpoint button on the toolbar.
A breakpoint will be set at this function call. The function call will be highlighted and there will be a red dot in the margin to show that there is a breakpoint there.
To view all defined breakpoints, choose View>Breakpoints to open the Breakpoints window. You can find information about the breakpoint execution in the Debug Log window.
Executing up to a breakpoint
2
Alternatively, click the Go button on the toolbar.
The application will execute up to the breakpoint you have set. The Watch window will display the value of the Fib expression and the Debug Log window will contain information about the breakpoint.
3
Select the breakpoint, right-click and choose Toggle Breakpoint (Code) from the context menu, alternatively choose Toggle Breakpoint from the Edit menu to remove the breakpoint.
Debugging in disassembly mode
Debugging with C-SPY is usually quicker and more straightforward in C/C++ source mode. However, if you want to have full control over low-level routines, you can debug in disassembly mode where each step corresponds to one assembler instruction. C-SPY lets you switch freely between the two modes.
1
First reset your application by clicking the Reset button on the toolbar.
2
Choose View>Disassembly to open the Disassembly window, if it is not already open.You will see the assembler code corresponding to the current C statement.
To view code coverage information, right-click in the Disassembly window and choose Code Coverage>Enable and then Code Coverage>Show from the context menu.
Try the step commands also in the Disassembly window and note the code coverage information indicated by green diamonds.
Monitoring memory
The Memory window lets you monitor selected areas of memory. In the following example, the memory corresponding to the array Fib will be monitored.
1
Choose View>Memory to open the Memory window.
2
Make the Utilities.c window active and select Fib. Then drag it from the C source window to the Memory window.
The memory contents in the Memory window corresponding to Fib will be selected.
3
To display the memory contents as 16-bit data units, choose the x2 Units command from the drop-down arrow menu on the Memory window toolbar.
If not all of the memory units have been initialized by the InitFib function of the C application yet, continue to step over and you will notice how the memory contents are updated.
To change the memory contents, edit the values in the Memory window. Just place the insertion point at the memory content that you want to edit and type the desired value.
Close the Memory window.
Viewing terminal I/O
Sometimes you might have to debug constructions in your application that make use of stdin and stdout without the possibility of having hardware support. C-SPY lets you simulate stdin and stdout by using the Terminal I/O window.
Note: The Terminal I/O window is only available in C-SPY if you have linked your project using the output option Semihosted or the IAR breakpoint option. This means that some low-level routines are linked that direct stdin and stdout to the Terminal I/O window, see Linking the application.
1
Choose View>Terminal I/O to display the output from the I/O operations.
The contents of the window depends on how far you have executed the application.
Reaching program exit
1
Alternatively, click the Go button on the toolbar.
As no more breakpoints are encountered, C-SPY reaches the end of the application and a Program exit reached message is printed in the Debug Log window.
All output from the application has now been displayed in the Terminal I/O window.
If you want to start again with the existing application, choose Debug>Reset, or click the Reset button on the toolbar.
2
To exit from C-SPY, choose Debug>Stop Debugging. Alternatively, click the Stop Debugging button on the toolbar. The IAR Embedded Workbench IDE main window is displayed.
C-SPY also provides many other debugging facilities. Some of these—for example macros and interrupt simulation—are described in the following tutorials.
For further details about how to use C-SPY, see the C-SPY® Debugging Guide for ARM®.