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