iOS lldb斷點調(diào)試

  Xcode的使用中總是離不開調(diào)試這個環(huán)境,在一年多的iOS開發(fā)時間中妥泉,我更多地依賴于XCode本身提供的GUI工具來進行調(diào)試,而對LLDB敬而遠之相种,這段時間好好學習了LLDB的使用遣妥,發(fā)覺我錯過了太多東西了……因此做一個比較完備的總結(jié),也希望在寫這篇文章的過程中進一步學習LLDB調(diào)試的各種實踐方法谈跛。

LLDB闡述

LLDB 是一個有著 REPL 的特性和 C++ ,Python 插件的開源調(diào)試器羊苟。LLDB 綁定在 Xcode 內(nèi)部,存在于主窗口底部的控制臺中感憾。調(diào)試器允許你在程序運行的特定時暫停它蜡励,你可以查看變量的值,執(zhí)行自定的指令阻桅,并且按照你所認為合適的步驟來操作程序的進展凉倚。

lldb下,相信大家用的較多的是po鳍刷,或者使用條件斷點占遥。但是面對復雜的問題時俯抖,比如.a靜態(tài)庫输瓜,就需要其他調(diào)試方法了。

breakpoint

給某個文件的某一行下斷點芬萍∮却В可以使用如下兩種方法,比如我想給Person.m文件的10行下一個斷點柬祠”毕罚可以使用如下的方法。

(lldb) breakpoint set --file Person.m --line 10

如果出現(xiàn)如下提示則說明設(shè)置斷點成功

Breakpoint 2: where = BreakPointDemo`-[Person foo] + 23 at Foo.m:10, address = 0x000000010b22e687

也可以使用簡寫的形式如下漫蛔。

(lldb) breakpoint set -f Foo.m -l 10

給一個函數(shù)下斷點

(lldb) breakpoint set --name foo
(lldb) breakpoint set -n foo

一次性給多個函數(shù)下斷點

(lldb) breakpoint set --name foo --name bar

OC的方法嗜愈,可以使用以下兩種方式打斷點,第二種S需要大寫

(lldb) breakpoint set --selector foo

(lldb) breakpoint set -S foo

使用 正則匹配 你要打斷點的函數(shù)莽龟。這個不限語言

(lldb) breakpoint set -r cFoo
(lldb) breakpoint set -r foo

也可以指定加載的動態(tài)庫

(lldb) breakpoint set --shlib foo.dylib --name foo 
(lldb) breakpoint set -s foo.dylib -n foo

我們同樣可以對命令進行簡寫蠕嫁。下面兩個命令的效果是一樣的

(lldb) breakpoint set -n "-[Foo foo]"
(lldb) br s -n "-[Foo foo]"

查看有多少斷點可以使用

(lldb) breakpoint list

打印結(jié)果如下:

  Current breakpoints:
  1: file =       '/Users/jianquan/Xcode/BreakPointDemo/BreakPointDemo/ViewCo ntroller.m', line = 20, exact_match = 0, locations = 0 (pending)
  2: file = '/Users/jianquan/Xcode/BreakPointDemo/BreakPointDemo/ViewController.mm', line = 33, exact_match = 0, locations = 1, resolved = 1, hit count = 0
  2.1: where = BreakPointDemo`::-[ViewController viewDidLoad]() + 186 at ViewController.mm:34, address = 0x0000000105f8362a, resolved, hit count = 0 
  ......

我們可以對斷點進行相關(guān)的操作,比如在執(zhí)行到2.1斷點的時候打印追蹤軌跡毯盈。bt是

(lldb) breakpoint command add 2.1
Enter your debugger command(s).  Type 'DONE' to end.
> bt
> DONE

刪除所有斷點

(lldb) breakpoint delete

watchpoint

觀察變量值的具體變化
比如我需要觀察某個變量a的值變化,我可以使用如下命令

(lldb) watchpoint set variable a

bt命令用來追蹤程序的運行過程

(lldb) bt
* thread #1: tid = 0x5c52c2, 0x000000010ff465fe     BreakPointDemo`::-[ViewController viewDidLoad](self=0x00007f932cc07c50, _cmd="viewDidLoad") + 158 at ViewController.mm:36, queue = 'com.apple.main-thread', stop reason = watchpoint 1
* frame #0: 0x000000010ff465fe BreakPointDemo`::-[ViewController viewDidLoad](self=0x00007f932cc07c50, _cmd="viewDidLoad") + 158 at ViewController.mm:36
frame #1: 0x000000011112ba3d UIKit`-[UIViewController loadViewIfRequired] + 1258
 ......

