Using The Xcode® Debugger: Objectives
Using The Xcode® Debugger: Objectives
Objectives
In this appendix you’ll:
■ Set breakpoints and run a
program in the debugger.
■ Use the Continue program
execution command to
continue execution.
■ Use the Auto window to view
and modify the values of
variables and watch
expression values.
■ Use the Step Into, Step Out
and Step Over commands to
control execution.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
J-2 Appendix J Using the Xcode® Debugger
J.1 Introduction
In Chapter 2, you learned that there are two types of errors—compilation errors and logic
errors—and you learned how to eliminate compilation errors from your code. Logic errors
(also called bugs) do not prevent a program from compiling successfully, but can cause the
program to produce erroneous results when it runs. Most C++ compiler vendors provide
software called a debugger, which allows you to monitor the execution of your programs
to locate and remove logic errors. The debugger will be one of your most important pro-
gram development tools. This appendix demonstrates key features of the Xcode debugger.
Fig. J.1 | Header file for the Account class. (Part 1 of 2.)
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Using the Xcode® Debugger J-3
Fig. J.1 | Header file for the Account class. (Part 2 of 2.)
and double click the Debugging.xcodeproj to open the project’s workspace window in
Xcode.
Inserting Breakpoints
In the following steps, you’ll use breakpoints and various debugger commands to examine
the value of the variable withdrawalAmount declared in Fig. J.3.
1. Inserting breakpoints. Click figJ_03.cpp in the Project navigator to display the
file in the Xcode Editor area. To insert a breakpoint, click inside the gray bar to
the left of the line of code at which you wish to break. You can set as many break-
points as necessary. Set breakpoints at lines 17 and 21. A blue arrow appears to
the left of the line where you clicked, indicating that a breakpoint has been set
(Fig. J.4). When the program runs, the debugger pauses execution at any line that
contains a breakpoint. The program is said to be in break mode when the debug-
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Using the Xcode® Debugger J-5
Breakpoints
ger pauses the program. Breakpoints can be set before running a program, in
break mode and while a program is running.
2. Starting to debug. After setting breakpoints in the code editor, click the Run ( )
button on the workspace window’s tool bar to build the program and begin the
debugging process. When you debug an application that normally runs in a Ter-
minal window, you can see the program’s output and provide input in the Debug
area below the Editor area in the workspace window (Fig. J.5). The debugger en-
ters break mode when execution reaches the breakpoint at line 17.
3. Examining program execution. Upon entering break mode at the first breakpoint
(line 17), the IDE becomes the active window (Fig. J.5). The green arrow to the
left of line 17 indicates that this line contains the next statement to execute.
4. Using the Continue program execution button to resume execution. To resume ex-
ecution, click the Continue program execution ( ) button in the Debug area’s
toolbar, which resumes program execution until the next breakpoint or until the
program terminates, whichever comes first. The program continues executing
and pauses for input at line 18. Enter 13 as the withdrawal amount. The program
executes until it stops at the next breakpoint (line 21). Notice that when you
place your mouse pointer over the variable name withdrawalAmount, the value
stored in the variable is displayed in a yellow tooltip below the mouse cursor
(Fig. J.6). As you’ll see, this can help you spot logic errors in your programs.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
J-6 Appendix J Using the Xcode® Debugger
Next line of code to execute is highlighted in green You interact with the application in this part of the Debug area
Green arrow shows next Debug area toolbar Tooltip showing variable value
statement to execute
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Using the Xcode® Debugger J-7
6. Removing a breakpoint. Right click the breakpoint and select Delete Breakpoint.
7. Finishing program execution. Select Debug > Continue to execute the program to
completion.
In this section, you learned how to enable the debugger and set breakpoints so that
you can examine the results of code while a program is running. You also learned how to
continue execution after a program suspends execution at a breakpoint and how to remove
breakpoints.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
J-8 Appendix J Using the Xcode® Debugger
Auto Window
5. Evaluating arithmetic and boolean expressions. You can watch the values of spe-
cific variables, arithmetic expressions and boolean expressions using the Auto
window. To do so, right click in the Auto window and select Add Expression….
In the small window that appears, type the variable or expression to watch. For
example, enter the expression (withdrawalAmount + 3) * 5, then press Return.
The expression’s type and value (80 in this case) are displayed to the right of the
expression (Fig. J.9). Repeat this to add the expression withdrawalAmount == 3,
then press Return. Expressions containing the == operator (or any other relational
or equality operator) are treated as bool expressions. The value of the expression
in this case is false (Fig. J.9), because withdrawalAmount currently contains 13,
not 3. The icon indicates that a line in the Auto window represents an expres-
sion that you are watching. When configuring a variable or expression to watch,
if you check the Show in All Stack Frames checkbox, the expression will be dis-
played in the Auto window throughout the debugging process; otherwise, the ex-
pression will be displayed only in the scope where you created the expression. You
can delete a watched expression by right clicking it and selecting Delete Expres-
sion.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Using the Xcode® Debugger J-9
7. Modifying values. Based on the value input by the user (13), the account balance
output by the program should be $37. However, you can use the Auto window to
change the values of variables during the program’s execution. This can be valu-
able for experimenting with different values and for locating logic errors. In the
Auto window, expand the account1 node, select balance then click its value (37)
to make it editable. Type 33, then press Return. The debugger changes the value
of balance (Fig. J.11).
8. Viewing the program result. Click the Continue program execution ( ) button
to resume execution. The program displays the result. Notice that the result is $33
(Fig. J.12). This shows that Step 7 changed the value of balance from the calcu-
lated value (37) to 33.
In this section, you learned how to use the debugger’s Watch and Locals windows to
evaluate arithmetic and boolean expressions. You also learned how to modify the value of
a variable during your program’s execution.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
J-10 Appendix J Using the Xcode® Debugger
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Using the Xcode® Debugger J-11
debugger then pauses execution at line 24, the next executable line in the current
function, main.
9. Stopping the debugger. Click the Stop button on the Xcode toolbar.
In this section, you learned how to use the debugger’s Step Into command to debug
functions called during your program’s execution. You saw how the Step Over command
can be used to step over a function call. You used the Step Out command to continue exe-
cution until the end of the current function.
J.5 Wrap-Up
In this appendix, you learned how to insert, disable and remove breakpoints in the Xcode
debugger. Breakpoints allow you to pause program execution so you can examine variable
values. This capability will help you locate and fix logic errors in your programs. You saw
how to use the Auto window to examine the value of an expression and how to change the
value of a variable. You also learned debugger commands Step Into, Step Over, Step Out
and Continue program execution that can be used to determine whether a function is exe-
cuting correctly.
© 2014 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved