]> git.cworth.org Git - apitrace/blobdiff - retrace.py
Fix glCompressed*Image*DARB tracing/retracing
[apitrace] / retrace.py
index d54b25d730b89114953d155204336c2d7ca64f3e..27d0afe8879c94e6bb013ee34e4b33d9a5c7159b 100644 (file)
@@ -37,10 +37,7 @@ class ConstRemover(stdapi.Rebuilder):
         return const.type
 
     def visit_opaque(self, opaque):
-        expr = opaque.expr
-        if expr.startswith('const '):
-            expr = expr[6:]
-        return stdapi.Opaque(expr)
+        return opaque
 
 
 def handle_entry(handle, value):
@@ -77,7 +74,7 @@ class ValueExtractor(stdapi.Visitor):
         length = '__a%s->values.size()' % array.id
         print '        %s = new %s[%s];' % (lvalue, array.type, length)
         index = '__j' + array.id
-        print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
+        print '        for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
         try:
             self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
         finally:
@@ -98,9 +95,11 @@ class ValueExtractor(stdapi.Visitor):
             print '    }'
 
     def visit_handle(self, handle, lvalue, rvalue):
-        self.visit(handle.type, lvalue, handle_entry(handle, rvalue));
-        print '    if (verbosity >= 2)'
-        print '        std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
+        OpaqueValueExtractor().visit(handle.type, lvalue, rvalue);
+        new_lvalue = handle_entry(handle, lvalue)
+        print '    if (retrace::verbosity >= 2)'
+        print '        std::cout << "%s " << size_t(%s) << " <- " << size_t(%s) << "\\n";' % (handle.name, lvalue, new_lvalue)
+        print '    %s = %s;' % (lvalue, new_lvalue)
     
     def visit_blob(self, blob, lvalue, rvalue):
         print '    %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
@@ -109,6 +108,15 @@ class ValueExtractor(stdapi.Visitor):
         print '    %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
 
 
+class OpaqueValueExtractor(ValueExtractor):
+    '''Value extractor that also understands opaque values.
+
+    Normally opaque values can't be retraced, unless they are being extracted
+    in the context of handles.'''
+
+    def visit_opaque(self, opaque, lvalue, rvalue):
+        print '    %s = static_cast<%s>((%s).blob());' % (lvalue, opaque, rvalue)
+
 
 class ValueWrapper(stdapi.Visitor):
 
@@ -129,7 +137,7 @@ class ValueWrapper(stdapi.Visitor):
         print '    if (__a%s) {' % (array.id)
         length = '__a%s->values.size()' % array.id
         index = '__j' + array.id
-        print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
+        print '        for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
         try:
             self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
         finally:
@@ -144,22 +152,23 @@ class ValueWrapper(stdapi.Visitor):
         finally:
             print '    }'
     
-
     def visit_handle(self, handle, lvalue, rvalue):
+        print '    %s __orig_result;' % handle.type
+        OpaqueValueExtractor().visit(handle.type, '__orig_result', rvalue);
         if handle.range is None:
-            rvalue = "static_cast<%s>(%s)" % (handle.type, rvalue)
+            rvalue = "__orig_result"
             entry = handle_entry(handle, rvalue) 
             print "    %s = %s;" % (entry, lvalue)
-            print '    if (verbosity >= 2)'
+            print '    if (retrace::verbosity >= 2)'
             print '        std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals())
         else:
             i = '__h' + handle.id
             lvalue = "%s + %s" % (lvalue, i)
-            rvalue = "static_cast<%s>(%s) + %s" % (handle.type, rvalue, i)
+            rvalue = "__orig_result + %s" % (i,)
             entry = handle_entry(handle, rvalue) 
-            print '    for({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals())
+            print '    for ({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals())
             print '        {entry} = {lvalue};'.format(**locals())
-            print '        if (verbosity >= 2)'
+            print '        if (retrace::verbosity >= 2)'
             print '            std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals())
             print '    }'
     
@@ -174,6 +183,11 @@ class Retracer:
 
     def retrace_function(self, function):
         print 'static void retrace_%s(Trace::Call &call) {' % function.name
+        self.retrace_function_body(function)
+        print '}'
+        print
+
+    def retrace_function_body(self, function):
         success = True
         for arg in function.args:
             arg_type = ConstRemover().visit(arg.type)
@@ -205,11 +219,10 @@ class Retracer:
                 ValueWrapper().visit(function.type, lvalue, rvalue)
             except NotImplementedError:
                 print '   // FIXME: result'
-        print '}'
-        print
 
     def fail_function(self, function):
-        print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
+        print '    if (retrace::verbosity >= 0)'
+        print '        std::cerr << "warning: unsupported call %s\\n";' % function.name
         print '    return;'
 
     def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
@@ -233,10 +246,10 @@ class Retracer:
             if function.sideeffects:
                 self.retrace_function(function)
 
-        print 'static bool retrace_call(Trace::Call &call) {'
+        print 'bool retrace::retrace_call(Trace::Call &call) {'
         print '    const char *name = call.name().c_str();'
         print
-        print '    if (verbosity >=1 ) {'
+        print '    if (retrace::verbosity >= 1) {'
         print '        std::cout << call;'
         print '        std::cout.flush();'
         print '    };'
@@ -252,7 +265,8 @@ class Retracer:
     
         string_switch('name', func_dict.keys(), handle_case)
 
-        print '    std::cerr << "warning: unknown call " << call.name() << "\\n";'
+        print '    if (retrace::verbosity >= 0)'
+        print '        std::cerr << call.no << ": warning: unknown call " << call.name() << "\\n";'
         print '    return false;'
         print '}'
         print
@@ -261,6 +275,7 @@ class Retracer:
     def retrace_api(self, api):
 
         print '#include "trace_parser.hpp"'
+        print '#include "retrace.hpp"'
         print
 
         types = api.all_types()
@@ -269,15 +284,12 @@ class Retracer:
         for handle in handles:
             if handle.name not in handle_names:
                 if handle.key is None:
-                    print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
+                    print 'static retrace::map<%s> __%s_map;' % (handle.type, handle.name)
                 else:
                     key_name, key_type = handle.key
-                    print 'static std::map<%s, std::map<%s, %s> > __%s_map;' % (key_type, handle.type, handle.type, handle.name)
+                    print 'static std::map<%s, retrace::map<%s> > __%s_map;' % (key_type, handle.type, handle.name)
                 handle_names.add(handle.name)
         print
 
-        print 'int verbosity = 0;'
-        print
-
         self.retrace_functions(api.functions)