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) 2013, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
44 #include <libproc_compat.h>
47 #define DT_MASK_LO 0x00000000FFFFFFFFULL
50 * We declare this here because (1) we need it and (2) we want to avoid a
51 * dependency on libm in libdtrace.
54 dt_fabsl(long double x)
63 dt_ndigits(long long val)
69 val = val == INT64_MIN ? INT64_MAX : -val;
73 while (val > cmp && cmp > 0) {
78 return (rval < 4 ? 4 : rval);
82 * 128-bit arithmetic functions needed to support the stddev() aggregating
86 dt_gt_128(uint64_t *a, uint64_t *b)
88 return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
92 dt_ge_128(uint64_t *a, uint64_t *b)
94 return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
98 dt_le_128(uint64_t *a, uint64_t *b)
100 return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
104 * Shift the 128-bit value in a by b. If b is positive, shift left.
105 * If b is negative, shift right.
108 dt_shift_128(uint64_t *a, int b)
118 a[0] = a[1] >> (b - 64);
122 mask = 1LL << (64 - b);
124 a[0] |= ((a[1] & mask) << (64 - b));
129 a[1] = a[0] << (b - 64);
133 mask = a[0] >> (64 - b);
141 dt_nbits_128(uint64_t *a)
145 uint64_t zero[2] = { 0, 0 };
150 dt_shift_128(tmp, -1);
151 while (dt_gt_128(tmp, zero)) {
152 dt_shift_128(tmp, -1);
160 dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
164 result[0] = minuend[0] - subtrahend[0];
165 result[1] = minuend[1] - subtrahend[1] -
166 (minuend[0] < subtrahend[0] ? 1 : 0);
168 difference[0] = result[0];
169 difference[1] = result[1];
173 dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
177 result[0] = addend1[0] + addend2[0];
178 result[1] = addend1[1] + addend2[1] +
179 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
186 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
187 * use native multiplication on those, and then re-combine into the
188 * resulting 128-bit value.
190 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
197 dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
199 uint64_t hi1, hi2, lo1, lo2;
205 lo1 = factor1 & DT_MASK_LO;
206 lo2 = factor2 & DT_MASK_LO;
208 product[0] = lo1 * lo2;
209 product[1] = hi1 * hi2;
213 dt_shift_128(tmp, 32);
214 dt_add_128(product, tmp, product);
218 dt_shift_128(tmp, 32);
219 dt_add_128(product, tmp, product);
223 * This is long-hand division.
225 * We initialize subtrahend by shifting divisor left as far as possible. We
226 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we
227 * subtract and set the appropriate bit in the result. We then shift
228 * subtrahend right by one bit for the next comparison.
231 dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
233 uint64_t result[2] = { 0, 0 };
234 uint64_t remainder[2];
235 uint64_t subtrahend[2];
236 uint64_t divisor_128[2];
237 uint64_t mask[2] = { 1, 0 };
240 assert(divisor != 0);
242 divisor_128[0] = divisor;
245 remainder[0] = dividend[0];
246 remainder[1] = dividend[1];
248 subtrahend[0] = divisor;
251 while (divisor > 0) {
256 dt_shift_128(subtrahend, 128 - log);
257 dt_shift_128(mask, 128 - log);
259 while (dt_ge_128(remainder, divisor_128)) {
260 if (dt_ge_128(remainder, subtrahend)) {
261 dt_subtract_128(remainder, subtrahend, remainder);
262 result[0] |= mask[0];
263 result[1] |= mask[1];
266 dt_shift_128(subtrahend, -1);
267 dt_shift_128(mask, -1);
270 quotient[0] = result[0];
271 quotient[1] = result[1];
275 * This is the long-hand method of calculating a square root.
276 * The algorithm is as follows:
278 * 1. Group the digits by 2 from the right.
279 * 2. Over the leftmost group, find the largest single-digit number
280 * whose square is less than that group.
281 * 3. Subtract the result of the previous step (2 or 4, depending) and
282 * bring down the next two-digit group.
283 * 4. For the result R we have so far, find the largest single-digit number
284 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
285 * (Note that this is doubling R and performing a decimal left-shift by 1
286 * and searching for the appropriate decimal to fill the one's place.)
287 * The value x is the next digit in the square root.
288 * Repeat steps 3 and 4 until the desired precision is reached. (We're
289 * dealing with integers, so the above is sufficient.)
291 * In decimal, the square root of 582,734 would be calculated as so:
295 * -49 (7^2 == 49 => 7 is the first digit in the square root)
297 * 9 27 (Subtract and bring down the next group.)
298 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
299 * ----- the square root)
300 * 51 34 (Subtract and bring down the next group.)
301 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
302 * ----- the square root)
305 * The above algorithm applies similarly in binary, but note that the
306 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
307 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
308 * preceding difference?
310 * In binary, the square root of 11011011 would be calculated as so:
314 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1)
317 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1)
320 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
323 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
327 dt_sqrt_128(uint64_t *square)
329 uint64_t result[2] = { 0, 0 };
330 uint64_t diff[2] = { 0, 0 };
331 uint64_t one[2] = { 1, 0 };
332 uint64_t next_pair[2];
333 uint64_t next_try[2];
334 uint64_t bit_pairs, pair_shift;
337 bit_pairs = dt_nbits_128(square) / 2;
338 pair_shift = bit_pairs * 2;
340 for (i = 0; i <= bit_pairs; i++) {
342 * Bring down the next pair of bits.
344 next_pair[0] = square[0];
345 next_pair[1] = square[1];
346 dt_shift_128(next_pair, -pair_shift);
350 dt_shift_128(diff, 2);
351 dt_add_128(diff, next_pair, diff);
354 * next_try = R << 2 + 1
356 next_try[0] = result[0];
357 next_try[1] = result[1];
358 dt_shift_128(next_try, 2);
359 dt_add_128(next_try, one, next_try);
361 if (dt_le_128(next_try, diff)) {
362 dt_subtract_128(diff, next_try, diff);
363 dt_shift_128(result, 1);
364 dt_add_128(result, one, result);
366 dt_shift_128(result, 1);
372 assert(result[1] == 0);
378 dt_stddev(uint64_t *data, uint64_t normal)
380 uint64_t avg_of_squares[2];
381 uint64_t square_of_avg[2];
389 * The standard approximation for standard deviation is
390 * sqrt(average(x**2) - average(x)**2), i.e. the square root
391 * of the average of the squares minus the square of the average.
393 dt_divide_128(data + 2, normal, avg_of_squares);
394 dt_divide_128(avg_of_squares, data[0], avg_of_squares);
396 norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
399 norm_avg = -norm_avg;
401 dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
403 dt_subtract_128(avg_of_squares, square_of_avg, diff);
405 return (dt_sqrt_128(diff));
409 dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
410 dtrace_bufdesc_t *buf, size_t offs)
412 dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
413 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
414 char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
415 dtrace_flowkind_t flow = DTRACEFLOW_NONE;
416 const char *str = NULL;
417 static const char *e_str[2] = { " -> ", " => " };
418 static const char *r_str[2] = { " <- ", " <= " };
419 static const char *ent = "entry", *ret = "return";
420 static int entlen = 0, retlen = 0;
421 dtrace_epid_t next, id = epd->dtepd_epid;
426 entlen = strlen(ent);
427 retlen = strlen(ret);
431 * If the name of the probe is "entry" or ends with "-entry", we
432 * treat it as an entry; if it is "return" or ends with "-return",
433 * we treat it as a return. (This allows application-provided probes
434 * like "method-entry" or "function-entry" to participate in flow
435 * indentation -- without accidentally misinterpreting popular probe
436 * names like "carpentry", "gentry" or "Coventry".)
438 if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
439 (sub == n || sub[-1] == '-')) {
440 flow = DTRACEFLOW_ENTRY;
441 str = e_str[strcmp(p, "syscall") == 0];
442 } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
443 (sub == n || sub[-1] == '-')) {
444 flow = DTRACEFLOW_RETURN;
445 str = r_str[strcmp(p, "syscall") == 0];
449 * If we're going to indent this, we need to check the ID of our last
450 * call. If we're looking at the same probe ID but a different EPID,
451 * we _don't_ want to indent. (Yes, there are some minor holes in
452 * this scheme -- it's a heuristic.)
454 if (flow == DTRACEFLOW_ENTRY) {
455 if ((last != DTRACE_EPIDNONE && id != last &&
456 pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
457 flow = DTRACEFLOW_NONE;
461 * If we're going to unindent this, it's more difficult to see if
462 * we don't actually want to unindent it -- we need to look at the
465 if (flow == DTRACEFLOW_RETURN) {
466 offs += epd->dtepd_size;
469 if (offs >= buf->dtbd_size)
472 next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
474 if (next == DTRACE_EPIDNONE)
476 } while (next == DTRACE_EPIDNONE);
478 if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
481 if (next != id && npd->dtpd_id == pd->dtpd_id)
482 flow = DTRACEFLOW_NONE;
486 if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
487 data->dtpda_prefix = str;
489 data->dtpda_prefix = "| ";
492 if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
493 data->dtpda_indent -= 2;
495 data->dtpda_flow = flow;
503 return (DTRACE_CONSUME_THIS);
509 return (DTRACE_CONSUME_NEXT);
513 dt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total)
515 long double val = dt_fabsl((long double)datum);
517 if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) {
523 * If we're zooming in on an aggregation, we want the height of the
524 * highest value to be approximately 95% of total bar height -- so we
525 * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
528 val *= 1 / DTRACE_AGGZOOM_MAX;
535 dt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width)
537 return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n",
538 width ? width : 16, width ? "key" : "value",
539 "------------- Distribution -------------", "count"));
543 dt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width,
544 const dtrace_aggdata_t *aggdata, dtrace_actkind_t action)
546 int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin;
547 int minwidth, maxwidth, i;
549 assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE);
551 if (action == DTRACEAGG_QUANTIZE) {
552 if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
555 if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
558 minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min));
559 maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max));
562 minwidth = maxwidth - 1;
566 if (dt_printf(dtp, fp, "\n%*s %*s .",
567 width, width > 0 ? "key" : "", minwidth, "min") < 0)
570 for (i = min; i <= max; i++) {
571 if (dt_printf(dtp, fp, "-") < 0)
575 return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max"));
579 * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
580 * inclusive) to represent aggregations via UTF-8 -- which are expressed via
581 * 3-byte UTF-8 sequences.
583 #define DTRACE_AGGUTF8_FULL 0x2588
584 #define DTRACE_AGGUTF8_BASE 0x258f
585 #define DTRACE_AGGUTF8_LEVELS 8
587 #define DTRACE_AGGUTF8_BYTE0(val) (0xe0 | ((val) >> 12))
588 #define DTRACE_AGGUTF8_BYTE1(val) (0x80 | (((val) >> 6) & 0x3f))
589 #define DTRACE_AGGUTF8_BYTE2(val) (0x80 | ((val) & 0x3f))
592 dt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
593 uint64_t normal, long double total)
595 uint_t len = 40, i, whole, partial;
596 long double f = (dt_fabsl((long double)val) * len) / total;
597 const char *spaces = " ";
600 partial = (uint_t)((f - (long double)(uint_t)f) *
601 (long double)DTRACE_AGGUTF8_LEVELS);
603 if (dt_printf(dtp, fp, "|") < 0)
606 for (i = 0; i < whole; i++) {
607 if (dt_printf(dtp, fp, "%c%c%c",
608 DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL),
609 DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL),
610 DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0)
615 partial = DTRACE_AGGUTF8_BASE - (partial - 1);
617 if (dt_printf(dtp, fp, "%c%c%c",
618 DTRACE_AGGUTF8_BYTE0(partial),
619 DTRACE_AGGUTF8_BYTE1(partial),
620 DTRACE_AGGUTF8_BYTE2(partial)) < 0)
626 return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i,
627 (long long)val / normal));
631 dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
632 uint64_t normal, long double total, char positives, char negatives)
635 uint_t depth, len = 40;
637 const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
638 const char *spaces = " ";
640 assert(strlen(ats) == len && strlen(spaces) == len);
641 assert(!(total == 0 && (positives || negatives)));
642 assert(!(val < 0 && !negatives));
643 assert(!(val > 0 && !positives));
644 assert(!(val != 0 && total == 0));
648 if (dtp->dt_encoding == DT_ENCODING_UTF8) {
649 return (dt_print_quantline_utf8(dtp, fp, val,
653 f = (dt_fabsl((long double)val) * len) / total;
654 depth = (uint_t)(f + 0.5);
659 return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
660 spaces + depth, (long long)val / normal));
664 f = (dt_fabsl((long double)val) * len) / total;
665 depth = (uint_t)(f + 0.5);
667 return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
668 ats + len - depth, (long long)val / normal));
672 * If we're here, we have both positive and negative bucket values.
673 * To express this graphically, we're going to generate both positive
674 * and negative bars separated by a centerline. These bars are half
675 * the size of normal quantize()/lquantize() bars, so we divide the
676 * length in half before calculating the bar length.
680 spaces = &spaces[len];
682 f = (dt_fabsl((long double)val) * len) / total;
683 depth = (uint_t)(f + 0.5);
686 return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
687 ats + len - depth, len, "", (long long)val / normal));
689 return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
690 ats + len - depth, spaces + depth,
691 (long long)val / normal));
696 * As with UTF-8 printing of aggregations, we use a subset of the Unicode
697 * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
700 #define DTRACE_AGGPACK_BASE 0x2581
701 #define DTRACE_AGGPACK_LEVELS 8
704 dt_print_packed(dtrace_hdl_t *dtp, FILE *fp,
705 long double datum, long double total)
707 static boolean_t utf8_checked = B_FALSE;
708 static boolean_t utf8;
709 char *ascii = "__xxxxXX";
710 char *neg = "vvvvVV";
718 * We want to determine if we can reasonably emit UTF-8 for our
719 * packed aggregation. To do this, we will check for terminals
720 * that are known to be primitive to emit UTF-8 on these.
722 utf8_checked = B_TRUE;
724 if (dtp->dt_encoding == DT_ENCODING_ASCII) {
726 } else if (dtp->dt_encoding == DT_ENCODING_UTF8) {
728 } else if ((term = getenv("TERM")) != NULL &&
729 (strcmp(term, "sun") == 0 ||
730 strcmp(term, "sun-color") == 0 ||
731 strcmp(term, "dumb") == 0)) {
739 return (dt_printf(dtp, fp, " "));
743 val = dt_fabsl(datum * (len - 1)) / total;
744 return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)]));
748 int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum *
749 (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5);
751 return (dt_printf(dtp, fp, "%c%c%c",
752 DTRACE_AGGUTF8_BYTE0(block),
753 DTRACE_AGGUTF8_BYTE1(block),
754 DTRACE_AGGUTF8_BYTE2(block)));
758 val = (datum * (len - 1)) / total;
759 return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)]));
763 dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
764 size_t size, uint64_t normal)
766 const int64_t *data = addr;
767 int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
768 long double total = 0;
769 char positives = 0, negatives = 0;
771 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
772 return (dt_set_errno(dtp, EDT_DMISMATCH));
774 while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
777 if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
779 * There isn't any data. This is possible if the aggregation
780 * has been clear()'d or if negative increment values have been
781 * used. Regardless, we'll print the buckets around 0.
783 first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
784 last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
789 while (last_bin > 0 && data[last_bin] == 0)
792 if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
796 for (i = first_bin; i <= last_bin; i++) {
797 positives |= (data[i] > 0);
798 negatives |= (data[i] < 0);
799 dt_quantize_total(dtp, data[i], &total);
802 if (dt_print_quanthdr(dtp, fp, 0) < 0)
805 for (i = first_bin; i <= last_bin; i++) {
806 if (dt_printf(dtp, fp, "%16lld ",
807 (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
810 if (dt_print_quantline(dtp, fp, data[i], normal, total,
811 positives, negatives) < 0)
819 dt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
820 size_t size, const dtrace_aggdata_t *aggdata)
822 const int64_t *data = addr;
823 long double total = 0, count = 0;
824 int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i;
825 int64_t minval, maxval;
827 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
828 return (dt_set_errno(dtp, EDT_DMISMATCH));
830 if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
833 if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
836 minval = DTRACE_QUANTIZE_BUCKETVAL(min);
837 maxval = DTRACE_QUANTIZE_BUCKETVAL(max);
839 if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval),
840 (long long)minval) < 0)
843 for (i = min; i <= max; i++) {
844 dt_quantize_total(dtp, data[i], &total);
848 for (i = min; i <= max; i++) {
849 if (dt_print_packed(dtp, fp, data[i], total) < 0)
853 if (dt_printf(dtp, fp, ": %*lld | %lld\n",
854 -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0)
861 dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
862 size_t size, uint64_t normal)
864 const int64_t *data = addr;
865 int i, first_bin, last_bin, base;
867 long double total = 0;
868 uint16_t step, levels;
869 char positives = 0, negatives = 0;
871 if (size < sizeof (uint64_t))
872 return (dt_set_errno(dtp, EDT_DMISMATCH));
875 size -= sizeof (uint64_t);
877 base = DTRACE_LQUANTIZE_BASE(arg);
878 step = DTRACE_LQUANTIZE_STEP(arg);
879 levels = DTRACE_LQUANTIZE_LEVELS(arg);
882 last_bin = levels + 1;
884 if (size != sizeof (uint64_t) * (levels + 2))
885 return (dt_set_errno(dtp, EDT_DMISMATCH));
887 while (first_bin <= levels + 1 && data[first_bin] == 0)
890 if (first_bin > levels + 1) {
897 while (last_bin > 0 && data[last_bin] == 0)
900 if (last_bin < levels + 1)
904 for (i = first_bin; i <= last_bin; i++) {
905 positives |= (data[i] > 0);
906 negatives |= (data[i] < 0);
907 dt_quantize_total(dtp, data[i], &total);
910 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
911 "------------- Distribution -------------", "count") < 0)
914 for (i = first_bin; i <= last_bin; i++) {
919 (void) snprintf(c, sizeof (c), "< %d", base);
920 err = dt_printf(dtp, fp, "%16s ", c);
921 } else if (i == levels + 1) {
922 (void) snprintf(c, sizeof (c), ">= %d",
923 base + (levels * step));
924 err = dt_printf(dtp, fp, "%16s ", c);
926 err = dt_printf(dtp, fp, "%16d ",
927 base + (i - 1) * step);
930 if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
931 total, positives, negatives) < 0)
940 dt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
941 size_t size, const dtrace_aggdata_t *aggdata)
943 const int64_t *data = addr;
944 long double total = 0, count = 0;
945 int min, max, base, err;
947 uint16_t step, levels;
951 if (size < sizeof (uint64_t))
952 return (dt_set_errno(dtp, EDT_DMISMATCH));
955 size -= sizeof (uint64_t);
957 base = DTRACE_LQUANTIZE_BASE(arg);
958 step = DTRACE_LQUANTIZE_STEP(arg);
959 levels = DTRACE_LQUANTIZE_LEVELS(arg);
961 if (size != sizeof (uint64_t) * (levels + 2))
962 return (dt_set_errno(dtp, EDT_DMISMATCH));
968 (void) snprintf(c, sizeof (c), "< %d", base);
969 err = dt_printf(dtp, fp, "%8s :", c);
971 err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step);
977 for (i = min; i <= max; i++) {
978 dt_quantize_total(dtp, data[i], &total);
982 for (i = min; i <= max; i++) {
983 if (dt_print_packed(dtp, fp, data[i], total) < 0)
987 (void) snprintf(c, sizeof (c), ">= %d", base + (levels * step));
988 return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count));
992 dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
993 size_t size, uint64_t normal)
995 int i, first_bin, last_bin, bin = 1, order, levels;
996 uint16_t factor, low, high, nsteps;
997 const int64_t *data = addr;
998 int64_t value = 1, next, step;
999 char positives = 0, negatives = 0;
1000 long double total = 0;
1004 if (size < sizeof (uint64_t))
1005 return (dt_set_errno(dtp, EDT_DMISMATCH));
1008 size -= sizeof (uint64_t);
1010 factor = DTRACE_LLQUANTIZE_FACTOR(arg);
1011 low = DTRACE_LLQUANTIZE_LOW(arg);
1012 high = DTRACE_LLQUANTIZE_HIGH(arg);
1013 nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
1016 * We don't expect to be handed invalid llquantize() parameters here,
1017 * but sanity check them (to a degree) nonetheless.
1019 if (size > INT32_MAX || factor < 2 || low >= high ||
1020 nsteps == 0 || factor > nsteps)
1021 return (dt_set_errno(dtp, EDT_DMISMATCH));
1023 levels = (int)size / sizeof (uint64_t);
1026 last_bin = levels - 1;
1028 while (first_bin < levels && data[first_bin] == 0)
1031 if (first_bin == levels) {
1038 while (last_bin > 0 && data[last_bin] == 0)
1041 if (last_bin < levels - 1)
1045 for (i = first_bin; i <= last_bin; i++) {
1046 positives |= (data[i] > 0);
1047 negatives |= (data[i] < 0);
1048 dt_quantize_total(dtp, data[i], &total);
1051 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
1052 "------------- Distribution -------------", "count") < 0)
1055 for (order = 0; order < low; order++)
1058 next = value * factor;
1059 step = next > nsteps ? next / nsteps : 1;
1061 if (first_bin == 0) {
1062 (void) snprintf(c, sizeof (c), "< %lld", (long long)value);
1064 if (dt_printf(dtp, fp, "%16s ", c) < 0)
1067 if (dt_print_quantline(dtp, fp, data[0], normal,
1068 total, positives, negatives) < 0)
1072 while (order <= high) {
1073 if (bin >= first_bin && bin <= last_bin) {
1074 if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
1077 if (dt_print_quantline(dtp, fp, data[bin],
1078 normal, total, positives, negatives) < 0)
1082 assert(value < next);
1085 if ((value += step) != next)
1088 next = value * factor;
1089 step = next > nsteps ? next / nsteps : 1;
1096 assert(last_bin == bin);
1097 (void) snprintf(c, sizeof (c), ">= %lld", (long long)value);
1099 if (dt_printf(dtp, fp, "%16s ", c) < 0)
1102 return (dt_print_quantline(dtp, fp, data[bin], normal,
1103 total, positives, negatives));
1108 dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1109 size_t size, uint64_t normal)
1111 /* LINTED - alignment */
1112 int64_t *data = (int64_t *)addr;
1114 return (dt_printf(dtp, fp, " %16lld", data[0] ?
1115 (long long)(data[1] / (int64_t)normal / data[0]) : 0));
1120 dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1121 size_t size, uint64_t normal)
1123 /* LINTED - alignment */
1124 uint64_t *data = (uint64_t *)addr;
1126 return (dt_printf(dtp, fp, " %16llu", data[0] ?
1127 (unsigned long long) dt_stddev(data, normal) : 0));
1132 dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1133 size_t nbytes, int width, int quiet, int forceraw)
1136 * If the byte stream is a series of printable characters, followed by
1137 * a terminating byte, we print it out as a string. Otherwise, we
1138 * assume that it's something else and just print the bytes.
1140 int i, j, margin = 5;
1141 char *c = (char *)addr;
1149 if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
1152 for (i = 0; i < nbytes; i++) {
1154 * We define a "printable character" to be one for which
1155 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
1156 * or a character which is either backspace or the bell.
1157 * Backspace and the bell are regrettably special because
1158 * they fail the first two tests -- and yet they are entirely
1159 * printable. These are the only two control characters that
1160 * have meaning for the terminal and for which isprint(3C) and
1161 * isspace(3C) return 0.
1163 if (isprint(c[i]) || isspace(c[i]) ||
1164 c[i] == '\b' || c[i] == '\a')
1167 if (c[i] == '\0' && i > 0) {
1169 * This looks like it might be a string. Before we
1170 * assume that it is indeed a string, check the
1171 * remainder of the byte range; if it contains
1172 * additional non-nul characters, we'll assume that
1173 * it's a binary stream that just happens to look like
1174 * a string, and we'll print out the individual bytes.
1176 for (j = i + 1; j < nbytes; j++) {
1185 return (dt_printf(dtp, fp, "%s", c));
1187 return (dt_printf(dtp, fp, " %s%*s",
1188 width < 0 ? " " : "", width, c));
1197 * The byte range is all printable characters, but there is
1198 * no trailing nul byte. We'll assume that it's a string and
1201 char *s = alloca(nbytes + 1);
1202 bcopy(c, s, nbytes);
1204 return (dt_printf(dtp, fp, " %-*s", width, s));
1208 if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0)
1211 for (i = 0; i < 16; i++)
1212 if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0)
1215 if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0)
1219 for (i = 0; i < nbytes; i += 16) {
1220 if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
1223 for (j = i; j < i + 16 && j < nbytes; j++) {
1224 if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
1229 if (dt_printf(dtp, fp, " ") < 0)
1233 if (dt_printf(dtp, fp, " ") < 0)
1236 for (j = i; j < i + 16 && j < nbytes; j++) {
1237 if (dt_printf(dtp, fp, "%c",
1238 c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
1242 if (dt_printf(dtp, fp, "\n") < 0)
1250 dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1251 caddr_t addr, int depth, int size)
1253 dtrace_syminfo_t dts;
1256 char c[PATH_MAX * 2];
1259 if (dt_printf(dtp, fp, "\n") < 0)
1265 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1266 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1268 indent = _dtrace_stkindent;
1270 for (i = 0; i < depth; i++) {
1272 case sizeof (uint32_t):
1273 /* LINTED - alignment */
1274 pc = *((uint32_t *)addr);
1277 case sizeof (uint64_t):
1278 /* LINTED - alignment */
1279 pc = *((uint64_t *)addr);
1283 return (dt_set_errno(dtp, EDT_BADSTACKPC));
1291 if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
1294 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1295 if (pc > sym.st_value) {
1296 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
1297 dts.dts_object, dts.dts_name,
1298 (u_longlong_t)(pc - sym.st_value));
1300 (void) snprintf(c, sizeof (c), "%s`%s",
1301 dts.dts_object, dts.dts_name);
1305 * We'll repeat the lookup, but this time we'll specify
1306 * a NULL GElf_Sym -- indicating that we're only
1307 * interested in the containing module.
1309 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1310 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1311 dts.dts_object, (u_longlong_t)pc);
1313 (void) snprintf(c, sizeof (c), "0x%llx",
1318 if (dt_printf(dtp, fp, format, c) < 0)
1321 if (dt_printf(dtp, fp, "\n") < 0)
1329 dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1330 caddr_t addr, uint64_t arg)
1332 /* LINTED - alignment */
1333 uint64_t *pc = (uint64_t *)addr;
1334 uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1335 uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1336 const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1337 const char *str = strsize ? strbase : NULL;
1340 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1341 struct ps_prochandle *P;
1351 if (dt_printf(dtp, fp, "\n") < 0)
1357 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1358 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1360 indent = _dtrace_stkindent;
1363 * Ultimately, we need to add an entry point in the library vector for
1364 * determining <symbol, offset> from <pid, address>. For now, if
1365 * this is a vector open, we just print the raw address or string.
1367 if (dtp->dt_vector == NULL)
1368 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1373 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1375 for (i = 0; i < depth && pc[i] != 0; i++) {
1378 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1381 if (P != NULL && Plookup_by_addr(P, pc[i],
1382 name, sizeof (name), &sym) == 0) {
1383 (void) Pobjname(P, pc[i], objname, sizeof (objname));
1385 if (pc[i] > sym.st_value) {
1386 (void) snprintf(c, sizeof (c),
1387 "%s`%s+0x%llx", dt_basename(objname), name,
1388 (u_longlong_t)(pc[i] - sym.st_value));
1390 (void) snprintf(c, sizeof (c),
1391 "%s`%s", dt_basename(objname), name);
1393 } else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1394 (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1395 (map->pr_mflags & MA_WRITE)))) {
1397 * If the current string pointer in the string table
1398 * does not point to an empty string _and_ the program
1399 * counter falls in a writable region, we'll use the
1400 * string from the string table instead of the raw
1401 * address. This last condition is necessary because
1402 * some (broken) ustack helpers will return a string
1403 * even for a program counter that they can't
1404 * identify. If we have a string for a program
1405 * counter that falls in a segment that isn't
1406 * writable, we assume that we have fallen into this
1407 * case and we refuse to use the string.
1409 (void) snprintf(c, sizeof (c), "%s", str);
1411 if (P != NULL && Pobjname(P, pc[i], objname,
1412 sizeof (objname)) != 0) {
1413 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1414 dt_basename(objname), (u_longlong_t)pc[i]);
1416 (void) snprintf(c, sizeof (c), "0x%llx",
1417 (u_longlong_t)pc[i]);
1421 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1424 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1427 if (str != NULL && str[0] == '@') {
1429 * If the first character of the string is an "at" sign,
1430 * then the string is inferred to be an annotation --
1431 * and it is printed out beneath the frame and offset
1434 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1437 (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]);
1439 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1442 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1447 str += strlen(str) + 1;
1448 if (str - strbase >= strsize)
1454 dt_proc_unlock(dtp, P);
1455 dt_proc_release(dtp, P);
1462 dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1464 /* LINTED - alignment */
1465 uint64_t pid = ((uint64_t *)addr)[0];
1466 /* LINTED - alignment */
1467 uint64_t pc = ((uint64_t *)addr)[1];
1468 const char *format = " %-50s";
1472 if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1473 struct ps_prochandle *P;
1475 if ((P = dt_proc_grab(dtp, pid,
1476 PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1479 dt_proc_lock(dtp, P);
1481 if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1484 dt_proc_unlock(dtp, P);
1485 dt_proc_release(dtp, P);
1492 } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1494 return (dt_printf(dtp, fp, format, s));
1498 dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1500 /* LINTED - alignment */
1501 uint64_t pid = ((uint64_t *)addr)[0];
1502 /* LINTED - alignment */
1503 uint64_t pc = ((uint64_t *)addr)[1];
1506 char objname[PATH_MAX], c[PATH_MAX * 2];
1507 struct ps_prochandle *P;
1513 * See the comment in dt_print_ustack() for the rationale for
1514 * printing raw addresses in the vectored case.
1516 if (dtp->dt_vector == NULL)
1517 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1522 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1524 if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) {
1525 (void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1527 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1530 err = dt_printf(dtp, fp, format, c);
1533 dt_proc_unlock(dtp, P);
1534 dt_proc_release(dtp, P);
1541 dt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1543 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1544 size_t nbytes = *((uintptr_t *) addr);
1546 return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t),
1547 nbytes, 50, quiet, 1));
1550 typedef struct dt_type_cbdata {
1552 dtrace_typeinfo_t dtt;
1563 static int dt_print_type_data(dt_type_cbdata_t *, ctf_id_t);
1566 dt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1568 dt_type_cbdata_t cbdata;
1569 dt_type_cbdata_t *cbdatap = arg;
1572 if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0)
1580 cbdata.addrend = cbdata.addr + ssz;
1582 return (dt_print_type_data(&cbdata, type));
1586 dt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg)
1588 char buf[DT_TYPE_NAMELEN];
1590 dt_type_cbdata_t *cbdatap = arg;
1591 size_t sz = strlen(name);
1593 ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1595 if ((p = strchr(buf, '[')) != NULL)
1602 if (sz > cbdatap->name_width)
1603 cbdatap->name_width = sz;
1607 if (sz > cbdatap->type_width)
1608 cbdatap->type_width = sz;
1614 dt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type)
1616 caddr_t addr = cbdatap->addr;
1617 caddr_t addrend = cbdatap->addrend;
1618 char buf[DT_TYPE_NAMELEN];
1621 uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type);
1622 ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type);
1624 ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1626 if ((p = strchr(buf, '[')) != NULL)
1631 if (cbdatap->f_type) {
1632 int type_width = roundup(cbdatap->type_width + 1, 4);
1633 int name_width = roundup(cbdatap->name_width + 1, 4);
1635 name_width -= strlen(cbdatap->name);
1637 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s = ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p);
1640 while (addr < addrend) {
1641 dt_type_cbdata_t cbdata;
1642 ctf_arinfo_t arinfo;
1649 cbdata.addrend = addr + ssz;
1652 cbdata.type_width = 0;
1653 cbdata.name_width = 0;
1656 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,"");
1660 if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0)
1662 if ((cte.cte_format & CTF_INT_SIGNED) != 0)
1663 switch (cte.cte_bits) {
1665 if (isprint(*((char *) vp)))
1666 dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp));
1667 dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp));
1670 dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp));
1673 dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp));
1676 dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp));
1679 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);
1683 switch (cte.cte_bits) {
1685 dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff);
1688 dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp));
1691 dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp));
1694 dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp));
1697 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);
1702 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);
1705 dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr));
1708 if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0)
1710 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,"");
1711 dt_print_type_data(&cbdata, arinfo.ctr_contents);
1712 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1714 case CTF_K_FUNCTION:
1715 dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n");
1719 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1720 dt_print_type_width, &cbdata) != 0)
1722 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1723 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1724 dt_print_type_member, &cbdata) != 0)
1726 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1730 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1731 dt_print_type_width, &cbdata) != 0)
1733 dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1734 if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1735 dt_print_type_member, &cbdata) != 0)
1737 dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1740 dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp)));
1743 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1745 case CTF_K_VOLATILE:
1746 if (cbdatap->f_type)
1747 dt_printf(cbdatap->dtp, cbdatap->fp, "volatile ");
1748 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1751 if (cbdatap->f_type)
1752 dt_printf(cbdatap->dtp, cbdatap->fp, "const ");
1753 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1755 case CTF_K_RESTRICT:
1756 if (cbdatap->f_type)
1757 dt_printf(cbdatap->dtp, cbdatap->fp, "restrict ");
1758 dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1772 dt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1776 dtrace_typeinfo_t dtt;
1777 dt_type_cbdata_t cbdata;
1779 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1783 dt_printf(dtp, fp, "\n");
1785 /* Get the total number of bytes of data buffered. */
1786 size_t nbytes = *((uintptr_t *) addr);
1787 addr += sizeof(uintptr_t);
1790 * Get the size of the type so that we can check that it matches
1791 * the CTF data we look up and so that we can figure out how many
1792 * type elements are buffered.
1794 size_t typs = *((uintptr_t *) addr);
1795 addr += sizeof(uintptr_t);
1798 * Point to the type string in the buffer. Get it's string
1799 * length and round it up to become the offset to the start
1800 * of the buffered type data which we would like to be aligned
1803 char *strp = (char *) addr;
1804 int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t));
1807 * The type string might have a format such as 'int [20]'.
1808 * Check if there is an array dimension present.
1810 if ((p = strchr(strp, '[')) != NULL) {
1811 /* Strip off the array dimension. */
1814 for (; *p != '\0' && *p != ']'; p++)
1815 num = num * 10 + *p - '0';
1817 /* No array dimension, so default. */
1820 /* Lookup the CTF type from the type string. */
1821 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY, strp, &dtt) < 0)
1824 /* Offset the buffer address to the start of the data... */
1827 ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1830 printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz);
1838 cbdata.addrend = addr + nbytes;
1841 cbdata.type_width = 0;
1842 cbdata.name_width = 0;
1845 return (dt_print_type_data(&cbdata, dtt.dtt_type));
1849 dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1851 /* LINTED - alignment */
1852 uint64_t pc = *((uint64_t *)addr);
1853 dtrace_syminfo_t dts;
1855 char c[PATH_MAX * 2];
1860 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1861 (void) snprintf(c, sizeof (c), "%s`%s",
1862 dts.dts_object, dts.dts_name);
1865 * We'll repeat the lookup, but this time we'll specify a
1866 * NULL GElf_Sym -- indicating that we're only interested in
1867 * the containing module.
1869 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1870 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1871 dts.dts_object, (u_longlong_t)pc);
1873 (void) snprintf(c, sizeof (c), "0x%llx",
1878 if (dt_printf(dtp, fp, format, c) < 0)
1885 dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1887 /* LINTED - alignment */
1888 uint64_t pc = *((uint64_t *)addr);
1889 dtrace_syminfo_t dts;
1890 char c[PATH_MAX * 2];
1895 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1896 (void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1898 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1901 if (dt_printf(dtp, fp, format, c) < 0)
1907 typedef struct dt_normal {
1908 dtrace_aggvarid_t dtnd_id;
1909 uint64_t dtnd_normal;
1913 dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1915 dt_normal_t *normal = arg;
1916 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1917 dtrace_aggvarid_t id = normal->dtnd_id;
1919 if (agg->dtagd_nrecs == 0)
1920 return (DTRACE_AGGWALK_NEXT);
1922 if (agg->dtagd_varid != id)
1923 return (DTRACE_AGGWALK_NEXT);
1925 ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1926 return (DTRACE_AGGWALK_NORMALIZE);
1930 dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1936 * We (should) have two records: the aggregation ID followed by the
1937 * normalization value.
1939 addr = base + rec->dtrd_offset;
1941 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1942 return (dt_set_errno(dtp, EDT_BADNORMAL));
1944 /* LINTED - alignment */
1945 normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1948 if (rec->dtrd_action != DTRACEACT_LIBACT)
1949 return (dt_set_errno(dtp, EDT_BADNORMAL));
1951 if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1952 return (dt_set_errno(dtp, EDT_BADNORMAL));
1954 addr = base + rec->dtrd_offset;
1956 switch (rec->dtrd_size) {
1957 case sizeof (uint64_t):
1958 /* LINTED - alignment */
1959 normal.dtnd_normal = *((uint64_t *)addr);
1961 case sizeof (uint32_t):
1962 /* LINTED - alignment */
1963 normal.dtnd_normal = *((uint32_t *)addr);
1965 case sizeof (uint16_t):
1966 /* LINTED - alignment */
1967 normal.dtnd_normal = *((uint16_t *)addr);
1969 case sizeof (uint8_t):
1970 normal.dtnd_normal = *((uint8_t *)addr);
1973 return (dt_set_errno(dtp, EDT_BADNORMAL));
1976 (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1982 dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1984 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1985 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1987 if (agg->dtagd_nrecs == 0)
1988 return (DTRACE_AGGWALK_NEXT);
1990 if (agg->dtagd_varid != id)
1991 return (DTRACE_AGGWALK_NEXT);
1993 return (DTRACE_AGGWALK_DENORMALIZE);
1997 dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1999 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2000 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
2002 if (agg->dtagd_nrecs == 0)
2003 return (DTRACE_AGGWALK_NEXT);
2005 if (agg->dtagd_varid != id)
2006 return (DTRACE_AGGWALK_NEXT);
2008 return (DTRACE_AGGWALK_CLEAR);
2011 typedef struct dt_trunc {
2012 dtrace_aggvarid_t dttd_id;
2013 uint64_t dttd_remaining;
2017 dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
2019 dt_trunc_t *trunc = arg;
2020 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2021 dtrace_aggvarid_t id = trunc->dttd_id;
2023 if (agg->dtagd_nrecs == 0)
2024 return (DTRACE_AGGWALK_NEXT);
2026 if (agg->dtagd_varid != id)
2027 return (DTRACE_AGGWALK_NEXT);
2029 if (trunc->dttd_remaining == 0)
2030 return (DTRACE_AGGWALK_REMOVE);
2032 trunc->dttd_remaining--;
2033 return (DTRACE_AGGWALK_NEXT);
2037 dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
2042 int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
2045 * We (should) have two records: the aggregation ID followed by the
2046 * number of aggregation entries after which the aggregation is to be
2049 addr = base + rec->dtrd_offset;
2051 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
2052 return (dt_set_errno(dtp, EDT_BADTRUNC));
2054 /* LINTED - alignment */
2055 trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
2058 if (rec->dtrd_action != DTRACEACT_LIBACT)
2059 return (dt_set_errno(dtp, EDT_BADTRUNC));
2061 if (rec->dtrd_arg != DT_ACT_TRUNC)
2062 return (dt_set_errno(dtp, EDT_BADTRUNC));
2064 addr = base + rec->dtrd_offset;
2066 switch (rec->dtrd_size) {
2067 case sizeof (uint64_t):
2068 /* LINTED - alignment */
2069 remaining = *((int64_t *)addr);
2071 case sizeof (uint32_t):
2072 /* LINTED - alignment */
2073 remaining = *((int32_t *)addr);
2075 case sizeof (uint16_t):
2076 /* LINTED - alignment */
2077 remaining = *((int16_t *)addr);
2079 case sizeof (uint8_t):
2080 remaining = *((int8_t *)addr);
2083 return (dt_set_errno(dtp, EDT_BADNORMAL));
2086 if (remaining < 0) {
2087 func = dtrace_aggregate_walk_valsorted;
2088 remaining = -remaining;
2090 func = dtrace_aggregate_walk_valrevsorted;
2093 assert(remaining >= 0);
2094 trunc.dttd_remaining = remaining;
2096 (void) func(dtp, dt_trunc_agg, &trunc);
2102 dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
2103 caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata,
2104 uint64_t normal, dt_print_aggdata_t *pd)
2107 dtrace_actkind_t act = rec->dtrd_action;
2108 boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack;
2109 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2115 } *fmt, fmttab[] = {
2116 { sizeof (uint8_t), 3, 3 },
2117 { sizeof (uint16_t), 5, 5 },
2118 { sizeof (uint32_t), 8, 8 },
2119 { sizeof (uint64_t), 16, 16 },
2123 if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) {
2124 dtrace_recdesc_t *r;
2129 * To print our quantization header for either an agghist or
2130 * aggpack aggregation, we need to iterate through all of our
2131 * of our records to determine their width.
2133 for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) {
2134 for (fmt = fmttab; fmt->size &&
2135 fmt->size != r->dtrd_size; fmt++)
2138 width += fmt->packedwidth + 1;
2141 if (pd->dtpa_agghist) {
2142 if (dt_print_quanthdr(dtp, fp, width) < 0)
2145 if (dt_print_quanthdr_packed(dtp, fp,
2146 width, aggdata, r->dtrd_action) < 0)
2150 pd->dtpa_agghisthdr = agg->dtagd_varid;
2153 if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) {
2154 char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES;
2155 char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES;
2158 assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT);
2159 val = (long long)*((uint64_t *)addr);
2161 if (dt_printf(dtp, fp, " ") < 0)
2164 return (dt_print_quantline(dtp, fp, val, normal,
2165 aggdata->dtada_total, positives, negatives));
2168 if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) {
2170 case DTRACEAGG_QUANTIZE:
2171 return (dt_print_quantize_packed(dtp,
2172 fp, addr, size, aggdata));
2173 case DTRACEAGG_LQUANTIZE:
2174 return (dt_print_lquantize_packed(dtp,
2175 fp, addr, size, aggdata));
2182 case DTRACEACT_STACK:
2183 return (dt_print_stack(dtp, fp, NULL, addr,
2184 rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
2186 case DTRACEACT_USTACK:
2187 case DTRACEACT_JSTACK:
2188 return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
2190 case DTRACEACT_USYM:
2191 case DTRACEACT_UADDR:
2192 return (dt_print_usym(dtp, fp, addr, act));
2194 case DTRACEACT_UMOD:
2195 return (dt_print_umod(dtp, fp, NULL, addr));
2198 return (dt_print_sym(dtp, fp, NULL, addr));
2201 return (dt_print_mod(dtp, fp, NULL, addr));
2203 case DTRACEAGG_QUANTIZE:
2204 return (dt_print_quantize(dtp, fp, addr, size, normal));
2206 case DTRACEAGG_LQUANTIZE:
2207 return (dt_print_lquantize(dtp, fp, addr, size, normal));
2209 case DTRACEAGG_LLQUANTIZE:
2210 return (dt_print_llquantize(dtp, fp, addr, size, normal));
2213 return (dt_print_average(dtp, fp, addr, size, normal));
2215 case DTRACEAGG_STDDEV:
2216 return (dt_print_stddev(dtp, fp, addr, size, normal));
2222 for (fmt = fmttab; fmt->size && fmt->size != size; fmt++)
2225 width = packed ? fmt->packedwidth : fmt->width;
2228 case sizeof (uint64_t):
2229 err = dt_printf(dtp, fp, " %*lld", width,
2230 /* LINTED - alignment */
2231 (long long)*((uint64_t *)addr) / normal);
2233 case sizeof (uint32_t):
2234 /* LINTED - alignment */
2235 err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) /
2238 case sizeof (uint16_t):
2239 /* LINTED - alignment */
2240 err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) /
2243 case sizeof (uint8_t):
2244 err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) /
2248 err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0);
2256 dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
2259 dt_print_aggdata_t *pd = arg;
2260 const dtrace_aggdata_t *aggdata = aggsdata[0];
2261 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2262 FILE *fp = pd->dtpa_fp;
2263 dtrace_hdl_t *dtp = pd->dtpa_dtp;
2264 dtrace_recdesc_t *rec;
2265 dtrace_actkind_t act;
2269 pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL);
2270 pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN);
2273 * Iterate over each record description in the key, printing the traced
2274 * data, skipping the first datum (the tuple member created by the
2277 for (i = 1; i < agg->dtagd_nrecs; i++) {
2278 rec = &agg->dtagd_rec[i];
2279 act = rec->dtrd_action;
2280 addr = aggdata->dtada_data + rec->dtrd_offset;
2281 size = rec->dtrd_size;
2283 if (DTRACEACT_ISAGG(act)) {
2288 if (dt_print_datum(dtp, fp, rec, addr,
2289 size, aggdata, 1, pd) < 0)
2292 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2293 DTRACE_BUFDATA_AGGKEY) < 0)
2297 assert(aggact != 0);
2299 for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
2302 aggdata = aggsdata[i];
2303 agg = aggdata->dtada_desc;
2304 rec = &agg->dtagd_rec[aggact];
2305 act = rec->dtrd_action;
2306 addr = aggdata->dtada_data + rec->dtrd_offset;
2307 size = rec->dtrd_size;
2309 assert(DTRACEACT_ISAGG(act));
2310 normal = aggdata->dtada_normal;
2312 if (dt_print_datum(dtp, fp, rec, addr,
2313 size, aggdata, normal, pd) < 0)
2316 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2317 DTRACE_BUFDATA_AGGVAL) < 0)
2320 if (!pd->dtpa_allunprint)
2321 agg->dtagd_flags |= DTRACE_AGD_PRINTED;
2324 if (!pd->dtpa_agghist && !pd->dtpa_aggpack) {
2325 if (dt_printf(dtp, fp, "\n") < 0)
2329 if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
2330 DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
2337 dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
2339 dt_print_aggdata_t *pd = arg;
2340 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2341 dtrace_aggvarid_t aggvarid = pd->dtpa_id;
2343 if (pd->dtpa_allunprint) {
2344 if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
2348 * If we're not printing all unprinted aggregations, then the
2349 * aggregation variable ID denotes a specific aggregation
2350 * variable that we should print -- skip any other aggregations
2351 * that we encounter.
2353 if (agg->dtagd_nrecs == 0)
2356 if (aggvarid != agg->dtagd_varid)
2360 return (dt_print_aggs(&aggdata, 1, arg));
2364 dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
2365 const char *option, const char *value)
2370 dtrace_setoptdata_t optdata;
2372 bzero(&optdata, sizeof (optdata));
2373 (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
2375 if (dtrace_setopt(dtp, option, value) == 0) {
2376 (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
2377 optdata.dtsda_probe = data;
2378 optdata.dtsda_option = option;
2379 optdata.dtsda_handle = dtp;
2381 if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
2387 errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
2388 len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2391 (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2392 option, value, errstr);
2394 if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2401 dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu,
2402 dtrace_bufdesc_t *buf, boolean_t just_one,
2403 dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2407 int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2408 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2410 uint64_t tracememsize = 0;
2411 dtrace_probedata_t data;
2414 bzero(&data, sizeof (data));
2415 data.dtpda_handle = dtp;
2416 data.dtpda_cpu = cpu;
2417 data.dtpda_flow = dtp->dt_flow;
2418 data.dtpda_indent = dtp->dt_indent;
2419 data.dtpda_prefix = dtp->dt_prefix;
2421 for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) {
2422 dtrace_eprobedesc_t *epd;
2425 * We're guaranteed to have an ID.
2427 id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2429 if (id == DTRACE_EPIDNONE) {
2431 * This is filler to assure proper alignment of the
2432 * next record; we simply ignore it.
2434 offs += sizeof (id);
2438 if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2439 &data.dtpda_pdesc)) != 0)
2442 epd = data.dtpda_edesc;
2443 data.dtpda_data = buf->dtbd_data + offs;
2445 if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2446 rval = dt_handle(dtp, &data);
2448 if (rval == DTRACE_CONSUME_NEXT)
2451 if (rval == DTRACE_CONSUME_ERROR)
2456 (void) dt_flowindent(dtp, &data, dtp->dt_last_epid,
2459 rval = (*efunc)(&data, arg);
2462 if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2463 data.dtpda_indent += 2;
2466 if (rval == DTRACE_CONSUME_NEXT)
2469 if (rval == DTRACE_CONSUME_ABORT)
2470 return (dt_set_errno(dtp, EDT_DIRABORT));
2472 if (rval != DTRACE_CONSUME_THIS)
2473 return (dt_set_errno(dtp, EDT_BADRVAL));
2475 for (i = 0; i < epd->dtepd_nrecs; i++) {
2477 dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2478 dtrace_actkind_t act = rec->dtrd_action;
2480 data.dtpda_data = buf->dtbd_data + offs +
2482 addr = data.dtpda_data;
2484 if (act == DTRACEACT_LIBACT) {
2485 uint64_t arg = rec->dtrd_arg;
2486 dtrace_aggvarid_t id;
2490 /* LINTED - alignment */
2491 id = *((dtrace_aggvarid_t *)addr);
2492 (void) dtrace_aggregate_walk(dtp,
2496 case DT_ACT_DENORMALIZE:
2497 /* LINTED - alignment */
2498 id = *((dtrace_aggvarid_t *)addr);
2499 (void) dtrace_aggregate_walk(dtp,
2500 dt_denormalize_agg, &id);
2503 case DT_ACT_FTRUNCATE:
2508 (void) ftruncate(fileno(fp), 0);
2509 (void) fseeko(fp, 0, SEEK_SET);
2512 case DT_ACT_NORMALIZE:
2513 if (i == epd->dtepd_nrecs - 1)
2514 return (dt_set_errno(dtp,
2517 if (dt_normalize(dtp,
2518 buf->dtbd_data + offs, rec) != 0)
2524 case DT_ACT_SETOPT: {
2525 uint64_t *opts = dtp->dt_options;
2526 dtrace_recdesc_t *valrec;
2531 if (i == epd->dtepd_nrecs - 1) {
2532 return (dt_set_errno(dtp,
2536 valrec = &epd->dtepd_rec[++i];
2537 valsize = valrec->dtrd_size;
2539 if (valrec->dtrd_action != act ||
2540 valrec->dtrd_arg != arg) {
2541 return (dt_set_errno(dtp,
2545 if (valsize > sizeof (uint64_t)) {
2546 val = buf->dtbd_data + offs +
2547 valrec->dtrd_offset;
2552 rv = dt_setopt(dtp, &data, addr, val);
2557 flow = (opts[DTRACEOPT_FLOWINDENT] !=
2559 quiet = (opts[DTRACEOPT_QUIET] !=
2566 if (i == epd->dtepd_nrecs - 1)
2567 return (dt_set_errno(dtp,
2571 buf->dtbd_data + offs, rec) != 0)
2582 if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
2583 rec->dtrd_size == sizeof (uint64_t)) {
2584 /* LINTED - alignment */
2585 tracememsize = *((unsigned long long *)addr);
2589 rval = (*rfunc)(&data, rec, arg);
2591 if (rval == DTRACE_CONSUME_NEXT)
2594 if (rval == DTRACE_CONSUME_ABORT)
2595 return (dt_set_errno(dtp, EDT_DIRABORT));
2597 if (rval != DTRACE_CONSUME_THIS)
2598 return (dt_set_errno(dtp, EDT_BADRVAL));
2600 if (act == DTRACEACT_STACK) {
2601 int depth = rec->dtrd_arg;
2603 if (dt_print_stack(dtp, fp, NULL, addr, depth,
2604 rec->dtrd_size / depth) < 0)
2609 if (act == DTRACEACT_USTACK ||
2610 act == DTRACEACT_JSTACK) {
2611 if (dt_print_ustack(dtp, fp, NULL,
2612 addr, rec->dtrd_arg) < 0)
2617 if (act == DTRACEACT_SYM) {
2618 if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2623 if (act == DTRACEACT_MOD) {
2624 if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2629 if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2630 if (dt_print_usym(dtp, fp, addr, act) < 0)
2635 if (act == DTRACEACT_UMOD) {
2636 if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2641 if (act == DTRACEACT_PRINTM) {
2642 if (dt_print_memory(dtp, fp, addr) < 0)
2647 if (act == DTRACEACT_PRINTT) {
2648 if (dt_print_type(dtp, fp, addr) < 0)
2653 if (DTRACEACT_ISPRINTFLIKE(act)) {
2655 int (*func)(dtrace_hdl_t *, FILE *, void *,
2656 const dtrace_probedata_t *,
2657 const dtrace_recdesc_t *, uint_t,
2658 const void *buf, size_t);
2660 if ((fmtdata = dt_format_lookup(dtp,
2661 rec->dtrd_format)) == NULL)
2665 case DTRACEACT_PRINTF:
2666 func = dtrace_fprintf;
2668 case DTRACEACT_PRINTA:
2669 func = dtrace_fprinta;
2671 case DTRACEACT_SYSTEM:
2672 func = dtrace_system;
2674 case DTRACEACT_FREOPEN:
2675 func = dtrace_freopen;
2679 n = (*func)(dtp, fp, fmtdata, &data,
2680 rec, epd->dtepd_nrecs - i,
2681 (uchar_t *)buf->dtbd_data + offs,
2682 buf->dtbd_size - offs);
2685 return (-1); /* errno is set for us */
2693 * If this is a DIF expression, and the record has a
2694 * format set, this indicates we have a CTF type name
2695 * associated with the data and we should try to print
2698 if (act == DTRACEACT_DIFEXPR) {
2699 const char *strdata = dt_strdata_lookup(dtp,
2701 if (strdata != NULL) {
2702 n = dtrace_print(dtp, fp, strdata,
2703 addr, rec->dtrd_size);
2706 * dtrace_print() will return -1 on
2707 * error, or return the number of bytes
2708 * consumed. It will return 0 if the
2709 * type couldn't be determined, and we
2710 * should fall through to the normal
2722 if (act == DTRACEACT_PRINTA) {
2723 dt_print_aggdata_t pd;
2724 dtrace_aggvarid_t *aggvars;
2725 int j, naggvars = 0;
2726 size_t size = ((epd->dtepd_nrecs - i) *
2727 sizeof (dtrace_aggvarid_t));
2729 if ((aggvars = dt_alloc(dtp, size)) == NULL)
2733 * This might be a printa() with multiple
2734 * aggregation variables. We need to scan
2735 * forward through the records until we find
2736 * a record from a different statement.
2738 for (j = i; j < epd->dtepd_nrecs; j++) {
2739 dtrace_recdesc_t *nrec;
2742 nrec = &epd->dtepd_rec[j];
2744 if (nrec->dtrd_uarg != rec->dtrd_uarg)
2747 if (nrec->dtrd_action != act) {
2748 return (dt_set_errno(dtp,
2752 naddr = buf->dtbd_data + offs +
2755 aggvars[naggvars++] =
2756 /* LINTED - alignment */
2757 *((dtrace_aggvarid_t *)naddr);
2761 bzero(&pd, sizeof (pd));
2765 assert(naggvars >= 1);
2767 if (naggvars == 1) {
2768 pd.dtpa_id = aggvars[0];
2769 dt_free(dtp, aggvars);
2771 if (dt_printf(dtp, fp, "\n") < 0 ||
2772 dtrace_aggregate_walk_sorted(dtp,
2773 dt_print_agg, &pd) < 0)
2778 if (dt_printf(dtp, fp, "\n") < 0 ||
2779 dtrace_aggregate_walk_joined(dtp, aggvars,
2780 naggvars, dt_print_aggs, &pd) < 0) {
2781 dt_free(dtp, aggvars);
2785 dt_free(dtp, aggvars);
2789 if (act == DTRACEACT_TRACEMEM) {
2790 if (tracememsize == 0 ||
2791 tracememsize > rec->dtrd_size) {
2792 tracememsize = rec->dtrd_size;
2795 n = dt_print_bytes(dtp, fp, addr,
2796 tracememsize, -33, quiet, 1);
2806 switch (rec->dtrd_size) {
2807 case sizeof (uint64_t):
2808 n = dt_printf(dtp, fp,
2809 quiet ? "%lld" : " %16lld",
2810 /* LINTED - alignment */
2811 *((unsigned long long *)addr));
2813 case sizeof (uint32_t):
2814 n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2815 /* LINTED - alignment */
2816 *((uint32_t *)addr));
2818 case sizeof (uint16_t):
2819 n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2820 /* LINTED - alignment */
2821 *((uint16_t *)addr));
2823 case sizeof (uint8_t):
2824 n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2825 *((uint8_t *)addr));
2828 n = dt_print_bytes(dtp, fp, addr,
2829 rec->dtrd_size, -33, quiet, 0);
2834 return (-1); /* errno is set for us */
2837 if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2838 return (-1); /* errno is set for us */
2842 * Call the record callback with a NULL record to indicate
2843 * that we're done processing this EPID.
2845 rval = (*rfunc)(&data, NULL, arg);
2847 offs += epd->dtepd_size;
2848 dtp->dt_last_epid = id;
2850 buf->dtbd_oldest = offs;
2855 dtp->dt_flow = data.dtpda_flow;
2856 dtp->dt_indent = data.dtpda_indent;
2857 dtp->dt_prefix = data.dtpda_prefix;
2859 if ((drops = buf->dtbd_drops) == 0)
2863 * Explicitly zero the drops to prevent us from processing them again.
2865 buf->dtbd_drops = 0;
2867 return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2871 * Reduce memory usage by shrinking the buffer if it's no more than half full.
2872 * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2873 * only 4-byte aligned.
2876 dt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize)
2878 uint64_t used = buf->dtbd_size - buf->dtbd_oldest;
2879 if (used < cursize / 2) {
2880 int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2881 char *newdata = dt_alloc(dtp, used + misalign);
2882 if (newdata == NULL)
2884 bzero(newdata, misalign);
2885 bcopy(buf->dtbd_data + buf->dtbd_oldest,
2886 newdata + misalign, used);
2887 dt_free(dtp, buf->dtbd_data);
2888 buf->dtbd_oldest = misalign;
2889 buf->dtbd_size = used + misalign;
2890 buf->dtbd_data = newdata;
2895 * If the ring buffer has wrapped, the data is not in order. Rearrange it
2896 * so that it is. Note, we need to preserve the alignment of the data at
2897 * dtbd_oldest, which is only 4-byte aligned.
2900 dt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2903 char *newdata, *ndp;
2905 if (buf->dtbd_oldest == 0)
2908 misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2909 newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign);
2911 if (newdata == NULL)
2914 assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1)));
2916 bzero(ndp, misalign);
2919 bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp,
2920 buf->dtbd_size - buf->dtbd_oldest);
2921 ndp += buf->dtbd_size - buf->dtbd_oldest;
2923 bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest);
2925 dt_free(dtp, buf->dtbd_data);
2926 buf->dtbd_oldest = 0;
2927 buf->dtbd_data = newdata;
2928 buf->dtbd_size += misalign;
2934 dt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2936 dt_free(dtp, buf->dtbd_data);
2941 * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2942 * data, or NULL if there is no data for this CPU.
2943 * Returns -1 on failure and sets dt_errno.
2946 dt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp)
2948 dtrace_optval_t size;
2949 dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf));
2955 (void) dtrace_getopt(dtp, "bufsize", &size);
2956 buf->dtbd_data = dt_alloc(dtp, size);
2957 if (buf->dtbd_data == NULL) {
2961 buf->dtbd_size = size;
2962 buf->dtbd_cpu = cpu;
2965 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2967 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2970 * If we failed with ENOENT, it may be because the
2971 * CPU was unconfigured -- this is okay. Any other
2972 * error, however, is unexpected.
2974 if (errno == ENOENT) {
2978 rval = dt_set_errno(dtp, errno);
2980 dt_put_buf(dtp, buf);
2984 error = dt_unring_buf(dtp, buf);
2986 dt_put_buf(dtp, buf);
2989 dt_realloc_buf(dtp, buf, size);
2995 typedef struct dt_begin {
2996 dtrace_consume_probe_f *dtbgn_probefunc;
2997 dtrace_consume_rec_f *dtbgn_recfunc;
2999 dtrace_handle_err_f *dtbgn_errhdlr;
3001 int dtbgn_beginonly;
3005 dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
3007 dt_begin_t *begin = arg;
3008 dtrace_probedesc_t *pd = data->dtpda_pdesc;
3010 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3011 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3013 if (begin->dtbgn_beginonly) {
3015 return (DTRACE_CONSUME_NEXT);
3018 return (DTRACE_CONSUME_NEXT);
3022 * We have a record that we're interested in. Now call the underlying
3025 return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
3029 dt_consume_begin_record(const dtrace_probedata_t *data,
3030 const dtrace_recdesc_t *rec, void *arg)
3032 dt_begin_t *begin = arg;
3034 return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
3038 dt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
3040 dt_begin_t *begin = (dt_begin_t *)arg;
3041 dtrace_probedesc_t *pd = data->dteda_pdesc;
3043 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3044 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3046 if (begin->dtbgn_beginonly) {
3048 return (DTRACE_HANDLE_OK);
3051 return (DTRACE_HANDLE_OK);
3054 return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
3058 dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp,
3059 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3062 * There's this idea that the BEGIN probe should be processed before
3063 * everything else, and that the END probe should be processed after
3064 * anything else. In the common case, this is pretty easy to deal
3065 * with. However, a situation may arise where the BEGIN enabling and
3066 * END enabling are on the same CPU, and some enabling in the middle
3067 * occurred on a different CPU. To deal with this (blech!) we need to
3068 * consume the BEGIN buffer up until the end of the BEGIN probe, and
3069 * then set it aside. We will then process every other CPU, and then
3070 * we'll return to the BEGIN CPU and process the rest of the data
3071 * (which will inevitably include the END probe, if any). Making this
3072 * even more complicated (!) is the library's ERROR enabling. Because
3073 * this enabling is processed before we even get into the consume call
3074 * back, any ERROR firing would result in the library's ERROR enabling
3075 * being processed twice -- once in our first pass (for BEGIN probes),
3076 * and again in our second pass (for everything but BEGIN probes). To
3077 * deal with this, we interpose on the ERROR handler to assure that we
3078 * only process ERROR enablings induced by BEGIN enablings in the
3079 * first pass, and that we only process ERROR enablings _not_ induced
3080 * by BEGIN enablings in the second pass.
3084 processorid_t cpu = dtp->dt_beganon;
3086 static int max_ncpus;
3087 dtrace_bufdesc_t *buf;
3089 dtp->dt_beganon = -1;
3091 if (dt_get_buf(dtp, cpu, &buf) != 0)
3096 if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
3098 * This is the simple case. We're either not stopped, or if
3099 * we are, we actually processed any END probes on another
3100 * CPU. We can simply consume this buffer and return.
3102 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3104 dt_put_buf(dtp, buf);
3108 begin.dtbgn_probefunc = pf;
3109 begin.dtbgn_recfunc = rf;
3110 begin.dtbgn_arg = arg;
3111 begin.dtbgn_beginonly = 1;
3114 * We need to interpose on the ERROR handler to be sure that we
3115 * only process ERRORs induced by BEGIN.
3117 begin.dtbgn_errhdlr = dtp->dt_errhdlr;
3118 begin.dtbgn_errarg = dtp->dt_errarg;
3119 dtp->dt_errhdlr = dt_consume_begin_error;
3120 dtp->dt_errarg = &begin;
3122 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3123 dt_consume_begin_probe, dt_consume_begin_record, &begin);
3125 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3126 dtp->dt_errarg = begin.dtbgn_errarg;
3129 dt_put_buf(dtp, buf);
3134 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3136 for (i = 0; i < max_ncpus; i++) {
3137 dtrace_bufdesc_t *nbuf;
3141 if (dt_get_buf(dtp, i, &nbuf) != 0) {
3142 dt_put_buf(dtp, buf);
3148 rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE,
3150 dt_put_buf(dtp, nbuf);
3152 dt_put_buf(dtp, buf);
3158 * Okay -- we're done with the other buffers. Now we want to
3159 * reconsume the first buffer -- but this time we're looking for
3160 * everything _but_ BEGIN. And of course, in order to only consume
3161 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
3162 * ERROR interposition function...
3164 begin.dtbgn_beginonly = 0;
3166 assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
3167 assert(begin.dtbgn_errarg == dtp->dt_errarg);
3168 dtp->dt_errhdlr = dt_consume_begin_error;
3169 dtp->dt_errarg = &begin;
3171 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3172 dt_consume_begin_probe, dt_consume_begin_record, &begin);
3174 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3175 dtp->dt_errarg = begin.dtbgn_errarg;
3182 dt_buf_oldest(void *elem, void *arg)
3184 dtrace_bufdesc_t *buf = elem;
3185 size_t offs = buf->dtbd_oldest;
3187 while (offs < buf->dtbd_size) {
3188 dtrace_rechdr_t *dtrh =
3189 /* LINTED - alignment */
3190 (dtrace_rechdr_t *)(buf->dtbd_data + offs);
3191 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
3192 offs += sizeof (dtrace_epid_t);
3194 return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh));
3198 /* There are no records left; use the time the buffer was retrieved. */
3199 return (buf->dtbd_timestamp);
3203 dtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
3204 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3206 dtrace_optval_t size;
3207 static int max_ncpus;
3209 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
3210 hrtime_t now = gethrtime();
3212 if (dtp->dt_lastswitch != 0) {
3213 if (now - dtp->dt_lastswitch < interval)
3216 dtp->dt_lastswitch += interval;
3218 dtp->dt_lastswitch = now;
3221 if (!dtp->dt_active)
3222 return (dt_set_errno(dtp, EINVAL));
3225 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3228 pf = (dtrace_consume_probe_f *)dt_nullprobe;
3231 rf = (dtrace_consume_rec_f *)dt_nullrec;
3233 if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) {
3235 * The output will not be in the order it was traced. Rather,
3236 * we will consume all of the data from each CPU's buffer in
3237 * turn. We apply special handling for the records from BEGIN
3238 * and END probes so that they are consumed first and last,
3241 * If we have just begun, we want to first process the CPU that
3242 * executed the BEGIN probe (if any).
3244 if (dtp->dt_active && dtp->dt_beganon != -1 &&
3245 (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0)
3248 for (i = 0; i < max_ncpus; i++) {
3249 dtrace_bufdesc_t *buf;
3252 * If we have stopped, we want to process the CPU on
3253 * which the END probe was processed only _after_ we
3254 * have processed everything else.
3256 if (dtp->dt_stopped && (i == dtp->dt_endedon))
3259 if (dt_get_buf(dtp, i, &buf) != 0)
3266 dtp->dt_prefix = NULL;
3267 rval = dt_consume_cpu(dtp, fp, i,
3268 buf, B_FALSE, pf, rf, arg);
3269 dt_put_buf(dtp, buf);
3273 if (dtp->dt_stopped) {
3274 dtrace_bufdesc_t *buf;
3276 if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0)
3281 rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon,
3282 buf, B_FALSE, pf, rf, arg);
3283 dt_put_buf(dtp, buf);
3288 * The output will be in the order it was traced (or for
3289 * speculations, when it was committed). We retrieve a buffer
3290 * from each CPU and put it into a priority queue, which sorts
3291 * based on the first entry in the buffer. This is sufficient
3292 * because entries within a buffer are already sorted.
3294 * We then consume records one at a time, always consuming the
3295 * oldest record, as determined by the priority queue. When
3296 * we reach the end of the time covered by these buffers,
3297 * we need to stop and retrieve more records on the next pass.
3298 * The kernel tells us the time covered by each buffer, in
3299 * dtbd_timestamp. The first buffer's timestamp tells us the
3300 * time covered by all buffers, as subsequently retrieved
3301 * buffers will cover to a more recent time.
3304 uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t));
3305 uint64_t first_timestamp = 0;
3307 dtrace_bufdesc_t *buf;
3309 bzero(drops, max_ncpus * sizeof (uint64_t));
3311 if (dtp->dt_bufq == NULL) {
3312 dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2,
3313 dt_buf_oldest, NULL);
3314 if (dtp->dt_bufq == NULL) /* ENOMEM */
3318 /* Retrieve data from each CPU. */
3319 (void) dtrace_getopt(dtp, "bufsize", &size);
3320 for (i = 0; i < max_ncpus; i++) {
3321 dtrace_bufdesc_t *buf;
3323 if (dt_get_buf(dtp, i, &buf) != 0)
3326 if (first_timestamp == 0)
3327 first_timestamp = buf->dtbd_timestamp;
3328 assert(buf->dtbd_timestamp >= first_timestamp);
3330 dt_pq_insert(dtp->dt_bufq, buf);
3331 drops[i] = buf->dtbd_drops;
3332 buf->dtbd_drops = 0;
3336 /* Consume records. */
3338 dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq);
3344 timestamp = dt_buf_oldest(buf, dtp);
3345 assert(timestamp >= dtp->dt_last_timestamp);
3346 dtp->dt_last_timestamp = timestamp;
3348 if (timestamp == buf->dtbd_timestamp) {
3350 * We've reached the end of the time covered
3351 * by this buffer. If this is the oldest
3352 * buffer, we must do another pass
3353 * to retrieve more data.
3355 dt_put_buf(dtp, buf);
3356 if (timestamp == first_timestamp &&
3362 if ((rval = dt_consume_cpu(dtp, fp,
3363 buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0)
3365 dt_pq_insert(dtp->dt_bufq, buf);
3368 /* Consume drops. */
3369 for (i = 0; i < max_ncpus; i++) {
3370 if (drops[i] != 0) {
3371 int error = dt_handle_cpudrop(dtp, i,
3372 DTRACEDROP_PRINCIPAL, drops[i]);
3379 * Reduce memory usage by re-allocating smaller buffers
3380 * for the "remnants".
3382 while (buf = dt_pq_walk(dtp->dt_bufq, &cookie))
3383 dt_realloc_buf(dtp, buf, buf->dtbd_size);