X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglwrap.c;h=4c17edfca772755f16a983253e96d37cf3ef0de6;hb=4aba13fc7690cf79e949d9765b371e85e7530207;hp=d51eb3192106b9ce94fa77b200a2c6797565f0de;hpb=151c65f1157f3b55d0b794db788b557bbc50ed0c;p=gldbg.git diff --git a/source/glwrap.c b/source/glwrap.c index d51eb31..4c17edf 100644 --- a/source/glwrap.c +++ b/source/glwrap.c @@ -1,44 +1,89 @@ /* $Id$ This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ #include #include #include +#include #include +#include #include -#include -#include -#include -#include "arraysize.h" -#include "functions.h" -static inline void *glsym(const char *sym) +#define INTERNAL __attribute__((visibility("internal"))) + +INTERNAL inline const char *get_lib_names(void) { - static void *libgl = NULL; - if(!libgl) + const char *env = getenv("GLWRAP_LIBS"); + if(env) + return env; + return "libGL.so"; +} + +INTERNAL inline void *glsym(const char *name) +{ + static void **gl_libs = NULL; + unsigned i; + + if(!gl_libs) { - libgl = dlopen("libGL.so", RTLD_NOW); - if(!libgl) + char *lib_names = strdup(get_lib_names()); + unsigned n_libs = 1; + unsigned j; + + for(i=0; lib_names[i]; ++i) + if(lib_names[i]==':') + ++n_libs; + + gl_libs = (void **)malloc((n_libs+1)*sizeof(void *)); + i = 0; + n_libs = 0; + for(j=0;; ++j) { - fprintf(stderr, "Could not open libGL: %s\n", dlerror()); - abort(); + if(lib_names[j]==':' || lib_names[j]==0) + { + int at_end = (lib_names[j]==0); + lib_names[j] = 0; + + gl_libs[n_libs] = dlopen(lib_names+i, RTLD_NOW); + if(!gl_libs[n_libs]) + { + fprintf(stderr, "Could not open %s: %s\n", lib_names+i, dlerror()); + abort(); + } + + i = j+1; + ++n_libs; + + if(at_end) + break; + } } + + gl_libs[n_libs] = 0; + free(lib_names); + } + + for(i=0; gl_libs[i]; ++i) + { + void *sym = dlsym(gl_libs[i], name); + if(sym) + return sym; } - return dlsym(libgl, sym); + return NULL; } -static char *buffer = 0; -static char *write_pos; -static struct iovec *iovecs = 0; -static struct iovec *cur_vec; -static unsigned length; +INTERNAL char *buffer = 0; +INTERNAL char *write_pos; +INTERNAL struct iovec *iovecs = 0; +INTERNAL struct iovec *cur_vec; +INTERNAL unsigned length; -static inline void next_vec() +INTERNAL inline void next_vec(void) { if(write_pos!=cur_vec->iov_base) { @@ -49,54 +94,54 @@ static inline void next_vec() } } -static inline void write_bytes(const char *ptr, unsigned size) +INTERNAL inline void write_bytes(const char *ptr, unsigned size) { unsigned i; for(i=0; iiov_base = write_pos; length = 0; + write_int(0); write_short(func); } -static inline void send_packet() +INTERNAL inline int get_out_fd(void) { static int fd = -1; + if(fd<0) { const char *var = getenv("GLWRAP_FD"); @@ -151,52 +199,29 @@ static inline void send_packet() else fd = 2; } - next_vec(); - write_pos = buffer; - write_int(length); - writev(fd, iovecs, cur_vec-iovecs); -} - -GLenum cur_error = GL_NO_ERROR; -static void check_error() -{ - GLenum (*orig_glGetError)() = 0; - GLenum code; - if(!orig_glGetError) - orig_glGetError = glsym("glGetError"); - code = orig_glGetError(); - if(code!=GL_NO_ERROR) - { - begin_packet(FUNC_GLDERROR); - write_int(code); - send_packet(); - if(cur_error==GL_NO_ERROR) - cur_error = code; - } + return fd; } -GLenum APIENTRY glGetError() +INTERNAL inline void send_partial_packet(void) { - GLenum ret = cur_error; - cur_error = GL_NO_ERROR; - begin_packet(FUNC_GLGETERROR); - write_int(ret); - send_packet(); - return ret; + next_vec(); + write_pos = buffer; + write_int(length|0x80000000); + writev(get_out_fd(), iovecs, cur_vec-iovecs); + + write_pos = buffer; + cur_vec = iovecs; + cur_vec->iov_base = write_pos; + length = 0; + + write_int(0); } -void (*glXGetProcAddress(const GLubyte *procname))(void) +INTERNAL inline void send_packet(void) { - void *handle = dlopen(NULL, RTLD_LAZY); - void (*ret)() = dlsym(handle, (const char *)procname); - begin_packet(FUNC_GLXGETPROCADDRESS); - write_pointer(ret); - write_string(procname); - send_packet(); - return ret; + next_vec(); + write_pos = buffer; + write_int(length); + writev(get_out_fd(), iovecs, cur_vec-iovecs); } - -void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress"))); - -#include "glwrap.funcs"