]> git.lizzy.rs Git - rust.git/blob - src/etc/gdb_providers.py
Rollup merge of #77829 - gburgessiv:unused-features-var, r=alexcrichton
[rust.git] / src / etc / gdb_providers.py
1 from sys import version_info
2
3 import gdb
4 from gdb import lookup_type
5
6 if version_info[0] >= 3:
7     xrange = range
8
9 ZERO_FIELD = "__0"
10 FIRST_FIELD = "__1"
11
12
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]
18
19
20 class 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
44 class 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
59 class 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
76 class 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
90 class 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
119 class 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
144 class 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
165 class 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
176 class 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
193 class 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
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())
217
218     node_ptr = unwrap_unique_or_non_null(boxed_node["ptr"])
219     leaf = node_ptr.dereference()
220     keys = leaf["keys"]
221     vals = leaf["vals"]
222     edges = cast_to_internal(node_ptr)["edges"] if height > 0 else None
223     length = int(leaf["len"])
224
225     for i in xrange(0, length + 1):
226         if height > 0:
227             boxed_child_node = edges[i]["value"]["value"]
228             for child in children_of_node(boxed_child_node, height - 1):
229                 yield child
230         if i < length:
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
234             yield key, val
235
236
237 # Yields children for a BTreeMap.
238 def 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 i, (key, val) in enumerate(children_of_node(boxed_root_node, height)):
246             if key is not None:
247                 yield "key{}".format(i), key
248             if val is not None:
249                 yield "val{}".format(i), val
250
251
252 class StdBTreeSetProvider:
253     def __init__(self, valobj):
254         self.valobj = valobj
255
256     def to_string(self):
257         return "BTreeSet(size={})".format(self.valobj["map"]["length"])
258
259     def children(self):
260         inner_map = self.valobj["map"]
261         for child in children_of_map(inner_map):
262             yield child
263
264     @staticmethod
265     def display_hint():
266         return "array"
267
268
269 class StdBTreeMapProvider:
270     def __init__(self, valobj):
271         self.valobj = valobj
272
273     def to_string(self):
274         return "BTreeMap(size={})".format(self.valobj["length"])
275
276     def children(self):
277         for child in children_of_map(self.valobj):
278             yield child
279
280     @staticmethod
281     def display_hint():
282         return "map"
283
284
285 # BACKCOMPAT: rust 1.35
286 class StdOldHashMapProvider:
287     def __init__(self, valobj, show_values=True):
288         self.valobj = valobj
289         self.show_values = show_values
290
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"]
298
299         self.capacity_mask = int(self.table["capacity_mask"])
300         self.capacity = (self.capacity_mask + 1) % self.modulo
301
302         marker = self.table["marker"].type
303         self.pair_type = marker.template_argument(0)
304         self.pair_type_size = self.pair_type.sizeof
305
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)
314
315     def to_string(self):
316         if self.show_values:
317             return "HashMap(size={})".format(self.size)
318         else:
319             return "HashSet(size={})".format(self.size)
320
321     def children(self):
322         start = int(self.data_ptr) & ~1
323
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
328
329         pairs_offset = hashes + len_rounded_up
330         pairs_start = gdb.Value(start + pairs_offset).cast(self.pair_type.pointer())
331
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()
336             if self.show_values:
337                 yield "key{}".format(index), element[ZERO_FIELD]
338                 yield "val{}".format(index), element[FIRST_FIELD]
339             else:
340                 yield "[{}]".format(index), element[ZERO_FIELD]
341
342     def display_hint(self):
343         return "map" if self.show_values else "array"
344
345
346 class StdHashMapProvider:
347     def __init__(self, valobj, show_values=True):
348         self.valobj = valobj
349         self.show_values = show_values
350
351         table = self.table()
352         capacity = int(table["bucket_mask"]) + 1
353         ctrl = table["ctrl"]["pointer"]
354
355         self.size = int(table["items"])
356         self.pair_type = table.type.template_argument(0)
357
358         self.new_layout = not table.type.has_key("data")
359         if self.new_layout:
360             self.data_ptr = ctrl.cast(self.pair_type.pointer())
361         else:
362             self.data_ptr = table["data"]["pointer"]
363
364         self.valid_indices = []
365         for idx in range(capacity):
366             address = ctrl + idx
367             value = address.dereference()
368             is_presented = value & 128 == 0
369             if is_presented:
370                 self.valid_indices.append(idx)
371
372     def table(self):
373         if self.show_values:
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"]
379         else:
380             # HashSet wraps hashbrown::HashSet, which wraps hashbrown::HashMap
381             hashbrown_hashmap = self.valobj["base"]["map"]
382         return hashbrown_hashmap["table"]
383
384     def to_string(self):
385         if self.show_values:
386             return "HashMap(size={})".format(self.size)
387         else:
388             return "HashSet(size={})".format(self.size)
389
390     def children(self):
391         pairs_start = self.data_ptr
392
393         for index in range(self.size):
394             idx = self.valid_indices[index]
395             if self.new_layout:
396                 idx = -(idx + 1)
397             element = (pairs_start + idx).dereference()
398             if self.show_values:
399                 yield "key{}".format(index), element[ZERO_FIELD]
400                 yield "val{}".format(index), element[FIRST_FIELD]
401             else:
402                 yield "[{}]".format(index), element[ZERO_FIELD]
403
404     def display_hint(self):
405         return "map" if self.show_values else "array"