9. C Preprocessor Macros

Some languages, such as C and C++, provide a way to define and invoke
"preprocessor macros" which expand into strings of tokens.
GDB can evaluate expressions containing macro invocations, show
the result of macro expansion, and show a macro's definition, including
where it was defined.

You may need to compile your program specially to provide GDB
with information about preprocessor macros. Most compilers do not
include macros in their debugging information, even when you compile
with the `-g' flag. See section 4.1 Compiling for debugging.

A program may define a macro at one point, remove that definition later,
and then provide a different definition after that. Thus, at different
points in the program, a macro may have different definitions, or have
no definition at all. If there is a current stack frame, GDB
uses the macros in scope at that frame's source code line. Otherwise,
GDB uses the macros in scope at the current listing location;
see 7.1 Printing source lines.

At the moment, GDB does not support the ##
token-splicing operator, the # stringification operator, or
variable-arity macros.

Whenever GDB evaluates an expression, it always expands any
macro invocations present in the expression. GDB also provides
the following commands for working with macros explicitly.

macro expand expression

macro exp expression

Show the results of expanding all preprocessor macro invocations in
expression. Since GDB simply expands macros, but does
not parse the result, expression need not be a valid expression;
it can be any string of tokens.

macro expand-once expression

macro exp1 expression

(This command is not yet implemented.) Show the results of
expanding those preprocessor macro invocations that appear explicitly in
expression. Macro invocations appearing in that expansion are
left unchanged. This command allows you to see the effect of a
particular macro more clearly, without being confused by further
expansions. Since GDB simply expands macros, but does not
parse the result, expression need not be a valid expression; it
can be any string of tokens.

info macro macro

Show the definition of the macro named macro, and describe the
source location where that definition was established.

macro define macroreplacement-list

macro define macro(arglist) replacement-list

(This command is not yet implemented.) Introduce a definition for a
preprocessor macro named macro, invocations of which are replaced
by the tokens given in replacement-list. The first form of this
command defines an "object-like" macro, which takes no arguments; the
second form defines a "function-like" macro, which takes the arguments
given in arglist.

A definition introduced by this command is in scope in every expression
evaluated in GDB, until it is removed with the macro
undef command, described below. The definition overrides all
definitions for macro present in the program being debugged, as
well as any previous user-supplied definition.

macro undef macro

(This command is not yet implemented.) Remove any user-supplied
definition for the macro named macro. This command only affects
definitions provided with the macro define command, described
above; it cannot remove definitions present in the program being
debugged.

Here is a transcript showing the above commands in action. First, we
show our source files:

In the example above, note that macro expand-once expands only
the macro invocation explicit in the original text -- the invocation of
ADD -- but does not expand the invocation of the macro M,
which was introduced by ADD.

Once the program is running, GDB uses the macro definitions in force at
the source line of the current stack frame: