]> git.cworth.org Git - apitrace/blob - glproc_gl.cpp
Fix glretrace on MacOSX.
[apitrace] / glproc_gl.cpp
1 /**************************************************************************
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)
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE // for dladdr
33 #endif
34 #include <dlfcn.h>
35 #endif
36
37
38 /*
39  * Handle to the true OpenGL library.
40  */
41 #if defined(_WIN32)
42 HINSTANCE __libGlHandle = NULL;
43 #else
44 void *__libGlHandle = NULL;
45 #endif
46
47
48
49 #if defined(_WIN32)
50
51 void *
52 __getPublicProcAddress(const char *procName)
53 {
54     if (!__libGlHandle) {
55         char szDll[MAX_PATH] = {0};
56         
57         if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
58             return NULL;
59         }
60         
61         strcat(szDll, "\\\\opengl32.dll");
62         
63         __libGlHandle = LoadLibraryA(szDll);
64         if (!__libGlHandle) {
65             os::log("apitrace: error: couldn't load %s\n", szDll);
66             return NULL;
67         }
68     }
69         
70     return (void *)GetProcAddress(__libGlHandle, procName);
71 }
72
73
74 void *
75 __getPrivateProcAddress(const char *procName) {
76     return (void *)__wglGetProcAddress(procName);
77 }
78
79
80 #elif defined(__APPLE__)
81
82
83 /*
84  * Path to the true OpenGL framework
85  */
86 static const char *libgl_filename = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
87
88
89 /*
90  * Lookup a libGL symbol
91  */
92 void * __libgl_sym(const char *symbol)
93 {
94     void *result;
95
96     if (!__libGlHandle) {
97         /* 
98          * Unfortunately we can't just dlopen the true dynamic library because
99          * DYLD_LIBRARY_PATH/DYLD_FRAMEWORK_PATH take precedence, even for
100          * absolute paths.  So we create a temporary symlink, and dlopen that
101          * instead.
102          */
103
104         char temp_filename[] = "/tmp/tmp.XXXXXX";
105
106         if (mktemp(temp_filename) != NULL) {
107             if (symlink(libgl_filename, temp_filename) == 0) {
108                 __libGlHandle = dlopen(temp_filename, RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
109                 remove(temp_filename);
110             }
111         }
112
113         if (!__libGlHandle) {
114             os::log("apitrace: error: couldn't load %s\n", libgl_filename);
115             os::abort();
116             return NULL;
117         }
118     }
119
120     result = dlsym(__libGlHandle, symbol);
121
122 #ifndef RETRACE
123     if (result == dlsym(RTLD_SELF, symbol)) {
124         os::log("apitrace: error: symbol lookup recursion\n");
125         os::abort();
126         return NULL;
127     }
128 #endif
129
130     return result;
131 }
132
133
134 void *
135 __getPublicProcAddress(const char *procName)
136 {
137     return __libgl_sym(procName);
138 }
139
140 void *
141 __getPrivateProcAddress(const char *procName)
142 {
143     return __libgl_sym(procName);
144 }
145
146
147 #else
148
149
150 /*
151  * Invoke the true dlopen() function.
152  */
153 static void *
154 __dlopen(const char *filename, int flag)
155 {
156     typedef void * (*PFNDLOPEN)(const char *, int);
157     static PFNDLOPEN dlopen_ptr = NULL;
158
159     if (!dlopen_ptr) {
160         dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
161         if (!dlopen_ptr) {
162             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
163             return NULL;
164         }
165     }
166
167     return dlopen_ptr(filename, flag);
168 }
169
170
171 /*
172  * Lookup a libGL symbol
173  */
174 void * __libgl_sym(const char *symbol)
175 {
176     void *result;
177
178     if (!__libGlHandle) {
179         /*
180          * The app doesn't directly link against libGL.so, nor does it directly
181          * dlopen it.  So we have to load it ourselves.
182          */
183
184         const char * libgl_filename = getenv("TRACE_LIBGL");
185
186         if (!libgl_filename) {
187             /*
188              * Try to use whatever libGL.so the library is linked against.
189              */
190
191             result = dlsym(RTLD_NEXT, symbol);
192             if (result) {
193                 __libGlHandle = RTLD_NEXT;
194                 return result;
195             }
196
197             libgl_filename = "libGL.so.1";
198         }
199
200         /*
201          * It would have been preferable to use RTLD_LOCAL to ensure that the
202          * application can never access libGL.so symbols directly, but this
203          * won't work, given libGL.so often loads a driver specific SO and
204          * exposes symbols to it.
205          */
206
207         __libGlHandle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
208         if (!__libGlHandle) {
209             os::log("apitrace: error: couldn't find libGL.so\n");
210             return NULL;
211         }
212     }
213
214     return dlsym(__libGlHandle, symbol);
215 }
216
217
218 void *
219 __getPublicProcAddress(const char *procName)
220 {
221     return __libgl_sym(procName);
222 }
223
224 void *
225 __getPrivateProcAddress(const char *procName)
226 {
227     return (void *)__glXGetProcAddressARB((const GLubyte *)procName);
228 }
229
230
231 #endif 
232