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."""
29 import specs.stdapi as stdapi
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 _write__%s(const %s &value) {' % (struct.tag, 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' % (struct.id, struct.name, len(struct.members))
63 print ' trace::localWriter.beginStruct(&sig);'
64 for type, name in struct.members:
65 dump_instance(type, 'value.%s' % (name,))
66 print ' trace::localWriter.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 _write__%s(const %s value) {' % (enum.tag, 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 ' trace::localWriter.writeSInt(value);'
96 print ' trace::localWriter.writeEnum(sig);'
100 def visit_bitmask(self, bitmask):
101 print 'static const trace::BitmaskFlag __bitmask%s_flags[] = {' % (bitmask.tag)
102 for value in bitmask.values:
103 print ' {"%s", %s},' % (value, value)
106 print 'static const trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.tag)
107 print ' %u, %u, __bitmask%s_flags' % (bitmask.id, len(bitmask.values), bitmask.tag)
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,)
138 def visit_polymorphic(self, polymorphic):
139 print 'static void _write__%s(int selector, const %s & value) {' % (polymorphic.tag, polymorphic.expr)
140 print ' switch (selector) {'
141 for cases, type in polymorphic.iterswitch():
144 dump_instance(type, 'static_cast<%s>(value)' % (type,))
151 class DumpImplementer(stdapi.Visitor):
152 '''Dump an instance.'''
154 def visit_literal(self, literal, instance):
155 print ' trace::localWriter.write%s(%s);' % (literal.kind, instance)
157 def visit_string(self, string, instance):
158 if string.length is not None:
159 print ' trace::localWriter.writeString((const char *)%s, %s);' % (instance, string.length)
161 print ' trace::localWriter.writeString((const char *)%s);' % instance
163 def visit_const(self, const, instance):
164 self.visit(const.type, instance)
166 def visit_struct(self, struct, instance):
167 print ' _write__%s(%s);' % (struct.tag, instance)
169 def visit_array(self, array, instance):
170 length = '__c' + array.type.tag
171 index = '__i' + array.type.tag
172 print ' if (%s) {' % instance
173 print ' size_t %s = %s;' % (length, array.length)
174 print ' trace::localWriter.beginArray(%s);' % length
175 print ' for (size_t %s = 0; %s < %s; ++%s) {' % (index, index, length, index)
176 print ' trace::localWriter.beginElement();'
177 self.visit(array.type, '(%s)[%s]' % (instance, index))
178 print ' trace::localWriter.endElement();'
180 print ' trace::localWriter.endArray();'
182 print ' trace::localWriter.writeNull();'
185 def visit_blob(self, blob, instance):
186 print ' trace::localWriter.writeBlob(%s, %s);' % (instance, blob.size)
188 def visit_enum(self, enum, instance):
189 print ' _write__%s(%s);' % (enum.tag, instance)
191 def visit_bitmask(self, bitmask, instance):
192 print ' trace::localWriter.writeBitmask(&__bitmask%s_sig, %s);' % (bitmask.tag, instance)
194 def visit_pointer(self, pointer, instance):
195 print ' if (%s) {' % instance
196 print ' trace::localWriter.beginArray(1);'
197 print ' trace::localWriter.beginElement();'
198 dump_instance(pointer.type, "*" + instance)
199 print ' trace::localWriter.endElement();'
200 print ' trace::localWriter.endArray();'
202 print ' trace::localWriter.writeNull();'
205 def visit_handle(self, handle, instance):
206 self.visit(handle.type, instance)
208 def visit_alias(self, alias, instance):
209 self.visit(alias.type, instance)
211 def visit_opaque(self, opaque, instance):
212 print ' trace::localWriter.writeOpaque((const void *)%s);' % instance
214 def visit_interface(self, interface, instance):
215 print ' trace::localWriter.writeOpaque((const void *)&%s);' % instance
217 def visit_polymorphic(self, polymorphic, instance):
218 print ' _write__%s(%s, %s);' % (polymorphic.tag, polymorphic.switch_expr, instance)
221 dump_instance = DumpImplementer().visit
225 class Wrapper(stdapi.Visitor):
226 '''Wrap an instance.'''
228 def visit_void(self, type, instance):
229 raise NotImplementedError
231 def visit_literal(self, type, instance):
234 def visit_string(self, type, instance):
237 def visit_const(self, type, instance):
240 def visit_struct(self, struct, instance):
241 for type, name in struct.members:
242 self.visit(type, "(%s).%s" % (instance, name))
244 def visit_array(self, array, instance):
245 # XXX: actually it is possible to return an array of pointers
248 def visit_blob(self, blob, instance):
251 def visit_enum(self, enum, instance):
254 def visit_bitmask(self, bitmask, instance):
257 def visit_pointer(self, pointer, instance):
258 print " if (%s) {" % instance
259 self.visit(pointer.type, "*" + instance)
262 def visit_handle(self, handle, instance):
263 self.visit(handle.type, instance)
265 def visit_alias(self, alias, instance):
266 self.visit(alias.type, instance)
268 def visit_opaque(self, opaque, instance):
271 def visit_interface(self, interface, instance):
272 assert instance.startswith('*')
273 instance = instance[1:]
274 print " if (%s) {" % instance
275 print " %s = new %s(%s);" % (instance, interface_wrap_name(interface), instance)
278 def visit_polymorphic(self, type, instance):
279 # XXX: There might be polymorphic values that need wrapping in the future
283 class Unwrapper(Wrapper):
285 def visit_interface(self, interface, instance):
286 assert instance.startswith('*')
287 instance = instance[1:]
288 print " if (%s) {" % instance
289 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface_wrap_name(interface), instance)
293 wrap_instance = Wrapper().visit
294 unwrap_instance = Unwrapper().visit
302 def trace_api(self, api):
308 for header in api.headers:
313 types = api.all_types()
314 visitor = DumpDeclarator()
315 map(visitor.visit, types)
319 interfaces = [type for type in types if isinstance(type, stdapi.Interface)]
320 map(self.interface_wrap_impl, interfaces)
324 map(self.trace_function_decl, api.functions)
325 map(self.trace_function_impl, api.functions)
330 def header(self, api):
333 def footer(self, api):
336 def trace_function_decl(self, function):
337 # Per-function declarations
340 print 'static const char * __%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args]))
342 print 'static const char ** __%s_args = NULL;' % (function.name,)
343 print 'static const trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, function.id, function.name, len(function.args), function.name)
346 def is_public_function(self, function):
349 def trace_function_impl(self, function):
350 if self.is_public_function(function):
351 print 'extern "C" PUBLIC'
353 print 'extern "C" PRIVATE'
354 print function.prototype() + ' {'
355 if function.type is not stdapi.Void:
356 print ' %s __result;' % function.type
357 self.trace_function_impl_body(function)
358 if function.type is not stdapi.Void:
359 self.wrap_ret(function, "__result")
360 print ' return __result;'
364 def trace_function_impl_body(self, function):
365 print ' unsigned __call = trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
366 for arg in function.args:
368 self.unwrap_arg(function, arg)
369 self.dump_arg(function, arg)
370 print ' trace::localWriter.endEnter();'
371 self.dispatch_function(function)
372 print ' trace::localWriter.beginLeave(__call);'
373 for arg in function.args:
375 self.dump_arg(function, arg)
376 self.wrap_arg(function, arg)
377 if function.type is not stdapi.Void:
378 self.dump_ret(function, "__result")
379 print ' trace::localWriter.endLeave();'
381 def dispatch_function(self, function, prefix='__', suffix=''):
382 if function.type is stdapi.Void:
385 result = '__result = '
386 dispatch = prefix + function.name + suffix
387 print ' %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
389 def dump_arg(self, function, arg):
390 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
391 self.dump_arg_instance(function, arg)
392 print ' trace::localWriter.endArg();'
394 def dump_arg_instance(self, function, arg):
395 dump_instance(arg.type, arg.name)
397 def wrap_arg(self, function, arg):
398 wrap_instance(arg.type, arg.name)
400 def unwrap_arg(self, function, arg):
401 unwrap_instance(arg.type, arg.name)
403 def dump_ret(self, function, instance):
404 print ' trace::localWriter.beginReturn();'
405 dump_instance(function.type, instance)
406 print ' trace::localWriter.endReturn();'
408 def wrap_ret(self, function, instance):
409 wrap_instance(function.type, instance)
411 def unwrap_ret(self, function, instance):
412 unwrap_instance(function.type, instance)
414 def interface_wrap_impl(self, interface):
415 print '%s::%s(%s * pInstance) {' % (interface_wrap_name(interface), interface_wrap_name(interface), interface.name)
416 print ' m_pInstance = pInstance;'
419 print '%s::~%s() {' % (interface_wrap_name(interface), interface_wrap_name(interface))
422 for method in interface.itermethods():
423 self.trace_method(interface, method)
426 def trace_method(self, interface, method):
427 print method.prototype(interface_wrap_name(interface) + '::' + method.name) + ' {'
428 print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
429 print ' static const trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (method.id, interface.name + '::' + method.name, len(method.args) + 1)
430 print ' unsigned __call = trace::localWriter.beginEnter(&__sig);'
431 print ' trace::localWriter.beginArg(0);'
432 print ' trace::localWriter.writeOpaque((const void *)m_pInstance);'
433 print ' trace::localWriter.endArg();'
434 for arg in method.args:
436 self.unwrap_arg(method, arg)
437 self.dump_arg(method, arg)
438 if method.type is stdapi.Void:
441 print ' %s __result;' % method.type
442 result = '__result = '
443 print ' trace::localWriter.endEnter();'
444 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
445 print ' trace::localWriter.beginLeave(__call);'
446 for arg in method.args:
448 self.dump_arg(method, arg)
449 self.wrap_arg(method, arg)
450 if method.type is not stdapi.Void:
451 print ' trace::localWriter.beginReturn();'
452 dump_instance(method.type, "__result")
453 print ' trace::localWriter.endReturn();'
454 wrap_instance(method.type, '__result')
455 print ' trace::localWriter.endLeave();'
456 if method.name == 'QueryInterface':
457 print ' if (ppvObj && *ppvObj) {'
458 print ' if (*ppvObj == m_pInstance) {'
459 print ' *ppvObj = this;'
461 for iface in self.api.interfaces:
462 print r' else if (riid == IID_%s) {' % iface.name
463 print r' *ppvObj = new Wrap%s((%s *) *ppvObj);' % (iface.name, iface.name)
466 print r' os::log("apitrace: warning: unknown REFIID {0x%08lX,0x%04X,0x%04X,{0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X}}\n",'
467 print r' riid.Data1, riid.Data2, riid.Data3,'
468 print r' riid.Data4[0],'
469 print r' riid.Data4[1],'
470 print r' riid.Data4[2],'
471 print r' riid.Data4[3],'
472 print r' riid.Data4[4],'
473 print r' riid.Data4[5],'
474 print r' riid.Data4[6],'
475 print r' riid.Data4[7]);'
478 if method.name == 'Release':
479 assert method.type is not stdapi.Void
480 print ' if (!__result)'
481 print ' delete this;'
482 if method.type is not stdapi.Void:
483 print ' return __result;'
488 class DllTracer(Tracer):
490 def __init__(self, dllname):
491 self.dllname = dllname
493 def header(self, api):
495 static HINSTANCE g_hDll = NULL;
498 __getPublicProcAddress(LPCSTR lpProcName)
501 char szDll[MAX_PATH] = {0};
503 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
507 strcat(szDll, "\\\\%s");
509 g_hDll = LoadLibraryA(szDll);
515 return GetProcAddress(g_hDll, lpProcName);
520 dispatcher = Dispatcher()
521 dispatcher.dispatch_api(api)
523 Tracer.header(self, api)