[commit/python] Enhance gdb-gdb.py to handle main_type.type_specific.

Joel Brobecker brobecker@adacore.com
Fri Jan 15 08:59:00 GMT 2010


Hello,

Now that we have the discriminant for the type_specific union,
it's a lot easier to print the correct data in this union.
Since I wanted to take a break without actually doing nothing
(in sports, we call this an "active rest" ;-), I decided to implement
something simple. Basically: I print the correct field name, and
a pointer to the data.  For instance, for a struct that has cplus_stuff,
you'll just get:

    (gdb) print *type.main_type
    $1 =
    {name = 0x7f34d0c4076b "geo__T5b",
     tag_name = 0x7f34d0c4076b "geo__T5b",
     code = TYPE_CODE_STRUCT,
     [...]
     cplus_stuff = 0xc139e0}

I decided to not the contents of the cplus_stuff, because the current
pretty-printer can generate a lot of output.  So for now, to pretty
print the cplus_stuff, the user will have to explicitly dereference
this field:

    (gdb) print *type.main_type.type_specific.cplus_stuff

(and we will have to implement the associated pretty-printer :-).

There is one exception, however, for the gnat_stuff, since the
gnat_stuff is really just one field, so I printed it on the same
line.  For instance, showing the whole type, now:

    (top-gdb) p *type.main_type
    $1 =
    {name = 0x7f34d0c4076b "geo__T5b",
     tag_name = 0x7f34d0c4076b "geo__T5b",
     code = TYPE_CODE_STRUCT,
     flags = [stub_supported|objfile_owned],
     owner = 0x1671600 (objfile),
     target_type = 0x0,
     vptr_basetype = 0x0,
     field[0]:
      {name = 0x7f34d0c3fb9a "_parent",
       type = 0x16fa2c0,
       loc_kind = FIELD_LOC_KIND_BITPOS,
       bitsize = 0,
       bitpos = 0},
     field[1]:
      {name = 0x7f34d0c4c9aa "C6b",
       type = 0x16fd490,
       loc_kind = FIELD_LOC_KIND_BITPOS,
       bitsize = 0,
       bitpos = 128},
     gnat_stuff = {descriptive_type = 0x16fd370}}

gdb/ChangeLog:

        * gdb-gdb.py: Print the type-specific part of struct main_type.

Will commit in a moment.
-- 
Joel

---
 gdb/gdb-gdb.py |   31 ++++++++++++++++++++++++++-----
 1 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/gdb/gdb-gdb.py b/gdb/gdb-gdb.py
index 49695b5..44133ab 100644
--- a/gdb/gdb-gdb.py
+++ b/gdb/gdb-gdb.py
@@ -187,6 +187,30 @@ class StructMainTypePrettyPrinter:
         if b['high_undefined'] != 0:
             high += " (undefined)"
         return "bounds = {%s, %s}" % (low, high)
+    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
+        the value of the type_specific_kind field.
+        """
+        type_specific_kind = str(self.val['type_specific_field'])
+        type_specific = self.val['type_specific']
+        if type_specific_kind == "TYPE_SPECIFIC_NONE":
+            img = 'type_specific_field = %s' % type_specific_kind
+        elif type_specific_kind == "TYPE_SPECIFIC_CPLUS_STUFF":
+            img = "cplus_stuff = %s" % type_specific['cplus_stuff']
+        elif type_specific_kind == "TYPE_SPECIFIC_GNAT_STUFF":
+            img = ("gnat_stuff = {descriptive_type = %s}"
+                   % type_specific['gnat_stuff']['descriptive_type'])
+        elif type_specific_kind == "TYPE_SPECIFIC_FLOATFORMAT":
+            img = "floatformat[0..1] = %s" % type_specific['floatformat']
+        elif type_specific_kind == "TYPE_SPECIFIC_CALLING_CONVENTION":
+            img = ("calling_convention = %d"
+                   % type_specific['calling_convention'])
+        else:
+            img = ("type_specific = ??? (unknown type_secific_kind: %s)"
+                   % type_specific_kind)
+        return img
+
     def to_string(self):
         """Return a pretty-printed image of our main_type.
         """
@@ -200,14 +224,11 @@ class StructMainTypePrettyPrinter:
         fields.append("vptr_basetype = %s" % self.val['vptr_basetype'])
         if self.val['nfields'] > 0:
             for fieldno in range(self.val['nfields']):
-                fields.append("field[%d]:")
                 fields.append(self.struct_field_img(fieldno))
         if self.val.type.code == gdb.TYPE_CODE_RANGE:
             fields.append(self.bounds_img())
-        # FIXME: We need to print the type_specific field as well.
-        # But I will wait for a patch that introduces a discriminant.
-        # This will simplify the selection of the right component in
-        # that union.
+        fields.append(self.type_specific_img())
+
         return "\n{" + ",\n ".join(fields) + "}"
 
 def type_lookup_function(val):
-- 
1.6.3.3



More information about the Gdb-patches mailing list