1 from sys import version_info
4 from gdb import lookup_type
6 if version_info[0] >= 3:
13 def 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]
21 def __init__(self, valobj):
22 content = valobj[valobj.type.fields()[0]]
23 fields = content.type.fields()
24 self.empty = len(fields) == 0
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)
34 self.full_name = valobj.type.name
41 yield self.name, self.active_variant
44 class StdStringProvider:
45 def __init__(self, valobj):
48 self.length = int(vec["len"])
49 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
52 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
59 class StdOsStringProvider:
60 def __init__(self, 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
66 self.length = int(vec["len"])
67 self.data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"])
70 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
72 def display_hint(self):
77 def __init__(self, valobj):
79 self.length = int(valobj["length"])
80 self.data_ptr = valobj["data_ptr"]
83 return self.data_ptr.lazy_string(encoding="utf-8", length=self.length)
91 def __init__(self, valobj):
93 self.length = int(valobj["len"])
94 self.data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"])
97 return "Vec(size={})".format(self.length)
100 saw_inaccessible = False
101 for index in xrange(self.length):
102 element_ptr = self.data_ptr + index
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()
111 saw_inaccessible = True
112 yield str(index), "inaccessible"
119 class StdVecDequeProvider:
120 def __init__(self, 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
129 self.size = self.cap + self.head - self.tail
132 return "VecDeque(size={})".format(self.size)
135 for index in xrange(0, self.size):
136 value = (self.data_ptr + ((self.tail + index) % self.cap)).dereference()
137 yield "[{}]".format(index), value
145 def __init__(self, valobj, is_atomic=False):
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
155 return "Arc(strong={}, weak={})".format(int(self.strong), int(self.weak))
157 return "Rc(strong={}, weak={})".format(int(self.strong), int(self.weak))
160 yield "value", self.value
161 yield "strong", self.strong
162 yield "weak", self.weak
165 class StdCellProvider:
166 def __init__(self, valobj):
167 self.value = valobj["value"]["value"]
173 yield "value", self.value
176 class StdRefProvider:
177 def __init__(self, valobj):
178 self.value = valobj["value"].dereference()
179 self.borrow = valobj["borrow"]["borrow"]["value"]["value"]
182 borrow = int(self.borrow)
184 return "Ref(borrow={})".format(borrow)
186 return "Ref(borrow_mut={})".format(-borrow)
189 yield "*value", self.value
190 yield "borrow", self.borrow
193 class StdRefCellProvider:
194 def __init__(self, valobj):
195 self.value = valobj["value"]["value"]
196 self.borrow = valobj["borrow"]["value"]["value"]
199 borrow = int(self.borrow)
201 return "RefCell(borrow={})".format(borrow)
203 return "RefCell(borrow_mut={})".format(-borrow)
206 yield "value", self.value
207 yield "borrow", self.borrow
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.
212 def children_of_node(boxed_node, height):
213 def cast_to_internal(node):
214 internal_type_name = node.type.target().name.replace("LeafNode", "InternalNode", 1)
215 internal_type = lookup_type(internal_type_name)
216 return node.cast(internal_type.pointer())
218 node_ptr = unwrap_unique_or_non_null(boxed_node["ptr"])
219 leaf = node_ptr.dereference()
222 edges = cast_to_internal(node_ptr)["edges"] if height > 0 else None
223 length = int(leaf["len"])
225 for i in xrange(0, length + 1):
227 boxed_child_node = edges[i]["value"]["value"]
228 for child in children_of_node(boxed_child_node, height - 1):
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 None
233 val = vals[i]["value"]["value"] if vals.type.sizeof > 0 else None
237 # Yields children for a BTreeMap.
238 def children_of_map(map):
239 if map["length"] > 0:
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 i, (key, val) in enumerate(children_of_node(boxed_root_node, height)):
247 yield "key{}".format(i), key
249 yield "val{}".format(i), val
252 class StdBTreeSetProvider:
253 def __init__(self, valobj):
257 return "BTreeSet(size={})".format(self.valobj["map"]["length"])
260 inner_map = self.valobj["map"]
261 for child in children_of_map(inner_map):
269 class StdBTreeMapProvider:
270 def __init__(self, valobj):
274 return "BTreeMap(size={})".format(self.valobj["length"])
277 for child in children_of_map(self.valobj):
285 # BACKCOMPAT: rust 1.35
286 class StdOldHashMapProvider:
287 def __init__(self, valobj, show_values=True):
289 self.show_values = show_values
291 self.table = self.valobj["table"]
292 self.size = int(self.table["size"])
293 self.hashes = self.table["hashes"]
294 self.hash_uint_type = self.hashes.type
295 self.hash_uint_size = self.hashes.type.sizeof
296 self.modulo = 2 ** self.hash_uint_size
297 self.data_ptr = self.hashes[ZERO_FIELD]["pointer"]
299 self.capacity_mask = int(self.table["capacity_mask"])
300 self.capacity = (self.capacity_mask + 1) % self.modulo
302 marker = self.table["marker"].type
303 self.pair_type = marker.template_argument(0)
304 self.pair_type_size = self.pair_type.sizeof
306 self.valid_indices = []
307 for idx in range(self.capacity):
308 data_ptr = self.data_ptr.cast(self.hash_uint_type.pointer())
309 address = data_ptr + idx
310 hash_uint = address.dereference()
311 hash_ptr = hash_uint[ZERO_FIELD]["pointer"]
312 if int(hash_ptr) != 0:
313 self.valid_indices.append(idx)
317 return "HashMap(size={})".format(self.size)
319 return "HashSet(size={})".format(self.size)
322 start = int(self.data_ptr) & ~1
324 hashes = self.hash_uint_size * self.capacity
325 align = self.pair_type_size
326 len_rounded_up = (((((hashes + align) % self.modulo - 1) % self.modulo) & ~(
327 (align - 1) % self.modulo)) % self.modulo - hashes) % self.modulo
329 pairs_offset = hashes + len_rounded_up
330 pairs_start = gdb.Value(start + pairs_offset).cast(self.pair_type.pointer())
332 for index in range(self.size):
333 table_index = self.valid_indices[index]
334 idx = table_index & self.capacity_mask
335 element = (pairs_start + idx).dereference()
337 yield "key{}".format(index), element[ZERO_FIELD]
338 yield "val{}".format(index), element[FIRST_FIELD]
340 yield "[{}]".format(index), element[ZERO_FIELD]
342 def display_hint(self):
343 return "map" if self.show_values else "array"
346 class StdHashMapProvider:
347 def __init__(self, valobj, show_values=True):
349 self.show_values = show_values
352 capacity = int(table["bucket_mask"]) + 1
353 ctrl = table["ctrl"]["pointer"]
355 self.size = int(table["items"])
356 self.pair_type = table.type.template_argument(0)
358 self.new_layout = not table.type.has_key("data")
360 self.data_ptr = ctrl.cast(self.pair_type.pointer())
362 self.data_ptr = table["data"]["pointer"]
364 self.valid_indices = []
365 for idx in range(capacity):
367 value = address.dereference()
368 is_presented = value & 128 == 0
370 self.valid_indices.append(idx)
374 hashbrown_hashmap = self.valobj["base"]
375 elif self.valobj.type.fields()[0].name == "map":
376 # BACKCOMPAT: rust 1.47
377 # HashSet wraps std::collections::HashMap, which wraps hashbrown::HashMap
378 hashbrown_hashmap = self.valobj["map"]["base"]
380 # HashSet wraps hashbrown::HashSet, which wraps hashbrown::HashMap
381 hashbrown_hashmap = self.valobj["base"]["map"]
382 return hashbrown_hashmap["table"]
386 return "HashMap(size={})".format(self.size)
388 return "HashSet(size={})".format(self.size)
391 pairs_start = self.data_ptr
393 for index in range(self.size):
394 idx = self.valid_indices[index]
397 element = (pairs_start + idx).dereference()
399 yield "key{}".format(index), element[ZERO_FIELD]
400 yield "val{}".format(index), element[FIRST_FIELD]
402 yield "[{}]".format(index), element[ZERO_FIELD]
404 def display_hint(self):
405 return "map" if self.show_values else "array"