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