3 This file is part of gldbg
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
16 #define INTERNAL __attribute__((visibility("internal")))
18 INTERNAL inline const char *get_lib_names(void)
20 const char *env = getenv("GLWRAP_LIBS");
26 INTERNAL inline void *glsym(const char *name)
28 static void **gl_libs = NULL;
33 char *lib_names = strdup(get_lib_names());
37 for(i=0; lib_names[i]; ++i)
41 gl_libs = (void **)malloc((n_libs+1)*sizeof(void *));
46 if(lib_names[j]==':' || lib_names[j]==0)
48 int at_end = (lib_names[j]==0);
51 gl_libs[n_libs] = dlopen(lib_names+i, RTLD_NOW);
54 fprintf(stderr, "Could not open %s: %s\n", lib_names+i, dlerror());
70 for(i=0; gl_libs[i]; ++i)
72 void *sym = dlsym(gl_libs[i], name);
80 INTERNAL char *buffer = 0;
81 INTERNAL char *write_pos;
82 INTERNAL struct iovec *iovecs = 0;
83 INTERNAL struct iovec *cur_vec;
84 INTERNAL unsigned length;
86 INTERNAL inline void next_vec(void)
88 if(write_pos!=cur_vec->iov_base)
90 cur_vec->iov_len = write_pos-(char *)cur_vec->iov_base;
91 length += cur_vec->iov_len;
93 cur_vec->iov_base = write_pos;
97 INTERNAL inline void write_bytes(const char *ptr, unsigned size)
100 for(i=0; i<size; ++i)
101 *write_pos++ = *ptr++;
104 INTERNAL inline void write_char(char v)
109 INTERNAL inline void write_short(short v)
111 write_bytes((char *)&v, sizeof(short));
114 INTERNAL inline void write_int(int v)
116 write_bytes((char *)&v, sizeof(int));
119 INTERNAL inline void write_long(long v)
121 write_bytes((char *)&v, sizeof(long));
124 INTERNAL inline void write_long_long(long long v)
126 write_bytes((char *)&v, sizeof(long long));
129 INTERNAL inline void write_float(float v)
131 write_bytes((char *)&v, sizeof(float));
134 INTERNAL inline void write_double(double v)
136 write_bytes((char *)&v, sizeof(double));
139 INTERNAL inline void write_pointer(const void *p)
141 write_bytes((char *)&p, sizeof(void *));
144 INTERNAL inline void write_data(const void *data, unsigned size)
150 cur_vec->iov_base = (void *)data;
151 cur_vec->iov_len = size;
154 cur_vec->iov_base = write_pos;
160 INTERNAL inline void write_string(const char *s)
162 write_data(s, strlen(s)+1);
165 INTERNAL inline void write_string_array(const char **sa, unsigned size)
168 size /= sizeof(const char *);
170 for(i=0; i<size; ++i)
174 INTERNAL inline void begin_packet(int func)
177 buffer = (char *)malloc(1024);
179 iovecs = (struct iovec *)malloc(16*sizeof(struct iovec));
183 cur_vec->iov_base = write_pos;
190 INTERNAL inline int get_out_fd(void)
196 const char *var = getenv("GLWRAP_FD");
198 fd = strtol(var, NULL, 0);
201 var = getenv("GLWRAP_FILE");
204 fd = open(var, O_WRONLY|O_CREAT, 0644);
207 fprintf(stderr, "Couldn't open dumpfile %s for output: %s", var, strerror(errno));
219 INTERNAL inline void send_partial_packet(void)
223 write_int(length|0x80000000);
224 writev(get_out_fd(), iovecs, cur_vec-iovecs);
228 cur_vec->iov_base = write_pos;
234 INTERNAL inline void send_packet(void)
239 writev(get_out_fd(), iovecs, cur_vec-iovecs);