]> git.cworth.org Git - apitrace/blob - trace.py
More efficient bitmask representation.
[apitrace] / trace.py
1 ##########################################################################
2 #
3 # Copyright 2008-2010 VMware, Inc.
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26 """C basic types"""
27
28
29 import stdapi
30
31
32 all_types = {}
33
34
35 class DumpDeclarator(stdapi.OnceVisitor):
36     '''Declare helper functions to dump complex types.'''
37
38     def visit_void(self, literal):
39         pass
40
41     def visit_literal(self, literal):
42         pass
43
44     def visit_string(self, string):
45         pass
46
47     def visit_const(self, const):
48         self.visit(const.type)
49
50     def visit_struct(self, struct):
51         for type, name in struct.members:
52             self.visit(type)
53         print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr)
54         print '    Trace::BeginStruct(%u);' % len(struct.members)
55         for type, name in struct.members:
56             print '    Trace::BeginMember("%s");' % (name,)
57             dump_instance(type, 'value.%s' % (name,))
58             print '    Trace::EndMember();'
59         print '    Trace::EndStruct();'
60         print '}'
61         print
62
63     def visit_array(self, array):
64         self.visit(array.type)
65
66     def visit_blob(self, array):
67         pass
68
69     def visit_enum(self, enum):
70         print 'static void __traceEnum%s(const %s value) {' % (enum.id, enum.expr)
71         print '    switch(value) {'
72         for value in enum.values:
73             print '    case %s:' % value
74             print '        Trace::LiteralNamedConstant("%s", %s);' % (value, value)
75             print '        break;'
76         print '    default:'
77         print '        Trace::LiteralSInt(value);'
78         print '        break;'
79         print '    }'
80         print '}'
81         print
82
83     def visit_bitmask(self, bitmask):
84         print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
85         for value in bitmask.values:
86             print '   {"%s", %s},' % (value, value)
87         print '};'
88         print
89         print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
90         print '   %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
91         print '};'
92         print
93
94     def visit_pointer(self, pointer):
95         self.visit(pointer.type)
96
97     def visit_handle(self, handle):
98         self.visit(handle.type)
99
100     def visit_alias(self, alias):
101         self.visit(alias.type)
102
103     def visit_opaque(self, opaque):
104         pass
105
106     def visit_interface(self, interface):
107         pass
108
109
110 class DumpImplementer(stdapi.Visitor):
111     '''Dump an instance.'''
112
113     def visit_literal(self, literal, instance):
114         print '    Trace::Literal%s(%s);' % (literal.format, instance)
115
116     def visit_string(self, string, instance):
117         if string.length is not None:
118             print '    Trace::LiteralString((const char *)%s, %s);' % (instance, string.length)
119         else:
120             print '    Trace::LiteralString((const char *)%s);' % instance
121
122     def visit_const(self, const, instance):
123         self.visit(const.type, instance)
124
125     def visit_struct(self, struct, instance):
126         print '    __traceStruct%s(%s);' % (struct.id, instance)
127
128     def visit_array(self, array, instance):
129         print '    if(%s) {' % instance
130         index = '__i' + array.type.id
131         print '        Trace::BeginArray(%s);' % (array.length,)
132         print '        for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
133         print '            Trace::BeginElement();'
134         self.visit(array.type, '(%s)[%s]' % (instance, index))
135         print '            Trace::EndElement();'
136         print '        }'
137         print '        Trace::EndArray();'
138         print '    }'
139         print '    else'
140         print '        Trace::LiteralNull();'
141
142     def visit_blob(self, blob, instance):
143         print '    Trace::LiteralBlob(%s, %s);' % (instance, blob.size)
144
145     def visit_enum(self, enum, instance):
146         print '    __traceEnum%s(%s);' % (enum.id, instance)
147
148     def visit_bitmask(self, bitmask, instance):
149         print '    Trace::LiteralBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
150
151     def visit_pointer(self, pointer, instance):
152         print '    if(%s) {' % instance
153         print '        Trace::BeginArray(1);'
154         print '        Trace::BeginElement();'
155         dump_instance(pointer.type, "*" + instance)
156         print '        Trace::EndElement();'
157         print '        Trace::EndArray();'
158         print '    }'
159         print '    else'
160         print '        Trace::LiteralNull();'
161
162     def visit_handle(self, handle, instance):
163         self.visit(handle.type, instance)
164
165     def visit_alias(self, alias, instance):
166         self.visit(alias.type, instance)
167
168     def visit_opaque(self, opaque, instance):
169         print '    Trace::LiteralOpaque((const void *)%s);' % instance
170
171     def visit_interface(self, interface, instance):
172         print '    Trace::LiteralOpaque((const void *)%s);' % instance
173
174
175 dump_instance = DumpImplementer().visit
176
177
178
179 class Wrapper(stdapi.Visitor):
180     '''Wrap an instance.'''
181
182     def visit_void(self, type, instance):
183         raise NotImplementedError
184
185     def visit_literal(self, type, instance):
186         pass
187
188     def visit_string(self, type, instance):
189         pass
190
191     def visit_const(self, type, instance):
192         pass
193
194     def visit_struct(self, struct, instance):
195         for type, name in struct.members:
196             self.visit(type, "(%s).%s" % (instance, name))
197
198     def visit_array(self, array, instance):
199         # XXX: actually it is possible to return an array of pointers
200         pass
201
202     def visit_blob(self, blob, instance):
203         pass
204
205     def visit_enum(self, enum, instance):
206         pass
207
208     def visit_bitmask(self, bitmask, instance):
209         pass
210
211     def visit_pointer(self, pointer, instance):
212         self.visit(pointer.type, "*" + instance)
213
214     def visit_handle(self, handle, instance):
215         self.visit(handle.type, instance)
216
217     def visit_alias(self, alias, instance):
218         self.visit(alias.type, instance)
219
220     def visit_opaque(self, opaque, instance):
221         pass
222     
223     def visit_interface(self, interface, instance):
224         print "    if(%s)" % instance
225         print "        %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
226
227
228 class Unwrapper(Wrapper):
229
230     def visit_interface(self, interface, instance):
231         print "    if(%s)" % instance
232         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
233
234 wrap_instance = Wrapper().visit
235 unwrap_instance = Unwrapper().visit
236
237
238 class Tracer:
239
240     def trace_api(self, api):
241         self.header(api)
242
243         # Includes
244         for header in api.headers:
245             print header
246         print
247
248         # Type dumpers
249         types = api.all_types()
250         visitor = DumpDeclarator()
251         map(visitor.visit, types)
252         print
253
254         # Interfaces wrapers
255         map(self.interface_wrap_name, api.interfaces)
256         map(self.interface_pre_decl, api.interfaces)
257         map(self.interface_decl, api.interfaces)
258         map(self.interface_wrap_impl, api.interfaces)
259         print
260
261         # Function wrappers
262         map(self.trace_function_decl, api.functions)
263         map(self.trace_function_impl, api.functions)
264         print
265
266         self.footer(api)
267
268     def header(self, api):
269         pass
270
271     def footer(self, api):
272         pass
273
274     def function_pointer_type(self, function):
275         return 'P' + function.name
276
277     def function_pointer_value(self, function):
278         return 'p' + function.name
279
280     def trace_function_decl(self, function):
281         ptype = self.function_pointer_type(function)
282         pvalue = self.function_pointer_value(function)
283         print 'typedef ' + function.prototype('* %s' % ptype) + ';'
284         print 'static %s %s = NULL;' % (ptype, pvalue)
285         print
286
287     def trace_function_fail(self, function):
288         if function.fail is not None:
289             if function.type is stdapi.Void:
290                 assert function.fail == ''
291                 print '            return;' 
292             else:
293                 assert function.fail != ''
294                 print '            return %s;' % function.fail
295         else:
296             print '            Trace::Abort();'
297
298     def get_function_address(self, function):
299         raise NotImplementedError
300
301     def _get_true_pointer(self, function):
302         ptype = self.function_pointer_type(function)
303         pvalue = self.function_pointer_value(function)
304         print '    if(!%s) {' % (pvalue,)
305         print '        %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
306         print '        if(!%s)' % (pvalue,)
307         self.trace_function_fail(function)
308         print '    }'
309
310     def trace_function_impl(self, function):
311         pvalue = self.function_pointer_value(function)
312         print function.prototype() + ' {'
313         if function.type is stdapi.Void:
314             result = ''
315         else:
316             print '    %s __result;' % function.type
317             result = '__result = '
318         self._get_true_pointer(function)
319         print '    unsigned __call = Trace::BeginEnter("%s");' % (function.name)
320         for arg in function.args:
321             if not arg.output:
322                 self.unwrap_arg(function, arg)
323                 self.dump_arg(function, arg)
324         print '    Trace::EndEnter();'
325         print '    %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
326         print '    Trace::BeginLeave(__call);'
327         for arg in function.args:
328             if arg.output:
329                 self.dump_arg(function, arg)
330                 self.wrap_arg(function, arg)
331         if function.type is not stdapi.Void:
332             self.dump_ret(function, "__result")
333         print '    Trace::EndLeave();'
334         if function.type is not stdapi.Void:
335             self.wrap_ret(function, "__result")
336             print '    return __result;'
337         print '}'
338         print
339
340     def dump_arg(self, function, arg):
341         print '    Trace::BeginArg(%u, "%s");' % (arg.index, arg.name,)
342         dump_instance(arg.type, arg.name)
343         print '    Trace::EndArg();'
344
345     def wrap_arg(self, function, arg):
346         wrap_instance(arg.type, arg.name)
347
348     def unwrap_arg(self, function, arg):
349         unwrap_instance(arg.type, arg.name)
350
351     def dump_ret(self, function, instance):
352         print '    Trace::BeginReturn();'
353         dump_instance(function.type, instance)
354         print '    Trace::EndReturn();'
355
356     def wrap_ret(self, function, instance):
357         wrap_instance(function.type, instance)
358
359     def unwrap_ret(self, function, instance):
360         unwrap_instance(function.type, instance)
361
362     def interface_wrap_name(self, interface):
363         return "Wrap" + interface.expr
364
365     def interface_pre_decl(self, interface):
366         print "class %s;" % interface.wrap_name()
367
368     def interface_decl(self, interface):
369         print "class %s : public %s " % (interface.wrap_name(), interface.name)
370         print "{"
371         print "public:"
372         print "    %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
373         print "    virtual ~%s();" % interface.wrap_name()
374         print
375         for method in interface.itermethods():
376             print "    " + method.prototype() + ";"
377         print
378         #print "private:"
379         print "    %s * m_pInstance;" % (interface.name,)
380         print "};"
381         print
382
383     def interface_wrap_impl(self, interface):
384         print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
385         print '    m_pInstance = pInstance;'
386         print '}'
387         print
388         print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
389         print '}'
390         print
391         for method in interface.itermethods():
392             self.trace_method(interface, method)
393         print
394
395     def trace_method(self, interface, method):
396         print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
397         if method.type is Void:
398             result = ''
399         else:
400             print '    %s __result;' % method.type
401             result = '__result = '
402         print '    Trace::BeginCall("%s");' % (interface.name + '::' + method.name)
403         print '    Trace::BeginArg(0, "this");'
404         print '    Trace::LiteralOpaque((const void *)m_pInstance);'
405         print '    Trace::EndArg();'
406         for arg in method.args:
407             if not arg.output:
408                 self.unwrap_arg(method, arg)
409                 self.dump_arg(method, arg)
410         print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
411         for arg in method.args:
412             if arg.output:
413                 self.dump_arg(method, arg)
414                 self.wrap_arg(method, arg)
415         if method.type is not Void:
416             print '    Trace::BeginReturn("%s");' % method.type
417             dump_instance(method.type, "__result")
418             print '    Trace::EndReturn();'
419             wrap_instance(method.type, '__result')
420         print '    Trace::EndCall();'
421         if method.name == 'QueryInterface':
422             print '    if (*ppvObj == m_pInstance)'
423             print '        *ppvObj = this;'
424         if method.name == 'Release':
425             assert method.type is not Void
426             print '    if (!__result)'
427             print '        delete this;'
428         if method.type is not Void:
429             print '    return __result;'
430         print '}'
431         print
432
433