xref: /titanic_52/usr/src/lib/efcode/engine/tracing.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <dlfcn.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <fcode/private.h>
33*7c478bd9Sstevel@tonic-gate #include <fcode/log.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate static void (*trace_fn)(fcode_env_t *);
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate void
40*7c478bd9Sstevel@tonic-gate set_tracer(fcode_env_t *env, void (*tracer)(fcode_env_t *))
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	trace_fn = tracer;
43*7c478bd9Sstevel@tonic-gate }
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate void
46*7c478bd9Sstevel@tonic-gate set_level(long lvl)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	long debug;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	debug = get_interpreter_debug_level();
51*7c478bd9Sstevel@tonic-gate 	set_interpreter_debug_level(debug | lvl);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate void
55*7c478bd9Sstevel@tonic-gate unset_level(long lvl)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	long debug;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	debug = get_interpreter_debug_level();
60*7c478bd9Sstevel@tonic-gate 	set_interpreter_debug_level(debug & ~lvl);
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate void
64*7c478bd9Sstevel@tonic-gate enable_trace(fcode_env_t *env)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	set_level(DEBUG_TRACING);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate void
70*7c478bd9Sstevel@tonic-gate enable_stack_trace(fcode_env_t *env)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	set_level(DEBUG_TRACE_STACK);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate void
76*7c478bd9Sstevel@tonic-gate disable_stack_trace(fcode_env_t *env)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	unset_level(DEBUG_TRACE_STACK);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate void
82*7c478bd9Sstevel@tonic-gate disable_trace(fcode_env_t *env)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	unset_level(DEBUG_TRACING);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate void
88*7c478bd9Sstevel@tonic-gate call_trace(fcode_env_t *env)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	set_level(DEBUG_CALL_METHOD);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate void
94*7c478bd9Sstevel@tonic-gate no_call_trace(fcode_env_t *env)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	unset_level(DEBUG_CALL_METHOD);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate void
100*7c478bd9Sstevel@tonic-gate do_fclib_trace(fcode_env_t *env, void *fn)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	void *address;
103*7c478bd9Sstevel@tonic-gate 	Dl_info dlip;
104*7c478bd9Sstevel@tonic-gate 	static char buf[80];
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (dladdr((void *) fn, &dlip)) {
107*7c478bd9Sstevel@tonic-gate 		int offset;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		address = dlsym(RTLD_DEFAULT, dlip.dli_sname);
110*7c478bd9Sstevel@tonic-gate 		offset = ((char *) fn) - ((char *) address);
111*7c478bd9Sstevel@tonic-gate 		if (offset == 0) {
112*7c478bd9Sstevel@tonic-gate 			log_message(MSG_FC_DEBUG, "%s: tracing %s()\n",
113*7c478bd9Sstevel@tonic-gate 			    dlip.dli_fname, dlip.dli_sname);
114*7c478bd9Sstevel@tonic-gate 		} else {
115*7c478bd9Sstevel@tonic-gate 			log_message(MSG_FC_DEBUG, "%s: tracing %s%s0x%x()\n",
116*7c478bd9Sstevel@tonic-gate 			    dlip.dli_fname, dlip.dli_sname,
117*7c478bd9Sstevel@tonic-gate 			    ((offset < 0) ? "-" : "+"),
118*7c478bd9Sstevel@tonic-gate 			    ((offset < 0) ? -offset : offset));
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 	} else {
121*7c478bd9Sstevel@tonic-gate 		log_message(MSG_FC_DEBUG, "do_fclib_trace: <Unknown> %p\n", fn);
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 	if (trace_fn)
124*7c478bd9Sstevel@tonic-gate 		trace_fn(env);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate void
128*7c478bd9Sstevel@tonic-gate output_step_message(fcode_env_t *env)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	log_message(MSG_INFO, "Step keys: <space>, Continue, Forth, Go,"
131*7c478bd9Sstevel@tonic-gate 	    " Help, Step, Quit\n");
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate void
135*7c478bd9Sstevel@tonic-gate enable_step(fcode_env_t *env)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	output_step_message(env);
138*7c478bd9Sstevel@tonic-gate 	set_level(DEBUG_STEPPING);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate void
143*7c478bd9Sstevel@tonic-gate disable_step(fcode_env_t *env)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate 	unset_level(DEBUG_STEPPING);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate  * Output of state info is done elsewhere
150*7c478bd9Sstevel@tonic-gate  */
151*7c478bd9Sstevel@tonic-gate int
152*7c478bd9Sstevel@tonic-gate do_fclib_step(fcode_env_t *env)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	int c;
155*7c478bd9Sstevel@tonic-gate 	fcode_env_t *new_env;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	for (; ; ) {
158*7c478bd9Sstevel@tonic-gate 		c = getchar();
159*7c478bd9Sstevel@tonic-gate 		if (c != '\n') {
160*7c478bd9Sstevel@tonic-gate 			while (getchar() != '\n')
161*7c478bd9Sstevel@tonic-gate 				;
162*7c478bd9Sstevel@tonic-gate 		}
163*7c478bd9Sstevel@tonic-gate 		switch (c) {
164*7c478bd9Sstevel@tonic-gate 		case EOF:
165*7c478bd9Sstevel@tonic-gate 		case 'q':
166*7c478bd9Sstevel@tonic-gate 			unbug(env);
167*7c478bd9Sstevel@tonic-gate 			IP = 0;
168*7c478bd9Sstevel@tonic-gate 			return (1);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		case 'c':
171*7c478bd9Sstevel@tonic-gate 			debug_set_level(env,
172*7c478bd9Sstevel@tonic-gate 			    DEBUG_EXEC_TRACE|DEBUG_EXEC_DUMP_DS);
173*7c478bd9Sstevel@tonic-gate 			break;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 		case 'g':
176*7c478bd9Sstevel@tonic-gate 			unbug(env);
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 		case 'f':
180*7c478bd9Sstevel@tonic-gate 			unset_level(DEBUG_STEPPING);
181*7c478bd9Sstevel@tonic-gate 			new_env = clone_environment(env, NULL);
182*7c478bd9Sstevel@tonic-gate 			do_interact(new_env);
183*7c478bd9Sstevel@tonic-gate 			destroy_environment(new_env);
184*7c478bd9Sstevel@tonic-gate 			set_level(DEBUG_STEPPING);
185*7c478bd9Sstevel@tonic-gate 			continue;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		case ' ':
188*7c478bd9Sstevel@tonic-gate 		case '\n':
189*7c478bd9Sstevel@tonic-gate 			break;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 		case 'd':	/* Unimplemented */
192*7c478bd9Sstevel@tonic-gate 		case 'u':	/* Unimplemented */
193*7c478bd9Sstevel@tonic-gate 		default:
194*7c478bd9Sstevel@tonic-gate 			output_step_message(env);
195*7c478bd9Sstevel@tonic-gate 			continue;
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		break;
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	return (0);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate #pragma init(_init)
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate static void
205*7c478bd9Sstevel@tonic-gate _init(void)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	ASSERT(env);
210*7c478bd9Sstevel@tonic-gate 	NOTICE;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"stack-trace",		enable_stack_trace);
214*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"no-stack-trace",	disable_stack_trace);
215*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"trace-on",		enable_trace);
216*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"trace-off",		disable_trace);
217*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"call-trace",		call_trace);
218*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"no-call-trace",	no_call_trace);
219*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"step-on",		enable_step);
220*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"step-off",		disable_step);
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate #endif
224