blob: 6b5d786b6d91ff16f1562ceb9790f544481d51ed [file] [log] [blame]
José Fonseca8fbdd3a2010-11-23 20:55:07 +00001##########################################################################
2#
José Fonseca23691292011-04-22 10:40:25 +01003# Copyright 2011 Jose Fonseca
José Fonseca99771fc2010-11-25 20:32:59 +00004# Copyright 2008-2010 VMware, Inc.
José Fonseca8fbdd3a2010-11-23 20:55:07 +00005# 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
José Fonseca4a826ed2010-11-30 16:58:22 +000028"""GLX tracing generator."""
29
30
José Fonseca68ec4122011-02-20 11:25:25 +000031from stdapi import API
32from glapi import glapi
José Fonseca99771fc2010-11-25 20:32:59 +000033from glxapi import glxapi
José Fonseca669b1222011-02-20 09:05:10 +000034from gltrace import GlTracer
José Fonseca669b2002011-02-20 13:32:19 +000035from dispatch import function_pointer_type, function_pointer_value
José Fonseca8fbdd3a2010-11-23 20:55:07 +000036
37
José Fonseca669b1222011-02-20 09:05:10 +000038class GlxTracer(GlTracer):
José Fonseca8fbdd3a2010-11-23 20:55:07 +000039
José Fonseca23691292011-04-22 10:40:25 +010040 def is_public_function(self, function):
41 # The symbols visible in libGL.so can vary, so expose them all
42 return True
43
José Fonsecad46b8d32011-06-13 10:18:31 +010044 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
José Fonseca8fbdd3a2010-11-23 20:55:07 +000061 def wrap_ret(self, function, instance):
José Fonseca867b1b72011-04-24 11:58:04 +010062 GlTracer.wrap_ret(self, function, instance)
63
José Fonseca14c21bc2011-02-20 23:32:22 +000064 if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
65 print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000066
67
68if __name__ == '__main__':
69 print
70 print '#include <stdlib.h>'
71 print '#include <string.h>'
José Fonseca85a20682011-04-16 13:41:24 +010072 print
73 print '#ifndef _GNU_SOURCE'
74 print '#define _GNU_SOURCE // for dladdr'
75 print '#endif'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000076 print '#include <dlfcn.h>'
José Fonsecad3b80592010-11-29 11:51:23 +000077 print
José Fonseca9ed67222011-05-24 08:42:59 +010078 print '#include "trace_writer.hpp"'
José Fonseca68ec4122011-02-20 11:25:25 +000079 print
José Fonsecaa3b3d862011-05-08 10:45:05 +010080 print '// To validate our prototypes'
81 print '#define GL_GLEXT_PROTOTYPES'
82 print '#define GLX_GLXEXT_PROTOTYPES'
83 print
José Fonseca68ec4122011-02-20 11:25:25 +000084 print '#include "glproc.hpp"'
José Fonseca48e37fe2010-11-25 12:15:17 +000085 print '#include "glsize.hpp"'
José Fonsecad46b8d32011-06-13 10:18:31 +010086 print '#include "glsnapshot.hpp"'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000087 print
José Fonseca14c21bc2011-02-20 23:32:22 +000088 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
89 print
90
José Fonseca68ec4122011-02-20 11:25:25 +000091 api = API()
92 api.add_api(glxapi)
93 api.add_api(glapi)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000094 tracer = GlxTracer()
José Fonseca68ec4122011-02-20 11:25:25 +000095 tracer.trace_api(api)
José Fonseca14c21bc2011-02-20 23:32:22 +000096
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)
José Fonseca8f34d342011-07-15 20:16:40 +0100104 print ' if (strcmp("%s", (const char *)procName) == 0) {' % f.name
José Fonseca14c21bc2011-02-20 23:32:22 +0000105 print ' %s = (%s)procPtr;' % (pvalue, ptype)
106 print ' return (__GLXextFuncPtr)&%s;' % (f.name,)
107 print ' }'
José Fonsecacbd225f2011-06-09 00:07:18 +0100108 print ' OS::DebugMessage("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
José Fonseca14c21bc2011-02-20 23:32:22 +0000109 print ' return procPtr;'
110 print '}'
José Fonseca8fbdd3a2010-11-23 20:55:07 +0000111 print
José Fonseca85a20682011-04-16 13:41:24 +0100112 print r'''
113
José Fonseca68b14052011-04-21 10:32:45 +0100114
José Fonsecac9e80092011-04-23 10:46:21 +0100115/*
116 * Handle to the true libGL.so
117 */
José Fonseca68b14052011-04-21 10:32:45 +0100118static void *libgl_handle = NULL;
119
120
José Fonseca85a20682011-04-16 13:41:24 +0100121/*
José Fonseca68b14052011-04-21 10:32:45 +0100122 * Invoke the true dlopen() function.
José Fonseca85a20682011-04-16 13:41:24 +0100123 */
José Fonseca68b14052011-04-21 10:32:45 +0100124static void *__dlopen(const char *filename, int flag)
José Fonseca85a20682011-04-16 13:41:24 +0100125{
126 typedef void * (*PFNDLOPEN)(const char *, int);
127 static PFNDLOPEN dlopen_ptr = NULL;
José Fonseca85a20682011-04-16 13:41:24 +0100128
129 if (!dlopen_ptr) {
130 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
131 if (!dlopen_ptr) {
José Fonsecacbd225f2011-06-09 00:07:18 +0100132 OS::DebugMessage("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
José Fonseca85a20682011-04-16 13:41:24 +0100133 return NULL;
134 }
135 }
136
José Fonseca68b14052011-04-21 10:32:45 +0100137 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 */
José Fonseca23691292011-04-22 10:40:25 +0100147extern "C" PUBLIC
148void * dlopen(const char *filename, int flag)
José Fonseca68b14052011-04-21 10:32:45 +0100149{
150 void *handle;
151
152 handle = __dlopen(filename, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100153
José Fonsecac9e80092011-04-23 10:46:21 +0100154 const char * libgl_filename = getenv("TRACE_LIBGL");
155
156 if (filename && handle && !libgl_filename) {
José Fonseca85a20682011-04-16 13:41:24 +0100157 if (0) {
José Fonsecacbd225f2011-06-09 00:07:18 +0100158 OS::DebugMessage("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100159 }
160
161 // FIXME: handle absolute paths and other versions
José Fonseca94215532011-04-17 22:30:26 +0100162 if (strcmp(filename, "libGL.so") == 0 ||
163 strcmp(filename, "libGL.so.1") == 0) {
José Fonsecac9e80092011-04-23 10:46:21 +0100164
José Fonseca85a20682011-04-16 13:41:24 +0100165 // 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);
José Fonseca68b14052011-04-21 10:32:45 +0100173 handle = __dlopen(info.dli_fname, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100174 } else {
José Fonsecacbd225f2011-06-09 00:07:18 +0100175 OS::DebugMessage("apitrace: warning: dladdr() failed\n");
José Fonseca85a20682011-04-16 13:41:24 +0100176 }
177 }
178 }
179
180 return handle;
181}
182
José Fonseca68b14052011-04-21 10:32:45 +0100183
184/*
185 * Lookup a libGL symbol
186 */
José Fonsecac7b1c2a2011-06-11 12:26:39 +0100187void * __libgl_sym(const char *symbol)
José Fonseca68b14052011-04-21 10:32:45 +0100188{
189 void *result;
190
191 if (!libgl_handle) {
192 /*
José Fonseca68b14052011-04-21 10:32:45 +0100193 * The app doesn't directly link against libGL.so, nor does it directly
194 * dlopen it. So we have to load it ourselves.
195 */
José Fonsecac9e80092011-04-23 10:46:21 +0100196
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);
José Fonseca68b14052011-04-21 10:32:45 +0100221 if (!libgl_handle) {
José Fonsecacbd225f2011-06-09 00:07:18 +0100222 OS::DebugMessage("apitrace: error: couldn't find libGL.so\n");
José Fonseca68b14052011-04-21 10:32:45 +0100223 return NULL;
224 }
225 }
226
227 return dlsym(libgl_handle, symbol);
228}
229
230
José Fonseca85a20682011-04-16 13:41:24 +0100231'''