annotate debug.c @ 4:28737b9480e4

fix usage help
author carl
date Thu, 23 Dec 2004 11:20:24 -0800
parents 6b1b602514db
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6b1b602514db Initial revision
carl
parents:
diff changeset
1 /* Contains the debug functions */
6b1b602514db Initial revision
carl
parents:
diff changeset
2 #include <stdio.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
3 #include <stdlib.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
4 #include <stdarg.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
5 #include <ctype.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
6 #include <string.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
7 #include <limits.h>
6b1b602514db Initial revision
carl
parents:
diff changeset
8 #include "define.h"
6b1b602514db Initial revision
carl
parents:
diff changeset
9
6b1b602514db Initial revision
carl
parents:
diff changeset
10 #ifdef _WIN32
6b1b602514db Initial revision
carl
parents:
diff changeset
11 # define vsnprintf _vsnprintf
6b1b602514db Initial revision
carl
parents:
diff changeset
12 #endif
6b1b602514db Initial revision
carl
parents:
diff changeset
13
6b1b602514db Initial revision
carl
parents:
diff changeset
14 struct _debug_item {
6b1b602514db Initial revision
carl
parents:
diff changeset
15 int type;
6b1b602514db Initial revision
carl
parents:
diff changeset
16 char * function;
6b1b602514db Initial revision
carl
parents:
diff changeset
17 unsigned int line;
6b1b602514db Initial revision
carl
parents:
diff changeset
18 char * file;
6b1b602514db Initial revision
carl
parents:
diff changeset
19 char * text;
6b1b602514db Initial revision
carl
parents:
diff changeset
20 struct _debug_item *next;
6b1b602514db Initial revision
carl
parents:
diff changeset
21 } *item_head=NULL, *item_tail=NULL, *item_ptr=NULL, *info_ptr=NULL, *temp_list=NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
22
6b1b602514db Initial revision
carl
parents:
diff changeset
23 struct _debug_func {
6b1b602514db Initial revision
carl
parents:
diff changeset
24 char * name;
6b1b602514db Initial revision
carl
parents:
diff changeset
25 struct _debug_func *next;
6b1b602514db Initial revision
carl
parents:
diff changeset
26 } *func_head=NULL, *func_ptr=NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
27
6b1b602514db Initial revision
carl
parents:
diff changeset
28
6b1b602514db Initial revision
carl
parents:
diff changeset
29 void _debug_init(char *fname);
6b1b602514db Initial revision
carl
parents:
diff changeset
30 void _debug_msg_info (int line, char *file, int type);
6b1b602514db Initial revision
carl
parents:
diff changeset
31 void _debug_msg(char* fmt, ...);
6b1b602514db Initial revision
carl
parents:
diff changeset
32 void _debug_hexdump(char *x, int y, int cols);
6b1b602514db Initial revision
carl
parents:
diff changeset
33 void _debug_func(char *function);
6b1b602514db Initial revision
carl
parents:
diff changeset
34 void _debug_func_ret();
6b1b602514db Initial revision
carl
parents:
diff changeset
35 void _debug_close();
6b1b602514db Initial revision
carl
parents:
diff changeset
36 void _debug_write();
6b1b602514db Initial revision
carl
parents:
diff changeset
37 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size);
6b1b602514db Initial revision
carl
parents:
diff changeset
38 void _debug_write_hex(struct _debug_item *item, char *buf, int size, int col);
6b1b602514db Initial revision
carl
parents:
diff changeset
39 void * xmalloc(size_t size);
6b1b602514db Initial revision
carl
parents:
diff changeset
40
6b1b602514db Initial revision
carl
parents:
diff changeset
41 // the largest text size we will store in memory. Otherwise we
6b1b602514db Initial revision
carl
parents:
diff changeset
42 // will do a debug_write, then create a new record, and write the
6b1b602514db Initial revision
carl
parents:
diff changeset
43 // text body directly to the file
6b1b602514db Initial revision
carl
parents:
diff changeset
44 #define MAX_MESSAGE_SIZE 4096
6b1b602514db Initial revision
carl
parents:
diff changeset
45
6b1b602514db Initial revision
carl
parents:
diff changeset
46 void _pst_debug(char *fmt, ...) {
6b1b602514db Initial revision
carl
parents:
diff changeset
47 va_list ap;
6b1b602514db Initial revision
carl
parents:
diff changeset
48 va_start(ap,fmt);
6b1b602514db Initial revision
carl
parents:
diff changeset
49 vfprintf(stderr, fmt, ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
50 va_end(ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
51 }
6b1b602514db Initial revision
carl
parents:
diff changeset
52
6b1b602514db Initial revision
carl
parents:
diff changeset
53 #define NUM_COL 30
6b1b602514db Initial revision
carl
parents:
diff changeset
54 void _pst_debug_hexdump(FILE *out, unsigned char *buf, size_t size, int col) {
6b1b602514db Initial revision
carl
parents:
diff changeset
55 int off = 0, toff;
6b1b602514db Initial revision
carl
parents:
diff changeset
56 int count = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
57
6b1b602514db Initial revision
carl
parents:
diff changeset
58 if (col == -1) {
6b1b602514db Initial revision
carl
parents:
diff changeset
59 col = NUM_COL;
6b1b602514db Initial revision
carl
parents:
diff changeset
60 }
6b1b602514db Initial revision
carl
parents:
diff changeset
61 fprintf(out, "\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
62 while (off < size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
63 fprintf(out, "%X\t:", off);
6b1b602514db Initial revision
carl
parents:
diff changeset
64 toff = off;
6b1b602514db Initial revision
carl
parents:
diff changeset
65 while (count < col && off < size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
66 fprintf(out, "%02hhx ", buf[off]);
6b1b602514db Initial revision
carl
parents:
diff changeset
67 off++; count++;
6b1b602514db Initial revision
carl
parents:
diff changeset
68 }
6b1b602514db Initial revision
carl
parents:
diff changeset
69 off = toff;
6b1b602514db Initial revision
carl
parents:
diff changeset
70 while (count < col) {
6b1b602514db Initial revision
carl
parents:
diff changeset
71 // only happens at end of block to pad the text over to the text column
6b1b602514db Initial revision
carl
parents:
diff changeset
72 fprintf(out, " ");
6b1b602514db Initial revision
carl
parents:
diff changeset
73 count++;
6b1b602514db Initial revision
carl
parents:
diff changeset
74 }
6b1b602514db Initial revision
carl
parents:
diff changeset
75 count = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
76 fprintf(out, ":");
6b1b602514db Initial revision
carl
parents:
diff changeset
77 while (count < col && off < size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
78 fprintf(out, "%c", isgraph(buf[off])?buf[off]:'.');
6b1b602514db Initial revision
carl
parents:
diff changeset
79 off++; count ++;
6b1b602514db Initial revision
carl
parents:
diff changeset
80 }
6b1b602514db Initial revision
carl
parents:
diff changeset
81
6b1b602514db Initial revision
carl
parents:
diff changeset
82 fprintf(out, "\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
83 count=0;
6b1b602514db Initial revision
carl
parents:
diff changeset
84 }
6b1b602514db Initial revision
carl
parents:
diff changeset
85
6b1b602514db Initial revision
carl
parents:
diff changeset
86 fprintf(out, "\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
87 }
6b1b602514db Initial revision
carl
parents:
diff changeset
88
6b1b602514db Initial revision
carl
parents:
diff changeset
89 void _pst_debug_hexprint(char *data, int size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
90 int i = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
91 while (i < size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
92 fprintf(stderr, "%02hhX", data[i]);
6b1b602514db Initial revision
carl
parents:
diff changeset
93 i++;
6b1b602514db Initial revision
carl
parents:
diff changeset
94 }
6b1b602514db Initial revision
carl
parents:
diff changeset
95 }
6b1b602514db Initial revision
carl
parents:
diff changeset
96
6b1b602514db Initial revision
carl
parents:
diff changeset
97 FILE *debug_fp = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
98 unsigned int max_items=DEBUG_MAX_ITEMS, curr_items=0;
6b1b602514db Initial revision
carl
parents:
diff changeset
99
6b1b602514db Initial revision
carl
parents:
diff changeset
100 void _debug_init(char* fname) {
6b1b602514db Initial revision
carl
parents:
diff changeset
101 unsigned char version = DEBUG_VERSION;
6b1b602514db Initial revision
carl
parents:
diff changeset
102 item_head = item_tail = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
103 curr_items = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
104 if (debug_fp != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
105 _debug_close();
6b1b602514db Initial revision
carl
parents:
diff changeset
106 if ((debug_fp = fopen(fname, "wb")) == NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
107 fprintf(stderr, "Opening of file %s failed\n", fname);
6b1b602514db Initial revision
carl
parents:
diff changeset
108 exit(1);
6b1b602514db Initial revision
carl
parents:
diff changeset
109 }
6b1b602514db Initial revision
carl
parents:
diff changeset
110 fwrite(&version, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
111 }
6b1b602514db Initial revision
carl
parents:
diff changeset
112
6b1b602514db Initial revision
carl
parents:
diff changeset
113 // function must be called before _debug_msg. It sets up the
6b1b602514db Initial revision
carl
parents:
diff changeset
114 // structure for the function that follows
6b1b602514db Initial revision
carl
parents:
diff changeset
115 void _debug_msg_info(int line, char* file, int type) {
6b1b602514db Initial revision
carl
parents:
diff changeset
116 char *x;
6b1b602514db Initial revision
carl
parents:
diff changeset
117 if (debug_fp == NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
118 fprintf(stderr, "debug_fp is NULL\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
119 return;
6b1b602514db Initial revision
carl
parents:
diff changeset
120 }
6b1b602514db Initial revision
carl
parents:
diff changeset
121 info_ptr = (struct _debug_item*) xmalloc(sizeof(struct _debug_item));
6b1b602514db Initial revision
carl
parents:
diff changeset
122 info_ptr->type = type;
6b1b602514db Initial revision
carl
parents:
diff changeset
123 info_ptr->line = line;
6b1b602514db Initial revision
carl
parents:
diff changeset
124 x = (func_head==NULL?"No Function":func_head->name);
6b1b602514db Initial revision
carl
parents:
diff changeset
125 info_ptr->function = (char*) xmalloc(strlen(x)+1);
6b1b602514db Initial revision
carl
parents:
diff changeset
126 strcpy(info_ptr->function, x);
6b1b602514db Initial revision
carl
parents:
diff changeset
127
6b1b602514db Initial revision
carl
parents:
diff changeset
128 info_ptr->file = (char*) xmalloc(strlen(file)+1);
6b1b602514db Initial revision
carl
parents:
diff changeset
129 strcpy(info_ptr->file, file);
6b1b602514db Initial revision
carl
parents:
diff changeset
130
6b1b602514db Initial revision
carl
parents:
diff changeset
131 //put the current record on a temp linked list
6b1b602514db Initial revision
carl
parents:
diff changeset
132 info_ptr->next = temp_list;
6b1b602514db Initial revision
carl
parents:
diff changeset
133 temp_list = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
134 }
6b1b602514db Initial revision
carl
parents:
diff changeset
135
6b1b602514db Initial revision
carl
parents:
diff changeset
136 void _debug_msg_text(char* fmt, ...) {
6b1b602514db Initial revision
carl
parents:
diff changeset
137 va_list ap;
6b1b602514db Initial revision
carl
parents:
diff changeset
138 int f, g;
6b1b602514db Initial revision
carl
parents:
diff changeset
139 char x[2];
6b1b602514db Initial revision
carl
parents:
diff changeset
140 struct _debug_item *temp;
6b1b602514db Initial revision
carl
parents:
diff changeset
141 if (debug_fp == NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
142 return;
6b1b602514db Initial revision
carl
parents:
diff changeset
143 va_start(ap, fmt);
6b1b602514db Initial revision
carl
parents:
diff changeset
144 // get the record off of the temp_list
6b1b602514db Initial revision
carl
parents:
diff changeset
145 info_ptr = temp_list;
6b1b602514db Initial revision
carl
parents:
diff changeset
146 if (info_ptr != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
147 temp_list = info_ptr->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
148 else {
6b1b602514db Initial revision
carl
parents:
diff changeset
149 fprintf(stderr, "NULL info_ptr. ERROR!!\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
150 exit(-2);
6b1b602514db Initial revision
carl
parents:
diff changeset
151 }
6b1b602514db Initial revision
carl
parents:
diff changeset
152 // according to glibc 2.1, this should return the req. number of bytes for
6b1b602514db Initial revision
carl
parents:
diff changeset
153 // the string
6b1b602514db Initial revision
carl
parents:
diff changeset
154 #ifdef _WIN32
6b1b602514db Initial revision
carl
parents:
diff changeset
155 // vsnprintf trick doesn't work. must use function called _vscprintf
6b1b602514db Initial revision
carl
parents:
diff changeset
156 // cannot find much documentation about this on internet or anywhere.
6b1b602514db Initial revision
carl
parents:
diff changeset
157 // I assume it isn't a standard function, but only in VisualC++
6b1b602514db Initial revision
carl
parents:
diff changeset
158 f = _vscprintf(fmt, ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
159 #else
6b1b602514db Initial revision
carl
parents:
diff changeset
160 f = vsnprintf(x, 1, fmt, ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
161 #endif
6b1b602514db Initial revision
carl
parents:
diff changeset
162
6b1b602514db Initial revision
carl
parents:
diff changeset
163 if (f > 0 && f < MAX_MESSAGE_SIZE) {
6b1b602514db Initial revision
carl
parents:
diff changeset
164 info_ptr->text = (char*) xmalloc(f+1);
6b1b602514db Initial revision
carl
parents:
diff changeset
165 if ((g = vsnprintf(info_ptr->text, f, fmt, ap)) == -1) {
6b1b602514db Initial revision
carl
parents:
diff changeset
166 fprintf(stderr, "_debug_msg: Dieing! vsnprintf returned -1 for format \"%s\"\n", fmt);
6b1b602514db Initial revision
carl
parents:
diff changeset
167 exit(-2);
6b1b602514db Initial revision
carl
parents:
diff changeset
168 }
6b1b602514db Initial revision
carl
parents:
diff changeset
169 info_ptr->text[g] = '\0';
6b1b602514db Initial revision
carl
parents:
diff changeset
170 if (f != g) {
6b1b602514db Initial revision
carl
parents:
diff changeset
171 fprintf(stderr, "_debug_msg: f != g\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
172 }
6b1b602514db Initial revision
carl
parents:
diff changeset
173 } else if (f > 0) { // it is over the max_message_size then
6b1b602514db Initial revision
carl
parents:
diff changeset
174 f += strlen(info_ptr->file)+strlen(info_ptr->function);
6b1b602514db Initial revision
carl
parents:
diff changeset
175 temp = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
176 _debug_write(); // dump the current messages
6b1b602514db Initial revision
carl
parents:
diff changeset
177 info_ptr = temp;
6b1b602514db Initial revision
carl
parents:
diff changeset
178 _debug_write_msg(info_ptr, fmt, &ap, f);
6b1b602514db Initial revision
carl
parents:
diff changeset
179 free(info_ptr->function);
6b1b602514db Initial revision
carl
parents:
diff changeset
180 free(info_ptr->file);
6b1b602514db Initial revision
carl
parents:
diff changeset
181 free(info_ptr);
6b1b602514db Initial revision
carl
parents:
diff changeset
182 info_ptr = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
183 return;
6b1b602514db Initial revision
carl
parents:
diff changeset
184 } else {
6b1b602514db Initial revision
carl
parents:
diff changeset
185 fprintf(stderr, "_debug_msg: error getting requested size of debug message\n");
6b1b602514db Initial revision
carl
parents:
diff changeset
186 info_ptr->text = "ERROR Saving\n";
6b1b602514db Initial revision
carl
parents:
diff changeset
187 }
6b1b602514db Initial revision
carl
parents:
diff changeset
188 va_end(ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
189
6b1b602514db Initial revision
carl
parents:
diff changeset
190 if (item_head == NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
191 item_head = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
192
6b1b602514db Initial revision
carl
parents:
diff changeset
193 info_ptr->next = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
194 if (item_tail != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
195 item_tail->next = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
196 item_tail = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
197
6b1b602514db Initial revision
carl
parents:
diff changeset
198 if (++curr_items == max_items) {
6b1b602514db Initial revision
carl
parents:
diff changeset
199 // here we will jump off and save the contents
6b1b602514db Initial revision
carl
parents:
diff changeset
200 _debug_write();
6b1b602514db Initial revision
carl
parents:
diff changeset
201 info_ptr = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
202 }
6b1b602514db Initial revision
carl
parents:
diff changeset
203 }
6b1b602514db Initial revision
carl
parents:
diff changeset
204
6b1b602514db Initial revision
carl
parents:
diff changeset
205 void _debug_hexdump(char *x, int y, int cols) {
6b1b602514db Initial revision
carl
parents:
diff changeset
206 struct _debug_item *temp;
6b1b602514db Initial revision
carl
parents:
diff changeset
207 if (debug_fp == NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
208 return;
6b1b602514db Initial revision
carl
parents:
diff changeset
209 info_ptr = temp_list;
6b1b602514db Initial revision
carl
parents:
diff changeset
210 if (info_ptr != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
211 temp_list = info_ptr->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
212 temp = info_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
213 _debug_write();
6b1b602514db Initial revision
carl
parents:
diff changeset
214 info_ptr = temp;
6b1b602514db Initial revision
carl
parents:
diff changeset
215 _debug_write_hex(info_ptr, x, y, cols);
6b1b602514db Initial revision
carl
parents:
diff changeset
216 free(info_ptr->function);
6b1b602514db Initial revision
carl
parents:
diff changeset
217 free(info_ptr->file);
6b1b602514db Initial revision
carl
parents:
diff changeset
218 free(info_ptr);
6b1b602514db Initial revision
carl
parents:
diff changeset
219 info_ptr = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
220 }
6b1b602514db Initial revision
carl
parents:
diff changeset
221
6b1b602514db Initial revision
carl
parents:
diff changeset
222 void _debug_func(char *function) {
6b1b602514db Initial revision
carl
parents:
diff changeset
223 func_ptr = xmalloc (sizeof(struct _debug_func));
6b1b602514db Initial revision
carl
parents:
diff changeset
224 func_ptr->name = xmalloc(strlen(function)+1);
6b1b602514db Initial revision
carl
parents:
diff changeset
225 strcpy(func_ptr->name, function);
6b1b602514db Initial revision
carl
parents:
diff changeset
226 func_ptr->next = func_head;
6b1b602514db Initial revision
carl
parents:
diff changeset
227 func_head = func_ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
228 }
6b1b602514db Initial revision
carl
parents:
diff changeset
229
6b1b602514db Initial revision
carl
parents:
diff changeset
230 void _debug_func_ret() {
6b1b602514db Initial revision
carl
parents:
diff changeset
231 //remove the head item
6b1b602514db Initial revision
carl
parents:
diff changeset
232 func_ptr = func_head;
6b1b602514db Initial revision
carl
parents:
diff changeset
233 if (func_head != NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
234 func_head = func_head->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
235 free(func_ptr->name);
6b1b602514db Initial revision
carl
parents:
diff changeset
236 free(func_ptr);
6b1b602514db Initial revision
carl
parents:
diff changeset
237 } else {
6b1b602514db Initial revision
carl
parents:
diff changeset
238 DIE(("function list is empty!\n"));
6b1b602514db Initial revision
carl
parents:
diff changeset
239 }
6b1b602514db Initial revision
carl
parents:
diff changeset
240 }
6b1b602514db Initial revision
carl
parents:
diff changeset
241
6b1b602514db Initial revision
carl
parents:
diff changeset
242 void _debug_close(void) {
6b1b602514db Initial revision
carl
parents:
diff changeset
243 _debug_write();
6b1b602514db Initial revision
carl
parents:
diff changeset
244 while (func_head != NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
245 func_ptr = func_head;
6b1b602514db Initial revision
carl
parents:
diff changeset
246 func_head = func_head->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
247 free(func_ptr->name);
6b1b602514db Initial revision
carl
parents:
diff changeset
248 free(func_ptr);
6b1b602514db Initial revision
carl
parents:
diff changeset
249 }
6b1b602514db Initial revision
carl
parents:
diff changeset
250
6b1b602514db Initial revision
carl
parents:
diff changeset
251 if (debug_fp != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
252 fclose(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
253 debug_fp = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
254
6b1b602514db Initial revision
carl
parents:
diff changeset
255 if (func_head != NULL)
6b1b602514db Initial revision
carl
parents:
diff changeset
256 while (func_head != NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
257 printf("function '%s' still on stack\n", func_head->name);
6b1b602514db Initial revision
carl
parents:
diff changeset
258 func_head = func_head->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
259 }
6b1b602514db Initial revision
carl
parents:
diff changeset
260 }
6b1b602514db Initial revision
carl
parents:
diff changeset
261
6b1b602514db Initial revision
carl
parents:
diff changeset
262 void _debug_write() {
6b1b602514db Initial revision
carl
parents:
diff changeset
263 size_t size, ptr, funcname, filename, text, end;
6b1b602514db Initial revision
carl
parents:
diff changeset
264 char *buf, rec_type;
6b1b602514db Initial revision
carl
parents:
diff changeset
265 long index_pos = ftell (debug_fp), file_pos = index_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
266 // add 2. One for the pointer to the next index,
6b1b602514db Initial revision
carl
parents:
diff changeset
267 // one for the count of this index
6b1b602514db Initial revision
carl
parents:
diff changeset
268 int index_size = ((curr_items+2) * sizeof(int));
6b1b602514db Initial revision
carl
parents:
diff changeset
269 int *index;
6b1b602514db Initial revision
carl
parents:
diff changeset
270 int index_ptr = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
271 struct _debug_file_rec_m mfile_rec;
6b1b602514db Initial revision
carl
parents:
diff changeset
272 struct _debug_file_rec_l lfile_rec;
6b1b602514db Initial revision
carl
parents:
diff changeset
273
6b1b602514db Initial revision
carl
parents:
diff changeset
274 if (curr_items == 0)
6b1b602514db Initial revision
carl
parents:
diff changeset
275 // no items to write.
6b1b602514db Initial revision
carl
parents:
diff changeset
276 return;
6b1b602514db Initial revision
carl
parents:
diff changeset
277 index = (int*) xmalloc(index_size);
6b1b602514db Initial revision
carl
parents:
diff changeset
278 file_pos += index_size;
6b1b602514db Initial revision
carl
parents:
diff changeset
279 // write the index first, we will re-write it later, but
6b1b602514db Initial revision
carl
parents:
diff changeset
280 // we want to allocate the space
6b1b602514db Initial revision
carl
parents:
diff changeset
281 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
282 index[index_ptr++] = curr_items;
6b1b602514db Initial revision
carl
parents:
diff changeset
283
6b1b602514db Initial revision
carl
parents:
diff changeset
284 item_ptr = item_head;
6b1b602514db Initial revision
carl
parents:
diff changeset
285 while (item_ptr != NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
286 file_pos = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
287 index[index_ptr++] = file_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
288 size = strlen(item_ptr->function)+strlen(item_ptr->file)+
6b1b602514db Initial revision
carl
parents:
diff changeset
289 strlen(item_ptr->text) + 3; //for the three \0s
6b1b602514db Initial revision
carl
parents:
diff changeset
290 buf = xmalloc(size+1);
6b1b602514db Initial revision
carl
parents:
diff changeset
291 ptr = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
292 funcname=ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
293 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
294 filename=ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
295 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->file)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
296 text=ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
297 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->text)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
298 end=ptr;
6b1b602514db Initial revision
carl
parents:
diff changeset
299 if (end > USHRT_MAX) { // bigger than can be stored in a short
6b1b602514db Initial revision
carl
parents:
diff changeset
300 rec_type = 'L';
6b1b602514db Initial revision
carl
parents:
diff changeset
301 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
302 lfile_rec.type = item_ptr->type;
6b1b602514db Initial revision
carl
parents:
diff changeset
303 lfile_rec.line = item_ptr->line;
6b1b602514db Initial revision
carl
parents:
diff changeset
304 lfile_rec.funcname = funcname;
6b1b602514db Initial revision
carl
parents:
diff changeset
305 lfile_rec.filename = filename;
6b1b602514db Initial revision
carl
parents:
diff changeset
306 lfile_rec.text = text;
6b1b602514db Initial revision
carl
parents:
diff changeset
307 lfile_rec.end = end;
6b1b602514db Initial revision
carl
parents:
diff changeset
308 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
309 } else {
6b1b602514db Initial revision
carl
parents:
diff changeset
310 rec_type = 'M';
6b1b602514db Initial revision
carl
parents:
diff changeset
311 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
312 mfile_rec.type = item_ptr->type;
6b1b602514db Initial revision
carl
parents:
diff changeset
313 mfile_rec.line = item_ptr->line;
6b1b602514db Initial revision
carl
parents:
diff changeset
314 mfile_rec.funcname = funcname;
6b1b602514db Initial revision
carl
parents:
diff changeset
315 mfile_rec.filename = filename;
6b1b602514db Initial revision
carl
parents:
diff changeset
316 mfile_rec.text = text;
6b1b602514db Initial revision
carl
parents:
diff changeset
317 mfile_rec.end = end;
6b1b602514db Initial revision
carl
parents:
diff changeset
318 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
319 }
6b1b602514db Initial revision
carl
parents:
diff changeset
320 fwrite(buf, 1, ptr, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
321 item_head = item_ptr->next;
6b1b602514db Initial revision
carl
parents:
diff changeset
322 free(item_ptr->function);
6b1b602514db Initial revision
carl
parents:
diff changeset
323 free(item_ptr->file);
6b1b602514db Initial revision
carl
parents:
diff changeset
324 free(item_ptr->text);
6b1b602514db Initial revision
carl
parents:
diff changeset
325 free(item_ptr);
6b1b602514db Initial revision
carl
parents:
diff changeset
326 item_ptr = item_head;
6b1b602514db Initial revision
carl
parents:
diff changeset
327 }
6b1b602514db Initial revision
carl
parents:
diff changeset
328 curr_items = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
329 index[index_ptr] = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
330
6b1b602514db Initial revision
carl
parents:
diff changeset
331 // we should now have a complete index
6b1b602514db Initial revision
carl
parents:
diff changeset
332 fseek(debug_fp, index_pos, SEEK_SET);
6b1b602514db Initial revision
carl
parents:
diff changeset
333 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
334 fseek(debug_fp, 0, SEEK_END);
6b1b602514db Initial revision
carl
parents:
diff changeset
335 item_ptr = item_head = item_tail = NULL;
6b1b602514db Initial revision
carl
parents:
diff changeset
336 free(index);
6b1b602514db Initial revision
carl
parents:
diff changeset
337 }
6b1b602514db Initial revision
carl
parents:
diff changeset
338
6b1b602514db Initial revision
carl
parents:
diff changeset
339 void _debug_write_msg(struct _debug_item *item, char *fmt, va_list *ap, int size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
340 struct _debug_file_rec_l lfile_rec;
6b1b602514db Initial revision
carl
parents:
diff changeset
341 struct _debug_file_rec_m mfile_rec;
6b1b602514db Initial revision
carl
parents:
diff changeset
342 unsigned char rec_type;
6b1b602514db Initial revision
carl
parents:
diff changeset
343 int index_size = 3 * sizeof(int);
6b1b602514db Initial revision
carl
parents:
diff changeset
344 int *index = malloc(index_size);
6b1b602514db Initial revision
carl
parents:
diff changeset
345 int index_pos, file_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
346 char zero='\0';
6b1b602514db Initial revision
carl
parents:
diff changeset
347 unsigned int end;
6b1b602514db Initial revision
carl
parents:
diff changeset
348 index[0] = 1; //only one item in this index
6b1b602514db Initial revision
carl
parents:
diff changeset
349 index_pos = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
350 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
351
6b1b602514db Initial revision
carl
parents:
diff changeset
352 index[1] = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
353
6b1b602514db Initial revision
carl
parents:
diff changeset
354 if (size > USHRT_MAX) { // bigger than can be stored in a short
6b1b602514db Initial revision
carl
parents:
diff changeset
355 rec_type = 'L';
6b1b602514db Initial revision
carl
parents:
diff changeset
356 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
357 lfile_rec.type = item->type;
6b1b602514db Initial revision
carl
parents:
diff changeset
358 lfile_rec.line = item->line;
6b1b602514db Initial revision
carl
parents:
diff changeset
359 lfile_rec.funcname = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
360 lfile_rec.filename = strlen(item->function)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
361 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
362 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
363 } else {
6b1b602514db Initial revision
carl
parents:
diff changeset
364 rec_type = 'M';
6b1b602514db Initial revision
carl
parents:
diff changeset
365 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
366 mfile_rec.type = item->type;
6b1b602514db Initial revision
carl
parents:
diff changeset
367 mfile_rec.line = item->line;
6b1b602514db Initial revision
carl
parents:
diff changeset
368 mfile_rec.funcname = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
369 mfile_rec.filename = strlen(item->function)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
370 mfile_rec.text = mfile_rec.filename+strlen(item->file)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
371 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
372 }
6b1b602514db Initial revision
carl
parents:
diff changeset
373 file_pos = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
374 fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
375 fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
376 vfprintf(debug_fp, fmt, *ap);
6b1b602514db Initial revision
carl
parents:
diff changeset
377 fwrite(&zero, 1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
378
6b1b602514db Initial revision
carl
parents:
diff changeset
379 end = ftell(debug_fp)-file_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
380
6b1b602514db Initial revision
carl
parents:
diff changeset
381 index[2] = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
382 fseek(debug_fp, index_pos, SEEK_SET);
6b1b602514db Initial revision
carl
parents:
diff changeset
383 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
384 if (size > USHRT_MAX) {
6b1b602514db Initial revision
carl
parents:
diff changeset
385 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
386 lfile_rec.end = end;
6b1b602514db Initial revision
carl
parents:
diff changeset
387 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
388 } else {
6b1b602514db Initial revision
carl
parents:
diff changeset
389 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
390 mfile_rec.end = end;
6b1b602514db Initial revision
carl
parents:
diff changeset
391 fwrite(&mfile_rec, sizeof(mfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
392 }
6b1b602514db Initial revision
carl
parents:
diff changeset
393 fseek(debug_fp, 0, SEEK_END);
6b1b602514db Initial revision
carl
parents:
diff changeset
394 // that should do it...
6b1b602514db Initial revision
carl
parents:
diff changeset
395 }
6b1b602514db Initial revision
carl
parents:
diff changeset
396
6b1b602514db Initial revision
carl
parents:
diff changeset
397 void _debug_write_hex(struct _debug_item *item, char *buf, int size, int col) {
6b1b602514db Initial revision
carl
parents:
diff changeset
398 struct _debug_file_rec_l lfile_rec;
6b1b602514db Initial revision
carl
parents:
diff changeset
399 unsigned char rec_type;
6b1b602514db Initial revision
carl
parents:
diff changeset
400 int index_size = 3 * sizeof(int);
6b1b602514db Initial revision
carl
parents:
diff changeset
401 int *index = malloc(index_size);
6b1b602514db Initial revision
carl
parents:
diff changeset
402 int index_pos, file_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
403 char zero='\0';
6b1b602514db Initial revision
carl
parents:
diff changeset
404 index[0] = 1; // only one item in this index run
6b1b602514db Initial revision
carl
parents:
diff changeset
405 index_pos = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
406 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
407 index[1] = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
408
6b1b602514db Initial revision
carl
parents:
diff changeset
409 // always use the long
6b1b602514db Initial revision
carl
parents:
diff changeset
410 rec_type = 'L';
6b1b602514db Initial revision
carl
parents:
diff changeset
411 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
412 lfile_rec.type = item->type;
6b1b602514db Initial revision
carl
parents:
diff changeset
413 lfile_rec.line = item->line;
6b1b602514db Initial revision
carl
parents:
diff changeset
414 lfile_rec.funcname = 0;
6b1b602514db Initial revision
carl
parents:
diff changeset
415 lfile_rec.filename = strlen(item->function)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
416 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
6b1b602514db Initial revision
carl
parents:
diff changeset
417 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
418
6b1b602514db Initial revision
carl
parents:
diff changeset
419 file_pos = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
420 fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
421 fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
422
6b1b602514db Initial revision
carl
parents:
diff changeset
423 _pst_debug_hexdump(debug_fp, buf, size, col);
6b1b602514db Initial revision
carl
parents:
diff changeset
424 fwrite(&zero, 1, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
425 lfile_rec.end = ftell(debug_fp)-file_pos;
6b1b602514db Initial revision
carl
parents:
diff changeset
426
6b1b602514db Initial revision
carl
parents:
diff changeset
427 index[2] = ftell(debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
428 fseek(debug_fp, index_pos, SEEK_SET);
6b1b602514db Initial revision
carl
parents:
diff changeset
429 fwrite(index, index_size, 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
430 fwrite(&rec_type, 1, sizeof(char), debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
431 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
6b1b602514db Initial revision
carl
parents:
diff changeset
432 fseek(debug_fp, 0, SEEK_END);
6b1b602514db Initial revision
carl
parents:
diff changeset
433 }
6b1b602514db Initial revision
carl
parents:
diff changeset
434
6b1b602514db Initial revision
carl
parents:
diff changeset
435 void * xmalloc(size_t size) {
6b1b602514db Initial revision
carl
parents:
diff changeset
436 void *mem = malloc(size);
6b1b602514db Initial revision
carl
parents:
diff changeset
437 if (mem == NULL) {
6b1b602514db Initial revision
carl
parents:
diff changeset
438 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size);
6b1b602514db Initial revision
carl
parents:
diff changeset
439 exit(1);
6b1b602514db Initial revision
carl
parents:
diff changeset
440 }
6b1b602514db Initial revision
carl
parents:
diff changeset
441 return mem;
6b1b602514db Initial revision
carl
parents:
diff changeset
442 }
6b1b602514db Initial revision
carl
parents:
diff changeset
443