15. gdbの起動
gdbの
プロンプト
gdbのオプションに
実行バイナリa.outを指定
ホスト
コンテナ
$ docker compose exec mygcc gdb ./a.out
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
(gdb)
17. start
mainの最初の
実行行
start: デバッグを開始.main関数で一旦停止
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(gdb) start
Temporary breakpoint 1 at 0x401148: file main.c, line 13.
Starting program: /mnt/a.out
Temporary breakpoint 1, main () at main.c:13
13 int a[5] = {1, 2, 3, 4, 5};
(gdb)
18. l(小文字のエル)
l, list: 現在の停止行前後のコードを表示(list)
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(gdb) l
8 return z;
9 }
10
11 int main(void)
12 {
13 int a[5] = {1, 2, 3, 4, 5};
14 int b, c;
15
16 c = a[1] + a[5];
17
(gdb)
19. p, n
p variable: 変数の値を表示(print)
n: 次の実行行へ移動(next)
13行目をこれ
から実行する
からまだ初期
化されていな
い
13行目が実行
されたので初
期化された
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(gdb) p a
$1 = {4198832, 0, 4198464, 0, -5280}
(gdb)
(gdb) n
16 c = a[1] + a[5];
(gdb) p a
$2 = {1, 2, 3, 4, 5}
(gdb)
20. n, c
n: 次の実行行へ移動(next)
c: 実行を再開(continue)
関数はスキッ
プして次の行
へすすむ
問題なく
最後まで
実行が終了
main()のあとは
cで終了する
(nではエラーになる)
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(gdb) n
18 b = c + a[1];
(gdb) n
20 b = myfunc(b, c);
(gdb) n
22 printf("b=%d¥n", b);
(gdb) n
b=32773
24 return 0;
(gdb) n
25 }
(gdb) c
Continuing.
[Inferior 1 (process 68) exited normally]
(gdb)
21. n, s
n: 次の実行へ移動(next)
関数があっても1行進む(step over)
s: 次の実行へ移動(step)
関数があったら関数内へ進む(step in)
関数の中へ
進む
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13 int a[5] = {1, 2, 3, 4, 5};
(gdb) n
16 c = a[1] + a[5];
(gdb) n
18 b = c + a[1];
(gdb) n
20 b = myfunc(b, c);
(gdb) s
myfunc (x=32771, y=32769) at main.c:7
7 z = 2 * x - y;
(gdb) n
8 return z;
(gdb)
22. n, s
n: 次の実行へ移動(next)
関数があっても1行進む(step over)
s: 次の実行へ移動(step)
関数があったら関数内へ進む(step in)
関数から
戻ってくる
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
8 return z;
(gdb) n
9 }
(gdb) n
main () at main.c:22
22 printf("b=%d¥n", b);
(gdb) n
b=32773
24 return 0;
(gdb) n
25 }
(gdb) c
Continuing.
[Inferior 1 (process 72) exited normally]
(gdb)
23. where
where: 関数呼び出しスタックのどこにいるのかを表示
関数の中へ
進む
現在は
mainの中
現在はmainから呼び出された
myfuncの中
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13 int a[5] = {1, 2, 3, 4, 5};
(gdb) n
16 c = a[1] + a[5];
(gdb) n
18 b = c + a[1];
(gdb) where
#0 main () at main.c:18
(gdb) n
20 b = myfunc(b, c);
(gdb) s
myfunc (x=32771, y=32769) at main.c:7
7 z = 2 * x - y;
(gdb) where
#0 myfunc (x=32771, y=32769) at main.c:7
#1 0x0000000000401190 in main () at main.c:20
(gdb)
24. (gdb) start
Temporary breakpoint 5 at 0x401148: file main.c, line 13.
Starting program: /mnt/a.out
Temporary breakpoint 1, main () at main.c:13
13 int a[5] = {1, 2, 3, 4, 5};
(gdb) c
Continuing.
b=32773
[Inferior 1 (process 75) exited normally]
(gdb)
b: breakpoint
b: ブレークポイント(実行を中断する行)を設定
ブレークポイントが
ないと,continueで
最後まで実行される
start直後は
デフォルトで
main()に設定
されている
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
25. (gdb) i b
No breakpoints or watchpoints.
(gdb) b 20
Breakpoint 2 at 0x401181: file main.c, line 20.
(gdb) i b
Num Type Disp Enb Address What
2 breakpoint keep y 0x0000000000401181 in main at main.c:20
(gdb)
b: breakpoint
b: ブレークポイント(実行を中断する行)を設定
ブレークポイント
を20行目に設定
i b (info breakpoint)
設定されているブレークポイントをすべて表示
start後
ブレークポイント
で実行が中断
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13 int a[5] = {1, 2, 3, 4, 5};
(gdb) c
Continuing.
Breakpoint 2, main () at main.c:20
20 b = myfunc(b, c);
(gdb)
26. (gdb) i b
Num Type Disp Enb Address What
2 breakpoint keep y 0x0000000000401181 in main at main.c:20
breakpoint already hit 1 time
(gdb) d 2
(gdb) i b
No breakpoints or watchpoints.
(gdb)
d
d n: ブレークポイントnを削除(delete)
2番目のブレークポイントを削除
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ブレークポイント番号
27. 13 int a[5] = {1, 2, 3, 4, 5};
(gdb) watch b
Hardware watchpoint 5: b
(gdb) i b
Num Type Disp Enb Address What
5 hw watchpoint keep y b
(gdb) c
Continuing.
Hardware watchpoint 5: b
Old value = 0
New value = 32771
main () at main.c:20
20 b = myfunc(b, c);
(gdb)
watchpoint
ブレークポイント:実行を中断する行を設定
ウォッチポイント:値が変わったら実行を中断する変数を設定
変数bにウォッ
チポイントを
設定
ウォッチポイントに
なっている
実行を再
開すると
18行目でbの
値が変わる
ので
20行目の実
行前で停止
する
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
28. watchpoint
ブレークポイント:実行を中断する行を設定
ウォッチポイント:値が変わったら実行を中断する変数を設定
22行目で
停止
20行目でも
変わるので
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
20 b = myfunc(b, c);
(gdb) c
Continuing.
Hardware watchpoint 5: b
Old value = 32771
New value = 32773
main () at main.c:22
22 printf("b=%d¥n", b);
(gdb)
cで続けると
29. display, undisplay
display variable: 停止毎に値を表示する変数を設定
変数aを表示
する設定
変数bを表示
する設定
変数cを表示
する設定
停止したの
でa, b, cを表
示
停止したの
でa, b, cを表
示
undisplay variable: 表示を停止
#include <stdio.h>
int myfunc(int x, int y)
{
int z;
z = 2 * x - y;
return z;
}
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int b, c;
c = a[1] + a[5];
b = c + a[1];
b = myfunc(b, c);
printf("b=%d¥n", b);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13 int a[5] = {1, 2, 3, 4, 5};
(gdb) display a
1: a = {4198832, 0, 4198464, 0, -5280}
(gdb) display b
2: b = 0
(gdb) display c
3: c = 0
(gdb) n
16 c = a[1] + a[5];
1: a = {1, 2, 3, 4, 5}
2: b = 0
3: c = 0
(gdb) n
18 b = c + a[1];
1: a = {1, 2, 3, 4, 5}
2: b = 0
3: c = 32769
(gdb)