Contents|Index|Previous|Next

Options for Debugging Your Program or GNU CC

GNU CC has the following special options that are used for debugging either your program or GCC.

-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 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, -gdwarf+1, or -gdwarf-1 (see the explanations for each option in the following discussion).
Unlike most other C compilers, GNU CC 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 GNU CC is generated with the capability for more than one debugging format.

-ggdb
Produce debugging information in the native format (if that is supported), including GDB extensions if at all possible.
-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 which 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.
-gdwarf-1    
Produce debugging information in DWARF version 1 format (if that is supported). This is the format used by SDB on most System V Release 4 systems.
-gdwarf-1+    
Produce debugging information in DWARF version 1 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.
-gdwarf-2    
Produce debugging information in DWARF version 2 format (if that is supported). This is the format used by DBX on IRIX 6.
-gdwarf    
Produce debugging information in DWARF version 1 format, if that is the default debugging format for the target, or version 2 if that is supported. This is the format used by SDB on most System V Release 4 systems.
-glevel    
-ggdblevel    
-gstabslevel    
-gcofflevel    
-gxcofflevel    
-gdwarflevel    
Request debugging information and also use level to specify how much information. The default level is 2.
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’.
-p    
Generate extra code to write profile information suitable for the analysis program prof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
-pg    
Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
-a    
Generate extra code to write profile information for basic blocks, which will record the number of times each basic block is executed, the basic block start address, and the function name containing the basic block. If
‘
-g’ is used, the line number and filename of the start of the basic block will also be recorded. If not overridden by the machine description, the default action is to append to the text file ‘bb.out’.
This data could be analyzed by a program like tcov. However, the format of the data is not what tcov expects. Eventually GNU gprof should be extended to process this data.
-ax    
Generate extra code to profile basic blocks. Your executable will produce output that is a superset of that produced when ‘-a’ is used. Additional output is the source and target address of the basic blocks where a jump takes place, the number of times a jump is executed, and (optionally) the complete sequence of basic blocks being executed. The output is appended to file ‘bb.out’.
You can examine different profiling aspects without recompilation. Your executable will read a list of function names from file ‘bb.in’. Profiling starts when a function on the list is entered and stops when that invocation is exited. To exclude a function from profiling, use a hyphen (-), to prefix its name.
If a function name is not unique, specify its location, for example, ‘/path/filename.d:functionname’. Your executable will write the available paths and filenames in file ‘bb.out’.
The following function names have a special meaning.
What follows is a short example using different profiling parameters in file ‘bb.in’. Assume function foo consists of basic blocks 1 and 2 and is called twice from block 3 of function main. After the calls, block 3 transfers control to block 4 of main.
With __bb_trace__ and main contained in file ‘bb.in’, the following sequence of blocks is written to file ‘bbtrace.gz’: 0 3 1 2 1 2 4. The return from block 2 to block 3 is not shown, because the return is to a point inside the block and not to the top. The block address 0 always indicates that control is transferred to the trace from somewhere outside the observed functions. With -foo added to ‘bb.in’, the blocks of function foo are removed from the trace, so only 0 3 4 remains.
With __bb_jumps__ and main contained in file ‘bb.in’, jump frequencies will be written to file ‘b.out’. The frequencies are obtained by constructing a trace of blocks and incrementing a counter for every neighboring pair of blocks in the trace. The trace 0 3 1 2 1 2 4 displays the following frequencies.
With __bb_hidecall__, due to call instructions, control transfer is removed from the trace; that is, the trace is cut into three parts: 0 3 4, 0 1 2, and 0 1 2. With __bb_showret__, control transfer is added to the trace. The trace becomes: 0 3 1 2 3 1 2 3 4.
-fprofile-arcs    
Instrument arcs during compilation. For each function of your program, GNU CC 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.
Since not every arc in the program must be instrumented, programs compiled with this option run faster than programs compiled with ‘-a’, which adds instrumentation code to every basic block in the program. The tradeoff: since gcov does not have execution counts for all branches, it must start with the execution counts for the instrumented branches, and then iterate over the program flow graph until the entire graph has been solved. Hence, gcov runs a little more slowly than a program which uses information from ‘-a’.
-fprofile-arcs also makes it possible to estimate branch probabilities, and to calculate basic block execution counts. In general, basic block execution counts do not give enough information to estimate all branch probabilities. When the compiled program exits, it saves the arc execution counts to a file called sourcename.da. Use the compiler option, -fbranch-probabilities, when recompiling, to optimize using estimated branch probabilities (see Options that Control Optimization).
-ftest-coverage    
Create data files for the gcov code-coverage utility (see gcov: a Test Coverage Program). The data filenames begin with the name of your source file:

-dletters    
This option says to  make debugging dumps during compilation at times specified by letters. This is used for debugging the compiler. The filenames for most of the dumps are made by appending a word to the source filename (e.g., foo.c.rtl or foo.c.jump). What follows are the possible letters for use in letters, and their meanings.

-fpretend-float   
When running a cross-compiler, pretend that the target machine uses the same floating point format as the host machine. This causes incorrect output of the actual floating constants, but the actual instruction sequence will probably be the same as GNU CC would make when running on the target machine.
-save-temps   
Store the usual temporary intermediate files permanently; place them in the current directory and name them based on the source file. Thus, compiling ‘foo.c’ with -c-save-temps would produce files ‘foo.i’ and ‘foo.s’, as well as ‘foo.o’.
-print-file-name=library    
Print the full absolute name of the library file library that would be used when linking—and don’t do anything else. With this option, GNU CC does not compile or link anything; it just prints the filename.
-print-prog-name=program    
Like -print-file-name, but searches for a program such as ‘cpp’.
-print-libgcc-file-name    
Same as -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 use the following command.
-print-search-dirs    
Print the name of the configured installation directory and a list of program and library directories gcc will search—and don’t do anything else.
This is useful when gcc prints the following error message:
To resolve this you either need to put ‘cpp’ 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 slash). See Environment Variables Affecting GNU CC.