X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglwrap.c;h=c70b308384b339888dc4ab4527fc7a86df4fa4dc;hb=ea3d851aa52e999b1c5a5fa52c97ff5019756c0e;hp=d51eb3192106b9ce94fa77b200a2c6797565f0de;hpb=151c65f1157f3b55d0b794db788b557bbc50ed0c;p=gldbg.git diff --git a/source/glwrap.c b/source/glwrap.c index d51eb31..c70b308 100644 --- a/source/glwrap.c +++ b/source/glwrap.c @@ -1,202 +1,195 @@ /* $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 "arraysize.h" +#include +#include +#include "breakpoint.h" #include "functions.h" +#include "packet.h" -static inline void *glsym(const char *sym) +static unsigned char breakpoints[N_FUNCS] = { }; +static char break_any = 0; +static char hold = 0; + +static 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.1"; +} + +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; + } } - } - - return dlsym(libgl, sym); -} -static char *buffer = 0; -static char *write_pos; -static struct iovec *iovecs = 0; -static struct iovec *cur_vec; -static unsigned length; + gl_libs[n_libs] = 0; + free(lib_names); + } -static inline void next_vec() -{ - if(write_pos!=cur_vec->iov_base) + for(i=0; gl_libs[i]; ++i) { - cur_vec->iov_len = write_pos-(char *)cur_vec->iov_base; - length += cur_vec->iov_len; - ++cur_vec; - cur_vec->iov_base = write_pos; + void *sym = dlsym(gl_libs[i], name); + if(sym) + return sym; } -} -static inline void write_bytes(const char *ptr, unsigned size) -{ - unsigned i; - for(i=0; iiov_base = (void *)data; - cur_vec->iov_len = size; - length += size; - ++cur_vec; - cur_vec->iov_base = write_pos; + breakpoints[func] &= ~flags_clr; + breakpoints[func] |= flags_set; } else - write_int(0); -} - -static inline void write_string(const char *s) -{ - write_data(s, strlen(s)+1); + break_any = flags_set; } -static inline void write_string_array(const char **sa, unsigned size) +static void receive_gldHold(GlPacket *pkt __attribute__((unused))) { - unsigned i; - size /= sizeof(const char *); - write_int(size); - for(i=0; iiov_base = write_pos; - length = 0; - write_int(0); - write_short(func); -} + GlPacket *pkt; -static inline void send_packet() -{ - static int fd = -1; - if(fd<0) + while((pkt = packet_receive(get_in_fd()))) { - const char *var = getenv("GLWRAP_FD"); - if(var) - fd = strtol(var, NULL, 0); - else - fd = 2; - } - next_vec(); - write_pos = buffer; - write_int(length); - writev(fd, iovecs, cur_vec-iovecs); -} + unsigned short func; -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; + packet_read_short(pkt, (short *)&func); + switch(func) + { + case FUNC_GLDBREAK: receive_gldBreak(pkt); break; + case FUNC_GLDHOLD: receive_gldHold(pkt); break; + default:; + } } } -GLenum APIENTRY glGetError() +void tracepoint(unsigned short func, int flag) { - GLenum ret = cur_error; - cur_error = GL_NO_ERROR; - begin_packet(FUNC_GLGETERROR); - write_int(ret); - send_packet(); - return ret; -} + receive(); -void (*glXGetProcAddress(const GLubyte *procname))(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; -} + if((breakpoints[func]|break_any)&flag) + { + GlPacket *pkt; + + pkt = packet_begin(FUNC_GLDBREAK); + packet_write_short(pkt, func); + packet_send(pkt, get_out_fd()); -void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress"))); + break_any = 0; -#include "glwrap.funcs" + while(1) + { + hold = 0; + raise(SIGTRAP); + receive(); + if(!hold) + break; + } + } +}