]>
Commit | Line | Data |
---|---|---|
1d506c26 | 1 | # Copyright (C) 2008-2024 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 | 21 | |
13123da8 SM |
22 | |
23 | def _iterator(pointer, len): | |
2960a434 PK |
24 | start = pointer |
25 | end = pointer + len | |
26 | while pointer != end: | |
13123da8 | 27 | yield ("[%d]" % int(pointer - start), pointer.dereference()) |
2960a434 PK |
28 | pointer += 1 |
29 | ||
13123da8 | 30 | |
2960a434 | 31 | # Same as _iterator but can be told to raise an exception. |
13123da8 | 32 | def _iterator_except(pointer, len): |
9325cb04 PK |
33 | start = pointer |
34 | end = pointer + len | |
35 | while pointer != end: | |
36 | if exception_flag: | |
13123da8 SM |
37 | raise gdb.MemoryError("hi bob") |
38 | yield ("[%d]" % int(pointer - start), pointer.dereference()) | |
9325cb04 PK |
39 | pointer += 1 |
40 | ||
13123da8 | 41 | |
a6bac58e | 42 | # Test returning a Value from a printer. |
13123da8 | 43 | class string_print(object): |
a6bac58e TT |
44 | def __init__(self, val): |
45 | self.val = val | |
46 | ||
47 | def to_string(self): | |
13123da8 | 48 | return self.val["whybother"]["contents"] |
a6bac58e | 49 | |
a6bac58e | 50 | |
13123da8 SM |
51 | # Test a class-based printer. |
52 | class ContainerPrinter(object): | |
a6bac58e TT |
53 | def __init__(self, val): |
54 | self.val = val | |
55 | ||
56 | def to_string(self): | |
13123da8 | 57 | return "container %s with %d elements" % (self.val["name"], self.val["len"]) |
a6bac58e TT |
58 | |
59 | def children(self): | |
13123da8 | 60 | return _iterator(self.val["elements"], self.val["len"]) |
a6bac58e | 61 | |
13123da8 SM |
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") | |
b1f3973b | 65 | |
13123da8 SM |
66 | if self.val["is_map_p"]: |
67 | return "map" | |
68 | elif self.val["is_array_p"]: | |
69 | return "array" | |
9f9aa852 AB |
70 | else: |
71 | return None | |
72 | ||
13123da8 | 73 | |
731145cb | 74 | # Treats a container as array. |
13123da8 | 75 | class ArrayPrinter(object): |
731145cb TT |
76 | def __init__(self, val): |
77 | self.val = val | |
78 | ||
79 | def to_string(self): | |
13123da8 | 80 | return "array %s with %d elements" % (self.val["name"], self.val["len"]) |
731145cb TT |
81 | |
82 | def children(self): | |
13123da8 SM |
83 | return _iterator(self.val["elements"], self.val["len"]) |
84 | ||
85 | def display_hint(self): | |
86 | return "array" | |
731145cb | 87 | |
731145cb | 88 | |
a4c8e806 TT |
89 | # Flag to make NoStringContainerPrinter throw an exception. |
90 | exception_flag = False | |
91 | ||
09de95fb | 92 | |
79f283fe | 93 | # Test a printer where to_string is None |
13123da8 | 94 | class NoStringContainerPrinter(object): |
79f283fe PM |
95 | def __init__(self, val): |
96 | self.val = val | |
97 | ||
98 | def to_string(self): | |
99 | return None | |
100 | ||
101 | def children(self): | |
13123da8 SM |
102 | return _iterator_except(self.val["elements"], self.val["len"]) |
103 | ||
79f283fe | 104 | |
0625771b LS |
105 | # See ToStringReturnsValueWrapper. |
106 | class ToStringReturnsValueInner: | |
0625771b LS |
107 | def __init__(self, val): |
108 | self.val = val | |
109 | ||
110 | def to_string(self): | |
13123da8 SM |
111 | return "Inner to_string {}".format(int(self.val["val"])) |
112 | ||
0625771b LS |
113 | |
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: | |
0625771b LS |
117 | def __init__(self, val): |
118 | self.val = val | |
119 | ||
120 | def to_string(self): | |
13123da8 SM |
121 | return self.val["inner"] |
122 | ||
0625771b | 123 | |
13123da8 | 124 | class pp_s(object): |
a6bac58e TT |
125 | def __init__(self, val): |
126 | self.val = val | |
127 | ||
128 | def to_string(self): | |
129 | a = self.val["a"] | |
130 | b = self.val["b"] | |
131 | if a.address != b: | |
132 | raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b))) | |
133 | return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
134 | ||
13123da8 SM |
135 | |
136 | class pp_ss(object): | |
a6bac58e TT |
137 | def __init__(self, val): |
138 | self.val = val | |
139 | ||
140 | def to_string(self): | |
141 | return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
142 | ||
13123da8 SM |
143 | |
144 | class pp_sss(object): | |
a6bac58e TT |
145 | def __init__(self, val): |
146 | self.val = val | |
147 | ||
148 | def to_string(self): | |
13123da8 | 149 | return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" |
a6bac58e | 150 | |
13123da8 SM |
151 | |
152 | class pp_multiple_virtual(object): | |
153 | def __init__(self, val): | |
a6bac58e TT |
154 | self.val = val |
155 | ||
13123da8 SM |
156 | def to_string(self): |
157 | return "pp value variable is: " + str(self.val["value"]) | |
158 | ||
a6bac58e | 159 | |
13123da8 SM |
160 | class pp_vbase1(object): |
161 | def __init__(self, val): | |
a6bac58e TT |
162 | self.val = val |
163 | ||
13123da8 | 164 | def to_string(self): |
a6bac58e TT |
165 | return "pp class name: " + self.val.type.tag |
166 | ||
13123da8 SM |
167 | |
168 | class pp_nullstr(object): | |
0cc7d26f TT |
169 | def __init__(self, val): |
170 | self.val = val | |
171 | ||
172 | def to_string(self): | |
13123da8 SM |
173 | return self.val["s"].string(gdb.target_charset()) |
174 | ||
0cc7d26f | 175 | |
13123da8 | 176 | class pp_ns(object): |
fbb8f299 PM |
177 | "Print a std::basic_string of some kind" |
178 | ||
179 | def __init__(self, val): | |
180 | self.val = val | |
181 | ||
182 | def to_string(self): | |
13123da8 SM |
183 | len = self.val["length"] |
184 | return self.val["null_str"].string(gdb.target_charset(), length=len) | |
185 | ||
186 | def display_hint(self): | |
187 | return "string" | |
fbb8f299 | 188 | |
fbb8f299 | 189 | |
3a772aa4 TT |
190 | pp_ls_encoding = None |
191 | ||
13123da8 SM |
192 | |
193 | class pp_ls(object): | |
be759fcf PM |
194 | "Print a std::basic_string of some kind" |
195 | ||
196 | def __init__(self, val): | |
197 | self.val = val | |
198 | ||
199 | def to_string(self): | |
13123da8 | 200 | length = self.val["len"] |
3a772aa4 | 201 | if pp_ls_encoding is not None: |
4ea6efe9 | 202 | if length >= 0: |
13123da8 SM |
203 | return self.val["lazy_str"].lazy_string( |
204 | encoding=pp_ls_encoding, length=length | |
205 | ) | |
4ea6efe9 | 206 | else: |
13123da8 | 207 | return self.val["lazy_str"].lazy_string(encoding=pp_ls_encoding) |
3a772aa4 | 208 | else: |
4ea6efe9 | 209 | if length >= 0: |
13123da8 | 210 | return self.val["lazy_str"].lazy_string(length=length) |
4ea6efe9 | 211 | else: |
13123da8 SM |
212 | return self.val["lazy_str"].lazy_string() |
213 | ||
214 | def display_hint(self): | |
215 | return "string" | |
be759fcf | 216 | |
be759fcf | 217 | |
13123da8 | 218 | class pp_hint_error(object): |
e1ab1f9c JK |
219 | "Throw error from display_hint" |
220 | ||
221 | def __init__(self, val): | |
222 | self.val = val | |
223 | ||
224 | def to_string(self): | |
13123da8 | 225 | return "hint_error_val" |
e1ab1f9c | 226 | |
13123da8 | 227 | def display_hint(self): |
e1ab1f9c JK |
228 | raise Exception("hint failed") |
229 | ||
13123da8 SM |
230 | |
231 | class pp_children_as_list(object): | |
2c12abee TT |
232 | "Throw error from display_hint" |
233 | ||
234 | def __init__(self, val): | |
235 | self.val = val | |
236 | ||
237 | def to_string(self): | |
13123da8 SM |
238 | return "children_as_list_val" |
239 | ||
240 | def children(self): | |
241 | return [("one", 1)] | |
2c12abee | 242 | |
2c12abee | 243 | |
13123da8 | 244 | class pp_outer(object): |
0cc7d26f TT |
245 | "Print struct outer" |
246 | ||
13123da8 | 247 | def __init__(self, val): |
0cc7d26f TT |
248 | self.val = val |
249 | ||
13123da8 SM |
250 | def to_string(self): |
251 | return "x = %s" % self.val["x"] | |
252 | ||
253 | def children(self): | |
254 | yield "s", self.val["s"] | |
255 | yield "x", self.val["x"] | |
0cc7d26f | 256 | |
0cc7d26f | 257 | |
13123da8 | 258 | class MemoryErrorString(object): |
00bd41d6 PM |
259 | "Raise an error" |
260 | ||
261 | def __init__(self, val): | |
262 | self.val = val | |
263 | ||
264 | def to_string(self): | |
13123da8 | 265 | raise gdb.MemoryError("Cannot access memory.") |
00bd41d6 | 266 | |
13123da8 SM |
267 | def display_hint(self): |
268 | return "string" | |
00bd41d6 | 269 | |
13123da8 SM |
270 | |
271 | class pp_eval_type(object): | |
8f043999 JK |
272 | def __init__(self, val): |
273 | self.val = val | |
274 | ||
275 | def to_string(self): | |
9325cb04 | 276 | gdb.execute("bt", to_string=True) |
13123da8 SM |
277 | return ( |
278 | "eval=<" | |
279 | + str(gdb.parse_and_eval("eval_func (123456789, 2, 3, 4, 5, 6, 7, 8)")) | |
280 | + ">" | |
281 | ) | |
282 | ||
8f043999 | 283 | |
13123da8 | 284 | class pp_int_typedef(object): |
c973d0aa PA |
285 | def __init__(self, val): |
286 | self.val = val | |
287 | ||
288 | def to_string(self): | |
289 | return "type=%s, val=%s" % (self.val.type, int(self.val)) | |
290 | ||
13123da8 SM |
291 | |
292 | class pp_int_typedef3(object): | |
332cf4c9 TT |
293 | "A printer without a to_string method" |
294 | ||
295 | def __init__(self, val): | |
296 | self.val = val | |
297 | ||
298 | def children(self): | |
13123da8 | 299 | yield "s", 27 |
332cf4c9 | 300 | |
13123da8 SM |
301 | |
302 | def lookup_function(val): | |
a6bac58e TT |
303 | "Look-up and return a pretty-printer that can print val." |
304 | ||
305 | # Get the type. | |
0cc7d26f | 306 | type = val.type |
a6bac58e TT |
307 | |
308 | # If it points to a reference, get the reference. | |
309 | if type.code == gdb.TYPE_CODE_REF: | |
13123da8 | 310 | type = type.target() |
a6bac58e TT |
311 | |
312 | # Get the unqualified type, stripped of typedefs. | |
13123da8 | 313 | type = type.unqualified().strip_typedefs() |
a6bac58e | 314 | |
13123da8 | 315 | # Get the type name. |
a6bac58e TT |
316 | typename = type.tag |
317 | ||
f9e59d06 | 318 | if typename is None: |
a6bac58e TT |
319 | return None |
320 | ||
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: | |
13123da8 SM |
325 | if function.match(typename): |
326 | return pretty_printers_dict[function](val) | |
327 | ||
a6bac58e TT |
328 | # Cannot find a pretty printer. Return None. |
329 | ||
330 | return None | |
331 | ||
13123da8 SM |
332 | |
333 | def disable_lookup_function(): | |
967cf477 DE |
334 | lookup_function.enabled = False |
335 | ||
13123da8 SM |
336 | |
337 | def enable_lookup_function(): | |
967cf477 | 338 | lookup_function.enabled = True |
a6bac58e | 339 | |
13123da8 | 340 | |
c973d0aa | 341 | # Lookup a printer for VAL in the typedefs dict. |
13123da8 | 342 | def lookup_typedefs_function(val): |
c973d0aa PA |
343 | "Look-up and return a pretty-printer that can print val (typedefs)." |
344 | ||
345 | # Get the type. | |
346 | type = val.type | |
347 | ||
f9e59d06 | 348 | if type is None or type.name is None or type.code != gdb.TYPE_CODE_TYPEDEF: |
c973d0aa PA |
349 | return None |
350 | ||
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: | |
13123da8 SM |
355 | if function.match(type.name): |
356 | return typedefs_pretty_printers_dict[function](val) | |
c973d0aa PA |
357 | |
358 | # Cannot find a pretty printer. | |
359 | return None | |
360 | ||
13123da8 SM |
361 | |
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 | |
366 | ||
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 | |
371 | ||
372 | pretty_printers_dict[re.compile("^VirtualTest$")] = pp_multiple_virtual | |
373 | pretty_printers_dict[re.compile("^Vbase1$")] = pp_vbase1 | |
374 | ||
375 | pretty_printers_dict[re.compile("^struct nullstr$")] = pp_nullstr | |
376 | pretty_printers_dict[re.compile("^nullstr$")] = pp_nullstr | |
377 | ||
a6bac58e TT |
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. | |
13123da8 SM |
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 | |
388 | ||
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 | |
0625771b | 401 | |
13123da8 SM |
402 | pretty_printers_dict[re.compile("^struct ns$")] = pp_ns |
403 | pretty_printers_dict[re.compile("^ns$")] = pp_ns | |
0625771b | 404 | |
13123da8 SM |
405 | pretty_printers_dict[re.compile("^struct lazystring$")] = pp_ls |
406 | pretty_printers_dict[re.compile("^lazystring$")] = pp_ls | |
0cc7d26f | 407 | |
13123da8 SM |
408 | pretty_printers_dict[re.compile("^struct outerstruct$")] = pp_outer |
409 | pretty_printers_dict[re.compile("^outerstruct$")] = pp_outer | |
be759fcf | 410 | |
13123da8 SM |
411 | pretty_printers_dict[re.compile("^struct hint_error$")] = pp_hint_error |
412 | pretty_printers_dict[re.compile("^hint_error$")] = pp_hint_error | |
0cc7d26f | 413 | |
13123da8 SM |
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 | |
e1ab1f9c | 416 | |
13123da8 | 417 | pretty_printers_dict[re.compile("^memory_error$")] = MemoryErrorString |
2c12abee | 418 | |
13123da8 | 419 | pretty_printers_dict[re.compile("^eval_type_s$")] = pp_eval_type |
00bd41d6 | 420 | |
13123da8 SM |
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 | |
8f043999 | 424 | |
c973d0aa PA |
425 | |
426 | # Dict for struct types with typedefs fully stripped. | |
a6bac58e | 427 | pretty_printers_dict = {} |
c973d0aa PA |
428 | # Dict for typedef types. |
429 | typedefs_pretty_printers_dict = {} | |
a6bac58e | 430 | |
13123da8 SM |
431 | register_pretty_printers() |
432 | gdb.pretty_printers.append(lookup_function) | |
433 | gdb.pretty_printers.append(lookup_typedefs_function) |