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