print.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ###############################################################################
  2. # Name: misc/gdb/print.py
  3. # Purpose: pretty-printers for wx data structures: this file is meant to
  4. # be sourced from gdb using "source -p" (or, better, autoloaded
  5. # in the future...)
  6. # Author: Vadim Zeitlin
  7. # Created: 2009-01-04
  8. # Copyright: (c) 2009 Vadim Zeitlin
  9. # Licence: wxWindows licence
  10. ###############################################################################
  11. # Define wxFooPrinter class implementing (at least) to_string() method for each
  12. # wxFoo class we want to pretty print. Then just add wxFoo to the types array
  13. # in wxLookupFunction at the bottom of this file.
  14. import datetime
  15. # shamelessly stolen from std::string example
  16. class wxStringPrinter:
  17. def __init__(self, val):
  18. self.val = val
  19. def to_string(self):
  20. return self.val['m_impl']['_M_dataplus']['_M_p']
  21. def display_hint(self):
  22. return 'string'
  23. class wxDateTimePrinter:
  24. def __init__(self, val):
  25. self.val = val
  26. def to_string(self):
  27. # A value of type wxLongLong can't be used in Python arithmetic
  28. # expressions directly so we need to convert it to long long first and
  29. # then cast to int explicitly to be able to use it as a timestamp.
  30. msec = self.val['m_time'].cast(gdb.lookup_type('long long'))
  31. if msec == 0x8000000000000000:
  32. return 'NONE'
  33. sec = int(msec / 1000)
  34. return datetime.datetime.fromtimestamp(sec).isoformat(' ')
  35. class wxFileNamePrinter:
  36. def __init__(self, val):
  37. self.val = val
  38. def to_string(self):
  39. # It is simpler to just call the internal function here than to iterate
  40. # over m_dirs array ourselves. The disadvantage of this approach is
  41. # that it requires a live inferior process and so doesn't work when
  42. # debugging using only a core file. If this ever becomes a serious
  43. # problem, this should be rewritten to use m_dirs and m_name and m_ext.
  44. return gdb.parse_and_eval('((wxFileName*)%s)->GetFullPath(0)' %
  45. self.val.address)
  46. class wxXYPrinterBase:
  47. def __init__(self, val):
  48. self.x = val['x']
  49. self.y = val['y']
  50. class wxPointPrinter(wxXYPrinterBase):
  51. def to_string(self):
  52. return '(%d, %d)' % (self.x, self.y)
  53. class wxSizePrinter(wxXYPrinterBase):
  54. def to_string(self):
  55. return '%d*%d' % (self.x, self.y)
  56. class wxRectPrinter(wxXYPrinterBase):
  57. def __init__(self, val):
  58. wxXYPrinterBase.__init__(self, val)
  59. self.width = val['width']
  60. self.height = val['height']
  61. def to_string(self):
  62. return '(%d, %d) %d*%d' % (self.x, self.y, self.width, self.height)
  63. # The function looking up the pretty-printer to use for the given value.
  64. def wxLookupFunction(val):
  65. # Using a list is probably ok for so few items but consider switching to a
  66. # set (or a dict and cache class types as the keys in it?) if needed later.
  67. types = ['wxString',
  68. 'wxDateTime',
  69. 'wxFileName',
  70. 'wxPoint',
  71. 'wxSize',
  72. 'wxRect']
  73. for t in types:
  74. if val.type.tag == t:
  75. # Not sure if this is the best name to create the object of a class
  76. # by name but at least it beats eval()
  77. return globals()[t + 'Printer'](val)
  78. return None
  79. gdb.pretty_printers.append(wxLookupFunction)