libcruft-util/debug/gdb/printers.py

104 lines
2.5 KiB
Python

import re
import gdb
class CoordPrinter(object):
def __init__(self, title, val):
self.title = title
self.val = val
def to_string(self):
return self.title
def display_hint(self):
return 'array'
class _iterator(object):
def __init__(self, data):
self.data = data
def __iter__(self):
indices = self.data.type.range()
for i in range(indices[0], indices[1] + 1):
res = (str(i), self.data[i])
yield res
raise StopIteration()
def children(self):
return self._iterator(self.val['data'])
class ViewPrinter(object):
def __init__(self, title, val):
self.title = title
self.val = val
def to_string(self):
return self.title
def display_hint(self):
return 'array'
class _iterator(object):
def __init__(self, first, last):
self.cursor = first
self.last = last
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.cursor == self.last:
raise StopIteration
val = self.cursor.dereference()
res = ("[%d]" % self.index, val)
self.cursor = self.cursor + 1
self.index = self.index + 1
return res
def children(self):
return self._iterator(
self.val["m_begin"],
self.val["m_end"]
)
def build_cruft_dict():
pretty_printers_dict[re.compile('^cruft::point.*$') ] = lambda title, val: CoordPrinter(title, val)
pretty_printers_dict[re.compile('^cruft::vector.*$')] = lambda title, val: CoordPrinter(title, val)
pretty_printers_dict[re.compile('^cruft::extent.*$')] = lambda title, val: CoordPrinter(title, val)
pretty_printers_dict[re.compile('^cruft::colour.*$')] = lambda title, val: CoordPrinter(title, val)
pretty_printers_dict[re.compile('^cruft::view')] = lambda title, val: ViewPrinter(title, val)
def lookup(val):
type = val.type
if type.code == gdb.TYPE_CODE_REF:
type = type.target()
type = type.unqualified().strip_typedefs()
typename = type.tag
if typename == None:
return None
for function in pretty_printers_dict:
if function.search(typename):
return pretty_printers_dict[function](typename, val)
return None
def register_cruft_printers():
gdb.pretty_printers.append(lookup)
pretty_printers_dict = {}
build_cruft_dict ()