blob: 78d6146e8dadf91b5e3629f5777f38bac83f297e [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é Fonseca8fbdd3a2010-11-23 20:55:07 +000044 def get_function_address(self, function):
José Fonseca68ec4122011-02-20 11:25:25 +000045 return '__%s' % (function.name,)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000046
47 def wrap_ret(self, function, instance):
José Fonseca14c21bc2011-02-20 23:32:22 +000048 if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
49 print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000050
51
52if __name__ == '__main__':
53 print
54 print '#include <stdlib.h>'
55 print '#include <string.h>'
José Fonseca85a20682011-04-16 13:41:24 +010056 print
57 print '#ifndef _GNU_SOURCE'
58 print '#define _GNU_SOURCE // for dladdr'
59 print '#endif'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000060 print '#include <dlfcn.h>'
José Fonsecad3b80592010-11-29 11:51:23 +000061 print
José Fonsecaf2efcea2010-11-26 11:35:54 +000062 print '#include "trace_write.hpp"'
José Fonseca68ec4122011-02-20 11:25:25 +000063 print
64 print '#include "glproc.hpp"'
José Fonseca48e37fe2010-11-25 12:15:17 +000065 print '#include "glsize.hpp"'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000066 print
José Fonseca14c21bc2011-02-20 23:32:22 +000067 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
68 print
69
José Fonseca68ec4122011-02-20 11:25:25 +000070 api = API()
71 api.add_api(glxapi)
72 api.add_api(glapi)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000073 tracer = GlxTracer()
José Fonseca68ec4122011-02-20 11:25:25 +000074 tracer.trace_api(api)
José Fonseca14c21bc2011-02-20 23:32:22 +000075
76 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
77 print ' if (!procPtr) {'
78 print ' return procPtr;'
79 print ' }'
80 for f in api.functions:
81 ptype = function_pointer_type(f)
82 pvalue = function_pointer_value(f)
José Fonsecadbaae492011-04-21 09:28:10 +010083 print ' if (!strcmp("%s", (const char *)procName)) {' % f.name
José Fonseca14c21bc2011-02-20 23:32:22 +000084 print ' %s = (%s)procPtr;' % (pvalue, ptype)
85 print ' return (__GLXextFuncPtr)&%s;' % (f.name,)
86 print ' }'
87 print ' return procPtr;'
88 print '}'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000089 print
José Fonseca85a20682011-04-16 13:41:24 +010090 print r'''
91
José Fonseca68b14052011-04-21 10:32:45 +010092
José Fonsecac9e80092011-04-23 10:46:21 +010093/*
94 * Handle to the true libGL.so
95 */
José Fonseca68b14052011-04-21 10:32:45 +010096static void *libgl_handle = NULL;
97
98
José Fonseca85a20682011-04-16 13:41:24 +010099/*
José Fonseca68b14052011-04-21 10:32:45 +0100100 * Invoke the true dlopen() function.
José Fonseca85a20682011-04-16 13:41:24 +0100101 */
José Fonseca68b14052011-04-21 10:32:45 +0100102static void *__dlopen(const char *filename, int flag)
José Fonseca85a20682011-04-16 13:41:24 +0100103{
104 typedef void * (*PFNDLOPEN)(const char *, int);
105 static PFNDLOPEN dlopen_ptr = NULL;
José Fonseca85a20682011-04-16 13:41:24 +0100106
107 if (!dlopen_ptr) {
108 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
109 if (!dlopen_ptr) {
110 OS::DebugMessage("error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
111 return NULL;
112 }
113 }
114
José Fonseca68b14052011-04-21 10:32:45 +0100115 return dlopen_ptr(filename, flag);
116}
117
118
119/*
120 * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
121 * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
122 * we need to intercept the dlopen() call here, and redirect to our wrapper
123 * shared object.
124 */
José Fonseca23691292011-04-22 10:40:25 +0100125extern "C" PUBLIC
126void * dlopen(const char *filename, int flag)
José Fonseca68b14052011-04-21 10:32:45 +0100127{
128 void *handle;
129
130 handle = __dlopen(filename, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100131
José Fonsecac9e80092011-04-23 10:46:21 +0100132 const char * libgl_filename = getenv("TRACE_LIBGL");
133
134 if (filename && handle && !libgl_filename) {
José Fonseca85a20682011-04-16 13:41:24 +0100135 if (0) {
136 OS::DebugMessage("warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
137 }
138
139 // FIXME: handle absolute paths and other versions
José Fonseca94215532011-04-17 22:30:26 +0100140 if (strcmp(filename, "libGL.so") == 0 ||
141 strcmp(filename, "libGL.so.1") == 0) {
José Fonsecac9e80092011-04-23 10:46:21 +0100142
José Fonseca85a20682011-04-16 13:41:24 +0100143 // Use the true libGL.so handle instead of RTLD_NEXT from now on
144 libgl_handle = handle;
145
146 // Get the file path for our shared object, and use it instead
147 static int dummy = 0xdeedbeef;
148 Dl_info info;
149 if (dladdr(&dummy, &info)) {
150 OS::DebugMessage("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
José Fonseca68b14052011-04-21 10:32:45 +0100151 handle = __dlopen(info.dli_fname, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100152 } else {
153 OS::DebugMessage("warning: dladdr() failed\n");
154 }
155 }
156 }
157
158 return handle;
159}
160
José Fonseca68b14052011-04-21 10:32:45 +0100161
162/*
163 * Lookup a libGL symbol
164 */
165static void * __dlsym(const char *symbol)
166{
167 void *result;
168
169 if (!libgl_handle) {
170 /*
José Fonseca68b14052011-04-21 10:32:45 +0100171 * The app doesn't directly link against libGL.so, nor does it directly
172 * dlopen it. So we have to load it ourselves.
173 */
José Fonsecac9e80092011-04-23 10:46:21 +0100174
175 const char * libgl_filename = getenv("TRACE_LIBGL");
176
177 if (!libgl_filename) {
178 /*
179 * Try to use whatever libGL.so the library is linked against.
180 */
181
182 result = dlsym(RTLD_NEXT, symbol);
183 if (result) {
184 libgl_handle = RTLD_NEXT;
185 return result;
186 }
187
188 libgl_filename = "libGL.so.1";
189 }
190
191 /*
192 * It would have been preferable to use RTLD_LOCAL to ensure that the
193 * application can never access libGL.so symbols directly, but this
194 * won't work, given libGL.so often loads a driver specific SO and
195 * exposes symbols to it.
196 */
197
198 libgl_handle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
José Fonseca68b14052011-04-21 10:32:45 +0100199 if (!libgl_handle) {
200 OS::DebugMessage("error: couldn't find libGL.so\n");
201 return NULL;
202 }
203 }
204
205 return dlsym(libgl_handle, symbol);
206}
207
208
José Fonseca85a20682011-04-16 13:41:24 +0100209'''