1 # Copyright (C) 2008-2016 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'])
88 def __init__(self
, val
):
95 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
96 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
99 def __init__(self
, val
):
103 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
105 class pp_sss (object):
106 def __init__(self
, val
):
110 return "a=<" + str(self
.val
['a']) + "> b=<" + str(self
.val
["b"]) + ">"
112 class pp_multiple_virtual (object):
113 def __init__ (self
, val
):
116 def to_string (self
):
117 return "pp value variable is: " + str (self
.val
['value'])
119 class pp_vbase1 (object):
120 def __init__ (self
, val
):
123 def to_string (self
):
124 return "pp class name: " + self
.val
.type.tag
126 class pp_nullstr (object):
127 def __init__(self
, val
):
131 return self
.val
['s'].string(gdb
.target_charset())
133 class pp_ns (object):
134 "Print a std::basic_string of some kind"
136 def __init__(self
, val
):
140 len = self
.val
['length']
141 return self
.val
['null_str'].string (gdb
.target_charset(), length
= len)
143 def display_hint (self
):
146 pp_ls_encoding
= None
148 class pp_ls (object):
149 "Print a std::basic_string of some kind"
151 def __init__(self
, val
):
155 length
= self
.val
['len']
156 if pp_ls_encoding
is not None:
158 return self
.val
['lazy_str'].lazy_string(
159 encoding
= pp_ls_encoding
,
162 return self
.val
['lazy_str'].lazy_string(
163 encoding
= pp_ls_encoding
)
166 return self
.val
['lazy_str'].lazy_string(length
= length
)
168 return self
.val
['lazy_str'].lazy_string()
170 def display_hint (self
):
173 class pp_hint_error (object):
174 "Throw error from display_hint"
176 def __init__(self
, val
):
180 return 'hint_error_val'
182 def display_hint (self
):
183 raise Exception("hint failed")
185 class pp_children_as_list (object):
186 "Throw error from display_hint"
188 def __init__(self
, val
):
192 return 'children_as_list_val'
197 class pp_outer (object):
200 def __init__ (self
, val
):
203 def to_string (self
):
204 return "x = %s" % self
.val
['x']
207 yield 's', self
.val
['s']
208 yield 'x', self
.val
['x']
210 class MemoryErrorString (object):
213 def __init__(self
, val
):
217 raise gdb
.MemoryError ("Cannot access memory.")
219 def display_hint (self
):
222 class pp_eval_type (object):
223 def __init__(self
, val
):
227 gdb
.execute("bt", to_string
=True)
228 return "eval=<" + str(gdb
.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
230 def lookup_function (val
):
231 "Look-up and return a pretty-printer that can print val."
236 # If it points to a reference, get the reference.
237 if type.code
== gdb
.TYPE_CODE_REF
:
238 type = type.target ()
240 # Get the unqualified type, stripped of typedefs.
241 type = type.unqualified ().strip_typedefs ()
249 # Iterate over local dictionary of types to determine
250 # if a printer is registered for that type. Return an
251 # instantiation of the printer if found.
252 for function
in pretty_printers_dict
:
253 if function
.match (typename
):
254 return pretty_printers_dict
[function
] (val
)
256 # Cannot find a pretty printer. Return None.
260 def disable_lookup_function ():
261 lookup_function
.enabled
= False
263 def enable_lookup_function ():
264 lookup_function
.enabled
= True
266 def register_pretty_printers ():
267 pretty_printers_dict
[re
.compile ('^struct s$')] = pp_s
268 pretty_printers_dict
[re
.compile ('^s$')] = pp_s
269 pretty_printers_dict
[re
.compile ('^S$')] = pp_s
271 pretty_printers_dict
[re
.compile ('^struct ss$')] = pp_ss
272 pretty_printers_dict
[re
.compile ('^ss$')] = pp_ss
273 pretty_printers_dict
[re
.compile ('^const S &$')] = pp_s
274 pretty_printers_dict
[re
.compile ('^SSS$')] = pp_sss
276 pretty_printers_dict
[re
.compile ('^VirtualTest$')] = pp_multiple_virtual
277 pretty_printers_dict
[re
.compile ('^Vbase1$')] = pp_vbase1
279 pretty_printers_dict
[re
.compile ('^struct nullstr$')] = pp_nullstr
280 pretty_printers_dict
[re
.compile ('^nullstr$')] = pp_nullstr
282 # Note that we purposely omit the typedef names here.
283 # Printer lookup is based on canonical name.
284 # However, we do need both tagged and untagged variants, to handle
285 # both the C and C++ cases.
286 pretty_printers_dict
[re
.compile ('^struct string_repr$')] = string_print
287 pretty_printers_dict
[re
.compile ('^struct container$')] = ContainerPrinter
288 pretty_printers_dict
[re
.compile ('^struct justchildren$')] = NoStringContainerPrinter
289 pretty_printers_dict
[re
.compile ('^string_repr$')] = string_print
290 pretty_printers_dict
[re
.compile ('^container$')] = ContainerPrinter
291 pretty_printers_dict
[re
.compile ('^justchildren$')] = NoStringContainerPrinter
293 pretty_printers_dict
[re
.compile ('^struct ns$')] = pp_ns
294 pretty_printers_dict
[re
.compile ('^ns$')] = pp_ns
296 pretty_printers_dict
[re
.compile ('^struct lazystring$')] = pp_ls
297 pretty_printers_dict
[re
.compile ('^lazystring$')] = pp_ls
299 pretty_printers_dict
[re
.compile ('^struct outerstruct$')] = pp_outer
300 pretty_printers_dict
[re
.compile ('^outerstruct$')] = pp_outer
302 pretty_printers_dict
[re
.compile ('^struct hint_error$')] = pp_hint_error
303 pretty_printers_dict
[re
.compile ('^hint_error$')] = pp_hint_error
305 pretty_printers_dict
[re
.compile ('^struct children_as_list$')] = pp_children_as_list
306 pretty_printers_dict
[re
.compile ('^children_as_list$')] = pp_children_as_list
308 pretty_printers_dict
[re
.compile ('^memory_error$')] = MemoryErrorString
310 pretty_printers_dict
[re
.compile ('^eval_type_s$')] = pp_eval_type
312 pretty_printers_dict
= {}
314 register_pretty_printers ()
315 gdb
.pretty_printers
.append (lookup_function
)
This page took 0.057455 seconds and 4 git commands to generate.