AIにLLDBを操作してもらう
このエントリは言語実装 - Qiita Advent Calendar 2025 - Qiitaの24日目のエントリです。
たまにはZennにも投稿しようかなということで。16日目の記事でも書いたように最近LLDBでのデバッグをしていたのですが、マニュアルにこんなページがあることに気づきました。
要はClaude CodeなどのAIにMCP経由でLLDBを操作してもらえるということですね。ということでちょっとだけ触ってみました。
セットアップ
手元にはLLVM-18を入れていたのですが、この機能はLLVM 21で入ったため、lldb-21が必要でした。LLVM全体をアップグレードしなくても、lldbだけ21のを入れれば大丈夫だと思います。(Ubuntuだと https://apt.llvm.org/ のおかげでこういうことがやりやすいですね)
まずlldbを起動し、以下のコマンドでMCPサーバを開始します。
(lldb) protocol-server start MCP listen://localhost:59999
次にAI側の設定をします。例えばClaude Codeだと以下のようになります。ncコマンドでポート59999につないでね、という指示ですね。
$ claude mcp add lldb --transport stdio --scope project -- /usr/bin/nc localhost 59999
使ってみる
とりあえず、うまく接続できてそうか確認します。
> installed lldb mcp. what can you do with it?
● I can now use LLDB (the LLVM debugger) directly through the mcp__lldb__lldb_command tool. This is particularly useful for your Rust compiler project. Here's what I can help with:
Debugging Capabilities:
- Set breakpoints in the Shiika compiler or compiled programs
- Step through code execution (step, next, continue)
- Inspect variables, memory, and registers
- Examine stack traces and backtraces
- Analyze crashes and segmentation faults
- Debug optimized LLVM IR code
Practical use cases for Shiika:
1. Debugging the compiler - Set breakpoints in Rust code to understand compilation stages
2. Analyzing codegen issues - Inspect LLVM IR generation and execution
3. Investigating runtime crashes - Like the SEGV mentioned in your a.dump.txt file
4. Understanding async runtime behavior - Debug the tokio-based async experiment
Example workflows:
# I could help you:
- Attach to a running process
- Load a core dump and analyze crashes
- Set conditional breakpoints at specific compiler stages
- Inspect vtable structures or async continuations
- Trace method calls through the runtime
...
よさそうですね。
ただ、一度claudeを終了するとlldb mcpのことを忘れてしまったりします。> check that lldb mcp is availableとかで思い出させてあげるといいかもしれません。
ということでSEGVする実行ファイルを食わせてみましょう。lldbならtarget create a.out; runとかですが、claudeからは自然言語で命令することになりますね。
> run a.out via lldb mcp
● I'll run a.out using the lldb MCP. Let me start by creating a target and then running it.
● lldb - lldb_command (MCP)(debugger_id: 0, arguments: "target create a.out")
⎿ Error: MCP error -32603: no debugger with id 0
● lldb - lldb_command (MCP)(debugger_id: 1, arguments: "target create a.out")
⎿ Current executable set to '/home/yhara/shiika/a.out' (x86_64).
● Perfect! The target is set. Now let me run the program:
● lldb - lldb_command (MCP)(debugger_id: 1, arguments: "run")
⎿ Process 28996 launched: '/home/yhara/shiika/a.out' (x86_64)
● The program has been launched successfully! Let me check its status and any output:
● lldb - lldb_command (MCP)(debugger_id: 1, arguments: "process status")
⎿ Process 28996 stopped
* thread #1, name = 'a.out', stop reason = signal SIGABRT
frame #0: 0x00007ffff7d159fc libc.so.6`__pthread_kill_implementation(no_tid=0, signo=6, threadid=140737350458752) at pthread_kill.c
:44:76
● The program stopped with a SIGABRT signal (abort signal). Let me get a backtrace to see what led to the crash:
● lldb - lldb_command (MCP)(debugger_id: 1, arguments: "bt")
⎿ * thread #1, name = 'a.out', stop reason = signal SIGABRT
* frame #0: 0x00007ffff7d159fc libc.so.6`__pthread_kill_implementation(no_tid=0, signo=6, threadid=140737350458752) at pthread_kill.c
:44:76
… +88 lines (ctrl+o to expand)
● Perfect! I can see the crash is happening in shiika_insert_wtable at wtable.rs:11. The backtrace shows:
...
SIGABRTが出てるのを見て、btでバックトレースを表示して、デバッグを始めてくれました。
気になるのは最初にdebugger_id: 0でやろうとしてエラーになってることですね。debugger_idは1から始まることをCLAUDE.mdあたりで教えておいてやるとよさそうです(あるいは自分で追記してもらうか)
おわりに
このあとですが、今回はSEGV位置だけでバグの原因がわかってしまったので、bt以外のコマンドが必要になることはありませんでした。
それだけであればMCPを介さずとも、出力をコピペしてやるだけですむのですが、もっと複雑なバグ、例えばbreakpointやwatchpointを駆使するようなデバッグであれば、AIが直接lldbとやりとりできる価値がでてきそうです。
Discussion