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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
42 #include <libproc_compat.h>
45 #define DT_MASK_LO 0x00000000FFFFFFFFULL
48 * We declare this here because (1) we need it and (2) we want to avoid a
49 * dependency on libm in libdtrace.
52 dt_fabsl(long double x)
61 * 128-bit arithmetic functions needed to support the stddev() aggregating
65 dt_gt_128(uint64_t *a, uint64_t *b)
67 return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
71 dt_ge_128(uint64_t *a, uint64_t *b)
73 return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
77 dt_le_128(uint64_t *a, uint64_t *b)
79 return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
83 * Shift the 128-bit value in a by b. If b is positive, shift left.
84 * If b is negative, shift right.
87 dt_shift_128(uint64_t *a, int b)
97 a[0] = a[1] >> (b - 64);
101 mask = 1LL << (64 - b);
103 a[0] |= ((a[1] & mask) << (64 - b));
108 a[1] = a[0] << (b - 64);
112 mask = a[0] >> (64 - b);
120 dt_nbits_128(uint64_t *a)
124 uint64_t zero[2] = { 0, 0 };
129 dt_shift_128(tmp, -1);
130 while (dt_gt_128(tmp, zero)) {
131 dt_shift_128(tmp, -1);
139 dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
143 result[0] = minuend[0] - subtrahend[0];
144 result[1] = minuend[1] - subtrahend[1] -
145 (minuend[0] < subtrahend[0] ? 1 : 0);
147 difference[0] = result[0];
148 difference[1] = result[1];
152 dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
156 result[0] = addend1[0] + addend2[0];
157 result[1] = addend1[1] + addend2[1] +
158 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
165 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
166 * use native multiplication on those, and then re-combine into the
167 * resulting 128-bit value.
169 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
176 dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
178 uint64_t hi1, hi2, lo1, lo2;
184 lo1 = factor1 & DT_MASK_LO;
185 lo2 = factor2 & DT_MASK_LO;
187 product[0] = lo1 * lo2;
188 product[1] = hi1 * hi2;
192 dt_shift_128(tmp, 32);
193 dt_add_128(product, tmp, product);
197 dt_shift_128(tmp, 32);
198 dt_add_128(product, tmp, product);
202 * This is long-hand division.
204 * We initialize subtrahend by shifting divisor left as far as possible. We
205 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we
206 * subtract and set the appropriate bit in the result. We then shift
207 * subtrahend right by one bit for the next comparison.
210 dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
212 uint64_t result[2] = { 0, 0 };
213 uint64_t remainder[2];
214 uint64_t subtrahend[2];
215 uint64_t divisor_128[2];
216 uint64_t mask[2] = { 1, 0 };
219 assert(divisor != 0);
221 divisor_128[0] = divisor;
224 remainder[0] = dividend[0];
225 remainder[1] = dividend[1];
227 subtrahend[0] = divisor;
230 while (divisor > 0) {
235 dt_shift_128(subtrahend, 128 - log);
236 dt_shift_128(mask, 128 - log);
238 while (dt_ge_128(remainder, divisor_128)) {
239 if (dt_ge_128(remainder, subtrahend)) {
240 dt_subtract_128(remainder, subtrahend, remainder);
241 result[0] |= mask[0];
242 result[1] |= mask[1];
245 dt_shift_128(subtrahend, -1);
246 dt_shift_128(mask, -1);
249 quotient[0] = result[0];
250 quotient[1] = result[1];
254 * This is the long-hand method of calculating a square root.
255 * The algorithm is as follows:
257 * 1. Group the digits by 2 from the right.
258 * 2. Over the leftmost group, find the largest single-digit number
259 * whose square is less than that group.
260 * 3. Subtract the result of the previous step (2 or 4, depending) and
261 * bring down the next two-digit group.
262 * 4. For the result R we have so far, find the largest single-digit number
263 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
264 * (Note that this is doubling R and performing a decimal left-shift by 1
265 * and searching for the appropriate decimal to fill the one's place.)
266 * The value x is the next digit in the square root.
267 * Repeat steps 3 and 4 until the desired precision is reached. (We're
268 * dealing with integers, so the above is sufficient.)
270 * In decimal, the square root of 582,734 would be calculated as so:
274 * -49 (7^2 == 49 => 7 is the first digit in the square root)
276 * 9 27 (Subtract and bring down the next group.)
277 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
278 * ----- the square root)
279 * 51 34 (Subtract and bring down the next group.)
280 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
281 * ----- the square root)
284 * The above algorithm applies similarly in binary, but note that the
285 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
286 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
287 * preceding difference?
289 * In binary, the square root of 11011011 would be calculated as so:
293 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1)
296 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1)
299 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
302 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
306 dt_sqrt_128(uint64_t *square)
308 uint64_t result[2] = { 0, 0 };
309 uint64_t diff[2] = { 0, 0 };
310 uint64_t one[2] = { 1, 0 };
311 uint64_t next_pair[2];
312 uint64_t next_try[2];
313 uint64_t bit_pairs, pair_shift;
316 bit_pairs = dt_nbits_128(square) / 2;
317 pair_shift = bit_pairs * 2;
319 for (i = 0; i <= bit_pairs; i++) {
321 * Bring down the next pair of bits.
323 next_pair[0] = square[0];
324 next_pair[1] = square[1];
325 dt_shift_128(next_pair, -pair_shift);
329 dt_shift_128(diff, 2);
330 dt_add_128(diff, next_pair, diff);
333 * next_try = R << 2 + 1
335 next_try[0] = result[0];
336 next_try[1] = result[1];
337 dt_shift_128(next_try, 2);
338 dt_add_128(next_try, one, next_try);
340 if (dt_le_128(next_try, diff)) {
341 dt_subtract_128(diff, next_try, diff);
342 dt_shift_128(result, 1);
343 dt_add_128(result, one, result);
345 dt_shift_128(result, 1);
351 assert(result[1] == 0);
357 dt_stddev(uint64_t *data, uint64_t normal)
359 uint64_t avg_of_squares[2];
360 uint64_t square_of_avg[2];
365 * The standard approximation for standard deviation is
366 * sqrt(average(x**2) - average(x)**2), i.e. the square root
367 * of the average of the squares minus the square of the average.
369 dt_divide_128(data + 2, normal, avg_of_squares);
370 dt_divide_128(avg_of_squares, data[0], avg_of_squares);
372 norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
375 norm_avg = -norm_avg;
377 dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
379 dt_subtract_128(avg_of_squares, square_of_avg, diff);
381 return (dt_sqrt_128(diff));
385 dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
386 dtrace_bufdesc_t *buf, size_t offs)
388 dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
389 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
390 char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
391 dtrace_flowkind_t flow = DTRACEFLOW_NONE;
392 const char *str = NULL;
393 static const char *e_str[2] = { " -> ", " => " };
394 static const char *r_str[2] = { " <- ", " <= " };
395 static const char *ent = "entry", *ret = "return";
396 static int entlen = 0, retlen = 0;
397 dtrace_epid_t next, id = epd->dtepd_epid;
402 entlen = strlen(ent);
403 retlen = strlen(ret);
407 * If the name of the probe is "entry" or ends with "-entry", we
408 * treat it as an entry; if it is "return" or ends with "-return",
409 * we treat it as a return. (This allows application-provided probes
410 * like "method-entry" or "function-entry" to participate in flow
411 * indentation -- without accidentally misinterpreting popular probe
412 * names like "carpentry", "gentry" or "Coventry".)
414 if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
415 (sub == n || sub[-1] == '-')) {
416 flow = DTRACEFLOW_ENTRY;
417 str = e_str[strcmp(p, "syscall") == 0];
418 } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
419 (sub == n || sub[-1] == '-')) {
420 flow = DTRACEFLOW_RETURN;
421 str = r_str[strcmp(p, "syscall") == 0];
425 * If we're going to indent this, we need to check the ID of our last
426 * call. If we're looking at the same probe ID but a different EPID,
427 * we _don't_ want to indent. (Yes, there are some minor holes in
428 * this scheme -- it's a heuristic.)
430 if (flow == DTRACEFLOW_ENTRY) {
431 if ((last != DTRACE_EPIDNONE && id != last &&
432 pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
433 flow = DTRACEFLOW_NONE;
437 * If we're going to unindent this, it's more difficult to see if
438 * we don't actually want to unindent it -- we need to look at the
441 if (flow == DTRACEFLOW_RETURN) {
442 offs += epd->dtepd_size;
445 if (offs >= buf->dtbd_size) {
447 * We're at the end -- maybe. If the oldest
448 * record is non-zero, we need to wrap.
450 if (buf->dtbd_oldest != 0) {
457 next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
459 if (next == DTRACE_EPIDNONE)
461 } while (next == DTRACE_EPIDNONE);
463 if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
466 if (next != id && npd->dtpd_id == pd->dtpd_id)
467 flow = DTRACEFLOW_NONE;
471 if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
472 data->dtpda_prefix = str;
474 data->dtpda_prefix = "| ";
477 if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
478 data->dtpda_indent -= 2;
480 data->dtpda_flow = flow;
488 return (DTRACE_CONSUME_THIS);
494 return (DTRACE_CONSUME_NEXT);
498 dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
499 uint64_t normal, long double total, char positives, char negatives)
502 uint_t depth, len = 40;
504 const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
505 const char *spaces = " ";
507 assert(strlen(ats) == len && strlen(spaces) == len);
508 assert(!(total == 0 && (positives || negatives)));
509 assert(!(val < 0 && !negatives));
510 assert(!(val > 0 && !positives));
511 assert(!(val != 0 && total == 0));
515 f = (dt_fabsl((long double)val) * len) / total;
516 depth = (uint_t)(f + 0.5);
521 return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
522 spaces + depth, (long long)val / normal));
526 f = (dt_fabsl((long double)val) * len) / total;
527 depth = (uint_t)(f + 0.5);
529 return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
530 ats + len - depth, (long long)val / normal));
534 * If we're here, we have both positive and negative bucket values.
535 * To express this graphically, we're going to generate both positive
536 * and negative bars separated by a centerline. These bars are half
537 * the size of normal quantize()/lquantize() bars, so we divide the
538 * length in half before calculating the bar length.
542 spaces = &spaces[len];
544 f = (dt_fabsl((long double)val) * len) / total;
545 depth = (uint_t)(f + 0.5);
548 return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
549 ats + len - depth, len, "", (long long)val / normal));
551 return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
552 ats + len - depth, spaces + depth,
553 (long long)val / normal));
558 dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
559 size_t size, uint64_t normal)
561 const int64_t *data = addr;
562 int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
563 long double total = 0;
564 char positives = 0, negatives = 0;
566 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
567 return (dt_set_errno(dtp, EDT_DMISMATCH));
569 while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
572 if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
574 * There isn't any data. This is possible if (and only if)
575 * negative increment values have been used. In this case,
576 * we'll print the buckets around 0.
578 first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
579 last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
584 while (last_bin > 0 && data[last_bin] == 0)
587 if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
591 for (i = first_bin; i <= last_bin; i++) {
592 positives |= (data[i] > 0);
593 negatives |= (data[i] < 0);
594 total += dt_fabsl((long double)data[i]);
597 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
598 "------------- Distribution -------------", "count") < 0)
601 for (i = first_bin; i <= last_bin; i++) {
602 if (dt_printf(dtp, fp, "%16lld ",
603 (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
606 if (dt_print_quantline(dtp, fp, data[i], normal, total,
607 positives, negatives) < 0)
615 dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
616 size_t size, uint64_t normal)
618 const int64_t *data = addr;
619 int i, first_bin, last_bin, base;
621 long double total = 0;
622 uint16_t step, levels;
623 char positives = 0, negatives = 0;
625 if (size < sizeof (uint64_t))
626 return (dt_set_errno(dtp, EDT_DMISMATCH));
629 size -= sizeof (uint64_t);
631 base = DTRACE_LQUANTIZE_BASE(arg);
632 step = DTRACE_LQUANTIZE_STEP(arg);
633 levels = DTRACE_LQUANTIZE_LEVELS(arg);
636 last_bin = levels + 1;
638 if (size != sizeof (uint64_t) * (levels + 2))
639 return (dt_set_errno(dtp, EDT_DMISMATCH));
641 while (first_bin <= levels + 1 && data[first_bin] == 0)
644 if (first_bin > levels + 1) {
651 while (last_bin > 0 && data[last_bin] == 0)
654 if (last_bin < levels + 1)
658 for (i = first_bin; i <= last_bin; i++) {
659 positives |= (data[i] > 0);
660 negatives |= (data[i] < 0);
661 total += dt_fabsl((long double)data[i]);
664 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
665 "------------- Distribution -------------", "count") < 0)
668 for (i = first_bin; i <= last_bin; i++) {
673 (void) snprintf(c, sizeof (c), "< %d",
674 base / (uint32_t)normal);
675 err = dt_printf(dtp, fp, "%16s ", c);
676 } else if (i == levels + 1) {
677 (void) snprintf(c, sizeof (c), ">= %d",
678 base + (levels * step));
679 err = dt_printf(dtp, fp, "%16s ", c);
681 err = dt_printf(dtp, fp, "%16d ",
682 base + (i - 1) * step);
685 if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
686 total, positives, negatives) < 0)
694 dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
695 size_t size, uint64_t normal)
697 int i, first_bin, last_bin, bin = 1, order, levels;
698 uint16_t factor, low, high, nsteps;
699 const int64_t *data = addr;
700 int64_t value = 1, next, step;
701 char positives = 0, negatives = 0;
702 long double total = 0;
706 if (size < sizeof (uint64_t))
707 return (dt_set_errno(dtp, EDT_DMISMATCH));
710 size -= sizeof (uint64_t);
712 factor = DTRACE_LLQUANTIZE_FACTOR(arg);
713 low = DTRACE_LLQUANTIZE_LOW(arg);
714 high = DTRACE_LLQUANTIZE_HIGH(arg);
715 nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
718 * We don't expect to be handed invalid llquantize() parameters here,
719 * but sanity check them (to a degree) nonetheless.
721 if (size > INT32_MAX || factor < 2 || low >= high ||
722 nsteps == 0 || factor > nsteps)
723 return (dt_set_errno(dtp, EDT_DMISMATCH));
725 levels = (int)size / sizeof (uint64_t);
728 last_bin = levels - 1;
730 while (first_bin < levels && data[first_bin] == 0)
733 if (first_bin == levels) {
740 while (last_bin > 0 && data[last_bin] == 0)
743 if (last_bin < levels - 1)
747 for (i = first_bin; i <= last_bin; i++) {
748 positives |= (data[i] > 0);
749 negatives |= (data[i] < 0);
750 total += dt_fabsl((long double)data[i]);
753 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
754 "------------- Distribution -------------", "count") < 0)
757 for (order = 0; order < low; order++)
760 next = value * factor;
761 step = next > nsteps ? next / nsteps : 1;
763 if (first_bin == 0) {
764 (void) snprintf(c, sizeof (c), "< %lld", (long long)value);
766 if (dt_printf(dtp, fp, "%16s ", c) < 0)
769 if (dt_print_quantline(dtp, fp, data[0], normal,
770 total, positives, negatives) < 0)
774 while (order <= high) {
775 if (bin >= first_bin && bin <= last_bin) {
776 if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
779 if (dt_print_quantline(dtp, fp, data[bin],
780 normal, total, positives, negatives) < 0)
784 assert(value < next);
787 if ((value += step) != next)
790 next = value * factor;
791 step = next > nsteps ? next / nsteps : 1;
798 assert(last_bin == bin);
799 (void) snprintf(c, sizeof (c), ">= %lld", value);
801 if (dt_printf(dtp, fp, "%16s ", c) < 0)
804 return (dt_print_quantline(dtp, fp, data[bin], normal,
805 total, positives, negatives));
810 dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
811 size_t size, uint64_t normal)
813 /* LINTED - alignment */
814 int64_t *data = (int64_t *)addr;
816 return (dt_printf(dtp, fp, " %16lld", data[0] ?
817 (long long)(data[1] / (int64_t)normal / data[0]) : 0));
822 dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
823 size_t size, uint64_t normal)
825 /* LINTED - alignment */
826 uint64_t *data = (uint64_t *)addr;
828 return (dt_printf(dtp, fp, " %16llu", data[0] ?
829 (unsigned long long) dt_stddev(data, normal) : 0));
834 dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
835 size_t nbytes, int width, int quiet, int raw)
838 * If the byte stream is a series of printable characters, followed by
839 * a terminating byte, we print it out as a string. Otherwise, we
840 * assume that it's something else and just print the bytes.
842 int i, j, margin = 5;
843 char *c = (char *)addr;
848 if (raw || dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
851 for (i = 0; i < nbytes; i++) {
853 * We define a "printable character" to be one for which
854 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
855 * or a character which is either backspace or the bell.
856 * Backspace and the bell are regrettably special because
857 * they fail the first two tests -- and yet they are entirely
858 * printable. These are the only two control characters that
859 * have meaning for the terminal and for which isprint(3C) and
860 * isspace(3C) return 0.
862 if (isprint(c[i]) || isspace(c[i]) ||
863 c[i] == '\b' || c[i] == '\a')
866 if (c[i] == '\0' && i > 0) {
868 * This looks like it might be a string. Before we
869 * assume that it is indeed a string, check the
870 * remainder of the byte range; if it contains
871 * additional non-nul characters, we'll assume that
872 * it's a binary stream that just happens to look like
873 * a string, and we'll print out the individual bytes.
875 for (j = i + 1; j < nbytes; j++) {
884 return (dt_printf(dtp, fp, "%s", c));
886 return (dt_printf(dtp, fp, " %-*s", width, c));
894 * The byte range is all printable characters, but there is
895 * no trailing nul byte. We'll assume that it's a string and
898 char *s = alloca(nbytes + 1);
901 return (dt_printf(dtp, fp, " %-*s", width, s));
905 if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0)
908 for (i = 0; i < 16; i++)
909 if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0)
912 if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0)
916 for (i = 0; i < nbytes; i += 16) {
917 if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
920 for (j = i; j < i + 16 && j < nbytes; j++) {
921 if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
926 if (dt_printf(dtp, fp, " ") < 0)
930 if (dt_printf(dtp, fp, " ") < 0)
933 for (j = i; j < i + 16 && j < nbytes; j++) {
934 if (dt_printf(dtp, fp, "%c",
935 c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
939 if (dt_printf(dtp, fp, "\n") < 0)
947 dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
948 caddr_t addr, int depth, int size)
950 dtrace_syminfo_t dts;
953 char c[PATH_MAX * 2];
956 if (dt_printf(dtp, fp, "\n") < 0)
962 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
963 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
965 indent = _dtrace_stkindent;
967 for (i = 0; i < depth; i++) {
969 case sizeof (uint32_t):
970 /* LINTED - alignment */
971 pc = *((uint32_t *)addr);
974 case sizeof (uint64_t):
975 /* LINTED - alignment */
976 pc = *((uint64_t *)addr);
980 return (dt_set_errno(dtp, EDT_BADSTACKPC));
988 if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
991 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
992 if (pc > sym.st_value) {
993 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
994 dts.dts_object, dts.dts_name,
997 (void) snprintf(c, sizeof (c), "%s`%s",
998 dts.dts_object, dts.dts_name);
1002 * We'll repeat the lookup, but this time we'll specify
1003 * a NULL GElf_Sym -- indicating that we're only
1004 * interested in the containing module.
1006 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1007 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1008 dts.dts_object, pc);
1010 (void) snprintf(c, sizeof (c), "0x%llx", pc);
1014 if (dt_printf(dtp, fp, format, c) < 0)
1017 if (dt_printf(dtp, fp, "\n") < 0)
1025 dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1026 caddr_t addr, uint64_t arg)
1028 /* LINTED - alignment */
1029 uint64_t *pc = (uint64_t *)addr;
1030 uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1031 uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1032 const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1033 const char *str = strsize ? strbase : NULL;
1036 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1037 struct ps_prochandle *P;
1047 if (dt_printf(dtp, fp, "\n") < 0)
1053 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1054 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1056 indent = _dtrace_stkindent;
1059 * Ultimately, we need to add an entry point in the library vector for
1060 * determining <symbol, offset> from <pid, address>. For now, if
1061 * this is a vector open, we just print the raw address or string.
1063 if (dtp->dt_vector == NULL)
1064 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1069 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1071 for (i = 0; i < depth && pc[i] != 0; i++) {
1074 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1077 if (P != NULL && Plookup_by_addr(P, pc[i],
1078 name, sizeof (name), &sym) == 0) {
1079 (void) Pobjname(P, pc[i], objname, sizeof (objname));
1081 if (pc[i] > sym.st_value) {
1082 (void) snprintf(c, sizeof (c),
1083 "%s`%s+0x%llx", dt_basename(objname), name,
1084 (u_longlong_t)(pc[i] - sym.st_value));
1086 (void) snprintf(c, sizeof (c),
1087 "%s`%s", dt_basename(objname), name);
1089 } else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1090 (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1091 (map->pr_mflags & MA_WRITE)))) {
1093 * If the current string pointer in the string table
1094 * does not point to an empty string _and_ the program
1095 * counter falls in a writable region, we'll use the
1096 * string from the string table instead of the raw
1097 * address. This last condition is necessary because
1098 * some (broken) ustack helpers will return a string
1099 * even for a program counter that they can't
1100 * identify. If we have a string for a program
1101 * counter that falls in a segment that isn't
1102 * writable, we assume that we have fallen into this
1103 * case and we refuse to use the string.
1105 (void) snprintf(c, sizeof (c), "%s", str);
1107 if (P != NULL && Pobjname(P, pc[i], objname,
1108 sizeof (objname)) != 0) {
1109 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1110 dt_basename(objname), (u_longlong_t)pc[i]);
1112 (void) snprintf(c, sizeof (c), "0x%llx",
1113 (u_longlong_t)pc[i]);
1117 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1120 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1123 if (str != NULL && str[0] == '@') {
1125 * If the first character of the string is an "at" sign,
1126 * then the string is inferred to be an annotation --
1127 * and it is printed out beneath the frame and offset
1130 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1133 (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]);
1135 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1138 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1143 str += strlen(str) + 1;
1144 if (str - strbase >= strsize)
1150 dt_proc_unlock(dtp, P);
1151 dt_proc_release(dtp, P);
1158 dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1160 /* LINTED - alignment */
1161 uint64_t pid = ((uint64_t *)addr)[0];
1162 /* LINTED - alignment */
1163 uint64_t pc = ((uint64_t *)addr)[1];
1164 const char *format = " %-50s";
1168 if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1169 struct ps_prochandle *P;
1171 if ((P = dt_proc_grab(dtp, pid,
1172 PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1175 dt_proc_lock(dtp, P);
1177 if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1180 dt_proc_unlock(dtp, P);
1181 dt_proc_release(dtp, P);
1188 } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1190 return (dt_printf(dtp, fp, format, s));
1194 dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1196 /* LINTED - alignment */
1197 uint64_t pid = ((uint64_t *)addr)[0];
1198 /* LINTED - alignment */
1199 uint64_t pc = ((uint64_t *)addr)[1];
1202 char objname[PATH_MAX], c[PATH_MAX * 2];
1203 struct ps_prochandle *P;
1209 * See the comment in dt_print_ustack() for the rationale for
1210 * printing raw addresses in the vectored case.
1212 if (dtp->dt_vector == NULL)
1213 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1218 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1220 if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) {
1221 (void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1223 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1226 err = dt_printf(dtp, fp, format, c);
1229 dt_proc_unlock(dtp, P);
1230 dt_proc_release(dtp, P);
1237 dt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1239 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1240 size_t nbytes = *((uintptr_t *) addr);
1242 return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t),
1243 nbytes, 50, quiet, 1));
1246 typedef struct dt_type_cbdata {
1248 dtrace_typeinfo_t dtt;
1259 static int dt_print_type_data(dt_type_cbdata_t *, ctf_id_t);
1262 dt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1264 dt_type_cbdata_t cbdata;
1265 dt_type_cbdata_t *cbdatap = arg;
1268 if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0)
1276 cbdata.addrend = cbdata.addr + ssz;
1278 return (dt_print_type_data(&cbdata, type));
1282 dt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg)
1284 char buf[DT_TYPE_NAMELEN];
1286 dt_type_cbdata_t *cbdatap = arg;
1287 size_t sz = strlen(name);
1289 ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1291 if ((p = strchr(buf, '[')) != NULL)
1298 if (sz > cbdatap->name_width)
1299 cbdatap->name_width = sz;
1303 if (sz > cbdatap->type_width)
1304 cbdatap->type_width = sz;
1310 dt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type)
1312 caddr_t addr = cbdatap->addr;
1313 caddr_t addrend = cbdatap->addrend;
1314 char buf[DT_TYPE_NAMELEN];
1317 uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type);
1318 ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type);
1320 ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1322 if ((p = strchr(buf, '[')) != NULL)
1327 if (cbdatap->f_type) {
1328 int type_width = roundup(cbdatap->type_width + 1, 4);
1329 int name_width = roundup(cbdatap->name_width + 1, 4);
1331 name_width -= strlen(cbdatap->name);
1333 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s = ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p);
1336 while (addr < addrend) {
1337 dt_type_cbdata_t cbdata;
1338 ctf_arinfo_t arinfo;
1345 cbdata.addrend = addr + ssz;
1348 cbdata.type_width = 0;
1349 cbdata.name_width = 0;
1352 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,"");
1356 if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0)
1358 if ((cte.cte_format & CTF_INT_SIGNED) != 0)
1359 switch (cte.cte_bits) {
1361 if (isprint(*((char *) vp)))
1362 dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp));
1363 dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp));
1366 dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp));
1369 dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp));
1372 dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp));
1375 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);
1379 switch (cte.cte_bits) {
1381 dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff);
1384 dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp));
1387 dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp));
1390 dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp));
1393 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);
1398 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);
1401 dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr));
1404 if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0)
1406 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,"");
1407 dt_print_type_data(&cbdata, arinfo.ctr_contents);
1408 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1410 case CTF_K_FUNCTION:
1411 dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n");
1415 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1416 dt_print_type_width, &cbdata) != 0)
1418 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1419 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1420 dt_print_type_member, &cbdata) != 0)
1422 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1426 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1427 dt_print_type_width, &cbdata) != 0)
1429 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1430 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1431 dt_print_type_member, &cbdata) != 0)
1433 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1436 dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp)));
1439 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1441 case CTF_K_VOLATILE:
1442 if (cbdatap->f_type)
1443 dt_printf(cbdatap->dtp, cbdatap->fp, "volatile ");
1444 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1447 if (cbdatap->f_type)
1448 dt_printf(cbdatap->dtp, cbdatap->fp, "const ");
1449 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1451 case CTF_K_RESTRICT:
1452 if (cbdatap->f_type)
1453 dt_printf(cbdatap->dtp, cbdatap->fp, "restrict ");
1454 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1468 dt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1472 dtrace_typeinfo_t dtt;
1473 dt_type_cbdata_t cbdata;
1475 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1479 dt_printf(dtp, fp, "\n");
1481 /* Get the total number of bytes of data buffered. */
1482 size_t nbytes = *((uintptr_t *) addr);
1483 addr += sizeof(uintptr_t);
1486 * Get the size of the type so that we can check that it matches
1487 * the CTF data we look up and so that we can figure out how many
1488 * type elements are buffered.
1490 size_t typs = *((uintptr_t *) addr);
1491 addr += sizeof(uintptr_t);
1494 * Point to the type string in the buffer. Get it's string
1495 * length and round it up to become the offset to the start
1496 * of the buffered type data which we would like to be aligned
1499 char *strp = (char *) addr;
1500 int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t));
1503 * The type string might have a format such as 'int [20]'.
1504 * Check if there is an array dimension present.
1506 if ((p = strchr(strp, '[')) != NULL) {
1507 /* Strip off the array dimension. */
1510 for (; *p != '\0' && *p != ']'; p++)
1511 num = num * 10 + *p - '0';
1513 /* No array dimension, so default. */
1516 /* Lookup the CTF type from the type string. */
1517 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY, strp, &dtt) < 0)
1520 /* Offset the buffer address to the start of the data... */
1523 ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1526 printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz);
1534 cbdata.addrend = addr + nbytes;
1537 cbdata.type_width = 0;
1538 cbdata.name_width = 0;
1541 return (dt_print_type_data(&cbdata, dtt.dtt_type));
1545 dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1547 /* LINTED - alignment */
1548 uint64_t pc = *((uint64_t *)addr);
1549 dtrace_syminfo_t dts;
1551 char c[PATH_MAX * 2];
1556 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1557 (void) snprintf(c, sizeof (c), "%s`%s",
1558 dts.dts_object, dts.dts_name);
1561 * We'll repeat the lookup, but this time we'll specify a
1562 * NULL GElf_Sym -- indicating that we're only interested in
1563 * the containing module.
1565 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1566 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1567 dts.dts_object, (u_longlong_t)pc);
1569 (void) snprintf(c, sizeof (c), "0x%llx",
1574 if (dt_printf(dtp, fp, format, c) < 0)
1581 dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1583 /* LINTED - alignment */
1584 uint64_t pc = *((uint64_t *)addr);
1585 dtrace_syminfo_t dts;
1586 char c[PATH_MAX * 2];
1591 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1592 (void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1594 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1597 if (dt_printf(dtp, fp, format, c) < 0)
1603 typedef struct dt_normal {
1604 dtrace_aggvarid_t dtnd_id;
1605 uint64_t dtnd_normal;
1609 dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1611 dt_normal_t *normal = arg;
1612 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1613 dtrace_aggvarid_t id = normal->dtnd_id;
1615 if (agg->dtagd_nrecs == 0)
1616 return (DTRACE_AGGWALK_NEXT);
1618 if (agg->dtagd_varid != id)
1619 return (DTRACE_AGGWALK_NEXT);
1621 ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1622 return (DTRACE_AGGWALK_NORMALIZE);
1626 dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1632 * We (should) have two records: the aggregation ID followed by the
1633 * normalization value.
1635 addr = base + rec->dtrd_offset;
1637 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1638 return (dt_set_errno(dtp, EDT_BADNORMAL));
1640 /* LINTED - alignment */
1641 normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1644 if (rec->dtrd_action != DTRACEACT_LIBACT)
1645 return (dt_set_errno(dtp, EDT_BADNORMAL));
1647 if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1648 return (dt_set_errno(dtp, EDT_BADNORMAL));
1650 addr = base + rec->dtrd_offset;
1652 switch (rec->dtrd_size) {
1653 case sizeof (uint64_t):
1654 /* LINTED - alignment */
1655 normal.dtnd_normal = *((uint64_t *)addr);
1657 case sizeof (uint32_t):
1658 /* LINTED - alignment */
1659 normal.dtnd_normal = *((uint32_t *)addr);
1661 case sizeof (uint16_t):
1662 /* LINTED - alignment */
1663 normal.dtnd_normal = *((uint16_t *)addr);
1665 case sizeof (uint8_t):
1666 normal.dtnd_normal = *((uint8_t *)addr);
1669 return (dt_set_errno(dtp, EDT_BADNORMAL));
1672 (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1678 dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1680 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1681 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1683 if (agg->dtagd_nrecs == 0)
1684 return (DTRACE_AGGWALK_NEXT);
1686 if (agg->dtagd_varid != id)
1687 return (DTRACE_AGGWALK_NEXT);
1689 return (DTRACE_AGGWALK_DENORMALIZE);
1693 dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1695 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1696 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1698 if (agg->dtagd_nrecs == 0)
1699 return (DTRACE_AGGWALK_NEXT);
1701 if (agg->dtagd_varid != id)
1702 return (DTRACE_AGGWALK_NEXT);
1704 return (DTRACE_AGGWALK_CLEAR);
1707 typedef struct dt_trunc {
1708 dtrace_aggvarid_t dttd_id;
1709 uint64_t dttd_remaining;
1713 dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
1715 dt_trunc_t *trunc = arg;
1716 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1717 dtrace_aggvarid_t id = trunc->dttd_id;
1719 if (agg->dtagd_nrecs == 0)
1720 return (DTRACE_AGGWALK_NEXT);
1722 if (agg->dtagd_varid != id)
1723 return (DTRACE_AGGWALK_NEXT);
1725 if (trunc->dttd_remaining == 0)
1726 return (DTRACE_AGGWALK_REMOVE);
1728 trunc->dttd_remaining--;
1729 return (DTRACE_AGGWALK_NEXT);
1733 dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1738 int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
1741 * We (should) have two records: the aggregation ID followed by the
1742 * number of aggregation entries after which the aggregation is to be
1745 addr = base + rec->dtrd_offset;
1747 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1748 return (dt_set_errno(dtp, EDT_BADTRUNC));
1750 /* LINTED - alignment */
1751 trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
1754 if (rec->dtrd_action != DTRACEACT_LIBACT)
1755 return (dt_set_errno(dtp, EDT_BADTRUNC));
1757 if (rec->dtrd_arg != DT_ACT_TRUNC)
1758 return (dt_set_errno(dtp, EDT_BADTRUNC));
1760 addr = base + rec->dtrd_offset;
1762 switch (rec->dtrd_size) {
1763 case sizeof (uint64_t):
1764 /* LINTED - alignment */
1765 remaining = *((int64_t *)addr);
1767 case sizeof (uint32_t):
1768 /* LINTED - alignment */
1769 remaining = *((int32_t *)addr);
1771 case sizeof (uint16_t):
1772 /* LINTED - alignment */
1773 remaining = *((int16_t *)addr);
1775 case sizeof (uint8_t):
1776 remaining = *((int8_t *)addr);
1779 return (dt_set_errno(dtp, EDT_BADNORMAL));
1782 if (remaining < 0) {
1783 func = dtrace_aggregate_walk_valsorted;
1784 remaining = -remaining;
1786 func = dtrace_aggregate_walk_valrevsorted;
1789 assert(remaining >= 0);
1790 trunc.dttd_remaining = remaining;
1792 (void) func(dtp, dt_trunc_agg, &trunc);
1798 dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
1799 caddr_t addr, size_t size, uint64_t normal)
1802 dtrace_actkind_t act = rec->dtrd_action;
1805 case DTRACEACT_STACK:
1806 return (dt_print_stack(dtp, fp, NULL, addr,
1807 rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
1809 case DTRACEACT_USTACK:
1810 case DTRACEACT_JSTACK:
1811 return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
1813 case DTRACEACT_USYM:
1814 case DTRACEACT_UADDR:
1815 return (dt_print_usym(dtp, fp, addr, act));
1817 case DTRACEACT_UMOD:
1818 return (dt_print_umod(dtp, fp, NULL, addr));
1821 return (dt_print_sym(dtp, fp, NULL, addr));
1824 return (dt_print_mod(dtp, fp, NULL, addr));
1826 case DTRACEAGG_QUANTIZE:
1827 return (dt_print_quantize(dtp, fp, addr, size, normal));
1829 case DTRACEAGG_LQUANTIZE:
1830 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1832 case DTRACEAGG_LLQUANTIZE:
1833 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1836 return (dt_print_average(dtp, fp, addr, size, normal));
1838 case DTRACEAGG_STDDEV:
1839 return (dt_print_stddev(dtp, fp, addr, size, normal));
1846 case sizeof (uint64_t):
1847 err = dt_printf(dtp, fp, " %16lld",
1848 /* LINTED - alignment */
1849 (long long)*((uint64_t *)addr) / normal);
1851 case sizeof (uint32_t):
1852 /* LINTED - alignment */
1853 err = dt_printf(dtp, fp, " %8d", *((uint32_t *)addr) /
1856 case sizeof (uint16_t):
1857 /* LINTED - alignment */
1858 err = dt_printf(dtp, fp, " %5d", *((uint16_t *)addr) /
1861 case sizeof (uint8_t):
1862 err = dt_printf(dtp, fp, " %3d", *((uint8_t *)addr) /
1866 err = dt_print_bytes(dtp, fp, addr, size, 50, 0, 0);
1874 dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1877 dt_print_aggdata_t *pd = arg;
1878 const dtrace_aggdata_t *aggdata = aggsdata[0];
1879 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1880 FILE *fp = pd->dtpa_fp;
1881 dtrace_hdl_t *dtp = pd->dtpa_dtp;
1882 dtrace_recdesc_t *rec;
1883 dtrace_actkind_t act;
1888 * Iterate over each record description in the key, printing the traced
1889 * data, skipping the first datum (the tuple member created by the
1892 for (i = 1; i < agg->dtagd_nrecs; i++) {
1893 rec = &agg->dtagd_rec[i];
1894 act = rec->dtrd_action;
1895 addr = aggdata->dtada_data + rec->dtrd_offset;
1896 size = rec->dtrd_size;
1898 if (DTRACEACT_ISAGG(act)) {
1903 if (dt_print_datum(dtp, fp, rec, addr, size, 1) < 0)
1906 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1907 DTRACE_BUFDATA_AGGKEY) < 0)
1911 assert(aggact != 0);
1913 for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
1916 aggdata = aggsdata[i];
1917 agg = aggdata->dtada_desc;
1918 rec = &agg->dtagd_rec[aggact];
1919 act = rec->dtrd_action;
1920 addr = aggdata->dtada_data + rec->dtrd_offset;
1921 size = rec->dtrd_size;
1923 assert(DTRACEACT_ISAGG(act));
1924 normal = aggdata->dtada_normal;
1926 if (dt_print_datum(dtp, fp, rec, addr, size, normal) < 0)
1929 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1930 DTRACE_BUFDATA_AGGVAL) < 0)
1933 if (!pd->dtpa_allunprint)
1934 agg->dtagd_flags |= DTRACE_AGD_PRINTED;
1937 if (dt_printf(dtp, fp, "\n") < 0)
1940 if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
1941 DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
1948 dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
1950 dt_print_aggdata_t *pd = arg;
1951 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1952 dtrace_aggvarid_t aggvarid = pd->dtpa_id;
1954 if (pd->dtpa_allunprint) {
1955 if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
1959 * If we're not printing all unprinted aggregations, then the
1960 * aggregation variable ID denotes a specific aggregation
1961 * variable that we should print -- skip any other aggregations
1962 * that we encounter.
1964 if (agg->dtagd_nrecs == 0)
1967 if (aggvarid != agg->dtagd_varid)
1971 return (dt_print_aggs(&aggdata, 1, arg));
1975 dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
1976 const char *option, const char *value)
1981 dtrace_setoptdata_t optdata;
1983 bzero(&optdata, sizeof (optdata));
1984 (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
1986 if (dtrace_setopt(dtp, option, value) == 0) {
1987 (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
1988 optdata.dtsda_probe = data;
1989 optdata.dtsda_option = option;
1990 optdata.dtsda_handle = dtp;
1992 if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
1998 errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
1999 len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2002 (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2003 option, value, errstr);
2005 if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2012 dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu, dtrace_bufdesc_t *buf,
2013 dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2016 size_t offs, start = buf->dtbd_oldest, end = buf->dtbd_size;
2017 int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2018 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2020 dtrace_epid_t last = DTRACE_EPIDNONE;
2021 dtrace_probedata_t data;
2025 bzero(&data, sizeof (data));
2026 data.dtpda_handle = dtp;
2027 data.dtpda_cpu = cpu;
2030 for (offs = start; offs < end; ) {
2031 dtrace_eprobedesc_t *epd;
2034 * We're guaranteed to have an ID.
2036 id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2038 if (id == DTRACE_EPIDNONE) {
2040 * This is filler to assure proper alignment of the
2041 * next record; we simply ignore it.
2043 offs += sizeof (id);
2047 if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2048 &data.dtpda_pdesc)) != 0)
2051 epd = data.dtpda_edesc;
2052 data.dtpda_data = buf->dtbd_data + offs;
2054 if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2055 rval = dt_handle(dtp, &data);
2057 if (rval == DTRACE_CONSUME_NEXT)
2060 if (rval == DTRACE_CONSUME_ERROR)
2065 (void) dt_flowindent(dtp, &data, last, buf, offs);
2067 rval = (*efunc)(&data, arg);
2070 if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2071 data.dtpda_indent += 2;
2074 if (rval == DTRACE_CONSUME_NEXT)
2077 if (rval == DTRACE_CONSUME_ABORT)
2078 return (dt_set_errno(dtp, EDT_DIRABORT));
2080 if (rval != DTRACE_CONSUME_THIS)
2081 return (dt_set_errno(dtp, EDT_BADRVAL));
2083 for (i = 0; i < epd->dtepd_nrecs; i++) {
2084 dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2085 dtrace_actkind_t act = rec->dtrd_action;
2087 data.dtpda_data = buf->dtbd_data + offs +
2089 addr = data.dtpda_data;
2091 if (act == DTRACEACT_LIBACT) {
2092 uint64_t arg = rec->dtrd_arg;
2093 dtrace_aggvarid_t id;
2097 /* LINTED - alignment */
2098 id = *((dtrace_aggvarid_t *)addr);
2099 (void) dtrace_aggregate_walk(dtp,
2103 case DT_ACT_DENORMALIZE:
2104 /* LINTED - alignment */
2105 id = *((dtrace_aggvarid_t *)addr);
2106 (void) dtrace_aggregate_walk(dtp,
2107 dt_denormalize_agg, &id);
2110 case DT_ACT_FTRUNCATE:
2115 (void) ftruncate(fileno(fp), 0);
2116 (void) fseeko(fp, 0, SEEK_SET);
2119 case DT_ACT_NORMALIZE:
2120 if (i == epd->dtepd_nrecs - 1)
2121 return (dt_set_errno(dtp,
2124 if (dt_normalize(dtp,
2125 buf->dtbd_data + offs, rec) != 0)
2131 case DT_ACT_SETOPT: {
2132 uint64_t *opts = dtp->dt_options;
2133 dtrace_recdesc_t *valrec;
2138 if (i == epd->dtepd_nrecs - 1) {
2139 return (dt_set_errno(dtp,
2143 valrec = &epd->dtepd_rec[++i];
2144 valsize = valrec->dtrd_size;
2146 if (valrec->dtrd_action != act ||
2147 valrec->dtrd_arg != arg) {
2148 return (dt_set_errno(dtp,
2152 if (valsize > sizeof (uint64_t)) {
2153 val = buf->dtbd_data + offs +
2154 valrec->dtrd_offset;
2159 rv = dt_setopt(dtp, &data, addr, val);
2164 flow = (opts[DTRACEOPT_FLOWINDENT] !=
2166 quiet = (opts[DTRACEOPT_QUIET] !=
2173 if (i == epd->dtepd_nrecs - 1)
2174 return (dt_set_errno(dtp,
2178 buf->dtbd_data + offs, rec) != 0)
2189 rval = (*rfunc)(&data, rec, arg);
2191 if (rval == DTRACE_CONSUME_NEXT)
2194 if (rval == DTRACE_CONSUME_ABORT)
2195 return (dt_set_errno(dtp, EDT_DIRABORT));
2197 if (rval != DTRACE_CONSUME_THIS)
2198 return (dt_set_errno(dtp, EDT_BADRVAL));
2200 if (act == DTRACEACT_STACK) {
2201 int depth = rec->dtrd_arg;
2203 if (dt_print_stack(dtp, fp, NULL, addr, depth,
2204 rec->dtrd_size / depth) < 0)
2209 if (act == DTRACEACT_USTACK ||
2210 act == DTRACEACT_JSTACK) {
2211 if (dt_print_ustack(dtp, fp, NULL,
2212 addr, rec->dtrd_arg) < 0)
2217 if (act == DTRACEACT_SYM) {
2218 if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2223 if (act == DTRACEACT_MOD) {
2224 if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2229 if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2230 if (dt_print_usym(dtp, fp, addr, act) < 0)
2235 if (act == DTRACEACT_UMOD) {
2236 if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2241 if (act == DTRACEACT_PRINTM) {
2242 if (dt_print_memory(dtp, fp, addr) < 0)
2247 if (act == DTRACEACT_PRINTT) {
2248 if (dt_print_type(dtp, fp, addr) < 0)
2253 if (DTRACEACT_ISPRINTFLIKE(act)) {
2255 int (*func)(dtrace_hdl_t *, FILE *, void *,
2256 const dtrace_probedata_t *,
2257 const dtrace_recdesc_t *, uint_t,
2258 const void *buf, size_t);
2260 if ((fmtdata = dt_format_lookup(dtp,
2261 rec->dtrd_format)) == NULL)
2265 case DTRACEACT_PRINTF:
2266 func = dtrace_fprintf;
2268 case DTRACEACT_PRINTA:
2269 func = dtrace_fprinta;
2271 case DTRACEACT_SYSTEM:
2272 func = dtrace_system;
2274 case DTRACEACT_FREOPEN:
2275 func = dtrace_freopen;
2279 n = (*func)(dtp, fp, fmtdata, &data,
2280 rec, epd->dtepd_nrecs - i,
2281 (uchar_t *)buf->dtbd_data + offs,
2282 buf->dtbd_size - offs);
2285 return (-1); /* errno is set for us */
2293 if (act == DTRACEACT_PRINTA) {
2294 dt_print_aggdata_t pd;
2295 dtrace_aggvarid_t *aggvars;
2296 int j, naggvars = 0;
2297 size_t size = ((epd->dtepd_nrecs - i) *
2298 sizeof (dtrace_aggvarid_t));
2300 if ((aggvars = dt_alloc(dtp, size)) == NULL)
2304 * This might be a printa() with multiple
2305 * aggregation variables. We need to scan
2306 * forward through the records until we find
2307 * a record from a different statement.
2309 for (j = i; j < epd->dtepd_nrecs; j++) {
2310 dtrace_recdesc_t *nrec;
2313 nrec = &epd->dtepd_rec[j];
2315 if (nrec->dtrd_uarg != rec->dtrd_uarg)
2318 if (nrec->dtrd_action != act) {
2319 return (dt_set_errno(dtp,
2323 naddr = buf->dtbd_data + offs +
2326 aggvars[naggvars++] =
2327 /* LINTED - alignment */
2328 *((dtrace_aggvarid_t *)naddr);
2332 bzero(&pd, sizeof (pd));
2336 assert(naggvars >= 1);
2338 if (naggvars == 1) {
2339 pd.dtpa_id = aggvars[0];
2340 dt_free(dtp, aggvars);
2342 if (dt_printf(dtp, fp, "\n") < 0 ||
2343 dtrace_aggregate_walk_sorted(dtp,
2344 dt_print_agg, &pd) < 0)
2349 if (dt_printf(dtp, fp, "\n") < 0 ||
2350 dtrace_aggregate_walk_joined(dtp, aggvars,
2351 naggvars, dt_print_aggs, &pd) < 0) {
2352 dt_free(dtp, aggvars);
2356 dt_free(dtp, aggvars);
2360 switch (rec->dtrd_size) {
2361 case sizeof (uint64_t):
2362 n = dt_printf(dtp, fp,
2363 quiet ? "%lld" : " %16lld",
2364 /* LINTED - alignment */
2365 *((unsigned long long *)addr));
2367 case sizeof (uint32_t):
2368 n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2369 /* LINTED - alignment */
2370 *((uint32_t *)addr));
2372 case sizeof (uint16_t):
2373 n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2374 /* LINTED - alignment */
2375 *((uint16_t *)addr));
2377 case sizeof (uint8_t):
2378 n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2379 *((uint8_t *)addr));
2382 n = dt_print_bytes(dtp, fp, addr,
2383 rec->dtrd_size, 33, quiet, 0);
2388 return (-1); /* errno is set for us */
2391 if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2392 return (-1); /* errno is set for us */
2396 * Call the record callback with a NULL record to indicate
2397 * that we're done processing this EPID.
2399 rval = (*rfunc)(&data, NULL, arg);
2401 offs += epd->dtepd_size;
2405 if (buf->dtbd_oldest != 0 && start == buf->dtbd_oldest) {
2406 end = buf->dtbd_oldest;
2411 if ((drops = buf->dtbd_drops) == 0)
2415 * Explicitly zero the drops to prevent us from processing them again.
2417 buf->dtbd_drops = 0;
2419 return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2422 typedef struct dt_begin {
2423 dtrace_consume_probe_f *dtbgn_probefunc;
2424 dtrace_consume_rec_f *dtbgn_recfunc;
2426 dtrace_handle_err_f *dtbgn_errhdlr;
2428 int dtbgn_beginonly;
2432 dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
2434 dt_begin_t *begin = (dt_begin_t *)arg;
2435 dtrace_probedesc_t *pd = data->dtpda_pdesc;
2437 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2438 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2440 if (begin->dtbgn_beginonly) {
2442 return (DTRACE_CONSUME_NEXT);
2445 return (DTRACE_CONSUME_NEXT);
2449 * We have a record that we're interested in. Now call the underlying
2452 return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
2456 dt_consume_begin_record(const dtrace_probedata_t *data,
2457 const dtrace_recdesc_t *rec, void *arg)
2459 dt_begin_t *begin = (dt_begin_t *)arg;
2461 return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
2465 dt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
2467 dt_begin_t *begin = (dt_begin_t *)arg;
2468 dtrace_probedesc_t *pd = data->dteda_pdesc;
2470 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2471 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2473 if (begin->dtbgn_beginonly) {
2475 return (DTRACE_HANDLE_OK);
2478 return (DTRACE_HANDLE_OK);
2481 return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
2485 dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp, dtrace_bufdesc_t *buf,
2486 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2489 * There's this idea that the BEGIN probe should be processed before
2490 * everything else, and that the END probe should be processed after
2491 * anything else. In the common case, this is pretty easy to deal
2492 * with. However, a situation may arise where the BEGIN enabling and
2493 * END enabling are on the same CPU, and some enabling in the middle
2494 * occurred on a different CPU. To deal with this (blech!) we need to
2495 * consume the BEGIN buffer up until the end of the BEGIN probe, and
2496 * then set it aside. We will then process every other CPU, and then
2497 * we'll return to the BEGIN CPU and process the rest of the data
2498 * (which will inevitably include the END probe, if any). Making this
2499 * even more complicated (!) is the library's ERROR enabling. Because
2500 * this enabling is processed before we even get into the consume call
2501 * back, any ERROR firing would result in the library's ERROR enabling
2502 * being processed twice -- once in our first pass (for BEGIN probes),
2503 * and again in our second pass (for everything but BEGIN probes). To
2504 * deal with this, we interpose on the ERROR handler to assure that we
2505 * only process ERROR enablings induced by BEGIN enablings in the
2506 * first pass, and that we only process ERROR enablings _not_ induced
2507 * by BEGIN enablings in the second pass.
2510 processorid_t cpu = dtp->dt_beganon;
2511 dtrace_bufdesc_t nbuf;
2513 dtrace_bufdesc_t *pbuf;
2516 static int max_ncpus;
2517 dtrace_optval_t size;
2519 dtp->dt_beganon = -1;
2522 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2524 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2527 * We really don't expect this to fail, but it is at least
2528 * technically possible for this to fail with ENOENT. In this
2529 * case, we just drive on...
2531 if (errno == ENOENT)
2534 return (dt_set_errno(dtp, errno));
2537 if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
2539 * This is the simple case. We're either not stopped, or if
2540 * we are, we actually processed any END probes on another
2541 * CPU. We can simply consume this buffer and return.
2543 return (dt_consume_cpu(dtp, fp, cpu, buf, pf, rf, arg));
2546 begin.dtbgn_probefunc = pf;
2547 begin.dtbgn_recfunc = rf;
2548 begin.dtbgn_arg = arg;
2549 begin.dtbgn_beginonly = 1;
2552 * We need to interpose on the ERROR handler to be sure that we
2553 * only process ERRORs induced by BEGIN.
2555 begin.dtbgn_errhdlr = dtp->dt_errhdlr;
2556 begin.dtbgn_errarg = dtp->dt_errarg;
2557 dtp->dt_errhdlr = dt_consume_begin_error;
2558 dtp->dt_errarg = &begin;
2560 rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe,
2561 dt_consume_begin_record, &begin);
2563 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2564 dtp->dt_errarg = begin.dtbgn_errarg;
2570 * Now allocate a new buffer. We'll use this to deal with every other
2573 bzero(&nbuf, sizeof (dtrace_bufdesc_t));
2574 (void) dtrace_getopt(dtp, "bufsize", &size);
2575 if ((nbuf.dtbd_data = malloc(size)) == NULL)
2576 return (dt_set_errno(dtp, EDT_NOMEM));
2579 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2581 for (i = 0; i < max_ncpus; i++) {
2588 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &nbuf) == -1) {
2591 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &pbuf) == -1) {
2594 * If we failed with ENOENT, it may be because the
2595 * CPU was unconfigured -- this is okay. Any other
2596 * error, however, is unexpected.
2598 if (errno == ENOENT)
2601 free(nbuf.dtbd_data);
2603 return (dt_set_errno(dtp, errno));
2606 if ((rval = dt_consume_cpu(dtp, fp,
2607 i, &nbuf, pf, rf, arg)) != 0) {
2608 free(nbuf.dtbd_data);
2613 free(nbuf.dtbd_data);
2616 * Okay -- we're done with the other buffers. Now we want to
2617 * reconsume the first buffer -- but this time we're looking for
2618 * everything _but_ BEGIN. And of course, in order to only consume
2619 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
2620 * ERROR interposition function...
2622 begin.dtbgn_beginonly = 0;
2624 assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
2625 assert(begin.dtbgn_errarg == dtp->dt_errarg);
2626 dtp->dt_errhdlr = dt_consume_begin_error;
2627 dtp->dt_errarg = &begin;
2629 rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe,
2630 dt_consume_begin_record, &begin);
2632 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2633 dtp->dt_errarg = begin.dtbgn_errarg;
2639 dtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
2640 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2642 dtrace_bufdesc_t *buf = &dtp->dt_buf;
2643 dtrace_optval_t size;
2644 static int max_ncpus;
2646 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
2647 hrtime_t now = gethrtime();
2649 if (dtp->dt_lastswitch != 0) {
2650 if (now - dtp->dt_lastswitch < interval)
2653 dtp->dt_lastswitch += interval;
2655 dtp->dt_lastswitch = now;
2658 if (!dtp->dt_active)
2659 return (dt_set_errno(dtp, EINVAL));
2662 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2665 pf = (dtrace_consume_probe_f *)dt_nullprobe;
2668 rf = (dtrace_consume_rec_f *)dt_nullrec;
2670 if (buf->dtbd_data == NULL) {
2671 (void) dtrace_getopt(dtp, "bufsize", &size);
2672 if ((buf->dtbd_data = malloc(size)) == NULL)
2673 return (dt_set_errno(dtp, EDT_NOMEM));
2675 buf->dtbd_size = size;
2679 * If we have just begun, we want to first process the CPU that
2680 * executed the BEGIN probe (if any).
2682 if (dtp->dt_active && dtp->dt_beganon != -1) {
2683 buf->dtbd_cpu = dtp->dt_beganon;
2684 if ((rval = dt_consume_begin(dtp, fp, buf, pf, rf, arg)) != 0)
2688 for (i = 0; i < max_ncpus; i++) {
2692 * If we have stopped, we want to process the CPU on which the
2693 * END probe was processed only _after_ we have processed
2696 if (dtp->dt_stopped && (i == dtp->dt_endedon))
2700 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2702 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2705 * If we failed with ENOENT, it may be because the
2706 * CPU was unconfigured -- this is okay. Any other
2707 * error, however, is unexpected.
2709 if (errno == ENOENT)
2712 return (dt_set_errno(dtp, errno));
2715 if ((rval = dt_consume_cpu(dtp, fp, i, buf, pf, rf, arg)) != 0)
2719 if (!dtp->dt_stopped)
2722 buf->dtbd_cpu = dtp->dt_endedon;
2725 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2727 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2730 * This _really_ shouldn't fail, but it is strictly speaking
2731 * possible for this to return ENOENT if the CPU that called
2732 * the END enabling somehow managed to become unconfigured.
2733 * It's unclear how the user can possibly expect anything
2734 * rational to happen in this case -- the state has been thrown
2735 * out along with the unconfigured CPU -- so we'll just drive
2738 if (errno == ENOENT)
2741 return (dt_set_errno(dtp, errno));
2744 return (dt_consume_cpu(dtp, fp, dtp->dt_endedon, buf, pf, rf, arg));