我們可以使用frame命令查看變量a的具體值剃毒。

(lldb) frame variable a
(int) a = 100

補充一點watchpoint list的東西。這個命令包括了三個可選參數(shù),我們可以使用help命令查看具體的值

(lldb) help watchpoint list

   -b ( --brief )
        Give a brief description of the watchpoint (no location info).

   -f ( --full )
        Give a full description of the watchpoint and its locations.

   -v ( --verbose )
        Explain everything we know about the watchpoint (for debugging
        debugger bugs).

-b是比較簡略的信息赘阀,-f是比較全面的信息益缠,-v是完整的信息。經(jīng)過我的實驗基公,如果使用watchpoint list,默認的是 watchpoint list -f幅慌。

process

使用process命令也可以做很多有趣的操作。具體能做什么酌媒,我們也可使用help命令查看

(lldb) process help
  attach    -- Attach to a process.
  connect   -- Connect to a remote debug service.
  continue  -- Continue execution of all threads in the current process.
  detach    -- Detach from the current target process.
  handle    -- Manage LLDB handling of OS signals for the current target
......

查看更詳細的命令使用help <command> <subcommand>欠痴。比如

 (lldb) help process attach

thread

其實這個功能主要就是斷點調(diào)試里面的如下這個功能。

1654054-8d533324eaa728a8.png

我們可以使用thread命令來做一些斷點的操作秒咨,具體有那些命令我們可以使用thread help進行查看

(lldb) thread help

  ......

  select         -- Change the currently selected thread.
  step-in        -- Source level single step, stepping into calls. 
                    Defaults to current thread unless specified.
  step-inst      -- Instruction level single step, stepping into calls. 
                    Defaults to current thread unless specified.
  step-inst-over -- Instruction level single step, stepping over calls. 
                    Defaults to current thread unless specified.
  step-out       -- Finish executing the current stack frame and stop after
                    returning.  Defaults to current thread unless
                    specified.
  step-over      -- Source level single step, stepping over calls. 
                    Defaults to current thread unless specified.
  step-scripted  -- Step as instructed by the script class passed in the -C
                    option.
  until          -- Continue until a line number or address is reached by
                    the current or specified thread.  Stops when returning
                    from the current function as a safety measure.

用得比較多的應(yīng)該是 step-開頭的這幾個命令喇辽,使用起來很容易。我個人感覺比用鼠標點擊斷點好用多了~
檢查當前進程的狀態(tài)雨席,可以使用如下命令

 lldb)  thread list
Process 22323 stopped
* thread #1: tid = 0x62d0d7, 0x00000001082185fe BreakPointDemo`::-[ViewController viewDidLoad](self=0x00007ff81b60ab20, _cmd="viewDidLoad") + 158 at ViewController.mm:36, queue = 'com.apple.main-thread', stop reason = step until
......

*表明的就是當前的線程菩咨,可以使用如下的命令得到線程的回溯,這個詞我也不確定怎么表達好陡厘,backtrace抽米,也可以說是追蹤。

(lldb) thread backtrace
* thread #1: tid = 0x354e72, 0x0000000103ce94bc RunTimeUse`-[ViewController viewDidLoad](self=0x00007ffea04050e0, _cmd="viewDidLoad") + 140 at ViewController.m:24, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x0000000103ce94bc RunTimeUse`-[ViewController viewDidLoad](self=0x00007ffea04050e0, _cmd="viewDidLoad") + 140 at ViewController.m:24
frame #1: 0x0000000104e21c99 UIKit`-[UIViewController loadViewIfRequired] + 1258
frame #2: 0x0000000104e220cc UIKit`-[UIViewController view] + 27
frame #3: 0x0000000104cebc51 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 71
frame #4: 0x0000000104cec3a2 UIKit`-[UIWindow _setHidden:forced:] + 293
frame #5: 0x0000000104cffcb5 UIKit`-[UIWindow makeKeyAndVisible] + 42
frame #6: 0x0000000104c78c89 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
frame #7: 0x0000000104c7ede9 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
frame #8: 0x0000000104c7bf69 UIKit`-[UIApplication workspaceDidEndTransaction:] + 188
frame #9: 0x0000000107dd4723 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
frame #10: 0x0000000107dd459c FrontBoardServices`-[FBSSerialQueue _performNext] + 189
frame #11: 0x0000000107dd4925 FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 45
frame #12: 0x0000000104802311 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #13: 0x00000001047e759c CoreFoundation`__CFRunLoopDoSources0 + 556
frame #14: 0x00000001047e6a86 CoreFoundation`__CFRunLoopRun + 918
frame #15: 0x00000001047e6494 CoreFoundation`CFRunLoopRunSpecific + 420
frame #16: 0x0000000104c7a7e6 UIKit`-[UIApplication _run] + 434
frame #17: 0x0000000104c80964 UIKit`UIApplicationMain + 159
frame #18: 0x0000000103ce9b1f RunTimeUse`main(argc=1, argv=0x00007fff5bf165e8) + 111 at main.m:14
frame #19: 0x000000010763d68d libdyld.dylib`start + 1
frame #20: 0x000000010763d68d libdyld.dylib`start + 1

