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