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