Debugger and Instruments (iOS 4)

Now that we’ve covered some of the fundamental features in Xcode, let’s talk about an important part of writing an application: debugging. Although you may have some reservations about the debugging process as a developer, it’s critical to discover and remove bugs in the product development lifecycle.

Xcode provides a handy tool for debugging: Debugger. The iOS SDK package provides another important application: Instruments. These tools will provide you with a better debugging experience.

In this section, we’ll cover the basics of Debugger in Xcode, such as monitoring the value of an object under the console window and setting up a breakpoint. Then we’ll explore the functions under Instruments.

Fix-it function

While you’re typing the demo application in Xcode, you’ll notice the Fix-it function, as shown in figure 4.7. Under the hood, when your target is set to use the LLVM compiler, Fix-it scans your source code as you type. It’s handy for correcting mistakes. When there’s an error, you can see the red highlight, and if you click the icon in the gutter, you may discover the solution to your bug.

Another way to discover bugs early is by using static analysis under Xcode.

Fix-it solutions under Xcode


Figure 4.7 Fix-it solutions under Xcode

Analyze

Use static analysis to examine the semantics of your code to capture bugs early. Xcode lets you perform the analysis, examine the results, and edit your source files all from within the workspace window.

In Xcode, navigate to Product > Analyze (or click the Run button on the Xcode toolbar to activate the drop-down menu and then select Analyze). By static-analyzing code, you may discover a potential leak of an object or mismatching arguments, as shown in figure 4.8.

When you need to trace the variables inside the project, it’s time to use the Debugger.

Debugger essentials

Under the Xcode menu, navigate to View > Show Debug Area (or click the debug view on the Xcode toolbar). When you select to show the debug area, Xcode will automatically launch the Debugger toolbar on the top, the variable window at lower left, and the console window at lower right, as shown in figure 4.9.

Static analysis under Xcode reports a potential leak for an object.

Figure 4.8 Static analysis under Xcode reports a potential leak for an object.

The Xcode Debugger is a graphical interface for GDB, the underlying debugger used by Xcode. Now let’s add a breakpoint to the code. You can add a breakpoint at any line in your code by single-clicking at the line number in the gutter. When that line of code is about to execute during runtime, Xcode will pause at the breakpoint so you can trace local variables, function output, and so on. Once a breakpoint is added, you’ll see a blue breakpoint marker at the line number. When the Debugger is running with breakpoints on, you can trace the program’s variables in the Debugger window, as shown in figure 4.10.

Debugger console window

Figure 4.9 Debugger console window

The Debugger window with breakpoints on pause

Figure 4.10 The Debugger window with breakpoints on pause

When the code is paused at the breakpoint, you can use the toolbar to step into the code line by line. If you select a thread or a stack within a thread in the debug bar, Xcode will display the corresponding source file or assembly code in the main editor. Notice that all of the variables will show up in the variable window on the bottom left.

Although you can read out the memory address for every variable, it would be nice to read out the content from the NSString on the fly. You can print out the object by typing po under the GDB console window. For example, try printing out the web view’s description at the breakpoint, as shown in figure 4.11.

How to print out an object's details in the Debugger console window

Figure 4.11 How to print out an object’s details in the Debugger console window

You can also manage all the breakpoints under the Breakpoints Navigator, as shown in figure 4.12.

In the next section, we’ll cover some basics under Instruments.

Breakpoints Navigator window

Figure 4.12 Breakpoints Navigator window

Running Instruments from Xcode

In the previous section, you learned how to use the Debugger under Xcode; but certain memory-allocation bugs are hard to discover with the Debugger, and that’s when Instruments is useful. Under Xcode, navigate to Product > Profile (or click the Run button to bring up the drop-down menu, and then select Profile). Selecting Profile will launch Instruments with the application running on the iOS Simulator, as shown in figure 4.13.

Launching instruments from Xcode

Figure 4.13 Launching instruments from Xcode

Use Allocations under Instruments to analyze the memory allocations during application runtime.

Figure 4.14 Use Allocations under Instruments to analyze the memory allocations during application runtime.

Select Allocations under the iOS Simulator. With this runtime memory-analysis tool, you can monitor and improve memory allocations (see figure 4.14).

Leaks is another trace tool under Instruments, which comes in handy for memory-related bugs. Select Leaks when Instruments launches under the template prompt window. Leaks runs with a sample of every 10 seconds by default; you can manually check for leaks by clicking the Check for Leaks Now button on the bottom-left control panel (see figure 4.15).

Once you find a leaked object, double-click that object. Instruments will show you the function name related to the memory leak.

In this section, we covered some essential debugging tools for your application development. Try to play with each tool in order to discover your preferred debugging procedure.

Instruments with Leaks to trace leaked blocks

Figure 4.15 Instruments with Leaks to trace leaked blocks

Summary

In the previous topic, we showed you how to create some simple programs using Xcode. You also have access to Interface Builder, a powerful graphic design program that allows you to lay out objects by dragging and dropping and then linking those objects back to Xcode for use there.

The example you created in this topic, which focused on creating a new class in Xcode, provided a demo of how to mock up the iPhone Safari browser with a subclass of UIView. You may not use the Debugger for your application right away, but it will come in handy when the time comes for debugging.

Although you now have the fundamental development tools of the iOS SDK well in hand, we’ve neglected two of the SDK building blocks you’ll use to create projects: view controllers and events. In the next three topics, we’ll cover those topics, and in the process, we’ll complete our look at the iOS classes you’ll use in almost any iOS program you write.

Next post:

Previous post: