]> git.tdb.fi Git - gldbg.git/blobdiff - source/glwrap.c
Query implementation limits on process startup
[gldbg.git] / source / glwrap.c
index bf05214b8a09d7ee844d5567626b395b2066f1db..ee679577011931b7264fda5e011fecda59077d35 100644 (file)
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 #include <dlfcn.h>
-#include <sys/uio.h>
-#include <X11/Xlib.h>
-#include <GL/gl.h>
-#include <GL/glx.h>
+#include <fcntl.h>
+#include <signal.h>
 #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)
        {
-               const char *libgl_name = getenv("GLWRAP_LIBGL");
-               if(!libgl_name)
-                       libgl_name = "libGL.so";
-               libgl = dlopen(libgl_name, 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 %s: %s\n", libgl_name, 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; i<size; ++i)
-               *write_pos++ = *ptr++;
+       return NULL;
 }
 
-static inline void write_char(char v)
+int get_out_fd(void)
 {
-       *write_pos++ = v;
-}
+       static int fd = -1;
 
-static inline void write_short(short v)
-{
-       write_bytes((char *)&v, sizeof(short));
-}
+       if(fd<0)
+       {
+               const char *var = getenv("GLWRAP_FD");
+               if(var)
+                       fd = strtol(var, NULL, 0);
+               else
+               {
+                       var = getenv("GLWRAP_FILE");
+                       if(var)
+                       {
+                               fd = open(var, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+                               if(fd==-1)
+                               {
+                                       fprintf(stderr, "Couldn't open dumpfile %s for output: %s", var, strerror(errno));
+                                       abort();
+                               }
+                       }
+                       else
+                               fd = 2;
+               }
+       }
 
-static inline void write_int(int v)
-{
-       write_bytes((char *)&v, sizeof(int));
+       return fd;
 }
 
-static inline void write_long(long v)
+int get_in_fd(void)
 {
-       write_bytes((char *)&v, sizeof(long));
-}
+       static int fd = -1;
 
-static inline void write_long_long(long long v)
-{
-       write_bytes((char *)&v, sizeof(long long));
-}
+       if(fd<0)
+       {
+               const char *var = getenv("GLWRAP_CTRL_FD");
+               if(var)
+                       fd = strtol(var, NULL, 0);
+               else
+                       fd = 0;
+       }
 
-static inline void write_float(float v)
-{
-       write_bytes((char *)&v, sizeof(float));
+       return fd;
 }
 
-static inline void write_double(double v)
+static void receive_gldBreak(GlPacket *pkt)
 {
-       write_bytes((char *)&v, sizeof(double));
-}
+       unsigned short func;
+       unsigned char flags_set;
+       unsigned char flags_clr;
 
-static inline void write_pointer(const void *p)
-{
-       write_bytes((char *)&p, sizeof(void *));
-}
+       packet_read_short(pkt, (short *)&func);
+       packet_read_char(pkt, (char *)&flags_set);
+       packet_read_char(pkt, (char *)&flags_clr);
 
-static inline void write_data(const void *data, unsigned size)
-{
-       if(data)
+       if(func)
        {
-               write_int(size);
-               next_vec();
-               cur_vec->iov_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 char *s)
+static void receive_gldHold(GlPacket *pkt UNUSED)
 {
-       write_data(s, strlen(s)+1);
+       hold = 1;
 }
 
-static inline void write_string_array(const char **sa, unsigned size)
+static void receive_gldQueryViewport(GlPacket *pkt UNUSED)
 {
-       unsigned i;
-       size /= sizeof(const char *);
-       write_int(size);
-       for(i=0; i<size; ++i)
-               write_string(sa[i]);
+       int viewport[4];
+
+       no_break = 1;
+       glGetIntegerv(GL_VIEWPORT, viewport);
+       no_break = 0;
 }
 
-static inline void begin_packet(int func)
+static void receive_gldReadPixels(GlPacket *pkt)
 {
-       if(!buffer)
-               buffer = (char *)malloc(1024);
-       if(!iovecs)
-               iovecs = (struct iovec *)malloc(16*sizeof(struct iovec));
-       write_pos = buffer;
-       cur_vec = iovecs;
-       cur_vec->iov_base = write_pos;
-       length = 0;
-       write_int(0);
-       write_short(func);
+       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);
+
+       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 inline void send_packet()
+static void receive_gldQueryLimits(GlPacket *pkt UNUSED)
 {
-       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);
-}
+       int value;
 
-GLenum cur_error = GL_NO_ERROR;
+       no_break = 1;
+       value = 0;
+       glGetIntegerv(GL_MAX_TEXTURE_UNITS, &value);
+       value = 0;
+       glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &value);
+       value = 0;
+       glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &value);
+       no_break = 0;
+}
 
-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;
+               case FUNC_GLDQUERYLIMITS: receive_gldQueryLimits(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 = 0;
-       void (*ret)() = 0;
+       if(no_break)
+               return;
 
-       if(glsym((const char *)procname))
-       {
-               handle = dlopen(NULL, RTLD_LAZY);
-               ret = dlsym(handle, (const char *)procname);
-       }
+       receive();
 
-       begin_packet(FUNC_GLXGETPROCADDRESS);
-       write_pointer(ret);
-       write_string((const char *)procname);
-       send_packet();
+       breakpoint = (breakpoints[func]|break_any)&flag;
+       if(breakpoint || hold)
+       {
+               GlPacket *pkt;
 
-       return ret;
-}
+               if(breakpoint)
+               {
+                       pkt = packet_begin(FUNC_GLDBREAK);
+                       packet_write_short(pkt, func);
+                       packet_write_char(pkt, flag);
+                       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;
+               }
+       }
+}