|
1 | 1 | from __future__ import annotations |
2 | | -from typing import Dict |
| 2 | +from typing import List |
| 3 | + |
3 | 4 |
|
4 | 5 | import lldb |
5 | 6 |
|
6 | 7 | from lldb_providers import * |
7 | | -from rust_types import RustType, classify_struct, classify_union |
| 8 | +from rust_types import ( |
| 9 | + ENUM_DISR_FIELD_NAME, |
| 10 | + ENUM_LLDB_ENCODED_VARIANTS, |
| 11 | + RustType, |
| 12 | + classify_union, |
| 13 | + is_tuple_fields, |
| 14 | +) |
| 15 | + |
| 16 | +#################################################################################################### |
| 17 | +# This file contains lookup functions that associate rust types with their synthetic/summary |
| 18 | +# providers. |
| 19 | +# |
| 20 | +# LLDB caches the results of the the commands in `lldb_commands`, but that caching is "shallow". It |
| 21 | +# purely associates the type with the function given, whether it is a regular function or a class |
| 22 | +# constructor. If the function makes decisions about what type of SyntheticProvider to return, that |
| 23 | +# processing is done **each time a value of that type is encountered**. |
| 24 | +# |
| 25 | +# To reiterate, inspecting a `vec![T; 100_000]` will call `T`'s lookup function/constructor 100,000 |
| 26 | +# times. This can lead to significant delays in value visualization if the lookup logic is complex. |
| 27 | +# |
| 28 | +# As such, lookup functions should be kept as minimal as possible. LLDB technically expects a |
| 29 | +# SyntheticProvider class constructor. If you can provide just a class constructor, that should be |
| 30 | +# preferred. If extra processing must be done, try to keep it as minimal and as targeted as possible |
| 31 | +# (see: `classify_hashmap()` vs `classify_hashset()`). |
| 32 | +#################################################################################################### |
8 | 33 |
|
9 | 34 |
|
10 | 35 | # BACKCOMPAT: rust 1.35 |
11 | 36 | def is_hashbrown_hashmap(hash_map: lldb.SBValue) -> bool: |
12 | 37 | return len(hash_map.type.fields) == 1 |
13 | 38 |
|
14 | 39 |
|
15 | | -def classify_rust_type(type: lldb.SBType) -> RustType: |
16 | | - if type.IsPointerType(): |
17 | | - type = type.GetPointeeType() |
18 | | - |
19 | | - type_class = type.GetTypeClass() |
20 | | - if type_class == lldb.eTypeClassStruct: |
21 | | - return classify_struct(type.name, type.fields) |
22 | | - if type_class == lldb.eTypeClassUnion: |
23 | | - return classify_union(type.fields) |
| 40 | +def classify_hashmap(valobj: lldb.SBValue, _dict: LLDBOpaque) -> object: |
| 41 | + if is_hashbrown_hashmap(valobj): |
| 42 | + return StdHashMapSyntheticProvider(valobj, _dict) |
| 43 | + else: |
| 44 | + return StdOldHashMapSyntheticProvider(valobj, _dict) |
24 | 45 |
|
25 | | - return RustType.Other |
26 | 46 |
|
| 47 | +def classify_hashset(valobj: lldb.SBValue, _dict: LLDBOpaque) -> object: |
| 48 | + hash_map = valobj.GetChildAtIndex(0) |
| 49 | + if is_hashbrown_hashmap(hash_map): |
| 50 | + return StdHashMapSyntheticProvider(valobj, _dict, show_values=False) |
| 51 | + else: |
| 52 | + return StdOldHashMapSyntheticProvider(hash_map, _dict, show_values=False) |
27 | 53 |
|
28 | | -def summary_lookup(valobj: lldb.SBValue, _dict: LLDBOpaque) -> str: |
29 | | - """Returns the summary provider for the given value""" |
30 | | - rust_type = classify_rust_type(valobj.GetType()) |
31 | 54 |
|
32 | | - if rust_type == RustType.StdString: |
33 | | - return StdStringSummaryProvider(valobj, _dict) |
34 | | - if rust_type == RustType.StdOsString: |
35 | | - return StdOsStringSummaryProvider(valobj, _dict) |
36 | | - if rust_type == RustType.StdStr: |
37 | | - return StdStrSummaryProvider(valobj, _dict) |
| 55 | +def arc_synthetic(valobj: lldb.SBValue, _dict: LLDBOpaque) -> object: |
| 56 | + return StdRcSyntheticProvider(valobj, _dict, is_atomic=True) |
38 | 57 |
|
39 | | - if rust_type == RustType.StdVec: |
40 | | - return SizeSummaryProvider(valobj, _dict) |
41 | | - if rust_type == RustType.StdVecDeque: |
42 | | - return SizeSummaryProvider(valobj, _dict) |
43 | | - if rust_type == RustType.StdSlice: |
44 | | - return SizeSummaryProvider(valobj, _dict) |
45 | 58 |
|
46 | | - if rust_type == RustType.StdHashMap: |
47 | | - return SizeSummaryProvider(valobj, _dict) |
48 | | - if rust_type == RustType.StdHashSet: |
49 | | - return SizeSummaryProvider(valobj, _dict) |
| 59 | +def classify_rust_type(type: lldb.SBType, is_msvc: bool) -> RustType: |
| 60 | + if type.IsPointerType(): |
| 61 | + return RustType.Indirection |
50 | 62 |
|
51 | | - if rust_type == RustType.StdRc: |
52 | | - return StdRcSummaryProvider(valobj, _dict) |
53 | | - if rust_type == RustType.StdArc: |
54 | | - return StdRcSummaryProvider(valobj, _dict) |
| 63 | + # there is a bit of code duplication here because we don't want to check all of the standard |
| 64 | + # library regexes since LLDB handles that for us |
| 65 | + type_class = type.GetTypeClass() |
| 66 | + if type_class == lldb.eTypeClassStruct: |
| 67 | + fields: List[lldb.SBTypeMember] = type.fields |
| 68 | + if len(fields) == 0: |
| 69 | + return RustType.Empty |
55 | 70 |
|
56 | | - if rust_type == RustType.StdRef: |
57 | | - return StdRefSummaryProvider(valobj, _dict) |
58 | | - if rust_type == RustType.StdRefMut: |
59 | | - return StdRefSummaryProvider(valobj, _dict) |
60 | | - if rust_type == RustType.StdRefCell: |
61 | | - return StdRefSummaryProvider(valobj, _dict) |
| 71 | + # <<variant>> is emitted by GDB while LLDB(18.1+) emits "$variants$" |
| 72 | + if ( |
| 73 | + fields[0].name == ENUM_DISR_FIELD_NAME |
| 74 | + or fields[0].name == ENUM_LLDB_ENCODED_VARIANTS |
| 75 | + ): |
| 76 | + return RustType.Enum |
62 | 77 |
|
63 | | - if rust_type == RustType.StdNonZeroNumber: |
64 | | - return StdNonZeroNumberSummaryProvider(valobj, _dict) |
| 78 | + if is_tuple_fields(fields): |
| 79 | + return RustType.Tuple |
65 | 80 |
|
66 | | - if rust_type == RustType.StdPathBuf: |
67 | | - return StdPathBufSummaryProvider(valobj, _dict) |
68 | | - if rust_type == RustType.StdPath: |
69 | | - return StdPathSummaryProvider(valobj, _dict) |
| 81 | + return RustType.Struct |
| 82 | + if type_class == lldb.eTypeClassUnion: |
| 83 | + # If we're debugging msvc, sum-type enums should have been caught by the regex in lldb |
| 84 | + # commands since they all start with "enum2$<" |
| 85 | + if is_msvc: |
| 86 | + return RustType.Union |
| 87 | + return classify_union(type.fields) |
70 | 88 |
|
71 | | - return "" |
| 89 | + return RustType.Other |
72 | 90 |
|
73 | 91 |
|
74 | 92 | def synthetic_lookup(valobj: lldb.SBValue, _dict: LLDBOpaque) -> object: |
75 | 93 | """Returns the synthetic provider for the given value""" |
76 | | - rust_type = classify_rust_type(valobj.GetType()) |
| 94 | + is_msvc = valobj.GetTarget().GetTriple().endswith("msvc") |
| 95 | + rust_type = classify_rust_type(valobj.GetType(), is_msvc) |
77 | 96 |
|
78 | | - if rust_type == RustType.Struct: |
| 97 | + if rust_type == RustType.Struct or rust_type == RustType.Union: |
79 | 98 | return StructSyntheticProvider(valobj, _dict) |
80 | 99 | if rust_type == RustType.StructVariant: |
81 | 100 | return StructSyntheticProvider(valobj, _dict, is_variant=True) |
@@ -112,37 +131,7 @@ def synthetic_lookup(valobj: lldb.SBValue, _dict: LLDBOpaque) -> object: |
112 | 131 | ) |
113 | 132 |
|
114 | 133 | return ClangEncodedEnumProvider(valobj, _dict) |
115 | | - if rust_type == RustType.StdVec: |
116 | | - return StdVecSyntheticProvider(valobj, _dict) |
117 | | - if rust_type == RustType.StdVecDeque: |
118 | | - return StdVecDequeSyntheticProvider(valobj, _dict) |
119 | | - if rust_type == RustType.StdSlice or rust_type == RustType.StdStr: |
120 | | - return StdSliceSyntheticProvider(valobj, _dict) |
121 | | - |
122 | | - if rust_type == RustType.StdHashMap: |
123 | | - if is_hashbrown_hashmap(valobj): |
124 | | - return StdHashMapSyntheticProvider(valobj, _dict) |
125 | | - else: |
126 | | - return StdOldHashMapSyntheticProvider(valobj, _dict) |
127 | | - if rust_type == RustType.StdHashSet: |
128 | | - hash_map = valobj.GetChildAtIndex(0) |
129 | | - if is_hashbrown_hashmap(hash_map): |
130 | | - return StdHashMapSyntheticProvider(valobj, _dict, show_values=False) |
131 | | - else: |
132 | | - return StdOldHashMapSyntheticProvider(hash_map, _dict, show_values=False) |
133 | | - |
134 | | - if rust_type == RustType.StdRc: |
135 | | - return StdRcSyntheticProvider(valobj, _dict) |
136 | | - if rust_type == RustType.StdArc: |
137 | | - return StdRcSyntheticProvider(valobj, _dict, is_atomic=True) |
138 | | - |
139 | | - if rust_type == RustType.StdCell: |
140 | | - return StdCellSyntheticProvider(valobj, _dict) |
141 | | - if rust_type == RustType.StdRef: |
142 | | - return StdRefSyntheticProvider(valobj, _dict) |
143 | | - if rust_type == RustType.StdRefMut: |
144 | | - return StdRefSyntheticProvider(valobj, _dict) |
145 | | - if rust_type == RustType.StdRefCell: |
146 | | - return StdRefSyntheticProvider(valobj, _dict, is_cell=True) |
| 134 | + if rust_type == RustType.Indirection: |
| 135 | + return IndirectionSyntheticProvider(valobj, _dict) |
147 | 136 |
|
148 | 137 | return DefaultSyntheticProvider(valobj, _dict) |
0 commit comments