玩转LLDB之基础篇
什么是LLDB
LLDB是XCode内置的为我们开发者提供的调试工具,它与LLVM编译器一起,存在于主窗口底部的控制台中,能够带给我们更丰富的流程控制和数据检测的调试功能。在调试过程中熟练使用LLDB,可以让你debug事半功倍。
LLDB可以带来以下体验:
- 允许你在程序运行的特定时暂停它
- 查看变量的值
- 执行自定的指令
- 按照你所认为合适的步骤来操作程序的进展
关于LLDB详细内容可以点击这里
LLDB语法
与LLDB进行交互就是在调试区域部分输入相应的命令,每一个LLDB命令都包含着0个或者多个子命令,并且可能具有一个或者多个可选的参数,就像下面一样:
<code>
<command> [<subcommand> [<subcommand>...]] <action> [-options [option-value]] [argument [argument...]]
</code>
语法说明
<command>(命令)和<subcommand>(子命令):LLDB调试命令的名称,命令和子命令按层级结构来排列:一个命令对象为跟随其的子命令对象创建一个上下文,子命令又为其子命令创建一个上下文,依此类推。子命令是组织相关操作的分隔标识。一个命令的最终子命令将是一个动词表面将要执行的动作。
<action>:在上下文中执行命令的操作;
<options>:命令选项,动作的修改者;一个命令当中可能包含一个或者多个命令选项,命令选项使用双虚线(--)开始用于不同的组合来修改执行的动作。有一些命令也使用了单虚线 (-)开始进行操作.
<arguement>:命令的参数, 根据使用的命令的上下文来表示各种不同的东西;一个命令可能要求一个或者多个参数,参数是动作执行的分隔标识。
[]:表示命令是可选的,可以有也可以没有
注意:
LLBD命令行的解析操作在执行命令之前完成。上面的这些元素之间通过空格来分割,如果某一元素自身含有空格,则可以使用双引用。而如果元素中又包含双引号,则可以使用反斜杠;或者元素使用单引号。在LLDB中单引号和双引号是等价的,例如:
(lldb) command [subcommand] -option "some "quoted" string"
can also be written:
(lldb) command [subcommand] -option 'some "quoted" string'
缩写
LLDB命令是可以以许多格式显示的,这里不做太多的介绍,可以通过help command alias得到对应的帮助信息。
帮助
LDB为我们提供了大量的文档,在调试的时候,我们经常可以通过help得到帮助信息
****help <command-name>****
- 直接输入help,可以得到大量的命令信息
- 对于如何使用对应的子命令,都会有相应的提示,根据提示操作即可,例如:我们想了解与断点相关的信息,就可以help breakpoint,我们将可以看到所有与断点相关的信息。
- 同理如果想知道操作断点的具体命令。
常用基本命令介绍
- expr
可以在调试时动态执行指定表达式,并将结果打印出来。常用于在调试过程中修改变量的值。
如图设置断点,然后运行程序。程序中断后输入下面的命令:
screenshot.png-
call
一般只在不需要显示输出,或是方法无返回值时使用call -
bt
打印调用堆栈,加all可打印所有thread的堆栈 -
image
image 命令可用于寻址,有多个组合命令。比较实用的用法是用于寻找栈地址对应的代码位置。
image lookup --address --- -
breakpoint
设置断点
breakpoint set命令用于设置断点
<code>
clear -- Clears a breakpoint or set of breakpoints in the executable.
command -- A set of commands for adding, removing and examining bits of
code to be executed when the breakpoint is hit (breakpoint
'commands').
delete -- Delete the specified breakpoint(s). If no breakpoints are
specified, delete them all.
disable -- Disable the specified breakpoint(s) without removing them. If
none are specified, disable all breakpoints.
enable -- Enable the specified disabled breakpoint(s). If no breakpoints
are specified, enable all of them.
list -- List some or all breakpoints at configurable levels of detail.
modify -- Modify the options on a breakpoint or set of breakpoints in
the executable. If no breakpoint is specified, acts on the
last created breakpoint. With the exception of -e, -d and -i,
passing an empty argument clears the modification.
name -- A set of commands to manage name tags for breakpoints
set -- Sets a breakpoint or set of breakpoints in the executable.
For more help on any particular subcommand, type 'help <command> <subcommand>'.
</code>
- watchpoint
监听地址内容变化