Skip to content

Commit

Permalink
chore(libalgo: avl): add GDB pretty printer for avl_t
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d4ilyrun committed May 8, 2024
1 parent 9504d4d commit 4d06397
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source scripts/gdb/pretty_printer.py
1 change: 1 addition & 0 deletions scripts/debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions scripts/gdb/pretty_printer.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4d06397

Please sign in to comment.