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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 2011, Joyent, Inc. All rights reserved. 28 */ 29 30 #include <stdlib.h> 31 #include <strings.h> 32 #include <errno.h> 33 #include <unistd.h> 34 #include <limits.h> 35 #include <assert.h> 36 #include <ctype.h> 37 #include <alloca.h> 38 #include <dt_impl.h> 39 40 #define DT_MASK_LO 0x00000000FFFFFFFFULL 41 42 /* 43 * We declare this here because (1) we need it and (2) we want to avoid a 44 * dependency on libm in libdtrace. 45 */ 46 static long double 47 dt_fabsl(long double x) 48 { 49 if (x < 0) 50 return (-x); 51 52 return (x); 53 } 54 55 /* 56 * 128-bit arithmetic functions needed to support the stddev() aggregating 57 * action. 58 */ 59 static int 60 dt_gt_128(uint64_t *a, uint64_t *b) 61 { 62 return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0])); 63 } 64 65 static int 66 dt_ge_128(uint64_t *a, uint64_t *b) 67 { 68 return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0])); 69 } 70 71 static int 72 dt_le_128(uint64_t *a, uint64_t *b) 73 { 74 return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0])); 75 } 76 77 /* 78 * Shift the 128-bit value in a by b. If b is positive, shift left. 79 * If b is negative, shift right. 80 */ 81 static void 82 dt_shift_128(uint64_t *a, int b) 83 { 84 uint64_t mask; 85 86 if (b == 0) 87 return; 88 89 if (b < 0) { 90 b = -b; 91 if (b >= 64) { 92 a[0] = a[1] >> (b - 64); 93 a[1] = 0; 94 } else { 95 a[0] >>= b; 96 mask = 1LL << (64 - b); 97 mask -= 1; 98 a[0] |= ((a[1] & mask) << (64 - b)); 99 a[1] >>= b; 100 } 101 } else { 102 if (b >= 64) { 103 a[1] = a[0] << (b - 64); 104 a[0] = 0; 105 } else { 106 a[1] <<= b; 107 mask = a[0] >> (64 - b); 108 a[1] |= mask; 109 a[0] <<= b; 110 } 111 } 112 } 113 114 static int 115 dt_nbits_128(uint64_t *a) 116 { 117 int nbits = 0; 118 uint64_t tmp[2]; 119 uint64_t zero[2] = { 0, 0 }; 120 121 tmp[0] = a[0]; 122 tmp[1] = a[1]; 123 124 dt_shift_128(tmp, -1); 125 while (dt_gt_128(tmp, zero)) { 126 dt_shift_128(tmp, -1); 127 nbits++; 128 } 129 130 return (nbits); 131 } 132 133 static void 134 dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference) 135 { 136 uint64_t result[2]; 137 138 result[0] = minuend[0] - subtrahend[0]; 139 result[1] = minuend[1] - subtrahend[1] - 140 (minuend[0] < subtrahend[0] ? 1 : 0); 141 142 difference[0] = result[0]; 143 difference[1] = result[1]; 144 } 145 146 static void 147 dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 148 { 149 uint64_t result[2]; 150 151 result[0] = addend1[0] + addend2[0]; 152 result[1] = addend1[1] + addend2[1] + 153 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 154 155 sum[0] = result[0]; 156 sum[1] = result[1]; 157 } 158 159 /* 160 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 161 * use native multiplication on those, and then re-combine into the 162 * resulting 128-bit value. 163 * 164 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 165 * hi1 * hi2 << 64 + 166 * hi1 * lo2 << 32 + 167 * hi2 * lo1 << 32 + 168 * lo1 * lo2 169 */ 170 static void 171 dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 172 { 173 uint64_t hi1, hi2, lo1, lo2; 174 uint64_t tmp[2]; 175 176 hi1 = factor1 >> 32; 177 hi2 = factor2 >> 32; 178 179 lo1 = factor1 & DT_MASK_LO; 180 lo2 = factor2 & DT_MASK_LO; 181 182 product[0] = lo1 * lo2; 183 product[1] = hi1 * hi2; 184 185 tmp[0] = hi1 * lo2; 186 tmp[1] = 0; 187 dt_shift_128(tmp, 32); 188 dt_add_128(product, tmp, product); 189 190 tmp[0] = hi2 * lo1; 191 tmp[1] = 0; 192 dt_shift_128(tmp, 32); 193 dt_add_128(product, tmp, product); 194 } 195 196 /* 197 * This is long-hand division. 198 * 199 * We initialize subtrahend by shifting divisor left as far as possible. We 200 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we 201 * subtract and set the appropriate bit in the result. We then shift 202 * subtrahend right by one bit for the next comparison. 203 */ 204 static void 205 dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient) 206 { 207 uint64_t result[2] = { 0, 0 }; 208 uint64_t remainder[2]; 209 uint64_t subtrahend[2]; 210 uint64_t divisor_128[2]; 211 uint64_t mask[2] = { 1, 0 }; 212 int log = 0; 213 214 assert(divisor != 0); 215 216 divisor_128[0] = divisor; 217 divisor_128[1] = 0; 218 219 remainder[0] = dividend[0]; 220 remainder[1] = dividend[1]; 221 222 subtrahend[0] = divisor; 223 subtrahend[1] = 0; 224 225 while (divisor > 0) { 226 log++; 227 divisor >>= 1; 228 } 229 230 dt_shift_128(subtrahend, 128 - log); 231 dt_shift_128(mask, 128 - log); 232 233 while (dt_ge_128(remainder, divisor_128)) { 234 if (dt_ge_128(remainder, subtrahend)) { 235 dt_subtract_128(remainder, subtrahend, remainder); 236 result[0] |= mask[0]; 237 result[1] |= mask[1]; 238 } 239 240 dt_shift_128(subtrahend, -1); 241 dt_shift_128(mask, -1); 242 } 243 244 quotient[0] = result[0]; 245 quotient[1] = result[1]; 246 } 247 248 /* 249 * This is the long-hand method of calculating a square root. 250 * The algorithm is as follows: 251 * 252 * 1. Group the digits by 2 from the right. 253 * 2. Over the leftmost group, find the largest single-digit number 254 * whose square is less than that group. 255 * 3. Subtract the result of the previous step (2 or 4, depending) and 256 * bring down the next two-digit group. 257 * 4. For the result R we have so far, find the largest single-digit number 258 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3. 259 * (Note that this is doubling R and performing a decimal left-shift by 1 260 * and searching for the appropriate decimal to fill the one's place.) 261 * The value x is the next digit in the square root. 262 * Repeat steps 3 and 4 until the desired precision is reached. (We're 263 * dealing with integers, so the above is sufficient.) 264 * 265 * In decimal, the square root of 582,734 would be calculated as so: 266 * 267 * __7__6__3 268 * | 58 27 34 269 * -49 (7^2 == 49 => 7 is the first digit in the square root) 270 * -- 271 * 9 27 (Subtract and bring down the next group.) 272 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in 273 * ----- the square root) 274 * 51 34 (Subtract and bring down the next group.) 275 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in 276 * ----- the square root) 277 * 5 65 (remainder) 278 * 279 * The above algorithm applies similarly in binary, but note that the 280 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a 281 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the 282 * preceding difference? 283 * 284 * In binary, the square root of 11011011 would be calculated as so: 285 * 286 * __1__1__1__0 287 * | 11 01 10 11 288 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1) 289 * -- 290 * 10 01 10 11 291 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1) 292 * ----- 293 * 1 00 10 11 294 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1) 295 * ------- 296 * 1 01 11 297 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0) 298 * 299 */ 300 static uint64_t 301 dt_sqrt_128(uint64_t *square) 302 { 303 uint64_t result[2] = { 0, 0 }; 304 uint64_t diff[2] = { 0, 0 }; 305 uint64_t one[2] = { 1, 0 }; 306 uint64_t next_pair[2]; 307 uint64_t next_try[2]; 308 uint64_t bit_pairs, pair_shift; 309 int i; 310 311 bit_pairs = dt_nbits_128(square) / 2; 312 pair_shift = bit_pairs * 2; 313 314 for (i = 0; i <= bit_pairs; i++) { 315 /* 316 * Bring down the next pair of bits. 317 */ 318 next_pair[0] = square[0]; 319 next_pair[1] = square[1]; 320 dt_shift_128(next_pair, -pair_shift); 321 next_pair[0] &= 0x3; 322 next_pair[1] = 0; 323 324 dt_shift_128(diff, 2); 325 dt_add_128(diff, next_pair, diff); 326 327 /* 328 * next_try = R << 2 + 1 329 */ 330 next_try[0] = result[0]; 331 next_try[1] = result[1]; 332 dt_shift_128(next_try, 2); 333 dt_add_128(next_try, one, next_try); 334 335 if (dt_le_128(next_try, diff)) { 336 dt_subtract_128(diff, next_try, diff); 337 dt_shift_128(result, 1); 338 dt_add_128(result, one, result); 339 } else { 340 dt_shift_128(result, 1); 341 } 342 343 pair_shift -= 2; 344 } 345 346 assert(result[1] == 0); 347 348 return (result[0]); 349 } 350 351 uint64_t 352 dt_stddev(uint64_t *data, uint64_t normal) 353 { 354 uint64_t avg_of_squares[2]; 355 uint64_t square_of_avg[2]; 356 int64_t norm_avg; 357 uint64_t diff[2]; 358 359 /* 360 * The standard approximation for standard deviation is 361 * sqrt(average(x**2) - average(x)**2), i.e. the square root 362 * of the average of the squares minus the square of the average. 363 */ 364 dt_divide_128(data + 2, normal, avg_of_squares); 365 dt_divide_128(avg_of_squares, data[0], avg_of_squares); 366 367 norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0]; 368 369 if (norm_avg < 0) 370 norm_avg = -norm_avg; 371 372 dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg); 373 374 dt_subtract_128(avg_of_squares, square_of_avg, diff); 375 376 return (dt_sqrt_128(diff)); 377 } 378 379 static int 380 dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last, 381 dtrace_bufdesc_t *buf, size_t offs) 382 { 383 dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd; 384 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd; 385 char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub; 386 dtrace_flowkind_t flow = DTRACEFLOW_NONE; 387 const char *str = NULL; 388 static const char *e_str[2] = { " -> ", " => " }; 389 static const char *r_str[2] = { " <- ", " <= " }; 390 static const char *ent = "entry", *ret = "return"; 391 static int entlen = 0, retlen = 0; 392 dtrace_epid_t next, id = epd->dtepd_epid; 393 int rval; 394 395 if (entlen == 0) { 396 assert(retlen == 0); 397 entlen = strlen(ent); 398 retlen = strlen(ret); 399 } 400 401 /* 402 * If the name of the probe is "entry" or ends with "-entry", we 403 * treat it as an entry; if it is "return" or ends with "-return", 404 * we treat it as a return. (This allows application-provided probes 405 * like "method-entry" or "function-entry" to participate in flow 406 * indentation -- without accidentally misinterpreting popular probe 407 * names like "carpentry", "gentry" or "Coventry".) 408 */ 409 if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' && 410 (sub == n || sub[-1] == '-')) { 411 flow = DTRACEFLOW_ENTRY; 412 str = e_str[strcmp(p, "syscall") == 0]; 413 } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' && 414 (sub == n || sub[-1] == '-')) { 415 flow = DTRACEFLOW_RETURN; 416 str = r_str[strcmp(p, "syscall") == 0]; 417 } 418 419 /* 420 * If we're going to indent this, we need to check the ID of our last 421 * call. If we're looking at the same probe ID but a different EPID, 422 * we _don't_ want to indent. (Yes, there are some minor holes in 423 * this scheme -- it's a heuristic.) 424 */ 425 if (flow == DTRACEFLOW_ENTRY) { 426 if ((last != DTRACE_EPIDNONE && id != last && 427 pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id)) 428 flow = DTRACEFLOW_NONE; 429 } 430 431 /* 432 * If we're going to unindent this, it's more difficult to see if 433 * we don't actually want to unindent it -- we need to look at the 434 * _next_ EPID. 435 */ 436 if (flow == DTRACEFLOW_RETURN) { 437 offs += epd->dtepd_size; 438 439 do { 440 if (offs >= buf->dtbd_size) { 441 /* 442 * We're at the end -- maybe. If the oldest 443 * record is non-zero, we need to wrap. 444 */ 445 if (buf->dtbd_oldest != 0) { 446 offs = 0; 447 } else { 448 goto out; 449 } 450 } 451 452 next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 453 454 if (next == DTRACE_EPIDNONE) 455 offs += sizeof (id); 456 } while (next == DTRACE_EPIDNONE); 457 458 if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0) 459 return (rval); 460 461 if (next != id && npd->dtpd_id == pd->dtpd_id) 462 flow = DTRACEFLOW_NONE; 463 } 464 465 out: 466 if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) { 467 data->dtpda_prefix = str; 468 } else { 469 data->dtpda_prefix = "| "; 470 } 471 472 if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0) 473 data->dtpda_indent -= 2; 474 475 data->dtpda_flow = flow; 476 477 return (0); 478 } 479 480 static int 481 dt_nullprobe() 482 { 483 return (DTRACE_CONSUME_THIS); 484 } 485 486 static int 487 dt_nullrec() 488 { 489 return (DTRACE_CONSUME_NEXT); 490 } 491 492 int 493 dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val, 494 uint64_t normal, long double total, char positives, char negatives) 495 { 496 long double f; 497 uint_t depth, len = 40; 498 499 const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; 500 const char *spaces = " "; 501 502 assert(strlen(ats) == len && strlen(spaces) == len); 503 assert(!(total == 0 && (positives || negatives))); 504 assert(!(val < 0 && !negatives)); 505 assert(!(val > 0 && !positives)); 506 assert(!(val != 0 && total == 0)); 507 508 if (!negatives) { 509 if (positives) { 510 f = (dt_fabsl((long double)val) * len) / total; 511 depth = (uint_t)(f + 0.5); 512 } else { 513 depth = 0; 514 } 515 516 return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth, 517 spaces + depth, (long long)val / normal)); 518 } 519 520 if (!positives) { 521 f = (dt_fabsl((long double)val) * len) / total; 522 depth = (uint_t)(f + 0.5); 523 524 return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth, 525 ats + len - depth, (long long)val / normal)); 526 } 527 528 /* 529 * If we're here, we have both positive and negative bucket values. 530 * To express this graphically, we're going to generate both positive 531 * and negative bars separated by a centerline. These bars are half 532 * the size of normal quantize()/lquantize() bars, so we divide the 533 * length in half before calculating the bar length. 534 */ 535 len /= 2; 536 ats = &ats[len]; 537 spaces = &spaces[len]; 538 539 f = (dt_fabsl((long double)val) * len) / total; 540 depth = (uint_t)(f + 0.5); 541 542 if (val <= 0) { 543 return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth, 544 ats + len - depth, len, "", (long long)val / normal)); 545 } else { 546 return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "", 547 ats + len - depth, spaces + depth, 548 (long long)val / normal)); 549 } 550 } 551 552 int 553 dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 554 size_t size, uint64_t normal) 555 { 556 const int64_t *data = addr; 557 int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1; 558 long double total = 0; 559 char positives = 0, negatives = 0; 560 561 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t)) 562 return (dt_set_errno(dtp, EDT_DMISMATCH)); 563 564 while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0) 565 first_bin++; 566 567 if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) { 568 /* 569 * There isn't any data. This is possible if (and only if) 570 * negative increment values have been used. In this case, 571 * we'll print the buckets around 0. 572 */ 573 first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1; 574 last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1; 575 } else { 576 if (first_bin > 0) 577 first_bin--; 578 579 while (last_bin > 0 && data[last_bin] == 0) 580 last_bin--; 581 582 if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1) 583 last_bin++; 584 } 585 586 for (i = first_bin; i <= last_bin; i++) { 587 positives |= (data[i] > 0); 588 negatives |= (data[i] < 0); 589 total += dt_fabsl((long double)data[i]); 590 } 591 592 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 593 "------------- Distribution -------------", "count") < 0) 594 return (-1); 595 596 for (i = first_bin; i <= last_bin; i++) { 597 if (dt_printf(dtp, fp, "%16lld ", 598 (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0) 599 return (-1); 600 601 if (dt_print_quantline(dtp, fp, data[i], normal, total, 602 positives, negatives) < 0) 603 return (-1); 604 } 605 606 return (0); 607 } 608 609 int 610 dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 611 size_t size, uint64_t normal) 612 { 613 const int64_t *data = addr; 614 int i, first_bin, last_bin, base; 615 uint64_t arg; 616 long double total = 0; 617 uint16_t step, levels; 618 char positives = 0, negatives = 0; 619 620 if (size < sizeof (uint64_t)) 621 return (dt_set_errno(dtp, EDT_DMISMATCH)); 622 623 arg = *data++; 624 size -= sizeof (uint64_t); 625 626 base = DTRACE_LQUANTIZE_BASE(arg); 627 step = DTRACE_LQUANTIZE_STEP(arg); 628 levels = DTRACE_LQUANTIZE_LEVELS(arg); 629 630 first_bin = 0; 631 last_bin = levels + 1; 632 633 if (size != sizeof (uint64_t) * (levels + 2)) 634 return (dt_set_errno(dtp, EDT_DMISMATCH)); 635 636 while (first_bin <= levels + 1 && data[first_bin] == 0) 637 first_bin++; 638 639 if (first_bin > levels + 1) { 640 first_bin = 0; 641 last_bin = 2; 642 } else { 643 if (first_bin > 0) 644 first_bin--; 645 646 while (last_bin > 0 && data[last_bin] == 0) 647 last_bin--; 648 649 if (last_bin < levels + 1) 650 last_bin++; 651 } 652 653 for (i = first_bin; i <= last_bin; i++) { 654 positives |= (data[i] > 0); 655 negatives |= (data[i] < 0); 656 total += dt_fabsl((long double)data[i]); 657 } 658 659 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 660 "------------- Distribution -------------", "count") < 0) 661 return (-1); 662 663 for (i = first_bin; i <= last_bin; i++) { 664 char c[32]; 665 int err; 666 667 if (i == 0) { 668 (void) snprintf(c, sizeof (c), "< %d", 669 base / (uint32_t)normal); 670 err = dt_printf(dtp, fp, "%16s ", c); 671 } else if (i == levels + 1) { 672 (void) snprintf(c, sizeof (c), ">= %d", 673 base + (levels * step)); 674 err = dt_printf(dtp, fp, "%16s ", c); 675 } else { 676 err = dt_printf(dtp, fp, "%16d ", 677 base + (i - 1) * step); 678 } 679 680 if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal, 681 total, positives, negatives) < 0) 682 return (-1); 683 } 684 685 return (0); 686 } 687 688 int 689 dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 690 size_t size, uint64_t normal) 691 { 692 int i, first_bin, last_bin, bin = 1, order, levels; 693 uint16_t factor, low, high, nsteps; 694 const int64_t *data = addr; 695 int64_t value = 1, next, step; 696 char positives = 0, negatives = 0; 697 long double total = 0; 698 uint64_t arg; 699 char c[32]; 700 701 if (size < sizeof (uint64_t)) 702 return (dt_set_errno(dtp, EDT_DMISMATCH)); 703 704 arg = *data++; 705 size -= sizeof (uint64_t); 706 707 factor = DTRACE_LLQUANTIZE_FACTOR(arg); 708 low = DTRACE_LLQUANTIZE_LOW(arg); 709 high = DTRACE_LLQUANTIZE_HIGH(arg); 710 nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 711 712 /* 713 * We don't expect to be handed invalid llquantize() parameters here, 714 * but sanity check them (to a degree) nonetheless. 715 */ 716 if (size > INT32_MAX || factor < 2 || low >= high || 717 nsteps == 0 || factor > nsteps) 718 return (dt_set_errno(dtp, EDT_DMISMATCH)); 719 720 levels = (int)size / sizeof (uint64_t); 721 722 first_bin = 0; 723 last_bin = levels - 1; 724 725 while (first_bin < levels && data[first_bin] == 0) 726 first_bin++; 727 728 if (first_bin == levels) { 729 first_bin = 0; 730 last_bin = 1; 731 } else { 732 if (first_bin > 0) 733 first_bin--; 734 735 while (last_bin > 0 && data[last_bin] == 0) 736 last_bin--; 737 738 if (last_bin < levels - 1) 739 last_bin++; 740 } 741 742 for (i = first_bin; i <= last_bin; i++) { 743 positives |= (data[i] > 0); 744 negatives |= (data[i] < 0); 745 total += dt_fabsl((long double)data[i]); 746 } 747 748 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 749 "------------- Distribution -------------", "count") < 0) 750 return (-1); 751 752 for (order = 0; order < low; order++) 753 value *= factor; 754 755 next = value * factor; 756 step = next > nsteps ? next / nsteps : 1; 757 758 if (first_bin == 0) { 759 (void) snprintf(c, sizeof (c), "< %lld", value); 760 761 if (dt_printf(dtp, fp, "%16s ", c) < 0) 762 return (-1); 763 764 if (dt_print_quantline(dtp, fp, data[0], normal, 765 total, positives, negatives) < 0) 766 return (-1); 767 } 768 769 while (order <= high) { 770 if (bin >= first_bin && bin <= last_bin) { 771 if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0) 772 return (-1); 773 774 if (dt_print_quantline(dtp, fp, data[bin], 775 normal, total, positives, negatives) < 0) 776 return (-1); 777 } 778 779 assert(value < next); 780 bin++; 781 782 if ((value += step) != next) 783 continue; 784 785 next = value * factor; 786 step = next > nsteps ? next / nsteps : 1; 787 order++; 788 } 789 790 if (last_bin < bin) 791 return (0); 792 793 assert(last_bin == bin); 794 (void) snprintf(c, sizeof (c), ">= %lld", value); 795 796 if (dt_printf(dtp, fp, "%16s ", c) < 0) 797 return (-1); 798 799 return (dt_print_quantline(dtp, fp, data[bin], normal, 800 total, positives, negatives)); 801 } 802 803 /*ARGSUSED*/ 804 static int 805 dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 806 size_t size, uint64_t normal) 807 { 808 /* LINTED - alignment */ 809 int64_t *data = (int64_t *)addr; 810 811 return (dt_printf(dtp, fp, " %16lld", data[0] ? 812 (long long)(data[1] / (int64_t)normal / data[0]) : 0)); 813 } 814 815 /*ARGSUSED*/ 816 static int 817 dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 818 size_t size, uint64_t normal) 819 { 820 /* LINTED - alignment */ 821 uint64_t *data = (uint64_t *)addr; 822 823 return (dt_printf(dtp, fp, " %16llu", data[0] ? 824 (unsigned long long) dt_stddev(data, normal) : 0)); 825 } 826 827 /*ARGSUSED*/ 828 int 829 dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 830 size_t nbytes, int width, int quiet) 831 { 832 /* 833 * If the byte stream is a series of printable characters, followed by 834 * a terminating byte, we print it out as a string. Otherwise, we 835 * assume that it's something else and just print the bytes. 836 */ 837 int i, j, margin = 5; 838 char *c = (char *)addr; 839 840 if (nbytes == 0) 841 return (0); 842 843 if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET) 844 goto raw; 845 846 for (i = 0; i < nbytes; i++) { 847 /* 848 * We define a "printable character" to be one for which 849 * isprint(3C) returns non-zero, isspace(3C) returns non-zero, 850 * or a character which is either backspace or the bell. 851 * Backspace and the bell are regrettably special because 852 * they fail the first two tests -- and yet they are entirely 853 * printable. These are the only two control characters that 854 * have meaning for the terminal and for which isprint(3C) and 855 * isspace(3C) return 0. 856 */ 857 if (isprint(c[i]) || isspace(c[i]) || 858 c[i] == '\b' || c[i] == '\a') 859 continue; 860 861 if (c[i] == '\0' && i > 0) { 862 /* 863 * This looks like it might be a string. Before we 864 * assume that it is indeed a string, check the 865 * remainder of the byte range; if it contains 866 * additional non-nul characters, we'll assume that 867 * it's a binary stream that just happens to look like 868 * a string, and we'll print out the individual bytes. 869 */ 870 for (j = i + 1; j < nbytes; j++) { 871 if (c[j] != '\0') 872 break; 873 } 874 875 if (j != nbytes) 876 break; 877 878 if (quiet) 879 return (dt_printf(dtp, fp, "%s", c)); 880 else 881 return (dt_printf(dtp, fp, " %-*s", width, c)); 882 } 883 884 break; 885 } 886 887 if (i == nbytes) { 888 /* 889 * The byte range is all printable characters, but there is 890 * no trailing nul byte. We'll assume that it's a string and 891 * print it as such. 892 */ 893 char *s = alloca(nbytes + 1); 894 bcopy(c, s, nbytes); 895 s[nbytes] = '\0'; 896 return (dt_printf(dtp, fp, " %-*s", width, s)); 897 } 898 899 raw: 900 if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0) 901 return (-1); 902 903 for (i = 0; i < 16; i++) 904 if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0) 905 return (-1); 906 907 if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0) 908 return (-1); 909 910 911 for (i = 0; i < nbytes; i += 16) { 912 if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0) 913 return (-1); 914 915 for (j = i; j < i + 16 && j < nbytes; j++) { 916 if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0) 917 return (-1); 918 } 919 920 while (j++ % 16) { 921 if (dt_printf(dtp, fp, " ") < 0) 922 return (-1); 923 } 924 925 if (dt_printf(dtp, fp, " ") < 0) 926 return (-1); 927 928 for (j = i; j < i + 16 && j < nbytes; j++) { 929 if (dt_printf(dtp, fp, "%c", 930 c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0) 931 return (-1); 932 } 933 934 if (dt_printf(dtp, fp, "\n") < 0) 935 return (-1); 936 } 937 938 return (0); 939 } 940 941 int 942 dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 943 caddr_t addr, int depth, int size) 944 { 945 dtrace_syminfo_t dts; 946 GElf_Sym sym; 947 int i, indent; 948 char c[PATH_MAX * 2]; 949 uint64_t pc; 950 951 if (dt_printf(dtp, fp, "\n") < 0) 952 return (-1); 953 954 if (format == NULL) 955 format = "%s"; 956 957 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 958 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 959 else 960 indent = _dtrace_stkindent; 961 962 for (i = 0; i < depth; i++) { 963 switch (size) { 964 case sizeof (uint32_t): 965 /* LINTED - alignment */ 966 pc = *((uint32_t *)addr); 967 break; 968 969 case sizeof (uint64_t): 970 /* LINTED - alignment */ 971 pc = *((uint64_t *)addr); 972 break; 973 974 default: 975 return (dt_set_errno(dtp, EDT_BADSTACKPC)); 976 } 977 978 if (pc == NULL) 979 break; 980 981 addr += size; 982 983 if (dt_printf(dtp, fp, "%*s", indent, "") < 0) 984 return (-1); 985 986 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 987 if (pc > sym.st_value) { 988 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", 989 dts.dts_object, dts.dts_name, 990 pc - sym.st_value); 991 } else { 992 (void) snprintf(c, sizeof (c), "%s`%s", 993 dts.dts_object, dts.dts_name); 994 } 995 } else { 996 /* 997 * We'll repeat the lookup, but this time we'll specify 998 * a NULL GElf_Sym -- indicating that we're only 999 * interested in the containing module. 1000 */ 1001 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1002 (void) snprintf(c, sizeof (c), "%s`0x%llx", 1003 dts.dts_object, pc); 1004 } else { 1005 (void) snprintf(c, sizeof (c), "0x%llx", pc); 1006 } 1007 } 1008 1009 if (dt_printf(dtp, fp, format, c) < 0) 1010 return (-1); 1011 1012 if (dt_printf(dtp, fp, "\n") < 0) 1013 return (-1); 1014 } 1015 1016 return (0); 1017 } 1018 1019 int 1020 dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 1021 caddr_t addr, uint64_t arg) 1022 { 1023 /* LINTED - alignment */ 1024 uint64_t *pc = (uint64_t *)addr; 1025 uint32_t depth = DTRACE_USTACK_NFRAMES(arg); 1026 uint32_t strsize = DTRACE_USTACK_STRSIZE(arg); 1027 const char *strbase = addr + (depth + 1) * sizeof (uint64_t); 1028 const char *str = strsize ? strbase : NULL; 1029 int err = 0; 1030 1031 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 1032 struct ps_prochandle *P; 1033 GElf_Sym sym; 1034 int i, indent; 1035 pid_t pid; 1036 1037 if (depth == 0) 1038 return (0); 1039 1040 pid = (pid_t)*pc++; 1041 1042 if (dt_printf(dtp, fp, "\n") < 0) 1043 return (-1); 1044 1045 if (format == NULL) 1046 format = "%s"; 1047 1048 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 1049 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 1050 else 1051 indent = _dtrace_stkindent; 1052 1053 /* 1054 * Ultimately, we need to add an entry point in the library vector for 1055 * determining <symbol, offset> from <pid, address>. For now, if 1056 * this is a vector open, we just print the raw address or string. 1057 */ 1058 if (dtp->dt_vector == NULL) 1059 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1060 else 1061 P = NULL; 1062 1063 if (P != NULL) 1064 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1065 1066 for (i = 0; i < depth && pc[i] != NULL; i++) { 1067 const prmap_t *map; 1068 1069 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1070 break; 1071 1072 if (P != NULL && Plookup_by_addr(P, pc[i], 1073 name, sizeof (name), &sym) == 0) { 1074 (void) Pobjname(P, pc[i], objname, sizeof (objname)); 1075 1076 if (pc[i] > sym.st_value) { 1077 (void) snprintf(c, sizeof (c), 1078 "%s`%s+0x%llx", dt_basename(objname), name, 1079 (u_longlong_t)(pc[i] - sym.st_value)); 1080 } else { 1081 (void) snprintf(c, sizeof (c), 1082 "%s`%s", dt_basename(objname), name); 1083 } 1084 } else if (str != NULL && str[0] != '\0' && str[0] != '@' && 1085 (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL || 1086 (map->pr_mflags & MA_WRITE)))) { 1087 /* 1088 * If the current string pointer in the string table 1089 * does not point to an empty string _and_ the program 1090 * counter falls in a writable region, we'll use the 1091 * string from the string table instead of the raw 1092 * address. This last condition is necessary because 1093 * some (broken) ustack helpers will return a string 1094 * even for a program counter that they can't 1095 * identify. If we have a string for a program 1096 * counter that falls in a segment that isn't 1097 * writable, we assume that we have fallen into this 1098 * case and we refuse to use the string. 1099 */ 1100 (void) snprintf(c, sizeof (c), "%s", str); 1101 } else { 1102 if (P != NULL && Pobjname(P, pc[i], objname, 1103 sizeof (objname)) != NULL) { 1104 (void) snprintf(c, sizeof (c), "%s`0x%llx", 1105 dt_basename(objname), (u_longlong_t)pc[i]); 1106 } else { 1107 (void) snprintf(c, sizeof (c), "0x%llx", 1108 (u_longlong_t)pc[i]); 1109 } 1110 } 1111 1112 if ((err = dt_printf(dtp, fp, format, c)) < 0) 1113 break; 1114 1115 if ((err = dt_printf(dtp, fp, "\n")) < 0) 1116 break; 1117 1118 if (str != NULL && str[0] == '@') { 1119 /* 1120 * If the first character of the string is an "at" sign, 1121 * then the string is inferred to be an annotation -- 1122 * and it is printed out beneath the frame and offset 1123 * with brackets. 1124 */ 1125 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1126 break; 1127 1128 (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]); 1129 1130 if ((err = dt_printf(dtp, fp, format, c)) < 0) 1131 break; 1132 1133 if ((err = dt_printf(dtp, fp, "\n")) < 0) 1134 break; 1135 } 1136 1137 if (str != NULL) { 1138 str += strlen(str) + 1; 1139 if (str - strbase >= strsize) 1140 str = NULL; 1141 } 1142 } 1143 1144 if (P != NULL) { 1145 dt_proc_unlock(dtp, P); 1146 dt_proc_release(dtp, P); 1147 } 1148 1149 return (err); 1150 } 1151 1152 static int 1153 dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act) 1154 { 1155 /* LINTED - alignment */ 1156 uint64_t pid = ((uint64_t *)addr)[0]; 1157 /* LINTED - alignment */ 1158 uint64_t pc = ((uint64_t *)addr)[1]; 1159 const char *format = " %-50s"; 1160 char *s; 1161 int n, len = 256; 1162 1163 if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) { 1164 struct ps_prochandle *P; 1165 1166 if ((P = dt_proc_grab(dtp, pid, 1167 PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) { 1168 GElf_Sym sym; 1169 1170 dt_proc_lock(dtp, P); 1171 1172 if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0) 1173 pc = sym.st_value; 1174 1175 dt_proc_unlock(dtp, P); 1176 dt_proc_release(dtp, P); 1177 } 1178 } 1179 1180 do { 1181 n = len; 1182 s = alloca(n); 1183 } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n); 1184 1185 return (dt_printf(dtp, fp, format, s)); 1186 } 1187 1188 int 1189 dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1190 { 1191 /* LINTED - alignment */ 1192 uint64_t pid = ((uint64_t *)addr)[0]; 1193 /* LINTED - alignment */ 1194 uint64_t pc = ((uint64_t *)addr)[1]; 1195 int err = 0; 1196 1197 char objname[PATH_MAX], c[PATH_MAX * 2]; 1198 struct ps_prochandle *P; 1199 1200 if (format == NULL) 1201 format = " %-50s"; 1202 1203 /* 1204 * See the comment in dt_print_ustack() for the rationale for 1205 * printing raw addresses in the vectored case. 1206 */ 1207 if (dtp->dt_vector == NULL) 1208 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1209 else 1210 P = NULL; 1211 1212 if (P != NULL) 1213 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1214 1215 if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != NULL) { 1216 (void) snprintf(c, sizeof (c), "%s", dt_basename(objname)); 1217 } else { 1218 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1219 } 1220 1221 err = dt_printf(dtp, fp, format, c); 1222 1223 if (P != NULL) { 1224 dt_proc_unlock(dtp, P); 1225 dt_proc_release(dtp, P); 1226 } 1227 1228 return (err); 1229 } 1230 1231 static int 1232 dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1233 { 1234 /* LINTED - alignment */ 1235 uint64_t pc = *((uint64_t *)addr); 1236 dtrace_syminfo_t dts; 1237 GElf_Sym sym; 1238 char c[PATH_MAX * 2]; 1239 1240 if (format == NULL) 1241 format = " %-50s"; 1242 1243 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 1244 (void) snprintf(c, sizeof (c), "%s`%s", 1245 dts.dts_object, dts.dts_name); 1246 } else { 1247 /* 1248 * We'll repeat the lookup, but this time we'll specify a 1249 * NULL GElf_Sym -- indicating that we're only interested in 1250 * the containing module. 1251 */ 1252 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1253 (void) snprintf(c, sizeof (c), "%s`0x%llx", 1254 dts.dts_object, (u_longlong_t)pc); 1255 } else { 1256 (void) snprintf(c, sizeof (c), "0x%llx", 1257 (u_longlong_t)pc); 1258 } 1259 } 1260 1261 if (dt_printf(dtp, fp, format, c) < 0) 1262 return (-1); 1263 1264 return (0); 1265 } 1266 1267 int 1268 dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1269 { 1270 /* LINTED - alignment */ 1271 uint64_t pc = *((uint64_t *)addr); 1272 dtrace_syminfo_t dts; 1273 char c[PATH_MAX * 2]; 1274 1275 if (format == NULL) 1276 format = " %-50s"; 1277 1278 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1279 (void) snprintf(c, sizeof (c), "%s", dts.dts_object); 1280 } else { 1281 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1282 } 1283 1284 if (dt_printf(dtp, fp, format, c) < 0) 1285 return (-1); 1286 1287 return (0); 1288 } 1289 1290 typedef struct dt_normal { 1291 dtrace_aggvarid_t dtnd_id; 1292 uint64_t dtnd_normal; 1293 } dt_normal_t; 1294 1295 static int 1296 dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1297 { 1298 dt_normal_t *normal = arg; 1299 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1300 dtrace_aggvarid_t id = normal->dtnd_id; 1301 1302 if (agg->dtagd_nrecs == 0) 1303 return (DTRACE_AGGWALK_NEXT); 1304 1305 if (agg->dtagd_varid != id) 1306 return (DTRACE_AGGWALK_NEXT); 1307 1308 ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal; 1309 return (DTRACE_AGGWALK_NORMALIZE); 1310 } 1311 1312 static int 1313 dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 1314 { 1315 dt_normal_t normal; 1316 caddr_t addr; 1317 1318 /* 1319 * We (should) have two records: the aggregation ID followed by the 1320 * normalization value. 1321 */ 1322 addr = base + rec->dtrd_offset; 1323 1324 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 1325 return (dt_set_errno(dtp, EDT_BADNORMAL)); 1326 1327 /* LINTED - alignment */ 1328 normal.dtnd_id = *((dtrace_aggvarid_t *)addr); 1329 rec++; 1330 1331 if (rec->dtrd_action != DTRACEACT_LIBACT) 1332 return (dt_set_errno(dtp, EDT_BADNORMAL)); 1333 1334 if (rec->dtrd_arg != DT_ACT_NORMALIZE) 1335 return (dt_set_errno(dtp, EDT_BADNORMAL)); 1336 1337 addr = base + rec->dtrd_offset; 1338 1339 switch (rec->dtrd_size) { 1340 case sizeof (uint64_t): 1341 /* LINTED - alignment */ 1342 normal.dtnd_normal = *((uint64_t *)addr); 1343 break; 1344 case sizeof (uint32_t): 1345 /* LINTED - alignment */ 1346 normal.dtnd_normal = *((uint32_t *)addr); 1347 break; 1348 case sizeof (uint16_t): 1349 /* LINTED - alignment */ 1350 normal.dtnd_normal = *((uint16_t *)addr); 1351 break; 1352 case sizeof (uint8_t): 1353 normal.dtnd_normal = *((uint8_t *)addr); 1354 break; 1355 default: 1356 return (dt_set_errno(dtp, EDT_BADNORMAL)); 1357 } 1358 1359 (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal); 1360 1361 return (0); 1362 } 1363 1364 static int 1365 dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1366 { 1367 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1368 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 1369 1370 if (agg->dtagd_nrecs == 0) 1371 return (DTRACE_AGGWALK_NEXT); 1372 1373 if (agg->dtagd_varid != id) 1374 return (DTRACE_AGGWALK_NEXT); 1375 1376 return (DTRACE_AGGWALK_DENORMALIZE); 1377 } 1378 1379 static int 1380 dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg) 1381 { 1382 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1383 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 1384 1385 if (agg->dtagd_nrecs == 0) 1386 return (DTRACE_AGGWALK_NEXT); 1387 1388 if (agg->dtagd_varid != id) 1389 return (DTRACE_AGGWALK_NEXT); 1390 1391 return (DTRACE_AGGWALK_CLEAR); 1392 } 1393 1394 typedef struct dt_trunc { 1395 dtrace_aggvarid_t dttd_id; 1396 uint64_t dttd_remaining; 1397 } dt_trunc_t; 1398 1399 static int 1400 dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg) 1401 { 1402 dt_trunc_t *trunc = arg; 1403 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1404 dtrace_aggvarid_t id = trunc->dttd_id; 1405 1406 if (agg->dtagd_nrecs == 0) 1407 return (DTRACE_AGGWALK_NEXT); 1408 1409 if (agg->dtagd_varid != id) 1410 return (DTRACE_AGGWALK_NEXT); 1411 1412 if (trunc->dttd_remaining == 0) 1413 return (DTRACE_AGGWALK_REMOVE); 1414 1415 trunc->dttd_remaining--; 1416 return (DTRACE_AGGWALK_NEXT); 1417 } 1418 1419 static int 1420 dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 1421 { 1422 dt_trunc_t trunc; 1423 caddr_t addr; 1424 int64_t remaining; 1425 int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *); 1426 1427 /* 1428 * We (should) have two records: the aggregation ID followed by the 1429 * number of aggregation entries after which the aggregation is to be 1430 * truncated. 1431 */ 1432 addr = base + rec->dtrd_offset; 1433 1434 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 1435 return (dt_set_errno(dtp, EDT_BADTRUNC)); 1436 1437 /* LINTED - alignment */ 1438 trunc.dttd_id = *((dtrace_aggvarid_t *)addr); 1439 rec++; 1440 1441 if (rec->dtrd_action != DTRACEACT_LIBACT) 1442 return (dt_set_errno(dtp, EDT_BADTRUNC)); 1443 1444 if (rec->dtrd_arg != DT_ACT_TRUNC) 1445 return (dt_set_errno(dtp, EDT_BADTRUNC)); 1446 1447 addr = base + rec->dtrd_offset; 1448 1449 switch (rec->dtrd_size) { 1450 case sizeof (uint64_t): 1451 /* LINTED - alignment */ 1452 remaining = *((int64_t *)addr); 1453 break; 1454 case sizeof (uint32_t): 1455 /* LINTED - alignment */ 1456 remaining = *((int32_t *)addr); 1457 break; 1458 case sizeof (uint16_t): 1459 /* LINTED - alignment */ 1460 remaining = *((int16_t *)addr); 1461 break; 1462 case sizeof (uint8_t): 1463 remaining = *((int8_t *)addr); 1464 break; 1465 default: 1466 return (dt_set_errno(dtp, EDT_BADNORMAL)); 1467 } 1468 1469 if (remaining < 0) { 1470 func = dtrace_aggregate_walk_valsorted; 1471 remaining = -remaining; 1472 } else { 1473 func = dtrace_aggregate_walk_valrevsorted; 1474 } 1475 1476 assert(remaining >= 0); 1477 trunc.dttd_remaining = remaining; 1478 1479 (void) func(dtp, dt_trunc_agg, &trunc); 1480 1481 return (0); 1482 } 1483 1484 static int 1485 dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec, 1486 caddr_t addr, size_t size, uint64_t normal) 1487 { 1488 int err; 1489 dtrace_actkind_t act = rec->dtrd_action; 1490 1491 switch (act) { 1492 case DTRACEACT_STACK: 1493 return (dt_print_stack(dtp, fp, NULL, addr, 1494 rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg)); 1495 1496 case DTRACEACT_USTACK: 1497 case DTRACEACT_JSTACK: 1498 return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg)); 1499 1500 case DTRACEACT_USYM: 1501 case DTRACEACT_UADDR: 1502 return (dt_print_usym(dtp, fp, addr, act)); 1503 1504 case DTRACEACT_UMOD: 1505 return (dt_print_umod(dtp, fp, NULL, addr)); 1506 1507 case DTRACEACT_SYM: 1508 return (dt_print_sym(dtp, fp, NULL, addr)); 1509 1510 case DTRACEACT_MOD: 1511 return (dt_print_mod(dtp, fp, NULL, addr)); 1512 1513 case DTRACEAGG_QUANTIZE: 1514 return (dt_print_quantize(dtp, fp, addr, size, normal)); 1515 1516 case DTRACEAGG_LQUANTIZE: 1517 return (dt_print_lquantize(dtp, fp, addr, size, normal)); 1518 1519 case DTRACEAGG_LLQUANTIZE: 1520 return (dt_print_llquantize(dtp, fp, addr, size, normal)); 1521 1522 case DTRACEAGG_AVG: 1523 return (dt_print_average(dtp, fp, addr, size, normal)); 1524 1525 case DTRACEAGG_STDDEV: 1526 return (dt_print_stddev(dtp, fp, addr, size, normal)); 1527 1528 default: 1529 break; 1530 } 1531 1532 switch (size) { 1533 case sizeof (uint64_t): 1534 err = dt_printf(dtp, fp, " %16lld", 1535 /* LINTED - alignment */ 1536 (long long)*((uint64_t *)addr) / normal); 1537 break; 1538 case sizeof (uint32_t): 1539 /* LINTED - alignment */ 1540 err = dt_printf(dtp, fp, " %8d", *((uint32_t *)addr) / 1541 (uint32_t)normal); 1542 break; 1543 case sizeof (uint16_t): 1544 /* LINTED - alignment */ 1545 err = dt_printf(dtp, fp, " %5d", *((uint16_t *)addr) / 1546 (uint32_t)normal); 1547 break; 1548 case sizeof (uint8_t): 1549 err = dt_printf(dtp, fp, " %3d", *((uint8_t *)addr) / 1550 (uint32_t)normal); 1551 break; 1552 default: 1553 err = dt_print_bytes(dtp, fp, addr, size, 50, 0); 1554 break; 1555 } 1556 1557 return (err); 1558 } 1559 1560 int 1561 dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg) 1562 { 1563 int i, aggact = 0; 1564 dt_print_aggdata_t *pd = arg; 1565 const dtrace_aggdata_t *aggdata = aggsdata[0]; 1566 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1567 FILE *fp = pd->dtpa_fp; 1568 dtrace_hdl_t *dtp = pd->dtpa_dtp; 1569 dtrace_recdesc_t *rec; 1570 dtrace_actkind_t act; 1571 caddr_t addr; 1572 size_t size; 1573 1574 /* 1575 * Iterate over each record description in the key, printing the traced 1576 * data, skipping the first datum (the tuple member created by the 1577 * compiler). 1578 */ 1579 for (i = 1; i < agg->dtagd_nrecs; i++) { 1580 rec = &agg->dtagd_rec[i]; 1581 act = rec->dtrd_action; 1582 addr = aggdata->dtada_data + rec->dtrd_offset; 1583 size = rec->dtrd_size; 1584 1585 if (DTRACEACT_ISAGG(act)) { 1586 aggact = i; 1587 break; 1588 } 1589 1590 if (dt_print_datum(dtp, fp, rec, addr, size, 1) < 0) 1591 return (-1); 1592 1593 if (dt_buffered_flush(dtp, NULL, rec, aggdata, 1594 DTRACE_BUFDATA_AGGKEY) < 0) 1595 return (-1); 1596 } 1597 1598 assert(aggact != 0); 1599 1600 for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) { 1601 uint64_t normal; 1602 1603 aggdata = aggsdata[i]; 1604 agg = aggdata->dtada_desc; 1605 rec = &agg->dtagd_rec[aggact]; 1606 act = rec->dtrd_action; 1607 addr = aggdata->dtada_data + rec->dtrd_offset; 1608 size = rec->dtrd_size; 1609 1610 assert(DTRACEACT_ISAGG(act)); 1611 normal = aggdata->dtada_normal; 1612 1613 if (dt_print_datum(dtp, fp, rec, addr, size, normal) < 0) 1614 return (-1); 1615 1616 if (dt_buffered_flush(dtp, NULL, rec, aggdata, 1617 DTRACE_BUFDATA_AGGVAL) < 0) 1618 return (-1); 1619 1620 if (!pd->dtpa_allunprint) 1621 agg->dtagd_flags |= DTRACE_AGD_PRINTED; 1622 } 1623 1624 if (dt_printf(dtp, fp, "\n") < 0) 1625 return (-1); 1626 1627 if (dt_buffered_flush(dtp, NULL, NULL, aggdata, 1628 DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0) 1629 return (-1); 1630 1631 return (0); 1632 } 1633 1634 int 1635 dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg) 1636 { 1637 dt_print_aggdata_t *pd = arg; 1638 dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1639 dtrace_aggvarid_t aggvarid = pd->dtpa_id; 1640 1641 if (pd->dtpa_allunprint) { 1642 if (agg->dtagd_flags & DTRACE_AGD_PRINTED) 1643 return (0); 1644 } else { 1645 /* 1646 * If we're not printing all unprinted aggregations, then the 1647 * aggregation variable ID denotes a specific aggregation 1648 * variable that we should print -- skip any other aggregations 1649 * that we encounter. 1650 */ 1651 if (agg->dtagd_nrecs == 0) 1652 return (0); 1653 1654 if (aggvarid != agg->dtagd_varid) 1655 return (0); 1656 } 1657 1658 return (dt_print_aggs(&aggdata, 1, arg)); 1659 } 1660 1661 int 1662 dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data, 1663 const char *option, const char *value) 1664 { 1665 int len, rval; 1666 char *msg; 1667 const char *errstr; 1668 dtrace_setoptdata_t optdata; 1669 1670 bzero(&optdata, sizeof (optdata)); 1671 (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval); 1672 1673 if (dtrace_setopt(dtp, option, value) == 0) { 1674 (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval); 1675 optdata.dtsda_probe = data; 1676 optdata.dtsda_option = option; 1677 optdata.dtsda_handle = dtp; 1678 1679 if ((rval = dt_handle_setopt(dtp, &optdata)) != 0) 1680 return (rval); 1681 1682 return (0); 1683 } 1684 1685 errstr = dtrace_errmsg(dtp, dtrace_errno(dtp)); 1686 len = strlen(option) + strlen(value) + strlen(errstr) + 80; 1687 msg = alloca(len); 1688 1689 (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n", 1690 option, value, errstr); 1691 1692 if ((rval = dt_handle_liberr(dtp, data, msg)) == 0) 1693 return (0); 1694 1695 return (rval); 1696 } 1697 1698 static int 1699 dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu, dtrace_bufdesc_t *buf, 1700 dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg) 1701 { 1702 dtrace_epid_t id; 1703 size_t offs, start = buf->dtbd_oldest, end = buf->dtbd_size; 1704 int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET); 1705 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 1706 int rval, i, n; 1707 dtrace_epid_t last = DTRACE_EPIDNONE; 1708 dtrace_probedata_t data; 1709 uint64_t drops; 1710 caddr_t addr; 1711 1712 bzero(&data, sizeof (data)); 1713 data.dtpda_handle = dtp; 1714 data.dtpda_cpu = cpu; 1715 1716 again: 1717 for (offs = start; offs < end; ) { 1718 dtrace_eprobedesc_t *epd; 1719 1720 /* 1721 * We're guaranteed to have an ID. 1722 */ 1723 id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 1724 1725 if (id == DTRACE_EPIDNONE) { 1726 /* 1727 * This is filler to assure proper alignment of the 1728 * next record; we simply ignore it. 1729 */ 1730 offs += sizeof (id); 1731 continue; 1732 } 1733 1734 if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc, 1735 &data.dtpda_pdesc)) != 0) 1736 return (rval); 1737 1738 epd = data.dtpda_edesc; 1739 data.dtpda_data = buf->dtbd_data + offs; 1740 1741 if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) { 1742 rval = dt_handle(dtp, &data); 1743 1744 if (rval == DTRACE_CONSUME_NEXT) 1745 goto nextepid; 1746 1747 if (rval == DTRACE_CONSUME_ERROR) 1748 return (-1); 1749 } 1750 1751 if (flow) 1752 (void) dt_flowindent(dtp, &data, last, buf, offs); 1753 1754 rval = (*efunc)(&data, arg); 1755 1756 if (flow) { 1757 if (data.dtpda_flow == DTRACEFLOW_ENTRY) 1758 data.dtpda_indent += 2; 1759 } 1760 1761 if (rval == DTRACE_CONSUME_NEXT) 1762 goto nextepid; 1763 1764 if (rval == DTRACE_CONSUME_ABORT) 1765 return (dt_set_errno(dtp, EDT_DIRABORT)); 1766 1767 if (rval != DTRACE_CONSUME_THIS) 1768 return (dt_set_errno(dtp, EDT_BADRVAL)); 1769 1770 for (i = 0; i < epd->dtepd_nrecs; i++) { 1771 dtrace_recdesc_t *rec = &epd->dtepd_rec[i]; 1772 dtrace_actkind_t act = rec->dtrd_action; 1773 1774 data.dtpda_data = buf->dtbd_data + offs + 1775 rec->dtrd_offset; 1776 addr = data.dtpda_data; 1777 1778 if (act == DTRACEACT_LIBACT) { 1779 uint64_t arg = rec->dtrd_arg; 1780 dtrace_aggvarid_t id; 1781 1782 switch (arg) { 1783 case DT_ACT_CLEAR: 1784 /* LINTED - alignment */ 1785 id = *((dtrace_aggvarid_t *)addr); 1786 (void) dtrace_aggregate_walk(dtp, 1787 dt_clear_agg, &id); 1788 continue; 1789 1790 case DT_ACT_DENORMALIZE: 1791 /* LINTED - alignment */ 1792 id = *((dtrace_aggvarid_t *)addr); 1793 (void) dtrace_aggregate_walk(dtp, 1794 dt_denormalize_agg, &id); 1795 continue; 1796 1797 case DT_ACT_FTRUNCATE: 1798 if (fp == NULL) 1799 continue; 1800 1801 (void) fflush(fp); 1802 (void) ftruncate(fileno(fp), 0); 1803 (void) fseeko(fp, 0, SEEK_SET); 1804 continue; 1805 1806 case DT_ACT_NORMALIZE: 1807 if (i == epd->dtepd_nrecs - 1) 1808 return (dt_set_errno(dtp, 1809 EDT_BADNORMAL)); 1810 1811 if (dt_normalize(dtp, 1812 buf->dtbd_data + offs, rec) != 0) 1813 return (-1); 1814 1815 i++; 1816 continue; 1817 1818 case DT_ACT_SETOPT: { 1819 uint64_t *opts = dtp->dt_options; 1820 dtrace_recdesc_t *valrec; 1821 uint32_t valsize; 1822 caddr_t val; 1823 int rv; 1824 1825 if (i == epd->dtepd_nrecs - 1) { 1826 return (dt_set_errno(dtp, 1827 EDT_BADSETOPT)); 1828 } 1829 1830 valrec = &epd->dtepd_rec[++i]; 1831 valsize = valrec->dtrd_size; 1832 1833 if (valrec->dtrd_action != act || 1834 valrec->dtrd_arg != arg) { 1835 return (dt_set_errno(dtp, 1836 EDT_BADSETOPT)); 1837 } 1838 1839 if (valsize > sizeof (uint64_t)) { 1840 val = buf->dtbd_data + offs + 1841 valrec->dtrd_offset; 1842 } else { 1843 val = "1"; 1844 } 1845 1846 rv = dt_setopt(dtp, &data, addr, val); 1847 1848 if (rv != 0) 1849 return (-1); 1850 1851 flow = (opts[DTRACEOPT_FLOWINDENT] != 1852 DTRACEOPT_UNSET); 1853 quiet = (opts[DTRACEOPT_QUIET] != 1854 DTRACEOPT_UNSET); 1855 1856 continue; 1857 } 1858 1859 case DT_ACT_TRUNC: 1860 if (i == epd->dtepd_nrecs - 1) 1861 return (dt_set_errno(dtp, 1862 EDT_BADTRUNC)); 1863 1864 if (dt_trunc(dtp, 1865 buf->dtbd_data + offs, rec) != 0) 1866 return (-1); 1867 1868 i++; 1869 continue; 1870 1871 default: 1872 continue; 1873 } 1874 } 1875 1876 rval = (*rfunc)(&data, rec, arg); 1877 1878 if (rval == DTRACE_CONSUME_NEXT) 1879 continue; 1880 1881 if (rval == DTRACE_CONSUME_ABORT) 1882 return (dt_set_errno(dtp, EDT_DIRABORT)); 1883 1884 if (rval != DTRACE_CONSUME_THIS) 1885 return (dt_set_errno(dtp, EDT_BADRVAL)); 1886 1887 if (act == DTRACEACT_STACK) { 1888 int depth = rec->dtrd_arg; 1889 1890 if (dt_print_stack(dtp, fp, NULL, addr, depth, 1891 rec->dtrd_size / depth) < 0) 1892 return (-1); 1893 goto nextrec; 1894 } 1895 1896 if (act == DTRACEACT_USTACK || 1897 act == DTRACEACT_JSTACK) { 1898 if (dt_print_ustack(dtp, fp, NULL, 1899 addr, rec->dtrd_arg) < 0) 1900 return (-1); 1901 goto nextrec; 1902 } 1903 1904 if (act == DTRACEACT_SYM) { 1905 if (dt_print_sym(dtp, fp, NULL, addr) < 0) 1906 return (-1); 1907 goto nextrec; 1908 } 1909 1910 if (act == DTRACEACT_MOD) { 1911 if (dt_print_mod(dtp, fp, NULL, addr) < 0) 1912 return (-1); 1913 goto nextrec; 1914 } 1915 1916 if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) { 1917 if (dt_print_usym(dtp, fp, addr, act) < 0) 1918 return (-1); 1919 goto nextrec; 1920 } 1921 1922 if (act == DTRACEACT_UMOD) { 1923 if (dt_print_umod(dtp, fp, NULL, addr) < 0) 1924 return (-1); 1925 goto nextrec; 1926 } 1927 1928 if (DTRACEACT_ISPRINTFLIKE(act)) { 1929 void *fmtdata; 1930 int (*func)(dtrace_hdl_t *, FILE *, void *, 1931 const dtrace_probedata_t *, 1932 const dtrace_recdesc_t *, uint_t, 1933 const void *buf, size_t); 1934 1935 if ((fmtdata = dt_format_lookup(dtp, 1936 rec->dtrd_format)) == NULL) 1937 goto nofmt; 1938 1939 switch (act) { 1940 case DTRACEACT_PRINTF: 1941 func = dtrace_fprintf; 1942 break; 1943 case DTRACEACT_PRINTA: 1944 func = dtrace_fprinta; 1945 break; 1946 case DTRACEACT_SYSTEM: 1947 func = dtrace_system; 1948 break; 1949 case DTRACEACT_FREOPEN: 1950 func = dtrace_freopen; 1951 break; 1952 } 1953 1954 n = (*func)(dtp, fp, fmtdata, &data, 1955 rec, epd->dtepd_nrecs - i, 1956 (uchar_t *)buf->dtbd_data + offs, 1957 buf->dtbd_size - offs); 1958 1959 if (n < 0) 1960 return (-1); /* errno is set for us */ 1961 1962 if (n > 0) 1963 i += n - 1; 1964 goto nextrec; 1965 } 1966 1967 nofmt: 1968 if (act == DTRACEACT_PRINTA) { 1969 dt_print_aggdata_t pd; 1970 dtrace_aggvarid_t *aggvars; 1971 int j, naggvars = 0; 1972 size_t size = ((epd->dtepd_nrecs - i) * 1973 sizeof (dtrace_aggvarid_t)); 1974 1975 if ((aggvars = dt_alloc(dtp, size)) == NULL) 1976 return (-1); 1977 1978 /* 1979 * This might be a printa() with multiple 1980 * aggregation variables. We need to scan 1981 * forward through the records until we find 1982 * a record from a different statement. 1983 */ 1984 for (j = i; j < epd->dtepd_nrecs; j++) { 1985 dtrace_recdesc_t *nrec; 1986 caddr_t naddr; 1987 1988 nrec = &epd->dtepd_rec[j]; 1989 1990 if (nrec->dtrd_uarg != rec->dtrd_uarg) 1991 break; 1992 1993 if (nrec->dtrd_action != act) { 1994 return (dt_set_errno(dtp, 1995 EDT_BADAGG)); 1996 } 1997 1998 naddr = buf->dtbd_data + offs + 1999 nrec->dtrd_offset; 2000 2001 aggvars[naggvars++] = 2002 /* LINTED - alignment */ 2003 *((dtrace_aggvarid_t *)naddr); 2004 } 2005 2006 i = j - 1; 2007 bzero(&pd, sizeof (pd)); 2008 pd.dtpa_dtp = dtp; 2009 pd.dtpa_fp = fp; 2010 2011 assert(naggvars >= 1); 2012 2013 if (naggvars == 1) { 2014 pd.dtpa_id = aggvars[0]; 2015 dt_free(dtp, aggvars); 2016 2017 if (dt_printf(dtp, fp, "\n") < 0 || 2018 dtrace_aggregate_walk_sorted(dtp, 2019 dt_print_agg, &pd) < 0) 2020 return (-1); 2021 goto nextrec; 2022 } 2023 2024 if (dt_printf(dtp, fp, "\n") < 0 || 2025 dtrace_aggregate_walk_joined(dtp, aggvars, 2026 naggvars, dt_print_aggs, &pd) < 0) { 2027 dt_free(dtp, aggvars); 2028 return (-1); 2029 } 2030 2031 dt_free(dtp, aggvars); 2032 goto nextrec; 2033 } 2034 2035 switch (rec->dtrd_size) { 2036 case sizeof (uint64_t): 2037 n = dt_printf(dtp, fp, 2038 quiet ? "%lld" : " %16lld", 2039 /* LINTED - alignment */ 2040 *((unsigned long long *)addr)); 2041 break; 2042 case sizeof (uint32_t): 2043 n = dt_printf(dtp, fp, quiet ? "%d" : " %8d", 2044 /* LINTED - alignment */ 2045 *((uint32_t *)addr)); 2046 break; 2047 case sizeof (uint16_t): 2048 n = dt_printf(dtp, fp, quiet ? "%d" : " %5d", 2049 /* LINTED - alignment */ 2050 *((uint16_t *)addr)); 2051 break; 2052 case sizeof (uint8_t): 2053 n = dt_printf(dtp, fp, quiet ? "%d" : " %3d", 2054 *((uint8_t *)addr)); 2055 break; 2056 default: 2057 n = dt_print_bytes(dtp, fp, addr, 2058 rec->dtrd_size, 33, quiet); 2059 break; 2060 } 2061 2062 if (n < 0) 2063 return (-1); /* errno is set for us */ 2064 2065 nextrec: 2066 if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0) 2067 return (-1); /* errno is set for us */ 2068 } 2069 2070 /* 2071 * Call the record callback with a NULL record to indicate 2072 * that we're done processing this EPID. 2073 */ 2074 rval = (*rfunc)(&data, NULL, arg); 2075 nextepid: 2076 offs += epd->dtepd_size; 2077 last = id; 2078 } 2079 2080 if (buf->dtbd_oldest != 0 && start == buf->dtbd_oldest) { 2081 end = buf->dtbd_oldest; 2082 start = 0; 2083 goto again; 2084 } 2085 2086 if ((drops = buf->dtbd_drops) == 0) 2087 return (0); 2088 2089 /* 2090 * Explicitly zero the drops to prevent us from processing them again. 2091 */ 2092 buf->dtbd_drops = 0; 2093 2094 return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops)); 2095 } 2096 2097 typedef struct dt_begin { 2098 dtrace_consume_probe_f *dtbgn_probefunc; 2099 dtrace_consume_rec_f *dtbgn_recfunc; 2100 void *dtbgn_arg; 2101 dtrace_handle_err_f *dtbgn_errhdlr; 2102 void *dtbgn_errarg; 2103 int dtbgn_beginonly; 2104 } dt_begin_t; 2105 2106 static int 2107 dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg) 2108 { 2109 dt_begin_t *begin = (dt_begin_t *)arg; 2110 dtrace_probedesc_t *pd = data->dtpda_pdesc; 2111 2112 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 2113 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 2114 2115 if (begin->dtbgn_beginonly) { 2116 if (!(r1 && r2)) 2117 return (DTRACE_CONSUME_NEXT); 2118 } else { 2119 if (r1 && r2) 2120 return (DTRACE_CONSUME_NEXT); 2121 } 2122 2123 /* 2124 * We have a record that we're interested in. Now call the underlying 2125 * probe function... 2126 */ 2127 return (begin->dtbgn_probefunc(data, begin->dtbgn_arg)); 2128 } 2129 2130 static int 2131 dt_consume_begin_record(const dtrace_probedata_t *data, 2132 const dtrace_recdesc_t *rec, void *arg) 2133 { 2134 dt_begin_t *begin = (dt_begin_t *)arg; 2135 2136 return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg)); 2137 } 2138 2139 static int 2140 dt_consume_begin_error(const dtrace_errdata_t *data, void *arg) 2141 { 2142 dt_begin_t *begin = (dt_begin_t *)arg; 2143 dtrace_probedesc_t *pd = data->dteda_pdesc; 2144 2145 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 2146 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 2147 2148 if (begin->dtbgn_beginonly) { 2149 if (!(r1 && r2)) 2150 return (DTRACE_HANDLE_OK); 2151 } else { 2152 if (r1 && r2) 2153 return (DTRACE_HANDLE_OK); 2154 } 2155 2156 return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg)); 2157 } 2158 2159 static int 2160 dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp, dtrace_bufdesc_t *buf, 2161 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 2162 { 2163 /* 2164 * There's this idea that the BEGIN probe should be processed before 2165 * everything else, and that the END probe should be processed after 2166 * anything else. In the common case, this is pretty easy to deal 2167 * with. However, a situation may arise where the BEGIN enabling and 2168 * END enabling are on the same CPU, and some enabling in the middle 2169 * occurred on a different CPU. To deal with this (blech!) we need to 2170 * consume the BEGIN buffer up until the end of the BEGIN probe, and 2171 * then set it aside. We will then process every other CPU, and then 2172 * we'll return to the BEGIN CPU and process the rest of the data 2173 * (which will inevitably include the END probe, if any). Making this 2174 * even more complicated (!) is the library's ERROR enabling. Because 2175 * this enabling is processed before we even get into the consume call 2176 * back, any ERROR firing would result in the library's ERROR enabling 2177 * being processed twice -- once in our first pass (for BEGIN probes), 2178 * and again in our second pass (for everything but BEGIN probes). To 2179 * deal with this, we interpose on the ERROR handler to assure that we 2180 * only process ERROR enablings induced by BEGIN enablings in the 2181 * first pass, and that we only process ERROR enablings _not_ induced 2182 * by BEGIN enablings in the second pass. 2183 */ 2184 dt_begin_t begin; 2185 processorid_t cpu = dtp->dt_beganon; 2186 dtrace_bufdesc_t nbuf; 2187 int rval, i; 2188 static int max_ncpus; 2189 dtrace_optval_t size; 2190 2191 dtp->dt_beganon = -1; 2192 2193 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2194 /* 2195 * We really don't expect this to fail, but it is at least 2196 * technically possible for this to fail with ENOENT. In this 2197 * case, we just drive on... 2198 */ 2199 if (errno == ENOENT) 2200 return (0); 2201 2202 return (dt_set_errno(dtp, errno)); 2203 } 2204 2205 if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) { 2206 /* 2207 * This is the simple case. We're either not stopped, or if 2208 * we are, we actually processed any END probes on another 2209 * CPU. We can simply consume this buffer and return. 2210 */ 2211 return (dt_consume_cpu(dtp, fp, cpu, buf, pf, rf, arg)); 2212 } 2213 2214 begin.dtbgn_probefunc = pf; 2215 begin.dtbgn_recfunc = rf; 2216 begin.dtbgn_arg = arg; 2217 begin.dtbgn_beginonly = 1; 2218 2219 /* 2220 * We need to interpose on the ERROR handler to be sure that we 2221 * only process ERRORs induced by BEGIN. 2222 */ 2223 begin.dtbgn_errhdlr = dtp->dt_errhdlr; 2224 begin.dtbgn_errarg = dtp->dt_errarg; 2225 dtp->dt_errhdlr = dt_consume_begin_error; 2226 dtp->dt_errarg = &begin; 2227 2228 rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe, 2229 dt_consume_begin_record, &begin); 2230 2231 dtp->dt_errhdlr = begin.dtbgn_errhdlr; 2232 dtp->dt_errarg = begin.dtbgn_errarg; 2233 2234 if (rval != 0) 2235 return (rval); 2236 2237 /* 2238 * Now allocate a new buffer. We'll use this to deal with every other 2239 * CPU. 2240 */ 2241 bzero(&nbuf, sizeof (dtrace_bufdesc_t)); 2242 (void) dtrace_getopt(dtp, "bufsize", &size); 2243 if ((nbuf.dtbd_data = malloc(size)) == NULL) 2244 return (dt_set_errno(dtp, EDT_NOMEM)); 2245 2246 if (max_ncpus == 0) 2247 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 2248 2249 for (i = 0; i < max_ncpus; i++) { 2250 nbuf.dtbd_cpu = i; 2251 2252 if (i == cpu) 2253 continue; 2254 2255 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &nbuf) == -1) { 2256 /* 2257 * If we failed with ENOENT, it may be because the 2258 * CPU was unconfigured -- this is okay. Any other 2259 * error, however, is unexpected. 2260 */ 2261 if (errno == ENOENT) 2262 continue; 2263 2264 free(nbuf.dtbd_data); 2265 2266 return (dt_set_errno(dtp, errno)); 2267 } 2268 2269 if ((rval = dt_consume_cpu(dtp, fp, 2270 i, &nbuf, pf, rf, arg)) != 0) { 2271 free(nbuf.dtbd_data); 2272 return (rval); 2273 } 2274 } 2275 2276 free(nbuf.dtbd_data); 2277 2278 /* 2279 * Okay -- we're done with the other buffers. Now we want to 2280 * reconsume the first buffer -- but this time we're looking for 2281 * everything _but_ BEGIN. And of course, in order to only consume 2282 * those ERRORs _not_ associated with BEGIN, we need to reinstall our 2283 * ERROR interposition function... 2284 */ 2285 begin.dtbgn_beginonly = 0; 2286 2287 assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr); 2288 assert(begin.dtbgn_errarg == dtp->dt_errarg); 2289 dtp->dt_errhdlr = dt_consume_begin_error; 2290 dtp->dt_errarg = &begin; 2291 2292 rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe, 2293 dt_consume_begin_record, &begin); 2294 2295 dtp->dt_errhdlr = begin.dtbgn_errhdlr; 2296 dtp->dt_errarg = begin.dtbgn_errarg; 2297 2298 return (rval); 2299 } 2300 2301 int 2302 dtrace_consume(dtrace_hdl_t *dtp, FILE *fp, 2303 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 2304 { 2305 dtrace_bufdesc_t *buf = &dtp->dt_buf; 2306 dtrace_optval_t size; 2307 static int max_ncpus; 2308 int i, rval; 2309 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE]; 2310 hrtime_t now = gethrtime(); 2311 2312 if (dtp->dt_lastswitch != 0) { 2313 if (now - dtp->dt_lastswitch < interval) 2314 return (0); 2315 2316 dtp->dt_lastswitch += interval; 2317 } else { 2318 dtp->dt_lastswitch = now; 2319 } 2320 2321 if (!dtp->dt_active) 2322 return (dt_set_errno(dtp, EINVAL)); 2323 2324 if (max_ncpus == 0) 2325 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 2326 2327 if (pf == NULL) 2328 pf = (dtrace_consume_probe_f *)dt_nullprobe; 2329 2330 if (rf == NULL) 2331 rf = (dtrace_consume_rec_f *)dt_nullrec; 2332 2333 if (buf->dtbd_data == NULL) { 2334 (void) dtrace_getopt(dtp, "bufsize", &size); 2335 if ((buf->dtbd_data = malloc(size)) == NULL) 2336 return (dt_set_errno(dtp, EDT_NOMEM)); 2337 2338 buf->dtbd_size = size; 2339 } 2340 2341 /* 2342 * If we have just begun, we want to first process the CPU that 2343 * executed the BEGIN probe (if any). 2344 */ 2345 if (dtp->dt_active && dtp->dt_beganon != -1) { 2346 buf->dtbd_cpu = dtp->dt_beganon; 2347 if ((rval = dt_consume_begin(dtp, fp, buf, pf, rf, arg)) != 0) 2348 return (rval); 2349 } 2350 2351 for (i = 0; i < max_ncpus; i++) { 2352 buf->dtbd_cpu = i; 2353 2354 /* 2355 * If we have stopped, we want to process the CPU on which the 2356 * END probe was processed only _after_ we have processed 2357 * everything else. 2358 */ 2359 if (dtp->dt_stopped && (i == dtp->dt_endedon)) 2360 continue; 2361 2362 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2363 /* 2364 * If we failed with ENOENT, it may be because the 2365 * CPU was unconfigured -- this is okay. Any other 2366 * error, however, is unexpected. 2367 */ 2368 if (errno == ENOENT) 2369 continue; 2370 2371 return (dt_set_errno(dtp, errno)); 2372 } 2373 2374 if ((rval = dt_consume_cpu(dtp, fp, i, buf, pf, rf, arg)) != 0) 2375 return (rval); 2376 } 2377 2378 if (!dtp->dt_stopped) 2379 return (0); 2380 2381 buf->dtbd_cpu = dtp->dt_endedon; 2382 2383 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2384 /* 2385 * This _really_ shouldn't fail, but it is strictly speaking 2386 * possible for this to return ENOENT if the CPU that called 2387 * the END enabling somehow managed to become unconfigured. 2388 * It's unclear how the user can possibly expect anything 2389 * rational to happen in this case -- the state has been thrown 2390 * out along with the unconfigured CPU -- so we'll just drive 2391 * on... 2392 */ 2393 if (errno == ENOENT) 2394 return (0); 2395 2396 return (dt_set_errno(dtp, errno)); 2397 } 2398 2399 return (dt_consume_cpu(dtp, fp, dtp->dt_endedon, buf, pf, rf, arg)); 2400 } 2401