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