• [iOS]LLDB调试


    官方API文档https://lldb.llvm.org/python_reference/index.html

    LLDB是一个有着REPL(read-eval-print-loop 交互式)的特性和C++、Phtyon插件的开源调试器,是Xcode工程中默认的调试器。

    一、LLDB语法

    [ [...]] [-options [option-value]] [argument [argument...]]

    命令LLDB调试命令的名称
    子命令

    子命令是组织相关操作的分隔标识。

    一个命令的最终子命令将是一个动词表面将要执行的动作。

    操作在上下文中执行命令的操作
    命令选项一个命令当中可能包含一个或者多个命令选项,命令选项使用双虚线(--)开始,用于不同的组合来修改执行的动作。有一些命令也使用了单虚线 (-)开始进行操作。
    命令的参数

    一个命令可能要求一个或者多个参数,参数是动作执行的分隔标识。

    []表示命令是可选的,可以有也可以没有。

    命令和子命令按层级结构来排列。一个命令对象为跟随其的子命令对象创建一个上下文,子命令又为其子命令创建一个上下文,循序往下。

    注意:
    元素之间通过空格来分割,如果某一元素自身含有空格,则可以使用双引用。如果双引号包含的元素中又包含双引号,则里面的双引号可以改成单引号,也可以在里面的双引号前加反斜杠转义。

    例:给方法fetchData设置断点

    (lldb) breakpoint set -n fetchData
    commandbreakpoint 表示断点命令
    actionset 表示设置断点
    option-n 是--name的缩写,表示根据方法名称设置断点。
    arguementfetchData 表示方法名为fetchData

     例:给方法fetchData设置一个只生效一次的断点

    (lldb) breakpoint set -o true -n fetchData
    commandbreakpoint 表示断点命令
    actionset 表示设置断点
    option

    -o 是--one-shot的缩写,在第一次触发的时候删除。

    -n 是--name的缩写,表示根据方法名称设置断点。

    arguementfetchData 表示方法名为fetchData

    二、LLDB命令的格式

    LLDB命令可以用多种格式书写

    格式命令(打印变量)
    Canonical form(标准样式)expression --object-description -- someVariable
    Abbreviated form(缩写样式)e -O -- someVariable
    Alias(别名样式)po someVariable

    三、LLDB常用调试命令

    1. help

    1. // 查看LLDB所有调试器命令
    2. (lldb) help
    3. // 查看有关任何命令的
    4. // 例:(lldb) help breakpoint
    5. (lldb) help
    6. // 查看有关任何特定子命令的更多帮助
    7. // 例:(lldb) help breakpoint set
    8. (lldb) help

    2. breakpoint

    breakpoint简写为br

    1. // 给函数设置断点,
    2. // breakpoint可简写为br
    3. // 例:(lldb) breakpoint set -n fetchData
    4. (lldb) br set -n 函数名
    5. // 调试指定动态库里的函数
    6. // 例:(lldb) br set -s libafc.tbd -r est
    7. (lldb) br set -s 动态库名 匹配参数
    8. // 查看断点列表和序号
    9. (lldb) br list
    10. // 删除断点
    11. // 例:(lldb) br delete 12
    12. (lldb) br delete 断点序号
    13. // 断点失效
    14. // 例:(lldb) br disable 12
    15. (lldb) br disable 断点序号
    16. // 断点生效
    17. // 例:(lldb) br enable 12
    18. (lldb) br enable 断点序号
    19. // 给断点添加命令
    20. // 输入指令,直到输入大写的DONE结束。执行到这个断点的时候自动执行下面命令。
    21. // 例:
    22. // Enter your debugger command(s). Type 'DONE' to end.
    23. // > frame variable value1
    24. // > DONE
    25. // (lldb)
    26. (lldb) breakpoint command add 序号
    27. // 查看断点命令列表
    28. // 例:
    29. // (lldb) breakpoint command list 7
    30. // Breakpoint 7:
    31. // Breakpoint commands:
    32. // frame variable value1
    33. // (lldb)
    34. (lldb) breakpoint command list 序号

    3. frame

    frame简写为fr

    1. // 查看当前堆栈帧的所有变量
    2. (lldb) frame variable
    3. // 打印指定变量
    4. // 例:(lldb) frame variable params
    5. (lldb) frame variable 变量名
    6. // 打印出堆栈地址
    7. // 例:(lldb) frame variable -L params
    8. (lldb) frame variable -L 变量名

    4. expression

    expression简写为ex

    1. // 表达式,对当前线程上的表达式求值。使用LLDB的默认格式显示任何返回值。
    2. // expression可简写为ex或expr
    3. // 例:(lldb) expression value1 + value2
    4. // 例:(lldb) ex 100 * 1234
    5. (lldb) expression 表达式

    5. thread

    thread简写为th

    1. // 显示线程调用堆栈
    2. // 默认为当前线程 all:查看所有线程 unique:查看按unique调用堆栈分组的线程
    3. // 例:(lldb) thread backtrace all
    4. (lldb) thread backtrace
    5. // 过早地从堆栈帧返回,缩短新帧的执行,并可选地产生指定的值。默认为退出当前堆栈帧。
    6. // thread return [-x] -- []
    7. // thread return []
    8. // -x(——from-expression) 从最里面的表达式求值返回。
    9. // 例:(lldb)
    10. (lldb) thread return
    11. // 继续执行当前目标流程。可以指定一个或多个线程,默认情况下继续所有线程。
    12. // 语法:thread continue [[…]]
    13. (lldb) thread continue
    14. // 缩写成continue或c
    15. (lldb) c
    16. // 单步入,如果有函数进入函数
    17. (lldb) thread step-in
    18. // 缩写成step或者s
    19. (lldb) s
    20. // 单步过,如果有函数不进入函数
    21. (lldb) thread step-over
    22. // 缩写成next或者n
    23. (lldb) n
    24. // 单步,直接返回到函数结束
    25. (lldb) thread step-out
    26. // 缩写成finish
    27. (lldb) finish
    28. // 汇编代码,单步入
    29. (lldb) thread step-inst
    30. // 汇编代码,单步过
    31. (lldb) thread step-inst-over

    6. watchpoint

    watchpoint简写为wa

    1. // 通过变量名添加内存断点
    2. (lldb) watchpoint set variable 变量名
    3. // 通过内存地址添加内存断点
    4. (lldb) watchpoint set expression 内存地址
    5. // 启用指定的禁用内存断点,如果没有指定则启用所有内存断点。
    6. // 语法:watchpoint enable []
    7. (lldb) watchpoint enable
    8. // 禁用指定的内存断点,而不删除它们,如果没有指定则禁用所有内存断点。
    9. // 语法:watchpoint disable []
    10. (lldb) watchpoint disable
    11. // 删除指定的内存断点,如果没有指定则将它们全部删除。
    12. // 语法:watchpoint delete []
    13. (lldb) watchpoint delete

    7. image

    1. // 列出所有模块
    2. (lldb) image list
    3. // 在一个或多个目标模块的调试符号中按名称查找类型
    4. // 例:(lldb) image lookup -t SearchVC
    5. (lldb) image lookup -t 类型名称
    6. // 在一个或多个目标模块中查找地址和所在行数
    7. // 例:(lldb) image lookup -a testAction
    8. // 例:(lldb) image lookup -a 0x000000015acec504
    9. (lldb) image lookup -a 函数名或函数地址
    10. // 查找函数名所在位置
    11. // 例:(lldb) image lookup -n testAction
    12. (lldb) image lookup -n 函数名

    8. register

    1. // 读取寄存器,如果不写寄存器名字就列出全部寄存器。
    2. // 例:(lldb) register read x30
    3. // lr = 0x000000010514877c hdjlm`hdjlm.HDSearchVC.testAction() -> Swift.Float + 260 [inlined] generic specialization of Swift._allocateUninitializedArray<τ_0_0>(Builtin.Word) -> (Swift.Array<τ_0_0>, Builtin.RawPointer) + 12 at
    4. (lldb) register read 寄存器名字
    5. // 缩写成 re re 寄存器名字
    6. (lldb) re re 寄存器名字
    7. // 写入寄存器
    8. // 例:(lldb) register write x29 0x10
    9. (lldb) register write 寄存器名字 数值
    10. // 缩写成re wr
    11. (lldb) re wr 寄存器名字 数值

    9. memory

    1. // 查看地址内存值
    2. // 例:(lldb) memory read 0x18819f81c
    3. // 0x18819f81c: fd 7b 01 a9 fd 43 00 91 f3 03 00 aa c8 53 2e b0 .{...C.......S..
    4. // 0x18819f82c: 03 a5 42 f9 08 00 00 f0 08 11 1b 91 7f 00 08 eb ..B.............
    5. (lldb) memory read 地址值
    6. // 缩写成x
    7. (lldb) x 地址值

    四、LLDB所有调试器命令

    (lldb) help
    1. Debugger commands:
    2. apropos -- List debugger commands related to a word or subject.
    3. breakpoint -- Commands for operating on breakpoints (see 'help b' for
    4. shorthand.)
    5. command -- Commands for managing custom LLDB commands.
    6. disassemble -- Disassemble specified instructions in the current
    7. target. Defaults to the current function for the
    8. current thread and stack frame.
    9. expression -- Evaluate an expression on the current thread. Displays
    10. any returned value with LLDB's default formatting.
    11. frame -- Commands for selecting and examing the current thread's
    12. stack frames.
    13. gdb-remote -- Connect to a process via remote GDB server.
    14. If no host is specifed, localhost is assumed.
    15. gdb-remote is an abbreviation for 'process connect
    16. --plugin gdb-remote connect://:'
    17. gui -- Switch into the curses based GUI mode.
    18. help -- Show a list of all debugger commands, or give details
    19. about a specific command.
    20. kdp-remote -- Connect to a process via remote KDP server.
    21. If no UDP port is specified, port 41139 is
    22. assumed.
    23. kdp-remote is an abbreviation for 'process connect
    24. --plugin kdp-remote udp://:'
    25. language -- Commands specific to a source language.
    26. log -- Commands controlling LLDB internal logging.
    27. memory -- Commands for operating on memory in the current target
    28. process.
    29. platform -- Commands to manage and create platforms.
    30. plugin -- Commands for managing LLDB plugins.
    31. process -- Commands for interacting with processes on the current
    32. platform.
    33. quit -- Quit the LLDB debugger.
    34. register -- Commands to access registers for the current thread and
    35. stack frame.
    36. reproducer -- Commands for manipulating reproducers. Reproducers make
    37. it possible to capture full debug sessions with all its
    38. dependencies. The resulting reproducer is used to replay
    39. the debug session while debugging the debugger.
    40. Because reproducers need the whole the debug session
    41. from beginning to end, you need to launch the debugger
    42. in capture or replay mode, commonly though the command
    43. line driver.
    44. Reproducers are unrelated record-replay debugging, as
    45. you cannot interact with the debugger during replay.
    46. script -- Invoke the script interpreter with provided code and
    47. display any results. Start the interactive interpreter
    48. if no code is supplied.
    49. session -- Commands controlling LLDB session.
    50. settings -- Commands for managing LLDB settings.
    51. source -- Commands for examining source code described by debug
    52. information for the current target process.
    53. statistics -- Print statistics about a debugging session
    54. swift-healthcheck -- Provides logging related to the Swift expression
    55. evaluator, including Swift compiler diagnostics. This
    56. makes it easier to identify project misconfigurations
    57. that result in module import failures in the debugger.
    58. The command is meant to be run after a expression
    59. evaluator failure has occurred.
    60. target -- Commands for operating on debugger targets.
    61. thread -- Commands for operating on one or more threads in the
    62. current process.
    63. trace -- Commands for loading and using processor trace
    64. information.
    65. type -- Commands for operating on the type system.
    66. version -- Show the LLDB debugger version.
    67. watchpoint -- Commands for operating on watchpoints.
    68. Current command abbreviations (type 'help command alias' for more info):
    69. add-dsym -- Add a debug symbol file to one of the target's current modules
    70. by specifying a path to a debug symbols file or by using the
    71. options to specify a module.
    72. attach -- Attach to process by ID or name.
    73. b -- Set a breakpoint using one of several shorthand formats.
    74. bt -- Show the current thread's call stack. Any numeric argument
    75. displays at most that many frames. The argument 'all' displays
    76. all threads.
    77. c -- Continue execution of all threads in the current process.
    78. call -- Evaluate an expression on the current thread. Displays any
    79. returned value with LLDB's default formatting.
    80. continue -- Continue execution of all threads in the current process.
    81. detach -- Detach from the current target process.
    82. di -- Disassemble specified instructions in the current target.
    83. Defaults to the current function for the current thread and
    84. stack frame.
    85. dis -- Disassemble specified instructions in the current target.
    86. Defaults to the current function for the current thread and
    87. stack frame.
    88. display -- Evaluate an expression at every stop (see 'help target
    89. stop-hook'.)
    90. down -- Select a newer stack frame. Defaults to moving one frame, a
    91. numeric argument can specify an arbitrary number.
    92. env -- Shorthand for viewing and setting environment variables.
    93. exit -- Quit the LLDB debugger.
    94. f -- Select the current stack frame by index from within the current
    95. thread (see 'thread backtrace'.)
    96. file -- Create a target using the argument as the main executable.
    97. finish -- Finish executing the current stack frame and stop after
    98. returning. Defaults to current thread unless specified.
    99. history -- Dump the history of commands in this session.
    100. Commands in the history list can be run again using "!".
    101. "!-" will re-run the command that is commands
    102. from the end of the list (counting the current command).
    103. image -- Commands for accessing information for one or more target
    104. modules.
    105. j -- Set the program counter to a new address.
    106. jump -- Set the program counter to a new address.
    107. kill -- Terminate the current target process.
    108. l -- List relevant source code using one of several shorthand formats.
    109. list -- List relevant source code using one of several shorthand formats.
    110. n -- Source level single step, stepping over calls. Defaults to
    111. current thread unless specified.
    112. next -- Source level single step, stepping over calls. Defaults to
    113. current thread unless specified.
    114. nexti -- Instruction level single step, stepping over calls. Defaults to
    115. current thread unless specified.
    116. ni -- Instruction level single step, stepping over calls. Defaults to
    117. current thread unless specified.
    118. p -- Evaluate an expression on the current thread. Displays any
    119. returned value with LLDB's default formatting.
    120. parray -- parray -- lldb will evaluate EXPRESSION to
    121. get a typed-pointer-to-an-array in memory, and will display
    122. COUNT elements of that type from the array.
    123. po -- Evaluate an expression on the current thread. Displays any
    124. returned value with formatting controlled by the type's author.
    125. poarray -- poarray -- lldb will evaluate EXPRESSION to
    126. get the address of an array of COUNT objects in memory, and will
    127. call po on them.
    128. print -- Evaluate an expression on the current thread. Displays any
    129. returned value with LLDB's default formatting.
    130. q -- Quit the LLDB debugger.
    131. r -- Launch the executable in the debugger.
    132. rbreak -- Sets a breakpoint or set of breakpoints in the executable.
    133. re -- Commands to access registers for the current thread and stack
    134. frame.
    135. repl -- Evaluate an expression on the current thread. Displays any
    136. returned value with LLDB's default formatting.
    137. run -- Launch the executable in the debugger.
    138. s -- Source level single step, stepping into calls. Defaults to
    139. current thread unless specified.
    140. shell -- Run a shell command on the host.
    141. si -- Instruction level single step, stepping into calls. Defaults to
    142. current thread unless specified.
    143. sif -- Step through the current block, stopping if you step directly
    144. into a function whose name matches the TargetFunctionName.
    145. step -- Source level single step, stepping into calls. Defaults to
    146. current thread unless specified.
    147. stepi -- Instruction level single step, stepping into calls. Defaults to
    148. current thread unless specified.
    149. t -- Change the currently selected thread.
    150. tbreak -- Set a one-shot breakpoint using one of several shorthand formats.
    151. undisplay -- Stop displaying expression at every stop (specified by stop-hook
    152. index.)
    153. up -- Select an older stack frame. Defaults to moving one frame, a
    154. numeric argument can specify an arbitrary number.
    155. v -- Show variables for the current stack frame. Defaults to all
    156. arguments and local variables in scope. Names of argument,
    157. local, file static and file global variables can be specified.
    158. Children of aggregate variables can be specified such as
    159. 'var->child.x'. The -> and [] operators in 'frame variable' do
    160. not invoke operator overloads if they exist, but directly access
    161. the specified element. If you want to trigger operator
    162. overloads use the expression command to print the variable
    163. instead.
    164. It is worth noting that except for overloaded operators, when
    165. printing local variables 'expr local_var' and 'frame var
    166. local_var' produce the same results. However, 'frame variable'
    167. is more efficient, since it uses debug information and memory
    168. reads directly, rather than parsing and evaluating an
    169. expression, which may even involve JITing and running code in
    170. the target program.
    171. var -- Show variables for the current stack frame. Defaults to all
    172. arguments and local variables in scope. Names of argument,
    173. local, file static and file global variables can be specified.
    174. Children of aggregate variables can be specified such as
    175. 'var->child.x'. The -> and [] operators in 'frame variable' do
    176. not invoke operator overloads if they exist, but directly access
    177. the specified element. If you want to trigger operator
    178. overloads use the expression command to print the variable
    179. instead.
    180. It is worth noting that except for overloaded operators, when
    181. printing local variables 'expr local_var' and 'frame var
    182. local_var' produce the same results. However, 'frame variable'
    183. is more efficient, since it uses debug information and memory
    184. reads directly, rather than parsing and evaluating an
    185. expression, which may even involve JITing and running code in
    186. the target program.
    187. vo -- Show variables for the current stack frame. Defaults to all
    188. arguments and local variables in scope. Names of argument,
    189. local, file static and file global variables can be specified.
    190. Children of aggregate variables can be specified such as
    191. 'var->child.x'. The -> and [] operators in 'frame variable' do
    192. not invoke operator overloads if they exist, but directly access
    193. the specified element. If you want to trigger operator
    194. overloads use the expression command to print the variable
    195. instead.
    196. It is worth noting that except for overloaded operators, when
    197. printing local variables 'expr local_var' and 'frame var
    198. local_var' produce the same results. However, 'frame variable'
    199. is more efficient, since it uses debug information and memory
    200. reads directly, rather than parsing and evaluating an
    201. expression, which may even involve JITing and running code in
    202. the target program.
    203. x -- Read from the memory of the current target process.
    204. For more information on any command, type 'help '.

    五、查询有关任何命令的更多信息

    有关任何命令的更多信息,请键入'help '。

    例:查看breakpoint命令的更多信息

    (lldb) help breakpoint
    1. Commands for operating on breakpoints (see 'help b' for shorthand.)
    2. Syntax: breakpoint []
    3. The following subcommands are supported:
    4. clear -- Delete or disable breakpoints matching the specified source
    5. file and line.
    6. command -- Commands for adding, removing and listing LLDB commands
    7. executed when a breakpoint is hit.
    8. delete -- Delete the specified breakpoint(s). If no breakpoints are
    9. specified, delete them all.
    10. disable -- Disable the specified breakpoint(s) without deleting them. If
    11. none are specified, disable all breakpoints.
    12. enable -- Enable the specified disabled breakpoint(s). If no breakpoints
    13. are specified, enable all of them.
    14. list -- List some or all breakpoints at configurable levels of detail.
    15. modify -- Modify the options on a breakpoint or set of breakpoints in
    16. the executable. If no breakpoint is specified, acts on the
    17. last created breakpoint. With the exception of -e, -d and -i,
    18. passing an empty argument clears the modification.
    19. name -- Commands to manage name tags for breakpoints
    20. read -- Read and set the breakpoints previously saved to a file with
    21. "breakpoint write".
    22. set -- Sets a breakpoint or set of breakpoints in the executable.
    23. write -- Write the breakpoints listed to a file that can be read in
    24. with "breakpoint read". If given no arguments, writes all
    25. breakpoints.
    26. For more help on any particular subcommand, type 'help '.

    六、查询有关任何特定子命令的更多信息

    有关任何特定子命令的更多信息,请键入'help '。

    例:查看breakpoint命令下delete子命令的更多帮助

    (lldb) help breakpoint delete
    1. Delete the specified breakpoint(s). If no breakpoints are specified, delete
    2. them all.
    3. Syntax: breakpoint delete [id | breakpt-id-list>]
    4. Command Options Usage:
    5. breakpoint delete [-Ddf] [id | breakpt-id-list>]
    6. -D ( --dummy-breakpoints )
    7. Delete Dummy breakpoints - i.e. breakpoints set before a file is
    8. provided, which prime new targets.
    9. -d ( --disabled )
    10. Delete all breakpoints which are currently disabled. When using
    11. the disabled option any breakpoints listed on the command line are
    12. EXCLUDED from deletion.
    13. -f ( --force )
    14. Delete all breakpoints without querying for confirmation.
    15. This command takes options and free-form arguments. If your arguments
    16. resemble option specifiers (i.e., they start with a - or --), you must use
    17. ' -- ' between the end of the command options and the beginning of the
    18. arguments.

    参考

    LLDB之理解LLDB基本命令语法

    iOS开发调试 - LLDB使用概览

    ios lldb调试指令汇总(持续更新)

  • 相关阅读:
    Ubuntu install vncserver
    Spring Batch 中的 chunk
    基于STM32的温控风扇
    【Java设计模式】二、单例模式
    centos7中卸载Java、jdk命令
    Kotlin高仿微信-第5篇-主页-通讯录
    docker 安装本地starrocks测试环境
    单片机的变量竟然被无故修改了.
    文件恢复软件哪个最好用?数据恢复软件,推荐这几款
    Linux:多线程概念 | Windows的线程 | 线程的优缺点 | 进程与线程 | 线程控制 | 线程创建 | 线程终止 | 线程等待 | 分离线程
  • 原文地址:https://blog.csdn.net/u012881779/article/details/127952055