Debugging with GDB (September 2007)

Chapter 4: Running Programs Under GDB 23
4 Running Programs Under GDB
When you run a program under GDB, you must first generate debugging information
when you compile it using compiler option cc -g -O.
You may start GDB with its arguments, if any, in an environment of your choice. If you
are doing native debugging, you may redirect your program’s input and output, debug an
already running pro ce ss, or kill a child proce ss .
4.1 Compiling for debugging
Following points are noteable while compiling programs for debugging:
Compile your program with the -g-O option to generate debugging information.
The -g-O option is supported by HP ANSI C and HP aC++ compilers and GNU gcc
compiler.
Some compilers do not support the -g-O options together.
The -g-O options do not work on machines with instruction scheduling.
Note:
Older versions of the gnu C compiler permitted a variant option -gg for debugging
information. GDB no longer supports this format; if your gnu C compiler has this
option, do not use it.
4.2 Starting your program
run
r Use the run command to start your program under GDB. You must first spec-
ify the program name (except on VxWorks) with an argument to GDB (see
Chapter 2 [Getting In and Out of GDB], page 11), or by using the file or
exec-file command (see Section 12.1 [Commands to specify files], page 103).
Note:
If you are running your program in an execution environment that supports processes,
run creates an inferior process and makes that process run your program. (In environments
without processes, run jumps to the start of your program.)
The execution of a program is affected by the information it rece ives from the parent
process. You must provide GDB the information before starting the program. (You can
change the information after starting your program, but such changes only affect your
program the next time you start it.) The information that must be passed to GDB can be
categorized into four categories:
arguments.
Specify the arguments to give your program as the arguments of the run com-
mand. If a shell is available on your target, the shell is used to pass the ar-
guments, s o that you may use normal conventions (such as wildcard expansion