To tell GCC to emit extra information for use by a debugger, in almost all cases you need only to add -g to your other options.
GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally be surprising: 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 is possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.
If you are not using some other optimization option, consider using -Og (see Optimize Options) with -g. With no -O option at all, some compiler passes that collect information useful for debugging do not run at all, so that -Og may result in a better debugging experience.
-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 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).
-ggdb
-gdwarf
-gdwarf-
versionNote 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.
GCC no longer supports DWARF Version 1, which is substantially
different than Version 2 and later. For historical reasons, some
other DWARF-related options such as
-fno-dwarf2-cfi-asm) retain a reference to DWARF Version 2
in their names, but apply to all currently-supported versions of DWARF.
-gstabs
-gstabs+
-gxcoff
-gxcoff+
-gvms
-g
level-ggdb
level-gstabs
level-gxcoff
level-gvms
levelLevel 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, and line number tables, but no information about local variables.
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 does not accept a concatenated debug level, to avoid
confusion with -gdwarf-level.
Instead use an additional -glevel option to change the
debug level for DWARF.
-feliminate-unused-debug-symbols
-femit-class-debug-always
-fno-merge-debug-strings
-fdebug-prefix-map=
old=
new-fvar-tracking
It is enabled by default when compiling with optimization (-Os,
-O, -O2, ...), debugging information (-g) and
the debug info format supports it.
-fvar-tracking-assignments
It can be enabled even if var-tracking is disabled, in which case
annotations are created and maintained, but discarded at the end.
By default, this flag is enabled together with -fvar-tracking,
except when selective scheduling is enabled.
-gsplit-dwarf
-gpubnames
.debug_pubnames
and .debug_pubtypes
sections.
-ggnu-pubnames
.debug_pubnames
and .debug_pubtypes
sections in a format
suitable for conversion into a GDB index. This option is only useful
with a linker that can produce GDB index version 7.
-fdebug-types-section
.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
debugging information.
-grecord-gcc-switches
-gno-record-gcc-switches
-gstrict-dwarf
-gno-strict-dwarf
-gas-loc-support
.loc
directives.
It may then use them for the assembler to generate DWARF2+ line number
tables.
This is generally desirable, because assembler-generated line-number tables are a lot more compact than those the compiler can generate itself.
This option will be enabled by default if, at GCC configure time, the
assembler was found to support such directives.
-gno-as-loc-support
gas-locview-support
view
assignment
and reset assertion checking in .loc
directives.
This option will be enabled by default if, at GCC configure time, the
assembler was found to support them.
gno-as-locview-support
-gcolumn-info
-gno-column-info
-gstatement-frontiers
-gno-statement-frontiers
is_stmt
markers in the line number table. This is enabled by default when
compiling with optimization (-Os, -O, -O2,
...), and outputting DWARF 2 debug information at the normal level.
-gvariable-location-views
-gvariable-location-views=incompat5
-gno-variable-location-views
This is enabled by default when outputting DWARF 2 debug information at the normal level, as long as there is assembler support, -fvar-tracking-assignments is enabled and -gstrict-dwarf is not. When assembler support is not available, this may still be enabled, but it will force GCC to output internal line number tables, and if -ginternal-reset-location-views is not enabled, that will most certainly lead to silently mismatching location views.
There is a proposed representation for view numbers that is not backward
compatible with the location list format introduced in DWARF 5, that can
be enabled with -gvariable-location-views=incompat5. This
option may be removed in the future, is only provided as a reference
implementation of the proposed representation. Debug information
consumers are not expected to support this extended format, and they
would be rendered unable to decode location lists using it.
-ginternal-reset-location-views
-gnointernal-reset-location-views
view number mismatch
. This is only enabled
on ports that define a reliable estimation function.
-ginline-points
-gno-inline-points
-gz
[=
type]-femit-struct-debug-baseonly
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 debug output.
-femit-struct-debug-reduced
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 debug output.
-femit-struct-debug-detailed
[=
spec-list]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
[‘dir:’|‘ind:’][‘ord:’|‘gen:’](‘any’|‘sys’|‘base’|‘none’)
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 debug output.
-fno-dwarf2-cfi-asm
.eh_frame
section
instead of using GAS .cfi_*
directives.
-fno-eliminate-unused-debug-types