16
|
1 /* Contains the debug functions */
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <stdarg.h>
|
|
5 #include <ctype.h>
|
|
6 #include <string.h>
|
|
7 #include <limits.h>
|
|
8 #include "define.h"
|
|
9
|
|
10 #ifdef _WIN32
|
|
11 # define vsnprintf _vsnprintf
|
|
12 #endif
|
|
13
|
|
14 struct _debug_item {
|
|
15 int type;
|
|
16 char * function;
|
|
17 unsigned int line;
|
|
18 char * file;
|
|
19 char * text;
|
|
20 struct _debug_item *next;
|
|
21 } *item_head=NULL, *item_tail=NULL, *item_ptr=NULL, *info_ptr=NULL, *temp_list=NULL;
|
|
22
|
|
23 struct _debug_func {
|
|
24 char * name;
|
|
25 struct _debug_func *next;
|
|
26 } *func_head=NULL, *func_ptr=NULL;
|
|
27
|
|
28
|
|
29 void _debug_init(char *fname);
|
|
30 void _debug_msg_info (int line, char *file, int type);
|
|
31 void _debug_msg(char* fmt, ...);
|
31
|
32 void _debug_hexdump(unsigned char *x, int y, int cols);
|
16
|
33 void _debug_func(char *function);
|
|
34 void _debug_func_ret();
|
|
35 void _debug_close();
|
|
36 void _debug_write();
|
|
37 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size);
|
31
|
38 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col);
|
16
|
39 void * xmalloc(size_t size);
|
|
40
|
|
41 // the largest text size we will store in memory. Otherwise we
|
|
42 // will do a debug_write, then create a new record, and write the
|
|
43 // text body directly to the file
|
|
44 #define MAX_MESSAGE_SIZE 4096
|
|
45
|
|
46 void _pst_debug(char *fmt, ...) {
|
|
47 va_list ap;
|
|
48 va_start(ap,fmt);
|
|
49 vfprintf(stderr, fmt, ap);
|
|
50 va_end(ap);
|
|
51 }
|
|
52
|
|
53 #define NUM_COL 30
|
|
54 void _pst_debug_hexdump(FILE *out, unsigned char *buf, size_t size, int col) {
|
|
55 int off = 0, toff;
|
|
56 int count = 0;
|
|
57
|
|
58 if (col == -1) {
|
|
59 col = NUM_COL;
|
|
60 }
|
|
61 fprintf(out, "\n");
|
|
62 while (off < size) {
|
|
63 fprintf(out, "%X\t:", off);
|
|
64 toff = off;
|
|
65 while (count < col && off < size) {
|
|
66 fprintf(out, "%02hhx ", buf[off]);
|
|
67 off++; count++;
|
|
68 }
|
|
69 off = toff;
|
|
70 while (count < col) {
|
|
71 // only happens at end of block to pad the text over to the text column
|
|
72 fprintf(out, " ");
|
|
73 count++;
|
|
74 }
|
|
75 count = 0;
|
|
76 fprintf(out, ":");
|
|
77 while (count < col && off < size) {
|
|
78 fprintf(out, "%c", isgraph(buf[off])?buf[off]:'.');
|
|
79 off++; count ++;
|
|
80 }
|
|
81
|
|
82 fprintf(out, "\n");
|
|
83 count=0;
|
|
84 }
|
|
85
|
|
86 fprintf(out, "\n");
|
|
87 }
|
|
88
|
|
89 void _pst_debug_hexprint(char *data, int size) {
|
|
90 int i = 0;
|
|
91 while (i < size) {
|
|
92 fprintf(stderr, "%02hhX", data[i]);
|
|
93 i++;
|
|
94 }
|
|
95 }
|
|
96
|
|
97 FILE *debug_fp = NULL;
|
|
98 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0;
|
|
99
|
|
100 void _debug_init(char* fname) {
|
|
101 unsigned char version = DEBUG_VERSION;
|
|
102 item_head = item_tail = NULL;
|
|
103 curr_items = 0;
|
|
104 if (debug_fp != NULL)
|
|
105 _debug_close();
|
|
106 if ((debug_fp = fopen(fname, "wb")) == NULL) {
|
|
107 fprintf(stderr, "Opening of file %s failed\n", fname);
|
|
108 exit(1);
|
|
109 }
|
|
110 fwrite(&version, 1, sizeof(char), debug_fp);
|
|
111 }
|
|
112
|
|
113 // function must be called before _debug_msg. It sets up the
|
|
114 // structure for the function that follows
|
|
115 void _debug_msg_info(int line, char* file, int type) {
|
|
116 char *x;
|
|
117 if (debug_fp == NULL) {
|
|
118 fprintf(stderr, "debug_fp is NULL\n");
|
|
119 return;
|
|
120 }
|
|
121 info_ptr = (struct _debug_item*) xmalloc(sizeof(struct _debug_item));
|
|
122 info_ptr->type = type;
|
|
123 info_ptr->line = line;
|
|
124 x = (func_head==NULL?"No Function":func_head->name);
|
|
125 info_ptr->function = (char*) xmalloc(strlen(x)+1);
|
|
126 strcpy(info_ptr->function, x);
|
|
127
|
|
128 info_ptr->file = (char*) xmalloc(strlen(file)+1);
|
|
129 strcpy(info_ptr->file, file);
|
|
130
|
|
131 //put the current record on a temp linked list
|
|
132 info_ptr->next = temp_list;
|
|
133 temp_list = info_ptr;
|
|
134 }
|
|
135
|
|
136 void _debug_msg_text(char* fmt, ...) {
|
|
137 va_list ap;
|
|
138 int f, g;
|
|
139 char x[2];
|
|
140 struct _debug_item *temp;
|
|
141 if (debug_fp == NULL)
|
|
142 return;
|
|
143 va_start(ap, fmt);
|
|
144 // get the record off of the temp_list
|
|
145 info_ptr = temp_list;
|
|
146 if (info_ptr != NULL)
|
|
147 temp_list = info_ptr->next;
|
|
148 else {
|
|
149 fprintf(stderr, "NULL info_ptr. ERROR!!\n");
|
|
150 exit(-2);
|
|
151 }
|
|
152 // according to glibc 2.1, this should return the req. number of bytes for
|
|
153 // the string
|
|
154 #ifdef _WIN32
|
|
155 // vsnprintf trick doesn't work. must use function called _vscprintf
|
|
156 // cannot find much documentation about this on internet or anywhere.
|
|
157 // I assume it isn't a standard function, but only in VisualC++
|
|
158 f = _vscprintf(fmt, ap);
|
|
159 #else
|
|
160 f = vsnprintf(x, 1, fmt, ap);
|
|
161 #endif
|
31
|
162 va_end(ap); // must be called after vsnprintf()
|
16
|
163
|
|
164 if (f > 0 && f < MAX_MESSAGE_SIZE) {
|
|
165 info_ptr->text = (char*) xmalloc(f+1);
|
31
|
166 va_start(ap, fmt);
|
16
|
167 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) {
|
|
168 fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt);
|
|
169 exit(-2);
|
|
170 }
|
31
|
171 va_end(ap);
|
16
|
172 info_ptr->text[g] = '\0';
|
|
173 if (f != g) {
|
|
174 fprintf(stderr, "_debug_msg: f != g\n");
|
|
175 }
|
|
176 } else if (f > 0) { // it is over the max_message_size then
|
|
177 f += strlen(info_ptr->file)+strlen(info_ptr->function);
|
|
178 temp = info_ptr;
|
|
179 _debug_write(); // dump the current messages
|
|
180 info_ptr = temp;
|
31
|
181 va_start(ap, fmt);
|
16
|
182 _debug_write_msg(info_ptr, fmt, &ap, f);
|
31
|
183 va_end(ap);
|
16
|
184 free(info_ptr->function);
|
|
185 free(info_ptr->file);
|
|
186 free(info_ptr);
|
|
187 info_ptr = NULL;
|
|
188 return;
|
|
189 } else {
|
|
190 fprintf(stderr, "_debug_msg: error getting requested size of debug message\n");
|
|
191 info_ptr->text = "ERROR Saving\n";
|
|
192 }
|
|
193
|
|
194 if (item_head == NULL)
|
|
195 item_head = info_ptr;
|
|
196
|
|
197 info_ptr->next = NULL;
|
|
198 if (item_tail != NULL)
|
|
199 item_tail->next = info_ptr;
|
|
200 item_tail = info_ptr;
|
|
201
|
|
202 if (++curr_items == max_items) {
|
|
203 // here we will jump off and save the contents
|
|
204 _debug_write();
|
|
205 info_ptr = NULL;
|
|
206 }
|
|
207 }
|
|
208
|
31
|
209 void _debug_hexdump(unsigned char *x, int y, int cols) {
|
16
|
210 struct _debug_item *temp;
|
|
211 if (debug_fp == NULL)
|
|
212 return;
|
|
213 info_ptr = temp_list;
|
|
214 if (info_ptr != NULL)
|
|
215 temp_list = info_ptr->next;
|
|
216 temp = info_ptr;
|
|
217 _debug_write();
|
|
218 info_ptr = temp;
|
|
219 _debug_write_hex(info_ptr, x, y, cols);
|
|
220 free(info_ptr->function);
|
|
221 free(info_ptr->file);
|
|
222 free(info_ptr);
|
|
223 info_ptr = NULL;
|
|
224 }
|
|
225
|
|
226 void _debug_func(char *function) {
|
|
227 func_ptr = xmalloc (sizeof(struct _debug_func));
|
|
228 func_ptr->name = xmalloc(strlen(function)+1);
|
|
229 strcpy(func_ptr->name, function);
|
|
230 func_ptr->next = func_head;
|
|
231 func_head = func_ptr;
|
|
232 }
|
|
233
|
|
234 void _debug_func_ret() {
|
|
235 //remove the head item
|
|
236 func_ptr = func_head;
|
|
237 if (func_head != NULL) {
|
|
238 func_head = func_head->next;
|
|
239 free(func_ptr->name);
|
|
240 free(func_ptr);
|
|
241 } else {
|
|
242 DIE(("function list is empty!\n"));
|
|
243 }
|
|
244 }
|
|
245
|
|
246 void _debug_close(void) {
|
|
247 _debug_write();
|
|
248 while (func_head != NULL) {
|
|
249 func_ptr = func_head;
|
|
250 func_head = func_head->next;
|
|
251 free(func_ptr->name);
|
|
252 free(func_ptr);
|
|
253 }
|
|
254
|
|
255 if (debug_fp != NULL)
|
|
256 fclose(debug_fp);
|
|
257 debug_fp = NULL;
|
|
258
|
|
259 if (func_head != NULL)
|
|
260 while (func_head != NULL) {
|
|
261 printf("function '%s' still on stack\n", func_head->name);
|
|
262 func_head = func_head->next;
|
|
263 }
|
|
264 }
|
|
265
|
|
266 void _debug_write() {
|
|
267 size_t size, ptr, funcname, filename, text, end;
|
31
|
268 char *buf = NULL, rec_type;
|
16
|
269 long index_pos = ftell (debug_fp), file_pos = index_pos;
|
|
270 // add 2. One for the pointer to the next index,
|
|
271 // one for the count of this index
|
|
272 int index_size = ((curr_items+2) * sizeof(int));
|
|
273 int *index;
|
|
274 int index_ptr = 0;
|
|
275 struct _debug_file_rec_m mfile_rec;
|
|
276 struct _debug_file_rec_l lfile_rec;
|
|
277
|
|
278 if (curr_items == 0)
|
|
279 // no items to write.
|
|
280 return;
|
|
281 index = (int*) xmalloc(index_size);
|
|
282 file_pos += index_size;
|
|
283 // write the index first, we will re-write it later, but
|
|
284 // we want to allocate the space
|
|
285 fwrite(index, index_size, 1, debug_fp);
|
|
286 index[index_ptr++] = curr_items;
|
|
287
|
|
288 item_ptr = item_head;
|
|
289 while (item_ptr != NULL) {
|
|
290 file_pos = ftell(debug_fp);
|
|
291 index[index_ptr++] = file_pos;
|
|
292 size = strlen(item_ptr->function)+strlen(item_ptr->file)+
|
|
293 strlen(item_ptr->text) + 3; //for the three \0s
|
31
|
294 if (buf) free(buf);
|
16
|
295 buf = xmalloc(size+1);
|
|
296 ptr = 0;
|
|
297 funcname=ptr;
|
|
298 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
|
|
299 filename=ptr;
|
|
300 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1;
|
|
301 text=ptr;
|
|
302 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1;
|
|
303 end=ptr;
|
|
304 if (end > USHRT_MAX) { // bigger than can be stored in a short
|
|
305 rec_type = 'L';
|
|
306 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
307 lfile_rec.type = item_ptr->type;
|
|
308 lfile_rec.line = item_ptr->line;
|
|
309 lfile_rec.funcname = funcname;
|
|
310 lfile_rec.filename = filename;
|
|
311 lfile_rec.text = text;
|
|
312 lfile_rec.end = end;
|
|
313 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
|
|
314 } else {
|
|
315 rec_type = 'M';
|
|
316 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
317 mfile_rec.type = item_ptr->type;
|
|
318 mfile_rec.line = item_ptr->line;
|
|
319 mfile_rec.funcname = funcname;
|
|
320 mfile_rec.filename = filename;
|
|
321 mfile_rec.text = text;
|
|
322 mfile_rec.end = end;
|
|
323 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
|
|
324 }
|
|
325 fwrite(buf, 1, ptr, debug_fp);
|
31
|
326 if (buf) free(buf); buf = NULL;
|
16
|
327 item_head = item_ptr->next;
|
|
328 free(item_ptr->function);
|
|
329 free(item_ptr->file);
|
|
330 free(item_ptr->text);
|
|
331 free(item_ptr);
|
|
332 item_ptr = item_head;
|
|
333 }
|
|
334 curr_items = 0;
|
|
335 index[index_ptr] = ftell(debug_fp);
|
|
336
|
|
337 // we should now have a complete index
|
|
338 fseek(debug_fp, index_pos, SEEK_SET);
|
|
339 fwrite(index, index_size, 1, debug_fp);
|
|
340 fseek(debug_fp, 0, SEEK_END);
|
|
341 item_ptr = item_head = item_tail = NULL;
|
|
342 free(index);
|
31
|
343 if (buf) free(buf); buf = NULL;
|
16
|
344 }
|
|
345
|
|
346 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size) {
|
|
347 struct _debug_file_rec_l lfile_rec;
|
|
348 struct _debug_file_rec_m mfile_rec;
|
|
349 unsigned char rec_type;
|
|
350 int index_size = 3 * sizeof(int);
|
|
351 int *index = malloc(index_size);
|
|
352 int index_pos, file_pos;
|
|
353 char zero='\0';
|
|
354 unsigned int end;
|
|
355 index[0] = 1; //only one item in this index
|
|
356 index_pos = ftell(debug_fp);
|
|
357 fwrite(index, index_size, 1, debug_fp);
|
|
358
|
|
359 index[1] = ftell(debug_fp);
|
|
360
|
|
361 if (size > USHRT_MAX) { // bigger than can be stored in a short
|
|
362 rec_type = 'L';
|
|
363 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
364 lfile_rec.type = item->type;
|
|
365 lfile_rec.line = item->line;
|
|
366 lfile_rec.funcname = 0;
|
|
367 lfile_rec.filename = strlen(item->function)+1;
|
|
368 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
|
|
369 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
|
|
370 } else {
|
|
371 rec_type = 'M';
|
|
372 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
373 mfile_rec.type = item->type;
|
|
374 mfile_rec.line = item->line;
|
|
375 mfile_rec.funcname = 0;
|
|
376 mfile_rec.filename = strlen(item->function)+1;
|
|
377 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
|
|
378 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
|
|
379 }
|
|
380 file_pos = ftell(debug_fp);
|
|
381 fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
|
|
382 fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
|
|
383 vfprintf(debug_fp, fmt, *ap);
|
|
384 fwrite(&zero, 1, 1, debug_fp);
|
|
385
|
|
386 end = ftell(debug_fp)-file_pos;
|
|
387
|
|
388 index[2] = ftell(debug_fp);
|
|
389 fseek(debug_fp, index_pos, SEEK_SET);
|
|
390 fwrite(index, index_size, 1, debug_fp);
|
|
391 if (size > USHRT_MAX) {
|
|
392 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
393 lfile_rec.end = end;
|
|
394 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
|
|
395 } else {
|
|
396 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
397 mfile_rec.end = end;
|
|
398 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
|
|
399 }
|
|
400 fseek(debug_fp, 0, SEEK_END);
|
|
401 // that should do it...
|
|
402 }
|
|
403
|
31
|
404 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col) {
|
16
|
405 struct _debug_file_rec_l lfile_rec;
|
|
406 unsigned char rec_type;
|
|
407 int index_size = 3 * sizeof(int);
|
|
408 int *index = malloc(index_size);
|
|
409 int index_pos, file_pos;
|
|
410 char zero='\0';
|
|
411 index[0] = 1; // only one item in this index run
|
|
412 index_pos = ftell(debug_fp);
|
|
413 fwrite(index, index_size, 1, debug_fp);
|
|
414 index[1] = ftell(debug_fp);
|
|
415
|
|
416 // always use the long
|
|
417 rec_type = 'L';
|
|
418 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
419 lfile_rec.type = item->type;
|
|
420 lfile_rec.line = item->line;
|
|
421 lfile_rec.funcname = 0;
|
|
422 lfile_rec.filename = strlen(item->function)+1;
|
|
423 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
|
|
424 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
|
|
425
|
|
426 file_pos = ftell(debug_fp);
|
|
427 fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
|
|
428 fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
|
|
429
|
|
430 _pst_debug_hexdump(debug_fp, buf, size, col);
|
|
431 fwrite(&zero, 1, 1, debug_fp);
|
|
432 lfile_rec.end = ftell(debug_fp)-file_pos;
|
|
433
|
|
434 index[2] = ftell(debug_fp);
|
|
435 fseek(debug_fp, index_pos, SEEK_SET);
|
|
436 fwrite(index, index_size, 1, debug_fp);
|
|
437 fwrite(&rec_type, 1, sizeof(char), debug_fp);
|
|
438 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
|
|
439 fseek(debug_fp, 0, SEEK_END);
|
|
440 }
|
|
441
|
|
442 void * xmalloc(size_t size) {
|
|
443 void *mem = malloc(size);
|
|
444 if (mem == NULL) {
|
|
445 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size);
|
|
446 exit(1);
|
|
447 }
|
|
448 return mem;
|
|
449 }
|
|
450
|