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 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 self.serializer.visit(type, 'static_cast<%s>(value)' % (type,))
151 class ValueSerializer(stdapi.Visitor):
152 '''Visitor which generates code to serialize any type.
154 Simple types are serialized inline here, whereas the serialization of
155 complex types is dispatched to the serialization functions generated by
156 ComplexValueSerializer visitor above.
159 def visitLiteral(self, literal, instance):
160 print ' trace::localWriter.write%s(%s);' % (literal.kind, instance)
162 def visitString(self, string, instance):
163 if string.kind == 'String':
164 cast = 'const char *'
165 elif string.kind == 'WString':
166 cast = 'const wchar_t *'
169 if cast != string.expr:
170 # reinterpret_cast is necessary for GLubyte * <=> char *
171 instance = 'reinterpret_cast<%s>(%s)' % (cast, instance)
172 if string.length is not None:
173 length = ', %s' % string.length
176 print ' trace::localWriter.write%s(%s%s);' % (string.kind, instance, length)
178 def visitConst(self, const, instance):
179 self.visit(const.type, instance)
181 def visitStruct(self, struct, instance):
182 print ' _write__%s(%s);' % (struct.tag, instance)
184 def visitArray(self, array, instance):
185 length = '__c' + array.type.tag
186 index = '__i' + array.type.tag
187 print ' if (%s) {' % instance
188 print ' size_t %s = %s;' % (length, array.length)
189 print ' trace::localWriter.beginArray(%s);' % length
190 print ' for (size_t %s = 0; %s < %s; ++%s) {' % (index, index, length, index)
191 print ' trace::localWriter.beginElement();'
192 self.visit(array.type, '(%s)[%s]' % (instance, index))
193 print ' trace::localWriter.endElement();'
195 print ' trace::localWriter.endArray();'
197 print ' trace::localWriter.writeNull();'
200 def visitBlob(self, blob, instance):
201 print ' trace::localWriter.writeBlob(%s, %s);' % (instance, blob.size)
203 def visitEnum(self, enum, instance):
204 print ' trace::localWriter.writeEnum(&__enum%s_sig, %s);' % (enum.tag, instance)
206 def visitBitmask(self, bitmask, instance):
207 print ' trace::localWriter.writeBitmask(&__bitmask%s_sig, %s);' % (bitmask.tag, instance)
209 def visitPointer(self, pointer, instance):
210 print ' if (%s) {' % instance
211 print ' trace::localWriter.beginArray(1);'
212 print ' trace::localWriter.beginElement();'
213 self.visit(pointer.type, "*" + instance)
214 print ' trace::localWriter.endElement();'
215 print ' trace::localWriter.endArray();'
217 print ' trace::localWriter.writeNull();'
220 def visitIntPointer(self, pointer, instance):
221 print ' trace::localWriter.writeOpaque((const void *)%s);' % instance
223 def visitObjPointer(self, pointer, instance):
224 print ' trace::localWriter.writeOpaque((const void *)%s);' % instance
226 def visitLinearPointer(self, pointer, instance):
227 print ' trace::localWriter.writeOpaque((const void *)%s);' % instance
229 def visitReference(self, reference, instance):
230 self.visit(reference.type, instance)
232 def visitHandle(self, handle, instance):
233 self.visit(handle.type, instance)
235 def visitAlias(self, alias, instance):
236 self.visit(alias.type, instance)
238 def visitOpaque(self, opaque, instance):
239 print ' trace::localWriter.writeOpaque((const void *)%s);' % instance
241 def visitInterface(self, interface, instance):
242 print ' trace::localWriter.writeOpaque((const void *)&%s);' % instance
244 def visitPolymorphic(self, polymorphic, instance):
245 print ' _write__%s(%s, %s);' % (polymorphic.tag, polymorphic.switchExpr, instance)
248 class WrapDecider(stdapi.Traverser):
249 '''Type visitor which will decide wheter this type will need wrapping or not.
251 For complex types (arrays, structures), we need to know this before hand.
255 self.needsWrapping = False
257 def visitLinearPointer(self, void):
260 def visitInterface(self, interface):
261 self.needsWrapping = True
264 class ValueWrapper(stdapi.Traverser):
265 '''Type visitor which will generate the code to wrap an instance.
267 Wrapping is necessary mostly for interfaces, however interface pointers can
268 appear anywhere inside complex types.
271 def visitStruct(self, struct, instance):
272 for type, name in struct.members:
273 self.visit(type, "(%s).%s" % (instance, name))
275 def visitArray(self, array, instance):
276 print " if (%s) {" % instance
277 print " for (size_t _i = 0, _s = %s; _i < _s; ++_i) {" % array.length
278 self.visit(array.type, instance + "[_i]")
282 def visitPointer(self, pointer, instance):
283 print " if (%s) {" % instance
284 self.visit(pointer.type, "*" + instance)
287 def visitObjPointer(self, pointer, instance):
288 elem_type = pointer.type.mutable()
289 if isinstance(elem_type, stdapi.Interface):
290 self.visitInterfacePointer(elem_type, instance)
292 self.visitPointer(self, pointer, instance)
294 def visitInterface(self, interface, instance):
295 raise NotImplementedError
297 def visitInterfacePointer(self, interface, instance):
298 print " if (%s) {" % instance
299 print " %s = new %s(%s);" % (instance, getWrapperInterfaceName(interface), instance)
302 def visitPolymorphic(self, type, instance):
303 # XXX: There might be polymorphic values that need wrapping in the future
304 raise NotImplementedError
307 class ValueUnwrapper(ValueWrapper):
308 '''Reverse of ValueWrapper.'''
312 def visitArray(self, array, instance):
313 if self.allocated or isinstance(instance, stdapi.Interface):
314 return ValueWrapper.visitArray(self, array, instance)
315 elem_type = array.type.mutable()
316 print " if (%s && %s) {" % (instance, array.length)
317 print " %s * _t = static_cast<%s *>(alloca(%s * sizeof *_t));" % (elem_type, elem_type, array.length)
318 print " for (size_t _i = 0, _s = %s; _i < _s; ++_i) {" % array.length
319 print " _t[_i] = %s[_i];" % instance
320 self.allocated = True
321 self.visit(array.type, "_t[_i]")
323 print " %s = _t;" % instance
326 def visitInterfacePointer(self, interface, instance):
327 print r' if (%s) {' % instance
328 print r' const %s *pWrapper = static_cast<const %s*>(%s);' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), instance)
329 print r' if (pWrapper && pWrapper->m_dwMagic == 0xd8365d6c) {'
330 print r' %s = pWrapper->m_pInstance;' % (instance,)
332 print r' os::log("apitrace: warning: %%s: unexpected %%s pointer\n", __FUNCTION__, "%s");' % interface.name
338 '''Base class to orchestrate the code generation of API tracing.'''
343 def serializerFactory(self):
344 '''Create a serializer.
346 Can be overriden by derived classes to inject their own serialzer.
349 return ValueSerializer()
351 def trace_api(self, api):
357 for header in api.headers:
361 # Generate the serializer functions
362 types = api.getAllTypes()
363 visitor = ComplexValueSerializer(self.serializerFactory())
364 map(visitor.visit, types)
368 interfaces = api.getAllInterfaces()
369 map(self.declareWrapperInterface, interfaces)
370 map(self.implementWrapperInterface, interfaces)
374 map(self.traceFunctionDecl, api.functions)
375 map(self.traceFunctionImpl, api.functions)
380 def header(self, api):
383 def footer(self, api):
386 def traceFunctionDecl(self, function):
387 # Per-function declarations
390 print 'static const char * __%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args]))
392 print 'static const char ** __%s_args = NULL;' % (function.name,)
393 print 'static const trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, function.id, function.name, len(function.args), function.name)
396 def isFunctionPublic(self, function):
399 def traceFunctionImpl(self, function):
400 if self.isFunctionPublic(function):
401 print 'extern "C" PUBLIC'
403 print 'extern "C" PRIVATE'
404 print function.prototype() + ' {'
405 if function.type is not stdapi.Void:
406 print ' %s __result;' % function.type
407 self.traceFunctionImplBody(function)
408 if function.type is not stdapi.Void:
409 self.wrapRet(function, "__result")
410 print ' return __result;'
414 def traceFunctionImplBody(self, function):
415 print ' unsigned __call = trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
416 for arg in function.args:
418 self.unwrapArg(function, arg)
419 self.serializeArg(function, arg)
420 print ' trace::localWriter.endEnter();'
421 self.invokeFunction(function)
422 print ' trace::localWriter.beginLeave(__call);'
423 for arg in function.args:
425 self.serializeArg(function, arg)
426 self.wrapArg(function, arg)
427 if function.type is not stdapi.Void:
428 self.serializeRet(function, "__result")
429 print ' trace::localWriter.endLeave();'
431 def invokeFunction(self, function, prefix='__', suffix=''):
432 if function.type is stdapi.Void:
435 result = '__result = '
436 dispatch = prefix + function.name + suffix
437 print ' %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
439 def serializeArg(self, function, arg):
440 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
441 self.serializeArgValue(function, arg)
442 print ' trace::localWriter.endArg();'
444 def serializeArgValue(self, function, arg):
445 self.serializeValue(arg.type, arg.name)
447 def wrapArg(self, function, arg):
448 assert not isinstance(arg.type, stdapi.ObjPointer)
449 self.wrapValue(arg.type, arg.name)
451 def unwrapArg(self, function, arg):
452 self.unwrapValue(arg.type, arg.name)
454 def serializeRet(self, function, instance):
455 print ' trace::localWriter.beginReturn();'
456 self.serializeValue(function.type, instance)
457 print ' trace::localWriter.endReturn();'
459 def serializeValue(self, type, instance):
460 serializer = self.serializerFactory()
461 serializer.visit(type, instance)
463 def wrapRet(self, function, instance):
464 self.wrapValue(function.type, instance)
466 def unwrapRet(self, function, instance):
467 self.unwrapValue(function.type, instance)
469 def needsWrapping(self, type):
470 visitor = WrapDecider()
472 return visitor.needsWrapping
474 def wrapValue(self, type, instance):
475 if self.needsWrapping(type):
476 visitor = ValueWrapper()
477 visitor.visit(type, instance)
479 def unwrapValue(self, type, instance):
480 if self.needsWrapping(type):
481 visitor = ValueUnwrapper()
482 visitor.visit(type, instance)
484 def declareWrapperInterface(self, interface):
485 print "class %s : public %s " % (getWrapperInterfaceName(interface), interface.name)
488 print " %s(%s * pInstance);" % (getWrapperInterfaceName(interface), interface.name)
489 print " virtual ~%s();" % getWrapperInterfaceName(interface)
491 for method in interface.iterMethods():
492 print " " + method.prototype() + ";"
494 self.declareWrapperInterfaceVariables(interface)
498 def declareWrapperInterfaceVariables(self, interface):
500 print " DWORD m_dwMagic;"
501 print " %s * m_pInstance;" % (interface.name,)
503 def implementWrapperInterface(self, interface):
504 print '%s::%s(%s * pInstance) {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), interface.name)
505 print ' m_dwMagic = 0xd8365d6c;'
506 print ' m_pInstance = pInstance;'
509 print '%s::~%s() {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface))
512 for base, method in interface.iterBaseMethods():
513 self.implementWrapperInterfaceMethod(interface, base, method)
516 def implementWrapperInterfaceMethod(self, interface, base, method):
517 print method.prototype(getWrapperInterfaceName(interface) + '::' + method.name) + ' {'
518 if method.type is not stdapi.Void:
519 print ' %s __result;' % method.type
521 self.implementWrapperInterfaceMethodBody(interface, base, method)
523 if method.type is not stdapi.Void:
524 print ' return __result;'
528 def implementWrapperInterfaceMethodBody(self, interface, base, method):
529 print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
530 print ' static const trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (method.id, interface.name + '::' + method.name, len(method.args) + 1)
531 print ' unsigned __call = trace::localWriter.beginEnter(&__sig);'
532 print ' trace::localWriter.beginArg(0);'
533 print ' trace::localWriter.writeOpaque((const void *)m_pInstance);'
534 print ' trace::localWriter.endArg();'
536 from specs.winapi import REFIID
539 for arg in method.args:
541 self.unwrapArg(method, arg)
542 self.serializeArg(method, arg)
543 if arg.type is REFIID:
545 print ' trace::localWriter.endEnter();'
547 self.invokeMethod(interface, base, method)
549 print ' trace::localWriter.beginLeave(__call);'
550 for arg in method.args:
552 self.serializeArg(method, arg)
553 self.wrapArg(method, arg)
554 if riid is not None and isinstance(arg.type, stdapi.Pointer):
555 assert isinstance(arg.type.type, stdapi.ObjPointer)
556 obj_type = arg.type.type.type
557 assert obj_type is stdapi.Void
558 self.wrapIid(interface, method, riid, arg)
562 if method.type is not stdapi.Void:
563 print ' trace::localWriter.beginReturn();'
564 self.serializeValue(method.type, "__result")
565 print ' trace::localWriter.endReturn();'
566 self.wrapValue(method.type, '__result')
567 print ' trace::localWriter.endLeave();'
568 if method.name == 'Release':
569 assert method.type is not stdapi.Void
570 print ' if (!__result)'
571 print ' delete this;'
573 def wrapIid(self, interface, method, riid, out):
574 print ' if (%s && *%s) {' % (out.name, out.name)
575 print ' if (*%s == m_pInstance) {' % (out.name,)
576 print ' *%s = this;' % (out.name,)
578 for iface in self.api.getAllInterfaces():
579 print r' else if (%s == IID_%s) {' % (riid.name, iface.name)
580 print r' *%s = new Wrap%s((%s *) *%s);' % (out.name, iface.name, iface.name, out.name)
583 print r' os::log("apitrace: warning: %s::%s: unknown IID {0x%08lX,0x%04X,0x%04X,{0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X,0x%02X}}\n",'
584 print r' "%s", "%s",' % (interface.name, method.name)
585 print r' %s.Data1, %s.Data2, %s.Data3,' % (riid.name, riid.name, riid.name)
586 print r' %s.Data4[0],' % (riid.name,)
587 print r' %s.Data4[1],' % (riid.name,)
588 print r' %s.Data4[2],' % (riid.name,)
589 print r' %s.Data4[3],' % (riid.name,)
590 print r' %s.Data4[4],' % (riid.name,)
591 print r' %s.Data4[5],' % (riid.name,)
592 print r' %s.Data4[6],' % (riid.name,)
593 print r' %s.Data4[7]);' % (riid.name,)
597 def invokeMethod(self, interface, base, method):
598 if method.type is stdapi.Void:
601 result = '__result = '
602 print ' %sstatic_cast<%s *>(m_pInstance)->%s(%s);' % (result, base, method.name, ', '.join([str(arg.name) for arg in method.args]))
604 def emit_memcpy(self, dest, src, length):
605 print ' unsigned __call = trace::localWriter.beginEnter(&trace::memcpy_sig);'
606 print ' trace::localWriter.beginArg(0);'
607 print ' trace::localWriter.writeOpaque(%s);' % dest
608 print ' trace::localWriter.endArg();'
609 print ' trace::localWriter.beginArg(1);'
610 print ' trace::localWriter.writeBlob(%s, %s);' % (src, length)
611 print ' trace::localWriter.endArg();'
612 print ' trace::localWriter.beginArg(2);'
613 print ' trace::localWriter.writeUInt(%s);' % length
614 print ' trace::localWriter.endArg();'
615 print ' trace::localWriter.endEnter();'
616 print ' trace::localWriter.beginLeave(__call);'
617 print ' trace::localWriter.endLeave();'