]> git.cworth.org Git - apitrace/blob - trace.py
8e2e2af3e061cc41e6d9145d1b12d754947eadaf
[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 base
30
31
32 all_types = {}
33
34
35 class DumpDeclarator(base.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 '    Log::BeginStruct("%s");' % struct.name
55         for type, name in struct.members:
56             print '    Log::BeginMember("%s");' % (name,)
57             dump_instance(type, 'value.%s' % (name,))
58             print '    Log::EndMember();'
59         print '    Log::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 '        Log::LiteralNamedConstant("%s", %s);' % (value, value)
75             print '        break;'
76         print '    default:'
77         print '        Log::LiteralSInt(value);'
78         print '        break;'
79         print '    }'
80         print '}'
81         print
82
83     def visit_bitmask(self, bitmask):
84         print 'static void __traceBitmask%s(%s value) {' % (bitmask.id, bitmask.type)
85         print '    Log::BeginBitmask();'
86         for value in bitmask.values:
87             print '    if((value & %s) == %s) {' % (value, value)
88             print '        Log::LiteralNamedConstant("%s", %s);' % (value, value)
89             print '        value &= ~%s;' % value
90             print '    }'
91         print '    if(value) {'
92         dump_instance(bitmask.type, "value");
93         print '    }'
94         print '    Log::EndBitmask();'
95         print '}'
96         print
97
98     def visit_pointer(self, pointer):
99         self.visit(pointer.type)
100
101     def visit_handle(self, handle):
102         self.visit(handle.type)
103
104     def visit_alias(self, alias):
105         self.visit(alias.type)
106
107     def visit_opaque(self, opaque):
108         pass
109
110     def visit_interface(self, interface):
111         pass
112
113
114 class DumpImplementer(base.Visitor):
115     '''Dump an instance.'''
116
117     def visit_literal(self, literal, instance):
118         print '    Log::Literal%s(%s);' % (literal.format, instance)
119
120     def visit_string(self, string, instance):
121         if string.length is not None:
122             print '    Log::LiteralString((const char *)%s, %s);' % (instance, string.length)
123         else:
124             print '    Log::LiteralString((const char *)%s);' % instance
125
126     def visit_const(self, const, instance):
127         self.visit(const.type, instance)
128
129     def visit_struct(self, struct, instance):
130         print '    __traceStruct%s(%s);' % (struct.id, instance)
131
132     def visit_array(self, array, instance):
133         print '    if(%s) {' % instance
134         index = '__i' + array.type.id
135         print '        Log::BeginArray(%s);' % (array.length,)
136         print '        for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
137         print '            Log::BeginElement();'
138         self.visit(array.type, '(%s)[%s]' % (instance, index))
139         print '            Log::EndElement();'
140         print '        }'
141         print '        Log::EndArray();'
142         print '    }'
143         print '    else'
144         print '        Log::LiteralNull();'
145
146     def visit_blob(self, blob, instance):
147         print '    Log::LiteralBlob(%s, %s);' % (instance, blob.size)
148
149     def visit_enum(self, enum, instance):
150         print '    __traceEnum%s(%s);' % (enum.id, instance)
151
152     def visit_bitmask(self, bitmask, instance):
153         print '    __traceBitmask%s(%s);' % (bitmask.id, instance)
154
155     def visit_pointer(self, pointer, instance):
156         print '    if(%s) {' % instance
157         print '        Log::BeginArray(1);'
158         print '        Log::BeginElement();'
159         dump_instance(pointer.type, "*" + instance)
160         print '        Log::EndElement();'
161         print '        Log::EndArray();'
162         print '    }'
163         print '    else'
164         print '        Log::LiteralNull();'
165
166     def visit_handle(self, handle, instance):
167         self.visit(handle.type, instance)
168
169     def visit_alias(self, alias, instance):
170         self.visit(alias.type, instance)
171
172     def visit_opaque(self, opaque, instance):
173         print '    Log::LiteralOpaque((const void *)%s);' % instance
174
175     def visit_interface(self, interface, instance):
176         print '    Log::LiteralOpaque((const void *)%s);' % instance
177
178
179 dump_instance = DumpImplementer().visit
180
181
182
183 class Wrapper(base.Visitor):
184     '''Wrap an instance.'''
185
186     def visit_void(self, type, instance):
187         raise NotImplementedError
188
189     def visit_literal(self, type, instance):
190         pass
191
192     def visit_string(self, type, instance):
193         pass
194
195     def visit_const(self, type, instance):
196         pass
197
198     def visit_struct(self, struct, instance):
199         for type, name in struct.members:
200             self.visit(type, "(%s).%s" % (instance, name))
201
202     def visit_array(self, array, instance):
203         # XXX: actually it is possible to return an array of pointers
204         pass
205
206     def visit_blob(self, blob, instance):
207         pass
208
209     def visit_enum(self, enum, instance):
210         pass
211
212     def visit_bitmask(self, bitmask, instance):
213         pass
214
215     def visit_pointer(self, pointer, instance):
216         self.visit(pointer.type, "*" + instance)
217
218     def visit_handle(self, handle, instance):
219         self.visit(handle.type, instance)
220
221     def visit_alias(self, alias, instance):
222         self.visit(alias.type, instance)
223
224     def visit_opaque(self, opaque, instance):
225         pass
226     
227     def visit_interface(self, interface, instance):
228         print "    if(%s)" % instance
229         print "        %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
230
231
232 class Unwrapper(Wrapper):
233
234     def visit_interface(self, interface, instance):
235         print "    if(%s)" % instance
236         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
237
238 wrap_instance = Wrapper().visit
239 unwrap_instance = Unwrapper().visit
240
241
242 class Tracer:
243
244     def trace_api(self, api):
245         self.header(api)
246
247         # Includes
248         for header in api.headers:
249             print header
250         print
251
252         # Type dumpers
253         types = api.all_types()
254         visitor = DumpDeclarator()
255         map(visitor.visit, types)
256         print
257
258         # Interfaces wrapers
259         map(self.interface_wrap_name, api.interfaces)
260         map(self.interface_pre_decl, api.interfaces)
261         map(self.interface_decl, api.interfaces)
262         map(self.interface_wrap_impl, api.interfaces)
263         print
264
265         # Function wrappers
266         map(self.trace_function_decl, api.functions)
267         map(self.trace_function_impl, api.functions)
268         print
269
270         self.footer(api)
271
272     def header(self, api):
273         pass
274
275     def footer(self, api):
276         pass
277
278     def function_pointer_type(self, function):
279         return 'P' + function.name
280
281     def function_pointer_value(self, function):
282         return 'p' + function.name
283
284     def trace_function_decl(self, function):
285         ptype = self.function_pointer_type(function)
286         pvalue = self.function_pointer_value(function)
287         print 'typedef ' + function.prototype('* %s' % ptype) + ';'
288         print 'static %s %s = NULL;' % (ptype, pvalue)
289         print
290
291     def trace_function_fail(self, function):
292         if function.fail is not None:
293             if function.type is base.Void:
294                 assert function.fail == ''
295                 print '            return;' 
296             else:
297                 assert function.fail != ''
298                 print '            return %s;' % function.fail
299         else:
300             print '            Log::Abort();'
301
302     def get_function_address(self, function):
303         raise NotImplementedError
304
305     def _get_true_pointer(self, function):
306         ptype = self.function_pointer_type(function)
307         pvalue = self.function_pointer_value(function)
308         print '    if(!%s) {' % (pvalue,)
309         print '        %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
310         print '        if(!%s)' % (pvalue,)
311         self.trace_function_fail(function)
312         print '    }'
313
314     def trace_function_impl(self, function):
315         pvalue = self.function_pointer_value(function)
316         print function.prototype() + ' {'
317         if function.type is base.Void:
318             result = ''
319         else:
320             print '    %s __result;' % function.type
321             result = '__result = '
322         self._get_true_pointer(function)
323         print '    Log::BeginCall("%s");' % (function.name)
324         for arg in function.args:
325             if not arg.output:
326                 self.unwrap_arg(function, arg)
327                 self.dump_arg(function, arg)
328         print '    %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
329         for arg in function.args:
330             if arg.output:
331                 self.dump_arg(function, arg)
332                 self.wrap_arg(function, arg)
333         if function.type is not base.Void:
334             self.dump_ret(function, "__result")
335         print '    Log::EndCall();'
336         if function.type is not base.Void:
337             self.wrap_ret(function, "__result")
338             print '    return __result;'
339         print '}'
340         print
341
342     def dump_arg(self, function, arg):
343         print '    Log::BeginArg("%s");' % (arg.name,)
344         dump_instance(arg.type, arg.name)
345         print '    Log::EndArg();'
346
347     def wrap_arg(self, function, arg):
348         wrap_instance(arg.type, arg.name)
349
350     def unwrap_arg(self, function, arg):
351         unwrap_instance(arg.type, arg.name)
352
353     def dump_ret(self, function, instance):
354         print '    Log::BeginReturn();'
355         dump_instance(function.type, instance)
356         print '    Log::EndReturn();'
357
358     def wrap_ret(self, function, instance):
359         wrap_instance(function.type, instance)
360
361     def unwrap_ret(self, function, instance):
362         unwrap_instance(function.type, instance)
363
364     def interface_wrap_name(self, interface):
365         return "Wrap" + interface.expr
366
367     def interface_pre_decl(self, interface):
368         print "class %s;" % interface.wrap_name()
369
370     def interface_decl(self, interface):
371         print "class %s : public %s " % (interface.wrap_name(), interface.name)
372         print "{"
373         print "public:"
374         print "    %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
375         print "    virtual ~%s();" % interface.wrap_name()
376         print
377         for method in interface.itermethods():
378             print "    " + method.prototype() + ";"
379         print
380         #print "private:"
381         print "    %s * m_pInstance;" % (interface.name,)
382         print "};"
383         print
384
385     def interface_wrap_impl(self, interface):
386         print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
387         print '    m_pInstance = pInstance;'
388         print '}'
389         print
390         print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
391         print '}'
392         print
393         for method in interface.itermethods():
394             self.trace_method(interface, method)
395         print
396
397     def trace_method(self, interface, method):
398         print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
399         if method.type is Void:
400             result = ''
401         else:
402             print '    %s __result;' % method.type
403             result = '__result = '
404         print '    Log::BeginCall("%s");' % (interface.name + '::' + method.name)
405         print '    Log::BeginArg("this");'
406         print '    Log::LiteralOpaque((const void *)m_pInstance);'
407         print '    Log::EndArg();'
408         for arg in method.args:
409             if not arg.output:
410                 self.unwrap_arg(method, arg)
411                 self.dump_arg(method, arg)
412         print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
413         for arg in method.args:
414             if arg.output:
415                 self.dump_arg(method, arg)
416                 self.wrap_arg(method, arg)
417         if method.type is not Void:
418             print '    Log::BeginReturn("%s");' % method.type
419             dump_instance(method.type, "__result")
420             print '    Log::EndReturn();'
421             wrap_instance(method.type, '__result')
422         print '    Log::EndCall();'
423         if method.name == 'QueryInterface':
424             print '    if (*ppvObj == m_pInstance)'
425             print '        *ppvObj = this;'
426         if method.name == 'Release':
427             assert method.type is not Void
428             print '    if (!__result)'
429             print '        delete this;'
430         if method.type is not Void:
431             print '    return __result;'
432         print '}'
433         print
434
435