Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)

18 Canned Sequences of Commands
In addition to breakpoint commands (see “Breakpoint command lists” (page 43)), GDB provides
the following two ways to store sequence of commands for execution as a unit:
user-defined commands
command files
User-defined commands
A user-defined command is a sequence of GDB commands to which you assign a new name as
a command. This is done with the define command. User commands may accept up to 10
arguments separated by whitespace. Arguments are accessed within the user command via $arg0.
. . $arg9. The following example illustrates the use of canned sequence of commands:
define adder
print $arg0 + $arg1 + $arg2
To execute the command use:
adder 1 2 3
This defines the command adder, which prints the sum of its three arguments. Note the arguments
are text substitutions, so they may reference variables, use complex expressions, or even perform
further functions calls.
The following constructs can be used to create canned sequence of commands:
define commandname
dont-repeat
Define a command named commandname. If there is
already a command by that name, you are asked to confirm
that you want to redefine it. The definition of the command
is made up of other GDB command lines, which are given
following the define command. The end of these
commands is marked by a line containing end.
If the dont-repeat option is specified, then the
commandname is not repeated, if the user hits the Enter key
again.
if Takes a single argument, which is an expression to evaluate.
It is followed by a series of commands that are executed
only if the expression is true (nonzero). The if clause can
be followed by an optional else clause. You can add a list
of commands to the else clause which get executed only
if the expression is false.
while The syntax is similar to if: the command takes a single
argument, which is an expression to evaluate, and must be
followed by the commands to execute, one per line,
terminated by an end. The commands are executed
repeatedly as long as the expression evaluates to true.
document commandname Document the user-defined command commandname, so
that it can be accessed by help. The command
commandname must already be defined. This command
reads lines of documentation just as define reads the lines
of the command definition, ending with end. After the
document command is finished, help on command
commandname displays the documentation you have written.
220 Canned Sequences of Commands