# Xcode 編譯器除錯命令(所有)

語歌發表於2019-03-03

bk
bk

之前使用編譯器除錯的時候,每次只是用常規的幾個除錯命令。但是本著折騰的原則,今天把 所有的除錯命令 及功能都羅列出來。

語歌 部落格

速覽表在最後:

下面舉例常見比較重要的命令:

再下面有更詳細的示範

如果想要了解更多編譯器除錯的命令: 傳送門

接下來看一下常用的除錯命令用法:

1.apropos

列出與某個單詞或主題相關的偵錯程式命令。
e.g
(lldb) apropos po

appropos
appropos

2.breakpoint

看截圖文件:
(lldb) breakpoint

breakPoint
breakPoint

可以簡寫為 br
功能非常強大,下面還有詳細的描述

3.breakpoint

重要

4.print

(lldb) print sums 可以簡寫成 (lldb) p sums
既: print 寫成 p

程式碼中這樣:
var sums = ["0.00","0.00","0.00","0.00"]複製程式碼

除錯視窗這樣:
結果:

(lldb) print sums
([String]) $R14 = 4 values {
  [0] = "0.00"
  [1] = "0.00"
  [2] = "0.00"
  [3] = "0.00"
}複製程式碼

如果你想在命令列列印進位制數:

輸入引數 表示進位制 (e.g)
p/x 66 (x表示16進位制)(Int) $R17 = 0x0000000000000042
p/t 6 (t表示2進位制)(Int) $R20 = 0b0000000000000000000000000000000000000000000000000000000000000110
p/c “s” (c表示字元)(String) $R24 = “s”

5.expression

直接改變其值,點選繼續執行,執行的結果就是本次賦值後的結果

(lldb) expression sums = ["10.00","0.00","0.00","0.00"]複製程式碼

示例:

expression
expression

更多的用法:
以物件的方式來列印:
expression -o -- sums可以直接簡寫成這樣: e -o -- sums

其中:
e -o -- sums 可以寫成 po ,而且作用是等效的。

process

與程式互動的命令,當然是配合其後面的引數來達到相應的目的 執行 (lldb) process help 如下:

process
process

舉個常見栗子:
continue -- Continue execution of all threads in the current process.

就是繼續執行程式,當遇到斷點的時候,在 LLDB 中執行就是繼續執行程式

thread

與程式互動的命令,當然是配合其後面的引數來達到相應的目的 執行 (lldb) thread help 如下:

thread
thread

其搭配的引數命令執行的作用後面描繪的相當清楚。
這裡要重點介紹幾個:

* **`(lldb) thread return`** 過早的從堆疊中返回,立即執行返回命令,退出當前堆疊。可以偽裝一些返回資訊等等。從寫一些函式的行為等等。複製程式碼

frame

同樣是配合其引數完成除錯

frame
frame

常用的一條命令:
lldb) frame info
列印出當前: 工程名字類名字函式名字所在的行數
其它的作用參照引數後面的解釋

看完上面的命令,接下來看編譯器除錯的幾個 常用按鈕

lldb
lldb

由圖中可以看出用於除錯的 4 個按鈕

  1. 第一個 continue 如遇到如圖所示,就點選後程式就正常執行,如果有其它斷點,就會跳到下一個斷點.

    ps: 點選它與在 LLDB除錯框 裡面輸入
    (lldb) process continue 作用是一樣的。
    c 作用效果也是一樣的

  2. 第二個 step over 當遇到一個斷點暫停後,點選該按鈕程式就會一行一行的執行,即使遇到了函式的呼叫也不會進入函式裡面去,而是直接跳過這個函式的執行,如下圖:

    stepOve
    stepOve

    115 行打了一個斷點,然後點選該按鈕,他會執行 116 行,再點選後會執行 117 行,而不會去執行 116 所呼叫的函式 裡面的行
    ps: 在程式當中與該按鈕作用相同的 LLDB 命令引數是一樣的命令是:
    (lldb) n
    (lldb) next
    (lldb) thread step-over
    作用效果是一樣的

  3. 第三個step into.它才是真正意義上的一行一行的執行命令,即使遇到函式的執行,也會跳 該函式裡面去一行一行的執行程式碼。就是說你想進入函式裡面的時候用它
    ps: 在程式當中與該按鈕作用相同的 LLDB 命令引數是一樣的命令是:
    (lldb) thread step-in
    (lldb) step
    (lldb) s

  4. 第四個 step out 如果你進入了一個函式,執行一兩行之後你想跳過該函式就用這個按鈕。其實它的執行就是一個 堆疊的結束。

快速檢視 Xcode 的所有斷點

如圖這是通過點選檢視工程檔案中所有的斷點

everyBreak
everyBreak

那麼通過 LLDB 命令來檢視所有的斷點:
(lldb) br list 或者 (lldb) br li 也可以達到相同的目的

在偵錯程式中通過 LLDB 快速建立斷點

使用下面的命令完成了 115行 斷點的設定
(lldb) breakpoint set -f ViewController.swift -l 115
這個時候我們執行 continue 按鈕會發現跳到 115行斷點了。

我們通過大列表檢視 b 其介紹是:
Set a breakpoint using one of several shorthand formats.

設定斷點的命令是:
(lldb) b ViewController.swift:127127 處設定了斷點

Xcode UI 畫面上有條件的執行 斷點

如圖:

UIdebug
UIdebug

由圖可看:

1步:我們在 line 24 的地方打了一個斷點。

2步:我們看到標 2 的框框,這裡 i==2 表示當 i等於2的時候才會執行這個斷點

3步:我們看到標 3 的框框,這裡表示當執行這個斷點的時候,LLDB 會執行 po i 的命令

4步:我們看到標 4 的框框,i為2 的時候執行了斷點的列印操作

