-g

Category

Error checking and debugging

Pragma equivalent

None.

Purpose

Generates debugging information for use by a symbolic debugger, and makes the program state available to the debugging session at selected source locations.

Program state refers to the values of user variables at certain points during the execution of a program.

You can use different -g levels to balance between debug capability and compiler optimization. Higher -g levels provide a more complete debug support, at the cost of runtime or possible compile-time performance, while lower -g levels provide higher runtime performance, at the cost of some capability in the debugging session.

When the -O2 optimization level is in effect, the debug capability is completely supported.
Note: When an optimization level higher than -O2 is in effect, the debug capability is limited.

Syntax

Read syntax diagramSkip visual syntax diagram
        .-0-.   
>>- -g--+---+--------------------------------------------------><
        +-1-+   
        +-2-+   
        +-3-+   
        +-4-+   
        +-5-+   
        +-6-+   
        +-7-+   
        +-8-+   
        '-9-'   

Defaults

-g0

Parameters

-g
  • When no optimization is enabled (-qnoopt), -g is equivalent to -g9.
  • When the -O2 optimization level is in effect, -g is equivalent to -g2.
-g0
Generates no debugging information. No program state is preserved.
-g1
Generates minimal read-only debugging information about line numbers and source file names. No program state is preserved. This option is equivalent to -qlinedebug.
-g2
Generates read-only debugging information about line numbers, source file names, and variables.

When the -O2 optimization level is in effect, no program state is preserved.

-g3, -g4
Generates read-only debugging information about line numbers, source file names, and variables.
When the -O2 optimization level is in effect:
  • No program state is preserved.
  • Function parameter values are available to the debugger at the beginning of each function.
-g5, -g6, -g7
Generates read-only debugging information about line numbers, source file names, and variables.
When the -O2 optimization level is in effect:
  • Program state is available to the debugger at if constructs, loop constructs, function definitions, and function calls. For details, see Usage.
  • Function parameter values are available to the debugger at the beginning of each function.
-g8
Generates read-only debugging information about line numbers, source file names, and variables.
When the -O2 optimization level is in effect:
  • Program state is available to the debugger at the beginning of every executable statement.
  • Function parameter values are available to the debugger at the beginning of each function.
-g9
Generates debugging information about line numbers, source file names, and variables. You can modify the value of the variables in the debugger.
When the -O2 optimization level is in effect:
  • Program state is available to the debugger at the beginning of every executable statement.
  • Function parameter values are available to the debugger at the beginning of each function.

Usage

When no optimization is enabled, the debugging information is always available if you specify -g2 or a higher level. When the -O2 optimization level is in effect, the debugging information is available at selected source locations if you specify -g5 or a higher level.

When you specify -g8 or -g9 with -O2, the debugging information is available at every source line with an executable statement.

When you specify -g5, -g6, or -g7 with -O2, the debugging information is available for the following language constructs:

Examples

Use the following command to compile myprogram.c and generate an executable program called testing for debugging:
xlc myprogram.c -o testing -g 
The following command uses a specific -g level with -O2 to compile myprogram.c and generate debugging information:
xlc myprogram.c -O2 -g8 

Related information