mcpField
L
lldb

lldb

github.com/mohitmishra786开发269660 次安装18 周前更新
代码仓库

关于 lldb

为使用 LLDB 的 Agent 提供调试指导,并帮助熟悉 GDB 的开发者迁移命令。内容涵盖命令映射、Apple 平台调试、Python 脚本和 IDE 集成。

主要功能

  1. 01GDB 与 LLDB 命令对照
  2. 02断点、监视点与线程操作
  3. 03macOS 和 Objective-C 调试
  4. 04VS Code 集成配置
  5. 05LLDB Python 脚本示例

适用场景

  1. 01将 GDB 命令转换为 LLDB 用法
  2. 02在 macOS 上调试 C++ 程序
  3. 03配置 VS Code 的 LLDB 调试
  4. 04编写 LLDB Python 脚本

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill lldb

安装后在 Claude Code 中输入/lldb确认已加载。

附带文件references/gdb-lldb-map.md

详细介绍

LLDB

用途

指导 Agent 使用 LLDB 调试,并将现有的 GDB 知识映射到 LLDB。涵盖命令差异、Apple 特定事项、Python 脚本和 IDE 集成。

触发条件

  • “我在 macOS 上,需要调试一个 C++ 程序”
  • “LLDB 和 GDB 有什么区别?”
  • “如何在 LLDB 中执行 [GDB command]?”
  • “LLDB 对变量显示 <unavailable>”
  • “如何在 VS Code 中使用 LLDB?”
  • “如何编写 LLDB Python 脚本?”

工作流

1. 启动 LLDB
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
2. GDB → LLDB 命令对照表

来源:https://lldb.llvm.org/use/map.html

GDBLLDB备注
run [args]process launch [args] / r
continueprocess continue / c
nextthread step-over / n
stepthread step-in / s
nextithread step-inst-over / ni
stepithread step-inst / si
finishthread step-out / finish
break mainbreakpoint set -n main / b main
break file.c:42breakpoint set -f file.c -l 42 / b file.c:42
break *0x400abcbreakpoint set -a 0x400abc / b -a 0x400abc
watch xwatchpoint set variable x / wa s v x
print xframe variable x / p x
print/x xp/x x
info localsframe variable / fr v
info argsframe variable --arguments
backtracethread backtrace / bt
frame Nframe select N / f N
info threadsthread list
thread Nthread select N
thread apply all btthread backtrace all
x/10wx addrmemory read -s4 -fx -c10 addr / x/10xw addr
set var = 42expression var = 42 / expr var = 42
quitquit / q
3. 断点
# By name
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

# By address
b -a 0x100003f20

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

# List
breakpoint list / br l

# Delete
breakpoint delete 2

# Disable/enable
breakpoint disable 1
breakpoint enable 1

# Commands on hit
breakpoint command add 1
  > p x
  > continue
  > DONE
4. 检查状态
# Print variable
p x
frame variable x
p *ptr
p arr[0]

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

# All locals
frame variable
fr v -a          # include arguments

# Registers
register read
register read rip rsp

# Memory
memory read --size 4 --format x --count 10 0x7fff0000
x/10xw 0x7fff0000          # GDB-compatible syntax

# Type info
image lookup --type MyClass
type lookup MyClass
5. 观察点
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
6. 线程
thread list
thread select 3
thread backtrace all
thread backtrace --count 5           # limit depth

# Per-thread stepping
thread step-over                     # step this thread only
7. macOS / Apple 特定事项
# Symbol lookup in shared cache
image lookup --address 0x18ab12345
image lookup --name objc_msgSend

# Objective-C method breakpoint
b "-[NSArray objectAtIndex:]"
b "+[NSString stringWithFormat:]"

# Inspect Objective-C object
po myObject                          # print-object (calls -description)
po [arr count]

# Show loaded libraries
image list
image list -b                        # brief (names only)
8. VS Code 集成

安装 CodeLLDB 扩展。.vscode/launch.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 脚本
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。

相关技能

  • 使用 skills/debuggers/gdb 处理 GDB 工作流
  • 使用 skills/debuggers/core-dumps 分析 core dump
  • 使用 skills/compilers/clang 构建带有调试信息的程序

评论

支持 Markdown