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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2013 by Delphix. All rights reserved. 25 * Copyright (c) 2013 Joyent, Inc. All rights reserved. 26 * Copyright 2022 Oxide Computer Company 27 */ 28 29 #include <sys/sysmacros.h> 30 #include <strings.h> 31 #include <stdlib.h> 32 #include <alloca.h> 33 #include <assert.h> 34 #include <errno.h> 35 #include <ctype.h> 36 #include <sys/procfs_isa.h> 37 #include <limits.h> 38 39 #include <dt_ident.h> 40 #include <dt_parser.h> 41 #include <dt_provider.h> 42 #include <dt_strtab.h> 43 #include <dt_impl.h> 44 45 /* 46 * Common code for cooking an identifier that uses a typed signature list (we 47 * use this for associative arrays and functions). If the argument list is 48 * of the same length and types, then return the return type. Otherwise 49 * print an appropriate compiler error message and abort the compile. 50 */ 51 static void 52 dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp, 53 int argc, dt_node_t *args, const char *prefix, const char *suffix) 54 { 55 dt_idsig_t *isp = idp->di_data; 56 int i, compat, mismatch, arglimit, iskey; 57 58 char n1[DT_TYPE_NAMELEN]; 59 char n2[DT_TYPE_NAMELEN]; 60 61 iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG; 62 63 if (isp->dis_varargs >= 0) { 64 mismatch = argc < isp->dis_varargs; 65 arglimit = isp->dis_varargs; 66 } else if (isp->dis_optargs >= 0) { 67 mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc); 68 arglimit = argc; 69 } else { 70 mismatch = argc != isp->dis_argc; 71 arglimit = isp->dis_argc; 72 } 73 74 if (mismatch) { 75 xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s" 76 "passed, %s%d expected\n", prefix, idp->di_name, suffix, 77 argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ", 78 isp->dis_optargs >= 0 ? "at least " : "", 79 isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit); 80 } 81 82 for (i = 0; i < arglimit; i++, args = args->dn_list) { 83 if (isp->dis_args[i].dn_ctfp != NULL) 84 compat = dt_node_is_argcompat(&isp->dis_args[i], args); 85 else 86 compat = 1; /* "@" matches any type */ 87 88 if (!compat) { 89 xyerror(D_PROTO_ARG, 90 "%s%s%s %s #%d is incompatible with " 91 "prototype:\n\tprototype: %s\n\t%9s: %s\n", 92 prefix, idp->di_name, suffix, 93 iskey ? "key" : "argument", i + 1, 94 dt_node_type_name(&isp->dis_args[i], n1, 95 sizeof (n1)), 96 iskey ? "key" : "argument", 97 dt_node_type_name(args, n2, sizeof (n2))); 98 } 99 } 100 101 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE); 102 } 103 104 /* 105 * Cook an associative array identifier. If this is the first time we are 106 * cooking this array, create its signature based on the argument list. 107 * Otherwise validate the argument list against the existing signature. 108 */ 109 static void 110 dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) 111 { 112 if (idp->di_data == NULL) { 113 dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t)); 114 char n[DT_TYPE_NAMELEN]; 115 int i; 116 117 if (isp == NULL) 118 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 119 120 isp->dis_varargs = -1; 121 isp->dis_optargs = -1; 122 isp->dis_argc = argc; 123 isp->dis_args = NULL; 124 isp->dis_auxinfo = 0; 125 126 if (argc != 0 && (isp->dis_args = calloc(argc, 127 sizeof (dt_node_t))) == NULL) { 128 idp->di_data = NULL; 129 free(isp); 130 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 131 } 132 133 /* 134 * If this identifier has not been explicitly declared earlier, 135 * set the identifier's base type to be our special type <DYN>. 136 * If this ident is an aggregation, it will remain as is. If 137 * this ident is an associative array, it will be reassigned 138 * based on the result type of the first assignment statement. 139 */ 140 if (!(idp->di_flags & DT_IDFLG_DECL)) { 141 idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl); 142 idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl); 143 } 144 145 for (i = 0; i < argc; i++, args = args->dn_list) { 146 if (dt_node_is_dynamic(args) || dt_node_is_void(args)) { 147 xyerror(D_KEY_TYPE, "%s expression may not be " 148 "used as %s index: key #%d\n", 149 dt_node_type_name(args, n, sizeof (n)), 150 dt_idkind_name(idp->di_kind), i + 1); 151 } 152 153 dt_node_type_propagate(args, &isp->dis_args[i]); 154 isp->dis_args[i].dn_list = &isp->dis_args[i + 1]; 155 } 156 157 if (argc != 0) 158 isp->dis_args[argc - 1].dn_list = NULL; 159 160 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE); 161 162 } else { 163 dt_idcook_sign(dnp, idp, argc, args, 164 idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]"); 165 } 166 } 167 168 /* 169 * Cook a function call. If this is the first time we are cooking this 170 * identifier, create its type signature based on predefined prototype stored 171 * in di_iarg. We then validate the argument list against this signature. 172 */ 173 static void 174 dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) 175 { 176 if (idp->di_data == NULL) { 177 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 178 dtrace_typeinfo_t dtt; 179 dt_idsig_t *isp; 180 char *s, *p1, *p2; 181 int i = 0; 182 183 assert(idp->di_iarg != NULL); 184 s = strdupa(idp->di_iarg); 185 186 if ((p2 = strrchr(s, ')')) != NULL) 187 *p2 = '\0'; /* mark end of parameter list string */ 188 189 if ((p1 = strchr(s, '(')) != NULL) 190 *p1++ = '\0'; /* mark end of return type string */ 191 192 if (p1 == NULL || p2 == NULL) { 193 xyerror(D_UNKNOWN, "internal error: malformed entry " 194 "for built-in function %s\n", idp->di_name); 195 } 196 197 for (p2 = p1; *p2 != '\0'; p2++) { 198 if (!isspace(*p2)) { 199 i++; 200 break; 201 } 202 } 203 204 for (p2 = strchr(p2, ','); p2++ != NULL; i++) 205 p2 = strchr(p2, ','); 206 207 /* 208 * We first allocate a new ident signature structure with the 209 * appropriate number of argument entries, and then look up 210 * the return type and store its CTF data in di_ctfp/type. 211 */ 212 if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL) 213 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 214 215 isp->dis_varargs = -1; 216 isp->dis_optargs = -1; 217 isp->dis_argc = i; 218 isp->dis_args = NULL; 219 isp->dis_auxinfo = 0; 220 221 if (i != 0 && (isp->dis_args = calloc(i, 222 sizeof (dt_node_t))) == NULL) { 223 idp->di_data = NULL; 224 free(isp); 225 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 226 } 227 228 if (dt_type_lookup(s, &dtt) == -1) { 229 xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):" 230 " %s\n", idp->di_name, s, 231 dtrace_errmsg(dtp, dtrace_errno(dtp))); 232 } 233 234 if (idp->di_kind == DT_IDENT_AGGFUNC) { 235 idp->di_ctfp = DT_DYN_CTFP(dtp); 236 idp->di_type = DT_DYN_TYPE(dtp); 237 } else { 238 idp->di_ctfp = dtt.dtt_ctfp; 239 idp->di_type = dtt.dtt_type; 240 } 241 242 /* 243 * For each comma-delimited parameter in the prototype string, 244 * we look up the corresponding type and store its CTF data in 245 * the corresponding location in dis_args[]. We also recognize 246 * the special type string "@" to indicate that the specified 247 * parameter may be a D expression of *any* type (represented 248 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR). 249 * If a varargs "..." is present, we record the argument index 250 * in dis_varargs for the benefit of dt_idcook_sign(), above. 251 * If the type of an argument is enclosed in square brackets 252 * (e.g. "[int]"), the argument is considered optional: the 253 * argument may be absent, but if it is present, it must be of 254 * the specified type. Note that varargs may not optional, 255 * optional arguments may not follow varargs, and non-optional 256 * arguments may not follow optional arguments. 257 */ 258 for (i = 0; i < isp->dis_argc; i++, p1 = p2) { 259 while (isspace(*p1)) 260 p1++; /* skip leading whitespace */ 261 262 if ((p2 = strchr(p1, ',')) == NULL) 263 p2 = p1 + strlen(p1); 264 else 265 *p2++ = '\0'; 266 267 if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) { 268 isp->dis_args[i].dn_ctfp = NULL; 269 isp->dis_args[i].dn_type = CTF_ERR; 270 isp->dis_args[i].dn_bitoff = 0; 271 if (*p1 == '.') 272 isp->dis_varargs = i; 273 continue; 274 } 275 276 if (*p1 == '[' && p1[strlen(p1) - 1] == ']') { 277 if (isp->dis_varargs != -1) { 278 xyerror(D_UNKNOWN, "optional arg#%d " 279 "may not follow variable arg#%d\n", 280 i + 1, isp->dis_varargs + 1); 281 } 282 283 if (isp->dis_optargs == -1) 284 isp->dis_optargs = i; 285 286 p1[strlen(p1) - 1] = '\0'; 287 p1++; 288 } else if (isp->dis_optargs != -1) { 289 xyerror(D_UNKNOWN, "required arg#%d may not " 290 "follow optional arg#%d\n", i + 1, 291 isp->dis_optargs + 1); 292 } 293 294 if (dt_type_lookup(p1, &dtt) == -1) { 295 xyerror(D_UNKNOWN, "failed to resolve type of " 296 "%s arg#%d (%s): %s\n", idp->di_name, i + 1, 297 p1, dtrace_errmsg(dtp, dtrace_errno(dtp))); 298 } 299 300 dt_node_type_assign(&isp->dis_args[i], 301 dtt.dtt_ctfp, dtt.dtt_type, B_FALSE); 302 } 303 } 304 305 dt_idcook_sign(dnp, idp, argc, args, "", "( )"); 306 } 307 308 /* 309 * Cook a reference to the dynamically typed args[] array. We verify that the 310 * reference is using a single integer constant, and then construct a new ident 311 * representing the appropriate type or translation specifically for this node. 312 */ 313 static void 314 dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap) 315 { 316 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 317 dt_probe_t *prp = yypcb->pcb_probe; 318 319 dt_node_t tag, *nnp, *xnp; 320 dt_xlator_t *dxp; 321 dt_ident_t *xidp; 322 323 char n1[DT_TYPE_NAMELEN]; 324 char n2[DT_TYPE_NAMELEN]; 325 326 if (argc != 1) { 327 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s" 328 "passed, 1 expected\n", idp->di_name, argc, 329 argc == 1 ? " " : "s "); 330 } 331 332 if (ap->dn_kind != DT_NODE_INT) { 333 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with " 334 "prototype:\n\tprototype: %s\n\t argument: %s\n", 335 idp->di_name, "integer constant", 336 dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1))); 337 } 338 339 if (yypcb->pcb_pdesc == NULL) { 340 xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside " 341 "of a probe clause\n", idp->di_name); 342 } 343 344 if (prp == NULL) { 345 xyerror(D_ARGS_MULTI, 346 "%s[ ] may not be referenced because probe description %s " 347 "matches an unstable set of probes\n", idp->di_name, 348 dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1))); 349 } 350 351 if (ap->dn_value >= prp->pr_argc) { 352 xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n", 353 (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc, 354 n1, sizeof (n1)), idp->di_name); 355 } 356 357 /* 358 * Look up the native and translated argument types for the probe. 359 * If no translation is needed, these will be the same underlying node. 360 * If translation is needed, look up the appropriate translator. Once 361 * we have the appropriate node, create a new dt_ident_t for this node, 362 * assign it the appropriate attributes, and set the type of 'dnp'. 363 */ 364 xnp = prp->pr_xargv[ap->dn_value]; 365 nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]]; 366 367 if (xnp->dn_type == CTF_ERR) { 368 xyerror(D_ARGS_TYPE, "failed to resolve translated type for " 369 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value); 370 } 371 372 if (nnp->dn_type == CTF_ERR) { 373 xyerror(D_ARGS_TYPE, "failed to resolve native type for " 374 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value); 375 } 376 377 if (dtp->dt_xlatemode == DT_XL_STATIC && ( 378 nnp == xnp || dt_node_is_argcompat(nnp, xnp))) { 379 dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind, 380 idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr, 381 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen); 382 383 if (dnp->dn_ident == NULL) 384 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 385 386 dt_node_type_assign(dnp, 387 prp->pr_argv[ap->dn_value].dtt_ctfp, 388 prp->pr_argv[ap->dn_value].dtt_type, 389 prp->pr_argv[ap->dn_value].dtt_flags & DTT_FL_USER ? 390 B_TRUE : B_FALSE); 391 392 } else if ((dxp = dt_xlator_lookup(dtp, 393 nnp, xnp, DT_XLATE_FUZZY)) != NULL || ( 394 dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag), 395 xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) { 396 397 xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type); 398 399 dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind, 400 xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr, 401 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen); 402 403 if (dnp->dn_ident == NULL) 404 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 405 406 if (dt_xlator_dynamic(dxp)) 407 dxp->dx_arg = (int)ap->dn_value; 408 409 /* 410 * Propagate relevant members from the translator's internal 411 * dt_ident_t. This code must be kept in sync with the state 412 * that is initialized for idents in dt_xlator_create(). 413 */ 414 dnp->dn_ident->di_data = xidp->di_data; 415 dnp->dn_ident->di_ctfp = xidp->di_ctfp; 416 dnp->dn_ident->di_type = xidp->di_type; 417 418 dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp), 419 B_FALSE); 420 421 } else { 422 xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s " 423 "is not defined\n", idp->di_name, (longlong_t)ap->dn_value, 424 dt_node_type_name(nnp, n1, sizeof (n1)), 425 dt_node_type_name(xnp, n2, sizeof (n2))); 426 } 427 428 assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN); 429 assert(dnp->dn_ident->di_id == idp->di_id); 430 } 431 432 static void 433 dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap) 434 { 435 dtrace_typeinfo_t dtt; 436 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 437 char n[DT_TYPE_NAMELEN]; 438 439 if (argc != 1) { 440 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s" 441 "passed, 1 expected\n", idp->di_name, 442 argc, argc == 1 ? " " : "s "); 443 } 444 445 if (ap->dn_kind != DT_NODE_INT) { 446 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with " 447 "prototype:\n\tprototype: %s\n\t argument: %s\n", 448 idp->di_name, "integer constant", 449 dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n))); 450 } 451 452 if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) { 453 xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n", 454 (longlong_t)ap->dn_value, idp->di_name); 455 } 456 457 if (dt_type_lookup("uint64_t", &dtt) == -1) { 458 xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n", 459 idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp))); 460 } 461 462 idp->di_ctfp = dtt.dtt_ctfp; 463 idp->di_type = dtt.dtt_type; 464 465 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE); 466 } 467 468 /*ARGSUSED*/ 469 static void 470 dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) 471 { 472 if (idp->di_type == CTF_ERR) { 473 dtrace_hdl_t *dtp = yypcb->pcb_hdl; 474 dtrace_typeinfo_t dtt; 475 476 if (dt_type_lookup(idp->di_iarg, &dtt) == -1) { 477 xyerror(D_UNKNOWN, 478 "failed to resolve type %s for identifier %s: %s\n", 479 (const char *)idp->di_iarg, idp->di_name, 480 dtrace_errmsg(dtp, dtrace_errno(dtp))); 481 } 482 483 idp->di_ctfp = dtt.dtt_ctfp; 484 idp->di_type = dtt.dtt_type; 485 } 486 487 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE); 488 } 489 490 /*ARGSUSED*/ 491 static void 492 dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) 493 { 494 if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR) 495 dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE); 496 } 497 498 static void 499 dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) 500 { 501 if (idp->di_kind == DT_IDENT_ARRAY) 502 dt_idcook_assc(dnp, idp, argc, args); 503 else 504 dt_idcook_thaw(dnp, idp, argc, args); 505 } 506 507 static void 508 dt_iddtor_sign(dt_ident_t *idp) 509 { 510 if (idp->di_data != NULL) 511 free(((dt_idsig_t *)idp->di_data)->dis_args); 512 free(idp->di_data); 513 } 514 515 static void 516 dt_iddtor_free(dt_ident_t *idp) 517 { 518 free(idp->di_data); 519 } 520 521 static void 522 dt_iddtor_inline(dt_ident_t *idp) 523 { 524 dt_idnode_t *inp = idp->di_iarg; 525 526 if (inp != NULL) { 527 dt_node_link_free(&inp->din_list); 528 529 if (inp->din_hash != NULL) 530 dt_idhash_destroy(inp->din_hash); 531 532 free(inp->din_argv); 533 free(inp); 534 } 535 536 if (idp->di_kind == DT_IDENT_ARRAY) 537 dt_iddtor_sign(idp); 538 else 539 dt_iddtor_free(idp); 540 } 541 542 /*ARGSUSED*/ 543 static void 544 dt_iddtor_none(dt_ident_t *idp) 545 { 546 /* do nothing */ 547 } 548 549 static void 550 dt_iddtor_probe(dt_ident_t *idp) 551 { 552 if (idp->di_data != NULL) 553 dt_probe_destroy(idp->di_data); 554 } 555 556 static size_t 557 dt_idsize_type(dt_ident_t *idp) 558 { 559 return (ctf_type_size(idp->di_ctfp, idp->di_type)); 560 } 561 562 /*ARGSUSED*/ 563 static size_t 564 dt_idsize_none(dt_ident_t *idp) 565 { 566 return (0); 567 } 568 569 const dt_idops_t dt_idops_assc = { 570 dt_idcook_assc, 571 dt_iddtor_sign, 572 dt_idsize_none, 573 }; 574 575 const dt_idops_t dt_idops_func = { 576 dt_idcook_func, 577 dt_iddtor_sign, 578 dt_idsize_none, 579 }; 580 581 const dt_idops_t dt_idops_args = { 582 dt_idcook_args, 583 dt_iddtor_none, 584 dt_idsize_none, 585 }; 586 587 const dt_idops_t dt_idops_regs = { 588 dt_idcook_regs, 589 dt_iddtor_free, 590 dt_idsize_none, 591 }; 592 593 const dt_idops_t dt_idops_type = { 594 dt_idcook_type, 595 dt_iddtor_free, 596 dt_idsize_type, 597 }; 598 599 const dt_idops_t dt_idops_thaw = { 600 dt_idcook_thaw, 601 dt_iddtor_free, 602 dt_idsize_type, 603 }; 604 605 const dt_idops_t dt_idops_inline = { 606 dt_idcook_inline, 607 dt_iddtor_inline, 608 dt_idsize_type, 609 }; 610 611 const dt_idops_t dt_idops_probe = { 612 dt_idcook_thaw, 613 dt_iddtor_probe, 614 dt_idsize_none, 615 }; 616 617 static void 618 dt_idhash_populate(dt_idhash_t *dhp) 619 { 620 const dt_ident_t *idp = dhp->dh_tmpl; 621 622 dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */ 623 dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp); 624 625 for (; idp->di_name != NULL; idp++) { 626 if (dt_idhash_insert(dhp, idp->di_name, 627 idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr, 628 idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw, 629 idp->di_iarg, 0) == NULL) 630 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); 631 } 632 } 633 634 dt_idhash_t * 635 dt_idhash_create(const char *name, const dt_ident_t *tmpl, 636 uint_t min, uint_t max) 637 { 638 dt_idhash_t *dhp; 639 size_t size; 640 641 assert(min <= max); 642 643 size = sizeof (dt_idhash_t) + 644 sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1); 645 646 if ((dhp = malloc(size)) == NULL) 647 return (NULL); 648 649 bzero(dhp, size); 650 dhp->dh_name = name; 651 dhp->dh_tmpl = tmpl; 652 dhp->dh_nextid = min; 653 dhp->dh_minid = min; 654 dhp->dh_maxid = max; 655 dhp->dh_hashsz = _dtrace_strbuckets; 656 657 return (dhp); 658 } 659 660 /* 661 * Destroy an entire identifier hash. This must be done using two passes with 662 * an inlined version of dt_ident_destroy() to avoid referencing freed memory. 663 * In the first pass di_dtor() is called for all identifiers; then the second 664 * pass frees the actual dt_ident_t's. These must be done separately because 665 * a di_dtor() may operate on data structures which contain references to other 666 * identifiers inside of this hash itself (e.g. a global inline definition 667 * which contains a parse tree that refers to another global variable). 668 */ 669 void 670 dt_idhash_destroy(dt_idhash_t *dhp) 671 { 672 dt_ident_t *idp, *next; 673 ulong_t i; 674 675 for (i = 0; i < dhp->dh_hashsz; i++) { 676 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) { 677 next = idp->di_next; 678 idp->di_ops->di_dtor(idp); 679 } 680 } 681 682 for (i = 0; i < dhp->dh_hashsz; i++) { 683 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) { 684 next = idp->di_next; 685 free(idp->di_name); 686 free(idp); 687 } 688 } 689 690 free(dhp); 691 } 692 693 void 694 dt_idhash_update(dt_idhash_t *dhp) 695 { 696 uint_t nextid = dhp->dh_minid; 697 dt_ident_t *idp; 698 ulong_t i; 699 700 for (i = 0; i < dhp->dh_hashsz; i++) { 701 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) { 702 /* 703 * Right now we're hard coding which types need to be 704 * reset, but ideally this would be done dynamically. 705 */ 706 if (idp->di_kind == DT_IDENT_ARRAY || 707 idp->di_kind == DT_IDENT_SCALAR || 708 idp->di_kind == DT_IDENT_AGG) 709 nextid = MAX(nextid, idp->di_id + 1); 710 } 711 } 712 713 dhp->dh_nextid = nextid; 714 } 715 716 dt_ident_t * 717 dt_idhash_lookup(dt_idhash_t *dhp, const char *name) 718 { 719 size_t len; 720 ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz; 721 dt_ident_t *idp; 722 723 if (dhp->dh_tmpl != NULL) 724 dt_idhash_populate(dhp); /* fill hash w/ initial population */ 725 726 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) { 727 if (strcmp(idp->di_name, name) == 0) 728 return (idp); 729 } 730 731 return (NULL); 732 } 733 734 int 735 dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p) 736 { 737 if (dhp->dh_nextid >= dhp->dh_maxid) 738 return (-1); /* no more id's are free to allocate */ 739 740 *p = dhp->dh_nextid++; 741 return (0); 742 } 743 744 ulong_t 745 dt_idhash_size(const dt_idhash_t *dhp) 746 { 747 return (dhp->dh_nelems); 748 } 749 750 const char * 751 dt_idhash_name(const dt_idhash_t *dhp) 752 { 753 return (dhp->dh_name); 754 } 755 756 dt_ident_t * 757 dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind, 758 ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers, 759 const dt_idops_t *ops, void *iarg, ulong_t gen) 760 { 761 dt_ident_t *idp; 762 ulong_t h; 763 764 if (dhp->dh_tmpl != NULL) 765 dt_idhash_populate(dhp); /* fill hash w/ initial population */ 766 767 idp = dt_ident_create(name, kind, flags, id, 768 attr, vers, ops, iarg, gen); 769 770 if (idp == NULL) 771 return (NULL); 772 773 h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz; 774 idp->di_next = dhp->dh_hash[h]; 775 776 dhp->dh_hash[h] = idp; 777 dhp->dh_nelems++; 778 779 if (dhp->dh_defer != NULL) 780 dhp->dh_defer(dhp, idp); 781 782 return (idp); 783 } 784 785 void 786 dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp) 787 { 788 ulong_t h; 789 790 if (dhp->dh_tmpl != NULL) 791 dt_idhash_populate(dhp); /* fill hash w/ initial population */ 792 793 h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz; 794 idp->di_next = dhp->dh_hash[h]; 795 idp->di_flags &= ~DT_IDFLG_ORPHAN; 796 797 dhp->dh_hash[h] = idp; 798 dhp->dh_nelems++; 799 800 if (dhp->dh_defer != NULL) 801 dhp->dh_defer(dhp, idp); 802 } 803 804 void 805 dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key) 806 { 807 size_t len; 808 ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz; 809 dt_ident_t **pp = &dhp->dh_hash[h]; 810 dt_ident_t *idp; 811 812 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) { 813 if (idp == key) 814 break; 815 else 816 pp = &idp->di_next; 817 } 818 819 assert(idp == key); 820 *pp = idp->di_next; 821 822 assert(dhp->dh_nelems != 0); 823 dhp->dh_nelems--; 824 825 if (!(idp->di_flags & DT_IDFLG_ORPHAN)) 826 dt_ident_destroy(idp); 827 } 828 829 static int 830 dt_idhash_comp(const void *lp, const void *rp) 831 { 832 const dt_ident_t *lhs = *((const dt_ident_t **)lp); 833 const dt_ident_t *rhs = *((const dt_ident_t **)rp); 834 835 if (lhs->di_id != rhs->di_id) 836 return ((int)(lhs->di_id - rhs->di_id)); 837 else 838 return (strcmp(lhs->di_name, rhs->di_name)); 839 } 840 841 int 842 dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data) 843 { 844 dt_ident_t **ids; 845 dt_ident_t *idp; 846 ulong_t i, j, n; 847 int rv; 848 849 if (dhp->dh_tmpl != NULL) 850 dt_idhash_populate(dhp); /* fill hash w/ initial population */ 851 852 n = dhp->dh_nelems; 853 ids = alloca(sizeof (dt_ident_t *) * n); 854 855 for (i = 0, j = 0; i < dhp->dh_hashsz; i++) { 856 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) 857 ids[j++] = idp; 858 } 859 860 qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp); 861 862 for (i = 0; i < n; i++) { 863 if ((rv = func(dhp, ids[i], data)) != 0) 864 return (rv); 865 } 866 867 return (0); 868 } 869 870 dt_ident_t * 871 dt_idstack_lookup(dt_idstack_t *sp, const char *name) 872 { 873 dt_idhash_t *dhp; 874 dt_ident_t *idp; 875 876 for (dhp = dt_list_prev(&sp->dids_list); 877 dhp != NULL; dhp = dt_list_prev(dhp)) { 878 if ((idp = dt_idhash_lookup(dhp, name)) != NULL) 879 return (idp); 880 } 881 882 return (NULL); 883 } 884 885 void 886 dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp) 887 { 888 dt_list_append(&sp->dids_list, dhp); 889 } 890 891 void 892 dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp) 893 { 894 assert(dt_list_prev(&sp->dids_list) == dhp); 895 dt_list_delete(&sp->dids_list, dhp); 896 } 897 898 dt_ident_t * 899 dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id, 900 dtrace_attribute_t attr, uint_t vers, 901 const dt_idops_t *ops, void *iarg, ulong_t gen) 902 { 903 dt_ident_t *idp; 904 char *s = NULL; 905 906 if ((name != NULL && (s = strdup(name)) == NULL) || 907 (idp = malloc(sizeof (dt_ident_t))) == NULL) { 908 free(s); 909 return (NULL); 910 } 911 912 idp->di_name = s; 913 idp->di_kind = kind; 914 idp->di_flags = flags; 915 idp->di_id = id; 916 idp->di_attr = attr; 917 idp->di_vers = vers; 918 idp->di_ops = ops; 919 idp->di_iarg = iarg; 920 idp->di_data = NULL; 921 idp->di_ctfp = NULL; 922 idp->di_type = CTF_ERR; 923 idp->di_next = NULL; 924 idp->di_gen = gen; 925 idp->di_lineno = yylineno; 926 927 return (idp); 928 } 929 930 /* 931 * Destroy an individual identifier. This code must be kept in sync with the 932 * dt_idhash_destroy() function below, which separates out the call to di_dtor. 933 */ 934 void 935 dt_ident_destroy(dt_ident_t *idp) 936 { 937 idp->di_ops->di_dtor(idp); 938 free(idp->di_name); 939 free(idp); 940 } 941 942 void 943 dt_ident_morph(dt_ident_t *idp, ushort_t kind, 944 const dt_idops_t *ops, void *iarg) 945 { 946 idp->di_ops->di_dtor(idp); 947 idp->di_kind = kind; 948 idp->di_ops = ops; 949 idp->di_iarg = iarg; 950 idp->di_data = NULL; 951 } 952 953 dtrace_attribute_t 954 dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp) 955 { 956 dtrace_attribute_t attr; 957 dt_node_t *args, *argp; 958 int argc = 0; 959 960 attr = dt_node_list_cook(pargp, DT_IDFLG_REF); 961 args = pargp ? *pargp : NULL; 962 963 for (argp = args; argp != NULL; argp = argp->dn_list) 964 argc++; 965 966 idp->di_ops->di_cook(dnp, idp, argc, args); 967 968 if (idp->di_flags & DT_IDFLG_USER) 969 dnp->dn_flags |= DT_NF_USERLAND; 970 971 return (dt_attr_min(attr, idp->di_attr)); 972 } 973 974 void 975 dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type) 976 { 977 idp->di_ctfp = fp; 978 idp->di_type = type; 979 } 980 981 dt_ident_t * 982 dt_ident_resolve(dt_ident_t *idp) 983 { 984 while (idp->di_flags & DT_IDFLG_INLINE) { 985 const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root; 986 987 if (dnp == NULL) 988 break; /* can't resolve any further yet */ 989 990 switch (dnp->dn_kind) { 991 case DT_NODE_VAR: 992 case DT_NODE_SYM: 993 case DT_NODE_FUNC: 994 case DT_NODE_AGG: 995 case DT_NODE_INLINE: 996 case DT_NODE_PROBE: 997 idp = dnp->dn_ident; 998 continue; 999 } 1000 1001 if (dt_node_is_dynamic(dnp)) 1002 idp = dnp->dn_ident; 1003 else 1004 break; 1005 } 1006 1007 return (idp); 1008 } 1009 1010 size_t 1011 dt_ident_size(dt_ident_t *idp) 1012 { 1013 idp = dt_ident_resolve(idp); 1014 return (idp->di_ops->di_size(idp)); 1015 } 1016 1017 int 1018 dt_ident_unref(const dt_ident_t *idp) 1019 { 1020 return (idp->di_gen == yypcb->pcb_hdl->dt_gen && 1021 (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0); 1022 } 1023 1024 const char * 1025 dt_idkind_name(uint_t kind) 1026 { 1027 switch (kind) { 1028 case DT_IDENT_ARRAY: return ("associative array"); 1029 case DT_IDENT_SCALAR: return ("scalar"); 1030 case DT_IDENT_PTR: return ("pointer"); 1031 case DT_IDENT_FUNC: return ("function"); 1032 case DT_IDENT_AGG: return ("aggregation"); 1033 case DT_IDENT_AGGFUNC: return ("aggregating function"); 1034 case DT_IDENT_ACTFUNC: return ("tracing function"); 1035 case DT_IDENT_XLSOU: return ("translated data"); 1036 case DT_IDENT_XLPTR: return ("pointer to translated data"); 1037 case DT_IDENT_SYMBOL: return ("external symbol reference"); 1038 case DT_IDENT_ENUM: return ("enumerator"); 1039 case DT_IDENT_PRAGAT: return ("#pragma attributes"); 1040 case DT_IDENT_PRAGBN: return ("#pragma binding"); 1041 case DT_IDENT_PROBE: return ("probe definition"); 1042 default: return ("<?>"); 1043 } 1044 } 1045