]> git.cworth.org Git - apitrace/blob - glxtrace.py
Ensure our prototypes match those in glext.
[apitrace] / glxtrace.py
1 ##########################################################################
2 #
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2010 VMware, Inc.
5 # 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
28 """GLX tracing generator."""
29
30
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
36
37
38 class GlxTracer(GlTracer):
39
40     def is_public_function(self, function):
41         # The symbols visible in libGL.so can vary, so expose them all
42         return True
43
44     def get_function_address(self, function):
45         return '__%s' % (function.name,)
46
47     def wrap_ret(self, function, instance):
48         GlTracer.wrap_ret(self, function, instance)
49
50         if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
51             print '    %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
52
53
54 if __name__ == '__main__':
55     print
56     print '#include <stdlib.h>'
57     print '#include <string.h>'
58     print
59     print '#ifndef _GNU_SOURCE'
60     print '#define _GNU_SOURCE // for dladdr'
61     print '#endif'
62     print '#include <dlfcn.h>'
63     print
64     print '#include "trace_write.hpp"'
65     print
66     print '// To validate our prototypes'
67     print '#define GL_GLEXT_PROTOTYPES'
68     print '#define GLX_GLXEXT_PROTOTYPES'
69     print
70     print '#include "glproc.hpp"'
71     print '#include "glsize.hpp"'
72     print
73     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
74     print
75
76     api = API()
77     api.add_api(glxapi)
78     api.add_api(glapi)
79     tracer = GlxTracer()
80     tracer.trace_api(api)
81
82     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
83     print '    if (!procPtr) {'
84     print '        return procPtr;'
85     print '    }'
86     for f in api.functions:
87         ptype = function_pointer_type(f)
88         pvalue = function_pointer_value(f)
89         print '    if (!strcmp("%s", (const char *)procName)) {' % f.name
90         print '        %s = (%s)procPtr;' % (pvalue, ptype)
91         print '        return (__GLXextFuncPtr)&%s;' % (f.name,)
92         print '    }'
93     print '    return procPtr;'
94     print '}'
95     print
96     print r'''
97
98
99 /*
100  * Handle to the true libGL.so
101  */
102 static void *libgl_handle = NULL;
103
104
105 /*
106  * Invoke the true dlopen() function.
107  */
108 static void *__dlopen(const char *filename, int flag)
109 {
110     typedef void * (*PFNDLOPEN)(const char *, int);
111     static PFNDLOPEN dlopen_ptr = NULL;
112
113     if (!dlopen_ptr) {
114         dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
115         if (!dlopen_ptr) {
116             OS::DebugMessage("error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
117             return NULL;
118         }
119     }
120
121     return dlopen_ptr(filename, flag);
122 }
123
124
125 /*
126  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
127  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
128  * we need to intercept the dlopen() call here, and redirect to our wrapper
129  * shared object.
130  */
131 extern "C" PUBLIC
132 void * dlopen(const char *filename, int flag)
133 {
134     void *handle;
135
136     handle = __dlopen(filename, flag);
137
138     const char * libgl_filename = getenv("TRACE_LIBGL");
139
140     if (filename && handle && !libgl_filename) {
141         if (0) {
142             OS::DebugMessage("warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
143         }
144
145         // FIXME: handle absolute paths and other versions
146         if (strcmp(filename, "libGL.so") == 0 ||
147             strcmp(filename, "libGL.so.1") == 0) {
148
149             // Use the true libGL.so handle instead of RTLD_NEXT from now on
150             libgl_handle = handle;
151
152             // Get the file path for our shared object, and use it instead
153             static int dummy = 0xdeedbeef;
154             Dl_info info;
155             if (dladdr(&dummy, &info)) {
156                 OS::DebugMessage("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
157                 handle = __dlopen(info.dli_fname, flag);
158             } else {
159                 OS::DebugMessage("warning: dladdr() failed\n");
160             }
161         }
162     }
163
164     return handle;
165 }
166
167
168 /*
169  * Lookup a libGL symbol
170  */
171 static void * __dlsym(const char *symbol)
172 {
173     void *result;
174
175     if (!libgl_handle) {
176         /*
177          * The app doesn't directly link against libGL.so, nor does it directly
178          * dlopen it.  So we have to load it ourselves.
179          */
180
181         const char * libgl_filename = getenv("TRACE_LIBGL");
182
183         if (!libgl_filename) {
184             /*
185              * Try to use whatever libGL.so the library is linked against.
186              */
187
188             result = dlsym(RTLD_NEXT, symbol);
189             if (result) {
190                 libgl_handle = RTLD_NEXT;
191                 return result;
192             }
193
194             libgl_filename = "libGL.so.1";
195         }
196
197         /*
198          * It would have been preferable to use RTLD_LOCAL to ensure that the
199          * application can never access libGL.so symbols directly, but this
200          * won't work, given libGL.so often loads a driver specific SO and
201          * exposes symbols to it.
202          */
203
204         libgl_handle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
205         if (!libgl_handle) {
206             OS::DebugMessage("error: couldn't find libGL.so\n");
207             return NULL;
208         }
209     }
210
211     return dlsym(libgl_handle, symbol);
212 }
213
214
215 '''