annotate src/debug.c @ 33:12cac756bc05 stable-0-5-5

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