How do I use the debugger?

Click here to access the Apple documentation for the Xcode debugger. Running your project using Command+R starts the simulator or runs the app on your iOS device. The build configuration must be set to Debug. Use the Command+< keys to bring up the options screen, click on the Run target, then select Info. The dropdown for Build Configuration should be set to Debug. Click next to the line of code to set a breakpoint. You will see a blue arrow. After running the program, when the breakpoint is reached the debugger console will be shown. (Use Shift+Command+C to display it or Shift+Command+Y to hide it). The Debug Navigator will displayed showing the call stack. The Debugger console will show the variables on the left and a command prompt on the right.

The most handy thing to do in the debugger is inspecting variables. This is easily accomplished by using the “expr” command. For instance, to see the value of variable i, just enter “expr i”. You can also change variables, for instance “expr i++” will increment i. Most variables are actually pointers in Objective-C, therefore you must use * to see the contents of the object.

For example:

(lldb) expr *c
(CardClass) $4 = {
    NSObject = {
        isa = CardClass
    }
    _suit = Diamonds
    _faceUpDown = FACE_DOWN
    _value = 1
}

 

You can also follow a bunch of pointers using “expr -P 10 — c” for instance.

Often times, logging messages as the app runs is the best way to debug. Use the following code to print functions as they are executed works well, and it is disabled when the build scheme is changed to “Released”.

-(id)yourmethod {
    #ifdef DEBUG
    NSLog(@"%s", __func__);
    #endif
}

The official documentation for the debugger is at lldb.llvm.org.

One of the most useful breakpoints to set is “Debug/Breakpoints/Create Exception Breakpoint…”, this will help you find problems like trying to put “nil” into an NSArray or sending the wrong message to a class.

Assert is your friend, use this function call to force an exception so you can examine the call stack if something goes awry. It’s best to avoid using asserts in production code, therefore this construct is best:

#ifdef DEBUG
    NSLog(@"%s", __func__);
    assert(self.gameState == NotStarted);
#endif

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s