Debian Codegraph @

tools/gdb/boswars_printers.py

raw
1 # Pretty printing of Bos Wars objects in GDB.
2 #
3 # (c) Copyright 2010 by Kalle Olavi Niemitalo.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 """
20 Pretty printing of Bos Wars objects in the GNU Debugger.
21 This requires GDB 7.0 or later, configured --with-python.
22 The code was written for Python 2.5.5 but may work with earlier versions too.
23 """
24
25 import gdb
26
27 class COrderPrinter:
28 "Pretty-print a COrder."
29
30 def __init__(self, val):
31 object.__init__(self)
32 self.val = val
33
34 def children(self):
35 "Called by GDB to enumerate the children of the value."
36 for field in self.val.type.fields():
37 if field.name == "Action":
38 # COrder::Action is defined as unsigned char, but its
39 # possible values are listed in enum _unit_action_.
40 yield field.name, self.val[field.name].cast(gdb.lookup_type("enum _unit_action_"))
41 elif field.name == "Width" or field.name == "Height":
42 # These are unsigned char but cast them to int so
43 # gdb won't try to display them as e.g. '\0'.
44 yield field.name, self.val[field.name].cast(gdb.lookup_type("int"))
45 elif field.name == "Arg1":
46 # COrder::Action shows which member of the union is used.
47 for enum in gdb.lookup_type("enum _unit_action_").fields():
48 if enum.bitpos == self.val["Action"]:
49 if enum.name == "UnitActionPatrol":
50 yield "Arg1.Patrol", self.val["Arg1"]["Patrol"];
51 elif enum.name == "UnitActionSpellCast":
52 yield "Arg1.Spell", self.val["Arg1"]["Spell"];
53 else:
54 yield field.name, self.val[field.name]
55
56 def to_string(self):
57 "Called by GDB to find what to display as the value."
58 return "COrder"
59
60 class CUnitTypePrinter:
61 "Pretty-print a CUnitType."
62
63 def __init__(self, val):
64 object.__init__(self)
65 self.val = val
66
67 def children(self):
68 "Called by GDB to enumerate the children of the value."
69 for field in self.val.type.fields():
70 if field.name == "FieldFlags" or field.name == "MovementMask":
71 # These are flags, so print in hexadecimal.
72 yield field.name, "%#x" % self.val[field.name]
73 else:
74 yield field.name, self.val[field.name]
75
76 def to_string(self):
77 "Called by GDB to find what to display as the value."
78 return "CUnitType"
79
80 class COrderPointerPrinter:
81 "Pretty-print a pointer to COrder."
82
83 def __init__(self, val):
84 object.__init__(self)
85 self.val = val
86
87 def to_string(self):
88 "Called by GDB to find what to display as the value."
89 voidPointerType = gdb.lookup_type("void").pointer()
90 # str(self.val) would cause a recursive call; the cast avoids that.
91 result = str(self.val.cast(voidPointerType))
92 if self.val != 0:
93 order = self.val.dereference()
94 action = order["Action"].cast(gdb.lookup_type("enum _unit_action_"))
95 result = result + (" {%s}" % action)
96 return result
97
98 class CPatchPointerPrinter:
99 "Pretty-print a pointer to CPatch."
100
101 def __init__(self, val):
102 object.__init__(self)
103 self.val = val
104
105 def to_string(self):
106 "Called by GDB to find what to display as the value."
107 voidPointerType = gdb.lookup_type("void").pointer()
108 # str(self.val) would cause a recursive call; the cast avoids that.
109 result = str(self.val.cast(voidPointerType))
110 if self.val != 0:
111 patch = self.val.dereference()
112 result = result + (" {(%d,%d)" % (patch["x"], patch["y"]))
113 patchTypePointer = patch["type"]
114 if patchTypePointer != 0:
115 patchType = patchTypePointer.dereference()
116 result = result + (" %s" % patchType["name"])
117 result = result + "}"
118 return result
119
120 class StructPointerPrinter:
121 "Pretty-print a pointer to a structure that has one important member."
122
123 def __init__(self, val, memberName, indirectMemberName = None):
124 object.__init__(self)
125 self.val = val
126 self.memberName = memberName
127 self.indirectMemberName = indirectMemberName
128
129 def to_string(self):
130 "Called by GDB to find what to display as the value."
131 voidPointerType = gdb.lookup_type("void").pointer()
132 # str(self.val) would cause a recursive call; the cast avoids that.
133 result = str(self.val.cast(voidPointerType))
134 if self.val != 0:
135 memberValue = self.val.dereference()[self.memberName]
136 if self.indirectMemberName == None:
137 result = result + (" {%s}" % memberValue)
138 elif memberValue != 0:
139 memberValue = memberValue.dereference()[self.indirectMemberName]
140 result = result + (" {%s}" % memberValue)
141 return result
142
143 def lookup_printer(val):
144 "Return a printer for the value, if supported; else None."
145 type = val.type
146 if type.code == gdb.TYPE_CODE_STRUCT:
147 tag = type.tag # might be None
148 if tag == "COrder":
149 return COrderPrinter(val)
150 elif tag == "CUnitType":
151 return CUnitTypePrinter(val)
152 elif type.code == gdb.TYPE_CODE_PTR:
153 tag = type.target().tag # might be None
154 if tag == "CConstruction":
155 return StructPointerPrinter(val, "Ident")
156 elif tag == "CGraphic":
157 return StructPointerPrinter(val, "File")
158 elif tag == "CIcon":
159 return StructPointerPrinter(val, "Ident")
160 elif tag == "COrder":
161 return COrderPointerPrinter(val)
162 elif tag == "CPatch":
163 return CPatchPointerPrinter(val)
164 elif tag == "CPatchType":
165 return StructPointerPrinter(val, "name")
166 elif tag == "CPlayerColorGraphic":
167 return StructPointerPrinter(val, "File")
168 elif tag == "CUnit":
169 return StructPointerPrinter(val, "Type", "Ident")
170 elif tag == "CUnitType":
171 return StructPointerPrinter(val, "Ident")
172 elif tag == "Missile":
173 return StructPointerPrinter(val, "Type", "Ident")
174 elif tag == "MissileType":
175 return StructPointerPrinter(val, "Ident")
176
177 def register_printers(objfile):
178 """Register all the pretty printers defined by this module.
179 OBJFILE may be an objfile, a progspace, or the entire gdb module."""
180 objfile.pretty_printers.append(lookup_printer)