gdb/
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.py
1 # Copyright (C) 2008-2012 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the GDB testsuite. It tests python pretty
17 # printers.
18
19 import re
20 import gdb
21
22 # Test returning a Value from a printer.
23 class string_print:
24 def __init__(self, val):
25 self.val = val
26
27 def to_string(self):
28 return self.val['whybother']['contents']
29
30 # Test a class-based printer.
31 class ContainerPrinter:
32 class _iterator:
33 def __init__ (self, pointer, len):
34 self.start = pointer
35 self.pointer = pointer
36 self.end = pointer + len
37
38 def __iter__(self):
39 return self
40
41 def next(self):
42 if self.pointer == self.end:
43 raise StopIteration
44 result = self.pointer
45 self.pointer = self.pointer + 1
46 return ('[%d]' % int (result - self.start), result.dereference())
47
48 def __init__(self, val):
49 self.val = val
50
51 def to_string(self):
52 return 'container %s with %d elements' % (self.val['name'], self.val['len'])
53
54 def children(self):
55 return self._iterator(self.val['elements'], self.val['len'])
56
57 # Flag to make NoStringContainerPrinter throw an exception.
58 exception_flag = False
59
60 # Test a printer where to_string is None
61 class NoStringContainerPrinter:
62 class _iterator:
63 def __init__ (self, pointer, len):
64 self.start = pointer
65 self.pointer = pointer
66 self.end = pointer + len
67
68 def __iter__(self):
69 return self
70
71 def next(self):
72 if self.pointer == self.end:
73 raise StopIteration
74 if exception_flag:
75 raise gdb.MemoryError, 'hi bob'
76 result = self.pointer
77 self.pointer = self.pointer + 1
78 return ('[%d]' % int (result - self.start), result.dereference())
79
80 def __init__(self, val):
81 self.val = val
82
83 def to_string(self):
84 return None
85
86 def children(self):
87 return self._iterator(self.val['elements'], self.val['len'])
88
89 class pp_s:
90 def __init__(self, val):
91 self.val = val
92
93 def to_string(self):
94 a = self.val["a"]
95 b = self.val["b"]
96 if a.address != b:
97 raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b)))
98 return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
99
100 class pp_ss:
101 def __init__(self, val):
102 self.val = val
103
104 def to_string(self):
105 return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
106
107 class pp_sss:
108 def __init__(self, val):
109 self.val = val
110
111 def to_string(self):
112 return "a=<" + str(self.val['a']) + "> b=<" + str(self.val["b"]) + ">"
113
114 class pp_multiple_virtual:
115 def __init__ (self, val):
116 self.val = val
117
118 def to_string (self):
119 return "pp value variable is: " + str (self.val['value'])
120
121 class pp_vbase1:
122 def __init__ (self, val):
123 self.val = val
124
125 def to_string (self):
126 return "pp class name: " + self.val.type.tag
127
128 class pp_nullstr:
129 def __init__(self, val):
130 self.val = val
131
132 def to_string(self):
133 return self.val['s'].string(gdb.target_charset())
134
135 class pp_ns:
136 "Print a std::basic_string of some kind"
137
138 def __init__(self, val):
139 self.val = val
140
141 def to_string(self):
142 len = self.val['length']
143 return self.val['null_str'].string (gdb.target_charset(), length = len)
144
145 def display_hint (self):
146 return 'string'
147
148 pp_ls_encoding = None
149
150 class pp_ls:
151 "Print a std::basic_string of some kind"
152
153 def __init__(self, val):
154 self.val = val
155
156 def to_string(self):
157 if pp_ls_encoding is not None:
158 return self.val['lazy_str'].lazy_string(encoding = pp_ls_encoding)
159 else:
160 return self.val['lazy_str'].lazy_string()
161
162 def display_hint (self):
163 return 'string'
164
165 class pp_hint_error:
166 "Throw error from display_hint"
167
168 def __init__(self, val):
169 self.val = val
170
171 def to_string(self):
172 return 'hint_error_val'
173
174 def display_hint (self):
175 raise Exception("hint failed")
176
177 class pp_outer:
178 "Print struct outer"
179
180 def __init__ (self, val):
181 self.val = val
182
183 def to_string (self):
184 return "x = %s" % self.val['x']
185
186 def children (self):
187 yield 's', self.val['s']
188 yield 'x', self.val['x']
189
190 class MemoryErrorString:
191 "Raise an error"
192
193 def __init__(self, val):
194 self.val = val
195
196 def to_string(self):
197 raise gdb.MemoryError ("Cannot access memory.");
198
199 def display_hint (self):
200 return 'string'
201
202 class pp_eval_type:
203 def __init__(self, val):
204 self.val = val
205
206 def to_string(self):
207 gdb.execute("bt", to_string=True)
208 return "eval=<" + str(gdb.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
209
210 def lookup_function (val):
211 "Look-up and return a pretty-printer that can print val."
212
213 # Get the type.
214 type = val.type
215
216 # If it points to a reference, get the reference.
217 if type.code == gdb.TYPE_CODE_REF:
218 type = type.target ()
219
220 # Get the unqualified type, stripped of typedefs.
221 type = type.unqualified ().strip_typedefs ()
222
223 # Get the type name.
224 typename = type.tag
225
226 if typename == None:
227 return None
228
229 # Iterate over local dictionary of types to determine
230 # if a printer is registered for that type. Return an
231 # instantiation of the printer if found.
232 for function in pretty_printers_dict:
233 if function.match (typename):
234 return pretty_printers_dict[function] (val)
235
236 # Cannot find a pretty printer. Return None.
237
238 return None
239
240 def disable_lookup_function ():
241 lookup_function.enabled = False
242
243 def enable_lookup_function ():
244 lookup_function.enabled = True
245
246 def register_pretty_printers ():
247 pretty_printers_dict[re.compile ('^struct s$')] = pp_s
248 pretty_printers_dict[re.compile ('^s$')] = pp_s
249 pretty_printers_dict[re.compile ('^S$')] = pp_s
250
251 pretty_printers_dict[re.compile ('^struct ss$')] = pp_ss
252 pretty_printers_dict[re.compile ('^ss$')] = pp_ss
253 pretty_printers_dict[re.compile ('^const S &$')] = pp_s
254 pretty_printers_dict[re.compile ('^SSS$')] = pp_sss
255
256 pretty_printers_dict[re.compile ('^VirtualTest$')] = pp_multiple_virtual
257 pretty_printers_dict[re.compile ('^Vbase1$')] = pp_vbase1
258
259 pretty_printers_dict[re.compile ('^struct nullstr$')] = pp_nullstr
260 pretty_printers_dict[re.compile ('^nullstr$')] = pp_nullstr
261
262 # Note that we purposely omit the typedef names here.
263 # Printer lookup is based on canonical name.
264 # However, we do need both tagged and untagged variants, to handle
265 # both the C and C++ cases.
266 pretty_printers_dict[re.compile ('^struct string_repr$')] = string_print
267 pretty_printers_dict[re.compile ('^struct container$')] = ContainerPrinter
268 pretty_printers_dict[re.compile ('^struct justchildren$')] = NoStringContainerPrinter
269 pretty_printers_dict[re.compile ('^string_repr$')] = string_print
270 pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter
271 pretty_printers_dict[re.compile ('^justchildren$')] = NoStringContainerPrinter
272
273 pretty_printers_dict[re.compile ('^struct ns$')] = pp_ns
274 pretty_printers_dict[re.compile ('^ns$')] = pp_ns
275
276 pretty_printers_dict[re.compile ('^struct lazystring$')] = pp_ls
277 pretty_printers_dict[re.compile ('^lazystring$')] = pp_ls
278
279 pretty_printers_dict[re.compile ('^struct outerstruct$')] = pp_outer
280 pretty_printers_dict[re.compile ('^outerstruct$')] = pp_outer
281
282 pretty_printers_dict[re.compile ('^struct hint_error$')] = pp_hint_error
283 pretty_printers_dict[re.compile ('^hint_error$')] = pp_hint_error
284
285 pretty_printers_dict[re.compile ('^memory_error$')] = MemoryErrorString
286
287 pretty_printers_dict[re.compile ('^eval_type_s$')] = pp_eval_type
288
289 pretty_printers_dict = {}
290
291 register_pretty_printers ()
292 gdb.pretty_printers.append (lookup_function)
This page took 0.042685 seconds and 5 git commands to generate.