- Adjusted some tests while debugging PrettyPrinters

- Adjusted RDTreePrinter to print more "tree-like"
 - Added SetPrinter and MapPrinter
 - Fixed Issues with CStringPrinter and StringPrinter
This commit is contained in:
2025-08-10 00:27:04 -04:00
parent d2be083a8f
commit 9f96155856
13 changed files with 188 additions and 43 deletions

31
gdb/fennec/__init__.py Normal file
View File

@@ -0,0 +1,31 @@
# ======================================================================================================================
# fennec, a free and open source game engine
# Copyright © 2025 Medusa Slockbower
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================
# GDB CODE =============================================================================================================
import gdb
from . import containers
from . import strings
from . import memory
def register_printers(obj):
gdb.printing.register_pretty_printer(obj, containers.printer)
gdb.printing.register_pretty_printer(obj, strings.printer)
gdb.printing.register_pretty_printer(obj, memory.printer)

314
gdb/fennec/containers.py Normal file
View File

@@ -0,0 +1,314 @@
# ======================================================================================================================
# fennec, a free and open source game engine
# Copyright © 2025 Medusa Slockbower
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================
import gdb
import re
from collections import deque
# OPTIONAL =============================================================================================================
class OptionalPrinter:
"""Print a fennec::optional"""
def __init__(self, val):
self.val = val
def to_string(self):
is_initialized = self.val['_set']
if is_initialized:
return "{{ value = {} }}".format(self.val['_val'])
else:
return "{ empty }"
# OPTIONAL =============================================================================================================
class PairPrinter:
"""Print a fennec::optional"""
def __init__(self, val):
self.val = val
def to_string(self):
return "{ first = " + str(self.val['first']) + ", second = " + str(self.val['second']) + " }"
def children(self):
return ("first", self.val['first']), ("second", self.val['second'])
# ARRAY ================================================================================================================
class ArrayPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return "{ length = " + str(self.val['elements'].type.range()[1] + 1) + " }"
def display_hint(self):
return 'array'
def children(self):
start = self.val['elements']
return (('[{}]'.format(i), start[i]) for i in range(0, self.val['elements'].type.range()[1] + 1))
# DYNARRAY =============================================================================================================
class DynArrayPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return "{ length " + str(self.val['_size']) + ", capacity " + str(self.val['_alloc']['_capacity']) + " }"
def children(self):
size = int(self.val['_size'])
start = self.val['_alloc']['_data']
return (('[{}]'.format(i), start[i]) for i in range(size))
# LIST =================================================================================================================
class ListPrinter:
"""Print a fennec::list"""
class Iterator:
def __init__(self, val):
self.list = val
self.node = self.list['_root']
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.node == 18446744073709551615:
raise StopIteration
i = self.index
self.index = self.index + 1
value = self.list['_table']['_data'][self.node]['value']['_val']
self.node = self.list['_table']['_data'][self.node]['next']
return '[{}]'.format(i), value
def __init__(self, val):
self.val = val
def to_string(self):
return "{ length " + str(self.val['_size']) + ", capacity " + str(self.val['_table']['_capacity']) + " }"
def children(self):
return self.Iterator(self.val)
def display_hint(self):
return 'array'
# RDTREE ===============================================================================================================
class RDTreePrinter:
"""Print a fennec::rdtree"""
class Iterator:
def __init__(self, tree, node, capacity):
self.tree = tree
self.capacity = capacity
self.visit = deque()
self.visit.append((node, 0, 0))
def __iter__(self):
return self
def __next__(self):
if len(self.visit) == 0:
raise StopIteration
node = self.visit[0][0]
i = self.visit[0][1]
depth = self.visit[0][2]
self.visit.popleft()
opt = self.tree[node]['value']
value = None
if opt['_set']:
value = opt['_val']
nnext = self.tree[node]['next']
nprev = self.tree[node]['prev']
nprevc = self.tree[nprev]['child'] if nprev != 18446744073709551615 else 18446744073709551615
child = self.tree[node]['child']
index = '' * depth * 2 # Uses Braille Space, otherwise it would get eaten as whitespace by parsers
if nnext < self.capacity:
self.visit.appendleft((nnext, i + 1, depth))
if child < self.capacity:
self.visit.appendleft((child, 0, depth + 1))
if child == 18446744073709551615: # If the child is NULL
if nprevc != 18446744073709551615: # And there was a previous child
if nnext != 18446744073709551615: # And there is a next node
index += '' # Begin new branch
else: # Otherwise
index += '' # Add single branch
elif nnext != 18446744073709551615: # Else if there is a next node
index += '' # Continue Branch
else: # Otherwise
index += '' # Terminate Branch
elif nprevc != 18446744073709551615: # Else if there is a previous child (this node has a child)
index += '' # Add single branch
else: # Otherwise
index += '' # Terminate Branch
index += ''
index += '[{}]'.format(i)
print(index)
if value is None:
return index, '{ empty }'
return index, value
def __init__(self, val):
self.tree = val['_table']['_data']
self.size = val['_size']
self.capacity = val['_table']['_capacity']
def to_string(self):
if self.size == 0:
return "{ empty }"
return "{ size = " + str(self.size) + " }"
def children(self):
if self.size == 0:
return None
return self.Iterator(self.tree, 0, self.capacity)
# SET ==================================================================================================================
class SetPrinter:
class Iterator:
def __init__(self, table, capacity):
self.table = table
self.capacity = capacity
self.node = 0
self.index = 0
def __iter__(self):
return self
def __next__(self):
while self.node < self.capacity:
if self.table[self.node]['value']['_set']:
break
self.node = self.node + 1
if self.node >= self.capacity:
raise StopIteration
i = self.index
value = self.table[self.node]['value']['_val']
self.node = self.node + 1
self.index = self.index + 1
return '[{}]'.format(i), value
def __init__(self, val):
self.table = val['_alloc']['_data']
self.capacity = val['_alloc']['_capacity']
self.size = val['_size']
def to_string(self):
if self.size == 0:
return "{ empty }"
return "{ size = " + str(self.size) + " }"
def children(self):
return self.Iterator(self.table, self.capacity)
# MAP ==================================================================================================================
class MapPrinter:
class Iterator:
def __init__(self, table, capacity):
self.table = table
self.capacity = capacity
self.node = 0
self.index = 0
self.move = False
def __iter__(self):
return self
def __next__(self):
while self.node < self.capacity:
if self.table[self.node]['value']['_set']:
break
self.node = self.node + 1
if self.node >= self.capacity:
raise StopIteration
i = self.index
pair = self.table[self.node]['value']['_val']
key = pair['first']
val = pair['second']
if not self.move:
self.move = True
return ('key' + str(i), key)
else:
self.move = False
self.index = self.index + 1
self.node = self.node + 1
return ('value' + str(i), val)
def __init__(self, val):
self.table = val['_set']['_alloc']['_data']
self.capacity = val['_set']['_alloc']['_capacity']
self.size = val['_set']['_size']
def to_string(self):
if self.size == 0:
return "{ empty }"
return "{ size = " + str(self.size) + " }"
def children(self):
return self.Iterator(self.table, self.capacity)
def display_hint(self):
return 'map'
# GDB Code =============================================================================================================
def register_printers():
print("registering containers")
pp = gdb.printing.RegexpCollectionPrettyPrinter("fennec::containers")
pp.add_printer('optional', '^fennec::optional<.*>$', OptionalPrinter)
pp.add_printer('pair', '^fennec::pair<.*>$', PairPrinter)
pp.add_printer('array', '^fennec::array<.*>$', ArrayPrinter)
pp.add_printer('dynarray', '^fennec::dynarray<.*>$', DynArrayPrinter)
pp.add_printer('list', '^fennec::list<.*>$', ListPrinter)
pp.add_printer('rdtree', '^fennec::rdtree<.*>$', RDTreePrinter)
pp.add_printer('set', '^fennec::set<.*>$', SetPrinter)
pp.add_printer('map', '^fennec::map<.*>$', MapPrinter)
return pp
printer = register_printers()

