xref: /titanic_51/usr/src/common/ctf/ctf_lookup.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 <sys/sysmacros.h>
30 #include <ctf_impl.h>
31 
32 /*
33  * Compare the given input string and length against a table of known C storage
34  * qualifier keywords.  We just ignore these in ctf_lookup_by_name, below.
35  */
36 static int
37 isqualifier(const char *s, size_t len)
38 {
39 	static const struct qual {
40 		const char *q_name;
41 		size_t q_len;
42 	} q[] = {
43 		{ "auto", 4 },
44 		{ "const", 5 },
45 		{ "extern", 6 },
46 		{ "register", 8 },
47 		{ "restrict", 8 },
48 		{ "_Restrict", 9 },
49 		{ "static", 6 },
50 		{ "volatile", 8 },
51 		{ NULL, 0 }
52 	};
53 
54 	int i;
55 
56 	for (i = 0; q[i].q_name != NULL; i++) {
57 		if (len == q[i].q_len && strncmp(s, q[i].q_name, len) == 0)
58 			return (1);
59 	}
60 
61 	return (0);
62 }
63 
64 /*
65  * Attempt to convert the given C type name into the corresponding CTF type ID.
66  * It is not possible to do complete and proper conversion of type names
67  * without implementing a more full-fledged parser, which is necessary to
68  * handle things like types that are function pointers to functions that
69  * have arguments that are function pointers, and fun stuff like that.
70  * Instead, this function implements a very simple conversion algorithm that
71  * finds the things that we actually care about: structs, unions, enums,
72  * integers, floats, typedefs, and pointers to any of these named types.
73  */
74 ctf_id_t
75 ctf_lookup_by_name(ctf_file_t *fp, const char *name)
76 {
77 	static const char delimiters[] = " \t\n\r\v\f*";
78 
79 	const ctf_lookup_t *lp;
80 	const ctf_helem_t *hp;
81 	const char *p, *q, *end;
82 	ctf_id_t type = 0;
83 	ctf_id_t ntype, ptype;
84 
85 	if (name == NULL)
86 		return (ctf_set_errno(fp, EINVAL));
87 
88 	for (p = name, end = name + strlen(name); *p != '\0'; p = q) {
89 		while (isspace(*p))
90 			p++; /* skip leading ws */
91 
92 		if (p == end)
93 			break;
94 
95 		if ((q = strpbrk(p + 1, delimiters)) == NULL)
96 			q = end; /* compare until end */
97 
98 		if (*p == '*') {
99 			/*
100 			 * Find a pointer to type by looking in fp->ctf_ptrtab.
101 			 * If we can't find a pointer to the given type, see if
102 			 * we can compute a pointer to the type resulting from
103 			 * resolving the type down to its base type and use
104 			 * that instead.  This helps with cases where the CTF
105 			 * data includes "struct foo *" but not "foo_t *" and
106 			 * the user tries to access "foo_t *" in the debugger.
107 			 */
108 			ntype = fp->ctf_ptrtab[CTF_TYPE_TO_INDEX(type)];
109 			if (ntype == 0) {
110 				ntype = ctf_type_resolve(fp, type);
111 				if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[
112 				    CTF_TYPE_TO_INDEX(ntype)]) == 0) {
113 					(void) ctf_set_errno(fp, ECTF_NOTYPE);
114 					goto err;
115 				}
116 			}
117 
118 			type = CTF_INDEX_TO_TYPE(ntype,
119 			    (fp->ctf_flags & LCTF_CHILD));
120 
121 			q = p + 1;
122 			continue;
123 		}
124 
125 		if (isqualifier(p, (size_t)(q - p)))
126 			continue; /* skip qualifier keyword */
127 
128 		for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {
129 			if (lp->ctl_prefix[0] == '\0' ||
130 			    strncmp(p, lp->ctl_prefix, (size_t)(q - p)) == 0) {
131 				for (p += lp->ctl_len; isspace(*p); p++)
132 					continue; /* skip prefix and next ws */
133 
134 				if ((q = strchr(p, '*')) == NULL)
135 					q = end;  /* compare until end */
136 
137 				while (isspace(q[-1]))
138 					q--;	  /* exclude trailing ws */
139 
140 				if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p,
141 				    (size_t)(q - p))) == NULL) {
142 					(void) ctf_set_errno(fp, ECTF_NOTYPE);
143 					goto err;
144 				}
145 
146 				type = hp->h_type;
147 				break;
148 			}
149 		}
150 
151 		if (lp->ctl_prefix == NULL) {
152 			(void) ctf_set_errno(fp, ECTF_NOTYPE);
153 			goto err;
154 		}
155 	}
156 
157 	if (*p != '\0' || type == 0)
158 		return (ctf_set_errno(fp, ECTF_SYNTAX));
159 
160 	return (type);
161 
162 err:
163 	if (fp->ctf_parent != NULL &&
164 	    (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)
165 		return (ptype);
166 
167 	return (CTF_ERR);
168 }
169 
170 /*
171  * Given a symbol table index, return the type of the data object described
172  * by the corresponding entry in the symbol table.
173  */
174 ctf_id_t
175 ctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)
176 {
177 	const ctf_sect_t *sp = &fp->ctf_symtab;
178 	ctf_id_t type;
179 
180 	if (sp->cts_data == NULL)
181 		return (ctf_set_errno(fp, ECTF_NOSYMTAB));
182 
183 	if (symidx >= fp->ctf_nsyms)
184 		return (ctf_set_errno(fp, EINVAL));
185 
186 	if (sp->cts_entsize == sizeof (Elf32_Sym)) {
187 		const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
188 		if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
189 			return (ctf_set_errno(fp, ECTF_NOTDATA));
190 	} else {
191 		const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
192 		if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
193 			return (ctf_set_errno(fp, ECTF_NOTDATA));
194 	}
195 
196 	if (fp->ctf_sxlate[symidx] == -1u)
197 		return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
198 
199 	type = *(ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
200 	if (type == 0)
201 		return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
202 
203 	return (type);
204 }
205 
206 /*
207  * Return the pointer to the internal CTF type data corresponding to the
208  * given type ID.  If the ID is invalid, the function returns NULL.
209  * This function is not exported outside of the library.
210  */
211 const ctf_type_t *
212 ctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type)
213 {
214 	ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */
215 
216 	if ((fp->ctf_flags & LCTF_CHILD) && CTF_TYPE_ISPARENT(type) &&
217 	    (fp = fp->ctf_parent) == NULL) {
218 		(void) ctf_set_errno(*fpp, ECTF_NOPARENT);
219 		return (NULL);
220 	}
221 
222 	type = CTF_TYPE_TO_INDEX(type);
223 	if (type > 0 && type <= fp->ctf_typemax) {
224 		*fpp = fp; /* function returns ending CTF container */
225 		return (LCTF_INDEX_TO_TYPEPTR(fp, type));
226 	}
227 
228 	(void) ctf_set_errno(fp, ECTF_BADID);
229 	return (NULL);
230 }
231 
232 /*
233  * Given a symbol table index, return the info for the function described
234  * by the corresponding entry in the symbol table.
235  */
236 int
237 ctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)
238 {
239 	const ctf_sect_t *sp = &fp->ctf_symtab;
240 	const ushort_t *dp;
241 	ushort_t info, kind, n;
242 
243 	if (sp->cts_data == NULL)
244 		return (ctf_set_errno(fp, ECTF_NOSYMTAB));
245 
246 	if (symidx >= fp->ctf_nsyms)
247 		return (ctf_set_errno(fp, EINVAL));
248 
249 	if (sp->cts_entsize == sizeof (Elf32_Sym)) {
250 		const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
251 		if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
252 			return (ctf_set_errno(fp, ECTF_NOTFUNC));
253 	} else {
254 		const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
255 		if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
256 			return (ctf_set_errno(fp, ECTF_NOTFUNC));
257 	}
258 
259 	if (fp->ctf_sxlate[symidx] == -1u)
260 		return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
261 
262 	dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
263 
264 	info = *dp++;
265 	kind = LCTF_INFO_KIND(fp, info);
266 	n = LCTF_INFO_VLEN(fp, info);
267 
268 	if (kind == CTF_K_UNKNOWN && n == 0)
269 		return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
270 
271 	if (kind != CTF_K_FUNCTION)
272 		return (ctf_set_errno(fp, ECTF_CORRUPT));
273 
274 	fip->ctc_return = *dp++;
275 	fip->ctc_argc = n;
276 	fip->ctc_flags = 0;
277 
278 	if (n != 0 && dp[n - 1] == 0) {
279 		fip->ctc_flags |= CTF_FUNC_VARARG;
280 		fip->ctc_argc--;
281 	}
282 
283 	return (0);
284 }
285 
286 /*
287  * Given a symbol table index, return the arguments for the function described
288  * by the corresponding entry in the symbol table.
289  */
290 int
291 ctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)
292 {
293 	const ushort_t *dp;
294 	ctf_funcinfo_t f;
295 
296 	if (ctf_func_info(fp, symidx, &f) == CTF_ERR)
297 		return (CTF_ERR); /* errno is set for us */
298 
299 	/*
300 	 * The argument data is two ushort_t's past the translation table
301 	 * offset: one for the function info, and one for the return type.
302 	 */
303 	dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;
304 
305 	for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)
306 		*argv++ = *dp++;
307 
308 	return (0);
309 }
310