blob: fdbea05c64c2069270548ae8fcbde468b40c005f [file] [log] [blame]
José Fonseca1cfd89b2011-12-01 21:09:57 +00001/**************************************************************************
2 *
3 * Copyright 2011 Jose Fonseca
4 * All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27#include "glproc.hpp"
28
29
30#if !defined(_WIN32)
José Fonseca2dc8ebe2011-12-03 11:00:32 +000031#include <unistd.h> // for symlink
Alexander Monakovd6aa74c2013-05-03 21:03:27 +040032#include "dlopen.hpp"
José Fonseca1cfd89b2011-12-01 21:09:57 +000033#endif
34
35
36/*
37 * Handle to the true OpenGL library.
38 */
39#if defined(_WIN32)
José Fonseca3947e362012-04-20 21:04:43 +010040HMODULE _libGlHandle = NULL;
José Fonseca1cfd89b2011-12-01 21:09:57 +000041#else
José Fonseca632a78d2012-04-19 07:18:59 +010042void *_libGlHandle = NULL;
José Fonseca1cfd89b2011-12-01 21:09:57 +000043#endif
44
45
46
47#if defined(_WIN32)
48
49void *
José Fonseca632a78d2012-04-19 07:18:59 +010050_getPublicProcAddress(const char *procName)
José Fonseca1cfd89b2011-12-01 21:09:57 +000051{
José Fonseca632a78d2012-04-19 07:18:59 +010052 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +000053 char szDll[MAX_PATH] = {0};
54
55 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
56 return NULL;
57 }
58
José Fonseca1a24faa2014-05-23 11:16:41 +010059 strcat(szDll, "\\opengl32.dll");
José Fonseca1cfd89b2011-12-01 21:09:57 +000060
José Fonseca632a78d2012-04-19 07:18:59 +010061 _libGlHandle = LoadLibraryA(szDll);
62 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +000063 os::log("apitrace: error: couldn't load %s\n", szDll);
64 return NULL;
65 }
66 }
67
José Fonseca632a78d2012-04-19 07:18:59 +010068 return (void *)GetProcAddress(_libGlHandle, procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +000069}
70
71
72void *
José Fonseca632a78d2012-04-19 07:18:59 +010073_getPrivateProcAddress(const char *procName) {
74 return (void *)_wglGetProcAddress(procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +000075}
76
77
78#elif defined(__APPLE__)
79
80
81/*
82 * Path to the true OpenGL framework
83 */
84static const char *libgl_filename = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
85
86
87/*
88 * Lookup a libGL symbol
89 */
José Fonseca632a78d2012-04-19 07:18:59 +010090void * _libgl_sym(const char *symbol)
José Fonseca1cfd89b2011-12-01 21:09:57 +000091{
92 void *result;
93
José Fonseca632a78d2012-04-19 07:18:59 +010094 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +000095 /*
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
99 * instead.
100 */
101
102 char temp_filename[] = "/tmp/tmp.XXXXXX";
103
104 if (mktemp(temp_filename) != NULL) {
105 if (symlink(libgl_filename, temp_filename) == 0) {
José Fonseca632a78d2012-04-19 07:18:59 +0100106 _libGlHandle = dlopen(temp_filename, RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000107 remove(temp_filename);
108 }
109 }
110
José Fonseca632a78d2012-04-19 07:18:59 +0100111 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +0000112 os::log("apitrace: error: couldn't load %s\n", libgl_filename);
113 os::abort();
114 return NULL;
115 }
116 }
117
José Fonseca632a78d2012-04-19 07:18:59 +0100118 result = dlsym(_libGlHandle, symbol);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000119
José Fonseca39879632012-04-14 22:26:09 +0100120#if 0
121 if (result && result == dlsym(RTLD_SELF, symbol)) {
José Fonseca1cfd89b2011-12-01 21:09:57 +0000122 os::log("apitrace: error: symbol lookup recursion\n");
123 os::abort();
124 return NULL;
125 }
José Fonseca39879632012-04-14 22:26:09 +0100126#endif
José Fonseca1cfd89b2011-12-01 21:09:57 +0000127
128 return result;
129}
130
131
132void *
José Fonseca632a78d2012-04-19 07:18:59 +0100133_getPublicProcAddress(const char *procName)
José Fonseca1cfd89b2011-12-01 21:09:57 +0000134{
José Fonseca632a78d2012-04-19 07:18:59 +0100135 return _libgl_sym(procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000136}
137
138void *
José Fonseca632a78d2012-04-19 07:18:59 +0100139_getPrivateProcAddress(const char *procName)
José Fonseca1cfd89b2011-12-01 21:09:57 +0000140{
José Fonseca632a78d2012-04-19 07:18:59 +0100141 return _libgl_sym(procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000142}
143
144
145#else
146
147
José Fonseca7700f742013-06-15 11:31:53 +0100148static inline void
149logSymbol(const char *name, void *ptr) {
150 if (0) {
151 if (ptr) {
152 Dl_info info;
153 if (ptr && dladdr(ptr, &info)) {
154 os::log("apitrace: %s -> \"%s\"\n", name, info.dli_fname);
155 }
156 } else {
157 os::log("apitrace: %s -> NULL\n", name);
158 }
159 }
160}
161
162
José Fonseca1cfd89b2011-12-01 21:09:57 +0000163/*
José Fonseca1cfd89b2011-12-01 21:09:57 +0000164 * Lookup a libGL symbol
165 */
José Fonseca632a78d2012-04-19 07:18:59 +0100166void * _libgl_sym(const char *symbol)
José Fonseca1cfd89b2011-12-01 21:09:57 +0000167{
168 void *result;
169
José Fonseca632a78d2012-04-19 07:18:59 +0100170 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +0000171 /*
172 * The app doesn't directly link against libGL.so, nor does it directly
173 * dlopen it. So we have to load it ourselves.
174 */
175
176 const char * libgl_filename = getenv("TRACE_LIBGL");
177
178 if (!libgl_filename) {
179 /*
180 * Try to use whatever libGL.so the library is linked against.
181 */
182
183 result = dlsym(RTLD_NEXT, symbol);
184 if (result) {
José Fonseca632a78d2012-04-19 07:18:59 +0100185 _libGlHandle = RTLD_NEXT;
José Fonseca1cfd89b2011-12-01 21:09:57 +0000186 return result;
187 }
188
189 libgl_filename = "libGL.so.1";
190 }
191
192 /*
193 * It would have been preferable to use RTLD_LOCAL to ensure that the
194 * application can never access libGL.so symbols directly, but this
195 * won't work, given libGL.so often loads a driver specific SO and
196 * exposes symbols to it.
197 */
198
José Fonseca632a78d2012-04-19 07:18:59 +0100199 _libGlHandle = _dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
200 if (!_libGlHandle) {
José Fonseca1cfd89b2011-12-01 21:09:57 +0000201 os::log("apitrace: error: couldn't find libGL.so\n");
202 return NULL;
203 }
204 }
205
José Fonseca0e725472014-05-23 23:24:13 +0100206 result = dlsym(_libGlHandle, symbol);
José Fonseca7700f742013-06-15 11:31:53 +0100207
208 logSymbol(symbol, result);
209
210 return result;
José Fonseca1cfd89b2011-12-01 21:09:57 +0000211}
212
213
214void *
José Fonseca632a78d2012-04-19 07:18:59 +0100215_getPublicProcAddress(const char *procName)
José Fonseca1cfd89b2011-12-01 21:09:57 +0000216{
José Fonseca632a78d2012-04-19 07:18:59 +0100217 return _libgl_sym(procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000218}
219
220void *
José Fonseca632a78d2012-04-19 07:18:59 +0100221_getPrivateProcAddress(const char *procName)
José Fonseca1cfd89b2011-12-01 21:09:57 +0000222{
José Fonseca632a78d2012-04-19 07:18:59 +0100223 return (void *)_glXGetProcAddressARB((const GLubyte *)procName);
José Fonseca1cfd89b2011-12-01 21:09:57 +0000224}
225
226
227#endif
228