comparison src/debug.c @ 31:b88ceb81dba2

mege changes from Joe Nahmias
author carl
date Tue, 10 Jul 2007 17:17:28 -0700
parents c508ee15dfca
children 12cac756bc05
comparison
equal deleted inserted replaced
30:45eccad4b606 31:b88ceb81dba2
27 27
28 28
29 void _debug_init(char *fname); 29 void _debug_init(char *fname);
30 void _debug_msg_info (int line, char *file, int type); 30 void _debug_msg_info (int line, char *file, int type);
31 void _debug_msg(char* fmt, ...); 31 void _debug_msg(char* fmt, ...);
32 void _debug_hexdump(char *x, int y, int cols); 32 void _debug_hexdump(unsigned char *x, int y, int cols);
33 void _debug_func(char *function); 33 void _debug_func(char *function);
34 void _debug_func_ret(); 34 void _debug_func_ret();
35 void _debug_close(); 35 void _debug_close();
36 void _debug_write(); 36 void _debug_write();
37 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size); 37 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size);
38 void _debug_write_hex(struct _debug_item *item, char *buf, int size, int col); 38 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col);
39 void * xmalloc(size_t size); 39 void * xmalloc(size_t size);
40 40
41 // the largest text size we will store in memory. Otherwise we 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 42 // will do a debug_write, then create a new record, and write the
43 // text body directly to the file 43 // text body directly to the file
157 // I assume it isn't a standard function, but only in VisualC++ 157 // I assume it isn't a standard function, but only in VisualC++
158 f = _vscprintf(fmt, ap); 158 f = _vscprintf(fmt, ap);
159 #else 159 #else
160 f = vsnprintf(x, 1, fmt, ap); 160 f = vsnprintf(x, 1, fmt, ap);
161 #endif 161 #endif
162 va_end(ap); // must be called after vsnprintf()
162 163
163 if (f > 0 && f < MAX_MESSAGE_SIZE) { 164 if (f > 0 && f < MAX_MESSAGE_SIZE) {
164 info_ptr->text = (char*) xmalloc(f+1); 165 info_ptr->text = (char*) xmalloc(f+1);
166 va_start(ap, fmt);
165 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) { 167 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) {
166 fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt); 168 fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt);
167 exit(-2); 169 exit(-2);
168 } 170 }
171 va_end(ap);
169 info_ptr->text[g] = '\0'; 172 info_ptr->text[g] = '\0';
170 if (f != g) { 173 if (f != g) {
171 fprintf(stderr, "_debug_msg: f != g\n"); 174 fprintf(stderr, "_debug_msg: f != g\n");
172 } 175 }
173 } else if (f > 0) { // it is over the max_message_size then 176 } else if (f > 0) { // it is over the max_message_size then
174 f += strlen(info_ptr->file)+strlen(info_ptr->function); 177 f += strlen(info_ptr->file)+strlen(info_ptr->function);
175 temp = info_ptr; 178 temp = info_ptr;
176 _debug_write(); // dump the current messages 179 _debug_write(); // dump the current messages
177 info_ptr = temp; 180 info_ptr = temp;
181 va_start(ap, fmt);
178 _debug_write_msg(info_ptr, fmt, &ap, f); 182 _debug_write_msg(info_ptr, fmt, &ap, f);
183 va_end(ap);
179 free(info_ptr->function); 184 free(info_ptr->function);
180 free(info_ptr->file); 185 free(info_ptr->file);
181 free(info_ptr); 186 free(info_ptr);
182 info_ptr = NULL; 187 info_ptr = NULL;
183 return; 188 return;
184 } else { 189 } else {
185 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");
186 info_ptr->text = "ERROR Saving\n"; 191 info_ptr->text = "ERROR Saving\n";
187 } 192 }
188 va_end(ap);
189 193
190 if (item_head == NULL) 194 if (item_head == NULL)
191 item_head = info_ptr; 195 item_head = info_ptr;
192 196
193 info_ptr->next = NULL; 197 info_ptr->next = NULL;
200 _debug_write(); 204 _debug_write();
201 info_ptr = NULL; 205 info_ptr = NULL;
202 } 206 }
203 } 207 }
204 208
205 void _debug_hexdump(char *x, int y, int cols) { 209 void _debug_hexdump(unsigned char *x, int y, int cols) {
206 struct _debug_item *temp; 210 struct _debug_item *temp;
207 if (debug_fp == NULL) 211 if (debug_fp == NULL)
208 return; 212 return;
209 info_ptr = temp_list; 213 info_ptr = temp_list;
210 if (info_ptr != NULL) 214 if (info_ptr != NULL)
259 } 263 }
260 } 264 }
261 265
262 void _debug_write() { 266 void _debug_write() {
263 size_t size, ptr, funcname, filename, text, end; 267 size_t size, ptr, funcname, filename, text, end;
264 char *buf, rec_type; 268 char *buf = NULL, rec_type;
265 long index_pos = ftell (debug_fp), file_pos = index_pos; 269 long index_pos = ftell (debug_fp), file_pos = index_pos;
266 // add 2. One for the pointer to the next index, 270 // add 2. One for the pointer to the next index,
267 // one for the count of this index 271 // one for the count of this index
268 int index_size = ((curr_items+2) * sizeof(int)); 272 int index_size = ((curr_items+2) * sizeof(int));
269 int *index; 273 int *index;
285 while (item_ptr != NULL) { 289 while (item_ptr != NULL) {
286 file_pos = ftell(debug_fp); 290 file_pos = ftell(debug_fp);
287 index[index_ptr++] = file_pos; 291 index[index_ptr++] = file_pos;
288 size = strlen(item_ptr->function)+strlen(item_ptr->file)+ 292 size = strlen(item_ptr->function)+strlen(item_ptr->file)+
289 strlen(item_ptr->text) + 3; //for the three \0s 293 strlen(item_ptr->text) + 3; //for the three \0s
294 if (buf) free(buf);
290 buf = xmalloc(size+1); 295 buf = xmalloc(size+1);
291 ptr = 0; 296 ptr = 0;
292 funcname=ptr; 297 funcname=ptr;
293 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1; 298 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
294 filename=ptr; 299 filename=ptr;
316 mfile_rec.text = text; 321 mfile_rec.text = text;
317 mfile_rec.end = end; 322 mfile_rec.end = end;
318 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); 323 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
319 } 324 }
320 fwrite(buf, 1, ptr, debug_fp); 325 fwrite(buf, 1, ptr, debug_fp);
326 if (buf) free(buf); buf = NULL;
321 item_head = item_ptr->next; 327 item_head = item_ptr->next;
322 free(item_ptr->function); 328 free(item_ptr->function);
323 free(item_ptr->file); 329 free(item_ptr->file);
324 free(item_ptr->text); 330 free(item_ptr->text);
325 free(item_ptr); 331 free(item_ptr);
332 fseek(debug_fp, index_pos, SEEK_SET); 338 fseek(debug_fp, index_pos, SEEK_SET);
333 fwrite(index, index_size, 1, debug_fp); 339 fwrite(index, index_size, 1, debug_fp);
334 fseek(debug_fp, 0, SEEK_END); 340 fseek(debug_fp, 0, SEEK_END);
335 item_ptr = item_head = item_tail = NULL; 341 item_ptr = item_head = item_tail = NULL;
336 free(index); 342 free(index);
343 if (buf) free(buf); buf = NULL;
337 } 344 }
338 345
339 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size) { 346 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size) {
340 struct _debug_file_rec_l lfile_rec; 347 struct _debug_file_rec_l lfile_rec;
341 struct _debug_file_rec_m mfile_rec; 348 struct _debug_file_rec_m mfile_rec;
392 } 399 }
393 fseek(debug_fp, 0, SEEK_END); 400 fseek(debug_fp, 0, SEEK_END);
394 // that should do it... 401 // that should do it...
395 } 402 }
396 403
397 void _debug_write_hex(struct _debug_item *item, char *buf, int size, int col) { 404 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col) {
398 struct _debug_file_rec_l lfile_rec; 405 struct _debug_file_rec_l lfile_rec;
399 unsigned char rec_type; 406 unsigned char rec_type;
400 int index_size = 3 * sizeof(int); 407 int index_size = 3 * sizeof(int);
401 int *index = malloc(index_size); 408 int *index = malloc(index_size);
402 int index_pos, file_pos; 409 int index_pos, file_pos;