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

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

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

调试是在编程过程中解决错误和问题的重要步骤。GNU调试器(GDB)是一种功能强大的调试工具,它允许我们在源代码级别上进行调试。 首先,为了使用GDB进行源代码级别调试,我们需要安装GDB。我们可以通过访问GNU调试器项目的官方网站,从那里下载并安装GDB软件包。 一旦安装完成,我们可以在终端中使用“gdb”命令来启动GDB调试器。然后,通过在命令行中指定可执行文件的路径,我们可以将程序加载到GDB中。 在源代码级别调试中,我们可以使用GDB的一些命令来执行不同的操作。例如,我们可以使用“break”命令设置断点,以便在程序执行时停止。我们还可以使用“run”命令开始执行程序,直到遇到断点为止。 一旦程序停止在断点处,我们可以使用GDB的其他命令来查看和修改程序状态。例如,我们可以使用“print”命令显示变量的值,以帮助我们理解程序的行为。我们还可以使用“step”命令逐行执行程序,并在每一步检查变量和代码的状态。 除了这些基本命令外,GDB还提供了许多其他功能,如条件断点、查看内存内容、查看函数调用栈等等。这些功能可以帮助我们更全面地理解程序的执行过程和错误的来源。 总之,GDB是一个强大的源代码级别调试工具,可以帮助我们解决编程过程中的错误和问题。通过使用GDB,我们可以在程序执行过程中查看和修改变量和代码的状态,以便更好地理解程序的行为和调试错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值