From d72d9b9c716f4b56dbcf471bc4b7e7441db32cb3 Mon Sep 17 00:00:00 2001 From: heinezen Date: Tue, 16 Apr 2024 01:03:54 +0200 Subject: [PATCH 1/6] etc: Pretty printers for fixed-point coordinates. --- etc/gdb_pretty/printers.py | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index e5bbb42d84..1b239092ac 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -92,6 +92,57 @@ def _register_printer(printer): return _register_printer +@printer_regex('^openage::coord::((phys|scene)2|(chunk|tile))(_delta)?') +class CoordNeSePrinter: + """ + Pretty printer for openage::coord::CoordNeSe. + + TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + """ + + def __init__(self, val: gdb.Value): + self.__val = val + + def to_string(self): + """ + Get the coord as a string. + """ + return self.__val.type.name + + def children(self): + """ + Get the displayed children of the coord. + """ + yield ('ne', self.__val['ne']) + yield ('se', self.__val['se']) + + +@printer_regex('^openage::coord::(chunk|phys|scene|tile)3(_delta)?') +class CoordNeSeUpPrinter: + """ + Pretty printer for openage::coord::CoordNeSeUp. + + TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + """ + + def __init__(self, val: gdb.Value): + self.__val = val + + def to_string(self): + """ + Get the coord as a string. + """ + return self.__val.type.name + + def children(self): + """ + Get the displayed children of the coord. + """ + yield ('ne', self.__val['ne']) + yield ('se', self.__val['se']) + yield ('up', self.__val['up']) + + @printer_typedef('openage::time::time_t') class TimePrinter: """ From a0de523a2985bc96a7c252f8ed0d0ea4089272bd Mon Sep 17 00:00:00 2001 From: heinezen Date: Tue, 16 Apr 2024 01:10:23 +0200 Subject: [PATCH 2/6] etc: Pretty printers for integer coordinates. --- etc/gdb_pretty/printers.py | 52 +++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index 1b239092ac..1f0d354150 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -143,6 +143,57 @@ def children(self): yield ('up', self.__val['up']) +@printer_regex('^openage::coord::(camhud|viewport|input|term)(_delta)?') +class CoordXYPrinter: + """ + Pretty printer for openage::coord::CoordXY. + + TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + """ + + def __init__(self, val: gdb.Value): + self.__val = val + + def to_string(self): + """ + Get the coord as a string. + """ + return self.__val.type.name + + def children(self): + """ + Get the displayed children of the coord. + """ + yield ('x', self.__val['x']) + yield ('y', self.__val['y']) + + +@printer_regex('^openage::coord::(camhud|viewport|input|term)3(_delta)?') +class CoordXYZPrinter: + """ + Pretty printer for openage::coord::CoordXYZ. + + TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + """ + + def __init__(self, val: gdb.Value): + self.__val = val + + def to_string(self): + """ + Get the coord as a string. + """ + return self.__val.type.name + + def children(self): + """ + Get the displayed children of the coord. + """ + yield ('x', self.__val['x']) + yield ('y', self.__val['y']) + yield ('z', self.__val['z']) + + @printer_typedef('openage::time::time_t') class TimePrinter: """ @@ -286,7 +337,6 @@ def children(self): yield ('value', self.__val['value']) # TODO: curve types -# TODO: coord types # TODO: pathfinding types # TODO: input event codes # TODO: eigen types https://github.com/dmillard/eigengdb From 7e73cef274bfc0c0d70e595479a250135500b5bb Mon Sep 17 00:00:00 2001 From: heinezen Date: Tue, 16 Apr 2024 01:26:36 +0200 Subject: [PATCH 3/6] etc: Flatten coord printer into single class. --- etc/gdb_pretty/printers.py | 89 ++++---------------------------------- 1 file changed, 8 insertions(+), 81 deletions(-) diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index 1f0d354150..9a083f36ae 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -92,8 +92,8 @@ def _register_printer(printer): return _register_printer -@printer_regex('^openage::coord::((phys|scene)2|(chunk|tile))(_delta)?') -class CoordNeSePrinter: +@printer_regex('^openage::coord::(camhud|chunk|input|phys|scene|term|tile|viewport)(2|3)?(_delta)?') +class CoordPrinter: """ Pretty printer for openage::coord::CoordNeSe. @@ -113,85 +113,12 @@ def children(self): """ Get the displayed children of the coord. """ - yield ('ne', self.__val['ne']) - yield ('se', self.__val['se']) - - -@printer_regex('^openage::coord::(chunk|phys|scene|tile)3(_delta)?') -class CoordNeSeUpPrinter: - """ - Pretty printer for openage::coord::CoordNeSeUp. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. - """ - - def __init__(self, val: gdb.Value): - self.__val = val - - def to_string(self): - """ - Get the coord as a string. - """ - return self.__val.type.name - - def children(self): - """ - Get the displayed children of the coord. - """ - yield ('ne', self.__val['ne']) - yield ('se', self.__val['se']) - yield ('up', self.__val['up']) - - -@printer_regex('^openage::coord::(camhud|viewport|input|term)(_delta)?') -class CoordXYPrinter: - """ - Pretty printer for openage::coord::CoordXY. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. - """ - - def __init__(self, val: gdb.Value): - self.__val = val - - def to_string(self): - """ - Get the coord as a string. - """ - return self.__val.type.name - - def children(self): - """ - Get the displayed children of the coord. - """ - yield ('x', self.__val['x']) - yield ('y', self.__val['y']) - - -@printer_regex('^openage::coord::(camhud|viewport|input|term)3(_delta)?') -class CoordXYZPrinter: - """ - Pretty printer for openage::coord::CoordXYZ. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. - """ - - def __init__(self, val: gdb.Value): - self.__val = val - - def to_string(self): - """ - Get the coord as a string. - """ - return self.__val.type.name - - def children(self): - """ - Get the displayed children of the coord. - """ - yield ('x', self.__val['x']) - yield ('y', self.__val['y']) - yield ('z', self.__val['z']) + # Each coord type has one parent which is either + # of CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ + # From this parent we can get the fields + parent_type = self.__val.type.fields()[0].type + for child in parent_type.fields(): + yield (child.name, self.__val[child.name]) @printer_typedef('openage::time::time_t') From b284aa812d2b11a56c70057ba3dd923d7610fd76 Mon Sep 17 00:00:00 2001 From: heinezen Date: Tue, 16 Apr 2024 23:11:36 +0200 Subject: [PATCH 4/6] etc: Pretty print coord values in to_string(). --- etc/gdb_pretty/printers.py | 56 +++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index 9a083f36ae..d2f19685ad 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -92,10 +92,23 @@ def _register_printer(printer): return _register_printer +def format_fixed_point(value: int, fractional_bits: int) -> float: + """ + Formats a fixed point value to a double. + + :param value: The fixed point value. + :type value: int + :param fractional_bits: The number of fractional bits. + :type fractional_bits: int + """ + to_double_factor = 1 / pow(2, fractional_bits) + return float(value) * to_double_factor + + @printer_regex('^openage::coord::(camhud|chunk|input|phys|scene|term|tile|viewport)(2|3)?(_delta)?') class CoordPrinter: """ - Pretty printer for openage::coord::CoordNeSe. + Pretty printer for openage::coord types (CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ). TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ @@ -103,21 +116,33 @@ class CoordPrinter: def __init__(self, val: gdb.Value): self.__val = val + # Each coord type has one parent which is either + # of CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ + # From this parent we can get the fields + self._parent_type = self.__val.type.fields()[0].type + def to_string(self): """ Get the coord as a string. """ - return self.__val.type.name + field_vals = [] + for child in self._parent_type.fields(): + # Include the fixed point coordinates in the summary + val = self.__val[child.name] + num = format_fixed_point( + int(val['raw_value']), + int(val.type.template_argument(1)) + ) + field_vals.append(f"{num:.5f}") + + # Example: phys3[1.00000, 2.00000, 3.00000] + return f"{self.__val.type.tag.split('::')[-1]}[{', '.join(field_vals)}]" def children(self): """ Get the displayed children of the coord. """ - # Each coord type has one parent which is either - # of CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ - # From this parent we can get the fields - parent_type = self.__val.type.fields()[0].type - for child in parent_type.fields(): + for child in self._parent_type.fields(): yield (child.name, self.__val[child.name]) @@ -138,11 +163,11 @@ def to_string(self): Format: SS.sss (e.g. 12.345s) """ - fractional_bits = int(self.__val.type.template_argument(1)) + seconds = format_fixed_point( + int(self.__val['raw_value']), + int(self.__val.type.template_argument(1)) + ) - # convert the fixed point value to double - to_double_factor = 1 / pow(2, fractional_bits) - seconds = float(self.__val['raw_value']) * to_double_factor # show as seconds with millisecond precision return f'{seconds:.3f}s' @@ -170,11 +195,10 @@ def to_string(self): Format: 0.12345 """ - fractional_bits = int(self.__val.type.template_argument(1)) - - # convert the fixed point value to double - to_double_factor = 1 / pow(2, fractional_bits) - num = float(self.__val['raw_value']) * to_double_factor + num = format_fixed_point( + int(self.__val['raw_value']), + int(self.__val.type.template_argument(1)) + ) return f'{num:.5f}' def children(self): From b92138d401ac8f298cfb48c399a28eded5624ed0 Mon Sep 17 00:00:00 2001 From: heinezen Date: Fri, 19 Apr 2024 02:36:07 +0200 Subject: [PATCH 5/6] etc: Move Pretty print TODOs to the top of the module. --- etc/gdb_pretty/printers.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/etc/gdb_pretty/printers.py b/etc/gdb_pretty/printers.py index d2f19685ad..ca8f92083c 100644 --- a/etc/gdb_pretty/printers.py +++ b/etc/gdb_pretty/printers.py @@ -7,6 +7,8 @@ import re import gdb # type: ignore +# TODO: Printers should inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. + class PrinterControl(gdb.printing.PrettyPrinter): """ @@ -109,8 +111,6 @@ def format_fixed_point(value: int, fractional_bits: int) -> float: class CoordPrinter: """ Pretty printer for openage::coord types (CoordNeSe, CoordNeSeUp, CoordXY, CoordXYZ). - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -150,8 +150,6 @@ def children(self): class TimePrinter: """ Pretty printer for openage::time::time_t. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -182,8 +180,6 @@ def children(self): class FixedPointPrinter: """ Pretty printer for openage::util::FixedPoint. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -220,8 +216,6 @@ def children(self): class VectorPrinter: """ Pretty printer for openage::util::Vector. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): @@ -267,8 +261,6 @@ def display_hint(): class KeyframePrinter: """ Pretty printer for openage::curve::Keyframe. - - TODO: Inherit from gdb.ValuePrinter when gdb 14.1 is available in all distros. """ def __init__(self, val: gdb.Value): From d79640e4bf98eeb771c7b51caaf1b6fd2d33e9c2 Mon Sep 17 00:00:00 2001 From: heinezen Date: Sat, 20 Apr 2024 13:26:29 +0200 Subject: [PATCH 6/6] etc: Put reminders for changing pretty printers in CPP classes. --- libopenage/coord/coord.h.template | 8 +++++++- libopenage/curve/keyframe.h | 5 ++++- libopenage/util/fixed_point.h | 3 +++ libopenage/util/vector.h | 3 +++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/libopenage/coord/coord.h.template b/libopenage/coord/coord.h.template index d4af555ea0..8aeafadc5f 100644 --- a/libopenage/coord/coord.h.template +++ b/libopenage/coord/coord.h.template @@ -1,4 +1,4 @@ -// Copyright 2016-2023 the openage authors. See copying.md for legal info. +// Copyright 2016-2024 the openage authors. See copying.md for legal info. #pragma once @@ -24,6 +24,9 @@ namespace coord { * * 'Absolute' and 'Relative' are the absolute and relative types of the * derived class (CRTP). + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template struct Coord${camelcase}Absolute { @@ -87,6 +90,9 @@ struct Coord${camelcase}Absolute { * * 'Absolute' and 'Relative' are the absolute and relative types of the * derived class (CRTP). + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template struct Coord${camelcase}Relative { diff --git a/libopenage/curve/keyframe.h b/libopenage/curve/keyframe.h index 82e36ed147..484085cc70 100644 --- a/libopenage/curve/keyframe.h +++ b/libopenage/curve/keyframe.h @@ -1,4 +1,4 @@ -// Copyright 2019-2023 the openage authors. See copying.md for legal info. +// Copyright 2019-2024 the openage authors. See copying.md for legal info. #pragma once @@ -11,6 +11,9 @@ namespace openage::curve { /** * A element of the curvecontainer. This is especially used to keep track of * the value-timing. + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class Keyframe { diff --git a/libopenage/util/fixed_point.h b/libopenage/util/fixed_point.h index 7558ac9cb8..4e97e77323 100644 --- a/libopenage/util/fixed_point.h +++ b/libopenage/util/fixed_point.h @@ -81,6 +81,9 @@ constexpr static * For example, * FixedPoint * can store values from -2**32 to +2**32 with a constant precision of 2**-32. + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class FixedPoint { diff --git a/libopenage/util/vector.h b/libopenage/util/vector.h index 8e7c7bceb8..e79283fb64 100644 --- a/libopenage/util/vector.h +++ b/libopenage/util/vector.h @@ -20,6 +20,9 @@ namespace openage::util { * * N = dimensions * T = underlying single value type (double, float, ...) + * + * If you change this class, remember to update the gdb pretty printers + * in etc/gdb_pretty/printers.py. */ template class Vector : public std::array {