1 ##########################################################################
3 # Copyright 2008-2010 VMware, Inc.
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 ##########################################################################/
26 """Common trace code generation."""
30 from dispatch import Dispatcher
33 def interface_wrap_name(interface):
34 return "Wrap" + interface.expr
37 class DumpDeclarator(stdapi.OnceVisitor):
38 '''Declare helper functions to dump complex types.'''
40 def visit_void(self, literal):
43 def visit_literal(self, literal):
46 def visit_string(self, string):
49 def visit_const(self, const):
50 self.visit(const.type)
52 def visit_struct(self, struct):
53 for type, name in struct.members:
55 print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr)
56 print ' static const char * members[%u] = {' % (len(struct.members),)
57 for type, name, in struct.members:
58 print ' "%s",' % (name,)
60 print ' static const Trace::StructSig sig = {'
61 print ' %u, "%s", %u, members' % (int(struct.id), struct.name, len(struct.members))
63 print ' __writer.beginStruct(&sig);'
64 for type, name in struct.members:
65 dump_instance(type, 'value.%s' % (name,))
66 print ' __writer.endStruct();'
70 def visit_array(self, array):
71 self.visit(array.type)
73 def visit_blob(self, array):
78 def visit_enum(self, enum):
79 print 'static void __traceEnum%s(const %s value) {' % (enum.id, enum.expr)
82 value = enum.values[i]
83 print ' static const Trace::EnumSig sig%u = {%u, "%s", %s};' % (i, DumpDeclarator.__enum_id, value, value)
84 DumpDeclarator.__enum_id += 1
85 print ' const Trace::EnumSig *sig;'
86 print ' switch(value) {'
88 value = enum.values[i]
89 print ' case %s:' % value
90 print ' sig = &sig%u;' % i
93 print ' __writer.writeSInt(value);'
96 print ' __writer.writeEnum(sig);'
100 def visit_bitmask(self, bitmask):
101 print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
102 for value in bitmask.values:
103 print ' {"%s", %s},' % (value, value)
106 print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
107 print ' %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
111 def visit_pointer(self, pointer):
112 self.visit(pointer.type)
114 def visit_handle(self, handle):
115 self.visit(handle.type)
117 def visit_alias(self, alias):
118 self.visit(alias.type)
120 def visit_opaque(self, opaque):
123 def visit_interface(self, interface):
124 print "class %s : public %s " % (interface_wrap_name(interface), interface.name)
127 print " %s(%s * pInstance);" % (interface_wrap_name(interface), interface.name)
128 print " virtual ~%s();" % interface_wrap_name(interface)
130 for method in interface.itermethods():
131 print " " + method.prototype() + ";"
134 print " %s * m_pInstance;" % (interface.name,)
139 class DumpImplementer(stdapi.Visitor):
140 '''Dump an instance.'''
142 def visit_literal(self, literal, instance):
143 print ' __writer.write%s(%s);' % (literal.format, instance)
145 def visit_string(self, string, instance):
146 if string.length is not None:
147 print ' __writer.writeString((const char *)%s, %s);' % (instance, string.length)
149 print ' __writer.writeString((const char *)%s);' % instance
151 def visit_const(self, const, instance):
152 self.visit(const.type, instance)
154 def visit_struct(self, struct, instance):
155 print ' __traceStruct%s(%s);' % (struct.id, instance)
157 def visit_array(self, array, instance):
158 print ' if (%s) {' % instance
159 index = '__i' + array.type.id
160 print ' __writer.beginArray(%s);' % (array.length,)
161 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
162 print ' __writer.beginElement();'
163 self.visit(array.type, '(%s)[%s]' % (instance, index))
164 print ' __writer.endElement();'
166 print ' __writer.endArray();'
169 print ' __writer.writeNull();'
171 def visit_blob(self, blob, instance):
172 print ' __writer.writeBlob(%s, %s);' % (instance, blob.size)
174 def visit_enum(self, enum, instance):
175 print ' __traceEnum%s(%s);' % (enum.id, instance)
177 def visit_bitmask(self, bitmask, instance):
178 print ' __writer.writeBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
180 def visit_pointer(self, pointer, instance):
181 print ' if (%s) {' % instance
182 print ' __writer.beginArray(1);'
183 print ' __writer.beginElement();'
184 dump_instance(pointer.type, "*" + instance)
185 print ' __writer.endElement();'
186 print ' __writer.endArray();'
189 print ' __writer.writeNull();'
191 def visit_handle(self, handle, instance):
192 self.visit(handle.type, instance)
194 def visit_alias(self, alias, instance):
195 self.visit(alias.type, instance)
197 def visit_opaque(self, opaque, instance):
198 print ' __writer.writeOpaque((const void *)%s);' % instance
200 def visit_interface(self, interface, instance):
201 print ' __writer.writeOpaque((const void *)&%s);' % instance
204 dump_instance = DumpImplementer().visit
208 class Wrapper(stdapi.Visitor):
209 '''Wrap an instance.'''
211 def visit_void(self, type, instance):
212 raise NotImplementedError
214 def visit_literal(self, type, instance):
217 def visit_string(self, type, instance):
220 def visit_const(self, type, instance):
223 def visit_struct(self, struct, instance):
224 for type, name in struct.members:
225 self.visit(type, "(%s).%s" % (instance, name))
227 def visit_array(self, array, instance):
228 # XXX: actually it is possible to return an array of pointers
231 def visit_blob(self, blob, instance):
234 def visit_enum(self, enum, instance):
237 def visit_bitmask(self, bitmask, instance):
240 def visit_pointer(self, pointer, instance):
241 print " if (%s) {" % instance
242 self.visit(pointer.type, "*" + instance)
245 def visit_handle(self, handle, instance):
246 self.visit(handle.type, instance)
248 def visit_alias(self, alias, instance):
249 self.visit(alias.type, instance)
251 def visit_opaque(self, opaque, instance):
254 def visit_interface(self, interface, instance):
255 assert instance.startswith('*')
256 instance = instance[1:]
257 print " if (%s) {" % instance
258 print " %s = new %s(%s);" % (instance, interface_wrap_name(interface), instance)
262 class Unwrapper(Wrapper):
264 def visit_interface(self, interface, instance):
265 assert instance.startswith('*')
266 instance = instance[1:]
267 print " if (%s) {" % instance
268 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface_wrap_name(interface), instance)
272 wrap_instance = Wrapper().visit
273 unwrap_instance = Unwrapper().visit
281 def trace_api(self, api):
287 for header in api.headers:
292 types = api.all_types()
293 visitor = DumpDeclarator()
294 map(visitor.visit, types)
298 interfaces = [type for type in types if isinstance(type, stdapi.Interface)]
299 map(self.interface_wrap_impl, interfaces)
303 map(self.trace_function_decl, api.functions)
304 map(self.trace_function_impl, api.functions)
309 def header(self, api):
310 print 'Trace::Writer __writer;'
313 def footer(self, api):
316 def trace_function_decl(self, function):
317 # Per-function declarations
320 print 'static const char * __%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args]))
322 print 'static const char ** __%s_args = NULL;' % (function.name,)
323 print 'static const Trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, int(function.id), function.name, len(function.args), function.name)
326 def get_dispatch_function(self, function):
327 return '__' + function.name
329 def is_public_function(self, function):
332 def trace_function_impl(self, function):
333 if self.is_public_function(function):
334 print 'extern "C" PUBLIC'
336 print 'extern "C" PRIVATE'
337 print function.prototype() + ' {'
338 if function.type is not stdapi.Void:
339 print ' %s __result;' % function.type
340 self.trace_function_impl_body(function)
341 if function.type is not stdapi.Void:
342 self.wrap_ret(function, "__result")
343 print ' return __result;'
347 def trace_function_impl_body(self, function):
348 print ' unsigned __call = __writer.beginEnter(__%s_sig);' % (function.name,)
349 for arg in function.args:
351 self.unwrap_arg(function, arg)
352 self.dump_arg(function, arg)
353 print ' __writer.endEnter();'
354 self.dispatch_function(function)
355 print ' __writer.beginLeave(__call);'
356 for arg in function.args:
358 self.dump_arg(function, arg)
359 self.wrap_arg(function, arg)
360 if function.type is not stdapi.Void:
361 self.dump_ret(function, "__result")
362 print ' __writer.endLeave();'
364 def dispatch_function(self, function):
365 if function.type is stdapi.Void:
368 result = '__result = '
369 dispatch = self.get_dispatch_function(function)
370 print ' %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
372 def dump_arg(self, function, arg):
373 print ' __writer.beginArg(%u);' % (arg.index,)
374 self.dump_arg_instance(function, arg)
375 print ' __writer.endArg();'
377 def dump_arg_instance(self, function, arg):
378 dump_instance(arg.type, arg.name)
380 def wrap_arg(self, function, arg):
381 wrap_instance(arg.type, arg.name)
383 def unwrap_arg(self, function, arg):
384 unwrap_instance(arg.type, arg.name)
386 def dump_ret(self, function, instance):
387 print ' __writer.beginReturn();'
388 dump_instance(function.type, instance)
389 print ' __writer.endReturn();'
391 def wrap_ret(self, function, instance):
392 wrap_instance(function.type, instance)
394 def unwrap_ret(self, function, instance):
395 unwrap_instance(function.type, instance)
397 def interface_wrap_impl(self, interface):
398 print '%s::%s(%s * pInstance) {' % (interface_wrap_name(interface), interface_wrap_name(interface), interface.name)
399 print ' m_pInstance = pInstance;'
402 print '%s::~%s() {' % (interface_wrap_name(interface), interface_wrap_name(interface))
405 for method in interface.itermethods():
406 self.trace_method(interface, method)
409 def trace_method(self, interface, method):
410 print method.prototype(interface_wrap_name(interface) + '::' + method.name) + ' {'
411 print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
412 print ' static const Trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (int(method.id), interface.name + '::' + method.name, len(method.args) + 1)
413 print ' unsigned __call = __writer.beginEnter(__sig);'
414 print ' __writer.beginArg(0);'
415 print ' __writer.writeOpaque((const void *)m_pInstance);'
416 print ' __writer.endArg();'
417 for arg in method.args:
419 self.unwrap_arg(method, arg)
420 self.dump_arg(method, arg)
421 if method.type is stdapi.Void:
424 print ' %s __result;' % method.type
425 result = '__result = '
426 print ' __writer.endEnter();'
427 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
428 print ' __writer.beginLeave(__call);'
429 for arg in method.args:
431 self.dump_arg(method, arg)
432 self.wrap_arg(method, arg)
433 if method.type is not stdapi.Void:
434 print ' __writer.beginReturn();'
435 dump_instance(method.type, "__result")
436 print ' __writer.endReturn();'
437 wrap_instance(method.type, '__result')
438 print ' __writer.endLeave();'
439 if method.name == 'QueryInterface':
440 print ' if (ppvObj && *ppvObj) {'
441 print ' if (*ppvObj == m_pInstance) {'
442 print ' *ppvObj = this;'
444 for iface in self.api.interfaces:
445 print ' else if (riid == IID_%s) {' % iface.name
446 print ' *ppvObj = new Wrap%s((%s *) *ppvObj);' % (iface.name, iface.name)
449 if method.name == 'Release':
450 assert method.type is not stdapi.Void
451 print ' if (!__result)'
452 print ' delete this;'
453 if method.type is not stdapi.Void:
454 print ' return __result;'
459 class DllTracer(Tracer):
461 def __init__(self, dllname):
462 self.dllname = dllname
464 def header(self, api):
466 static HINSTANCE g_hDll = NULL;
469 __getPublicProcAddress(LPCSTR lpProcName)
472 char szDll[MAX_PATH] = {0};
474 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
478 strcat(szDll, "\\\\%s");
480 g_hDll = LoadLibraryA(szDll);
486 return GetProcAddress(g_hDll, lpProcName);
491 dispatcher = Dispatcher()
492 dispatcher.dispatch_api(api)
494 Tracer.header(self, api)