當然我們?nèi)绻肟此芯€程的backtrace糙置,可以使用thread backtrace all命令云茸。內(nèi)容太多,我這里就不演示log輸出了谤饭。

如果我們想單獨查看某個線程标捺,我們可以先使用thread select 2跳到某個具體的線程,然后再進行其他操作揉抵,比如thread backtrace

為了方便的觀測架構(gòu)參數(shù)和本地變量亡容,我們可以使用 frame variable 命令

如果我什么參數(shù)也不加,將會把所有的參數(shù)和本地變量到打印出來冤今。

(lldb) frame variable
(ViewController *) self = 0x00007ffea04050e0
(SEL) _cmd = "viewDidLoad"
(Person *) p = 0x0000610000001940

要打印某個變量需要在參數(shù)里面指定闺兢,這個命令我們在前面也使用過,比如要查看self

(lldb) frame variable self
(ViewController *) self = 0x00007ff81b60ab20

更進一步,我們可以查看一些子元素

(lldb) frame variable self->isa
(Class) self->isa = ViewController

命令雖然不是完整的表達式解釋器戏罢,當時可以識別一些基本的操作 比如 &, *, ->, []屋谭,不是重載運算符,數(shù)組也可以使用龟糕,因為數(shù)組本身也是指針桐磁。

(lldb) frame variable *self 

