1 # Copyright (C) 2008-2023 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
23 def _iterator(pointer
, len):
27 yield ("[%d]" % int(pointer
- start
), pointer
.dereference())
31 # Same as _iterator but can be told to raise an exception.
32 def _iterator_except(pointer
, len):
37 raise gdb
.MemoryError("hi bob")
38 yield ("[%d]" % int(pointer
- start
), pointer
.dereference())
42 # Test returning a Value from a printer.
43 class string_print(object):
44 def __init__(self
, val
):
48 return self
.val
["whybother"]["contents"]
51 # Test a class-based printer.
52 class ContainerPrinter(object):
53 def __init__(self
, val
):
57 return "container %s with %d elements" % (self
.val
["name"], self
.val
["len"])
60 return _iterator(self
.val
["elements"], self
.val
["len"])
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")
66 if self
.val
["is_map_p"]:
68 elif self
.val
["is_array_p"]:
74 # Treats a container as array.
75 class ArrayPrinter(object):
76 def __init__(self
, val
):
80 return "array %s with %d elements" % (self
.val
["name"], self
.val
["len"])
83 return _iterator(self
.val
["elements"], self
.val
["len"])
85 def display_hint(self
):
89 # Flag to make NoStringContainerPrinter throw an exception.
90 exception_flag
= False
93 # Test a printer where to_string is None
94 class NoStringContainerPrinter(object):
95 def __init__(self
, val
):
102 return _iterator_except(self
.val
["elements"], self
.val
["len"])
105 # See ToStringReturnsValueWrapper.
106 class ToStringReturnsValueInner
:
107 def __init__(self
, val
):
111 return "Inner to_string {}".format(int(self
.val
["val"]))
114 # Test a printer that returns a gdb.Value in its to_string. That gdb.Value
115 # also has its own pretty-printer.
116 class ToStringReturnsValueWrapper
:
117 def __init__(self
, val
):
121 return self
.val
["inner"]
125 def __init__(self
, val
):
132 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
133 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
137 def __init__(self
, val
):
141 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
144 class pp_sss(object):
145 def __init__(self
, val
):
149 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
152 class pp_multiple_virtual(object):
153 def __init__(self
, val
):
157 return "pp value variable is: " + str(self
.val
["value"])
160 class pp_vbase1(object):
161 def __init__(self
, val
):
165 return "pp class name: " + self
.val
.type.tag
168 class pp_nullstr(object):
169 def __init__(self
, val
):
173 return self
.val
["s"].string(gdb
.target_charset())
177 "Print a std::basic_string of some kind"
179 def __init__(self
, val
):
183 len = self
.val
["length"]
184 return self
.val
["null_str"].string(gdb
.target_charset(), length
=len)
186 def display_hint(self
):
190 pp_ls_encoding
= None
194 "Print a std::basic_string of some kind"
196 def __init__(self
, val
):
200 length
= self
.val
["len"]
201 if pp_ls_encoding
is not None:
203 return self
.val
["lazy_str"].lazy_string(
204 encoding
=pp_ls_encoding
, length
=length
207 return self
.val
["lazy_str"].lazy_string(encoding
=pp_ls_encoding
)
210 return self
.val
["lazy_str"].lazy_string(length
=length
)
212 return self
.val
["lazy_str"].lazy_string()
214 def display_hint(self
):
218 class pp_hint_error(object):
219 "Throw error from display_hint"
221 def __init__(self
, val
):
225 return "hint_error_val"
227 def display_hint(self
):
228 raise Exception("hint failed")
231 class pp_children_as_list(object):
232 "Throw error from display_hint"
234 def __init__(self
, val
):
238 return "children_as_list_val"
244 class pp_outer(object):
247 def __init__(self
, val
):
251 return "x = %s" % self
.val
["x"]
254 yield "s", self
.val
["s"]
255 yield "x", self
.val
["x"]
258 class MemoryErrorString(object):
261 def __init__(self
, val
):
265 raise gdb
.MemoryError("Cannot access memory.")
267 def display_hint(self
):
271 class pp_eval_type(object):
272 def __init__(self
, val
):
276 gdb
.execute("bt", to_string
=True)
279 + str(gdb
.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)"))
284 class pp_int_typedef(object):
285 def __init__(self
, val
):
289 return "type=%s, val=%s" % (self
.val
.type, int(self
.val
))
292 class pp_int_typedef3(object):
293 "A printer without a to_string method"
295 def __init__(self
, val
):
302 def lookup_function(val
):
303 "Look-up and return a pretty-printer that can print val."
308 # If it points to a reference, get the reference.
309 if type.code
== gdb
.TYPE_CODE_REF
:
312 # Get the unqualified type, stripped of typedefs.
313 type = type.unqualified().strip_typedefs()
321 # Iterate over local dictionary of types to determine
322 # if a printer is registered for that type. Return an
323 # instantiation of the printer if found.
324 for function
in pretty_printers_dict
:
325 if function
.match(typename
):
326 return pretty_printers_dict
[function
](val
)
328 # Cannot find a pretty printer. Return None.
333 def disable_lookup_function():
334 lookup_function
.enabled
= False
337 def enable_lookup_function():
338 lookup_function
.enabled
= True
341 # Lookup a printer for VAL in the typedefs dict.
342 def lookup_typedefs_function(val
):
343 "Look-up and return a pretty-printer that can print val (typedefs)."
348 if type is None or type.name
is None or type.code
!= gdb
.TYPE_CODE_TYPEDEF
:
351 # Iterate over local dictionary of typedef types to determine if a
352 # printer is registered for that type. Return an instantiation of
353 # the printer if found.
354 for function
in typedefs_pretty_printers_dict
:
355 if function
.match(type.name
):
356 return typedefs_pretty_printers_dict
[function
](val
)
358 # Cannot find a pretty printer.
362 def register_pretty_printers():
363 pretty_printers_dict
[re
.compile("^struct s$")] = pp_s
364 pretty_printers_dict
[re
.compile("^s$")] = pp_s
365 pretty_printers_dict
[re
.compile("^S$")] = pp_s
367 pretty_printers_dict
[re
.compile("^struct ss$")] = pp_ss
368 pretty_printers_dict
[re
.compile("^ss$")] = pp_ss
369 pretty_printers_dict
[re
.compile("^const S &$")] = pp_s
370 pretty_printers_dict
[re
.compile("^SSS$")] = pp_sss
372 pretty_printers_dict
[re
.compile("^VirtualTest$")] = pp_multiple_virtual
373 pretty_printers_dict
[re
.compile("^Vbase1$")] = pp_vbase1
375 pretty_printers_dict
[re
.compile("^struct nullstr$")] = pp_nullstr
376 pretty_printers_dict
[re
.compile("^nullstr$")] = pp_nullstr
378 # Note that we purposely omit the typedef names here.
379 # Printer lookup is based on canonical name.
380 # However, we do need both tagged and untagged variants, to handle
381 # both the C and C++ cases.
382 pretty_printers_dict
[re
.compile("^struct string_repr$")] = string_print
383 pretty_printers_dict
[re
.compile("^struct container$")] = ContainerPrinter
384 pretty_printers_dict
[re
.compile("^struct justchildren$")] = NoStringContainerPrinter
385 pretty_printers_dict
[re
.compile("^string_repr$")] = string_print
386 pretty_printers_dict
[re
.compile("^container$")] = ContainerPrinter
387 pretty_printers_dict
[re
.compile("^justchildren$")] = NoStringContainerPrinter
389 pretty_printers_dict
[
390 re
.compile("^struct to_string_returns_value_inner$")
391 ] = ToStringReturnsValueInner
392 pretty_printers_dict
[
393 re
.compile("^to_string_returns_value_inner$")
394 ] = ToStringReturnsValueInner
395 pretty_printers_dict
[
396 re
.compile("^struct to_string_returns_value_wrapper$")
397 ] = ToStringReturnsValueWrapper
398 pretty_printers_dict
[
399 re
.compile("^to_string_returns_value_wrapper$")
400 ] = ToStringReturnsValueWrapper
402 pretty_printers_dict
[re
.compile("^struct ns$")] = pp_ns
403 pretty_printers_dict
[re
.compile("^ns$")] = pp_ns
405 pretty_printers_dict
[re
.compile("^struct lazystring$")] = pp_ls
406 pretty_printers_dict
[re
.compile("^lazystring$")] = pp_ls
408 pretty_printers_dict
[re
.compile("^struct outerstruct$")] = pp_outer
409 pretty_printers_dict
[re
.compile("^outerstruct$")] = pp_outer
411 pretty_printers_dict
[re
.compile("^struct hint_error$")] = pp_hint_error
412 pretty_printers_dict
[re
.compile("^hint_error$")] = pp_hint_error
414 pretty_printers_dict
[re
.compile("^struct children_as_list$")] = pp_children_as_list
415 pretty_printers_dict
[re
.compile("^children_as_list$")] = pp_children_as_list
417 pretty_printers_dict
[re
.compile("^memory_error$")] = MemoryErrorString
419 pretty_printers_dict
[re
.compile("^eval_type_s$")] = pp_eval_type
421 typedefs_pretty_printers_dict
[re
.compile("^int_type$")] = pp_int_typedef
422 typedefs_pretty_printers_dict
[re
.compile("^int_type2$")] = pp_int_typedef
423 typedefs_pretty_printers_dict
[re
.compile("^int_type3$")] = pp_int_typedef3
426 # Dict for struct types with typedefs fully stripped.
427 pretty_printers_dict
= {}
428 # Dict for typedef types.
429 typedefs_pretty_printers_dict
= {}
431 register_pretty_printers()
432 gdb
.pretty_printers
.append(lookup_function
)
433 gdb
.pretty_printers
.append(lookup_typedefs_function
)