lldb

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LLDB

LLDB

Purpose

用途

Guide agents through LLDB sessions and map existing GDB knowledge to LLDB. Covers command differences, Apple specifics, Python scripting, and IDE integration.
引导用户完成LLDB调试会话,并将已有的GDB知识转换为LLDB的使用方法。涵盖命令差异、Apple平台专属特性、Python脚本编写以及IDE集成等内容。

Triggers

触发场景

  • "I'm on macOS and need to debug a C++ program"
  • "How does LLDB differ from GDB?"
  • "How do I do [GDB command] in LLDB?"
  • "LLDB shows
    <unavailable>
    for variables"
  • "How do I use LLDB in VS Code?"
  • "How do I write an LLDB Python script?"
  • "我在macOS上,需要调试一个C++程序"
  • "LLDB和GDB有什么区别?"
  • "如何在LLDB中执行[GDB命令]?"
  • "LLDB显示变量为
    <unavailable>
    "
  • "如何在VS Code中使用LLDB?"
  • "如何编写LLDB Python脚本?"

Workflow

工作流程

1. Start LLDB

1. 启动LLDB

bash
lldb ./prog                         # load binary
lldb ./prog -- arg1 arg2            # with arguments
lldb -p 12345                       # attach to PID
lldb -c core.1234                   # load core dump
lldb ./prog core.1234               # binary + core
bash
lldb ./prog                         # 加载二进制文件
lldb ./prog -- arg1 arg2            # 带启动参数
lldb -p 12345                       # 附加到指定PID的进程
lldb -c core.1234                   # 加载核心转储文件
lldb ./prog core.1234               # 加载二进制文件+核心转储文件

2. GDB → LLDB command map

2. GDB → LLDB命令映射

GDBLLDBNotes
run [args]
process launch [args]
/
r
continue
process continue
/
c
next
thread step-over
/
n
step
thread step-in
/
s
nexti
thread step-inst-over
/
ni
stepi
thread step-inst
/
si
finish
thread step-out
/
finish
break main
breakpoint set -n main
/
b main
break file.c:42
breakpoint set -f file.c -l 42
/
b file.c:42
break *0x400abc
breakpoint set -a 0x400abc
/
b -a 0x400abc
watch x
watchpoint set variable x
/
wa s v x
print x
frame variable x
/
p x
print/x x
p/x x
info locals
frame variable
/
fr v
info args
frame variable --arguments
backtrace
thread backtrace
/
bt
frame N
frame select N
/
f N
info threads
thread list
thread N
thread select N
thread apply all bt
thread backtrace all
x/10wx addr
memory read -s4 -fx -c10 addr
/
x/10xw addr
set var = 42
expression var = 42
/
expr var = 42
quit
quit
/
q
GDBLLDB说明
run [args]
process launch [args]
/
r
continue
process continue
/
c
next
thread step-over
/
n
step
thread step-in
/
s
nexti
thread step-inst-over
/
ni
stepi
thread step-inst
/
si
finish
thread step-out
/
finish
break main
breakpoint set -n main
/
b main
break file.c:42
breakpoint set -f file.c -l 42
/
b file.c:42
break *0x400abc
breakpoint set -a 0x400abc
/
b -a 0x400abc
watch x
watchpoint set variable x
/
wa s v x
print x
frame variable x
/
p x
print/x x
p/x x
info locals
frame variable
/
fr v
info args
frame variable --arguments
backtrace
thread backtrace
/
bt
frame N
frame select N
/
f N
info threads
thread list
thread N
thread select N
thread apply all bt
thread backtrace all
x/10wx addr
memory read -s4 -fx -c10 addr
/
x/10xw addr
set var = 42
expression var = 42
/
expr var = 42
quit
quit
/
q

3. Breakpoints

3. 断点

lldb
undefined
lldb
undefined

By name

按函数名设置

b main breakpoint set --name foo breakpoint set --name foo --condition 'x > 0'
b main breakpoint set --name foo breakpoint set --name foo --condition 'x > 0'

By file:line

按文件:行号设置

b file.c:42 breakpoint set --file file.c --line 42
b file.c:42 breakpoint set --file file.c --line 42

By address

按内存地址设置

b -a 0x100003f20
b -a 0x100003f20

By regex

按正则表达式设置

breakpoint set --func-regex '^MyClass::'
breakpoint set --func-regex '^MyClass::'

List

列出断点

breakpoint list / br l
breakpoint list / br l

Delete

删除断点

breakpoint delete 2
breakpoint delete 2

Disable/enable

禁用/启用断点

breakpoint disable 1 breakpoint enable 1
breakpoint disable 1 breakpoint enable 1

Commands on hit

断点命中时执行命令

breakpoint command add 1
p x continue DONE
undefined
breakpoint command add 1
p x continue DONE
undefined

