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.
42 #define DT_MASK_LO 0x00000000FFFFFFFFULL
45 * We declare this here because (1) we need it and (2) we want to avoid a
46 * dependency on libm in libdtrace.
49 dt_fabsl(long double x
)
58 dt_ndigits(long long val
)
64 val
= val
== INT64_MIN
? INT64_MAX
: -val
;
68 while (val
> cmp
&& cmp
> 0) {
73 return (rval
< 4 ? 4 : rval
);
77 * 128-bit arithmetic functions needed to support the stddev() aggregating
81 dt_gt_128(uint64_t *a
, uint64_t *b
)
83 return (a
[1] > b
[1] || (a
[1] == b
[1] && a
[0] > b
[0]));
87 dt_ge_128(uint64_t *a
, uint64_t *b
)
89 return (a
[1] > b
[1] || (a
[1] == b
[1] && a
[0] >= b
[0]));
93 dt_le_128(uint64_t *a
, uint64_t *b
)
95 return (a
[1] < b
[1] || (a
[1] == b
[1] && a
[0] <= b
[0]));
99 * Shift the 128-bit value in a by b. If b is positive, shift left.
100 * If b is negative, shift right.
103 dt_shift_128(uint64_t *a
, int b
)
113 a
[0] = a
[1] >> (b
- 64);
117 mask
= 1LL << (64 - b
);
119 a
[0] |= ((a
[1] & mask
) << (64 - b
));
124 a
[1] = a
[0] << (b
- 64);
128 mask
= a
[0] >> (64 - b
);
136 dt_nbits_128(uint64_t *a
)
140 uint64_t zero
[2] = { 0, 0 };
145 dt_shift_128(tmp
, -1);
146 while (dt_gt_128(tmp
, zero
)) {
147 dt_shift_128(tmp
, -1);
155 dt_subtract_128(uint64_t *minuend
, uint64_t *subtrahend
, uint64_t *difference
)
159 result
[0] = minuend
[0] - subtrahend
[0];
160 result
[1] = minuend
[1] - subtrahend
[1] -
161 (minuend
[0] < subtrahend
[0] ? 1 : 0);
163 difference
[0] = result
[0];
164 difference
[1] = result
[1];
168 dt_add_128(uint64_t *addend1
, uint64_t *addend2
, uint64_t *sum
)
172 result
[0] = addend1
[0] + addend2
[0];
173 result
[1] = addend1
[1] + addend2
[1] +
174 (result
[0] < addend1
[0] || result
[0] < addend2
[0] ? 1 : 0);
181 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
182 * use native multiplication on those, and then re-combine into the
183 * resulting 128-bit value.
185 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
192 dt_multiply_128(uint64_t factor1
, uint64_t factor2
, uint64_t *product
)
194 uint64_t hi1
, hi2
, lo1
, lo2
;
200 lo1
= factor1
& DT_MASK_LO
;
201 lo2
= factor2
& DT_MASK_LO
;
203 product
[0] = lo1
* lo2
;
204 product
[1] = hi1
* hi2
;
208 dt_shift_128(tmp
, 32);
209 dt_add_128(product
, tmp
, product
);
213 dt_shift_128(tmp
, 32);
214 dt_add_128(product
, tmp
, product
);
218 * This is long-hand division.
220 * We initialize subtrahend by shifting divisor left as far as possible. We
221 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we
222 * subtract and set the appropriate bit in the result. We then shift
223 * subtrahend right by one bit for the next comparison.
226 dt_divide_128(uint64_t *dividend
, uint64_t divisor
, uint64_t *quotient
)
228 uint64_t result
[2] = { 0, 0 };
229 uint64_t remainder
[2];
230 uint64_t subtrahend
[2];
231 uint64_t divisor_128
[2];
232 uint64_t mask
[2] = { 1, 0 };
235 assert(divisor
!= 0);
237 divisor_128
[0] = divisor
;
240 remainder
[0] = dividend
[0];
241 remainder
[1] = dividend
[1];
243 subtrahend
[0] = divisor
;
246 while (divisor
> 0) {
251 dt_shift_128(subtrahend
, 128 - log
);
252 dt_shift_128(mask
, 128 - log
);
254 while (dt_ge_128(remainder
, divisor_128
)) {
255 if (dt_ge_128(remainder
, subtrahend
)) {
256 dt_subtract_128(remainder
, subtrahend
, remainder
);
257 result
[0] |= mask
[0];
258 result
[1] |= mask
[1];
261 dt_shift_128(subtrahend
, -1);
262 dt_shift_128(mask
, -1);
265 quotient
[0] = result
[0];
266 quotient
[1] = result
[1];
270 * This is the long-hand method of calculating a square root.
271 * The algorithm is as follows:
273 * 1. Group the digits by 2 from the right.
274 * 2. Over the leftmost group, find the largest single-digit number
275 * whose square is less than that group.
276 * 3. Subtract the result of the previous step (2 or 4, depending) and
277 * bring down the next two-digit group.
278 * 4. For the result R we have so far, find the largest single-digit number
279 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
280 * (Note that this is doubling R and performing a decimal left-shift by 1
281 * and searching for the appropriate decimal to fill the one's place.)
282 * The value x is the next digit in the square root.
283 * Repeat steps 3 and 4 until the desired precision is reached. (We're
284 * dealing with integers, so the above is sufficient.)
286 * In decimal, the square root of 582,734 would be calculated as so:
290 * -49 (7^2 == 49 => 7 is the first digit in the square root)
292 * 9 27 (Subtract and bring down the next group.)
293 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
294 * ----- the square root)
295 * 51 34 (Subtract and bring down the next group.)
296 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
297 * ----- the square root)
300 * The above algorithm applies similarly in binary, but note that the
301 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
302 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
303 * preceding difference?
305 * In binary, the square root of 11011011 would be calculated as so:
309 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1)
312 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1)
315 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
318 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
322 dt_sqrt_128(uint64_t *square
)
324 uint64_t result
[2] = { 0, 0 };
325 uint64_t diff
[2] = { 0, 0 };
326 uint64_t one
[2] = { 1, 0 };
327 uint64_t next_pair
[2];
328 uint64_t next_try
[2];
329 uint64_t bit_pairs
, pair_shift
;
332 bit_pairs
= dt_nbits_128(square
) / 2;
333 pair_shift
= bit_pairs
* 2;
335 for (i
= 0; i
<= bit_pairs
; i
++) {
337 * Bring down the next pair of bits.
339 next_pair
[0] = square
[0];
340 next_pair
[1] = square
[1];
341 dt_shift_128(next_pair
, -pair_shift
);
345 dt_shift_128(diff
, 2);
346 dt_add_128(diff
, next_pair
, diff
);
349 * next_try = R << 2 + 1
351 next_try
[0] = result
[0];
352 next_try
[1] = result
[1];
353 dt_shift_128(next_try
, 2);
354 dt_add_128(next_try
, one
, next_try
);
356 if (dt_le_128(next_try
, diff
)) {
357 dt_subtract_128(diff
, next_try
, diff
);
358 dt_shift_128(result
, 1);
359 dt_add_128(result
, one
, result
);
361 dt_shift_128(result
, 1);
367 assert(result
[1] == 0);
373 dt_stddev(uint64_t *data
, uint64_t normal
)
375 uint64_t avg_of_squares
[2];
376 uint64_t square_of_avg
[2];
381 * The standard approximation for standard deviation is
382 * sqrt(average(x**2) - average(x)**2), i.e. the square root
383 * of the average of the squares minus the square of the average.
385 dt_divide_128(data
+ 2, normal
, avg_of_squares
);
386 dt_divide_128(avg_of_squares
, data
[0], avg_of_squares
);
388 norm_avg
= (int64_t)data
[1] / (int64_t)normal
/ (int64_t)data
[0];
391 norm_avg
= -norm_avg
;
393 dt_multiply_128((uint64_t)norm_avg
, (uint64_t)norm_avg
, square_of_avg
);
395 dt_subtract_128(avg_of_squares
, square_of_avg
, diff
);
397 return (dt_sqrt_128(diff
));
401 dt_flowindent(dtrace_hdl_t
*dtp
, dtrace_probedata_t
*data
, dtrace_epid_t last
,
402 dtrace_bufdesc_t
*buf
, size_t offs
)
404 dtrace_probedesc_t
*pd
= data
->dtpda_pdesc
, *npd
;
405 dtrace_eprobedesc_t
*epd
= data
->dtpda_edesc
, *nepd
;
406 char *p
= pd
->dtpd_provider
, *n
= pd
->dtpd_name
, *sub
;
407 dtrace_flowkind_t flow
= DTRACEFLOW_NONE
;
408 const char *str
= NULL
;
409 static const char *e_str
[2] = { " -> ", " => " };
410 static const char *r_str
[2] = { " <- ", " <= " };
411 static const char *ent
= "entry", *ret
= "return";
412 static int entlen
= 0, retlen
= 0;
413 dtrace_epid_t next
, id
= epd
->dtepd_epid
;
418 entlen
= strlen(ent
);
419 retlen
= strlen(ret
);
423 * If the name of the probe is "entry" or ends with "-entry", we
424 * treat it as an entry; if it is "return" or ends with "-return",
425 * we treat it as a return. (This allows application-provided probes
426 * like "method-entry" or "function-entry" to participate in flow
427 * indentation -- without accidentally misinterpreting popular probe
428 * names like "carpentry", "gentry" or "Coventry".)
430 if ((sub
= strstr(n
, ent
)) != NULL
&& sub
[entlen
] == '\0' &&
431 (sub
== n
|| sub
[-1] == '-')) {
432 flow
= DTRACEFLOW_ENTRY
;
433 str
= e_str
[strcmp(p
, "syscall") == 0];
434 } else if ((sub
= strstr(n
, ret
)) != NULL
&& sub
[retlen
] == '\0' &&
435 (sub
== n
|| sub
[-1] == '-')) {
436 flow
= DTRACEFLOW_RETURN
;
437 str
= r_str
[strcmp(p
, "syscall") == 0];
441 * If we're going to indent this, we need to check the ID of our last
442 * call. If we're looking at the same probe ID but a different EPID,
443 * we _don't_ want to indent. (Yes, there are some minor holes in
444 * this scheme -- it's a heuristic.)
446 if (flow
== DTRACEFLOW_ENTRY
) {
447 if ((last
!= DTRACE_EPIDNONE
&& id
!= last
&&
448 pd
->dtpd_id
== dtp
->dt_pdesc
[last
]->dtpd_id
))
449 flow
= DTRACEFLOW_NONE
;
453 * If we're going to unindent this, it's more difficult to see if
454 * we don't actually want to unindent it -- we need to look at the
457 if (flow
== DTRACEFLOW_RETURN
) {
458 offs
+= epd
->dtepd_size
;
461 if (offs
>= buf
->dtbd_size
)
464 next
= *(uint32_t *)((uintptr_t)buf
->dtbd_data
+ offs
);
466 if (next
== DTRACE_EPIDNONE
)
468 } while (next
== DTRACE_EPIDNONE
);
470 if ((rval
= dt_epid_lookup(dtp
, next
, &nepd
, &npd
)) != 0)
473 if (next
!= id
&& npd
->dtpd_id
== pd
->dtpd_id
)
474 flow
= DTRACEFLOW_NONE
;
478 if (flow
== DTRACEFLOW_ENTRY
|| flow
== DTRACEFLOW_RETURN
) {
479 data
->dtpda_prefix
= str
;
481 data
->dtpda_prefix
= "| ";
484 if (flow
== DTRACEFLOW_RETURN
&& data
->dtpda_indent
> 0)
485 data
->dtpda_indent
-= 2;
487 data
->dtpda_flow
= flow
;
495 return (DTRACE_CONSUME_THIS
);
501 return (DTRACE_CONSUME_NEXT
);
505 dt_quantize_total(dtrace_hdl_t
*dtp
, int64_t datum
, long double *total
)
507 long double val
= dt_fabsl((long double)datum
);
509 if (dtp
->dt_options
[DTRACEOPT_AGGZOOM
] == DTRACEOPT_UNSET
) {
515 * If we're zooming in on an aggregation, we want the height of the
516 * highest value to be approximately 95% of total bar height -- so we
517 * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
520 val
*= 1 / DTRACE_AGGZOOM_MAX
;
527 dt_print_quanthdr(dtrace_hdl_t
*dtp
, FILE *fp
, int width
)
529 return (dt_printf(dtp
, fp
, "\n%*s %41s %-9s\n",
530 width
? width
: 16, width
? "key" : "value",
531 "------------- Distribution -------------", "count"));
535 dt_print_quanthdr_packed(dtrace_hdl_t
*dtp
, FILE *fp
, int width
,
536 const dtrace_aggdata_t
*aggdata
, dtrace_actkind_t action
)
538 int min
= aggdata
->dtada_minbin
, max
= aggdata
->dtada_maxbin
;
539 int minwidth
, maxwidth
, i
;
541 assert(action
== DTRACEAGG_QUANTIZE
|| action
== DTRACEAGG_LQUANTIZE
);
543 if (action
== DTRACEAGG_QUANTIZE
) {
544 if (min
!= 0 && min
!= DTRACE_QUANTIZE_ZEROBUCKET
)
547 if (max
< DTRACE_QUANTIZE_NBUCKETS
- 1)
550 minwidth
= dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min
));
551 maxwidth
= dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max
));
554 minwidth
= maxwidth
- 1;
558 if (dt_printf(dtp
, fp
, "\n%*s %*s .",
559 width
, width
> 0 ? "key" : "", minwidth
, "min") < 0)
562 for (i
= min
; i
<= max
; i
++) {
563 if (dt_printf(dtp
, fp
, "-") < 0)
567 return (dt_printf(dtp
, fp
, ". %*s | count\n", -maxwidth
, "max"));
571 * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
572 * inclusive) to represent aggregations via UTF-8 -- which are expressed via
573 * 3-byte UTF-8 sequences.
575 #define DTRACE_AGGUTF8_FULL 0x2588
576 #define DTRACE_AGGUTF8_BASE 0x258f
577 #define DTRACE_AGGUTF8_LEVELS 8
579 #define DTRACE_AGGUTF8_BYTE0(val) (0xe0 | ((val) >> 12))
580 #define DTRACE_AGGUTF8_BYTE1(val) (0x80 | (((val) >> 6) & 0x3f))
581 #define DTRACE_AGGUTF8_BYTE2(val) (0x80 | ((val) & 0x3f))
584 dt_print_quantline_utf8(dtrace_hdl_t
*dtp
, FILE *fp
, int64_t val
,
585 uint64_t normal
, long double total
)
587 uint_t len
= 40, i
, whole
, partial
;
588 long double f
= (dt_fabsl((long double)val
) * len
) / total
;
589 const char *spaces
= " ";
592 partial
= (uint_t
)((f
- (long double)(uint_t
)f
) *
593 (long double)DTRACE_AGGUTF8_LEVELS
);
595 if (dt_printf(dtp
, fp
, "|") < 0)
598 for (i
= 0; i
< whole
; i
++) {
599 if (dt_printf(dtp
, fp
, "%c%c%c",
600 DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL
),
601 DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL
),
602 DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL
)) < 0)
607 partial
= DTRACE_AGGUTF8_BASE
- (partial
- 1);
609 if (dt_printf(dtp
, fp
, "%c%c%c",
610 DTRACE_AGGUTF8_BYTE0(partial
),
611 DTRACE_AGGUTF8_BYTE1(partial
),
612 DTRACE_AGGUTF8_BYTE2(partial
)) < 0)
618 return (dt_printf(dtp
, fp
, "%s %-9lld\n", spaces
+ i
,
619 (long long)val
/ normal
));
623 dt_print_quantline(dtrace_hdl_t
*dtp
, FILE *fp
, int64_t val
,
624 uint64_t normal
, long double total
, char positives
, char negatives
)
627 uint_t depth
, len
= 40;
629 const char *ats
= "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
630 const char *spaces
= " ";
632 assert(strlen(ats
) == len
&& strlen(spaces
) == len
);
633 assert(!(total
== 0 && (positives
|| negatives
)));
634 assert(!(val
< 0 && !negatives
));
635 assert(!(val
> 0 && !positives
));
636 assert(!(val
!= 0 && total
== 0));
640 if (dtp
->dt_encoding
== DT_ENCODING_UTF8
) {
641 return (dt_print_quantline_utf8(dtp
, fp
, val
,
645 f
= (dt_fabsl((long double)val
) * len
) / total
;
646 depth
= (uint_t
)(f
+ 0.5);
651 return (dt_printf(dtp
, fp
, "|%s%s %-9lld\n", ats
+ len
- depth
,
652 spaces
+ depth
, (long long)val
/ normal
));
656 f
= (dt_fabsl((long double)val
) * len
) / total
;
657 depth
= (uint_t
)(f
+ 0.5);
659 return (dt_printf(dtp
, fp
, "%s%s| %-9lld\n", spaces
+ depth
,
660 ats
+ len
- depth
, (long long)val
/ normal
));
664 * If we're here, we have both positive and negative bucket values.
665 * To express this graphically, we're going to generate both positive
666 * and negative bars separated by a centerline. These bars are half
667 * the size of normal quantize()/lquantize() bars, so we divide the
668 * length in half before calculating the bar length.
672 spaces
= &spaces
[len
];
674 f
= (dt_fabsl((long double)val
) * len
) / total
;
675 depth
= (uint_t
)(f
+ 0.5);
678 return (dt_printf(dtp
, fp
, "%s%s|%*s %-9lld\n", spaces
+ depth
,
679 ats
+ len
- depth
, len
, "", (long long)val
/ normal
));
681 return (dt_printf(dtp
, fp
, "%20s|%s%s %-9lld\n", "",
682 ats
+ len
- depth
, spaces
+ depth
,
683 (long long)val
/ normal
));
688 * As with UTF-8 printing of aggregations, we use a subset of the Unicode
689 * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
692 #define DTRACE_AGGPACK_BASE 0x2581
693 #define DTRACE_AGGPACK_LEVELS 8
696 dt_print_packed(dtrace_hdl_t
*dtp
, FILE *fp
,
697 long double datum
, long double total
)
699 static boolean_t utf8_checked
= B_FALSE
;
700 static boolean_t utf8
;
701 char *ascii
= "__xxxxXX";
702 char *neg
= "vvvvVV";
710 * We want to determine if we can reasonably emit UTF-8 for our
711 * packed aggregation. To do this, we will check for terminals
712 * that are known to be primitive to emit UTF-8 on these.
714 utf8_checked
= B_TRUE
;
716 if (dtp
->dt_encoding
== DT_ENCODING_ASCII
) {
718 } else if (dtp
->dt_encoding
== DT_ENCODING_UTF8
) {
720 } else if ((term
= getenv("TERM")) != NULL
&&
721 (strcmp(term
, "sun") == 0 ||
722 strcmp(term
, "sun-color") == 0) ||
723 strcmp(term
, "dumb") == 0) {
731 return (dt_printf(dtp
, fp
, " "));
735 val
= dt_fabsl(datum
* (len
- 1)) / total
;
736 return (dt_printf(dtp
, fp
, "%c", neg
[(uint_t
)(val
+ 0.5)]));
740 int block
= DTRACE_AGGPACK_BASE
+ (unsigned int)(((datum
*
741 (DTRACE_AGGPACK_LEVELS
- 1)) / total
) + 0.5);
743 return (dt_printf(dtp
, fp
, "%c%c%c",
744 DTRACE_AGGUTF8_BYTE0(block
),
745 DTRACE_AGGUTF8_BYTE1(block
),
746 DTRACE_AGGUTF8_BYTE2(block
)));
750 val
= (datum
* (len
- 1)) / total
;
751 return (dt_printf(dtp
, fp
, "%c", ascii
[(uint_t
)(val
+ 0.5)]));
755 dt_print_quantize(dtrace_hdl_t
*dtp
, FILE *fp
, const void *addr
,
756 size_t size
, uint64_t normal
)
758 const int64_t *data
= addr
;
759 int i
, first_bin
= 0, last_bin
= DTRACE_QUANTIZE_NBUCKETS
- 1;
760 long double total
= 0;
761 char positives
= 0, negatives
= 0;
763 if (size
!= DTRACE_QUANTIZE_NBUCKETS
* sizeof (uint64_t))
764 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
766 while (first_bin
< DTRACE_QUANTIZE_NBUCKETS
- 1 && data
[first_bin
] == 0)
769 if (first_bin
== DTRACE_QUANTIZE_NBUCKETS
- 1) {
771 * There isn't any data. This is possible if the aggregation
772 * has been clear()'d or if negative increment values have been
773 * used. Regardless, we'll print the buckets around 0.
775 first_bin
= DTRACE_QUANTIZE_ZEROBUCKET
- 1;
776 last_bin
= DTRACE_QUANTIZE_ZEROBUCKET
+ 1;
781 while (last_bin
> 0 && data
[last_bin
] == 0)
784 if (last_bin
< DTRACE_QUANTIZE_NBUCKETS
- 1)
788 for (i
= first_bin
; i
<= last_bin
; i
++) {
789 positives
|= (data
[i
] > 0);
790 negatives
|= (data
[i
] < 0);
791 dt_quantize_total(dtp
, data
[i
], &total
);
794 if (dt_print_quanthdr(dtp
, fp
, 0) < 0)
797 for (i
= first_bin
; i
<= last_bin
; i
++) {
798 if (dt_printf(dtp
, fp
, "%16lld ",
799 (long long)DTRACE_QUANTIZE_BUCKETVAL(i
)) < 0)
802 if (dt_print_quantline(dtp
, fp
, data
[i
], normal
, total
,
803 positives
, negatives
) < 0)
811 dt_print_quantize_packed(dtrace_hdl_t
*dtp
, FILE *fp
, const void *addr
,
812 size_t size
, const dtrace_aggdata_t
*aggdata
)
814 const int64_t *data
= addr
;
815 long double total
= 0, count
= 0;
816 int min
= aggdata
->dtada_minbin
, max
= aggdata
->dtada_maxbin
, i
;
817 int64_t minval
, maxval
;
819 if (size
!= DTRACE_QUANTIZE_NBUCKETS
* sizeof (uint64_t))
820 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
822 if (min
!= 0 && min
!= DTRACE_QUANTIZE_ZEROBUCKET
)
825 if (max
< DTRACE_QUANTIZE_NBUCKETS
- 1)
828 minval
= DTRACE_QUANTIZE_BUCKETVAL(min
);
829 maxval
= DTRACE_QUANTIZE_BUCKETVAL(max
);
831 if (dt_printf(dtp
, fp
, " %*lld :", dt_ndigits(minval
),
832 (long long)minval
) < 0)
835 for (i
= min
; i
<= max
; i
++) {
836 dt_quantize_total(dtp
, data
[i
], &total
);
840 for (i
= min
; i
<= max
; i
++) {
841 if (dt_print_packed(dtp
, fp
, data
[i
], total
) < 0)
845 if (dt_printf(dtp
, fp
, ": %*lld | %lld\n",
846 -dt_ndigits(maxval
), (long long)maxval
, (long long)count
) < 0)
853 dt_print_lquantize(dtrace_hdl_t
*dtp
, FILE *fp
, const void *addr
,
854 size_t size
, uint64_t normal
)
856 const int64_t *data
= addr
;
857 int i
, first_bin
, last_bin
, base
;
859 long double total
= 0;
860 uint16_t step
, levels
;
861 char positives
= 0, negatives
= 0;
863 if (size
< sizeof (uint64_t))
864 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
867 size
-= sizeof (uint64_t);
869 base
= DTRACE_LQUANTIZE_BASE(arg
);
870 step
= DTRACE_LQUANTIZE_STEP(arg
);
871 levels
= DTRACE_LQUANTIZE_LEVELS(arg
);
874 last_bin
= levels
+ 1;
876 if (size
!= sizeof (uint64_t) * (levels
+ 2))
877 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
879 while (first_bin
<= levels
+ 1 && data
[first_bin
] == 0)
882 if (first_bin
> levels
+ 1) {
889 while (last_bin
> 0 && data
[last_bin
] == 0)
892 if (last_bin
< levels
+ 1)
896 for (i
= first_bin
; i
<= last_bin
; i
++) {
897 positives
|= (data
[i
] > 0);
898 negatives
|= (data
[i
] < 0);
899 dt_quantize_total(dtp
, data
[i
], &total
);
902 if (dt_printf(dtp
, fp
, "\n%16s %41s %-9s\n", "value",
903 "------------- Distribution -------------", "count") < 0)
906 for (i
= first_bin
; i
<= last_bin
; i
++) {
911 (void) snprintf(c
, sizeof (c
), "< %d", base
);
912 err
= dt_printf(dtp
, fp
, "%16s ", c
);
913 } else if (i
== levels
+ 1) {
914 (void) snprintf(c
, sizeof (c
), ">= %d",
915 base
+ (levels
* step
));
916 err
= dt_printf(dtp
, fp
, "%16s ", c
);
918 err
= dt_printf(dtp
, fp
, "%16d ",
919 base
+ (i
- 1) * step
);
922 if (err
< 0 || dt_print_quantline(dtp
, fp
, data
[i
], normal
,
923 total
, positives
, negatives
) < 0)
932 dt_print_lquantize_packed(dtrace_hdl_t
*dtp
, FILE *fp
, const void *addr
,
933 size_t size
, const dtrace_aggdata_t
*aggdata
)
935 const int64_t *data
= addr
;
936 long double total
= 0, count
= 0;
937 int min
, max
, base
, err
;
939 uint16_t step
, levels
;
943 if (size
< sizeof (uint64_t))
944 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
947 size
-= sizeof (uint64_t);
949 base
= DTRACE_LQUANTIZE_BASE(arg
);
950 step
= DTRACE_LQUANTIZE_STEP(arg
);
951 levels
= DTRACE_LQUANTIZE_LEVELS(arg
);
953 if (size
!= sizeof (uint64_t) * (levels
+ 2))
954 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
960 (void) snprintf(c
, sizeof (c
), "< %d", base
);
961 err
= dt_printf(dtp
, fp
, "%8s :", c
);
963 err
= dt_printf(dtp
, fp
, "%8d :", base
+ (min
- 1) * step
);
969 for (i
= min
; i
<= max
; i
++) {
970 dt_quantize_total(dtp
, data
[i
], &total
);
974 for (i
= min
; i
<= max
; i
++) {
975 if (dt_print_packed(dtp
, fp
, data
[i
], total
) < 0)
979 (void) snprintf(c
, sizeof (c
), ">= %d", base
+ (levels
* step
));
980 return (dt_printf(dtp
, fp
, ": %-8s | %lld\n", c
, (long long)count
));
984 dt_print_llquantize(dtrace_hdl_t
*dtp
, FILE *fp
, const void *addr
,
985 size_t size
, uint64_t normal
)
987 int i
, first_bin
, last_bin
, bin
= 1, order
, levels
;
988 uint16_t factor
, low
, high
, nsteps
;
989 const int64_t *data
= addr
;
990 int64_t value
= 1, next
, step
;
991 char positives
= 0, negatives
= 0;
992 long double total
= 0;
996 if (size
< sizeof (uint64_t))
997 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1000 size
-= sizeof (uint64_t);
1002 factor
= DTRACE_LLQUANTIZE_FACTOR(arg
);
1003 low
= DTRACE_LLQUANTIZE_LOW(arg
);
1004 high
= DTRACE_LLQUANTIZE_HIGH(arg
);
1005 nsteps
= DTRACE_LLQUANTIZE_NSTEP(arg
);
1008 * We don't expect to be handed invalid llquantize() parameters here,
1009 * but sanity check them (to a degree) nonetheless.
1011 if (size
> INT32_MAX
|| factor
< 2 || low
>= high
||
1012 nsteps
== 0 || factor
> nsteps
)
1013 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1015 levels
= (int)size
/ sizeof (uint64_t);
1018 last_bin
= levels
- 1;
1020 while (first_bin
< levels
&& data
[first_bin
] == 0)
1023 if (first_bin
== levels
) {
1030 while (last_bin
> 0 && data
[last_bin
] == 0)
1033 if (last_bin
< levels
- 1)
1037 for (i
= first_bin
; i
<= last_bin
; i
++) {
1038 positives
|= (data
[i
] > 0);
1039 negatives
|= (data
[i
] < 0);
1040 dt_quantize_total(dtp
, data
[i
], &total
);
1043 if (dt_printf(dtp
, fp
, "\n%16s %41s %-9s\n", "value",
1044 "------------- Distribution -------------", "count") < 0)
1047 for (order
= 0; order
< low
; order
++)
1050 next
= value
* factor
;
1051 step
= next
> nsteps
? next
/ nsteps
: 1;
1053 if (first_bin
== 0) {
1054 (void) snprintf(c
, sizeof (c
), "< %lld", value
);
1056 if (dt_printf(dtp
, fp
, "%16s ", c
) < 0)
1059 if (dt_print_quantline(dtp
, fp
, data
[0], normal
,
1060 total
, positives
, negatives
) < 0)
1064 while (order
<= high
) {
1065 if (bin
>= first_bin
&& bin
<= last_bin
) {
1066 if (dt_printf(dtp
, fp
, "%16lld ", (long long)value
) < 0)
1069 if (dt_print_quantline(dtp
, fp
, data
[bin
],
1070 normal
, total
, positives
, negatives
) < 0)
1074 assert(value
< next
);
1077 if ((value
+= step
) != next
)
1080 next
= value
* factor
;
1081 step
= next
> nsteps
? next
/ nsteps
: 1;
1088 assert(last_bin
== bin
);
1089 (void) snprintf(c
, sizeof (c
), ">= %lld", value
);
1091 if (dt_printf(dtp
, fp
, "%16s ", c
) < 0)
1094 return (dt_print_quantline(dtp
, fp
, data
[bin
], normal
,
1095 total
, positives
, negatives
));
1100 dt_print_average(dtrace_hdl_t
*dtp
, FILE *fp
, caddr_t addr
,
1101 size_t size
, uint64_t normal
)
1103 /* LINTED - alignment */
1104 int64_t *data
= (int64_t *)addr
;
1106 return (dt_printf(dtp
, fp
, " %16lld", data
[0] ?
1107 (long long)(data
[1] / (int64_t)normal
/ data
[0]) : 0));
1112 dt_print_stddev(dtrace_hdl_t
*dtp
, FILE *fp
, caddr_t addr
,
1113 size_t size
, uint64_t normal
)
1115 /* LINTED - alignment */
1116 uint64_t *data
= (uint64_t *)addr
;
1118 return (dt_printf(dtp
, fp
, " %16llu", data
[0] ?
1119 (unsigned long long) dt_stddev(data
, normal
) : 0));
1124 dt_print_bytes(dtrace_hdl_t
*dtp
, FILE *fp
, caddr_t addr
,
1125 size_t nbytes
, int width
, int quiet
, int forceraw
)
1128 * If the byte stream is a series of printable characters, followed by
1129 * a terminating byte, we print it out as a string. Otherwise, we
1130 * assume that it's something else and just print the bytes.
1132 int i
, j
, margin
= 5;
1133 char *c
= (char *)addr
;
1141 if (dtp
->dt_options
[DTRACEOPT_RAWBYTES
] != DTRACEOPT_UNSET
)
1144 for (i
= 0; i
< nbytes
; i
++) {
1146 * We define a "printable character" to be one for which
1147 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
1148 * or a character which is either backspace or the bell.
1149 * Backspace and the bell are regrettably special because
1150 * they fail the first two tests -- and yet they are entirely
1151 * printable. These are the only two control characters that
1152 * have meaning for the terminal and for which isprint(3C) and
1153 * isspace(3C) return 0.
1155 if (isprint(c
[i
]) || isspace(c
[i
]) ||
1156 c
[i
] == '\b' || c
[i
] == '\a')
1159 if (c
[i
] == '\0' && i
> 0) {
1161 * This looks like it might be a string. Before we
1162 * assume that it is indeed a string, check the
1163 * remainder of the byte range; if it contains
1164 * additional non-nul characters, we'll assume that
1165 * it's a binary stream that just happens to look like
1166 * a string, and we'll print out the individual bytes.
1168 for (j
= i
+ 1; j
< nbytes
; j
++) {
1177 return (dt_printf(dtp
, fp
, "%s", c
));
1179 return (dt_printf(dtp
, fp
, " %s%*s",
1180 width
< 0 ? " " : "", width
, c
));
1189 * The byte range is all printable characters, but there is
1190 * no trailing nul byte. We'll assume that it's a string and
1193 char *s
= alloca(nbytes
+ 1);
1194 bcopy(c
, s
, nbytes
);
1196 return (dt_printf(dtp
, fp
, " %-*s", width
, s
));
1200 if (dt_printf(dtp
, fp
, "\n%*s ", margin
, "") < 0)
1203 for (i
= 0; i
< 16; i
++)
1204 if (dt_printf(dtp
, fp
, " %c", "0123456789abcdef"[i
]) < 0)
1207 if (dt_printf(dtp
, fp
, " 0123456789abcdef\n") < 0)
1211 for (i
= 0; i
< nbytes
; i
+= 16) {
1212 if (dt_printf(dtp
, fp
, "%*s%5x:", margin
, "", i
) < 0)
1215 for (j
= i
; j
< i
+ 16 && j
< nbytes
; j
++) {
1216 if (dt_printf(dtp
, fp
, " %02x", (uchar_t
)c
[j
]) < 0)
1221 if (dt_printf(dtp
, fp
, " ") < 0)
1225 if (dt_printf(dtp
, fp
, " ") < 0)
1228 for (j
= i
; j
< i
+ 16 && j
< nbytes
; j
++) {
1229 if (dt_printf(dtp
, fp
, "%c",
1230 c
[j
] < ' ' || c
[j
] > '~' ? '.' : c
[j
]) < 0)
1234 if (dt_printf(dtp
, fp
, "\n") < 0)
1242 dt_print_stack(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1243 caddr_t addr
, int depth
, int size
)
1245 dtrace_syminfo_t dts
;
1248 char c
[PATH_MAX
* 2];
1251 if (dt_printf(dtp
, fp
, "\n") < 0)
1257 if (dtp
->dt_options
[DTRACEOPT_STACKINDENT
] != DTRACEOPT_UNSET
)
1258 indent
= (int)dtp
->dt_options
[DTRACEOPT_STACKINDENT
];
1260 indent
= _dtrace_stkindent
;
1262 for (i
= 0; i
< depth
; i
++) {
1264 case sizeof (uint32_t):
1265 /* LINTED - alignment */
1266 pc
= *((uint32_t *)addr
);
1269 case sizeof (uint64_t):
1270 /* LINTED - alignment */
1271 pc
= *((uint64_t *)addr
);
1275 return (dt_set_errno(dtp
, EDT_BADSTACKPC
));
1283 if (dt_printf(dtp
, fp
, "%*s", indent
, "") < 0)
1286 if (dtrace_lookup_by_addr(dtp
, pc
, &sym
, &dts
) == 0) {
1287 if (pc
> sym
.st_value
) {
1288 (void) snprintf(c
, sizeof (c
), "%s`%s+0x%llx",
1289 dts
.dts_object
, dts
.dts_name
,
1292 (void) snprintf(c
, sizeof (c
), "%s`%s",
1293 dts
.dts_object
, dts
.dts_name
);
1297 * We'll repeat the lookup, but this time we'll specify
1298 * a NULL GElf_Sym -- indicating that we're only
1299 * interested in the containing module.
1301 if (dtrace_lookup_by_addr(dtp
, pc
, NULL
, &dts
) == 0) {
1302 (void) snprintf(c
, sizeof (c
), "%s`0x%llx",
1303 dts
.dts_object
, pc
);
1305 (void) snprintf(c
, sizeof (c
), "0x%llx", pc
);
1309 if (dt_printf(dtp
, fp
, format
, c
) < 0)
1312 if (dt_printf(dtp
, fp
, "\n") < 0)
1320 dt_print_ustack(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1321 caddr_t addr
, uint64_t arg
)
1323 /* LINTED - alignment */
1324 uint64_t *pc
= (uint64_t *)addr
;
1325 uint32_t depth
= DTRACE_USTACK_NFRAMES(arg
);
1326 uint32_t strsize
= DTRACE_USTACK_STRSIZE(arg
);
1327 const char *strbase
= addr
+ (depth
+ 1) * sizeof (uint64_t);
1328 const char *str
= strsize
? strbase
: NULL
;
1331 char name
[PATH_MAX
], objname
[PATH_MAX
], c
[PATH_MAX
* 2];
1332 struct ps_prochandle
*P
;
1342 if (dt_printf(dtp
, fp
, "\n") < 0)
1348 if (dtp
->dt_options
[DTRACEOPT_STACKINDENT
] != DTRACEOPT_UNSET
)
1349 indent
= (int)dtp
->dt_options
[DTRACEOPT_STACKINDENT
];
1351 indent
= _dtrace_stkindent
;
1354 * Ultimately, we need to add an entry point in the library vector for
1355 * determining <symbol, offset> from <pid, address>. For now, if
1356 * this is a vector open, we just print the raw address or string.
1358 if (dtp
->dt_vector
== NULL
)
1359 P
= dt_proc_grab(dtp
, pid
, PGRAB_RDONLY
| PGRAB_FORCE
, 0);
1364 dt_proc_lock(dtp
, P
); /* lock handle while we perform lookups */
1366 for (i
= 0; i
< depth
&& pc
[i
] != NULL
; i
++) {
1369 if ((err
= dt_printf(dtp
, fp
, "%*s", indent
, "")) < 0)
1372 if (P
!= NULL
&& Plookup_by_addr(P
, pc
[i
],
1373 name
, sizeof (name
), &sym
) == 0) {
1374 (void) Pobjname(P
, pc
[i
], objname
, sizeof (objname
));
1376 if (pc
[i
] > sym
.st_value
) {
1377 (void) snprintf(c
, sizeof (c
),
1378 "%s`%s+0x%llx", dt_basename(objname
), name
,
1379 (u_longlong_t
)(pc
[i
] - sym
.st_value
));
1381 (void) snprintf(c
, sizeof (c
),
1382 "%s`%s", dt_basename(objname
), name
);
1384 } else if (str
!= NULL
&& str
[0] != '\0' && str
[0] != '@' &&
1385 (P
!= NULL
&& ((map
= Paddr_to_map(P
, pc
[i
])) == NULL
||
1386 (map
->pr_mflags
& MA_WRITE
)))) {
1388 * If the current string pointer in the string table
1389 * does not point to an empty string _and_ the program
1390 * counter falls in a writable region, we'll use the
1391 * string from the string table instead of the raw
1392 * address. This last condition is necessary because
1393 * some (broken) ustack helpers will return a string
1394 * even for a program counter that they can't
1395 * identify. If we have a string for a program
1396 * counter that falls in a segment that isn't
1397 * writable, we assume that we have fallen into this
1398 * case and we refuse to use the string.
1400 (void) snprintf(c
, sizeof (c
), "%s", str
);
1402 if (P
!= NULL
&& Pobjname(P
, pc
[i
], objname
,
1403 sizeof (objname
)) != NULL
) {
1404 (void) snprintf(c
, sizeof (c
), "%s`0x%llx",
1405 dt_basename(objname
), (u_longlong_t
)pc
[i
]);
1407 (void) snprintf(c
, sizeof (c
), "0x%llx",
1408 (u_longlong_t
)pc
[i
]);
1412 if ((err
= dt_printf(dtp
, fp
, format
, c
)) < 0)
1415 if ((err
= dt_printf(dtp
, fp
, "\n")) < 0)
1418 if (str
!= NULL
&& str
[0] == '@') {
1420 * If the first character of the string is an "at" sign,
1421 * then the string is inferred to be an annotation --
1422 * and it is printed out beneath the frame and offset
1425 if ((err
= dt_printf(dtp
, fp
, "%*s", indent
, "")) < 0)
1428 (void) snprintf(c
, sizeof (c
), " [ %s ]", &str
[1]);
1430 if ((err
= dt_printf(dtp
, fp
, format
, c
)) < 0)
1433 if ((err
= dt_printf(dtp
, fp
, "\n")) < 0)
1438 str
+= strlen(str
) + 1;
1439 if (str
- strbase
>= strsize
)
1445 dt_proc_unlock(dtp
, P
);
1446 dt_proc_release(dtp
, P
);
1453 dt_print_usym(dtrace_hdl_t
*dtp
, FILE *fp
, caddr_t addr
, dtrace_actkind_t act
)
1455 /* LINTED - alignment */
1456 uint64_t pid
= ((uint64_t *)addr
)[0];
1457 /* LINTED - alignment */
1458 uint64_t pc
= ((uint64_t *)addr
)[1];
1459 const char *format
= " %-50s";
1463 if (act
== DTRACEACT_USYM
&& dtp
->dt_vector
== NULL
) {
1464 struct ps_prochandle
*P
;
1466 if ((P
= dt_proc_grab(dtp
, pid
,
1467 PGRAB_RDONLY
| PGRAB_FORCE
, 0)) != NULL
) {
1470 dt_proc_lock(dtp
, P
);
1472 if (Plookup_by_addr(P
, pc
, NULL
, 0, &sym
) == 0)
1475 dt_proc_unlock(dtp
, P
);
1476 dt_proc_release(dtp
, P
);
1483 } while ((len
= dtrace_uaddr2str(dtp
, pid
, pc
, s
, n
)) > n
);
1485 return (dt_printf(dtp
, fp
, format
, s
));
1489 dt_print_umod(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
, caddr_t addr
)
1491 /* LINTED - alignment */
1492 uint64_t pid
= ((uint64_t *)addr
)[0];
1493 /* LINTED - alignment */
1494 uint64_t pc
= ((uint64_t *)addr
)[1];
1497 char objname
[PATH_MAX
], c
[PATH_MAX
* 2];
1498 struct ps_prochandle
*P
;
1504 * See the comment in dt_print_ustack() for the rationale for
1505 * printing raw addresses in the vectored case.
1507 if (dtp
->dt_vector
== NULL
)
1508 P
= dt_proc_grab(dtp
, pid
, PGRAB_RDONLY
| PGRAB_FORCE
, 0);
1513 dt_proc_lock(dtp
, P
); /* lock handle while we perform lookups */
1515 if (P
!= NULL
&& Pobjname(P
, pc
, objname
, sizeof (objname
)) != NULL
) {
1516 (void) snprintf(c
, sizeof (c
), "%s", dt_basename(objname
));
1518 (void) snprintf(c
, sizeof (c
), "0x%llx", (u_longlong_t
)pc
);
1521 err
= dt_printf(dtp
, fp
, format
, c
);
1524 dt_proc_unlock(dtp
, P
);
1525 dt_proc_release(dtp
, P
);
1532 dt_print_sym(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
, caddr_t addr
)
1534 /* LINTED - alignment */
1535 uint64_t pc
= *((uint64_t *)addr
);
1536 dtrace_syminfo_t dts
;
1538 char c
[PATH_MAX
* 2];
1543 if (dtrace_lookup_by_addr(dtp
, pc
, &sym
, &dts
) == 0) {
1544 (void) snprintf(c
, sizeof (c
), "%s`%s",
1545 dts
.dts_object
, dts
.dts_name
);
1548 * We'll repeat the lookup, but this time we'll specify a
1549 * NULL GElf_Sym -- indicating that we're only interested in
1550 * the containing module.
1552 if (dtrace_lookup_by_addr(dtp
, pc
, NULL
, &dts
) == 0) {
1553 (void) snprintf(c
, sizeof (c
), "%s`0x%llx",
1554 dts
.dts_object
, (u_longlong_t
)pc
);
1556 (void) snprintf(c
, sizeof (c
), "0x%llx",
1561 if (dt_printf(dtp
, fp
, format
, c
) < 0)
1568 dt_print_mod(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
, caddr_t addr
)
1570 /* LINTED - alignment */
1571 uint64_t pc
= *((uint64_t *)addr
);
1572 dtrace_syminfo_t dts
;
1573 char c
[PATH_MAX
* 2];
1578 if (dtrace_lookup_by_addr(dtp
, pc
, NULL
, &dts
) == 0) {
1579 (void) snprintf(c
, sizeof (c
), "%s", dts
.dts_object
);
1581 (void) snprintf(c
, sizeof (c
), "0x%llx", (u_longlong_t
)pc
);
1584 if (dt_printf(dtp
, fp
, format
, c
) < 0)
1590 typedef struct dt_normal
{
1591 dtrace_aggvarid_t dtnd_id
;
1592 uint64_t dtnd_normal
;
1596 dt_normalize_agg(const dtrace_aggdata_t
*aggdata
, void *arg
)
1598 dt_normal_t
*normal
= arg
;
1599 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1600 dtrace_aggvarid_t id
= normal
->dtnd_id
;
1602 if (agg
->dtagd_nrecs
== 0)
1603 return (DTRACE_AGGWALK_NEXT
);
1605 if (agg
->dtagd_varid
!= id
)
1606 return (DTRACE_AGGWALK_NEXT
);
1608 ((dtrace_aggdata_t
*)aggdata
)->dtada_normal
= normal
->dtnd_normal
;
1609 return (DTRACE_AGGWALK_NORMALIZE
);
1613 dt_normalize(dtrace_hdl_t
*dtp
, caddr_t base
, dtrace_recdesc_t
*rec
)
1619 * We (should) have two records: the aggregation ID followed by the
1620 * normalization value.
1622 addr
= base
+ rec
->dtrd_offset
;
1624 if (rec
->dtrd_size
!= sizeof (dtrace_aggvarid_t
))
1625 return (dt_set_errno(dtp
, EDT_BADNORMAL
));
1627 /* LINTED - alignment */
1628 normal
.dtnd_id
= *((dtrace_aggvarid_t
*)addr
);
1631 if (rec
->dtrd_action
!= DTRACEACT_LIBACT
)
1632 return (dt_set_errno(dtp
, EDT_BADNORMAL
));
1634 if (rec
->dtrd_arg
!= DT_ACT_NORMALIZE
)
1635 return (dt_set_errno(dtp
, EDT_BADNORMAL
));
1637 addr
= base
+ rec
->dtrd_offset
;
1639 switch (rec
->dtrd_size
) {
1640 case sizeof (uint64_t):
1641 /* LINTED - alignment */
1642 normal
.dtnd_normal
= *((uint64_t *)addr
);
1644 case sizeof (uint32_t):
1645 /* LINTED - alignment */
1646 normal
.dtnd_normal
= *((uint32_t *)addr
);
1648 case sizeof (uint16_t):
1649 /* LINTED - alignment */
1650 normal
.dtnd_normal
= *((uint16_t *)addr
);
1652 case sizeof (uint8_t):
1653 normal
.dtnd_normal
= *((uint8_t *)addr
);
1656 return (dt_set_errno(dtp
, EDT_BADNORMAL
));
1659 (void) dtrace_aggregate_walk(dtp
, dt_normalize_agg
, &normal
);
1665 dt_denormalize_agg(const dtrace_aggdata_t
*aggdata
, void *arg
)
1667 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1668 dtrace_aggvarid_t id
= *((dtrace_aggvarid_t
*)arg
);
1670 if (agg
->dtagd_nrecs
== 0)
1671 return (DTRACE_AGGWALK_NEXT
);
1673 if (agg
->dtagd_varid
!= id
)
1674 return (DTRACE_AGGWALK_NEXT
);
1676 return (DTRACE_AGGWALK_DENORMALIZE
);
1680 dt_clear_agg(const dtrace_aggdata_t
*aggdata
, void *arg
)
1682 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1683 dtrace_aggvarid_t id
= *((dtrace_aggvarid_t
*)arg
);
1685 if (agg
->dtagd_nrecs
== 0)
1686 return (DTRACE_AGGWALK_NEXT
);
1688 if (agg
->dtagd_varid
!= id
)
1689 return (DTRACE_AGGWALK_NEXT
);
1691 return (DTRACE_AGGWALK_CLEAR
);
1694 typedef struct dt_trunc
{
1695 dtrace_aggvarid_t dttd_id
;
1696 uint64_t dttd_remaining
;
1700 dt_trunc_agg(const dtrace_aggdata_t
*aggdata
, void *arg
)
1702 dt_trunc_t
*trunc
= arg
;
1703 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1704 dtrace_aggvarid_t id
= trunc
->dttd_id
;
1706 if (agg
->dtagd_nrecs
== 0)
1707 return (DTRACE_AGGWALK_NEXT
);
1709 if (agg
->dtagd_varid
!= id
)
1710 return (DTRACE_AGGWALK_NEXT
);
1712 if (trunc
->dttd_remaining
== 0)
1713 return (DTRACE_AGGWALK_REMOVE
);
1715 trunc
->dttd_remaining
--;
1716 return (DTRACE_AGGWALK_NEXT
);
1720 dt_trunc(dtrace_hdl_t
*dtp
, caddr_t base
, dtrace_recdesc_t
*rec
)
1725 int (*func
)(dtrace_hdl_t
*, dtrace_aggregate_f
*, void *);
1728 * We (should) have two records: the aggregation ID followed by the
1729 * number of aggregation entries after which the aggregation is to be
1732 addr
= base
+ rec
->dtrd_offset
;
1734 if (rec
->dtrd_size
!= sizeof (dtrace_aggvarid_t
))
1735 return (dt_set_errno(dtp
, EDT_BADTRUNC
));
1737 /* LINTED - alignment */
1738 trunc
.dttd_id
= *((dtrace_aggvarid_t
*)addr
);
1741 if (rec
->dtrd_action
!= DTRACEACT_LIBACT
)
1742 return (dt_set_errno(dtp
, EDT_BADTRUNC
));
1744 if (rec
->dtrd_arg
!= DT_ACT_TRUNC
)
1745 return (dt_set_errno(dtp
, EDT_BADTRUNC
));
1747 addr
= base
+ rec
->dtrd_offset
;
1749 switch (rec
->dtrd_size
) {
1750 case sizeof (uint64_t):
1751 /* LINTED - alignment */
1752 remaining
= *((int64_t *)addr
);
1754 case sizeof (uint32_t):
1755 /* LINTED - alignment */
1756 remaining
= *((int32_t *)addr
);
1758 case sizeof (uint16_t):
1759 /* LINTED - alignment */
1760 remaining
= *((int16_t *)addr
);
1762 case sizeof (uint8_t):
1763 remaining
= *((int8_t *)addr
);
1766 return (dt_set_errno(dtp
, EDT_BADNORMAL
));
1769 if (remaining
< 0) {
1770 func
= dtrace_aggregate_walk_valsorted
;
1771 remaining
= -remaining
;
1773 func
= dtrace_aggregate_walk_valrevsorted
;
1776 assert(remaining
>= 0);
1777 trunc
.dttd_remaining
= remaining
;
1779 (void) func(dtp
, dt_trunc_agg
, &trunc
);
1785 dt_print_datum(dtrace_hdl_t
*dtp
, FILE *fp
, dtrace_recdesc_t
*rec
,
1786 caddr_t addr
, size_t size
, const dtrace_aggdata_t
*aggdata
,
1787 uint64_t normal
, dt_print_aggdata_t
*pd
)
1790 dtrace_actkind_t act
= rec
->dtrd_action
;
1791 boolean_t packed
= pd
->dtpa_agghist
|| pd
->dtpa_aggpack
;
1792 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1798 } *fmt
, fmttab
[] = {
1799 { sizeof (uint8_t), 3, 3 },
1800 { sizeof (uint16_t), 5, 5 },
1801 { sizeof (uint32_t), 8, 8 },
1802 { sizeof (uint64_t), 16, 16 },
1806 if (packed
&& pd
->dtpa_agghisthdr
!= agg
->dtagd_varid
) {
1807 dtrace_recdesc_t
*r
;
1812 * To print our quantization header for either an agghist or
1813 * aggpack aggregation, we need to iterate through all of our
1814 * of our records to determine their width.
1816 for (r
= rec
; !DTRACEACT_ISAGG(r
->dtrd_action
); r
++) {
1817 for (fmt
= fmttab
; fmt
->size
&&
1818 fmt
->size
!= r
->dtrd_size
; fmt
++)
1821 width
+= fmt
->packedwidth
+ 1;
1824 if (pd
->dtpa_agghist
) {
1825 if (dt_print_quanthdr(dtp
, fp
, width
) < 0)
1828 if (dt_print_quanthdr_packed(dtp
, fp
,
1829 width
, aggdata
, r
->dtrd_action
) < 0)
1833 pd
->dtpa_agghisthdr
= agg
->dtagd_varid
;
1836 if (pd
->dtpa_agghist
&& DTRACEACT_ISAGG(act
)) {
1837 char positives
= aggdata
->dtada_flags
& DTRACE_A_HASPOSITIVES
;
1838 char negatives
= aggdata
->dtada_flags
& DTRACE_A_HASNEGATIVES
;
1841 assert(act
== DTRACEAGG_SUM
|| act
== DTRACEAGG_COUNT
);
1842 val
= (long long)*((uint64_t *)addr
);
1844 if (dt_printf(dtp
, fp
, " ") < 0)
1847 return (dt_print_quantline(dtp
, fp
, val
, normal
,
1848 aggdata
->dtada_total
, positives
, negatives
));
1851 if (pd
->dtpa_aggpack
&& DTRACEACT_ISAGG(act
)) {
1853 case DTRACEAGG_QUANTIZE
:
1854 return (dt_print_quantize_packed(dtp
,
1855 fp
, addr
, size
, aggdata
));
1856 case DTRACEAGG_LQUANTIZE
:
1857 return (dt_print_lquantize_packed(dtp
,
1858 fp
, addr
, size
, aggdata
));
1865 case DTRACEACT_STACK
:
1866 return (dt_print_stack(dtp
, fp
, NULL
, addr
,
1867 rec
->dtrd_arg
, rec
->dtrd_size
/ rec
->dtrd_arg
));
1869 case DTRACEACT_USTACK
:
1870 case DTRACEACT_JSTACK
:
1871 return (dt_print_ustack(dtp
, fp
, NULL
, addr
, rec
->dtrd_arg
));
1873 case DTRACEACT_USYM
:
1874 case DTRACEACT_UADDR
:
1875 return (dt_print_usym(dtp
, fp
, addr
, act
));
1877 case DTRACEACT_UMOD
:
1878 return (dt_print_umod(dtp
, fp
, NULL
, addr
));
1881 return (dt_print_sym(dtp
, fp
, NULL
, addr
));
1884 return (dt_print_mod(dtp
, fp
, NULL
, addr
));
1886 case DTRACEAGG_QUANTIZE
:
1887 return (dt_print_quantize(dtp
, fp
, addr
, size
, normal
));
1889 case DTRACEAGG_LQUANTIZE
:
1890 return (dt_print_lquantize(dtp
, fp
, addr
, size
, normal
));
1892 case DTRACEAGG_LLQUANTIZE
:
1893 return (dt_print_llquantize(dtp
, fp
, addr
, size
, normal
));
1896 return (dt_print_average(dtp
, fp
, addr
, size
, normal
));
1898 case DTRACEAGG_STDDEV
:
1899 return (dt_print_stddev(dtp
, fp
, addr
, size
, normal
));
1905 for (fmt
= fmttab
; fmt
->size
&& fmt
->size
!= size
; fmt
++)
1908 width
= packed
? fmt
->packedwidth
: fmt
->width
;
1911 case sizeof (uint64_t):
1912 err
= dt_printf(dtp
, fp
, " %*lld", width
,
1913 /* LINTED - alignment */
1914 (long long)*((uint64_t *)addr
) / normal
);
1916 case sizeof (uint32_t):
1917 /* LINTED - alignment */
1918 err
= dt_printf(dtp
, fp
, " %*d", width
, *((uint32_t *)addr
) /
1921 case sizeof (uint16_t):
1922 /* LINTED - alignment */
1923 err
= dt_printf(dtp
, fp
, " %*d", width
, *((uint16_t *)addr
) /
1926 case sizeof (uint8_t):
1927 err
= dt_printf(dtp
, fp
, " %*d", width
, *((uint8_t *)addr
) /
1931 err
= dt_print_bytes(dtp
, fp
, addr
, size
, width
, 0, 0);
1939 dt_print_aggs(const dtrace_aggdata_t
**aggsdata
, int naggvars
, void *arg
)
1942 dt_print_aggdata_t
*pd
= arg
;
1943 const dtrace_aggdata_t
*aggdata
= aggsdata
[0];
1944 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1945 FILE *fp
= pd
->dtpa_fp
;
1946 dtrace_hdl_t
*dtp
= pd
->dtpa_dtp
;
1947 dtrace_recdesc_t
*rec
;
1948 dtrace_actkind_t act
;
1952 pd
->dtpa_agghist
= (aggdata
->dtada_flags
& DTRACE_A_TOTAL
);
1953 pd
->dtpa_aggpack
= (aggdata
->dtada_flags
& DTRACE_A_MINMAXBIN
);
1956 * Iterate over each record description in the key, printing the traced
1957 * data, skipping the first datum (the tuple member created by the
1960 for (i
= 1; i
< agg
->dtagd_nrecs
; i
++) {
1961 rec
= &agg
->dtagd_rec
[i
];
1962 act
= rec
->dtrd_action
;
1963 addr
= aggdata
->dtada_data
+ rec
->dtrd_offset
;
1964 size
= rec
->dtrd_size
;
1966 if (DTRACEACT_ISAGG(act
)) {
1971 if (dt_print_datum(dtp
, fp
, rec
, addr
,
1972 size
, aggdata
, 1, pd
) < 0)
1975 if (dt_buffered_flush(dtp
, NULL
, rec
, aggdata
,
1976 DTRACE_BUFDATA_AGGKEY
) < 0)
1980 assert(aggact
!= 0);
1982 for (i
= (naggvars
== 1 ? 0 : 1); i
< naggvars
; i
++) {
1985 aggdata
= aggsdata
[i
];
1986 agg
= aggdata
->dtada_desc
;
1987 rec
= &agg
->dtagd_rec
[aggact
];
1988 act
= rec
->dtrd_action
;
1989 addr
= aggdata
->dtada_data
+ rec
->dtrd_offset
;
1990 size
= rec
->dtrd_size
;
1992 assert(DTRACEACT_ISAGG(act
));
1993 normal
= aggdata
->dtada_normal
;
1995 if (dt_print_datum(dtp
, fp
, rec
, addr
,
1996 size
, aggdata
, normal
, pd
) < 0)
1999 if (dt_buffered_flush(dtp
, NULL
, rec
, aggdata
,
2000 DTRACE_BUFDATA_AGGVAL
) < 0)
2003 if (!pd
->dtpa_allunprint
)
2004 agg
->dtagd_flags
|= DTRACE_AGD_PRINTED
;
2007 if (!pd
->dtpa_agghist
&& !pd
->dtpa_aggpack
) {
2008 if (dt_printf(dtp
, fp
, "\n") < 0)
2012 if (dt_buffered_flush(dtp
, NULL
, NULL
, aggdata
,
2013 DTRACE_BUFDATA_AGGFORMAT
| DTRACE_BUFDATA_AGGLAST
) < 0)
2020 dt_print_agg(const dtrace_aggdata_t
*aggdata
, void *arg
)
2022 dt_print_aggdata_t
*pd
= arg
;
2023 dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
2024 dtrace_aggvarid_t aggvarid
= pd
->dtpa_id
;
2026 if (pd
->dtpa_allunprint
) {
2027 if (agg
->dtagd_flags
& DTRACE_AGD_PRINTED
)
2031 * If we're not printing all unprinted aggregations, then the
2032 * aggregation variable ID denotes a specific aggregation
2033 * variable that we should print -- skip any other aggregations
2034 * that we encounter.
2036 if (agg
->dtagd_nrecs
== 0)
2039 if (aggvarid
!= agg
->dtagd_varid
)
2043 return (dt_print_aggs(&aggdata
, 1, arg
));
2047 dt_setopt(dtrace_hdl_t
*dtp
, const dtrace_probedata_t
*data
,
2048 const char *option
, const char *value
)
2053 dtrace_setoptdata_t optdata
;
2055 bzero(&optdata
, sizeof (optdata
));
2056 (void) dtrace_getopt(dtp
, option
, &optdata
.dtsda_oldval
);
2058 if (dtrace_setopt(dtp
, option
, value
) == 0) {
2059 (void) dtrace_getopt(dtp
, option
, &optdata
.dtsda_newval
);
2060 optdata
.dtsda_probe
= data
;
2061 optdata
.dtsda_option
= option
;
2062 optdata
.dtsda_handle
= dtp
;
2064 if ((rval
= dt_handle_setopt(dtp
, &optdata
)) != 0)
2070 errstr
= dtrace_errmsg(dtp
, dtrace_errno(dtp
));
2071 len
= strlen(option
) + strlen(value
) + strlen(errstr
) + 80;
2074 (void) snprintf(msg
, len
, "couldn't set option \"%s\" to \"%s\": %s\n",
2075 option
, value
, errstr
);
2077 if ((rval
= dt_handle_liberr(dtp
, data
, msg
)) == 0)
2084 dt_consume_cpu(dtrace_hdl_t
*dtp
, FILE *fp
, int cpu
,
2085 dtrace_bufdesc_t
*buf
, boolean_t just_one
,
2086 dtrace_consume_probe_f
*efunc
, dtrace_consume_rec_f
*rfunc
, void *arg
)
2090 int flow
= (dtp
->dt_options
[DTRACEOPT_FLOWINDENT
] != DTRACEOPT_UNSET
);
2091 int quiet
= (dtp
->dt_options
[DTRACEOPT_QUIET
] != DTRACEOPT_UNSET
);
2093 uint64_t tracememsize
= 0;
2094 dtrace_probedata_t data
;
2097 bzero(&data
, sizeof (data
));
2098 data
.dtpda_handle
= dtp
;
2099 data
.dtpda_cpu
= cpu
;
2100 data
.dtpda_flow
= dtp
->dt_flow
;
2101 data
.dtpda_indent
= dtp
->dt_indent
;
2102 data
.dtpda_prefix
= dtp
->dt_prefix
;
2104 for (offs
= buf
->dtbd_oldest
; offs
< buf
->dtbd_size
; ) {
2105 dtrace_eprobedesc_t
*epd
;
2108 * We're guaranteed to have an ID.
2110 id
= *(uint32_t *)((uintptr_t)buf
->dtbd_data
+ offs
);
2112 if (id
== DTRACE_EPIDNONE
) {
2114 * This is filler to assure proper alignment of the
2115 * next record; we simply ignore it.
2117 offs
+= sizeof (id
);
2121 if ((rval
= dt_epid_lookup(dtp
, id
, &data
.dtpda_edesc
,
2122 &data
.dtpda_pdesc
)) != 0)
2125 epd
= data
.dtpda_edesc
;
2126 data
.dtpda_data
= buf
->dtbd_data
+ offs
;
2128 if (data
.dtpda_edesc
->dtepd_uarg
!= DT_ECB_DEFAULT
) {
2129 rval
= dt_handle(dtp
, &data
);
2131 if (rval
== DTRACE_CONSUME_NEXT
)
2134 if (rval
== DTRACE_CONSUME_ERROR
)
2139 (void) dt_flowindent(dtp
, &data
, dtp
->dt_last_epid
,
2142 rval
= (*efunc
)(&data
, arg
);
2145 if (data
.dtpda_flow
== DTRACEFLOW_ENTRY
)
2146 data
.dtpda_indent
+= 2;
2149 if (rval
== DTRACE_CONSUME_NEXT
)
2152 if (rval
== DTRACE_CONSUME_ABORT
)
2153 return (dt_set_errno(dtp
, EDT_DIRABORT
));
2155 if (rval
!= DTRACE_CONSUME_THIS
)
2156 return (dt_set_errno(dtp
, EDT_BADRVAL
));
2158 for (i
= 0; i
< epd
->dtepd_nrecs
; i
++) {
2160 dtrace_recdesc_t
*rec
= &epd
->dtepd_rec
[i
];
2161 dtrace_actkind_t act
= rec
->dtrd_action
;
2163 data
.dtpda_data
= buf
->dtbd_data
+ offs
+
2165 addr
= data
.dtpda_data
;
2167 if (act
== DTRACEACT_LIBACT
) {
2168 uint64_t arg
= rec
->dtrd_arg
;
2169 dtrace_aggvarid_t id
;
2173 /* LINTED - alignment */
2174 id
= *((dtrace_aggvarid_t
*)addr
);
2175 (void) dtrace_aggregate_walk(dtp
,
2179 case DT_ACT_DENORMALIZE
:
2180 /* LINTED - alignment */
2181 id
= *((dtrace_aggvarid_t
*)addr
);
2182 (void) dtrace_aggregate_walk(dtp
,
2183 dt_denormalize_agg
, &id
);
2186 case DT_ACT_FTRUNCATE
:
2191 (void) ftruncate(fileno(fp
), 0);
2192 (void) fseeko(fp
, 0, SEEK_SET
);
2195 case DT_ACT_NORMALIZE
:
2196 if (i
== epd
->dtepd_nrecs
- 1)
2197 return (dt_set_errno(dtp
,
2200 if (dt_normalize(dtp
,
2201 buf
->dtbd_data
+ offs
, rec
) != 0)
2207 case DT_ACT_SETOPT
: {
2208 uint64_t *opts
= dtp
->dt_options
;
2209 dtrace_recdesc_t
*valrec
;
2214 if (i
== epd
->dtepd_nrecs
- 1) {
2215 return (dt_set_errno(dtp
,
2219 valrec
= &epd
->dtepd_rec
[++i
];
2220 valsize
= valrec
->dtrd_size
;
2222 if (valrec
->dtrd_action
!= act
||
2223 valrec
->dtrd_arg
!= arg
) {
2224 return (dt_set_errno(dtp
,
2228 if (valsize
> sizeof (uint64_t)) {
2229 val
= buf
->dtbd_data
+ offs
+
2230 valrec
->dtrd_offset
;
2235 rv
= dt_setopt(dtp
, &data
, addr
, val
);
2240 flow
= (opts
[DTRACEOPT_FLOWINDENT
] !=
2242 quiet
= (opts
[DTRACEOPT_QUIET
] !=
2249 if (i
== epd
->dtepd_nrecs
- 1)
2250 return (dt_set_errno(dtp
,
2254 buf
->dtbd_data
+ offs
, rec
) != 0)
2265 if (act
== DTRACEACT_TRACEMEM_DYNSIZE
&&
2266 rec
->dtrd_size
== sizeof (uint64_t)) {
2267 /* LINTED - alignment */
2268 tracememsize
= *((unsigned long long *)addr
);
2272 rval
= (*rfunc
)(&data
, rec
, arg
);
2274 if (rval
== DTRACE_CONSUME_NEXT
)
2277 if (rval
== DTRACE_CONSUME_ABORT
)
2278 return (dt_set_errno(dtp
, EDT_DIRABORT
));
2280 if (rval
!= DTRACE_CONSUME_THIS
)
2281 return (dt_set_errno(dtp
, EDT_BADRVAL
));
2283 if (act
== DTRACEACT_STACK
) {
2284 int depth
= rec
->dtrd_arg
;
2286 if (dt_print_stack(dtp
, fp
, NULL
, addr
, depth
,
2287 rec
->dtrd_size
/ depth
) < 0)
2292 if (act
== DTRACEACT_USTACK
||
2293 act
== DTRACEACT_JSTACK
) {
2294 if (dt_print_ustack(dtp
, fp
, NULL
,
2295 addr
, rec
->dtrd_arg
) < 0)
2300 if (act
== DTRACEACT_SYM
) {
2301 if (dt_print_sym(dtp
, fp
, NULL
, addr
) < 0)
2306 if (act
== DTRACEACT_MOD
) {
2307 if (dt_print_mod(dtp
, fp
, NULL
, addr
) < 0)
2312 if (act
== DTRACEACT_USYM
|| act
== DTRACEACT_UADDR
) {
2313 if (dt_print_usym(dtp
, fp
, addr
, act
) < 0)
2318 if (act
== DTRACEACT_UMOD
) {
2319 if (dt_print_umod(dtp
, fp
, NULL
, addr
) < 0)
2324 if (DTRACEACT_ISPRINTFLIKE(act
)) {
2326 int (*func
)(dtrace_hdl_t
*, FILE *, void *,
2327 const dtrace_probedata_t
*,
2328 const dtrace_recdesc_t
*, uint_t
,
2329 const void *buf
, size_t);
2331 if ((fmtdata
= dt_format_lookup(dtp
,
2332 rec
->dtrd_format
)) == NULL
)
2336 case DTRACEACT_PRINTF
:
2337 func
= dtrace_fprintf
;
2339 case DTRACEACT_PRINTA
:
2340 func
= dtrace_fprinta
;
2342 case DTRACEACT_SYSTEM
:
2343 func
= dtrace_system
;
2345 case DTRACEACT_FREOPEN
:
2346 func
= dtrace_freopen
;
2350 n
= (*func
)(dtp
, fp
, fmtdata
, &data
,
2351 rec
, epd
->dtepd_nrecs
- i
,
2352 (uchar_t
*)buf
->dtbd_data
+ offs
,
2353 buf
->dtbd_size
- offs
);
2356 return (-1); /* errno is set for us */
2364 * If this is a DIF expression, and the record has a
2365 * format set, this indicates we have a CTF type name
2366 * associated with the data and we should try to print
2369 if (act
== DTRACEACT_DIFEXPR
) {
2370 const char *strdata
= dt_strdata_lookup(dtp
,
2372 if (strdata
!= NULL
) {
2373 n
= dtrace_print(dtp
, fp
, strdata
,
2374 addr
, rec
->dtrd_size
);
2377 * dtrace_print() will return -1 on
2378 * error, or return the number of bytes
2379 * consumed. It will return 0 if the
2380 * type couldn't be determined, and we
2381 * should fall through to the normal
2393 if (act
== DTRACEACT_PRINTA
) {
2394 dt_print_aggdata_t pd
;
2395 dtrace_aggvarid_t
*aggvars
;
2396 int j
, naggvars
= 0;
2397 size_t size
= ((epd
->dtepd_nrecs
- i
) *
2398 sizeof (dtrace_aggvarid_t
));
2400 if ((aggvars
= dt_alloc(dtp
, size
)) == NULL
)
2404 * This might be a printa() with multiple
2405 * aggregation variables. We need to scan
2406 * forward through the records until we find
2407 * a record from a different statement.
2409 for (j
= i
; j
< epd
->dtepd_nrecs
; j
++) {
2410 dtrace_recdesc_t
*nrec
;
2413 nrec
= &epd
->dtepd_rec
[j
];
2415 if (nrec
->dtrd_uarg
!= rec
->dtrd_uarg
)
2418 if (nrec
->dtrd_action
!= act
) {
2419 return (dt_set_errno(dtp
,
2423 naddr
= buf
->dtbd_data
+ offs
+
2426 aggvars
[naggvars
++] =
2427 /* LINTED - alignment */
2428 *((dtrace_aggvarid_t
*)naddr
);
2432 bzero(&pd
, sizeof (pd
));
2436 assert(naggvars
>= 1);
2438 if (naggvars
== 1) {
2439 pd
.dtpa_id
= aggvars
[0];
2440 dt_free(dtp
, aggvars
);
2442 if (dt_printf(dtp
, fp
, "\n") < 0 ||
2443 dtrace_aggregate_walk_sorted(dtp
,
2444 dt_print_agg
, &pd
) < 0)
2449 if (dt_printf(dtp
, fp
, "\n") < 0 ||
2450 dtrace_aggregate_walk_joined(dtp
, aggvars
,
2451 naggvars
, dt_print_aggs
, &pd
) < 0) {
2452 dt_free(dtp
, aggvars
);
2456 dt_free(dtp
, aggvars
);
2460 if (act
== DTRACEACT_TRACEMEM
) {
2461 if (tracememsize
== 0 ||
2462 tracememsize
> rec
->dtrd_size
) {
2463 tracememsize
= rec
->dtrd_size
;
2466 n
= dt_print_bytes(dtp
, fp
, addr
,
2467 tracememsize
, -33, quiet
, 1);
2477 switch (rec
->dtrd_size
) {
2478 case sizeof (uint64_t):
2479 n
= dt_printf(dtp
, fp
,
2480 quiet
? "%lld" : " %16lld",
2481 /* LINTED - alignment */
2482 *((unsigned long long *)addr
));
2484 case sizeof (uint32_t):
2485 n
= dt_printf(dtp
, fp
, quiet
? "%d" : " %8d",
2486 /* LINTED - alignment */
2487 *((uint32_t *)addr
));
2489 case sizeof (uint16_t):
2490 n
= dt_printf(dtp
, fp
, quiet
? "%d" : " %5d",
2491 /* LINTED - alignment */
2492 *((uint16_t *)addr
));
2494 case sizeof (uint8_t):
2495 n
= dt_printf(dtp
, fp
, quiet
? "%d" : " %3d",
2496 *((uint8_t *)addr
));
2499 n
= dt_print_bytes(dtp
, fp
, addr
,
2500 rec
->dtrd_size
, -33, quiet
, 0);
2505 return (-1); /* errno is set for us */
2508 if (dt_buffered_flush(dtp
, &data
, rec
, NULL
, 0) < 0)
2509 return (-1); /* errno is set for us */
2513 * Call the record callback with a NULL record to indicate
2514 * that we're done processing this EPID.
2516 rval
= (*rfunc
)(&data
, NULL
, arg
);
2518 offs
+= epd
->dtepd_size
;
2519 dtp
->dt_last_epid
= id
;
2521 buf
->dtbd_oldest
= offs
;
2526 dtp
->dt_flow
= data
.dtpda_flow
;
2527 dtp
->dt_indent
= data
.dtpda_indent
;
2528 dtp
->dt_prefix
= data
.dtpda_prefix
;
2530 if ((drops
= buf
->dtbd_drops
) == 0)
2534 * Explicitly zero the drops to prevent us from processing them again.
2536 buf
->dtbd_drops
= 0;
2538 return (dt_handle_cpudrop(dtp
, cpu
, DTRACEDROP_PRINCIPAL
, drops
));
2542 * Reduce memory usage by shrinking the buffer if it's no more than half full.
2543 * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2544 * only 4-byte aligned.
2547 dt_realloc_buf(dtrace_hdl_t
*dtp
, dtrace_bufdesc_t
*buf
, int cursize
)
2549 uint64_t used
= buf
->dtbd_size
- buf
->dtbd_oldest
;
2550 if (used
< cursize
/ 2) {
2551 int misalign
= buf
->dtbd_oldest
& (sizeof (uint64_t) - 1);
2552 char *newdata
= dt_alloc(dtp
, used
+ misalign
);
2553 if (newdata
== NULL
)
2555 bzero(newdata
, misalign
);
2556 bcopy(buf
->dtbd_data
+ buf
->dtbd_oldest
,
2557 newdata
+ misalign
, used
);
2558 dt_free(dtp
, buf
->dtbd_data
);
2559 buf
->dtbd_oldest
= misalign
;
2560 buf
->dtbd_size
= used
+ misalign
;
2561 buf
->dtbd_data
= newdata
;
2566 * If the ring buffer has wrapped, the data is not in order. Rearrange it
2567 * so that it is. Note, we need to preserve the alignment of the data at
2568 * dtbd_oldest, which is only 4-byte aligned.
2571 dt_unring_buf(dtrace_hdl_t
*dtp
, dtrace_bufdesc_t
*buf
)
2574 char *newdata
, *ndp
;
2576 if (buf
->dtbd_oldest
== 0)
2579 misalign
= buf
->dtbd_oldest
& (sizeof (uint64_t) - 1);
2580 newdata
= ndp
= dt_alloc(dtp
, buf
->dtbd_size
+ misalign
);
2582 if (newdata
== NULL
)
2585 assert(0 == (buf
->dtbd_size
& (sizeof (uint64_t) - 1)));
2587 bzero(ndp
, misalign
);
2590 bcopy(buf
->dtbd_data
+ buf
->dtbd_oldest
, ndp
,
2591 buf
->dtbd_size
- buf
->dtbd_oldest
);
2592 ndp
+= buf
->dtbd_size
- buf
->dtbd_oldest
;
2594 bcopy(buf
->dtbd_data
, ndp
, buf
->dtbd_oldest
);
2596 dt_free(dtp
, buf
->dtbd_data
);
2597 buf
->dtbd_oldest
= 0;
2598 buf
->dtbd_data
= newdata
;
2599 buf
->dtbd_size
+= misalign
;
2605 dt_put_buf(dtrace_hdl_t
*dtp
, dtrace_bufdesc_t
*buf
)
2607 dt_free(dtp
, buf
->dtbd_data
);
2612 * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2613 * data, or NULL if there is no data for this CPU.
2614 * Returns -1 on failure and sets dt_errno.
2617 dt_get_buf(dtrace_hdl_t
*dtp
, int cpu
, dtrace_bufdesc_t
**bufp
)
2619 dtrace_optval_t size
;
2620 dtrace_bufdesc_t
*buf
= dt_zalloc(dtp
, sizeof (*buf
));
2626 (void) dtrace_getopt(dtp
, "bufsize", &size
);
2627 buf
->dtbd_data
= dt_alloc(dtp
, size
);
2628 if (buf
->dtbd_data
== NULL
) {
2632 buf
->dtbd_size
= size
;
2633 buf
->dtbd_cpu
= cpu
;
2635 if (dt_ioctl(dtp
, DTRACEIOC_BUFSNAP
, buf
) == -1) {
2636 dt_put_buf(dtp
, buf
);
2638 * If we failed with ENOENT, it may be because the
2639 * CPU was unconfigured -- this is okay. Any other
2640 * error, however, is unexpected.
2642 if (errno
== ENOENT
) {
2647 return (dt_set_errno(dtp
, errno
));
2650 error
= dt_unring_buf(dtp
, buf
);
2652 dt_put_buf(dtp
, buf
);
2655 dt_realloc_buf(dtp
, buf
, size
);
2661 typedef struct dt_begin
{
2662 dtrace_consume_probe_f
*dtbgn_probefunc
;
2663 dtrace_consume_rec_f
*dtbgn_recfunc
;
2665 dtrace_handle_err_f
*dtbgn_errhdlr
;
2667 int dtbgn_beginonly
;
2671 dt_consume_begin_probe(const dtrace_probedata_t
*data
, void *arg
)
2673 dt_begin_t
*begin
= arg
;
2674 dtrace_probedesc_t
*pd
= data
->dtpda_pdesc
;
2676 int r1
= (strcmp(pd
->dtpd_provider
, "dtrace") == 0);
2677 int r2
= (strcmp(pd
->dtpd_name
, "BEGIN") == 0);
2679 if (begin
->dtbgn_beginonly
) {
2681 return (DTRACE_CONSUME_NEXT
);
2684 return (DTRACE_CONSUME_NEXT
);
2688 * We have a record that we're interested in. Now call the underlying
2691 return (begin
->dtbgn_probefunc(data
, begin
->dtbgn_arg
));
2695 dt_consume_begin_record(const dtrace_probedata_t
*data
,
2696 const dtrace_recdesc_t
*rec
, void *arg
)
2698 dt_begin_t
*begin
= arg
;
2700 return (begin
->dtbgn_recfunc(data
, rec
, begin
->dtbgn_arg
));
2704 dt_consume_begin_error(const dtrace_errdata_t
*data
, void *arg
)
2706 dt_begin_t
*begin
= (dt_begin_t
*)arg
;
2707 dtrace_probedesc_t
*pd
= data
->dteda_pdesc
;
2709 int r1
= (strcmp(pd
->dtpd_provider
, "dtrace") == 0);
2710 int r2
= (strcmp(pd
->dtpd_name
, "BEGIN") == 0);
2712 if (begin
->dtbgn_beginonly
) {
2714 return (DTRACE_HANDLE_OK
);
2717 return (DTRACE_HANDLE_OK
);
2720 return (begin
->dtbgn_errhdlr(data
, begin
->dtbgn_errarg
));
2724 dt_consume_begin(dtrace_hdl_t
*dtp
, FILE *fp
,
2725 dtrace_consume_probe_f
*pf
, dtrace_consume_rec_f
*rf
, void *arg
)
2728 * There's this idea that the BEGIN probe should be processed before
2729 * everything else, and that the END probe should be processed after
2730 * anything else. In the common case, this is pretty easy to deal
2731 * with. However, a situation may arise where the BEGIN enabling and
2732 * END enabling are on the same CPU, and some enabling in the middle
2733 * occurred on a different CPU. To deal with this (blech!) we need to
2734 * consume the BEGIN buffer up until the end of the BEGIN probe, and
2735 * then set it aside. We will then process every other CPU, and then
2736 * we'll return to the BEGIN CPU and process the rest of the data
2737 * (which will inevitably include the END probe, if any). Making this
2738 * even more complicated (!) is the library's ERROR enabling. Because
2739 * this enabling is processed before we even get into the consume call
2740 * back, any ERROR firing would result in the library's ERROR enabling
2741 * being processed twice -- once in our first pass (for BEGIN probes),
2742 * and again in our second pass (for everything but BEGIN probes). To
2743 * deal with this, we interpose on the ERROR handler to assure that we
2744 * only process ERROR enablings induced by BEGIN enablings in the
2745 * first pass, and that we only process ERROR enablings _not_ induced
2746 * by BEGIN enablings in the second pass.
2750 processorid_t cpu
= dtp
->dt_beganon
;
2752 static int max_ncpus
;
2753 dtrace_bufdesc_t
*buf
;
2755 dtp
->dt_beganon
= -1;
2757 if (dt_get_buf(dtp
, cpu
, &buf
) != 0)
2762 if (!dtp
->dt_stopped
|| buf
->dtbd_cpu
!= dtp
->dt_endedon
) {
2764 * This is the simple case. We're either not stopped, or if
2765 * we are, we actually processed any END probes on another
2766 * CPU. We can simply consume this buffer and return.
2768 rval
= dt_consume_cpu(dtp
, fp
, cpu
, buf
, B_FALSE
,
2770 dt_put_buf(dtp
, buf
);
2774 begin
.dtbgn_probefunc
= pf
;
2775 begin
.dtbgn_recfunc
= rf
;
2776 begin
.dtbgn_arg
= arg
;
2777 begin
.dtbgn_beginonly
= 1;
2780 * We need to interpose on the ERROR handler to be sure that we
2781 * only process ERRORs induced by BEGIN.
2783 begin
.dtbgn_errhdlr
= dtp
->dt_errhdlr
;
2784 begin
.dtbgn_errarg
= dtp
->dt_errarg
;
2785 dtp
->dt_errhdlr
= dt_consume_begin_error
;
2786 dtp
->dt_errarg
= &begin
;
2788 rval
= dt_consume_cpu(dtp
, fp
, cpu
, buf
, B_FALSE
,
2789 dt_consume_begin_probe
, dt_consume_begin_record
, &begin
);
2791 dtp
->dt_errhdlr
= begin
.dtbgn_errhdlr
;
2792 dtp
->dt_errarg
= begin
.dtbgn_errarg
;
2795 dt_put_buf(dtp
, buf
);
2800 max_ncpus
= dt_sysconf(dtp
, _SC_CPUID_MAX
) + 1;
2802 for (i
= 0; i
< max_ncpus
; i
++) {
2803 dtrace_bufdesc_t
*nbuf
;
2807 if (dt_get_buf(dtp
, i
, &nbuf
) != 0) {
2808 dt_put_buf(dtp
, buf
);
2814 rval
= dt_consume_cpu(dtp
, fp
, i
, nbuf
, B_FALSE
,
2816 dt_put_buf(dtp
, nbuf
);
2818 dt_put_buf(dtp
, buf
);
2824 * Okay -- we're done with the other buffers. Now we want to
2825 * reconsume the first buffer -- but this time we're looking for
2826 * everything _but_ BEGIN. And of course, in order to only consume
2827 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
2828 * ERROR interposition function...
2830 begin
.dtbgn_beginonly
= 0;
2832 assert(begin
.dtbgn_errhdlr
== dtp
->dt_errhdlr
);
2833 assert(begin
.dtbgn_errarg
== dtp
->dt_errarg
);
2834 dtp
->dt_errhdlr
= dt_consume_begin_error
;
2835 dtp
->dt_errarg
= &begin
;
2837 rval
= dt_consume_cpu(dtp
, fp
, cpu
, buf
, B_FALSE
,
2838 dt_consume_begin_probe
, dt_consume_begin_record
, &begin
);
2840 dtp
->dt_errhdlr
= begin
.dtbgn_errhdlr
;
2841 dtp
->dt_errarg
= begin
.dtbgn_errarg
;
2848 dt_buf_oldest(void *elem
, void *arg
)
2850 dtrace_bufdesc_t
*buf
= elem
;
2851 size_t offs
= buf
->dtbd_oldest
;
2853 while (offs
< buf
->dtbd_size
) {
2854 dtrace_rechdr_t
*dtrh
=
2855 /* LINTED - alignment */
2856 (dtrace_rechdr_t
*)(buf
->dtbd_data
+ offs
);
2857 if (dtrh
->dtrh_epid
== DTRACE_EPIDNONE
) {
2858 offs
+= sizeof (dtrace_epid_t
);
2860 return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh
));
2864 /* There are no records left; use the time the buffer was retrieved. */
2865 return (buf
->dtbd_timestamp
);
2869 dtrace_consume(dtrace_hdl_t
*dtp
, FILE *fp
,
2870 dtrace_consume_probe_f
*pf
, dtrace_consume_rec_f
*rf
, void *arg
)
2872 dtrace_optval_t size
;
2873 static int max_ncpus
;
2875 dtrace_optval_t interval
= dtp
->dt_options
[DTRACEOPT_SWITCHRATE
];
2876 hrtime_t now
= gethrtime();
2878 if (dtp
->dt_lastswitch
!= 0) {
2879 if (now
- dtp
->dt_lastswitch
< interval
)
2882 dtp
->dt_lastswitch
+= interval
;
2884 dtp
->dt_lastswitch
= now
;
2887 if (!dtp
->dt_active
)
2888 return (dt_set_errno(dtp
, EINVAL
));
2891 max_ncpus
= dt_sysconf(dtp
, _SC_CPUID_MAX
) + 1;
2894 pf
= (dtrace_consume_probe_f
*)dt_nullprobe
;
2897 rf
= (dtrace_consume_rec_f
*)dt_nullrec
;
2899 if (dtp
->dt_options
[DTRACEOPT_TEMPORAL
] == DTRACEOPT_UNSET
) {
2901 * The output will not be in the order it was traced. Rather,
2902 * we will consume all of the data from each CPU's buffer in
2903 * turn. We apply special handling for the records from BEGIN
2904 * and END probes so that they are consumed first and last,
2907 * If we have just begun, we want to first process the CPU that
2908 * executed the BEGIN probe (if any).
2910 if (dtp
->dt_active
&& dtp
->dt_beganon
!= -1 &&
2911 (rval
= dt_consume_begin(dtp
, fp
, pf
, rf
, arg
)) != 0)
2914 for (i
= 0; i
< max_ncpus
; i
++) {
2915 dtrace_bufdesc_t
*buf
;
2918 * If we have stopped, we want to process the CPU on
2919 * which the END probe was processed only _after_ we
2920 * have processed everything else.
2922 if (dtp
->dt_stopped
&& (i
== dtp
->dt_endedon
))
2925 if (dt_get_buf(dtp
, i
, &buf
) != 0)
2932 dtp
->dt_prefix
= NULL
;
2933 rval
= dt_consume_cpu(dtp
, fp
, i
,
2934 buf
, B_FALSE
, pf
, rf
, arg
);
2935 dt_put_buf(dtp
, buf
);
2939 if (dtp
->dt_stopped
) {
2940 dtrace_bufdesc_t
*buf
;
2942 if (dt_get_buf(dtp
, dtp
->dt_endedon
, &buf
) != 0)
2947 rval
= dt_consume_cpu(dtp
, fp
, dtp
->dt_endedon
,
2948 buf
, B_FALSE
, pf
, rf
, arg
);
2949 dt_put_buf(dtp
, buf
);
2954 * The output will be in the order it was traced (or for
2955 * speculations, when it was committed). We retrieve a buffer
2956 * from each CPU and put it into a priority queue, which sorts
2957 * based on the first entry in the buffer. This is sufficient
2958 * because entries within a buffer are already sorted.
2960 * We then consume records one at a time, always consuming the
2961 * oldest record, as determined by the priority queue. When
2962 * we reach the end of the time covered by these buffers,
2963 * we need to stop and retrieve more records on the next pass.
2964 * The kernel tells us the time covered by each buffer, in
2965 * dtbd_timestamp. The first buffer's timestamp tells us the
2966 * time covered by all buffers, as subsequently retrieved
2967 * buffers will cover to a more recent time.
2970 uint64_t *drops
= alloca(max_ncpus
* sizeof (uint64_t));
2971 uint64_t first_timestamp
= 0;
2973 dtrace_bufdesc_t
*buf
;
2975 bzero(drops
, max_ncpus
* sizeof (uint64_t));
2977 if (dtp
->dt_bufq
== NULL
) {
2978 dtp
->dt_bufq
= dt_pq_init(dtp
, max_ncpus
* 2,
2979 dt_buf_oldest
, NULL
);
2980 if (dtp
->dt_bufq
== NULL
) /* ENOMEM */
2984 /* Retrieve data from each CPU. */
2985 (void) dtrace_getopt(dtp
, "bufsize", &size
);
2986 for (i
= 0; i
< max_ncpus
; i
++) {
2987 dtrace_bufdesc_t
*buf
;
2989 if (dt_get_buf(dtp
, i
, &buf
) != 0)
2992 if (first_timestamp
== 0)
2993 first_timestamp
= buf
->dtbd_timestamp
;
2994 assert(buf
->dtbd_timestamp
>= first_timestamp
);
2996 dt_pq_insert(dtp
->dt_bufq
, buf
);
2997 drops
[i
] = buf
->dtbd_drops
;
2998 buf
->dtbd_drops
= 0;
3002 /* Consume records. */
3004 dtrace_bufdesc_t
*buf
= dt_pq_pop(dtp
->dt_bufq
);
3010 timestamp
= dt_buf_oldest(buf
, dtp
);
3011 assert(timestamp
>= dtp
->dt_last_timestamp
);
3012 dtp
->dt_last_timestamp
= timestamp
;
3014 if (timestamp
== buf
->dtbd_timestamp
) {
3016 * We've reached the end of the time covered
3017 * by this buffer. If this is the oldest
3018 * buffer, we must do another pass
3019 * to retrieve more data.
3021 dt_put_buf(dtp
, buf
);
3022 if (timestamp
== first_timestamp
&&
3028 if ((rval
= dt_consume_cpu(dtp
, fp
,
3029 buf
->dtbd_cpu
, buf
, B_TRUE
, pf
, rf
, arg
)) != 0)
3031 dt_pq_insert(dtp
->dt_bufq
, buf
);
3034 /* Consume drops. */
3035 for (i
= 0; i
< max_ncpus
; i
++) {
3036 if (drops
[i
] != 0) {
3037 int error
= dt_handle_cpudrop(dtp
, i
,
3038 DTRACEDROP_PRINCIPAL
, drops
[i
]);
3045 * Reduce memory usage by re-allocating smaller buffers
3046 * for the "remnants".
3048 while (buf
= dt_pq_walk(dtp
->dt_bufq
, &cookie
))
3049 dt_realloc_buf(dtp
, buf
, buf
->dtbd_size
);