Debugging with GDB Manual HP WDB v6.3 (5900-2180, August 2012)

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
process, or kill a child process.
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 specify the program
name (except on VxWorks) with an argument to GDB (see Chapter 2 (page 21)), or by
using the file or exec-file command (see “Commands to specify files” (page 95)).
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 receives 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 command. If a shell is available on
your target, the shell is used to pass the arguments, so that
you may use normal conventions (such as wildcard
expansion or variable substitution) in describing the
arguments. On Unix systems, you can control which shell is
used with the SHELL environment variable. GDB uses the
C shell (/usr/bin/csh). See Arguments To Your
Program” (page 32).
environment. Your program inherits its environment from GDB. However,
you can use the GDB commands set environment and
unset environment to change parts of the environment
that affect your program. See“Program Environment”
(page 32).
working directory. Your program inherits its working directory from GDB. You
can set the GDB working directory with the cd command
in GDB. See“Working directory” (page 33).
4.1 Compiling for debugging 31