现在的位置: 首页 > 综合 > 正文

[Cocoa]XCode的一些调试技巧

2013年09月06日 ⁄ 综合 ⁄ 共 2471字 ⁄ 字号 评论关闭

XCode的一些调试技巧

罗朝辉 (http://blog.csdn.net/kesalin/)

CC 许可,转载请注明出处

XCode 内置GDB,我们可以在命令行中使用 GDB 命令来调试我们的程序。下面将介绍一些常用的命令以及调试技巧。

po 命令:为 print object 的缩写,显示对象的文本描述(显示从对象的 description 消息获得的字符串信息)。

比如:

上图中,我使用 po 命令显示一个 NSDictionary 的内容。注意在左侧我们可以看到 dict 的一些信息:3 key/value pairs,显示该 dict 包含的数据量,而展开的信息显示 isa 层次体系(即class 和 metaclass结构关系)。我们可以右击左侧的
dict,选中“Print Description of "dict"”,则可以在控制台输出 dict 的详细信息:

  1. Printing description of dict:  
  2. <CFBasicHash 0x1001149e0 [0x7fff7e27ff40]>{type = immutable dict, count = 3,  
  3. entries =>  
  4.     0 : <CFString 0x100002458 [0x7fff7e27ff40]>{contents = "first"} = <CFString 0x100002438 [0x7fff7e27ff40]>{contents = "one"}  
  5.     1 : <CFString 0x100002498 [0x7fff7e27ff40]>{contents = "second"} = <CFString 0x100002478 [0x7fff7e27ff40]>{contents = "two"}  
  6.     2 : <CFString 0x1000024d8 [0x7fff7e27ff40]>{contents = "third"} = <CFString 0x1000024b8 [0x7fff7e27ff40]>{contents = "three"}  
  7. }  
  8. (gdb)   


print 命令:有点类似于格式化输出,可以输出对象的不同信息:

如:

  1. (gdb) print (char *)[[dict description] cStringUsingEncoding:4]  
  2. $1 = 0x1001159c0 "{\n    first = one;\n    second = two;\n    third = three;\n}"  
  3. (gdb) print (int)[dict retainCount]  
  4. $2 = 1  
  5. (gdb)   

注:4是 NSUTF8StringEncoding 的值。

info 命令:我们可以查看内存地址所在信息

比如 "info symbol 内存地址" 可以获取内存地址所在的 symbol 相关信息:

  1. (gdb) info symbol 0x00000001000017f7  
  2. main + 343 in section LC_SEGMENT.__TEXT.__text of /Users/LuoZhaohui/Library/Developer/Xcode/DerivedData/RunTimeSystem-anzdlhiwvlbizpfureuvenvmatnp/Build/Products/Debug/RunTimeSystem  

比如 "info line *内存地址"
可以获取内存地址所在的代码行相关信息:

  1. (gdb) info line *0x00000001000017f7  
  2. Line 62 of "/Users/LuoZhaohui/Documents/Study/RunTimeSystem/RunTimeSystem/main.m" starts at address 0x1000017f7 <main+343> and ends at 0x10000180a <main+362>.  

show 命令:显示 GDB 相关的信息。如:show version 显示GDB版本信息

  1. (gdb) show version  
  2. GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)  
  3. Copyright 2004 Free Software Foundation, Inc.  
  4. GDB is free software, covered by the GNU General Public License, and you are  
  5. welcome to change it and/or distribute copies of it under certain conditions.  
  6. Type "show copying" to see the conditions.  
  7. There is absolutely no warranty for GDB.  Type "show warranty" for details.  
  8. This GDB was configured as "x86_64-apple-darwin".  


help 命令:如果忘记某条命令的语法了,可以使用 help 命令名 来获取帮助信息。如:help info 显示 info 命令的用法。

  1. (gdb) help info  
  2. Generic command for showing things about the program being debugged.  

抱歉!评论已关闭.