You can implement new GDB CLI commands in Guile. A CLI
command object is created with the
make-command Guile function,
and added to GDB with the
register-command! Guile function.
This two-step approach is taken to separate out the side-effect of adding
the command to GDB from
There is no support for multi-line commands, that is commands that
consist of multiple lines and are terminated with
name is the name of the command. If
name consists of
multiple words, then the initial words are looked for as prefix
commands. In this case, if one of the prefix commands does not exist,
an exception is raised.
The result is the
<gdb:command> object representing the command.
The command is not usable until it has been registered with GDB
The rest of the arguments are optional.
invoke is a procedure of three arguments:
from-tty. The argument
self is the
<gdb:command> object representing the command.
args is a string representing the arguments passed to
the command, after leading and trailing whitespace has been stripped.
from-tty is a boolean flag and specifies whether the
command should consider itself to have been originated from the user
invoking it interactively. If this function throws an exception,
it is turned into a GDB
Otherwise, the return value is ignored.
command-class is one of the ‘
defined below. This argument tells GDB how to categorize the
new command in the help system. The default is
completer is either
#f, one of the ‘
constants defined below, or a procedure, also defined below.
This argument tells GDB how to perform completion
for this command. If not provided or if the value is
then no completion is performed on the command.
prefix is a boolean flag indicating whether the new
command is a prefix command; sub-commands of this command may be
doc-string is help text for the new command.
If no documentation string is provided, the default value “This command is
not documented.” is used.
<gdb:command>object, to GDB’s list of commands. It is an error to register a command more than once. The result is unspecified.
<gdb:command>object. Otherwise return
dont-repeatfunction. This is similar to the user command
dont-repeat, see dont-repeat.
Convert a string to a list of strings split up according to GDB’s argv parsing rules. It is recommended to use this for consistency. Arguments are separated by spaces and may be quoted. Example:
[email protected](guile-user)> (string->argv "1 2\\ \\\"3 '4 \"5' \"6 '7\"") $1 = ("1" "2 \"3" "4 \"5" "6 '7")
message is the error message as a format string, like the
fmt argument to the
format Scheme function.
See Formatted Output in GNU Guile Reference Manual.
args is a list of the optional arguments of
This is used when the command detects a user error of some kind, say a bad command argument.
(gdb) guile (use-modules (gdb)) (gdb) guile (register-command! (make-command "test-user-error" #:command-class COMMAND_OBSCURE #:invoke (lambda (self arg from-tty) (throw-user-error "Bad argument ~a" arg)))) end (gdb) test-user-error ugh ERROR: Bad argument ugh
completer option to
make-command is a procedure,
it takes three arguments:
self which is the
word which are both strings.
text holds the complete command line up to the cursor’s
location. The argument
word holds the last word of the command line;
this is computed using a word-breaking heuristic.
This procedure can return several kinds of values:
completerto ensure that the contents actually do complete the word. An empty list is allowed, it means that there were no completions available. Only string elements of the list are used; other elements in the list are ignored.
<gdb:iterator>object, it is iterated over to obtain the completions. It is up to
completer-procedureto ensure that the results actually do complete the word. Only string elements of the result are used; other elements in the sequence are ignored.
When a new command is registered, it will have been declared as a member of
some general class of commands. This is used to classify top-level
commands in the on-line help system; note that prefix commands are not
listed under their own category but rather that of their top-level
command. The available classifications are represented by constants
defined in the
continueare in this category. Type help running at the GDB prompt to see a list of commands in this category.
returnare in this category. Type help stack at the GDB prompt to see a list of commands in this category.
sectionare in this category. Type help files at the GDB prompt to see a list of commands in this category.
shellare in this category. Type help support at the GDB prompt to see a list of commands in this category.
info’-related command, that is, related to the state of GDB itself. For example,
showare in this category. Type help status at the GDB prompt to see a list of commands in this category.
deleteare in this category. Type help breakpoints at the GDB prompt to see a list of commands in this category.
tfindare in this category. Type help tracepoints at the GDB prompt to see a list of commands in this category.
stopare in this category. Type help obscure at the GDB prompt to see a list of commands in this category.
flushregscommands are in this category. Type help internals at the GDB prompt to see a list of commands in this category.
A new command can use a predefined completion function, either by
specifying it via an argument at initialization, or by returning it
completer procedure. These predefined completion
constants are all defined in the
The following code snippet shows how a trivial CLI command can be implemented in Guile:
(gdb) guile (register-command! (make-command "hello-world" #:command-class COMMAND_USER #:doc "Greet the whole world." #:invoke (lambda (self args from-tty) (display "Hello, World!\n")))) end (gdb) hello-world Hello, World!