Previous: Exception Handling, Up: Python API


22.2.2.3 Values From Inferior

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.

For pointer data types, gdb.Value provides a method for dereferencing the pointer to obtain the object it points to.

— Method on Value: dereference

This method returns a new gdb.Value object whose contents is the object pointed to by the pointer. For example, if foo is a C pointer to an int, declared in your C program as

          int *foo;

then you can use the corresponding gdb.Value to access what foo points to like this:

          bar = foo.dereference ()

The result bar will be a gdb.Value object holding the value pointed to by foo.