lldb指令的格式:
<command> [<subcommand> [<subcommand>...]] <action> [-options [option?value]] [argument [argument...]]
- <command>: 命令
- [<subcommand> [<subcommand>...]]:子命令
- <action>:命令操作
- [-options [option?value]] :命令選項(xiàng)
- [argument [argument...]]:命令參數(shù)
- []:表示命令是可選的峭拘,可以有也可以沒(méi)有
例如:給test函數(shù)設(shè)置斷點(diǎn)
(lldb) breakpoint set -n test
- breakpoint:命令
- set:子命令
- -n:命令選項(xiàng)
- test:命令參數(shù)
常用指令
一恋沃、help
查看指令的用法,比如help breakpoin
栏渺、help breakpoint set
二焰檩、Enter
敲Enter,會(huì)自動(dòng)執(zhí)行上次的指令
三哺呜、執(zhí)行表達(dá)式
expression
執(zhí)行一個(gè)表達(dá)式沾鳄,并將表達(dá)式返回的結(jié)果輸出,expression的完整語(yǔ)法是這樣的:expression <cmd-options> -- <expr>
- <cmd-options>:命令選項(xiàng)
- --:命令結(jié)束符懂版,表示所有的命令選項(xiàng)已經(jīng)設(shè)置完畢霹粥,如果沒(méi)有命令選項(xiàng),--可以省略
- <expr>:需要執(zhí)行的表達(dá)式
假如我們?cè)谶\(yùn)行過(guò)程中套媚,想把view的顏色改成紅色看看效果稽寒,我們不必寫(xiě)下代碼扮碧,重新run,只需斷點(diǎn)調(diào)試杏糙,用expression改變顏色慎王,再刷新一下界面,就能看到效果
(lldb) expression self.view.layer.backgroundColor = UIColor.redColor.CGColor
(CGColorRef) $0 = 0x0000600000b63540
(lldb) expression (void)[CATransaction flush]
打印對(duì)象地址
(lldb) expression self.view
(UIView *) $1 = 0x00007f7f18c085a0
打印對(duì)象
(lldb) expression -O -- self.view
<UIView: 0x7f7f18c085a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600002f266e0>>
p & print & call
expression
的別名宏侍,p
是print
的縮寫(xiě)
(lldb) p self.view.layer.backgroundColor = UIColor.greenColor.CGColor
(CGColorRef) $1 = 0x0000600000b64fc0
(lldb) p (void)[CATransaction flush]
(lldb) call self.view
(UIView *) $2 = 0x00007f7f18c085a0
(lldb) p self.view
(UIView *) $3 = 0x00007f7f18c085a0
(lldb) expression self.view
(UIView *) $4 = 0x00007f7f18c085a0
(lldb) call self.view
(UIView *) $5 = 0x00007f7f18c085a0
(lldb) print self.view
(UIView *) $6 = 0x00007f7f18c085a0
po
expression -O --
的別名
(lldb) expression -O -- self.view
<UIView: 0x7f7f18c085a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600002f266e0>>
(lldb) po self.view
<UIView: 0x7f7f18c085a0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600002f266e0>>
四赖淤、打印信息
thread backtrace
打印線程堆棧信息
(lldb) thread backtrace
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
* frame #0: 0x00000001020b3e6d daf`-[ViewController touchesBegan:withEvent:](self=0x00007f7f18f07170, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x0000600001a60d80) at ViewController.m:34:5
frame #1: 0x00007fff48cb94e2 UIKitCore`forwardTouchMethod + 323
frame #2: 0x00007fff48cb938e UIKitCore`-[UIResponder touchesBegan:withEvent:] + 49
frame #3: 0x00007fff48cc82ab UIKitCore`-[UIWindow _sendTouchesForEvent:] + 622
frame #4: 0x00007fff48cca311 UIKitCore`-[UIWindow sendEvent:] + 4501
frame #5: 0x00007fff48ca4755 UIKitCore`-[UIApplication sendEvent:] + 356
frame #6: 0x00007fff48d2f552 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 7628
frame #7: 0x00007fff48d32716 UIKitCore`__handleEventQueueInternal + 6584
frame #8: 0x00007fff48d28fb9 UIKitCore`__handleHIDEventFetcherDrain + 88
frame #9: 0x00007fff23da0d31 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #10: 0x00007fff23da0c5c CoreFoundation`__CFRunLoopDoSource0 + 76
frame #11: 0x00007fff23da0434 CoreFoundation`__CFRunLoopDoSources0 + 180
frame #12: 0x00007fff23d9b02e CoreFoundation`__CFRunLoopRun + 974
frame #13: 0x00007fff23d9a944 CoreFoundation`CFRunLoopRunSpecific + 404
frame #14: 0x00007fff38ba6c1a GraphicsServices`GSEventRunModal + 139
frame #15: 0x00007fff48c8b9ec UIKitCore`UIApplicationMain + 1605
frame #16: 0x00000001020b41a2 daf`main(argc=1, argv=0x00007ffeedb4acd0) at main.m:18:12
frame #17: 0x00007fff51a231fd libdyld.dylib`start + 1
bt
bt是thread backtrace的別名
thread return []
讓函數(shù)直接返回某個(gè)值,不會(huì)執(zhí)行斷點(diǎn)后面的代碼谅河,thread return
可以接受一個(gè)表達(dá)式咱旱,調(diào)用命令之后直接從當(dāng)前的frame返回表達(dá)式的值。
frame variable []
打印當(dāng)前棧幀的變量绷耍,也就是當(dāng)前函數(shù)的局部變量
//打印所有變量
(lldb) frame variable
(ViewController *) self = 0x00007f8ac2c088e0
(SEL) _cmd = "touchesBegan:withEvent:"
(__NSSetM *) touches = 0x0000600000429f00 1 element
(UITouchesEvent *) event = 0x0000600003144b40
//打印指定變量
(lldb) frame variable self
(ViewController *) self = 0x00007f8ac2c088e0
五吐限、流程控制
thread continue、continue褂始、c
表示程序繼續(xù)運(yùn)行
thread step-over诸典、next、n
源碼級(jí)別單步運(yùn)行崎苗,把子函數(shù)當(dāng)做整體一步執(zhí)行
thread step-in狐粱、step舀寓、s
源碼級(jí)別單步運(yùn)行,遇到子函數(shù)會(huì)進(jìn)入子函數(shù)
thread step-out肌蜻、finish
直接執(zhí)行完當(dāng)前函數(shù)的所有代碼互墓,返回到上一個(gè)函數(shù)
threadstep-inst-over、nexti宋欺、ni
ni
和n
類(lèi)似:n
源碼級(jí)別轰豆、ni
匯編指令級(jí)別
threadstep-inst胰伍、stepi齿诞、si
si
和s
類(lèi)似:s
源碼級(jí)別、si
匯編指令級(jí)別
五骂租、代碼斷點(diǎn)
breakpoint set -n 函數(shù)名
(lldb) breakpoint set -n test
Breakpoint 3: 4 locations.
(lldb) breakpoint set -n touchesBegan:withEvent:
Breakpoint 2: 90 locations.
(lldb) breakpoint set -n "-[ViewController touchesBegan:withEvent:]"
Breakpoint 3: where = daf`-[ViewController touchesBegan:withEvent:] + 70 at ViewController.m:30:6, address = 0x0000000106d1ff06
breakpoint set -a 函數(shù)地址
-
breakpointset-r 正則表達(dá)式
模糊搜索
(lldb) breakpoint set -n test
breakpoint set -s 動(dòng)態(tài)庫(kù) -n 函數(shù)名
-
breakpoint list
列出所有的斷點(diǎn)(每個(gè)斷點(diǎn)都有自己的編號(hào)) -
breakpoint disable 斷點(diǎn)編號(hào)
禁用斷點(diǎn) -
breakpoint enable 斷點(diǎn)編號(hào)
啟用斷點(diǎn) -
breakpoint delete 斷點(diǎn)編號(hào)
刪除斷點(diǎn) -
breakpoint command add 斷點(diǎn)編號(hào)
給斷點(diǎn)預(yù)先設(shè)置需要執(zhí)行的命令祷杈,到觸發(fā)斷點(diǎn)時(shí),就會(huì)按順序執(zhí)行
(lldb) breakpoint set -n "-[ViewController touchesBegan:withEvent:]"
Breakpoint 2: where = daf`-[ViewController touchesBegan:withEvent:] + 70 at ViewController.m:30:6, address = 0x000000010a6dcf06
(lldb) breakpoint command add 2
Enter your debugger command(s). Type 'DONE' to end.
> po self
> p self
> p self.view.layer.backgroundColor = [UIColor redColor].CGColor
> DONE
(lldb) c
Process 25531 resuming
po self
<ViewController: 0x7f87ea40a680>
p self
(ViewController *) $1 = 0x00007f87ea40a680
p self.view.layer.backgroundColor = [UIColor redColor].CGColor
(CGColorRef) $2 = 0x00006000007b1f20
-
breakpoint command list 斷點(diǎn)編號(hào)
查看某個(gè)斷點(diǎn)設(shè)置的命令
(lldb) breakpoint command list 2
Breakpoint 2:
Breakpoint commands:
po self
p self
p self.view.layer.backgroundColor = [UIColor redColor].CGColor
-
breakpoint command delete 斷點(diǎn)編號(hào)
刪除某個(gè)斷點(diǎn)設(shè)置的命令
(lldb) breakpoint command delete 2
(lldb) breakpoint command list 2
Breakpoint 2 does not have an associated command.
六渗饮、內(nèi)存斷點(diǎn)
在內(nèi)存數(shù)據(jù)發(fā)生改變的時(shí)候觸發(fā)
-
watchpoint set variable 變量
設(shè)置斷點(diǎn)
(lldb) watchpoint set variable self->_age
Watchpoint created: Watchpoint 1: addr = 0x7fdf1bc07150 size = 4 state = enabled type = w
watchpoint spec = 'self->_age'
new value: 0
watchpoint set expression 地址
(lldb) p &(self->_age)
(int *) $0 = 0x00007f8011f06ff0
(lldb) watchpoint set expression 0x00007f8011f06ff0
Watchpoint created: Watchpoint 1: addr = 0x7f8011f06ff0 size = 8 state = enabled type = w
new value: 0
(lldb)
-
watchpoint list
查看斷點(diǎn) -
watchpoint disable 斷點(diǎn)編號(hào)
禁用斷點(diǎn) -
watchpoint enable 斷點(diǎn)編號(hào)
啟用斷點(diǎn) -
watchpoint delete 斷點(diǎn)編號(hào)
刪除斷點(diǎn) -
watchpoint command add 斷點(diǎn)編號(hào)
給斷點(diǎn)預(yù)先設(shè)置需要執(zhí)行的命令但汞,到觸發(fā)斷點(diǎn)時(shí),就會(huì)按順序執(zhí)行 -
watchpoint command list 斷點(diǎn)編號(hào)
查看某個(gè)斷點(diǎn)設(shè)置的命令 -
watchpoint command delete 斷點(diǎn)編號(hào)
刪除某個(gè)斷點(diǎn)設(shè)置的命令
七互站、模塊查找
-
image list
列出所加載的模塊信息
(lldb) image list
[ 0] 4EE15C85-378E-38DE-8790-6047181E3944 0x0000000100c8b000 /Users/zhanglingli/Library/Developer/Xcode/DerivedData/daf-eqaqhhwbbadyvibyktibzhhnytym/Build/Products/Debug-iphonesimulator/daf.app/daf
[ 1] E4698FBD-806A-3396-B279-E685BA37430B 0x0000000105b24000 /usr/lib/dyld
[ 2] 548289A2-DC22-3BAA-A2F6-01EADE8D86D7 0x0000000100c99000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim
-
image lookup -t 類(lèi)型
查找某個(gè)類(lèi)型的信息
(lldb) image lookup -t ViewController
Best match found in /Users/zhanglingli/Library/Developer/Xcode/DerivedData/daf-eqaqhhwbbadyvibyktibzhhnytym/Build/Products/Debug-iphonesimulator/daf.app/daf:
id = {0x10000002b}, name = "ViewController", byte-size = 16, decl = ViewController.h:11, compiler_type = "@interface ViewController : UIViewController{
int _age;
}
@property(nonatomic, assign, readwrite, getter = age, setter = setAge:) int age;
@end"
(lldb) image lookup -t NSInteger
Best match found in /Users/zhanglingli/Library/Developer/Xcode/DerivedData/daf-eqaqhhwbbadyvibyktibzhhnytym/Build/Products/Debug-iphonesimulator/daf.app/daf:
id = {0x7fffffff000002e8}, name = "NSInteger", byte-size = 8, decl = NSObjCRuntime.h:12, compiler_type = "typedef NSInteger"
typedef 'NSInteger': id = {0x7fffffff000005b5}, name = "long int", qualified = "long", byte-size = 8, compiler_type = "long"
-
image lookup -a 地址
根據(jù)內(nèi)存地址查找在模塊中的位置
例如私蕾,當(dāng)我們發(fā)生一個(gè)crash
2020-08-14 10:09:09.007340+0800 daf[29312:2133141] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndexedSubscript:]: index 4 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3cf0e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50ba89b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23ecfa51 _CFThrowFormattedException + 194
3 CoreFoundation 0x00007fff23eae9bd -[__NSArrayI objectAtIndexedSubscript:] + 93
4 daf 0x000000010c4b8e16 -[ViewController touchesBegan:withEvent:] + 182
5 UIKitCore 0x00007fff48cb94e2 forwardTouchMethod + 323
6 UIKitCore 0x00007fff48cb938e -[UIResponder touchesBegan:withEvent:] + 49
我們可以看出,是由于數(shù)組越界導(dǎo)致的crash胡桃,并且是在ViewController的touchesBegan:withEvent:方法中調(diào)用的踩叭,那么具體在哪一行呢,我們就可以通過(guò)image lookup -a
指令查看
(lldb) image lookup -a 0x000000010c4b8e16
Address: daf[0x0000000100000e16] (daf.__TEXT.__text + 246)
Summary: daf`-[ViewController touchesBegan:withEvent:] + 182 at ViewController.m:26:5
由此可知翠胰,crash發(fā)生在ViewController.m的第26行
-
image lookup -n 符號(hào)或者函數(shù)名
查找某個(gè)符號(hào)或者函數(shù)的位置
(lldb) image lookup -n touchesBegan:withEvent:
1 match found in /Users/zhanglingli/Library/Developer/Xcode/DerivedData/daf-eqaqhhwbbadyvibyktibzhhnytym/Build/Products/Debug-iphonesimulator/daf.app/daf:
Address: daf[0x0000000100000d60] (daf.__TEXT.__text + 64)
Summary: daf`-[ViewController touchesBegan:withEvent:] at ViewController.m:23
79 matches found in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:
Address: UIKitCore[0x000000000002590c] (UIKitCore.__TEXT.__text + 142380)
Summary: UIKitCore`-[UIInterfaceActionGroupView touchesBegan:withEvent:] Address: UIKitCore[0x000000000002c297] (UIKitCore.__TEXT.__text + 169399)
-
image list -o -f
打印模塊的偏移地址容贝、全路徑