Next: Optimize Options, Previous: Warning Options, Up: Invoking GCC [Contents][Index]

### 3.9 Options for Debugging Your Program

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`

  Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

  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`

  Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.

`-gdwarf`
`-gdwarf-version`

  Produce debugging information in DWARF format (if that is supported). The value of version may be either 2, 3, 4 or 5; the default version for most targets is 4. DWARF Version 5 is only experimental.

  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.

  GCC no longer supports DWARF Version 1, which is substantially different than Version 2 and later. For historical reasons, some other DWARF-related options (including `-feliminate-dwarf2-dups` and `-fno-dwarf2-cfi-asm`) retain a reference to DWARF Version 2 in their names, but apply to all currently-supported versions of DWARF.

`-gstabs`

  Produce debugging information in stabs format (if that is supported), without GDB extensions. This is the format used by DBX on most BSD systems. On MIPS, Alpha and System V Release 4 systems this option produces stabs debugging output that is not understood by DBX or SDB. On System V Release 4 systems this option requires the GNU assembler.

`-gstabs+`

  Produce debugging information in stabs format (if that is supported), using GNU extensions understood only by the GNU debugger (GDB). The use of these extensions is likely to make other debuggers crash or refuse to read the program.

`-gcoff`

  Produce debugging information in COFF format (if that is supported). This is the format used by SDB on most System V systems prior to System V Release 4.

`-gxcoff`

  Produce debugging information in XCOFF format (if that is supported). This is the format used by the DBX debugger on IBM RS/6000 systems.

`-gxcoff+`

  Produce debugging information in XCOFF format (if that is supported), using GNU extensions understood only by the GNU debugger (GDB). The use of these extensions is likely to make other debuggers crash or refuse to read the program, and may cause assemblers other than the GNU assembler (GAS) to fail with an error.

`-gvms`

  Produce debugging information in Alpha/VMS debug format (if that is supported). This is the format used by DEBUG on Alpha/VMS systems.

`-glevel`
`-ggdblevel`
`-gstabslevel`
`-gcofflevel`
`-gxcofflevel`
`-gvmslevel`

  Request debugging information and also use level to specify how much information. The default level is 2.

  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, 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`

  Produce debugging information in stabs format (if that is supported), for only symbols that are actually used.

`-femit-class-debug-always`

  Instead of emitting debugging information for a C++ class in only one object file, emit it in all object files using the class. This option should be used only with debuggers that are unable to handle the way GCC normally emits debugging information for classes because using this option increases the size of debugging information by as much as a factor of two.

`-fno-merge-debug-strings`

  Direct the linker to not merge together strings in the debugging information that are identical in different object files. Merging is not supported by all assemblers or linkers. Merging decreases the size of the debug information in the output file at the cost of increasing link processing time. Merging is enabled by default.

`-fdebug-prefix-map=old=new`

  When compiling files in directory `old`, record debugging information describing them as in `new` instead.

`-fvar-tracking`

  Run variable tracking pass. It computes where variables are stored at each position in code. Better debugging information is then generated (if the debugging information format supports this information).

  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`

  Annotate assignments to user variables early in the compilation and attempt to carry the annotations over throughout the compilation all the way to the end, in an attempt to improve debug information while optimizing. Use of `-gdwarf-4` is recommended along with it.

  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`

  Separate as much DWARF debugging information as possible into a separate output file with the extension `.dwo`. This option allows the build system to avoid linking files with debug information. To be useful, this option requires a debugger capable of reading `.dwo` files.

`-gpubnames`

  Generate DWARF `.debug_pubnames` and `.debug_pubtypes` sections.

`-ggnu-pubnames`

  Generate `.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`

  When using DWARF Version 4 or higher, type DIEs can be put into their own `.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`

  This switch causes the command-line options used to invoke the compiler that may affect code generation to be appended to the DW_AT_producer attribute in DWARF debugging information. The options are concatenated with spaces separating them from each other and from the compiler version. It is enabled by default. See also `-frecord-gcc-switches` for another way of storing compiler options into the object file.

`-gstrict-dwarf`

  Disallow using extensions of later DWARF standard version than selected with `-gdwarf-version`. On most targets using non-conflicting DWARF extensions from later standard versions is allowed.

`-gno-strict-dwarf`

  Allow using extensions of later DWARF standard version than selected with `-gdwarf-version`.

`-gcolumn-info`
`-gno-column-info`

  Emit location column information into DWARF debugging information, rather than just file and line. This option is disabled by default.

`-gz[=type]`

  Produce compressed debug sections in DWARF format, if that is supported. If type is not given, the default type depends on the capabilities of the assembler and linker used. type may be one of ‘`none`’ (don’t compress debug sections), ‘`zlib`’ (use zlib compression in ELF gABI format), or ‘`zlib-gnu`’ (use zlib compression in traditional GNU format). If the linker doesn’t support writing compressed debug sections, the option is rejected. Otherwise, if the assembler does not support them, `-gz` is silently ignored when producing object files.

`-feliminate-dwarf2-dups`

  Compress DWARF debugging information by eliminating duplicated information about each symbol. This option only makes sense when generating DWARF debugging information.

`-femit-struct-debug-baseonly`

  Emit debug information for struct-like types only when the base name of the compilation source file matches the base name of file in which the struct is defined.

  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`

  Emit debug information for struct-like types only when the base name of the compilation source file matches the base name of file in which the type is defined, unless the struct is a template or defined in a system header.

  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]`

  Specify the struct-like types for which the compiler generates debug information. The intent is to reduce duplicate struct debug information between different object files within the same program.

  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`

  Emit DWARF unwind info as compiler generated `.eh_frame` section instead of using GAS `.cfi_*` directives.

`-fno-eliminate-unused-debug-types`

  Normally, when producing DWARF output, GCC avoids producing debug symbol output for types that are nowhere used in the source file being compiled. Sometimes it is useful to have GCC emit debugging information for all types declared in a compilation unit, regardless of whether or not they are actually used in that compilation unit, for example if, in the debugger, you want to cast a value to a type that is not actually used in your program (but is declared). More often, however, this results in a significant amount of wasted space.

Next: Optimize Options, Previous: Warning Options, Up: Invoking GCC [Contents][Index]