]> git.proxmox.com Git - rustc.git/blame - src/etc/gdb_providers.py
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / src / etc / gdb_providers.py
CommitLineData
f035d41b
XL
1from sys import version_info
2
3import gdb
4from gdb import lookup_type
5
6if version_info[0] >= 3:
7 xrange = range
8
9ZERO_FIELD = "__0"
10FIRST_FIELD = "__1"
11
12
13def unwrap_unique_or_non_null(unique_or_nonnull):
14 # BACKCOMPAT: rust 1.32
15 # https://github.com/rust-lang/rust/commit/7a0911528058e87d22ea305695f4047572c5e067
16 ptr = unique_or_nonnull["pointer"]
17 return ptr if ptr.type.code == gdb.TYPE_CODE_PTR else ptr[ZERO_FIELD]
18
19
20class EnumProvider:
21 def __init__(self, valobj):
22 content = valobj[valobj.type.fields()[0]]
23 fields = content.type.fields()
24 self.empty = len(fields) == 0
25 if not self.empty:
26 if len(fields) == 1:
27 discriminant = 0
28 else:
29 discriminant = int(content[fields[0]]) + 1
30 self.active_variant = content[fields[discriminant]]
31 self.name = fields[discriminant].name
32 self.full_name = "{}::{}".format(valobj.type.name, self.name)
33 else:
34 self.full_name = valobj.type.name
35
36 def to_string(self):
37 return self.full_name
38
39 def children(self):
40 if not self.empty:
41 yield self.name, self.active_variant
42
43
44class StdStringProvider:
45 def __init__(self, valobj):
46 self.valobj = valobj
47 vec = valobj["vec"]
48 self.length = int(vec["len"])
49 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
50
51 def to_string(self):
52 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
53
54 @staticmethod
55 def display_hint():
56 return "string"
57
58
59class StdOsStringProvider:
60 def __init__(self, valobj):
61 self.valobj = valobj
62 buf = self.valobj["inner"]["inner"]
63 is_windows = "Wtf8Buf" in buf.type.name
64 vec = buf[ZERO_FIELD] if is_windows else buf
65
66 self.length = int(vec["len"])
67 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
68
69 def to_string(self):
70 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
71
72 def display_hint(self):
73 return "string"
74
75
76class StdStrProvider:
77 def __init__(self, valobj):
78 self.valobj = valobj
79 self.length = int(valobj["length"])
80 self.data_ptr = valobj["data_ptr"]
81
82 def to_string(self):
83 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
84
85 @staticmethod
86 def display_hint():
87 return "string"
88
89
90class StdVecProvider:
91 def __init__(self, valobj):
92 self.valobj = valobj
93 self.length = int(valobj["len"])
94 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
95
96 def to_string(self):
97 return "Vec(size={})".format(self.length)
98
99 def children(self):
100 saw_inaccessible = False
101 for index in xrange(self.length):
102 element_ptr = self.data_ptr + index
103 if saw_inaccessible:
104 return
105 try:
106 # rust-lang/rust#64343: passing deref expr to `str` allows
107 # catching exception on garbage pointer
108 str(element_ptr.dereference())
109 yield "[{}]".format(index), element_ptr.dereference()
110 except RuntimeError:
111 saw_inaccessible = True
112 yield str(index), "inaccessible"
113
114 @staticmethod
115 def display_hint():
116 return "array"
117
118
119class StdVecDequeProvider:
120 def __init__(self, valobj):
121 self.valobj = valobj
122 self.head = int(valobj["head"])
123 self.tail = int(valobj["tail"])
124 self.cap = int(valobj["buf"]["cap"])
125 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
126 if self.head >= self.tail:
127 self.size = self.head - self.tail
128 else:
129 self.size = self.cap + self.head - self.tail
130
131 def to_string(self):
132 return "VecDeque(size={})".format(self.size)
133
134 def children(self):
135 for index in xrange(0, self.size):
136 value = (self.data_ptr + ((self.tail + index) % self.cap)).dereference()
137 yield "[{}]".format(index), value
138
139 @staticmethod
140 def display_hint():
141 return "array"
142
143
144class StdRcProvider:
145 def __init__(self, valobj, is_atomic=False):
146 self.valobj = valobj
147 self.is_atomic = is_atomic
148 self.ptr = unwrap_unique_or_non_null(valobj["ptr"])
149 self.value = self.ptr["data" if is_atomic else "value"]
150 self.strong = self.ptr["strong"]["v" if is_atomic else "value"]["value"]
151 self.weak = self.ptr["weak"]["v" if is_atomic else "value"]["value"] - 1
152
153 def to_string(self):
154 if self.is_atomic:
155 return "Arc(strong={}, weak={})".format(int(self.strong), int(self.weak))
156 else:
157 return "Rc(strong={}, weak={})".format(int(self.strong), int(self.weak))
158
159 def children(self):
160 yield "value", self.value
161 yield "strong", self.strong
162 yield "weak", self.weak
163
164
165class StdCellProvider:
166 def __init__(self, valobj):
167 self.value = valobj["value"]["value"]
168
169 def to_string(self):
170 return "Cell"
171
172 def children(self):
173 yield "value", self.value
174
175
176class StdRefProvider:
177 def __init__(self, valobj):
178 self.value = valobj["value"].dereference()
179 self.borrow = valobj["borrow"]["borrow"]["value"]["value"]
180
181 def to_string(self):
182 borrow = int(self.borrow)
183 if borrow >= 0:
184 return "Ref(borrow={})".format(borrow)
185 else:
186 return "Ref(borrow_mut={})".format(-borrow)
187
188 def children(self):
189 yield "*value", self.value
190 yield "borrow", self.borrow
191
192
193class StdRefCellProvider:
194 def __init__(self, valobj):
195 self.value = valobj["value"]["value"]
196 self.borrow = valobj["borrow"]["value"]["value"]
197
198 def to_string(self):
199 borrow = int(self.borrow)
200 if borrow >= 0:
201 return "RefCell(borrow={})".format(borrow)
202 else:
203 return "RefCell(borrow_mut={})".format(-borrow)
204
205 def children(self):
206 yield "value", self.value
207 yield "borrow", self.borrow
208
209
29967ef6
XL
210# Yields children (in a provider's sense of the word) for a tree headed by a BoxedNode.
211# In particular, yields each key/value pair in the node and in any child nodes.
212def children_of_node(boxed_node, height):
f035d41b 213 def cast_to_internal(node):
29967ef6 214 internal_type_name = node.type.target().name.replace("LeafNode", "InternalNode", 1)
f035d41b
XL
215 internal_type = lookup_type(internal_type_name)
216 return node.cast(internal_type.pointer())
217
218 node_ptr = unwrap_unique_or_non_null(boxed_node["ptr"])
29967ef6 219 leaf = node_ptr.dereference()
f035d41b 220 keys = leaf["keys"]
29967ef6
XL
221 vals = leaf["vals"]
222 edges = cast_to_internal(node_ptr)["edges"] if height > 0 else None
f035d41b
XL
223 length = int(leaf["len"])
224
225 for i in xrange(0, length + 1):
226 if height > 0:
29967ef6
XL
227 boxed_child_node = edges[i]["value"]["value"]
228 for child in children_of_node(boxed_child_node, height - 1):
f035d41b
XL
229 yield child
230 if i < length:
29967ef6
XL
231 # Avoid "Cannot perform pointer math on incomplete type" on zero-sized arrays.
232 key = keys[i]["value"]["value"] if keys.type.sizeof > 0 else "()"
233 val = vals[i]["value"]["value"] if vals.type.sizeof > 0 else "()"
234 yield key, val
235
236
237# Yields children for a BTreeMap.
238def children_of_map(map):
239 if map["length"] > 0:
240 root = map["root"]
241 if root.type.name.startswith("core::option::Option<"):
242 root = root.cast(gdb.lookup_type(root.type.name[21:-1]))
243 boxed_root_node = root["node"]
244 height = root["height"]
245 for child in children_of_node(boxed_root_node, height):
246 yield child
f035d41b
XL
247
248
249class StdBTreeSetProvider:
250 def __init__(self, valobj):
251 self.valobj = valobj
252
253 def to_string(self):
254 return "BTreeSet(size={})".format(self.valobj["map"]["length"])
255
256 def children(self):
257 inner_map = self.valobj["map"]
29967ef6
XL
258 for i, (child, _) in enumerate(children_of_map(inner_map)):
259 yield "[{}]".format(i), child
f035d41b
XL
260
261 @staticmethod
262 def display_hint():
263 return "array"
264
265
266class StdBTreeMapProvider:
267 def __init__(self, valobj):
268 self.valobj = valobj
269
270 def to_string(self):
271 return "BTreeMap(size={})".format(self.valobj["length"])
272
273 def children(self):
29967ef6
XL
274 for i, (key, val) in enumerate(children_of_map(self.valobj)):
275 yield "key{}".format(i), key
276 yield "val{}".format(i), val
f035d41b
XL
277
278 @staticmethod
279 def display_hint():
280 return "map"
281
282
283# BACKCOMPAT: rust 1.35
284class StdOldHashMapProvider:
285 def __init__(self, valobj, show_values=True):
286 self.valobj = valobj
287 self.show_values = show_values
288
289 self.table = self.valobj["table"]
290 self.size = int(self.table["size"])
291 self.hashes = self.table["hashes"]
292 self.hash_uint_type = self.hashes.type
293 self.hash_uint_size = self.hashes.type.sizeof
294 self.modulo = 2 ** self.hash_uint_size
295 self.data_ptr = self.hashes[ZERO_FIELD]["pointer"]
296
297 self.capacity_mask = int(self.table["capacity_mask"])
298 self.capacity = (self.capacity_mask + 1) % self.modulo
299
300 marker = self.table["marker"].type
301 self.pair_type = marker.template_argument(0)
302 self.pair_type_size = self.pair_type.sizeof
303
304 self.valid_indices = []
305 for idx in range(self.capacity):
306 data_ptr = self.data_ptr.cast(self.hash_uint_type.pointer())
307 address = data_ptr + idx
308 hash_uint = address.dereference()
309 hash_ptr = hash_uint[ZERO_FIELD]["pointer"]
310 if int(hash_ptr) != 0:
311 self.valid_indices.append(idx)
312
313 def to_string(self):
314 if self.show_values:
315 return "HashMap(size={})".format(self.size)
316 else:
317 return "HashSet(size={})".format(self.size)
318
319 def children(self):
320 start = int(self.data_ptr) & ~1
321
322 hashes = self.hash_uint_size * self.capacity
323 align = self.pair_type_size
324 len_rounded_up = (((((hashes + align) % self.modulo - 1) % self.modulo) & ~(
325 (align - 1) % self.modulo)) % self.modulo - hashes) % self.modulo
326
327 pairs_offset = hashes + len_rounded_up
328 pairs_start = gdb.Value(start + pairs_offset).cast(self.pair_type.pointer())
329
330 for index in range(self.size):
331 table_index = self.valid_indices[index]
332 idx = table_index & self.capacity_mask
333 element = (pairs_start + idx).dereference()
334 if self.show_values:
335 yield "key{}".format(index), element[ZERO_FIELD]
336 yield "val{}".format(index), element[FIRST_FIELD]
337 else:
338 yield "[{}]".format(index), element[ZERO_FIELD]
339
340 def display_hint(self):
341 return "map" if self.show_values else "array"
342
343
344class StdHashMapProvider:
345 def __init__(self, valobj, show_values=True):
346 self.valobj = valobj
347 self.show_values = show_values
348
1b1a35ee 349 table = self.table()
f035d41b
XL
350 capacity = int(table["bucket_mask"]) + 1
351 ctrl = table["ctrl"]["pointer"]
352
353 self.size = int(table["items"])
3dfed10e
XL
354 self.pair_type = table.type.template_argument(0)
355
356 self.new_layout = not table.type.has_key("data")
357 if self.new_layout:
358 self.data_ptr = ctrl.cast(self.pair_type.pointer())
359 else:
360 self.data_ptr = table["data"]["pointer"]
f035d41b
XL
361
362 self.valid_indices = []
363 for idx in range(capacity):
364 address = ctrl + idx
365 value = address.dereference()
366 is_presented = value & 128 == 0
367 if is_presented:
368 self.valid_indices.append(idx)
369
1b1a35ee
XL
370 def table(self):
371 if self.show_values:
372 hashbrown_hashmap = self.valobj["base"]
373 elif self.valobj.type.fields()[0].name == "map":
374 # BACKCOMPAT: rust 1.47
375 # HashSet wraps std::collections::HashMap, which wraps hashbrown::HashMap
376 hashbrown_hashmap = self.valobj["map"]["base"]
377 else:
378 # HashSet wraps hashbrown::HashSet, which wraps hashbrown::HashMap
379 hashbrown_hashmap = self.valobj["base"]["map"]
380 return hashbrown_hashmap["table"]
381
f035d41b
XL
382 def to_string(self):
383 if self.show_values:
384 return "HashMap(size={})".format(self.size)
385 else:
386 return "HashSet(size={})".format(self.size)
387
388 def children(self):
389 pairs_start = self.data_ptr
390
391 for index in range(self.size):
392 idx = self.valid_indices[index]
3dfed10e
XL
393 if self.new_layout:
394 idx = -(idx + 1)
f035d41b
XL
395 element = (pairs_start + idx).dereference()
396 if self.show_values:
397 yield "key{}".format(index), element[ZERO_FIELD]
398 yield "val{}".format(index), element[FIRST_FIELD]
399 else:
400 yield "[{}]".format(index), element[ZERO_FIELD]
401
402 def display_hint(self):
403 return "map" if self.show_values else "array"