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 # BACKCOMPAT: rust 1.60
16 # https://github.com/rust-lang/rust/commit/2a91eeac1a2d27dd3de1bf55515d765da20fd86f
17 ptr = unique_or_nonnull["pointer"]
18 return ptr if ptr.type.code == gdb.TYPE_CODE_PTR else ptr[ptr.type.fields()[0]]
22 def __init__(self, valobj):
23 content = valobj[valobj.type.fields()[0]]
24 fields = content.type.fields()
25 self.empty = len(fields) == 0
30 discriminant = int(content[fields[0]]) + 1
31 self.active_variant = content[fields[discriminant]]
32 self.name = fields[discriminant].name
33 self.full_name = "{}::{}".format(valobj.type.name, self.name)
35 self.full_name = valobj.type.name
42 yield self.name, self.active_variant
45 class StdStringProvider:
46 def __init__(self, valobj):
49 self.length = int(vec["len"])
50 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
53 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
60 class StdOsStringProvider:
61 def __init__(self, valobj):
63 buf = self.valobj["inner"]["inner"]
64 is_windows = "Wtf8Buf" in buf.type.name
65 vec = buf[ZERO_FIELD] if is_windows else buf
67 self.length = int(vec["len"])
68 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
71 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
73 def display_hint(self):
78 def __init__(self, valobj):
80 self.length = int(valobj["length"])
81 self.data_ptr = valobj["data_ptr"]
84 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
90 def _enumerate_array_elements(element_ptrs):
91 for (i, element_ptr) in enumerate(element_ptrs):
92 key = "[{}]".format(i)
93 element = element_ptr.dereference()
96 # rust-lang/rust#64343: passing deref expr to `str` allows
97 # catching exception on garbage pointer
100 yield key, "inaccessible"
106 class StdSliceProvider:
107 def __init__(self, valobj):
109 self.length = int(valobj["length"])
110 self.data_ptr = valobj["data_ptr"]
113 return "{}(size={})".format(self.valobj.type, self.length)
116 return _enumerate_array_elements(
117 self.data_ptr + index for index in xrange(self.length)
124 class StdVecProvider:
125 def __init__(self, valobj):
127 self.length = int(valobj["len"])
128 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
131 return "Vec(size={})".format(self.length)
134 return _enumerate_array_elements(
135 self.data_ptr + index for index in xrange(self.length)
143 class StdVecDequeProvider:
144 def __init__(self, valobj):
146 self.head = int(valobj["head"])
147 self.tail = int(valobj["tail"])
148 self.cap = int(valobj["buf"]["cap"])
149 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
150 if self.head >= self.tail:
151 self.size = self.head - self.tail
153 self.size = self.cap + self.head - self.tail
156 return "VecDeque(size={})".format(self.size)
159 return _enumerate_array_elements(
160 (self.data_ptr + ((self.tail + index) % self.cap)) for index in xrange(self.size)
169 def __init__(self, valobj, is_atomic=False):
171 self.is_atomic = is_atomic
172 self.ptr = unwrap_unique_or_non_null(valobj["ptr"])
173 self.value = self.ptr["data" if is_atomic else "value"]
174 self.strong = self.ptr["strong"]["v" if is_atomic else "value"]["value"]
175 self.weak = self.ptr["weak"]["v" if is_atomic else "value"]["value"] - 1
179 return "Arc(strong={}, weak={})".format(int(self.strong), int(self.weak))
181 return "Rc(strong={}, weak={})".format(int(self.strong), int(self.weak))
184 yield "value", self.value
185 yield "strong", self.strong
186 yield "weak", self.weak
189 class StdCellProvider:
190 def __init__(self, valobj):
191 self.value = valobj["value"]["value"]
197 yield "value", self.value
200 class StdRefProvider:
201 def __init__(self, valobj):
202 self.value = valobj["value"].dereference()
203 self.borrow = valobj["borrow"]["borrow"]["value"]["value"]
206 borrow = int(self.borrow)
208 return "Ref(borrow={})".format(borrow)
210 return "Ref(borrow_mut={})".format(-borrow)
213 yield "*value", self.value
214 yield "borrow", self.borrow
217 class StdRefCellProvider:
218 def __init__(self, valobj):
219 self.value = valobj["value"]["value"]
220 self.borrow = valobj["borrow"]["value"]["value"]
223 borrow = int(self.borrow)
225 return "RefCell(borrow={})".format(borrow)
227 return "RefCell(borrow_mut={})".format(-borrow)
230 yield "value", self.value
231 yield "borrow", self.borrow
234 # Yields children (in a provider's sense of the word) for a BTreeMap.
235 def children_of_btree_map(map):
236 # Yields each key/value pair in the node and in any child nodes.
237 def children_of_node(node_ptr, height):
238 def cast_to_internal(node):
239 internal_type_name = node.type.target().name.replace("LeafNode", "InternalNode", 1)
240 internal_type = gdb.lookup_type(internal_type_name)
241 return node.cast(internal_type.pointer())
243 if node_ptr.type.name.startswith("alloc::collections::btree::node::BoxedNode<"):
244 # BACKCOMPAT: rust 1.49
245 node_ptr = node_ptr["ptr"]
246 node_ptr = unwrap_unique_or_non_null(node_ptr)
247 leaf = node_ptr.dereference()
250 edges = cast_to_internal(node_ptr)["edges"] if height > 0 else None
253 for i in xrange(0, length + 1):
255 child_ptr = edges[i]["value"]["value"]
256 for child in children_of_node(child_ptr, height - 1):
259 # Avoid "Cannot perform pointer math on incomplete type" on zero-sized arrays.
260 key_type_size = keys.type.sizeof
261 val_type_size = vals.type.sizeof
262 key = keys[i]["value"]["value"] if key_type_size > 0 else gdb.parse_and_eval("()")
263 val = vals[i]["value"]["value"] if val_type_size > 0 else gdb.parse_and_eval("()")
266 if map["length"] > 0:
268 if root.type.name.startswith("core::option::Option<"):
269 root = root.cast(gdb.lookup_type(root.type.name[21:-1]))
270 node_ptr = root["node"]
271 height = root["height"]
272 for child in children_of_node(node_ptr, height):
276 class StdBTreeSetProvider:
277 def __init__(self, valobj):
281 return "BTreeSet(size={})".format(self.valobj["map"]["length"])
284 inner_map = self.valobj["map"]
285 for i, (child, _) in enumerate(children_of_btree_map(inner_map)):
286 yield "[{}]".format(i), child
293 class StdBTreeMapProvider:
294 def __init__(self, valobj):
298 return "BTreeMap(size={})".format(self.valobj["length"])
301 for i, (key, val) in enumerate(children_of_btree_map(self.valobj)):
302 yield "key{}".format(i), key
303 yield "val{}".format(i), val
310 # BACKCOMPAT: rust 1.35
311 class StdOldHashMapProvider:
312 def __init__(self, valobj, show_values=True):
314 self.show_values = show_values
316 self.table = self.valobj["table"]
317 self.size = int(self.table["size"])
318 self.hashes = self.table["hashes"]
319 self.hash_uint_type = self.hashes.type
320 self.hash_uint_size = self.hashes.type.sizeof
321 self.modulo = 2 ** self.hash_uint_size
322 self.data_ptr = self.hashes[ZERO_FIELD]["pointer"]
324 self.capacity_mask = int(self.table["capacity_mask"])
325 self.capacity = (self.capacity_mask + 1) % self.modulo
327 marker = self.table["marker"].type
328 self.pair_type = marker.template_argument(0)
329 self.pair_type_size = self.pair_type.sizeof
331 self.valid_indices = []
332 for idx in range(self.capacity):
333 data_ptr = self.data_ptr.cast(self.hash_uint_type.pointer())
334 address = data_ptr + idx
335 hash_uint = address.dereference()
336 hash_ptr = hash_uint[ZERO_FIELD]["pointer"]
337 if int(hash_ptr) != 0:
338 self.valid_indices.append(idx)
342 return "HashMap(size={})".format(self.size)
344 return "HashSet(size={})".format(self.size)
347 start = int(self.data_ptr) & ~1
349 hashes = self.hash_uint_size * self.capacity
350 align = self.pair_type_size
351 len_rounded_up = (((((hashes + align) % self.modulo - 1) % self.modulo) & ~(
352 (align - 1) % self.modulo)) % self.modulo - hashes) % self.modulo
354 pairs_offset = hashes + len_rounded_up
355 pairs_start = gdb.Value(start + pairs_offset).cast(self.pair_type.pointer())
357 for index in range(self.size):
358 table_index = self.valid_indices[index]
359 idx = table_index & self.capacity_mask
360 element = (pairs_start + idx).dereference()
362 yield "key{}".format(index), element[ZERO_FIELD]
363 yield "val{}".format(index), element[FIRST_FIELD]
365 yield "[{}]".format(index), element[ZERO_FIELD]
367 def display_hint(self):
368 return "map" if self.show_values else "array"
371 class StdHashMapProvider:
372 def __init__(self, valobj, show_values=True):
374 self.show_values = show_values
377 table_inner = table["table"]
378 capacity = int(table_inner["bucket_mask"]) + 1
379 ctrl = table_inner["ctrl"]["pointer"]
381 self.size = int(table_inner["items"])
382 self.pair_type = table.type.template_argument(0).strip_typedefs()
384 self.new_layout = not table_inner.type.has_key("data")
386 self.data_ptr = ctrl.cast(self.pair_type.pointer())
388 self.data_ptr = table_inner["data"]["pointer"]
390 self.valid_indices = []
391 for idx in range(capacity):
393 value = address.dereference()
394 is_presented = value & 128 == 0
396 self.valid_indices.append(idx)
400 hashbrown_hashmap = self.valobj["base"]
401 elif self.valobj.type.fields()[0].name == "map":
402 # BACKCOMPAT: rust 1.47
403 # HashSet wraps std::collections::HashMap, which wraps hashbrown::HashMap
404 hashbrown_hashmap = self.valobj["map"]["base"]
406 # HashSet wraps hashbrown::HashSet, which wraps hashbrown::HashMap
407 hashbrown_hashmap = self.valobj["base"]["map"]
408 return hashbrown_hashmap["table"]
412 return "HashMap(size={})".format(self.size)
414 return "HashSet(size={})".format(self.size)
417 pairs_start = self.data_ptr
419 for index in range(self.size):
420 idx = self.valid_indices[index]
423 element = (pairs_start + idx).dereference()
425 yield "key{}".format(index), element[ZERO_FIELD]
426 yield "val{}".format(index), element[FIRST_FIELD]
428 yield "[{}]".format(index), element[ZERO_FIELD]
430 def display_hint(self):
431 return "map" if self.show_values else "array"