1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
31 #include <unistd.h> // for symlink
37 * Handle to the true OpenGL library.
40 HINSTANCE __libGlHandle = NULL;
42 void *__libGlHandle = NULL;
50 __getPublicProcAddress(const char *procName)
53 char szDll[MAX_PATH] = {0};
55 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
59 strcat(szDll, "\\\\opengl32.dll");
61 __libGlHandle = LoadLibraryA(szDll);
63 os::log("apitrace: error: couldn't load %s\n", szDll);
68 return (void *)GetProcAddress(__libGlHandle, procName);
73 __getPrivateProcAddress(const char *procName) {
74 return (void *)__wglGetProcAddress(procName);
78 #elif defined(__APPLE__)
82 * Path to the true OpenGL framework
84 static const char *libgl_filename = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
88 * Lookup a libGL symbol
90 void * __libgl_sym(const char *symbol)
96 * Unfortunately we can't just dlopen the true dynamic library because
97 * DYLD_LIBRARY_PATH/DYLD_FRAMEWORK_PATH take precedence, even for
98 * absolute paths. So we create a temporary symlink, and dlopen that
102 char temp_filename[] = "/tmp/tmp.XXXXXX";
104 if (mktemp(temp_filename) != NULL) {
105 if (symlink(libgl_filename, temp_filename) == 0) {
106 __libGlHandle = dlopen(temp_filename, RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
107 remove(temp_filename);
111 if (!__libGlHandle) {
112 os::log("apitrace: error: couldn't load %s\n", libgl_filename);
118 result = dlsym(__libGlHandle, symbol);
121 if (result == dlsym(RTLD_SELF, symbol)) {
122 os::log("apitrace: error: symbol lookup recursion\n");
133 __getPublicProcAddress(const char *procName)
135 return __libgl_sym(procName);
139 __getPrivateProcAddress(const char *procName)
141 return __libgl_sym(procName);
149 * Invoke the true dlopen() function.
152 __dlopen(const char *filename, int flag)
154 typedef void * (*PFNDLOPEN)(const char *, int);
155 static PFNDLOPEN dlopen_ptr = NULL;
158 dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
160 os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
165 return dlopen_ptr(filename, flag);
170 * Lookup a libGL symbol
172 void * __libgl_sym(const char *symbol)
176 if (!__libGlHandle) {
178 * The app doesn't directly link against libGL.so, nor does it directly
179 * dlopen it. So we have to load it ourselves.
182 const char * libgl_filename = getenv("TRACE_LIBGL");
184 if (!libgl_filename) {
186 * Try to use whatever libGL.so the library is linked against.
189 result = dlsym(RTLD_NEXT, symbol);
191 __libGlHandle = RTLD_NEXT;
195 libgl_filename = "libGL.so.1";
199 * It would have been preferable to use RTLD_LOCAL to ensure that the
200 * application can never access libGL.so symbols directly, but this
201 * won't work, given libGL.so often loads a driver specific SO and
202 * exposes symbols to it.
205 __libGlHandle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
206 if (!__libGlHandle) {
207 os::log("apitrace: error: couldn't find libGL.so\n");
212 return dlsym(__libGlHandle, symbol);
217 __getPublicProcAddress(const char *procName)
219 return __libgl_sym(procName);
223 __getPrivateProcAddress(const char *procName)
225 return (void *)__glXGetProcAddressARB((const GLubyte *)procName);