44
gdb/fennec/memory.py Normal file
View File

@@ -0,0 +1,44 @@
# ======================================================================================================================
# fennec, a free and open source game engine
# Copyright © 2025 Medusa Slockbower
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================
import gdb
import re
# ALLOCATION ===========================================================================================================
class AllocationPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
return "{ capacity = " + str(self.val['_capacity']) + " }"
def children(self):
size = int(self.val['_capacity'])
start = self.val['_data']
return (('[{}]'.format(i), start[i]) for i in range(size))
# GDB Code =============================================================================================================
def register_printers():
print("registering memory")
pp = gdb.printing.RegexpCollectionPrettyPrinter("fennec::memory")
pp.add_printer('allocation', '^fennec::allocation<.*>$', AllocationPrinter)
return pp
printer = register_printers()

62
gdb/fennec/strings.py Normal file
View File

@@ -0,0 +1,62 @@
# ======================================================================================================================
# fennec, a free and open source game engine
# Copyright © 2025 Medusa Slockbower
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ======================================================================================================================
import gdb
import re
# CSTRING ==============================================================================================================
class CStringPrinter:
def __init__(self, val):
self.val = val
def display_hint(self):
return 'string'
def to_string(self):
value = "\"" + self.val['_str'].string('', 'replace', self.val['_size'] - 1) + "\""
return value
def display_hint(self):
return 'string'
# STRING ===============================================================================================================
class StringPrinter:
def __init__(self, val):
self.val = val['_str']
def to_string(self):
value = "\"" + self.val['_data'].string('', 'replace', self.val['_capacity'] - 1) + "\""
return value
def display_hint(self):
return 'string'
# GDB Code =============================================================================================================
def register_printers():
print("registering strings")
pp = gdb.printing.RegexpCollectionPrettyPrinter("fennec::strings")
pp.add_printer('cstring', '^fennec::cstring$', CStringPrinter)
pp.add_printer('wcstring', '^fennec::wcstring$', CStringPrinter)
pp.add_printer('string', '^fennec::_string<.*>$', StringPrinter)
pp.add_printer('wstring', '^fennec::_wstring<.*>$', StringPrinter)
return pp
printer = register_printers()