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