/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, Joyent Inc. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include /* * This callback function is installed in a given identifier hash to search for * and apply deferred pragmas that are pending for a given new identifier name. * Multiple pragmas may be pending for a given name; we processs all of them. */ /*ARGSUSED*/ static void dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp) { dt_idhash_t *php; dt_ident_t *pdp; if ((php = yypcb->pcb_pragmas) == NULL) return; /* no pragmas pending for current compilation pass */ while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) { switch (pdp->di_kind) { case DT_IDENT_PRAGAT: idp->di_attr = pdp->di_attr; break; case DT_IDENT_PRAGBN: idp->di_vers = pdp->di_vers; break; } dt_idhash_delete(php, pdp); } } /* * The #pragma attributes directive can be used to reset stability attributes * on a global identifier or inline definition. If the identifier is already * defined, we can just change di_attr. If not, we insert the pragma into a * hash table of the current pcb's deferred pragmas for later processing. */ static void dt_pragma_attributes(const char *prname, dt_node_t *dnp) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dtrace_attribute_t attr, *a; dt_provider_t *pvp; const char *name, *part; dt_ident_t *idp; if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT || dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) { xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s " " \n", prname); } if (dtrace_str2attr(dnp->dn_string, &attr) == -1) { xyerror(D_PRAGMA_INVAL, "invalid attributes " "specified by #pragma %s\n", prname); } dnp = dnp->dn_list; name = dnp->dn_string; if (strcmp(name, "provider") == 0) { dnp = dnp->dn_list; name = dnp->dn_string; dnp = dnp->dn_list; part = dnp->dn_string; if ((pvp = dt_provider_lookup(dtp, name)) != NULL) { if (strcmp(part, "provider") == 0) { a = &pvp->pv_desc.dtvd_attr.dtpa_provider; } else if (strcmp(part, "module") == 0) { a = &pvp->pv_desc.dtvd_attr.dtpa_mod; } else if (strcmp(part, "function") == 0) { a = &pvp->pv_desc.dtvd_attr.dtpa_func; } else if (strcmp(part, "name") == 0) { a = &pvp->pv_desc.dtvd_attr.dtpa_name; } else if (strcmp(part, "args") == 0) { a = &pvp->pv_desc.dtvd_attr.dtpa_args; } else { xyerror(D_PRAGMA_INVAL, "invalid component " "\"%s\" in attribute #pragma " "for provider %s\n", name, part); } *a = attr; return; } } else if ((idp = dt_idstack_lookup( &yypcb->pcb_globals, name)) != NULL) { if (idp->di_gen != dtp->dt_gen) { xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify " "entity defined outside program scope\n", prname); } idp->di_attr = attr; return; } if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas = dt_idhash_create("pragma", NULL, 0, 0)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0, attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen); if (idp == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); if (dtp->dt_globals->dh_defer == NULL) dtp->dt_globals->dh_defer = &dt_pragma_apply; } /* * The #pragma binding directive can be used to reset the version binding * on a global identifier or inline definition. If the identifier is already * defined, we can just change di_vers. If not, we insert the pragma into a * hash table of the current pcb's deferred pragmas for later processing. */ static void dt_pragma_binding(const char *prname, dt_node_t *dnp) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dt_version_t vers; const char *name; dt_ident_t *idp; if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING || dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) { xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s " "\"version\" \n", prname); } if (dt_version_str2num(dnp->dn_string, &vers) == -1) { xyerror(D_PRAGMA_INVAL, "invalid version string " "specified by #pragma %s\n", prname); } name = dnp->dn_list->dn_string; idp = dt_idstack_lookup(&yypcb->pcb_globals, name); if (idp != NULL) { if (idp->di_gen != dtp->dt_gen) { xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify " "entity defined outside program scope\n", prname); } idp->di_vers = vers; return; } if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas = dt_idhash_create("pragma", NULL, 0, 0)) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0, _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen); if (idp == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); if (dtp->dt_globals->dh_defer == NULL) dtp->dt_globals->dh_defer = &dt_pragma_apply; } static void dt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib, size_t len) { dt_dirpath_t *dirp; struct stat sbuf; int found = 0; for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL; dirp = dt_list_next(dirp)) { (void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname); if (stat(lib, &sbuf) == 0) { found = 1; break; } } if (!found) xyerror(D_PRAGMA_DEPEND, "failed to find dependency in libpath: %s", lname); } /* * The #pragma depends_on directive can be used to express a dependency on a * module, provider or library which if not present will cause processing to * abort. */ static void dt_pragma_depends(const char *prname, dt_node_t *cnp) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dt_node_t *nnp = cnp ? cnp->dn_list : NULL; int found; dt_lib_depend_t *dld; char lib[MAXPATHLEN]; if (cnp == NULL || nnp == NULL || cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) { xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s " " \n", prname); } if (strcmp(cnp->dn_string, "provider") == 0) found = dt_provider_lookup(dtp, nnp->dn_string) != NULL; else if (strcmp(cnp->dn_string, "module") == 0) { dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string); found = mp != NULL && dt_module_getctf(dtp, mp) != NULL; } else if (strcmp(cnp->dn_string, "library") == 0) { if (yypcb->pcb_cflags & DTRACE_C_CTL) { assert(dtp->dt_filetag != NULL); dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, sizeof (lib)); dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, dtp->dt_filetag); assert(dld != NULL); if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies, lib)) != 0) { xyerror(D_PRAGMA_DEPEND, "failed to add dependency %s:%s\n", lib, dtrace_errmsg(dtp, dtrace_errno(dtp))); } } else { /* * By this point we have already performed a topological * sort of the dependencies; we process this directive * as satisfied as long as the dependency was properly * loaded. */ if (dtp->dt_filetag == NULL) xyerror(D_PRAGMA_DEPEND, "main program may " "not explicitly depend on a library"); dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, dtp->dt_filetag); assert(dld != NULL); dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, sizeof (lib)); dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted, lib); assert(dld != NULL); if (!dld->dtld_loaded) xyerror(D_PRAGMA_DEPEND, "program requires " "library \"%s\" which failed to load", lib); } found = B_TRUE; } else { xyerror(D_PRAGMA_INVAL, "invalid class %s " "specified by #pragma %s\n", cnp->dn_string, prname); } if (!found) { xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n", cnp->dn_string, nnp->dn_string); } } /* * The #pragma error directive can be followed by any list of tokens, which we * just concatenate and print as part of our error message. */ static void dt_pragma_error(const char *prname, dt_node_t *dnp) { dt_node_t *enp; size_t n = 0; char *s; for (enp = dnp; enp != NULL; enp = enp->dn_list) { if (enp->dn_kind == DT_NODE_IDENT || enp->dn_kind == DT_NODE_STRING) n += strlen(enp->dn_string) + 1; } s = alloca(n + 1); s[0] = '\0'; for (enp = dnp; enp != NULL; enp = enp->dn_list) { if (enp->dn_kind == DT_NODE_IDENT || enp->dn_kind == DT_NODE_STRING) { (void) strcat(s, enp->dn_string); (void) strcat(s, " "); } } xyerror(D_PRAGERR, "#%s: %s\n", prname, s); } /*ARGSUSED*/ static void dt_pragma_ident(const char *prname, dt_node_t *dnp) { /* ignore any #ident or #pragma ident lines */ } static void dt_pragma_option(const char *prname, dt_node_t *dnp) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; char *opt, *val; if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) { xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s