Values From Inferior (Debugging with GDB)
Next: Types In Python, Previous: Exception Handling, Up: Python API [Contents][Index]
23.3.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. Valid Python operations can also be performed on gdb.Value objects representing a struct or class object. For such cases, the overloaded operator (if present), is used to perform the operation. For example, if val1 and val2 are gdb.Value objects representing instances of a class which overloads the + operator, then one can use the + operator in their Python script as follows:
val3 = val1 + val2
The result of the operation val3 is also a gdb.Value object corresponding to the value returned by the overloaded + operator. In general, overloaded operators are invoked for the following operations: + (binary addition), - (binary subtraction), * (multiplication), /, %, <<, >>, |, &, ^.
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. Structure elements can also be accessed by using gdb.Field objects as subscripts (see Types In Python, for more information on gdb.Field objects). For example, if foo_field is a gdb.Field object corresponding to element foo of the above structure, then bar can also be accessed as follows:
bar = some_val[foo_field]
A gdb.Value that represents a function can be executed via inferior function call. Any arguments provided to the call must match the function’s prototype, and must be provided in the order specified by that prototype.
For example, some_val is a gdb.Value instance representing a function that takes two integers as arguments. To execute this function, call it like so:
result = some_val (10,20)
Any values returned from a function call will be stored as a gdb.Value.
The following attributes are provided:
- Variable
- Value.address
- If this object is addressable, this read-only attribute holds a
gdb.Valueobject representing the address. Otherwise, this attribute holdsNone.
- Variable
- Value.is_optimized_out
- This read-only boolean attribute is true if the compiler optimized out this value, thus it is not available for fetching from the inferior.
- Variable
- Value.type
- The type of this
gdb.Value. The value of this attribute is agdb.Typeobject (see Types In Python).
- Variable: Value.dynamic_type
The dynamic type of this
gdb.Value. This uses the object’s virtual table and the C++ run-time type information (RTTI) to determine the dynamic type of the value. If this value is of class type, it will return the class in which the value is embedded, if any. If this value is of pointer or reference to a class type, it will compute the dynamic type of the referenced object, and return a pointer or reference to that type, respectively. In all other cases, it will return the value’s static type.Note that this feature will only work when debugging a C++ program that includes RTTI for the object in question. Otherwise, it will just return the static type of the value as in ptype foo (see ptype).
- Variable: Value.is_lazy
The value of this read-only boolean attribute is
Trueif thisgdb.Valuehas not yet been fetched from the inferior. GDB does not fetch values until necessary, for efficiency. For example:myval = gdb.parse_and_eval ('somevar')The value of
somevaris not fetched at this time. It will be fetched when the value is needed, or when thefetch_lazymethod is invoked.
The following methods are provided:
- Function
- Value.__init__ (val)
- Many Python values can be converted directly to a
gdb.Valuevia this object initializer. Specifically:
- Python boolean
- A Python boolean is converted to the boolean type from the current language.
- Python integer
- A Python integer is converted to the C
longtype for the current architecture. - Python long
- A Python long is converted to the C
long longtype for the current architecture. - Python float
- A Python float is converted to the C
doubletype for the current architecture. - Python string
- A Python string is converted to a target string in the current target language using the current target encoding. If a character cannot be represented in the current target encoding, then an exception is thrown.
gdb.Value- If
valis agdb.Value, then a copy of the value is made. gdb.LazyString- If
valis agdb.LazyString(see Lazy Strings In Python), then the lazy string’svaluemethod is called, and its result is used.
- Function
- Value.__init__ (val, type)
- This second form of the
gdb.Valueconstructor returns agdb.Valueof typetypewhere the value contents are taken from the Python buffer object specified byval. The number of bytes in the Python buffer object must be greater than or equal to the size oftype.
- Function
- Value.cast (type)
- Return a new instance of
gdb.Valuethat is the result of casting this instance to the type described bytype, which must be agdb.Typeobject. If the cast cannot be performed for some reason, this method throws an exception.
- Function: Value.dereference ()
For pointer data types, this method returns a new
gdb.Valueobject whose contents is the object pointed to by the pointer. For example, iffoois a C pointer to anint, declared in your C program asint *foo;
then you can use the corresponding
gdb.Valueto access whatfoopoints to like this:bar = foo.dereference ()
The result
barwill be agdb.Valueobject holding the value pointed to byfoo.A similar function
Value.referenced_valueexists which also returnsgdb.Valueobjects corresponding to the values pointed to by pointer values (and additionally, values referenced by reference values). However, the behavior ofValue.dereferencediffers fromValue.referenced_valueby the fact that the behavior ofValue.dereferenceis identical to applying the C unary operator*on a given value. For example, consider a reference to a pointerptrref, declared in your C++ program astypedef int *intptr; ... int val = 10; intptr ptr = &val; intptr &ptrref = ptr;
Though
ptrrefis a reference value, one can apply the methodValue.dereferenceto thegdb.Valueobject corresponding to it and obtain agdb.Valuewhich is identical to that corresponding toval. However, if you apply the methodValue.referenced_value, the result would be agdb.Valueobject identical to that corresponding toptr.py_ptrref = gdb.parse_and_eval ("ptrref") py_val = py_ptrref.dereference () py_ptr = py_ptrref.referenced_value ()The
gdb.Valueobjectpy_valis identical to that corresponding toval, andpy_ptris identical to that corresponding toptr. In general,Value.dereferencecan be applied whenever the C unary operator*can be applied to the corresponding C value. For those cases where applying bothValue.dereferenceandValue.referenced_valueis allowed, the results obtained need not be identical (as we have seen in the above example). The results are however identical when applied ongdb.Valueobjects corresponding to pointers (gdb.Valueobjects with type codeTYPE_CODE_PTR) in a C/C++ program.
- Function: Value.referenced_value ()
For pointer or reference data types, this method returns a new
gdb.Valueobject corresponding to the value referenced by the pointer/reference value. For pointer data types,Value.dereferenceandValue.referenced_valueproduce identical results. The difference between these methods is thatValue.dereferencecannot get the values referenced by reference values. For example, consider a reference to anint, declared in your C++ program asint val = 10; int &ref = val;
then applying
Value.dereferenceto thegdb.Valueobject corresponding torefwill result in an error, while applyingValue.referenced_valuewill result in agdb.Valueobject identical to that corresponding toval.py_ref = gdb.parse_and_eval ("ref") er_ref = py_ref.dereference () # Results in error py_val = py_ref.referenced_value () # Returns the referenced valueThe
gdb.Valueobjectpy_valis identical to that corresponding toval.
- Function
- Value.reference_value ()
- Return a
gdb.Valueobject which is a reference to the value encapsulated by this instance.
- Function
- Value.const_value ()
- Return a
gdb.Valueobject which is aconstversion of the value encapsulated by this instance.
- Function
- Value.dynamic_cast (type)
- Like
Value.cast, but works as if the C++dynamic_castoperator were used. Consult a C++ reference for details.
- Function
- Value.reinterpret_cast (type)
- Like
Value.cast, but works as if the C++reinterpret_castoperator were used. Consult a C++ reference for details.
- Function: Value.format_string (...)
Convert a
gdb.Valueto a string, similarly to what theprintcommand does. Invoked with no arguments, this is equivalent to calling thestrfunction on thegdb.Value. The representation of the same value may change across different versions of GDB, so you shouldn’t, for instance, parse the strings returned by this method.All the arguments are keyword only. If an argument is not specified, the current global default setting is used.
rawTrueif pretty-printers (see Pretty Printing) should not be used to format the value.Falseif enabled pretty-printers matching the type represented by thegdb.Valueshould be used to format it.pretty_arraysTrueif arrays should be pretty printed to be more convenient to read,Falseif they shouldn’t (seeset print arrayin Print Settings).pretty_structsTrueif structs should be pretty printed to be more convenient to read,Falseif they shouldn’t (seeset print prettyin Print Settings).array_indexesTrueif array indexes should be included in the string representation of arrays,Falseif they shouldn’t (seeset print array-indexesin Print Settings).symbolsTrueif the string representation of a pointer should include the corresponding symbol name (if one exists),Falseif it shouldn’t (seeset print symbolin Print Settings).unionsTrueif unions which are contained in other structures or unions should be expanded,Falseif they shouldn’t (seeset print unionin Print Settings).addressTrueif the string representation of a pointer should include the address,Falseif it shouldn’t (seeset print addressin Print Settings).deref_refsTrueif C++ references should be resolved to the value they refer to,False(the default) if they shouldn’t. Note that, unlike for theprintcommand, references are not automatically expanded when using theformat_stringmethod or thestrfunction. There is no globalprintsetting to change the default behaviour.actual_objectsTrueif the representation of a pointer to an object should identify the actual (derived) type of the object rather than the declared type, using the virtual function table.Falseif the declared type should be used. (Seeset print objectin Print Settings).static_membersTrueif static members should be included in the string representation of a C++ object,Falseif they shouldn’t (seeset print static-membersin Print Settings).max_elementsNumber of array elements to print, or
0to print an unlimited number of elements (seeset print elementsin Print Settings).max_depthThe maximum depth to print for nested structs and unions, or
-1to print an unlimited number of elements (seeset print max-depthin Print Settings).repeat_thresholdSet the threshold for suppressing display of repeated array elements, or
0to represent all elements, even if repeated. (Seeset print repeatsin Print Settings).formatA string containing a single character representing the format to use for the returned string. For instance,
'x'is equivalent to using the GDB commandprintwith the/xoption and formats the value as a hexadecimal number.
- Function: Value.string ([encoding[, errors[, length]]])
If this
gdb.Valuerepresents a string, then this method converts the contents to a Python string. Otherwise, this method will throw an exception.Values are interpreted as strings according to the rules of the current language. If the optional length argument is given, the string will be converted to that length, and will include any embedded zeroes that the string may contain. Otherwise, for languages where the string is zero-terminated, the entire string will be converted.
For example, in C-like languages, a value is a string if it is a pointer to or an array of characters or ints of type
wchar_t,char16_t, orchar32_t.If the optional
encodingargument is given, it must be a string naming the encoding of the string in thegdb.Value, such as"ascii","iso-8859-6"or"utf-8". It accepts the same encodings as the corresponding argument to Python’sstring.decodemethod, and the Python codec machinery will be used to convert the string. Ifencodingis not given, or ifencodingis 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
errorsargument is the same as the corresponding argument to Python’sstring.decodemethod.If the optional
lengthargument is given, the string will be fetched and converted to the given length.
- Function: Value.lazy_string ([encoding [, length]])
If this
gdb.Valuerepresents a string, then this method converts the contents to agdb.LazyString(see Lazy Strings In Python). Otherwise, this method will throw an exception.If the optional
encodingargument is given, it must be a string naming the encoding of thegdb.LazyString. Some examples are: ‘ascii’, ‘iso-8859-6’ or ‘utf-8’. If theencodingargument is an encoding that GDB does recognize, GDB will raise an error.When a lazy string is printed, the GDB encoding machinery is used to convert the string during printing. If the optional
encodingargument is not provided, or is an empty string, GDB will automatically select the encoding most suitable for the string type. For further information on encoding in GDB please see Character Sets.If the optional
lengthargument is given, the string will be fetched and encoded to the length of characters specified. If thelengthargument is not provided, the string will be fetched and encoded until a null of appropriate width is found.
- Function: Value.fetch_lazy ()
If the
gdb.Valueobject is currently a lazy value (gdb.Value.is_lazyisTrue), then the value is fetched from the inferior. Any errors that occur in the process will produce a Python exception.If the
gdb.Valueobject is not a lazy value, this method has no effect.This method does not return a value.
Next: Types In Python, Previous: Exception Handling, Up: Python API [Contents][Index]