xref: /titanic_50/usr/src/lib/libdtrace/common/dt_pragma.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <assert.h>
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, provider or library which if not present will cause processing to
204  * abort.
205  */
206 static void
207 dt_pragma_depends(const char *prname, dt_node_t *cnp)
208 {
209 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
210 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
211 	int found;
212 	dt_lib_depend_t *dld;
213 
214 	if (cnp == NULL || nnp == NULL ||
215 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
216 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
217 		    "<class> <name>\n", prname);
218 	}
219 
220 	if (strcmp(cnp->dn_string, "provider") == 0)
221 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
222 	else if (strcmp(cnp->dn_string, "module") == 0) {
223 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
224 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
225 	} else if (strcmp(cnp->dn_string, "library") == 0) {
226 
227 		/*
228 		 * We have the file we are working on in dtp->dt_filetag
229 		 * so find that node and add the dependency in.
230 		 */
231 		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
232 			char lib[MAXPATHLEN];
233 
234 			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
235 			    dtp->dt_filetag);
236 			assert(dld != NULL);
237 
238 			(void) snprintf(lib, MAXPATHLEN, "%s%s",
239 			    dld->dtld_libpath, nnp->dn_string);
240 			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
241 			    lib)) != 0) {
242 				xyerror(D_PRAGMA_DEPEND,
243 				    "failed to add dependency %s:%s\n",
244 				    lib,
245 				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
246 			}
247 		}
248 		found = 1;
249 	} else {
250 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
251 		    "specified by #pragma %s\n", cnp->dn_string, prname);
252 	}
253 
254 	if (!found) {
255 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
256 		    cnp->dn_string, nnp->dn_string);
257 	}
258 }
259 
260 /*
261  * The #pragma error directive can be followed by any list of tokens, which we
262  * just concatenate and print as part of our error message.
263  */
264 static void
265 dt_pragma_error(const char *prname, dt_node_t *dnp)
266 {
267 	dt_node_t *enp;
268 	size_t n = 0;
269 	char *s;
270 
271 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
272 		if (enp->dn_kind == DT_NODE_IDENT ||
273 		    enp->dn_kind == DT_NODE_STRING)
274 			n += strlen(enp->dn_string) + 1;
275 	}
276 
277 	s = alloca(n + 1);
278 	s[0] = '\0';
279 
280 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
281 		if (enp->dn_kind == DT_NODE_IDENT ||
282 		    enp->dn_kind == DT_NODE_STRING) {
283 			(void) strcat(s, enp->dn_string);
284 			(void) strcat(s, " ");
285 		}
286 	}
287 
288 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
289 }
290 
291 /*ARGSUSED*/
292 static void
293 dt_pragma_ident(const char *prname, dt_node_t *dnp)
294 {
295 	/* ignore any #ident or #pragma ident lines */
296 }
297 
298 static void
299 dt_pragma_option(const char *prname, dt_node_t *dnp)
300 {
301 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
302 	char *opt, *val;
303 
304 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
305 		xyerror(D_PRAGMA_MALFORM,
306 		    "malformed #pragma %s <option>=<val>\n", prname);
307 	}
308 
309 	if (dnp->dn_list != NULL) {
310 		xyerror(D_PRAGMA_MALFORM,
311 		    "superfluous arguments specified for #pragma %s\n", prname);
312 	}
313 
314 	opt = alloca(strlen(dnp->dn_string) + 1);
315 	(void) strcpy(opt, dnp->dn_string);
316 
317 	if ((val = strchr(opt, '=')) != NULL)
318 		*val++ = '\0';
319 
320 	if (dtrace_setopt(dtp, opt, val) == -1) {
321 		if (val == NULL) {
322 			xyerror(D_PRAGMA_OPTSET,
323 			    "failed to set option '%s': %s\n", opt,
324 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
325 		} else {
326 			xyerror(D_PRAGMA_OPTSET,
327 			    "failed to set option '%s' to '%s': %s\n",
328 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
329 		}
330 	}
331 }
332 
333 /*
334  * The #line directive is used to reset the input line number and to optionally
335  * note the file name for use in error messages.  Sun cpp(1) also produces a
336  * third integer token after the filename which is one of the following:
337  *
338  * 0 - line change has nothing to do with an #include file
339  * 1 - line change because we just entered a #include file
340  * 2 - line change because we just exited a #include file
341  *
342  * We use these state tokens to adjust pcb_idepth, which in turn controls
343  * whether type lookups access the global type space or not.
344  */
345 static void
346 dt_pragma_line(const char *prname, dt_node_t *dnp)
347 {
348 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
349 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
350 
351 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
352 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
353 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
354 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
355 		    "<line> [ [\"file\"] state ]\n", prname);
356 	}
357 
358 	/*
359 	 * If a file is specified, free any old pcb_filetag and swap fnp's
360 	 * dn_string into pcb_filetag as the new filename for error messages.
361 	 */
362 	if (fnp != NULL) {
363 		if (yypcb->pcb_filetag != NULL)
364 			free(yypcb->pcb_filetag);
365 
366 		/*
367 		 * This is not pretty, but is a necessary evil until we either
368 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
369 		 * the filename begins with /dev/fd, we know it's the master
370 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
371 		 * dt_filetag pointer so error messages refer to the main file.
372 		 */
373 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
374 			yypcb->pcb_filetag = fnp->dn_string;
375 			fnp->dn_string = NULL;
376 		} else
377 			yypcb->pcb_filetag = NULL;
378 	}
379 
380 	if (inp != NULL) {
381 		if (inp->dn_value == 1)
382 			yypcb->pcb_idepth++;
383 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
384 			yypcb->pcb_idepth--;
385 	}
386 
387 	yylineno = dnp->dn_value;
388 }
389 
390 /*
391  * D compiler pragma types range from control directives to common pragmas to
392  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
393  * as a special prefix for our pragmas so they can be used in mixed headers.
394  */
395 #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
396 #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
397 #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
398 
399 static const struct dt_pragmadesc {
400 	const char *dpd_name;
401 	void (*dpd_func)(const char *, dt_node_t *);
402 	int dpd_kind;
403 } dt_pragmas[] = {
404 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
405 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
406 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
407 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
408 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
409 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
410 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
411 	{ NULL, NULL }
412 };
413 
414 /*
415  * Process a control line #directive by looking up the directive name in our
416  * lookup table and invoking the corresponding function with the token list.
417  * According to K&R[A12.9], we silently ignore null directive lines.
418  */
419 void
420 dt_pragma(dt_node_t *pnp)
421 {
422 	const struct dt_pragmadesc *dpd;
423 	dt_node_t *dnp;
424 	int kind = DT_PRAGMA_DIR;
425 
426 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
427 		if (dnp->dn_kind == DT_NODE_INT) {
428 			dt_pragma_line("line", dnp);
429 			break;
430 		}
431 
432 		if (dnp->dn_kind != DT_NODE_IDENT)
433 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
434 
435 		if (kind == DT_PRAGMA_DIR &&
436 		    strcmp(dnp->dn_string, "pragma") == 0) {
437 			kind = DT_PRAGMA_SUB;
438 			continue;
439 		}
440 
441 		if (kind == DT_PRAGMA_SUB &&
442 		    strcmp(dnp->dn_string, "D") == 0) {
443 			kind = DT_PRAGMA_DCP;
444 			continue;
445 		}
446 
447 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
448 			if (dpd->dpd_kind <= kind &&
449 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
450 				break;
451 		}
452 
453 		yylineno--; /* since we've already seen \n */
454 
455 		if (dpd->dpd_name != NULL) {
456 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
457 			yylineno++;
458 			break;
459 		}
460 
461 		switch (kind) {
462 		case DT_PRAGMA_DIR:
463 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
464 			    "#%s\n", dnp->dn_string);
465 			/*NOTREACHED*/
466 		case DT_PRAGMA_SUB:
467 			break; /* K&R[A12.8] says to ignore unknown pragmas */
468 		case DT_PRAGMA_DCP:
469 		default:
470 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
471 			    dnp->dn_string);
472 		}
473 
474 		yylineno++;
475 		break;
476 	}
477 
478 	dt_node_list_free(&pnp);
479 }
480