2022-10-16 19:50:13 +00:00
|
|
|
"""GDB pretty-printer macros for GNU Make."""
|
2021-07-25 21:19:09 +00:00
|
|
|
|
|
|
|
import gdb # pylint: disable=import-error
|
|
|
|
import gdb.printing # pylint: disable=import-error
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Memoize types we commonly use
|
|
|
|
_TYPES = {}
|
2021-07-25 21:19:09 +00:00
|
|
|
|
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
def getType(tname):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Given a type name return a GDB type."""
|
2019-05-19 15:25:06 +00:00
|
|
|
global _TYPES
|
|
|
|
if tname not in _TYPES:
|
|
|
|
tn = tname.rstrip('*')
|
|
|
|
if tn not in _TYPES:
|
|
|
|
_TYPES[tn] = gdb.lookup_type(tn)
|
|
|
|
while tn != tname:
|
|
|
|
# Want a pointer type
|
|
|
|
t = tn
|
|
|
|
tn += '*'
|
|
|
|
_TYPES[tn] = _TYPES[t].pointer()
|
|
|
|
return _TYPES[tname]
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
|
|
|
def isNullptr(val):
|
|
|
|
"""Return True if the value is a null pointer."""
|
|
|
|
return int(val.cast(getType('unsigned long long'))) == 0
|
|
|
|
|
|
|
|
|
|
|
|
class ShowArgv(gdb.Command):
|
|
|
|
"""Print a null-terminated array of strings.
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
Argument:
|
|
|
|
A char** where the last one is NULL (e.g., argv)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create the showargv function."""
|
|
|
|
gdb.Command.__init__(self, "showargv", gdb.COMMAND_USER)
|
|
|
|
|
|
|
|
def invoke(self, arg, from_tty):
|
|
|
|
"""Show the argv."""
|
|
|
|
args = gdb.string_to_argv(arg)
|
|
|
|
if len(args) != 1:
|
|
|
|
raise gdb.GdbError(self._usage)
|
|
|
|
|
|
|
|
val = gdb.parse_and_eval(args[0])
|
|
|
|
if val is None:
|
|
|
|
raise gdb.GdbError('%s is not a valid expression' % (args[0]))
|
|
|
|
|
|
|
|
strs = []
|
|
|
|
while not isNullptr(val.dereference()):
|
|
|
|
strs.append('"'+val.dereference().string()+'"')
|
|
|
|
val += 1
|
|
|
|
|
|
|
|
gdb.write("[%d] = [%s]\n" % (len(strs), ', '.join(strs)))
|
|
|
|
gdb.flush()
|
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
ShowArgv()
|
|
|
|
|
|
|
|
|
|
|
|
class ShowNextList(gdb.Command):
|
|
|
|
"""Print a structure that has a "next" pointer.
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
A pointer to a struct which contains a "next" member.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_usage = 'usage: showlist <listptr>'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Create a "showlist" function."""
|
|
|
|
gdb.Command.__init__(self, "showlist", gdb.COMMAND_USER)
|
|
|
|
|
|
|
|
def invoke(self, arg, from_tty):
|
|
|
|
"""Show the elements in the provided list."""
|
|
|
|
args = gdb.string_to_argv(arg)
|
|
|
|
if len(args) != 1:
|
|
|
|
raise gdb.GdbError(self._usage)
|
|
|
|
|
|
|
|
val = gdb.parse_and_eval(args[0])
|
|
|
|
if val is None:
|
|
|
|
raise gdb.GdbError('%s is not a valid expression' % (args[0]))
|
2019-05-19 15:25:06 +00:00
|
|
|
i = 0
|
2021-07-25 21:19:09 +00:00
|
|
|
while not isNullptr(val):
|
|
|
|
gdb.write("%s : %s\n" % (val, val.dereference()))
|
|
|
|
gdb.flush()
|
2019-05-19 15:25:06 +00:00
|
|
|
i += 1
|
2021-07-25 21:19:09 +00:00
|
|
|
val = val['next']
|
|
|
|
gdb.write("%s contains %d elements\n" % (args[0], i))
|
|
|
|
gdb.flush()
|
2019-05-19 15:25:06 +00:00
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
|
|
|
ShowNextList()
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FileLocation(object):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Print a file location."""
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
def __init__(self, val):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create a FileLocation object."""
|
2019-05-19 15:25:06 +00:00
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def to_string(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Convert a FileLocation to a string."""
|
|
|
|
if int(self.val['filenm']):
|
2019-05-19 15:25:06 +00:00
|
|
|
return "%s:%d" % (str(self.val['filenm']), self.val['lineno'])
|
2021-07-25 21:19:09 +00:00
|
|
|
return 'NILF'
|
|
|
|
|
|
|
|
|
|
|
|
class StringListPrinter(object):
|
|
|
|
"""Print a stringlist."""
|
|
|
|
|
|
|
|
def __init__(self, val):
|
|
|
|
"""Create a StringListPrinter object."""
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
"""Convert a HashTable into a string."""
|
|
|
|
return "size=%d, capacity=%d" % (self.val['idx'], self.val['max'])
|
|
|
|
|
|
|
|
def children(self):
|
|
|
|
"""Yield each string in the list."""
|
|
|
|
i = 0
|
|
|
|
elts = self.val['list']
|
|
|
|
while i < self.val['idx']:
|
|
|
|
nm = '[%d] ' % i
|
|
|
|
yield (nm, elts.dereference())
|
|
|
|
i += 1
|
|
|
|
elts += 1
|
|
|
|
|
|
|
|
def display_hint(self):
|
|
|
|
"""Show the display hint for the pretty-printer."""
|
|
|
|
return 'array'
|
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
class VariablePrinter(object):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Print a struct variable."""
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
def __init__(self, val):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create a VariablePrinter object."""
|
2019-05-19 15:25:06 +00:00
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def to_string(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Convert a VariablePrinter object into a string."""
|
2019-05-19 15:25:06 +00:00
|
|
|
if self.val['append']:
|
|
|
|
a = '+='
|
|
|
|
elif self.val['conditional']:
|
|
|
|
a = '?='
|
|
|
|
else:
|
|
|
|
a = '='
|
|
|
|
flags = []
|
|
|
|
s = str(self.val['flavor'])
|
|
|
|
if s != 'f_bogus':
|
|
|
|
flags.append(s)
|
|
|
|
s = str(self.val['origin'])
|
|
|
|
if s != 'o_default':
|
|
|
|
flags.append(s)
|
|
|
|
s = str(self.val['export'])
|
|
|
|
if s != 'v_default':
|
|
|
|
flags.append(s)
|
|
|
|
return '%s[%s]: "%s" %s "%s"' % (
|
|
|
|
self.val['fileinfo'], ','.join(flags),
|
|
|
|
self.val['name'].string(), a, self.val['value'].string())
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
class HashTablePrinter(object):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Pretty-print a hash table."""
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
DELITEM = None
|
|
|
|
|
|
|
|
def __init__(self, val):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create a HashTablePrinter object."""
|
2019-05-19 15:25:06 +00:00
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def to_string(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Convert a HashTable into a string."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return "size=%d, capacity=%d, empty=%d, collisions=%d, rehashes=%d" % (
|
|
|
|
self.val['ht_size'], self.val['ht_capacity'],
|
|
|
|
self.val['ht_empty_slots'], self.val['ht_collisions'],
|
|
|
|
self.val['ht_rehashes'])
|
|
|
|
|
|
|
|
def children(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Yield each ID and value."""
|
2019-05-19 15:25:06 +00:00
|
|
|
for (i, v) in self.iterator():
|
|
|
|
nm = '[%d] ' % i
|
|
|
|
yield (nm, i)
|
|
|
|
yield (nm, v)
|
|
|
|
|
|
|
|
def iterator(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Provide an iterator for HashTable."""
|
2019-05-19 15:25:06 +00:00
|
|
|
if HashTablePrinter.DELITEM is None:
|
|
|
|
HashTablePrinter.DELITEM = gdb.lookup_global_symbol('hash_deleted_item').value()
|
|
|
|
lst = self.val['ht_vec']
|
2021-07-25 21:19:09 +00:00
|
|
|
for i in range(0, self.val['ht_size']):
|
2019-05-19 15:25:06 +00:00
|
|
|
v = lst[i]
|
2021-07-25 21:19:09 +00:00
|
|
|
if int(v) != 0 and v != HashTablePrinter.DELITEM:
|
2019-05-19 15:25:06 +00:00
|
|
|
yield (i, v)
|
|
|
|
|
|
|
|
def display_hint(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Show the display hint for the pretty-printer."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return 'map'
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
class VariableSetPrinter(object):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Print a variable_set."""
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
def __init__(self, val):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create a variable_set pretty-printer."""
|
2019-05-19 15:25:06 +00:00
|
|
|
self.tbl = HashTablePrinter(val['table'])
|
|
|
|
|
|
|
|
def to_string(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Convert a variable_set to string."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return self.tbl.to_string()
|
|
|
|
|
|
|
|
def children(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Iterate through variables and values."""
|
2019-05-19 15:25:06 +00:00
|
|
|
for (i, v) in self.tbl.iterator():
|
|
|
|
ptr = v.cast(getType('struct variable*'))
|
|
|
|
nm = '[%d] ' % (i)
|
|
|
|
yield (nm, ptr)
|
|
|
|
yield (nm, str(ptr.dereference()))
|
|
|
|
|
|
|
|
def display_hint(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Show the display hint for the pretty-printer."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return 'map'
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
class VariableSetListPrinter(object):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Print a variable_set_list."""
|
2019-05-19 15:25:06 +00:00
|
|
|
|
|
|
|
GLOBALSET = None
|
|
|
|
|
|
|
|
def __init__(self, val):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Create a variable_set_list pretty-printer."""
|
2019-05-19 15:25:06 +00:00
|
|
|
self.val = val
|
|
|
|
|
|
|
|
def to_string(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Convert a variable_set_list to string."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return str(self.val.address)
|
|
|
|
|
|
|
|
def children(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Iterate through variables and values."""
|
2019-05-19 15:25:06 +00:00
|
|
|
if VariableSetListPrinter.GLOBALSET is None:
|
|
|
|
block = gdb.lookup_global_symbol('init_hash_global_variable_set').symtab.static_block()
|
2021-07-25 21:19:09 +00:00
|
|
|
VariableSetListPrinter.GLOBALSET = gdb.lookup_symbol(
|
|
|
|
'global_variable_set', block)[0].value().address
|
2019-05-19 15:25:06 +00:00
|
|
|
ptr = self.val.address
|
|
|
|
i = 0
|
2021-07-25 21:19:09 +00:00
|
|
|
while not isNullptr(ptr):
|
2019-05-19 15:25:06 +00:00
|
|
|
nm = '[%d] ' % (i)
|
|
|
|
yield (nm, ptr['set'])
|
2021-07-25 21:19:09 +00:00
|
|
|
if int(ptr['set']) == int(VariableSetListPrinter.GLOBALSET):
|
2019-05-19 15:25:06 +00:00
|
|
|
yield (nm, "global_variable_set")
|
|
|
|
else:
|
|
|
|
yield (nm, str(ptr['set'].dereference()))
|
|
|
|
i += 1
|
|
|
|
ptr = ptr['next']
|
|
|
|
|
|
|
|
def display_hint(self):
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Show the display hint for the pretty-printer."""
|
2019-05-19 15:25:06 +00:00
|
|
|
return 'map'
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
def build_pretty_printer():
|
2021-07-25 21:19:09 +00:00
|
|
|
"""Install all the pretty-printers."""
|
2019-05-19 15:25:06 +00:00
|
|
|
pp = gdb.printing.RegexpCollectionPrettyPrinter("gnumake")
|
|
|
|
pp.add_printer('floc', r'^floc$', FileLocation)
|
2021-07-25 21:19:09 +00:00
|
|
|
pp.add_printer('stringlist', r'^stringlist$', StringListPrinter)
|
2019-05-19 15:25:06 +00:00
|
|
|
pp.add_printer('variable', r'^variable$', VariablePrinter)
|
|
|
|
pp.add_printer('hashtable', r'^hash_table$', HashTablePrinter)
|
|
|
|
pp.add_printer('variableset', r'^variable_set$', VariableSetPrinter)
|
|
|
|
pp.add_printer('variablesetlist', r'^variable_set_list$', VariableSetListPrinter)
|
|
|
|
return pp
|
|
|
|
|
2021-07-25 21:19:09 +00:00
|
|
|
|
2019-05-19 15:25:06 +00:00
|
|
|
# Use replace=True so we can re-source this file
|
|
|
|
gdb.printing.register_pretty_printer(gdb.current_objfile(),
|
|
|
|
build_pretty_printer(), replace=True)
|