seer/utils/gdb/seer_pretty_printers.py

420 lines
10 KiB
Python
Raw Permalink Normal View History

2024-03-31 21:02:23 +02:00
import enum
2024-04-01 15:39:05 +02:00
import gdb
2022-07-15 23:52:16 +02:00
import gdb.printing
def optional(constructor, val):
try:
return constructor(val["Some"]["__0"])
except gdb.error:
return None
def print_opt(val):
return "(None)" if val is None else str(val)
2022-07-15 23:52:16 +02:00
class Square(object):
"""
Python representation of a 'seer::board::square::Square' raw value.
"""
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):
2024-04-04 00:39:37 +02:00
if isinstance(val, Square):
val = val._val
2022-07-15 23:52:16 +02:00
self._val = val
@classmethod
def from_gdb(cls, val):
return cls(int(val))
@classmethod
def from_file_rank(cls, file, rank):
return cls(file * 8 + rank)
2022-07-15 23:52:16 +02:00
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):
"""
Python representation of a 'seer::board::bitboard::Bitboard' raw value.
"""
def __init__(self, val):
2024-04-04 00:39:37 +02:00
if isinstance(val, Bitboard):
val = val._val
2022-07-15 23:52:16 +02:00
self._val = val
@classmethod
def from_gdb(cls, val):
return cls(int(val["__0"]))
2022-07-15 23:52:16 +02:00
def __str__(self):
return "[" + ", ".join(map(str, self.squares)) + "]"
2024-04-01 15:38:26 +02:00
def at(self, square):
return bool(self._val & (1 << square._val))
2022-07-15 23:52:16 +02:00
@property
def squares(self):
n = self._val
while n:
b = n & (~n + 1)
yield Square(b.bit_length() - 1)
n ^= b
2024-04-01 13:28:22 +02:00
class CastleRights(enum.IntEnum):
"""
Python representation of a 'seer::board::castle_rights::CastleRights' raw value.
"""
# Should be kept in sync with the enum in `color.rs`
NO_SIDE = 0
KING_SIDE = 1
QUEEN_SIDE = 2
BOTH_SIDES = 3
@classmethod
def from_gdb(cls, val):
return cls(int(val))
2024-04-01 13:28:22 +02:00
def __str__(self):
return self.name.title().replace("_", "")
2024-03-31 21:02:23 +02:00
class Color(enum.IntEnum):
"""
Python representation of a 'seer::board::color::Color' raw value.
"""
# Should be kept in sync with the enum in `color.rs`
WHITE = 0
BLACK = 1
@classmethod
def from_gdb(cls, val):
return cls(int(val))
2024-03-31 21:02:23 +02:00
def __str__(self):
return self.name.title()
2024-03-31 21:50:49 +02:00
class File(enum.IntEnum):
"""
Python representation of a 'seer::board::file::File' raw value.
"""
# Should be kept in sync with the enum in `file.rs`
A = 0
B = 1
C = 2
D = 3
E = 4
F = 5
G = 6
H = 7
@classmethod
def from_gdb(cls, val):
return cls(int(val))
2024-03-31 21:50:49 +02:00
def __str__(self):
return self.name.title()
2024-03-31 21:50:49 +02:00
class Rank(enum.IntEnum):
"""
Python representation of a 'seer::board::rank::Rank' raw value.
"""
# Should be kept in sync with the enum in `rank.rs`
First = 0
Second = 1
Third = 2
Fourth = 3
Fifth = 4
Sixth = 5
Seventh = 6
Eighth = 7
@classmethod
def from_gdb(cls, val):
return cls(int(val))
2024-03-31 21:50:49 +02:00
def __str__(self):
return self.name.title()
2024-03-31 21:02:23 +02:00
class Piece(enum.IntEnum):
"""
Python representation of a 'seer::board::piece::Piece' raw value.
"""
# Should be kept in sync with the enum in `piece.rs`
KING = 0
QUEEN = 1
ROOK = 2
BISHOP = 3
KNIGHT = 4
PAWN = 5
@classmethod
def from_gdb(cls, val):
return cls(int(val))
2024-03-31 21:02:23 +02:00
def __str__(self):
return self.name.title()
2024-03-31 21:02:23 +02:00
class Move(object):
"""
Wrapper around GDB's representation of a 'seer::board::move::Move'
in memory.
"""
def __init__(self, start, destination, promotion):
self._start = Square(start)
self._destination = Square(destination)
self._promotion = Piece(promotion)
2024-03-31 21:02:23 +02:00
@classmethod
def from_gdb(cls, val):
start = Square(int(val["start"]))
destination = Square(int(val["destination"]))
promotion = optional(Piece.from_gdb, val["promotion"])
cls(start, destination, promotion)
2024-03-31 21:02:23 +02:00
@property
def start(self):
return self._start
2024-03-31 21:02:23 +02:00
@property
def destination(self):
return self._destination
2024-03-31 21:02:23 +02:00
@property
def promotion(self):
return self._promotion
2024-03-31 21:02:23 +02:00
def __str__(self):
KEYS = [
"start",
"destination",
"promotion",
]
indent = lambda s: " " + s
values = [key + ": " + print_opt(getattr(self, key)) + ",\n" for key in KEYS]
return "Move{\n" + "".join(map(indent, values)) + "}"
2024-04-01 15:39:05 +02:00
class ChessBoard(object):
"""
Wrapper around GDB's representation of a 'seer::board::chess_board::ChessBoard'
in memory.
"""
def __init__(
self,
piece_occupancy,
color_occupancy,
castle_rights,
half_move_clock,
total_plies,
side,
en_passant,
2024-04-01 15:39:05 +02:00
):
self._piece_occupancy = list(map(Bitboard, piece_occupancy))
self._color_occupancy = list(map(Bitboard, color_occupancy))
self._castle_rights = list(map(CastleRights, castle_rights))
self._half_move_clock = int(half_move_clock)
self._total_plies = int(total_plies)
self._side = Color(side)
self._en_passant = None if en_passant is None else Square(en_passant)
2024-04-01 15:39:05 +02:00
@classmethod
def from_gdb(cls, val):
return cls(
[Bitboard.from_gdb(val["piece_occupancy"][p]) for p in Piece],
[Bitboard.from_gdb(val["color_occupancy"][c]) for c in Color],
[CastleRights.from_gdb(val["castle_rights"][c]) for c in Color],
2024-04-01 15:39:05 +02:00
int(val["half_move_clock"]),
int(val["total_plies"]),
Color.from_gdb(val["side"]),
optional(Square.from_gdb, val["en_passant"]),
2024-04-01 15:39:05 +02:00
)
def at(self, square):
for piece in Piece:
if not self._piece_occupancy[piece].at(square):
continue
for color in Color:
if not self._color_occupancy[color].at(square):
continue
return (piece, color)
return None
def pretty_str(self):
def pretty_piece(piece, color):
return [
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
][piece][color]
board = [
[self.at(Square.from_file_rank(file, rank)) for file in File]
for rank in Rank
]
res = []
res.append(" A B C D E F G H ")
for n, line in reversed(list(enumerate(board, start=1))):
strings = [str(n) + " "]
strings.extend(" " if p is None else pretty_piece(*p) for p in line)
strings.append(" " + str(n))
res.append("|".join(strings))
res.append(" A B C D E F G H ")
res += [
"Half-move clock: " + str(self._half_move_clock),
"Total plies: " + str(self._total_plies),
"Side to play: " + str(self._side),
"En passant: " + print_opt(self._en_passant),
2024-04-01 15:39:05 +02:00
]
return "\n".join(res)
2022-07-15 23:52:16 +02:00
class SquarePrinter(object):
"Print a seer::board::square::Square"
def __init__(self, val):
self._val = Square.from_gdb(val)
2022-07-15 23:52:16 +02:00
def to_string(self):
return str(self._val)
class BitboardPrinter(object):
"Print a seer::board::bitboard::Bitboard"
def __init__(self, val):
self._val = Bitboard.from_gdb(val)
2022-07-15 23:52:16 +02:00
def to_string(self):
return "Bitboard{" + str(self._val)[1:-1] + "}"
2024-04-01 13:28:22 +02:00
class CastleRightsPrinter(object):
"Print a seer::board::castle_rights::CastleRights"
def __init__(self, val):
self._val = CastleRights.from_gdb(val)
2024-04-01 13:28:22 +02:00
def to_string(self):
return str(self._val)
2024-03-31 21:02:23 +02:00
class ColorPrinter(object):
"Print a seer::board::color::Color"
def __init__(self, val):
self._val = Color.from_gdb(val)
2024-03-31 21:02:23 +02:00
def to_string(self):
return str(self._val)
2024-03-31 21:50:49 +02:00
class FilePrinter(object):
"Print a seer::board::file::File"
def __init__(self, val):
self._val = File.from_gdb(val)
2024-03-31 21:50:49 +02:00
def to_string(self):
return str(self._val)
2024-03-31 21:50:49 +02:00
class RankPrinter(object):
"Print a seer::board::rank::Rank"
def __init__(self, val):
self._val = Rank.from_gdb(val)
2024-03-31 21:50:49 +02:00
def to_string(self):
return str(self._val)
2024-03-31 21:02:23 +02:00
class PiecePrinter(object):
"Print a seer::board::piece::Piece"
def __init__(self, val):
self._val = Piece.from_gdb(val)
2024-03-31 21:02:23 +02:00
def to_string(self):
return str(self._val)
2024-03-31 21:02:23 +02:00
class MovePrinter(object):
"Print a seer::board::move::Move"
def __init__(self, val):
self._val = Move.from_gdb(val)
2024-03-31 21:02:23 +02:00
def to_string(self):
return str(self._val)
2024-04-01 15:39:05 +02:00
class PrintBoard(gdb.Command):
"""
Pretty-print a 'seer::board::chess_board::ChessBoard' as a 2D textual chess board.
"""
def __init__(self):
super(PrintBoard, self).__init__(
"print-board", gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION
)
def invoke(self, arg, from_tty):
board = ChessBoard.from_gdb(gdb.parse_and_eval(arg))
print(board.pretty_str())
2022-07-15 23:52:16 +02:00
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter('seer')
pp.add_printer('Square', '^seer::board::square::Square$', SquarePrinter)
pp.add_printer('Bitboard', '^seer::board::bitboard::Bitboard$', BitboardPrinter)
2024-04-01 13:28:22 +02:00
pp.add_printer('CastleRights', '^seer::board::castle_rights::CastleRights$', CastleRightsPrinter)
2024-03-31 21:02:23 +02:00
pp.add_printer('Color', '^seer::board::color::Color$', ColorPrinter)
2024-03-31 21:50:49 +02:00
pp.add_printer('File', '^seer::board::file::File$', FilePrinter)
2024-03-31 21:50:49 +02:00
pp.add_printer('Rank', '^seer::board::rank::Rank$', RankPrinter)
2024-03-31 21:02:23 +02:00
pp.add_printer('Piece', '^seer::board::piece::Piece$', ColorPrinter)
2024-03-31 21:02:23 +02:00
pp.add_printer('Move', '^seer::board::move::Move$', MovePrinter)
2022-07-15 23:52:16 +02:00
return pp
2024-04-01 15:39:05 +02:00
def register_commands():
PrintBoard()
2022-07-15 23:52:16 +02:00
gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer(), True)
2024-04-01 15:39:05 +02:00
register_commands()