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