Skip to content

Commit a5fbc20

Browse files
committed
add lldb providers
1 parent dab4d21 commit a5fbc20

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/etc/lldb_commands

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ type synthetic add -l lldb_lookup.synthetic_lookup -x "^(core::([a-z_]+::)+)NonZ
1818
type synthetic add -l lldb_lookup.synthetic_lookup -x "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
1919
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(std::([a-z_]+::)+)PathBuf$" --category Rust
2020
type synthetic add -l lldb_lookup.synthetic_lookup -x "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
21-
type synthetic add -l lldb_lookup.synthetic_lookup -x "^(.*)$" --category Rust
21+
type synthetic add -l lldb_lookup.synthetic_lookup -x "^\(.*\)$" --category Rust
22+
type synthetic add -l lldb_lookup.PtrSyntheticProvider -x "^(const )?.* &$" --category Rust
23+
type synthetic add -l lldb_lookup.PtrSyntheticProvider -x "^(const )?.*&&$" --category Rust
24+
type synthetic add -l lldb_lookup.PtrSyntheticProvider -x "^(const )?.* \*$" --category Rust
2225
type summary add -F _ -e -x -h "^.*$" --category Rust
2326
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(alloc::([a-z_]+::)+)String$" --category Rust
2427
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?str$" --category Rust
@@ -40,4 +43,7 @@ type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)N
4043
type summary add -F lldb_lookup.summary_lookup -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
4144
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(std::([a-z_]+::)+)PathBuf$" --category Rust
4245
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?(std::([a-z_]+::)+)Path$" --category Rust
46+
type summary add -F lldb_lookup.PtrSummaryProvider -e -h -x "^(const )?.* &$" --category Rust
47+
type summary add -F lldb_lookup.PtrSummaryProvider -e -h -x "^(const )?.*&&$" --category Rust
48+
type summary add -F lldb_lookup.PtrSummaryProvider -e -h -x "^(const )?.* \*$" --category Rust
4349
type category enable Rust

src/etc/lldb_providers.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sys
22

33
from lldb import (
4+
SBValue,
45
SBData,
6+
SBType,
57
SBError,
68
eBasicTypeLong,
79
eBasicTypeUnsignedLong,
@@ -912,3 +914,77 @@ def StdNonZeroNumberSummaryProvider(valobj, _dict):
912914
return str(inner_inner.GetValueAsSigned())
913915
else:
914916
return inner_inner.GetValue()
917+
918+
919+
class PtrSyntheticProvider(DefaultSyntheticProvider):
920+
def get_type_name(self) -> str:
921+
type: SBType = self.valobj.GetType()
922+
name_parts: list[str] = []
923+
924+
# "&&" indicates an rval reference. This doesn't technically mean anything in Rust, but the
925+
# debug info is generated as such so we can differentiate between "ref-to-ref" (illegal in
926+
# TypeSystemClang) and "ref-to-pointer".
927+
#
928+
# Whenever there is a "&&", we can be sure that the pointer it is pointing to is actually
929+
# supposed to be a reference. (e.g. u8 *&& -> &mut &mut u8)
930+
was_r_ref: bool = False
931+
ptr_type: SBType = type
932+
ptee_type: SBType = type.GetPointeeType()
933+
934+
while ptr_type.is_pointer or ptr_type.is_reference:
935+
# remove the `const` modifier as it indicates the const-ness of any pointer/ref pointing *to* it
936+
# not its own constness
937+
# For example:
938+
# const u8 *const * -> &&u8
939+
# u8 *const * -> &&mut u8
940+
# const u8 ** -> &mut &u8
941+
# u8 ** -> &mut &mut u8
942+
ptr_name: str = ptr_type.GetName().removesuffix("const")
943+
ptee_name: str = ptee_type.GetName()
944+
945+
is_ref: bool = False
946+
947+
if was_r_ref or ptr_name[-1] == "&":
948+
is_ref = True
949+
950+
was_r_ref = ptr_name[-2:] == "&&"
951+
952+
if ptee_type.is_pointer or ptee_type.is_reference:
953+
if ptee_name.endswith("const"):
954+
if is_ref:
955+
name_parts.append("&")
956+
else:
957+
name_parts.append("*const ")
958+
else:
959+
if is_ref:
960+
name_parts.append("&mut ")
961+
else:
962+
name_parts.append("*mut ")
963+
964+
else:
965+
if ptee_name.startswith("const "):
966+
if is_ref:
967+
name_parts.append("&")
968+
else:
969+
name_parts.append("*const ")
970+
else:
971+
if is_ref:
972+
name_parts.append("&mut ")
973+
else:
974+
name_parts.append("*mut ")
975+
976+
ptr_type = ptee_type
977+
ptee_type = ptee_type.GetPointeeType()
978+
979+
name_parts.append(ptr_type.GetUnqualifiedType().GetName())
980+
return "".join(name_parts)
981+
982+
983+
def PtrSummaryProvider(valobj: SBValue, dict) -> str:
984+
while True:
985+
t: SBType = valobj.GetType()
986+
if t.is_pointer or t.is_reference:
987+
valobj = valobj.Dereference()
988+
else:
989+
break
990+
return valobj.value

0 commit comments

Comments
 (0)