11.3 シンボリックデバッガ(gdb)によるデバッグ

  5.プログラム例2のデバッグ(2)

$ gdb a.out
GNU gdb 4.17.0.11 with Linux support
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) break 11 if odd > 10   
                            #whileループ内の11行で、奇数値oddが10を超えた場合のみ停止
                              するように設定する。
Breakpoint 1 at 0x80483f3: file whike.c, line 11.
(gdb) run
Starting program: /home/miyao/Soft2/a.out 

Breakpoint 1, main () at while.c:11
11 odd = odd+2; /* 次の奇数へ */
(gdb) print odd
$1 = 11                 #oddの値を表示させると、確かに10を超える数値となっている。そ
                              こで、whileループの条件を確かめると、常に真になることに気づく。
(gdb) cont   #次のブレイクポイントまで処理を続行する。
Continuing.

Breakpoint 1, main () at while.c:11
11 odd = odd+2;       /* 次の奇数へ */
(gdb) print odd
$2 = 13                 #oddの値は2ずつ増加することが確かめられる。
(gdb) quit
The program is running. Exit anyway? (y or n) y
 上記の例で、printコマンドの代わりにdisplayコマンドを使うと、ブレイクポイントで停止するたびに、指定した変数値を表示してくれるようになります。従って、常に監視している変数の値は、displayコマンドで指定すると良いでしょう。

 また、break 11 if odd > 10の部分はwatch odd > 10とすることができます。前者は指定行(この場合は11行目)で、条件に合致した時のみ停止することになり、後者はこのブロックを実行中の範囲内で、条件に合致した時に停止することになります。