LLDB的學(xué)習(xí)流水賬(一)

LLDB的語法:

<command> [<subcommand> [<subcommand>...]] <action> [-options [option-value]] [argument [argument...]]

  1. <command>(命令)和<subcommand>(子命令):LLDB調(diào)試命令的名稱。命令和子命令按層級結(jié)構(gòu)來排列:一個命令對象為跟隨其的子命令對象創(chuàng)建一個上下文,子命令又為其子命令創(chuàng)建一個上下文,依此類推。
  2. <action>:執(zhí)行命令的操作
  3. <options>:命令選項(xiàng)
  4. <arguement>:命令的參數(shù)
  5. []:表示命令是可選的,可以有也可以沒有

如何使用命令選項(xiàng)?

命令選項(xiàng)可以選擇全稱拼寫,也可以縮寫,示例:

breakpoint set
   -M <method> ( --method <method> )

   -S <selector> ( --selector <selector> )

   -b <function-name> ( --basename <function-name> )

   -f <filename> ( --file <filename> )

   -l <linenum> ( --line <linenum> )

   -n <function-name> ( --name <function-name> )

使用LLDB幫助

help指令: help\help -a

LLDB作為單獨(dú)調(diào)試器的使用方法

需要掌握的方法:
1、加載需要調(diào)試的進(jìn)程
2、把一個正在運(yùn)行的進(jìn)程加入到LLDB中
3、設(shè)置斷點(diǎn)和觀察點(diǎn)
4、控制進(jìn)程的執(zhí)行
5、控制被調(diào)試進(jìn)程的運(yùn)行
6、觀測變量的狀態(tài)和值
7、執(zhí)行你想執(zhí)行的邏輯分支

加載需要調(diào)試的進(jìn)程:

$ lldb /Projects/Sketch/build/Debug/Sketch.app

設(shè)置斷點(diǎn)

(lldb) breakpoint set --selector alignLeftEdges:
breakpoint list可用于查看當(dāng)前加入的斷點(diǎn)

設(shè)置觀察點(diǎn)(可使用help watchpoint來查看該命令的幫助)

Watchpoint created: Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
   declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
(lldb) watch modify -c '(global==5)'
(lldb) watch list
Current watchpoints:
Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
    declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
    condition = '(global==5)'
(lldb) c
Process 15562 resuming
(lldb) about to write to 'global'...
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped and was programmatically restarted.
Process 15562 stopped
* thread #1: tid = 0x1c03, 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16, stop reason = watchpoint 1
    frame #0: 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16
   13
   14      static void modify(int32_t &var) {
   15          ++var;
-> 16      }
   17
   18      int main(int argc, char** argv) {
   19          int local = 0;
(lldb) bt
* thread #1: tid = 0x1c03, 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16, stop reason = watchpoint 1
    frame #0: 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16
    frame #1: 0x0000000100000eac a.out`main + 108 at main.cpp:25
    frame #2: 0x00007fff8ac9c7e1 libdyld.dylib`start + 1
(lldb) frame var global
(int32_t) global = 5
(lldb) watch list -v
Current watchpoints:
Watchpoint 1: addr = 0x100001018 size = 4 state = enabled type = w
    declare @ '/Volumes/data/lldb/svn/ToT/test/functionalities/watchpoint/watchpoint_commands/condition/main.cpp:12'
    condition = '(global==5)'
    hw_index = 0  hit_count = 5     ignore_count = 0
(lldb)

載入程序到LLDB中

對于未運(yùn)行的程序,使用如下命令

(lldb) process launch
(lldb) run
(lldb) r

對于已經(jīng)在運(yùn)行的程序,使用如下命令把進(jìn)程附到LLDB

(lldb) process attach --pid 123
(lldb) process attach --name Sketch
(lldb) process attach --name Sketch --waitfor

控制程序的運(yùn)行

(lldb) thread continue
Resuming thread 0x2c03 in process 46915
Resuming process 46915
(lldb)
(lldb) thread step-in // The same as "step" or "s" in GDB.
(lldb) thread step-over // The same as "next" or "n" in GDB.
(lldb) thread step-out // The same as "finish" or "f" in GDB.
(lldb) thread step-inst // The same as "stepi" / "si" in GDB.
(lldb) thread step-over-inst // The same as "nexti" / "ni" in GDB.
(lldb) thread until 100
(lldb) process continue
(lldb) breakpoint set --name stop_here

檢查進(jìn)程狀態(tài)

(lldb) thread list
Process 46915 state is Stopped
* thread #1: tid = 0x2c03, 0x00007fff85cac76a, where = libSystem.B.dylib`__getdirentries64 + 10, stop reason = signal = SIGSTOP, queue = com.apple.main-thread
  thread #2: tid = 0x2e03, 0x00007fff85cbb08a, where = libSystem.B.dylib`kevent + 10, queue = com.apple.libdispatch-manager
  thread #3: tid = 0x2f03, 0x00007fff85cbbeaa, where = libSystem.B.dylib`__workq_kernreturn + 10
(lldb) thread backtrace
thread #1: tid = 0x2c03, stop reason = breakpoint 1.1, queue = com.apple.main-thread
 frame #0: 0x0000000100010d5b, where = Sketch`-[SKTGraphicView alignLeftEdges:] + 33 at /Projects/Sketch/SKTGraphicView.m:1405
 frame #1: 0x00007fff8602d152, where = AppKit`-[NSApplication sendAction:to:from:] + 95
 frame #2: 0x00007fff860516be, where = AppKit`-[NSMenuItem _corePerformAction] + 365
 frame #3: 0x00007fff86051428, where = AppKit`-[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 121
 frame #4: 0x00007fff860370c1, where = AppKit`-[NSMenu performKeyEquivalent:] + 272
 frame #5: 0x00007fff86035e69, where = AppKit`-[NSApplication _handleKeyEquivalent:] + 559
 frame #6: 0x00007fff85f06aa1, where = AppKit`-[NSApplication sendEvent:] + 3630
 frame #7: 0x00007fff85e9d922, where = AppKit`-[NSApplication run] + 474
 frame #8: 0x00007fff85e965f8, where = AppKit`NSApplicationMain + 364
 frame #9: 0x0000000100015ae3, where = Sketch`main + 33 at /Projects/Sketch/SKTMain.m:11
 frame #10: 0x0000000100000f20, where = Sketch`start + 52
(lldb) thread backtrace all
(lldb) thread select 2

檢查堆棧的狀態(tài)

(lldb) frame variable
(lldb) frame variable self
(lldb) frame variable self.isa

執(zhí)行自己想要執(zhí)行的代碼

(lldb) expr (int) printf ("I have a pointer 0x%llx.\n", self)
?著作權(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)容

  • [轉(zhuǎn)]淺談LLDB調(diào)試器文章來源于:http://www.cocoachina.com/ios/20150126/...
    loveobjc閱讀 2,728評論 2 6
  • 隨著Xcode 5的發(fā)布,LLDB調(diào)試器已經(jīng)取代了GDB,成為了Xcode工程中默認(rèn)的調(diào)試器。它與LLVM編譯器一...
    隨風(fēng)飄蕩的小逗逼閱讀 1,459評論 0 0
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    木易林1閱讀 1,033評論 0 4
  • 與調(diào)試器共舞 - LLDB 的華爾茲 nangege 2014/12/19 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試...
    McDan閱讀 940評論 0 0
  • 你是否曾經(jīng)苦惱于理解你的代碼,而去嘗試打印一個變量的值? NSLog(@"%@", whatIsInsideThi...
    我是啊梁閱讀 886評論 1 1

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