GCC has various special options that are used for debugging either your program or GCC:
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 probably makes 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).
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 are already at hand; some statements may execute in different places because they have been 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.
.debug_types section instead of making them part of the
.debug_info section. It is more efficient to put them in a separate
comdat sections since the linker can then remove duplicates.
But not all DWARF consumers support
.debug_types sections yet
and on some objects
.debug_types produces larger instead of smaller
Note that with DWARF Version 2, some ports require and always use some non-conflicting DWARF 3 extensions in the unwind tables.
Version 4 may require GDB 7.0 and -fvar-tracking-assignments
for maximum benefit.
Level 0 produces no debug information at all. Thus, -g0 negates -g.
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.
-gdwarf-2 does not accept a concatenated debug level, because
GCC used to support an option -gdwarf that meant to generate
debug information in version 1 of the DWARF format (which is very
different from version 2), and it would have been too confusing. That
debug format is long obsolete, but the option cannot be changed now.
Instead use an additional -glevel option to change the
debug level for DWARF.
.), the name
of the dump file is determined by appending
.gkd to the
compilation output file name.
If the equal sign is omitted, the default -gtoggle is used.
The environment variable GCC_COMPARE_DEBUG, if defined, non-empty and nonzero, implicitly enables -fcompare-debug. If GCC_COMPARE_DEBUG is defined to a string starting with a dash, then it is used for opts, otherwise the default -gtoggle is used.
-fcompare-debug=, with the equal sign but without opts, is equivalent to -fno-compare-debug, which disables the dumping of the final representation and the second compilation, preventing even GCC_COMPARE_DEBUG from taking effect.
To verify full coverage during -fcompare-debug testing, set
GCC_COMPARE_DEBUG to say ‘-fcompare-debug-not-overridden’,
which GCC rejects as an invalid option in any actual compilation
(rather than preprocessing, assembly or linking). To get just a
warning, setting GCC_COMPARE_DEBUG to ‘-w%n-fcompare-debug
not overridden’ will do.
.gk additional extension during the second compilation, to avoid
overwriting those generated by the first.
When this option is passed to the compiler driver, it causes the
first compilation to be skipped, which makes it useful for little
other than debugging the compiler proper.
This option substantially reduces the size of debugging information, but at significant potential loss in type information to the debugger. See -femit-struct-debug-reduced for a less aggressive option. See -femit-struct-debug-detailed for more detailed control.
This option works only with DWARF 2.
This option significantly reduces the size of debugging information, with some potential loss in type information to the debugger. See -femit-struct-debug-baseonly for a more aggressive option. See -femit-struct-debug-detailed for more detailed control.
This option works only with DWARF 2.
This option is a detailed version of -femit-struct-debug-reduced and -femit-struct-debug-baseonly, which serves for most needs.
A specification has the syntax
The optional first word limits the specification to structs that are used directly (‘dir:’) or used indirectly (‘ind:’). A struct type is used directly when it is the type of a variable, member. Indirect uses arise through pointers to structs. That is, when use of an incomplete struct is valid, the use is indirect. An example is ‘struct one direct; struct two * indirect;’.
The optional second word limits the specification to ordinary structs (‘ord:’) or generic structs (‘gen:’). Generic structs are a bit complicated to explain. For C++, these are non-explicit specializations of template classes, or non-template classes within the above. Other programming languages have generics, but -femit-struct-debug-detailed does not yet implement them.
The third word specifies the source files for those structs for which the compiler should emit debug information. The values ‘none’ and ‘any’ have the normal meaning. The value ‘base’ means that the base of name of the file in which the type declaration appears must match the base of the name of the main compilation file. In practice, this means that when compiling foo.c, debug information is generated for types declared in that file and foo.h, but not other header files. The value ‘sys’ means those types satisfying ‘base’ or declared in system or compiler headers.
You may need to experiment to determine the best settings for your application.
The default is -femit-struct-debug-detailed=all.
This option works only with DWARF 2.
instead of using GAS
static means that the function manipulates the stack
statically: a fixed number of bytes are allocated for the frame on function
entry and released on function exit; no stack adjustments are otherwise made
in the function. The second field is this fixed number of bytes.
dynamic means that the function manipulates the stack
dynamically: in addition to the static allocation described above, stack
adjustments are made in the body of the function, for example to push/pop
arguments around function calls. If the qualifier
bounded is also
present, the amount of these adjustments is bounded at compile time and
the second field is an upper bound of the total amount of stack used by
the function. If it is not present, the amount of these adjustments is
not bounded at compile time and the second field only represents the
fork calls are detected and correctly handled (double counting
will not happen).
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.
dbg_cnt() returns true always unless the upper bound
is set by this option.
For example, with -fdbg-cnt=dce:10,tail_call:0,
dbg_cnt(dce) returns true only for first 10 invocations.
Here are some examples showing uses of these options.
# disable ccp1 for all functions -fdisable-tree-ccp1 # disable complete unroll for function whose cgraph node uid is 1 -fenable-tree-cunroll=1 # disable gcse2 for functions at the following ranges [1,1], # [300,400], and [400,1000] # disable gcse2 for functions foo and foo2 -fdisable-rtl-gcse2=foo,foo2 # disable early inlining -fdisable-tree-einline # disable ipa inlining -fdisable-ipa-inline # enable tree full unroll -fenable-tree-unroll
Debug dumps can be enabled with a -fdump-rtl switch or some -d option letters. Here are the possible letters for use in pass and letters, and their meanings:
-fdump-translation-unit (C++ only)
-fdump-translation-unit-options (C++ only)
-fdump-class-hierarchy (C++ only)
-fdump-class-hierarchy-options (C++ only)
DECL_ASSEMBLER_NAME has been set for a given decl, use that
in the dump instead of
DECL_NAME. Its primary use is ease of
use working backward from mangled names in the assembly file.
When dumping pretty-printed trees, this option inhibits dumping the bodies of control structures.
When dumping RTL, print the RTL in slim (condensed) form instead of
the default LISP-like representation.
This option currently only works for RTL dumps, and the RTL is always
dumped in slim form.
DECL_UID) for each variable.
gcc -O2 -ftree-vectorize -fdump-tree-vect-blocks=foo.dump -fdump-tree-pre=stderr file.c
outputs vectorizer dump into foo.dump, while the PRE dump is
output on to stderr. If two conflicting dump filenames are
given for the same pass, then the latter option overrides the earlier
The following tree dumps are possible:
The options can be divided into two groups, 1) options describing the verbosity of the dump, and 2) options describing which optimizations should be included. The options from both the groups can be freely mixed as they are non-overlapping. However, in case of any conflicts, the latter options override the earlier options on the command line. Though multiple -fopt-info options are accepted, only one of them can have =filename. If other filenames are provided then all but the first one are ignored.
The dump verbosity has the following options
gcc -O2 -ftree-vectorize -fopt-info-vec-missed
will print information about missed optimization opportunities from
vectorization passes on stderr.
The second set of options describes a group of optimizations and may include one or more of the following.
gcc -O3 -fopt-info-missed=missed.all
outputs missed optimization report from all the passes into missed.all.
As another example,
gcc -O3 -fopt-info-inline-optimized-missed=inline.txt
will output information about missed optimizations as well as optimized locations from all the inlining passes into inline.txt.
If the filename is provided, then the dumps from all the applicable optimizations are concatenated into the filename. Otherwise the dump is output onto stderr. If options is omitted, it defaults to all-optall, which means dump all available optimization info from all the passes. In the following example, all optimization info is output on to stderr.
gcc -O3 -fopt-info
Note that -fopt-info-vec-missed behaves the same as -fopt-info-missed-vec.
As another example, consider
gcc -fopt-info-vec-missed=vec.miss -fopt-info-loop-optimized=loop.opt
Here the two output filenames vec.miss and loop.opt are
in conflict since only one output file is allowed. In this case, only
the first option takes effect and the subsequent options are
ignored. Thus only the vec.miss is produced which cotaints
dumps from the vectorizer about missed opportunities.
Note that the information output by -ftree-vectorizer-verbose
option is sent to stderr. If the equivalent form
-fopt-info-options=filename is used then the
output is sent into filename instead.
The string should be different for every file you compile.
For n greater than zero, -fsched-verbose outputs the
same information as -fdump-rtl-sched1 and -fdump-rtl-sched2.
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
When used in combination with the -x command-line option, -save-temps is sensible enough to avoid over writing an input source file with the same extension as an intermediate file. The corresponding intermediate file may be obtained by renaming the source file before using -save-temps.
If you invoke GCC in parallel, compiling several different source files that share a common base name in different subdirectories or the same source file compiled for multiple output destinations, it is likely that the different parallel compilers will interfere with each other, and overwrite the temporary files. For instance:
gcc -save-temps -o outdir1/foo.o indir1/foo.c& gcc -save-temps -o outdir2/foo.o indir2/foo.c&
may result in foo.i and foo.o being written to
simultaneously by both compilers.
gcc -save-temps=obj -c foo.c gcc -save-temps=obj -c bar.c -o dir/xbar.o gcc -save-temps=obj foobar.c -o dir2/yfoobar
creates foo.i, foo.s, dir/xbar.i,
dir/xbar.s, dir2/yfoobar.i, dir2/yfoobar.s, and
Without the specification of an output file, the output looks like this:
# 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.
With the specification of an output file, the output is appended to the named file, and it looks like this:
0.12 0.01 cc1 options 0.00 0.01 as options
The “user time” and the “system time” are moved before the program
name, and the options passed to the program are displayed, so that one
can later tell what file was being compiled, and with which options.
It is enabled by default when compiling with optimization (-Os,
-O, -O2, ...), debugging information (-g) and
the debug info format supports it.
It can be enabled even if var-tracking is disabled, in which case
annotations are created and maintained, but discarded at the end.
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`
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.