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