]> git.cworth.org Git - apitrace/blob - trace.py
More efficient call 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         print 'static const char * __%s_args[] = {' % (function.name)
287         for arg in function.args:
288             print '   "%s",' % (arg.name,)
289         print '};'
290         print
291         print 'static const Trace::FunctionSig __%s_sig = {' % (function.name)
292         print '   %u, "%s", %u, __%s_args' % (int(function.id), function.name, len(function.args), function.name)
293         print '};'
294         print
295
296
297     def trace_function_fail(self, function):
298         if function.fail is not None:
299             if function.type is stdapi.Void:
300                 assert function.fail == ''
301                 print '            return;' 
302             else:
303                 assert function.fail != ''
304                 print '            return %s;' % function.fail
305         else:
306             print '            Trace::Abort();'
307
308     def get_function_address(self, function):
309         raise NotImplementedError
310
311     def _get_true_pointer(self, function):
312         ptype = self.function_pointer_type(function)
313         pvalue = self.function_pointer_value(function)
314         print '    if(!%s) {' % (pvalue,)
315         print '        %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
316         print '        if(!%s)' % (pvalue,)
317         self.trace_function_fail(function)
318         print '    }'
319
320     def trace_function_impl(self, function):
321         pvalue = self.function_pointer_value(function)
322         print function.prototype() + ' {'
323         if function.type is stdapi.Void:
324             result = ''
325         else:
326             print '    %s __result;' % function.type
327             result = '__result = '
328         self._get_true_pointer(function)
329         print '    unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name)
330         for arg in function.args:
331             if not arg.output:
332                 self.unwrap_arg(function, arg)
333                 self.dump_arg(function, arg)
334         print '    Trace::EndEnter();'
335         print '    %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
336         print '    Trace::BeginLeave(__call);'
337         for arg in function.args:
338             if arg.output:
339                 self.dump_arg(function, arg)
340                 self.wrap_arg(function, arg)
341         if function.type is not stdapi.Void:
342             self.dump_ret(function, "__result")
343         print '    Trace::EndLeave();'
344         if function.type is not stdapi.Void:
345             self.wrap_ret(function, "__result")
346             print '    return __result;'
347         print '}'
348         print
349
350     def dump_arg(self, function, arg):
351         print '    Trace::BeginArg(%u);' % (arg.index,)
352         dump_instance(arg.type, arg.name)
353         print '    Trace::EndArg();'
354
355     def wrap_arg(self, function, arg):
356         wrap_instance(arg.type, arg.name)
357
358     def unwrap_arg(self, function, arg):
359         unwrap_instance(arg.type, arg.name)
360
361     def dump_ret(self, function, instance):
362         print '    Trace::BeginReturn();'
363         dump_instance(function.type, instance)
364         print '    Trace::EndReturn();'
365
366     def wrap_ret(self, function, instance):
367         wrap_instance(function.type, instance)
368
369     def unwrap_ret(self, function, instance):
370         unwrap_instance(function.type, instance)
371
372     def interface_wrap_name(self, interface):
373         return "Wrap" + interface.expr
374
375     def interface_pre_decl(self, interface):
376         print "class %s;" % interface.wrap_name()
377
378     def interface_decl(self, interface):
379         print "class %s : public %s " % (interface.wrap_name(), interface.name)
380         print "{"
381         print "public:"
382         print "    %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
383         print "    virtual ~%s();" % interface.wrap_name()
384         print
385         for method in interface.itermethods():
386             print "    " + method.prototype() + ";"
387         print
388         #print "private:"
389         print "    %s * m_pInstance;" % (interface.name,)
390         print "};"
391         print
392
393     def interface_wrap_impl(self, interface):
394         print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
395         print '    m_pInstance = pInstance;'
396         print '}'
397         print
398         print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
399         print '}'
400         print
401         for method in interface.itermethods():
402             self.trace_method(interface, method)
403         print
404
405     def trace_method(self, interface, method):
406         print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
407         if method.type is Void:
408             result = ''
409         else:
410             print '    %s __result;' % method.type
411             result = '__result = '
412         print '    Trace::BeginCall("%s");' % (interface.name + '::' + method.name)
413         print '    Trace::BeginArg(0);'
414         print '    Trace::LiteralOpaque((const void *)m_pInstance);'
415         print '    Trace::EndArg();'
416         for arg in method.args:
417             if not arg.output:
418                 self.unwrap_arg(method, arg)
419                 self.dump_arg(method, arg)
420         print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
421         for arg in method.args:
422             if arg.output:
423                 self.dump_arg(method, arg)
424                 self.wrap_arg(method, arg)
425         if method.type is not Void:
426             print '    Trace::BeginReturn("%s");' % method.type
427             dump_instance(method.type, "__result")
428             print '    Trace::EndReturn();'
429             wrap_instance(method.type, '__result')
430         print '    Trace::EndCall();'
431         if method.name == 'QueryInterface':
432             print '    if (*ppvObj == m_pInstance)'
433             print '        *ppvObj = this;'
434         if method.name == 'Release':
435             assert method.type is not Void
436             print '    if (!__result)'
437             print '        delete this;'
438         if method.type is not Void:
439             print '    return __result;'
440         print '}'
441         print
442
443