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