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