watch

変数値の変化を追跡できるとうれしいかもしれない.
変数値の追跡

変数値の追跡

ソースの何行目かはわからないが,とにかくある変数の値がいつ参照されたり書き込まれたかを示すことができる.以下にまずソースを示す.

1: #include
2: #include

4: int main(void)
5: {
6: int i = 0;
7: int j = 0;

9: i++;
10: j++;
11: j = i;
12: j++;
13: i–;

15: fprintf(stdout,”i = %d\n”,i);

17: exit(EXIT_SUCCESS);
18: }

変数iへの書き込みを追跡するには,watchを使用する.
$ gdb a.out 
(gdb) break main
Breakpoint 1 at 0x8048456: file watch.c, line 6.
(gdb) run
Starting program: /home/hoge/programing/c/learn-gdb/watch/a.out 

Breakpoint 1, main () at watch.c:6
6         int i = 0;
(gdb) delete 
Delete all breakpoints? (y or n) y
(gdb) watch i
Hardware watchpoint 2: i
(gdb) c
Continuing.
Hardware watchpoint 2: i

Old value = -1073744200
New value = 0
main () at watch.c:7
7         int j = 0;
(gdb) 
Continuing.
Hardware watchpoint 2: i

Old value = 0
New value = 1
main () at watch.c:10
10        j++;
(gdb) 
Continuing.
Hardware watchpoint 2: i

Old value = 1
New value = 0
main () at watch.c:15
15        fprintf(stdout,"i = %d\n",i);
(gdb) 
Continuing.
i = 0

Program exited normally.
このようにiへの書き込みを行なう6,9,13行目の実行後にそれぞれ停止する.iを読み込んだときを追跡するにはrwatchを使用する.
$ gdb a.out 
(gdb) break main
Breakpoint 1 at 0x8048456: file watch.c, line 6.
(gdb) run
Starting program: /home/hoge/programing/c/learn-gdb/watch/a.out 

Breakpoint 1, main () at watch.c:6
6         int i = 0;
(gdb) delete 
Delete all breakpoints? (y or n) y
(gdb) rwatch i
Hardware read watchpoint 2: i
(gdb) c
Continuing.
Hardware read watchpoint 2: i

Value = 1
0x0804846d in main () at watch.c:11
11        j = i;
(gdb) 
Continuing.
Hardware read watchpoint 2: i

Value = 0
0x0804847c in main () at watch.c:15
15        fprintf(stdout,"i = %d\n",i);
(gdb) 
Continuing.
i = 0

Program exited normally.
そして変数の読み込みと書き込みを追跡するにはawatchを使用する.
$ gdb a.out 
(gdb) break main
Breakpoint 1 at 0x8048456: file watch.c, line 6.
(gdb) run
Starting program: /home/hoge/programing/c/learn-gdb/watch/a.out 

Breakpoint 1, main () at watch.c:6
6         int i = 0;
(gdb) delete 
Delete all breakpoints? (y or n) y
(gdb) awatch i
Hardware access (read/write) watchpoint 2: i
(gdb) c
Continuing.
Hardware access (read/write) watchpoint 2: i

Old value = -1073744200
New value = 0
main () at watch.c:7
7         int j = 0;
(gdb) 
Continuing.
Hardware access (read/write) watchpoint 2: i

Old value = 0
New value = 1
main () at watch.c:10
10        j++;
(gdb) 
Continuing.
Hardware access (read/write) watchpoint 2: i

Value = 1
0x0804846d in main () at watch.c:11
11        j = i;
(gdb) 
Continuing.
Hardware access (read/write) watchpoint 2: i

Old value = 1
New value = 0
main () at watch.c:15
15        fprintf(stdout,"i = %d\n",i);
(gdb) 
Continuing.
Hardware access (read/write) watchpoint 2: i

Value = 0
0x0804847c in main () at watch.c:15
15        fprintf(stdout,"i = %d\n",i);
(gdb) 
Continuing.
i = 0

Program exited normally.

This article was written by Fujiko