]> git.cworth.org Git - apitrace/blob - glxtrace.py
Add support for GL_APPLE_flush_render
[apitrace] / glxtrace.py
1 ##########################################################################
2 #
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2010 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 """GLX tracing generator."""
29
30
31 from stdapi import API
32 from glapi import glapi
33 from glxapi import glxapi
34 from gltrace import GlTracer
35 from dispatch import function_pointer_type, function_pointer_value
36
37
38 class GlxTracer(GlTracer):
39
40     def is_public_function(self, function):
41         # The symbols visible in libGL.so can vary, so expose them all
42         return True
43
44     def trace_function_impl_body(self, function):
45         GlTracer.trace_function_impl_body(self, function)
46
47         # Take snapshots
48         if function.name == 'glXSwapBuffers':
49             print '    glsnapshot::snapshot(__call);'
50         if function.name in ('glFinish', 'glFlush'):
51             print '    GLint __draw_framebuffer = 0;'
52             print '    __glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &__draw_framebuffer);'
53             print '    if (__draw_framebuffer == 0) {'
54             print '        GLint __draw_buffer = GL_NONE;'
55             print '        __glGetIntegerv(GL_DRAW_BUFFER, &__draw_buffer);'
56             print '        if (__draw_buffer == GL_FRONT) {'
57             print '             glsnapshot::snapshot(__call);'
58             print '        }'
59             print '    }'
60
61     def wrap_ret(self, function, instance):
62         GlTracer.wrap_ret(self, function, instance)
63
64         if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
65             print '    %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
66
67
68 if __name__ == '__main__':
69     print
70     print '#include <stdlib.h>'
71     print '#include <string.h>'
72     print
73     print '#ifndef _GNU_SOURCE'
74     print '#define _GNU_SOURCE // for dladdr'
75     print '#endif'
76     print '#include <dlfcn.h>'
77     print
78     print '#include "trace_writer.hpp"'
79     print
80     print '// To validate our prototypes'
81     print '#define GL_GLEXT_PROTOTYPES'
82     print '#define GLX_GLXEXT_PROTOTYPES'
83     print
84     print '#include "glproc.hpp"'
85     print '#include "glsize.hpp"'
86     print '#include "glsnapshot.hpp"'
87     print
88     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
89     print
90
91     api = API()
92     api.add_api(glxapi)
93     api.add_api(glapi)
94     tracer = GlxTracer()
95     tracer.trace_api(api)
96
97     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
98     print '    if (!procPtr) {'
99     print '        return procPtr;'
100     print '    }'
101     for f in api.functions:
102         ptype = function_pointer_type(f)
103         pvalue = function_pointer_value(f)
104         print '    if (!strcmp("%s", (const char *)procName)) {' % f.name
105         print '        %s = (%s)procPtr;' % (pvalue, ptype)
106         print '        return (__GLXextFuncPtr)&%s;' % (f.name,)
107         print '    }'
108     print '    OS::DebugMessage("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
109     print '    return procPtr;'
110     print '}'
111     print
112     print r'''
113
114
115 /*
116  * Handle to the true libGL.so
117  */
118 static void *libgl_handle = NULL;
119
120
121 /*
122  * Invoke the true dlopen() function.
123  */
124 static void *__dlopen(const char *filename, int flag)
125 {
126     typedef void * (*PFNDLOPEN)(const char *, int);
127     static PFNDLOPEN dlopen_ptr = NULL;
128
129     if (!dlopen_ptr) {
130         dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
131         if (!dlopen_ptr) {
132             OS::DebugMessage("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
133             return NULL;
134         }
135     }
136
137     return dlopen_ptr(filename, flag);
138 }
139
140
141 /*
142  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
143  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
144  * we need to intercept the dlopen() call here, and redirect to our wrapper
145  * shared object.
146  */
147 extern "C" PUBLIC
148 void * dlopen(const char *filename, int flag)
149 {
150     void *handle;
151
152     handle = __dlopen(filename, flag);
153
154     const char * libgl_filename = getenv("TRACE_LIBGL");
155
156     if (filename && handle && !libgl_filename) {
157         if (0) {
158             OS::DebugMessage("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
159         }
160
161         // FIXME: handle absolute paths and other versions
162         if (strcmp(filename, "libGL.so") == 0 ||
163             strcmp(filename, "libGL.so.1") == 0) {
164
165             // Use the true libGL.so handle instead of RTLD_NEXT from now on
166             libgl_handle = handle;
167
168             // Get the file path for our shared object, and use it instead
169             static int dummy = 0xdeedbeef;
170             Dl_info info;
171             if (dladdr(&dummy, &info)) {
172                 OS::DebugMessage("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
173                 handle = __dlopen(info.dli_fname, flag);
174             } else {
175                 OS::DebugMessage("apitrace: warning: dladdr() failed\n");
176             }
177         }
178     }
179
180     return handle;
181 }
182
183
184 /*
185  * Lookup a libGL symbol
186  */
187 void * __libgl_sym(const char *symbol)
188 {
189     void *result;
190
191     if (!libgl_handle) {
192         /*
193          * The app doesn't directly link against libGL.so, nor does it directly
194          * dlopen it.  So we have to load it ourselves.
195          */
196
197         const char * libgl_filename = getenv("TRACE_LIBGL");
198
199         if (!libgl_filename) {
200             /*
201              * Try to use whatever libGL.so the library is linked against.
202              */
203
204             result = dlsym(RTLD_NEXT, symbol);
205             if (result) {
206                 libgl_handle = RTLD_NEXT;
207                 return result;
208             }
209
210             libgl_filename = "libGL.so.1";
211         }
212
213         /*
214          * It would have been preferable to use RTLD_LOCAL to ensure that the
215          * application can never access libGL.so symbols directly, but this
216          * won't work, given libGL.so often loads a driver specific SO and
217          * exposes symbols to it.
218          */
219
220         libgl_handle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
221         if (!libgl_handle) {
222             OS::DebugMessage("apitrace: error: couldn't find libGL.so\n");
223             return NULL;
224         }
225     }
226
227     return dlsym(libgl_handle, symbol);
228 }
229
230
231 '''