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