From fdfc1fcf6303175070f7dd479a379e2c8a7640ac Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 15 Jul 2022 23:52:16 +0200 Subject: [PATCH] Add GDB pretty-printers --- .gdbinit | 2 + utils/gdb/seer_pretty_printers.py | 78 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 .gdbinit create mode 100644 utils/gdb/seer_pretty_printers.py diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..d04df33 --- /dev/null +++ b/.gdbinit @@ -0,0 +1,2 @@ +# Register pretty-printers +source utils/gdb/seer_pretty_printers.py diff --git a/utils/gdb/seer_pretty_printers.py b/utils/gdb/seer_pretty_printers.py new file mode 100644 index 0000000..48a2e94 --- /dev/null +++ b/utils/gdb/seer_pretty_printers.py @@ -0,0 +1,78 @@ +import gdb.printing + +class Square(object): + """ + Wrapper around GDB's representation of a 'seer::board::square::Square' in + memory. + """ + + FILES = list(map(lambda n: chr(ord('A') + n), range(8))) + RANKS = list(map(lambda n: str(n + 1), range(8))) + + def __init__(self, val): + self._val = val + + def __str__(self): + return self.FILES[self.file] + self.RANKS[self.rank] + + @property + def rank(self): + return int(self._val) % 8 + + @property + def file(self): + return int(self._val) // 8 + +class Bitboard(object): + """ + Wrapper around GDB's representation of a 'seer::board::bitboard::Bitboard' + in memory. + """ + + def __init__(self, val): + self._val = val + + def __str__(self): + return "[" + ", ".join(map(str, self.squares)) + "]" + + @property + def squares(self): + n = int(self._val["__0"]) + while n: + b = n & (~n+1) + yield Square(b.bit_length() - 1) + n ^= b + +class SquarePrinter(object): + "Print a seer::board::square::Square" + + def __init__(self, val): + self._val = Square(val) + + def to_string(self): + return str(self._val) + + def display_hint(self): + return 'string' + +class BitboardPrinter(object): + "Print a seer::board::bitboard::Bitboard" + + def __init__(self, val): + self._val = Bitboard(val) + + def to_string(self): + return "Bitboard{" + str(self._val)[1:-1] + "}" + + def display_hint(self): + return 'string' + +def build_pretty_printer(): + pp = gdb.printing.RegexpCollectionPrettyPrinter('seer') + + pp.add_printer('BigNum', '^seer::board::square::Square$', SquarePrinter) + pp.add_printer('BigNum', '^seer::board::bitboard::Bitboard$', BitboardPrinter) + + return pp + +gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer(), True)