xref: /titanic_50/usr/src/lib/libdtrace/common/dt_pragma.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <strings.h>
30 #include <alloca.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 #include <dt_parser.h>
35 #include <dt_impl.h>
36 #include <dt_provider.h>
37 #include <dt_module.h>
38 
39 /*
40  * This callback function is installed in a given identifier hash to search for
41  * and apply deferred pragmas that are pending for a given new identifier name.
42  * Multiple pragmas may be pending for a given name; we processs all of them.
43  */
44 /*ARGSUSED*/
45 static void
46 dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
47 {
48 	dt_idhash_t *php;
49 	dt_ident_t *pdp;
50 
51 	if ((php = yypcb->pcb_pragmas) == NULL)
52 		return; /* no pragmas pending for current compilation pass */
53 
54 	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
55 		switch (pdp->di_kind) {
56 		case DT_IDENT_PRAGAT:
57 			idp->di_attr = pdp->di_attr;
58 			break;
59 		case DT_IDENT_PRAGBN:
60 			idp->di_vers = pdp->di_vers;
61 			break;
62 		}
63 		dt_idhash_delete(php, pdp);
64 	}
65 }
66 
67 /*
68  * The #pragma attributes directive can be used to reset stability attributes
69  * on a global identifier or inline definition.  If the identifier is already
70  * defined, we can just change di_attr.  If not, we insert the pragma into a
71  * hash table of the current pcb's deferred pragmas for later processing.
72  */
73 static void
74 dt_pragma_attributes(const char *prname, dt_node_t *dnp)
75 {
76 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
77 	dtrace_attribute_t attr, *a;
78 	dt_provider_t *pvp;
79 	const char *name, *part;
80 	dt_ident_t *idp;
81 
82 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
83 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
84 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
85 		    "<attributes> <ident>\n", prname);
86 	}
87 
88 	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
89 		xyerror(D_PRAGMA_INVAL, "invalid attributes "
90 		    "specified by #pragma %s\n", prname);
91 	}
92 
93 	dnp = dnp->dn_list;
94 	name = dnp->dn_string;
95 
96 	if (strcmp(name, "provider") == 0) {
97 		dnp = dnp->dn_list;
98 		name = dnp->dn_string;
99 
100 		dnp = dnp->dn_list;
101 		part = dnp->dn_string;
102 
103 		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
104 			if (strcmp(part, "provider") == 0) {
105 				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
106 			} else if (strcmp(part, "module") == 0) {
107 				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
108 			} else if (strcmp(part, "function") == 0) {
109 				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
110 			} else if (strcmp(part, "name") == 0) {
111 				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
112 			} else if (strcmp(part, "args") == 0) {
113 				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
114 			} else {
115 				xyerror(D_PRAGMA_INVAL, "invalid component "
116 				    "\"%s\" in attribute #pragma "
117 				    "for provider %s\n", name, part);
118 			}
119 
120 			*a = attr;
121 			return;
122 		}
123 
124 	} else if ((idp = dt_idstack_lookup(
125 	    &yypcb->pcb_globals, name)) != NULL) {
126 
127 		if (idp->di_gen != dtp->dt_gen) {
128 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
129 			    "entity defined outside program scope\n", prname);
130 		}
131 
132 		idp->di_attr = attr;
133 		return;
134 	}
135 
136 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
137 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
138 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
139 
140 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
141 	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
142 
143 	if (idp == NULL)
144 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
145 
146 	if (dtp->dt_globals->dh_defer == NULL)
147 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
148 }
149 
150 /*
151  * The #pragma binding directive can be used to reset the version binding
152  * on a global identifier or inline definition.  If the identifier is already
153  * defined, we can just change di_vers.  If not, we insert the pragma into a
154  * hash table of the current pcb's deferred pragmas for later processing.
155  */
156 static void
157 dt_pragma_binding(const char *prname, dt_node_t *dnp)
158 {
159 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
160 	dt_version_t vers;
161 	const char *name;
162 	dt_ident_t *idp;
163 
164 	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
165 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
166 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
167 		    "\"version\" <ident>\n", prname);
168 	}
169 
170 	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
171 		xyerror(D_PRAGMA_INVAL, "invalid version string "
172 		    "specified by #pragma %s\n", prname);
173 	}
174 
175 	name = dnp->dn_list->dn_string;
176 	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
177 
178 	if (idp != NULL) {
179 		if (idp->di_gen != dtp->dt_gen) {
180 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
181 			    "entity defined outside program scope\n", prname);
182 		}
183 		idp->di_vers = vers;
184 		return;
185 	}
186 
187 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
188 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
189 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
190 
191 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
192 	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
193 
194 	if (idp == NULL)
195 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
196 
197 	if (dtp->dt_globals->dh_defer == NULL)
198 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
199 }
200 
201 /*
202  * The #pragma depends_on directive can be used to express a dependency on a
203  * module or provider, which if not present will cause processing to abort.
204  */
205 static void
206 dt_pragma_depends(const char *prname, dt_node_t *cnp)
207 {
208 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
209 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
210 	int found;
211 
212 	if (cnp == NULL || nnp == NULL ||
213 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
214 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
215 		    "<class> <name>\n", prname);
216 	}
217 
218 	if (strcmp(cnp->dn_string, "provider") == 0)
219 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
220 	else if (strcmp(cnp->dn_string, "module") == 0) {
221 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
222 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
223 	} else {
224 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
225 		    "specified by #pragma %s\n", cnp->dn_string, prname);
226 	}
227 
228 	if (!found) {
229 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
230 		    cnp->dn_string, nnp->dn_string);
231 	}
232 }
233 
234 /*
235  * The #pragma error directive can be followed by any list of tokens, which we
236  * just concatenate and print as part of our error message.
237  */
238 static void
239 dt_pragma_error(const char *prname, dt_node_t *dnp)
240 {
241 	dt_node_t *enp;
242 	size_t n = 0;
243 	char *s;
244 
245 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
246 		if (enp->dn_kind == DT_NODE_IDENT ||
247 		    enp->dn_kind == DT_NODE_STRING)
248 			n += strlen(enp->dn_string) + 1;
249 	}
250 
251 	s = alloca(n + 1);
252 	s[0] = '\0';
253 
254 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
255 		if (enp->dn_kind == DT_NODE_IDENT ||
256 		    enp->dn_kind == DT_NODE_STRING) {
257 			(void) strcat(s, enp->dn_string);
258 			(void) strcat(s, " ");
259 		}
260 	}
261 
262 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
263 }
264 
265 /*ARGSUSED*/
266 static void
267 dt_pragma_ident(const char *prname, dt_node_t *dnp)
268 {
269 	/* ignore any #ident or #pragma ident lines */
270 }
271 
272 static void
273 dt_pragma_option(const char *prname, dt_node_t *dnp)
274 {
275 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
276 	char *opt, *val;
277 
278 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
279 		xyerror(D_PRAGMA_MALFORM,
280 		    "malformed #pragma %s <option>=<val>\n", prname);
281 	}
282 
283 	if (dnp->dn_list != NULL) {
284 		xyerror(D_PRAGMA_MALFORM,
285 		    "superfluous arguments specified for #pragma %s\n", prname);
286 	}
287 
288 	opt = alloca(strlen(dnp->dn_string) + 1);
289 	(void) strcpy(opt, dnp->dn_string);
290 
291 	if ((val = strchr(opt, '=')) != NULL)
292 		*val++ = '\0';
293 
294 	if (dtrace_setopt(dtp, opt, val) == -1) {
295 		if (val == NULL) {
296 			xyerror(D_PRAGMA_OPTSET,
297 			    "failed to set option '%s': %s\n", opt,
298 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
299 		} else {
300 			xyerror(D_PRAGMA_OPTSET,
301 			    "failed to set option '%s' to '%s': %s\n",
302 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
303 		}
304 	}
305 }
306 
307 /*
308  * The #line directive is used to reset the input line number and to optionally
309  * note the file name for use in error messages.  Sun cpp(1) also produces a
310  * third integer token after the filename which is one of the following:
311  *
312  * 0 - line change has nothing to do with an #include file
313  * 1 - line change because we just entered a #include file
314  * 2 - line change because we just exited a #include file
315  *
316  * We use these state tokens to adjust pcb_idepth, which in turn controls
317  * whether type lookups access the global type space or not.
318  */
319 static void
320 dt_pragma_line(const char *prname, dt_node_t *dnp)
321 {
322 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
323 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
324 
325 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
326 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
327 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
328 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
329 		    "<line> [ [\"file\"] state ]\n", prname);
330 	}
331 
332 	/*
333 	 * If a file is specified, free any old pcb_filetag and swap fnp's
334 	 * dn_string into pcb_filetag as the new filename for error messages.
335 	 */
336 	if (fnp != NULL) {
337 		if (yypcb->pcb_filetag != NULL)
338 			free(yypcb->pcb_filetag);
339 
340 		/*
341 		 * This is not pretty, but is a necessary evil until we either
342 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
343 		 * the filename begins with /dev/fd, we know it's the master
344 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
345 		 * dt_filetag pointer so error messages refer to the main file.
346 		 */
347 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
348 			yypcb->pcb_filetag = fnp->dn_string;
349 			fnp->dn_string = NULL;
350 		} else
351 			yypcb->pcb_filetag = NULL;
352 	}
353 
354 	if (inp != NULL) {
355 		if (inp->dn_value == 1)
356 			yypcb->pcb_idepth++;
357 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
358 			yypcb->pcb_idepth--;
359 	}
360 
361 	yylineno = dnp->dn_value;
362 }
363 
364 /*
365  * D compiler pragma types range from control directives to common pragmas to
366  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
367  * as a special prefix for our pragmas so they can be used in mixed headers.
368  */
369 #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
370 #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
371 #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
372 
373 static const struct dt_pragmadesc {
374 	const char *dpd_name;
375 	void (*dpd_func)(const char *, dt_node_t *);
376 	int dpd_kind;
377 } dt_pragmas[] = {
378 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
379 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
380 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
381 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
382 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
383 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
384 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
385 	{ NULL, NULL }
386 };
387 
388 /*
389  * Process a control line #directive by looking up the directive name in our
390  * lookup table and invoking the corresponding function with the token list.
391  * According to K&R[A12.9], we silently ignore null directive lines.
392  */
393 void
394 dt_pragma(dt_node_t *pnp)
395 {
396 	const struct dt_pragmadesc *dpd;
397 	dt_node_t *dnp;
398 	int kind = DT_PRAGMA_DIR;
399 
400 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
401 		if (dnp->dn_kind == DT_NODE_INT) {
402 			dt_pragma_line("line", dnp);
403 			break;
404 		}
405 
406 		if (dnp->dn_kind != DT_NODE_IDENT)
407 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
408 
409 		if (kind == DT_PRAGMA_DIR &&
410 		    strcmp(dnp->dn_string, "pragma") == 0) {
411 			kind = DT_PRAGMA_SUB;
412 			continue;
413 		}
414 
415 		if (kind == DT_PRAGMA_SUB &&
416 		    strcmp(dnp->dn_string, "D") == 0) {
417 			kind = DT_PRAGMA_DCP;
418 			continue;
419 		}
420 
421 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
422 			if (dpd->dpd_kind <= kind &&
423 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
424 				break;
425 		}
426 
427 		yylineno--; /* since we've already seen \n */
428 
429 		if (dpd->dpd_name != NULL) {
430 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
431 			yylineno++;
432 			break;
433 		}
434 
435 		switch (kind) {
436 		case DT_PRAGMA_DIR:
437 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
438 			    "#%s\n", dnp->dn_string);
439 			/*NOTREACHED*/
440 		case DT_PRAGMA_SUB:
441 			break; /* K&R[A12.8] says to ignore unknown pragmas */
442 		case DT_PRAGMA_DCP:
443 		default:
444 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
445 			    dnp->dn_string);
446 		}
447 
448 		yylineno++;
449 		break;
450 	}
451 
452 	dt_node_list_free(&pnp);
453 }
454