2012年1月11日星期三

Debugging with GDB: Introduction to Commands, Print and Print-Object

Debugging with GDB: Introduction to Commands, Print and Print-Object:

GDB is the debugging system built into Xcode. Xcode handles much of the interaction with GDB to provide support for breakpoints, stepping through/over code, wowever, GBD also provides a command line that you can use to work directly with the debugger. This tutorial walks through the basics of the command line interface along with an introduction to a handful of commands for viewing variable and object data.


The first thing to understand is that commands can only be entered into GDB when the debugging process is stopped, which is done via breakpoints. If you hit a breakpoint, the debug console will look as follows:



Notice the (gdb) prompt, politely waiting your input. No better place to start than requesting help information. While in the debug console (and on a breakpoint), from the GDB prompt, type in “help”



Once the high-level help categories are shown, you can look further by viewing one of the classes of commands. For example, below is a list of all the commands for examing data:



GDB Print Command

From the help info shown above, let’s look at the print command:



The expression (EXP) to be printed can be as simple as showing the current value of a variable – for example, using the code below, here’s how to print the value of the variable ‘counter’



#define MAX_CLICK  5

- (void)someMethod:(int)counter title:(NSString *)title message:(NSString *)msg
{
if (counter++ > MAX_CLICK)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}
}


Each time the method above stops on breakpoint at the if statement, the value of counter can be viewed:



Notice the shortcut for print (p) command is used above in the second command.


You can also use expressions when working with variables. Understand the examples that follow are a bit contrived, however, for the sake of illustration please bear with me. Notice that depending on how use define an expression, the value of the expression will vary:



GDB Print-Object Command

Looking at the help information for the data commands, notice there is also a print-object command, which will call the description method of an Objective-C object. Let’s assume we call someMethod (which is listed above), using the following call:



[self someMethod:x++ title:@"Error Message" message:@"Max Counter Value Reached."];


If we were to set a breakpoint at the line if (counter++ > MAX_CLICK), we could view the objects title and msg:



Notice the shortcut for print object (po) is used in the second example above.


Print-object is very helpful when looking at objects that are collections, for example NSDictionary and NSArray objects. We can view the following dictionary object using the print-object command:



NSArray *arrayOfDictionaryObjects = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"1234", @"accountID", [NSNumber numberWithBool:YES],  @"isActive", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"2345", @"accountID", [NSNumber numberWithBool:NO],  @"isActive", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"3456", @"accountID", [NSNumber numberWithBool:YES], @"isActive", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"4567", @"accountID", [NSNumber numberWithBool:NO],  @"isActive", nil], nil];



At this point, even with this cursory introduction to GDB, hopefully you have a basic idea of the power of GDB. In future posts I dive further into GDB and show you how to make the most of debugging from the command interface.


没有评论:

发表评论