]> git.cworth.org Git - apitrace/blob - glretrace.py
First stab at binary trace and retracing.
[apitrace] / glretrace.py
1 ##########################################################################
2 #
3 # Copyright 2010 VMware, Inc.
4 # All Rights Reserved.
5 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
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
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26
27 import base
28 from glx import libgl
29
30
31 def is_arg_supported(arg_type):
32     if isinstance(arg_type, (base.Literal, base._String, base.Enum)):
33         return True
34     if isinstance(arg_type, (base.Alias, base.Flags)):
35         return is_arg_supported(arg_type.type)
36     return False
37
38
39 def is_function_supported(function):
40     for arg_type, arg_name in function.args:
41         if not is_arg_supported(arg_type):
42             return False
43     return True
44
45
46 def extract_value(arg_type, arg_value):
47     if isinstance(arg_type, base.Literal):
48         return 'Trace::as%s(%s)' % (arg_type.format, value)
49     if isinstance(arg_type, base.Enum):
50         return 'Trace::asSInt(%s)' % (value)
51     if isinstance(arg_type, (base.Alias, base.Flags)):
52         return extract_value(arg_type.type, value)
53     assert false
54     return '0'
55
56
57 if __name__ == '__main__':
58     print
59     print '#include <stdlib.h>'
60     print '#include <string.h>'
61     print '#include <GL/glew.h>'
62     print '#include <GL/glut.h>'
63     print
64     print '#include "trace_parser.hpp"'
65     print
66
67     functions = filter(is_function_supported, libgl.functions)
68    
69     for function in functions:
70         print 'static void retrace_%s(Trace::Call &call) {' % function.name
71         for arg_type, arg_name in function.args:
72             print '    %s %s;' % (arg_type, arg_name)
73         for arg_type, arg_name in function.args:
74             value = 'call.get_arg("%s")' % (arg_name,)
75             value = extract_value(arg_type, value)
76             print '    %s = static_cast<%s>(%s);' % (arg_name, arg_type, value)
77         arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
78         print '    %s(%s);' % (function.name, arg_names)
79         print '}'
80         print
81
82     print 'static bool retrace_call(Trace::Call &call) {'
83     for function in functions:
84         print '    if (call.name == "%s") {' % function.name
85         print '        retrace_%s(call);' % function.name
86         print '        return true;'
87         print '    }'
88     print '    std::cerr << "Unsupported call " << call.name << "\\n";'
89     print '    return false;'
90     print '}'
91     print '''
92
93 class Retracer : public Trace::Parser
94 {
95     void handle_call(Trace::Call &call) {
96         std::cout << call;
97         std::cout.flush();
98         retrace_call(call);
99     }
100 };
101
102 int main(int argc, char **argv)
103 {
104    glutInit(&argc, argv);
105    glutInitWindowPosition( 0, 0 );
106    glutInitWindowSize( 800, 600 );
107    glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
108    glutCreateWindow(argv[0]);
109    glewInit();
110    for (int i = 1; i < argc; ++i) {
111       Retracer p;
112       p.parse(argv[i]);
113       glutMainLoop();
114    }
115    return 0;
116 }
117
118 '''