Fixing pretty printers
This commit is contained in:
+31
-2
@@ -1,12 +1,11 @@
|
||||
import sys
|
||||
sys.path.insert(0, '/usr/share/gdb/python/')
|
||||
import gdb.printing
|
||||
import glm_pp
|
||||
|
||||
class ArrayPrinter:
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
|
||||
def to_string(self):
|
||||
return f"Array(size={int(self.val['arraySize'])})"
|
||||
|
||||
@@ -23,9 +22,39 @@ class ArrayPrinter:
|
||||
def display_hint(self):
|
||||
return "array"
|
||||
|
||||
class VectorPrinter:
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
t = val.type.strip_typedefs()
|
||||
self.length = int(t.template_argument(0))
|
||||
self.value_type = t.template_argument(1)
|
||||
|
||||
def to_string(self):
|
||||
comps = [float(self.val['x']), float(self.val['y'])]
|
||||
if self.length >= 3:
|
||||
comps.append(float(self.val['z']))
|
||||
if self.length == 4:
|
||||
comps.append(float(self.val['w']))
|
||||
return "(" + ", ".join(map(str, comps)) + ")"
|
||||
|
||||
def display_hint(self):
|
||||
return "string"
|
||||
|
||||
def _children(self):
|
||||
comps = {'x': self.val['x'], 'y': self.val['y']}
|
||||
if self.length >= 3:
|
||||
comps['z'] = self.val['z']
|
||||
if self.length == 4:
|
||||
comps['w'] = self.val['w']
|
||||
for key, value in comps.items():
|
||||
yield key, value
|
||||
|
||||
|
||||
def build_pretty_printer():
|
||||
pp = gdb.printing.RegexpCollectionPrettyPrinter("Seele")
|
||||
pp.add_printer("Array", r'^Seele::Array<.*>$', ArrayPrinter)
|
||||
pp.add_printer("Vector", r"^glm::(detail::)?t?vec(\d)?<[^<>]*>$", VectorPrinter)
|
||||
return pp
|
||||
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer())
|
||||
@@ -1,68 +0,0 @@
|
||||
import gdb.printing
|
||||
|
||||
_type_letters = {
|
||||
gdb.TYPE_CODE_FLT: "d", # or "f"
|
||||
gdb.TYPE_CODE_INT: "i",
|
||||
gdb.TYPE_CODE_BOOL: "b"
|
||||
}
|
||||
|
||||
def _vec_info(v):
|
||||
# vec contains either a union of structs or a struct of unions, depending on
|
||||
# configuration. gdb can't properly access the named members, and in some
|
||||
# cases the names are wrong.
|
||||
# It would be simple to cast to an array, similarly to how operator[] is
|
||||
# implemented, but values returned by functions called from gdb don't have
|
||||
# an address.
|
||||
# Instead, recursively find all fields of required type and sort by offset.
|
||||
T = v.type.template_argument(0)
|
||||
letter = _type_letters.get(T.code, "t")
|
||||
length = v.type.sizeof // T.sizeof
|
||||
items = {}
|
||||
def find(v, bitpos):
|
||||
t = v.type.strip_typedefs()
|
||||
if t.code in (gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION):
|
||||
for f in t.fields():
|
||||
if hasattr(f, "bitpos"): # not static
|
||||
find(v[f], bitpos + f.bitpos)
|
||||
elif t == T:
|
||||
items[bitpos] = v
|
||||
find(v, 0)
|
||||
assert len(items) == length
|
||||
items = [str(f) for k, f in sorted(items.items())]
|
||||
return letter, length, items
|
||||
|
||||
class VecPrinter:
|
||||
def __init__(self, v):
|
||||
self.v = v
|
||||
|
||||
def to_string(self):
|
||||
letter, length, items = _vec_info(self.v)
|
||||
return "{}vec{}({})".format(letter, length, ", ".join(items))
|
||||
|
||||
class MatPrinter:
|
||||
def __init__(self, v):
|
||||
self.v = v
|
||||
|
||||
def to_string(self):
|
||||
V = self.v["value"]
|
||||
columns = []
|
||||
for i in range(V.type.range()[1] + 1):
|
||||
letter, length, items = _vec_info(V[i])
|
||||
columns.append("({})".format(", ".join(items)))
|
||||
return "{}mat{}x{}({})".format(
|
||||
letter, len(columns), length, ", ".join(columns))
|
||||
|
||||
def build_pretty_printer():
|
||||
pp = gdb.printing.RegexpCollectionPrettyPrinter("glm_pp")
|
||||
pp.add_printer(
|
||||
"glm::vec", r"^glm::(detail::)?tvec\d<[^<>]*>$", VecPrinter)
|
||||
pp.add_printer(
|
||||
"glm::mat", r"^glm::(detail::)?tmat\dx\d<[^<>]*>$", MatPrinter)
|
||||
pp.add_printer(
|
||||
"Seele::Vector", r"^Seele::Vector.?$", VecPrinter)
|
||||
pp.add_printer(
|
||||
"Seele::Matrix", r"^Seele::Matrix.?$", MatPrinter)
|
||||
return pp
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(),
|
||||
build_pretty_printer())
|
||||
Reference in New Issue
Block a user