José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 1 | ########################################################################## |
| 2 | # |
José Fonseca | 2369129 | 2011-04-22 10:40:25 +0100 | [diff] [blame] | 3 | # Copyright 2011 Jose Fonseca |
José Fonseca | 99771fc | 2010-11-25 20:32:59 +0000 | [diff] [blame] | 4 | # Copyright 2008-2010 VMware, Inc. |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 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 | |
José Fonseca | 4a826ed | 2010-11-30 16:58:22 +0000 | [diff] [blame] | 28 | """GLX tracing generator.""" |
| 29 | |
| 30 | |
José Fonseca | bd86a22 | 2011-09-27 09:21:38 +0100 | [diff] [blame] | 31 | from specs.stdapi import API |
| 32 | from specs.glapi import glapi |
| 33 | from specs.glxapi import glxapi |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 34 | from gltrace import GlTracer |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 35 | from dispatch import function_pointer_type, function_pointer_value |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 36 | |
| 37 | |
José Fonseca | 669b122 | 2011-02-20 09:05:10 +0000 | [diff] [blame] | 38 | class GlxTracer(GlTracer): |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 39 | |
José Fonseca | 2369129 | 2011-04-22 10:40:25 +0100 | [diff] [blame] | 40 | def is_public_function(self, function): |
| 41 | # The symbols visible in libGL.so can vary, so expose them all |
| 42 | return True |
| 43 | |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 44 | def wrap_ret(self, function, instance): |
José Fonseca | 867b1b7 | 2011-04-24 11:58:04 +0100 | [diff] [blame] | 45 | GlTracer.wrap_ret(self, function, instance) |
| 46 | |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 47 | if function.name in ("glXGetProcAddress", "glXGetProcAddressARB"): |
| 48 | print ' %s = __unwrap_proc_addr(procName, %s);' % (instance, instance) |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | print |
| 53 | print '#include <stdlib.h>' |
| 54 | print '#include <string.h>' |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 55 | print |
| 56 | print '#ifndef _GNU_SOURCE' |
| 57 | print '#define _GNU_SOURCE // for dladdr' |
| 58 | print '#endif' |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 59 | print '#include <dlfcn.h>' |
José Fonseca | d3b8059 | 2010-11-29 11:51:23 +0000 | [diff] [blame] | 60 | print |
José Fonseca | 9ed6722 | 2011-05-24 08:42:59 +0100 | [diff] [blame] | 61 | print '#include "trace_writer.hpp"' |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 62 | print |
José Fonseca | a3b3d86 | 2011-05-08 10:45:05 +0100 | [diff] [blame] | 63 | print '// To validate our prototypes' |
| 64 | print '#define GL_GLEXT_PROTOTYPES' |
| 65 | print '#define GLX_GLXEXT_PROTOTYPES' |
| 66 | print |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 67 | print '#include "glproc.hpp"' |
José Fonseca | 48e37fe | 2010-11-25 12:15:17 +0000 | [diff] [blame] | 68 | print '#include "glsize.hpp"' |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 69 | print |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 70 | print 'static __GLXextFuncPtr __unwrap_proc_addr(const GLubyte * procName, __GLXextFuncPtr procPtr);' |
| 71 | print |
| 72 | |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 73 | api = API() |
| 74 | api.add_api(glxapi) |
| 75 | api.add_api(glapi) |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 76 | tracer = GlxTracer() |
José Fonseca | 68ec412 | 2011-02-20 11:25:25 +0000 | [diff] [blame] | 77 | tracer.trace_api(api) |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 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) |
José Fonseca | 8f34d34 | 2011-07-15 20:16:40 +0100 | [diff] [blame] | 86 | print ' if (strcmp("%s", (const char *)procName) == 0) {' % f.name |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 87 | print ' %s = (%s)procPtr;' % (pvalue, ptype) |
| 88 | print ' return (__GLXextFuncPtr)&%s;' % (f.name,) |
| 89 | print ' }' |
José Fonseca | 559d534 | 2011-10-27 08:10:56 +0100 | [diff] [blame] | 90 | print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);' |
José Fonseca | 14c21bc | 2011-02-20 23:32:22 +0000 | [diff] [blame] | 91 | print ' return procPtr;' |
| 92 | print '}' |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 93 | print |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 94 | print r''' |
| 95 | |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 96 | |
José Fonseca | c9e8009 | 2011-04-23 10:46:21 +0100 | [diff] [blame] | 97 | /* |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 98 | * Invoke the true dlopen() function. |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 99 | */ |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 100 | static void *__dlopen(const char *filename, int flag) |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 101 | { |
| 102 | typedef void * (*PFNDLOPEN)(const char *, int); |
| 103 | static PFNDLOPEN dlopen_ptr = NULL; |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 104 | |
| 105 | if (!dlopen_ptr) { |
| 106 | dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen"); |
| 107 | if (!dlopen_ptr) { |
José Fonseca | 559d534 | 2011-10-27 08:10:56 +0100 | [diff] [blame] | 108 | os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n"); |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 113 | return dlopen_ptr(filename, flag); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Several applications, such as Quake3, use dlopen("libGL.so.1"), but |
| 119 | * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore |
| 120 | * we need to intercept the dlopen() call here, and redirect to our wrapper |
| 121 | * shared object. |
| 122 | */ |
José Fonseca | 2369129 | 2011-04-22 10:40:25 +0100 | [diff] [blame] | 123 | extern "C" PUBLIC |
| 124 | void * dlopen(const char *filename, int flag) |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 125 | { |
| 126 | void *handle; |
| 127 | |
| 128 | handle = __dlopen(filename, flag); |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 129 | |
José Fonseca | c9e8009 | 2011-04-23 10:46:21 +0100 | [diff] [blame] | 130 | const char * libgl_filename = getenv("TRACE_LIBGL"); |
| 131 | |
| 132 | if (filename && handle && !libgl_filename) { |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 133 | if (0) { |
José Fonseca | 559d534 | 2011-10-27 08:10:56 +0100 | [diff] [blame] | 134 | os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag); |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // FIXME: handle absolute paths and other versions |
José Fonseca | 9421553 | 2011-04-17 22:30:26 +0100 | [diff] [blame] | 138 | if (strcmp(filename, "libGL.so") == 0 || |
| 139 | strcmp(filename, "libGL.so.1") == 0) { |
José Fonseca | c9e8009 | 2011-04-23 10:46:21 +0100 | [diff] [blame] | 140 | |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 141 | // Use the true libGL.so handle instead of RTLD_NEXT from now on |
José Fonseca | 1cfd89b | 2011-12-01 21:09:57 +0000 | [diff] [blame^] | 142 | __libGlHandle = handle; |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 143 | |
| 144 | // Get the file path for our shared object, and use it instead |
| 145 | static int dummy = 0xdeedbeef; |
| 146 | Dl_info info; |
| 147 | if (dladdr(&dummy, &info)) { |
José Fonseca | 559d534 | 2011-10-27 08:10:56 +0100 | [diff] [blame] | 148 | os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag); |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 149 | handle = __dlopen(info.dli_fname, flag); |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 150 | } else { |
José Fonseca | 559d534 | 2011-10-27 08:10:56 +0100 | [diff] [blame] | 151 | os::log("apitrace: warning: dladdr() failed\n"); |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return handle; |
| 157 | } |
| 158 | |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 159 | |
José Fonseca | 68b1405 | 2011-04-21 10:32:45 +0100 | [diff] [blame] | 160 | |
José Fonseca | 85a2068 | 2011-04-16 13:41:24 +0100 | [diff] [blame] | 161 | ''' |