Files
Swiftgram/scripts/lldb_common.py
2026-02-24 10:57:32 +01:00

47 lines
1.3 KiB
Python

"""Common utilities for LLDB scripts in the VSCode/Cursor debugging workflow."""
import json
import os
import pathlib
import sys
from typing import Any
import lldb
# These files are generated by the lldb_launch_and_debug.sh script.
LAUNCH_INFO_PATH = pathlib.Path(".bsp/skbsp_generated/lldb.json")
BAZEL_INFO_PATH = pathlib.Path(".bsp/skbsp_generated/bazel_info.json")
def _load_file(path: pathlib.Path) -> str:
if not path.exists():
print(f"File not found: {path}. Exiting.", file=sys.stderr, flush=True)
lldb.SBDebugger.Terminate()
os._exit(0)
return path.read_text()
def load_launch_info() -> dict[str, Any]:
return json.loads(_load_file(LAUNCH_INFO_PATH))
def load_bazel_info() -> dict[str, Any]:
return json.loads(_load_file(BAZEL_INFO_PATH))
def run_command(command: str) -> None:
print(f"(lldb) {command}", flush=True)
result = lldb.SBCommandReturnObject()
lldb.debugger.GetCommandInterpreter().HandleCommand(command, result)
if result.Succeeded():
output = result.GetOutput()
if output:
print(output, flush=True)
else:
output = result.GetError()
if output:
print(output, file=sys.stderr, flush=True)
msg = "Command failed. See above for any error messages."
raise RuntimeError(msg)