コンテンツにスキップ

最終更新:2026-10-12

LLDB で C++ プログラムをデバッグ

本稿では、LLDB デバッガを使って簡単な C++ プログラムをデバッグする方法を紹介します。サンプルコードの名前は test.cpp とします。

#include <iostream>
#include <string>

struct MyStruct {
    int myInt;
    std::string myString;
};

int main() {
    MyStruct myStruct;
    myStruct.myInt = 42;
    myStruct.myString = "Hello, world!";

    int sum = 0;
    for (int i = 1; i <= 5; ++i) {
        sum += i;
    }

    return 0;
}

コンパイル

まず、-g フラグを付けてコードをコンパイルし、追加のデバッグ情報を有効にする必要があります。

clang++ -g test.cpp -o test

デバッグ開始

続いて、デバッガにアタッチします。

lldb test

ブレークポイント

ブレークポイントを main() 関数に設定します。

(lldb) b main

出力:

Breakpoint 1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3

実行

次に、プロセスを起動します。

(lldb) run

出力:

Process 11333 launched: '/Users/light/test' (x86_64)
Process 11333 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003be3 test`main at test.cpp:10:14
   7    };
   8    
   9    int main() {
-> 10       MyStruct myStruct;
   11       myStruct.myInt = 42;
   12       myStruct.myString = "Hello, world!";
   13  

ブレークポイントの確認

これで、main() 関数の先頭で停止しました。特定の行にブレークポイントを設定できます。

(lldb) b 16

出力:Breakpoint 2: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c

現在設定されているブレークポイントを一覧表示するには、次のコマンドを使用します。

(lldb) breakpoint list

出力:

(lldb) breakpoint list
Current breakpoints:
1: name = 'main', locations = 1, resolved = 1, hit count = 1
  1.1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3, resolved, hit count = 1 

2: file = '/Users/zhangxing/test.cpp', line = 16, exact_match = 0, locations = 1, resolved = 1, hit count = 0
  2.1: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c, resolved, hit count = 0 

プログラムの実行を続行

次のブレークポイントまでプログラムの実行を続行できます。

(lldb) c

出力:

Process 11823 resuming
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100003c1c test`main at test.cpp:16:16
   13   
   14       int sum = 0;
   15       for (int i = 1; i <= 5; ++i) {
-> 16           sum += i;
   17       }
   18   
   19       return 0;

変数の表示

現在のローカル変数の値を取得するには、次のコマンドが使えます:

(lldb) p myStruct

出力:

(MyStruct) $0 = (myInt = 42, myString = "Hello, world!")

ウォッチポイント

特定の変数の値の変更を監視するには、ウォッチポイントが使えます。

(lldb) watch set variable sum

出力:

Watchpoint created: Watchpoint 1: addr = 0x7ffeefbff1e8 size = 4 state = enabled type = w
    declare @ '/Users/zhangxing/test.cpp:14'
    watchpoint spec = 'sum'
    new value: 0

next を実行すると、Watchpoint の変化が見られます。

(lldb) n

Watchpoint 1 hit:
old value: 3
new value: 6
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
    frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
   12       myStruct.myString = "Hello, world!";
   13   
   14       int sum = 0;
-> 15       for (int i = 1; i <= 5; ++i) {
   16           sum += i;
   17       }
   18   

条件に基づいて実行を停止したい場合は、停止条件を設定できます。

(lldb) watch modify -c '(sum > 5)'

出力:

Watchpoint 1 hit:
new value: 6
Process 12848 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
    frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
   12       myStruct.myString = "Hello, world!";
   13   
   14       int sum = 0;
-> 15       for (int i = 1; i <= 5; ++i) {
   16           sum += i;
   17       }
   18   

デバッグが完了したら、プログラムの実行を続行できます。

(lldb) c

さらに詳しい情報については、LLDB のドキュメントを参照してください:


これらの手順を使えば、LLDB で C++ プログラムを手軽にデバッグし、バグを見つけて問題を解決できるようになります。

関連トピック

コメント