17_常用LLDB指令

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的別名宏侍,pprint的縮寫(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)行


Snip20200813_16.png

thread step-over诸典、next、n

源碼級(jí)別單步運(yùn)行崎苗,把子函數(shù)當(dāng)做整體一步執(zhí)行


Snip20200813_17.png

thread step-in狐粱、step舀寓、s

源碼級(jí)別單步運(yùn)行,遇到子函數(shù)會(huì)進(jìn)入子函數(shù)


Snip20200813_18.png

thread step-out肌蜻、finish

直接執(zhí)行完當(dāng)前函數(shù)的所有代碼互墓,返回到上一個(gè)函數(shù)


Snip20200813_19.png

threadstep-inst-over、nexti宋欺、ni

nin類(lèi)似:n源碼級(jí)別轰豆、ni匯編指令級(jí)別

threadstep-inst胰伍、stepi齿诞、si

sis類(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
    打印模塊的偏移地址容贝、全路徑
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市之景,隨后出現(xiàn)的幾起案子斤富,更是在濱河造成了極大的恐慌,老刑警劉巖锻狗,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件满力,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡轻纪,警方通過(guò)查閱死者的電腦和手機(jī)油额,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)桐磁,“玉大人悔耘,你說(shuō)我怎么就攤上這事∥依蓿” “怎么了衬以?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵缓艳,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我看峻,道長(zhǎng)阶淘,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任互妓,我火速辦了婚禮溪窒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘冯勉。我一直安慰自己澈蚌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布灼狰。 她就那樣靜靜地躺著宛瞄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪交胚。 梳的紋絲不亂的頭發(fā)上份汗,一...
    開(kāi)封第一講書(shū)人閱讀 49,007評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音蝴簇,去河邊找鬼杯活。 笑死,一個(gè)胖子當(dāng)著我的面吹牛熬词,可吹牛的內(nèi)容都是我干的旁钧。 我是一名探鬼主播,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼荡澎,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼均践!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起摩幔,我...
    開(kāi)封第一講書(shū)人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤彤委,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后或衡,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體焦影,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年封断,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了斯辰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡坡疼,死狀恐怖彬呻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤闸氮,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布剪况,位于F島的核電站,受9級(jí)特大地震影響蒲跨,放射性物質(zhì)發(fā)生泄漏译断。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一或悲、第九天 我趴在偏房一處隱蔽的房頂上張望孙咪。 院中可真熱鬧,春花似錦巡语、人聲如沸翎蹈。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)杨蛋。三九已至,卻和暖如春理澎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背曙寡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工糠爬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人举庶。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓执隧,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親户侥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子镀琉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345