trace.c revision 1.1 1 /* $NetBSD: trace.c,v 1.1 2001/11/14 06:16:10 tv Exp $ */
2 /* $OpenBSD: trace.c,v 1.3 2001/09/29 15:47:18 espie Exp $ */
3
4 /*
5 * Copyright (c) 2001 Marc Espie.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
20 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/types.h>
30 #include <err.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "mdef.h"
35 #include "stdd.h"
36 #include "extern.h"
37
38 FILE *traceout = stderr;
39
40 int traced_macros = 0;
41
42 #define TRACE_ARGS 1
43 #define TRACE_EXPANSION 2
44 #define TRACE_QUOTE 4
45 #define TRACE_FILENAME 8
46 #define TRACE_LINENO 16
47 #define TRACE_CONT 32
48 #define TRACE_ID 64
49 #define TRACE_NEWFILE 128 /* not implemented yet */
50 #define TRACE_INPUT 256 /* not implemented yet */
51 #define TRACE_ALL 512
52
53 static struct t {
54 struct t *next;
55 char *name;
56 int on;
57 } *l;
58
59 static unsigned int letter_to_flag __P((int));
60 static void print_header __P((struct input_file *));
61 static struct t *find_trace_entry __P((const char *));
62 static int frame_level __P((void));
63
64 static unsigned int flags = TRACE_QUOTE | TRACE_EXPANSION;
65
66 static struct t *
67 find_trace_entry(name)
68 const char *name;
69 {
70 struct t *n;
71
72 for (n = l; n != NULL; n = n->next)
73 if (STREQ(n->name, name))
74 return n;
75 return NULL;
76 }
77
78
79 void
80 mark_traced(name, on)
81 const char *name;
82 int on;
83 {
84 struct t *n, *n2;
85
86 traced_macros = 1;
87
88 if (name == NULL) {
89 if (on)
90 flags |= TRACE_ALL;
91 else {
92 flags &= ~TRACE_ALL;
93 traced_macros = 0;
94 }
95 for (n = l; n != NULL; n = n2) {
96 n2 = n->next;
97 free(n->name);
98 free(n);
99 }
100 l = NULL;
101 } else {
102 n = find_trace_entry(name);
103 if (n == NULL) {
104 n = xalloc(sizeof(struct t));
105 n->name = xstrdup(name);
106 n->next = l;
107 l = n;
108 }
109 n->on = on;
110 }
111 }
112
113 int
114 is_traced(name)
115 const char *name;
116 {
117 struct t *n;
118
119 for (n = l; n != NULL; n = n->next)
120 if (STREQ(n->name, name))
121 return n->on;
122 return (flags & TRACE_ALL) ? 1 : 0;
123 }
124
125 void
126 trace_file(name)
127 const char *name;
128 {
129
130 if (traceout != stderr)
131 fclose(traceout);
132 traceout = fopen(name, "w");
133 if (!traceout)
134 err(1, "can't open %s", name);
135 }
136
137 static unsigned int
138 letter_to_flag(c)
139 int c;
140 {
141 switch(c) {
142 case 'a':
143 return TRACE_ARGS;
144 case 'e':
145 return TRACE_EXPANSION;
146 case 'q':
147 return TRACE_QUOTE;
148 case 'c':
149 return TRACE_CONT;
150 case 'x':
151 return TRACE_ID;
152 case 'f':
153 return TRACE_FILENAME;
154 case 'l':
155 return TRACE_LINENO;
156 case 'p':
157 return TRACE_NEWFILE;
158 case 'i':
159 return TRACE_INPUT;
160 case 't':
161 return TRACE_ALL;
162 case 'V':
163 return ~0;
164 default:
165 return 0;
166 }
167 }
168
169 void
170 set_trace_flags(s)
171 const char *s;
172 {
173 char mode = 0;
174 unsigned int f = 0;
175
176 traced_macros = 1;
177
178 if (*s == '+' || *s == '-')
179 mode = *s++;
180 while (*s)
181 f |= letter_to_flag(*s++);
182 switch(mode) {
183 case 0:
184 flags = f;
185 break;
186 case '+':
187 flags |= f;
188 break;
189 case '-':
190 flags &= ~f;
191 break;
192 }
193 }
194
195 static int
196 frame_level()
197 {
198 int level;
199 int framep;
200
201 for (framep = fp, level = 0; framep != 0;
202 level++,framep = mstack[framep-2].sfra)
203 ;
204 return level;
205 }
206
207 static void
208 print_header(inp)
209 struct input_file *inp;
210 {
211 fprintf(traceout, "m4trace:");
212 if (flags & TRACE_FILENAME)
213 fprintf(traceout, "%s:", inp->name);
214 if (flags & TRACE_LINENO)
215 fprintf(traceout, "%lu:", inp->lineno);
216 fprintf(traceout, " -%d- ", frame_level());
217 if (flags & TRACE_ID)
218 fprintf(traceout, "id %lu: ", expansion_id);
219 }
220
221 ssize_t
222 trace(argv, argc, inp)
223 const char **argv;
224 int argc;
225 struct input_file *inp;
226 {
227 print_header(inp);
228 if (flags & TRACE_CONT) {
229 fprintf(traceout, "%s ...\n", argv[1]);
230 print_header(inp);
231 }
232 fprintf(traceout, "%s", argv[1]);
233 if ((flags & TRACE_ARGS) && argc > 2) {
234 char delim[3];
235 int i;
236
237 delim[0] = LPAREN;
238 delim[1] = EOS;
239 for (i = 2; i < argc; i++) {
240 fprintf(traceout, "%s%s%s%s", delim,
241 (flags & TRACE_QUOTE) ? lquote : "",
242 argv[i],
243 (flags & TRACE_QUOTE) ? rquote : "");
244 delim[0] = COMMA;
245 delim[1] = ' ';
246 delim[2] = EOS;
247 }
248 fprintf(traceout, "%c", RPAREN);
249 }
250 if (flags & TRACE_CONT) {
251 fprintf(traceout, " -> ???\n");
252 print_header(inp);
253 fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]);
254 }
255 if (flags & TRACE_EXPANSION)
256 return buffer_mark();
257 else {
258 fprintf(traceout, "\n");
259 return -1;
260 }
261 }
262
263 void
264 finish_trace(mark)
265 size_t mark;
266 {
267 fprintf(traceout, " -> ");
268 if (flags & TRACE_QUOTE)
269 fprintf(traceout, "%s", lquote);
270 dump_buffer(traceout, mark);
271 if (flags & TRACE_QUOTE)
272 fprintf(traceout, "%s", rquote);
273 fprintf(traceout, "\n");
274 }
275