]> git.tdb.fi Git - gldbg.git/commitdiff
Packetize the command stream for more robustness
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Sep 2009 10:08:44 +0000 (10:08 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Sep 2009 10:08:44 +0000 (10:08 +0000)
Deal with NULL data pointers
Rewrite gldump to use mmap to avoid hassle with large packets
Fix some style issues

Makefile
genwrap.py
source/gldecoder.c
source/gldecoder.h
source/gldump.c
source/glwrap.c

index e6f6d11bb4600c5f939d6cfc125f1d9d8634e754..93fe6813eec78a1eb54bc3b668a6d166d9125357 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 # $Id$
 
-CFLAGS=-Igensrc -ggdb
+CFLAGS = -Igensrc -ggdb
 
 all: glwrap.so gldump
 
index 35e343952c26f8f7be422e235b66cb1b145e1771..7f87c7f432cdabed585e5fe94e97e5fd63ad30ff 100755 (executable)
@@ -254,17 +254,17 @@ out.write("} GlDecoder;\n")
 
 out = open(os.path.join(outdir, "gldecoder.funcs"), "w")
 for f in funcs:
-       out.write("static unsigned decode_%s(GlDecoder *dec, const char *data, unsigned len)\n{\n"%(f[0]))
+       out.write("static unsigned decode_%s(GlDecoder *dec, const char *data)\n{\n"%(f[0]))
        out.write("\tunsigned pos = 0;\n")
        if f[1]!="void":
                out.write("\t%s ret;\n"%f[1])
        for p in f[2]:
                out.write("\t%s arg_%s;\n"%(p[1], p[0]))
        if f[1]!="void":
-               out.write("\tpos += read_%s(&ret, data+pos, len);\n"%iomap[f[1]])
+               out.write("\tpos += read_%s(&ret, data+pos);\n"%iomap[f[1]])
        for p in f[2]:
                (t, c) = getread(f, p)
-               out.write("\tpos += read_%s(%s&arg_%s, data+pos, len-pos);\n"%(t, c, p[0]))
+               out.write("\tpos += read_%s(%s&arg_%s, data+pos);\n"%(t, c, p[0]))
        out.write("\tif(dec->%s)\n"%f[0])
        out.write("\t\tdec->%s(dec->user_data"%f[0])
        if f[1]!="void":
@@ -275,20 +275,16 @@ for f in funcs:
        out.write("\treturn pos;\n")
        out.write("}\n\n")
 
-out.write("""unsigned gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
+out.write("""static int decode_func(GlDecoder *dec, short func, const char *data)
 {
-       unsigned pos = 0;
-       short func;
-
-       pos += read_short(&func, data, len);
        switch(func)
        {
 """)
 for f in funcs:
-       out.write("\t\tcase FUNC_%s: pos += decode_%s(dec, data+pos, len); break;\n"%(f[0].upper(), f[0]))
+       out.write("\t\tcase FUNC_%s: return decode_%s(dec, data);\n"%(f[0].upper(), f[0]))
 out.write("""  }
 
-       return pos;
+       return -1;
 }
 """)
 out.close()
index 93a525876c64c4b033bb92cd6f770239fcbc2dd9..85d089d2034af846d3d7c990db52db680d40ea66 100644 (file)
@@ -10,6 +10,10 @@ Distributed under the GPL
 #include "functions.h"
 #include "gldecoder.h"
 
+static unsigned read_short(short *, const char *);
+static unsigned read_int(int *, const char *);
+static int decode_func(GlDecoder *, short, const char *);
+
 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
 {
        GlDecoder *dec;
@@ -29,72 +33,94 @@ void gldecoder_delete(GlDecoder *dec)
        free(dec);
 }
 
-unsigned read_char(char *v, const char *data, unsigned len)
+int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
+{
+       unsigned pos = 0;
+       int pktlen;
+       short func;
+       int ret;
+
+       if(len<sizeof(int)+sizeof(short))
+               return -1;
+       pos += read_int(&pktlen, data);
+       if(len<pktlen)
+               return -1;
+       pos += read_short(&func, data+pos);
+       ret = decode_func(dec, func, data+pos);
+       if(ret<0)
+               return -1;
+       return pos+ret;
+}
+
+static unsigned read_char(char *v, const char *data)
 {
        *v = *data;
        return 1;
 }
 
-unsigned read_short(short *v, const char *data, unsigned len)
+static unsigned read_short(short *v, const char *data)
 {
        *v = *(short *)data;
        return sizeof(short);
 }
 
-unsigned read_int(int *v, const char *data, unsigned len)
+static unsigned read_int(int *v, const char *data)
 {
        *v = *(int *)data;
        return sizeof(int);
 }
 
-unsigned read_long(long *v, const char *data, unsigned len)
+static unsigned read_long(long *v, const char *data)
 {
        *v = *(long *)data;
        return sizeof(long);
 }
 
-unsigned read_ulong(unsigned long *v, const char *data, unsigned len)
+static unsigned read_ulong(unsigned long *v, const char *data)
 {
        *v = *(unsigned long *)data;
        return sizeof(unsigned long);
 }
 
-unsigned read_longlong(long long *v, const char *data, unsigned len)
+static unsigned read_longlong(long long *v, const char *data)
 {
        *v = *(long long *)data;
        return sizeof(long long);
 }
 
-unsigned read_float(float *v, const char *data, unsigned len)
+static unsigned read_float(float *v, const char *data)
 {
        *v = *(float *)data;
        return sizeof(float);
 }
 
-unsigned read_double(double *v, const char *data, unsigned len)
+static unsigned read_double(double *v, const char *data)
 {
        *v = *(double *)data;
        return sizeof(double);
 }
 
-unsigned read_pointer(void **v, const char *data, unsigned len)
+static unsigned read_pointer(void **v, const char *data)
 {
        *v = *(void **)data;
        return sizeof(void *);
 }
 
-unsigned read_data(const void **v, const char *data, unsigned len)
+static unsigned read_data(const void **v, const char *data)
 {
        int vlen;
        unsigned pos = 0;
-       pos += read_int(&vlen, data, len);
-       *v = data+pos;
+       pos += read_int(&vlen, data);
+       if(vlen)
+               *v = data+pos;
+       else
+               *v = NULL;
        return pos+vlen;
 }
 
-unsigned read_string(const unsigned char **v, const char *data, unsigned len)
+static unsigned read_string(const unsigned char **v, const char *data)
 {
-       return read_data((const void **)v, data, len);
+       return read_data((const void **)v, data);
 }
 
 #include "gldecoder.funcs"
index 41f785c85abec5496f38a7882f63622924084c57..2b76b408b1468ca7e96ef68c2ebb0f19c8030a08 100644 (file)
@@ -13,6 +13,6 @@ Distributed under the GPL
 
 GlDecoder *gldecoder_new(void *, void (*)(void *));
 void gldecoder_delete(GlDecoder *);
-unsigned gldecoder_decode(GlDecoder *, const char *, unsigned);
+int gldecoder_decode(GlDecoder *, const char *, unsigned);
 
 #endif
index 3a6707b962abd6e7cf8b058809067b28a781602a..79896c2fa4bad9c902726e949d03015b899160ae 100644 (file)
@@ -7,43 +7,54 @@ Distributed under the GPL
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
 #include "glprint.h"
 
 int main(int argc, char **argv)
 {
-       FILE *in;
+       int fd;
+       struct stat st;
        GlDecoder *dec;
-       char *buf;
-       unsigned size;
-       unsigned start;
-       unsigned end;
+       char *addr;
+       char *end;
+       char *ptr;
 
-       in = fopen(argv[1], "r");
+       if(argc<2)
+       {
+               fprintf(stderr, "Usage: %s <dumpfile>\n", argv[0]);
+               return 1;
+       }
+
+       fd = open(argv[1], O_RDONLY);
+       if(fd==-1)
+       {
+               perror("open");
+               return 1;
+       }
+       fstat(fd, &st);
+       addr = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+       end = addr+st.st_size;
+       ptr = addr;
 
        dec = glprint_new(NULL, 0);
 
-       size = 16384;
-       buf = (char *)malloc(size);
-       start = 0;
-       end = 0;
-       while(1)
+       while(ptr<end)
        {
-               if(start>size/2)
+               int ret;
+               ret = gldecoder_decode(dec, ptr, end-ptr);
+               if(ret<0)
                {
-                       memmove(buf, buf+start, end-start);
-                       end -= start;
-                       start = 0;
-               }
-               if(end<size && !feof(in))
-                       end += fread(buf+end, 1, size-end, in);
-               else if(start==end)
+                       fprintf(stderr, "Decode error\n");
                        break;
-               start += gldecoder_decode(dec, buf+start, end-start);
+               }
+               ptr += ret;
                printf("%s\n", glprint_get_buffer(dec));
        }
-       fclose(in);
-       free(buf);
+       munmap(addr, st.st_size);
+       close(fd);
        gldecoder_delete(dec);
 
        return 0;
index a33021a70a89f70c988ea0694ff9f7910ff9e702..38f5e274e72d07d9fc9d1d50faf502cb53498c98 100644 (file)
@@ -15,10 +15,10 @@ Distributed under the GPL
 
 static inline void *glsym(const char *sym)
 {
-       static void *libgl=NULL;
+       static void *libgl = NULL;
        if(!libgl)
        {
-               libgl=dlopen("libGL.so", RTLD_NOW);
+               libgl = dlopen("libGL.so", RTLD_NOW);
                if(!libgl)
                {
                        fprintf(stderr, "Could not open libGL: %s\n", dlerror());
@@ -29,18 +29,20 @@ static inline void *glsym(const char *sym)
        return dlsym(libgl, sym);
 }
 
-char *buffer=0;
-char *write_pos;
-struct iovec *iovecs=0;
-struct iovec *cur_vec;
+static char *buffer = 0;
+static char *write_pos;
+static struct iovec *iovecs = 0;
+static struct iovec *cur_vec;
+static unsigned length;
 
 static inline void next_vec()
 {
        if(write_pos!=cur_vec->iov_base)
        {
-               cur_vec->iov_len=write_pos-(char *)cur_vec->iov_base;
+               cur_vec->iov_len = write_pos-(char *)cur_vec->iov_base;
+               length += cur_vec->iov_len;
                ++cur_vec;
-               cur_vec->iov_base=write_pos;
+               cur_vec->iov_base = write_pos;
        }
 }
 
@@ -48,12 +50,12 @@ static inline void write_bytes(const char *ptr, unsigned size)
 {
        unsigned i;
        for(i=0; i<size; ++i)
-               *write_pos++=*ptr++;
+               *write_pos++ = *ptr++;
 }
 
 static inline void write_char(char v)
 {
-       *write_pos++=v;
+       *write_pos++ = v;
 }
 
 static inline void write_short(short v)
@@ -86,7 +88,7 @@ static inline void write_float(float v)
        write_bytes((char *)&v, sizeof(float));
 }
 
-static inline void write_double(float v)
+static inline void write_double(double v)
 {
        write_bytes((char *)&v, sizeof(double));
 }
@@ -98,46 +100,53 @@ static inline void write_pointer(const void *p)
 
 static inline void write_data(const void *data, unsigned size)
 {
-       write_int(size);
-       next_vec();
-       cur_vec->iov_base=(void *)data;
-       cur_vec->iov_len=size;
-       ++cur_vec;
-       cur_vec->iov_base=write_pos;
+       if(data)
+       {
+               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;
+       }
+       else
+               write_int(0);
 }
 
 static inline void write_string(const unsigned char *s)
 {
        write_data(s, strlen(s));
-       /*int len=strlen(s);
-       write_int(len);
-       write_bytes(s, len);*/
 }
 
 static inline void begin_packet(int func)
 {
        if(!buffer)
-               buffer=(char *)malloc(1024);
+               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;
+               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);
 }
 
 static inline void send_packet()
 {
-       static int fd=-1;
+       static int fd = -1;
        if(fd<0)
        {
-               const char *var=getenv("GLWRAP_FD");
+               const char *var = getenv("GLWRAP_FD");
                if(var)
-                       fd=strtol(var, NULL, 0);
+                       fd = strtol(var, NULL, 0);
                else
-                       fd=2;
+                       fd = 2;
        }
        next_vec();
+       write_pos = buffer;
+       write_int(length);
        writev(fd, iovecs, cur_vec-iovecs);
 }