status_code_gdb.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import gdb.printing
  2. import gdb
  3. import os
  4. def synthesise_gdb_value_from_string(s):
  5. '''For when you want to return a synthetic string from children()'''
  6. return gdb.Value(s + '\0').cast(gdb.lookup_type('char').pointer())
  7. class StatusCodePrinter(object):
  8. '''Print a system_error2::status_code<T>'''
  9. def __init__(self, val):
  10. self.val = val
  11. def children(self):
  12. s = str(self.val['_domain'])
  13. if 'posix_code_domain' in s or 'generic_code_domain' in s:
  14. yield ('msg', synthesise_gdb_value_from_string(str(self.val['_value']) + ' (' + os.strerror(int(self.val['_value'])) + ')'))
  15. yield ('domain', self.val['_domain'])
  16. yield ('value', self.val['_value'])
  17. def display_hint(self):
  18. return None
  19. def to_string(self):
  20. s = str(self.val['_domain'])
  21. if 'posix_code_domain' in s or 'generic_code_domain' in s:
  22. return str(self.val['_value']) + ' (' + os.strerror(int(self.val['_value'])) + ')'
  23. else:
  24. return self.val['_value']
  25. def build_pretty_printer():
  26. pp = gdb.printing.RegexpCollectionPrettyPrinter('system_error2')
  27. pp.add_printer('system_error2::status_code', '^(boost::)?system_error2::status_code<.*>$', StatusCodePrinter)
  28. return pp
  29. def register_printers(obj = None):
  30. gdb.printing.register_pretty_printer(obj, build_pretty_printer(), replace = True)
  31. register_printers(gdb.current_objfile())