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