ctf_lookup.c revision e083a0c2c99cea982dcf8e12ec3452cc575b5663
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * CDDL HEADER START
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * The contents of this file are subject to the terms of the
2f14f4556ae74ad40719bc9690534693abb71868vboxsync * Common Development and Distribution License, Version 1.0 only
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * (the "License"). You may not use this file except in compliance
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * with the License.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * See the License for the specific language governing permissions
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * and limitations under the License.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * When distributing Covered Code, include this CDDL HEADER in each
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * If applicable, add the following below this CDDL HEADER, with the
78a072e1b56619e3230735ae073668311232ec94vboxsync * fields enclosed by brackets "[]" replaced with your own identifying
78a072e1b56619e3230735ae073668311232ec94vboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
78a072e1b56619e3230735ae073668311232ec94vboxsync * CDDL HEADER END
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync * Use is subject to license terms.
78a072e1b56619e3230735ae073668311232ec94vboxsync#pragma ident "%Z%%M% %I% %E% SMI"
78a072e1b56619e3230735ae073668311232ec94vboxsync * Compare the given input string and length against a table of known C storage
b0c4bd49112a99f00ef48b7a8eae6fb310a62bdavboxsync * qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * do this quickly, we use a pre-computed Perfect Hash Function similar to the
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * technique originally described in the classic paper:
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * for the current set of qualifiers yields a unique H in the range [0 .. 20].
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * The hash can be modified when the keyword set changes as necessary. We also
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * store the length of each keyword and check it prior to the final strcmp().
e7184fff6d89903aed623860629a05047960ac2dvboxsync static const struct qual {
e7184fff6d89903aed623860629a05047960ac2dvboxsync const char *q_name;
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "volatile", 8 }, { "", 0 }, { "", 0 }, { "", 0 }, { "", 0 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "", 0 }, { "auto", 4 }, { "extern", 6 }, { "", 0 }, { "", 0 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "", 0 }, { "", 0 }, { "const", 5 }, { "register", 8 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (h >= 0 && h < sizeof (qhash) / sizeof (qhash[0]) &&
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync len == qp->q_len && strncmp(qp->q_name, s, qp->q_len) == 0);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Attempt to convert the given C type name into the corresponding CTF type ID.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * It is not possible to do complete and proper conversion of type names
78a072e1b56619e3230735ae073668311232ec94vboxsync * without implementing a more full-fledged parser, which is necessary to
78a072e1b56619e3230735ae073668311232ec94vboxsync * handle things like types that are function pointers to functions that
78a072e1b56619e3230735ae073668311232ec94vboxsync * have arguments that are function pointers, and fun stuff like that.
78a072e1b56619e3230735ae073668311232ec94vboxsync * Instead, this function implements a very simple conversion algorithm that
78a072e1b56619e3230735ae073668311232ec94vboxsync * finds the things that we actually care about: structs, unions, enums,
78a072e1b56619e3230735ae073668311232ec94vboxsync * integers, floats, typedefs, and pointers to any of these named types.
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_lookup_by_name(ctf_file_t *fp, const char *name)
78a072e1b56619e3230735ae073668311232ec94vboxsync const char *p, *q, *end;
08c4185261c17943cff6cc94522579696eeeb478vboxsync for (p = name, end = name + strlen(name); *p != '\0'; p = q) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync p++; /* skip leading ws */
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (*p == '*') {
08c4185261c17943cff6cc94522579696eeeb478vboxsync * Find a pointer to type by looking in fp->ctf_ptrtab.
08c4185261c17943cff6cc94522579696eeeb478vboxsync * If we can't find a pointer to the given type, see if
08c4185261c17943cff6cc94522579696eeeb478vboxsync * we can compute a pointer to the type resulting from
08c4185261c17943cff6cc94522579696eeeb478vboxsync * resolving the type down to its base type and use
08c4185261c17943cff6cc94522579696eeeb478vboxsync * that instead. This helps with cases where the CTF
08c4185261c17943cff6cc94522579696eeeb478vboxsync * data includes "struct foo *" but not "foo_t *" and
08c4185261c17943cff6cc94522579696eeeb478vboxsync * the user tries to access "foo_t *" in the debugger.
08c4185261c17943cff6cc94522579696eeeb478vboxsync continue; /* skip qualifier keyword */
08c4185261c17943cff6cc94522579696eeeb478vboxsync for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {
78a072e1b56619e3230735ae073668311232ec94vboxsync strncmp(p, lp->ctl_prefix, (size_t)(q - p)) == 0) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync continue; /* skip prefix and next ws */
78a072e1b56619e3230735ae073668311232ec94vboxsync q--; /* exclude trailing ws */
08c4185261c17943cff6cc94522579696eeeb478vboxsync (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)
08c4185261c17943cff6cc94522579696eeeb478vboxsync * Given a symbol table index, return the type of the data object described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync type = *(ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Return the pointer to the internal CTF type data corresponding to the
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * given type ID. If the ID is invalid, the function returns NULL.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * This function is not exported outside of the library.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if ((fp->ctf_flags & LCTF_CHILD) && CTF_TYPE_ISPARENT(type) &&
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync *fpp = fp; /* function returns ending CTF container */
78a072e1b56619e3230735ae073668311232ec94vboxsync * Given a symbol table index, return the info for the function described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
78a072e1b56619e3230735ae073668311232ec94vboxsync return (0);
78a072e1b56619e3230735ae073668311232ec94vboxsync * Given a symbol table index, return the arguments for the function described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)
78a072e1b56619e3230735ae073668311232ec94vboxsync * The argument data is two ushort_t's past the translation table
78a072e1b56619e3230735ae073668311232ec94vboxsync * offset: one for the function info, and one for the return type.
78a072e1b56619e3230735ae073668311232ec94vboxsync dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync return (0);