mcpField
L
lldb

lldb

github.com/mohitmishra786開発269660 回インストール18 週間前に更新
リポジトリ

lldb について

LLDB を使うエージェント向けのデバッグガイドです。GDB とのコマンド対応、Apple 固有のデバッグ、Python スクリプト、IDE 連携を扱います。

主な機能

  1. 01GDB と LLDB のコマンド対応
  2. 02ブレークポイント、監視点、スレッド操作
  3. 03macOS と Objective-C のデバッグ
  4. 04VS Code の設定ガイド
  5. 05LLDB Python スクリプトの例

活用シーン

  1. 01GDB コマンドを LLDB 用に置き換える
  2. 02macOS で C++ プログラムをデバッグする
  3. 03VS Code で LLDB を設定する
  4. 04LLDB Python スクリプトを作成する

インストール

コマンドをコピーしてターミナルで実行します。

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

インストール後、Claude Code で/lldbを入力して読み込まれたか確認します。

同梱ファイルreferences/gdb-lldb-map.md

詳細

LLDB

目的

LLDB セッションを通じてエージェントをガイドし、既存の GDB に関する知識を LLDB に対応付けます。コマンドの違い、Apple 固有の情報、Python スクリプト、IDE との統合について説明します。

トリガー

  • 「macOS を使っています。C++ プログラムをデバッグする必要があります」
  • 「LLDB は GDB とどう違いますか?」
  • 「LLDB で[GDB コマンド]を実行するにはどうすればよいですか?」
  • 「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 を参照してください。

関連スキル

  • GDB のワークフローには skills/debuggers/gdb を使用します
  • コアダンプの分析には skills/debuggers/core-dumps を使用します
  • デバッグ情報付きでビルドするには skills/compilers/clang を使用します

コメント