From 61949538ad6d114c1c2a788928a0b9706b4efc76 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 1 May 2026 20:46:21 +0200 Subject: debug: add support for kstd pretty printing --- .gitignore | 2 ++ .vscode/launch.json | 4 ++++ scripts/gdb/kstd/__init__.py | 17 +++++++++++++++++ scripts/gdb/kstd/std_types.py | 15 +++++++++++++++ scripts/gdb/kstd/string.py | 26 ++++++++++++++++++++++++++ scripts/gdb/kstd/vector.py | 22 ++++++++++++++++++++++ scripts/gdb/load_kstd.py | 14 ++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 scripts/gdb/kstd/__init__.py create mode 100644 scripts/gdb/kstd/std_types.py create mode 100644 scripts/gdb/kstd/string.py create mode 100644 scripts/gdb/kstd/vector.py create mode 100644 scripts/gdb/load_kstd.py diff --git a/.gitignore b/.gitignore index e96f481..4295aa6 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ qemu-*-*.log /desktop.ini coverage.info + +__pycache__/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 6478c96..4d15c6f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,6 +25,10 @@ { "description": "Load symbols", "text": "-file-exec-and-symbols ${command:cmake.buildDirectory}/bin/${command:cmake.buildType}/kernel.sym" + }, + { + "description": "Load custom Python helpers", + "text": "-interpreter-exec console \"source ${workspaceFolder}/scripts/gdb/load_kstd.py\"" } ] } diff --git a/scripts/gdb/kstd/__init__.py b/scripts/gdb/kstd/__init__.py new file mode 100644 index 0000000..7dc1596 --- /dev/null +++ b/scripts/gdb/kstd/__init__.py @@ -0,0 +1,17 @@ +import gdb.printing + +from .vector import KstdVectorPrinter +from .string import KstdStringPrinter +from .std_types import StdBytePrinter + + +def build_pretty_printers(): + pp = gdb.printing.RegexpCollectionPrettyPrinter("kstd") + pp.add_printer("vector", "^kstd::vector<.*>$", KstdVectorPrinter) + pp.add_printer("string", "^kstd::string$", KstdStringPrinter) + pp.add_printer("std_byte", "^std::byte$", StdBytePrinter) + return pp + + +def register_printers(objfile): + gdb.printing.register_pretty_printer(objfile, build_pretty_printers(), replace=True) diff --git a/scripts/gdb/kstd/std_types.py b/scripts/gdb/kstd/std_types.py new file mode 100644 index 0000000..78d094c --- /dev/null +++ b/scripts/gdb/kstd/std_types.py @@ -0,0 +1,15 @@ +import gdb + + +class StdBytePrinter: + + def __init__(self, val): + self.val = val + + def to_string(self): + try: + uint8_type = gdb.lookup_type("unsigned char") + numeric_value = int(self.val.cast(uint8_type)) + return f"{numeric_value:#04x}" + except gdb.error: + return f"" diff --git a/scripts/gdb/kstd/string.py b/scripts/gdb/kstd/string.py new file mode 100644 index 0000000..6fa9996 --- /dev/null +++ b/scripts/gdb/kstd/string.py @@ -0,0 +1,26 @@ +import gdb + + +class KstdStringPrinter: + + def __init__(self, val): + self.val = val + + def to_string(self): + storage = self.val["m_storage"] + storage_size = int(storage["m_size"]) + + if storage_size <= 0: + return '""' + + data_pointer = storage["m_data"] + string_length = storage_size - 1 + + try: + string_data = data_pointer.string(encoding="utf-8", length=string_length) + return f"{string_data}" + except gdb.error: + return "" + + def display_hint(self): + return "string" diff --git a/scripts/gdb/kstd/vector.py b/scripts/gdb/kstd/vector.py new file mode 100644 index 0000000..b42aeae --- /dev/null +++ b/scripts/gdb/kstd/vector.py @@ -0,0 +1,22 @@ +import gdb + + +class KstdVectorPrinter: + + def __init__(self, val): + self.val = val + self.type = val.type.template_argument(0) + + def to_string(self): + size = int(self.val(["m_size"])) + capacity = int(self.val(["m_capacity"])) + return f"kstd::vector<{self.type}> of length {size}, capacity {capacity}" + + def children(self): + size = int(self.val["m_size"]) + data_pointer = self.val["m_data"] + for i in range(size): + yield (f"[{i}]", (data_pointer + i).dereference()) + + def display_hint(self): + return "array" diff --git a/scripts/gdb/load_kstd.py b/scripts/gdb/load_kstd.py new file mode 100644 index 0000000..f3adce1 --- /dev/null +++ b/scripts/gdb/load_kstd.py @@ -0,0 +1,14 @@ +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