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 specs.stdapi import API
32 from specs.glapi import glapi
33 from specs.glxapi import glxapi
34 from gltrace import GlTracer
35 from dispatch import function_pointer_type, function_pointer_value
38 class GlxTracer(GlTracer):
40 def isFunctionPublic(self, function):
41 # The symbols visible in libGL.so can vary, so expose them all
44 def wrapRet(self, function, instance):
45 GlTracer.wrapRet(self, function, instance)
47 if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
48 print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
51 if __name__ == '__main__':
53 print '#include <stdlib.h>'
54 print '#include <string.h>'
56 print '#ifndef _GNU_SOURCE'
57 print '#define _GNU_SOURCE // for dladdr'
59 print '#include <dlfcn.h>'
61 print '#include "trace_writer_local.hpp"'
63 print '// To validate our prototypes'
64 print '#define GL_GLEXT_PROTOTYPES'
65 print '#define GLX_GLXEXT_PROTOTYPES'
67 print '#include "glproc.hpp"'
68 print '#include "glsize.hpp"'
70 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
79 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
80 print ' if (!procPtr) {'
81 print ' return procPtr;'
83 for f in api.functions:
84 ptype = function_pointer_type(f)
85 pvalue = function_pointer_value(f)
86 print ' if (strcmp("%s", (const char *)procName) == 0) {' % f.name
87 print ' %s = (%s)procPtr;' % (pvalue, ptype)
88 print ' return (__GLXextFuncPtr)&%s;' % (f.name,)
90 print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
91 print ' return procPtr;'
98 * Invoke the true dlopen() function.
100 static void *__dlopen(const char *filename, int flag)
102 typedef void * (*PFNDLOPEN)(const char *, int);
103 static PFNDLOPEN dlopen_ptr = NULL;
106 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
108 os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
113 return dlopen_ptr(filename, flag);
118 * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
119 * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
120 * we need to intercept the dlopen() call here, and redirect to our wrapper
124 void * dlopen(const char *filename, int flag)
128 handle = __dlopen(filename, flag);
130 const char * libgl_filename = getenv("TRACE_LIBGL");
132 if (filename && handle && !libgl_filename) {
134 os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
137 // FIXME: handle absolute paths and other versions
138 if (strcmp(filename, "libGL.so") == 0 ||
139 strcmp(filename, "libGL.so.1") == 0) {
141 // Use the true libGL.so handle instead of RTLD_NEXT from now on
142 __libGlHandle = handle;
144 // Get the file path for our shared object, and use it instead
145 static int dummy = 0xdeedbeef;
147 if (dladdr(&dummy, &info)) {
148 os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
149 handle = __dlopen(info.dli_fname, flag);
151 os::log("apitrace: warning: dladdr() failed\n");