Skip to content

Commit

Permalink
Add rust-lldb pretty printing for Path and PathBuf
Browse files Browse the repository at this point in the history
Fixes #120553
Fixes #48462
  • Loading branch information
n8henrie committed Feb 1, 2024
1 parent 11f32b7 commit 1e77350
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/etc/lldb_commands
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)R
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefMut<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^(core::([a-z_]+::)+)RefCell<.+>$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^core::num::([a-z_]+::)*NonZero.+$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^std::path::PathBuf$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?std::path::Path$" --category Rust
type category enable Rust
5 changes: 5 additions & 0 deletions src/etc/lldb_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def summary_lookup(valobj, dict):
if rust_type == RustType.STD_NONZERO_NUMBER:
return StdNonZeroNumberSummaryProvider(valobj, dict)

if rust_type == RustType.STD_PATHBUF:
return StdPathBufSummaryProvider(valobj, dict)
if rust_type == RustType.STD_PATH:
return StdPathSummaryProvider(valobj, dict)

return ""


Expand Down
29 changes: 29 additions & 0 deletions src/etc/lldb_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ def StdStrSummaryProvider(valobj, dict):
return '"%s"' % data


def StdPathBufSummaryProvider(valobj, dict):
# type: (SBValue, dict) -> str
# logger = Logger.Logger()
# logger >> "[StdPathBufSummaryProvider] for " + str(valobj.GetName())
return StdOsStringSummaryProvider(valobj.GetChildMemberWithName("inner"), dict)


def StdPathSummaryProvider(valobj, dict):
# type: (SBValue, dict) -> str
# logger = Logger.Logger()
# logger >> "[StdPathSummaryProvider] for " + str(valobj.GetName())
length = valobj.GetChildMemberWithName("length").GetValueAsUnsigned()
if length == 0:
return '""'

data_ptr = valobj.GetChildMemberWithName("data_ptr")

start = data_ptr.GetValueAsUnsigned()
error = SBError()
process = data_ptr.GetProcess()
data = process.ReadMemory(start, length, error)
if PY3:
try:
data = data.decode(encoding='UTF-8')
except UnicodeDecodeError:
return '%r' % data
return '"%s"' % data


class StructSyntheticProvider:
"""Pretty-printer for structs and struct enum variants"""

Expand Down
6 changes: 6 additions & 0 deletions src/etc/rust_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class RustType(object):
STD_REF_MUT = "StdRefMut"
STD_REF_CELL = "StdRefCell"
STD_NONZERO_NUMBER = "StdNonZeroNumber"
STD_PATH = "StdPath"
STD_PATHBUF = "StdPathBuf"


STD_STRING_REGEX = re.compile(r"^(alloc::(\w+::)+)String$")
Expand All @@ -51,6 +53,8 @@ class RustType(object):
STD_REF_MUT_REGEX = re.compile(r"^(core::(\w+::)+)RefMut<.+>$")
STD_REF_CELL_REGEX = re.compile(r"^(core::(\w+::)+)RefCell<.+>$")
STD_NONZERO_NUMBER_REGEX = re.compile(r"^core::num::([a-z_]+::)*NonZero.+$")
STD_PATHBUF_REGEX = re.compile(r"^std::path::PathBuf$")
STD_PATH_REGEX = re.compile(r"^&(mut )?std::path::Path$")

TUPLE_ITEM_REGEX = re.compile(r"__\d+$")

Expand All @@ -75,6 +79,8 @@ class RustType(object):
RustType.STD_REF_CELL: STD_REF_CELL_REGEX,
RustType.STD_CELL: STD_CELL_REGEX,
RustType.STD_NONZERO_NUMBER: STD_NONZERO_NUMBER_REGEX,
RustType.STD_PATHBUF: STD_PATHBUF_REGEX,
RustType.STD_PATH: STD_PATH_REGEX,
}

def is_tuple_fields(fields):
Expand Down

0 comments on commit 1e77350

Please sign in to comment.