blob: 9adcaaf98a50f8b89b5ff39650b5018e92662b4f [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é Fonsecabd86a222011-09-27 09:21:38 +010031from specs.stdapi import API
32from specs.glapi import glapi
33from specs.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 wrap_ret(self, function, instance):
José Fonseca867b1b72011-04-24 11:58:04 +010045 GlTracer.wrap_ret(self, function, instance)
46
José Fonseca14c21bc2011-02-20 23:32:22 +000047 if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
48 print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000049
50
51if __name__ == '__main__':
52 print
53 print '#include <stdlib.h>'
54 print '#include <string.h>'
José Fonseca85a20682011-04-16 13:41:24 +010055 print
56 print '#ifndef _GNU_SOURCE'
57 print '#define _GNU_SOURCE // for dladdr'
58 print '#endif'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000059 print '#include <dlfcn.h>'
José Fonsecad3b80592010-11-29 11:51:23 +000060 print
José Fonsecaa0e612d2011-12-27 19:02:57 +000061 print '#include "trace_writer_local.hpp"'
José Fonseca68ec4122011-02-20 11:25:25 +000062 print
José Fonsecaa3b3d862011-05-08 10:45:05 +010063 print '// To validate our prototypes'
64 print '#define GL_GLEXT_PROTOTYPES'
65 print '#define GLX_GLXEXT_PROTOTYPES'
66 print
José Fonseca68ec4122011-02-20 11:25:25 +000067 print '#include "glproc.hpp"'
José Fonseca48e37fe2010-11-25 12:15:17 +000068 print '#include "glsize.hpp"'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000069 print
José Fonseca14c21bc2011-02-20 23:32:22 +000070 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
71 print
72
José Fonseca68ec4122011-02-20 11:25:25 +000073 api = API()
74 api.add_api(glxapi)
75 api.add_api(glapi)
José Fonseca8fbdd3a2010-11-23 20:55:07 +000076 tracer = GlxTracer()
José Fonseca68ec4122011-02-20 11:25:25 +000077 tracer.trace_api(api)
José Fonseca14c21bc2011-02-20 23:32:22 +000078
79 print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
80 print ' if (!procPtr) {'
81 print ' return procPtr;'
82 print ' }'
83 for f in api.functions:
84 ptype = function_pointer_type(f)
85 pvalue = function_pointer_value(f)
José Fonseca8f34d342011-07-15 20:16:40 +010086 print ' if (strcmp("%s", (const char *)procName) == 0) {' % f.name
José Fonseca14c21bc2011-02-20 23:32:22 +000087 print ' %s = (%s)procPtr;' % (pvalue, ptype)
88 print ' return (__GLXextFuncPtr)&%s;' % (f.name,)
89 print ' }'
José Fonseca559d5342011-10-27 08:10:56 +010090 print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
José Fonseca14c21bc2011-02-20 23:32:22 +000091 print ' return procPtr;'
92 print '}'
José Fonseca8fbdd3a2010-11-23 20:55:07 +000093 print
José Fonseca85a20682011-04-16 13:41:24 +010094 print r'''
95
José Fonseca68b14052011-04-21 10:32:45 +010096
José Fonsecac9e80092011-04-23 10:46:21 +010097/*
José Fonseca68b14052011-04-21 10:32:45 +010098 * Invoke the true dlopen() function.
José Fonseca85a20682011-04-16 13:41:24 +010099 */
José Fonseca68b14052011-04-21 10:32:45 +0100100static void *__dlopen(const char *filename, int flag)
José Fonseca85a20682011-04-16 13:41:24 +0100101{
102 typedef void * (*PFNDLOPEN)(const char *, int);
103 static PFNDLOPEN dlopen_ptr = NULL;
José Fonseca85a20682011-04-16 13:41:24 +0100104
105 if (!dlopen_ptr) {
106 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
107 if (!dlopen_ptr) {
José Fonseca559d5342011-10-27 08:10:56 +0100108 os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
José Fonseca85a20682011-04-16 13:41:24 +0100109 return NULL;
110 }
111 }
112
José Fonseca68b14052011-04-21 10:32:45 +0100113 return dlopen_ptr(filename, flag);
114}
115
116
117/*
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
121 * shared object.
122 */
José Fonseca23691292011-04-22 10:40:25 +0100123extern "C" PUBLIC
124void * dlopen(const char *filename, int flag)
José Fonseca68b14052011-04-21 10:32:45 +0100125{
126 void *handle;
127
128 handle = __dlopen(filename, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100129
José Fonsecac9e80092011-04-23 10:46:21 +0100130 const char * libgl_filename = getenv("TRACE_LIBGL");
131
132 if (filename && handle && !libgl_filename) {
José Fonseca85a20682011-04-16 13:41:24 +0100133 if (0) {
José Fonseca559d5342011-10-27 08:10:56 +0100134 os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100135 }
136
137 // FIXME: handle absolute paths and other versions
José Fonseca94215532011-04-17 22:30:26 +0100138 if (strcmp(filename, "libGL.so") == 0 ||
139 strcmp(filename, "libGL.so.1") == 0) {
José Fonsecac9e80092011-04-23 10:46:21 +0100140
José Fonseca85a20682011-04-16 13:41:24 +0100141 // Use the true libGL.so handle instead of RTLD_NEXT from now on
José Fonseca1cfd89b2011-12-01 21:09:57 +0000142 __libGlHandle = handle;
José Fonseca85a20682011-04-16 13:41:24 +0100143
144 // Get the file path for our shared object, and use it instead
145 static int dummy = 0xdeedbeef;
146 Dl_info info;
147 if (dladdr(&dummy, &info)) {
José Fonseca559d5342011-10-27 08:10:56 +0100148 os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
José Fonseca68b14052011-04-21 10:32:45 +0100149 handle = __dlopen(info.dli_fname, flag);
José Fonseca85a20682011-04-16 13:41:24 +0100150 } else {
José Fonseca559d5342011-10-27 08:10:56 +0100151 os::log("apitrace: warning: dladdr() failed\n");
José Fonseca85a20682011-04-16 13:41:24 +0100152 }
153 }
154 }
155
156 return handle;
157}
158
José Fonseca68b14052011-04-21 10:32:45 +0100159
José Fonseca68b14052011-04-21 10:32:45 +0100160
José Fonseca85a20682011-04-16 13:41:24 +0100161'''