Chapter 2. Tools for Developing Cocoa Applications
2.4 Debugging Programs with gdb
2.4.1 Using gdb in Project Builder, Step by Step
The easiest way to use the gdb debugger is in PB. We'll show you how to get started with that in this section.
First, we must have an application to work with, so we'll use a copy of the CircleView example application that is bundled with the Mac OS X developer system.
When you debug a running program within PB, you can access some of the most useful gdb commands
graphically. You can set breakpoints by clicking the mouse next to a line of code (a breakpoint is a place where a running program stops executing and control is returned to the debugger). When a breakpoint is reached in a running program, the stack frame and the variables on the stack will be displayed in the debugger window. You can also use the buttons on the upper-right side of the PB window to control execution. The up and down arrows will step you up and down the call stack. The arrow over the parenthesis will execute a gdb step command. The pause button will pause execution, and the button with the triangle will continue program execution.
Let's try a few of these commands on a real example:
1. Open the Go to Folder sheet in the Finder and enter "/Developer/Examples/AppKit".
2. Copy the CircleView folder into your Home folder by Option-dragging the folder and dropping it on your Home icon. (If CircleView is not available, choose another example application in the same folder.) 3. Click your Home icon and then click the CircleView folder. You'll see the files in the folder in
column view, as shown in Figure 2-13.
Figure 2-13. CircleView folder with files for the CircleView project
4. Open the CircleView project in PB by double-clicking on the CircleView.pbproj project file in the CircleView folder.
5. Click the build and run button above the Groups & Files pane in PB.
After a few seconds, the application will be built (compiled) and will run. The build and run button will turn into a stop button. The running CircleView application is shown in Figure 2-14.
Figure 2-14. The CircleView application running
6. You can play with this fun little application by dragging the sliders, changing the text (try entering "Mac OS X"), changing the color, and clicking the nondescript button below the color well to animate the text.
7. Quit the CircleView application by clicking the stop button in PB's toolbar (or quit CircleView directly).
8. Back in PB, click the disclosure triangle next to Classes in the Groups & Files pane to see the CircleView.h and CircleView.m class files.
9. Single-click the CircleView.m file to open it in the lower-right pane in PB's main window, as shown in Figure 2-15.
Figure 2-15. The CircleView project in PB
10. Press the up-down ("stepper") arrows to the right of "<No selected symbol>" in the middle of the PB window, drag to "-setColor:", and release the mouse button. This action takes you to the "-setColor:"
method (like a function) in the CircleView.m file.
11. Set a breakpoint by clicking the mouse in the white column to the left of "-(void)setColor:". An arrow should appear, as in Figure 2-16.
Figure 2-16. Stopped at a CircleView breakpoint in PB
In a moment we'll run the CircleView program in debug mode within PB. PB will run gdb in the background
and provide us with a nice graphical interface to many of its commands. (If you want to see that gdb is running in the background, look at your running processes in ProcessViewer). We'll test some of the graphical commands available in PB, but first we'll clean out (remove) all of the derived build files in the project to ensure that we start from scratch:
12. Clean the CircleView build files by clicking the clean active target (whiskbroom) button in PB's toolbar.
Click the clean active target (blue) button in the resulting sheet.
13. Now click the build and debug button in PB's toolbar. The button displays a hammer and a bug spray can icon that turns into a stop button when the application runs.
The CircleView application should now be running in the foreground. Because we set the breakpoint at the setColor: method, we won't reach it until we try to change the color.
14. Click the border of the color well in the CircleView window.
You might expect the Font window to appear, but it doesn't because of the breakpoint. Your PB window should now look like the one in Figure 2-16. You can see the "Step into method or function call" button at the right of PB's toolbar. This action executes one step (line of code). You can use it repeatedly to step through your program and debug it line by line.
15. Click the "Step over method or function call" button. This action steps out of the current method.
16. Click the "Continue execution" button. This action continues execution of the CircleView program.
17. Now reactivate CircleView, and the Font window will appear.
18. Quit CircleView.
PB made it easy for us to use gdb to debug CircleView. Next, we'll show you the "hard" way, in a Terminal window.