]> git.cworth.org Git - apitrace/blob - wrappers/glxtrace.py
Define _GNU_SOURCE for the whole tree.
[apitrace] / wrappers / 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 gltrace import GlTracer
32 from specs.stdapi import Module, API
33 from specs.glapi import glapi
34 from specs.glxapi import glxapi
35
36
37 class GlxTracer(GlTracer):
38
39     def isFunctionPublic(self, function):
40         # The symbols visible in libGL.so can vary, so expose them all
41         return True
42
43     getProcAddressFunctionNames = [
44         "glXGetProcAddress",
45         "glXGetProcAddressARB",
46     ]
47
48     createContextFunctionNames = [
49         'glXCreateContext',
50         'glXCreateContextAttribsARB',
51         'glXCreateContextWithConfigSGIX',
52         'glXCreateNewContext',
53     ]
54
55     destroyContextFunctionNames = [
56         'glXDestroyContext',
57     ]
58
59     makeCurrentFunctionNames = [
60         'glXMakeCurrent',
61         'glXMakeContextCurrent',
62         'glXMakeCurrentReadSGI',
63     ]
64
65     def traceFunctionImplBody(self, function):
66         if function.name in self.destroyContextFunctionNames:
67             print '    gltrace::releaseContext((uintptr_t)ctx);'
68
69         GlTracer.traceFunctionImplBody(self, function)
70
71         if function.name in self.createContextFunctionNames:
72             print '    if (_result != NULL)'
73             print '        gltrace::createContext((uintptr_t)_result);'
74
75         if function.name in self.makeCurrentFunctionNames:
76             print '    if (_result) {'
77             print '        if (ctx != NULL)'
78             print '            gltrace::setContext((uintptr_t)ctx);'
79             print '        else'
80             print '            gltrace::clearContext();'
81             print '    }'
82
83
84 if __name__ == '__main__':
85     print
86     print '#include <stdlib.h>'
87     print '#include <string.h>'
88     print
89     print '#include <dlfcn.h>'
90     print
91     print '#include "trace_writer_local.hpp"'
92     print
93     print '// To validate our prototypes'
94     print '#define GL_GLEXT_PROTOTYPES'
95     print '#define GLX_GLXEXT_PROTOTYPES'
96     print
97     print '#include "glproc.hpp"'
98     print '#include "glsize.hpp"'
99     print
100
101     module = Module()
102     module.mergeModule(glxapi)
103     module.mergeModule(glapi)
104     api = API()
105     api.addModule(module)
106     tracer = GlxTracer()
107     tracer.traceApi(api)
108
109     print r'''
110
111
112 /*
113  * Invoke the true dlopen() function.
114  */
115 static void *_dlopen(const char *filename, int flag)
116 {
117     typedef void * (*PFN_DLOPEN)(const char *, int);
118     static PFN_DLOPEN dlopen_ptr = NULL;
119
120     if (!dlopen_ptr) {
121         dlopen_ptr = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
122         if (!dlopen_ptr) {
123             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
124             return NULL;
125         }
126     }
127
128     return dlopen_ptr(filename, flag);
129 }
130
131
132 /*
133  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
134  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
135  * we need to intercept the dlopen() call here, and redirect to our wrapper
136  * shared object.
137  */
138 extern "C" PUBLIC
139 void * dlopen(const char *filename, int flag)
140 {
141     void *handle;
142
143     handle = _dlopen(filename, flag);
144
145     const char * libgl_filename = getenv("TRACE_LIBGL");
146
147     if (filename && handle && !libgl_filename) {
148         if (0) {
149             os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
150         }
151
152         // FIXME: handle absolute paths and other versions
153         if (strcmp(filename, "libGL.so") == 0 ||
154             strcmp(filename, "libGL.so.1") == 0) {
155
156             // Use the true libGL.so handle instead of RTLD_NEXT from now on
157             _libGlHandle = handle;
158
159             // Get the file path for our shared object, and use it instead
160             static int dummy = 0xdeedbeef;
161             Dl_info info;
162             if (dladdr(&dummy, &info)) {
163                 os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
164                 handle = _dlopen(info.dli_fname, flag);
165             } else {
166                 os::log("apitrace: warning: dladdr() failed\n");
167             }
168         }
169     }
170
171     return handle;
172 }
173
174
175
176 '''