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 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 <strings.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <dt_xlator.h>
33*7c478bd9Sstevel@tonic-gate #include <dt_impl.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate dt_xlator_t *
36*7c478bd9Sstevel@tonic-gate dt_xlator_create(dtrace_hdl_t *dtp,
37*7c478bd9Sstevel@tonic-gate     const dtrace_typeinfo_t *src, const dtrace_typeinfo_t *dst,
38*7c478bd9Sstevel@tonic-gate     const char *name, dt_node_t *members, dt_node_t *nodes)
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp = malloc(sizeof (dt_xlator_t));
41*7c478bd9Sstevel@tonic-gate 	dtrace_typeinfo_t ptr = *dst;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 	if (dxp == NULL)
44*7c478bd9Sstevel@tonic-gate 		return (NULL);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	if (dt_type_pointer(&ptr) == -1) {
47*7c478bd9Sstevel@tonic-gate 		ptr.dtt_ctfp = NULL;
48*7c478bd9Sstevel@tonic-gate 		ptr.dtt_type = CTF_ERR;
49*7c478bd9Sstevel@tonic-gate 	}
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	bzero(dxp, sizeof (dt_xlator_t));
52*7c478bd9Sstevel@tonic-gate 	dt_list_append(&dtp->dt_xlators, dxp);
53*7c478bd9Sstevel@tonic-gate 	dxp->dx_locals = dt_idhash_create("translator", NULL, 0, 0);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (dxp->dx_locals == NULL)
56*7c478bd9Sstevel@tonic-gate 		goto err; /* no memory for identifier hash */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	dxp->dx_ident = dt_idhash_insert(dxp->dx_locals, name,
59*7c478bd9Sstevel@tonic-gate 	    DT_IDENT_SCALAR, DT_IDFLG_REF, 0, _dtrace_defattr, 0,
60*7c478bd9Sstevel@tonic-gate 	    &dt_idops_thaw, NULL, dtp->dt_gen);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if (dxp->dx_ident == NULL)
63*7c478bd9Sstevel@tonic-gate 		goto err; /* no memory for identifier */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	dxp->dx_ident->di_ctfp = src->dtt_ctfp;
66*7c478bd9Sstevel@tonic-gate 	dxp->dx_ident->di_type = src->dtt_type;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_name = "translator";
69*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_kind = DT_IDENT_XLSOU;
70*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_flags = DT_IDFLG_REF;
71*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_attr = _dtrace_defattr;
72*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_ops = &dt_idops_thaw;
73*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_data = dxp;
74*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_ctfp = dst->dtt_ctfp;
75*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_type = dst->dtt_type;
76*7c478bd9Sstevel@tonic-gate 	dxp->dx_souid.di_gen = dtp->dt_gen;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_name = "translator";
79*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_kind = DT_IDENT_XLPTR;
80*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_flags = DT_IDFLG_REF;
81*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_attr = _dtrace_defattr;
82*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_ops = &dt_idops_thaw;
83*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_data = dxp;
84*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_ctfp = ptr.dtt_ctfp;
85*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_type = ptr.dtt_type;
86*7c478bd9Sstevel@tonic-gate 	dxp->dx_ptrid.di_gen = dtp->dt_gen;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/*
89*7c478bd9Sstevel@tonic-gate 	 * If a deferred pragma is pending on the keyword "translator", run all
90*7c478bd9Sstevel@tonic-gate 	 * the deferred pragmas on dx_souid and then copy results to dx_ptrid.
91*7c478bd9Sstevel@tonic-gate 	 * See the code in dt_pragma.c for details on deferred ident pragmas.
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer != NULL && yypcb->pcb_pragmas != NULL &&
94*7c478bd9Sstevel@tonic-gate 	    dt_idhash_lookup(yypcb->pcb_pragmas, "translator") != NULL) {
95*7c478bd9Sstevel@tonic-gate 		dtp->dt_globals->dh_defer(dtp->dt_globals, &dxp->dx_souid);
96*7c478bd9Sstevel@tonic-gate 		dxp->dx_ptrid.di_attr = dxp->dx_souid.di_attr;
97*7c478bd9Sstevel@tonic-gate 		dxp->dx_ptrid.di_vers = dxp->dx_souid.di_vers;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	dxp->dx_src_ctfp = src->dtt_ctfp;
101*7c478bd9Sstevel@tonic-gate 	dxp->dx_src_type = src->dtt_type;
102*7c478bd9Sstevel@tonic-gate 	dxp->dx_src_base = ctf_type_resolve(src->dtt_ctfp, src->dtt_type);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	dxp->dx_dst_ctfp = dst->dtt_ctfp;
105*7c478bd9Sstevel@tonic-gate 	dxp->dx_dst_type = dst->dtt_type;
106*7c478bd9Sstevel@tonic-gate 	dxp->dx_dst_base = ctf_type_resolve(dst->dtt_ctfp, dst->dtt_type);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	dxp->dx_members = members;
109*7c478bd9Sstevel@tonic-gate 	dxp->dx_nodes = nodes;
110*7c478bd9Sstevel@tonic-gate 	dxp->dx_gen = dtp->dt_gen;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	return (dxp);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate err:
115*7c478bd9Sstevel@tonic-gate 	dt_xlator_destroy(dtp, dxp);
116*7c478bd9Sstevel@tonic-gate 	return (NULL);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate void
120*7c478bd9Sstevel@tonic-gate dt_xlator_destroy(dtrace_hdl_t *dtp, dt_xlator_t *dxp)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	dt_node_link_free(&dxp->dx_nodes);
123*7c478bd9Sstevel@tonic-gate 	dt_idhash_destroy(dxp->dx_locals);
124*7c478bd9Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_xlators, dxp);
125*7c478bd9Sstevel@tonic-gate 	free(dxp);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate dt_xlator_t *
129*7c478bd9Sstevel@tonic-gate dt_xlator_lookup(dtrace_hdl_t *dtp, dt_node_t *src, dt_node_t *dst, int flag)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	ctf_file_t *src_ctfp = src->dn_ctfp;
132*7c478bd9Sstevel@tonic-gate 	ctf_id_t src_type = src->dn_type;
133*7c478bd9Sstevel@tonic-gate 	ctf_id_t src_base = ctf_type_resolve(src_ctfp, src_type);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	ctf_file_t *dst_ctfp = dst->dn_ctfp;
136*7c478bd9Sstevel@tonic-gate 	ctf_id_t dst_type = dst->dn_type;
137*7c478bd9Sstevel@tonic-gate 	ctf_id_t dst_base = ctf_type_resolve(dst_ctfp, dst_type);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	int ptr = ctf_type_kind(dst_ctfp, dst_base) == CTF_K_POINTER;
140*7c478bd9Sstevel@tonic-gate 	dt_node_t xn = { 0 };
141*7c478bd9Sstevel@tonic-gate 	dt_xlator_t *dxp;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (src_base == CTF_ERR || dst_base == CTF_ERR)
144*7c478bd9Sstevel@tonic-gate 		return (NULL); /* fail if these are unresolvable types */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/*
147*7c478bd9Sstevel@tonic-gate 	 * Translators are always defined using a struct or union type, so if
148*7c478bd9Sstevel@tonic-gate 	 * we are attempting to translate to type "T *", we internally look
149*7c478bd9Sstevel@tonic-gate 	 * for a translation to type "T" by following the pointer reference.
150*7c478bd9Sstevel@tonic-gate 	 */
151*7c478bd9Sstevel@tonic-gate 	if (ptr) {
152*7c478bd9Sstevel@tonic-gate 		dst_type = ctf_type_reference(dst_ctfp, dst_type);
153*7c478bd9Sstevel@tonic-gate 		dst_base = ctf_type_resolve(dst_ctfp, dst_type);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * In order to find a matching translator, we iterate over the set of
158*7c478bd9Sstevel@tonic-gate 	 * available translators in three passes.  First, we look for a
159*7c478bd9Sstevel@tonic-gate 	 * translation from the exact source type to the resolved destination.
160*7c478bd9Sstevel@tonic-gate 	 * Second, we look for a translation from the resolved source type to
161*7c478bd9Sstevel@tonic-gate 	 * the resolved destination.  Third, we look for a translation from a
162*7c478bd9Sstevel@tonic-gate 	 * compatible source type (using the same rules as parameter formals)
163*7c478bd9Sstevel@tonic-gate 	 * to the resolved destination.  If all passes fail, return NULL.
164*7c478bd9Sstevel@tonic-gate 	 */
165*7c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
166*7c478bd9Sstevel@tonic-gate 	    dxp = dt_list_next(dxp)) {
167*7c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_type,
168*7c478bd9Sstevel@tonic-gate 		    src_ctfp, src_type) &&
169*7c478bd9Sstevel@tonic-gate 		    ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
170*7c478bd9Sstevel@tonic-gate 		    dst_ctfp, dst_base))
171*7c478bd9Sstevel@tonic-gate 			goto out;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (flag == DT_XLATE_EXACT)
175*7c478bd9Sstevel@tonic-gate 		goto out; /* skip remaining passes if exact match required */
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
178*7c478bd9Sstevel@tonic-gate 	    dxp = dt_list_next(dxp)) {
179*7c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(dxp->dx_src_ctfp, dxp->dx_src_base,
180*7c478bd9Sstevel@tonic-gate 		    src_ctfp, src_type) &&
181*7c478bd9Sstevel@tonic-gate 		    ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
182*7c478bd9Sstevel@tonic-gate 		    dst_ctfp, dst_base))
183*7c478bd9Sstevel@tonic-gate 			goto out;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL;
187*7c478bd9Sstevel@tonic-gate 	    dxp = dt_list_next(dxp)) {
188*7c478bd9Sstevel@tonic-gate 		dt_node_type_assign(&xn, dxp->dx_src_ctfp, dxp->dx_src_type);
189*7c478bd9Sstevel@tonic-gate 		if (ctf_type_compat(dxp->dx_dst_ctfp, dxp->dx_dst_base,
190*7c478bd9Sstevel@tonic-gate 		    dst_ctfp, dst_base) && dt_node_is_argcompat(src, &xn))
191*7c478bd9Sstevel@tonic-gate 			goto out;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate out:
195*7c478bd9Sstevel@tonic-gate 	if (ptr && dxp != NULL && dxp->dx_ptrid.di_type == CTF_ERR)
196*7c478bd9Sstevel@tonic-gate 		return (NULL); /* no translation available to pointer type */
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	return (dxp);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate dt_ident_t *
202*7c478bd9Sstevel@tonic-gate dt_xlator_ident(dt_xlator_t *dxp, ctf_file_t *ctfp, ctf_id_t type)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	if (ctf_type_kind(ctfp, ctf_type_resolve(ctfp, type)) == CTF_K_POINTER)
205*7c478bd9Sstevel@tonic-gate 		return (&dxp->dx_ptrid);
206*7c478bd9Sstevel@tonic-gate 	else
207*7c478bd9Sstevel@tonic-gate 		return (&dxp->dx_souid);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate dt_node_t *
211*7c478bd9Sstevel@tonic-gate dt_xlator_member(dt_xlator_t *dxp, const char *name)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list) {
216*7c478bd9Sstevel@tonic-gate 		if (strcmp(dnp->dn_membname, name) == 0)
217*7c478bd9Sstevel@tonic-gate 			return (dnp);
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	return (NULL);
221*7c478bd9Sstevel@tonic-gate }
222