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