Commit | Line | Data |
---|---|---|
88b9d363 | 1 | # Copyright (C) 2019-2022 Free Software Foundation, Inc. |
2e62ab40 AB |
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 GDB's printing of | |
17 | # nested map like structures. | |
18 | ||
19 | import re | |
20 | import gdb | |
21 | ||
13123da8 SM |
22 | |
23 | def _iterator1(pointer, len): | |
2e62ab40 AB |
24 | while len > 0: |
25 | map = pointer.dereference() | |
13123da8 SM |
26 | yield ("", map["name"]) |
27 | yield ("", map.dereference()) | |
2e62ab40 AB |
28 | pointer += 1 |
29 | len -= 1 | |
30 | ||
13123da8 SM |
31 | |
32 | def _iterator2(pointer1, pointer2, len): | |
2e62ab40 AB |
33 | while len > 0: |
34 | yield ("", pointer1.dereference()) | |
35 | yield ("", pointer2.dereference()) | |
36 | pointer1 += 1 | |
37 | pointer2 += 1 | |
38 | len -= 1 | |
39 | ||
13123da8 SM |
40 | |
41 | class pp_map(object): | |
2e62ab40 AB |
42 | def __init__(self, val): |
43 | self.val = val | |
44 | ||
45 | def to_string(self): | |
13123da8 | 46 | if self.val["show_header"] == 0: |
2e62ab40 AB |
47 | return None |
48 | else: | |
49 | return "pp_map" | |
50 | ||
51 | def children(self): | |
13123da8 SM |
52 | return _iterator2(self.val["keys"], self.val["values"], self.val["length"]) |
53 | ||
54 | def display_hint(self): | |
55 | return "map" | |
2e62ab40 | 56 | |
2e62ab40 | 57 | |
13123da8 | 58 | class pp_map_map(object): |
2e62ab40 AB |
59 | def __init__(self, val): |
60 | self.val = val | |
61 | ||
62 | def to_string(self): | |
13123da8 | 63 | if self.val["show_header"] == 0: |
2e62ab40 AB |
64 | return None |
65 | else: | |
66 | return "pp_map_map" | |
67 | ||
68 | def children(self): | |
13123da8 | 69 | return _iterator1(self.val["values"], self.val["length"]) |
2e62ab40 | 70 | |
13123da8 SM |
71 | def display_hint(self): |
72 | return "map" | |
2e62ab40 | 73 | |
13123da8 SM |
74 | |
75 | def lookup_function(val): | |
2e62ab40 AB |
76 | "Look-up and return a pretty-printer that can print val." |
77 | ||
78 | # Get the type. | |
79 | type = val.type | |
80 | ||
81 | # If it points to a reference, get the reference. | |
82 | if type.code == gdb.TYPE_CODE_REF: | |
13123da8 | 83 | type = type.target() |
2e62ab40 AB |
84 | |
85 | # Get the unqualified type, stripped of typedefs. | |
13123da8 | 86 | type = type.unqualified().strip_typedefs() |
2e62ab40 AB |
87 | |
88 | # Get the type name. | |
89 | typename = type.tag | |
90 | ||
f9e59d06 | 91 | if typename is None: |
2e62ab40 AB |
92 | return None |
93 | ||
94 | # Iterate over local dictionary of types to determine | |
95 | # if a printer is registered for that type. Return an | |
96 | # instantiation of the printer if found. | |
97 | for function in pretty_printers_dict: | |
13123da8 SM |
98 | if function.match(typename): |
99 | return pretty_printers_dict[function](val) | |
2e62ab40 AB |
100 | |
101 | # Cannot find a pretty printer. Return None. | |
102 | return None | |
103 | ||
13123da8 | 104 | |
2e62ab40 | 105 | # Lookup a printer for VAL in the typedefs dict. |
13123da8 | 106 | def lookup_typedefs_function(val): |
2e62ab40 AB |
107 | "Look-up and return a pretty-printer that can print val (typedefs)." |
108 | ||
109 | # Get the type. | |
110 | type = val.type | |
111 | ||
f9e59d06 | 112 | if type is None or type.name is None or type.code != gdb.TYPE_CODE_TYPEDEF: |
2e62ab40 AB |
113 | return None |
114 | ||
115 | # Iterate over local dictionary of typedef types to determine if a | |
116 | # printer is registered for that type. Return an instantiation of | |
117 | # the printer if found. | |
118 | for function in typedefs_pretty_printers_dict: | |
13123da8 SM |
119 | if function.match(type.name): |
120 | return typedefs_pretty_printers_dict[function](val) | |
2e62ab40 AB |
121 | |
122 | # Cannot find a pretty printer. | |
123 | return None | |
124 | ||
13123da8 SM |
125 | |
126 | def register_pretty_printers(): | |
127 | pretty_printers_dict[re.compile("^struct map_t$")] = pp_map | |
128 | pretty_printers_dict[re.compile("^map_t$")] = pp_map | |
129 | pretty_printers_dict[re.compile("^struct map_map_t$")] = pp_map_map | |
130 | pretty_printers_dict[re.compile("^map_map_t$")] = pp_map_map | |
131 | ||
2e62ab40 AB |
132 | |
133 | # Dict for struct types with typedefs fully stripped. | |
134 | pretty_printers_dict = {} | |
135 | # Dict for typedef types. | |
136 | typedefs_pretty_printers_dict = {} | |
137 | ||
13123da8 SM |
138 | register_pretty_printers() |
139 | gdb.pretty_printers.append(lookup_function) | |
140 | gdb.pretty_printers.append(lookup_typedefs_function) |