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 245 if (cnp == NULL || nnp == NULL || 246 cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) { 247 xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s " 248 "<class> <name>\n", prname); 249 } 250 251 if (strcmp(cnp->dn_string, "provider") == 0) 252 found = dt_provider_lookup(dtp, nnp->dn_string) != NULL; 253 else if (strcmp(cnp->dn_string, "module") == 0) { 254 dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string); 255 found = mp != NULL && dt_module_getctf(dtp, mp) != NULL; 256 } else if (strcmp(cnp->dn_string, "library") == 0) { 257 if (yypcb->pcb_cflags & DTRACE_C_CTL) { 258 assert(dtp->dt_filetag != NULL); 259 260 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, 261 sizeof (lib)); 262 263 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, 264 dtp->dt_filetag); 265 assert(dld != NULL); 266 267 if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies, 268 lib)) != 0) { 269 xyerror(D_PRAGMA_DEPEND, 270 "failed to add dependency %s:%s\n", lib, 271 dtrace_errmsg(dtp, dtrace_errno(dtp))); 272 } 273 } else { 274 /* 275 * By this point we have already performed a topological 276 * sort of the dependencies; we process this directive 277 * as satisfied as long as the dependency was properly 278 * loaded. 279 */ 280 if (dtp->dt_filetag == NULL) 281 xyerror(D_PRAGMA_DEPEND, "main program may " 282 "not explicitly depend on a library"); 283 284 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep, 285 dtp->dt_filetag); 286 assert(dld != NULL); 287 288 dt_pragma_depends_finddep(dtp, nnp->dn_string, lib, 289 sizeof (lib)); 290 dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted, 291 lib); 292 assert(dld != NULL); 293 294 if (!dld->dtld_loaded) 295 xyerror(D_PRAGMA_DEPEND, "program requires " 296 "library \"%s\" which failed to load", 297 lib); 298 } 299 300 found = B_TRUE; 301 } else { 302 xyerror(D_PRAGMA_INVAL, "invalid class %s " 303 "specified by #pragma %s\n", cnp->dn_string, prname); 304 } 305 306 if (!found) { 307 xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n", 308 cnp->dn_string, nnp->dn_string); 309 } 310 } 311 312 /* 313 * The #pragma error directive can be followed by any list of tokens, which we 314 * just concatenate and print as part of our error message. 315 */ 316 static void 317 dt_pragma_error(const char *prname, dt_node_t *dnp) 318 { 319 dt_node_t *enp; 320 size_t n = 0; 321 char *s; 322 323 for (enp = dnp; enp != NULL; enp = enp->dn_list) { 324 if (enp->dn_kind == DT_NODE_IDENT || 325 enp->dn_kind == DT_NODE_STRING) 326 n += strlen(enp->dn_string) + 1; 327 } 328 329 s = alloca(n + 1); 330 s[0] = '\0'; 331 332 for (enp = dnp; enp != NULL; enp = enp->dn_list) { 333 if (enp->dn_kind == DT_NODE_IDENT || 334 enp->dn_kind == DT_NODE_STRING) { 335 (void) strcat(s, enp->dn_string); 336 (void) strcat(s, " "); 337 } 338 } 339 340 xyerror(D_PRAGERR, "#%s: %s\n", prname, s); 341 } 342 343 /*ARGSUSED*/ 344 static void 345 dt_pragma_ident(const char *prname, dt_node_t *dnp) 346 { 347 /* ignore any #ident or #pragma ident lines */ 348 } 349 350 static void 351 dt_pragma_option(const char *prname, dt_node_t *dnp) 352 { 353 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 354 char *opt, *val; 355 356 if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) { 357 xyerror(D_PRAGMA_MALFORM, 358 "malformed #pragma %s <option>=<val>\n", prname); 359 } 360 361 if (dnp->dn_list != NULL) { 362 xyerror(D_PRAGMA_MALFORM, 363 "superfluous arguments specified for #pragma %s\n", prname); 364 } 365 366 opt = alloca(strlen(dnp->dn_string) + 1); 367 (void) strcpy(opt, dnp->dn_string); 368 369 if ((val = strchr(opt, '=')) != NULL) 370 *val++ = '\0'; 371 372 if (dtrace_setopt(dtp, opt, val) == -1) { 373 if (val == NULL) { 374 xyerror(D_PRAGMA_OPTSET, 375 "failed to set option '%s': %s\n", opt, 376 dtrace_errmsg(dtp, dtrace_errno(dtp))); 377 } else { 378 xyerror(D_PRAGMA_OPTSET, 379 "failed to set option '%s' to '%s': %s\n", 380 opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp))); 381 } 382 } 383 } 384 385 /* 386 * The #line directive is used to reset the input line number and to optionally 387 * note the file name for use in error messages. Sun cpp(1) also produces a 388 * third integer token after the filename which is one of the following: 389 * 390 * 0 - line change has nothing to do with an #include file 391 * 1 - line change because we just entered a #include file 392 * 2 - line change because we just exited a #include file 393 * 394 * We use these state tokens to adjust pcb_idepth, which in turn controls 395 * whether type lookups access the global type space or not. 396 */ 397 static void 398 dt_pragma_line(const char *prname, dt_node_t *dnp) 399 { 400 dt_node_t *fnp = dnp ? dnp->dn_list : NULL; 401 dt_node_t *inp = fnp ? fnp->dn_list : NULL; 402 403 if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) || 404 (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) || 405 (inp != NULL && inp->dn_kind != DT_NODE_INT)) { 406 xyerror(D_PRAGMA_MALFORM, "malformed #%s " 407 "<line> [ [\"file\"] state ]\n", prname); 408 } 409 410 /* 411 * If a file is specified, free any old pcb_filetag and swap fnp's 412 * dn_string into pcb_filetag as the new filename for error messages. 413 */ 414 if (fnp != NULL) { 415 if (yypcb->pcb_filetag != NULL) 416 free(yypcb->pcb_filetag); 417 418 /* 419 * This is not pretty, but is a necessary evil until we either 420 * write "dpp" or get a useful standalone cpp from DevPro. If 421 * the filename begins with /dev/fd, we know it's the master 422 * input file (see dt_preproc() in dt_cc.c), so just clear the 423 * dt_filetag pointer so error messages refer to the main file. 424 */ 425 if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) { 426 yypcb->pcb_filetag = fnp->dn_string; 427 fnp->dn_string = NULL; 428 } else 429 yypcb->pcb_filetag = NULL; 430 } 431 432 if (inp != NULL) { 433 if (inp->dn_value == 1) 434 yypcb->pcb_idepth++; 435 else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0) 436 yypcb->pcb_idepth--; 437 } 438 439 yylineno = dnp->dn_value; 440 } 441 442 /* 443 * D compiler pragma types range from control directives to common pragmas to 444 * D custom pragmas, in order of specificity. Similar to gcc, we use #pragma D 445 * as a special prefix for our pragmas so they can be used in mixed headers. 446 */ 447 #define DT_PRAGMA_DIR 0 /* pragma directive may be used after naked # */ 448 #define DT_PRAGMA_SUB 1 /* pragma directive may be used after #pragma */ 449 #define DT_PRAGMA_DCP 2 /* pragma may only be used after #pragma D */ 450 451 static const struct dt_pragmadesc { 452 const char *dpd_name; 453 void (*dpd_func)(const char *, dt_node_t *); 454 int dpd_kind; 455 } dt_pragmas[] = { 456 { "attributes", dt_pragma_attributes, DT_PRAGMA_DCP }, 457 { "binding", dt_pragma_binding, DT_PRAGMA_DCP }, 458 { "depends_on", dt_pragma_depends, DT_PRAGMA_DCP }, 459 { "error", dt_pragma_error, DT_PRAGMA_DIR }, 460 { "ident", dt_pragma_ident, DT_PRAGMA_DIR }, 461 { "line", dt_pragma_line, DT_PRAGMA_DIR }, 462 { "option", dt_pragma_option, DT_PRAGMA_DCP }, 463 { NULL, NULL } 464 }; 465 466 /* 467 * Process a control line #directive by looking up the directive name in our 468 * lookup table and invoking the corresponding function with the token list. 469 * According to K&R[A12.9], we silently ignore null directive lines. 470 */ 471 void 472 dt_pragma(dt_node_t *pnp) 473 { 474 const struct dt_pragmadesc *dpd; 475 dt_node_t *dnp; 476 int kind = DT_PRAGMA_DIR; 477 478 for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) { 479 if (dnp->dn_kind == DT_NODE_INT) { 480 dt_pragma_line("line", dnp); 481 break; 482 } 483 484 if (dnp->dn_kind != DT_NODE_IDENT) 485 xyerror(D_PRAGCTL_INVAL, "invalid control directive\n"); 486 487 if (kind == DT_PRAGMA_DIR && 488 strcmp(dnp->dn_string, "pragma") == 0) { 489 kind = DT_PRAGMA_SUB; 490 continue; 491 } 492 493 if (kind == DT_PRAGMA_SUB && 494 strcmp(dnp->dn_string, "D") == 0) { 495 kind = DT_PRAGMA_DCP; 496 continue; 497 } 498 499 for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) { 500 if (dpd->dpd_kind <= kind && 501 strcmp(dpd->dpd_name, dnp->dn_string) == 0) 502 break; 503 } 504 505 yylineno--; /* since we've already seen \n */ 506 507 if (dpd->dpd_name != NULL) { 508 dpd->dpd_func(dpd->dpd_name, dnp->dn_list); 509 yylineno++; 510 break; 511 } 512 513 switch (kind) { 514 case DT_PRAGMA_DIR: 515 xyerror(D_PRAGCTL_INVAL, "invalid control directive: " 516 "#%s\n", dnp->dn_string); 517 /*NOTREACHED*/ 518 case DT_PRAGMA_SUB: 519 break; /* K&R[A12.8] says to ignore unknown pragmas */ 520 case DT_PRAGMA_DCP: 521 default: 522 xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n", 523 dnp->dn_string); 524 } 525 526 yylineno++; 527 break; 528 } 529 530 dt_node_list_free(&pnp); 531 } 532