Debian Codegraph @

tools/gdb/libstdcxx_printers.py

raw
1 # Pretty printing of C++ STL 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 C++ STL 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 The C++ STL pretty printers support libstdc++ 6.0.13 of GCC 4.4.4.
24
25 These pretty printers should not be used with GCC 4.5 or later,
26 because those versions of libstdc++ already include pretty printers
27 maintained as part of the library and therefore better up to date.
28 According to the documentation, GDB looks for pretty printers first
29 in the objfiles, then in the progspace, and last in the gdb module;
30 thus, if you register these pretty printers in the gdb module and
31 let GDB register the pretty printers of libstdc++ in the objfile of
32 libstdc++, then they should automatically take precedence over these.
33 """
34
35 import gdb
36 import re
37
38 class StdStringPrinter:
39 "Pretty-print an std::string."
40
41 __typeRegex = re.compile("^std::basic_string<.*>$")
42
43 def __init__(self, val):
44 object.__init__(self)
45 self.val = val
46
47 def display_hint(self):
48 "Called by GDB to find how to display the value and children."
49 return "string"
50
51 def to_string(self):
52 "Called by GDB to find what to display as the value."
53 data = self.val["_M_dataplus"]["_M_p"]
54 if data == 0:
55 return data
56 repType = gdb.lookup_type(str(self.val.type.strip_typedefs().unqualified()) + "::_Rep")
57 rep = data.cast(repType.pointer()) - 1
58 return data.string(length = rep["_M_length"])
59
60 @staticmethod
61 def lookup_printer(val):
62 "Return a printer for the value, if supported; else None."
63 tag = val.type.strip_typedefs().tag
64 if tag and StdStringPrinter.__typeRegex.match(tag):
65 return StdStringPrinter(val)
66
67 class StdVectorPrinter:
68 "Pretty-print an std::vector."
69
70 __typeRegex = re.compile("^std::vector<.*>$")
71
72 def __init__(self, val):
73 object.__init__(self)
74 self.val = val
75
76 def children(self):
77 "Called by GDB to enumerate the children of the value."
78 start = self.val["_M_impl"]["_M_start"]
79 finish = self.val["_M_impl"]["_M_finish"]
80 for index in range(finish - start):
81 element = (start + index).dereference()
82 yield "[%d]" % index, element
83
84 def display_hint(self):
85 "Called by GDB to find how to display the value and children."
86 return "array"
87
88 def to_string(self):
89 "Called by GDB to find what to display as the value."
90 return "std::vector"
91
92 @staticmethod
93 def lookup_printer(val):
94 "Return a printer for the value, if supported; else None."
95 tag = val.type.tag
96 if tag and StdVectorPrinter.__typeRegex.match(tag):
97 return StdVectorPrinter(val)
98
99 class StdListPrinter:
100 "Pretty-print an std::list."
101
102 __typeRegex = re.compile("^std::list<.*>$")
103
104 def __init__(self, val):
105 object.__init__(self)
106 self.val = val
107
108 def children(self):
109 "Called by GDB to enumerate the children of the value."
110 elementType = self.val.type.template_argument(0)
111 nodeTypeName = "std::_List_node<%s >" % elementType
112 nodeType = gdb.lookup_type(nodeTypeName)
113 nodePointerType = nodeType.pointer()
114 sentinelPointer = self.val["_M_impl"]["_M_node"].address
115 nodeBasePointer = sentinelPointer
116 while nodeBasePointer.dereference()["_M_next"] != sentinelPointer:
117 nodeBasePointer = nodeBasePointer.dereference()["_M_next"]
118 nodePointer = nodeBasePointer.cast(nodePointerType)
119 yield "element", nodePointer.dereference()["_M_data"]
120
121 def display_hint(self):
122 "Called by GDB to find how to display the value and children."
123 return "array"
124
125 def to_string(self):
126 "Called by GDB to find what to display as the value."
127 return "std::list"
128
129 @staticmethod
130 def lookup_printer(val):
131 "Return a printer for the value, if supported; else None."
132 tag = val.type.tag
133 if tag and StdListPrinter.__typeRegex.match(tag):
134 return StdListPrinter(val)
135
136 class StdMapPrinter:
137 "Pretty-print an std::map."
138
139 __typeRegex = re.compile("^std::map<.*>$")
140
141 def __init__(self, val):
142 object.__init__(self)
143 self.val = val
144
145 def children(self):
146 "Called by GDB to enumerate the children of the value."
147 keyType = self.val.type.template_argument(0)
148 valueType = self.val.type.template_argument(1)
149 nodeTypeName = "std::_Rb_tree_node<std::pair<%s const, %s > >" \
150 % (keyType, valueType)
151 nodeType = gdb.lookup_type(nodeTypeName)
152 nodePointerType = nodeType.pointer()
153 def recurse(nodeBasePointer):
154 if nodeBasePointer != 0:
155 nodeBase = nodeBasePointer.dereference()
156 node = nodeBasePointer.cast(nodePointerType).dereference()
157 for tag, data in recurse(nodeBase["_M_left"]):
158 yield tag, data
159 yield "key", node["_M_value_field"]["first"]
160 yield "value", node["_M_value_field"]["second"]
161 for tag, data in recurse(nodeBase["_M_right"]):
162 yield tag, data
163 return recurse(self.val["_M_t"]["_M_impl"]["_M_header"]["_M_parent"])
164
165 def display_hint(self):
166 "Called by GDB to find how to display the value and children."
167 return "map"
168
169 def to_string(self):
170 "Called by GDB to find what to display as the value."
171 return "std::map"
172
173 @staticmethod
174 def lookup_printer(val):
175 "Return a printer for the value, if supported; else None."
176 tag = val.type.tag
177 if tag and StdMapPrinter.__typeRegex.match(tag):
178 return StdMapPrinter(val)
179
180 def register_printers(objfile):
181 """Register all the pretty printers defined by this module.
182 OBJFILE may be an objfile, a progspace, or the entire gdb module."""
183 objfile.pretty_printers.append(StdStringPrinter.lookup_printer)
184 objfile.pretty_printers.append(StdVectorPrinter.lookup_printer)
185 objfile.pretty_printers.append(StdListPrinter.lookup_printer)
186 objfile.pretty_printers.append(StdMapPrinter.lookup_printer)