Skip to content
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
44 changes: 11 additions & 33 deletions src/etc/gdb_trim_paths.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GDB Python script to handle Cargo `<exe>.trim-paths.jsonl` files from the trim-paths feature
# GDB Python script to handle Cargo `<exe>.trim-paths.json` files from the trim-paths feature
# - https://github.com/rust-lang/cargo/issues/12137
# - https://github.com/rust-lang/rust/issues/111540

Expand All @@ -9,52 +9,30 @@


# https://doc.guix.gnu.org/gdb/16.3/en/html_node/Source-Path.html#index-set-substitute_002dpath
def _process_v1_trim_paths(lines, trim_paths_path):
for idx, line in enumerate(lines[2:], start=3):
try:
entry = json.loads(line)
if "from" in entry and "to" in entry:
cmd = f'set substitute-path "{entry["from"]}" "{entry["to"]}"'
gdb.execute(cmd)
except json.JSONDecodeError:
print(
f"(rust-gdb) warning: invalid JSON on line {idx} of {trim_paths_path}",
file=sys.stderr,
)
def _process_v1_trim_paths(doc):
for entry in doc.get("remaps", []):
if "from" in entry and "to" in entry:
cmd = f'set substitute-path "{entry["from"]}" "{entry["to"]}"'
gdb.execute(cmd)


def _load_trim_paths(filepath):
trim_paths_path = f"{filepath}.trim-paths.jsonl"
trim_paths_path = f"{filepath}.trim-paths.json"

# FIXME: It might be worth looking into the debuginfod fetch content if the local file
# doesn't exists (maybe with `debuginfod-find debuginfo`).
if not os.path.isfile(trim_paths_path):
return

try:
# Load all the lines of the trim-paths file
with open(trim_paths_path, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f]

# Abort if we have less than 3 lines as that means that we cannot have any
# substitutions (header + metadata is already 2 lines)
if not lines or len(lines) < 3:
return

# Try loading the header line, which contains the version (v) field
try:
header = json.loads(lines[0])
ver = header["v"]
Comment thread
Urgau marked this conversation as resolved.
except json.JSONDecodeError:
print(
f"(rust-gdb) warning: header line 1 of {trim_paths_path} is not valid JSON",
file=sys.stderr,
)
return
doc = json.load(f)

ver = doc.get("v")

# We only handle version 1
if ver == 1:
_process_v1_trim_paths(lines, trim_paths_path)
_process_v1_trim_paths(doc)
else:
print(
f"(rust-gdb) warning: unsupported trim-paths version {ver}: {trim_paths_path}",
Expand Down
46 changes: 12 additions & 34 deletions src/etc/lldb_trim_paths.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LLDB Python script to handle Cargo `<exe>.trim-paths.jsonl` files from the trim-paths feature
# LLDB Python script to handle Cargo `<exe>.trim-paths.json` files from the trim-paths feature
# - https://github.com/rust-lang/cargo/issues/12137
# - https://github.com/rust-lang/rust/issues/111540

Expand All @@ -9,51 +9,29 @@
import threading


def _process_v1_trim_paths(debugger, lines, trim_paths_path):
for idx, line in enumerate(lines[2:], start=3):
try:
entry = json.loads(line)
if "from" in entry and "to" in entry:
# LLDB syntax: settings append target.source-map <from> <to>
cmd = f'settings append target.source-map "{entry["from"]}" "{entry["to"]}"'
debugger.HandleCommand(cmd)
except json.JSONDecodeError:
print(
f"(rust-lldb) warning: invalid JSON on line {idx} of {trim_paths_path}",
file=sys.stderr,
)
def _process_v1_trim_paths(debugger, doc):
for entry in doc.get("remaps", []):
if "from" in entry and "to" in entry:
# LLDB syntax: settings append target.source-map <from> <to>
cmd = f'settings append target.source-map "{entry["from"]}" "{entry["to"]}"'
debugger.HandleCommand(cmd)


def _load_trim_paths(debugger, filepath):
trim_paths_path = f"{filepath}.trim-paths.jsonl"
trim_paths_path = f"{filepath}.trim-paths.json"

if not os.path.isfile(trim_paths_path):
return

try:
# Load all the lines of the trim-paths file
with open(trim_paths_path, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f]

# Abort if we have less than 3 lines as that means that we cannot have any
# substitutions (header + metadata is already 2 lines)
if not lines or len(lines) < 3:
return

# Try loading the header line, which contains the version (v) field
try:
header = json.loads(lines[0])
ver = header.get("v")
except json.JSONDecodeError:
print(
f"(rust-lldb) warning: header line 1 of {trim_paths_path} is not valid JSON",
file=sys.stderr,
)
return
doc = json.load(f)

ver = doc.get("v")

# We only handle version 1
if ver == 1:
_process_v1_trim_paths(debugger, lines, trim_paths_path)
_process_v1_trim_paths(debugger, doc)
else:
print(
f"(rust-lldb) warning: unsupported trim-paths version {ver}: {trim_paths_path}",
Expand Down
Loading