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

本文介绍如何使用Xcode内置的GDB调试系统,包括通过命令行接口查看变量和对象数据的基本操作,以及如何利用断点进行调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 titleand 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.

转载于:https://www.cnblogs.com/rothwell/archive/2012/05/24/2517008.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值