GCC has various special options that are used for debugging either your program or GCC:
-g
On most systems that use stabs format, -g
enables use of extra
debugging information that only GDB can use; this extra information
makes debugging work better in GDB but will probably make other debuggers
crash or
refuse to read the program. If you want to control for certain whether
to generate the extra information, use -gstabs+
, -gstabs
,
-gxcoff+
, -gxcoff
, or -gvms
(see below).
Unlike most other C compilers, GCC allows you to use -g
with
-O
. The shortcuts taken by optimized code may occasionally
produce surprising results: some variables you declared may not exist
at all; flow of control may briefly move where you did not expect it;
some statements may not be executed because they compute constant
results or their values were already at hand; some statements may
execute in different places because they were moved out of loops.
Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.
The following options are useful when GCC is generated with the
capability for more than one debugging format.
-ggdb
-gstabs
-feliminate-unused-debug-symbols
-gstabs+
-gcoff
-gxcoff
-gxcoff+
-gdwarf-2
-gvms
-g
level
-ggdb
level
-gstabs
level
-gcoff
level
-gxcoff
level
-gvms
level
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions
present in the program. Some debuggers support macro expansion when
you use -g3
.
Note that in order to avoid confusion between DWARF1 debug level 2,
and DWARF2 -gdwarf-2
does not accept a concatenated debug
level. Instead use an additional -g
level option to
change the debug level for DWARF2.
-feliminate-dwarf2-dups
-gdwarf-2
.
-p
prof
. You must use this option when compiling
the source files you want data about, and you must also use it when
linking.
-pg
gprof
. You must use this option when compiling
the source files you want data about, and you must also use it when
linking.
-Q
-ftime-report
-fmem-report
-fprofile-arcs
auxname.gcda
for each source file. The data may be used for
profile-directed optimizations (-fbranch-probabilities
), or for
test coverage analysis (-ftest-coverage
). Each object file's
auxname is generated from the name of the output file, if
explicitly specified and it is not the final executable, otherwise it is
the basename of the source file. In both cases any suffix is removed
(e.g. foo.gcda
for input file dir/foo.c
, or
dir/foo.gcda
for output file specified as -o dir/foo.o
).
-fprofile-arcs
plus optimization
and code generation options. For test coverage analysis, use the
additional -ftest-coverage
option. You do not need to profile
every source file in a program.
-lgcov
or -fprofile-arcs
(the latter implies the former).
fork
calls are detected and correctly handled (double counting
will not happen).
-fbranch-probabilities
(see Options that Control Optimization).
gcov
to produce human readable
information from the .gcno
and .gcda
files. Refer to the
gcov
documentation for further information.
With -fprofile-arcs
, for each function of your program GCC
creates a program flow graph, then finds a spanning tree for the graph.
Only arcs that are not on the spanning tree have to be instrumented: the
compiler adds code to count the number of times that these arcs are
executed. When an arc is the only exit or only entrance to a block, the
instrumentation code can be added to the block; otherwise, a new basic
block must be created to hold the instrumentation code.
-ftest-coverage
gcov
code-coverage utility
(see gcov
--a Test Coverage Program) can use to
show program coverage. Each source file's note file is called
auxname.gcno
. Refer to the -fprofile-arcs
option
above for a description of auxname and instructions on how to
generate test coverage data. Coverage data will match the source files
more closely, if you do not optimize.
-d
letters
foo.01.rtl
or foo.02.sibling
).
Here are the possible letters for use in letters, and their
meanings:
A
b
file.12.bp
.
B
file.31.bbro
.
c
file.20.combine
.
C
file.14.ce1
.
Also dump after the second if conversion, to the file
file.21.ce2
.
d
file.32.btl
.
Also dump after delayed branch scheduling, to
file.36.dbr
.
D
E
file.30.ce3
.
f
file.11.cfg
.
Also dump after life analysis, to
file.19.life
.
F
ADDRESSOF
codes, to
file.07.addressof
.
g
file.25.greg
.
G
file.08.gcse
.
Also dump after jump bypassing and control flow optimizations, to
file.10.bypass
.
h
file.03.eh
.
i
file.02.sibling
.
j
file.04.jump
.
k
file.34.stack
.
l
file.24.lreg
.
L
file.09.loop
and
file.16.loop2
.
M
file.35.mach
.
n
file.29.rnreg
.
N
file.22.regmove
.
o
file.26.postreload
.
r
file.01.rtl
.
R
file.33.sched2
.
s
file.06.cse
.
S
file.23.sched
.
t
file.18.cse2
.
T
file.15.tracer
.
u
file.05.null
.
U
file.00.unit
.
V
file.13.vpt
.
w
file.27.flow2
.
z
file.28.peephole2
.
Z
file.17.web
.
a
H
m
p
P
-dp
annotation.
v
file.01.rtl
), dump a representation of the control flow graph
suitable for viewing with VCG to
file.
pass.vcg
.
x
r
.
y
-fdump-unnumbered
-d
option above), suppress instruction
numbers and line number note output. This makes it more feasible to
use diff on debugging dumps for compiler invocations with different
options, in particular with and without -g
.
-fdump-translation-unit
(C and C++ only)
-fdump-translation-unit-
options
(C and C++ only)
.tu
to the
source file name. If the -
options
form is used, options
controls the details of the dump as described for the
-fdump-tree
options.
-fdump-class-hierarchy
(C++ only)
-fdump-class-hierarchy-
options
(C++ only)
.class
to the source file name. If the -
options
form is used,
options controls the details of the dump as described for the
-fdump-tree
options.
-fdump-tree-
switch
(C++ only)
-fdump-tree-
switch-
options
(C++ only)
-
options
form is used, options is a list of -
separated options that
control the details of the dump. Not all options are applicable to all
dumps, those which are not meaningful will be ignored. The following
options are available
address
slim
all
The following tree dumps are possible:
original
file.original
.
optimized
file.optimized
.
inlined
file.inlined
.
-frandom-seed=
string
-frandom-seed
option to produce
reproducibly identical object files.
The string should be different for every file you compile.
-fsched-verbose=
n
-dS
or -dR
is
specified, in which case it is output to the usual dump
listing file, .sched
or .sched2
respectively. However
for n greater than nine, the output is always printed to standard
error.
For n greater than zero, -fsched-verbose
outputs the
same information as -dRS
. For n greater than one, it
also output basic block probabilities, detailed ready list information
and unit/insn info. For n greater than two, it includes RTL
at abort point, control-flow and regions info. And for n over
four, -fsched-verbose
also includes dependence info.
-save-temps
foo.c
with -c -save-temps
would produce files
foo.i
and foo.s
, as well as foo.o
. This creates a
preprocessed foo.i
output file even though the compiler now
normally uses an integrated preprocessor.
-time
# cc1 0.12 0.01 # as 0.00 0.01
The first number on each line is the "user time," that is time spent
executing the program itself. The second number is "system time,"
time spent executing operating system routines on behalf of the program.
Both numbers are in seconds.
-print-file-name=
library
-print-multi-directory
GCC_EXEC_PREFIX
.
-print-multi-lib
;
, and each switch starts with an @
instead of the
-
, without spaces between multiple switches. This is supposed to
ease shell-processing.
-print-prog-name=
program
-print-file-name
, but searches for a program such as cpp
.
-print-libgcc-file-name
-print-file-name=libgcc.a
.
This is useful when you use -nostdlib
or -nodefaultlibs
but you do want to link with libgcc.a
. You can do
gcc -nostdlib files... `gcc -print-libgcc-file-name`
-print-search-dirs
gcc
will search--and don't do anything else.
This is useful when gcc
prints the error message
installation problem, cannot exec cpp0: No such file or directory
.
To resolve this you either need to put cpp0
and the other compiler
components where gcc
expects to find them, or you can set the environment
variable GCC_EXEC_PREFIX
to the directory where you installed them.
Don't forget the trailing '/'.
See Environment Variables.
-dumpmachine
i686-pc-linux-gnu
)--and don't do anything else.
-dumpversion
3.0
)--and don't do
anything else.
-dumpspecs
-feliminate-unused-debug-types