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