Skip to content

Add rust-lldb pretty printing for Path and PathBuf #120557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/etc/lldb_commands
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ 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::([a-z_]+::)+)NonZero<.+>$" --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::([a-z_]+::)+)PathBuf$" --category Rust
type summary add -F lldb_lookup.summary_lookup -e -x -h "^&(mut )?(std::([a-z_]+::)+)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::([a-z_]+::)+)String$")
Expand All @@ -51,6 +53,8 @@ class RustType(object):
STD_REF_MUT_REGEX = re.compile(r"^(core::([a-z_]+::)+)RefMut<.+>$")
STD_REF_CELL_REGEX = re.compile(r"^(core::([a-z_]+::)+)RefCell<.+>$")
STD_NONZERO_NUMBER_REGEX = re.compile(r"^(core::([a-z_]+::)+)NonZero<.+>$")
STD_PATHBUF_REGEX = re.compile(r"^(std::([a-z_]+::)+)PathBuf$")
STD_PATH_REGEX = re.compile(r"^&(mut )?(std::([a-z_]+::)+)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
29 changes: 29 additions & 0 deletions tests/debuginfo/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ ignore-gdb

//@ compile-flags:-g

// === LLDB TESTS =================================================================================

// lldb-command:run

// lldb-command:print pathbuf
// lldb-check:[...] "/some/path" { inner = "/some/path" { inner = { inner = size=10 { [0] = '/' [1] = 's' [2] = 'o' [3] = 'm' [4] = 'e' [5] = '/' [6] = 'p' [7] = 'a' [8] = 't' [9] = 'h' } } } }
// lldb-command:po pathbuf
// lldb-check:"/some/path"
// lldb-command:print path
// lldb-check:[...] "/some/path" { data_ptr = [...] length = 10 }
// lldb-command:po path
// lldb-check:"/some/path"

use std::path::Path;

fn main() {
let path = Path::new("/some/path");
let pathbuf = path.to_path_buf();

zzz(); // #break
}

fn zzz() {
()
}
Loading