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