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."""
32 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
35 import specs.stdapi as stdapi
38 def getWrapperInterfaceName(interface):
39 return "Wrap" + interface.expr
42 class ComplexValueSerializer(stdapi.OnceVisitor):
43 '''Type visitors which generates serialization functions for
46 Simple types are serialized inline.
49 def __init__(self, serializer):
50 stdapi.OnceVisitor.__init__(self)
51 self.serializer = serializer
53 def visitVoid(self, literal):
56 def visitLiteral(self, literal):
59 def visitString(self, string):
62 def visitConst(self, const):
63 self.visit(const.type)
65 def visitStruct(self, struct):
66 for type, name in struct.members:
68 print 'static void _write__%s(const %s &value) {' % (struct.tag, struct.expr)
69 print ' static const char * members[%u] = {' % (len(struct.members),)
70 for type, name, in struct.members:
71 print ' "%s",' % (name,)
73 print ' static const trace::StructSig sig = {'
74 print ' %u, "%s", %u, members' % (struct.id, struct.name, len(struct.members))
76 print ' trace::localWriter.beginStruct(&sig);'
77 for type, name in struct.members:
78 self.serializer.visit(type, 'value.%s' % (name,))
79 print ' trace::localWriter.endStruct();'
83 def visitArray(self, array):
84 self.visit(array.type)
86 def visitBlob(self, array):
89 def visitEnum(self, enum):
90 print 'static const trace::EnumValue __enum%s_values[] = {' % (enum.tag)
91 for value in enum.values:
92 print ' {"%s", %s},' % (value, value)
95 print 'static const trace::EnumSig __enum%s_sig = {' % (enum.tag)
96 print ' %u, %u, __enum%s_values' % (enum.id, len(enum.values), enum.tag)
100 def visitBitmask(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 visitPointer(self, pointer):
112 self.visit(pointer.type)
114 def visitIntPointer(self, pointer):
117 def visitObjPointer(self, pointer):
118 self.visit(pointer.type)
120 def visitLinearPointer(self, pointer):
121 self.visit(pointer.type)
123 def visitHandle(self, handle):
124 self.visit(handle.type)
126 def visitReference(self, reference):
127 self.visit(reference.type)
129 def visitAlias(self, alias):
130 self.visit(alias.type)
132 def visitOpaque(self, opaque):
135 def visitInterface(self, interface):
138 def visitPolymorphic(self, polymorphic):
139 if not polymorphic.contextLess:
141 print 'static void _write__%s(int selector, const %s & value) {' % (polymorphic.tag, polymorphic.expr)
142 print ' switch (selector) {'
143 for cases, type in polymorphic.iterSwitch():
146 self.serializer.visit(type, 'static_cast<%s>(value)' % (type,))
153 class ValueSerializer(stdapi.Visitor):
154 '''Visitor which generates code to serialize any type.
156 Simple types are serialized inline here, whereas the serialization of
157 complex types is dispatched to the serialization functions generated by
158 ComplexValueSerializer visitor above.
161 def visitLiteral(self, literal, instance):
162 print ' trace::localWriter.write%s(%s);' % (literal.kind, instance)
164 def visitString(self, string, instance):
165 if string.kind == 'String':
166 cast = 'const char *'
167 elif string.kind == 'WString':
168 cast = 'const wchar_t *'
171 if cast != string.expr:
172 # reinterpret_cast is necessary for GLubyte * <=> char *
173 instance = 'reinterpret_cast<%s>(%s)' % (cast, instance)
174 if string.length is not None:
175 length = ', %s' % string.length
178 print ' trace::localWriter.write%s(%s%s);' % (string.kind, instance, length)
180 def visitConst(self, const, instance):
181 self.visit(const.type, instance)
183 def visitStruct(self, struct, instance):
184 print ' _write__%s(%s);' % (struct.tag, instance)
186 def visitArray(self, array, instance):
187 length = '__c' + array.type.tag
188 index = '__i' + array.type.tag
189 print ' if (%s) {' % instance
190 print ' size_t %s = %s;' % (length, array.length)
191 print ' trace::localWriter.beginArray(%s);' % length
192 print ' for (size_t %s = 0; %s < %s; ++%s) {' % (index, index, length, index)
193 print ' trace::localWriter.beginElement();'
194 self.visit(array.type, '(%s)[%s]' % (instance, index))
195 print ' trace::localWriter.endElement();'
197 print ' trace::localWriter.endArray();'
199 print ' trace::localWriter.writeNull();'
202 def visitBlob(self, blob, instance):
203 print ' trace::localWriter.writeBlob(%s, %s);' % (instance, blob.size)
205 def visitEnum(self, enum, instance):
206 print ' trace::localWriter.writeEnum(&__enum%s_sig, %s);' % (enum.tag, instance)
208 def visitBitmask(self, bitmask, instance):
209 print ' trace::localWriter.writeBitmask(&__bitmask%s_sig, %s);' % (bitmask.tag, instance)
211 def visitPointer(self, pointer, instance):
212 print ' if (%s) {' % instance
213 print ' trace::localWriter.beginArray(1);'
214 print ' trace::localWriter.beginElement();'
215 self.visit(pointer.type, "*" + instance)
216 print ' trace::localWriter.endElement();'
217 print ' trace::localWriter.endArray();'
219 print ' trace::localWriter.writeNull();'
222 def visitIntPointer(self, pointer, instance):
223 print ' trace::localWriter.writePointer((uintptr_t)%s);' % instance
225 def visitObjPointer(self, pointer, instance):
226 print ' trace::localWriter.writePointer((uintptr_t)%s);' % instance
228 def visitLinearPointer(self, pointer, instance):
229 print ' trace::localWriter.writePointer((uintptr_t)%s);' % instance
231 def visitReference(self, reference, instance):
232 self.visit(reference.type, instance)
234 def visitHandle(self, handle, instance):
235 self.visit(handle.type, instance)
237 def visitAlias(self, alias, instance):
238 self.visit(alias.type, instance)
240 def visitOpaque(self, opaque, instance):
241 print ' trace::localWriter.writePointer((uintptr_t)%s);' % instance
243 def visitInterface(self, interface, instance):
246 def visitPolymorphic(self, polymorphic, instance):
247 if polymorphic.contextLess:
248 print ' _write__%s(%s, %s);' % (polymorphic.tag, polymorphic.switchExpr, instance)
250 print ' switch (%s) {' % polymorphic.switchExpr
251 for cases, type in polymorphic.iterSwitch():
254 self.visit(type, 'static_cast<%s>(%s)' % (type, instance))
259 class WrapDecider(stdapi.Traverser):
260 '''Type visitor which will decide wheter this type will need wrapping or not.
262 For complex types (arrays, structures), we need to know this before hand.
266 self.needsWrapping = False
268 def visitLinearPointer(self, void):
271 def visitInterface(self, interface):
272 self.needsWrapping = True
275 class ValueWrapper(stdapi.Traverser):
276 '''Type visitor which will generate the code to wrap an instance.
278 Wrapping is necessary mostly for interfaces, however interface pointers can
279 appear anywhere inside complex types.
282 def visitStruct(self, struct, instance):
283 for type, name in struct.members:
284 self.visit(type, "(%s).%s" % (instance, name))
286 def visitArray(self, array, instance):
287 print " if (%s) {" % instance
288 print " for (size_t _i = 0, _s = %s; _i < _s; ++_i) {" % array.length
289 self.visit(array.type, instance + "[_i]")
293 def visitPointer(self, pointer, instance):
294 print " if (%s) {" % instance
295 self.visit(pointer.type, "*" + instance)
298 def visitObjPointer(self, pointer, instance):
299 elem_type = pointer.type.mutable()
300 if isinstance(elem_type, stdapi.Interface):
301 self.visitInterfacePointer(elem_type, instance)
303 self.visitPointer(self, pointer, instance)
305 def visitInterface(self, interface, instance):
306 raise NotImplementedError
308 def visitInterfacePointer(self, interface, instance):
309 print " if (%s) {" % instance
310 print " %s = new %s(%s);" % (instance, getWrapperInterfaceName(interface), instance)
313 def visitPolymorphic(self, type, instance):
314 # XXX: There might be polymorphic values that need wrapping in the future
315 raise NotImplementedError
318 class ValueUnwrapper(ValueWrapper):
319 '''Reverse of ValueWrapper.'''
323 def visitArray(self, array, instance):
324 if self.allocated or isinstance(instance, stdapi.Interface):
325 return ValueWrapper.visitArray(self, array, instance)
326 elem_type = array.type.mutable()
327 print " if (%s && %s) {" % (instance, array.length)
328 print " %s * _t = static_cast<%s *>(alloca(%s * sizeof *_t));" % (elem_type, elem_type, array.length)
329 print " for (size_t _i = 0, _s = %s; _i < _s; ++_i) {" % array.length
330 print " _t[_i] = %s[_i];" % instance
331 self.allocated = True
332 self.visit(array.type, "_t[_i]")
334 print " %s = _t;" % instance
337 def visitInterfacePointer(self, interface, instance):
338 print r' if (%s) {' % instance
339 print r' const %s *pWrapper = static_cast<const %s*>(%s);' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), instance)
340 print r' if (pWrapper && pWrapper->m_dwMagic == 0xd8365d6c) {'
341 print r' %s = pWrapper->m_pInstance;' % (instance,)
343 print r' os::log("apitrace: warning: %%s: unexpected %%s pointer\n", __FUNCTION__, "%s");' % interface.name
349 '''Base class to orchestrate the code generation of API tracing.'''
354 def serializerFactory(self):
355 '''Create a serializer.
357 Can be overriden by derived classes to inject their own serialzer.
360 return ValueSerializer()
362 def traceApi(self, api):
368 for header in api.headers:
372 # Generate the serializer functions
373 types = api.getAllTypes()
374 visitor = ComplexValueSerializer(self.serializerFactory())
375 map(visitor.visit, types)
379 self.traceInterfaces(api)
382 self.interface = None
384 map(self.traceFunctionDecl, api.functions)
385 map(self.traceFunctionImpl, api.functions)
390 def header(self, api):
393 def footer(self, api):
396 def traceFunctionDecl(self, function):
397 # Per-function declarations
400 print 'static const char * __%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args]))
402 print 'static const char ** __%s_args = NULL;' % (function.name,)
403 print 'static const trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, function.id, function.name, len(function.args), function.name)
406 def isFunctionPublic(self, function):
409 def traceFunctionImpl(self, function):
410 if self.isFunctionPublic(function):
411 print 'extern "C" PUBLIC'
413 print 'extern "C" PRIVATE'
414 print function.prototype() + ' {'
415 if function.type is not stdapi.Void:
416 print ' %s __result;' % function.type
417 self.traceFunctionImplBody(function)
418 if function.type is not stdapi.Void:
419 self.wrapRet(function, "__result")
420 print ' return __result;'
424 def traceFunctionImplBody(self, function):
425 print ' unsigned __call = trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
426 for arg in function.args:
428 self.unwrapArg(function, arg)
429 self.serializeArg(function, arg)
430 print ' trace::localWriter.endEnter();'
431 self.invokeFunction(function)
432 print ' trace::localWriter.beginLeave(__call);'
433 for arg in function.args:
435 self.serializeArg(function, arg)
436 self.wrapArg(function, arg)
437 if function.type is not stdapi.Void:
438 self.serializeRet(function, "__result")
439 print ' trace::localWriter.endLeave();'
441 def invokeFunction(self, function, prefix='__', suffix=''):
442 if function.type is stdapi.Void:
445 result = '__result = '
446 dispatch = prefix + function.name + suffix
447 print ' %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
449 def serializeArg(self, function, arg):
450 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
451 self.serializeArgValue(function, arg)
452 print ' trace::localWriter.endArg();'
454 def serializeArgValue(self, function, arg):
455 self.serializeValue(arg.type, arg.name)
457 def wrapArg(self, function, arg):
458 assert not isinstance(arg.type, stdapi.ObjPointer)
460 from specs.winapi import REFIID
462 for other_arg in function.args:
463 if not other_arg.output and other_arg.type is REFIID:
465 if riid is not None and isinstance(arg.type, stdapi.Pointer):
466 assert isinstance(arg.type.type, stdapi.ObjPointer)
467 obj_type = arg.type.type.type
468 assert obj_type is stdapi.Void
469 self.wrapIid(function, riid, arg)
472 self.wrapValue(arg.type, arg.name)
474 def unwrapArg(self, function, arg):
475 self.unwrapValue(arg.type, arg.name)
477 def serializeRet(self, function, instance):
478 print ' trace::localWriter.beginReturn();'
479 self.serializeValue(function.type, instance)
480 print ' trace::localWriter.endReturn();'
482 def serializeValue(self, type, instance):
483 serializer = self.serializerFactory()
484 serializer.visit(type, instance)
486 def wrapRet(self, function, instance):
487 self.wrapValue(function.type, instance)
489 def unwrapRet(self, function, instance):
490 self.unwrapValue(function.type, instance)
492 def needsWrapping(self, type):
493 visitor = WrapDecider()
495 return visitor.needsWrapping
497 def wrapValue(self, type, instance):
498 if self.needsWrapping(type):
499 visitor = ValueWrapper()
500 visitor.visit(type, instance)
502 def unwrapValue(self, type, instance):
503 if self.needsWrapping(type):
504 visitor = ValueUnwrapper()
505 visitor.visit(type, instance)
507 def traceInterfaces(self, api):
508 interfaces = api.getAllInterfaces()
511 map(self.declareWrapperInterface, interfaces)
512 self.implementIidWrapper(api)
513 map(self.implementWrapperInterface, interfaces)
516 def declareWrapperInterface(self, interface):
517 print "class %s : public %s " % (getWrapperInterfaceName(interface), interface.name)
520 print " %s(%s * pInstance);" % (getWrapperInterfaceName(interface), interface.name)
521 print " virtual ~%s();" % getWrapperInterfaceName(interface)
523 for method in interface.iterMethods():
524 print " " + method.prototype() + ";"
526 self.declareWrapperInterfaceVariables(interface)
530 def declareWrapperInterfaceVariables(self, interface):
532 print " DWORD m_dwMagic;"
533 print " %s * m_pInstance;" % (interface.name,)
535 def implementWrapperInterface(self, interface):
536 self.interface = interface
538 print '%s::%s(%s * pInstance) {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), interface.name)
539 print ' m_dwMagic = 0xd8365d6c;'
540 print ' m_pInstance = pInstance;'
543 print '%s::~%s() {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface))
547 for base, method in interface.iterBaseMethods():
549 self.implementWrapperInterfaceMethod(interface, base, method)
553 def implementWrapperInterfaceMethod(self, interface, base, method):
554 print method.prototype(getWrapperInterfaceName(interface) + '::' + method.name) + ' {'
555 if method.type is not stdapi.Void:
556 print ' %s __result;' % method.type
558 self.implementWrapperInterfaceMethodBody(interface, base, method)
560 if method.type is not stdapi.Void:
561 print ' return __result;'
565 def implementWrapperInterfaceMethodBody(self, interface, base, method):
566 print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
567 print ' static const trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (method.id, interface.name + '::' + method.name, len(method.args) + 1)
568 print ' unsigned __call = trace::localWriter.beginEnter(&__sig);'
569 print ' trace::localWriter.beginArg(0);'
570 print ' trace::localWriter.writePointer((uintptr_t)m_pInstance);'
571 print ' trace::localWriter.endArg();'
572 for arg in method.args:
574 self.unwrapArg(method, arg)
575 self.serializeArg(method, arg)
576 print ' trace::localWriter.endEnter();'
578 self.invokeMethod(interface, base, method)
580 print ' trace::localWriter.beginLeave(__call);'
581 for arg in method.args:
583 self.serializeArg(method, arg)
584 self.wrapArg(method, arg)
586 if method.type is not stdapi.Void:
587 print ' trace::localWriter.beginReturn();'
588 self.serializeValue(method.type, "__result")
589 print ' trace::localWriter.endReturn();'
590 self.wrapValue(method.type, '__result')
591 print ' trace::localWriter.endLeave();'
592 if method.name == 'Release':
593 assert method.type is not stdapi.Void
594 print ' if (!__result)'
595 print ' delete this;'
597 def implementIidWrapper(self, api):
599 print r'warnIID(const char *functionName, REFIID riid, const char *reason) {'
600 print r' os::log("apitrace: warning: %s: %s IID {0x%08lX,0x%04X,0x%04X,{0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X}}\n",'
601 print r' functionName, reason,'
602 print r' riid.Data1, riid.Data2, riid.Data3,'
603 print r' riid.Data4[0], riid.Data4[1], riid.Data4[2], riid.Data4[3], riid.Data4[4], riid.Data4[5], riid.Data4[6], riid.Data4[7]);'
607 print r'wrapIID(const char *functionName, REFIID riid, void * * ppvObj) {'
608 print r' if (!ppvObj || !*ppvObj) {'
612 for iface in api.getAllInterfaces():
613 print r' %sif (riid == IID_%s) {' % (else_, iface.name)
614 print r' *ppvObj = new Wrap%s((%s *) *ppvObj);' % (iface.name, iface.name)
617 print r' %s{' % else_
618 print r' warnIID(functionName, riid, "unknown");'
623 def wrapIid(self, function, riid, out):
624 print r' if (%s && *%s) {' % (out.name, out.name)
625 functionName = function.name
627 if self.interface is not None:
628 functionName = self.interface.name + '::' + functionName
629 print r' if (*%s == m_pInstance &&' % (out.name,)
630 print r' (%s)) {' % ' || '.join('%s == IID_%s' % (riid.name, iface.name) for iface in self.interface.iterBases())
631 print r' *%s = this;' % (out.name,)
634 print r' %s{' % else_
635 print r' wrapIID("%s", %s, %s);' % (functionName, riid.name, out.name)
639 def invokeMethod(self, interface, base, method):
640 if method.type is stdapi.Void:
643 result = '__result = '
644 print ' %sstatic_cast<%s *>(m_pInstance)->%s(%s);' % (result, base, method.name, ', '.join([str(arg.name) for arg in method.args]))
646 def emit_memcpy(self, dest, src, length):
647 print ' unsigned __call = trace::localWriter.beginEnter(&trace::memcpy_sig);'
648 print ' trace::localWriter.beginArg(0);'
649 print ' trace::localWriter.writePointer((uintptr_t)%s);' % dest
650 print ' trace::localWriter.endArg();'
651 print ' trace::localWriter.beginArg(1);'
652 print ' trace::localWriter.writeBlob(%s, %s);' % (src, length)
653 print ' trace::localWriter.endArg();'
654 print ' trace::localWriter.beginArg(2);'
655 print ' trace::localWriter.writeUInt(%s);' % length
656 print ' trace::localWriter.endArg();'
657 print ' trace::localWriter.endEnter();'
658 print ' trace::localWriter.beginLeave(__call);'
659 print ' trace::localWriter.endLeave();'