Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)
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.
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.
• Some compilers do not support the -g-O options together.
• The -g-O options do not work on machines with instruction scheduling.
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 19)), or by
using the file or exec-file command (see “Commands to specify files” (page 88)).
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 29).
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 29).
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 30).
standard input and output. Your program as default uses the same device for standard
input and standard output as GDB is using. You can redirect
input and output in the run command line, or you can use
28 Running Programs Under GDB