Debugging with GDB Manual (5900-1473; WDB 6.2; January 2011)

Table Of Contents
18 Canned Sequences of Commands
In addition to breakpoint commands (see “Breakpoint command lists” (page 56)), GDB
provides the following two ways to store sequence of commands for execution as a unit:
user-defined commands
command files
18.1 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.
18.1 User-defined commands 289