Commit | Line | Data |
---|---|---|
c5a57081 | 1 | # Copyright (C) 2008-2012 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 | ||
19 | import re | |
00bd41d6 | 20 | import gdb |
a6bac58e TT |
21 | |
22 | # Test returning a Value from a printer. | |
23 | class string_print: | |
24 | def __init__(self, val): | |
25 | self.val = val | |
26 | ||
27 | def to_string(self): | |
28 | return self.val['whybother']['contents'] | |
29 | ||
30 | # Test a class-based printer. | |
31 | class ContainerPrinter: | |
32 | class _iterator: | |
33 | def __init__ (self, pointer, len): | |
34 | self.start = pointer | |
35 | self.pointer = pointer | |
36 | self.end = pointer + len | |
37 | ||
38 | def __iter__(self): | |
39 | return self | |
40 | ||
41 | def next(self): | |
42 | if self.pointer == self.end: | |
43 | raise StopIteration | |
44 | result = self.pointer | |
45 | self.pointer = self.pointer + 1 | |
46 | return ('[%d]' % int (result - self.start), result.dereference()) | |
47 | ||
48 | def __init__(self, val): | |
49 | self.val = val | |
50 | ||
51 | def to_string(self): | |
52 | return 'container %s with %d elements' % (self.val['name'], self.val['len']) | |
53 | ||
54 | def children(self): | |
55 | return self._iterator(self.val['elements'], self.val['len']) | |
56 | ||
a4c8e806 TT |
57 | # Flag to make NoStringContainerPrinter throw an exception. |
58 | exception_flag = False | |
59 | ||
79f283fe PM |
60 | # Test a printer where to_string is None |
61 | class NoStringContainerPrinter: | |
62 | class _iterator: | |
63 | def __init__ (self, pointer, len): | |
64 | self.start = pointer | |
65 | self.pointer = pointer | |
66 | self.end = pointer + len | |
67 | ||
68 | def __iter__(self): | |
69 | return self | |
70 | ||
71 | def next(self): | |
72 | if self.pointer == self.end: | |
73 | raise StopIteration | |
a4c8e806 TT |
74 | if exception_flag: |
75 | raise gdb.MemoryError, 'hi bob' | |
79f283fe PM |
76 | result = self.pointer |
77 | self.pointer = self.pointer + 1 | |
78 | return ('[%d]' % int (result - self.start), result.dereference()) | |
79 | ||
80 | def __init__(self, val): | |
81 | self.val = val | |
82 | ||
83 | def to_string(self): | |
84 | return None | |
85 | ||
86 | def children(self): | |
87 | return self._iterator(self.val['elements'], self.val['len']) | |
88 | ||
a6bac58e TT |
89 | class pp_s: |
90 | def __init__(self, val): | |
91 | self.val = val | |
92 | ||
93 | def to_string(self): | |
94 | a = self.val["a"] | |
95 | b = self.val["b"] | |
96 | if a.address != b: | |
97 | raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b))) | |
98 | return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
99 | ||
100 | class pp_ss: | |
101 | def __init__(self, val): | |
102 | self.val = val | |
103 | ||
104 | def to_string(self): | |
105 | return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
106 | ||
107 | class pp_sss: | |
108 | def __init__(self, val): | |
109 | self.val = val | |
110 | ||
111 | def to_string(self): | |
112 | return "a=<" + str(self.val['a']) + "> b=<" + str(self.val["b"]) + ">" | |
113 | ||
114 | class pp_multiple_virtual: | |
115 | def __init__ (self, val): | |
116 | self.val = val | |
117 | ||
118 | def to_string (self): | |
119 | return "pp value variable is: " + str (self.val['value']) | |
120 | ||
121 | class pp_vbase1: | |
122 | def __init__ (self, val): | |
123 | self.val = val | |
124 | ||
125 | def to_string (self): | |
126 | return "pp class name: " + self.val.type.tag | |
127 | ||
0cc7d26f TT |
128 | class pp_nullstr: |
129 | def __init__(self, val): | |
130 | self.val = val | |
131 | ||
132 | def to_string(self): | |
f870a310 | 133 | return self.val['s'].string(gdb.target_charset()) |
0cc7d26f | 134 | |
fbb8f299 PM |
135 | class pp_ns: |
136 | "Print a std::basic_string of some kind" | |
137 | ||
138 | def __init__(self, val): | |
139 | self.val = val | |
140 | ||
141 | def to_string(self): | |
142 | len = self.val['length'] | |
f870a310 | 143 | return self.val['null_str'].string (gdb.target_charset(), length = len) |
fbb8f299 PM |
144 | |
145 | def display_hint (self): | |
146 | return 'string' | |
147 | ||
3a772aa4 TT |
148 | pp_ls_encoding = None |
149 | ||
be759fcf PM |
150 | class pp_ls: |
151 | "Print a std::basic_string of some kind" | |
152 | ||
153 | def __init__(self, val): | |
154 | self.val = val | |
155 | ||
156 | def to_string(self): | |
3a772aa4 TT |
157 | if pp_ls_encoding is not None: |
158 | return self.val['lazy_str'].lazy_string(encoding = pp_ls_encoding) | |
159 | else: | |
160 | return self.val['lazy_str'].lazy_string() | |
be759fcf PM |
161 | |
162 | def display_hint (self): | |
163 | return 'string' | |
164 | ||
e1ab1f9c JK |
165 | class pp_hint_error: |
166 | "Throw error from display_hint" | |
167 | ||
168 | def __init__(self, val): | |
169 | self.val = val | |
170 | ||
171 | def to_string(self): | |
172 | return 'hint_error_val' | |
173 | ||
174 | def display_hint (self): | |
175 | raise Exception("hint failed") | |
176 | ||
0cc7d26f TT |
177 | class pp_outer: |
178 | "Print struct outer" | |
179 | ||
180 | def __init__ (self, val): | |
181 | self.val = val | |
182 | ||
183 | def to_string (self): | |
184 | return "x = %s" % self.val['x'] | |
185 | ||
186 | def children (self): | |
187 | yield 's', self.val['s'] | |
188 | yield 'x', self.val['x'] | |
189 | ||
00bd41d6 PM |
190 | class MemoryErrorString: |
191 | "Raise an error" | |
192 | ||
193 | def __init__(self, val): | |
194 | self.val = val | |
195 | ||
196 | def to_string(self): | |
197 | raise gdb.MemoryError ("Cannot access memory."); | |
198 | ||
199 | def display_hint (self): | |
200 | return 'string' | |
201 | ||
a6bac58e TT |
202 | def lookup_function (val): |
203 | "Look-up and return a pretty-printer that can print val." | |
204 | ||
205 | # Get the type. | |
0cc7d26f | 206 | type = val.type |
a6bac58e TT |
207 | |
208 | # If it points to a reference, get the reference. | |
209 | if type.code == gdb.TYPE_CODE_REF: | |
210 | type = type.target () | |
211 | ||
212 | # Get the unqualified type, stripped of typedefs. | |
213 | type = type.unqualified ().strip_typedefs () | |
214 | ||
215 | # Get the type name. | |
216 | typename = type.tag | |
217 | ||
218 | if typename == None: | |
219 | return None | |
220 | ||
221 | # Iterate over local dictionary of types to determine | |
222 | # if a printer is registered for that type. Return an | |
223 | # instantiation of the printer if found. | |
224 | for function in pretty_printers_dict: | |
225 | if function.match (typename): | |
226 | return pretty_printers_dict[function] (val) | |
227 | ||
228 | # Cannot find a pretty printer. Return None. | |
229 | ||
230 | return None | |
231 | ||
967cf477 DE |
232 | def disable_lookup_function (): |
233 | lookup_function.enabled = False | |
234 | ||
235 | def enable_lookup_function (): | |
236 | lookup_function.enabled = True | |
a6bac58e TT |
237 | |
238 | def register_pretty_printers (): | |
239 | pretty_printers_dict[re.compile ('^struct s$')] = pp_s | |
240 | pretty_printers_dict[re.compile ('^s$')] = pp_s | |
241 | pretty_printers_dict[re.compile ('^S$')] = pp_s | |
242 | ||
243 | pretty_printers_dict[re.compile ('^struct ss$')] = pp_ss | |
244 | pretty_printers_dict[re.compile ('^ss$')] = pp_ss | |
245 | pretty_printers_dict[re.compile ('^const S &$')] = pp_s | |
246 | pretty_printers_dict[re.compile ('^SSS$')] = pp_sss | |
247 | ||
248 | pretty_printers_dict[re.compile ('^VirtualTest$')] = pp_multiple_virtual | |
249 | pretty_printers_dict[re.compile ('^Vbase1$')] = pp_vbase1 | |
0cc7d26f TT |
250 | |
251 | pretty_printers_dict[re.compile ('^struct nullstr$')] = pp_nullstr | |
252 | pretty_printers_dict[re.compile ('^nullstr$')] = pp_nullstr | |
a6bac58e TT |
253 | |
254 | # Note that we purposely omit the typedef names here. | |
255 | # Printer lookup is based on canonical name. | |
256 | # However, we do need both tagged and untagged variants, to handle | |
257 | # both the C and C++ cases. | |
258 | pretty_printers_dict[re.compile ('^struct string_repr$')] = string_print | |
259 | pretty_printers_dict[re.compile ('^struct container$')] = ContainerPrinter | |
79f283fe | 260 | pretty_printers_dict[re.compile ('^struct justchildren$')] = NoStringContainerPrinter |
a6bac58e TT |
261 | pretty_printers_dict[re.compile ('^string_repr$')] = string_print |
262 | pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter | |
79f283fe | 263 | pretty_printers_dict[re.compile ('^justchildren$')] = NoStringContainerPrinter |
a6bac58e | 264 | |
fbb8f299 PM |
265 | pretty_printers_dict[re.compile ('^struct ns$')] = pp_ns |
266 | pretty_printers_dict[re.compile ('^ns$')] = pp_ns | |
0cc7d26f | 267 | |
be759fcf PM |
268 | pretty_printers_dict[re.compile ('^struct lazystring$')] = pp_ls |
269 | pretty_printers_dict[re.compile ('^lazystring$')] = pp_ls | |
270 | ||
0cc7d26f TT |
271 | pretty_printers_dict[re.compile ('^struct outerstruct$')] = pp_outer |
272 | pretty_printers_dict[re.compile ('^outerstruct$')] = pp_outer | |
273 | ||
e1ab1f9c JK |
274 | pretty_printers_dict[re.compile ('^struct hint_error$')] = pp_hint_error |
275 | pretty_printers_dict[re.compile ('^hint_error$')] = pp_hint_error | |
276 | ||
00bd41d6 PM |
277 | pretty_printers_dict[re.compile ('^memory_error$')] = MemoryErrorString |
278 | ||
a6bac58e TT |
279 | pretty_printers_dict = {} |
280 | ||
281 | register_pretty_printers () | |
282 | gdb.pretty_printers.append (lookup_function) |