1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <fcode/private.h>
38*0Sstevel@tonic-gate #include <fcode/log.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #ifndef DEBUG_LVL
41*0Sstevel@tonic-gate #define	DEBUG_LVL	0
42*0Sstevel@tonic-gate #endif
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate struct bitab {
45*0Sstevel@tonic-gate 	token_t bi_ptr;
46*0Sstevel@tonic-gate 	char *bi_name;
47*0Sstevel@tonic-gate 	int bi_type;
48*0Sstevel@tonic-gate };
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate struct bitab *lookup_builtin(token_t);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static int debug_level = DEBUG_LVL;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate void
55*0Sstevel@tonic-gate set_interpreter_debug_level(long lvl)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	debug_level = lvl;
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate long
61*0Sstevel@tonic-gate get_interpreter_debug_level(void)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	return (debug_level);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate void
67*0Sstevel@tonic-gate output_data_stack(fcode_env_t *env, int msglevel)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	int i;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	log_message(msglevel, "( ");
72*0Sstevel@tonic-gate 	if (DS > env->ds0) {
73*0Sstevel@tonic-gate 		for (i = 0; i < (DS - env->ds0); i++)
74*0Sstevel@tonic-gate 			log_message(msglevel, "%llx ",
75*0Sstevel@tonic-gate 			    (uint64_t)(env->ds0[i + 1]));
76*0Sstevel@tonic-gate 	} else
77*0Sstevel@tonic-gate 		log_message(msglevel, "<empty> ");
78*0Sstevel@tonic-gate 	log_message(msglevel, ") ");
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate void
82*0Sstevel@tonic-gate output_return_stack(fcode_env_t *env, int show_wa, int msglevel)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	int i;
85*0Sstevel@tonic-gate 	int anyout = 0;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	log_message(msglevel, "R:( ");
88*0Sstevel@tonic-gate 	if (show_wa) {
89*0Sstevel@tonic-gate 		log_message(msglevel, "%s ",
90*0Sstevel@tonic-gate 		    acf_backup_search(env, (acf_t)WA));
91*0Sstevel@tonic-gate 		anyout++;
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 	if (IP) {
94*0Sstevel@tonic-gate 		anyout++;
95*0Sstevel@tonic-gate 		log_message(msglevel, "%s ", acf_backup_search(env, IP));
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	for (i = (RS - env->rs0) - 1; i > 0; i--) {
98*0Sstevel@tonic-gate 		anyout++;
99*0Sstevel@tonic-gate 		log_message(msglevel, "%s ",
100*0Sstevel@tonic-gate 			    acf_backup_search(env, (acf_t)env->rs0[i+1]));
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 	if (!anyout)
103*0Sstevel@tonic-gate 		log_message(msglevel, "<empty> ");
104*0Sstevel@tonic-gate 	log_message(msglevel, ") ");
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate void
108*0Sstevel@tonic-gate dump_comma(fcode_env_t *env, char *type)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	xforth_t d;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if (strcmp(type, "x,") == 0)
113*0Sstevel@tonic-gate 		d = peek_xforth(env);
114*0Sstevel@tonic-gate 	else
115*0Sstevel@tonic-gate 		d = TOS;
116*0Sstevel@tonic-gate 	log_message(MSG_FC_DEBUG, "%s %p, %llx\n", type, HERE, (uint64_t)d);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate static int ndebug_names;
120*0Sstevel@tonic-gate #define	MAXDEBUG_NAMES	10
121*0Sstevel@tonic-gate static char *debug_names[MAXDEBUG_NAMES];
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static int ndebug_acfs;
124*0Sstevel@tonic-gate #define	MAXDEBUG_ACFS	10
125*0Sstevel@tonic-gate static acf_t debug_acfs[MAXDEBUG_ACFS];
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate void
128*0Sstevel@tonic-gate add_debug_acf(fcode_env_t *env, acf_t acf)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	int i;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	for (i = 0; i < ndebug_acfs; i++)
133*0Sstevel@tonic-gate 		if (acf == debug_acfs[i])
134*0Sstevel@tonic-gate 			return;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (!within_dictionary(env, acf))
137*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "Can't debug builtin\n");
138*0Sstevel@tonic-gate 	else if (ndebug_acfs >= MAXDEBUG_ACFS)
139*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "Too many debug ACF's\n");
140*0Sstevel@tonic-gate 	else {
141*0Sstevel@tonic-gate 		debug_acfs[ndebug_acfs++] = acf;
142*0Sstevel@tonic-gate 		*LINK_TO_FLAGS(ACF_TO_LINK(acf)) |= FLAG_DEBUG;
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate static void
147*0Sstevel@tonic-gate paren_debug(fcode_env_t *env)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	acf_t acf;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	acf = (acf_t)POP(DS);
152*0Sstevel@tonic-gate 	if (!within_dictionary(env, acf)) {
153*0Sstevel@tonic-gate 		log_message(MSG_INFO, "acf: %llx not in dictionary\n",
154*0Sstevel@tonic-gate 		    (uint64_t)acf);
155*0Sstevel@tonic-gate 		return;
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	if ((acf_t)_ALIGN(acf, token_t) != acf) {
158*0Sstevel@tonic-gate 		log_message(MSG_INFO, "acf: %llx not aligned\n",
159*0Sstevel@tonic-gate 		    (uint64_t)acf);
160*0Sstevel@tonic-gate 		return;
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 	if (*acf != (token_t)(&do_colon)) {
163*0Sstevel@tonic-gate 		log_message(MSG_INFO, "acf: %llx not a colon-def\n",
164*0Sstevel@tonic-gate 		    (uint64_t)acf);
165*0Sstevel@tonic-gate 		return;
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 	add_debug_acf(env, acf);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate static void
171*0Sstevel@tonic-gate debug(fcode_env_t *env)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	fstack_t d;
174*0Sstevel@tonic-gate 	char *word;
175*0Sstevel@tonic-gate 	acf_t acf;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	parse_word(env);
178*0Sstevel@tonic-gate 	dollar_find(env);
179*0Sstevel@tonic-gate 	d = POP(DS);
180*0Sstevel@tonic-gate 	if (d) {
181*0Sstevel@tonic-gate 		acf = (acf_t)POP(DS);
182*0Sstevel@tonic-gate 		add_debug_acf(env, acf);
183*0Sstevel@tonic-gate 	} else if (ndebug_names >= MAXDEBUG_NAMES) {
184*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "Too many forward debug words\n");
185*0Sstevel@tonic-gate 		two_drop(env);
186*0Sstevel@tonic-gate 	} else {
187*0Sstevel@tonic-gate 		word = pop_a_duped_string(env, NULL);
188*0Sstevel@tonic-gate 		log_message(MSG_INFO, "Forward defined word: %s\n", word);
189*0Sstevel@tonic-gate 		debug_names[ndebug_names++] = word;
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate  * Eliminate dups and add vocabulary forth to end if not already on list.
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate static void
197*0Sstevel@tonic-gate order_to_dict_list(fcode_env_t *env, token_t *order[])
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	int i, j, norder = 0;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (env->current)
202*0Sstevel@tonic-gate 		order[norder++] = env->current;
203*0Sstevel@tonic-gate 	for (i = env->order_depth; i >= 0; i--) {
204*0Sstevel@tonic-gate 		for (j = 0; j < norder && order[j] != env->order[i]; j++)
205*0Sstevel@tonic-gate 			;
206*0Sstevel@tonic-gate 		if (j == norder)
207*0Sstevel@tonic-gate 			order[norder++] = env->order[i];
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	for (j = 0; j < norder && order[j] != (token_t *)&env->forth_voc_link;
210*0Sstevel@tonic-gate 	    j++)
211*0Sstevel@tonic-gate 		;
212*0Sstevel@tonic-gate 	if (j == norder)
213*0Sstevel@tonic-gate 		order[norder++] = (token_t *)&env->forth_voc_link;
214*0Sstevel@tonic-gate 	order[norder] = NULL;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate static acf_t
218*0Sstevel@tonic-gate search_all_dictionaries(fcode_env_t *env,
219*0Sstevel@tonic-gate     acf_t (*fn)(fcode_env_t *, acf_t, void *),
220*0Sstevel@tonic-gate     void *arg)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	token_t *order[MAX_ORDER+1];
223*0Sstevel@tonic-gate 	int i;
224*0Sstevel@tonic-gate 	token_t *dptr;
225*0Sstevel@tonic-gate 	acf_t acf;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	order_to_dict_list(env, order);
228*0Sstevel@tonic-gate 	for (i = 0; (dptr = order[i]) != NULL; i++) {
229*0Sstevel@tonic-gate 		for (dptr = (token_t *)(*dptr); dptr;
230*0Sstevel@tonic-gate 		    dptr = (token_t *)(*dptr))
231*0Sstevel@tonic-gate 			if ((acf = (*fn)(env, LINK_TO_ACF(dptr), arg)) != NULL)
232*0Sstevel@tonic-gate 				return (acf);
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	return (NULL);
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate char *
238*0Sstevel@tonic-gate acf_to_str(acf_t acf)
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	static char msg[(sizeof (acf) * 2) + 3];
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	sprintf(msg, "(%08p)", acf);
243*0Sstevel@tonic-gate 	return (msg);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate char *
247*0Sstevel@tonic-gate get_name_or_acf(token_t *dptr)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	char *name;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if ((name = get_name(dptr)) != NULL)
252*0Sstevel@tonic-gate 		return (name);
253*0Sstevel@tonic-gate 	return (acf_to_str(LINK_TO_ACF(dptr)));
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate static void
257*0Sstevel@tonic-gate output_acf_name(acf_t acf)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	char *name;
260*0Sstevel@tonic-gate 	token_t *dptr;
261*0Sstevel@tonic-gate 	static int acf_count = 0;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	if (acf == NULL) {
264*0Sstevel@tonic-gate 		if (acf_count)
265*0Sstevel@tonic-gate 			log_message(MSG_INFO, "\n");
266*0Sstevel@tonic-gate 		acf_count = 0;
267*0Sstevel@tonic-gate 		return;
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	dptr = ACF_TO_LINK(acf);
270*0Sstevel@tonic-gate 	if ((name = get_name(dptr)) == NULL)
271*0Sstevel@tonic-gate 		name = "<noname>";
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	log_message(MSG_INFO, "%24s (%08p)", name, acf);
274*0Sstevel@tonic-gate 	if (++acf_count >= 2) {
275*0Sstevel@tonic-gate 		log_message(MSG_INFO, "\n");
276*0Sstevel@tonic-gate 		acf_count = 0;
277*0Sstevel@tonic-gate 	} else
278*0Sstevel@tonic-gate 		log_message(MSG_INFO, "    ");
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate static void
282*0Sstevel@tonic-gate dot_debug(fcode_env_t *env)
283*0Sstevel@tonic-gate {
284*0Sstevel@tonic-gate 	int i;
285*0Sstevel@tonic-gate 	token_t *dptr;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (ndebug_names == 0)
288*0Sstevel@tonic-gate 		log_message(MSG_INFO, "No forward debug words\n");
289*0Sstevel@tonic-gate 	else {
290*0Sstevel@tonic-gate 		for (i = 0; i < ndebug_names; i++)
291*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%s Forward\n", debug_names[i]);
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 	if (ndebug_acfs == 0)
294*0Sstevel@tonic-gate 		log_message(MSG_INFO, "No debug words\n");
295*0Sstevel@tonic-gate 	else {
296*0Sstevel@tonic-gate 		for (i = 0; i < ndebug_acfs; i++)
297*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%s\n",
298*0Sstevel@tonic-gate 			    get_name_or_acf(ACF_TO_LINK(debug_acfs[i])));
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static void
303*0Sstevel@tonic-gate do_undebug(fcode_env_t *env, char *name)
304*0Sstevel@tonic-gate {
305*0Sstevel@tonic-gate 	int i;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	for (i = 0; i < ndebug_names; i++) {
308*0Sstevel@tonic-gate 		if (strcmp(debug_names[i], name) == 0) {
309*0Sstevel@tonic-gate 			log_message(MSG_INFO, "Undebugging forward word %s\n",
310*0Sstevel@tonic-gate 			    name);
311*0Sstevel@tonic-gate 			FREE(debug_names[i]);
312*0Sstevel@tonic-gate 			for (i++; i < ndebug_names; i++)
313*0Sstevel@tonic-gate 				debug_names[i - 1] = debug_names[i];
314*0Sstevel@tonic-gate 			ndebug_names--;
315*0Sstevel@tonic-gate 			break;
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate static void
321*0Sstevel@tonic-gate undebug(fcode_env_t *env)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	fstack_t d;
324*0Sstevel@tonic-gate 	acf_t acf;
325*0Sstevel@tonic-gate 	flag_t *flagp;
326*0Sstevel@tonic-gate 	char *name;
327*0Sstevel@tonic-gate 	int i, j;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	parse_word(env);
330*0Sstevel@tonic-gate 	two_dup(env);
331*0Sstevel@tonic-gate 	dollar_find(env);
332*0Sstevel@tonic-gate 	d = POP(DS);
333*0Sstevel@tonic-gate 	if (d) {
334*0Sstevel@tonic-gate 		acf = (acf_t)POP(DS);
335*0Sstevel@tonic-gate 		flagp = LINK_TO_FLAGS(ACF_TO_LINK(acf));
336*0Sstevel@tonic-gate 		if ((*flagp & FLAG_DEBUG) == 0)
337*0Sstevel@tonic-gate 			log_message(MSG_WARN, "Word not debugged?\n");
338*0Sstevel@tonic-gate 		else {
339*0Sstevel@tonic-gate 			log_message(MSG_INFO, "Undebugging acf: %p\n", acf);
340*0Sstevel@tonic-gate 			*flagp &= ~FLAG_DEBUG;
341*0Sstevel@tonic-gate 			for (i = 0; i < ndebug_acfs; i++) {
342*0Sstevel@tonic-gate 				if (debug_acfs[i] == acf) {
343*0Sstevel@tonic-gate 					for (j = i + 1; j < ndebug_acfs; j++)
344*0Sstevel@tonic-gate 						debug_acfs[j-1] = debug_acfs[j];
345*0Sstevel@tonic-gate 					ndebug_acfs--;
346*0Sstevel@tonic-gate 					break;
347*0Sstevel@tonic-gate 				}
348*0Sstevel@tonic-gate 			}
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 	} else
351*0Sstevel@tonic-gate 		two_drop(env);
352*0Sstevel@tonic-gate 	name = pop_a_string(env, NULL);
353*0Sstevel@tonic-gate 	do_undebug(env, name);
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate int
357*0Sstevel@tonic-gate name_is_debugged(fcode_env_t *env, char *name)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	int i;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	if (ndebug_names <= 0)
362*0Sstevel@tonic-gate 		return (0);
363*0Sstevel@tonic-gate 	for (i = 0; i < ndebug_names; i++)
364*0Sstevel@tonic-gate 		if (strcmp(debug_names[i], name) == 0)
365*0Sstevel@tonic-gate 			return (1);
366*0Sstevel@tonic-gate 	return (0);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate /*
370*0Sstevel@tonic-gate  * This is complicated by being given ACF's to temporary compile words which
371*0Sstevel@tonic-gate  * don't have a header.
372*0Sstevel@tonic-gate  */
373*0Sstevel@tonic-gate int
374*0Sstevel@tonic-gate is_debug_word(fcode_env_t *env, acf_t acf)
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 	flag_t *flagp;
377*0Sstevel@tonic-gate 	int i;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	/* check to see if any words are being debugged */
380*0Sstevel@tonic-gate 	if (ndebug_acfs == 0)
381*0Sstevel@tonic-gate 		return (0);
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	/* only words in dictionary can be debugged */
384*0Sstevel@tonic-gate 	if (!within_dictionary(env, acf))
385*0Sstevel@tonic-gate 		return (0);
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/* check that word has "FLAG_DEBUG" on */
388*0Sstevel@tonic-gate 	flagp = LINK_TO_FLAGS(ACF_TO_LINK(acf));
389*0Sstevel@tonic-gate 	if ((*flagp & FLAG_DEBUG) == 0)
390*0Sstevel@tonic-gate 		return (0);
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	/* look in table of debug acf's */
393*0Sstevel@tonic-gate 	for (i = 0; i < ndebug_acfs; i++)
394*0Sstevel@tonic-gate 		if (debug_acfs[i] == acf)
395*0Sstevel@tonic-gate 			return (1);
396*0Sstevel@tonic-gate 	return (0);
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate #define	MAX_DEBUG_STACK	100
400*0Sstevel@tonic-gate token_t debug_low[MAX_DEBUG_STACK], debug_high[MAX_DEBUG_STACK];
401*0Sstevel@tonic-gate int debug_prev_level[MAX_DEBUG_STACK];
402*0Sstevel@tonic-gate int debug_curr_level[MAX_DEBUG_STACK];
403*0Sstevel@tonic-gate int ndebug_stack = 0;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate void
406*0Sstevel@tonic-gate debug_set_level(fcode_env_t *env, int level)
407*0Sstevel@tonic-gate {
408*0Sstevel@tonic-gate 	debug_curr_level[ndebug_stack - 1] = level;
409*0Sstevel@tonic-gate 	set_interpreter_debug_level(level);
410*0Sstevel@tonic-gate }
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate token_t
413*0Sstevel@tonic-gate find_semi_in_colon_def(fcode_env_t *env, acf_t acf)
414*0Sstevel@tonic-gate {
415*0Sstevel@tonic-gate 	for (; within_dictionary(env, acf); acf++)
416*0Sstevel@tonic-gate 		if (*acf == (token_t)(&semi_ptr))
417*0Sstevel@tonic-gate 			return ((token_t)acf);
418*0Sstevel@tonic-gate 	return (0);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate void
422*0Sstevel@tonic-gate check_for_debug_entry(fcode_env_t *env)
423*0Sstevel@tonic-gate {
424*0Sstevel@tonic-gate 	int top;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	if (is_debug_word(env, WA) && ndebug_stack < MAX_DEBUG_STACK) {
427*0Sstevel@tonic-gate 		top = ndebug_stack++;
428*0Sstevel@tonic-gate 		debug_prev_level[top] = get_interpreter_debug_level();
429*0Sstevel@tonic-gate 		debug_low[top] = (token_t)WA;
430*0Sstevel@tonic-gate 		if (*WA == (token_t)(&do_colon)) {
431*0Sstevel@tonic-gate 			debug_high[top] =
432*0Sstevel@tonic-gate 			    find_semi_in_colon_def(env, WA);
433*0Sstevel@tonic-gate 		} else {
434*0Sstevel@tonic-gate 			debug_high[top] = 0;	/* marker... */
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 		debug_set_level(env, DEBUG_STEPPING);
437*0Sstevel@tonic-gate 		output_step_message(env);
438*0Sstevel@tonic-gate 	}
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate void
442*0Sstevel@tonic-gate check_for_debug_exit(fcode_env_t *env)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	if (ndebug_stack) {
445*0Sstevel@tonic-gate 		int top = ndebug_stack - 1;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 		if (debug_high[top] == 0) {
448*0Sstevel@tonic-gate 			set_interpreter_debug_level(debug_prev_level[top]);
449*0Sstevel@tonic-gate 			ndebug_stack--;
450*0Sstevel@tonic-gate 		} else if ((token_t)IP >= debug_low[top] &&
451*0Sstevel@tonic-gate 		    (token_t)IP <= debug_high[top]) {
452*0Sstevel@tonic-gate 			set_interpreter_debug_level(debug_curr_level[top]);
453*0Sstevel@tonic-gate 		} else {
454*0Sstevel@tonic-gate 			set_interpreter_debug_level(debug_prev_level[top]);
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 	}
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate void
460*0Sstevel@tonic-gate check_semi_debug_exit(fcode_env_t *env)
461*0Sstevel@tonic-gate {
462*0Sstevel@tonic-gate 	if (ndebug_stack) {
463*0Sstevel@tonic-gate 		int top = ndebug_stack - 1;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 		if ((token_t)(IP - 1) == debug_high[top]) {
466*0Sstevel@tonic-gate 			set_interpreter_debug_level(debug_prev_level[top]);
467*0Sstevel@tonic-gate 			ndebug_stack--;
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 	}
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate /*
473*0Sstevel@tonic-gate  * Really entering do_run, since this may be a recursive entry to do_run,
474*0Sstevel@tonic-gate  * we need to set the debug level to what it was previously.
475*0Sstevel@tonic-gate  */
476*0Sstevel@tonic-gate int
477*0Sstevel@tonic-gate current_debug_state(fcode_env_t *env)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 	if (ndebug_stack) {
480*0Sstevel@tonic-gate 		int top = ndebug_stack - 1;
481*0Sstevel@tonic-gate 		set_interpreter_debug_level(debug_prev_level[top]);
482*0Sstevel@tonic-gate 	}
483*0Sstevel@tonic-gate 	return (ndebug_stack);
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate void
487*0Sstevel@tonic-gate clear_debug_state(fcode_env_t *env, int oldstate)
488*0Sstevel@tonic-gate {
489*0Sstevel@tonic-gate 	if (ndebug_stack && oldstate <= ndebug_stack) {
490*0Sstevel@tonic-gate 		set_interpreter_debug_level(debug_prev_level[oldstate]);
491*0Sstevel@tonic-gate 		ndebug_stack = oldstate;
492*0Sstevel@tonic-gate 	}
493*0Sstevel@tonic-gate }
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate void
496*0Sstevel@tonic-gate unbug(fcode_env_t *env)
497*0Sstevel@tonic-gate {
498*0Sstevel@tonic-gate 	int i;
499*0Sstevel@tonic-gate 	token_t *link;
500*0Sstevel@tonic-gate 	flag_t *flag;
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	for (i = ndebug_stack - 1; i >= 0; i--) {
503*0Sstevel@tonic-gate 		link = ACF_TO_LINK(debug_low[i]);
504*0Sstevel@tonic-gate 		flag = LINK_TO_FLAGS(link);
505*0Sstevel@tonic-gate 		*flag &= ~FLAG_DEBUG;
506*0Sstevel@tonic-gate 	}
507*0Sstevel@tonic-gate 	clear_debug_state(env, 0);
508*0Sstevel@tonic-gate }
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate void
511*0Sstevel@tonic-gate output_vitals(fcode_env_t *env)
512*0Sstevel@tonic-gate {
513*0Sstevel@tonic-gate 	log_message(MSG_FC_DEBUG, "IP=%p, *IP=%p, WA=%p, *WA=%p ", IP,
514*0Sstevel@tonic-gate 	    (IP ? *IP : 0), WA, (WA ? *WA : 0));
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate int
518*0Sstevel@tonic-gate do_exec_debug(fcode_env_t *env, void *fn)
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate 	int dl = debug_level;
521*0Sstevel@tonic-gate 	int show_wa = 1;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	if ((dl & (DEBUG_EXEC_DUMP_DS | DEBUG_EXEC_DUMP_RS |
524*0Sstevel@tonic-gate 	    DEBUG_EXEC_SHOW_VITALS | DEBUG_EXEC_TRACE | DEBUG_TRACING |
525*0Sstevel@tonic-gate 	    DEBUG_STEPPING)) == 0)
526*0Sstevel@tonic-gate 		return (0);
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	if (dl & DEBUG_STEPPING) {
529*0Sstevel@tonic-gate 		dl |= DEBUG_EXEC_DUMP_DS;
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 	if (dl & (DEBUG_STEPPING | DEBUG_EXEC_TRACE)) {
532*0Sstevel@tonic-gate 		log_message(MSG_FC_DEBUG, "%-15s ", acf_to_name(env, WA));
533*0Sstevel@tonic-gate 		show_wa = 0;
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 	if (dl & DEBUG_EXEC_DUMP_DS)
536*0Sstevel@tonic-gate 		output_data_stack(env, MSG_FC_DEBUG);
537*0Sstevel@tonic-gate 	if (dl & DEBUG_EXEC_DUMP_RS)
538*0Sstevel@tonic-gate 		output_return_stack(env, show_wa, MSG_FC_DEBUG);
539*0Sstevel@tonic-gate 	if (dl & DEBUG_EXEC_SHOW_VITALS)
540*0Sstevel@tonic-gate 		output_vitals(env);
541*0Sstevel@tonic-gate 	if (dl & DEBUG_TRACING)
542*0Sstevel@tonic-gate 		do_fclib_trace(env, (void *) fn);
543*0Sstevel@tonic-gate 	log_message(MSG_FC_DEBUG, "\n");
544*0Sstevel@tonic-gate 	if (dl & DEBUG_STEPPING)
545*0Sstevel@tonic-gate 		return (do_fclib_step(env));
546*0Sstevel@tonic-gate 	return (0);
547*0Sstevel@tonic-gate }
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate static void
550*0Sstevel@tonic-gate smatch(fcode_env_t *env)
551*0Sstevel@tonic-gate {
552*0Sstevel@tonic-gate 	int len;
553*0Sstevel@tonic-gate 	char *str, *p;
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	if ((str = parse_a_string(env, &len)) == NULL)
556*0Sstevel@tonic-gate 		log_message(MSG_INFO, "smatch: no string\n");
557*0Sstevel@tonic-gate 	else {
558*0Sstevel@tonic-gate 		for (p = (char *)env->base; p < (char *)HERE; p++)
559*0Sstevel@tonic-gate 			if (memcmp(p, str, len) == 0)
560*0Sstevel@tonic-gate 				log_message(MSG_DEBUG, "%p\n", p);
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate void
565*0Sstevel@tonic-gate check_vitals(fcode_env_t *env)
566*0Sstevel@tonic-gate {
567*0Sstevel@tonic-gate 	int i;
568*0Sstevel@tonic-gate 	token_t *dptr;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	dptr = env->current;
571*0Sstevel@tonic-gate 	if (*dptr && !within_dictionary(env, (uchar_t *)*dptr))
572*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "Current: %p outside dictionary\n",
573*0Sstevel@tonic-gate 		    *dptr);
574*0Sstevel@tonic-gate 	for (i = env->order_depth; i >= 0; i--) {
575*0Sstevel@tonic-gate 		dptr = env->order[i];
576*0Sstevel@tonic-gate 		if (!dptr)
577*0Sstevel@tonic-gate 			continue;
578*0Sstevel@tonic-gate 		if (*dptr && !within_dictionary(env, (uchar_t *)*dptr))
579*0Sstevel@tonic-gate 			log_message(MSG_ERROR, "Order%d: %p outside"
580*0Sstevel@tonic-gate 			    " dictionary\n", i, *dptr);
581*0Sstevel@tonic-gate 	}
582*0Sstevel@tonic-gate 	if (HERE < env->base || HERE >= env->base + dict_size) {
583*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "HERE: %p outside range\n", HERE);
584*0Sstevel@tonic-gate 	}
585*0Sstevel@tonic-gate 	if (DS < env->ds0 || DS >= &env->ds0[stack_size]) {
586*0Sstevel@tonic-gate 		forth_abort(env, "DS: %p outside range\n", DS);
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 	if (RS < env->rs0 || RS >= &env->rs0[stack_size]) {
589*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "RS: %p outside range\n", RS);
590*0Sstevel@tonic-gate 		RS = env->rs0;
591*0Sstevel@tonic-gate 	}
592*0Sstevel@tonic-gate 	if (IP && !within_dictionary(env, IP))
593*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "IP: %p outside dictionary\n", IP);
594*0Sstevel@tonic-gate 	if (!within_dictionary(env, (void *)env->forth_voc_link))
595*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "forth_voc_link: %p outside"
596*0Sstevel@tonic-gate 		    " dictionary\n", env->forth_voc_link);
597*0Sstevel@tonic-gate }
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate static void
600*0Sstevel@tonic-gate dump_table(fcode_env_t *env)
601*0Sstevel@tonic-gate {
602*0Sstevel@tonic-gate 	int i;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	for (i = 0; i < MAX_FCODE; i++) {
605*0Sstevel@tonic-gate 		if (*(env->table[i].apf) != (token_t)(&f_error)) {
606*0Sstevel@tonic-gate 			log_message(MSG_DEBUG, "Token: %4x %32s acf = %8p,"
607*0Sstevel@tonic-gate 			    " %8p\n", i, env->table[i].name, env->table[i].apf,
608*0Sstevel@tonic-gate 			    *(env->table[i].apf));
609*0Sstevel@tonic-gate 		}
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "%d FCODES implemented\n", fcode_impl_count);
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate void
615*0Sstevel@tonic-gate verify_usage(fcode_env_t *env)
616*0Sstevel@tonic-gate {
617*0Sstevel@tonic-gate 	int i, untested = 0;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	for (i = 0; i < MAX_FCODE; i++) {
620*0Sstevel@tonic-gate 		int verify;
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 		verify = env->table[i].flags & (ANSI_WORD|P1275_WORD);
623*0Sstevel@tonic-gate 		if ((verify) &&
624*0Sstevel@tonic-gate #ifdef DEBUG
625*0Sstevel@tonic-gate 			(env->table[i].usage == 0) &&
626*0Sstevel@tonic-gate #endif
627*0Sstevel@tonic-gate 			(env->table[i].apf)) {
628*0Sstevel@tonic-gate 			log_message(MSG_DEBUG,
629*0Sstevel@tonic-gate 			    "Untested: %4x %32s acf = %8p, %8p\n", i,
630*0Sstevel@tonic-gate 			    env->table[i].name, env->table[i].apf,
631*0Sstevel@tonic-gate 			    *(env->table[i].apf));
632*0Sstevel@tonic-gate 			untested++;
633*0Sstevel@tonic-gate 		}
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 	if (untested)
636*0Sstevel@tonic-gate 		log_message(MSG_DEBUG, "%d untested tokens\n", untested);
637*0Sstevel@tonic-gate }
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate static void
640*0Sstevel@tonic-gate debugf(fcode_env_t *env)
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&debug_level);
643*0Sstevel@tonic-gate }
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate static void
646*0Sstevel@tonic-gate control(fcode_env_t *env)
647*0Sstevel@tonic-gate {
648*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&env->control);
649*0Sstevel@tonic-gate }
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate struct bittab {
652*0Sstevel@tonic-gate 	int b_bitval;
653*0Sstevel@tonic-gate 	char *b_bitname;
654*0Sstevel@tonic-gate } bittab[] = {
655*0Sstevel@tonic-gate 	DEBUG_CONTEXT,		"context",
656*0Sstevel@tonic-gate 	DEBUG_BYTELOAD_DS,	"byteload-ds",
657*0Sstevel@tonic-gate 	DEBUG_BYTELOAD_RS,	"byteload-rs",
658*0Sstevel@tonic-gate 	DEBUG_BYTELOAD_TOKENS,	"byteload-tokens",
659*0Sstevel@tonic-gate 	DEBUG_NEW_TOKEN,	"new-token",
660*0Sstevel@tonic-gate 	DEBUG_EXEC_TRACE,	"exec-trace",
661*0Sstevel@tonic-gate 	DEBUG_EXEC_SHOW_VITALS,	"exec-show-vitals",
662*0Sstevel@tonic-gate 	DEBUG_EXEC_DUMP_DS,	"exec-dump-ds",
663*0Sstevel@tonic-gate 	DEBUG_EXEC_DUMP_RS,	"exec-dump-rs",
664*0Sstevel@tonic-gate 	DEBUG_COMMA,		"comma",
665*0Sstevel@tonic-gate 	DEBUG_HEADER,		"header",
666*0Sstevel@tonic-gate 	DEBUG_EXIT_WORDS,	"exit-words",
667*0Sstevel@tonic-gate 	DEBUG_EXIT_DUMP,	"exit-dump",
668*0Sstevel@tonic-gate 	DEBUG_DUMP_TOKENS,	"dump-tokens",
669*0Sstevel@tonic-gate 	DEBUG_COLON,		"colon",
670*0Sstevel@tonic-gate 	DEBUG_NEXT_VITALS,	"next-vitals",
671*0Sstevel@tonic-gate 	DEBUG_VOC_FIND,		"voc-find",
672*0Sstevel@tonic-gate 	DEBUG_DUMP_DICT_TOKENS,	"dump-dict-tokens",
673*0Sstevel@tonic-gate 	DEBUG_TOKEN_USAGE,	"token-usage",
674*0Sstevel@tonic-gate 	DEBUG_DUMP_TOKEN_TABLE,	"dump-token-table",
675*0Sstevel@tonic-gate 	DEBUG_SHOW_STACK,	"show-stack",
676*0Sstevel@tonic-gate 	DEBUG_SHOW_RS,		"show-rs",
677*0Sstevel@tonic-gate 	DEBUG_TRACING,		"tracing",
678*0Sstevel@tonic-gate 	DEBUG_TRACE_STACK,	"trace-stack",
679*0Sstevel@tonic-gate 	DEBUG_CALL_METHOD,	"call-method",
680*0Sstevel@tonic-gate 	DEBUG_ACTIONS,		"actions",
681*0Sstevel@tonic-gate 	DEBUG_STEPPING,		"stepping",
682*0Sstevel@tonic-gate 	DEBUG_REG_ACCESS,	"reg-access",
683*0Sstevel@tonic-gate 	DEBUG_ADDR_ABUSE,	"addr-abuse",
684*0Sstevel@tonic-gate 	DEBUG_FIND_FCODE,	"find-fcode",
685*0Sstevel@tonic-gate 	DEBUG_UPLOAD,		"upload",
686*0Sstevel@tonic-gate 	0
687*0Sstevel@tonic-gate };
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate void
690*0Sstevel@tonic-gate debug_flags_to_output(fcode_env_t *env, int flags)
691*0Sstevel@tonic-gate {
692*0Sstevel@tonic-gate 	int first = 1, i;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	for (i = 0; bittab[i].b_bitval != 0; i++)
695*0Sstevel@tonic-gate 		if (bittab[i].b_bitval & flags) {
696*0Sstevel@tonic-gate 			if (!first)
697*0Sstevel@tonic-gate 				log_message(MSG_INFO, ",");
698*0Sstevel@tonic-gate 			first = 0;
699*0Sstevel@tonic-gate 			log_message(MSG_INFO, bittab[i].b_bitname);
700*0Sstevel@tonic-gate 		}
701*0Sstevel@tonic-gate 	if (first)
702*0Sstevel@tonic-gate 		log_message(MSG_INFO, "<empty>");
703*0Sstevel@tonic-gate 	log_message(MSG_INFO, "\n");
704*0Sstevel@tonic-gate }
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate static void
707*0Sstevel@tonic-gate dot_debugf(fcode_env_t *env)
708*0Sstevel@tonic-gate {
709*0Sstevel@tonic-gate 	debug_flags_to_output(env, debug_level);
710*0Sstevel@tonic-gate }
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate static void
713*0Sstevel@tonic-gate debugf_qmark(fcode_env_t *env)
714*0Sstevel@tonic-gate {
715*0Sstevel@tonic-gate 	debug_flags_to_output(env, 0xffffffff);
716*0Sstevel@tonic-gate }
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate int
719*0Sstevel@tonic-gate debug_flags_to_mask(char *str)
720*0Sstevel@tonic-gate {
721*0Sstevel@tonic-gate 	int flags = 0;
722*0Sstevel@tonic-gate 	char *p;
723*0Sstevel@tonic-gate 	int i;
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	if (isdigit(*str)) {
726*0Sstevel@tonic-gate 		if (*str == '0') {
727*0Sstevel@tonic-gate 			str++;
728*0Sstevel@tonic-gate 			if (*str == 'x' || *str == 'X') {
729*0Sstevel@tonic-gate 				sscanf(str + 1, "%x", &flags);
730*0Sstevel@tonic-gate 			} else
731*0Sstevel@tonic-gate 				sscanf(str, "%o", &flags);
732*0Sstevel@tonic-gate 		} else
733*0Sstevel@tonic-gate 			sscanf(str, "%d", &flags);
734*0Sstevel@tonic-gate 		return (flags);
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate 	if (strcmp(str, "clear") == 0)
737*0Sstevel@tonic-gate 		return (0);
738*0Sstevel@tonic-gate 	if (strcmp(str, "all") == 0)
739*0Sstevel@tonic-gate 		return (0xffffffff & ~DEBUG_STEPPING);
740*0Sstevel@tonic-gate 	if (*str) {
741*0Sstevel@tonic-gate 		do {
742*0Sstevel@tonic-gate 			if (p = strchr(str, ','))
743*0Sstevel@tonic-gate 				*p++ = '\0';
744*0Sstevel@tonic-gate 			for (i = 0; bittab[i].b_bitname != 0; i++)
745*0Sstevel@tonic-gate 				if (strcmp(str, bittab[i].b_bitname) == 0) {
746*0Sstevel@tonic-gate 					flags |= bittab[i].b_bitval;
747*0Sstevel@tonic-gate 					break;
748*0Sstevel@tonic-gate 			}
749*0Sstevel@tonic-gate 			if (bittab[i].b_bitname == 0)
750*0Sstevel@tonic-gate 				log_message(MSG_WARN,
751*0Sstevel@tonic-gate 				    "Unknown debug flag: '%s'\n", str);
752*0Sstevel@tonic-gate 			str = p;
753*0Sstevel@tonic-gate 		} while (p);
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 	return (flags);
756*0Sstevel@tonic-gate }
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate static void
759*0Sstevel@tonic-gate set_debugf(fcode_env_t *env)
760*0Sstevel@tonic-gate {
761*0Sstevel@tonic-gate 	char *str;
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	str = parse_a_string(env, NULL);
764*0Sstevel@tonic-gate 	debug_level = debug_flags_to_mask(str);
765*0Sstevel@tonic-gate }
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate static acf_t
768*0Sstevel@tonic-gate show_a_word(fcode_env_t *env, acf_t acf, void *arg)
769*0Sstevel@tonic-gate {
770*0Sstevel@tonic-gate 	static int nshow_words = 0;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	if (acf == NULL) {
773*0Sstevel@tonic-gate 		if (nshow_words > 0) {
774*0Sstevel@tonic-gate 			log_message(MSG_DEBUG, "\n");
775*0Sstevel@tonic-gate 			nshow_words = 0;
776*0Sstevel@tonic-gate 		}
777*0Sstevel@tonic-gate 		return (NULL);
778*0Sstevel@tonic-gate 	}
779*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "%15s  ", get_name_or_acf(ACF_TO_LINK(acf)));
780*0Sstevel@tonic-gate 	nshow_words++;
781*0Sstevel@tonic-gate 	if (nshow_words >= 4) {
782*0Sstevel@tonic-gate 		log_message(MSG_DEBUG, "\n");
783*0Sstevel@tonic-gate 		nshow_words = 0;
784*0Sstevel@tonic-gate 	}
785*0Sstevel@tonic-gate 	return (NULL);
786*0Sstevel@tonic-gate }
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate void
789*0Sstevel@tonic-gate words(fcode_env_t *env)
790*0Sstevel@tonic-gate {
791*0Sstevel@tonic-gate 	(void) search_all_dictionaries(env, show_a_word, NULL);
792*0Sstevel@tonic-gate 	(void) show_a_word(env, NULL, NULL);
793*0Sstevel@tonic-gate }
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate static acf_t
796*0Sstevel@tonic-gate dump_a_word(fcode_env_t *env, acf_t acf, void *arg)
797*0Sstevel@tonic-gate {
798*0Sstevel@tonic-gate 	output_acf_name(acf);
799*0Sstevel@tonic-gate 	return (NULL);
800*0Sstevel@tonic-gate }
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate void
803*0Sstevel@tonic-gate dump_words(fcode_env_t *env)
804*0Sstevel@tonic-gate {
805*0Sstevel@tonic-gate 	(void) search_all_dictionaries(env, dump_a_word, NULL);
806*0Sstevel@tonic-gate 	output_acf_name(NULL);
807*0Sstevel@tonic-gate }
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate static void
810*0Sstevel@tonic-gate dump_line(uchar_t *ptr)
811*0Sstevel@tonic-gate {
812*0Sstevel@tonic-gate 	uchar_t *byte;
813*0Sstevel@tonic-gate 	int i;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	log_message(MSG_INFO, "%p  ", (uint32_t)ptr);
816*0Sstevel@tonic-gate 	for (i = 0, byte = ptr; i < 16; i++) {
817*0Sstevel@tonic-gate 		if (i == 8)
818*0Sstevel@tonic-gate 			log_message(MSG_INFO, " ");
819*0Sstevel@tonic-gate 		log_message(MSG_INFO, "%02.2x ", *byte++);
820*0Sstevel@tonic-gate 	}
821*0Sstevel@tonic-gate 	log_message(MSG_INFO, " ");
822*0Sstevel@tonic-gate 	for (i = 0, byte = ptr; i < 16; i++, byte++) {
823*0Sstevel@tonic-gate 		log_message(MSG_INFO, "%c",
824*0Sstevel@tonic-gate 		    ((*byte < 0x20) || (*byte > 0x7f)) ? '.' : *byte);
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 	log_message(MSG_INFO, "\n");
827*0Sstevel@tonic-gate }
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate void
830*0Sstevel@tonic-gate dump_dictionary(fcode_env_t *env)
831*0Sstevel@tonic-gate {
832*0Sstevel@tonic-gate 	uchar_t *ptr;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Dictionary dump: base: %p\n", env->base);
835*0Sstevel@tonic-gate 	for (ptr = (uchar_t *)(((long)(env->base)) & ~0xf); ptr < HERE;
836*0Sstevel@tonic-gate 	    ptr += 16)
837*0Sstevel@tonic-gate 		dump_line(ptr);
838*0Sstevel@tonic-gate }
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate static char *
841*0Sstevel@tonic-gate acf_to_fcode_name(fcode_env_t *env, acf_t acf)
842*0Sstevel@tonic-gate {
843*0Sstevel@tonic-gate 	int i;
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	for (i = 0; i < MAX_FCODE; i++)
846*0Sstevel@tonic-gate 		if (env->table[i].apf == acf)
847*0Sstevel@tonic-gate 			return (env->table[i].name);
848*0Sstevel@tonic-gate 	return (NULL);
849*0Sstevel@tonic-gate }
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate static acf_t
852*0Sstevel@tonic-gate acf_match(fcode_env_t *env, acf_t sacf, void *macf)
853*0Sstevel@tonic-gate {
854*0Sstevel@tonic-gate 	if (sacf == (acf_t)macf)
855*0Sstevel@tonic-gate 		return (sacf);
856*0Sstevel@tonic-gate 	return (NULL);
857*0Sstevel@tonic-gate }
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate /*
860*0Sstevel@tonic-gate  * Given an ACF, return ptr to name or "unknown" string.
861*0Sstevel@tonic-gate  */
862*0Sstevel@tonic-gate char *
863*0Sstevel@tonic-gate acf_to_name(fcode_env_t *env, acf_t acf)
864*0Sstevel@tonic-gate {
865*0Sstevel@tonic-gate 	struct bitab *bip;
866*0Sstevel@tonic-gate 	static char name_buf[256];
867*0Sstevel@tonic-gate 	uchar_t *p, *np;
868*0Sstevel@tonic-gate 	int i, n;
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	if (!within_dictionary(env, acf)) {
871*0Sstevel@tonic-gate 		if ((bip = lookup_builtin((token_t)acf)) != NULL)
872*0Sstevel@tonic-gate 			return (bip->bi_name);
873*0Sstevel@tonic-gate 		return (NULL);
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 	return (get_name_or_acf(ACF_TO_LINK(acf)));
876*0Sstevel@tonic-gate }
877*0Sstevel@tonic-gate 
878*0Sstevel@tonic-gate int
879*0Sstevel@tonic-gate within_dictionary(fcode_env_t *env, void *addr)
880*0Sstevel@tonic-gate {
881*0Sstevel@tonic-gate 	return ((uchar_t *)addr >= env->base &&
882*0Sstevel@tonic-gate 	    (uchar_t *)addr < env->base + dict_size);
883*0Sstevel@tonic-gate }
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate static int
886*0Sstevel@tonic-gate within_word(fcode_env_t *env, acf_t acf, acf_t wacf)
887*0Sstevel@tonic-gate {
888*0Sstevel@tonic-gate 	if (acf == wacf || acf + 1 == wacf)
889*0Sstevel@tonic-gate 		return (1);
890*0Sstevel@tonic-gate 	if (*acf == (token_t)(&do_colon)) {
891*0Sstevel@tonic-gate 		do {
892*0Sstevel@tonic-gate 			if (acf == wacf)
893*0Sstevel@tonic-gate 				return (1);
894*0Sstevel@tonic-gate 		} while (*acf++ != (token_t)(&semi_ptr));
895*0Sstevel@tonic-gate 	}
896*0Sstevel@tonic-gate 	return (0);
897*0Sstevel@tonic-gate }
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate /*
900*0Sstevel@tonic-gate  * Given an ACF in the middle of a colon definition, search dictionary towards
901*0Sstevel@tonic-gate  * beginning for "colon" acf.  If we find a "semi" acf first, we're not in
902*0Sstevel@tonic-gate  * the middle of a colon-def (temporary execute?).
903*0Sstevel@tonic-gate  */
904*0Sstevel@tonic-gate char *
905*0Sstevel@tonic-gate acf_backup_search(fcode_env_t *env, acf_t acf)
906*0Sstevel@tonic-gate {
907*0Sstevel@tonic-gate 	acf_t nacf;
908*0Sstevel@tonic-gate 	char *name;
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 	if ((acf_t)_ALIGN(acf, token_t) == acf && within_dictionary(env, acf)) {
911*0Sstevel@tonic-gate 		for (nacf = acf; nacf >= (acf_t)env->base; nacf--)
912*0Sstevel@tonic-gate 			if (*nacf == (token_t)(&do_colon) ||
913*0Sstevel@tonic-gate 			    *nacf == (token_t)(&semi_ptr))
914*0Sstevel@tonic-gate 				break;
915*0Sstevel@tonic-gate 		if (nacf >= (acf_t)env->base && *nacf == (token_t)(&do_colon) &&
916*0Sstevel@tonic-gate 		    (name = get_name(ACF_TO_LINK(nacf))) != NULL)
917*0Sstevel@tonic-gate 			return (name);
918*0Sstevel@tonic-gate 	}
919*0Sstevel@tonic-gate 	return (acf_to_str(acf));
920*0Sstevel@tonic-gate }
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate /*
923*0Sstevel@tonic-gate  * Print out current process's C stack using /usr/proc/bin/pstack
924*0Sstevel@tonic-gate  */
925*0Sstevel@tonic-gate void
926*0Sstevel@tonic-gate ctrace(fcode_env_t *env)
927*0Sstevel@tonic-gate {
928*0Sstevel@tonic-gate 	char buf[256];
929*0Sstevel@tonic-gate 	FILE *fd;
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Interpreter C Stack:\n");
932*0Sstevel@tonic-gate 	sprintf(buf, "/usr/proc/bin/pstack %d", getpid());
933*0Sstevel@tonic-gate 	if ((fd = popen(buf, "r")) == NULL)
934*0Sstevel@tonic-gate 		log_perror(MSG_ERROR, "Can't run: %s", buf);
935*0Sstevel@tonic-gate 	else {
936*0Sstevel@tonic-gate 		while (fgets(buf, sizeof (buf), fd))
937*0Sstevel@tonic-gate 			log_message(MSG_DEBUG, buf);
938*0Sstevel@tonic-gate 		fclose(fd);
939*0Sstevel@tonic-gate 	}
940*0Sstevel@tonic-gate }
941*0Sstevel@tonic-gate 
942*0Sstevel@tonic-gate /*
943*0Sstevel@tonic-gate  * Dump data, return stacks, try to unthread forth calling stack.
944*0Sstevel@tonic-gate  */
945*0Sstevel@tonic-gate void
946*0Sstevel@tonic-gate ftrace(fcode_env_t *env)
947*0Sstevel@tonic-gate {
948*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "Forth Interpreter Stacks:\n");
949*0Sstevel@tonic-gate 	output_data_stack(env, MSG_DEBUG);
950*0Sstevel@tonic-gate 	output_return_stack(env, 1, MSG_DEBUG);
951*0Sstevel@tonic-gate 	log_message(MSG_DEBUG, "\n");
952*0Sstevel@tonic-gate }
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate int in_forth_abort;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate /*
957*0Sstevel@tonic-gate  * Handle fatal error, if interactive mode, return to ok prompt.
958*0Sstevel@tonic-gate  */
959*0Sstevel@tonic-gate void
960*0Sstevel@tonic-gate forth_abort(fcode_env_t *env, char *fmt, ...)
961*0Sstevel@tonic-gate {
962*0Sstevel@tonic-gate 	va_list ap;
963*0Sstevel@tonic-gate 	char msg[256];
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 	if (in_forth_abort) {
966*0Sstevel@tonic-gate 		log_message(MSG_FATAL, "ABORT: abort within forth_abort\n");
967*0Sstevel@tonic-gate 		abort();
968*0Sstevel@tonic-gate 	}
969*0Sstevel@tonic-gate 	in_forth_abort++;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	va_start(ap, fmt);
972*0Sstevel@tonic-gate 	vsprintf(msg, fmt, ap);
973*0Sstevel@tonic-gate 	log_message(MSG_ERROR, "ABORT: %s\n", msg);
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 	if (env) {
976*0Sstevel@tonic-gate 		ctrace(env);
977*0Sstevel@tonic-gate 		ftrace(env);
978*0Sstevel@tonic-gate 	}
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 	return_to_interact(env);
981*0Sstevel@tonic-gate 	/*
982*0Sstevel@tonic-gate 	 * If not in interactive mode, return_to_interact just returns.
983*0Sstevel@tonic-gate 	 */
984*0Sstevel@tonic-gate 	exit(1);
985*0Sstevel@tonic-gate }
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate /*
988*0Sstevel@tonic-gate  * Handle fatal system call error
989*0Sstevel@tonic-gate  */
990*0Sstevel@tonic-gate void
991*0Sstevel@tonic-gate forth_perror(fcode_env_t *env, char *fmt, ...)
992*0Sstevel@tonic-gate {
993*0Sstevel@tonic-gate 	va_list ap;
994*0Sstevel@tonic-gate 	char msg[256];
995*0Sstevel@tonic-gate 	int save_errno = errno;	/* just in case... */
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 	va_start(ap, fmt);
998*0Sstevel@tonic-gate 	vsprintf(msg, fmt, ap);
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	forth_abort(env, "%s: %s", msg, strerror(save_errno));
1001*0Sstevel@tonic-gate }
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate static void
1004*0Sstevel@tonic-gate show_stack(fcode_env_t *env)
1005*0Sstevel@tonic-gate {
1006*0Sstevel@tonic-gate #ifdef DEBUG
1007*0Sstevel@tonic-gate 	debug_level ^= DEBUG_SHOW_STACK;
1008*0Sstevel@tonic-gate #else
1009*0Sstevel@tonic-gate 	/*EMPTY*/
1010*0Sstevel@tonic-gate #endif
1011*0Sstevel@tonic-gate }
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate static void
1014*0Sstevel@tonic-gate print_bytes_header(int width, int offset)
1015*0Sstevel@tonic-gate {
1016*0Sstevel@tonic-gate 	int i;
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	for (i = 0; i < width; i++)
1019*0Sstevel@tonic-gate 		log_message(MSG_INFO, " ");
1020*0Sstevel@tonic-gate 	log_message(MSG_INFO, "  ");
1021*0Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
1022*0Sstevel@tonic-gate 		if (i == 8)
1023*0Sstevel@tonic-gate 			log_message(MSG_INFO, " ");
1024*0Sstevel@tonic-gate 		if (i == offset)
1025*0Sstevel@tonic-gate 			log_message(MSG_INFO, "\\/ ");
1026*0Sstevel@tonic-gate 		else
1027*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%2x ", i);
1028*0Sstevel@tonic-gate 	}
1029*0Sstevel@tonic-gate 	log_message(MSG_INFO, " ");
1030*0Sstevel@tonic-gate 	for (i = 0; i < 16; i++) {
1031*0Sstevel@tonic-gate 		if (i == offset)
1032*0Sstevel@tonic-gate 			log_message(MSG_INFO, "v");
1033*0Sstevel@tonic-gate 		else
1034*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%x", i);
1035*0Sstevel@tonic-gate 	}
1036*0Sstevel@tonic-gate 	log_message(MSG_INFO, "\n");
1037*0Sstevel@tonic-gate }
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate static void
1040*0Sstevel@tonic-gate dump(fcode_env_t *env)
1041*0Sstevel@tonic-gate {
1042*0Sstevel@tonic-gate 	uchar_t *data;
1043*0Sstevel@tonic-gate 	int len, offset;
1044*0Sstevel@tonic-gate 	char buf[20];
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	len = POP(DS);
1047*0Sstevel@tonic-gate 	data = (uchar_t *)POP(DS);
1048*0Sstevel@tonic-gate 	offset = ((long)data) & 0xf;
1049*0Sstevel@tonic-gate 	len += offset;
1050*0Sstevel@tonic-gate 	data = (uchar_t *)((long)data & ~0xf);
1051*0Sstevel@tonic-gate 	sprintf(buf, "%p", (uint32_t)data);
1052*0Sstevel@tonic-gate 	print_bytes_header(strlen(buf), offset);
1053*0Sstevel@tonic-gate 	for (len += offset; len > 0; len -= 16, data += 16)
1054*0Sstevel@tonic-gate 		dump_line(data);
1055*0Sstevel@tonic-gate }
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate static acf_t
1058*0Sstevel@tonic-gate do_sifting(fcode_env_t *env, acf_t acf, void *pat)
1059*0Sstevel@tonic-gate {
1060*0Sstevel@tonic-gate 	char *name;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 	if ((name = get_name(ACF_TO_LINK(acf))) != NULL && strstr(name, pat))
1063*0Sstevel@tonic-gate 		output_acf_name(acf);
1064*0Sstevel@tonic-gate 	return (NULL);
1065*0Sstevel@tonic-gate }
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate static void
1068*0Sstevel@tonic-gate sifting(fcode_env_t *env)
1069*0Sstevel@tonic-gate {
1070*0Sstevel@tonic-gate 	char *pat;
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	if ((pat = parse_a_string(env, NULL)) != NULL) {
1073*0Sstevel@tonic-gate 		(void) search_all_dictionaries(env, do_sifting, pat);
1074*0Sstevel@tonic-gate 		output_acf_name(NULL);
1075*0Sstevel@tonic-gate 	}
1076*0Sstevel@tonic-gate }
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate void
1079*0Sstevel@tonic-gate print_level(int level, int *doprint)
1080*0Sstevel@tonic-gate {
1081*0Sstevel@tonic-gate 	int i;
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate 	if (*doprint) {
1084*0Sstevel@tonic-gate 		log_message(MSG_DEBUG, "\n    ");
1085*0Sstevel@tonic-gate 		for (i = 0; i < level; i++)
1086*0Sstevel@tonic-gate 			log_message(MSG_DEBUG, "    ");
1087*0Sstevel@tonic-gate 		*doprint = 0;
1088*0Sstevel@tonic-gate 	}
1089*0Sstevel@tonic-gate }
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate #define	BI_QUOTE	1
1092*0Sstevel@tonic-gate #define	BI_BLIT		2
1093*0Sstevel@tonic-gate #define	BI_BDO		3
1094*0Sstevel@tonic-gate #define	BI_QDO		4
1095*0Sstevel@tonic-gate #define	BI_BR		5
1096*0Sstevel@tonic-gate #define	BI_QBR		6
1097*0Sstevel@tonic-gate #define	BI_BOF		7
1098*0Sstevel@tonic-gate #define	BI_LOOP		8
1099*0Sstevel@tonic-gate #define	BI_PLOOP	9
1100*0Sstevel@tonic-gate #define	BI_TO		10
1101*0Sstevel@tonic-gate #define	BI_SEMI		11
1102*0Sstevel@tonic-gate #define	BI_COLON	12
1103*0Sstevel@tonic-gate #define	BI_NOOP		13
1104*0Sstevel@tonic-gate #define	BI_NOTYET	14	/* unimplented in "see" */
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate struct bitab bitab[] = {
1107*0Sstevel@tonic-gate 	(token_t)(&quote_ptr),			"\"",		BI_QUOTE,
1108*0Sstevel@tonic-gate 	(token_t)(&blit_ptr),			"blit",		BI_BLIT,
1109*0Sstevel@tonic-gate 	(token_t)(&do_bdo_ptr),			"do",		BI_BDO,
1110*0Sstevel@tonic-gate 	(token_t)(&do_bqdo_ptr),		"?do",		BI_QDO,
1111*0Sstevel@tonic-gate 	(token_t)(&bbranch_ptrs[0]),		"br",		BI_BR,
1112*0Sstevel@tonic-gate 	(token_t)(&bbranch_ptrs[1]),		"qbr",		BI_QBR,
1113*0Sstevel@tonic-gate 	(token_t)(&bbranch_ptrs[2]),		"bof",		BI_BOF,
1114*0Sstevel@tonic-gate 	(token_t)(&do_loop_ptr),		"loop",		BI_LOOP,
1115*0Sstevel@tonic-gate 	(token_t)(&do_ploop_ptr),		"+loop",	BI_PLOOP,
1116*0Sstevel@tonic-gate 	(token_t)(&to_ptr),			"to",		BI_NOOP,
1117*0Sstevel@tonic-gate 	(token_t)(&semi_ptr),			";",		BI_SEMI,
1118*0Sstevel@tonic-gate 	(token_t)(&do_colon),			":",		BI_COLON,
1119*0Sstevel@tonic-gate 	(token_t)(&tlit_ptr),			"[']",		BI_NOOP,
1120*0Sstevel@tonic-gate 	(token_t)(&do_leave_ptr),		"leave",	BI_NOTYET,
1121*0Sstevel@tonic-gate 	(token_t)(&create_ptr),			"create",	BI_NOTYET,
1122*0Sstevel@tonic-gate 	(token_t)(&does_ptr),			"does>",	BI_NOTYET,
1123*0Sstevel@tonic-gate 	(token_t)(&value_defines[0][0]),	"a.@",		BI_NOTYET,
1124*0Sstevel@tonic-gate 	(token_t)(&value_defines[0][1]),	"a.!",		BI_NOTYET,
1125*0Sstevel@tonic-gate 	(token_t)(&value_defines[0][2]),	"a.nop",	BI_NOTYET,
1126*0Sstevel@tonic-gate 	(token_t)(&value_defines[1][0]),	"a.i@",		BI_NOTYET,
1127*0Sstevel@tonic-gate 	(token_t)(&value_defines[1][1]),	"a.i!",		BI_NOTYET,
1128*0Sstevel@tonic-gate 	(token_t)(&value_defines[1][2]),	"a.iad",	BI_NOTYET,
1129*0Sstevel@tonic-gate 	(token_t)(&value_defines[2][0]),	"a.defer",	BI_NOTYET,
1130*0Sstevel@tonic-gate 	(token_t)(&value_defines[2][1]),	"a.@",		BI_NOTYET,
1131*0Sstevel@tonic-gate 	(token_t)(&value_defines[2][2]),	"a.nop",	BI_NOTYET,
1132*0Sstevel@tonic-gate 	(token_t)(&value_defines[3][0]),	"a.defexec",	BI_NOTYET,
1133*0Sstevel@tonic-gate 	(token_t)(&value_defines[3][1]),	"a.iset",	BI_NOTYET,
1134*0Sstevel@tonic-gate 	(token_t)(&value_defines[3][2]),	"a.iad",	BI_NOTYET,
1135*0Sstevel@tonic-gate 	(token_t)(&value_defines[4][0]),	"a.binit",	BI_NOTYET,
1136*0Sstevel@tonic-gate 	(token_t)(&value_defines[4][1]),	"a.2drop",	BI_NOTYET,
1137*0Sstevel@tonic-gate 	(token_t)(&value_defines[4][2]),	"a.nop",	BI_NOTYET,
1138*0Sstevel@tonic-gate 	(token_t)(&value_defines[5][0]),	"a.ibinit",	BI_NOTYET,
1139*0Sstevel@tonic-gate 	(token_t)(&value_defines[5][1]),	"a.2drop",	BI_NOTYET,
1140*0Sstevel@tonic-gate 	(token_t)(&value_defines[5][2]),	"a.iad",	BI_NOTYET,
1141*0Sstevel@tonic-gate 	0
1142*0Sstevel@tonic-gate };
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate struct bitab *
1145*0Sstevel@tonic-gate lookup_builtin(token_t builtin)
1146*0Sstevel@tonic-gate {
1147*0Sstevel@tonic-gate 	int i;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	for (i = 0; bitab[i].bi_ptr; i++)
1150*0Sstevel@tonic-gate 		if (bitab[i].bi_ptr == builtin)
1151*0Sstevel@tonic-gate 			return (&bitab[i]);
1152*0Sstevel@tonic-gate 	return (NULL);
1153*0Sstevel@tonic-gate }
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate static void
1156*0Sstevel@tonic-gate paren_see(fcode_env_t *env)
1157*0Sstevel@tonic-gate {
1158*0Sstevel@tonic-gate 	acf_t save_acf = (acf_t)POP(DS);
1159*0Sstevel@tonic-gate 	acf_t acf = save_acf;
1160*0Sstevel@tonic-gate 	int i, n, pass;
1161*0Sstevel@tonic-gate 	token_t brtab[30], thentab[30], brstk[30];
1162*0Sstevel@tonic-gate 	int nbrtab = 0, nthentab = 0, nbrstk = 0;
1163*0Sstevel@tonic-gate 	uchar_t *p;
1164*0Sstevel@tonic-gate 	int level = 0, doprintlevel = 1, nthen;
1165*0Sstevel@tonic-gate 	struct bitab *bip;
1166*0Sstevel@tonic-gate 	token_t last_lit = 0, case_lit = 0, endof_loc = 0, endcase_loc = 0;
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 	if ((bip = lookup_builtin(*acf)) == NULL ||
1169*0Sstevel@tonic-gate 	    bip->bi_type != BI_COLON) {
1170*0Sstevel@tonic-gate 		if (bip = lookup_builtin((token_t)acf))
1171*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%s: builtin\n", bip->bi_name);
1172*0Sstevel@tonic-gate 		else
1173*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%s: builtin\n",
1174*0Sstevel@tonic-gate 			    acf_to_name(env, acf));
1175*0Sstevel@tonic-gate 		return;
1176*0Sstevel@tonic-gate 	}
1177*0Sstevel@tonic-gate 	log_message(MSG_INFO, ": %s", acf_to_name(env, acf));
1178*0Sstevel@tonic-gate 	for (pass = 0; pass < 2; pass++) {
1179*0Sstevel@tonic-gate 		acf = save_acf;
1180*0Sstevel@tonic-gate 		for (acf++; ; acf++) {
1181*0Sstevel@tonic-gate 			if (pass) {
1182*0Sstevel@tonic-gate 				print_level(level, &doprintlevel);
1183*0Sstevel@tonic-gate 				for (nthen = 0; nthentab > 0 &&
1184*0Sstevel@tonic-gate 				    thentab[nthentab-1] == (token_t)acf;
1185*0Sstevel@tonic-gate 				    nthentab--)
1186*0Sstevel@tonic-gate 					nthen++;
1187*0Sstevel@tonic-gate 				if (nthen) {
1188*0Sstevel@tonic-gate 					level -= nthen;
1189*0Sstevel@tonic-gate 					doprintlevel = 1;
1190*0Sstevel@tonic-gate 					print_level(level, &doprintlevel);
1191*0Sstevel@tonic-gate 					for (i = 0; i < nthen; i++)
1192*0Sstevel@tonic-gate 						log_message(MSG_INFO, "then ");
1193*0Sstevel@tonic-gate 				}
1194*0Sstevel@tonic-gate 				print_level(level, &doprintlevel);
1195*0Sstevel@tonic-gate 				for (i = 0; i < nbrtab; i += 2)
1196*0Sstevel@tonic-gate 					if ((token_t)acf == brtab[i]) {
1197*0Sstevel@tonic-gate 						log_message(MSG_INFO, "begin ");
1198*0Sstevel@tonic-gate 						brstk[nbrstk++] = brtab[i+1];
1199*0Sstevel@tonic-gate 						level++;
1200*0Sstevel@tonic-gate 						doprintlevel = 1;
1201*0Sstevel@tonic-gate 					}
1202*0Sstevel@tonic-gate 				print_level(level, &doprintlevel);
1203*0Sstevel@tonic-gate 				if (case_lit == (token_t)acf) {
1204*0Sstevel@tonic-gate 					log_message(MSG_INFO, "case ");
1205*0Sstevel@tonic-gate 					doprintlevel = 1;
1206*0Sstevel@tonic-gate 					print_level(level, &doprintlevel);
1207*0Sstevel@tonic-gate 				}
1208*0Sstevel@tonic-gate 				if (endof_loc == (token_t)acf) {
1209*0Sstevel@tonic-gate 					log_message(MSG_INFO, "endof ");
1210*0Sstevel@tonic-gate 					doprintlevel = 1;
1211*0Sstevel@tonic-gate 					print_level(level, &doprintlevel);
1212*0Sstevel@tonic-gate 				}
1213*0Sstevel@tonic-gate 				if (endcase_loc == (token_t)acf) {
1214*0Sstevel@tonic-gate 					doprintlevel = 1;
1215*0Sstevel@tonic-gate 					print_level(level, &doprintlevel);
1216*0Sstevel@tonic-gate 					log_message(MSG_INFO, "endcase ");
1217*0Sstevel@tonic-gate 				}
1218*0Sstevel@tonic-gate 			}
1219*0Sstevel@tonic-gate 			if ((bip = lookup_builtin((token_t)*acf)) == 0) {
1220*0Sstevel@tonic-gate 				last_lit = (token_t)acf;
1221*0Sstevel@tonic-gate 				if (pass)
1222*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s ",
1223*0Sstevel@tonic-gate 					    acf_to_name(env, (acf_t)*acf));
1224*0Sstevel@tonic-gate 				continue;
1225*0Sstevel@tonic-gate 			}
1226*0Sstevel@tonic-gate 			if (bip->bi_type == BI_SEMI) {
1227*0Sstevel@tonic-gate 				if (pass) {
1228*0Sstevel@tonic-gate 					log_message(MSG_INFO, "\n");
1229*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s\n",
1230*0Sstevel@tonic-gate 					    bip->bi_name);
1231*0Sstevel@tonic-gate 				}
1232*0Sstevel@tonic-gate 				break;
1233*0Sstevel@tonic-gate 			}
1234*0Sstevel@tonic-gate 			switch (bip->bi_type) {
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 			case BI_NOOP:
1237*0Sstevel@tonic-gate 			case BI_NOTYET:
1238*0Sstevel@tonic-gate 				if (pass)
1239*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s ",
1240*0Sstevel@tonic-gate 					    bip->bi_name);
1241*0Sstevel@tonic-gate 				break;
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 			case BI_QUOTE:
1244*0Sstevel@tonic-gate 				if (pass)
1245*0Sstevel@tonic-gate 					log_message(MSG_INFO, "\" ");
1246*0Sstevel@tonic-gate 				acf++;
1247*0Sstevel@tonic-gate 				p = (uchar_t *)acf;
1248*0Sstevel@tonic-gate 				n = *p++;
1249*0Sstevel@tonic-gate 				if (pass)
1250*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s\" ", p);
1251*0Sstevel@tonic-gate 				p += n + 1;
1252*0Sstevel@tonic-gate 				for (; ((token_t)(p)) & (sizeof (token_t) - 1);
1253*0Sstevel@tonic-gate 				    p++)
1254*0Sstevel@tonic-gate 					;
1255*0Sstevel@tonic-gate 				acf = (acf_t)p;
1256*0Sstevel@tonic-gate 				acf--;
1257*0Sstevel@tonic-gate 				break;
1258*0Sstevel@tonic-gate 
1259*0Sstevel@tonic-gate 			case BI_BLIT:
1260*0Sstevel@tonic-gate 				acf++;
1261*0Sstevel@tonic-gate 				if (pass)
1262*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%x ", *acf);
1263*0Sstevel@tonic-gate 				break;
1264*0Sstevel@tonic-gate 
1265*0Sstevel@tonic-gate 			case BI_BDO:
1266*0Sstevel@tonic-gate 			case BI_QDO:
1267*0Sstevel@tonic-gate 				if (pass) {
1268*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s ",
1269*0Sstevel@tonic-gate 					    bip->bi_name);
1270*0Sstevel@tonic-gate 					doprintlevel = 1;
1271*0Sstevel@tonic-gate 					level++;
1272*0Sstevel@tonic-gate 				}
1273*0Sstevel@tonic-gate 				acf++;
1274*0Sstevel@tonic-gate 				break;
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate 			case BI_BR:
1277*0Sstevel@tonic-gate 				acf++;
1278*0Sstevel@tonic-gate 				if (pass) {
1279*0Sstevel@tonic-gate 					if (*acf < (token_t)acf) {
1280*0Sstevel@tonic-gate 						if (nbrstk) {
1281*0Sstevel@tonic-gate 							doprintlevel = 1;
1282*0Sstevel@tonic-gate 							level--;
1283*0Sstevel@tonic-gate 							print_level(level,
1284*0Sstevel@tonic-gate 							    &doprintlevel);
1285*0Sstevel@tonic-gate 							log_message(MSG_INFO,
1286*0Sstevel@tonic-gate 							    "repeat ");
1287*0Sstevel@tonic-gate 							nbrstk--;
1288*0Sstevel@tonic-gate 						} else
1289*0Sstevel@tonic-gate 							log_message(MSG_INFO,
1290*0Sstevel@tonic-gate 							    "[br back?]");
1291*0Sstevel@tonic-gate 					} else if (nthentab) {
1292*0Sstevel@tonic-gate 						doprintlevel = 1;
1293*0Sstevel@tonic-gate 						print_level(level - 1,
1294*0Sstevel@tonic-gate 						    &doprintlevel);
1295*0Sstevel@tonic-gate 						log_message(MSG_INFO, "else ");
1296*0Sstevel@tonic-gate 						doprintlevel = 1;
1297*0Sstevel@tonic-gate 						thentab[nthentab - 1] = *acf;
1298*0Sstevel@tonic-gate 					}
1299*0Sstevel@tonic-gate 				} else {
1300*0Sstevel@tonic-gate 					if (*acf < (token_t)acf) {
1301*0Sstevel@tonic-gate 						brtab[nbrtab++] = *acf;
1302*0Sstevel@tonic-gate 						brtab[nbrtab++] = (token_t)acf;
1303*0Sstevel@tonic-gate 					}
1304*0Sstevel@tonic-gate 					if (endcase_loc == 0 &&
1305*0Sstevel@tonic-gate 					    case_lit) {
1306*0Sstevel@tonic-gate 						endcase_loc = *acf;
1307*0Sstevel@tonic-gate 					}
1308*0Sstevel@tonic-gate 				}
1309*0Sstevel@tonic-gate 				break;
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate 			case BI_QBR:
1312*0Sstevel@tonic-gate 				acf++;
1313*0Sstevel@tonic-gate 				if (pass) {
1314*0Sstevel@tonic-gate 					if (*acf < (token_t)acf) {
1315*0Sstevel@tonic-gate 						if (nbrstk) {
1316*0Sstevel@tonic-gate 							doprintlevel = 1;
1317*0Sstevel@tonic-gate 							level--;
1318*0Sstevel@tonic-gate 							print_level(level,
1319*0Sstevel@tonic-gate 							    &doprintlevel);
1320*0Sstevel@tonic-gate 							log_message(MSG_INFO,
1321*0Sstevel@tonic-gate 							    "until ");
1322*0Sstevel@tonic-gate 							nbrstk--;
1323*0Sstevel@tonic-gate 						} else
1324*0Sstevel@tonic-gate 							log_message(MSG_INFO,
1325*0Sstevel@tonic-gate 							    "[br back?]");
1326*0Sstevel@tonic-gate 					} else if (nbrstk > 0 &&
1327*0Sstevel@tonic-gate 					    *acf >= brstk[nbrstk - 1]) {
1328*0Sstevel@tonic-gate 						doprintlevel = 1;
1329*0Sstevel@tonic-gate 						print_level(level - 1,
1330*0Sstevel@tonic-gate 						    &doprintlevel);
1331*0Sstevel@tonic-gate 						log_message(MSG_INFO,
1332*0Sstevel@tonic-gate 						    "while ");
1333*0Sstevel@tonic-gate 						doprintlevel = 1;
1334*0Sstevel@tonic-gate 					} else {
1335*0Sstevel@tonic-gate 						log_message(MSG_INFO, "if ");
1336*0Sstevel@tonic-gate 						doprintlevel = 1;
1337*0Sstevel@tonic-gate 						level++;
1338*0Sstevel@tonic-gate 						thentab[nthentab++] = *acf;
1339*0Sstevel@tonic-gate 					}
1340*0Sstevel@tonic-gate 				} else if (*acf < (token_t)acf) {
1341*0Sstevel@tonic-gate 					brtab[nbrtab++] = *acf;
1342*0Sstevel@tonic-gate 					brtab[nbrtab++] = (token_t)acf;
1343*0Sstevel@tonic-gate 				}
1344*0Sstevel@tonic-gate 				break;
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate 			case BI_BOF:
1347*0Sstevel@tonic-gate 				acf++;
1348*0Sstevel@tonic-gate 				if (pass) {
1349*0Sstevel@tonic-gate 					log_message(MSG_INFO, "of ");
1350*0Sstevel@tonic-gate 					endof_loc = *acf;
1351*0Sstevel@tonic-gate 				} else if (case_lit == 0) {
1352*0Sstevel@tonic-gate 					case_lit = last_lit;
1353*0Sstevel@tonic-gate 				}
1354*0Sstevel@tonic-gate 				break;
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 			case BI_LOOP:
1357*0Sstevel@tonic-gate 			case BI_PLOOP:
1358*0Sstevel@tonic-gate 				if (pass) {
1359*0Sstevel@tonic-gate 					level--;
1360*0Sstevel@tonic-gate 					doprintlevel = 1;
1361*0Sstevel@tonic-gate 					print_level(level, &doprintlevel);
1362*0Sstevel@tonic-gate 					log_message(MSG_INFO, "%s ",
1363*0Sstevel@tonic-gate 					    bip->bi_name);
1364*0Sstevel@tonic-gate 				}
1365*0Sstevel@tonic-gate 				acf++;
1366*0Sstevel@tonic-gate 				break;
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 			default:
1369*0Sstevel@tonic-gate 				log_message(MSG_ERROR, "Invalid builtin %s\n",
1370*0Sstevel@tonic-gate 				    bip->bi_name);
1371*0Sstevel@tonic-gate 			}
1372*0Sstevel@tonic-gate 		}
1373*0Sstevel@tonic-gate 	}
1374*0Sstevel@tonic-gate }
1375*0Sstevel@tonic-gate 
1376*0Sstevel@tonic-gate static void
1377*0Sstevel@tonic-gate see(fcode_env_t *env)
1378*0Sstevel@tonic-gate {
1379*0Sstevel@tonic-gate 	fstack_t d;
1380*0Sstevel@tonic-gate 
1381*0Sstevel@tonic-gate 	parse_word(env);
1382*0Sstevel@tonic-gate 	dollar_find(env);
1383*0Sstevel@tonic-gate 	d = POP(DS);
1384*0Sstevel@tonic-gate 	if (d)
1385*0Sstevel@tonic-gate 		paren_see(env);
1386*0Sstevel@tonic-gate 	else {
1387*0Sstevel@tonic-gate 		log_message(MSG_WARN, "?");
1388*0Sstevel@tonic-gate 		two_drop(env);
1389*0Sstevel@tonic-gate 	}
1390*0Sstevel@tonic-gate }
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate static acf_t
1393*0Sstevel@tonic-gate do_dot_calls(fcode_env_t *env, acf_t acf, void *cacf)
1394*0Sstevel@tonic-gate {
1395*0Sstevel@tonic-gate 	token_t *dptr = ACF_TO_LINK(acf);
1396*0Sstevel@tonic-gate 	token_t *wptr = acf;
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 	if (*wptr == (token_t)(&do_colon)) {
1399*0Sstevel@tonic-gate 		do {
1400*0Sstevel@tonic-gate 			if ((acf_t)(*wptr) == (acf_t)cacf)
1401*0Sstevel@tonic-gate 				output_acf_name(acf);
1402*0Sstevel@tonic-gate 		} while (*wptr++ != (token_t)(&semi_ptr));
1403*0Sstevel@tonic-gate 	} else if ((acf_t)(*wptr) == cacf)
1404*0Sstevel@tonic-gate 		output_acf_name(acf);
1405*0Sstevel@tonic-gate 	else if (wptr == (token_t *)cacf)
1406*0Sstevel@tonic-gate 		output_acf_name(acf);
1407*0Sstevel@tonic-gate 	return (NULL);
1408*0Sstevel@tonic-gate }
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate static void
1411*0Sstevel@tonic-gate dot_calls(fcode_env_t *env)
1412*0Sstevel@tonic-gate {
1413*0Sstevel@tonic-gate 	acf_t acf = (acf_t)POP(DS);
1414*0Sstevel@tonic-gate 
1415*0Sstevel@tonic-gate 	search_all_dictionaries(env, do_dot_calls, acf);
1416*0Sstevel@tonic-gate 	output_acf_name(NULL);
1417*0Sstevel@tonic-gate }
1418*0Sstevel@tonic-gate 
1419*0Sstevel@tonic-gate static void
1420*0Sstevel@tonic-gate dot_pci_space(fcode_env_t *env)
1421*0Sstevel@tonic-gate {
1422*0Sstevel@tonic-gate 	fstack_t d = POP(DS);
1423*0Sstevel@tonic-gate 
1424*0Sstevel@tonic-gate 	switch ((d >> 24) & 0x3) {
1425*0Sstevel@tonic-gate 	case 0: log_message(MSG_INFO, "Config,"); break;
1426*0Sstevel@tonic-gate 	case 1: log_message(MSG_INFO, "IO,"); break;
1427*0Sstevel@tonic-gate 	case 2: log_message(MSG_INFO, "Memory32,"); break;
1428*0Sstevel@tonic-gate 	case 3: log_message(MSG_INFO, "Memory64,"); break;
1429*0Sstevel@tonic-gate 	}
1430*0Sstevel@tonic-gate 	if (d & 0x80000000)
1431*0Sstevel@tonic-gate 		log_message(MSG_INFO, "Not_reloc,");
1432*0Sstevel@tonic-gate 	if (d & 0x400000000)
1433*0Sstevel@tonic-gate 		log_message(MSG_INFO, "Prefetch,");
1434*0Sstevel@tonic-gate 	if (d & 0x200000000)
1435*0Sstevel@tonic-gate 		log_message(MSG_INFO, "Alias,");
1436*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Bus%d,", (d >> 16) & 0xff);
1437*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Dev%d,", (d >> 11) & 0x1f);
1438*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Func%d,", (d >> 8) & 0x7);
1439*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Reg%x", d & 0xff);
1440*0Sstevel@tonic-gate 	log_message(MSG_INFO, "\n");
1441*0Sstevel@tonic-gate }
1442*0Sstevel@tonic-gate 
1443*0Sstevel@tonic-gate void
1444*0Sstevel@tonic-gate fcode_debug(fcode_env_t *env)
1445*0Sstevel@tonic-gate {
1446*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)(&env->fcode_debug));
1447*0Sstevel@tonic-gate }
1448*0Sstevel@tonic-gate 
1449*0Sstevel@tonic-gate static void
1450*0Sstevel@tonic-gate base_addr(fcode_env_t *env)
1451*0Sstevel@tonic-gate {
1452*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)env->base);
1453*0Sstevel@tonic-gate }
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate static int mw_valid;
1456*0Sstevel@tonic-gate static int mw_size;
1457*0Sstevel@tonic-gate static void *mw_addr;
1458*0Sstevel@tonic-gate static fstack_t mw_value;
1459*0Sstevel@tonic-gate static fstack_t mw_lastvalue;
1460*0Sstevel@tonic-gate 
1461*0Sstevel@tonic-gate static fstack_t
1462*0Sstevel@tonic-gate mw_fetch(void)
1463*0Sstevel@tonic-gate {
1464*0Sstevel@tonic-gate 	switch (mw_size) {
1465*0Sstevel@tonic-gate 	case 1: return (*((uint8_t *)mw_addr));
1466*0Sstevel@tonic-gate 	case 2: return (*((uint16_t *)mw_addr));
1467*0Sstevel@tonic-gate 	case 4: return (*((uint32_t *)mw_addr));
1468*0Sstevel@tonic-gate 	case 8: return (*((uint64_t *)mw_addr));
1469*0Sstevel@tonic-gate 	}
1470*0Sstevel@tonic-gate 	return (0);
1471*0Sstevel@tonic-gate }
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate void
1474*0Sstevel@tonic-gate do_memory_watch(fcode_env_t *env)
1475*0Sstevel@tonic-gate {
1476*0Sstevel@tonic-gate 	fstack_t value;
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 	if (!mw_valid)
1479*0Sstevel@tonic-gate 		return;
1480*0Sstevel@tonic-gate 	value = mw_fetch();
1481*0Sstevel@tonic-gate 	if (value != mw_lastvalue) {
1482*0Sstevel@tonic-gate 		if (mw_valid == 1 || mw_value == value) {
1483*0Sstevel@tonic-gate 			log_message(MSG_INFO,
1484*0Sstevel@tonic-gate 			    "memory-watch: %p/%d: %llx -> %llx\n",
1485*0Sstevel@tonic-gate 			    mw_addr, mw_size, (uint64_t)mw_lastvalue,
1486*0Sstevel@tonic-gate 			    (uint64_t)value);
1487*0Sstevel@tonic-gate 			do_fclib_step(env);
1488*0Sstevel@tonic-gate 		}
1489*0Sstevel@tonic-gate 		mw_lastvalue = value;
1490*0Sstevel@tonic-gate 	}
1491*0Sstevel@tonic-gate }
1492*0Sstevel@tonic-gate 
1493*0Sstevel@tonic-gate static void
1494*0Sstevel@tonic-gate set_memory_watch(fcode_env_t *env, int type, int size, void *addr,
1495*0Sstevel@tonic-gate     fstack_t value)
1496*0Sstevel@tonic-gate {
1497*0Sstevel@tonic-gate 	switch (size) {
1498*0Sstevel@tonic-gate 	case 1: case 2: case 4: case 8:
1499*0Sstevel@tonic-gate 		break;
1500*0Sstevel@tonic-gate 	default:
1501*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "set_memory_watch: invalid size: %d\n",
1502*0Sstevel@tonic-gate 		    size);
1503*0Sstevel@tonic-gate 		return;
1504*0Sstevel@tonic-gate 	}
1505*0Sstevel@tonic-gate 	mw_valid = type;
1506*0Sstevel@tonic-gate 	mw_size = size;
1507*0Sstevel@tonic-gate 	mw_addr = addr;
1508*0Sstevel@tonic-gate 	mw_value = value;
1509*0Sstevel@tonic-gate 	mw_lastvalue = mw_fetch();
1510*0Sstevel@tonic-gate }
1511*0Sstevel@tonic-gate 
1512*0Sstevel@tonic-gate static void
1513*0Sstevel@tonic-gate memory_watch(fcode_env_t *env)
1514*0Sstevel@tonic-gate {
1515*0Sstevel@tonic-gate 	int size = POP(DS);
1516*0Sstevel@tonic-gate 	void *addr = (void *)POP(DS);
1517*0Sstevel@tonic-gate 
1518*0Sstevel@tonic-gate 	set_memory_watch(env, 1, size, addr, 0);
1519*0Sstevel@tonic-gate }
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate static void
1522*0Sstevel@tonic-gate memory_watch_value(fcode_env_t *env)
1523*0Sstevel@tonic-gate {
1524*0Sstevel@tonic-gate 	int size = POP(DS);
1525*0Sstevel@tonic-gate 	void *addr = (void *)POP(DS);
1526*0Sstevel@tonic-gate 	fstack_t value = POP(DS);
1527*0Sstevel@tonic-gate 
1528*0Sstevel@tonic-gate 	set_memory_watch(env, 2, size, addr, value);
1529*0Sstevel@tonic-gate }
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate static void
1532*0Sstevel@tonic-gate memory_watch_clear(fcode_env_t *env)
1533*0Sstevel@tonic-gate {
1534*0Sstevel@tonic-gate 	mw_valid = 0;
1535*0Sstevel@tonic-gate }
1536*0Sstevel@tonic-gate 
1537*0Sstevel@tonic-gate static void
1538*0Sstevel@tonic-gate vsearch(fcode_env_t *env)
1539*0Sstevel@tonic-gate {
1540*0Sstevel@tonic-gate 	fstack_t value;
1541*0Sstevel@tonic-gate 	int size = POP(DS);
1542*0Sstevel@tonic-gate 	fstack_t match_value = POP(DS);
1543*0Sstevel@tonic-gate 	uchar_t *toaddr = (uchar_t *)POP(DS);
1544*0Sstevel@tonic-gate 	uchar_t *fromaddr = (uchar_t *)POP(DS);
1545*0Sstevel@tonic-gate 
1546*0Sstevel@tonic-gate 	log_message(MSG_INFO, "%p to %p by %d looking for %llx\n", fromaddr,
1547*0Sstevel@tonic-gate 	    toaddr, size, (uint64_t)match_value);
1548*0Sstevel@tonic-gate 	for (; fromaddr < toaddr; fromaddr += size) {
1549*0Sstevel@tonic-gate 		switch (size) {
1550*0Sstevel@tonic-gate 		case 1: value = *((uint8_t *)fromaddr); break;
1551*0Sstevel@tonic-gate 		case 2: value = *((uint16_t *)fromaddr); break;
1552*0Sstevel@tonic-gate 		case 4: value = *((uint32_t *)fromaddr); break;
1553*0Sstevel@tonic-gate 		case 8: value = *((uint64_t *)fromaddr); break;
1554*0Sstevel@tonic-gate 		default:
1555*0Sstevel@tonic-gate 			log_message(MSG_INFO, "Invalid size: %d\n", size);
1556*0Sstevel@tonic-gate 			return;
1557*0Sstevel@tonic-gate 		}
1558*0Sstevel@tonic-gate 		if (value == match_value)
1559*0Sstevel@tonic-gate 			log_message(MSG_INFO, "%p\n", fromaddr);
1560*0Sstevel@tonic-gate 	}
1561*0Sstevel@tonic-gate }
1562*0Sstevel@tonic-gate 
1563*0Sstevel@tonic-gate #pragma init(_init)
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate static void
1566*0Sstevel@tonic-gate _init(void)
1567*0Sstevel@tonic-gate {
1568*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
1569*0Sstevel@tonic-gate 
1570*0Sstevel@tonic-gate 	ASSERT(env);
1571*0Sstevel@tonic-gate 	NOTICE;
1572*0Sstevel@tonic-gate 
1573*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"words",		words);
1574*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-words",		dump_words);
1575*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-dict",		dump_dictionary);
1576*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"dump-table",		dump_table);
1577*0Sstevel@tonic-gate 	FORTH(0,		"debugf",		debugf);
1578*0Sstevel@tonic-gate 	FORTH(0,		".debugf",		dot_debugf);
1579*0Sstevel@tonic-gate 	FORTH(0,		"set-debugf",		set_debugf);
1580*0Sstevel@tonic-gate 	FORTH(0,		"debugf?",		debugf_qmark);
1581*0Sstevel@tonic-gate 	FORTH(0,		"control",		control);
1582*0Sstevel@tonic-gate 	FORTH(0,		"dump",			dump);
1583*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"showstack",		show_stack);
1584*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"sifting",		sifting);
1585*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"ctrace",		ctrace);
1586*0Sstevel@tonic-gate 	FORTH(IMMEDIATE,	"ftrace",		ftrace);
1587*0Sstevel@tonic-gate 	FORTH(0,		"see",			see);
1588*0Sstevel@tonic-gate 	FORTH(0,		"(see)",		paren_see);
1589*0Sstevel@tonic-gate 	FORTH(0,		"base-addr",		base_addr);
1590*0Sstevel@tonic-gate 	FORTH(0,		"smatch",		smatch);
1591*0Sstevel@tonic-gate 	FORTH(0,		".calls",		dot_calls);
1592*0Sstevel@tonic-gate 	FORTH(0,		".pci-space",		dot_pci_space);
1593*0Sstevel@tonic-gate 	FORTH(0,		"(debug)",		paren_debug);
1594*0Sstevel@tonic-gate 	FORTH(0,		"debug",		debug);
1595*0Sstevel@tonic-gate 	FORTH(0,		".debug",		dot_debug);
1596*0Sstevel@tonic-gate 	FORTH(0,		"undebug",		undebug);
1597*0Sstevel@tonic-gate 	FORTH(0,		"memory-watch",		memory_watch);
1598*0Sstevel@tonic-gate 	FORTH(0,		"memory-watch-value",	memory_watch_value);
1599*0Sstevel@tonic-gate 	FORTH(0,		"memory-watch-clear",	memory_watch_clear);
1600*0Sstevel@tonic-gate 	FORTH(0,		"vsearch",		vsearch);
1601*0Sstevel@tonic-gate }
1602