From 596550c8002c2a1fa375ff7f39b6b9707b9f042d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 2 May 2026 14:49:28 +0200 Subject: debug: add kapi formatter support --- scripts/gdb/kapi/__init__.py | 15 +++++++++++++++ scripts/gdb/kapi/address.py | 33 +++++++++++++++++++++++++++++++++ scripts/gdb/load.py | 16 ++++++++++++++++ scripts/gdb/load_kstd.py | 14 -------------- 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 scripts/gdb/kapi/__init__.py create mode 100644 scripts/gdb/kapi/address.py create mode 100644 scripts/gdb/load.py delete mode 100644 scripts/gdb/load_kstd.py (limited to 'scripts') diff --git a/scripts/gdb/kapi/__init__.py b/scripts/gdb/kapi/__init__.py new file mode 100644 index 0000000..c37c7b7 --- /dev/null +++ b/scripts/gdb/kapi/__init__.py @@ -0,0 +1,15 @@ +import gdb.printing + +from .address import KapiMemoryAddressPrinter + + +def build_pretty_printers(): + pp = gdb.printing.RegexpCollectionPrettyPrinter("kapi") + pp.add_printer( + "kapi_memory_address", "^kapi::memory::address<.*>$", KapiMemoryAddressPrinter + ) + return pp + + +def register_printers(objfile): + gdb.printing.register_pretty_printer(objfile, build_pretty_printers(), replace=True) diff --git a/scripts/gdb/kapi/address.py b/scripts/gdb/kapi/address.py new file mode 100644 index 0000000..677c9aa --- /dev/null +++ b/scripts/gdb/kapi/address.py @@ -0,0 +1,33 @@ +import gdb + + +class KapiMemoryAddressPrinter: + """Print kapi::MemoryAddress.""" + + def __init__(self, val): + self.val = val + self.address_type = val.type.template_argument(0) + + def to_string(self): + try: + raw_address = int(self.val["m_value"]) + type_string = str(self.address_type) + + if "linear" in type_string: + suffix = "%lin" + elif "physical" in type_string: + suffix = "%phy" + else: + suffix = "%???" + + return f"{raw_address:#018x}{suffix}" + except Exception as e: + return f"{self.val}: {e}" + + def children(self): + if "linear" in str(self.address_type): + yield ( + "std::byte *", + self.val["m_value"].cast(gdb.lookup_type("std::byte").pointer()), + ) + yield ("m_value", self.val["m_value"]) diff --git a/scripts/gdb/load.py b/scripts/gdb/load.py new file mode 100644 index 0000000..ec512b7 --- /dev/null +++ b/scripts/gdb/load.py @@ -0,0 +1,16 @@ +import sys +import os +import gdb + +script_dir = os.path.dirname(os.path.abspath(__file__)) + +if script_dir not in sys.path: + sys.path.insert(0, script_dir) + +import kstd +import kapi + +kstd.register_printers(gdb.current_objfile()) +kapi.register_printers(gdb.current_objfile()) + +gdb.write("Loaded TeachOS pretty printers.\n") diff --git a/scripts/gdb/load_kstd.py b/scripts/gdb/load_kstd.py deleted file mode 100644 index f3adce1..0000000 --- a/scripts/gdb/load_kstd.py +++ /dev/null @@ -1,14 +0,0 @@ -import sys -import os -import gdb - -script_dir = os.path.dirname(os.path.abspath(__file__)) - -if script_dir not in sys.path: - sys.path.insert(0, script_dir) - -import kstd - -kstd.register_printers(gdb.current_objfile()) - -gdb.write("Loaded kstd pretty printers.\n") -- cgit v1.2.3