现状
大多数做了好几年的iOS开发者(比如本人),用的最多的调试指令,大概是po
(print object
)吧,如下图。对其他命令知之甚少,当然这也无伤大雅,只是效率上略低一些罢了~
(lldb) po tip
"hello"
(lldb) po 1+1
2
(lldb) po tip + " world!"
"hello world!"
为了提高工作效率,我们需要了解LLDB,至少需要多了解几个命令
1. help
通向调试大门的钥匙。当我们不记得任何命令的时候,只要知道help
,就能唤起当初的记忆。
(lldb) help
Debugger commands:
……
p -- Evaluate an expression on the current thread. Displays any
returned value with LLDB's default formatting.
po -- Evaluate an expression on the current thread. Displays any
returned value with formatting controlled by the type's author.
print -- Evaluate an expression on the current thread. Displays any
returned value with LLDB's default formatting.
q -- Quit the LLDB debugger.
……
2. po(print object)
打印变量的值
(lldb) po tip
"hello"
(lldb) po ageList
▿ 3 elements
- 0 : 20
- 1 : 30
3. p(print)
打印变量的值,提供一个变量名,可以看到数据类型
修改变量的值
(lldb) p tip
(String) $R22 = "hello"
(lldb) p ageList
([Int]) $R4 = 3 values {
[0] = 20
[1] = 30
}
(lldb) p $R4
([Int]) $R6 = 3 values {
[0] = 10
[1] = 20
[2] = 30
}
# 修改变量的值
(lldb) p tip
(String) $R0 = "hello"
(lldb) p tip.uppercased()
(String) $R2 = "HELLO"
(lldb) p tip
(String) $R4 = "hello"
(lldb) p tip = tip + " world!"
(lldb) p tip
(String) $R8 = "hello world!"
4. e(expression)
打印变量
修改变量
创建变量(必须以$开头,其他语法和普通变量一样)
# 打印、修改变量
(lldb) e tip
(String) $R10 = "hello world!"
(lldb) e tip = tip.uppercased()
(lldb) e tip
(String) $R14 = "HELLO WORLD!"
(lldb) e let $age = 18
(lldb) e $age
(Int) $R16 = 18
(lldb) e $age + busNo
(Int) $R18 = 20
5. fr(frame)
打印当前断点在上下文中的位置
(lldb) fr info
frame #0: 0x000000010b1f056b LLDBDemo`ViewController.main(self=0x00007fbe84f05bd0) at ViewController.swift:28:24
6. br(breakpoint)
-
br list(li == list)
br li Current breakpoints: 1: file = '/Users/yong.chen/Documents/Workspace/LLDBDemo/LLDBDemo/ViewController.swift', line = 29, exact_match = 0, locations = 1, resolved = 1, hit count = 0 Condition: busNo != 2 1.1: where = LLDBDemo`LLDBDemo.ViewController.main() -> () + 152 at ViewController.swift:29:15, address = 0x000000010b1f05b8, resolved, hit count = 0 2: file = '/Users/yong.chen/Documents/Workspace/LLDBDemo/LLDBDemo/ViewController.swift', line = 28, exact_match = 0, locations = 1, resolved = 1, hit count = 1 2.1: where = LLDBDemo`LLDBDemo.ViewController.main() -> () + 75 at ViewController.swift:28:24, address = 0x000000010b1f056b, resolved, hit count = 1 3: name = 'help', locations = 1, resolved = 1, hit count = 0 3.1: where = UIKitCore`+[UIFocusDebugger help], address = 0x000000011104d9d7, resolved, hit count = 0
-
br del breakpointID(del == delete)
如果没有传breakpointID,就是删除所有断点,此时会有提示(lldb) br del 4 error: '4' is not a currently valid breakpoint ID. (lldb) br del 3 1 breakpoints deleted; 0 breakpoint locations disabled. (lldb) br del About to delete all breakpoints, do you want to do that?: [Y/n] * Y: All breakpoints removed. (7 breakpoints) * n: Operation cancelled...
-
br dis breakpointID(dis == disable)
(lldb) br dis 1 1 breakpoints disabled.
-
br en breakpointID(en == enable)
(lldb) br en 1 1 breakpoints enabled.
-
br set
-
br modify
-
br ……
6.1. 条件断点
只有当Condition为true的时候,才会进断点
7. th(thread)
当程序进入断点的时候,会出现以下视图:
th continue
: 同th c
,会取消程序的暂停,允许程序正常执行 (要么一直执行下去,要么到达下一个断点)。th step-over
: 同next
或n
,如果所在这行代码是一个函数调用,那么就不会跳进这个函数,而是会执行这个函数,然后继续。th step-into
: 同step
或s
,跳进一个函数调用来调试或者检查程序的执行情况。th step-out
: 继续执行到下一个返回语句 (跳出当前函数调用) ,然后再次停止。th backtrace
: 查看当前断点的调用堆栈。
8. q(quit)
如果你不想调试,就q
一下吧
示例代码
func main() {
let roadNo = 8, busNo = 2
let tip = "hello"
let ageList = [20, 30]
print("\(tip), I'm on the No.\(busNo) bus, No.\(roadNo)")
print("\(ageList)")
}
参考文献
https://swifting.io/blog/2016/02/19/6-basic-lldb-tips/