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 #if defined(sun) 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/stat.h> 40 41 #include <dt_parser.h> 42 #include <dt_impl.h> 43 #include <dt_provider.h> 44 #include <dt_module.h> 45 46 /* 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. 50 */ 51 /*ARGSUSED*/ 52 static void 53 dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp) 54 { 55 dt_idhash_t *php; 56 dt_ident_t *pdp; 57 58 if ((php = yypcb->pcb_pragmas) == NULL) 59 return; /* no pragmas pending for current compilation pass */ 60 61 while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) { 62 switch (pdp->di_kind) { 63 case DT_IDENT_PRAGAT: 64 idp->di_attr = pdp->di_attr; 65 break; 66 case DT_IDENT_PRAGBN: 67 idp->di_vers = pdp->di_vers; 68 break; 69 } 70 dt_idhash_delete(php, pdp); 71 } 72 } 73 74 /* 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. 79 */ 80 static void 81 dt_pragma_attributes(const char *prname, dt_node_t *dnp) 82 { 83 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 84 dtrace_attribute_t attr, *a; 85 dt_provider_t *pvp; 86 const char *name, *part; 87 dt_ident_t *idp; 88 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); 93 } 94 95 if (dtrace_str2attr(dnp->dn_string, &attr) == -1) { 96 xyerror(D_PRAGMA_INVAL, "invalid attributes " 97 "specified by #pragma %s\n", prname); 98 } 99 100 dnp = dnp->dn_list; 101 name = dnp->dn_string; 102 103 if (strcmp(name, "provider") == 0) { 104 dnp = dnp->dn_list; 105 name = dnp->dn_string; 106 107 dnp = dnp->dn_list; 108 part = dnp->dn_string; 109 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; 121 } else { 122 xyerror(D_PRAGMA_INVAL, "invalid component " 123 "\"%s\" in attribute #pragma " 124 "for provider %s\n", name, part); 125 } 126 127 *a = attr; 128 return; 129 } 130 131 } else if ((idp = dt_idstack_lookup( 132 &yypcb->pcb_globals, name)) != NULL) { 133 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); 137 } 138 139 idp->di_attr = attr; 140 return; 141 } 142 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); 146 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); 149 150 if (idp == NULL) 151 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 152 153 if (dtp->dt_globals->dh_defer == NULL) 154 dtp->dt_globals->dh_defer = &dt_pragma_apply; 155 } 156 157 /* 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. 162 */ 163 static void 164 dt_pragma_binding(const char *prname, dt_node_t *dnp) 165 { 166 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 167 dt_version_t vers; 168 const char *name; 169 dt_ident_t *idp; 170 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); 175 } 176 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); 180 } 181 182 name = dnp->dn_list->dn_string; 183 idp = dt_idstack_lookup(&yypcb->pcb_globals, name); 184 185 if (idp != NULL) { 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); 189 } 190 idp->di_vers = vers; 191 return; 192 } 193 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); 197 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); 200 201 if (idp == NULL) 202 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 203 204 if (dtp->dt_globals->dh_defer == NULL) 205 dtp->dt_globals->dh_defer = &dt_pragma_apply; 206 } 207 208 static void 209 dt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib, 210 size_t len) 211 { 212 dt_dirpath_t *dirp; 213 struct stat sbuf; 214 int found = 0; 215 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); 219 220 if (stat(lib, &sbuf) == 0) { 221 found = 1; 222 break; 223 } 224 } 225 226 if (!found) 227 xyerror(D_PRAGMA_DEPEND, 228 "failed to find dependency in libpath: %s", lname); 229 } 230 231 /* 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 234 * abort. 235 */ 236 static void 237 dt_pragma_depends(const char *prname, dt_node_t *cnp) 238 { 239 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 240 dt_node_t *nnp = cnp ? cnp->dn_list : NULL; 241 int found; 242 dt_lib_depend_t *dld; 243 char lib[MAXPATHLEN]; 244 size_t plen; 245 char *provs, *cpy, *tok; 246 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); 251 } 252 253 if (strcmp(cnp->dn_string, "provider") == 0) { 254 /* 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. 258 */ 259 provs = NULL; 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; 264 else { 265 found = B_FALSE; 266 for (cpy = provs; (tok = strsep(&cpy, " ")) != NULL; ) 267 if (strcmp(tok, nnp->dn_string) == 0) { 268 found = B_TRUE; 269 break; 270 } 271 if (found == B_FALSE) 272 found = dt_provider_lookup(dtp, 273 nnp->dn_string) != NULL; 274 } 275 if (provs != NULL) 276 dt_free(dtp, provs); 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); 283 284 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, 285 sizeof (lib)); 286 287 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, 288 dtp->dt_filetag); 289 assert(dld != NULL); 290 291 if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies, 292 lib)) != 0) { 293 xyerror(D_PRAGMA_DEPEND, 294 "failed to add dependency %s:%s\n", lib, 295 dtrace_errmsg(dtp, dtrace_errno(dtp))); 296 } 297 } else { 298 /* 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 302 * loaded. 303 */ 304 if (dtp->dt_filetag == NULL) 305 xyerror(D_PRAGMA_DEPEND, "main program may " 306 "not explicitly depend on a library"); 307 308 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, 309 dtp->dt_filetag); 310 assert(dld != NULL); 311 312 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, 313 sizeof (lib)); 314 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted, 315 lib); 316 assert(dld != NULL); 317 318 if (!dld->dtld_loaded) 319 xyerror(D_PRAGMA_DEPEND, "program requires " 320 "library \"%s\" which failed to load", 321 lib); 322 } 323 324 found = B_TRUE; 325 } else { 326 xyerror(D_PRAGMA_INVAL, "invalid class %s " 327 "specified by #pragma %s\n", cnp->dn_string, prname); 328 } 329 330 if (!found) { 331 xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n", 332 cnp->dn_string, nnp->dn_string); 333 } 334 } 335 336 /* 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. 339 */ 340 static void 341 dt_pragma_error(const char *prname, dt_node_t *dnp) 342 { 343 dt_node_t *enp; 344 size_t n = 0; 345 char *s; 346 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; 351 } 352 353 s = alloca(n + 1); 354 s[0] = '\0'; 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 (void) strcat(s, enp->dn_string); 360 (void) strcat(s, " "); 361 } 362 } 363 364 xyerror(D_PRAGERR, "#%s: %s\n", prname, s); 365 } 366 367 /*ARGSUSED*/ 368 static void 369 dt_pragma_ident(const char *prname, dt_node_t *dnp) 370 { 371 /* ignore any #ident or #pragma ident lines */ 372 } 373 374 static void 375 dt_pragma_option(const char *prname, dt_node_t *dnp) 376 { 377 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 378 char *opt, *val; 379 380 if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) { 381 xyerror(D_PRAGMA_MALFORM, 382 "malformed #pragma %s <option>=<val>\n", prname); 383 } 384 385 if (dnp->dn_list != NULL) { 386 xyerror(D_PRAGMA_MALFORM, 387 "superfluous arguments specified for #pragma %s\n", prname); 388 } 389 390 opt = alloca(strlen(dnp->dn_string) + 1); 391 (void) strcpy(opt, dnp->dn_string); 392 393 if ((val = strchr(opt, '=')) != NULL) 394 *val++ = '\0'; 395 396 if (dtrace_setopt(dtp, opt, val) == -1) { 397 if (val == NULL) { 398 xyerror(D_PRAGMA_OPTSET, 399 "failed to set option '%s': %s\n", opt, 400 dtrace_errmsg(dtp, dtrace_errno(dtp))); 401 } else { 402 xyerror(D_PRAGMA_OPTSET, 403 "failed to set option '%s' to '%s': %s\n", 404 opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp))); 405 } 406 } 407 } 408 409 /* 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: 413 * 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 417 * 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. 420 */ 421 static void 422 dt_pragma_line(const char *prname, dt_node_t *dnp) 423 { 424 dt_node_t *fnp = dnp ? dnp->dn_list : NULL; 425 dt_node_t *inp = fnp ? fnp->dn_list : NULL; 426 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); 432 } 433 434 /* 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. 437 */ 438 if (fnp != NULL) { 439 if (yypcb->pcb_filetag != NULL) 440 free(yypcb->pcb_filetag); 441 442 /* 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. 448 */ 449 if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) { 450 yypcb->pcb_filetag = fnp->dn_string; 451 fnp->dn_string = NULL; 452 } else 453 yypcb->pcb_filetag = NULL; 454 } 455 456 if (inp != NULL) { 457 if (inp->dn_value == 1) 458 yypcb->pcb_idepth++; 459 else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0) 460 yypcb->pcb_idepth--; 461 } 462 463 yylineno = dnp->dn_value; 464 } 465 466 /* 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. 470 */ 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 */ 474 475 static const struct dt_pragmadesc { 476 const char *dpd_name; 477 void (*dpd_func)(const char *, dt_node_t *); 478 int dpd_kind; 479 } dt_pragmas[] = { 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 }, 487 { NULL, NULL } 488 }; 489 490 /* 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. 494 */ 495 void 496 dt_pragma(dt_node_t *pnp) 497 { 498 const struct dt_pragmadesc *dpd; 499 dt_node_t *dnp; 500 int kind = DT_PRAGMA_DIR; 501 502 for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) { 503 if (dnp->dn_kind == DT_NODE_INT) { 504 dt_pragma_line("line", dnp); 505 break; 506 } 507 508 if (dnp->dn_kind != DT_NODE_IDENT) 509 xyerror(D_PRAGCTL_INVAL, "invalid control directive\n"); 510 511 if (kind == DT_PRAGMA_DIR && 512 strcmp(dnp->dn_string, "pragma") == 0) { 513 kind = DT_PRAGMA_SUB; 514 continue; 515 } 516 517 if (kind == DT_PRAGMA_SUB && 518 strcmp(dnp->dn_string, "D") == 0) { 519 kind = DT_PRAGMA_DCP; 520 continue; 521 } 522 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) 526 break; 527 } 528 529 yylineno--; /* since we've already seen \n */ 530 531 if (dpd->dpd_name != NULL) { 532 dpd->dpd_func(dpd->dpd_name, dnp->dn_list); 533 yylineno++; 534 break; 535 } 536 537 switch (kind) { 538 case DT_PRAGMA_DIR: 539 xyerror(D_PRAGCTL_INVAL, "invalid control directive: " 540 "#%s\n", dnp->dn_string); 541 /*NOTREACHED*/ 542 case DT_PRAGMA_SUB: 543 break; /* K&R[A12.8] says to ignore unknown pragmas */ 544 case DT_PRAGMA_DCP: 545 default: 546 xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n", 547 dnp->dn_string); 548 } 549 550 yylineno++; 551 break; 552 } 553 554 dt_node_list_free(&pnp); 555 } 556