]> Git Repo - binutils.git/blob - gdb/testsuite/gdb.python/py-prettyprint.py
89ffc0f8d1878feef19f0c48eec5db18f75156d7
[binutils.git] / gdb / testsuite / gdb.python / py-prettyprint.py
1 # Copyright (C) 2008-2021 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
23 def _iterator(pointer, len):
24     start = pointer
25     end = pointer + len
26     while pointer != end:
27         yield ("[%d]" % int(pointer - start), pointer.dereference())
28         pointer += 1
29
30
31 # Same as _iterator but can be told to raise an exception.
32 def _iterator_except(pointer, len):
33     start = pointer
34     end = pointer + len
35     while pointer != end:
36         if exception_flag:
37             raise gdb.MemoryError("hi bob")
38         yield ("[%d]" % int(pointer - start), pointer.dereference())
39         pointer += 1
40
41
42 # Test returning a Value from a printer.
43 class string_print(object):
44     def __init__(self, val):
45         self.val = val
46
47     def to_string(self):
48         return self.val["whybother"]["contents"]
49
50
51 # Test a class-based printer.
52 class ContainerPrinter(object):
53     def __init__(self, val):
54         self.val = val
55
56     def to_string(self):
57         return "container %s with %d elements" % (self.val["name"], self.val["len"])
58
59     def children(self):
60         return _iterator(self.val["elements"], self.val["len"])
61
62     def display_hint(self):
63         if self.val["is_map_p"] and self.val["is_array_p"]:
64             raise Exception("invalid object state found in display_hint")
65
66         if self.val["is_map_p"]:
67             return "map"
68         elif self.val["is_array_p"]:
69             return "array"
70         else:
71             return None
72
73
74 # Treats a container as array.
75 class ArrayPrinter(object):
76     def __init__(self, val):
77         self.val = val
78
79     def to_string(self):
80         return "array %s with %d elements" % (self.val["name"], self.val["len"])
81
82     def children(self):
83         return _iterator(self.val["elements"], self.val["len"])
84
85     def display_hint(self):
86         return "array"
87
88
89 # Flag to make NoStringContainerPrinter throw an exception.
90 exception_flag = False
91
92 # Test a printer where to_string is None
93 class NoStringContainerPrinter(object):
94     def __init__(self, val):
95         self.val = val
96
97     def to_string(self):
98         return None
99
100     def children(self):
101         return _iterator_except(self.val["elements"], self.val["len"])
102
103
104 # See ToStringReturnsValueWrapper.
105 class ToStringReturnsValueInner:
106     def __init__(self, val):
107         self.val = val
108
109     def to_string(self):
110         return "Inner to_string {}".format(int(self.val["val"]))
111
112
113 # Test a printer that returns a gdb.Value in its to_string.  That gdb.Value
114 # also has its own pretty-printer.
115 class ToStringReturnsValueWrapper:
116     def __init__(self, val):
117         self.val = val
118
119     def to_string(self):
120         return self.val["inner"]
121
122
123 class pp_s(object):
124     def __init__(self, val):
125         self.val = val
126
127     def to_string(self):
128         a = self.val["a"]
129         b = self.val["b"]
130         if a.address != b:
131             raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b)))
132         return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
133
134
135 class pp_ss(object):
136     def __init__(self, val):
137         self.val = val
138
139     def to_string(self):
140         return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
141
142
143 class pp_sss(object):
144     def __init__(self, val):
145         self.val = val
146
147     def to_string(self):
148         return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
149
150
151 class pp_multiple_virtual(object):
152     def __init__(self, val):
153         self.val = val
154
155     def to_string(self):
156         return "pp value variable is: " + str(self.val["value"])
157
158
159 class pp_vbase1(object):
160     def __init__(self, val):
161         self.val = val
162
163     def to_string(self):
164         return "pp class name: " + self.val.type.tag
165
166
167 class pp_nullstr(object):
168     def __init__(self, val):
169         self.val = val
170
171     def to_string(self):
172         return self.val["s"].string(gdb.target_charset())
173
174
175 class pp_ns(object):
176     "Print a std::basic_string of some kind"
177
178     def __init__(self, val):
179         self.val = val
180
181     def to_string(self):
182         len = self.val["length"]
183         return self.val["null_str"].string(gdb.target_charset(), length=len)
184
185     def display_hint(self):
186         return "string"
187
188
189 pp_ls_encoding = None
190
191
192 class pp_ls(object):
193     "Print a std::basic_string of some kind"
194
195     def __init__(self, val):
196         self.val = val
197
198     def to_string(self):
199         length = self.val["len"]
200         if pp_ls_encoding is not None:
201             if length >= 0:
202                 return self.val["lazy_str"].lazy_string(
203                     encoding=pp_ls_encoding, length=length
204                 )
205             else:
206                 return self.val["lazy_str"].lazy_string(encoding=pp_ls_encoding)
207         else:
208             if length >= 0:
209                 return self.val["lazy_str"].lazy_string(length=length)
210             else:
211                 return self.val["lazy_str"].lazy_string()
212
213     def display_hint(self):
214         return "string"
215
216
217 class pp_hint_error(object):
218     "Throw error from display_hint"
219
220     def __init__(self, val):
221         self.val = val
222
223     def to_string(self):
224         return "hint_error_val"
225
226     def display_hint(self):
227         raise Exception("hint failed")
228
229
230 class pp_children_as_list(object):
231     "Throw error from display_hint"
232
233     def __init__(self, val):
234         self.val = val
235
236     def to_string(self):
237         return "children_as_list_val"
238
239     def children(self):
240         return [("one", 1)]
241
242
243 class pp_outer(object):
244     "Print struct outer"
245
246     def __init__(self, val):
247         self.val = val
248
249     def to_string(self):
250         return "x = %s" % self.val["x"]
251
252     def children(self):
253         yield "s", self.val["s"]
254         yield "x", self.val["x"]
255
256
257 class MemoryErrorString(object):
258     "Raise an error"
259
260     def __init__(self, val):
261         self.val = val
262
263     def to_string(self):
264         raise gdb.MemoryError("Cannot access memory.")
265
266     def display_hint(self):
267         return "string"
268
269
270 class pp_eval_type(object):
271     def __init__(self, val):
272         self.val = val
273
274     def to_string(self):
275         gdb.execute("bt", to_string=True)
276         return (
277             "eval=<"
278             + str(gdb.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)"))
279             + ">"
280         )
281
282
283 class pp_int_typedef(object):
284     def __init__(self, val):
285         self.val = val
286
287     def to_string(self):
288         return "type=%s, val=%s" % (self.val.type, int(self.val))
289
290
291 class pp_int_typedef3(object):
292     "A printer without a to_string method"
293
294     def __init__(self, val):
295         self.val = val
296
297     def children(self):
298         yield "s", 27
299
300
301 def lookup_function(val):
302     "Look-up and return a pretty-printer that can print val."
303
304     # Get the type.
305     type = val.type
306
307     # If it points to a reference, get the reference.
308     if type.code == gdb.TYPE_CODE_REF:
309         type = type.target()
310
311     # Get the unqualified type, stripped of typedefs.
312     type = type.unqualified().strip_typedefs()
313
314     # Get the type name.
315     typename = type.tag
316
317     if typename is None:
318         return None
319
320     # Iterate over local dictionary of types to determine
321     # if a printer is registered for that type.  Return an
322     # instantiation of the printer if found.
323     for function in pretty_printers_dict:
324         if function.match(typename):
325             return pretty_printers_dict[function](val)
326
327     # Cannot find a pretty printer.  Return None.
328
329     return None
330
331
332 def disable_lookup_function():
333     lookup_function.enabled = False
334
335
336 def enable_lookup_function():
337     lookup_function.enabled = True
338
339
340 # Lookup a printer for VAL in the typedefs dict.
341 def lookup_typedefs_function(val):
342     "Look-up and return a pretty-printer that can print val (typedefs)."
343
344     # Get the type.
345     type = val.type
346
347     if type is None or type.name is None or type.code != gdb.TYPE_CODE_TYPEDEF:
348         return None
349
350     # Iterate over local dictionary of typedef types to determine if a
351     # printer is registered for that type.  Return an instantiation of
352     # the printer if found.
353     for function in typedefs_pretty_printers_dict:
354         if function.match(type.name):
355             return typedefs_pretty_printers_dict[function](val)
356
357     # Cannot find a pretty printer.
358     return None
359
360
361 def register_pretty_printers():
362     pretty_printers_dict[re.compile("^struct s$")] = pp_s
363     pretty_printers_dict[re.compile("^s$")] = pp_s
364     pretty_printers_dict[re.compile("^S$")] = pp_s
365
366     pretty_printers_dict[re.compile("^struct ss$")] = pp_ss
367     pretty_printers_dict[re.compile("^ss$")] = pp_ss
368     pretty_printers_dict[re.compile("^const S &$")] = pp_s
369     pretty_printers_dict[re.compile("^SSS$")] = pp_sss
370
371     pretty_printers_dict[re.compile("^VirtualTest$")] = pp_multiple_virtual
372     pretty_printers_dict[re.compile("^Vbase1$")] = pp_vbase1
373
374     pretty_printers_dict[re.compile("^struct nullstr$")] = pp_nullstr
375     pretty_printers_dict[re.compile("^nullstr$")] = pp_nullstr
376
377     # Note that we purposely omit the typedef names here.
378     # Printer lookup is based on canonical name.
379     # However, we do need both tagged and untagged variants, to handle
380     # both the C and C++ cases.
381     pretty_printers_dict[re.compile("^struct string_repr$")] = string_print
382     pretty_printers_dict[re.compile("^struct container$")] = ContainerPrinter
383     pretty_printers_dict[re.compile("^struct justchildren$")] = NoStringContainerPrinter
384     pretty_printers_dict[re.compile("^string_repr$")] = string_print
385     pretty_printers_dict[re.compile("^container$")] = ContainerPrinter
386     pretty_printers_dict[re.compile("^justchildren$")] = NoStringContainerPrinter
387
388     pretty_printers_dict[
389         re.compile("^struct to_string_returns_value_inner$")
390     ] = ToStringReturnsValueInner
391     pretty_printers_dict[
392         re.compile("^to_string_returns_value_inner$")
393     ] = ToStringReturnsValueInner
394     pretty_printers_dict[
395         re.compile("^struct to_string_returns_value_wrapper$")
396     ] = ToStringReturnsValueWrapper
397     pretty_printers_dict[
398         re.compile("^to_string_returns_value_wrapper$")
399     ] = ToStringReturnsValueWrapper
400
401     pretty_printers_dict[re.compile("^struct ns$")] = pp_ns
402     pretty_printers_dict[re.compile("^ns$")] = pp_ns
403
404     pretty_printers_dict[re.compile("^struct lazystring$")] = pp_ls
405     pretty_printers_dict[re.compile("^lazystring$")] = pp_ls
406
407     pretty_printers_dict[re.compile("^struct outerstruct$")] = pp_outer
408     pretty_printers_dict[re.compile("^outerstruct$")] = pp_outer
409
410     pretty_printers_dict[re.compile("^struct hint_error$")] = pp_hint_error
411     pretty_printers_dict[re.compile("^hint_error$")] = pp_hint_error
412
413     pretty_printers_dict[re.compile("^struct children_as_list$")] = pp_children_as_list
414     pretty_printers_dict[re.compile("^children_as_list$")] = pp_children_as_list
415
416     pretty_printers_dict[re.compile("^memory_error$")] = MemoryErrorString
417
418     pretty_printers_dict[re.compile("^eval_type_s$")] = pp_eval_type
419
420     typedefs_pretty_printers_dict[re.compile("^int_type$")] = pp_int_typedef
421     typedefs_pretty_printers_dict[re.compile("^int_type2$")] = pp_int_typedef
422     typedefs_pretty_printers_dict[re.compile("^int_type3$")] = pp_int_typedef3
423
424
425 # Dict for struct types with typedefs fully stripped.
426 pretty_printers_dict = {}
427 # Dict for typedef types.
428 typedefs_pretty_printers_dict = {}
429
430 register_pretty_printers()
431 gdb.pretty_printers.append(lookup_function)
432 gdb.pretty_printers.append(lookup_typedefs_function)
This page took 0.041639 seconds and 2 git commands to generate.