1 from sys import version_info
5 if version_info[0] >= 3:
12 def unwrap_unique_or_non_null(unique_or_nonnull):
13 # BACKCOMPAT: rust 1.32
14 # https://github.com/rust-lang/rust/commit/7a0911528058e87d22ea305695f4047572c5e067
15 ptr = unique_or_nonnull["pointer"]
16 return ptr if ptr.type.code == gdb.TYPE_CODE_PTR else ptr[ZERO_FIELD]
20 def __init__(self, valobj):
21 content = valobj[valobj.type.fields()[0]]
22 fields = content.type.fields()
23 self.empty = len(fields) == 0
28 discriminant = int(content[fields[0]]) + 1
29 self.active_variant = content[fields[discriminant]]
30 self.name = fields[discriminant].name
31 self.full_name = "{}::{}".format(valobj.type.name, self.name)
33 self.full_name = valobj.type.name
40 yield self.name, self.active_variant
43 class StdStringProvider:
44 def __init__(self, valobj):
47 self.length = int(vec["len"])
48 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
51 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
58 class StdOsStringProvider:
59 def __init__(self, valobj):
61 buf = self.valobj["inner"]["inner"]
62 is_windows = "Wtf8Buf" in buf.type.name
63 vec = buf[ZERO_FIELD] if is_windows else buf
65 self.length = int(vec["len"])
66 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
69 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
71 def display_hint(self):
76 def __init__(self, valobj):
78 self.length = int(valobj["length"])
79 self.data_ptr = valobj["data_ptr"]
82 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
88 def _enumerate_array_elements(element_ptrs):
89 for (i, element_ptr) in enumerate(element_ptrs):
90 key = "[{}]".format(i)
91 element = element_ptr.dereference()
94 # rust-lang/rust#64343: passing deref expr to `str` allows
95 # catching exception on garbage pointer
98 yield key, "inaccessible"
104 class StdSliceProvider:
105 def __init__(self, valobj):
107 self.length = int(valobj["length"])
108 self.data_ptr = valobj["data_ptr"]
111 return "{}(size={})".format(self.valobj.type, self.length)
114 return _enumerate_array_elements(
115 self.data_ptr + index for index in xrange(self.length)
122 class StdVecProvider:
123 def __init__(self, valobj):
125 self.length = int(valobj["len"])
126 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
129 return "Vec(size={})".format(self.length)
132 return _enumerate_array_elements(
133 self.data_ptr + index for index in xrange(self.length)
141 class StdVecDequeProvider:
142 def __init__(self, valobj):
144 self.head = int(valobj["head"])
145 self.tail = int(valobj["tail"])
146 self.cap = int(valobj["buf"]["cap"])
147 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
148 if self.head >= self.tail:
149 self.size = self.head - self.tail
151 self.size = self.cap + self.head - self.tail
154 return "VecDeque(size={})".format(self.size)
157 return _enumerate_array_elements(
158 (self.data_ptr + ((self.tail + index) % self.cap)) for index in xrange(self.size)
167 def __init__(self, valobj, is_atomic=False):
169 self.is_atomic = is_atomic
170 self.ptr = unwrap_unique_or_non_null(valobj["ptr"])
171 self.value = self.ptr["data" if is_atomic else "value"]
172 self.strong = self.ptr["strong"]["v" if is_atomic else "value"]["value"]
173 self.weak = self.ptr["weak"]["v" if is_atomic else "value"]["value"] - 1
177 return "Arc(strong={}, weak={})".format(int(self.strong), int(self.weak))
179 return "Rc(strong={}, weak={})".format(int(self.strong), int(self.weak))
182 yield "value", self.value
183 yield "strong", self.strong
184 yield "weak", self.weak
187 class StdCellProvider:
188 def __init__(self, valobj):
189 self.value = valobj["value"]["value"]
195 yield "value", self.value
198 class StdRefProvider:
199 def __init__(self, valobj):
200 self.value = valobj["value"].dereference()
201 self.borrow = valobj["borrow"]["borrow"]["value"]["value"]
204 borrow = int(self.borrow)
206 return "Ref(borrow={})".format(borrow)
208 return "Ref(borrow_mut={})".format(-borrow)
211 yield "*value", self.value
212 yield "borrow", self.borrow
215 class StdRefCellProvider:
216 def __init__(self, valobj):
217 self.value = valobj["value"]["value"]
218 self.borrow = valobj["borrow"]["value"]["value"]
221 borrow = int(self.borrow)
223 return "RefCell(borrow={})".format(borrow)
225 return "RefCell(borrow_mut={})".format(-borrow)
228 yield "value", self.value
229 yield "borrow", self.borrow
232 # Yields children (in a provider's sense of the word) for a BTreeMap.
233 def children_of_btree_map(map):
234 # Yields each key/value pair in the node and in any child nodes.
235 def children_of_node(node_ptr, height):
236 def cast_to_internal(node):
237 internal_type_name = node.type.target().name.replace("LeafNode", "InternalNode", 1)
238 internal_type = gdb.lookup_type(internal_type_name)
239 return node.cast(internal_type.pointer())
241 if node_ptr.type.name.startswith("alloc::collections::btree::node::BoxedNode<"):
242 # BACKCOMPAT: rust 1.49
243 node_ptr = node_ptr["ptr"]
244 node_ptr = unwrap_unique_or_non_null(node_ptr)
245 leaf = node_ptr.dereference()
248 edges = cast_to_internal(node_ptr)["edges"] if height > 0 else None
251 for i in xrange(0, length + 1):
253 child_ptr = edges[i]["value"]["value"]
254 for child in children_of_node(child_ptr, height - 1):
257 # Avoid "Cannot perform pointer math on incomplete type" on zero-sized arrays.
258 key_type_size = keys.type.sizeof
259 val_type_size = vals.type.sizeof
260 key = keys[i]["value"]["value"] if key_type_size > 0 else gdb.parse_and_eval("()")
261 val = vals[i]["value"]["value"] if val_type_size > 0 else gdb.parse_and_eval("()")
264 if map["length"] > 0:
266 if root.type.name.startswith("core::option::Option<"):
267 root = root.cast(gdb.lookup_type(root.type.name[21:-1]))
268 node_ptr = root["node"]
269 height = root["height"]
270 for child in children_of_node(node_ptr, height):
274 class StdBTreeSetProvider:
275 def __init__(self, valobj):
279 return "BTreeSet(size={})".format(self.valobj["map"]["length"])
282 inner_map = self.valobj["map"]
283 for i, (child, _) in enumerate(children_of_btree_map(inner_map)):
284 yield "[{}]".format(i), child
291 class StdBTreeMapProvider:
292 def __init__(self, valobj):
296 return "BTreeMap(size={})".format(self.valobj["length"])
299 for i, (key, val) in enumerate(children_of_btree_map(self.valobj)):
300 yield "key{}".format(i), key
301 yield "val{}".format(i), val
308 # BACKCOMPAT: rust 1.35
309 class StdOldHashMapProvider:
310 def __init__(self, valobj, show_values=True):
312 self.show_values = show_values
314 self.table = self.valobj["table"]
315 self.size = int(self.table["size"])
316 self.hashes = self.table["hashes"]
317 self.hash_uint_type = self.hashes.type
318 self.hash_uint_size = self.hashes.type.sizeof
319 self.modulo = 2 ** self.hash_uint_size
320 self.data_ptr = self.hashes[ZERO_FIELD]["pointer"]
322 self.capacity_mask = int(self.table["capacity_mask"])
323 self.capacity = (self.capacity_mask + 1) % self.modulo
325 marker = self.table["marker"].type
326 self.pair_type = marker.template_argument(0)
327 self.pair_type_size = self.pair_type.sizeof
329 self.valid_indices = []
330 for idx in range(self.capacity):
331 data_ptr = self.data_ptr.cast(self.hash_uint_type.pointer())
332 address = data_ptr + idx
333 hash_uint = address.dereference()
334 hash_ptr = hash_uint[ZERO_FIELD]["pointer"]
335 if int(hash_ptr) != 0:
336 self.valid_indices.append(idx)
340 return "HashMap(size={})".format(self.size)
342 return "HashSet(size={})".format(self.size)
345 start = int(self.data_ptr) & ~1
347 hashes = self.hash_uint_size * self.capacity
348 align = self.pair_type_size
349 len_rounded_up = (((((hashes + align) % self.modulo - 1) % self.modulo) & ~(
350 (align - 1) % self.modulo)) % self.modulo - hashes) % self.modulo
352 pairs_offset = hashes + len_rounded_up
353 pairs_start = gdb.Value(start + pairs_offset).cast(self.pair_type.pointer())
355 for index in range(self.size):
356 table_index = self.valid_indices[index]
357 idx = table_index & self.capacity_mask
358 element = (pairs_start + idx).dereference()
360 yield "key{}".format(index), element[ZERO_FIELD]
361 yield "val{}".format(index), element[FIRST_FIELD]
363 yield "[{}]".format(index), element[ZERO_FIELD]
365 def display_hint(self):
366 return "map" if self.show_values else "array"
369 class StdHashMapProvider:
370 def __init__(self, valobj, show_values=True):
372 self.show_values = show_values
375 table_inner = table["table"]
376 capacity = int(table_inner["bucket_mask"]) + 1
377 ctrl = table_inner["ctrl"]["pointer"]
379 self.size = int(table_inner["items"])
380 self.pair_type = table.type.template_argument(0).strip_typedefs()
382 self.new_layout = not table_inner.type.has_key("data")
384 self.data_ptr = ctrl.cast(self.pair_type.pointer())
386 self.data_ptr = table_inner["data"]["pointer"]
388 self.valid_indices = []
389 for idx in range(capacity):
391 value = address.dereference()
392 is_presented = value & 128 == 0
394 self.valid_indices.append(idx)
398 hashbrown_hashmap = self.valobj["base"]
399 elif self.valobj.type.fields()[0].name == "map":
400 # BACKCOMPAT: rust 1.47
401 # HashSet wraps std::collections::HashMap, which wraps hashbrown::HashMap
402 hashbrown_hashmap = self.valobj["map"]["base"]
404 # HashSet wraps hashbrown::HashSet, which wraps hashbrown::HashMap
405 hashbrown_hashmap = self.valobj["base"]["map"]
406 return hashbrown_hashmap["table"]
410 return "HashMap(size={})".format(self.size)
412 return "HashSet(size={})".format(self.size)
415 pairs_start = self.data_ptr
417 for index in range(self.size):
418 idx = self.valid_indices[index]
421 element = (pairs_start + idx).dereference()
423 yield "key{}".format(index), element[ZERO_FIELD]
424 yield "val{}".format(index), element[FIRST_FIELD]
426 yield "[{}]".format(index), element[ZERO_FIELD]
428 def display_hint(self):
429 return "map" if self.show_values else "array"