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