Mercurial > libpst
annotate src/debug.c @ 75:987aa872294e stable-0-6-11
Use ftello/fseeko to properly handle large files.
Document and properly use datasize field in b5 blocks.
Fix some MSVC compile issues and collect MSVC dependencies into one place.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 03 Jun 2008 12:00:58 -0700 |
parents | 3cb02cb1e6cd |
children | 56fa05fd5271 |
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]; | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
128 char *buf = NULL; |
46 | 129 struct pst_debug_item *temp; |
130 if (!debug_fp) return; // no file | |
131 // get the record off of the temp_list | |
132 info_ptr = temp_list; | |
133 if (info_ptr) | |
134 temp_list = info_ptr->next; | |
135 else { | |
136 fprintf(stderr, "NULL info_ptr. ERROR!!\n"); | |
137 exit(-2); | |
138 } | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
139 |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
140 #ifdef _WIN32 |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
141 // 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
|
142 g = 2000; |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
143 f = -1; |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
144 while (f < 0) { |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
145 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
|
146 va_start(ap, fmt); |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
147 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
|
148 va_end(ap); |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
149 g += g/2; |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
150 } |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
151 free(buf); |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
152 #else |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
153 // 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
|
154 // the string |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
155 va_start(ap, fmt); |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
156 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
|
157 va_end(ap); |
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
158 #endif |
16 | 159 |
46 | 160 if (f > 0 && f < MAX_MESSAGE_SIZE) { |
161 info_ptr->text = (char*) xmalloc(f+1); | |
162 va_start(ap, fmt); | |
163 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
|
164 fprintf(stderr, "_debug_msg: Dying! vsnprintf returned -1 for format \"%s\"\n", fmt); |
46 | 165 exit(-2); |
166 } | |
167 va_end(ap); | |
168 info_ptr->text[g] = '\0'; | |
169 if (f != g) { | |
170 fprintf(stderr, "_debug_msg: f != g\n"); | |
171 } | |
172 } else if (f > 0) { // it is over the max_message_size then | |
173 f += strlen(info_ptr->file)+strlen(info_ptr->function); | |
174 temp = info_ptr; | |
175 pst_debug_write(); // dump the current messages | |
176 info_ptr = temp; | |
177 va_start(ap, fmt); | |
178 pst_debug_write_msg(info_ptr, fmt, &ap, f); | |
179 va_end(ap); | |
180 free(info_ptr->function); | |
181 free(info_ptr->file); | |
182 free(info_ptr); | |
183 info_ptr = NULL; | |
184 return; | |
185 } else { | |
186 fprintf(stderr, "_debug_msg: error getting requested size of debug message\n"); | |
187 info_ptr->text = "ERROR Saving\n"; | |
188 } | |
16 | 189 |
46 | 190 if (!item_head) |
191 item_head = info_ptr; | |
16 | 192 |
46 | 193 info_ptr->next = NULL; |
194 if (item_tail) item_tail->next = info_ptr; | |
195 item_tail = info_ptr; | |
16 | 196 |
46 | 197 if (++curr_items == max_items) { |
198 // here we will jump off and save the contents | |
199 pst_debug_write(); | |
200 info_ptr = NULL; | |
201 } | |
16 | 202 } |
203 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
204 |
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
|
205 void pst_debug_hexdump(char *x, size_t y, int cols, int delta) { |
46 | 206 struct pst_debug_item *temp; |
207 if (!debug_fp) return; // no file | |
208 info_ptr = temp_list; | |
209 if (info_ptr) temp_list = info_ptr->next; | |
210 temp = info_ptr; | |
211 pst_debug_write(); | |
212 info_ptr = temp; | |
213 pst_debug_write_hex(info_ptr, x, y, cols); | |
214 free(info_ptr->function); | |
215 free(info_ptr->file); | |
216 free(info_ptr); | |
217 info_ptr = NULL; | |
16 | 218 } |
219 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
220 |
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
|
221 void pst_debug_func(const char *function) { |
46 | 222 func_ptr = xmalloc (sizeof(struct pst_debug_func)); |
223 func_ptr->name = xmalloc(strlen(function)+1); | |
224 strcpy(func_ptr->name, function); | |
225 func_ptr->next = func_head; | |
226 func_head = func_ptr; | |
16 | 227 } |
228 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
229 |
46 | 230 void pst_debug_func_ret() { |
231 //remove the head item | |
232 func_ptr = func_head; | |
233 if (func_head) { | |
234 func_head = func_head->next; | |
235 free(func_ptr->name); | |
236 free(func_ptr); | |
237 } else { | |
238 DIE(("function list is empty!\n")); | |
239 } | |
16 | 240 } |
241 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
242 |
46 | 243 void pst_debug_close(void) { |
244 pst_debug_write(); | |
245 while (func_head) { | |
246 func_ptr = func_head; | |
247 func_head = func_head->next; | |
248 free(func_ptr->name); | |
249 free(func_ptr); | |
250 } | |
251 if (debug_fp) fclose(debug_fp); | |
252 debug_fp = NULL; | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
253 } |
16 | 254 |
255 | |
46 | 256 void pst_debug_write() { |
257 size_t size, ptr, funcname, filename, text, end; | |
258 char *buf = NULL, rec_type; | |
259 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
|
260 off_t index_pos = ftello(debug_fp); |
43 | 261 off_t file_pos = index_pos; |
46 | 262 // add 2. One for the pointer to the next index, |
263 // one for the count of this index | |
264 int index_size = ((curr_items+2) * sizeof(off_t)); | |
265 off_t *index; | |
266 int index_ptr = 0; | |
267 struct pst_debug_file_rec_m mfile_rec; | |
268 struct pst_debug_file_rec_l lfile_rec; | |
16 | 269 |
46 | 270 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
|
271 |
46 | 272 index = (off_t*)xmalloc(index_size); |
273 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data | |
274 file_pos += index_size; | |
275 // write the index first, we will re-write it later, but | |
276 // 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
|
277 pst_debug_fwrite(index, index_size, 1, debug_fp); |
46 | 278 index[index_ptr++] = curr_items; |
16 | 279 |
46 | 280 item_ptr = item_head; |
281 while (item_ptr) { | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
282 file_pos = ftello(debug_fp); |
46 | 283 index[index_ptr++] = file_pos; |
284 size = strlen(item_ptr->function) + | |
285 strlen(item_ptr->file) + | |
286 strlen(item_ptr->text) + 3; //for the three \0s | |
287 if (buf) free(buf); | |
288 buf = xmalloc(size+1); | |
289 ptr = 0; | |
290 funcname=ptr; | |
291 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1; | |
292 filename=ptr; | |
293 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1; | |
294 text=ptr; | |
295 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1; | |
296 end=ptr; | |
297 if (end > USHRT_MAX) { // bigger than can be stored in a short | |
298 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
|
299 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 300 lfile_rec.type = item_ptr->type; |
301 lfile_rec.line = item_ptr->line; | |
302 lfile_rec.funcname = funcname; | |
303 lfile_rec.filename = filename; | |
304 lfile_rec.text = text; | |
305 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
|
306 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
46 | 307 } else { |
308 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
|
309 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 310 mfile_rec.type = item_ptr->type; |
311 mfile_rec.line = item_ptr->line; | |
312 mfile_rec.funcname = funcname; | |
313 mfile_rec.filename = filename; | |
314 mfile_rec.text = text; | |
315 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
|
316 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
46 | 317 } |
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(buf, 1, ptr, debug_fp); |
46 | 319 if (buf) free(buf); buf = NULL; |
320 item_head = item_ptr->next; | |
321 free(item_ptr->function); | |
322 free(item_ptr->file); | |
323 free(item_ptr->text); | |
324 free(item_ptr); | |
325 item_ptr = item_head; | |
326 } | |
327 curr_items = 0; | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
328 index[index_ptr] = ftello(debug_fp); |
16 | 329 |
46 | 330 // 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
|
331 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
|
332 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
|
333 fseeko(debug_fp, 0, SEEK_END); |
46 | 334 item_ptr = item_head = item_tail = NULL; |
335 free(index); | |
336 if (buf) free(buf); | |
16 | 337 } |
338 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
339 |
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
|
340 void pst_debug_write_msg(struct pst_debug_item *item, const char *fmt, va_list *ap, int size) { |
46 | 341 struct pst_debug_file_rec_l lfile_rec; |
342 struct pst_debug_file_rec_m mfile_rec; | |
343 unsigned char rec_type; | |
344 int index_size = 3 * sizeof(off_t); | |
345 off_t index[3]; | |
346 off_t index_pos, file_pos; | |
347 char zero='\0'; | |
348 unsigned int end; | |
349 if (!debug_fp) return; // no file | |
350 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
|
351 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
|
352 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
|
353 |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
354 index[1] = ftello(debug_fp); |
16 | 355 |
46 | 356 if (size > USHRT_MAX) { // bigger than can be stored in a short |
357 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
|
358 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 359 lfile_rec.type = item->type; |
360 lfile_rec.line = item->line; | |
361 lfile_rec.funcname = 0; | |
362 lfile_rec.filename = strlen(item->function)+1; | |
363 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
|
364 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
46 | 365 } else { |
366 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
|
367 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 368 mfile_rec.type = item->type; |
369 mfile_rec.line = item->line; | |
370 mfile_rec.funcname = 0; | |
371 mfile_rec.filename = strlen(item->function)+1; | |
372 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
|
373 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
46 | 374 } |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
375 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
|
376 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
|
377 pst_debug_fwrite(item->file, strlen(item->file)+1, 1, debug_fp); |
46 | 378 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
|
379 pst_debug_fwrite(&zero, 1, 1, debug_fp); |
16 | 380 |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
381 end = (unsigned int) (ftello(debug_fp) - file_pos); |
16 | 382 |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
383 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
|
384 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
|
385 pst_debug_fwrite(index, index_size, 1, debug_fp); |
46 | 386 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
|
387 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 388 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
|
389 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
46 | 390 } 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
|
391 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 392 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
|
393 pst_debug_fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp); |
46 | 394 } |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
395 fseeko(debug_fp, 0, SEEK_END); |
16 | 396 } |
397 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
398 |
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
|
399 void pst_debug_write_hex(struct pst_debug_item *item, char *buf, size_t size, int col) { |
46 | 400 struct pst_debug_file_rec_l lfile_rec; |
401 unsigned char rec_type; | |
402 int index_size = 3 * sizeof(off_t); | |
403 off_t index_pos, file_pos, index[3]; | |
404 char zero='\0'; | |
405 if (!debug_fp) return; // no file | |
406 index[0] = 1; // only one item in this index run | |
407 index[1] = 0; // valgrind, avoid writing uninitialized data | |
408 index[2] = 0; // "" | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
409 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
|
410 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
|
411 index[1] = ftello(debug_fp); |
16 | 412 |
46 | 413 // always use the long |
414 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
|
415 pst_debug_fwrite(&rec_type, 1, sizeof(char), debug_fp); |
46 | 416 lfile_rec.funcname = 0; |
417 lfile_rec.filename = strlen(item->function)+1; | |
418 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; | |
419 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data | |
420 lfile_rec.line = item->line; | |
421 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
|
422 pst_debug_fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); |
16 | 423 |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
424 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
|
425 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
|
426 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
|
427 |
46 | 428 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
|
429 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
|
430 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
|
431 |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
432 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
|
433 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
|
434 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
|
435 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
|
436 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
|
437 fseeko(debug_fp, 0, SEEK_END); |
16 | 438 } |
439 | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
440 |
36 | 441 void *xmalloc(size_t size) { |
46 | 442 void *mem = malloc(size); |
443 if (!mem) { | |
444 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size); | |
445 exit(1); | |
446 } | |
447 return mem; | |
33
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
448 } |
12cac756bc05
enable -d option, but if not specified, don't generate a debug file
carl
parents:
31
diff
changeset
|
449 |