From 4d06397fbc99a8b4e23979bc7f4cb5c4019fed86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20DUBOIN?= Date: Wed, 8 May 2024 16:19:28 +0200 Subject: [PATCH] chore(libalgo: avl): add GDB pretty printer for avl_t This is a nice and easy to have feature to help us eventual issues with our AVL implementation. It should not be useful anymore once we've debugged our implementation, but I'll leave it anyway just in case. --- .gdbinit | 1 + scripts/debug.sh | 1 + scripts/gdb/pretty_printer.py | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 .gdbinit create mode 100644 scripts/gdb/pretty_printer.py diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..02f3b5a --- /dev/null +++ b/.gdbinit @@ -0,0 +1 @@ +source scripts/gdb/pretty_printer.py diff --git a/scripts/debug.sh b/scripts/debug.sh index 5fda148..af55863 100755 --- a/scripts/debug.sh +++ b/scripts/debug.sh @@ -33,6 +33,7 @@ gdb --symbol ./build/kernel/kernel.sym \ -iex "set pagination of" \ -iex "target remote localhost:1234" \ "${BREAKPOINTS[@]}" \ + -x "${GITROOT}/.gdbinit" \ -ex "continue" echo "[INFO] Terminating debugging session" diff --git a/scripts/gdb/pretty_printer.py b/scripts/gdb/pretty_printer.py new file mode 100644 index 0000000..9e41b11 --- /dev/null +++ b/scripts/gdb/pretty_printer.py @@ -0,0 +1,40 @@ +class Avl: + + def __init__(self, val) -> None: + self.address = val.address + self.height = val["height"] + self.parent = val["parent"] + self.left = val["left"] + self.right = val["right"] + + def to_string_rec(self, parent, depth) -> str: + prefix = ' ' * depth + string = f'''{self.address} {{ + {prefix}height: {self.height} + {prefix}parent: {self.parent}{"" if self.parent == parent else " (error)"} +''' + + if self.left: + string += f" {prefix}left@{Avl(self.left.dereference()).to_string_rec(self.address, depth + 1)}\n" + if self.right: + string += f" {prefix}right@{Avl(self.right.dereference()).to_string_rec(self.address, depth + 1)}\n" + + string += f'{prefix}}}' + return string + + def to_string(self): + return f"avl@{self.to_string_rec(0, 0)}" + + +def pretty_printers(val): + printers = { + "avl_t": Avl + } + + if str(val.type) in printers: + return printers[str(val.type)](val) + + return None + + +gdb.pretty_printers.append(pretty_printers)