From 0ea43527332b7e5f1cfec6007506aa54e8f628cb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 7 May 2026 09:12:05 +0000 Subject: debug: enable libstdc++ helpers --- .vscode/launch.json | 8 ++++++-- libs/kstd/gdb/__init__.py | 2 -- libs/kstd/gdb/std_types.py | 13 ------------- scripts/gdb/load.py | 47 ---------------------------------------------- scripts/gdb/teachos.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/gdb/toolchain.py | 35 ++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 64 deletions(-) delete mode 100644 libs/kstd/gdb/std_types.py delete mode 100644 scripts/gdb/load.py create mode 100644 scripts/gdb/teachos.py create mode 100644 scripts/gdb/toolchain.py diff --git a/.vscode/launch.json b/.vscode/launch.json index d5b2401..90c7520 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -27,8 +27,12 @@ "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.py\"" + "description": "Load teachos python helpers", + "text": "source ${workspaceFolder}/scripts/gdb/teachos.py" + }, + { + "description": "Load toolchain python helpers", + "text": "source ${workspaceFolder}/scripts/gdb/toolchain.py" } ] } diff --git a/libs/kstd/gdb/__init__.py b/libs/kstd/gdb/__init__.py index fc5e8fb..2d61539 100644 --- a/libs/kstd/gdb/__init__.py +++ b/libs/kstd/gdb/__init__.py @@ -2,7 +2,6 @@ import gdb.printing from .vector import KstdVectorPrinter from .string import KstdStringPrinter -from .std_types import StdBytePrinter from .smart_pointers import KstdUniquePtrPrinter, KstdSharedPtrPrinter @@ -10,7 +9,6 @@ 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) pp.add_printer("unique_ptr", "^kstd::unique_ptr<.*>$", KstdUniquePtrPrinter) pp.add_printer("shared_ptr", "^kstd::shared_ptr<.*>$", KstdSharedPtrPrinter) return pp diff --git a/libs/kstd/gdb/std_types.py b/libs/kstd/gdb/std_types.py deleted file mode 100644 index deb5c58..0000000 --- a/libs/kstd/gdb/std_types.py +++ /dev/null @@ -1,13 +0,0 @@ -import gdb -from teachos import TeachOSBasePrinter - - -class StdBytePrinter(TeachOSBasePrinter): - - def to_string(self): - try: - uint8_type = gdb.lookup_type("unsigned char") - numeric_value = int(self.value.cast(uint8_type)) - return f"{numeric_value:#04x}" - except gdb.error: - return f"" diff --git a/scripts/gdb/load.py b/scripts/gdb/load.py deleted file mode 100644 index 355f6b9..0000000 --- a/scripts/gdb/load.py +++ /dev/null @@ -1,47 +0,0 @@ -import sys -import os -import gdb -import importlib.util - -script_path = os.path.abspath(__file__) -script_root = os.path.dirname(script_path) -repo_root = os.path.dirname(os.path.dirname(script_root)) - -if script_root not in sys.path: - sys.path.insert(0, script_root) - -from teachos.dump_mb2i import DumpMB2I - -components = { - "kstd": "libs/kstd/gdb", - "kapi": "kapi/gdb", -} - -for component, path in components.items(): - full_path = os.path.join(repo_root, *path.split("/")) - init_file = os.path.join(full_path, "__init__.py") - - if os.path.isfile(init_file): - try: - spec = importlib.util.spec_from_file_location(component, init_file) - module = importlib.util.module_from_spec(spec) - sys.modules[component] = module - spec.loader.exec_module(module) - - if hasattr(module, "register_printers"): - module.register_printers(gdb.current_objfile()) - gdb.write(f"Info: Registered pretty printers for '{component}'.\n") - else: - gdb.write( - f"Warning: '{component}' does not have 'register_printers' function\n" - ) - except Exception as e: - gdb.write(f"Warning: Failed to load '{component}' pretty printers: {e}\n") - else: - gdb.write(f"Warning: GDB extension init not found: '{init_file}'\n") - -gdb.write("Info: Loaded TeachOS pretty printers.\n") - -DumpMB2I() - -gdb.write("Info: Loaded TeachOS tools.\n") \ No newline at end of file diff --git a/scripts/gdb/teachos.py b/scripts/gdb/teachos.py new file mode 100644 index 0000000..355f6b9 --- /dev/null +++ b/scripts/gdb/teachos.py @@ -0,0 +1,47 @@ +import sys +import os +import gdb +import importlib.util + +script_path = os.path.abspath(__file__) +script_root = os.path.dirname(script_path) +repo_root = os.path.dirname(os.path.dirname(script_root)) + +if script_root not in sys.path: + sys.path.insert(0, script_root) + +from teachos.dump_mb2i import DumpMB2I + +components = { + "kstd": "libs/kstd/gdb", + "kapi": "kapi/gdb", +} + +for component, path in components.items(): + full_path = os.path.join(repo_root, *path.split("/")) + init_file = os.path.join(full_path, "__init__.py") + + if os.path.isfile(init_file): + try: + spec = importlib.util.spec_from_file_location(component, init_file) + module = importlib.util.module_from_spec(spec) + sys.modules[component] = module + spec.loader.exec_module(module) + + if hasattr(module, "register_printers"): + module.register_printers(gdb.current_objfile()) + gdb.write(f"Info: Registered pretty printers for '{component}'.\n") + else: + gdb.write( + f"Warning: '{component}' does not have 'register_printers' function\n" + ) + except Exception as e: + gdb.write(f"Warning: Failed to load '{component}' pretty printers: {e}\n") + else: + gdb.write(f"Warning: GDB extension init not found: '{init_file}'\n") + +gdb.write("Info: Loaded TeachOS pretty printers.\n") + +DumpMB2I() + +gdb.write("Info: Loaded TeachOS tools.\n") \ No newline at end of file diff --git a/scripts/gdb/toolchain.py b/scripts/gdb/toolchain.py new file mode 100644 index 0000000..bbb7810 --- /dev/null +++ b/scripts/gdb/toolchain.py @@ -0,0 +1,35 @@ +import os +import subprocess +import sys + + +def setup_toolchain_debugging(): + try: + gcc_path = ( + subprocess.check_output(["which", "x86_64-pc-elf-g++"]) + .decode("utf-8") + .strip() + ) + python_dir = None + + share_path = os.path.join(os.path.dirname(os.path.dirname(gcc_path)), "share") + for root, dirs, _ in os.walk(share_path): + if "libstdcxx" in dirs: + python_dir = root + break + + if python_dir: + sys.path.insert(0, python_dir) + + from libstdcxx.v6.printers import register_libstdcxx_printers + from libstdcxx.v6.xmethods import register_libstdcxx_xmethods + + register_libstdcxx_printers(None) + register_libstdcxx_xmethods(None) + + print(f"Loaded Printers & Xmethods from: {python_dir}") + except Exception as e: + print(f"Debug setup failed: {e}") + + +setup_toolchain_debugging() -- cgit v1.2.3