XCode的一些调试技巧
来源:互联网 发布:雀巢中国总部 知乎 编辑:程序博客网 时间:2024/06/16 23:21
XCode 内置GDB,我们可以在命令行中使用 GDB命令来调试我们的程序。下面将介绍一些常用的命令以及调试技巧。
po 命令:为print object 的缩写,显示对象的文本描述(显示从对象的description 消息获得的字符串信息)。
比如:
上图中,我使用 po 命令显示一个NSDictionary 的内容。注意在左侧我们可以看到 dict 的一些信息:3 key/value pairs,显示该dict 包含的数据量,而展开的信息显示 isa层次体系(即class和 metaclass结构关系)。我们可以右击左侧的dict,选中“Print Description of "dict"”,则可以在控制台输出dict 的详细信息:
Printing description of dict:
<CFBasicHash 0x1001149e0 [0x7fff7e27ff40]>{type = immutable dict, count = 3,
entries =>
0 : <CFString 0x100002458 [0x7fff7e27ff40]>{contents = "first"} = <CFString 0x100002438 [0x7fff7e27ff40]>{contents = "one"}
1 : <CFString 0x100002498 [0x7fff7e27ff40]>{contents = "second"} = <CFString 0x100002478 [0x7fff7e27ff40]>{contents = "two"}
2 : <CFString 0x1000024d8 [0x7fff7e27ff40]>{contents = "third"} = <CFString 0x1000024b8 [0x7fff7e27ff40]>{contents = "three"}
}
(gdb)
print 命令:有点类似于格式化输出,可以输出对象的不同信息:
如:
(gdb) print (char *)[[dict description] cStringUsingEncoding:4]
$1 = 0x100115620 "{\n first = one;\n second = two;\n third = three;\n}"
(gdb)
注:4是NSUTF8StringEncoding 的值。
info 命令:我们可以查看内存地址所在信息
比如 "info symbol 内存地址" 可以获取内存地址所在的symbol 相关信息:
(gdb) info symbol 0x00000001000017f7 main + 343 in section LC_SEGMENT.__TEXT.__text of /Users/LuoZhaohui/Library/Developer/Xcode/DerivedData/RunTimeSystem-anzdlhiwvlbizpfureuvenvmatnp/Build/Products/Debug/RunTimeSystem
比如 "info line *内存地址"可以获取内存地址所在的代码行相关信息:
(gdb) info line *0x00000001000017f7 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版本信息
(gdb) show version
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".
(gdb)
help 命令:如果忘记某条命令的语法了,可以使用 help 命令名 来获取帮助信息。如:help info显示 info 命令的用法。
(gdb) help info
Generic command for showing things about the program being debugged.
List of info subcommands:
info address -- Describe where symbol SYM is stored
info all-registers -- List of all registers and their contents
info args -- Argument variables of current stack frame
info auxv -- Display the inferior's auxiliary vector
info breakpoints -- Status of user-settable breakpoints
info catch -- Exceptions that can be caught in the current stack frame
info checkpoints -- Help
info classes -- All Objective-C classes
info vector -- Print the status of the vector unit
info warranty -- Various kinds of warranty you do not have
info watchpoints -- Synonym for ``info breakpoints''
Type "help info" followed by info subcommand name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)
在系统抛出异常处设置断点
有时候我们的程序不知道跑到哪个地方就 crash 了,而 crash 又很难重现。保守的做法是在系统抛出异常之前设置断点,具体来说是在 objc_exception_throw处设置断点。设置步骤为:首先在XCode 按 CMD + 6,进入断点管理窗口;然后点击右下方的+,增加新的 Symbolic Breakpoint,在Symbol 一栏输入:objc_exception_throw,然后点击done,完成。 这样在Debug 模式下,如果程序即将抛出异常,就能在抛出异常处中断了。比如在前面的代码中,我让 [firstObjctcrashTest];抛出异常。在 objc_exception_throw 处设置断点之后,程序就能在该代码处中断了,我们从而知道代码在什么地方出问题了。
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- xcode的一些调试技巧
- XCode的一些调试技巧
- XCode的一些调试技巧
- 调试一个MFC的点云可视化(PCL)程序
- [web安全] web安全小知识点汇总
- oracle解除表锁定
- SecureCRT乱码解决
- 数据存取之Preference浅析
- XCode的一些调试技巧
- Android设置TextView显示一行或多行
- Django开发博客(四)——建立文章列表
- android sqlite 应用
- 学习之随堂笔记——屏幕适配
- MyBits学习小知识及错误二
- Oracle11g 账号锁定及密码有效期问题
- 开源控件学习之-侧滑listview进行删除(一)(自定义listview)
- 数据库学习