comparison src/debug.c @ 73:3cb02cb1e6cd stable-0-6-10

Patch from Robert Simpson to fix doubly-linked list in the cache_ptr code, and allow arrays of unicode strings (without converting them). More changes for Fedora packaging (#434727) Fixes for const correctness.
author Carl Byington <carl@five-ten-sg.com>
date Thu, 29 May 2008 18:51:02 -0700
parents b12f4e50e2e8
children 987aa872294e
comparison
equal deleted inserted replaced
72:c21e9c001256 73:3cb02cb1e6cd
26 char * name; 26 char * name;
27 struct pst_debug_func *next; 27 struct pst_debug_func *next;
28 } *func_head=NULL, *func_ptr=NULL; 28 } *func_head=NULL, *func_ptr=NULL;
29 29
30 30
31 void pst_debug_write_msg(struct pst_debug_item *item, char *fmt, va_list *ap, int size); 31 void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size);
32 void pst_debug_write_hex(struct pst_debug_item *item, unsigned char *buf, size_t size, int col); 32 void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col);
33 void * xmalloc(size_t size); 33 void * xmalloc(size_t size);
34
35 size_t pst_debug_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
36 return fwrite(ptr, size, nitems, stream);
37 }
38
34 39
35 // the largest text size we will store in memory. Otherwise we 40 // the largest text size we will store in memory. Otherwise we
36 // will do a debug_write, then create a new record, and write the 41 // will do a debug_write, then create a new record, and write the
37 // text body directly to the file 42 // text body directly to the file
38 #define MAX_MESSAGE_SIZE 4096 43 #define MAX_MESSAGE_SIZE 4096
39 44
40 void pst_debug(char *fmt, ...) { 45 void pst_debug(const char *fmt, ...) {
41 va_list ap; 46 va_list ap;
42 va_start(ap,fmt); 47 va_start(ap,fmt);
43 vfprintf(stderr, fmt, ap); 48 vfprintf(stderr, fmt, ap);
44 va_end(ap); 49 va_end(ap);
45 } 50 }
46 51
47 52
48 #define NUM_COL 30 53 #define NUM_COL 30
49 void pst_debug_hexdumper(FILE *out, unsigned char *buf, size_t size, int col, int delta) { 54 void pst_debug_hexdumper(FILE *out, char *buf, size_t size, int col, int delta) {
50 size_t off = 0, toff; 55 size_t off = 0, toff;
51 int count = 0; 56 int count = 0;
52 57
53 if (!out) return; // no file 58 if (!out) return; // no file
54 if (col == -1) col = NUM_COL; 59 if (col == -1) col = NUM_COL;
55 fprintf(out, "\n"); 60 fprintf(out, "\n");
56 while (off < size) { 61 while (off < size) {
57 fprintf(out, "%06X\t:", off+delta); 62 fprintf(out, "%06X\t:", off+delta);
58 toff = off; 63 toff = off;
59 while (count < col && off < size) { 64 while (count < col && off < size) {
60 fprintf(out, "%02hhx ", buf[off]); 65 fprintf(out, "%02hhx ", (unsigned char)buf[off]);
61 off++; count++; 66 off++; count++;
62 } 67 }
63 off = toff; 68 off = toff;
64 while (count < col) { 69 while (count < col) {
65 // only happens at end of block to pad the text over to the text column 70 // only happens at end of block to pad the text over to the text column
83 88
84 FILE *debug_fp = NULL; 89 FILE *debug_fp = NULL;
85 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0; 90 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0;
86 91
87 92
88 void pst_debug_init(char* fname) { 93 void pst_debug_init(const char* fname) {
89 unsigned char version = DEBUG_VERSION; 94 unsigned char version = DEBUG_VERSION;
90 item_head = item_tail = NULL; 95 item_head = item_tail = NULL;
91 curr_items = 0; 96 curr_items = 0;
92 if (debug_fp) pst_debug_close(); 97 if (debug_fp) pst_debug_close();
93 if (!fname) return; 98 if (!fname) return;
94 if ((debug_fp = fopen(fname, "wb")) == NULL) { 99 if ((debug_fp = fopen(fname, "wb")) == NULL) {
95 fprintf(stderr, "Opening of file %s failed\n", fname); 100 fprintf(stderr, "Opening of file %s failed\n", fname);
96 exit(1); 101 exit(1);
97 } 102 }
98 fwrite(&version, 1, sizeof(char), debug_fp); 103 pst_debug_fwrite(&version, 1, sizeof(char), debug_fp);
99 } 104 }
100 105
101 106
102 // function must be called before pst_debug_msg. It sets up the 107 // function must be called before pst_debug_msg. It sets up the
103 // structure for the function that follows 108 // structure for the function that follows
104 void pst_debug_msg_info(int line, char* file, int type) { 109 void pst_debug_msg_info(int line, const char* file, int type) {
105 char *x; 110 char *x;
106 if (!debug_fp) return; // no file 111 if (!debug_fp) return; // no file
107 info_ptr = (struct pst_debug_item*) xmalloc(sizeof(struct pst_debug_item)); 112 info_ptr = (struct pst_debug_item*) xmalloc(sizeof(struct pst_debug_item));
108 info_ptr->type = type; 113 info_ptr->type = type;
109 info_ptr->line = line; 114 info_ptr->line = line;
118 info_ptr->next = temp_list; 123 info_ptr->next = temp_list;
119 temp_list = info_ptr; 124 temp_list = info_ptr;
120 } 125 }
121 126
122 127
123 void pst_debug_msg_text(char* fmt, ...) { 128 void pst_debug_msg_text(const char* fmt, ...) {
124 va_list ap; 129 va_list ap;
125 int f, g; 130 int f, g;
126 char x[2]; 131 char x[2];
127 struct pst_debug_item *temp; 132 struct pst_debug_item *temp;
128 if (!debug_fp) return; // no file 133 if (!debug_fp) return; // no file
190 info_ptr = NULL; 195 info_ptr = NULL;
191 } 196 }
192 } 197 }
193 198
194 199
195 void pst_debug_hexdump(unsigned char *x, size_t y, int cols, int delta) { 200 void pst_debug_hexdump(char *x, size_t y, int cols, int delta) {
196 struct pst_debug_item *temp; 201 struct pst_debug_item *temp;
197 if (!debug_fp) return; // no file 202 if (!debug_fp) return; // no file
198 info_ptr = temp_list; 203 info_ptr = temp_list;
199 if (info_ptr) temp_list = info_ptr->next; 204 if (info_ptr) temp_list = info_ptr->next;
200 temp = info_ptr; 205 temp = info_ptr;
206 free(info_ptr); 211 free(info_ptr);
207 info_ptr = NULL; 212 info_ptr = NULL;
208 } 213 }
209 214
210 215
211 void pst_debug_func(char *function) { 216 void pst_debug_func(const char *function) {
212 func_ptr = xmalloc (sizeof(struct pst_debug_func)); 217 func_ptr = xmalloc (sizeof(struct pst_debug_func));
213 func_ptr->name = xmalloc(strlen(function)+1); 218 func_ptr->name = xmalloc(strlen(function)+1);
214 strcpy(func_ptr->name, function); 219 strcpy(func_ptr->name, function);
215 func_ptr->next = func_head; 220 func_ptr->next = func_head;
216 func_head = func_ptr; 221 func_head = func_ptr;
262 index = (off_t*)xmalloc(index_size); 267 index = (off_t*)xmalloc(index_size);
263 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data 268 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data
264 file_pos += index_size; 269 file_pos += index_size;
265 // write the index first, we will re-write it later, but 270 // write the index first, we will re-write it later, but
266 // we want to allocate the space 271 // we want to allocate the space
267 fwrite(index, index_size, 1, debug_fp); 272 pst_debug_fwrite(index, index_size, 1, debug_fp);
268 index[index_ptr++] = curr_items; 273 index[index_ptr++] = curr_items;
269 274
270 item_ptr = item_head; 275 item_ptr = item_head;
271 while (item_ptr) { 276 while (item_ptr) {
272 file_pos = ftell(debug_fp); 277 file_pos = ftell(debug_fp);
284 text=ptr; 289 text=ptr;
285 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1; 290 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1;
286 end=ptr; 291 end=ptr;
287 if (end > USHRT_MAX) { // bigger than can be stored in a short 292 if (end > USHRT_MAX) { // bigger than can be stored in a short
288 rec_type = 'L'; 293 rec_type = 'L';
289 fwrite(&rec_type, 1, sizeof(char), debug_fp); 294 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
290 lfile_rec.type = item_ptr->type; 295 lfile_rec.type = item_ptr->type;
291 lfile_rec.line = item_ptr->line; 296 lfile_rec.line = item_ptr->line;
292 lfile_rec.funcname = funcname; 297 lfile_rec.funcname = funcname;
293 lfile_rec.filename = filename; 298 lfile_rec.filename = filename;
294 lfile_rec.text = text; 299 lfile_rec.text = text;
295 lfile_rec.end = end; 300 lfile_rec.end = end;
296 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 301 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
297 } else { 302 } else {
298 rec_type = 'M'; 303 rec_type = 'M';
299 fwrite(&rec_type, 1, sizeof(char), debug_fp); 304 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
300 mfile_rec.type = item_ptr->type; 305 mfile_rec.type = item_ptr->type;
301 mfile_rec.line = item_ptr->line; 306 mfile_rec.line = item_ptr->line;
302 mfile_rec.funcname = funcname; 307 mfile_rec.funcname = funcname;
303 mfile_rec.filename = filename; 308 mfile_rec.filename = filename;
304 mfile_rec.text = text; 309 mfile_rec.text = text;
305 mfile_rec.end = end; 310 mfile_rec.end = end;
306 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); 311 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
307 } 312 }
308 fwrite(buf, 1, ptr, debug_fp); 313 pst_debug_fwrite(buf, 1, ptr, debug_fp);
309 if (buf) free(buf); buf = NULL; 314 if (buf) free(buf); buf = NULL;
310 item_head = item_ptr->next; 315 item_head = item_ptr->next;
311 free(item_ptr->function); 316 free(item_ptr->function);
312 free(item_ptr->file); 317 free(item_ptr->file);
313 free(item_ptr->text); 318 free(item_ptr->text);
317 curr_items = 0; 322 curr_items = 0;
318 index[index_ptr] = ftell(debug_fp); 323 index[index_ptr] = ftell(debug_fp);
319 324
320 // we should now have a complete index 325 // we should now have a complete index
321 fseek(debug_fp, index_pos, SEEK_SET); 326 fseek(debug_fp, index_pos, SEEK_SET);
322 fwrite(index, index_size, 1, debug_fp); 327 pst_debug_fwrite(index, index_size, 1, debug_fp);
323 fseek(debug_fp, 0, SEEK_END); 328 fseek(debug_fp, 0, SEEK_END);
324 item_ptr = item_head = item_tail = NULL; 329 item_ptr = item_head = item_tail = NULL;
325 free(index); 330 free(index);
326 if (buf) free(buf); 331 if (buf) free(buf);
327 } 332 }
328 333
329 334
330 void pst_debug_write_msg(struct pst_debug_item *item, char *fmt, va_list *ap, int size) { 335 void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size) {
331 struct pst_debug_file_rec_l lfile_rec; 336 struct pst_debug_file_rec_l lfile_rec;
332 struct pst_debug_file_rec_m mfile_rec; 337 struct pst_debug_file_rec_m mfile_rec;
333 unsigned char rec_type; 338 unsigned char rec_type;
334 int index_size = 3 * sizeof(off_t); 339 int index_size = 3 * sizeof(off_t);
335 off_t index[3]; 340 off_t index[3];
337 char zero='\0'; 342 char zero='\0';
338 unsigned int end; 343 unsigned int end;
339 if (!debug_fp) return; // no file 344 if (!debug_fp) return; // no file
340 index[0] = 1; //only one item in this index 345 index[0] = 1; //only one item in this index
341 index_pos = ftell(debug_fp); 346 index_pos = ftell(debug_fp);
342 fwrite(index, index_size, 1, debug_fp); 347 pst_debug_fwrite(index, index_size, 1, debug_fp);
343 348
344 index[1] = ftell(debug_fp); 349 index[1] = ftell(debug_fp);
345 350
346 if (size > USHRT_MAX) { // bigger than can be stored in a short 351 if (size > USHRT_MAX) { // bigger than can be stored in a short
347 rec_type = 'L'; 352 rec_type = 'L';
348 fwrite(&rec_type, 1, sizeof(char), debug_fp); 353 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
349 lfile_rec.type = item->type; 354 lfile_rec.type = item->type;
350 lfile_rec.line = item->line; 355 lfile_rec.line = item->line;
351 lfile_rec.funcname = 0; 356 lfile_rec.funcname = 0;
352 lfile_rec.filename = strlen(item->function)+1; 357 lfile_rec.filename = strlen(item->function)+1;
353 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; 358 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
354 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 359 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
355 } else { 360 } else {
356 rec_type = 'M'; 361 rec_type = 'M';
357 fwrite(&rec_type, 1, sizeof(char), debug_fp); 362 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
358 mfile_rec.type = item->type; 363 mfile_rec.type = item->type;
359 mfile_rec.line = item->line; 364 mfile_rec.line = item->line;
360 mfile_rec.funcname = 0; 365 mfile_rec.funcname = 0;
361 mfile_rec.filename = strlen(item->function)+1; 366 mfile_rec.filename = strlen(item->function)+1;
362 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1; 367 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
363 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); 368 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
364 } 369 }
365 file_pos = ftell(debug_fp); 370 file_pos = ftell(debug_fp);
366 fwrite(item->function, strlen(item->function)+1, 1, debug_fp); 371 pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
367 fwrite(item->file, strlen(item->file)+1, 1, debug_fp); 372 pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
368 vfprintf(debug_fp, fmt, *ap); 373 vfprintf(debug_fp, fmt, *ap);
369 fwrite(&zero, 1, 1, debug_fp); 374 pst_debug_fwrite(&zero, 1, 1, debug_fp);
370 375
371 end = ftell(debug_fp)-file_pos; 376 end = ftell(debug_fp)-file_pos;
372 377
373 index[2] = ftell(debug_fp); 378 index[2] = ftell(debug_fp);
374 fseek(debug_fp, index_pos, SEEK_SET); 379 fseek(debug_fp, index_pos, SEEK_SET);
375 fwrite(index, index_size, 1, debug_fp); 380 pst_debug_fwrite(index, index_size, 1, debug_fp);
376 if (size > USHRT_MAX) { 381 if (size > USHRT_MAX) {
377 fwrite(&rec_type, 1, sizeof(char), debug_fp); 382 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
378 lfile_rec.end = end; 383 lfile_rec.end = end;
379 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 384 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
380 } else { 385 } else {
381 fwrite(&rec_type, 1, sizeof(char), debug_fp); 386 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
382 mfile_rec.end = end; 387 mfile_rec.end = end;
383 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); 388 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
384 } 389 }
385 fseek(debug_fp, 0, SEEK_END); 390 fseek(debug_fp, 0, SEEK_END);
386 } 391 }
387 392
388 393
389 void pst_debug_write_hex(struct pst_debug_item *item, unsigned char *buf, size_t size, int col) { 394 void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col) {
390 struct pst_debug_file_rec_l lfile_rec; 395 struct pst_debug_file_rec_l lfile_rec;
391 unsigned char rec_type; 396 unsigned char rec_type;
392 int index_size = 3 * sizeof(off_t); 397 int index_size = 3 * sizeof(off_t);
393 off_t index_pos, file_pos, index[3]; 398 off_t index_pos, file_pos, index[3];
394 char zero='\0'; 399 char zero='\0';
395 if (!debug_fp) return; // no file 400 if (!debug_fp) return; // no file
396 index[0] = 1; // only one item in this index run 401 index[0] = 1; // only one item in this index run
397 index[1] = 0; // valgrind, avoid writing uninitialized data 402 index[1] = 0; // valgrind, avoid writing uninitialized data
398 index[2] = 0; // "" 403 index[2] = 0; // ""
399 index_pos = ftell(debug_fp); 404 index_pos = ftell(debug_fp);
400 fwrite(index, index_size, 1, debug_fp); 405 pst_debug_fwrite(index, index_size, 1, debug_fp);
401 index[1] = ftell(debug_fp); 406 index[1] = ftell(debug_fp);
402 407
403 // always use the long 408 // always use the long
404 rec_type = 'L'; 409 rec_type = 'L';
405 fwrite(&rec_type, 1, sizeof(char), debug_fp); 410 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
406 lfile_rec.funcname = 0; 411 lfile_rec.funcname = 0;
407 lfile_rec.filename = strlen(item->function)+1; 412 lfile_rec.filename = strlen(item->function)+1;
408 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; 413 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
409 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data 414 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data
410 lfile_rec.line = item->line; 415 lfile_rec.line = item->line;
411 lfile_rec.type = item->type; 416 lfile_rec.type = item->type;
412 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 417 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
413 418
414 file_pos = ftell(debug_fp); 419 file_pos = ftell(debug_fp);
415 fwrite(item->function, strlen(item->function)+1, 1, debug_fp); 420 pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
416 fwrite(item->file, strlen(item->file)+1, 1, debug_fp); 421 pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
417 422
418 pst_debug_hexdumper(debug_fp, buf, size, col, 0); 423 pst_debug_hexdumper(debug_fp, buf, size, col, 0);
419 fwrite(&zero, 1, 1, debug_fp); 424 pst_debug_fwrite(&zero, 1, 1, debug_fp);
420 lfile_rec.end = ftell(debug_fp) - file_pos; 425 lfile_rec.end = ftell(debug_fp) - file_pos;
421 426
422 index[2] = ftell(debug_fp); 427 index[2] = ftell(debug_fp);
423 fseek(debug_fp, index_pos, SEEK_SET); 428 fseek(debug_fp, index_pos, SEEK_SET);
424 fwrite(index, index_size, 1, debug_fp); 429 pst_debug_fwrite(index, index_size, 1, debug_fp);
425 fwrite(&rec_type, 1, sizeof(char), debug_fp); 430 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp);
426 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 431 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
427 fseek(debug_fp, 0, SEEK_END); 432 fseek(debug_fp, 0, SEEK_END);
428 } 433 }
429 434
430 435
431 void *xmalloc(size_t size) { 436 void *xmalloc(size_t size) {