3 This file is part of gldbg
4 Copyright © 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
16 #include "arraysize.h"
17 #include "functions.h"
19 static inline void *glsym(const char *sym)
21 static void *libgl = NULL;
24 libgl = dlopen("libGL.so", RTLD_NOW);
27 fprintf(stderr, "Could not open libGL: %s\n", dlerror());
32 return dlsym(libgl, sym);
35 static char *buffer = 0;
36 static char *write_pos;
37 static struct iovec *iovecs = 0;
38 static struct iovec *cur_vec;
39 static unsigned length;
41 static inline void next_vec()
43 if(write_pos!=cur_vec->iov_base)
45 cur_vec->iov_len = write_pos-(char *)cur_vec->iov_base;
46 length += cur_vec->iov_len;
48 cur_vec->iov_base = write_pos;
52 static inline void write_bytes(const char *ptr, unsigned size)
56 *write_pos++ = *ptr++;
59 static inline void write_char(char v)
64 static inline void write_short(short v)
66 write_bytes((char *)&v, sizeof(short));
69 static inline void write_int(int v)
71 write_bytes((char *)&v, sizeof(int));
74 static inline void write_long(long v)
76 write_bytes((char *)&v, sizeof(long));
79 static inline void write_long_long(long long v)
81 write_bytes((char *)&v, sizeof(long long));
84 static inline void write_float(float v)
86 write_bytes((char *)&v, sizeof(float));
89 static inline void write_double(double v)
91 write_bytes((char *)&v, sizeof(double));
94 static inline void write_pointer(const void *p)
96 write_bytes((char *)&p, sizeof(void *));
99 static inline void write_data(const void *data, unsigned size)
105 cur_vec->iov_base = (void *)data;
106 cur_vec->iov_len = size;
109 cur_vec->iov_base = write_pos;
115 static inline void write_string(const char *s)
117 write_data(s, strlen(s)+1);
120 static inline void write_string_array(const char **sa, unsigned size)
123 size /= sizeof(const char *);
125 for(i=0; i<size; ++i)
129 static inline void begin_packet(int func)
132 buffer = (char *)malloc(1024);
134 iovecs = (struct iovec *)malloc(16*sizeof(struct iovec));
137 cur_vec->iov_base = write_pos;
143 static inline void send_packet()
148 const char *var = getenv("GLWRAP_FD");
150 fd = strtol(var, NULL, 0);
157 writev(fd, iovecs, cur_vec-iovecs);
160 GLenum cur_error = GL_NO_ERROR;
162 static void check_error()
164 GLenum (*orig_glGetError)() = 0;
167 orig_glGetError = glsym("glGetError");
168 code = orig_glGetError();
169 if(code!=GL_NO_ERROR)
171 begin_packet(FUNC_GLDERROR);
174 if(cur_error==GL_NO_ERROR)
179 GLenum APIENTRY glGetError()
181 GLenum ret = cur_error;
182 cur_error = GL_NO_ERROR;
183 begin_packet(FUNC_GLGETERROR);
189 void (*glXGetProcAddress(const GLubyte *procname))(void)
191 void *handle = dlopen(NULL, RTLD_LAZY);
192 void (*ret)() = dlsym(handle, (const char *)procname);
193 begin_packet(FUNC_GLXGETPROCADDRESS);
195 write_string(procname);
200 void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress")));
202 #include "glwrap.funcs"