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