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