4. Inspect state

4. 检查程序状态

lldb
undefined
lldb
undefined

Print variable

打印变量

p x frame variable x p *ptr p arr[0]
p x frame variable x p *ptr p arr[0]

Print expression

执行并打印表达式

expression x * 2 + 1 expr (int)sqrt(9.0)
expression x * 2 + 1 expr (int)sqrt(9.0)

All locals

打印所有局部变量

frame variable fr v -a # include arguments
frame variable fr v -a # 包含函数参数

Registers

查看寄存器

register read register read rip rsp
register read register read rip rsp

Memory

查看内存

memory read --size 4 --format x --count 10 0x7fff0000 x/10xw 0x7fff0000 # GDB-compatible syntax
memory read --size 4 --format x --count 10 0x7fff0000 x/10xw 0x7fff0000 # 兼容GDB的语法

Type info

查看类型信息

image lookup --type MyClass type lookup MyClass
undefined
image lookup --type MyClass type lookup MyClass
undefined

5. Watchpoints

5. 观察点

lldb
watchpoint set variable x           # write watchpoint
watchpoint set variable -w read x   # read watchpoint
watchpoint set variable -w read_write x
watchpoint set expression -- &x     # by address

watchpoint list
watchpoint delete 1
lldb
watchpoint set variable x           # 写观察点
watchpoint set variable -w read x   # 读观察点
watchpoint set variable -w read_write x
watchpoint set expression -- &x     # 按地址设置观察点

watchpoint list
watchpoint delete 1

6. Threads

6. 线程调试

lldb
thread list
thread select 3
thread backtrace all
thread backtrace --count 5           # limit depth
lldb
thread list
thread select 3
thread backtrace all
thread backtrace --count 5           # 限制回溯深度

Per-thread stepping

单线程步进

thread step-over # step this thread only
undefined
thread step-over # 仅当前线程步进
undefined

7. macOS / Apple specifics

7. macOS / Apple平台专属操作

lldb
undefined
lldb
undefined

Symbol lookup in shared cache

在共享缓存中查找符号

image lookup --address 0x18ab12345 image lookup --name objc_msgSend
image lookup --address 0x18ab12345 image lookup --name objc_msgSend

Objective-C method breakpoint

Objective-C方法断点

b "-[NSArray objectAtIndex:]" b "+[NSString stringWithFormat:]"
b "-[NSArray objectAtIndex:]" b "+[NSString stringWithFormat:]"

Inspect Objective-C object

检查Objective-C对象

po myObject # print-object (calls -description) po [arr count]
po myObject # print-object(调用-description方法) po [arr count]

Show loaded libraries

显示已加载的库

image list image list -b # brief (names only)
undefined
image list image list -b # 简洁模式(仅显示名称)
undefined

8. VS Code integration

8. VS Code集成

Install the
CodeLLDB
extension.
.vscode/launch.json
:
json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug (lldb)",
      "type": "lldb",
      "request": "launch",
      "program": "${workspaceFolder}/build/prog",
      "args": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "build"
    }
  ]
}
安装
CodeLLDB
扩展。配置文件
.vscode/launch.json
json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug (lldb)",
      "type": "lldb",
      "request": "launch",
      "program": "${workspaceFolder}/build/prog",
      "args": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "build"
    }
  ]
}

9. LLDB Python scripting

9. LLDB Python脚本编写

python
import lldb

def print_all_threads(debugger, command, result, internal_dict):
    target = debugger.GetSelectedTarget()
    process = target.GetProcess()
    for thread in process:
        print(f"Thread {thread.GetIndexID()}: {thread.GetName()}")
        for frame in thread:
            print(f"  {frame}")

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add -f myscript.print_all_threads pthreads')
Load:
command script import /path/to/myscript.py
For a full GDB↔LLDB command map, see references/gdb-lldb-map.md.
python
import lldb

def print_all_threads(debugger, command, result, internal_dict):
    target = debugger.GetSelectedTarget()
    process = target.GetProcess()
    for thread in process:
        print(f"Thread {thread.GetIndexID()}: {thread.GetName()}")
        for frame in thread:
            print(f"  {frame}")

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add -f myscript.print_all_threads pthreads')
加载脚本:
command script import /path/to/myscript.py
完整的GDB↔LLDB命令映射,请参考references/gdb-lldb-map.md

Related skills

相关技能

  • Use
    skills/debuggers/gdb
    for GDB workflows
  • Use
    skills/debuggers/core-dumps
    for core dump analysis
  • Use
    skills/compilers/clang
    for building with debug info
  • 若需GDB工作流程,请使用
    skills/debuggers/gdb
  • 若需核心转储分析,请使用
    skills/debuggers/core-dumps
  • 若需生成带调试信息的编译,请使用
    skills/compilers/clang