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 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #if defined(sun) 28 #include <sys/sysmacros.h> 29 #endif 30 #include <sys/isa_defs.h> 31 32 #include <strings.h> 33 #include <unistd.h> 34 #include <stdarg.h> 35 #include <stddef.h> 36 #include <stdlib.h> 37 #include <stdio.h> 38 #include <errno.h> 39 #include <ctype.h> 40 #if defined(sun) 41 #include <alloca.h> 42 #else 43 #include <sys/sysctl.h> 44 #include <libproc_compat.h> 45 #endif 46 #include <assert.h> 47 #include <libgen.h> 48 #include <limits.h> 49 #include <stdint.h> 50 51 #include <dt_impl.h> 52 53 static const struct { 54 size_t dtps_offset; 55 size_t dtps_len; 56 } dtrace_probespecs[] = { 57 { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN }, 58 { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN }, 59 { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN }, 60 { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN } 61 }; 62 63 int 64 dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec, 65 const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp) 66 { 67 size_t off, len, vlen, wlen; 68 const char *p, *q, *v, *w; 69 70 char buf[32]; /* for id_t as %d (see below) */ 71 72 if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME) 73 return (dt_set_errno(dtp, EINVAL)); 74 75 bzero(pdp, sizeof (dtrace_probedesc_t)); 76 p = s + strlen(s) - 1; 77 78 do { 79 for (len = 0; p >= s && *p != ':'; len++) 80 p--; /* move backward until we find a delimiter */ 81 82 q = p + 1; 83 vlen = 0; 84 w = NULL; 85 wlen = 0; 86 87 if ((v = strchr(q, '$')) != NULL && v < q + len) { 88 /* 89 * Set vlen to the length of the variable name and then 90 * reset len to the length of the text prior to '$'. If 91 * the name begins with a digit, interpret it using the 92 * the argv[] array. Otherwise we look in dt_macros. 93 * For the moment, all dt_macros variables are of type 94 * id_t (see dtrace_update() for more details on that). 95 */ 96 vlen = (size_t)(q + len - v); 97 len = (size_t)(v - q); 98 99 /* 100 * If the variable string begins with $$, skip past the 101 * leading dollar sign since $ and $$ are equivalent 102 * macro reference operators in a probe description. 103 */ 104 if (vlen > 2 && v[1] == '$') { 105 vlen--; 106 v++; 107 } 108 109 if (isdigit(v[1])) { 110 long i; 111 112 errno = 0; 113 i = strtol(v + 1, (char **)&w, 10); 114 115 wlen = vlen - (w - v); 116 117 if (i < 0 || i >= argc || errno != 0) 118 return (dt_set_errno(dtp, EDT_BADSPCV)); 119 120 v = argv[i]; 121 vlen = strlen(v); 122 123 if (yypcb != NULL && yypcb->pcb_sargv == argv) 124 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 125 126 } else if (vlen > 1) { 127 char *vstr = alloca(vlen); 128 dt_ident_t *idp; 129 130 (void) strncpy(vstr, v + 1, vlen - 1); 131 vstr[vlen - 1] = '\0'; 132 idp = dt_idhash_lookup(dtp->dt_macros, vstr); 133 134 if (idp == NULL) 135 return (dt_set_errno(dtp, EDT_BADSPCV)); 136 137 v = buf; 138 vlen = snprintf(buf, 32, "%d", idp->di_id); 139 140 } else 141 return (dt_set_errno(dtp, EDT_BADSPCV)); 142 } 143 144 if (spec == DTRACE_PROBESPEC_NONE) 145 return (dt_set_errno(dtp, EDT_BADSPEC)); 146 147 if (len + vlen >= dtrace_probespecs[spec].dtps_len) 148 return (dt_set_errno(dtp, ENAMETOOLONG)); 149 150 off = dtrace_probespecs[spec--].dtps_offset; 151 bcopy(q, (char *)pdp + off, len); 152 bcopy(v, (char *)pdp + off + len, vlen); 153 bcopy(w, (char *)pdp + off + len + vlen, wlen); 154 } while (--p >= s); 155 156 pdp->dtpd_id = DTRACE_IDNONE; 157 return (0); 158 } 159 160 int 161 dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec, 162 const char *s, dtrace_probedesc_t *pdp) 163 { 164 return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp)); 165 } 166 167 int 168 dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp) 169 { 170 bzero(pdp, sizeof (dtrace_probedesc_t)); 171 pdp->dtpd_id = id; 172 173 if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 || 174 pdp->dtpd_id != id) 175 return (dt_set_errno(dtp, EDT_BADID)); 176 177 return (0); 178 } 179 180 char * 181 dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len) 182 { 183 if (pdp->dtpd_id == 0) { 184 (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider, 185 pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 186 } else 187 (void) snprintf(buf, len, "%u", pdp->dtpd_id); 188 189 return (buf); 190 } 191 192 char * 193 dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len) 194 { 195 const char *name = dtrace_stability_name(attr.dtat_name); 196 const char *data = dtrace_stability_name(attr.dtat_data); 197 const char *class = dtrace_class_name(attr.dtat_class); 198 199 if (name == NULL || data == NULL || class == NULL) 200 return (NULL); /* one or more invalid attributes */ 201 202 (void) snprintf(buf, len, "%s/%s/%s", name, data, class); 203 return (buf); 204 } 205 206 static char * 207 dt_getstrattr(char *p, char **qp) 208 { 209 char *q; 210 211 if (*p == '\0') 212 return (NULL); 213 214 if ((q = strchr(p, '/')) == NULL) 215 q = p + strlen(p); 216 else 217 *q++ = '\0'; 218 219 *qp = q; 220 return (p); 221 } 222 223 int 224 dtrace_str2attr(const char *str, dtrace_attribute_t *attr) 225 { 226 dtrace_stability_t s; 227 dtrace_class_t c; 228 char *p, *q; 229 230 if (str == NULL || attr == NULL) 231 return (-1); /* invalid function arguments */ 232 233 *attr = _dtrace_maxattr; 234 p = alloca(strlen(str) + 1); 235 (void) strcpy(p, str); 236 237 if ((p = dt_getstrattr(p, &q)) == NULL) 238 return (0); 239 240 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 241 if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 242 attr->dtat_name = s; 243 break; 244 } 245 } 246 247 if (s > DTRACE_STABILITY_MAX) 248 return (-1); 249 250 if ((p = dt_getstrattr(q, &q)) == NULL) 251 return (0); 252 253 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 254 if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 255 attr->dtat_data = s; 256 break; 257 } 258 } 259 260 if (s > DTRACE_STABILITY_MAX) 261 return (-1); 262 263 if ((p = dt_getstrattr(q, &q)) == NULL) 264 return (0); 265 266 for (c = 0; c <= DTRACE_CLASS_MAX; c++) { 267 if (strcasecmp(p, dtrace_class_name(c)) == 0) { 268 attr->dtat_class = c; 269 break; 270 } 271 } 272 273 if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL) 274 return (-1); 275 276 return (0); 277 } 278 279 const char * 280 dtrace_stability_name(dtrace_stability_t s) 281 { 282 switch (s) { 283 case DTRACE_STABILITY_INTERNAL: return ("Internal"); 284 case DTRACE_STABILITY_PRIVATE: return ("Private"); 285 case DTRACE_STABILITY_OBSOLETE: return ("Obsolete"); 286 case DTRACE_STABILITY_EXTERNAL: return ("External"); 287 case DTRACE_STABILITY_UNSTABLE: return ("Unstable"); 288 case DTRACE_STABILITY_EVOLVING: return ("Evolving"); 289 case DTRACE_STABILITY_STABLE: return ("Stable"); 290 case DTRACE_STABILITY_STANDARD: return ("Standard"); 291 default: return (NULL); 292 } 293 } 294 295 const char * 296 dtrace_class_name(dtrace_class_t c) 297 { 298 switch (c) { 299 case DTRACE_CLASS_UNKNOWN: return ("Unknown"); 300 case DTRACE_CLASS_CPU: return ("CPU"); 301 case DTRACE_CLASS_PLATFORM: return ("Platform"); 302 case DTRACE_CLASS_GROUP: return ("Group"); 303 case DTRACE_CLASS_ISA: return ("ISA"); 304 case DTRACE_CLASS_COMMON: return ("Common"); 305 default: return (NULL); 306 } 307 } 308 309 dtrace_attribute_t 310 dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2) 311 { 312 dtrace_attribute_t am; 313 314 am.dtat_name = MIN(a1.dtat_name, a2.dtat_name); 315 am.dtat_data = MIN(a1.dtat_data, a2.dtat_data); 316 am.dtat_class = MIN(a1.dtat_class, a2.dtat_class); 317 318 return (am); 319 } 320 321 dtrace_attribute_t 322 dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2) 323 { 324 dtrace_attribute_t am; 325 326 am.dtat_name = MAX(a1.dtat_name, a2.dtat_name); 327 am.dtat_data = MAX(a1.dtat_data, a2.dtat_data); 328 am.dtat_class = MAX(a1.dtat_class, a2.dtat_class); 329 330 return (am); 331 } 332 333 /* 334 * Compare two attributes and return an integer value in the following ranges: 335 * 336 * <0 if any of a1's attributes are less than a2's attributes 337 * =0 if all of a1's attributes are equal to a2's attributes 338 * >0 if all of a1's attributes are greater than or equal to a2's attributes 339 * 340 * To implement this function efficiently, we subtract a2's attributes from 341 * a1's to obtain a negative result if an a1 attribute is less than its a2 342 * counterpart. We then OR the intermediate results together, relying on the 343 * twos-complement property that if any result is negative, the bitwise union 344 * will also be negative since the highest bit will be set in the result. 345 */ 346 int 347 dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2) 348 { 349 return (((int)a1.dtat_name - a2.dtat_name) | 350 ((int)a1.dtat_data - a2.dtat_data) | 351 ((int)a1.dtat_class - a2.dtat_class)); 352 } 353 354 char * 355 dt_attr_str(dtrace_attribute_t a, char *buf, size_t len) 356 { 357 static const char stability[] = "ipoxuesS"; 358 static const char class[] = "uCpgIc"; 359 360 if (a.dtat_name < sizeof (stability) && 361 a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) { 362 (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name], 363 stability[a.dtat_data], class[a.dtat_class]); 364 } else { 365 (void) snprintf(buf, len, "[%u/%u/%u]", 366 a.dtat_name, a.dtat_data, a.dtat_class); 367 } 368 369 return (buf); 370 } 371 372 char * 373 dt_version_num2str(dt_version_t v, char *buf, size_t len) 374 { 375 uint_t M = DT_VERSION_MAJOR(v); 376 uint_t m = DT_VERSION_MINOR(v); 377 uint_t u = DT_VERSION_MICRO(v); 378 379 if (u == 0) 380 (void) snprintf(buf, len, "%u.%u", M, m); 381 else 382 (void) snprintf(buf, len, "%u.%u.%u", M, m, u); 383 384 return (buf); 385 } 386 387 int 388 dt_version_str2num(const char *s, dt_version_t *vp) 389 { 390 int i = 0, n[3] = { 0, 0, 0 }; 391 char c; 392 393 while ((c = *s++) != '\0') { 394 if (isdigit(c)) 395 n[i] = n[i] * 10 + c - '0'; 396 else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1) 397 return (-1); 398 } 399 400 if (n[0] > DT_VERSION_MAJMAX || 401 n[1] > DT_VERSION_MINMAX || 402 n[2] > DT_VERSION_MICMAX) 403 return (-1); 404 405 if (vp != NULL) 406 *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]); 407 408 return (0); 409 } 410 411 int 412 dt_version_defined(dt_version_t v) 413 { 414 int i; 415 416 for (i = 0; _dtrace_versions[i] != 0; i++) { 417 if (_dtrace_versions[i] == v) 418 return (1); 419 } 420 421 return (0); 422 } 423 424 char * 425 dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str) 426 { 427 char *arg; 428 429 if (dtp->dt_cpp_argc == dtp->dt_cpp_args) { 430 int olds = dtp->dt_cpp_args; 431 int news = olds * 2; 432 char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news); 433 434 if (argv == NULL) 435 return (NULL); 436 437 bzero(&argv[olds], sizeof (char *) * olds); 438 dtp->dt_cpp_argv = argv; 439 dtp->dt_cpp_args = news; 440 } 441 442 if ((arg = strdup(str)) == NULL) 443 return (NULL); 444 445 assert(dtp->dt_cpp_argc < dtp->dt_cpp_args); 446 dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg; 447 return (arg); 448 } 449 450 char * 451 dt_cpp_pop_arg(dtrace_hdl_t *dtp) 452 { 453 char *arg; 454 455 if (dtp->dt_cpp_argc <= 1) 456 return (NULL); /* dt_cpp_argv[0] cannot be popped */ 457 458 arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc]; 459 dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL; 460 461 return (arg); 462 } 463 464 /*PRINTFLIKE1*/ 465 void 466 dt_dprintf(const char *format, ...) 467 { 468 if (_dtrace_debug) { 469 va_list alist; 470 471 va_start(alist, format); 472 (void) fputs("libdtrace DEBUG: ", stderr); 473 (void) vfprintf(stderr, format, alist); 474 va_end(alist); 475 } 476 } 477 478 int 479 #if defined(sun) 480 dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg) 481 #else 482 dt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg) 483 #endif 484 { 485 const dtrace_vector_t *v = dtp->dt_vector; 486 487 #if !defined(sun) 488 /* Avoid sign extension. */ 489 val &= 0xffffffff; 490 #endif 491 492 if (v != NULL) 493 return (v->dtv_ioctl(dtp->dt_varg, val, arg)); 494 495 if (dtp->dt_fd >= 0) 496 return (ioctl(dtp->dt_fd, val, arg)); 497 498 errno = EBADF; 499 return (-1); 500 } 501 502 int 503 dt_status(dtrace_hdl_t *dtp, processorid_t cpu) 504 { 505 const dtrace_vector_t *v = dtp->dt_vector; 506 507 if (v == NULL) { 508 #if defined(sun) 509 return (p_online(cpu, P_STATUS)); 510 #else 511 int maxid = 0; 512 size_t len = sizeof(maxid); 513 if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0) 514 return (cpu == 0 ? 1 : -1); 515 else 516 return (cpu <= maxid ? 1 : -1); 517 #endif 518 } 519 520 return (v->dtv_status(dtp->dt_varg, cpu)); 521 } 522 523 long 524 dt_sysconf(dtrace_hdl_t *dtp, int name) 525 { 526 const dtrace_vector_t *v = dtp->dt_vector; 527 528 if (v == NULL) 529 return (sysconf(name)); 530 531 return (v->dtv_sysconf(dtp->dt_varg, name)); 532 } 533 534 /* 535 * Wrapper around write(2) to handle partial writes. For maximum safety of 536 * output files and proper error reporting, we continuing writing in the 537 * face of partial writes until write(2) fails or 'buf' is completely written. 538 * We also record any errno in the specified dtrace_hdl_t as well as 'errno'. 539 */ 540 ssize_t 541 dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n) 542 { 543 ssize_t resid = n; 544 ssize_t len; 545 546 while (resid != 0) { 547 if ((len = write(fd, buf, resid)) <= 0) 548 break; 549 550 resid -= len; 551 buf = (char *)buf + len; 552 } 553 554 if (resid == n && n != 0) 555 return (dt_set_errno(dtp, errno)); 556 557 return (n - resid); 558 } 559 560 /* 561 * This function handles all output from libdtrace, as well as the 562 * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then 563 * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the 564 * specified buffer and return. Otherwise, if output is buffered (denoted by 565 * a NULL fp), we sprintf the desired output into the buffered buffer 566 * (expanding the buffer if required). If we don't satisfy either of these 567 * conditions (that is, if we are to actually generate output), then we call 568 * fprintf with the specified fp. In this case, we need to deal with one of 569 * the more annoying peculiarities of libc's printf routines: any failed 570 * write persistently sets an error flag inside the FILE causing every 571 * subsequent write to fail, but only the caller that initiated the error gets 572 * the errno. Since libdtrace clients often intercept SIGINT, this case is 573 * particularly frustrating since we don't want the EINTR on one attempt to 574 * write to the output file to preclude later attempts to write. This 575 * function therefore does a clearerr() if any error occurred, and saves the 576 * errno for the caller inside the specified dtrace_hdl_t. 577 */ 578 /*PRINTFLIKE3*/ 579 int 580 dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...) 581 { 582 va_list ap; 583 int n; 584 585 #if !defined(sun) 586 /* 587 * On FreeBSD, check if output is currently being re-directed 588 * to another file. If so, output to that file instead of the 589 * one the caller has specified. 590 */ 591 if (dtp->dt_freopen_fp != NULL) 592 fp = dtp->dt_freopen_fp; 593 #endif 594 595 va_start(ap, format); 596 597 if (dtp->dt_sprintf_buflen != 0) { 598 int len; 599 char *buf; 600 601 assert(dtp->dt_sprintf_buf != NULL); 602 603 buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)]; 604 len = dtp->dt_sprintf_buflen - len; 605 assert(len >= 0); 606 607 if ((n = vsnprintf(buf, len, format, ap)) < 0) 608 n = dt_set_errno(dtp, errno); 609 610 va_end(ap); 611 612 return (n); 613 } 614 615 if (fp == NULL) { 616 int needed, rval; 617 size_t avail; 618 619 /* 620 * It's not legal to use buffered ouput if there is not a 621 * handler for buffered output. 622 */ 623 if (dtp->dt_bufhdlr == NULL) { 624 va_end(ap); 625 return (dt_set_errno(dtp, EDT_NOBUFFERED)); 626 } 627 628 if (dtp->dt_buffered_buf == NULL) { 629 assert(dtp->dt_buffered_size == 0); 630 dtp->dt_buffered_size = 1; 631 dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size); 632 633 if (dtp->dt_buffered_buf == NULL) { 634 va_end(ap); 635 return (dt_set_errno(dtp, EDT_NOMEM)); 636 } 637 638 dtp->dt_buffered_offs = 0; 639 dtp->dt_buffered_buf[0] = '\0'; 640 } 641 642 if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) { 643 rval = dt_set_errno(dtp, errno); 644 va_end(ap); 645 return (rval); 646 } 647 648 if (needed == 0) { 649 va_end(ap); 650 return (0); 651 } 652 653 for (;;) { 654 char *newbuf; 655 656 assert(dtp->dt_buffered_offs < dtp->dt_buffered_size); 657 avail = dtp->dt_buffered_size - dtp->dt_buffered_offs; 658 659 if (needed + 1 < avail) 660 break; 661 662 if ((newbuf = realloc(dtp->dt_buffered_buf, 663 dtp->dt_buffered_size << 1)) == NULL) { 664 va_end(ap); 665 return (dt_set_errno(dtp, EDT_NOMEM)); 666 } 667 668 dtp->dt_buffered_buf = newbuf; 669 dtp->dt_buffered_size <<= 1; 670 } 671 672 if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs], 673 avail, format, ap) < 0) { 674 rval = dt_set_errno(dtp, errno); 675 va_end(ap); 676 return (rval); 677 } 678 679 dtp->dt_buffered_offs += needed; 680 assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0'); 681 va_end(ap); 682 return (0); 683 } 684 685 n = vfprintf(fp, format, ap); 686 fflush(fp); 687 va_end(ap); 688 689 if (n < 0) { 690 clearerr(fp); 691 return (dt_set_errno(dtp, errno)); 692 } 693 694 return (n); 695 } 696 697 int 698 dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata, 699 const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags) 700 { 701 dtrace_bufdata_t data; 702 703 if (dtp->dt_buffered_offs == 0) 704 return (0); 705 706 data.dtbda_handle = dtp; 707 data.dtbda_buffered = dtp->dt_buffered_buf; 708 data.dtbda_probe = pdata; 709 data.dtbda_recdesc = rec; 710 data.dtbda_aggdata = agg; 711 data.dtbda_flags = flags; 712 713 if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT) 714 return (dt_set_errno(dtp, EDT_DIRABORT)); 715 716 dtp->dt_buffered_offs = 0; 717 dtp->dt_buffered_buf[0] = '\0'; 718 719 return (0); 720 } 721 722 void 723 dt_buffered_destroy(dtrace_hdl_t *dtp) 724 { 725 free(dtp->dt_buffered_buf); 726 dtp->dt_buffered_buf = NULL; 727 dtp->dt_buffered_offs = 0; 728 dtp->dt_buffered_size = 0; 729 } 730 731 void * 732 dt_zalloc(dtrace_hdl_t *dtp, size_t size) 733 { 734 void *data; 735 736 if (size > 16 * 1024 * 1024) { 737 (void) dt_set_errno(dtp, EDT_NOMEM); 738 return (NULL); 739 } 740 741 if ((data = malloc(size)) == NULL) 742 (void) dt_set_errno(dtp, EDT_NOMEM); 743 else 744 bzero(data, size); 745 746 return (data); 747 } 748 749 void * 750 dt_alloc(dtrace_hdl_t *dtp, size_t size) 751 { 752 void *data; 753 754 if (size > 16 * 1024 * 1024) { 755 (void) dt_set_errno(dtp, EDT_NOMEM); 756 return (NULL); 757 } 758 759 if ((data = malloc(size)) == NULL) 760 (void) dt_set_errno(dtp, EDT_NOMEM); 761 762 return (data); 763 } 764 765 void 766 dt_free(dtrace_hdl_t *dtp, void *data) 767 { 768 assert(dtp != NULL); /* ensure sane use of this interface */ 769 free(data); 770 } 771 772 void 773 dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp) 774 { 775 if (dp == NULL) 776 return; /* simplify caller code */ 777 778 dt_free(dtp, dp->dtdo_buf); 779 dt_free(dtp, dp->dtdo_inttab); 780 dt_free(dtp, dp->dtdo_strtab); 781 dt_free(dtp, dp->dtdo_vartab); 782 dt_free(dtp, dp->dtdo_kreltab); 783 dt_free(dtp, dp->dtdo_ureltab); 784 dt_free(dtp, dp->dtdo_xlmtab); 785 786 dt_free(dtp, dp); 787 } 788 789 /* 790 * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also 791 * implements the behavior that an empty pattern matches any string. 792 */ 793 int 794 dt_gmatch(const char *s, const char *p) 795 { 796 return (p == NULL || *p == '\0' || gmatch(s, p)); 797 } 798 799 char * 800 dt_basename(char *str) 801 { 802 char *last = strrchr(str, '/'); 803 804 if (last == NULL) 805 return (str); 806 807 return (last + 1); 808 } 809 810 /* 811 * dt_popc() is a fast implementation of population count. The algorithm is 812 * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added. 813 */ 814 ulong_t 815 dt_popc(ulong_t x) 816 { 817 #if defined(_ILP32) 818 x = x - ((x >> 1) & 0x55555555UL); 819 x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL); 820 x = (x + (x >> 4)) & 0x0F0F0F0FUL; 821 x = x + (x >> 8); 822 x = x + (x >> 16); 823 return (x & 0x3F); 824 #elif defined(_LP64) 825 x = x - ((x >> 1) & 0x5555555555555555ULL); 826 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); 827 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL; 828 x = x + (x >> 8); 829 x = x + (x >> 16); 830 x = x + (x >> 32); 831 return (x & 0x7F); 832 #else 833 /* This should be a #warning but for now ignore error. Err: "need td_popc() implementation" */ 834 #endif 835 } 836 837 /* 838 * dt_popcb() is a bitmap-based version of population count that returns the 839 * number of one bits in the specified bitmap 'bp' at bit positions below 'n'. 840 */ 841 ulong_t 842 dt_popcb(const ulong_t *bp, ulong_t n) 843 { 844 ulong_t maxb = n & BT_ULMASK; 845 ulong_t maxw = n >> BT_ULSHIFT; 846 ulong_t w, popc = 0; 847 848 if (n == 0) 849 return (0); 850 851 for (w = 0; w < maxw; w++) 852 popc += dt_popc(bp[w]); 853 854 return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1))); 855 } 856 857 #if defined(sun) 858 struct _rwlock; 859 struct _lwp_mutex; 860 861 int 862 dt_rw_read_held(pthread_rwlock_t *lock) 863 { 864 extern int _rw_read_held(struct _rwlock *); 865 return (_rw_read_held((struct _rwlock *)lock)); 866 } 867 868 int 869 dt_rw_write_held(pthread_rwlock_t *lock) 870 { 871 extern int _rw_write_held(struct _rwlock *); 872 return (_rw_write_held((struct _rwlock *)lock)); 873 } 874 #endif 875 876 int 877 dt_mutex_held(pthread_mutex_t *lock) 878 { 879 #if defined(sun) 880 extern int _mutex_held(struct _lwp_mutex *); 881 return (_mutex_held((struct _lwp_mutex *)lock)); 882 #else 883 return (1); 884 #endif 885 } 886 887 static int 888 dt_string2str(char *s, char *str, int nbytes) 889 { 890 int len = strlen(s); 891 892 if (nbytes == 0) { 893 /* 894 * Like snprintf(3C), we don't check the value of str if the 895 * number of bytes is 0. 896 */ 897 return (len); 898 } 899 900 if (nbytes <= len) { 901 (void) strncpy(str, s, nbytes - 1); 902 /* 903 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee 904 * that the string is null-terminated. 905 */ 906 str[nbytes - 1] = '\0'; 907 } else { 908 (void) strcpy(str, s); 909 } 910 911 return (len); 912 } 913 914 int 915 dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes) 916 { 917 dtrace_syminfo_t dts; 918 GElf_Sym sym; 919 920 size_t n = 20; /* for 0x%llx\0 */ 921 char *s; 922 int err; 923 924 if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0) 925 n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */ 926 927 s = alloca(n); 928 929 if (err == 0 && addr != sym.st_value) { 930 (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object, 931 dts.dts_name, (u_longlong_t)addr - sym.st_value); 932 } else if (err == 0) { 933 (void) snprintf(s, n, "%s`%s", 934 dts.dts_object, dts.dts_name); 935 } else { 936 /* 937 * We'll repeat the lookup, but this time we'll specify a NULL 938 * GElf_Sym -- indicating that we're only interested in the 939 * containing module. 940 */ 941 if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) { 942 (void) snprintf(s, n, "%s`0x%llx", dts.dts_object, 943 (u_longlong_t)addr); 944 } else { 945 (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr); 946 } 947 } 948 949 return (dt_string2str(s, str, nbytes)); 950 } 951 952 int 953 dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid, 954 uint64_t addr, char *str, int nbytes) 955 { 956 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 957 struct ps_prochandle *P = NULL; 958 GElf_Sym sym; 959 char *obj; 960 961 if (pid != 0) 962 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 963 964 if (P == NULL) { 965 (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr); 966 return (dt_string2str(c, str, nbytes)); 967 } 968 969 dt_proc_lock(dtp, P); 970 971 if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) { 972 (void) Pobjname(P, addr, objname, sizeof (objname)); 973 974 obj = dt_basename(objname); 975 976 if (addr > sym.st_value) { 977 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj, 978 name, (u_longlong_t)(addr - sym.st_value)); 979 } else { 980 (void) snprintf(c, sizeof (c), "%s`%s", obj, name); 981 } 982 } else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) { 983 (void) snprintf(c, sizeof (c), "%s`0x%jx", 984 dt_basename(objname), (uintmax_t)addr); 985 } else { 986 (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr); 987 } 988 989 dt_proc_unlock(dtp, P); 990 dt_proc_release(dtp, P); 991 992 return (dt_string2str(c, str, nbytes)); 993 } 994