X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=wrappers%2Ftrace.py;h=61a2cb95fcf8249c13719464a36bee5891f8f76a;hb=HEAD;hp=506570dca018ed9bc0dde652940cd33babd83fb6;hpb=bb23f3a0bad625e3c427b9de991d4a42fa22e5f0;p=apitrace diff --git a/wrappers/trace.py b/wrappers/trace.py index 506570d..61a2cb9 100644 --- a/wrappers/trace.py +++ b/wrappers/trace.py @@ -323,7 +323,7 @@ class ValueWrapper(stdapi.Traverser, stdapi.ExpanderMixin): def visitInterfacePointer(self, interface, instance): print " if (%s) {" % instance - print " %s = new %s(%s);" % (instance, getWrapperInterfaceName(interface), instance) + print " %s = %s::_Create(__FUNCTION__, %s);" % (instance, getWrapperInterfaceName(interface), instance) print " }" def visitPolymorphic(self, type, instance): @@ -382,6 +382,9 @@ class ValueUnwrapper(ValueWrapper): class Tracer: '''Base class to orchestrate the code generation of API tracing.''' + # 0-3 are reserved to memcpy, malloc, free, and realloc + __id = 4 + def __init__(self): self.api = None @@ -436,6 +439,7 @@ class Tracer: print print '#include "trace.hpp"' print + print 'static std::map g_WrappedObjects;' def footer(self, api): pass @@ -448,9 +452,14 @@ class Tracer: print 'static const char * _%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args])) else: print 'static const char ** _%s_args = NULL;' % (function.name,) - print 'static const trace::FunctionSig _%s_sig = {%u, "%s", %u, _%s_args};' % (function.name, function.id, function.name, len(function.args), function.name) + print 'static const trace::FunctionSig _%s_sig = {%u, "%s", %u, _%s_args};' % (function.name, self.getFunctionSigId(), function.name, len(function.args), function.name) print + def getFunctionSigId(self): + id = Tracer.__id + Tracer.__id += 1 + return id + def isFunctionPublic(self, function): return True @@ -484,6 +493,8 @@ class Tracer: for arg in function.args: if not arg.output: self.unwrapArg(function, arg) + for arg in function.args: + if not arg.output: self.serializeArg(function, arg) print ' trace::localWriter.endEnter();' self.invokeFunction(function) @@ -497,9 +508,9 @@ class Tracer: print ' }' if function.type is not stdapi.Void: self.serializeRet(function, "_result") - print ' trace::localWriter.endLeave();' if function.type is not stdapi.Void: self.wrapRet(function, "_result") + print ' trace::localWriter.endLeave();' def invokeFunction(self, function, prefix='_', suffix=''): if function.type is stdapi.Void: @@ -555,9 +566,6 @@ class Tracer: def wrapRet(self, function, instance): self.wrapValue(function.type, instance) - def unwrapRet(self, function, instance): - self.unwrapValue(function.type, instance) - def needsWrapping(self, type): visitor = WrapDecider() visitor.visit(type) @@ -585,9 +593,11 @@ class Tracer: def declareWrapperInterface(self, interface): print "class %s : public %s " % (getWrapperInterfaceName(interface), interface.name) print "{" - print "public:" + print "private:" print " %s(%s * pInstance);" % (getWrapperInterfaceName(interface), interface.name) print " virtual ~%s();" % getWrapperInterfaceName(interface) + print "public:" + print " static %s* _Create(const char *functionName, %s * pInstance);" % (getWrapperInterfaceName(interface), interface.name) print for method in interface.iterMethods(): print " " + method.prototype() + ";" @@ -595,6 +605,11 @@ class Tracer: #print "private:" for type, name, value in self.enumWrapperInterfaceVariables(interface): print ' %s %s;' % (type, name) + for i in range(64): + print r' virtual void _dummy%i(void) const {' % i + print r' os::log("error: %s: unexpected virtual method\n");' % interface.name + print r' os::abort();' + print r' }' print "};" print @@ -602,17 +617,45 @@ class Tracer: return [ ("DWORD", "m_dwMagic", "0xd8365d6c"), ("%s *" % interface.name, "m_pInstance", "pInstance"), + ("void *", "m_pVtbl", "*(void **)pInstance"), + ("UINT", "m_NumMethods", len(list(interface.iterBaseMethods()))), ] def implementWrapperInterface(self, interface): self.interface = interface + # Private constructor print '%s::%s(%s * pInstance) {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), interface.name) for type, name, value in self.enumWrapperInterfaceVariables(interface): print ' %s = %s;' % (name, value) print '}' print + + # Public constructor + print '%s *%s::_Create(const char *functionName, %s * pInstance) {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface), interface.name) + print r' std::map::const_iterator it = g_WrappedObjects.find(pInstance);' + print r' if (it != g_WrappedObjects.end()) {' + print r' Wrap%s *pWrapper = (Wrap%s *)it->second;' % (interface.name, interface.name) + print r' assert(pWrapper);' + print r' assert(pWrapper->m_dwMagic == 0xd8365d6c);' + print r' assert(pWrapper->m_pInstance == pInstance);' + print r' if (pWrapper->m_pVtbl == *(void **)pInstance &&' + print r' pWrapper->m_NumMethods >= %s) {' % len(list(interface.iterBaseMethods())) + #print r' os::log("%s: fetched pvObj=%p pWrapper=%p pVtbl=%p\n", functionName, pInstance, pWrapper, pWrapper->m_pVtbl);' + print r' return pWrapper;' + print r' }' + print r' }' + print r' Wrap%s *pWrapper = new Wrap%s(pInstance);' % (interface.name, interface.name) + #print r' os::log("%%s: created %s pvObj=%%p pWrapper=%%p pVtbl=%%p\n", functionName, pInstance, pWrapper, pWrapper->m_pVtbl);' % interface.name + print r' g_WrappedObjects[pInstance] = pWrapper;' + print r' return pWrapper;' + print '}' + print + + # Destructor print '%s::~%s() {' % (getWrapperInterfaceName(interface), getWrapperInterfaceName(interface)) + #print r' os::log("%s::Release: deleted pvObj=%%p pWrapper=%%p pVtbl=%%p\n", m_pInstance, this, m_pVtbl);' % interface.name + print r' g_WrappedObjects.erase(m_pInstance);' print '}' print @@ -624,6 +667,10 @@ class Tracer: def implementWrapperInterfaceMethod(self, interface, base, method): print method.prototype(getWrapperInterfaceName(interface) + '::' + method.name) + ' {' + + if False: + print r' os::log("%%s(%%p -> %%p)\n", "%s", this, m_pInstance);' % (getWrapperInterfaceName(interface) + '::' + method.name) + if method.type is not stdapi.Void: print ' %s _result;' % method.type @@ -638,7 +685,7 @@ class Tracer: assert not method.internal print ' static const char * _args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args])) - print ' static const trace::FunctionSig _sig = {%u, "%s", %u, _args};' % (method.id, interface.name + '::' + method.name, len(method.args) + 1) + print ' static const trace::FunctionSig _sig = {%u, "%s", %u, _args};' % (self.getFunctionSigId(), interface.name + '::' + method.name, len(method.args) + 1) print ' %s *_this = static_cast<%s *>(m_pInstance);' % (base, base) @@ -649,6 +696,8 @@ class Tracer: for arg in method.args: if not arg.output: self.unwrapArg(method, arg) + for arg in method.args: + if not arg.output: self.serializeArg(method, arg) print ' trace::localWriter.endEnter();' @@ -665,14 +714,16 @@ class Tracer: if method.type is not stdapi.Void: self.serializeRet(method, '_result') - print ' trace::localWriter.endLeave();' if method.type is not stdapi.Void: self.wrapRet(method, '_result') if method.name == 'Release': assert method.type is not stdapi.Void - print ' if (!_result)' - print ' delete this;' + print r' if (!_result) {' + print r' delete this;' + print r' }' + + print ' trace::localWriter.endLeave();' def implementIidWrapper(self, api): print r'static void' @@ -691,7 +742,7 @@ class Tracer: else_ = '' for iface in api.getAllInterfaces(): print r' %sif (riid == IID_%s) {' % (else_, iface.name) - print r' *ppvObj = new Wrap%s((%s *) *ppvObj);' % (iface.name, iface.name) + print r' *ppvObj = Wrap%s::_Create(functionName, (%s *) *ppvObj);' % (iface.name, iface.name) print r' }' else_ = 'else ' print r' %s{' % else_