Mercurial > libpst
comparison src/debug.c @ 101:1fc33da23175
fix for orphan children when building descriptor tree, avoid writing uninitialized data to debug log file
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sun, 05 Oct 2008 09:56:32 -0700 |
parents | 9bd455802437 |
children | 0f1492b7fe8b |
comparison
equal
deleted
inserted
replaced
100:1e4a7610d525 | 101:1fc33da23175 |
---|---|
96 if (!fname) return; | 96 if (!fname) return; |
97 if ((debug_fp = fopen(fname, "wb")) == NULL) { | 97 if ((debug_fp = fopen(fname, "wb")) == NULL) { |
98 fprintf(stderr, "Opening of file %s failed\n", fname); | 98 fprintf(stderr, "Opening of file %s failed\n", fname); |
99 exit(1); | 99 exit(1); |
100 } | 100 } |
101 pst_debug_fwrite(&version, 1, sizeof(char), debug_fp); | 101 pst_debug_fwrite(&version, sizeof(char), 1, debug_fp); |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 // function must be called before pst_debug_msg. It sets up the | 105 // function must be called before pst_debug_msg. It sets up the |
106 // structure for the function that follows | 106 // structure for the function that follows |
189 } else { | 189 } else { |
190 fprintf(stderr, "_debug_msg: error getting requested size of debug message\n"); | 190 fprintf(stderr, "_debug_msg: error getting requested size of debug message\n"); |
191 info_ptr->text = "ERROR Saving\n"; | 191 info_ptr->text = "ERROR Saving\n"; |
192 } | 192 } |
193 | 193 |
194 if (!item_head) | 194 // add to the linked list of pending items |
195 item_head = info_ptr; | 195 if (!item_head) item_head = info_ptr; |
196 | |
197 info_ptr->next = NULL; | 196 info_ptr->next = NULL; |
198 if (item_tail) item_tail->next = info_ptr; | 197 if (item_tail) item_tail->next = info_ptr; |
199 item_tail = info_ptr; | 198 item_tail = info_ptr; |
200 | 199 |
201 if (++curr_items == max_items) { | 200 if (++curr_items == max_items) { |
298 text=ptr; | 297 text=ptr; |
299 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1; | 298 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1; |
300 end=ptr; | 299 end=ptr; |
301 if (end > USHRT_MAX) { // bigger than can be stored in a short | 300 if (end > USHRT_MAX) { // bigger than can be stored in a short |
302 rec_type = 'L'; | 301 rec_type = 'L'; |
303 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 302 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
304 lfile_rec.type = item_ptr->type; | 303 lfile_rec.type = item_ptr->type; |
305 lfile_rec.line = item_ptr->line; | 304 lfile_rec.line = item_ptr->line; |
306 lfile_rec.funcname = funcname; | 305 lfile_rec.funcname = funcname; |
307 lfile_rec.filename = filename; | 306 lfile_rec.filename = filename; |
308 lfile_rec.text = text; | 307 lfile_rec.text = text; |
309 lfile_rec.end = end; | 308 lfile_rec.end = end; |
310 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); | 309 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
311 } else { | 310 } else { |
312 rec_type = 'M'; | 311 rec_type = 'M'; |
313 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 312 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
314 mfile_rec.type = item_ptr->type; | 313 mfile_rec.type = item_ptr->type; |
315 mfile_rec.line = item_ptr->line; | 314 mfile_rec.line = item_ptr->line; |
316 mfile_rec.funcname = funcname; | 315 mfile_rec.funcname = funcname; |
317 mfile_rec.filename = filename; | 316 mfile_rec.filename = filename; |
318 mfile_rec.text = text; | 317 mfile_rec.text = text; |
319 mfile_rec.end = end; | 318 mfile_rec.end = end; |
320 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); | 319 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
321 } | 320 } |
322 pst_debug_fwrite(buf, 1, ptr, debug_fp); | 321 pst_debug_fwrite(buf, ptr, 1, debug_fp); |
323 if (buf) free(buf); buf = NULL; | 322 if (buf) free(buf); buf = NULL; |
324 item_head = item_ptr->next; | 323 item_head = item_ptr->next; |
325 free(item_ptr->function); | 324 free(item_ptr->function); |
326 free(item_ptr->file); | 325 free(item_ptr->file); |
327 free(item_ptr->text); | 326 free(item_ptr->text); |
346 struct pst_debug_file_rec_m mfile_rec; | 345 struct pst_debug_file_rec_m mfile_rec; |
347 unsigned char rec_type; | 346 unsigned char rec_type; |
348 int index_size = 3 * sizeof(off_t); | 347 int index_size = 3 * sizeof(off_t); |
349 off_t index[3]; | 348 off_t index[3]; |
350 off_t index_pos, file_pos; | 349 off_t index_pos, file_pos; |
351 char zero='\0'; | 350 char zero = '\0'; |
352 unsigned int end; | 351 unsigned int end; |
353 if (!debug_fp) return; // no file | 352 if (!debug_fp) return; // no file |
354 index[0] = 1; //only one item in this index | 353 index[0] = 1; // only one item in this index |
354 index[1] = 0; // valgrind, avoid writing uninitialized data | |
355 index[2] = 0; // "" | |
355 index_pos = ftello(debug_fp); | 356 index_pos = ftello(debug_fp); |
356 pst_debug_fwrite(index, index_size, 1, debug_fp); | 357 pst_debug_fwrite(index, index_size, 1, debug_fp); |
357 | 358 |
358 index[1] = ftello(debug_fp); | 359 index[1] = ftello(debug_fp); |
359 | 360 |
360 if (size > USHRT_MAX) { // bigger than can be stored in a short | 361 if (size > USHRT_MAX) { // bigger than can be stored in a short |
361 rec_type = 'L'; | 362 rec_type = 'L'; |
362 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 363 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
363 lfile_rec.type = item->type; | 364 lfile_rec.type = item->type; |
364 lfile_rec.line = item->line; | 365 lfile_rec.line = item->line; |
365 lfile_rec.funcname = 0; | 366 lfile_rec.funcname = 0; |
366 lfile_rec.filename = strlen(item->function)+1; | 367 lfile_rec.filename = strlen(item->function)+1; |
367 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; | 368 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; |
369 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data | |
368 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); | 370 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
369 } else { | 371 } else { |
370 rec_type = 'M'; | 372 rec_type = 'M'; |
371 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 373 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
372 mfile_rec.type = item->type; | 374 mfile_rec.type = item->type; |
373 mfile_rec.line = item->line; | 375 mfile_rec.line = item->line; |
374 mfile_rec.funcname = 0; | 376 mfile_rec.funcname = 0; |
375 mfile_rec.filename = strlen(item->function)+1; | 377 mfile_rec.filename = strlen(item->function)+1; |
376 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1; | 378 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1; |
379 mfile_rec.end = 0; // valgrind, avoid writing uninitialized data | |
377 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); | 380 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
378 } | 381 } |
379 file_pos = ftello(debug_fp); | 382 file_pos = ftello(debug_fp); |
380 pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp); | 383 pst_debug_fwrite(item->function, strlen(item->function)+1, 1, debug_fp); |
381 pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp); | 384 pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp); |
386 | 389 |
387 index[2] = ftello(debug_fp); | 390 index[2] = ftello(debug_fp); |
388 fseeko(debug_fp, index_pos, SEEK_SET); | 391 fseeko(debug_fp, index_pos, SEEK_SET); |
389 pst_debug_fwrite(index, index_size, 1, debug_fp); | 392 pst_debug_fwrite(index, index_size, 1, debug_fp); |
390 if (size > USHRT_MAX) { | 393 if (size > USHRT_MAX) { |
391 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 394 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
392 lfile_rec.end = end; | 395 lfile_rec.end = end; |
393 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); | 396 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
394 } else { | 397 } else { |
395 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 398 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
396 mfile_rec.end = end; | 399 mfile_rec.end = end; |
397 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); | 400 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
398 } | 401 } |
399 fseeko(debug_fp, 0, SEEK_END); | 402 fseeko(debug_fp, 0, SEEK_END); |
400 } | 403 } |
414 pst_debug_fwrite(index, index_size, 1, debug_fp); | 417 pst_debug_fwrite(index, index_size, 1, debug_fp); |
415 index[1] = ftello(debug_fp); | 418 index[1] = ftello(debug_fp); |
416 | 419 |
417 // always use the long | 420 // always use the long |
418 rec_type = 'L'; | 421 rec_type = 'L'; |
419 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 422 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
420 lfile_rec.funcname = 0; | 423 lfile_rec.funcname = 0; |
421 lfile_rec.filename = strlen(item->function)+1; | 424 lfile_rec.filename = strlen(item->function)+1; |
422 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; | 425 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; |
423 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data | 426 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data |
424 lfile_rec.line = item->line; | 427 lfile_rec.line = item->line; |
434 lfile_rec.end = ftello(debug_fp) - file_pos; | 437 lfile_rec.end = ftello(debug_fp) - file_pos; |
435 | 438 |
436 index[2] = ftello(debug_fp); | 439 index[2] = ftello(debug_fp); |
437 fseeko(debug_fp, index_pos, SEEK_SET); | 440 fseeko(debug_fp, index_pos, SEEK_SET); |
438 pst_debug_fwrite(index, index_size, 1, debug_fp); | 441 pst_debug_fwrite(index, index_size, 1, debug_fp); |
439 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); | 442 pst_debug_fwrite(&rec_type, sizeof(char), 1, debug_fp); |
440 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); | 443 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
441 fseeko(debug_fp, 0, SEEK_END); | 444 fseeko(debug_fp, 0, SEEK_END); |
442 } | 445 } |
443 | 446 |
444 | 447 |