(ViewController) *self = {
UIViewController = {
UIResponder = {
  NSObject = {
    isa = ViewController
  }
......
}

和之前thread命令很類似,我可以使用frame select去選擇另外的一個frame

(lldb) frame select 9

如果想看更復雜的數(shù)據(jù)翩蘸,我們可以使用expression命令

(lldb) expression self
(ViewController *) $0 = 0x00007fefa4705110

更復雜一些所意,我們可以用來輸出一個表達式

(lldb) expr (int) printf ("I have a pointer 0x%llx.\n", self)
I have a pointer 0x7fefa4705110.
(int) $1 = 33
call

其實這個命令完全可以使用po進行替代,call一般可以用來調(diào)用不需要返回值的調(diào)試命令,比如更改View的背景顏色扶踊,以下兩個命令都可以達到相似的作用泄鹏,更改當前View的背景顏色值。

(lldb) po [self.view setBackgroundColor:[UIColor redColor]]
(lldb) call [self.view setBackgroundColor:[UIColor redColor]]
image

這個比較實用秧耗,可用于尋找棧地址對應(yīng)的代碼位置备籽。

//測試image命令使用
NSArray *a = @[@"1"];
NSLog(@"%@",a[1]);
很顯然,數(shù)組越界了分井,以下顯示崩潰信息

RunTimeUse[46698:3510999] *** Terminating app due to     uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010934d34b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x0000000108dae21e objc_exception_throw + 48
2   CoreFoundation                      0x00000001093a5bdf -[__NSSingleObjectArrayI objectAtIndex:] + 111
3   RunTimeUse                          0x00000001087d9480 -[ViewController viewDidLoad] + 320
4   UIKit                               0x0000000109911c99 -[UIViewController loadViewIfRequired] + 1258
5   UIKit                               0x00000001099120cc -[UIViewController view] + 27
6   UIKit                               0x00000001097dbc51 -[UIWindow addRootViewControllerViewIfPossible] + 71
7   UIKit                               0x00000001097dc3a2 -[UIWindow _setHidden:forced:] + 293
8   UIKit                               0x00000001097efcb5 -[UIWindow makeKeyAndVisible] + 42
9   UIKit                               0x0000000109768c89 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
10  UIKit                               0x000000010976ede9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
11  UIKit                               0x000000010976bf69 -[UIApplication workspaceDidEndTransaction:] + 188
12  FrontBoardServices                  0x000000010c8c4723 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
13  FrontBoardServices                  0x000000010c8c459c -[FBSSerialQueue _performNext] + 189
14  FrontBoardServices                  0x000000010c8c4925 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
15  CoreFoundation                      0x00000001092f2311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16  CoreFoundation                      0x00000001092d759c __CFRunLoopDoSources0 + 556
17  CoreFoundation                      0x00000001092d6a86 __CFRunLoopRun + 918
18  CoreFoundation                      0x00000001092d6494 CFRunLoopRunSpecific + 420
19  UIKit                               0x000000010976a7e6 -[UIApplication _run] + 434
20  UIKit                               0x0000000109770964 UIApplicationMain + 159
21  RunTimeUse                          0x00000001087d9acf main + 111
22  libdyld.dylib                       0x000000010c12d68d start + 1
)

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
程序奔潰在3车猬,地址為:0x00000001087d9480,

因為我的Demo名字叫RunTimeUse尺锚。其他的名字很明顯是系統(tǒng)的庫珠闰。雖然log的21行也有RunTimeUse,但是經(jīng)過觀察應(yīng)該是main函數(shù),不在考慮范圍之內(nèi)瘫辩。
我們使用image的 lookup命令伏嗜,可以很快的定位到具體的代碼行。

  (lldb) image lookup --address 0x00000001087d9480
  Address: RunTimeUse[0x0000000100001480] (RunTimeUse.__TEXT.__text + 320)
  Summary: RunTimeUse`-[ViewController viewDidLoad] + 320 at ViewController.m:28
1654054-7dd117c55d5e5772.png

可以看出伐厌,奔潰在ViewController.m中的第28行承绸。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市挣轨,隨后出現(xiàn)的幾起案子军熏,更是在濱河造成了極大的恐慌,老刑警劉巖卷扮,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荡澎,死亡現(xiàn)場離奇詭異,居然都是意外死亡画饥,警方通過查閱死者的電腦和手機衔瓮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門浊猾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抖甘,“玉大人,你說我怎么就攤上這事葫慎∠纬梗” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵偷办,是天一觀的道長艰额。 經(jīng)常有香客問我,道長椒涯,這世上最難降的妖魔是什么柄沮? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上祖搓,老公的妹妹穿的比我還像新娘狱意。我一直安慰自己,他們只是感情好拯欧,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布详囤。 她就那樣靜靜地躺著,像睡著了一般镐作。 火紅的嫁衣襯著肌膚如雪藏姐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天该贾,我揣著相機與錄音羔杨,去河邊找鬼。 笑死杨蛋,一個胖子當著我的面吹牛问畅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播六荒,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼护姆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掏击?” 一聲冷哼從身側(cè)響起卵皂,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎砚亭,沒想到半個月后灯变,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡捅膘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年添祸,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寻仗。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡刃泌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出署尤,到底是詐尸還是另有隱情耙替,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布曹体,位于F島的核電站俗扇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏箕别。R本人自食惡果不足惜铜幽,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一滞谢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧除抛,春花似錦爹凹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绘趋,卻和暖如春颤陶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背陷遮。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工滓走, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人帽馋。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓搅方,卻偏偏與公主長得像,于是被迫代替她去往敵國和親绽族。 傳聞我的和親對象是個殘疾皇子姨涡,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容

  • LLDB的Xcode默認的調(diào)試器,它與LLVM編譯器一起吧慢,帶給我們更豐富的流程控制和數(shù)據(jù)檢測的調(diào)試功能涛漂。平時用Xc...
    CoderSC閱讀 1,358評論 0 2
  • [轉(zhuǎn)]淺談LLDB調(diào)試器文章來源于:http://www.cocoachina.com/ios/20150126/...
    loveobjc閱讀 2,498評論 2 6
  • 前言 今天花了一天的時間終于把iOS的幾種常見的調(diào)試方法給學習了一下,在這里給大家分享一下LLDB的使用检诗,同時也是...
    Peak_One閱讀 11,023評論 5 33
  • 轉(zhuǎn)載 與調(diào)試器共舞 - LLDB 的華爾茲: https://objccn.io/issue-19-2/ 推薦:i...
    F麥子閱讀 3,332評論 0 10
  • 不同于大年初一的陽光普照匈仗、初二云卷云舒,今兒(初三)寒風透骨把我從戶外壓回了室內(nèi)逢慌。這時刻多數(shù)人還在夢國悠轩,我隨...
    變臉加菲喵閱讀 413評論 0 3