1 # Copyright (C) 2008-2012 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 # Test returning a Value from a printer.
24 def __init__(self
, val
):
28 return self
.val
['whybother']['contents']
30 # Test a class-based printer.
31 class ContainerPrinter
:
33 def __init__ (self
, pointer
, len):
35 self
.pointer
= pointer
36 self
.end
= pointer
+ len
42 if self
.pointer
== self
.end
:
45 self
.pointer
= self
.pointer
+ 1
46 return ('[%d]' % int (result
- self
.start
), result
.dereference())
48 def __init__(self
, val
):
52 return 'container %s with %d elements' % (self
.val
['name'], self
.val
['len'])
55 return self
._iterator
(self
.val
['elements'], self
.val
['len'])
57 # Flag to make NoStringContainerPrinter throw an exception.
58 exception_flag
= False
60 # Test a printer where to_string is None
61 class NoStringContainerPrinter
:
63 def __init__ (self
, pointer
, len):
65 self
.pointer
= pointer
66 self
.end
= pointer
+ len
72 if self
.pointer
== self
.end
:
75 raise gdb
.MemoryError, 'hi bob'
77 self
.pointer
= self
.pointer
+ 1
78 return ('[%d]' % int (result
- self
.start
), result
.dereference())
80 def __init__(self
, val
):
87 return self
._iterator
(self
.val
['elements'], self
.val
['len'])
90 def __init__(self
, val
):
97 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
98 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
101 def __init__(self
, val
):
105 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
108 def __init__(self
, val
):
112 return "a=<" + str(self
.val
['a']) + "> b=<" + str(self
.val
["b"]) + ">"
114 class pp_multiple_virtual
:
115 def __init__ (self
, val
):
118 def to_string (self
):
119 return "pp value variable is: " + str (self
.val
['value'])
122 def __init__ (self
, val
):
125 def to_string (self
):
126 return "pp class name: " + self
.val
.type.tag
129 def __init__(self
, val
):
133 return self
.val
['s'].string(gdb
.target_charset())
136 "Print a std::basic_string of some kind"
138 def __init__(self
, val
):
142 len = self
.val
['length']
143 return self
.val
['null_str'].string (gdb
.target_charset(), length
= len)
145 def display_hint (self
):
148 pp_ls_encoding
= None
151 "Print a std::basic_string of some kind"
153 def __init__(self
, val
):
157 if pp_ls_encoding
is not None:
158 return self
.val
['lazy_str'].lazy_string(encoding
= pp_ls_encoding
)
160 return self
.val
['lazy_str'].lazy_string()
162 def display_hint (self
):
166 "Throw error from display_hint"
168 def __init__(self
, val
):
172 return 'hint_error_val'
174 def display_hint (self
):
175 raise Exception("hint failed")
180 def __init__ (self
, val
):
183 def to_string (self
):
184 return "x = %s" % self
.val
['x']
187 yield 's', self
.val
['s']
188 yield 'x', self
.val
['x']
190 class MemoryErrorString
:
193 def __init__(self
, val
):
197 raise gdb
.MemoryError ("Cannot access memory.");
199 def display_hint (self
):
203 def __init__(self
, val
):
207 gdb
.execute("bt", to_string
=True)
208 return "eval=<" + str(gdb
.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) + ">"
210 def lookup_function (val
):
211 "Look-up and return a pretty-printer that can print val."
216 # If it points to a reference, get the reference.
217 if type.code
== gdb
.TYPE_CODE_REF
:
218 type = type.target ()
220 # Get the unqualified type, stripped of typedefs.
221 type = type.unqualified ().strip_typedefs ()
229 # Iterate over local dictionary of types to determine
230 # if a printer is registered for that type. Return an
231 # instantiation of the printer if found.
232 for function
in pretty_printers_dict
:
233 if function
.match (typename
):
234 return pretty_printers_dict
[function
] (val
)
236 # Cannot find a pretty printer. Return None.
240 def disable_lookup_function ():
241 lookup_function
.enabled
= False
243 def enable_lookup_function ():
244 lookup_function
.enabled
= True
246 def register_pretty_printers ():
247 pretty_printers_dict
[re
.compile ('^struct s$')] = pp_s
248 pretty_printers_dict
[re
.compile ('^s$')] = pp_s
249 pretty_printers_dict
[re
.compile ('^S$')] = pp_s
251 pretty_printers_dict
[re
.compile ('^struct ss$')] = pp_ss
252 pretty_printers_dict
[re
.compile ('^ss$')] = pp_ss
253 pretty_printers_dict
[re
.compile ('^const S &$')] = pp_s
254 pretty_printers_dict
[re
.compile ('^SSS$')] = pp_sss
256 pretty_printers_dict
[re
.compile ('^VirtualTest$')] = pp_multiple_virtual
257 pretty_printers_dict
[re
.compile ('^Vbase1$')] = pp_vbase1
259 pretty_printers_dict
[re
.compile ('^struct nullstr$')] = pp_nullstr
260 pretty_printers_dict
[re
.compile ('^nullstr$')] = pp_nullstr
262 # Note that we purposely omit the typedef names here.
263 # Printer lookup is based on canonical name.
264 # However, we do need both tagged and untagged variants, to handle
265 # both the C and C++ cases.
266 pretty_printers_dict
[re
.compile ('^struct string_repr$')] = string_print
267 pretty_printers_dict
[re
.compile ('^struct container$')] = ContainerPrinter
268 pretty_printers_dict
[re
.compile ('^struct justchildren$')] = NoStringContainerPrinter
269 pretty_printers_dict
[re
.compile ('^string_repr$')] = string_print
270 pretty_printers_dict
[re
.compile ('^container$')] = ContainerPrinter
271 pretty_printers_dict
[re
.compile ('^justchildren$')] = NoStringContainerPrinter
273 pretty_printers_dict
[re
.compile ('^struct ns$')] = pp_ns
274 pretty_printers_dict
[re
.compile ('^ns$')] = pp_ns
276 pretty_printers_dict
[re
.compile ('^struct lazystring$')] = pp_ls
277 pretty_printers_dict
[re
.compile ('^lazystring$')] = pp_ls
279 pretty_printers_dict
[re
.compile ('^struct outerstruct$')] = pp_outer
280 pretty_printers_dict
[re
.compile ('^outerstruct$')] = pp_outer
282 pretty_printers_dict
[re
.compile ('^struct hint_error$')] = pp_hint_error
283 pretty_printers_dict
[re
.compile ('^hint_error$')] = pp_hint_error
285 pretty_printers_dict
[re
.compile ('^memory_error$')] = MemoryErrorString
287 pretty_printers_dict
[re
.compile ('^eval_type_s$')] = pp_eval_type
289 pretty_printers_dict
= {}
291 register_pretty_printers ()
292 gdb
.pretty_printers
.append (lookup_function
)
This page took 0.042685 seconds and 5 git commands to generate.