xref: /onnv-gate/usr/src/lib/efcode/engine/tracing.c (revision 0:68f95e015346)
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 (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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 <dlfcn.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <fcode/private.h>
33*0Sstevel@tonic-gate #include <fcode/log.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #ifdef DEBUG
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static void (*trace_fn)(fcode_env_t *);
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate void
set_tracer(fcode_env_t * env,void (* tracer)(fcode_env_t *))40*0Sstevel@tonic-gate set_tracer(fcode_env_t *env, void (*tracer)(fcode_env_t *))
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate 	trace_fn = tracer;
43*0Sstevel@tonic-gate }
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate void
set_level(long lvl)46*0Sstevel@tonic-gate set_level(long lvl)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	long debug;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	debug = get_interpreter_debug_level();
51*0Sstevel@tonic-gate 	set_interpreter_debug_level(debug | lvl);
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate void
unset_level(long lvl)55*0Sstevel@tonic-gate unset_level(long lvl)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	long debug;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	debug = get_interpreter_debug_level();
60*0Sstevel@tonic-gate 	set_interpreter_debug_level(debug & ~lvl);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate void
enable_trace(fcode_env_t * env)64*0Sstevel@tonic-gate enable_trace(fcode_env_t *env)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	set_level(DEBUG_TRACING);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate void
enable_stack_trace(fcode_env_t * env)70*0Sstevel@tonic-gate enable_stack_trace(fcode_env_t *env)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	set_level(DEBUG_TRACE_STACK);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate void
disable_stack_trace(fcode_env_t * env)76*0Sstevel@tonic-gate disable_stack_trace(fcode_env_t *env)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	unset_level(DEBUG_TRACE_STACK);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate void
disable_trace(fcode_env_t * env)82*0Sstevel@tonic-gate disable_trace(fcode_env_t *env)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	unset_level(DEBUG_TRACING);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate void
call_trace(fcode_env_t * env)88*0Sstevel@tonic-gate call_trace(fcode_env_t *env)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	set_level(DEBUG_CALL_METHOD);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate void
no_call_trace(fcode_env_t * env)94*0Sstevel@tonic-gate no_call_trace(fcode_env_t *env)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	unset_level(DEBUG_CALL_METHOD);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate void
do_fclib_trace(fcode_env_t * env,void * fn)100*0Sstevel@tonic-gate do_fclib_trace(fcode_env_t *env, void *fn)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	void *address;
103*0Sstevel@tonic-gate 	Dl_info dlip;
104*0Sstevel@tonic-gate 	static char buf[80];
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	if (dladdr((void *) fn, &dlip)) {
107*0Sstevel@tonic-gate 		int offset;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 		address = dlsym(RTLD_DEFAULT, dlip.dli_sname);
110*0Sstevel@tonic-gate 		offset = ((char *) fn) - ((char *) address);
111*0Sstevel@tonic-gate 		if (offset == 0) {
112*0Sstevel@tonic-gate 			log_message(MSG_FC_DEBUG, "%s: tracing %s()\n",
113*0Sstevel@tonic-gate 			    dlip.dli_fname, dlip.dli_sname);
114*0Sstevel@tonic-gate 		} else {
115*0Sstevel@tonic-gate 			log_message(MSG_FC_DEBUG, "%s: tracing %s%s0x%x()\n",
116*0Sstevel@tonic-gate 			    dlip.dli_fname, dlip.dli_sname,
117*0Sstevel@tonic-gate 			    ((offset < 0) ? "-" : "+"),
118*0Sstevel@tonic-gate 			    ((offset < 0) ? -offset : offset));
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 	} else {
121*0Sstevel@tonic-gate 		log_message(MSG_FC_DEBUG, "do_fclib_trace: <Unknown> %p\n", fn);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 	if (trace_fn)
124*0Sstevel@tonic-gate 		trace_fn(env);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate void
output_step_message(fcode_env_t * env)128*0Sstevel@tonic-gate output_step_message(fcode_env_t *env)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	log_message(MSG_INFO, "Step keys: <space>, Continue, Forth, Go,"
131*0Sstevel@tonic-gate 	    " Help, Step, Quit\n");
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate void
enable_step(fcode_env_t * env)135*0Sstevel@tonic-gate enable_step(fcode_env_t *env)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	output_step_message(env);
138*0Sstevel@tonic-gate 	set_level(DEBUG_STEPPING);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate void
disable_step(fcode_env_t * env)143*0Sstevel@tonic-gate disable_step(fcode_env_t *env)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	unset_level(DEBUG_STEPPING);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate  * Output of state info is done elsewhere
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate int
do_fclib_step(fcode_env_t * env)152*0Sstevel@tonic-gate do_fclib_step(fcode_env_t *env)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	int c;
155*0Sstevel@tonic-gate 	fcode_env_t *new_env;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	for (; ; ) {
158*0Sstevel@tonic-gate 		c = getchar();
159*0Sstevel@tonic-gate 		if (c != '\n') {
160*0Sstevel@tonic-gate 			while (getchar() != '\n')
161*0Sstevel@tonic-gate 				;
162*0Sstevel@tonic-gate 		}
163*0Sstevel@tonic-gate 		switch (c) {
164*0Sstevel@tonic-gate 		case EOF:
165*0Sstevel@tonic-gate 		case 'q':
166*0Sstevel@tonic-gate 			unbug(env);
167*0Sstevel@tonic-gate 			IP = 0;
168*0Sstevel@tonic-gate 			return (1);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 		case 'c':
171*0Sstevel@tonic-gate 			debug_set_level(env,
172*0Sstevel@tonic-gate 			    DEBUG_EXEC_TRACE|DEBUG_EXEC_DUMP_DS);
173*0Sstevel@tonic-gate 			break;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		case 'g':
176*0Sstevel@tonic-gate 			unbug(env);
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		case 'f':
180*0Sstevel@tonic-gate 			unset_level(DEBUG_STEPPING);
181*0Sstevel@tonic-gate 			new_env = clone_environment(env, NULL);
182*0Sstevel@tonic-gate 			do_interact(new_env);
183*0Sstevel@tonic-gate 			destroy_environment(new_env);
184*0Sstevel@tonic-gate 			set_level(DEBUG_STEPPING);
185*0Sstevel@tonic-gate 			continue;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		case ' ':
188*0Sstevel@tonic-gate 		case '\n':
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		case 'd':	/* Unimplemented */
192*0Sstevel@tonic-gate 		case 'u':	/* Unimplemented */
193*0Sstevel@tonic-gate 		default:
194*0Sstevel@tonic-gate 			output_step_message(env);
195*0Sstevel@tonic-gate 			continue;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 		break;
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 	return (0);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate #pragma init(_init)
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate static void
_init(void)205*0Sstevel@tonic-gate _init(void)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	ASSERT(env);
210*0Sstevel@tonic-gate 	NOTICE;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	FORTH(0,	"stack-trace",		enable_stack_trace);
214*0Sstevel@tonic-gate 	FORTH(0,	"no-stack-trace",	disable_stack_trace);
215*0Sstevel@tonic-gate 	FORTH(0,	"trace-on",		enable_trace);
216*0Sstevel@tonic-gate 	FORTH(0,	"trace-off",		disable_trace);
217*0Sstevel@tonic-gate 	FORTH(0,	"call-trace",		call_trace);
218*0Sstevel@tonic-gate 	FORTH(0,	"no-call-trace",	no_call_trace);
219*0Sstevel@tonic-gate 	FORTH(0,	"step-on",		enable_step);
220*0Sstevel@tonic-gate 	FORTH(0,	"step-off",		disable_step);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate #endif
224