其中 ignore 表示該斷點第幾次才會真正執行,比如 設定 ignore2 那麼該斷點會在第三次呼叫的時候觸發。

那麼這裡要說明的就是:斷點程式會先 比較 函式執行到該斷點的 次數。然後 再比較條件 ,條件滿足後 執行 LLDB 命令 語句

其中的 號可以支援多個 LLDB 命令。

其他的斷點條件及執行的命令,依次類推。

Action 後面的更多作用!

如圖:

action
action

1.AppleScript

蘋果的一種指令碼語言,可以在此開始執行

2.Capture GPU Frame

Unity遊戲 方面的除錯。暫時沒有研究 ?

3.Debugger Command

相當於在 LLDB 上直接使用命令

4.Log Message

log
log

當執行到該斷點的時候 LLDB 欄中會直接列印這個 hello 的資訊

5.Shell Command

如圖:

say
say

當執行該斷點的時候,電腦會讀 Hello world

6.Sound

選擇相應的聲音遇到該斷點會發出相應的聲音,也是挺有意思的。

一些 LLDB 及控制檯外掛,配合外掛及指令碼開發將大大提高開發效率。

chisel
Rainbow

隨便打個斷點:
命令列輸入: (lldb) help

快速查詢所以的命令 一覽表

命令 命令作用描述
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.(建立某個特點作用域的bug 命令)
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.
kdp-remote — Connect to a process via remote KDP server. If no UDP port is specified, port 41139 is assumed.
language — Commands specific to a source language.
log — Commands controlling LLDB internal logging.
memory — Commands for operating on memory in the current target process.
platform — Commands to manage and create platforms.
plugin — Commands for managing LLDB plugins.
process — Commands for interacting with processes on the current platform.(配合其包含的命令繼續執行 執行 process help 即可看到)
quit — Quit the LLDB debugger.
register — Commands to access registers for the current thread and stack frame.
script — Invoke the script interpreter with provided code and display any results. Start the interactive interpreter if no code is supplied.
settings — Commands for managing LLDB settings.
source — Commands for examining source code described by debug information for the current target process.
target — Commands for operating on debugger targets.
thread — Commands for operating on one or more threads in the current process.(在當前程式中操作一個或多個執行緒的命令,結合其下面的引數進行。下面有其搭配引數詳細說明)
type — Commands for operating on the type system.
version — Show the LLDB debugger version.(檢視開發語言的版本)
watchpoint — Commands for operating on watchpoints.
add-dsym — Add a debug symbol file to one of the target`s current modules by specifying a path to a debug symbols file, or using the options to specify a module to download symbols for.
attach — Attach to process by ID or name.
b — Set a breakpoint using one of several shorthand formats.
bt — Show the current thread`s call stack. Any numeric argument displays at most that many frames. The argument `all` displays all threads.
c — Continue execution of all threads in the current process.
call — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
continue — Continue execution of all threads in the current process.
detach — Detach from the current target process.
di — Disassemble specified instructions in the current target. Defaults to the current function for the current thread and stack frame.
dis — Disassemble specified instructions in the current target. Defaults to the current function for the current thread and stack frame.
display — Evaluate an expression at every stop (see `help target stop-hook`.)
down — Select a newer stack frame. Defaults to moving one frame, a numeric argument can specify an arbitrary number.
env — Shorthand for viewing and setting environment variables.
exit — Quit the LLDB debugger.
f — Select the current stack frame by index from within the current thread (see `thread backtrace`.)
file — Create a target using the argument as the main executable. finish — Finish executing the current stack frame and stop after returning. Defaults to current thread unless specified.
image — Commands for accessing information for one or more target modules.
j — Set the program counter to a new address.
jump — Set the program counter to a new address.
kill — Terminate the current target process.
l — List relevant source code using one of several shorthand formats.
list — List relevant source code using one of several shorthand formats.
n — Source level single step, stepping over calls. Defaults to current thread unless specified.(相當於一行一行的執行函式)
next — Source level single step, stepping over calls. Defaults to current thread unless specified.(與 n 的作用幾乎一致)
nexti — Instruction level single step, stepping over calls. Defaults to current thread unless specified.
ni — Instruction level single step, stepping over calls. Defaults to current thread unless specified.
p — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.(可以列印程式中相關引數的值,其屬性狀態)
parray — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.(與 p 相同)
po — Evaluate an expression on the current thread. Displays any returned value with formatting controlled by the type`s author.(與 p 的區別是列印的值所帶的引數相對簡潔一點)
poarray — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.(與 p 相同)
print — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.(與 p 相同)
q — Quit the LLDB debugger.
r — Launch the executable in the debugger.
rbreak — Sets a breakpoint or set of breakpoints in the executable.
repl — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
reveal_load_dev — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
reveal_load_sim — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
reveal_start — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
reveal_stop — Evaluate an expression on the current thread. Displays any returned value with LLDB`s default formatting.
run — Launch the executable in the debugger.
s — Source level single step, stepping into calls. Defaults to current thread unless specified.(一步一步執行,即使遇到函式也會進入該函式一步一步執行程式碼)
si — Instruction level single step, stepping into calls. Defaults to current thread unless specified.
sif — Step through the current block, stopping if you step directly into a function whose name matches the TargetFunctionName.
step — Source level single step, stepping into calls. Defaults to current thread unless specified.
stepi — Instruction level single step, stepping into calls. Defaults to current thread unless specified.
t — Change the currently selected thread.
tbreak — Set a one-shot breakpoint using one of several shorthand formats.
undisplay — Stop displaying expression at every stop (specified by stop-hook index.)
up — Select an older stack frame. Defaults to moving one frame, a numeric argument can specify an arbitrary number.
x — Read from the memory of the current target process.

相關文章