[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Aside from breakpoint commands (see section Breakpoint Command Lists), GDB provides two ways to store sequences of commands for execution as a unit: user-defined commands and command files.
20.1 User-defined Commands How to define your own commands 20.2 User-defined Command Hooks Hooks for user-defined commands 20.3 Command Files How to write scripts of commands to be stored in a file 20.4 Commands for Controlled Output Commands for controlled output
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
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
. A trivial example:
define adder print $arg0 + $arg1 + $arg2 end |
adder 1 2 3 |
define adder if $argc == 2 print $arg0 + $arg1 end if $argc == 3 print $arg0 + $arg1 + $arg2 end end |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
You may define hooks, which are a special kind of user-defined command. Whenever you run the command `foo', if the user-defined command `hook-foo' exists, it is executed (with no arguments) before that command.
A hook may also be defined which is run after the command you executed. Whenever you run the command `foo', if the user-defined command `hookpost-foo' exists, it is executed (with no arguments) after that command. Post-execution hooks may exist simultaneously with pre-execution hooks, for the same command.
It is valid for a hook to call the command which it hooks. If this occurs, the hook is not re-executed, thereby avoiding infinite recursion.
In addition, a pseudo-command, `stop' exists. Defining (`hook-stop') makes the associated commands execute every time execution stops in your program: before breakpoint commands are run, displays are printed, or the stack frame is printed.
For example, to ignore SIGALRM
signals while
single-stepping, but treat them normally during normal execution,
you could define:
define hook-stop handle SIGALRM nopass end define hook-run handle SIGALRM pass end define hook-continue handle SIGALRM pass end |
define hook-echo echo <<<--- end define hookpost-echo echo --->>>\n end (gdb) echo Hello World <<<---Hello World--->>> (gdb) |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
A command file for GDB is a text file made of lines that are GDB commands. Comments (lines starting with #) may also be included. An empty line in a command file does nothing; it does not mean to repeat the last command, as it would from the terminal.
You can request the execution of a command file with the source
command:
source [-v
] filename
The lines in a command file are generally executed sequentially, unless the order of execution is changed by one of the flow-control commands described below. The commands are not printed as they are executed. An error in any command terminates execution of the command file and control is returned to the console.
GDB searches for filename in the current directory and then on the search path (specified with the `directory' command).
If -v
, for verbose mode, is given then GDB displays
each command as it is executed. The option must be given before
filename, and is interpreted as part of the filename anywhere else.
Commands that would ask for confirmation if used interactively proceed without asking when used in a command file. Many GDB commands that normally print messages to say what they are doing omit the messages when called from command files.
GDB also accepts command input from standard input. In this mode, normal output goes to standard output and error output goes to standard error. Errors in a command file supplied on standard input do not terminate execution of the command file--execution continues with the next command.
gdb < cmds > log 2>&1 |
[ <] | [ >] | [ <<] | [ Up] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
During the execution of a command file or a user-defined command, normal GDB output is suppressed; the only output that appears is what is explicitly printed by the commands in the definition. This section describes three commands useful for generating exactly the output you want.
echo text
A backslash at the end of text can be used, as in C, to continue the command onto subsequent lines. For example,
echo This is some text\n\ which is continued\n\ onto several lines.\n |
echo This is some text\n echo which is continued\n echo onto several lines.\n |
printf (template, expressions ...); |
printf "foo, bar-foo = 0x%x, 0x%x\n", foo, bar-foo |
printf "D32: %Hf - D64: %Df - D128: %DDf\n",1.2345df,1.2E10dd,1.2E1dl |
[ <<] | [ >>] | [Top] | [Contents] | [Index] | [ ?] |
Please send FSF & GNU inquiries & questions to gnu@gnu.org. There are also other ways to contact the FSF.
These pages are maintained by the GDB developers.
Copyright Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
This document was generated by GDB Administrator on March, 27 2008 using texi2html