LLDB 调试工具介绍

发布时间: 更新时间: 总字数:2665 阅读时间:6m 作者:IP:上海 网址

LLDB 是下一代高性能调试器。它由一组可重用的组件组成,这些组件高度利用了更大的 LLVM 项目中的现有库,如 Clang 表达式解析器和 LLVM 反汇编器。

特点

  • 支持最新的 C、C++ 和 Objective-C 语言
  • 可声明局部变量和类型的多行表达式
  • 在支持的情况下,使用 JIT 处理表达式
  • 在无法使用 JIT 时,评估表达式的中间表示法 (IR)

安装

bash
apt install lldb -y  # python3-lldb-14  python3 库

help

lldb --help ...

格式

text
//命令 + 子命令 + 命令选项 + 命令参数
<command> [<subcommand> [<subcommand>...]] <action> [-options [option-value]] [argument [argument...]]

示例

lldb help ...

常用命令

breakpoint 代码调试断点

text
breakpoint set --file main.c --line 12
breakpoint set -f main.c -l 12

// 对于C函数,填写完整的函数名称。
breakpoint set --name main_func
breakpoint set -n main_func

# 删除断点
breakpoint delete 1
breakpoint delete

watchpoint 内存调试断点

text
watchpoint set variable value
watchpoint set variable self->_dataArray // 不能使用点语法

watchpoint list

watchpoint delete index
watchpoint delete

register 寄存器操作

text
register read

register read x1
register write x0 1

memory 内存操作

text
memory read/write

image 操作

text
# 列出进程空间加载的image
image list

// 查找类信息
image lookup --type UIViewController
image lookup -t UIViewController

// 查找符号
image lookup --name viewDidLoad
image lookup -n viewDidLoad

// 查找地址
image lookup -a  # 查看崩溃位置
image lookup --address 0x000000018ff60490
image lookup -a 0x000000018ff60490
导出image信息:

// 导出所有sections信息
image dump sections
// 导出所有符号信息
image dump symtab

thread 线程

text
thread list

// 切换 thread
thread select 1

// 获取线程调用栈
// 获取当前线程调用栈
thread backtrace
bt

// 获取所有线程调用栈
thread backtrace all

在Xcode中,有四个控制Debug的按钮:
- Continue:程序继续运行
- thread continue/continue/c
- Step over:单步指定,遇到子函数时断点不会进入子函数。
p thread step-over/next/n
- Step into:单步运行,遇到子函数时断点会进入子函数。
- thread step-into/step/s

frame 栈帧信息

text
frame variable

disassemble 显示汇编代码

使用 di/dis 同等效果。

text
disassemble -b
disassemble --frame
disassemble --name func_name
disassemble -a address
disassemble -s address

示例

text
lldb -c minidump.md --batch -o "image list"
lldb --local-lldbinit

参考

  1. https://lldb.llvm.org/