2 ##########################################################################
4 # Copyright 2008-2009 VMware, Inc.
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ##########################################################################/
28 '''Trace data model.'''
36 from cStringIO import StringIO
38 from StringIO import StringIO
43 def visit(self, visitor):
44 raise NotImplementedError
48 formatter = format.DefaultFormatter(stream)
49 pretty_printer = PrettyPrinter(formatter)
50 self.visit(pretty_printer)
51 return stream.getvalue()
56 def __init__(self, value):
59 def visit(self, visitor):
60 visitor.visit_literal(self)
63 class NamedConstant(Node):
65 def __init__(self, name):
68 def visit(self, visitor):
69 visitor.visit_named_constant(self)
74 def __init__(self, elements):
75 self.elements = elements
77 def visit(self, visitor):
78 visitor.visit_bitmask(self)
83 def __init__(self, elements):
84 self.elements = elements
86 def visit(self, visitor):
87 visitor.visit_array(self)
92 def __init__(self, name, members):
94 self.members = members
96 def visit(self, visitor):
97 visitor.visit_struct(self)
102 def __init__(self, address, value):
103 self.address = address
106 def visit(self, visitor):
107 visitor.visit_pointer(self)
112 def __init__(self, no, name, args, ret, attrs = None):
122 def visit(self, visitor):
123 visitor.visit_call(self)
128 def __init__(self, calls):
131 def visit(self, visitor):
132 visitor.visit_trace(self)
137 def visit_literal(self, node):
138 raise NotImplementedError
140 def visit_named_constant(self, node):
141 raise NotImplementedError
143 def visit_bitmask(self, node):
144 raise NotImplementedError
146 def visit_array(self, node):
147 raise NotImplementedError
149 def visit_struct(self, node):
150 raise NotImplementedError
152 def visit_pointer(self, node):
153 raise NotImplementedError
155 def visit_call(self, node):
156 raise NotImplementedError
158 def visit_trace(self, node):
159 raise NotImplementedError
164 def __init__(self, formatter):
165 self.formatter = formatter
167 def visit_literal(self, node):
168 if isinstance(node.value, basestring):
169 if len(node.value) >= 4096 or node.value.strip(string.printable):
170 self.formatter.text('...')
173 self.formatter.literal('"' + node.value + '"')
176 self.formatter.literal(str(node.value))
178 def visit_named_constant(self, node):
179 self.formatter.literal(node.name)
181 def visit_bitmask(self, node):
182 if len(node.elements) == 0:
183 self.formatter.literal('0')
185 if len(node.elements) > 1:
186 self.formatter.text('(')
188 for value in node.elements:
189 self.formatter.text(sep)
192 if len(node.elements) > 1:
193 self.formatter.text(')')
195 def visit_array(self, node):
196 self.formatter.text('{')
198 for value in node.elements:
199 self.formatter.text(sep)
202 self.formatter.text('}')
204 def visit_struct(self, node):
205 self.formatter.text('{')
207 for name, value in node.members:
208 self.formatter.text(sep)
209 self.formatter.variable(name)
210 self.formatter.text(' = ')
213 self.formatter.text('}')
215 def visit_pointer(self, node):
216 self.formatter.address(node.address)
218 def visit_call(self, node):
219 self.formatter.text('%s ' % node.no)
220 self.formatter.function(node.name)
221 self.formatter.text('(')
223 for name, value in node.args:
224 self.formatter.text(sep)
225 self.formatter.variable(name)
226 self.formatter.text(' = ')
229 self.formatter.text(')')
230 if node.ret is not None:
231 self.formatter.text(' = ')
234 def visit_trace(self, node):
235 for call in node.calls:
237 self.formatter.newline()