LLDB詳解

常用調(diào)試

你常用的調(diào)試是不是這樣?

NSLog(@"%@", whatIsInsideThisThing);

或者寫一個臨時變量?

NSNumber *n = @6; 

或者專門寫個檢查器來判斷?

if (1 || theBooleanAtStake) { ... }

或者專門寫一個方法?

int calculateTheTrickyValue {
  return 9;
  /*
   先這樣
   ...
}

是不是每次都要重新運(yùn)行程序,重新開始?

代碼已經(jīng)夠多,你還要多寫幾句代碼,然后重新編譯運(yùn)行看看結(jié)果對不對。如果你用的是低配電腦,或者項(xiàng)目比較大,運(yùn)行等待的過程那真是難受??!

其實(shí)你不需要重新編譯運(yùn)行,你完全可以使用調(diào)試器(也叫控制臺),即使你已經(jīng)會使用打個斷點(diǎn),或打個全局?jǐn)帱c(diǎn),但實(shí)際上調(diào)試器還可以做更多事情。

接下來我們來重新認(rèn)識Xcode中的調(diào)試器吧!看看到底能做多少事情。

LLDB

LLDB是一個帶著REPL(交互式解釋器)、C++特性,且?guī)в蠵ython插件的開源調(diào)試器。LLDB 綁定在 Xcode 內(nèi)部,也就是我們常見到的控制臺中(這里有一個關(guān)于調(diào)試器如何工作的總體的解釋。)

咱們以前使用調(diào)試器的時候,只是簡單的在Xcode中加一些斷點(diǎn),然后通過
(lldb) po value
來打印值對不對?但提到上面的那些缺點(diǎn),這已經(jīng)不能滿足我們調(diào)試的要求了。
接下來我們做更酷的事!

基礎(chǔ)

先寫一個簡單的代碼。這里會打印字符串。然后我們在打印字符串的那行代碼加個斷點(diǎn)。相信這點(diǎn)大家都會,如果你的確是新手,那你點(diǎn)下圖中22那個數(shù)字,就會看到和圖中一樣的斷點(diǎn)標(biāo)示。

斷點(diǎn)

我們運(yùn)行一下,接下來編譯器運(yùn)行到第22行的時候就停住了!
這時我們就可以使用調(diào)試器了 。先看看怎么用。
我們輸入: " help "

help

(lldb) help
Debugger commands:
  apropos           -- List debugger commands related to a word or subject.
  breakpoint        -- Commands for operating on breakpoints (see 'help b' for
                       shorthand.)
  bugreport         -- Commands for creating domain-specific bug reports.
  command           -- Commands for managing custom LLDB commands.
  disassemble       -- Disassemble specified instructions in the current
                       target.  Defaults to the current function for the
                       current thread and stack frame.
  expression        -- Evaluate an expression on the current thread.  Displays
                       any returned value with LLDB's default formatting.
  frame             -- Commands for selecting and examing the current thread's
                       stack frames.
  gdb-remote        -- Connect to a process via remote GDB server.  If no host
                       is specifed, localhost is assumed.
  gui               -- Switch into the curses based GUI mode.
  help              -- Show a list of all debugger commands, or give details
                       about a specific command.
....太長就不復(fù)制了

然后調(diào)試器打印了一些文字,這都是調(diào)試器的命令和說明,先簡單的看一看。

print

我們平時使用最多的就是p了,其實(shí)它是print,輸入看一下:

打印結(jié)果

實(shí)際上LLDB里面搞了很多別名,比如prin、prip等,都是print的別名,使用的效果和print是一樣的。但是你不能用pr,因?yàn)長LDB不能區(qū)分你輸入的pr是不是process。

圖中打印是不是還有個$0?,這個東西是用它來指向這個結(jié)果的,比如print $0 + 7也就是計(jì)算count+7的值,輸入后的結(jié)果是106。任何以美元符號開頭的東西都存在與LLDB的命名控件里面,它是為了幫助咱們調(diào)試而存在的。

expression

如果要改變一個值,你可用expression這個命令

改變一個值

你看,這個家伙它不僅改變了調(diào)試器中的值,它甚至還改變了程序中的值!牛逼吧。
它也有給別名e。

那么現(xiàn)在為了簡單一點(diǎn),從現(xiàn)在開始,我們用 pe 來代替 printexpression

什么是 print 命令

print就是打印的意思,比如想打印一個變量 print sum
這里有一個很有意思的事情,你輸入p count = 18expression count = 18,結(jié)果是一樣樣的!

輸入 help print,然后向下滾動,你會發(fā)現(xiàn):

'print' is an abbreviation for 'expression --'.   
(print是 `expression --` 的縮寫) `--` 標(biāo)示不需要參數(shù)

打印對象

嘗試輸入

p objects

控制臺輸出了

(NSString *) $7 = 0x0000000104da4040 @"red balloons"

如果打印復(fù)雜一點(diǎn)的的對象,你會發(fā)覺只打印了對象的指針地址

(lldb) p @[ @"foo", @"bar" ]

(NSArray *) $8 = 0x00007fdb9b71b3e0 @"2 elements" 

其實(shí)我只是想看看這個對象的 description 方法的結(jié)果。
那我就需要用-O(是字母O)這個參數(shù)了,我們輸入 e -O --$8,就打印了里面的值。

(lldb) e -O -- $8
<__NSArrayI 0x7fdb9b71b3e0>(
foo,
bar
)

其實(shí) e -o -- 也有個別名,那就是 po (print object 的縮寫),咱們用它更簡單??础?/p>

(lldb) po $8
<__NSArrayI 0x7fdb9b71b3e0>(
foo,
bar
)
(lldb) po @"lunar"
lunar
(lldb) p @"lunar"
(NSString *) $13 = 0x00007fdb9d0003b0 @"lunar"

還可以打印這些

我們可以設(shè)置打印格式,打印格式這么寫:print/<fmt>p/<fmt>
例子:
默認(rèn)的格式

(lldb) p 16
16

十六進(jìn)制:

(lldb) p/x 16
0x10

二進(jìn)制 (t 代表 two):

(lldb) p/t 16
0b00000000000000000000000000010000
(lldb) p/t (char)16
0b00010000

更多格式點(diǎn)這里。

變量

前面講完打印對象、又講完修改變量值,現(xiàn)在我們在講點(diǎn)東西。
我們可以用LLDB定義變量,是不是很酷!
咱們只需要記住,申明變量必須要以美元符開頭??蠢?/p>

(lldb) e int $a = 2
(lldb) p $a * 19
(int) $0 = 38
(lldb) e NSArray *$array = @[ @"Saturday", @"Sunday", @"Monday" ]
(lldb) p [$array count]
(NSUInteger) $1 = 3
(lldb) po [[$array objectAtIndex:0] uppercaseString]
SATURDAY
(lldb) p [[$array objectAtIndex:$a] characterAtIndex:0]
error: no known method '-characterAtIndex:'; cast the message send to the method's return type
error: 1 errors parsing expression

最后一個怎么回事!打印不出來?
這其實(shí)是因?yàn)闆]有確定返回的類型值。
解決辦法很簡單,前面加個類型,就像oc里面的類型強(qiáng)轉(zhuǎn)一樣。

(lldb) p (char)[[$array objectAtIndex:$a] characterAtIndex:0]
(char) $3 = 'M'
(lldb) p/d (char)[[$array objectAtIndex:$a] characterAtIndex:0]
(char) $4 = 77

搞定了。

流程控制

當(dāng)你通過 Xcode 的源碼編輯器的側(cè)邊槽 (或者通過下面的方法) 插入一個斷點(diǎn),程序到達(dá)斷點(diǎn)時會就會停止運(yùn)行。

調(diào)試條上會出現(xiàn)四個你可以用來控制程序的執(zhí)行流程的按鈕。

從左到右,四個按鈕分別是:continue,step over,step into,step out。

第一個,continue 按鈕,會取消程序的暫停,允許程序正常執(zhí)行 (要么一直執(zhí)行下去,要么到達(dá)下一個斷點(diǎn))。在 LLDB 中,你可以使用 process continue 命令來達(dá)到同樣的效果,它的別名為 continue,或者也可以縮寫為 c。

第二個,step over 按鈕,會以黑盒的方式執(zhí)行一行代碼。如果所在這行代碼是一個函數(shù)調(diào)用,那么就不會跳進(jìn)這個函數(shù),而是會執(zhí)行這個函數(shù),然后繼續(xù)。LLDB 則可以使用 thread step-overnext,或者 n 命令。

如果你確實(shí)想跳進(jìn)一個函數(shù)調(diào)用來調(diào)試或者檢查程序的執(zhí)行情況,那就用第三個按鈕,step in,或者在LLDB中使用 thread step instep,或者 s 命令。注意,當(dāng)前行不是函數(shù)調(diào)用時,nextstep 效果是一樣的。

大多數(shù)人知道 c,ns,但是其實(shí)還有第四個按鈕,step out。如果你曾經(jīng)不小心跳進(jìn)一個函數(shù),但實(shí)際上你想跳過它,常見的反應(yīng)是重復(fù)的運(yùn)行 n 直到函數(shù)返回。其實(shí)這種情況,step out 按鈕是你的救世主。它會繼續(xù)執(zhí)行到下一個返回語句 (直到一個堆棧幀結(jié)束) 然后再次停止。

例子

考慮下面一段程序:

代碼.jpg

假如我們運(yùn)行程序,讓它停止在斷點(diǎn),然后執(zhí)行下面一些列命令:

p i
n
s
p i
finish
p i
frame info

這里,frame info 會告訴你當(dāng)前的行數(shù)和源碼文件,以及其他一些信息;查看 help frame,help threadhelp process 來獲得更多信息。這一串命令的結(jié)果會是什么?看答案之前請先想一想。

(lldb) p i
(int) $0 = 99
(lldb) n
2021-10-26 23:10:00.101318+0800 LLDBTest[19576:205894] 101 is odd!
(lldb) s
(lldb) p i 
(int) $1 = 110
(lldb) finish
2021-10-26 23:10:17.660446+0800 LLDBTest[19576:205894] 110 is even!
(lldb) p i
(int) $2 = 99
(lldb) frame info
frame #0: 0x000000010e20de3c LLDBTest`-[ViewController viewDidLoad](self=0x00007fb7762059e0, _cmd="viewDidLoad") at ViewController.m:38:5
(lldb) 

它始終在 38 行的原因是 finish 命令一直運(yùn)行到 isEven() 函數(shù)的 return,然后立刻停止。注意即使它還在 38 行,其實(shí)這行已經(jīng)被執(zhí)行過了。

Thread Return

調(diào)試時,還有一個很棒的函數(shù)可以用來控制程序流程:thread return 。它有一個可選參數(shù),在執(zhí)行時它會把可選參數(shù)加載進(jìn)返回寄存器里,然后立刻執(zhí)行返回命令,跳出當(dāng)前棧幀。這意味這函數(shù)剩余的部分不會被執(zhí)行。這會給 ARC 的引用計(jì)數(shù)造成一些問題,或者會使函數(shù)內(nèi)的清理部分失效。但是在函數(shù)的開頭執(zhí)行這個命令,是個非常好的隔離這個函數(shù),偽造返回值的方式 。

讓我們稍微修改一下上面代碼段并運(yùn)行:

p i
s
thread return NO
n
p even0
frame info

看答案前思考一下。下面是答案:

(lldb) p i
(int) $0 = 99
(lldb) s
(lldb) thread return NO
(lldb) p even0
(BOOL) $2 = NO
(lldb) frame info
frame #0: 0x000000010a475df9 LLDBTest`-[ViewController viewDidLoad](self=0x00007fdf0f00d4e0, _cmd="viewDidLoad") at ViewController.m:43:5
(lldb) 

斷點(diǎn)

我們都把斷點(diǎn)作為一個停止程序運(yùn)行,檢查當(dāng)前狀態(tài),追蹤 bug 的方式。但是如果我們改變和斷點(diǎn)交互的方式,很多事情都變成可能。

斷點(diǎn)允許控制程序什么時候停止,然后允許命令的運(yùn)行。

想象把斷點(diǎn)放在函數(shù)的開頭,然后用 thread return 命令重寫函數(shù)的行為,然后繼續(xù)。想象一下讓這個過程自動化,聽起來不錯,不是嗎?

管理斷點(diǎn)

Xcode 提供了一系列工具來創(chuàng)建和管理斷點(diǎn)。我們會一個個看過來并介紹 LLDB 中等價的命令 (是的,你可以在調(diào)試器內(nèi)部添加斷點(diǎn))。

在 Xcode 的左側(cè)面板,有一組按鈕。其中一個看起來像斷點(diǎn)。點(diǎn)擊它打開斷點(diǎn)導(dǎo)航,這是一個可以快速管理所有斷點(diǎn)的面板。

在這里你可以看到所有的斷點(diǎn) - 在 LLDB 中通過 breakpoint list (或者 br li) 命令也做同樣的事兒。你也可以點(diǎn)擊單個斷點(diǎn)來開啟或關(guān)閉 - 在 LLDB 中使用 breakpoint enable <breakpointID>breakpoint disable <breakpointID>

(lldb) br li
Current breakpoints:
1: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 42, exact_match = 0, locations = 1, resolved = 1, hit count = 1

  1.1: where = LLDBTest`-[ViewController viewDidLoad] + 170 at ViewController.m:44:1, address = 0x0000000101f5fdfa, resolved, hit count = 1 

2: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 37, exact_match = 0, locations = 1, resolved = 1, hit count = 1

  2.1: where = LLDBTest`-[ViewController viewDidLoad] + 170 at ViewController.m:44:1, address = 0x0000000101f5fdfa, resolved, hit count = 1 
(lldb) br dis 1
1 breakpoints disabled.
(lldb) br li
1: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 42, exact_match = 0, locations = 1 Options: disabled 

  1.1: where = LLDBTest`-[ViewController viewDidLoad] + 170 at ViewController.m:44:1, address = 0x0000000101f5fdfa, unresolved, hit count = 1 

2: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 37, exact_match = 0, locations = 1, resolved = 1, hit count = 1

  2.1: where = LLDBTest`-[ViewController viewDidLoad] + 170 at ViewController.m:44:1, address = 0x0000000101f5fdfa, resolved, hit count = 1 

(lldb) br del 1
1 breakpoints deleted; 0 breakpoint locations disabled.
(lldb) br li
No breakpoints currently set.

創(chuàng)建斷點(diǎn)

在上面的例子中,我們通過在源碼頁面器的滾槽 16 上點(diǎn)擊來創(chuàng)建斷點(diǎn)。你可以通過把斷點(diǎn)拖拽出滾槽,然后釋放鼠標(biāo)來刪除斷點(diǎn) (消失時會有一個非??蓯鄣泥鄣囊幌碌膭赢?。你也可以在斷點(diǎn)導(dǎo)航頁選擇斷點(diǎn),然后按下刪除鍵刪除。

要在調(diào)試器中創(chuàng)建斷點(diǎn),可以使用 breakpoint set 命令。

(lldb) breakpoint set -f ViewController.m -l 25
Breakpoint 5: where = LLDBTest`-[ViewController viewDidLoad] + 66 at ViewController.m:25:15, address = 0x0000000101f5fd92

也可以使用縮寫形式 br。雖然 b 是一個完全不同的命令 (_regexp-break 的縮寫),但恰好也可以實(shí)現(xiàn)和上面同樣的效果。

(lldb) b ViewController.m:28
Breakpoint 6: where = LLDBTest`-[ViewController viewDidLoad] + 98 at ViewController.m:29:19, address = 0x0000000101f5fdb2

這些斷點(diǎn)會準(zhǔn)確的停止在函數(shù)的開始。Objective-C 的方法也完全可以:

(lldb) breakpoint set -F "-[NSArray objectAtIndex:]"
Breakpoint 5: where = CoreFoundation`-[NSArray objectAtIndex:], address = 0x000000010ac7a950
(lldb) b -[NSArray objectAtIndex:]
Breakpoint 6: where = CoreFoundation`-[NSArray objectAtIndex:], address = 0x000000010ac7a950
(lldb) breakpoint set -F "+[NSSet setWithObject:]"
Breakpoint 7: where = CoreFoundation`+[NSSet setWithObject:], address = 0x000000010abd3820
(lldb) b +[NSSet setWithObject:]
Breakpoint 8: where = CoreFoundation`+[NSSet setWithObject:], address = 0x000000010abd3820

如果想在 Xcode 的UI上創(chuàng)建符號斷點(diǎn),你可以點(diǎn)擊斷點(diǎn)欄左側(cè)的 + 按鈕,選擇第三個選項(xiàng)

添加.png

這時會出現(xiàn)一個彈出框,你可以在里面添加例如 testAddAction 這樣的符號斷點(diǎn)。這樣每次調(diào)用這個函數(shù)的時候,程序都會停止,不管是你調(diào)用還是蘋果調(diào)用。

如果你 Xcode 的 UI 上右擊任意斷點(diǎn),然后選擇 "Edit Breakpoint" 的話,會有一些非常誘人的選擇。

這里,斷點(diǎn)已經(jīng)被修改為只有當(dāng) i99 的時候才會停止。你也可以使用 "ignore" 選項(xiàng)來告訴斷點(diǎn)最初的 n次調(diào)用 (并且條件為真的時候) 的時候不要停止。

接下來介紹 'Add Action' 按鈕...

斷點(diǎn)行為 (Action)

上面的例子中,你或許想知道每一次到達(dá)斷點(diǎn)的時候 i 的值。我們可以使用 p i 作為斷點(diǎn)行為。這樣每次到達(dá)斷點(diǎn)的時候,都會自動運(yùn)行這個命令??梢钥吹剿蛴?i,接著打印了自定義的表達(dá)式。下面是在 LLDB 而不是 Xcode 的 UI 中做這些的時候,看起來的樣子。

(lldb) breakpoint set -F isEven
Breakpoint 4: where = LLDBTest`-[ViewController isEven] + 26 at ViewController.m:68:5, address = 0x000000010f53ae5a
(lldb) breakpoint modify -c 'i == 99' 1
(lldb) breakpoint command add 1
Enter your debugger command(s).  Type 'DONE' to end.
> p i
> DONE
(lldb) br li 1
1: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 23, exact_match = 0, locations = 1, resolved = 1, hit count = 1
    Breakpoint commands:
      p i

Condition: i == 99

  1.1: where = LLDBTest`-[ViewController viewDidLoad] + 58 at ViewController.m:23:15, address = 0x000000010f53ac7a, resolved, hit count = 1 

接下來說說自動化。

賦值后繼續(xù)運(yùn)行

看編輯斷點(diǎn)彈出窗口的底部,你還會看到一個選項(xiàng): "Automatically continue after evaluation actions." 。它僅僅是一個選擇框,但是卻很強(qiáng)大。選中它,調(diào)試器會運(yùn)行你所有的命令,然后繼續(xù)運(yùn)行??雌饋砭拖駴]有執(zhí)行任何斷點(diǎn)一樣 (除非斷點(diǎn)太多,運(yùn)行需要一段時間,拖慢了你的程序)。

這個選項(xiàng)框的效果和讓最后斷點(diǎn)的最后一個行為是 continue 一樣。選框只是讓這個操作變得更簡單。調(diào)試器的輸出是:

(lldb) breakpoint set -F isEven
Breakpoint 3: where = LLDBTest`-[ViewController isEven] + 26 at ViewController.m:68:5, address = 0x0000000104e13e5a
(lldb) breakpoint command add 1
Enter your debugger command(s).  Type 'DONE' to end.
> continue
> DONE
(lldb) br li 1
1: file = '/Users/issuser/Desktop/LLDBTest/LLDBTest/ViewController.m', line = 23, exact_match = 0, locations = 1, resolved = 1, hit count = 1
    Breakpoint commands:
      continue

  1.1: where = LLDBTest`-[ViewController viewDidLoad] + 58 at ViewController.m:23:15, address = 0x0000000104e13c7a, resolved, hit count = 1 

執(zhí)行斷點(diǎn)后自動繼續(xù)運(yùn)行,允許你完全通過斷點(diǎn)來修改程序!你可以在某一行停止,運(yùn)行一個 expression 命令來改變變量,然后繼續(xù)運(yùn)行。

例子

想想所謂的"打印調(diào)試"技術(shù)吧,不要這么做:

NSLog(@"%@", whatIsInsideThisThing);

而是用個打印變量的斷點(diǎn)替換 log 語句,然后繼續(xù)運(yùn)行。

也不要:

int calculateTheTrickyValue {
  return 9;

  /*
   Figure this out later.
   ...
}

而是加一個使用 thread return 9 命令的斷點(diǎn),然后讓它繼續(xù)運(yùn)行。

符號斷點(diǎn)加上 action 真的很強(qiáng)大。你也可以在你朋友的 Xcode 工程上添加一些斷點(diǎn),并且加上大聲朗讀某些東西的 action??纯此麄円ǘ嗑貌拍芘靼装l(fā)生了什么。

完全在調(diào)試器內(nèi)運(yùn)行

在開始舞蹈之前,還有一件事要看一看。實(shí)際上你可以在調(diào)試器中執(zhí)行任何 C/Objective-C/C++/Swift 的命令。唯一的缺點(diǎn)就是不能創(chuàng)建新函數(shù)... 這意味著不能創(chuàng)建新的類,block,函數(shù),有虛擬函數(shù)的 C++ 類等等。除此之外,它都可以做。

我們可以申請分配一些字節(jié):

(lldb) e char *$str = (char *)malloc(8)
(lldb) e (void)strcpy($str, "munkeys")
(lldb) e $str[1] = 'o'
(char) $0 = 'o'
(lldb) p $str
(char *) $str = 0x00007fd04a900040 "monkeys"

我們可以查看內(nèi)存 (使用 x 命令),來看看新數(shù)組中的四個字節(jié):

(lldb) x/1c $str
0x7fd04a900040: monk

我們也可以去掉 3 個字節(jié) (x 命令需要斜引號,因?yàn)樗挥幸粋€內(nèi)存地址的參數(shù),而不是表達(dá)式;使用 help x 來獲得更多信息):

(lldb) x/1w `$str + 3`
0x7fd04a900043: keys

做完了之后,一定不要忘了釋放內(nèi)存,這樣才不會內(nèi)存泄露。(哈,雖然這是調(diào)試器用到的內(nèi)存):

(lldb) e (void)free($str)

不用斷點(diǎn)調(diào)試

程序運(yùn)行時,Xcode 的調(diào)試條上會出現(xiàn)暫停按鈕,而不是繼續(xù)按鈕:

點(diǎn)擊按鈕會暫停 app (這會運(yùn)行 process interrupt 命令,因?yàn)?LLDB 總是在背后運(yùn)行)。這會讓你可以訪問調(diào)試器,但看起來可以做的事情不多,因?yàn)樵诋?dāng)前作用域沒有變量,也沒有特定的代碼讓你看。

這就是有意思的地方。如果你正在運(yùn)行 iOS app,你可以試試這個: (因?yàn)槿肿兞渴强稍L問的)

(lldb) po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
<UIWindow: 0x7fe9cc10a240; frame = (0 0; 390 844); gestureRecognizers = <NSArray: 0x600002702430>; layer = <UIWindowLayer: 0x60000273a970>>
   | <UITransitionView: 0x7fe9c9f08b40; frame = (0 0; 390 844); autoresize = W+H; layer = <CALayer: 0x60000296df00>>
   |    | <UIDropShadowView: 0x7fe9cc10b180; frame = (0 0; 390 844); autoresize = W+H; layer = <CALayer: 0x600002976860>>
   |    |    | <UIView: 0x7fe9cc10bca0; frame = (0 0; 390 844); autoresize = W+H; layer = <CALayer: 0x6000029767e0>>
   |    |    |    | <UILabel: 0x7fe9cc10c010; frame = (171 424; 48 24); text = 'LLDB'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000a78c30>>

你可以看到整個層次。Chisel(Chisel是一個LLDB命令集合,用于幫助調(diào)試iOS應(yīng)用程序。)中 pviews 就是這么實(shí)現(xiàn)的。

更新UI

有了上面的輸出,我們可以獲取這個 view:

(lldb) e id $myView = (id)0x7f82b1d01fd0

然后在調(diào)試器中改變它的背景色:

(lldb) e (void)[$myView setBackgroundColor:[UIColor blueColor]]

但是只有程序繼續(xù)運(yùn)行之后才會看到界面的變化。因?yàn)楦淖兊膬?nèi)容必須被發(fā)送到渲染服務(wù)中,然后顯示才會被更新。

渲染服務(wù)實(shí)際上是一個另外的進(jìn)程 (被稱作 backboardd)。這就是說即使我們正在調(diào)試的內(nèi)容所在的進(jìn)程被打斷了,backboardd 也還是繼續(xù)運(yùn)行著的。

這意味著你可以運(yùn)行下面的命令,而不用繼續(xù)運(yùn)行程序:

(lldb) e (void)[CATransaction flush]

即使你仍然在調(diào)試器中,UI 也會在模擬器或者真機(jī)上實(shí)時更新。Chisel 為此提供了一個別名叫做 caflush,這個命令被用來實(shí)現(xiàn)其他的快捷命令,例如 hide <view>,show <view> 以及其他很多命令。所有 Chisel 的命令都有文檔,所以安裝后隨意運(yùn)行 help show 來看更多信息。

Push 一個 View Controller

想象一個以 UINavigationController 為 root ViewController 的應(yīng)用。你可以通過下面的命令,輕松地獲取它:

(lldb) e id $nvc = [[[UIApplication sharedApplication] keyWindow] rootViewController]

然后 push 一個 child view controller:

(lldb) e id $vc = [UIViewController new]
(lldb) e (void)[[$vc view] setBackgroundColor:[UIColor yellowColor]]
(lldb) e (void)[$vc setTitle:@"Yay!"]
(lldb) e (void)[$nvc pushViewContoller:$vc animated:YES]

最后運(yùn)行下面的命令:

(lldb) caflush // e (void)[CATransaction flush]

navigation Controller 就會立刻就被 push 到你眼前。

查找按鈕的 target

想象你在調(diào)試器中有一個 $myButton 的變量,可以是創(chuàng)建出來的,也可以是從 UI 上抓取出來的,或者是你停止在斷點(diǎn)時的一個局部變量。你想知道,按鈕按下的時候誰會接收到按鈕發(fā)出的 action。非常簡單:

(lldb) po [$myButton allTargets]
{(
    <ViewController: 0x7fb58bd2e240>
)}
(lldb) po [$myButton actionsForTarget:(id)0x7fb58bd2e240 forControlEvent:0]
<__NSArrayM 0x7fb58bd2aa40>(
_handleTap:
)

LLDB 和 Python

LLDB 有內(nèi)建的,完整的 Python 支持。在LLDB中輸入 script,會打開一個 Python REPL。你也可以輸入一行 python 語句作為 script 命令 的參數(shù),這可以運(yùn)行 python 語句而不進(jìn)入REPL:

(lldb) script import os
(lldb) script os.system("open http://www.objc.io/")

這樣就允許你創(chuàng)造各種酷的命令。把下面的語句放到文件 ~/myCommands.py 中:

def caflushCommand(debugger, command, result, internal_dict):
  debugger.HandleCommand("e (void)[CATransaction flush]")

然后再 LLDB 中運(yùn)行:

command script import ~/myCommands.py

或者把這行命令放在 /.lldbinit 里,這樣每次進(jìn)入 LLDB 時都會自動運(yùn)行。Chisel 其實(shí)就是一個 Python 腳本的集合,這些腳本拼接 (命令) 字符串 ,然后讓 LLDB 執(zhí)行。很簡單,不是嗎?

緊握調(diào)試器這一武器

LLDB 可以做的事情很多。大多數(shù)人習(xí)慣于使用 p,po,n,sc,但實(shí)際上除此之外,LLDB 可以做的還有很多。掌握所有的命令 (實(shí)際上并不是很多),會讓你在揭示代碼運(yùn)行時的運(yùn)行狀態(tài),尋找 bug,強(qiáng)制執(zhí)行特定的運(yùn)行路徑時獲得更大的能力。你甚至可以構(gòu)建簡單的交互原型 - 比如要是現(xiàn)在以 modal 方式彈出一個 View Controller 會怎么樣?使用調(diào)試器,一試便知。

這篇文章是為了想你展示 LLDB 的強(qiáng)大之處,并且鼓勵你多去探索在控制臺輸入命令。熟悉LLDB,能給你增加更多加分技能。以下是LLDB更多相關(guān)內(nèi)容:

    1. 高級lldb操作與lldbinit文件
    1. 代碼模擬lldb執(zhí)行流程
    1. lldb接口學(xué)習(xí)
    1. 自定義lldb命令

這些都可以在了解LLDB后去做研究。打開 LLDB,輸入 help,看一看列舉的命令。你嘗試過多少?用了多少?但愿 NSLog 看起來不再那么吸引你去用,每次編輯再運(yùn)行并不有趣而且耗時。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Xcode po 是什么你以前調(diào)試是不是這樣打印的? 或者臨時寫一個臨時變量? 或者專門寫個檢查器來判斷? 或者專...
    Hanfank閱讀 13,024評論 0 16
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    木易林1閱讀 1,033評論 0 4
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    F麥子閱讀 1,294評論 1 2
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    paraneaeee閱讀 1,321評論 0 7
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    我是啊梁閱讀 886評論 1 1

友情鏈接更多精彩內(nèi)容