1 # Copyright (C) 2008-2019 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 # Treats a container as array.
60 class ArrayPrinter (object):
61 def __init__(self
, val
):
65 return 'array %s with %d elements' % (self
.val
['name'], self
.val
['len'])
68 return _iterator(self
.val
['elements'], self
.val
['len'])
70 def display_hint (self
):
73 # Flag to make NoStringContainerPrinter throw an exception.
74 exception_flag
= False
76 # Test a printer where to_string is None
77 class NoStringContainerPrinter (object):
78 def __init__(self
, val
):
85 return _iterator_except (self
.val
['elements'], self
.val
['len'])
87 # See ToStringReturnsValueWrapper.
88 class ToStringReturnsValueInner
:
90 def __init__(self
, val
):
94 return 'Inner to_string {}'.format(int(self
.val
['val']))
96 # Test a printer that returns a gdb.Value in its to_string. That gdb.Value
97 # also has its own pretty-printer.
98 class ToStringReturnsValueWrapper
:
100 def __init__(self
, val
):
104 return self
.val
['inner']
107 def __init__(self
, val
):
114 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
115 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
117 class pp_ss (object):
118 def __init__(self
, val
):
122 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
124 class pp_sss (object):
125 def __init__(self
, val
):
129 return "a=<" + str(self
.val
['a']) + "> b=<" + str(self
.val
["b"]) + ">"
131 class pp_multiple_virtual (object):
132 def __init__ (self
, val
):
135 def to_string (self
):
136 return "pp value variable is: " + str (self
.val
['value'])
138 class pp_vbase1 (object):
139 def __init__ (self
, val
):
142 def to_string (self
):
143 return "pp class name: " + self
.val
.type.tag
145 class pp_nullstr (object):
146 def __init__(self
, val
):
150 return self
.val
['s'].string(gdb
.target_charset())
152 class pp_ns (object):
153 "Print a std::basic_string of some kind"
155 def __init__(self
, val
):
159 len = self
.val
['length']
160 return self
.val
['null_str'].string (gdb
.target_charset(), length
= len)
162 def display_hint (self
):
165 pp_ls_encoding
= None
167 class pp_ls (object):
168 "Print a std::basic_string of some kind"
170 def __init__(self
, val
):
174 length
= self
.val
['len']
175 if pp_ls_encoding
is not None:
177 return self
.val
['lazy_str'].lazy_string(
178 encoding
= pp_ls_encoding
,
181 return self
.val
['lazy_str'].lazy_string(
182 encoding
= pp_ls_encoding
)
185 return self
.val
['lazy_str'].lazy_string(length
= length
)
187 return self
.val
['lazy_str'].lazy_string()
189 def display_hint (self
):
192 class pp_hint_error (object):
193 "Throw error from display_hint"
195 def __init__(self
, val
):
199 return 'hint_error_val'
201 def display_hint (self
):
202 raise Exception("hint failed")
204 class pp_children_as_list (object):
205 "Throw error from display_hint"
207 def __init__(self
, val
):
211 return 'children_as_list_val'
216 class pp_outer (object):
219 def __init__ (self
, val
):
222 def to_string (self
):
223 return "x = %s" % self
.val
['x']
226 yield 's', self
.val
['s']
227 yield 'x', self
.val
['x']
229 class MemoryErrorString (object):
232 def __init__(self
, val
):
236 raise gdb
.MemoryError ("Cannot access memory.")
238 def display_hint (self
):
241 class pp_eval_type (object):
242 def __init__(self
, val
):
246 gdb
.execute("bt", to_string
=True)
247 return "eval=<" + str(gdb
.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
249 class pp_int_typedef (object):
250 def __init__(self
, val
):
254 return "type=%s, val=%s" % (self
.val
.type, int(self
.val
))
256 class pp_int_typedef3 (object):
257 "A printer without a to_string method"
259 def __init__(self
, val
):
265 def lookup_function (val
):
266 "Look-up and return a pretty-printer that can print val."
271 # If it points to a reference, get the reference.
272 if type.code
== gdb
.TYPE_CODE_REF
:
273 type = type.target ()
275 # Get the unqualified type, stripped of typedefs.
276 type = type.unqualified ().strip_typedefs ()
284 # Iterate over local dictionary of types to determine
285 # if a printer is registered for that type. Return an
286 # instantiation of the printer if found.
287 for function
in pretty_printers_dict
:
288 if function
.match (typename
):
289 return pretty_printers_dict
[function
] (val
)
291 # Cannot find a pretty printer. Return None.
295 def disable_lookup_function ():
296 lookup_function
.enabled
= False
298 def enable_lookup_function ():
299 lookup_function
.enabled
= True
301 # Lookup a printer for VAL in the typedefs dict.
302 def lookup_typedefs_function (val
):
303 "Look-up and return a pretty-printer that can print val (typedefs)."
308 if type == None or type.name
== None or type.code
!= gdb
.TYPE_CODE_TYPEDEF
:
311 # Iterate over local dictionary of typedef types to determine if a
312 # printer is registered for that type. Return an instantiation of
313 # the printer if found.
314 for function
in typedefs_pretty_printers_dict
:
315 if function
.match (type.name
):
316 return typedefs_pretty_printers_dict
[function
] (val
)
318 # Cannot find a pretty printer.
321 def register_pretty_printers ():
322 pretty_printers_dict
[re
.compile ('^struct s$')] = pp_s
323 pretty_printers_dict
[re
.compile ('^s$')] = pp_s
324 pretty_printers_dict
[re
.compile ('^S$')] = pp_s
326 pretty_printers_dict
[re
.compile ('^struct ss$')] = pp_ss
327 pretty_printers_dict
[re
.compile ('^ss$')] = pp_ss
328 pretty_printers_dict
[re
.compile ('^const S &$')] = pp_s
329 pretty_printers_dict
[re
.compile ('^SSS$')] = pp_sss
331 pretty_printers_dict
[re
.compile ('^VirtualTest$')] = pp_multiple_virtual
332 pretty_printers_dict
[re
.compile ('^Vbase1$')] = pp_vbase1
334 pretty_printers_dict
[re
.compile ('^struct nullstr$')] = pp_nullstr
335 pretty_printers_dict
[re
.compile ('^nullstr$')] = pp_nullstr
337 # Note that we purposely omit the typedef names here.
338 # Printer lookup is based on canonical name.
339 # However, we do need both tagged and untagged variants, to handle
340 # both the C and C++ cases.
341 pretty_printers_dict
[re
.compile ('^struct string_repr$')] = string_print
342 pretty_printers_dict
[re
.compile ('^struct container$')] = ContainerPrinter
343 pretty_printers_dict
[re
.compile ('^struct justchildren$')] = NoStringContainerPrinter
344 pretty_printers_dict
[re
.compile ('^string_repr$')] = string_print
345 pretty_printers_dict
[re
.compile ('^container$')] = ContainerPrinter
346 pretty_printers_dict
[re
.compile ('^justchildren$')] = NoStringContainerPrinter
348 pretty_printers_dict
[re
.compile ('^struct to_string_returns_value_inner$')] = ToStringReturnsValueInner
349 pretty_printers_dict
[re
.compile ('^to_string_returns_value_inner$')] = ToStringReturnsValueInner
350 pretty_printers_dict
[re
.compile ('^struct to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
351 pretty_printers_dict
[re
.compile ('^to_string_returns_value_wrapper$')] = ToStringReturnsValueWrapper
353 pretty_printers_dict
[re
.compile ('^struct ns$')] = pp_ns
354 pretty_printers_dict
[re
.compile ('^ns$')] = pp_ns
356 pretty_printers_dict
[re
.compile ('^struct lazystring$')] = pp_ls
357 pretty_printers_dict
[re
.compile ('^lazystring$')] = pp_ls
359 pretty_printers_dict
[re
.compile ('^struct outerstruct$')] = pp_outer
360 pretty_printers_dict
[re
.compile ('^outerstruct$')] = pp_outer
362 pretty_printers_dict
[re
.compile ('^struct hint_error$')] = pp_hint_error
363 pretty_printers_dict
[re
.compile ('^hint_error$')] = pp_hint_error
365 pretty_printers_dict
[re
.compile ('^struct children_as_list$')] = pp_children_as_list
366 pretty_printers_dict
[re
.compile ('^children_as_list$')] = pp_children_as_list
368 pretty_printers_dict
[re
.compile ('^memory_error$')] = MemoryErrorString
370 pretty_printers_dict
[re
.compile ('^eval_type_s$')] = pp_eval_type
372 typedefs_pretty_printers_dict
[re
.compile ('^int_type$')] = pp_int_typedef
373 typedefs_pretty_printers_dict
[re
.compile ('^int_type2$')] = pp_int_typedef
374 typedefs_pretty_printers_dict
[re
.compile ('^int_type3$')] = pp_int_typedef3
376 # Dict for struct types with typedefs fully stripped.
377 pretty_printers_dict
= {}
378 # Dict for typedef types.
379 typedefs_pretty_printers_dict
= {}
381 register_pretty_printers ()
382 gdb
.pretty_printers
.append (lookup_function
)
383 gdb
.pretty_printers
.append (lookup_typedefs_function
)