1 # Copyright (C) 2008-2021 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests python pretty
22 def _iterator (pointer
, len):
26 yield ('[%d]' % int (pointer
- start
), pointer
.dereference())
29 # Same as _iterator but can be told to raise an exception.
30 def _iterator_except (pointer
, len):
35 raise gdb
.MemoryError ('hi bob')
36 yield ('[%d]' % int (pointer
- start
), pointer
.dereference())
39 # Test returning a Value from a printer.
40 class string_print (object):
41 def __init__(self
, val
):
45 return self
.val
['whybother']['contents']
47 # Test a class-based printer.
48 class ContainerPrinter (object):
50 def __init__(self
, val
):
54 return 'container %s with %d elements' % (self
.val
['name'], self
.val
['len'])
57 return _iterator(self
.val
['elements'], self
.val
['len'])
59 def display_hint (self
):
60 if (self
.val
['is_map_p']):
65 # Treats a container as array.
66 class ArrayPrinter (object):
67 def __init__(self
, val
):
71 return 'array %s with %d elements' % (self
.val
['name'], self
.val
['len'])
74 return _iterator(self
.val
['elements'], self
.val
['len'])
76 def display_hint (self
):
79 # Flag to make NoStringContainerPrinter throw an exception.
80 exception_flag
= False
82 # Test a printer where to_string is None
83 class NoStringContainerPrinter (object):
84 def __init__(self
, val
):
91 return _iterator_except (self
.val
['elements'], self
.val
['len'])
93 # See ToStringReturnsValueWrapper.
94 class ToStringReturnsValueInner
:
96 def __init__(self
, val
):
100 return 'Inner to_string {}'.format(int(self
.val
['val']))
102 # Test a printer that returns a gdb.Value in its to_string. That gdb.Value
103 # also has its own pretty-printer.
104 class ToStringReturnsValueWrapper
:
106 def __init__(self
, val
):
110 return self
.val
['inner']
113 def __init__(self
, val
):
120 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
121 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
123 class pp_ss (object):
124 def __init__(self
, val
):
128 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
130 class pp_sss (object):
131 def __init__(self
, val
):
135 return "a=<" + str(self
.val
['a']) + "> b=<" + str(self
.val
["b"]) + ">"
137 class pp_multiple_virtual (object):
138 def __init__ (self
, val
):
141 def to_string (self
):
142 return "pp value variable is: " + str (self
.val
['value'])
144 class pp_vbase1 (object):
145 def __init__ (self
, val
):
148 def to_string (self
):
149 return "pp class name: " + self
.val
.type.tag
151 class pp_nullstr (object):
152 def __init__(self
, val
):
156 return self
.val
['s'].string(gdb
.target_charset())
158 class pp_ns (object):
159 "Print a std::basic_string of some kind"
161 def __init__(self
, val
):
165 len = self
.val
['length']
166 return self
.val
['null_str'].string (gdb
.target_charset(), length
= len)
168 def display_hint (self
):
171 pp_ls_encoding
= None
173 class pp_ls (object):
174 "Print a std::basic_string of some kind"
176 def __init__(self
, val
):
180 length
= self
.val
['len']
181 if pp_ls_encoding
is not None:
183 return self
.val
['lazy_str'].lazy_string(
184 encoding
= pp_ls_encoding
,
187 return self
.val
['lazy_str'].lazy_string(
188 encoding
= pp_ls_encoding
)
191 return self
.val
['lazy_str'].lazy_string(length
= length
)
193 return self
.val
['lazy_str'].lazy_string()
195 def display_hint (self
):
198 class pp_hint_error (object):
199 "Throw error from display_hint"
201 def __init__(self
, val
):
205 return 'hint_error_val'
207 def display_hint (self
):
208 raise Exception("hint failed")
210 class pp_children_as_list (object):
211 "Throw error from display_hint"
213 def __init__(self
, val
):
217 return 'children_as_list_val'
222 class pp_outer (object):
225 def __init__ (self
, val
):
228 def to_string (self
):
229 return "x = %s" % self
.val
['x']
232 yield 's', self
.val
['s']
233 yield 'x', self
.val
['x']
235 class MemoryErrorString (object):
238 def __init__(self
, val
):
242 raise gdb
.MemoryError ("Cannot access memory.")
244 def display_hint (self
):
247 class pp_eval_type (object):
248 def __init__(self
, val
):
252 gdb
.execute("bt", to_string
=True)
253 return "eval=<" + str(gdb
.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
255 class pp_int_typedef (object):
256 def __init__(self
, val
):
260 return "type=%s, val=%s" % (self
.val
.type, int(self
.val
))
262 class pp_int_typedef3 (object):
263 "A printer without a to_string method"
265 def __init__(self
, val
):
271 def lookup_function (val
):
272 "Look-up and return a pretty-printer that can print val."
277 # If it points to a reference, get the reference.
278 if type.code
== gdb
.TYPE_CODE_REF
:
279 type = type.target ()
281 # Get the unqualified type, stripped of typedefs.
282 type = type.unqualified ().strip_typedefs ()
290 # Iterate over local dictionary of types to determine
291 # if a printer is registered for that type. Return an
292 # instantiation of the printer if found.
293 for function
in pretty_printers_dict
:
294 if function
.match (typename
):
295 return pretty_printers_dict
[function
] (val
)
297 # Cannot find a pretty printer. Return None.
301 def disable_lookup_function ():
302 lookup_function
.enabled
= False
304 def enable_lookup_function ():
305 lookup_function
.enabled
= True
307 # Lookup a printer for VAL in the typedefs dict.
308 def lookup_typedefs_function (val
):
309 "Look-up and return a pretty-printer that can print val (typedefs)."
314 if type == None or type.name
== None or type.code
!= gdb
.TYPE_CODE_TYPEDEF
:
317 # Iterate over local dictionary of typedef types to determine if a
318 # printer is registered for that type. Return an instantiation of
319 # the printer if found.
320 for function
in typedefs_pretty_printers_dict
:
321 if function
.match (type.name
):
322 return typedefs_pretty_printers_dict
[function
] (val
)
324 # Cannot find a pretty printer.
327 def register_pretty_printers ():
328 pretty_printers_dict
[re
.compile ('^struct s$')] = pp_s
329 pretty_printers_dict
[re
.compile ('^s$')] = pp_s
330 pretty_printers_dict
[re
.compile ('^S$')] = pp_s
332 pretty_printers_dict
[re
.compile ('^struct ss$')] = pp_ss
333 pretty_printers_dict
[re
.compile ('^ss$')] = pp_ss
334 pretty_printers_dict
[re
.compile ('^const S &$')] = pp_s
335 pretty_printers_dict
[re
.compile ('^SSS$')] = pp_sss
337 pretty_printers_dict
[re
.compile ('^VirtualTest$')] = pp_multiple_virtual
338 pretty_printers_dict
[re
.compile ('^Vbase1$')] = pp_vbase1
340 pretty_printers_dict
[re
.compile ('^struct nullstr$')] = pp_nullstr
341 pretty_printers_dict
[re
.compile ('^nullstr$')] = pp_nullstr
343 # Note that we purposely omit the typedef names here.
344 # Printer lookup is based on canonical name.
345 # However, we do need both tagged and untagged variants, to handle
346 # both the C and C++ cases.
347 pretty_printers_dict
[re
.compile ('^struct string_repr$')] = string_print
348 pretty_printers_dict
[re
.compile ('^struct container$')] = ContainerPrinter
349 pretty_printers_dict
[re
.compile ('^struct justchildren$')] = NoStringContainerPrinter
350 pretty_printers_dict
[re
.compile ('^string_repr$')] = string_print
351 pretty_printers_dict
[re
.compile ('^container$')] = ContainerPrinter
352 pretty_printers_dict
[re
.compile ('^justchildren$')] = NoStringContainerPrinter
354 pretty_printers_dict
[re
.compile ('^struct to_string_returns_value_inner$')] = ToStringReturnsValueInner
355 pretty_printers_dict
[re
.compile ('^to_string_returns_value_inner$')] = ToStringReturnsValueInner
356 pretty_printers_dict
[re
.compile ('^struct to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
357 pretty_printers_dict
[re
.compile ('^to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
359 pretty_printers_dict
[re
.compile ('^struct ns$')] = pp_ns
360 pretty_printers_dict
[re
.compile ('^ns$')] = pp_ns
362 pretty_printers_dict
[re
.compile ('^struct lazystring$')] = pp_ls
363 pretty_printers_dict
[re
.compile ('^lazystring$')] = pp_ls
365 pretty_printers_dict
[re
.compile ('^struct outerstruct$')] = pp_outer
366 pretty_printers_dict
[re
.compile ('^outerstruct$')] = pp_outer
368 pretty_printers_dict
[re
.compile ('^struct hint_error$')] = pp_hint_error
369 pretty_printers_dict
[re
.compile ('^hint_error$')] = pp_hint_error
371 pretty_printers_dict
[re
.compile ('^struct children_as_list$')] = pp_children_as_list
372 pretty_printers_dict
[re
.compile ('^children_as_list$')] = pp_children_as_list
374 pretty_printers_dict
[re
.compile ('^memory_error$')] = MemoryErrorString
376 pretty_printers_dict
[re
.compile ('^eval_type_s$')] = pp_eval_type
378 typedefs_pretty_printers_dict
[re
.compile ('^int_type$')] = pp_int_typedef
379 typedefs_pretty_printers_dict
[re
.compile ('^int_type2$')] = pp_int_typedef
380 typedefs_pretty_printers_dict
[re
.compile ('^int_type3$')] = pp_int_typedef3
382 # Dict for struct types with typedefs fully stripped.
383 pretty_printers_dict
= {}
384 # Dict for typedef types.
385 typedefs_pretty_printers_dict
= {}
387 register_pretty_printers ()
388 gdb
.pretty_printers
.append (lookup_function
)
389 gdb
.pretty_printers
.append (lookup_typedefs_function
)