1 ##########################################################################
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2010 VMware, Inc.
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
28 """GLX tracing generator."""
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
38 class GlxTracer(GlTracer):
40 def is_public_function(self, function):
41 # The symbols visible in libGL.so can vary, so expose them all
44 def get_function_address(self, function):
45 return '__%s' % (function.name,)
47 def wrap_ret(self, function, instance):
48 GlTracer.wrap_ret(self, function, instance)
50 if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
51 print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
54 if __name__ == '__main__':
56 print '#include <stdlib.h>'
57 print '#include <string.h>'
59 print '#ifndef _GNU_SOURCE'
60 print '#define _GNU_SOURCE // for dladdr'
62 print '#include <dlfcn.h>'
64 print '#include "trace_write.hpp"'
66 print '#include "glproc.hpp"'
67 print '#include "glsize.hpp"'
69 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
78 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
79 print ' if (!procPtr) {'
80 print ' return procPtr;'
82 for f in api.functions:
83 ptype = function_pointer_type(f)
84 pvalue = function_pointer_value(f)
85 print ' if (!strcmp("%s", (const char *)procName)) {' % f.name
86 print ' %s = (%s)procPtr;' % (pvalue, ptype)
87 print ' return (__GLXextFuncPtr)&%s;' % (f.name,)
89 print ' return procPtr;'
96 * Handle to the true libGL.so
98 static void *libgl_handle = NULL;
102 * Invoke the true dlopen() function.
104 static void *__dlopen(const char *filename, int flag)
106 typedef void * (*PFNDLOPEN)(const char *, int);
107 static PFNDLOPEN dlopen_ptr = NULL;
110 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
112 OS::DebugMessage("error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
117 return dlopen_ptr(filename, flag);
122 * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
123 * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
124 * we need to intercept the dlopen() call here, and redirect to our wrapper
128 void * dlopen(const char *filename, int flag)
132 handle = __dlopen(filename, flag);
134 const char * libgl_filename = getenv("TRACE_LIBGL");
136 if (filename && handle && !libgl_filename) {
138 OS::DebugMessage("warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
141 // FIXME: handle absolute paths and other versions
142 if (strcmp(filename, "libGL.so") == 0 ||
143 strcmp(filename, "libGL.so.1") == 0) {
145 // Use the true libGL.so handle instead of RTLD_NEXT from now on
146 libgl_handle = handle;
148 // Get the file path for our shared object, and use it instead
149 static int dummy = 0xdeedbeef;
151 if (dladdr(&dummy, &info)) {
152 OS::DebugMessage("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
153 handle = __dlopen(info.dli_fname, flag);
155 OS::DebugMessage("warning: dladdr() failed\n");
165 * Lookup a libGL symbol
167 static void * __dlsym(const char *symbol)
173 * The app doesn't directly link against libGL.so, nor does it directly
174 * dlopen it. So we have to load it ourselves.
177 const char * libgl_filename = getenv("TRACE_LIBGL");
179 if (!libgl_filename) {
181 * Try to use whatever libGL.so the library is linked against.
184 result = dlsym(RTLD_NEXT, symbol);
186 libgl_handle = RTLD_NEXT;
190 libgl_filename = "libGL.so.1";
194 * It would have been preferable to use RTLD_LOCAL to ensure that the
195 * application can never access libGL.so symbols directly, but this
196 * won't work, given libGL.so often loads a driver specific SO and
197 * exposes symbols to it.
200 libgl_handle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
202 OS::DebugMessage("error: couldn't find libGL.so\n");
207 return dlsym(libgl_handle, symbol);