86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| 
 | |
| import gdb
 | |
| import gdb.printing
 | |
| 
 | |
| if hasattr(gdb, 'ValuePrinter'):
 | |
|     printer_base = gdb.ValuePrinter
 | |
| else:
 | |
|     printer_base = object
 | |
| 
 | |
| 
 | |
| class MijinStrideSpanIteratorPrinter(printer_base):
 | |
|     """Print a mijin::StrideSpanIterator."""
 | |
|     class _iterator:
 | |
|         def __init__(self, val):
 | |
|             self._val = val
 | |
| 
 | |
|         def __iter__(self):
 | |
|             return self
 | |
| 
 | |
|         def __next__(self):
 | |
|             if self._val is None:
 | |
|                 raise StopIteration
 | |
|             self._val, val = None, self._val
 | |
|             return ('ele', val.dereference())
 | |
| 
 | |
|     def __init__(self, val):
 | |
|         self._val = val
 | |
| 
 | |
|     def children(self):
 | |
|         return self._iterator(self._val['ele_'])
 | |
| 
 | |
|     def to_string(self):
 | |
|         t = self._val.type.template_argument(0)
 | |
|         return 'mijin::StrideSpanIterator<%s> with stride %d' % (str(t), self._val['strideBytes_'])
 | |
| 
 | |
|     def display_hint(self):
 | |
|         return None # 'array'
 | |
| 
 | |
| class MijinStrideSpanPrinter(printer_base):
 | |
|     """Print a mijin::StrideSpan."""
 | |
| 
 | |
|     class _iterator:
 | |
|         def __init__(self, start, finish, stride_bytes):
 | |
|             self._stride_bytes = stride_bytes
 | |
|             self._item = start
 | |
|             self._finish = finish
 | |
|             self._count = 0
 | |
| 
 | |
|         def __iter__(self):
 | |
|             return self
 | |
| 
 | |
|         def __next__(self):
 | |
|             count = self._count
 | |
|             self._count = self._count + 1
 | |
|             if self._item == self._finish:
 | |
|                 raise StopIteration
 | |
|             elt = self._item.dereference()
 | |
|             self._item = (self._item.cast(gdb.lookup_type('size_t')) + self._stride_bytes).cast(self._finish.type)
 | |
|             return ('[%d]' % count, elt)
 | |
| 
 | |
|     def __init__(self, val):
 | |
|         self._typename = str(val.type)
 | |
|         self._val = val
 | |
| 
 | |
|     def children(self):
 | |
|         return self._iterator(self._val['begin_'],
 | |
|                               self._val['end_'],
 | |
|                               self._val['strideBytes_'])
 | |
| 
 | |
|     def to_string(self):
 | |
|         start = self._val['begin_']
 | |
|         finish = self._val['end_']
 | |
|         strideBytes = self._val['strideBytes_']
 | |
|         return ('%s of length %d, stride %d'
 | |
|                     % (self._typename, int(finish - start) / strideBytes, strideBytes))
 | |
| 
 | |
|     def display_hint(self):
 | |
|         return 'array'
 | |
| 
 | |
| def build_pretty_printer():
 | |
|     printer = gdb.printing.gdb.printing.RegexpCollectionPrettyPrinter('mijin_pp')
 | |
|     printer.add_printer('mijin::StrideSpan', r"^mijin::StrideSpan<.*>$", MijinStrideSpanPrinter)
 | |
|     printer.add_printer('mijin::StrideSpanIterator', r"^mijin::StrideSpanIterator<.*>$", MijinStrideSpanIteratorPrinter)
 | |
|     return printer
 | |
| 
 | |
| gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer()) |