- まずこれらのファイルをコンパイルします.この時にコンパイルオプショ
ン -gを付けます.このオプションを付けないと gdbでデバック作業は行
えません.以下のようにコンパイルします.
% gcc -g -c sample.c output.c
オブジェクトファイル sample.o output.oが生成される.
% gcc -g sample.o output.o -o sample
- 出来た実行ファイルを普通に実行すると以下のようになる.
mine@eric[src]% ./sample
100
100
100
100
100
- それでは,今度は gdbを使って sampleを実行してみる.以下のように
して実行する.メッセージが表示されて,(gdb)となれは gdb
モードになっています.
mine@eric[src]% gdb sample
GNU gdb 4.18
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-unknown-freebsd"...
(gdb)
- まずブレークポイントを指定します.sample.cの8行目にブレークポ
イントを指定します.
(gdb) break 8
Breakpoint 1 at 0x8048504: file sample.c, line 8.
- プログラムを実行する.
(gdb) run
Starting program:/.amd_mnt/shiozaki/home/4f/mine/study/free/CC/src/sample
Breakpoint 1, main () at sample.c:8
8 output(value);
ブレークポイントである sample.cの8行目で停止しいています.
- 変数 iと valueの値を表示す.
(gdb) print i
$1 = 0
(gdb) print value
$2 = 100
- ステップ実行する.
(gdb) step
output (value=100) at output.c:5
5 printf("%i\n", value);
output.cの5行目,関数 outputの中で停止している.
- どうのようにして関数outputに来ているか表示す.
(gdb) where
#0 output (value=100) at output.c:5
#1 0x8048510 in main () at sample.c:8
#2 0x8048459 in _start ()
これは下から読みます.つまりスタックを表示している._start()はシ
ステム側のものです.#1からがユーザプログラムです.現在 sample.c
8行目,main関数から,output.cの関数 outputが呼び出され,output.c
の5行目で停止していることを表している.
- continue コマンドを実行する.
(gdb) continue
Continuing.
100
Breakpoint 1, main () at sample.c:8
8 output(value);
2行目でvalue(=100)を出力している.ブレークポイントである
sample.cの8行目で停止している.
- nextコマンドを実行する.
(gdb) next
100
7 for( i = 0; i < 5; i++ )
前の stepコマンドのとの違いをみてください.output関数の中を処理し
たあとに,sample.cの次の行で停止している.value(=100)が出力され
ているいます.
- print コマンドで関数の値を評価してみる.
(gdb) print output(10)
10
$3 = void
関数 outputの引数として10を与えた場合の式を評価している.1行目で
10が出力されている.2行目の値は関数 outputの返り値です.
- ブレークポイントを消去して,最後までプログラムを実行する.
(gdb) clear 8
Deleted breakpoint 1
(gdb) continue
Continuing.
100
100
100
Program exited with code 04.
ブレークポイントを削除して,continueコマンドを使用すると,プログ
ラムの最後まで実行している.value(=100)を3回出力してプログラム
を終了している.