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