Debugging Applications

Starting the Debugger

After you build your application, choose Run. Select the C/C++ Local Application label in the Configurations pane. Then, click the New icon positioned towards the upper left of the window.

Figure 5.5. Creating a Launch Configuration

Creating a Launch Configuration
Click the New icon to create to create a new debug configuration.

When you create the launch configuration, a new window appears. On the Main tab, use the Browse... button to select your project, if it is not already selected. Then, use the Search Project.. button to select your application.

Figure 5.6. Selecting a Program

Selecting a Program
Use the Search Project... button to locate your program.

Choosing a Debugging Mode

Before you can use the Sourcery G++ IDE to debug your application, you must decide which debugging mode to use. Sourcery G++ supports several debugging modes, as described below.

Selecting a Debugger

Once you have decided which debugger to use, switch to the Debugger tab and select the appropriate Sourcery G++ option.

Figure 5.7. Selecting a Debugger

Selecting a Debugger
Pick the debugger that you want to use.

Once you have made any necessary adjustments, click the Debug button to start the debugger.

You do not need to repeat the debugger selection process the next time you launch the debugger. Instead, you can select RunDebug Last Launched to start the debugger using the settings you have selected.

Controlling Execution

When you start the debugger, the IDE switches from the C/C++ perspective to the debug perspective. Instead of showing panes that help you to develop your application, the IDE now shows panes that help you to debug your application.

Figure 5.8. Debug Perspective

Debug Perspective
The debug perspective displays the stack, local variables, and the current location.

The debugger automatically stops on the first line of main. The currently active source line is highlighted. The pane at the upper left shows the application threads and the stack associated with each thread. The pane at the upper right shows the values of local variables. (At this point, i and n have not yet been initialized, so their values are indeterminate.)

Use RunStep Over (F6) to advance by a single line. Because the program has changed the value of i, the IDE highlights the value in the variable pane.

By looking at the code, you can see that the program calls factorial and then calls printf to print out the resulting value. You can set a breakpoint right before the call to printf by clicking anywhere on that line, and then using Run (Ctrl-Shift-B).

Figure 5.9. Setting a Breakpoint

Setting a Breakpoint
Set a breakpoint by highlighting the line where you want to stop and then using the Run menu.

After setting the breakpoint, use RunStep Into (F5) to step into the body of factorial.

The IDE no longer displays the value of i because there is no local variable i within factorial. If you wish to see the value of i (from main), select the stack frame for main in the pane at the upper left. The IDE displays the variables for whichever frame is presently selected.

Now, proceed to the breakpoint by using RunResume (F8). The variable n now has the value 1 because the factorial of zero is one. Step over the call to printf to print the value in the console.

Low-Level Debugging

You may sometimes need to debug at the machine level, rather than at the source code level. For example, if you are working with an assembly code device driver, you may wish to see the values stored in machine registers and step through the code instruction by instruction.

To view machine registers, click on the Registers tab, and expand the Main register group. To see the instructions being executed, use WindowShow ViewDisassembly.

Figure 5.10. Low-Level Debugging

Low-Level Debugging
The Sourcery G++ IDE can display machine registers and assembly code.

When the disassembly window is active, the Step Over and Step Into commands operate at the assembly level, rather than at the source code level. So, a Step Over command advances by a single machine instruction. When the values of registers change, the registers are highlighted in the IDE. You can set breakpoints on particular machine instructions in the same way that you can set breakpoints on source code.

Troubleshooting

When your application is large, or the debugging device is relatively slow, you may encounter timeout errors when starting debugging. In that case, you should increase the timeout settings. Select the Preferences item in the Window menu, and in the dialog that appears select C/C++, Debug, GDB MI. Increase the values in the Debugger Timeout and the Launch Timeout fields until your application starts without errors.