You can implement new gdb parameters using Python. A new
parameter is implemented as an instance of the gdb.Parameter
class.
Parameters are exposed to the user via the set
and
show
commands. See Help.
There are many parameters that already exist and can be set in
gdb. Two examples are: set follow fork
and
set charset
. Setting these parameters influences certain
behavior in gdb. Similarly, you can define parameters that
can be used to influence behavior in custom Python scripts and commands.
The object initializer for
Parameter
registers the new parameter with gdb. This initializer is normally invoked from the subclass' own__init__
method.name is the name of the new parameter. If name consists of multiple words, then the initial words are looked for as prefix parameters. An example of this can be illustrated with the
set print
set of parameters. If name isprint foo
, thenset print foo
.If name consists of multiple words, and no prefix parameter group can be found, an exception is raised.
command-class should be one of the ‘COMMAND_’ constants (see Commands In Python). This argument tells gdb how to categorize the new parameter in the help system.
parameter-class should be one of the ‘PARAM_’ constants defined below. This argument tells gdb the type of the new parameter; this information is used for input validation and completion.
If parameter-class is
PARAM_ENUM
, then enum-sequence must be a sequence of strings. These strings represent the possible values for the parameter.If parameter-class is not
PARAM_ENUM
, then the presence of a fourth argument will cause an exception to be thrown.The help text for the new parameter is taken from the Python documentation string for the parameter's class, if there is one. If there is no documentation string, a default value is used.
If this attribute exists, and is a string, then its value is used as the help text for this parameter's
set
command. The value is examined whenParameter.__init__
is invoked; subsequent changes have no effect.
If this attribute exists, and is a string, then its value is used as the help text for this parameter's
show
command. The value is examined whenParameter.__init__
is invoked; subsequent changes have no effect.
The
value
attribute holds the underlying value of the parameter. It can be read and assigned to just as any other attribute. gdb does validation when assignments are made.
There are two methods that may be implemented in any Parameter
class. These are:
If this method exists, gdb will call it when a parameter's value has been changed via the
set
API (for example, set foo off). Thevalue
attribute has already been populated with the new value and may be used in output. This method must return a string. If the returned string is not empty, gdb will present it to the user.If this method raises the
gdb.GdbError
exception (see Exception Handling), then gdb will print the exception's string and theset
command will fail. Note, however, that thevalue
attribute will not be reset in this case. So, if your parameter must validate values, it should store the old value internally and reset the exposed value, like so:class ExampleParam (gdb.Parameter): def __init__ (self, name): super (ExampleParam, self).__init__ (name, gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN) self.value = True self.saved_value = True def validate(self): return False def get_set_string (self): if not self.validate(): self.value = self.saved_value raise gdb.GdbError('Failed to validate') self.saved_value = self.value
gdb will call this method when a parameter's
show
API has been invoked (for example, show foo). The argumentsvalue
receives the string representation of the current value. This method must return a string.
When a new parameter is defined, its type must be specified. The
available types are represented by constants defined in the gdb
module:
gdb.PARAM_BOOLEAN
True
and False
are the only valid values.
gdb.PARAM_AUTO_BOOLEAN
None
.
gdb.PARAM_UINTEGER
gdb.PARAM_INTEGER
gdb.PARAM_STRING
gdb.PARAM_STRING_NOESCAPE
gdb.PARAM_OPTIONAL_FILENAME
None
.
gdb.PARAM_FILENAME
PARAM_STRING_NOESCAPE
, but uses file names for completion.
gdb.PARAM_ZINTEGER
PARAM_INTEGER
, except 0
is interpreted as itself.
gdb.PARAM_ZUINTEGER
PARAM_INTEGER
,
except 0 is interpreted as itself, and the value cannot be negative.
gdb.PARAM_ZUINTEGER_UNLIMITED
PARAM_ZUINTEGER
,
except the special value -1 should be interpreted to mean
“unlimited”. Other negative values are not allowed.
gdb.PARAM_ENUM