关于 axiom-lldb-ref
axiom-lldb-ref 是按任务整理的 Xcode LLDB 命令参考,适合需要快速查找命令而非调试流程的开发者,内容涵盖检查、断点、线程、表达式、进程、内存和故障排查。
主要功能
- 01列出变量检查命令
- 02涵盖断点与观察点操作
- 03收录线程和回溯导航
- 04说明 Swift 与 Objective-C 表达式
- 05记录进程、内存和映像命令
- 06提供 LLDB 故障排查指导
适用场景
- 01在 Xcode 调试时查询 LLDB 语法
- 02设置和管理断点或观察点
- 03检查线程、内存或已加载模块
- 04解决常见表达式与断点问题
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ charleswiltgen/ axiom --skill axiom-lldb-ref安装后在 Claude Code 中输入/axiom-lldb-ref确认已加载。
详细介绍
LLDB 命令参考
Xcode 中 LLDB 的完整命令参考。按任务整理,方便你找到所需的确切命令。
有关调试工作流和决策树,请参阅 /skill axiom-lldb。
第 1 部分:变量检查
v / frame variable
直接读取内存。无需编译。对 Swift 值最可靠。
(lldb) v # All variables in current frame
(lldb) v self # Self in current context
(lldb) v self.propertyName # Specific property
(lldb) v localVariable # Local variable
(lldb) v self.array[0] # Collection element
(lldb) v self._showDetails # SwiftUI @State backing store (underscore prefix)
标志:
| 标志 | 效果 |
|---|---|
-d run | 运行动态类型解析(速度较慢,但更准确) |
-T | 显示类型 |
-R | 显示原始(未格式化)输出 |
-D N | 将嵌套类型的深度限制为 N 层 |
-P N | 将指针深度限制为 N 层 |
-F | 扁平输出(无层级结构) |
限制: 无法计算表达式、计算属性或函数调用。此类情况请使用 p。
p / expression(带格式)
编译并执行表达式。显示格式化后的结果。
(lldb) p self.computedProperty
(lldb) p items.count
(lldb) p someFunction()
(lldb) p String(describing: someValue)
(lldb) p (1...10).map { $0 * 2 }
结果存储在编号变量中:
(lldb) p someValue
$R0 = 42
(lldb) p $R0 + 10
$R1 = 52
po / expression --object-description
对结果调用 debugDescription(或 description)。
(lldb) po myObject
(lldb) po error
(lldb) po notification.userInfo
(lldb) po NSHomeDirectory()
po 有用的情况: 实现了 CustomDebugStringConvertible 的类、NSError、NSNotification、对象集合。
po 失效的情况: 未实现 CustomDebugStringConvertible 的 Swift 结构体、协议类型的值(请改用 v——它会迭代执行动态类型解析,而 po 不会)。
expression(完整形式)
包含所有选项的完整表达式求值。
(lldb) expression self.view.backgroundColor = UIColor.red
(lldb) expression self.debugFlag = true
(lldb) expression myArray.append("test")
(lldb) expression CATransaction.flush() # Force UI update
(lldb) expression Self._printChanges() # SwiftUI debug
标志:
| 标志 | 效果 |
|---|---|
-l objc | 按 Objective-C 求值 |
-l swift | 按 Swift 求值(默认) |
-O | 对象描述(与 po 相同) |
-i false | 在求值期间命中断点时停止(默认:忽略) |
-- | 标志与表达式之间的分隔符 |
用于调试 Swift 的 ObjC 表达式:
(lldb) expr -l objc -- (void)[[[UIApplication sharedApplication] keyWindow] recursiveDescription]
(lldb) expr -l objc -- (void)[CATransaction flush]
(lldb) expr -l objc -- (int)[[UIApplication sharedApplication] _isForeground]
register read
低级寄存器检查:
(lldb) register read
(lldb) register read x0 x1 # Specific registers (ARM64)
(lldb) register read --all # All register sets
第 2 部分:断点
设置断点
(lldb) breakpoint set -f File.swift -l 42 # File + line
(lldb) b File.swift:42 # Short form
(lldb) breakpoint set -n methodName # By function name
(lldb) breakpoint set -n "MyClass.myMethod" # Qualified name
(lldb) breakpoint set -S layoutSubviews # ObjC selector
(lldb) breakpoint set -r "viewDid.*" # Regex on name
(lldb) breakpoint set -a 0x100abc123 # Memory address
条件断点
(lldb) breakpoint set -f File.swift -l 42 -c "value == nil"
(lldb) breakpoint set -f File.swift -l 42 -c "index > 100"
(lldb) breakpoint set -f File.swift -l 42 -c 'name == "test"'
忽略次数
(lldb) breakpoint set -f File.swift -l 42 -i 50 # Skip first 50 hits
单次断点
(lldb) breakpoint set -f File.swift -l 42 -o # Delete after first hit
断点命令(日志断点)
添加在命中断点时执行的命令:
(lldb) breakpoint command add 1
> v self.state
> p self.items.count
> continue
> DONE
也可以写在一行中:
(lldb) breakpoint command add 1 -o "v self.state"
异常断点
(lldb) breakpoint set -E swift # All Swift errors
(lldb) breakpoint set -E objc # All ObjC exceptions
# Filtering by exception name requires Xcode's GUI (Edit Breakpoint → Exception field)
符号断点
(lldb) breakpoint set -n UIViewAlertForUnsatisfiableConstraints # Auto Layout
(lldb) breakpoint set -n "-[UIApplication _run]" # App launch
(lldb) breakpoint set -n swift_willThrow # Swift throw
管理断点
(lldb) breakpoint list # List all
(lldb) breakpoint list -b # Brief format
(lldb) breakpoint enable 3 # Enable breakpoint 3
(lldb) breakpoint disable 3 # Disable breakpoint 3
(lldb) breakpoint delete 3 # Delete breakpoint 3
(lldb) breakpoint delete # Delete ALL (asks confirmation)
(lldb) breakpoint modify 3 -c "x > 10" # Add condition to existing
监视点
在变量的内存发生变化时中断:
(lldb) watchpoint set variable self.count # Watch for write
(lldb) watchpoint set variable -w read_write myGlobal # Watch for read or write
(lldb) watchpoint set expression -- &myVariable # Watch memory address
(lldb) watchpoint list # List all
(lldb) watchpoint delete 1 # Delete watchpoint 1
(lldb) watchpoint modify 1 -c "self.count > 10" # Add condition
注意: 硬件监视点数量有限(每个进程约 4 个)。请谨慎使用。
第 3 部分:线程与回溯
回溯
(lldb) bt # Current thread backtrace
(lldb) bt 10 # Limit to 10 frames
(lldb) bt all # All threads
(lldb) thread backtrace all # Same as bt all
线程导航
(lldb) thread list # List all threads with state
(lldb) thread info # Current thread details + stop reason
(lldb) thread select 3 # Switch to thread 3
栈帧导航
(lldb) frame info # Current frame details
(lldb) frame select 5 # Jump to frame 5
(lldb) up # Go up one frame (toward caller)
(lldb) down # Shortcut: go down one frame
线程返回(跳过代码)
强制从当前函数提前返回:
(lldb) thread return # Return void
(lldb) thread return 42 # Return specific value
请谨慎使用——这会跳过清理代码,可能导致状态不一致。
第 4 部分:表达式求值
Swift 表达式
(lldb) expr let x = 42; print(x)
(lldb) expr self.view.backgroundColor = UIColor.red
(lldb) expr UIApplication.shared.windows.first?.rootViewController
(lldb) expr UserDefaults.standard.set(true, forKey: "debug")
Objective-C 表达式
Swift 表达式解析器失效时切换到 ObjC:
(lldb) expr -l objc -- (void)[CATransaction flush]
(lldb) expr -l objc -- (id)[[UIApplication sharedApplication] keyWindow]
(lldb) expr -l objc -- (void)[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil]
UI 调试表达式
(lldb) expr -l objc -- (void)[[[UIApplication sharedApplication] keyWindow] recursiveDescription]
(lldb) po UIApplication.shared.windows.first?.rootViewController?.view.recursiveDescription()
SwiftUI 调试
(lldb) expr Self._printChanges() # Print what triggered body re-eval (inside view body only)
运行时类型信息
(lldb) expr type(of: someValue)
(lldb) expr String(describing: type(of: someValue))
第 5 部分:进程控制
执行控制
(lldb) continue # Resume execution (c)
(lldb) c # Short form
(lldb) process interrupt # Pause running process
(lldb) thread step-over # Step over (n / next)
(lldb) n # Short form
(lldb) thread step-in # Step into (s / step)
(lldb) s # Short form
(lldb) thread step-out # Step out (finish)
(lldb) finish # Short form
(lldb) thread step-inst # Step one instruction (assembly-level)
(lldb) ni # Step over one instruction
进程管理
(lldb) process launch # Launch/restart
(lldb) process attach --pid 1234 # Attach to running process
(lldb) process attach --name MyApp # Attach by name
(lldb) process detach # Detach without killing
(lldb) kill # Kill debugged process
第 6 部分:内存与映像
读取内存
(lldb) memory read 0x100abc123 # Read memory at address
(lldb) memory read -c 64 0x100abc123 # Read 64 bytes
(lldb) memory read -f x 0x100abc123 # Format as hex
(lldb) memory read -f s 0x100abc123 # Format as string
搜索内存
(lldb) memory find -s "searchString" -- 0x100000000 0x200000000
映像/模块检查
(lldb) image lookup -a 0x100abc123 # Lookup symbol at address
(lldb) image lookup -n myFunction # Find function by name
(lldb) image lookup -rn "MyClass.*" # Regex search
(lldb) image list # List all loaded images/frameworks
(lldb) image list -b # Brief format
常见用途: 查找崩溃地址所属的框架:
(lldb) image lookup -a 0x1a2b3c4d5
第 7 部分:.lldbinit 与自定义
文件位置
LLDB 启动时会读取 ~/.lldbinit。在 Xcode 的方案设置中配置后,也支持项目级初始化文件。
实用别名
添加到 ~/.lldbinit:
# Quick reload — flush UI changes made via expression
command alias flush expr -l objc -- (void)[CATransaction flush]
# Print view hierarchy
command alias views expr -l objc -- (void)[[[UIApplication sharedApplication] keyWindow] recursiveDescription]
# Print auto layout constraints
command alias constraints po [[UIWindow keyWindow] _autolayoutTrace]
自定义类型摘要
# Show CLLocationCoordinate2D as "lat, lon"
type summary add CLLocationCoordinate2D --summary-string "${var.latitude}, ${var.longitude}"
设置
(lldb) settings show target.language # Current language
(lldb) settings set target.language swift # Force Swift mode
(lldb) settings set target.max-children-count 100 # Show more collection items
项目级 .lldbinit
在 Xcode 中:Edit Scheme → Run → Options → “LLDB Init File” 字段。
在项目根目录的 .lldbinit 文件中添加项目专用的别名和断点。
第 8 部分:LLDB 本身的故障排除
“expression failed to parse”
原因: Swift 表达式解析器无法从当前模块解析类型。
解决方法:
- 改用
v(无需编译) - 简化表达式
- 对 Objective-C 桥接类型,尝试使用
expr -l objc -- ... - 清理派生数据并重新构建
“variable not available”
原因: 编译器优化掉了该变量。
解决方法:
- 切换到 Debug 构建配置
- 为指定文件设置
-Onone(Build Settings → 按文件设置编译器标志) - 使用
register read检查值是否存储在寄存器中
“wrong language mode”
原因: 在某些上下文中(尤其是在框架中),LLDB 默认使用 Objective-C。
解决方法:
(lldb) settings set target.language swift
(lldb) expr -l swift -- mySwiftExpression
“expression caused a crash”
原因: 计算的表达式产生了导致崩溃的副作用。
解决方法:
- 除非有意为之,否则不要计算会修改状态的表达式
- 使用
v进行只读检查 - 如果崩溃破坏了状态,请重新启动调试会话
LLDB 挂起或运行缓慢
原因: 通常是因为编译复杂表达式,或在大型项目中解析类型。
解决方法:
- 使用
v代替p/po(无需编译) - 降低表达式的复杂度
- 如果 LLDB 在执行
po时挂起,请按 Ctrl+C 取消,然后改用v
未命中断点
原因和解决方法:
| 原因 | 解决方法 |
|---|---|
| 文件或行号错误(代码已移动) | 在当前代码上重新设置断点 |
| 断点已禁用 | breakpoint enable N |
| 代码未执行 | 确认执行路径已到达该处 |
| 代码已被优化掉(Release) | 切换到 Debug 配置 |
| 位于框架/SPM 包中 | 按函数名称设置符号断点 |
资源
WWDC:2019-429、2018-412、2022-110370、2015-402
文档:/xcode/stepping-through-code-and-inspecting-variables-to-isolate-bugs、/xcode/setting-breakpoints-to-pause-your-running-app
技能:axiom-lldb、axiom-xcode-debugging