Show Table of Contents
5.3.2. GDB の実行
このセクションでは、以下の単純なプログラムを使った GDB の基本的な実行について説明します。
hello.c
#include <stdio.h>
char hello[] = { "Hello, World!" };
int
main()
{
fprintf (stdout, "%s\n", hello);
return (0);
}
以下の手順は、最も基本的な形式でデバッグプロセスを説明しています。
手順5.1 'Hello World' プログラムのデバッグ
- 以下のように、デバッグフラグを使った実行可能ファイルに hello.c をコンパイルします。
gcc -g -o hello hello.c生成されるバイナリーhelloがhello.cと同じディレクトリーにあることを確認します。 helloバイナリーでgdbを実行します (つまりgdb hello)。- いくつかの導入部のコメントの後に、
gdbはデフォルトの GDB プロンプトを表示します。(gdb)
- 変数
helloはグローバルであるため、メインの手順が開始する前に表示することができます。gdb) p hello $1 = "Hello, World!" (gdb) p hello[0] $2 = 72 'H' (gdb) p *hello $3 = 72 'H' (gdb)
printはhello[0]をターゲットとしており、*helloには*(hello + 1)などのような式の評価が必要になることに注意してください。(gdb) p *(hello + 1) $4 = 101 'e'
- 次に、ソースをリストします。
(gdb) l 1 #include <stdio.h> 2 3 char hello[] = { "Hello, World!" }; 4 5 int 6 main() 7 { 8 fprintf (stdout, "%s\n", hello); 9 return (0); 10 }listは、fprintf呼び出しが 8 行目にあることを示しています。その行にブレークポイントを適用して、コードを再開します。(gdb) br 8 Breakpoint 1 at 0x80483ed: file hello.c, line 8. (gdb) r Starting program: /home/moller/tinkering/gdb-manual/hello Breakpoint 1, main () at hello.c:8 8 fprintf (stdout, "%s\n", hello);
- 最後に、
nextコマンドを使用して、fprintf呼び出しの後に進んでこれを実行します。(gdb) n Hello, World! 9 return (0);
以下のセクションでは、GDB のより複雑なアプリケーションについて説明します。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.