X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglwrap.c;h=cd1c328139e784f2a4bea80d90297e86401d4402;hb=a832996c884a0e0acc9a38ba4dd258edb75ec7af;hp=79d6404d2a34b21cf9cfeb3bfb045e6ecb7bda0b;hpb=3c32a221de1435ae7af8d96182560e8b28f1a4c0;p=gldbg.git diff --git a/source/glwrap.c b/source/glwrap.c index 79d6404..cd1c328 100644 --- a/source/glwrap.c +++ b/source/glwrap.c @@ -1,207 +1,235 @@ -/* $Id$ - -This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the GPL -*/ - #include #include #include +#include #include -#include -#include -#include -#include +#include +#include #include "arraysize.h" +#include "breakpoint.h" #include "functions.h" +#include "opengl.h" +#include "packet.h" + +#define UNUSED __attribute__((unused)) + +static unsigned char breakpoints[N_FUNCS] = { }; +static char break_any = 0; +static char hold = 0; +static char no_break = 0; + +static const char *get_lib_names(void) +{ + const char *env = getenv("GLWRAP_LIBS"); + if(env) + return env; + return "libGL.so.1"; +} -static inline void *glsym(const char *sym) +void *glsym(const char *name) { - static void *libgl = NULL; - if(!libgl) + 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); + break_any = flags_set; } -static inline void write_string(const unsigned char *s) +static void receive_gldHold(GlPacket *pkt UNUSED) { - write_data(s, strlen(s)+1); + hold = 1; } -static inline void write_string_array(const unsigned char **sa, unsigned size) +static void receive_gldQueryViewport(GlPacket *pkt UNUSED) { - unsigned i; - size /= sizeof(const unsigned char *); - write_int(size); - for(i=0; iiov_base = write_pos; - length = 0; - write_int(0); - write_short(func); + no_break = 1; + glGetIntegerv(GL_VIEWPORT, viewport); + no_break = 0; } -static inline void send_packet() +static void receive_gldReadPixels(GlPacket *pkt) { - static int fd = -1; - if(fd<0) - { - 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); -} + GLint x, y; + GLsizei width, height; + GLenum format, type; + char *data; + + packet_read_int(pkt, &x); + packet_read_int(pkt, &y); + packet_read_int(pkt, &width); + packet_read_int(pkt, &height); + packet_read_int(pkt, (int *)&format); + packet_read_int(pkt, (int *)&type); -GLenum cur_error = GL_NO_ERROR; + data = (char *)malloc(width*height*typesize(type)*formatsize(format)); + no_break = 1; + glReadPixels(x, y, width, height, format, type, data); + no_break = 0; + free(data); +} -static void check_error() +static void receive(void) { - GLenum (*orig_glGetError)() = 0; - GLenum code; - if(!orig_glGetError) - orig_glGetError = glsym("glGetError"); - code = orig_glGetError(); - if(code!=GL_NO_ERROR) + GlPacket *pkt; + + while((pkt = packet_receive(get_in_fd()))) { - begin_packet(FUNC_GLDERROR); - write_int(code); - send_packet(); - if(cur_error==GL_NO_ERROR) - cur_error = code; + unsigned short func; + + packet_read_short(pkt, (short *)&func); + switch(func) + { + case FUNC_GLDBREAK: receive_gldBreak(pkt); break; + case FUNC_GLDHOLD: receive_gldHold(pkt); break; + case FUNC_GLDQUERYVIEWPORT: receive_gldQueryViewport(pkt); break; + case FUNC_GLDREADPIXELS: receive_gldReadPixels(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; -} + int breakpoint; -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(no_break) + return; -void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress"))); + receive(); -#include "glwrap.funcs" + breakpoint = (breakpoints[func]|break_any)&flag; + if(breakpoint || hold) + { + GlPacket *pkt; + + if(breakpoint) + { + pkt = packet_begin(FUNC_GLDBREAK); + packet_write_short(pkt, func); + packet_write_char(pkt, flag); + packet_send(pkt, get_out_fd()); + } + + break_any = 0; + + while(1) + { + hold = 0; + raise(SIGTRAP); + receive(); + if(!hold) + break; + } + } +}