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