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