17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c9d6cd77Sjhaslam  * Common Development and Distribution License (the "License").
6c9d6cd77Sjhaslam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
216009dbc6Sahl 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
245eb667acSRobert Mustacchi  * Copyright (c) 2011, Joyent Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27c9d6cd77Sjhaslam #include <assert.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <alloca.h>
305eb667acSRobert Mustacchi #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate 
345eb667acSRobert Mustacchi #include <sys/types.h>
355eb667acSRobert Mustacchi #include <sys/stat.h>
365eb667acSRobert Mustacchi 
377c478bd9Sstevel@tonic-gate #include <dt_parser.h>
387c478bd9Sstevel@tonic-gate #include <dt_impl.h>
397c478bd9Sstevel@tonic-gate #include <dt_provider.h>
407c478bd9Sstevel@tonic-gate #include <dt_module.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * This callback function is installed in a given identifier hash to search for
447c478bd9Sstevel@tonic-gate  * and apply deferred pragmas that are pending for a given new identifier name.
457c478bd9Sstevel@tonic-gate  * Multiple pragmas may be pending for a given name; we processs all of them.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
487c478bd9Sstevel@tonic-gate static void
dt_pragma_apply(dt_idhash_t * dhp,dt_ident_t * idp)497c478bd9Sstevel@tonic-gate dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	dt_idhash_t *php;
527c478bd9Sstevel@tonic-gate 	dt_ident_t *pdp;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if ((php = yypcb->pcb_pragmas) == NULL)
557c478bd9Sstevel@tonic-gate 		return; /* no pragmas pending for current compilation pass */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
587c478bd9Sstevel@tonic-gate 		switch (pdp->di_kind) {
597c478bd9Sstevel@tonic-gate 		case DT_IDENT_PRAGAT:
607c478bd9Sstevel@tonic-gate 			idp->di_attr = pdp->di_attr;
617c478bd9Sstevel@tonic-gate 			break;
627c478bd9Sstevel@tonic-gate 		case DT_IDENT_PRAGBN:
637c478bd9Sstevel@tonic-gate 			idp->di_vers = pdp->di_vers;
647c478bd9Sstevel@tonic-gate 			break;
657c478bd9Sstevel@tonic-gate 		}
667c478bd9Sstevel@tonic-gate 		dt_idhash_delete(php, pdp);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * The #pragma attributes directive can be used to reset stability attributes
727c478bd9Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
737c478bd9Sstevel@tonic-gate  * defined, we can just change di_attr.  If not, we insert the pragma into a
747c478bd9Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate static void
dt_pragma_attributes(const char * prname,dt_node_t * dnp)777c478bd9Sstevel@tonic-gate dt_pragma_attributes(const char *prname, dt_node_t *dnp)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
807c478bd9Sstevel@tonic-gate 	dtrace_attribute_t attr, *a;
817c478bd9Sstevel@tonic-gate 	dt_provider_t *pvp;
827c478bd9Sstevel@tonic-gate 	const char *name, *part;
837c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
867c478bd9Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
877c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
887c478bd9Sstevel@tonic-gate 		    "<attributes> <ident>\n", prname);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
927c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid attributes "
937c478bd9Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	dnp = dnp->dn_list;
977c478bd9Sstevel@tonic-gate 	name = dnp->dn_string;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (strcmp(name, "provider") == 0) {
1007c478bd9Sstevel@tonic-gate 		dnp = dnp->dn_list;
1017c478bd9Sstevel@tonic-gate 		name = dnp->dn_string;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		dnp = dnp->dn_list;
1047c478bd9Sstevel@tonic-gate 		part = dnp->dn_string;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
1077c478bd9Sstevel@tonic-gate 			if (strcmp(part, "provider") == 0) {
1087c478bd9Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
1097c478bd9Sstevel@tonic-gate 			} else if (strcmp(part, "module") == 0) {
1107c478bd9Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
1117c478bd9Sstevel@tonic-gate 			} else if (strcmp(part, "function") == 0) {
1127c478bd9Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
1137c478bd9Sstevel@tonic-gate 			} else if (strcmp(part, "name") == 0) {
1147c478bd9Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
1157c478bd9Sstevel@tonic-gate 			} else if (strcmp(part, "args") == 0) {
1167c478bd9Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
1177c478bd9Sstevel@tonic-gate 			} else {
1187c478bd9Sstevel@tonic-gate 				xyerror(D_PRAGMA_INVAL, "invalid component "
1197c478bd9Sstevel@tonic-gate 				    "\"%s\" in attribute #pragma "
1207c478bd9Sstevel@tonic-gate 				    "for provider %s\n", name, part);
1217c478bd9Sstevel@tonic-gate 			}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 			*a = attr;
1247c478bd9Sstevel@tonic-gate 			return;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	} else if ((idp = dt_idstack_lookup(
1287c478bd9Sstevel@tonic-gate 	    &yypcb->pcb_globals, name)) != NULL) {
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
1317c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
1327c478bd9Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		idp->di_attr = attr;
1367c478bd9Sstevel@tonic-gate 		return;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
1407c478bd9Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
1417c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
1447c478bd9Sstevel@tonic-gate 	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (idp == NULL)
1477c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
1507c478bd9Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * The #pragma binding directive can be used to reset the version binding
1557c478bd9Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
1567c478bd9Sstevel@tonic-gate  * defined, we can just change di_vers.  If not, we insert the pragma into a
1577c478bd9Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate static void
dt_pragma_binding(const char * prname,dt_node_t * dnp)1607c478bd9Sstevel@tonic-gate dt_pragma_binding(const char *prname, dt_node_t *dnp)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1637c478bd9Sstevel@tonic-gate 	dt_version_t vers;
1647c478bd9Sstevel@tonic-gate 	const char *name;
1657c478bd9Sstevel@tonic-gate 	dt_ident_t *idp;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
1687c478bd9Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
1697c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
1707c478bd9Sstevel@tonic-gate 		    "\"version\" <ident>\n", prname);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
1747c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid version string "
1757c478bd9Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	name = dnp->dn_list->dn_string;
1797c478bd9Sstevel@tonic-gate 	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (idp != NULL) {
1827c478bd9Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
1837c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
1847c478bd9Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		idp->di_vers = vers;
1877c478bd9Sstevel@tonic-gate 		return;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
1917c478bd9Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
1927c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
1957c478bd9Sstevel@tonic-gate 	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (idp == NULL)
1987c478bd9Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
2017c478bd9Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
204*2b6389efSBryan Cantrill static void
dt_pragma_depends_finddep(dtrace_hdl_t * dtp,const char * lname,char * lib,size_t len)2055eb667acSRobert Mustacchi dt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
2065eb667acSRobert Mustacchi     size_t len)
2075eb667acSRobert Mustacchi {
2085eb667acSRobert Mustacchi 	dt_dirpath_t *dirp;
2095eb667acSRobert Mustacchi 	struct stat sbuf;
2105eb667acSRobert Mustacchi 	int found = 0;
2115eb667acSRobert Mustacchi 
2125eb667acSRobert Mustacchi 	for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
2135eb667acSRobert Mustacchi 	    dirp = dt_list_next(dirp)) {
2145eb667acSRobert Mustacchi 		(void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
2155eb667acSRobert Mustacchi 
2165eb667acSRobert Mustacchi 		if (stat(lib, &sbuf) == 0) {
2175eb667acSRobert Mustacchi 			found = 1;
2185eb667acSRobert Mustacchi 			break;
2195eb667acSRobert Mustacchi 		}
2205eb667acSRobert Mustacchi 	}
2215eb667acSRobert Mustacchi 
2225eb667acSRobert Mustacchi 	if (!found)
2235eb667acSRobert Mustacchi 		xyerror(D_PRAGMA_DEPEND,
2245eb667acSRobert Mustacchi 		    "failed to find dependency in libpath: %s", lname);
2255eb667acSRobert Mustacchi }
2265eb667acSRobert Mustacchi 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * The #pragma depends_on directive can be used to express a dependency on a
229c9d6cd77Sjhaslam  * module, provider or library which if not present will cause processing to
230c9d6cd77Sjhaslam  * abort.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static void
dt_pragma_depends(const char * prname,dt_node_t * cnp)2337c478bd9Sstevel@tonic-gate dt_pragma_depends(const char *prname, dt_node_t *cnp)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2367c478bd9Sstevel@tonic-gate 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
2377c478bd9Sstevel@tonic-gate 	int found;
238c9d6cd77Sjhaslam 	dt_lib_depend_t *dld;
2396009dbc6Sahl 	char lib[MAXPATHLEN];
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (cnp == NULL || nnp == NULL ||
2427c478bd9Sstevel@tonic-gate 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
2437c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
2447c478bd9Sstevel@tonic-gate 		    "<class> <name>\n", prname);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (strcmp(cnp->dn_string, "provider") == 0)
2487c478bd9Sstevel@tonic-gate 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
2497c478bd9Sstevel@tonic-gate 	else if (strcmp(cnp->dn_string, "module") == 0) {
2507c478bd9Sstevel@tonic-gate 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
2517c478bd9Sstevel@tonic-gate 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
252c9d6cd77Sjhaslam 	} else if (strcmp(cnp->dn_string, "library") == 0) {
253c9d6cd77Sjhaslam 		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
2546009dbc6Sahl 			assert(dtp->dt_filetag != NULL);
255c9d6cd77Sjhaslam 
2565eb667acSRobert Mustacchi 			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
2575eb667acSRobert Mustacchi 			    sizeof (lib));
2585eb667acSRobert Mustacchi 
259c9d6cd77Sjhaslam 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
260c9d6cd77Sjhaslam 			    dtp->dt_filetag);
261c9d6cd77Sjhaslam 			assert(dld != NULL);
262c9d6cd77Sjhaslam 
263c9d6cd77Sjhaslam 			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
264c9d6cd77Sjhaslam 			    lib)) != 0) {
265c9d6cd77Sjhaslam 				xyerror(D_PRAGMA_DEPEND,
2666009dbc6Sahl 				    "failed to add dependency %s:%s\n", lib,
267c9d6cd77Sjhaslam 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
268c9d6cd77Sjhaslam 			}
2696009dbc6Sahl 		} else {
2706009dbc6Sahl 			/*
2716009dbc6Sahl 			 * By this point we have already performed a topological
2726009dbc6Sahl 			 * sort of the dependencies; we process this directive
2736009dbc6Sahl 			 * as satisfied as long as the dependency was properly
2746009dbc6Sahl 			 * loaded.
2756009dbc6Sahl 			 */
2766009dbc6Sahl 			if (dtp->dt_filetag == NULL)
2776009dbc6Sahl 				xyerror(D_PRAGMA_DEPEND, "main program may "
2786009dbc6Sahl 				    "not explicitly depend on a library");
2796009dbc6Sahl 
2806009dbc6Sahl 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2816009dbc6Sahl 			    dtp->dt_filetag);
2826009dbc6Sahl 			assert(dld != NULL);
2836009dbc6Sahl 
2845eb667acSRobert Mustacchi 			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
2855eb667acSRobert Mustacchi 			    sizeof (lib));
2866009dbc6Sahl 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2876009dbc6Sahl 			    lib);
2886009dbc6Sahl 			assert(dld != NULL);
2896009dbc6Sahl 
2906009dbc6Sahl 			if (!dld->dtld_loaded)
2916009dbc6Sahl 				xyerror(D_PRAGMA_DEPEND, "program requires "
2926009dbc6Sahl 				    "library \"%s\" which failed to load",
2936009dbc6Sahl 				    lib);
294c9d6cd77Sjhaslam 		}
2956009dbc6Sahl 
2966009dbc6Sahl 		found = B_TRUE;
2977c478bd9Sstevel@tonic-gate 	} else {
2987c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
2997c478bd9Sstevel@tonic-gate 		    "specified by #pragma %s\n", cnp->dn_string, prname);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (!found) {
3037c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
3047c478bd9Sstevel@tonic-gate 		    cnp->dn_string, nnp->dn_string);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * The #pragma error directive can be followed by any list of tokens, which we
3107c478bd9Sstevel@tonic-gate  * just concatenate and print as part of our error message.
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate static void
dt_pragma_error(const char * prname,dt_node_t * dnp)3137c478bd9Sstevel@tonic-gate dt_pragma_error(const char *prname, dt_node_t *dnp)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	dt_node_t *enp;
3167c478bd9Sstevel@tonic-gate 	size_t n = 0;
3177c478bd9Sstevel@tonic-gate 	char *s;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
3207c478bd9Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
3217c478bd9Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING)
3227c478bd9Sstevel@tonic-gate 			n += strlen(enp->dn_string) + 1;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	s = alloca(n + 1);
3267c478bd9Sstevel@tonic-gate 	s[0] = '\0';
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
3297c478bd9Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
3307c478bd9Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING) {
3317c478bd9Sstevel@tonic-gate 			(void) strcat(s, enp->dn_string);
3327c478bd9Sstevel@tonic-gate 			(void) strcat(s, " ");
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3407c478bd9Sstevel@tonic-gate static void
dt_pragma_ident(const char * prname,dt_node_t * dnp)3417c478bd9Sstevel@tonic-gate dt_pragma_ident(const char *prname, dt_node_t *dnp)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	/* ignore any #ident or #pragma ident lines */
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate static void
dt_pragma_option(const char * prname,dt_node_t * dnp)3477c478bd9Sstevel@tonic-gate dt_pragma_option(const char *prname, dt_node_t *dnp)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3507c478bd9Sstevel@tonic-gate 	char *opt, *val;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
3537c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
3547c478bd9Sstevel@tonic-gate 		    "malformed #pragma %s <option>=<val>\n", prname);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (dnp->dn_list != NULL) {
3587c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
3597c478bd9Sstevel@tonic-gate 		    "superfluous arguments specified for #pragma %s\n", prname);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
36223a1cceaSRoger A. Faulkner 	opt = strdupa(dnp->dn_string);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if ((val = strchr(opt, '=')) != NULL)
3657c478bd9Sstevel@tonic-gate 		*val++ = '\0';
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (dtrace_setopt(dtp, opt, val) == -1) {
3687c478bd9Sstevel@tonic-gate 		if (val == NULL) {
3697c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
3707c478bd9Sstevel@tonic-gate 			    "failed to set option '%s': %s\n", opt,
3717c478bd9Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
3727c478bd9Sstevel@tonic-gate 		} else {
3737c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
3747c478bd9Sstevel@tonic-gate 			    "failed to set option '%s' to '%s': %s\n",
3757c478bd9Sstevel@tonic-gate 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * The #line directive is used to reset the input line number and to optionally
3827c478bd9Sstevel@tonic-gate  * note the file name for use in error messages.  Sun cpp(1) also produces a
3837c478bd9Sstevel@tonic-gate  * third integer token after the filename which is one of the following:
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  * 0 - line change has nothing to do with an #include file
3867c478bd9Sstevel@tonic-gate  * 1 - line change because we just entered a #include file
3877c478bd9Sstevel@tonic-gate  * 2 - line change because we just exited a #include file
3887c478bd9Sstevel@tonic-gate  *
3897c478bd9Sstevel@tonic-gate  * We use these state tokens to adjust pcb_idepth, which in turn controls
3907c478bd9Sstevel@tonic-gate  * whether type lookups access the global type space or not.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate static void
dt_pragma_line(const char * prname,dt_node_t * dnp)3937c478bd9Sstevel@tonic-gate dt_pragma_line(const char *prname, dt_node_t *dnp)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
3967c478bd9Sstevel@tonic-gate 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
3997c478bd9Sstevel@tonic-gate 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
4007c478bd9Sstevel@tonic-gate 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
4017c478bd9Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
4027c478bd9Sstevel@tonic-gate 		    "<line> [ [\"file\"] state ]\n", prname);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * If a file is specified, free any old pcb_filetag and swap fnp's
4077c478bd9Sstevel@tonic-gate 	 * dn_string into pcb_filetag as the new filename for error messages.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	if (fnp != NULL) {
4107c478bd9Sstevel@tonic-gate 		if (yypcb->pcb_filetag != NULL)
4117c478bd9Sstevel@tonic-gate 			free(yypcb->pcb_filetag);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		/*
4147c478bd9Sstevel@tonic-gate 		 * This is not pretty, but is a necessary evil until we either
4157c478bd9Sstevel@tonic-gate 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
4167c478bd9Sstevel@tonic-gate 		 * the filename begins with /dev/fd, we know it's the master
4177c478bd9Sstevel@tonic-gate 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
4187c478bd9Sstevel@tonic-gate 		 * dt_filetag pointer so error messages refer to the main file.
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
4217c478bd9Sstevel@tonic-gate 			yypcb->pcb_filetag = fnp->dn_string;
4227c478bd9Sstevel@tonic-gate 			fnp->dn_string = NULL;
4237c478bd9Sstevel@tonic-gate 		} else
4247c478bd9Sstevel@tonic-gate 			yypcb->pcb_filetag = NULL;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (inp != NULL) {
4287c478bd9Sstevel@tonic-gate 		if (inp->dn_value == 1)
4297c478bd9Sstevel@tonic-gate 			yypcb->pcb_idepth++;
4307c478bd9Sstevel@tonic-gate 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
4317c478bd9Sstevel@tonic-gate 			yypcb->pcb_idepth--;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	yylineno = dnp->dn_value;
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * D compiler pragma types range from control directives to common pragmas to
4397c478bd9Sstevel@tonic-gate  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
4407c478bd9Sstevel@tonic-gate  * as a special prefix for our pragmas so they can be used in mixed headers.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
4437c478bd9Sstevel@tonic-gate #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
4447c478bd9Sstevel@tonic-gate #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static const struct dt_pragmadesc {
4477c478bd9Sstevel@tonic-gate 	const char *dpd_name;
4487c478bd9Sstevel@tonic-gate 	void (*dpd_func)(const char *, dt_node_t *);
4497c478bd9Sstevel@tonic-gate 	int dpd_kind;
4507c478bd9Sstevel@tonic-gate } dt_pragmas[] = {
4517c478bd9Sstevel@tonic-gate 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
4527c478bd9Sstevel@tonic-gate 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
4537c478bd9Sstevel@tonic-gate 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
4547c478bd9Sstevel@tonic-gate 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
4557c478bd9Sstevel@tonic-gate 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
4567c478bd9Sstevel@tonic-gate 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
4577c478bd9Sstevel@tonic-gate 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
4587c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
4597c478bd9Sstevel@tonic-gate };
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Process a control line #directive by looking up the directive name in our
4637c478bd9Sstevel@tonic-gate  * lookup table and invoking the corresponding function with the token list.
4647c478bd9Sstevel@tonic-gate  * According to K&R[A12.9], we silently ignore null directive lines.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate void
dt_pragma(dt_node_t * pnp)4677c478bd9Sstevel@tonic-gate dt_pragma(dt_node_t *pnp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	const struct dt_pragmadesc *dpd;
4707c478bd9Sstevel@tonic-gate 	dt_node_t *dnp;
4717c478bd9Sstevel@tonic-gate 	int kind = DT_PRAGMA_DIR;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
4747c478bd9Sstevel@tonic-gate 		if (dnp->dn_kind == DT_NODE_INT) {
4757c478bd9Sstevel@tonic-gate 			dt_pragma_line("line", dnp);
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if (dnp->dn_kind != DT_NODE_IDENT)
4807c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		if (kind == DT_PRAGMA_DIR &&
4837c478bd9Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "pragma") == 0) {
4847c478bd9Sstevel@tonic-gate 			kind = DT_PRAGMA_SUB;
4857c478bd9Sstevel@tonic-gate 			continue;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		if (kind == DT_PRAGMA_SUB &&
4897c478bd9Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "D") == 0) {
4907c478bd9Sstevel@tonic-gate 			kind = DT_PRAGMA_DCP;
4917c478bd9Sstevel@tonic-gate 			continue;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
4957c478bd9Sstevel@tonic-gate 			if (dpd->dpd_kind <= kind &&
4967c478bd9Sstevel@tonic-gate 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
4977c478bd9Sstevel@tonic-gate 				break;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		yylineno--; /* since we've already seen \n */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		if (dpd->dpd_name != NULL) {
5037c478bd9Sstevel@tonic-gate 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
5047c478bd9Sstevel@tonic-gate 			yylineno++;
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		switch (kind) {
5097c478bd9Sstevel@tonic-gate 		case DT_PRAGMA_DIR:
5107c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
5117c478bd9Sstevel@tonic-gate 			    "#%s\n", dnp->dn_string);
5127c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5137c478bd9Sstevel@tonic-gate 		case DT_PRAGMA_SUB:
5147c478bd9Sstevel@tonic-gate 			break; /* K&R[A12.8] says to ignore unknown pragmas */
5157c478bd9Sstevel@tonic-gate 		case DT_PRAGMA_DCP:
5167c478bd9Sstevel@tonic-gate 		default:
5177c478bd9Sstevel@tonic-gate 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
5187c478bd9Sstevel@tonic-gate 			    dnp->dn_string);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		yylineno++;
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	dt_node_list_free(&pnp);
5267c478bd9Sstevel@tonic-gate }
527