Next: Types In Python, Previous: Auto-loading, Up: Python API
gdb provides values it obtains from the inferior program in
an object of type gdb.Value
. gdb uses this object
for its internal bookkeeping of the inferior's values, and for
fetching values when necessary.
Inferior values that are simple scalars can be used directly in
Python expressions that are valid for the value's data type. Here's
an example for an integer or floating-point value some_val
:
bar = some_val + 2
As result of this, bar
will also be a gdb.Value
object
whose values are of the same type as those of some_val
.
Inferior values that are structures or instances of some class can
be accessed using the Python dictionary syntax. For example, if
some_val
is a gdb.Value
instance holding a structure, you
can access its foo
element with:
bar = some_val['foo']
Again, bar
will also be a gdb.Value
object.
The following attributes are provided:
If this object is addressable, this read-only attribute holds a
gdb.Value
object representing the address. Otherwise, this attribute holdsNone
.
The following methods are provided:
For pointer data types, this method returns a new
gdb.Value
object whose contents is the object pointed to by the pointer. For example, iffoo
is a C pointer to anint
, declared in your C program asint *foo;then you can use the corresponding
gdb.Value
to access whatfoo
points to like this:bar = foo.dereference ()The result
bar
will be agdb.Value
object holding the value pointed to byfoo
.
If this
gdb.Value
represents a string, then this method converts the contents to a Python string. Otherwise, this method will throw an exception.Strings are recognized in a language-specific way; whether a given
gdb.Value
represents a string is determined by the current language.For C-like languages, a value is a string if it is a pointer to or an array of characters or ints. The string is assumed to be terminated by a zero of the appropriate width.
If the optional encoding argument is given, it must be a string naming the encoding of the string in the
gdb.Value
, such as"ascii"
,"iso-8859-6"
or"utf-8"
. It accepts the same encodings as the corresponding argument to Python'sstring.decode
method, and the Python codec machinery will be used to convert the string. If encoding is not given, or if encoding is the empty string, then either thetarget-charset
(see Character Sets) will be used, or a language-specific encoding will be used, if the current language is able to supply one.The optional errors argument is the same as the corresponding argument to Python's
string.decode
method.