Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_ident.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24* Copyright (c) 2013 by Delphix. All rights reserved.25* Copyright (c) 2013 Joyent, Inc. All rights reserved.26*/2728#pragma ident "%Z%%M% %I% %E% SMI"2930#ifdef illumos31#include <sys/sysmacros.h>32#endif33#include <strings.h>34#include <stdlib.h>35#ifdef illumos36#include <alloca.h>37#endif38#include <assert.h>39#include <errno.h>40#include <ctype.h>41#ifdef illumos42#include <sys/procfs_isa.h>43#endif44#include <limits.h>4546#include <dt_ident.h>47#include <dt_parser.h>48#include <dt_provider.h>49#include <dt_strtab.h>50#include <dt_impl.h>5152/*53* Common code for cooking an identifier that uses a typed signature list (we54* use this for associative arrays and functions). If the argument list is55* of the same length and types, then return the return type. Otherwise56* print an appropriate compiler error message and abort the compile.57*/58static void59dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,60int argc, dt_node_t *args, const char *prefix, const char *suffix)61{62dt_idsig_t *isp = idp->di_data;63int i, compat, mismatch, arglimit, iskey;6465char n1[DT_TYPE_NAMELEN];66char n2[DT_TYPE_NAMELEN];6768iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG;6970if (isp->dis_varargs >= 0) {71mismatch = argc < isp->dis_varargs;72arglimit = isp->dis_varargs;73} else if (isp->dis_optargs >= 0) {74mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);75arglimit = argc;76} else {77mismatch = argc != isp->dis_argc;78arglimit = isp->dis_argc;79}8081if (mismatch) {82xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s"83"passed, %s%d expected\n", prefix, idp->di_name, suffix,84argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ",85isp->dis_optargs >= 0 ? "at least " : "",86isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);87}8889for (i = 0; i < arglimit; i++, args = args->dn_list) {90if (isp->dis_args[i].dn_ctfp != NULL)91compat = dt_node_is_argcompat(&isp->dis_args[i], args);92else93compat = 1; /* "@" matches any type */9495if (!compat) {96xyerror(D_PROTO_ARG,97"%s%s%s %s #%d is incompatible with "98"prototype:\n\tprototype: %s\n\t%9s: %s\n",99prefix, idp->di_name, suffix,100iskey ? "key" : "argument", i + 1,101dt_node_type_name(&isp->dis_args[i], n1,102sizeof (n1)),103iskey ? "key" : "argument",104dt_node_type_name(args, n2, sizeof (n2)));105}106}107108dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);109}110111/*112* Cook an associative array identifier. If this is the first time we are113* cooking this array, create its signature based on the argument list.114* Otherwise validate the argument list against the existing signature.115*/116static void117dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)118{119if (idp->di_data == NULL) {120dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));121char n[DT_TYPE_NAMELEN];122int i;123124if (isp == NULL)125longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);126127isp->dis_varargs = -1;128isp->dis_optargs = -1;129isp->dis_argc = argc;130isp->dis_args = NULL;131isp->dis_auxinfo = 0;132133if (argc != 0 && (isp->dis_args = calloc(argc,134sizeof (dt_node_t))) == NULL) {135idp->di_data = NULL;136free(isp);137longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);138}139140/*141* If this identifier has not been explicitly declared earlier,142* set the identifier's base type to be our special type <DYN>.143* If this ident is an aggregation, it will remain as is. If144* this ident is an associative array, it will be reassigned145* based on the result type of the first assignment statement.146*/147if (!(idp->di_flags & DT_IDFLG_DECL)) {148idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);149idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);150}151152for (i = 0; i < argc; i++, args = args->dn_list) {153if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {154xyerror(D_KEY_TYPE, "%s expression may not be "155"used as %s index: key #%d\n",156dt_node_type_name(args, n, sizeof (n)),157dt_idkind_name(idp->di_kind), i + 1);158}159160dt_node_type_propagate(args, &isp->dis_args[i]);161isp->dis_args[i].dn_list = &isp->dis_args[i + 1];162}163164if (argc != 0)165isp->dis_args[argc - 1].dn_list = NULL;166167dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);168169} else {170dt_idcook_sign(dnp, idp, argc, args,171idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");172}173}174175/*176* Cook a function call. If this is the first time we are cooking this177* identifier, create its type signature based on predefined prototype stored178* in di_iarg. We then validate the argument list against this signature.179*/180static void181dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)182{183if (idp->di_data == NULL) {184dtrace_hdl_t *dtp = yypcb->pcb_hdl;185dtrace_typeinfo_t dtt;186dt_idsig_t *isp;187char *s, *p1, *p2;188int i = 0;189190assert(idp->di_iarg != NULL);191s = alloca(strlen(idp->di_iarg) + 1);192(void) strcpy(s, idp->di_iarg);193194if ((p2 = strrchr(s, ')')) != NULL)195*p2 = '\0'; /* mark end of parameter list string */196197if ((p1 = strchr(s, '(')) != NULL)198*p1++ = '\0'; /* mark end of return type string */199200if (p1 == NULL || p2 == NULL) {201xyerror(D_UNKNOWN, "internal error: malformed entry "202"for built-in function %s\n", idp->di_name);203}204205for (p2 = p1; *p2 != '\0'; p2++) {206if (!isspace(*p2)) {207i++;208break;209}210}211212for (p2 = strchr(p2, ','); p2 != NULL; i++) {213p2++;214p2 = strchr(p2, ',');215}216217/*218* We first allocate a new ident signature structure with the219* appropriate number of argument entries, and then look up220* the return type and store its CTF data in di_ctfp/type.221*/222if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)223longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);224225isp->dis_varargs = -1;226isp->dis_optargs = -1;227isp->dis_argc = i;228isp->dis_args = NULL;229isp->dis_auxinfo = 0;230231if (i != 0 && (isp->dis_args = calloc(i,232sizeof (dt_node_t))) == NULL) {233idp->di_data = NULL;234free(isp);235longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);236}237238if (dt_type_lookup(s, &dtt) == -1) {239xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"240" %s\n", idp->di_name, s,241dtrace_errmsg(dtp, dtrace_errno(dtp)));242}243244if (idp->di_kind == DT_IDENT_AGGFUNC) {245idp->di_ctfp = DT_DYN_CTFP(dtp);246idp->di_type = DT_DYN_TYPE(dtp);247} else {248idp->di_ctfp = dtt.dtt_ctfp;249idp->di_type = dtt.dtt_type;250}251252/*253* For each comma-delimited parameter in the prototype string,254* we look up the corresponding type and store its CTF data in255* the corresponding location in dis_args[]. We also recognize256* the special type string "@" to indicate that the specified257* parameter may be a D expression of *any* type (represented258* as a dis_args[] element with ctfp = NULL, type == CTF_ERR).259* If a varargs "..." is present, we record the argument index260* in dis_varargs for the benefit of dt_idcook_sign(), above.261* If the type of an argument is enclosed in square brackets262* (e.g. "[int]"), the argument is considered optional: the263* argument may be absent, but if it is present, it must be of264* the specified type. Note that varargs may not optional,265* optional arguments may not follow varargs, and non-optional266* arguments may not follow optional arguments.267*/268for (i = 0; i < isp->dis_argc; i++, p1 = p2) {269while (isspace(*p1))270p1++; /* skip leading whitespace */271272if ((p2 = strchr(p1, ',')) == NULL)273p2 = p1 + strlen(p1);274else275*p2++ = '\0';276277if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {278isp->dis_args[i].dn_ctfp = NULL;279isp->dis_args[i].dn_type = CTF_ERR;280if (*p1 == '.')281isp->dis_varargs = i;282continue;283}284285if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {286if (isp->dis_varargs != -1) {287xyerror(D_UNKNOWN, "optional arg#%d "288"may not follow variable arg#%d\n",289i + 1, isp->dis_varargs + 1);290}291292if (isp->dis_optargs == -1)293isp->dis_optargs = i;294295p1[strlen(p1) - 1] = '\0';296p1++;297} else if (isp->dis_optargs != -1) {298xyerror(D_UNKNOWN, "required arg#%d may not "299"follow optional arg#%d\n", i + 1,300isp->dis_optargs + 1);301}302303if (dt_type_lookup(p1, &dtt) == -1) {304xyerror(D_UNKNOWN, "failed to resolve type of "305"%s arg#%d (%s): %s\n", idp->di_name, i + 1,306p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));307}308309dt_node_type_assign(&isp->dis_args[i],310dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);311}312}313314dt_idcook_sign(dnp, idp, argc, args, "", "( )");315}316317/*318* Cook a reference to the dynamically typed args[] array. We verify that the319* reference is using a single integer constant, and then construct a new ident320* representing the appropriate type or translation specifically for this node.321*/322static void323dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)324{325dtrace_hdl_t *dtp = yypcb->pcb_hdl;326dt_probe_t *prp = yypcb->pcb_probe;327328dt_node_t tag, *nnp, *xnp;329dt_xlator_t *dxp;330dt_ident_t *xidp;331332char n1[DT_TYPE_NAMELEN];333char n2[DT_TYPE_NAMELEN];334335if (argc != 1) {336xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"337"passed, 1 expected\n", idp->di_name, argc,338argc == 1 ? " " : "s ");339}340341if (ap->dn_kind != DT_NODE_INT) {342xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "343"prototype:\n\tprototype: %s\n\t argument: %s\n",344idp->di_name, "integer constant",345dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));346}347348if (yypcb->pcb_pdesc == NULL) {349xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "350"of a probe clause\n", idp->di_name);351}352353if (prp == NULL) {354xyerror(D_ARGS_MULTI,355"%s[ ] may not be referenced because probe description %s "356"matches an unstable set of probes\n", idp->di_name,357dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));358}359360if (ap->dn_value >= prp->pr_argc) {361xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",362(longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,363n1, sizeof (n1)), idp->di_name);364}365366/*367* Look up the native and translated argument types for the probe.368* If no translation is needed, these will be the same underlying node.369* If translation is needed, look up the appropriate translator. Once370* we have the appropriate node, create a new dt_ident_t for this node,371* assign it the appropriate attributes, and set the type of 'dnp'.372*/373xnp = prp->pr_xargv[ap->dn_value];374nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];375376if (xnp->dn_type == CTF_ERR) {377xyerror(D_ARGS_TYPE, "failed to resolve translated type for "378"%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);379}380381if (nnp->dn_type == CTF_ERR) {382xyerror(D_ARGS_TYPE, "failed to resolve native type for "383"%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);384}385386if (dtp->dt_xlatemode == DT_XL_STATIC && (387nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {388dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,389idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,390idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);391392if (dnp->dn_ident == NULL)393longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);394395dt_node_type_assign(dnp,396prp->pr_argv[ap->dn_value].dtt_ctfp,397prp->pr_argv[ap->dn_value].dtt_type,398prp->pr_argv[ap->dn_value].dtt_flags & DTT_FL_USER ?399B_TRUE : B_FALSE);400401} else if ((dxp = dt_xlator_lookup(dtp,402nnp, xnp, DT_XLATE_FUZZY)) != NULL || (403dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),404xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {405406xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);407408dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,409xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,410idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);411412if (dnp->dn_ident == NULL)413longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);414415if (dt_xlator_dynamic(dxp))416dxp->dx_arg = (int)ap->dn_value;417418/*419* Propagate relevant members from the translator's internal420* dt_ident_t. This code must be kept in sync with the state421* that is initialized for idents in dt_xlator_create().422*/423dnp->dn_ident->di_data = xidp->di_data;424dnp->dn_ident->di_ctfp = xidp->di_ctfp;425dnp->dn_ident->di_type = xidp->di_type;426427dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),428B_FALSE);429430} else {431xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "432"is not defined\n", idp->di_name, (longlong_t)ap->dn_value,433dt_node_type_name(nnp, n1, sizeof (n1)),434dt_node_type_name(xnp, n2, sizeof (n2)));435}436437assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);438assert(dnp->dn_ident->di_id == idp->di_id);439}440441static void442dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)443{444dtrace_typeinfo_t dtt;445dtrace_hdl_t *dtp = yypcb->pcb_hdl;446char n[DT_TYPE_NAMELEN];447448if (argc != 1) {449xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"450"passed, 1 expected\n", idp->di_name,451argc, argc == 1 ? " " : "s ");452}453454if (ap->dn_kind != DT_NODE_INT) {455xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "456"prototype:\n\tprototype: %s\n\t argument: %s\n",457idp->di_name, "integer constant",458dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));459}460461if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {462xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",463(longlong_t)ap->dn_value, idp->di_name);464}465466if (dt_type_lookup("uint64_t", &dtt) == -1) {467xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",468idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));469}470471idp->di_ctfp = dtt.dtt_ctfp;472idp->di_type = dtt.dtt_type;473474dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);475}476477/*ARGSUSED*/478static void479dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)480{481if (idp->di_type == CTF_ERR) {482dtrace_hdl_t *dtp = yypcb->pcb_hdl;483dtrace_typeinfo_t dtt;484485if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {486xyerror(D_UNKNOWN,487"failed to resolve type %s for identifier %s: %s\n",488(const char *)idp->di_iarg, idp->di_name,489dtrace_errmsg(dtp, dtrace_errno(dtp)));490}491492idp->di_ctfp = dtt.dtt_ctfp;493idp->di_type = dtt.dtt_type;494}495496dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);497}498499/*ARGSUSED*/500static void501dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)502{503if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)504dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);505}506507static void508dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)509{510if (idp->di_kind == DT_IDENT_ARRAY)511dt_idcook_assc(dnp, idp, argc, args);512else513dt_idcook_thaw(dnp, idp, argc, args);514}515516static void517dt_iddtor_sign(dt_ident_t *idp)518{519if (idp->di_data != NULL)520free(((dt_idsig_t *)idp->di_data)->dis_args);521free(idp->di_data);522}523524static void525dt_iddtor_free(dt_ident_t *idp)526{527free(idp->di_data);528}529530static void531dt_iddtor_inline(dt_ident_t *idp)532{533dt_idnode_t *inp = idp->di_iarg;534535if (inp != NULL) {536dt_node_link_free(&inp->din_list);537538if (inp->din_hash != NULL)539dt_idhash_destroy(inp->din_hash);540541free(inp->din_argv);542free(inp);543}544545if (idp->di_kind == DT_IDENT_ARRAY)546dt_iddtor_sign(idp);547else548dt_iddtor_free(idp);549}550551/*ARGSUSED*/552static void553dt_iddtor_none(dt_ident_t *idp)554{555/* do nothing */556}557558static void559dt_iddtor_probe(dt_ident_t *idp)560{561if (idp->di_data != NULL)562dt_probe_destroy(idp->di_data);563}564565static size_t566dt_idsize_type(dt_ident_t *idp)567{568return (ctf_type_size(idp->di_ctfp, idp->di_type));569}570571/*ARGSUSED*/572static size_t573dt_idsize_none(dt_ident_t *idp)574{575return (0);576}577578const dt_idops_t dt_idops_assc = {579.di_cook = dt_idcook_assc,580.di_dtor = dt_iddtor_sign,581.di_size = dt_idsize_none,582};583584const dt_idops_t dt_idops_func = {585.di_cook = dt_idcook_func,586.di_dtor = dt_iddtor_sign,587.di_size = dt_idsize_none,588};589590const dt_idops_t dt_idops_args = {591.di_cook = dt_idcook_args,592.di_dtor = dt_iddtor_none,593.di_size = dt_idsize_none,594};595596const dt_idops_t dt_idops_regs = {597.di_cook = dt_idcook_regs,598.di_dtor = dt_iddtor_free,599.di_size = dt_idsize_none,600};601602const dt_idops_t dt_idops_type = {603.di_cook = dt_idcook_type,604.di_dtor = dt_iddtor_free,605.di_size = dt_idsize_type,606};607608const dt_idops_t dt_idops_thaw = {609.di_cook = dt_idcook_thaw,610.di_dtor = dt_iddtor_free,611.di_size = dt_idsize_type,612};613614const dt_idops_t dt_idops_inline = {615.di_cook = dt_idcook_inline,616.di_dtor = dt_iddtor_inline,617.di_size = dt_idsize_type,618};619620const dt_idops_t dt_idops_probe = {621.di_cook = dt_idcook_thaw,622.di_dtor = dt_iddtor_probe,623.di_size = dt_idsize_none,624};625626static void627dt_idhash_populate(dt_idhash_t *dhp)628{629const dt_ident_t *idp = dhp->dh_tmpl;630631dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */632dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);633634for (; idp->di_name != NULL; idp++) {635if (dt_idhash_insert(dhp, idp->di_name,636idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,637idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,638idp->di_iarg, 0) == NULL)639longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);640}641}642643dt_idhash_t *644dt_idhash_create(const char *name, const dt_ident_t *tmpl,645uint_t min, uint_t max)646{647dt_idhash_t *dhp;648size_t size;649650assert(min <= max);651652size = sizeof (dt_idhash_t) +653sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);654655if ((dhp = malloc(size)) == NULL)656return (NULL);657658bzero(dhp, size);659dhp->dh_name = name;660dhp->dh_tmpl = tmpl;661dhp->dh_nextid = min;662dhp->dh_minid = min;663dhp->dh_maxid = max;664dhp->dh_hashsz = _dtrace_strbuckets;665666return (dhp);667}668669/*670* Destroy an entire identifier hash. This must be done using two passes with671* an inlined version of dt_ident_destroy() to avoid referencing freed memory.672* In the first pass di_dtor() is called for all identifiers; then the second673* pass frees the actual dt_ident_t's. These must be done separately because674* a di_dtor() may operate on data structures which contain references to other675* identifiers inside of this hash itself (e.g. a global inline definition676* which contains a parse tree that refers to another global variable).677*/678void679dt_idhash_destroy(dt_idhash_t *dhp)680{681dt_ident_t *idp, *next;682ulong_t i;683684for (i = 0; i < dhp->dh_hashsz; i++) {685for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {686next = idp->di_next;687idp->di_ops->di_dtor(idp);688}689}690691for (i = 0; i < dhp->dh_hashsz; i++) {692for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {693next = idp->di_next;694free(idp->di_name);695free(idp);696}697}698699free(dhp);700}701702void703dt_idhash_update(dt_idhash_t *dhp)704{705uint_t nextid = dhp->dh_minid;706dt_ident_t *idp;707ulong_t i;708709for (i = 0; i < dhp->dh_hashsz; i++) {710for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {711/*712* Right now we're hard coding which types need to be713* reset, but ideally this would be done dynamically.714*/715if (idp->di_kind == DT_IDENT_ARRAY ||716idp->di_kind == DT_IDENT_SCALAR ||717idp->di_kind == DT_IDENT_AGG)718nextid = MAX(nextid, idp->di_id + 1);719}720}721722dhp->dh_nextid = nextid;723}724725dt_ident_t *726dt_idhash_lookup(dt_idhash_t *dhp, const char *name)727{728size_t len;729ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;730dt_ident_t *idp;731732if (dhp->dh_tmpl != NULL)733dt_idhash_populate(dhp); /* fill hash w/ initial population */734735for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {736if (strcmp(idp->di_name, name) == 0)737return (idp);738}739740return (NULL);741}742743int744dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)745{746if (dhp->dh_nextid >= dhp->dh_maxid)747return (-1); /* no more id's are free to allocate */748749*p = dhp->dh_nextid++;750return (0);751}752753ulong_t754dt_idhash_size(const dt_idhash_t *dhp)755{756return (dhp->dh_nelems);757}758759const char *760dt_idhash_name(const dt_idhash_t *dhp)761{762return (dhp->dh_name);763}764765dt_ident_t *766dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,767ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,768const dt_idops_t *ops, void *iarg, ulong_t gen)769{770dt_ident_t *idp;771ulong_t h;772773if (dhp->dh_tmpl != NULL)774dt_idhash_populate(dhp); /* fill hash w/ initial population */775776idp = dt_ident_create(name, kind, flags, id,777attr, vers, ops, iarg, gen);778779if (idp == NULL)780return (NULL);781782h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;783idp->di_next = dhp->dh_hash[h];784785dhp->dh_hash[h] = idp;786dhp->dh_nelems++;787788if (dhp->dh_defer != NULL)789dhp->dh_defer(dhp, idp);790791return (idp);792}793794void795dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)796{797ulong_t h;798799if (dhp->dh_tmpl != NULL)800dt_idhash_populate(dhp); /* fill hash w/ initial population */801802h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;803idp->di_next = dhp->dh_hash[h];804idp->di_flags &= ~DT_IDFLG_ORPHAN;805806dhp->dh_hash[h] = idp;807dhp->dh_nelems++;808809if (dhp->dh_defer != NULL)810dhp->dh_defer(dhp, idp);811}812813void814dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)815{816size_t len;817ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;818dt_ident_t **pp = &dhp->dh_hash[h];819dt_ident_t *idp;820821for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {822if (idp == key)823break;824else825pp = &idp->di_next;826}827828assert(idp == key);829*pp = idp->di_next;830831assert(dhp->dh_nelems != 0);832dhp->dh_nelems--;833834if (!(idp->di_flags & DT_IDFLG_ORPHAN))835dt_ident_destroy(idp);836}837838static int839dt_idhash_comp(const void *lp, const void *rp)840{841const dt_ident_t *lhs = *((const dt_ident_t **)lp);842const dt_ident_t *rhs = *((const dt_ident_t **)rp);843844if (lhs->di_id != rhs->di_id)845return ((int)(lhs->di_id - rhs->di_id));846else847return (strcmp(lhs->di_name, rhs->di_name));848}849850int851dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)852{853dt_ident_t **ids;854dt_ident_t *idp;855ulong_t i, j, n;856int rv;857858if (dhp->dh_tmpl != NULL)859dt_idhash_populate(dhp); /* fill hash w/ initial population */860861n = dhp->dh_nelems;862ids = alloca(sizeof (dt_ident_t *) * n);863864for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {865for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)866ids[j++] = idp;867}868869qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);870871for (i = 0; i < n; i++) {872if ((rv = func(dhp, ids[i], data)) != 0)873return (rv);874}875876return (0);877}878879dt_ident_t *880dt_idstack_lookup(dt_idstack_t *sp, const char *name)881{882dt_idhash_t *dhp;883dt_ident_t *idp;884885for (dhp = dt_list_prev(&sp->dids_list);886dhp != NULL; dhp = dt_list_prev(dhp)) {887if ((idp = dt_idhash_lookup(dhp, name)) != NULL)888return (idp);889}890891return (NULL);892}893894void895dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)896{897dt_list_append(&sp->dids_list, dhp);898}899900void901dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)902{903assert(dt_list_prev(&sp->dids_list) == dhp);904dt_list_delete(&sp->dids_list, dhp);905}906907dt_ident_t *908dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,909dtrace_attribute_t attr, uint_t vers,910const dt_idops_t *ops, void *iarg, ulong_t gen)911{912dt_ident_t *idp;913char *s = NULL;914915if ((name != NULL && (s = strdup(name)) == NULL) ||916(idp = malloc(sizeof (dt_ident_t))) == NULL) {917free(s);918return (NULL);919}920921idp->di_name = s;922idp->di_kind = kind;923idp->di_flags = flags;924idp->di_id = id;925idp->di_attr = attr;926idp->di_vers = vers;927idp->di_ops = ops;928idp->di_iarg = iarg;929idp->di_data = NULL;930idp->di_ctfp = NULL;931idp->di_type = CTF_ERR;932idp->di_next = NULL;933idp->di_gen = gen;934idp->di_lineno = yylineno;935936return (idp);937}938939/*940* Destroy an individual identifier. This code must be kept in sync with the941* dt_idhash_destroy() function below, which separates out the call to di_dtor.942*/943void944dt_ident_destroy(dt_ident_t *idp)945{946idp->di_ops->di_dtor(idp);947free(idp->di_name);948free(idp);949}950951void952dt_ident_morph(dt_ident_t *idp, ushort_t kind,953const dt_idops_t *ops, void *iarg)954{955idp->di_ops->di_dtor(idp);956idp->di_kind = kind;957idp->di_ops = ops;958idp->di_iarg = iarg;959idp->di_data = NULL;960}961962dtrace_attribute_t963dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)964{965dtrace_attribute_t attr;966dt_node_t *args, *argp;967int argc = 0;968969attr = dt_node_list_cook(pargp, DT_IDFLG_REF);970args = pargp ? *pargp : NULL;971972for (argp = args; argp != NULL; argp = argp->dn_list)973argc++;974975idp->di_ops->di_cook(dnp, idp, argc, args);976977if (idp->di_flags & DT_IDFLG_USER)978dnp->dn_flags |= DT_NF_USERLAND;979980return (dt_attr_min(attr, idp->di_attr));981}982983void984dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)985{986idp->di_ctfp = fp;987idp->di_type = type;988}989990dt_ident_t *991dt_ident_resolve(dt_ident_t *idp)992{993while (idp->di_flags & DT_IDFLG_INLINE) {994const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;995996if (dnp == NULL)997break; /* can't resolve any further yet */998999switch (dnp->dn_kind) {1000case DT_NODE_VAR:1001case DT_NODE_SYM:1002case DT_NODE_FUNC:1003case DT_NODE_AGG:1004case DT_NODE_INLINE:1005case DT_NODE_PROBE:1006idp = dnp->dn_ident;1007continue;1008}10091010if (dt_node_is_dynamic(dnp))1011idp = dnp->dn_ident;1012else1013break;1014}10151016return (idp);1017}10181019size_t1020dt_ident_size(dt_ident_t *idp)1021{1022idp = dt_ident_resolve(idp);1023return (idp->di_ops->di_size(idp));1024}10251026int1027dt_ident_unref(const dt_ident_t *idp)1028{1029return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&1030(idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);1031}10321033const char *1034dt_idkind_name(uint_t kind)1035{1036switch (kind) {1037case DT_IDENT_ARRAY: return ("associative array");1038case DT_IDENT_SCALAR: return ("scalar");1039case DT_IDENT_PTR: return ("pointer");1040case DT_IDENT_FUNC: return ("function");1041case DT_IDENT_AGG: return ("aggregation");1042case DT_IDENT_AGGFUNC: return ("aggregating function");1043case DT_IDENT_ACTFUNC: return ("tracing function");1044case DT_IDENT_XLSOU: return ("translated data");1045case DT_IDENT_XLPTR: return ("pointer to translated data");1046case DT_IDENT_SYMBOL: return ("external symbol reference");1047case DT_IDENT_ENUM: return ("enumerator");1048case DT_IDENT_PRAGAT: return ("#pragma attributes");1049case DT_IDENT_PRAGBN: return ("#pragma binding");1050case DT_IDENT_PROBE: return ("probe definition");1051default: return ("<?>");1052}1053}105410551056