I am writing a GDB pretty printer in Python for this linked list data structure:
struct listNode {
void *data; /* node's data */
struct listNode *next; /* next node in list */
};
struct xlist {
struct listNode *head; /* head of the list */
struct listNode **tail; /* addr of last node's 'next' ptr */
struct listIterator *iNext; /* iterator chain for list_destroy() */
ListDelF fDel; /* function to delete node data */
int count; /* number of nodes in list */
};
typedef struct xlist * List;
The data field is void* because it is generic, so it is cast to the appropriate pointer type as necessary. Below is my pretty printer which works fine hard coded for a certain type (print_field_t), but I am looking for a way to pass the desired type as an argument to the printer. As far as I can tell, this is not supported by the API, but is there a way to accomplish this or the equivalent?
#!/usr/bin/env python3
import gdb.printing
import gdb
class ListPrinter(object):
'''Print a List object'''
class _iterator:
def __init__(self, current):
self.current = current
self.count = 0
self.end = False
def __iter__(self):
return self
def __next__(self):
if self.end:
raise StopIteration
type = gdb.lookup_type('print_field_t').pointer()
value = self.current['data'].cast(type).dereference()
self.current = self.current['next']
self.count += 1
if self.current == 0:
self.end = True
return (str(self.count), value)
def __init__(self, val):
self.val = val
def to_string(self):
return 'List({}):'.format(self.val['count'])
def children(self):
return self._iterator(self.val['head'])
def display_hint(self):
return 'array'
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter('List')
pp.add_printer('List Printer', '^List$', ListPrinter)
return pp
gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer())