aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--.vscode/launch.json4
-rw-r--r--scripts/gdb/kstd/__init__.py17
-rw-r--r--scripts/gdb/kstd/std_types.py15
-rw-r--r--scripts/gdb/kstd/string.py26
-rw-r--r--scripts/gdb/kstd/vector.py22
-rw-r--r--scripts/gdb/load_kstd.py14
7 files changed, 100 insertions, 0 deletions
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"<optimized out or unreadable>"
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 "<unreadable memory>"
+
+ 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")