xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_pragma.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55478Sjhaslam  * Common Development and Distribution License (the "License").
65478Sjhaslam  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216390Sahl 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
265478Sjhaslam #include <assert.h>
270Sstevel@tonic-gate #include <strings.h>
280Sstevel@tonic-gate #include <alloca.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <dt_parser.h>
330Sstevel@tonic-gate #include <dt_impl.h>
340Sstevel@tonic-gate #include <dt_provider.h>
350Sstevel@tonic-gate #include <dt_module.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * This callback function is installed in a given identifier hash to search for
390Sstevel@tonic-gate  * and apply deferred pragmas that are pending for a given new identifier name.
400Sstevel@tonic-gate  * Multiple pragmas may be pending for a given name; we processs all of them.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate /*ARGSUSED*/
430Sstevel@tonic-gate static void
dt_pragma_apply(dt_idhash_t * dhp,dt_ident_t * idp)440Sstevel@tonic-gate dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	dt_idhash_t *php;
470Sstevel@tonic-gate 	dt_ident_t *pdp;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if ((php = yypcb->pcb_pragmas) == NULL)
500Sstevel@tonic-gate 		return; /* no pragmas pending for current compilation pass */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
530Sstevel@tonic-gate 		switch (pdp->di_kind) {
540Sstevel@tonic-gate 		case DT_IDENT_PRAGAT:
550Sstevel@tonic-gate 			idp->di_attr = pdp->di_attr;
560Sstevel@tonic-gate 			break;
570Sstevel@tonic-gate 		case DT_IDENT_PRAGBN:
580Sstevel@tonic-gate 			idp->di_vers = pdp->di_vers;
590Sstevel@tonic-gate 			break;
600Sstevel@tonic-gate 		}
610Sstevel@tonic-gate 		dt_idhash_delete(php, pdp);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * The #pragma attributes directive can be used to reset stability attributes
670Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
680Sstevel@tonic-gate  * defined, we can just change di_attr.  If not, we insert the pragma into a
690Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static void
dt_pragma_attributes(const char * prname,dt_node_t * dnp)720Sstevel@tonic-gate dt_pragma_attributes(const char *prname, dt_node_t *dnp)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
750Sstevel@tonic-gate 	dtrace_attribute_t attr, *a;
760Sstevel@tonic-gate 	dt_provider_t *pvp;
770Sstevel@tonic-gate 	const char *name, *part;
780Sstevel@tonic-gate 	dt_ident_t *idp;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
810Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
820Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
830Sstevel@tonic-gate 		    "<attributes> <ident>\n", prname);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
870Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid attributes "
880Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	dnp = dnp->dn_list;
920Sstevel@tonic-gate 	name = dnp->dn_string;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (strcmp(name, "provider") == 0) {
950Sstevel@tonic-gate 		dnp = dnp->dn_list;
960Sstevel@tonic-gate 		name = dnp->dn_string;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		dnp = dnp->dn_list;
990Sstevel@tonic-gate 		part = dnp->dn_string;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
1020Sstevel@tonic-gate 			if (strcmp(part, "provider") == 0) {
1030Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
1040Sstevel@tonic-gate 			} else if (strcmp(part, "module") == 0) {
1050Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
1060Sstevel@tonic-gate 			} else if (strcmp(part, "function") == 0) {
1070Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
1080Sstevel@tonic-gate 			} else if (strcmp(part, "name") == 0) {
1090Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
1100Sstevel@tonic-gate 			} else if (strcmp(part, "args") == 0) {
1110Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
1120Sstevel@tonic-gate 			} else {
1130Sstevel@tonic-gate 				xyerror(D_PRAGMA_INVAL, "invalid component "
1140Sstevel@tonic-gate 				    "\"%s\" in attribute #pragma "
1150Sstevel@tonic-gate 				    "for provider %s\n", name, part);
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			*a = attr;
1190Sstevel@tonic-gate 			return;
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	} else if ((idp = dt_idstack_lookup(
1230Sstevel@tonic-gate 	    &yypcb->pcb_globals, name)) != NULL) {
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
1260Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
1270Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		idp->di_attr = attr;
1310Sstevel@tonic-gate 		return;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
1350Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
1360Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
1390Sstevel@tonic-gate 	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (idp == NULL)
1420Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
1450Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * The #pragma binding directive can be used to reset the version binding
1500Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
1510Sstevel@tonic-gate  * defined, we can just change di_vers.  If not, we insert the pragma into a
1520Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate static void
dt_pragma_binding(const char * prname,dt_node_t * dnp)1550Sstevel@tonic-gate dt_pragma_binding(const char *prname, dt_node_t *dnp)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
1580Sstevel@tonic-gate 	dt_version_t vers;
1590Sstevel@tonic-gate 	const char *name;
1600Sstevel@tonic-gate 	dt_ident_t *idp;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
1630Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
1640Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
1650Sstevel@tonic-gate 		    "\"version\" <ident>\n", prname);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
1690Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid version string "
1700Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	name = dnp->dn_list->dn_string;
1740Sstevel@tonic-gate 	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (idp != NULL) {
1770Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
1780Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
1790Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		idp->di_vers = vers;
1820Sstevel@tonic-gate 		return;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
1860Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
1870Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
1900Sstevel@tonic-gate 	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (idp == NULL)
1930Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
1960Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * The #pragma depends_on directive can be used to express a dependency on a
2015478Sjhaslam  * module, provider or library which if not present will cause processing to
2025478Sjhaslam  * abort.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate static void
dt_pragma_depends(const char * prname,dt_node_t * cnp)2050Sstevel@tonic-gate dt_pragma_depends(const char *prname, dt_node_t *cnp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
2080Sstevel@tonic-gate 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
2090Sstevel@tonic-gate 	int found;
2105478Sjhaslam 	dt_lib_depend_t *dld;
2116390Sahl 	char lib[MAXPATHLEN];
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (cnp == NULL || nnp == NULL ||
2140Sstevel@tonic-gate 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
2150Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
2160Sstevel@tonic-gate 		    "<class> <name>\n", prname);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (strcmp(cnp->dn_string, "provider") == 0)
2200Sstevel@tonic-gate 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
2210Sstevel@tonic-gate 	else if (strcmp(cnp->dn_string, "module") == 0) {
2220Sstevel@tonic-gate 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
2230Sstevel@tonic-gate 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
2245478Sjhaslam 	} else if (strcmp(cnp->dn_string, "library") == 0) {
2256390Sahl 		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
2266390Sahl 			assert(dtp->dt_filetag != NULL);
2275478Sjhaslam 
2286390Sahl 			/*
2296390Sahl 			 * We have the file we are working on in dtp->dt_filetag
2306390Sahl 			 * so find that node and add the dependency in.
2316390Sahl 			 */
2326390Sahl 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2336390Sahl 			    dtp->dt_filetag);
2346390Sahl 			assert(dld != NULL);
2356390Sahl 
2366390Sahl 			(void) snprintf(lib, sizeof (lib), "%s%s",
2376390Sahl 			    dld->dtld_libpath, nnp->dn_string);
2386390Sahl 			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
2396390Sahl 			    lib)) != 0) {
2406390Sahl 				xyerror(D_PRAGMA_DEPEND,
2416390Sahl 				    "failed to add dependency %s:%s\n", lib,
2426390Sahl 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
2436390Sahl 			}
2446390Sahl 		} else {
2456390Sahl 			/*
2466390Sahl 			 * By this point we have already performed a topological
2476390Sahl 			 * sort of the dependencies; we process this directive
2486390Sahl 			 * as satisfied as long as the dependency was properly
2496390Sahl 			 * loaded.
2506390Sahl 			 */
2516390Sahl 			if (dtp->dt_filetag == NULL)
2526390Sahl 				xyerror(D_PRAGMA_DEPEND, "main program may "
2536390Sahl 				    "not explicitly depend on a library");
2545478Sjhaslam 
2555478Sjhaslam 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2565478Sjhaslam 			    dtp->dt_filetag);
2575478Sjhaslam 			assert(dld != NULL);
2585478Sjhaslam 
2596390Sahl 			(void) snprintf(lib, sizeof (lib), "%s%s",
2605478Sjhaslam 			    dld->dtld_libpath, nnp->dn_string);
2616390Sahl 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2626390Sahl 			    lib);
2636390Sahl 			assert(dld != NULL);
2646390Sahl 
2656390Sahl 			if (!dld->dtld_loaded)
2666390Sahl 				xyerror(D_PRAGMA_DEPEND, "program requires "
2676390Sahl 				    "library \"%s\" which failed to load",
2686390Sahl 				    lib);
2695478Sjhaslam 		}
2706390Sahl 
2716390Sahl 		found = B_TRUE;
2720Sstevel@tonic-gate 	} else {
2730Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
2740Sstevel@tonic-gate 		    "specified by #pragma %s\n", cnp->dn_string, prname);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (!found) {
2780Sstevel@tonic-gate 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
2790Sstevel@tonic-gate 		    cnp->dn_string, nnp->dn_string);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * The #pragma error directive can be followed by any list of tokens, which we
2850Sstevel@tonic-gate  * just concatenate and print as part of our error message.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate static void
dt_pragma_error(const char * prname,dt_node_t * dnp)2880Sstevel@tonic-gate dt_pragma_error(const char *prname, dt_node_t *dnp)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	dt_node_t *enp;
2910Sstevel@tonic-gate 	size_t n = 0;
2920Sstevel@tonic-gate 	char *s;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
2950Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
2960Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING)
2970Sstevel@tonic-gate 			n += strlen(enp->dn_string) + 1;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	s = alloca(n + 1);
3010Sstevel@tonic-gate 	s[0] = '\0';
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
3040Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
3050Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING) {
3060Sstevel@tonic-gate 			(void) strcat(s, enp->dn_string);
3070Sstevel@tonic-gate 			(void) strcat(s, " ");
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*ARGSUSED*/
3150Sstevel@tonic-gate static void
dt_pragma_ident(const char * prname,dt_node_t * dnp)3160Sstevel@tonic-gate dt_pragma_ident(const char *prname, dt_node_t *dnp)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	/* ignore any #ident or #pragma ident lines */
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static void
dt_pragma_option(const char * prname,dt_node_t * dnp)3220Sstevel@tonic-gate dt_pragma_option(const char *prname, dt_node_t *dnp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
3250Sstevel@tonic-gate 	char *opt, *val;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
3280Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
3290Sstevel@tonic-gate 		    "malformed #pragma %s <option>=<val>\n", prname);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (dnp->dn_list != NULL) {
3330Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
3340Sstevel@tonic-gate 		    "superfluous arguments specified for #pragma %s\n", prname);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 
337*13093SRoger.Faulkner@Oracle.COM 	opt = strdupa(dnp->dn_string);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if ((val = strchr(opt, '=')) != NULL)
3400Sstevel@tonic-gate 		*val++ = '\0';
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (dtrace_setopt(dtp, opt, val) == -1) {
3430Sstevel@tonic-gate 		if (val == NULL) {
3440Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
3450Sstevel@tonic-gate 			    "failed to set option '%s': %s\n", opt,
3460Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
3470Sstevel@tonic-gate 		} else {
3480Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
3490Sstevel@tonic-gate 			    "failed to set option '%s' to '%s': %s\n",
3500Sstevel@tonic-gate 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate  * The #line directive is used to reset the input line number and to optionally
3570Sstevel@tonic-gate  * note the file name for use in error messages.  Sun cpp(1) also produces a
3580Sstevel@tonic-gate  * third integer token after the filename which is one of the following:
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  * 0 - line change has nothing to do with an #include file
3610Sstevel@tonic-gate  * 1 - line change because we just entered a #include file
3620Sstevel@tonic-gate  * 2 - line change because we just exited a #include file
3630Sstevel@tonic-gate  *
3640Sstevel@tonic-gate  * We use these state tokens to adjust pcb_idepth, which in turn controls
3650Sstevel@tonic-gate  * whether type lookups access the global type space or not.
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate static void
dt_pragma_line(const char * prname,dt_node_t * dnp)3680Sstevel@tonic-gate dt_pragma_line(const char *prname, dt_node_t *dnp)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
3710Sstevel@tonic-gate 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
3740Sstevel@tonic-gate 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
3750Sstevel@tonic-gate 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
3760Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
3770Sstevel@tonic-gate 		    "<line> [ [\"file\"] state ]\n", prname);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * If a file is specified, free any old pcb_filetag and swap fnp's
3820Sstevel@tonic-gate 	 * dn_string into pcb_filetag as the new filename for error messages.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	if (fnp != NULL) {
3850Sstevel@tonic-gate 		if (yypcb->pcb_filetag != NULL)
3860Sstevel@tonic-gate 			free(yypcb->pcb_filetag);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		/*
3890Sstevel@tonic-gate 		 * This is not pretty, but is a necessary evil until we either
3900Sstevel@tonic-gate 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
3910Sstevel@tonic-gate 		 * the filename begins with /dev/fd, we know it's the master
3920Sstevel@tonic-gate 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
3930Sstevel@tonic-gate 		 * dt_filetag pointer so error messages refer to the main file.
3940Sstevel@tonic-gate 		 */
3950Sstevel@tonic-gate 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
3960Sstevel@tonic-gate 			yypcb->pcb_filetag = fnp->dn_string;
3970Sstevel@tonic-gate 			fnp->dn_string = NULL;
3980Sstevel@tonic-gate 		} else
3990Sstevel@tonic-gate 			yypcb->pcb_filetag = NULL;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (inp != NULL) {
4030Sstevel@tonic-gate 		if (inp->dn_value == 1)
4040Sstevel@tonic-gate 			yypcb->pcb_idepth++;
4050Sstevel@tonic-gate 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
4060Sstevel@tonic-gate 			yypcb->pcb_idepth--;
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	yylineno = dnp->dn_value;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * D compiler pragma types range from control directives to common pragmas to
4140Sstevel@tonic-gate  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
4150Sstevel@tonic-gate  * as a special prefix for our pragmas so they can be used in mixed headers.
4160Sstevel@tonic-gate  */
4170Sstevel@tonic-gate #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
4180Sstevel@tonic-gate #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
4190Sstevel@tonic-gate #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate static const struct dt_pragmadesc {
4220Sstevel@tonic-gate 	const char *dpd_name;
4230Sstevel@tonic-gate 	void (*dpd_func)(const char *, dt_node_t *);
4240Sstevel@tonic-gate 	int dpd_kind;
4250Sstevel@tonic-gate } dt_pragmas[] = {
4260Sstevel@tonic-gate 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
4270Sstevel@tonic-gate 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
4280Sstevel@tonic-gate 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
4290Sstevel@tonic-gate 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
4300Sstevel@tonic-gate 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
4310Sstevel@tonic-gate 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
4320Sstevel@tonic-gate 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
4330Sstevel@tonic-gate 	{ NULL, NULL }
4340Sstevel@tonic-gate };
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * Process a control line #directive by looking up the directive name in our
4380Sstevel@tonic-gate  * lookup table and invoking the corresponding function with the token list.
4390Sstevel@tonic-gate  * According to K&R[A12.9], we silently ignore null directive lines.
4400Sstevel@tonic-gate  */
4410Sstevel@tonic-gate void
dt_pragma(dt_node_t * pnp)4420Sstevel@tonic-gate dt_pragma(dt_node_t *pnp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	const struct dt_pragmadesc *dpd;
4450Sstevel@tonic-gate 	dt_node_t *dnp;
4460Sstevel@tonic-gate 	int kind = DT_PRAGMA_DIR;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
4490Sstevel@tonic-gate 		if (dnp->dn_kind == DT_NODE_INT) {
4500Sstevel@tonic-gate 			dt_pragma_line("line", dnp);
4510Sstevel@tonic-gate 			break;
4520Sstevel@tonic-gate 		}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if (dnp->dn_kind != DT_NODE_IDENT)
4550Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if (kind == DT_PRAGMA_DIR &&
4580Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "pragma") == 0) {
4590Sstevel@tonic-gate 			kind = DT_PRAGMA_SUB;
4600Sstevel@tonic-gate 			continue;
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		if (kind == DT_PRAGMA_SUB &&
4640Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "D") == 0) {
4650Sstevel@tonic-gate 			kind = DT_PRAGMA_DCP;
4660Sstevel@tonic-gate 			continue;
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
4700Sstevel@tonic-gate 			if (dpd->dpd_kind <= kind &&
4710Sstevel@tonic-gate 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
4720Sstevel@tonic-gate 				break;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		yylineno--; /* since we've already seen \n */
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (dpd->dpd_name != NULL) {
4780Sstevel@tonic-gate 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
4790Sstevel@tonic-gate 			yylineno++;
4800Sstevel@tonic-gate 			break;
4810Sstevel@tonic-gate 		}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 		switch (kind) {
4840Sstevel@tonic-gate 		case DT_PRAGMA_DIR:
4850Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
4860Sstevel@tonic-gate 			    "#%s\n", dnp->dn_string);
4870Sstevel@tonic-gate 			/*NOTREACHED*/
4880Sstevel@tonic-gate 		case DT_PRAGMA_SUB:
4890Sstevel@tonic-gate 			break; /* K&R[A12.8] says to ignore unknown pragmas */
4900Sstevel@tonic-gate 		case DT_PRAGMA_DCP:
4910Sstevel@tonic-gate 		default:
4920Sstevel@tonic-gate 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
4930Sstevel@tonic-gate 			    dnp->dn_string);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		yylineno++;
4970Sstevel@tonic-gate 		break;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	dt_node_list_free(&pnp);
5010Sstevel@tonic-gate }
502