]> git.cworth.org Git - apitrace/blob - glxtrace.py
Size the image widget more reasonably.
[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         if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"):
49             print '    %s = __unwrap_proc_addr(procName, %s);' % (instance, instance)
50
51
52 if __name__ == '__main__':
53     print
54     print '#include <stdlib.h>'
55     print '#include <string.h>'
56     print
57     print '#ifndef _GNU_SOURCE'
58     print '#define _GNU_SOURCE // for dladdr'
59     print '#endif'
60     print '#include <dlfcn.h>'
61     print
62     print '#include "trace_write.hpp"'
63     print
64     print '#include "glproc.hpp"'
65     print '#include "glsize.hpp"'
66     print
67     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);'
68     print
69
70     api = API()
71     api.add_api(glxapi)
72     api.add_api(glapi)
73     tracer = GlxTracer()
74     tracer.trace_api(api)
75
76     print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr) {'
77     print '    if (!procPtr) {'
78     print '        return procPtr;'
79     print '    }'
80     for f in api.functions:
81         ptype = function_pointer_type(f)
82         pvalue = function_pointer_value(f)
83         print '    if (!strcmp("%s", (const char *)procName)) {' % f.name
84         print '        %s = (%s)procPtr;' % (pvalue, ptype)
85         print '        return (__GLXextFuncPtr)&%s;' % (f.name,)
86         print '    }'
87     print '    return procPtr;'
88     print '}'
89     print
90     print r'''
91
92
93 static void *libgl_handle = NULL;
94
95
96 /*
97  * Invoke the true dlopen() function.
98  */
99 static void *__dlopen(const char *filename, int flag)
100 {
101     typedef void * (*PFNDLOPEN)(const char *, int);
102     static PFNDLOPEN dlopen_ptr = NULL;
103
104     if (!dlopen_ptr) {
105         dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
106         if (!dlopen_ptr) {
107             OS::DebugMessage("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     if (filename && handle) {
130         if (0) {
131             OS::DebugMessage("warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
132         }
133
134         // FIXME: handle absolute paths and other versions
135         if (strcmp(filename, "libGL.so") == 0 ||
136             strcmp(filename, "libGL.so.1") == 0) {
137             // Use the true libGL.so handle instead of RTLD_NEXT from now on
138             libgl_handle = handle;
139
140             // Get the file path for our shared object, and use it instead
141             static int dummy = 0xdeedbeef;
142             Dl_info info;
143             if (dladdr(&dummy, &info)) {
144                 OS::DebugMessage("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
145                 handle = __dlopen(info.dli_fname, flag);
146             } else {
147                 OS::DebugMessage("warning: dladdr() failed\n");
148             }
149         }
150     }
151
152     return handle;
153 }
154
155
156 /*
157  * Lookup a libGL symbol
158  */
159 static void * __dlsym(const char *symbol)
160 {
161     void *result;
162
163     if (!libgl_handle) {
164         /*
165          * Try to use whatever libGL.so the library is linked against.
166          */
167         result = dlsym(RTLD_NEXT, symbol);
168         if (result) {
169             libgl_handle = RTLD_NEXT;
170             return result;
171         }
172
173         /*
174          * The app doesn't directly link against libGL.so, nor does it directly
175          * dlopen it.  So we have to load it ourselves.
176          */
177         libgl_handle = __dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL);
178         if (!libgl_handle) {
179             OS::DebugMessage("error: couldn't find libGL.so\n");
180             return NULL;
181         }
182     }
183
184     return dlsym(libgl_handle, symbol);
185 }
186
187
188 '''