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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/types.h>
41 #include <dt_parser.h>
43 #include <dt_provider.h>
44 #include <dt_module.h>
47 * This callback function is installed in a given identifier hash to search for
48 * and apply deferred pragmas that are pending for a given new identifier name.
49 * Multiple pragmas may be pending for a given name; we processs all of them.
53 dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
58 if ((php = yypcb->pcb_pragmas) == NULL)
59 return; /* no pragmas pending for current compilation pass */
61 while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
62 switch (pdp->di_kind) {
64 idp->di_attr = pdp->di_attr;
67 idp->di_vers = pdp->di_vers;
70 dt_idhash_delete(php, pdp);
75 * The #pragma attributes directive can be used to reset stability attributes
76 * on a global identifier or inline definition. If the identifier is already
77 * defined, we can just change di_attr. If not, we insert the pragma into a
78 * hash table of the current pcb's deferred pragmas for later processing.
81 dt_pragma_attributes(const char *prname, dt_node_t *dnp)
83 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
84 dtrace_attribute_t attr, *a;
86 const char *name, *part;
89 if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
90 dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
91 xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
92 "<attributes> <ident>\n", prname);
95 if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
96 xyerror(D_PRAGMA_INVAL, "invalid attributes "
97 "specified by #pragma %s\n", prname);
101 name = dnp->dn_string;
103 if (strcmp(name, "provider") == 0) {
105 name = dnp->dn_string;
108 part = dnp->dn_string;
110 if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
111 if (strcmp(part, "provider") == 0) {
112 a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
113 } else if (strcmp(part, "module") == 0) {
114 a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
115 } else if (strcmp(part, "function") == 0) {
116 a = &pvp->pv_desc.dtvd_attr.dtpa_func;
117 } else if (strcmp(part, "name") == 0) {
118 a = &pvp->pv_desc.dtvd_attr.dtpa_name;
119 } else if (strcmp(part, "args") == 0) {
120 a = &pvp->pv_desc.dtvd_attr.dtpa_args;
122 xyerror(D_PRAGMA_INVAL, "invalid component "
123 "\"%s\" in attribute #pragma "
124 "for provider %s\n", name, part);
131 } else if ((idp = dt_idstack_lookup(
132 &yypcb->pcb_globals, name)) != NULL) {
134 if (idp->di_gen != dtp->dt_gen) {
135 xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
136 "entity defined outside program scope\n", prname);
143 if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
144 dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
145 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
147 idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
148 attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
151 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
153 if (dtp->dt_globals->dh_defer == NULL)
154 dtp->dt_globals->dh_defer = &dt_pragma_apply;
158 * The #pragma binding directive can be used to reset the version binding
159 * on a global identifier or inline definition. If the identifier is already
160 * defined, we can just change di_vers. If not, we insert the pragma into a
161 * hash table of the current pcb's deferred pragmas for later processing.
164 dt_pragma_binding(const char *prname, dt_node_t *dnp)
166 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
171 if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
172 dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
173 xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
174 "\"version\" <ident>\n", prname);
177 if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
178 xyerror(D_PRAGMA_INVAL, "invalid version string "
179 "specified by #pragma %s\n", prname);
182 name = dnp->dn_list->dn_string;
183 idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
186 if (idp->di_gen != dtp->dt_gen) {
187 xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
188 "entity defined outside program scope\n", prname);
194 if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
195 dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
196 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
198 idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
199 _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
202 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
204 if (dtp->dt_globals->dh_defer == NULL)
205 dtp->dt_globals->dh_defer = &dt_pragma_apply;
209 dt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
216 for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
217 dirp = dt_list_next(dirp)) {
218 (void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
220 if (stat(lib, &sbuf) == 0) {
227 xyerror(D_PRAGMA_DEPEND,
228 "failed to find dependency in libpath: %s", lname);
232 * The #pragma depends_on directive can be used to express a dependency on a
233 * module, provider or library which if not present will cause processing to
237 dt_pragma_depends(const char *prname, dt_node_t *cnp)
239 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
240 dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
242 dt_lib_depend_t *dld;
243 char lib[MAXPATHLEN];
245 char *provs, *cpy, *tok;
247 if (cnp == NULL || nnp == NULL ||
248 cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
249 xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
250 "<class> <name>\n", prname);
253 if (strcmp(cnp->dn_string, "provider") == 0) {
255 * First try to get the provider list using the
256 * debug.dtrace.providers sysctl, since that'll work even if
257 * we're not running as root.
260 if (sysctlbyname("debug.dtrace.providers", NULL, &plen, NULL, 0) ||
261 ((provs = dt_alloc(dtp, plen)) == NULL) ||
262 sysctlbyname("debug.dtrace.providers", provs, &plen, NULL, 0))
263 found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
266 for (cpy = provs; (tok = strsep(&cpy, " ")) != NULL; )
267 if (strcmp(tok, nnp->dn_string) == 0) {
271 if (found == B_FALSE)
272 found = dt_provider_lookup(dtp,
273 nnp->dn_string) != NULL;
277 } else if (strcmp(cnp->dn_string, "module") == 0) {
278 dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
279 found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
280 } else if (strcmp(cnp->dn_string, "library") == 0) {
281 if (yypcb->pcb_cflags & DTRACE_C_CTL) {
282 assert(dtp->dt_filetag != NULL);
284 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
287 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
291 if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
293 xyerror(D_PRAGMA_DEPEND,
294 "failed to add dependency %s:%s\n", lib,
295 dtrace_errmsg(dtp, dtrace_errno(dtp)));
299 * By this point we have already performed a topological
300 * sort of the dependencies; we process this directive
301 * as satisfied as long as the dependency was properly
304 if (dtp->dt_filetag == NULL)
305 xyerror(D_PRAGMA_DEPEND, "main program may "
306 "not explicitly depend on a library");
308 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
312 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
314 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
318 if (!dld->dtld_loaded)
319 xyerror(D_PRAGMA_DEPEND, "program requires "
320 "library \"%s\" which failed to load",
326 xyerror(D_PRAGMA_INVAL, "invalid class %s "
327 "specified by #pragma %s\n", cnp->dn_string, prname);
331 xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
332 cnp->dn_string, nnp->dn_string);
337 * The #pragma error directive can be followed by any list of tokens, which we
338 * just concatenate and print as part of our error message.
341 dt_pragma_error(const char *prname, dt_node_t *dnp)
347 for (enp = dnp; enp != NULL; enp = enp->dn_list) {
348 if (enp->dn_kind == DT_NODE_IDENT ||
349 enp->dn_kind == DT_NODE_STRING)
350 n += strlen(enp->dn_string) + 1;
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 (void) strcat(s, enp->dn_string);
360 (void) strcat(s, " ");
364 xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
369 dt_pragma_ident(const char *prname, dt_node_t *dnp)
371 /* ignore any #ident or #pragma ident lines */
375 dt_pragma_option(const char *prname, dt_node_t *dnp)
377 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
380 if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
381 xyerror(D_PRAGMA_MALFORM,
382 "malformed #pragma %s <option>=<val>\n", prname);
385 if (dnp->dn_list != NULL) {
386 xyerror(D_PRAGMA_MALFORM,
387 "superfluous arguments specified for #pragma %s\n", prname);
390 opt = alloca(strlen(dnp->dn_string) + 1);
391 (void) strcpy(opt, dnp->dn_string);
393 if ((val = strchr(opt, '=')) != NULL)
396 if (dtrace_setopt(dtp, opt, val) == -1) {
398 xyerror(D_PRAGMA_OPTSET,
399 "failed to set option '%s': %s\n", opt,
400 dtrace_errmsg(dtp, dtrace_errno(dtp)));
402 xyerror(D_PRAGMA_OPTSET,
403 "failed to set option '%s' to '%s': %s\n",
404 opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
410 * The #line directive is used to reset the input line number and to optionally
411 * note the file name for use in error messages. Sun cpp(1) also produces a
412 * third integer token after the filename which is one of the following:
414 * 0 - line change has nothing to do with an #include file
415 * 1 - line change because we just entered a #include file
416 * 2 - line change because we just exited a #include file
418 * We use these state tokens to adjust pcb_idepth, which in turn controls
419 * whether type lookups access the global type space or not.
422 dt_pragma_line(const char *prname, dt_node_t *dnp)
424 dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
425 dt_node_t *inp = fnp ? fnp->dn_list : NULL;
427 if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
428 (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
429 (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
430 xyerror(D_PRAGMA_MALFORM, "malformed #%s "
431 "<line> [ [\"file\"] state ]\n", prname);
435 * If a file is specified, free any old pcb_filetag and swap fnp's
436 * dn_string into pcb_filetag as the new filename for error messages.
439 if (yypcb->pcb_filetag != NULL)
440 free(yypcb->pcb_filetag);
443 * This is not pretty, but is a necessary evil until we either
444 * write "dpp" or get a useful standalone cpp from DevPro. If
445 * the filename begins with /dev/fd, we know it's the master
446 * input file (see dt_preproc() in dt_cc.c), so just clear the
447 * dt_filetag pointer so error messages refer to the main file.
449 if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
450 yypcb->pcb_filetag = fnp->dn_string;
451 fnp->dn_string = NULL;
453 yypcb->pcb_filetag = NULL;
457 if (inp->dn_value == 1)
459 else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
463 yylineno = dnp->dn_value;
467 * D compiler pragma types range from control directives to common pragmas to
468 * D custom pragmas, in order of specificity. Similar to gcc, we use #pragma D
469 * as a special prefix for our pragmas so they can be used in mixed headers.
471 #define DT_PRAGMA_DIR 0 /* pragma directive may be used after naked # */
472 #define DT_PRAGMA_SUB 1 /* pragma directive may be used after #pragma */
473 #define DT_PRAGMA_DCP 2 /* pragma may only be used after #pragma D */
475 static const struct dt_pragmadesc {
476 const char *dpd_name;
477 void (*dpd_func)(const char *, dt_node_t *);
480 { "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
481 { "binding", dt_pragma_binding, DT_PRAGMA_DCP },
482 { "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
483 { "error", dt_pragma_error, DT_PRAGMA_DIR },
484 { "ident", dt_pragma_ident, DT_PRAGMA_DIR },
485 { "line", dt_pragma_line, DT_PRAGMA_DIR },
486 { "option", dt_pragma_option, DT_PRAGMA_DCP },
491 * Process a control line #directive by looking up the directive name in our
492 * lookup table and invoking the corresponding function with the token list.
493 * According to K&R[A12.9], we silently ignore null directive lines.
496 dt_pragma(dt_node_t *pnp)
498 const struct dt_pragmadesc *dpd;
500 int kind = DT_PRAGMA_DIR;
502 for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
503 if (dnp->dn_kind == DT_NODE_INT) {
504 dt_pragma_line("line", dnp);
508 if (dnp->dn_kind != DT_NODE_IDENT)
509 xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
511 if (kind == DT_PRAGMA_DIR &&
512 strcmp(dnp->dn_string, "pragma") == 0) {
513 kind = DT_PRAGMA_SUB;
517 if (kind == DT_PRAGMA_SUB &&
518 strcmp(dnp->dn_string, "D") == 0) {
519 kind = DT_PRAGMA_DCP;
523 for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
524 if (dpd->dpd_kind <= kind &&
525 strcmp(dpd->dpd_name, dnp->dn_string) == 0)
529 yylineno--; /* since we've already seen \n */
531 if (dpd->dpd_name != NULL) {
532 dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
539 xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
540 "#%s\n", dnp->dn_string);
543 break; /* K&R[A12.8] says to ignore unknown pragmas */
546 xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
554 dt_node_list_free(&pnp);