//バージョン確認
lldb --version
//起動(実行ファイルを指定)
lldb ./main
//実行
run
//LLDBを終了
quit
//ブレークポイント設定(main関数の先頭で止める)
breakpoint set --name main
//ブレークポイント設定の短縮系。上記のブレークポイント設定と同義
b main
//ブレークポイント設定済みのリスト
breakpoint list
//次の行に進める(関数の中には入らない)
next
//次の行に進める(関数の中に入る)
step
//現在の関数を最後まで実行
finish
//次のブレークポイントまで実行
continue
//変数の値を見る(変数名を指定)
print p
//プリントコマンドの短縮系(ポインタをデリファレンスして値を見る)
p *p
//アドレスのメモリを確認
memory read p
//上記コマンドの表示するバイトを指定(ここではpが指すアドレスのメモリを表示)
memory read --format x --size 4 --count 1 p
//上記コマンドの短縮系
memory read -fx -s4 -c1 p
//現在のスタックフレームを確認
frame info
//呼び出し履歴を確認
bt
test@test lldb-tutorial % lldb ./main
(lldb) target create "./main"
Current executable set to '/Users/test/Desktop/lldb-tutorial/main' (x86_64).
(lldb) run
Process 60563 launched: '/Users/test/Desktop/lldb-tutorial/main' (x86_64)
a = 100
p = 100
Process 60563 exited with status = 0 (0x00000000)
(lldb)
run コマンドで実行することができます。Process 60563 exited with status = 0 (0x00000000) でstatus が0ですが、これは正常終了を意味しています。
次にブレークポイントを設定します。
(lldb) breakpoint set --name main
Breakpoint 1: where = main`main + 15 at main.c:4:9, address = 0x0000000100003f3f
(lldb) run
Process 66455 launched: '/Users/test/Desktop/lldb-tutorial/main' (x86_64)
Process 66455 stopped
* thread #1, stop reason = breakpoint 1.1
frame #0: 0x0000000100003f3f main`main at main.c:4:9
1 #include <stdio.h>
2
3 int main() {
-> 4 int a = 100;
5 int *p = &a;
6
7 printf("a = %d\n", a);
Target 0: (main) stopped.
(lldb)
ブレークポイントを設定することで、main関数の先頭で止めることができました。
ステップ実行
ブレークポイントを設定したので、ステップ実行をします。
(lldb) next
Process 66455 stopped
* thread #1, stop reason = step over
frame #0: 0x0000000100003f46 main`main at main.c:5:10
2
3 int main() {
4 int a = 100;
-> 5 int *p = &a;
6
7 printf("a = %d\n", a);
8 printf("p = %d\n", *p);
Target 0: (main) stopped.
(lldb)
LLDBには複数のステップ実行方法があります。まずは next を使って、1行ずつプログラムを実行してみます。
(lldb) next
Process 66455 stopped
* thread #1, stop reason = step over
frame #0: 0x0000000100003f4e main`main at main.c:7:24
4 int a = 100;
5 int *p = &a;
6
-> 7 printf("a = %d\n", a);
8 printf("p = %d\n", *p);
9
10 return 0;
Target 0: (main) stopped.
(lldb) next
a = 100
Process 66455 stopped
* thread #1, stop reason = step over
frame #0: 0x0000000100003f5f main`main at main.c:8:25
5 int *p = &a;
6
7 printf("a = %d\n", a);
-> 8 printf("p = %d\n", *p);
9
10 return 0;
11 }
Target 0: (main) stopped.
(lldb)
next は関数呼び出しがあっても、その関数の内部には入らず、現在のフレームで次のソース行まで実行します。ブレークポイントに設定した箇所から、関数の中もデバッグする場合は、step コマンドを使います。プログラムをちょっと修正します。
#include <stdio.h>
static void hello() {
printf("Hello World!\n");
}
int main() {
int a = 100;
int *p = &a;
printf("a = %d\n", a);
printf("p = %d\n", *p);
return 0;
}
(lldb) step
Process 86667 stopped
* thread #1, stop reason = step in
frame #0: 0x0000000100003f1e main`main at main.c:12:5
9 int a = 100;
10 int *p = &a;
11
-> 12 hello();
13
14 printf("a = %d\n", a);
15 printf("p = %d\n", *p);
Target 0: (main) stopped.
(lldb) step
Process 86667 stopped
* thread #1, stop reason = step in
frame #0: 0x0000000100003f64 main`hello at main.c:5:5
2
3
4 static void hello() {
-> 5 printf("Hello World!\n");
6 }
7
8 int main() {
Target 0: (main) stopped.
(lldb)
test@test goemon-src % lldb -- ./kama-e examples/study.gb
(lldb) target create "./kama-e"
Current executable set to '/Users/test/Desktop/goemon-src/kama-e' (x86_64).
(lldb) settings set -- target.run-args "examples/study.gb"
(lldb) run
Process 87030 launched: '/Users/test/Desktop/goemon-src/kama-e' (x86_64)
VM Output: 0
VM Output: 1
VM Output: 1
VM Output: 2
VM Output: 3
VM Output: 5
VM Output: 8
VM Output: 13
VM Output: 21
VM Output: 34
VM Output: 55
VM Output: 89
VM Output: 144
VM Output: 233
VM Output: 377
VM Output: 610
VM Output: 987
VM Output: 1597
VM Output: 2584
VM Output: 4181
Process 87030 exited with status = 0 (0x00000000)
(lldb)
実際にブレークポイントを設定して、VM内部の動きをデバッグします。
(lldb) breakpoint set --name run
...省略
(lldb) next
Process 97453 stopped
* thread #1, stop reason = step over
frame #0: 0x0000000100003285 kama-e`run(program=0x00007fd2bda04080) at kama_execute.c:25:9 [opt]
22 while (true) {
23 int instruction = program[pc++];
24
-> 25 switch (instruction) {
26 case OP_PUSH:
27 stack[++sp] = program[pc++];
28 if (sp >= 255) {
Target 0: (kama-e) stopped.
(lldb) step
Process 97453 stopped
* thread #1, stop reason = step in
frame #0: 0x0000000100003290 kama-e`run(program=0x00007fd2bda04080) at kama_execute.c:27:41 [opt]
24
25 switch (instruction) {
26 case OP_PUSH:
-> 27 stack[++sp] = program[pc++];
28 if (sp >= 255) {
29 printf("限界突破!スタックが溢れましたぞ! (pc=%d)\n", pc);
30 return;
Target 0: (kama-e) stopped.
(lldb) step
Process 97453 stopped
* thread #1, stop reason = step in
frame #0: 0x00000001000032ab kama-e`run(program=0x00007fd2bda04080) at kama_execute.c:28:24 [opt]
25 switch (instruction) {
26 case OP_PUSH:
27 stack[++sp] = program[pc++];
-> 28 if (sp >= 255) {
29 printf("限界突破!スタックが溢れましたぞ! (pc=%d)\n", pc);
30 return;
31 }
Target 0: (kama-e) stopped.
(lldb) print stack[0]
(int) $1 = 0
(lldb) print pc
(int) $3 = 38
(lldb) print sp
(int) $4 = 0
(lldb)
コメント