-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace.c
286 lines (268 loc) · 4.86 KB
/
trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/********************************************
trace.c
copyright 2012-2016,2019 Thomas E. Dickey
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: trace.c,v 1.16 2019/02/02 02:02:33 tom Exp $
*/
#include <mawk.h>
#include <repl.h>
#include <code.h>
static FILE *trace_fp;
void
Trace(const char *format, ...)
{
va_list args;
if (trace_fp == 0)
trace_fp = fopen("Trace.out", "w");
if (trace_fp == 0)
rt_error("cannot open Trace.out");
va_start(args, format);
vfprintf(trace_fp, format, args);
va_end(args);
fflush(trace_fp);
}
void
TraceVA(const char *format, va_list args)
{
vfprintf(trace_fp, format, args);
}
void
TraceCell(CELL *cp)
{
TRACE(("cell %p ", (void *) cp));
if (cp != 0) {
switch ((MAWK_CELL_TYPES) cp->type) {
case C_NOINIT:
TRACE(("is empty\n"));
break;
case C_DOUBLE:
TRACE(("double %g\n", cp->dval));
break;
case C_MBSTRN:
case C_STRNUM:
case C_STRING:
TRACE(("string "));
TraceString(string(cp));
TRACE((" (%d)\n", string(cp)->ref_cnt));
break;
case C_SPACE:
TRACE(("split on space\n"));
break;
case C_SNULL:
TRACE(("split on the empty string\n"));
break;
case C_RE:
TRACE(("a regular expression at %p: %s\n", cp->ptr, re_uncompile(cp->ptr)));
break;
case C_REPL:
TRACE(("a replacement string at %p: ", cp->ptr));
TraceString(string(cp));
TRACE(("\n"));
break;
case C_REPLV:
TRACE(("a vector replacement, count %d at %p\n", cp->vcnt, cp->ptr));
break;
case NUM_CELL_TYPES:
/* FALLTHRU */
default:
TRACE(("unknown type %d\n", cp->type));
break;
}
} else {
TRACE(("no cell\n"));
}
}
void
TraceFunc(const char *name, CELL *sp)
{
int nargs = sp->type;
int n;
TRACE(("** %s <-%p\n", name, (void *) sp));
for (n = 0; n < nargs; ++n) {
TRACE(("...arg%d: ", n));
TraceCell(sp + n - nargs);
}
}
void
TraceInst(INST * p, INST * base)
{
INST *q = da_this(p, base, trace_fp);
TRACE((" ...%ld\n", (long) (q - p)));
if (p++ != q) {
switch ((MAWK_OPCODES) (base->op)) {
case AE_PUSHA:
case AE_PUSHI:
case A_PUSHA:
TRACE(("\tST_ARRAY *%p\n", p->ptr));
break;
case F_PUSHA:
TRACE(("\tST_FIELD *%p\n", p->ptr));
break;
case _BUILTIN:
case _PRINT:
TRACE(("\tPF_CP *%p\n", p->ptr));
break;
case _CALL:
TRACE(("\tFBLOCK *%p\n", p->ptr));
break;
case _MATCH0:
case _MATCH1:
TRACE(("\tregex *%p\n", p->ptr));
break;
case _PUSHA:
TRACE(("\tST_VAR *%p\n", p->ptr));
break;
case _PUSHC:
case _PUSHI:
TRACE(("\tCELL *%p\n", p->ptr));
break;
case _PUSHD:
TRACE(("\tdouble *%p\n", p->ptr));
break;
case _PUSHS:
TRACE(("\tSTRING *%p\n", p->ptr));
break;
case ALOOP:
case A_CAT:
case A_DEL:
case A_LENGTH:
case A_TEST:
case DEL_A:
case FE_PUSHA:
case FE_PUSHI:
case F_ADD_ASG:
case F_ASSIGN:
case F_DIV_ASG:
case F_MOD_ASG:
case F_MUL_ASG:
case F_POST_DEC:
case F_POST_INC:
case F_POW_ASG:
case F_PRE_DEC:
case F_PRE_INC:
case F_PUSHI:
case F_SUB_ASG:
case LAE_PUSHA:
case LAE_PUSHI:
case LA_PUSHA:
case L_PUSHA:
case L_PUSHI:
case NF_PUSHI:
case OL_GL:
case OL_GL_NR:
case POP_AL:
case SET_ALOOP:
case _ADD:
case _ADD_ASG:
case _ASSIGN:
case _CAT:
case _DIV:
case _DIV_ASG:
case _EQ:
case _EXIT0:
case _EXIT:
case _GT:
case _GTE:
case _HALT:
case _JMAIN:
case _JMP:
case _JNZ:
case _JZ:
case _LJNZ:
case _LJZ:
case _LT:
case _LTE:
case _MATCH2:
case _MOD:
case _MOD_ASG:
case _MUL:
case _MUL_ASG:
case _NEQ:
case _NEXT:
case _NEXTFILE:
case _NOT:
case _OMAIN:
case _POP:
case _POST_DEC:
case _POST_INC:
case _POW:
case _POW_ASG:
case _PRE_DEC:
case _PRE_INC:
case _PUSHINT:
case _RANGE:
case _RET0:
case _RET:
case _STOP:
case _SUB:
case _SUB_ASG:
case _TEST:
case _UMINUS:
case _UPLUS:
break;
}
}
}
void
TraceString2(const char *str, size_t len)
{
size_t limit = len;
size_t n;
TRACE(("\""));
if (limit) {
for (n = 0; n < limit; ++n) {
UChar ch = (UChar) str[n];
switch (ch) {
case '\"':
TRACE(("\\\""));
break;
case '\\':
TRACE(("\\\\"));
break;
case '\b':
TRACE(("\\b"));
break;
case '\f':
TRACE(("\\f"));
break;
case '\n':
TRACE(("\\n"));
break;
case '\r':
TRACE(("\\r"));
break;
case '\t':
TRACE(("\\t"));
break;
default:
if (ch < 32 || ch > 126)
TRACE(("\\%03o", ch));
else
TRACE(("%c", ch));
break;
}
}
}
TRACE(("\""));
}
void
TraceString(STRING * sp)
{
TraceString2(sp ? sp->str : "",
sp ? sp->len : 0);
}
#ifdef NO_LEAKS
void
trace_leaks(void)
{
if (trace_fp != 0) {
fclose(trace_fp);
trace_fp = 0;
}
}
#endif