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 ' Trace::BeginStruct(&sig);'
64 for type, name in struct.members:
65 dump_instance(type, 'value.%s' % (name,))
66 print ' Trace::EndStruct();'
70 def visit_array(self, array):
71 self.visit(array.type)
73 def visit_blob(self, array):
76 def visit_enum(self, enum):
77 print 'static void __traceEnum%s(const %s value) {' % (enum.id, enum.expr)
80 value = enum.values[i]
81 print ' static const Trace::EnumSig sig%u = {%u, "%s", %s};' % (i, enum.vid + i, value, value)
82 print ' const Trace::EnumSig *sig;'
83 print ' switch(value) {'
85 value = enum.values[i]
86 print ' case %s:' % value
87 print ' sig = &sig%u;' % i
90 print ' Trace::LiteralSInt(value);'
93 print ' Trace::LiteralEnum(sig);'
97 def visit_bitmask(self, bitmask):
98 print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
99 for value in bitmask.values:
100 print ' {"%s", %s},' % (value, value)
103 print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
104 print ' %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
108 def visit_pointer(self, pointer):
109 self.visit(pointer.type)
111 def visit_handle(self, handle):
112 self.visit(handle.type)
114 def visit_alias(self, alias):
115 self.visit(alias.type)
117 def visit_opaque(self, opaque):
120 def visit_interface(self, interface):
121 print "class %s : public %s " % (interface_wrap_name(interface), interface.name)
124 print " %s(%s * pInstance);" % (interface_wrap_name(interface), interface.name)
125 print " virtual ~%s();" % interface_wrap_name(interface)
127 for method in interface.itermethods():
128 print " " + method.prototype() + ";"
131 print " %s * m_pInstance;" % (interface.name,)
136 class DumpImplementer(stdapi.Visitor):
137 '''Dump an instance.'''
139 def visit_literal(self, literal, instance):
140 print ' Trace::Literal%s(%s);' % (literal.format, instance)
142 def visit_string(self, string, instance):
143 if string.length is not None:
144 print ' Trace::LiteralString((const char *)%s, %s);' % (instance, string.length)
146 print ' Trace::LiteralString((const char *)%s);' % instance
148 def visit_const(self, const, instance):
149 self.visit(const.type, instance)
151 def visit_struct(self, struct, instance):
152 print ' __traceStruct%s(%s);' % (struct.id, instance)
154 def visit_array(self, array, instance):
155 print ' if (%s) {' % instance
156 index = '__i' + array.type.id
157 print ' Trace::BeginArray(%s);' % (array.length,)
158 print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
159 print ' Trace::BeginElement();'
160 self.visit(array.type, '(%s)[%s]' % (instance, index))
161 print ' Trace::EndElement();'
163 print ' Trace::EndArray();'
166 print ' Trace::LiteralNull();'
168 def visit_blob(self, blob, instance):
169 print ' Trace::LiteralBlob(%s, %s);' % (instance, blob.size)
171 def visit_enum(self, enum, instance):
172 print ' __traceEnum%s(%s);' % (enum.id, instance)
174 def visit_bitmask(self, bitmask, instance):
175 print ' Trace::LiteralBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
177 def visit_pointer(self, pointer, instance):
178 print ' if (%s) {' % instance
179 print ' Trace::BeginArray(1);'
180 print ' Trace::BeginElement();'
181 dump_instance(pointer.type, "*" + instance)
182 print ' Trace::EndElement();'
183 print ' Trace::EndArray();'
186 print ' Trace::LiteralNull();'
188 def visit_handle(self, handle, instance):
189 self.visit(handle.type, instance)
191 def visit_alias(self, alias, instance):
192 self.visit(alias.type, instance)
194 def visit_opaque(self, opaque, instance):
195 print ' Trace::LiteralOpaque((const void *)%s);' % instance
197 def visit_interface(self, interface, instance):
198 print ' Trace::LiteralOpaque((const void *)&%s);' % instance
201 dump_instance = DumpImplementer().visit
205 class Wrapper(stdapi.Visitor):
206 '''Wrap an instance.'''
208 def visit_void(self, type, instance):
209 raise NotImplementedError
211 def visit_literal(self, type, instance):
214 def visit_string(self, type, instance):
217 def visit_const(self, type, instance):
220 def visit_struct(self, struct, instance):
221 for type, name in struct.members:
222 self.visit(type, "(%s).%s" % (instance, name))
224 def visit_array(self, array, instance):
225 # XXX: actually it is possible to return an array of pointers
228 def visit_blob(self, blob, instance):
231 def visit_enum(self, enum, instance):
234 def visit_bitmask(self, bitmask, instance):
237 def visit_pointer(self, pointer, instance):
238 self.visit(pointer.type, "*" + instance)
240 def visit_handle(self, handle, instance):
241 self.visit(handle.type, instance)
243 def visit_alias(self, alias, instance):
244 self.visit(alias.type, instance)
246 def visit_opaque(self, opaque, instance):
249 def visit_interface(self, interface, instance):
250 assert instance.startswith('*')
251 instance = instance[1:]
252 print " if (%s)" % instance
253 print " %s = new %s(%s);" % (instance, interface_wrap_name(interface), instance)
256 class Unwrapper(Wrapper):
258 def visit_interface(self, interface, instance):
259 assert instance.startswith('*')
260 instance = instance[1:]
261 print " if (%s)" % instance
262 print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface_wrap_name(interface), instance)
265 wrap_instance = Wrapper().visit
266 unwrap_instance = Unwrapper().visit
271 def trace_api(self, api):
275 for header in api.headers:
280 types = api.all_types()
281 visitor = DumpDeclarator()
282 map(visitor.visit, types)
286 interfaces = [type for type in types if isinstance(type, stdapi.Interface)]
287 map(self.interface_wrap_impl, interfaces)
291 map(self.trace_function_decl, api.functions)
292 map(self.trace_function_impl, api.functions)
297 def header(self, api):
300 def footer(self, api):
303 def trace_function_decl(self, function):
304 # Per-function declarations
307 print 'static const char * __%s_args[%u] = {%s};' % (function.name, len(function.args), ', '.join(['"%s"' % arg.name for arg in function.args]))
309 print 'static const char ** __%s_args = NULL;' % (function.name,)
310 print 'static const Trace::FunctionSig __%s_sig = {%u, "%s", %u, __%s_args};' % (function.name, int(function.id), function.name, len(function.args), function.name)
313 def get_dispatch_function(self, function):
314 return '__' + function.name
316 def is_public_function(self, function):
319 def trace_function_impl(self, function):
320 if self.is_public_function(function):
321 print 'extern "C" PUBLIC'
323 print 'extern "C" PRIVATE'
324 print function.prototype() + ' {'
325 if function.type is not stdapi.Void:
326 print ' %s __result;' % function.type
327 self.trace_function_impl_body(function)
328 if function.type is not stdapi.Void:
329 self.wrap_ret(function, "__result")
330 print ' return __result;'
334 def trace_function_impl_body(self, function):
335 print ' unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name,)
336 for arg in function.args:
338 self.unwrap_arg(function, arg)
339 self.dump_arg(function, arg)
340 print ' Trace::EndEnter();'
341 self.dispatch_function(function)
342 print ' Trace::BeginLeave(__call);'
343 for arg in function.args:
345 self.dump_arg(function, arg)
346 self.wrap_arg(function, arg)
347 if function.type is not stdapi.Void:
348 self.dump_ret(function, "__result")
349 print ' Trace::EndLeave();'
351 def dispatch_function(self, function):
352 if function.type is stdapi.Void:
355 result = '__result = '
356 dispatch = self.get_dispatch_function(function)
357 print ' %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
359 def dump_arg(self, function, arg):
360 print ' Trace::BeginArg(%u);' % (arg.index,)
361 self.dump_arg_instance(function, arg)
362 print ' Trace::EndArg();'
364 def dump_arg_instance(self, function, arg):
365 dump_instance(arg.type, arg.name)
367 def wrap_arg(self, function, arg):
368 wrap_instance(arg.type, arg.name)
370 def unwrap_arg(self, function, arg):
371 unwrap_instance(arg.type, arg.name)
373 def dump_ret(self, function, instance):
374 print ' Trace::BeginReturn();'
375 dump_instance(function.type, instance)
376 print ' Trace::EndReturn();'
378 def wrap_ret(self, function, instance):
379 wrap_instance(function.type, instance)
381 def unwrap_ret(self, function, instance):
382 unwrap_instance(function.type, instance)
384 def interface_wrap_impl(self, interface):
385 print '%s::%s(%s * pInstance) {' % (interface_wrap_name(interface), interface_wrap_name(interface), interface.name)
386 print ' m_pInstance = pInstance;'
389 print '%s::~%s() {' % (interface_wrap_name(interface), interface_wrap_name(interface))
392 for method in interface.itermethods():
393 self.trace_method(interface, method)
396 def trace_method(self, interface, method):
397 print method.prototype(interface_wrap_name(interface) + '::' + method.name) + ' {'
398 print ' static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
399 print ' static const Trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (int(method.id), interface.name + '::' + method.name, len(method.args) + 1)
400 print ' unsigned __call = Trace::BeginEnter(__sig);'
401 print ' Trace::BeginArg(0);'
402 print ' Trace::LiteralOpaque((const void *)m_pInstance);'
403 print ' Trace::EndArg();'
404 for arg in method.args:
406 self.unwrap_arg(method, arg)
407 self.dump_arg(method, arg)
408 if method.type is stdapi.Void:
411 print ' %s __result;' % method.type
412 result = '__result = '
413 print ' Trace::EndEnter();'
414 print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
415 print ' Trace::BeginLeave(__call);'
416 for arg in method.args:
418 self.dump_arg(method, arg)
419 self.wrap_arg(method, arg)
420 if method.type is not stdapi.Void:
421 print ' Trace::BeginReturn();'
422 dump_instance(method.type, "__result")
423 print ' Trace::EndReturn();'
424 wrap_instance(method.type, '__result')
425 print ' Trace::EndLeave();'
426 if method.name == 'QueryInterface':
427 print ' if (*ppvObj == m_pInstance)'
428 print ' *ppvObj = this;'
429 if method.name == 'Release':
430 assert method.type is not stdapi.Void
431 print ' if (!__result)'
432 print ' delete this;'
433 if method.type is not stdapi.Void:
434 print ' return __result;'
439 class DllTracer(Tracer):
441 def __init__(self, dllname):
442 self.dllname = dllname
444 def get_function_address(self, function):
445 return '__%s' % (function.name,)
447 def header(self, api):
449 static HINSTANCE g_hDll = NULL;
452 __getPublicProcAddress(LPCSTR lpProcName)
455 char szDll[MAX_PATH] = {0};
457 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
461 strcat(szDll, "\\\\%s");
463 g_hDll = LoadLibraryA(szDll);
469 return GetProcAddress(g_hDll, lpProcName);
472 #define __abort() OS::Abort()
475 dispatcher = Dispatcher()
476 dispatcher.dispatch_api(api)
478 Tracer.header(self, api)