注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

Debugging Objective-C objects in GDB using po[quote]  

2012-02-20 20:49:44|  分类: 共享 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

You may be familiar with using GDB in Xcode to debug your apps. GDB is an interactive console that you can enter when you reach a breakpoint in your code.

If you’ve only used GDB before with C or C++ code, you might not be aware that Xcode adds some additional functionality into GDB that help makes it more useful for Mac and iOS development.

One of the most useful commands in GDB (which unfortunately I didn’t know about for a long time) is the “print-object”, or “po”, command.

Plain old “print” ( or “p”) will print the contents of a variable. If this is a pointer, you’ll often only see something like:

[code]
(gdb) p @"test"
$1 = (NSString *) 0x4b1cd20
[/code]

which isn’t very helpful.

However if you use “po” on an Objective-C object, you can get much more helpful output:

[code]
(gdb) po @"test"
test
[/code]

The “po” command prints out the object’s description to the console (more specifically, what the object returns when you call “description” on it). So for an NSString this is the string’s contents, or for NSNumber the actual number. It’s especially useful for things like NSArray or NSDictionary. Say we have an array defined as:

[code]
NSDictionary *myArray = [NSArray arrayWithObjects: @"value1", @"value2", nil];
[/code]

If we use the “p” command on this object, we get:

[code]
(gdb) p myArray
$1 = (NSArray *) 0x4e34050
[/code]

Not very useful. The “po” command, on the other hand, gives us:

[code]
(gdb) po myArray
<__NSArrayI 0x4e34050>(
value1,
value2
)
[/code]

which is much better.

In addition, “po” allows you to look at objects whose type isn’t clear. For example, getting an object from an array or dictionary.

Say I try to look at a particular object stored in the array above. If I try this with “p”, I don’t have much luck:

[code]
(gdb) p [myArray objectAtIndex:1]
Unable to call function "objc_msgSend" at 0xf08a4c: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')
[/code]

The debugger is getting back an object whose type is just “id”, in other words a pointer to some kind of object. It tells us the contents of the pointer itself (0xf08a4c) but doesn’t know what to do with this.

If we use “po”, we see the following:

[code]
(gdb) po [myArray objectAtIndex:1]
value2
[/code]

which is exactly what we want. The debugger is calling description on the result, and really doesn’t care what type the object is.

One thing to keep in mind is that “po” is only designed for Objective-C objects. If you try to use it on something like an int, you’ll see the following:

[code]
(gdb) po 1
0x1 does not appear to point to a valid object.
[/code]

What’s happening here is that the debugger is treating this integer like a pointer, and trying to look up an object at 0×1. In this case regular p should be used:

[code]
(gdb) p 1
$2 = 1
[/code]

  评论这张
 
阅读(919)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017