-# Copyright (C) 2009-2018 Free Software Foundation, Inc.
+# Copyright (C) 2009-2019 Free Software Foundation, Inc.
#
# This file is part of GDB.
#
import gdb
import os.path
+
class TypeFlag:
"""A class that allows us to store a flag name, its short name,
and its value.
value: The associated value.
short_name: The enumeration name, with the suffix stripped.
"""
+
def __init__(self, name, value):
self.name = name
self.value = value
def __lt__(self, other):
"""Sort by value order."""
return self.value < other.value
-
+
# A list of all existing TYPE_INSTANCE_FLAGS_* enumerations,
# stored as TypeFlags objects. Lazy-initialized.
TYPE_FLAGS = None
+
class TypeFlagsPrinter:
"""A class that prints a decoded form of an instance_flags value.
If not, then printing of the instance_flag is going to be degraded,
but it's not a fatal error.
"""
+
def __init__(self, val):
self.val = val
+
def __str__(self):
global TYPE_FLAGS
if TYPE_FLAGS is None:
else:
flag_list = ["???"]
return "0x%x [%s]" % (self.val, "|".join(flag_list))
+
def init_TYPE_FLAGS(self):
"""Initialize the TYPE_FLAGS global as a list of TypeFlag objects.
This operation requires the search of a couple of enumeration types.
for field in iflags.fields()]
TYPE_FLAGS.sort()
+
class StructTypePrettyPrinter:
"""Pretty-print an object of type struct type"""
+
def __init__(self, val):
self.val = val
+
def to_string(self):
fields = []
fields.append("pointer_type = %s" % self.val['pointer_type'])
fields.append("main_type = %s" % self.val['main_type'])
return "\n{" + ",\n ".join(fields) + "}"
+
class StructMainTypePrettyPrinter:
"""Pretty-print an objet of type main_type"""
+
def __init__(self, val):
self.val = val
+
def flags_to_string(self):
"""struct main_type contains a series of components that
are one-bit ints whose name start with "flag_". For instance:
"""
fields = [field.name.replace("flag_", "")
for field in self.val.type.fields()
- if field.name.startswith("flag_")
- and self.val[field.name]]
+ if field.name.startswith("flag_") and self.val[field.name]]
return "|".join(fields)
+
def owner_to_string(self):
"""Return an image of component "owner".
"""
return "%s (objfile)" % self.val['owner']['objfile']
else:
return "%s (gdbarch)" % self.val['owner']['gdbarch']
+
def struct_field_location_img(self, field_val):
"""Return an image of the loc component inside the given field
gdb.Value.
return 'dwarf_block = %s' % loc_val['dwarf_block']
else:
return 'loc = ??? (unsupported loc_kind value)'
+
def struct_field_img(self, fieldno):
"""Return an image of the main_type field number FIELDNO.
"""
fields.append("bitsize = %d" % f['bitsize'])
fields.append(self.struct_field_location_img(f))
return label + "\n" + " {" + ",\n ".join(fields) + "}"
+
+ def bound_img(self, bound_name):
+ """Return an image of the given main_type's bound."""
+ b = self.val['flds_bnds']['bounds'].dereference()[bound_name]
+ bnd_kind = str(b['kind'])
+ if bnd_kind == 'PROP_CONST':
+ return str(b['data']['const_val'])
+ elif bnd_kind == 'PROP_UNDEFINED':
+ return '(undefined)'
+ else:
+ info = [bnd_kind]
+ if bound_name == 'high' and b['flag_upper_bound_is_count']:
+ info.append('upper_bound_is_count')
+ return '{} ({})'.format(str(b['data']['baton']), ','.join(info))
+
def bounds_img(self):
"""Return an image of the main_type bounds.
"""
b = self.val['flds_bnds']['bounds'].dereference()
- low = str(b['low'])
- if b['low_undefined'] != 0:
- low += " (undefined)"
- high = str(b['high'])
- if b['high_undefined'] != 0:
- high += " (undefined)"
- return "flds_bnds.bounds = {%s, %s}" % (low, high)
+ low = self.bound_img('low')
+ high = self.bound_img('high')
+
+ img = "flds_bnds.bounds = {%s, %s}" % (low, high)
+ if b['flag_bound_evaluated']:
+ img += ' [evaluated]'
+ return img
+
def type_specific_img(self):
"""Return a string image of the main_type type_specific union.
Only the relevant component of that union is printed (based on
"""
fields = []
fields.append("name = %s" % self.val['name'])
- fields.append("tag_name = %s" % self.val['tag_name'])
fields.append("code = %s" % self.val['code'])
fields.append("flags = [%s]" % self.flags_to_string())
fields.append("owner = %s" % self.owner_to_string())
return "\n{" + ",\n ".join(fields) + "}"
+
+class CoreAddrPrettyPrinter:
+ """Print CORE_ADDR values as hex."""
+
+ def __init__(self, val):
+ self._val = val
+
+ def to_string(self):
+ return hex(int(self._val))
+
+
def type_lookup_function(val):
"""A routine that returns the correct pretty printer for VAL
if appropriate. Returns None otherwise.
return StructTypePrettyPrinter(val)
elif val.type.tag == "main_type":
return StructMainTypePrettyPrinter(val)
+ elif val.type.name == 'CORE_ADDR':
+ return CoreAddrPrettyPrinter(val)
return None
+
def register_pretty_printer(objfile):
"""A routine to register a pretty-printer against the given OBJFILE.
"""
objfile.pretty_printers.append(type_lookup_function)
+
if __name__ == "__main__":
if gdb.current_objfile() is not None:
# This is the case where this script is being "auto-loaded"