dt_consume.c revision 268578
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 */
30
31#include <stdlib.h>
32#include <strings.h>
33#include <errno.h>
34#include <unistd.h>
35#include <limits.h>
36#include <assert.h>
37#include <ctype.h>
38#if defined(sun)
39#include <alloca.h>
40#endif
41#include <dt_impl.h>
42#include <dt_pq.h>
43#if !defined(sun)
44#include <libproc_compat.h>
45#endif
46
47#define	DT_MASK_LO 0x00000000FFFFFFFFULL
48
49/*
50 * We declare this here because (1) we need it and (2) we want to avoid a
51 * dependency on libm in libdtrace.
52 */
53static long double
54dt_fabsl(long double x)
55{
56	if (x < 0)
57		return (-x);
58
59	return (x);
60}
61
62static int
63dt_ndigits(long long val)
64{
65	int rval = 1;
66	long long cmp = 10;
67
68	if (val < 0) {
69		val = val == INT64_MIN ? INT64_MAX : -val;
70		rval++;
71	}
72
73	while (val > cmp && cmp > 0) {
74		rval++;
75		cmp *= 10;
76	}
77
78	return (rval < 4 ? 4 : rval);
79}
80
81/*
82 * 128-bit arithmetic functions needed to support the stddev() aggregating
83 * action.
84 */
85static int
86dt_gt_128(uint64_t *a, uint64_t *b)
87{
88	return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
89}
90
91static int
92dt_ge_128(uint64_t *a, uint64_t *b)
93{
94	return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
95}
96
97static int
98dt_le_128(uint64_t *a, uint64_t *b)
99{
100	return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
101}
102
103/*
104 * Shift the 128-bit value in a by b. If b is positive, shift left.
105 * If b is negative, shift right.
106 */
107static void
108dt_shift_128(uint64_t *a, int b)
109{
110	uint64_t mask;
111
112	if (b == 0)
113		return;
114
115	if (b < 0) {
116		b = -b;
117		if (b >= 64) {
118			a[0] = a[1] >> (b - 64);
119			a[1] = 0;
120		} else {
121			a[0] >>= b;
122			mask = 1LL << (64 - b);
123			mask -= 1;
124			a[0] |= ((a[1] & mask) << (64 - b));
125			a[1] >>= b;
126		}
127	} else {
128		if (b >= 64) {
129			a[1] = a[0] << (b - 64);
130			a[0] = 0;
131		} else {
132			a[1] <<= b;
133			mask = a[0] >> (64 - b);
134			a[1] |= mask;
135			a[0] <<= b;
136		}
137	}
138}
139
140static int
141dt_nbits_128(uint64_t *a)
142{
143	int nbits = 0;
144	uint64_t tmp[2];
145	uint64_t zero[2] = { 0, 0 };
146
147	tmp[0] = a[0];
148	tmp[1] = a[1];
149
150	dt_shift_128(tmp, -1);
151	while (dt_gt_128(tmp, zero)) {
152		dt_shift_128(tmp, -1);
153		nbits++;
154	}
155
156	return (nbits);
157}
158
159static void
160dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
161{
162	uint64_t result[2];
163
164	result[0] = minuend[0] - subtrahend[0];
165	result[1] = minuend[1] - subtrahend[1] -
166	    (minuend[0] < subtrahend[0] ? 1 : 0);
167
168	difference[0] = result[0];
169	difference[1] = result[1];
170}
171
172static void
173dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
174{
175	uint64_t result[2];
176
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);
180
181	sum[0] = result[0];
182	sum[1] = result[1];
183}
184
185/*
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.
189 *
190 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
191 *     hi1 * hi2 << 64 +
192 *     hi1 * lo2 << 32 +
193 *     hi2 * lo1 << 32 +
194 *     lo1 * lo2
195 */
196static void
197dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
198{
199	uint64_t hi1, hi2, lo1, lo2;
200	uint64_t tmp[2];
201
202	hi1 = factor1 >> 32;
203	hi2 = factor2 >> 32;
204
205	lo1 = factor1 & DT_MASK_LO;
206	lo2 = factor2 & DT_MASK_LO;
207
208	product[0] = lo1 * lo2;
209	product[1] = hi1 * hi2;
210
211	tmp[0] = hi1 * lo2;
212	tmp[1] = 0;
213	dt_shift_128(tmp, 32);
214	dt_add_128(product, tmp, product);
215
216	tmp[0] = hi2 * lo1;
217	tmp[1] = 0;
218	dt_shift_128(tmp, 32);
219	dt_add_128(product, tmp, product);
220}
221
222/*
223 * This is long-hand division.
224 *
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.
229 */
230static void
231dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
232{
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 };
238	int log = 0;
239
240	assert(divisor != 0);
241
242	divisor_128[0] = divisor;
243	divisor_128[1] = 0;
244
245	remainder[0] = dividend[0];
246	remainder[1] = dividend[1];
247
248	subtrahend[0] = divisor;
249	subtrahend[1] = 0;
250
251	while (divisor > 0) {
252		log++;
253		divisor >>= 1;
254	}
255
256	dt_shift_128(subtrahend, 128 - log);
257	dt_shift_128(mask, 128 - log);
258
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];
264		}
265
266		dt_shift_128(subtrahend, -1);
267		dt_shift_128(mask, -1);
268	}
269
270	quotient[0] = result[0];
271	quotient[1] = result[1];
272}
273
274/*
275 * This is the long-hand method of calculating a square root.
276 * The algorithm is as follows:
277 *
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.)
290 *
291 * In decimal, the square root of 582,734 would be calculated as so:
292 *
293 *     __7__6__3
294 *    | 58 27 34
295 *     -49       (7^2 == 49 => 7 is the first digit in the square root)
296 *      --
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)
303 *          5 65 (remainder)
304 *
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?
309 *
310 * In binary, the square root of 11011011 would be calculated as so:
311 *
312 *     __1__1__1__0
313 *    | 11 01 10 11
314 *      01          (0 << 2 + 1 == 1 < 11 => this bit is 1)
315 *      --
316 *      10 01 10 11
317 * 101   1 01       (1 << 2 + 1 == 101 < 1001 => next bit is 1)
318 *      -----
319 *       1 00 10 11
320 * 1101    11 01    (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
321 *       -------
322 *          1 01 11
323 * 11101    1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
324 *
325 */
326static uint64_t
327dt_sqrt_128(uint64_t *square)
328{
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;
335	int i;
336
337	bit_pairs = dt_nbits_128(square) / 2;
338	pair_shift = bit_pairs * 2;
339
340	for (i = 0; i <= bit_pairs; i++) {
341		/*
342		 * Bring down the next pair of bits.
343		 */
344		next_pair[0] = square[0];
345		next_pair[1] = square[1];
346		dt_shift_128(next_pair, -pair_shift);
347		next_pair[0] &= 0x3;
348		next_pair[1] = 0;
349
350		dt_shift_128(diff, 2);
351		dt_add_128(diff, next_pair, diff);
352
353		/*
354		 * next_try = R << 2 + 1
355		 */
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);
360
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);
365		} else {
366			dt_shift_128(result, 1);
367		}
368
369		pair_shift -= 2;
370	}
371
372	assert(result[1] == 0);
373
374	return (result[0]);
375}
376
377uint64_t
378dt_stddev(uint64_t *data, uint64_t normal)
379{
380	uint64_t avg_of_squares[2];
381	uint64_t square_of_avg[2];
382	int64_t norm_avg;
383	uint64_t diff[2];
384
385	/*
386	 * The standard approximation for standard deviation is
387	 * sqrt(average(x**2) - average(x)**2), i.e. the square root
388	 * of the average of the squares minus the square of the average.
389	 */
390	dt_divide_128(data + 2, normal, avg_of_squares);
391	dt_divide_128(avg_of_squares, data[0], avg_of_squares);
392
393	norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
394
395	if (norm_avg < 0)
396		norm_avg = -norm_avg;
397
398	dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
399
400	dt_subtract_128(avg_of_squares, square_of_avg, diff);
401
402	return (dt_sqrt_128(diff));
403}
404
405static int
406dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
407    dtrace_bufdesc_t *buf, size_t offs)
408{
409	dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
410	dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
411	char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
412	dtrace_flowkind_t flow = DTRACEFLOW_NONE;
413	const char *str = NULL;
414	static const char *e_str[2] = { " -> ", " => " };
415	static const char *r_str[2] = { " <- ", " <= " };
416	static const char *ent = "entry", *ret = "return";
417	static int entlen = 0, retlen = 0;
418	dtrace_epid_t next, id = epd->dtepd_epid;
419	int rval;
420
421	if (entlen == 0) {
422		assert(retlen == 0);
423		entlen = strlen(ent);
424		retlen = strlen(ret);
425	}
426
427	/*
428	 * If the name of the probe is "entry" or ends with "-entry", we
429	 * treat it as an entry; if it is "return" or ends with "-return",
430	 * we treat it as a return.  (This allows application-provided probes
431	 * like "method-entry" or "function-entry" to participate in flow
432	 * indentation -- without accidentally misinterpreting popular probe
433	 * names like "carpentry", "gentry" or "Coventry".)
434	 */
435	if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
436	    (sub == n || sub[-1] == '-')) {
437		flow = DTRACEFLOW_ENTRY;
438		str = e_str[strcmp(p, "syscall") == 0];
439	} else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
440	    (sub == n || sub[-1] == '-')) {
441		flow = DTRACEFLOW_RETURN;
442		str = r_str[strcmp(p, "syscall") == 0];
443	}
444
445	/*
446	 * If we're going to indent this, we need to check the ID of our last
447	 * call.  If we're looking at the same probe ID but a different EPID,
448	 * we _don't_ want to indent.  (Yes, there are some minor holes in
449	 * this scheme -- it's a heuristic.)
450	 */
451	if (flow == DTRACEFLOW_ENTRY) {
452		if ((last != DTRACE_EPIDNONE && id != last &&
453		    pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
454			flow = DTRACEFLOW_NONE;
455	}
456
457	/*
458	 * If we're going to unindent this, it's more difficult to see if
459	 * we don't actually want to unindent it -- we need to look at the
460	 * _next_ EPID.
461	 */
462	if (flow == DTRACEFLOW_RETURN) {
463		offs += epd->dtepd_size;
464
465		do {
466			if (offs >= buf->dtbd_size)
467				goto out;
468
469			next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
470
471			if (next == DTRACE_EPIDNONE)
472				offs += sizeof (id);
473		} while (next == DTRACE_EPIDNONE);
474
475		if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
476			return (rval);
477
478		if (next != id && npd->dtpd_id == pd->dtpd_id)
479			flow = DTRACEFLOW_NONE;
480	}
481
482out:
483	if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
484		data->dtpda_prefix = str;
485	} else {
486		data->dtpda_prefix = "| ";
487	}
488
489	if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
490		data->dtpda_indent -= 2;
491
492	data->dtpda_flow = flow;
493
494	return (0);
495}
496
497static int
498dt_nullprobe()
499{
500	return (DTRACE_CONSUME_THIS);
501}
502
503static int
504dt_nullrec()
505{
506	return (DTRACE_CONSUME_NEXT);
507}
508
509static void
510dt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total)
511{
512	long double val = dt_fabsl((long double)datum);
513
514	if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) {
515		*total += val;
516		return;
517	}
518
519	/*
520	 * If we're zooming in on an aggregation, we want the height of the
521	 * highest value to be approximately 95% of total bar height -- so we
522	 * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
523	 * our highest value.
524	 */
525	val *= 1 / DTRACE_AGGZOOM_MAX;
526
527	if (*total < val)
528		*total = val;
529}
530
531static int
532dt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width)
533{
534	return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n",
535	    width ? width : 16, width ? "key" : "value",
536	    "------------- Distribution -------------", "count"));
537}
538
539static int
540dt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width,
541    const dtrace_aggdata_t *aggdata, dtrace_actkind_t action)
542{
543	int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin;
544	int minwidth, maxwidth, i;
545
546	assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE);
547
548	if (action == DTRACEAGG_QUANTIZE) {
549		if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
550			min--;
551
552		if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
553			max++;
554
555		minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min));
556		maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max));
557	} else {
558		maxwidth = 8;
559		minwidth = maxwidth - 1;
560		max++;
561	}
562
563	if (dt_printf(dtp, fp, "\n%*s %*s .",
564	    width, width > 0 ? "key" : "", minwidth, "min") < 0)
565		return (-1);
566
567	for (i = min; i <= max; i++) {
568		if (dt_printf(dtp, fp, "-") < 0)
569			return (-1);
570	}
571
572	return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max"));
573}
574
575/*
576 * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
577 * inclusive) to represent aggregations via UTF-8 -- which are expressed via
578 * 3-byte UTF-8 sequences.
579 */
580#define	DTRACE_AGGUTF8_FULL	0x2588
581#define	DTRACE_AGGUTF8_BASE	0x258f
582#define	DTRACE_AGGUTF8_LEVELS	8
583
584#define	DTRACE_AGGUTF8_BYTE0(val)	(0xe0 | ((val) >> 12))
585#define	DTRACE_AGGUTF8_BYTE1(val)	(0x80 | (((val) >> 6) & 0x3f))
586#define	DTRACE_AGGUTF8_BYTE2(val)	(0x80 | ((val) & 0x3f))
587
588static int
589dt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
590    uint64_t normal, long double total)
591{
592	uint_t len = 40, i, whole, partial;
593	long double f = (dt_fabsl((long double)val) * len) / total;
594	const char *spaces = "                                        ";
595
596	whole = (uint_t)f;
597	partial = (uint_t)((f - (long double)(uint_t)f) *
598	    (long double)DTRACE_AGGUTF8_LEVELS);
599
600	if (dt_printf(dtp, fp, "|") < 0)
601		return (-1);
602
603	for (i = 0; i < whole; i++) {
604		if (dt_printf(dtp, fp, "%c%c%c",
605		    DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL),
606		    DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL),
607		    DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0)
608			return (-1);
609	}
610
611	if (partial != 0) {
612		partial = DTRACE_AGGUTF8_BASE - (partial - 1);
613
614		if (dt_printf(dtp, fp, "%c%c%c",
615		    DTRACE_AGGUTF8_BYTE0(partial),
616		    DTRACE_AGGUTF8_BYTE1(partial),
617		    DTRACE_AGGUTF8_BYTE2(partial)) < 0)
618			return (-1);
619
620		i++;
621	}
622
623	return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i,
624	    (long long)val / normal));
625}
626
627static int
628dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
629    uint64_t normal, long double total, char positives, char negatives)
630{
631	long double f;
632	uint_t depth, len = 40;
633
634	const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
635	const char *spaces = "                                        ";
636
637	assert(strlen(ats) == len && strlen(spaces) == len);
638	assert(!(total == 0 && (positives || negatives)));
639	assert(!(val < 0 && !negatives));
640	assert(!(val > 0 && !positives));
641	assert(!(val != 0 && total == 0));
642
643	if (!negatives) {
644		if (positives) {
645			if (dtp->dt_encoding == DT_ENCODING_UTF8) {
646				return (dt_print_quantline_utf8(dtp, fp, val,
647				    normal, total));
648			}
649
650			f = (dt_fabsl((long double)val) * len) / total;
651			depth = (uint_t)(f + 0.5);
652		} else {
653			depth = 0;
654		}
655
656		return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
657		    spaces + depth, (long long)val / normal));
658	}
659
660	if (!positives) {
661		f = (dt_fabsl((long double)val) * len) / total;
662		depth = (uint_t)(f + 0.5);
663
664		return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
665		    ats + len - depth, (long long)val / normal));
666	}
667
668	/*
669	 * If we're here, we have both positive and negative bucket values.
670	 * To express this graphically, we're going to generate both positive
671	 * and negative bars separated by a centerline.  These bars are half
672	 * the size of normal quantize()/lquantize() bars, so we divide the
673	 * length in half before calculating the bar length.
674	 */
675	len /= 2;
676	ats = &ats[len];
677	spaces = &spaces[len];
678
679	f = (dt_fabsl((long double)val) * len) / total;
680	depth = (uint_t)(f + 0.5);
681
682	if (val <= 0) {
683		return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
684		    ats + len - depth, len, "", (long long)val / normal));
685	} else {
686		return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
687		    ats + len - depth, spaces + depth,
688		    (long long)val / normal));
689	}
690}
691
692/*
693 * As with UTF-8 printing of aggregations, we use a subset of the Unicode
694 * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
695 * aggregation.
696 */
697#define	DTRACE_AGGPACK_BASE	0x2581
698#define	DTRACE_AGGPACK_LEVELS	8
699
700static int
701dt_print_packed(dtrace_hdl_t *dtp, FILE *fp,
702    long double datum, long double total)
703{
704	static boolean_t utf8_checked = B_FALSE;
705	static boolean_t utf8;
706	char *ascii = "__xxxxXX";
707	char *neg = "vvvvVV";
708	unsigned int len;
709	long double val;
710
711	if (!utf8_checked) {
712		char *term;
713
714		/*
715		 * We want to determine if we can reasonably emit UTF-8 for our
716		 * packed aggregation.  To do this, we will check for terminals
717		 * that are known to be primitive to emit UTF-8 on these.
718		 */
719		utf8_checked = B_TRUE;
720
721		if (dtp->dt_encoding == DT_ENCODING_ASCII) {
722			utf8 = B_FALSE;
723		} else if (dtp->dt_encoding == DT_ENCODING_UTF8) {
724			utf8 = B_TRUE;
725		} else if ((term = getenv("TERM")) != NULL &&
726		    (strcmp(term, "sun") == 0 ||
727		    strcmp(term, "sun-color") == 0) ||
728		    strcmp(term, "dumb") == 0) {
729			utf8 = B_FALSE;
730		} else {
731			utf8 = B_TRUE;
732		}
733	}
734
735	if (datum == 0)
736		return (dt_printf(dtp, fp, " "));
737
738	if (datum < 0) {
739		len = strlen(neg);
740		val = dt_fabsl(datum * (len - 1)) / total;
741		return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)]));
742	}
743
744	if (utf8) {
745		int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum *
746		    (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5);
747
748		return (dt_printf(dtp, fp, "%c%c%c",
749		    DTRACE_AGGUTF8_BYTE0(block),
750		    DTRACE_AGGUTF8_BYTE1(block),
751		    DTRACE_AGGUTF8_BYTE2(block)));
752	}
753
754	len = strlen(ascii);
755	val = (datum * (len - 1)) / total;
756	return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)]));
757}
758
759int
760dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
761    size_t size, uint64_t normal)
762{
763	const int64_t *data = addr;
764	int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
765	long double total = 0;
766	char positives = 0, negatives = 0;
767
768	if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
769		return (dt_set_errno(dtp, EDT_DMISMATCH));
770
771	while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
772		first_bin++;
773
774	if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
775		/*
776		 * There isn't any data.  This is possible if the aggregation
777		 * has been clear()'d or if negative increment values have been
778		 * used.  Regardless, we'll print the buckets around 0.
779		 */
780		first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
781		last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
782	} else {
783		if (first_bin > 0)
784			first_bin--;
785
786		while (last_bin > 0 && data[last_bin] == 0)
787			last_bin--;
788
789		if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
790			last_bin++;
791	}
792
793	for (i = first_bin; i <= last_bin; i++) {
794		positives |= (data[i] > 0);
795		negatives |= (data[i] < 0);
796		dt_quantize_total(dtp, data[i], &total);
797	}
798
799	if (dt_print_quanthdr(dtp, fp, 0) < 0)
800		return (-1);
801
802	for (i = first_bin; i <= last_bin; i++) {
803		if (dt_printf(dtp, fp, "%16lld ",
804		    (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
805			return (-1);
806
807		if (dt_print_quantline(dtp, fp, data[i], normal, total,
808		    positives, negatives) < 0)
809			return (-1);
810	}
811
812	return (0);
813}
814
815int
816dt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
817    size_t size, const dtrace_aggdata_t *aggdata)
818{
819	const int64_t *data = addr;
820	long double total = 0, count = 0;
821	int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i;
822	int64_t minval, maxval;
823
824	if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
825		return (dt_set_errno(dtp, EDT_DMISMATCH));
826
827	if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
828		min--;
829
830	if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
831		max++;
832
833	minval = DTRACE_QUANTIZE_BUCKETVAL(min);
834	maxval = DTRACE_QUANTIZE_BUCKETVAL(max);
835
836	if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval),
837	    (long long)minval) < 0)
838		return (-1);
839
840	for (i = min; i <= max; i++) {
841		dt_quantize_total(dtp, data[i], &total);
842		count += data[i];
843	}
844
845	for (i = min; i <= max; i++) {
846		if (dt_print_packed(dtp, fp, data[i], total) < 0)
847			return (-1);
848	}
849
850	if (dt_printf(dtp, fp, ": %*lld | %lld\n",
851	    -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0)
852		return (-1);
853
854	return (0);
855}
856
857int
858dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
859    size_t size, uint64_t normal)
860{
861	const int64_t *data = addr;
862	int i, first_bin, last_bin, base;
863	uint64_t arg;
864	long double total = 0;
865	uint16_t step, levels;
866	char positives = 0, negatives = 0;
867
868	if (size < sizeof (uint64_t))
869		return (dt_set_errno(dtp, EDT_DMISMATCH));
870
871	arg = *data++;
872	size -= sizeof (uint64_t);
873
874	base = DTRACE_LQUANTIZE_BASE(arg);
875	step = DTRACE_LQUANTIZE_STEP(arg);
876	levels = DTRACE_LQUANTIZE_LEVELS(arg);
877
878	first_bin = 0;
879	last_bin = levels + 1;
880
881	if (size != sizeof (uint64_t) * (levels + 2))
882		return (dt_set_errno(dtp, EDT_DMISMATCH));
883
884	while (first_bin <= levels + 1 && data[first_bin] == 0)
885		first_bin++;
886
887	if (first_bin > levels + 1) {
888		first_bin = 0;
889		last_bin = 2;
890	} else {
891		if (first_bin > 0)
892			first_bin--;
893
894		while (last_bin > 0 && data[last_bin] == 0)
895			last_bin--;
896
897		if (last_bin < levels + 1)
898			last_bin++;
899	}
900
901	for (i = first_bin; i <= last_bin; i++) {
902		positives |= (data[i] > 0);
903		negatives |= (data[i] < 0);
904		dt_quantize_total(dtp, data[i], &total);
905	}
906
907	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
908	    "------------- Distribution -------------", "count") < 0)
909		return (-1);
910
911	for (i = first_bin; i <= last_bin; i++) {
912		char c[32];
913		int err;
914
915		if (i == 0) {
916			(void) snprintf(c, sizeof (c), "< %d", base);
917			err = dt_printf(dtp, fp, "%16s ", c);
918		} else if (i == levels + 1) {
919			(void) snprintf(c, sizeof (c), ">= %d",
920			    base + (levels * step));
921			err = dt_printf(dtp, fp, "%16s ", c);
922		} else {
923			err = dt_printf(dtp, fp, "%16d ",
924			    base + (i - 1) * step);
925		}
926
927		if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
928		    total, positives, negatives) < 0)
929			return (-1);
930	}
931
932	return (0);
933}
934
935/*ARGSUSED*/
936int
937dt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
938    size_t size, const dtrace_aggdata_t *aggdata)
939{
940	const int64_t *data = addr;
941	long double total = 0, count = 0;
942	int min, max, base, err;
943	uint64_t arg;
944	uint16_t step, levels;
945	char c[32];
946	unsigned int i;
947
948	if (size < sizeof (uint64_t))
949		return (dt_set_errno(dtp, EDT_DMISMATCH));
950
951	arg = *data++;
952	size -= sizeof (uint64_t);
953
954	base = DTRACE_LQUANTIZE_BASE(arg);
955	step = DTRACE_LQUANTIZE_STEP(arg);
956	levels = DTRACE_LQUANTIZE_LEVELS(arg);
957
958	if (size != sizeof (uint64_t) * (levels + 2))
959		return (dt_set_errno(dtp, EDT_DMISMATCH));
960
961	min = 0;
962	max = levels + 1;
963
964	if (min == 0) {
965		(void) snprintf(c, sizeof (c), "< %d", base);
966		err = dt_printf(dtp, fp, "%8s :", c);
967	} else {
968		err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step);
969	}
970
971	if (err < 0)
972		return (-1);
973
974	for (i = min; i <= max; i++) {
975		dt_quantize_total(dtp, data[i], &total);
976		count += data[i];
977	}
978
979	for (i = min; i <= max; i++) {
980		if (dt_print_packed(dtp, fp, data[i], total) < 0)
981			return (-1);
982	}
983
984	(void) snprintf(c, sizeof (c), ">= %d", base + (levels * step));
985	return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count));
986}
987
988int
989dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
990    size_t size, uint64_t normal)
991{
992	int i, first_bin, last_bin, bin = 1, order, levels;
993	uint16_t factor, low, high, nsteps;
994	const int64_t *data = addr;
995	int64_t value = 1, next, step;
996	char positives = 0, negatives = 0;
997	long double total = 0;
998	uint64_t arg;
999	char c[32];
1000
1001	if (size < sizeof (uint64_t))
1002		return (dt_set_errno(dtp, EDT_DMISMATCH));
1003
1004	arg = *data++;
1005	size -= sizeof (uint64_t);
1006
1007	factor = DTRACE_LLQUANTIZE_FACTOR(arg);
1008	low = DTRACE_LLQUANTIZE_LOW(arg);
1009	high = DTRACE_LLQUANTIZE_HIGH(arg);
1010	nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
1011
1012	/*
1013	 * We don't expect to be handed invalid llquantize() parameters here,
1014	 * but sanity check them (to a degree) nonetheless.
1015	 */
1016	if (size > INT32_MAX || factor < 2 || low >= high ||
1017	    nsteps == 0 || factor > nsteps)
1018		return (dt_set_errno(dtp, EDT_DMISMATCH));
1019
1020	levels = (int)size / sizeof (uint64_t);
1021
1022	first_bin = 0;
1023	last_bin = levels - 1;
1024
1025	while (first_bin < levels && data[first_bin] == 0)
1026		first_bin++;
1027
1028	if (first_bin == levels) {
1029		first_bin = 0;
1030		last_bin = 1;
1031	} else {
1032		if (first_bin > 0)
1033			first_bin--;
1034
1035		while (last_bin > 0 && data[last_bin] == 0)
1036			last_bin--;
1037
1038		if (last_bin < levels - 1)
1039			last_bin++;
1040	}
1041
1042	for (i = first_bin; i <= last_bin; i++) {
1043		positives |= (data[i] > 0);
1044		negatives |= (data[i] < 0);
1045		dt_quantize_total(dtp, data[i], &total);
1046	}
1047
1048	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
1049	    "------------- Distribution -------------", "count") < 0)
1050		return (-1);
1051
1052	for (order = 0; order < low; order++)
1053		value *= factor;
1054
1055	next = value * factor;
1056	step = next > nsteps ? next / nsteps : 1;
1057
1058	if (first_bin == 0) {
1059		(void) snprintf(c, sizeof (c), "< %lld", (long long)value);
1060
1061		if (dt_printf(dtp, fp, "%16s ", c) < 0)
1062			return (-1);
1063
1064		if (dt_print_quantline(dtp, fp, data[0], normal,
1065		    total, positives, negatives) < 0)
1066			return (-1);
1067	}
1068
1069	while (order <= high) {
1070		if (bin >= first_bin && bin <= last_bin) {
1071			if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
1072				return (-1);
1073
1074			if (dt_print_quantline(dtp, fp, data[bin],
1075			    normal, total, positives, negatives) < 0)
1076				return (-1);
1077		}
1078
1079		assert(value < next);
1080		bin++;
1081
1082		if ((value += step) != next)
1083			continue;
1084
1085		next = value * factor;
1086		step = next > nsteps ? next / nsteps : 1;
1087		order++;
1088	}
1089
1090	if (last_bin < bin)
1091		return (0);
1092
1093	assert(last_bin == bin);
1094	(void) snprintf(c, sizeof (c), ">= %lld", (long long)value);
1095
1096	if (dt_printf(dtp, fp, "%16s ", c) < 0)
1097		return (-1);
1098
1099	return (dt_print_quantline(dtp, fp, data[bin], normal,
1100	    total, positives, negatives));
1101}
1102
1103/*ARGSUSED*/
1104static int
1105dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1106    size_t size, uint64_t normal)
1107{
1108	/* LINTED - alignment */
1109	int64_t *data = (int64_t *)addr;
1110
1111	return (dt_printf(dtp, fp, " %16lld", data[0] ?
1112	    (long long)(data[1] / (int64_t)normal / data[0]) : 0));
1113}
1114
1115/*ARGSUSED*/
1116static int
1117dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1118    size_t size, uint64_t normal)
1119{
1120	/* LINTED - alignment */
1121	uint64_t *data = (uint64_t *)addr;
1122
1123	return (dt_printf(dtp, fp, " %16llu", data[0] ?
1124	    (unsigned long long) dt_stddev(data, normal) : 0));
1125}
1126
1127/*ARGSUSED*/
1128static int
1129dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1130    size_t nbytes, int width, int quiet, int forceraw)
1131{
1132	/*
1133	 * If the byte stream is a series of printable characters, followed by
1134	 * a terminating byte, we print it out as a string.  Otherwise, we
1135	 * assume that it's something else and just print the bytes.
1136	 */
1137	int i, j, margin = 5;
1138	char *c = (char *)addr;
1139
1140	if (nbytes == 0)
1141		return (0);
1142
1143	if (forceraw)
1144		goto raw;
1145
1146	if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
1147		goto raw;
1148
1149	for (i = 0; i < nbytes; i++) {
1150		/*
1151		 * We define a "printable character" to be one for which
1152		 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
1153		 * or a character which is either backspace or the bell.
1154		 * Backspace and the bell are regrettably special because
1155		 * they fail the first two tests -- and yet they are entirely
1156		 * printable.  These are the only two control characters that
1157		 * have meaning for the terminal and for which isprint(3C) and
1158		 * isspace(3C) return 0.
1159		 */
1160		if (isprint(c[i]) || isspace(c[i]) ||
1161		    c[i] == '\b' || c[i] == '\a')
1162			continue;
1163
1164		if (c[i] == '\0' && i > 0) {
1165			/*
1166			 * This looks like it might be a string.  Before we
1167			 * assume that it is indeed a string, check the
1168			 * remainder of the byte range; if it contains
1169			 * additional non-nul characters, we'll assume that
1170			 * it's a binary stream that just happens to look like
1171			 * a string, and we'll print out the individual bytes.
1172			 */
1173			for (j = i + 1; j < nbytes; j++) {
1174				if (c[j] != '\0')
1175					break;
1176			}
1177
1178			if (j != nbytes)
1179				break;
1180
1181			if (quiet) {
1182				return (dt_printf(dtp, fp, "%s", c));
1183			} else {
1184				return (dt_printf(dtp, fp, " %s%*s",
1185				    width < 0 ? " " : "", width, c));
1186			}
1187		}
1188
1189		break;
1190	}
1191
1192	if (i == nbytes) {
1193		/*
1194		 * The byte range is all printable characters, but there is
1195		 * no trailing nul byte.  We'll assume that it's a string and
1196		 * print it as such.
1197		 */
1198		char *s = alloca(nbytes + 1);
1199		bcopy(c, s, nbytes);
1200		s[nbytes] = '\0';
1201		return (dt_printf(dtp, fp, "  %-*s", width, s));
1202	}
1203
1204raw:
1205	if (dt_printf(dtp, fp, "\n%*s      ", margin, "") < 0)
1206		return (-1);
1207
1208	for (i = 0; i < 16; i++)
1209		if (dt_printf(dtp, fp, "  %c", "0123456789abcdef"[i]) < 0)
1210			return (-1);
1211
1212	if (dt_printf(dtp, fp, "  0123456789abcdef\n") < 0)
1213		return (-1);
1214
1215
1216	for (i = 0; i < nbytes; i += 16) {
1217		if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
1218			return (-1);
1219
1220		for (j = i; j < i + 16 && j < nbytes; j++) {
1221			if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
1222				return (-1);
1223		}
1224
1225		while (j++ % 16) {
1226			if (dt_printf(dtp, fp, "   ") < 0)
1227				return (-1);
1228		}
1229
1230		if (dt_printf(dtp, fp, "  ") < 0)
1231			return (-1);
1232
1233		for (j = i; j < i + 16 && j < nbytes; j++) {
1234			if (dt_printf(dtp, fp, "%c",
1235			    c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
1236				return (-1);
1237		}
1238
1239		if (dt_printf(dtp, fp, "\n") < 0)
1240			return (-1);
1241	}
1242
1243	return (0);
1244}
1245
1246int
1247dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1248    caddr_t addr, int depth, int size)
1249{
1250	dtrace_syminfo_t dts;
1251	GElf_Sym sym;
1252	int i, indent;
1253	char c[PATH_MAX * 2];
1254	uint64_t pc;
1255
1256	if (dt_printf(dtp, fp, "\n") < 0)
1257		return (-1);
1258
1259	if (format == NULL)
1260		format = "%s";
1261
1262	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1263		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1264	else
1265		indent = _dtrace_stkindent;
1266
1267	for (i = 0; i < depth; i++) {
1268		switch (size) {
1269		case sizeof (uint32_t):
1270			/* LINTED - alignment */
1271			pc = *((uint32_t *)addr);
1272			break;
1273
1274		case sizeof (uint64_t):
1275			/* LINTED - alignment */
1276			pc = *((uint64_t *)addr);
1277			break;
1278
1279		default:
1280			return (dt_set_errno(dtp, EDT_BADSTACKPC));
1281		}
1282
1283		if (pc == 0)
1284			break;
1285
1286		addr += size;
1287
1288		if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
1289			return (-1);
1290
1291		if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1292			if (pc > sym.st_value) {
1293				(void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
1294				    dts.dts_object, dts.dts_name,
1295				    (u_longlong_t)(pc - sym.st_value));
1296			} else {
1297				(void) snprintf(c, sizeof (c), "%s`%s",
1298				    dts.dts_object, dts.dts_name);
1299			}
1300		} else {
1301			/*
1302			 * We'll repeat the lookup, but this time we'll specify
1303			 * a NULL GElf_Sym -- indicating that we're only
1304			 * interested in the containing module.
1305			 */
1306			if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1307				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1308				    dts.dts_object, (u_longlong_t)pc);
1309			} else {
1310				(void) snprintf(c, sizeof (c), "0x%llx",
1311				    (u_longlong_t)pc);
1312			}
1313		}
1314
1315		if (dt_printf(dtp, fp, format, c) < 0)
1316			return (-1);
1317
1318		if (dt_printf(dtp, fp, "\n") < 0)
1319			return (-1);
1320	}
1321
1322	return (0);
1323}
1324
1325int
1326dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327    caddr_t addr, uint64_t arg)
1328{
1329	/* LINTED - alignment */
1330	uint64_t *pc = (uint64_t *)addr;
1331	uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1332	uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1333	const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1334	const char *str = strsize ? strbase : NULL;
1335	int err = 0;
1336
1337	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1338	struct ps_prochandle *P;
1339	GElf_Sym sym;
1340	int i, indent;
1341	pid_t pid;
1342
1343	if (depth == 0)
1344		return (0);
1345
1346	pid = (pid_t)*pc++;
1347
1348	if (dt_printf(dtp, fp, "\n") < 0)
1349		return (-1);
1350
1351	if (format == NULL)
1352		format = "%s";
1353
1354	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1355		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1356	else
1357		indent = _dtrace_stkindent;
1358
1359	/*
1360	 * Ultimately, we need to add an entry point in the library vector for
1361	 * determining <symbol, offset> from <pid, address>.  For now, if
1362	 * this is a vector open, we just print the raw address or string.
1363	 */
1364	if (dtp->dt_vector == NULL)
1365		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1366	else
1367		P = NULL;
1368
1369	if (P != NULL)
1370		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1371
1372	for (i = 0; i < depth && pc[i] != 0; i++) {
1373		const prmap_t *map;
1374
1375		if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1376			break;
1377
1378		if (P != NULL && Plookup_by_addr(P, pc[i],
1379		    name, sizeof (name), &sym) == 0) {
1380			(void) Pobjname(P, pc[i], objname, sizeof (objname));
1381
1382			if (pc[i] > sym.st_value) {
1383				(void) snprintf(c, sizeof (c),
1384				    "%s`%s+0x%llx", dt_basename(objname), name,
1385				    (u_longlong_t)(pc[i] - sym.st_value));
1386			} else {
1387				(void) snprintf(c, sizeof (c),
1388				    "%s`%s", dt_basename(objname), name);
1389			}
1390		} else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1391		    (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1392		    (map->pr_mflags & MA_WRITE)))) {
1393			/*
1394			 * If the current string pointer in the string table
1395			 * does not point to an empty string _and_ the program
1396			 * counter falls in a writable region, we'll use the
1397			 * string from the string table instead of the raw
1398			 * address.  This last condition is necessary because
1399			 * some (broken) ustack helpers will return a string
1400			 * even for a program counter that they can't
1401			 * identify.  If we have a string for a program
1402			 * counter that falls in a segment that isn't
1403			 * writable, we assume that we have fallen into this
1404			 * case and we refuse to use the string.
1405			 */
1406			(void) snprintf(c, sizeof (c), "%s", str);
1407		} else {
1408			if (P != NULL && Pobjname(P, pc[i], objname,
1409			    sizeof (objname)) != 0) {
1410				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1411				    dt_basename(objname), (u_longlong_t)pc[i]);
1412			} else {
1413				(void) snprintf(c, sizeof (c), "0x%llx",
1414				    (u_longlong_t)pc[i]);
1415			}
1416		}
1417
1418		if ((err = dt_printf(dtp, fp, format, c)) < 0)
1419			break;
1420
1421		if ((err = dt_printf(dtp, fp, "\n")) < 0)
1422			break;
1423
1424		if (str != NULL && str[0] == '@') {
1425			/*
1426			 * If the first character of the string is an "at" sign,
1427			 * then the string is inferred to be an annotation --
1428			 * and it is printed out beneath the frame and offset
1429			 * with brackets.
1430			 */
1431			if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1432				break;
1433
1434			(void) snprintf(c, sizeof (c), "  [ %s ]", &str[1]);
1435
1436			if ((err = dt_printf(dtp, fp, format, c)) < 0)
1437				break;
1438
1439			if ((err = dt_printf(dtp, fp, "\n")) < 0)
1440				break;
1441		}
1442
1443		if (str != NULL) {
1444			str += strlen(str) + 1;
1445			if (str - strbase >= strsize)
1446				str = NULL;
1447		}
1448	}
1449
1450	if (P != NULL) {
1451		dt_proc_unlock(dtp, P);
1452		dt_proc_release(dtp, P);
1453	}
1454
1455	return (err);
1456}
1457
1458static int
1459dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1460{
1461	/* LINTED - alignment */
1462	uint64_t pid = ((uint64_t *)addr)[0];
1463	/* LINTED - alignment */
1464	uint64_t pc = ((uint64_t *)addr)[1];
1465	const char *format = "  %-50s";
1466	char *s;
1467	int n, len = 256;
1468
1469	if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1470		struct ps_prochandle *P;
1471
1472		if ((P = dt_proc_grab(dtp, pid,
1473		    PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1474			GElf_Sym sym;
1475
1476			dt_proc_lock(dtp, P);
1477
1478			if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1479				pc = sym.st_value;
1480
1481			dt_proc_unlock(dtp, P);
1482			dt_proc_release(dtp, P);
1483		}
1484	}
1485
1486	do {
1487		n = len;
1488		s = alloca(n);
1489	} while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1490
1491	return (dt_printf(dtp, fp, format, s));
1492}
1493
1494int
1495dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1496{
1497	/* LINTED - alignment */
1498	uint64_t pid = ((uint64_t *)addr)[0];
1499	/* LINTED - alignment */
1500	uint64_t pc = ((uint64_t *)addr)[1];
1501	int err = 0;
1502
1503	char objname[PATH_MAX], c[PATH_MAX * 2];
1504	struct ps_prochandle *P;
1505
1506	if (format == NULL)
1507		format = "  %-50s";
1508
1509	/*
1510	 * See the comment in dt_print_ustack() for the rationale for
1511	 * printing raw addresses in the vectored case.
1512	 */
1513	if (dtp->dt_vector == NULL)
1514		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1515	else
1516		P = NULL;
1517
1518	if (P != NULL)
1519		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1520
1521	if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) {
1522		(void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1523	} else {
1524		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1525	}
1526
1527	err = dt_printf(dtp, fp, format, c);
1528
1529	if (P != NULL) {
1530		dt_proc_unlock(dtp, P);
1531		dt_proc_release(dtp, P);
1532	}
1533
1534	return (err);
1535}
1536
1537int
1538dt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1539{
1540	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1541	size_t nbytes = *((uintptr_t *) addr);
1542
1543	return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t),
1544	    nbytes, 50, quiet, 1));
1545}
1546
1547typedef struct dt_type_cbdata {
1548	dtrace_hdl_t		*dtp;
1549	dtrace_typeinfo_t	dtt;
1550	caddr_t			addr;
1551	caddr_t			addrend;
1552	const char		*name;
1553	int			f_type;
1554	int			indent;
1555	int			type_width;
1556	int			name_width;
1557	FILE			*fp;
1558} dt_type_cbdata_t;
1559
1560static int	dt_print_type_data(dt_type_cbdata_t *, ctf_id_t);
1561
1562static int
1563dt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1564{
1565	dt_type_cbdata_t cbdata;
1566	dt_type_cbdata_t *cbdatap = arg;
1567	ssize_t ssz;
1568
1569	if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0)
1570		return (0);
1571
1572	off /= 8;
1573
1574	cbdata = *cbdatap;
1575	cbdata.name = name;
1576	cbdata.addr += off;
1577	cbdata.addrend = cbdata.addr + ssz;
1578
1579	return (dt_print_type_data(&cbdata, type));
1580}
1581
1582static int
1583dt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg)
1584{
1585	char buf[DT_TYPE_NAMELEN];
1586	char *p;
1587	dt_type_cbdata_t *cbdatap = arg;
1588	size_t sz = strlen(name);
1589
1590	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1591
1592	if ((p = strchr(buf, '[')) != NULL)
1593		p[-1] = '\0';
1594	else
1595		p = "";
1596
1597	sz += strlen(p);
1598
1599	if (sz > cbdatap->name_width)
1600		cbdatap->name_width = sz;
1601
1602	sz = strlen(buf);
1603
1604	if (sz > cbdatap->type_width)
1605		cbdatap->type_width = sz;
1606
1607	return (0);
1608}
1609
1610static int
1611dt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type)
1612{
1613	caddr_t addr = cbdatap->addr;
1614	caddr_t addrend = cbdatap->addrend;
1615	char buf[DT_TYPE_NAMELEN];
1616	char *p;
1617	int cnt = 0;
1618	uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type);
1619	ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type);
1620
1621	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1622
1623	if ((p = strchr(buf, '[')) != NULL)
1624		p[-1] = '\0';
1625	else
1626		p = "";
1627
1628	if (cbdatap->f_type) {
1629		int type_width = roundup(cbdatap->type_width + 1, 4);
1630		int name_width = roundup(cbdatap->name_width + 1, 4);
1631
1632		name_width -= strlen(cbdatap->name);
1633
1634		dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s	= ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p);
1635	}
1636
1637	while (addr < addrend) {
1638		dt_type_cbdata_t cbdata;
1639		ctf_arinfo_t arinfo;
1640		ctf_encoding_t cte;
1641		uintptr_t *up;
1642		void *vp = addr;
1643		cbdata = *cbdatap;
1644		cbdata.name = "";
1645		cbdata.addr = addr;
1646		cbdata.addrend = addr + ssz;
1647		cbdata.f_type = 0;
1648		cbdata.indent++;
1649		cbdata.type_width = 0;
1650		cbdata.name_width = 0;
1651
1652		if (cnt > 0)
1653			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,"");
1654
1655		switch (kind) {
1656		case CTF_K_INTEGER:
1657			if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0)
1658				return (-1);
1659			if ((cte.cte_format & CTF_INT_SIGNED) != 0)
1660				switch (cte.cte_bits) {
1661				case 8:
1662					if (isprint(*((char *) vp)))
1663						dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp));
1664					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp));
1665					break;
1666				case 16:
1667					dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp));
1668					break;
1669				case 32:
1670					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp));
1671					break;
1672				case 64:
1673					dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp));
1674					break;
1675				default:
1676					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);
1677					break;
1678				}
1679			else
1680				switch (cte.cte_bits) {
1681				case 8:
1682					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff);
1683					break;
1684				case 16:
1685					dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp));
1686					break;
1687				case 32:
1688					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp));
1689					break;
1690				case 64:
1691					dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp));
1692					break;
1693				default:
1694					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);
1695					break;
1696				}
1697			break;
1698		case CTF_K_FLOAT:
1699			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);
1700			break;
1701		case CTF_K_POINTER:
1702			dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr));
1703			break;
1704		case CTF_K_ARRAY:
1705			if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0)
1706				return (-1);
1707			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,"");
1708			dt_print_type_data(&cbdata, arinfo.ctr_contents);
1709			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1710			break;
1711		case CTF_K_FUNCTION:
1712			dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n");
1713			break;
1714		case CTF_K_STRUCT:
1715			cbdata.f_type = 1;
1716			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1717			    dt_print_type_width, &cbdata) != 0)
1718				return (-1);
1719			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1720			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1721			    dt_print_type_member, &cbdata) != 0)
1722				return (-1);
1723			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1724			break;
1725		case CTF_K_UNION:
1726			cbdata.f_type = 1;
1727			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1728			    dt_print_type_width, &cbdata) != 0)
1729				return (-1);
1730			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1731			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1732			    dt_print_type_member, &cbdata) != 0)
1733				return (-1);
1734			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1735			break;
1736		case CTF_K_ENUM:
1737			dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp)));
1738			break;
1739		case CTF_K_TYPEDEF:
1740			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1741			break;
1742		case CTF_K_VOLATILE:
1743			if (cbdatap->f_type)
1744				dt_printf(cbdatap->dtp, cbdatap->fp, "volatile ");
1745			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1746			break;
1747		case CTF_K_CONST:
1748			if (cbdatap->f_type)
1749				dt_printf(cbdatap->dtp, cbdatap->fp, "const ");
1750			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1751			break;
1752		case CTF_K_RESTRICT:
1753			if (cbdatap->f_type)
1754				dt_printf(cbdatap->dtp, cbdatap->fp, "restrict ");
1755			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1756			break;
1757		default:
1758			break;
1759		}
1760
1761		addr += ssz;
1762		cnt++;
1763	}
1764
1765	return (0);
1766}
1767
1768static int
1769dt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1770{
1771	caddr_t addrend;
1772	char *p;
1773	dtrace_typeinfo_t dtt;
1774	dt_type_cbdata_t cbdata;
1775	int num = 0;
1776	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1777	ssize_t ssz;
1778
1779	if (!quiet)
1780		dt_printf(dtp, fp, "\n");
1781
1782	/* Get the total number of bytes of data buffered. */
1783	size_t nbytes = *((uintptr_t *) addr);
1784	addr += sizeof(uintptr_t);
1785
1786	/*
1787	 * Get the size of the type so that we can check that it matches
1788	 * the CTF data we look up and so that we can figure out how many
1789	 * type elements are buffered.
1790	 */
1791	size_t typs = *((uintptr_t *) addr);
1792	addr += sizeof(uintptr_t);
1793
1794	/*
1795	 * Point to the type string in the buffer. Get it's string
1796	 * length and round it up to become the offset to the start
1797	 * of the buffered type data which we would like to be aligned
1798	 * for easy access.
1799	 */
1800	char *strp = (char *) addr;
1801	int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t));
1802
1803	/*
1804	 * The type string might have a format such as 'int [20]'.
1805	 * Check if there is an array dimension present.
1806	 */
1807	if ((p = strchr(strp, '[')) != NULL) {
1808		/* Strip off the array dimension. */
1809		*p++ = '\0';
1810
1811		for (; *p != '\0' && *p != ']'; p++)
1812			num = num * 10 + *p - '0';
1813	} else
1814		/* No array dimension, so default. */
1815		num = 1;
1816
1817	/* Lookup the CTF type from the type string. */
1818	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, strp, &dtt) < 0)
1819		return (-1);
1820
1821	/* Offset the buffer address to the start of the data... */
1822	addr += offset;
1823
1824	ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1825
1826	if (typs != ssz) {
1827		printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz);
1828		return (-1);
1829	}
1830
1831	cbdata.dtp = dtp;
1832	cbdata.dtt = dtt;
1833	cbdata.name = "";
1834	cbdata.addr = addr;
1835	cbdata.addrend = addr + nbytes;
1836	cbdata.indent = 1;
1837	cbdata.f_type = 1;
1838	cbdata.type_width = 0;
1839	cbdata.name_width = 0;
1840	cbdata.fp = fp;
1841
1842	return (dt_print_type_data(&cbdata, dtt.dtt_type));
1843}
1844
1845static int
1846dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1847{
1848	/* LINTED - alignment */
1849	uint64_t pc = *((uint64_t *)addr);
1850	dtrace_syminfo_t dts;
1851	GElf_Sym sym;
1852	char c[PATH_MAX * 2];
1853
1854	if (format == NULL)
1855		format = "  %-50s";
1856
1857	if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1858		(void) snprintf(c, sizeof (c), "%s`%s",
1859		    dts.dts_object, dts.dts_name);
1860	} else {
1861		/*
1862		 * We'll repeat the lookup, but this time we'll specify a
1863		 * NULL GElf_Sym -- indicating that we're only interested in
1864		 * the containing module.
1865		 */
1866		if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1867			(void) snprintf(c, sizeof (c), "%s`0x%llx",
1868			    dts.dts_object, (u_longlong_t)pc);
1869		} else {
1870			(void) snprintf(c, sizeof (c), "0x%llx",
1871			    (u_longlong_t)pc);
1872		}
1873	}
1874
1875	if (dt_printf(dtp, fp, format, c) < 0)
1876		return (-1);
1877
1878	return (0);
1879}
1880
1881int
1882dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1883{
1884	/* LINTED - alignment */
1885	uint64_t pc = *((uint64_t *)addr);
1886	dtrace_syminfo_t dts;
1887	char c[PATH_MAX * 2];
1888
1889	if (format == NULL)
1890		format = "  %-50s";
1891
1892	if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1893		(void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1894	} else {
1895		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1896	}
1897
1898	if (dt_printf(dtp, fp, format, c) < 0)
1899		return (-1);
1900
1901	return (0);
1902}
1903
1904typedef struct dt_normal {
1905	dtrace_aggvarid_t dtnd_id;
1906	uint64_t dtnd_normal;
1907} dt_normal_t;
1908
1909static int
1910dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1911{
1912	dt_normal_t *normal = arg;
1913	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1914	dtrace_aggvarid_t id = normal->dtnd_id;
1915
1916	if (agg->dtagd_nrecs == 0)
1917		return (DTRACE_AGGWALK_NEXT);
1918
1919	if (agg->dtagd_varid != id)
1920		return (DTRACE_AGGWALK_NEXT);
1921
1922	((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1923	return (DTRACE_AGGWALK_NORMALIZE);
1924}
1925
1926static int
1927dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1928{
1929	dt_normal_t normal;
1930	caddr_t addr;
1931
1932	/*
1933	 * We (should) have two records:  the aggregation ID followed by the
1934	 * normalization value.
1935	 */
1936	addr = base + rec->dtrd_offset;
1937
1938	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1939		return (dt_set_errno(dtp, EDT_BADNORMAL));
1940
1941	/* LINTED - alignment */
1942	normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1943	rec++;
1944
1945	if (rec->dtrd_action != DTRACEACT_LIBACT)
1946		return (dt_set_errno(dtp, EDT_BADNORMAL));
1947
1948	if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1949		return (dt_set_errno(dtp, EDT_BADNORMAL));
1950
1951	addr = base + rec->dtrd_offset;
1952
1953	switch (rec->dtrd_size) {
1954	case sizeof (uint64_t):
1955		/* LINTED - alignment */
1956		normal.dtnd_normal = *((uint64_t *)addr);
1957		break;
1958	case sizeof (uint32_t):
1959		/* LINTED - alignment */
1960		normal.dtnd_normal = *((uint32_t *)addr);
1961		break;
1962	case sizeof (uint16_t):
1963		/* LINTED - alignment */
1964		normal.dtnd_normal = *((uint16_t *)addr);
1965		break;
1966	case sizeof (uint8_t):
1967		normal.dtnd_normal = *((uint8_t *)addr);
1968		break;
1969	default:
1970		return (dt_set_errno(dtp, EDT_BADNORMAL));
1971	}
1972
1973	(void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1974
1975	return (0);
1976}
1977
1978static int
1979dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1980{
1981	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1982	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1983
1984	if (agg->dtagd_nrecs == 0)
1985		return (DTRACE_AGGWALK_NEXT);
1986
1987	if (agg->dtagd_varid != id)
1988		return (DTRACE_AGGWALK_NEXT);
1989
1990	return (DTRACE_AGGWALK_DENORMALIZE);
1991}
1992
1993static int
1994dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1995{
1996	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1997	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1998
1999	if (agg->dtagd_nrecs == 0)
2000		return (DTRACE_AGGWALK_NEXT);
2001
2002	if (agg->dtagd_varid != id)
2003		return (DTRACE_AGGWALK_NEXT);
2004
2005	return (DTRACE_AGGWALK_CLEAR);
2006}
2007
2008typedef struct dt_trunc {
2009	dtrace_aggvarid_t dttd_id;
2010	uint64_t dttd_remaining;
2011} dt_trunc_t;
2012
2013static int
2014dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
2015{
2016	dt_trunc_t *trunc = arg;
2017	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2018	dtrace_aggvarid_t id = trunc->dttd_id;
2019
2020	if (agg->dtagd_nrecs == 0)
2021		return (DTRACE_AGGWALK_NEXT);
2022
2023	if (agg->dtagd_varid != id)
2024		return (DTRACE_AGGWALK_NEXT);
2025
2026	if (trunc->dttd_remaining == 0)
2027		return (DTRACE_AGGWALK_REMOVE);
2028
2029	trunc->dttd_remaining--;
2030	return (DTRACE_AGGWALK_NEXT);
2031}
2032
2033static int
2034dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
2035{
2036	dt_trunc_t trunc;
2037	caddr_t addr;
2038	int64_t remaining;
2039	int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
2040
2041	/*
2042	 * We (should) have two records:  the aggregation ID followed by the
2043	 * number of aggregation entries after which the aggregation is to be
2044	 * truncated.
2045	 */
2046	addr = base + rec->dtrd_offset;
2047
2048	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
2049		return (dt_set_errno(dtp, EDT_BADTRUNC));
2050
2051	/* LINTED - alignment */
2052	trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
2053	rec++;
2054
2055	if (rec->dtrd_action != DTRACEACT_LIBACT)
2056		return (dt_set_errno(dtp, EDT_BADTRUNC));
2057
2058	if (rec->dtrd_arg != DT_ACT_TRUNC)
2059		return (dt_set_errno(dtp, EDT_BADTRUNC));
2060
2061	addr = base + rec->dtrd_offset;
2062
2063	switch (rec->dtrd_size) {
2064	case sizeof (uint64_t):
2065		/* LINTED - alignment */
2066		remaining = *((int64_t *)addr);
2067		break;
2068	case sizeof (uint32_t):
2069		/* LINTED - alignment */
2070		remaining = *((int32_t *)addr);
2071		break;
2072	case sizeof (uint16_t):
2073		/* LINTED - alignment */
2074		remaining = *((int16_t *)addr);
2075		break;
2076	case sizeof (uint8_t):
2077		remaining = *((int8_t *)addr);
2078		break;
2079	default:
2080		return (dt_set_errno(dtp, EDT_BADNORMAL));
2081	}
2082
2083	if (remaining < 0) {
2084		func = dtrace_aggregate_walk_valsorted;
2085		remaining = -remaining;
2086	} else {
2087		func = dtrace_aggregate_walk_valrevsorted;
2088	}
2089
2090	assert(remaining >= 0);
2091	trunc.dttd_remaining = remaining;
2092
2093	(void) func(dtp, dt_trunc_agg, &trunc);
2094
2095	return (0);
2096}
2097
2098static int
2099dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
2100    caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata,
2101    uint64_t normal, dt_print_aggdata_t *pd)
2102{
2103	int err, width;
2104	dtrace_actkind_t act = rec->dtrd_action;
2105	boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack;
2106	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2107
2108	static struct {
2109		size_t size;
2110		int width;
2111		int packedwidth;
2112	} *fmt, fmttab[] = {
2113		{ sizeof (uint8_t),	3,	3 },
2114		{ sizeof (uint16_t),	5,	5 },
2115		{ sizeof (uint32_t),	8,	8 },
2116		{ sizeof (uint64_t),	16,	16 },
2117		{ 0,			-50,	16 }
2118	};
2119
2120	if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) {
2121		dtrace_recdesc_t *r;
2122
2123		width = 0;
2124
2125		/*
2126		 * To print our quantization header for either an agghist or
2127		 * aggpack aggregation, we need to iterate through all of our
2128		 * of our records to determine their width.
2129		 */
2130		for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) {
2131			for (fmt = fmttab; fmt->size &&
2132			    fmt->size != r->dtrd_size; fmt++)
2133				continue;
2134
2135			width += fmt->packedwidth + 1;
2136		}
2137
2138		if (pd->dtpa_agghist) {
2139			if (dt_print_quanthdr(dtp, fp, width) < 0)
2140				return (-1);
2141		} else {
2142			if (dt_print_quanthdr_packed(dtp, fp,
2143			    width, aggdata, r->dtrd_action) < 0)
2144				return (-1);
2145		}
2146
2147		pd->dtpa_agghisthdr = agg->dtagd_varid;
2148	}
2149
2150	if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) {
2151		char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES;
2152		char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES;
2153		int64_t val;
2154
2155		assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT);
2156		val = (long long)*((uint64_t *)addr);
2157
2158		if (dt_printf(dtp, fp, " ") < 0)
2159			return (-1);
2160
2161		return (dt_print_quantline(dtp, fp, val, normal,
2162		    aggdata->dtada_total, positives, negatives));
2163	}
2164
2165	if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) {
2166		switch (act) {
2167		case DTRACEAGG_QUANTIZE:
2168			return (dt_print_quantize_packed(dtp,
2169			    fp, addr, size, aggdata));
2170		case DTRACEAGG_LQUANTIZE:
2171			return (dt_print_lquantize_packed(dtp,
2172			    fp, addr, size, aggdata));
2173		default:
2174			break;
2175		}
2176	}
2177
2178	switch (act) {
2179	case DTRACEACT_STACK:
2180		return (dt_print_stack(dtp, fp, NULL, addr,
2181		    rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
2182
2183	case DTRACEACT_USTACK:
2184	case DTRACEACT_JSTACK:
2185		return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
2186
2187	case DTRACEACT_USYM:
2188	case DTRACEACT_UADDR:
2189		return (dt_print_usym(dtp, fp, addr, act));
2190
2191	case DTRACEACT_UMOD:
2192		return (dt_print_umod(dtp, fp, NULL, addr));
2193
2194	case DTRACEACT_SYM:
2195		return (dt_print_sym(dtp, fp, NULL, addr));
2196
2197	case DTRACEACT_MOD:
2198		return (dt_print_mod(dtp, fp, NULL, addr));
2199
2200	case DTRACEAGG_QUANTIZE:
2201		return (dt_print_quantize(dtp, fp, addr, size, normal));
2202
2203	case DTRACEAGG_LQUANTIZE:
2204		return (dt_print_lquantize(dtp, fp, addr, size, normal));
2205
2206	case DTRACEAGG_LLQUANTIZE:
2207		return (dt_print_llquantize(dtp, fp, addr, size, normal));
2208
2209	case DTRACEAGG_AVG:
2210		return (dt_print_average(dtp, fp, addr, size, normal));
2211
2212	case DTRACEAGG_STDDEV:
2213		return (dt_print_stddev(dtp, fp, addr, size, normal));
2214
2215	default:
2216		break;
2217	}
2218
2219	for (fmt = fmttab; fmt->size && fmt->size != size; fmt++)
2220		continue;
2221
2222	width = packed ? fmt->packedwidth : fmt->width;
2223
2224	switch (size) {
2225	case sizeof (uint64_t):
2226		err = dt_printf(dtp, fp, " %*lld", width,
2227		    /* LINTED - alignment */
2228		    (long long)*((uint64_t *)addr) / normal);
2229		break;
2230	case sizeof (uint32_t):
2231		/* LINTED - alignment */
2232		err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) /
2233		    (uint32_t)normal);
2234		break;
2235	case sizeof (uint16_t):
2236		/* LINTED - alignment */
2237		err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) /
2238		    (uint32_t)normal);
2239		break;
2240	case sizeof (uint8_t):
2241		err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) /
2242		    (uint32_t)normal);
2243		break;
2244	default:
2245		err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0);
2246		break;
2247	}
2248
2249	return (err);
2250}
2251
2252int
2253dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
2254{
2255	int i, aggact = 0;
2256	dt_print_aggdata_t *pd = arg;
2257	const dtrace_aggdata_t *aggdata = aggsdata[0];
2258	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2259	FILE *fp = pd->dtpa_fp;
2260	dtrace_hdl_t *dtp = pd->dtpa_dtp;
2261	dtrace_recdesc_t *rec;
2262	dtrace_actkind_t act;
2263	caddr_t addr;
2264	size_t size;
2265
2266	pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL);
2267	pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN);
2268
2269	/*
2270	 * Iterate over each record description in the key, printing the traced
2271	 * data, skipping the first datum (the tuple member created by the
2272	 * compiler).
2273	 */
2274	for (i = 1; i < agg->dtagd_nrecs; i++) {
2275		rec = &agg->dtagd_rec[i];
2276		act = rec->dtrd_action;
2277		addr = aggdata->dtada_data + rec->dtrd_offset;
2278		size = rec->dtrd_size;
2279
2280		if (DTRACEACT_ISAGG(act)) {
2281			aggact = i;
2282			break;
2283		}
2284
2285		if (dt_print_datum(dtp, fp, rec, addr,
2286		    size, aggdata, 1, pd) < 0)
2287			return (-1);
2288
2289		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2290		    DTRACE_BUFDATA_AGGKEY) < 0)
2291			return (-1);
2292	}
2293
2294	assert(aggact != 0);
2295
2296	for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
2297		uint64_t normal;
2298
2299		aggdata = aggsdata[i];
2300		agg = aggdata->dtada_desc;
2301		rec = &agg->dtagd_rec[aggact];
2302		act = rec->dtrd_action;
2303		addr = aggdata->dtada_data + rec->dtrd_offset;
2304		size = rec->dtrd_size;
2305
2306		assert(DTRACEACT_ISAGG(act));
2307		normal = aggdata->dtada_normal;
2308
2309		if (dt_print_datum(dtp, fp, rec, addr,
2310		    size, aggdata, normal, pd) < 0)
2311			return (-1);
2312
2313		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2314		    DTRACE_BUFDATA_AGGVAL) < 0)
2315			return (-1);
2316
2317		if (!pd->dtpa_allunprint)
2318			agg->dtagd_flags |= DTRACE_AGD_PRINTED;
2319	}
2320
2321	if (!pd->dtpa_agghist && !pd->dtpa_aggpack) {
2322		if (dt_printf(dtp, fp, "\n") < 0)
2323			return (-1);
2324	}
2325
2326	if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
2327	    DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
2328		return (-1);
2329
2330	return (0);
2331}
2332
2333int
2334dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
2335{
2336	dt_print_aggdata_t *pd = arg;
2337	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2338	dtrace_aggvarid_t aggvarid = pd->dtpa_id;
2339
2340	if (pd->dtpa_allunprint) {
2341		if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
2342			return (0);
2343	} else {
2344		/*
2345		 * If we're not printing all unprinted aggregations, then the
2346		 * aggregation variable ID denotes a specific aggregation
2347		 * variable that we should print -- skip any other aggregations
2348		 * that we encounter.
2349		 */
2350		if (agg->dtagd_nrecs == 0)
2351			return (0);
2352
2353		if (aggvarid != agg->dtagd_varid)
2354			return (0);
2355	}
2356
2357	return (dt_print_aggs(&aggdata, 1, arg));
2358}
2359
2360int
2361dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
2362    const char *option, const char *value)
2363{
2364	int len, rval;
2365	char *msg;
2366	const char *errstr;
2367	dtrace_setoptdata_t optdata;
2368
2369	bzero(&optdata, sizeof (optdata));
2370	(void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
2371
2372	if (dtrace_setopt(dtp, option, value) == 0) {
2373		(void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
2374		optdata.dtsda_probe = data;
2375		optdata.dtsda_option = option;
2376		optdata.dtsda_handle = dtp;
2377
2378		if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
2379			return (rval);
2380
2381		return (0);
2382	}
2383
2384	errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
2385	len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2386	msg = alloca(len);
2387
2388	(void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2389	    option, value, errstr);
2390
2391	if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2392		return (0);
2393
2394	return (rval);
2395}
2396
2397static int
2398dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu,
2399    dtrace_bufdesc_t *buf, boolean_t just_one,
2400    dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2401{
2402	dtrace_epid_t id;
2403	size_t offs;
2404	int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2405	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2406	int rval, i, n;
2407	uint64_t tracememsize = 0;
2408	dtrace_probedata_t data;
2409	uint64_t drops;
2410
2411	bzero(&data, sizeof (data));
2412	data.dtpda_handle = dtp;
2413	data.dtpda_cpu = cpu;
2414	data.dtpda_flow = dtp->dt_flow;
2415	data.dtpda_indent = dtp->dt_indent;
2416	data.dtpda_prefix = dtp->dt_prefix;
2417
2418	for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) {
2419		dtrace_eprobedesc_t *epd;
2420
2421		/*
2422		 * We're guaranteed to have an ID.
2423		 */
2424		id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2425
2426		if (id == DTRACE_EPIDNONE) {
2427			/*
2428			 * This is filler to assure proper alignment of the
2429			 * next record; we simply ignore it.
2430			 */
2431			offs += sizeof (id);
2432			continue;
2433		}
2434
2435		if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2436		    &data.dtpda_pdesc)) != 0)
2437			return (rval);
2438
2439		epd = data.dtpda_edesc;
2440		data.dtpda_data = buf->dtbd_data + offs;
2441
2442		if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2443			rval = dt_handle(dtp, &data);
2444
2445			if (rval == DTRACE_CONSUME_NEXT)
2446				goto nextepid;
2447
2448			if (rval == DTRACE_CONSUME_ERROR)
2449				return (-1);
2450		}
2451
2452		if (flow)
2453			(void) dt_flowindent(dtp, &data, dtp->dt_last_epid,
2454			    buf, offs);
2455
2456		rval = (*efunc)(&data, arg);
2457
2458		if (flow) {
2459			if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2460				data.dtpda_indent += 2;
2461		}
2462
2463		if (rval == DTRACE_CONSUME_NEXT)
2464			goto nextepid;
2465
2466		if (rval == DTRACE_CONSUME_ABORT)
2467			return (dt_set_errno(dtp, EDT_DIRABORT));
2468
2469		if (rval != DTRACE_CONSUME_THIS)
2470			return (dt_set_errno(dtp, EDT_BADRVAL));
2471
2472		for (i = 0; i < epd->dtepd_nrecs; i++) {
2473			caddr_t addr;
2474			dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2475			dtrace_actkind_t act = rec->dtrd_action;
2476
2477			data.dtpda_data = buf->dtbd_data + offs +
2478			    rec->dtrd_offset;
2479			addr = data.dtpda_data;
2480
2481			if (act == DTRACEACT_LIBACT) {
2482				uint64_t arg = rec->dtrd_arg;
2483				dtrace_aggvarid_t id;
2484
2485				switch (arg) {
2486				case DT_ACT_CLEAR:
2487					/* LINTED - alignment */
2488					id = *((dtrace_aggvarid_t *)addr);
2489					(void) dtrace_aggregate_walk(dtp,
2490					    dt_clear_agg, &id);
2491					continue;
2492
2493				case DT_ACT_DENORMALIZE:
2494					/* LINTED - alignment */
2495					id = *((dtrace_aggvarid_t *)addr);
2496					(void) dtrace_aggregate_walk(dtp,
2497					    dt_denormalize_agg, &id);
2498					continue;
2499
2500				case DT_ACT_FTRUNCATE:
2501					if (fp == NULL)
2502						continue;
2503
2504					(void) fflush(fp);
2505					(void) ftruncate(fileno(fp), 0);
2506					(void) fseeko(fp, 0, SEEK_SET);
2507					continue;
2508
2509				case DT_ACT_NORMALIZE:
2510					if (i == epd->dtepd_nrecs - 1)
2511						return (dt_set_errno(dtp,
2512						    EDT_BADNORMAL));
2513
2514					if (dt_normalize(dtp,
2515					    buf->dtbd_data + offs, rec) != 0)
2516						return (-1);
2517
2518					i++;
2519					continue;
2520
2521				case DT_ACT_SETOPT: {
2522					uint64_t *opts = dtp->dt_options;
2523					dtrace_recdesc_t *valrec;
2524					uint32_t valsize;
2525					caddr_t val;
2526					int rv;
2527
2528					if (i == epd->dtepd_nrecs - 1) {
2529						return (dt_set_errno(dtp,
2530						    EDT_BADSETOPT));
2531					}
2532
2533					valrec = &epd->dtepd_rec[++i];
2534					valsize = valrec->dtrd_size;
2535
2536					if (valrec->dtrd_action != act ||
2537					    valrec->dtrd_arg != arg) {
2538						return (dt_set_errno(dtp,
2539						    EDT_BADSETOPT));
2540					}
2541
2542					if (valsize > sizeof (uint64_t)) {
2543						val = buf->dtbd_data + offs +
2544						    valrec->dtrd_offset;
2545					} else {
2546						val = "1";
2547					}
2548
2549					rv = dt_setopt(dtp, &data, addr, val);
2550
2551					if (rv != 0)
2552						return (-1);
2553
2554					flow = (opts[DTRACEOPT_FLOWINDENT] !=
2555					    DTRACEOPT_UNSET);
2556					quiet = (opts[DTRACEOPT_QUIET] !=
2557					    DTRACEOPT_UNSET);
2558
2559					continue;
2560				}
2561
2562				case DT_ACT_TRUNC:
2563					if (i == epd->dtepd_nrecs - 1)
2564						return (dt_set_errno(dtp,
2565						    EDT_BADTRUNC));
2566
2567					if (dt_trunc(dtp,
2568					    buf->dtbd_data + offs, rec) != 0)
2569						return (-1);
2570
2571					i++;
2572					continue;
2573
2574				default:
2575					continue;
2576				}
2577			}
2578
2579			if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
2580			    rec->dtrd_size == sizeof (uint64_t)) {
2581			    	/* LINTED - alignment */
2582				tracememsize = *((unsigned long long *)addr);
2583				continue;
2584			}
2585
2586			rval = (*rfunc)(&data, rec, arg);
2587
2588			if (rval == DTRACE_CONSUME_NEXT)
2589				continue;
2590
2591			if (rval == DTRACE_CONSUME_ABORT)
2592				return (dt_set_errno(dtp, EDT_DIRABORT));
2593
2594			if (rval != DTRACE_CONSUME_THIS)
2595				return (dt_set_errno(dtp, EDT_BADRVAL));
2596
2597			if (act == DTRACEACT_STACK) {
2598				int depth = rec->dtrd_arg;
2599
2600				if (dt_print_stack(dtp, fp, NULL, addr, depth,
2601				    rec->dtrd_size / depth) < 0)
2602					return (-1);
2603				goto nextrec;
2604			}
2605
2606			if (act == DTRACEACT_USTACK ||
2607			    act == DTRACEACT_JSTACK) {
2608				if (dt_print_ustack(dtp, fp, NULL,
2609				    addr, rec->dtrd_arg) < 0)
2610					return (-1);
2611				goto nextrec;
2612			}
2613
2614			if (act == DTRACEACT_SYM) {
2615				if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2616					return (-1);
2617				goto nextrec;
2618			}
2619
2620			if (act == DTRACEACT_MOD) {
2621				if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2622					return (-1);
2623				goto nextrec;
2624			}
2625
2626			if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2627				if (dt_print_usym(dtp, fp, addr, act) < 0)
2628					return (-1);
2629				goto nextrec;
2630			}
2631
2632			if (act == DTRACEACT_UMOD) {
2633				if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2634					return (-1);
2635				goto nextrec;
2636			}
2637
2638			if (act == DTRACEACT_PRINTM) {
2639				if (dt_print_memory(dtp, fp, addr) < 0)
2640					return (-1);
2641				goto nextrec;
2642			}
2643
2644			if (act == DTRACEACT_PRINTT) {
2645				if (dt_print_type(dtp, fp, addr) < 0)
2646					return (-1);
2647				goto nextrec;
2648			}
2649
2650			if (DTRACEACT_ISPRINTFLIKE(act)) {
2651				void *fmtdata;
2652				int (*func)(dtrace_hdl_t *, FILE *, void *,
2653				    const dtrace_probedata_t *,
2654				    const dtrace_recdesc_t *, uint_t,
2655				    const void *buf, size_t);
2656
2657				if ((fmtdata = dt_format_lookup(dtp,
2658				    rec->dtrd_format)) == NULL)
2659					goto nofmt;
2660
2661				switch (act) {
2662				case DTRACEACT_PRINTF:
2663					func = dtrace_fprintf;
2664					break;
2665				case DTRACEACT_PRINTA:
2666					func = dtrace_fprinta;
2667					break;
2668				case DTRACEACT_SYSTEM:
2669					func = dtrace_system;
2670					break;
2671				case DTRACEACT_FREOPEN:
2672					func = dtrace_freopen;
2673					break;
2674				}
2675
2676				n = (*func)(dtp, fp, fmtdata, &data,
2677				    rec, epd->dtepd_nrecs - i,
2678				    (uchar_t *)buf->dtbd_data + offs,
2679				    buf->dtbd_size - offs);
2680
2681				if (n < 0)
2682					return (-1); /* errno is set for us */
2683
2684				if (n > 0)
2685					i += n - 1;
2686				goto nextrec;
2687			}
2688
2689			/*
2690			 * If this is a DIF expression, and the record has a
2691			 * format set, this indicates we have a CTF type name
2692			 * associated with the data and we should try to print
2693			 * it out by type.
2694			 */
2695			if (act == DTRACEACT_DIFEXPR) {
2696				const char *strdata = dt_strdata_lookup(dtp,
2697				    rec->dtrd_format);
2698				if (strdata != NULL) {
2699					n = dtrace_print(dtp, fp, strdata,
2700					    addr, rec->dtrd_size);
2701
2702					/*
2703					 * dtrace_print() will return -1 on
2704					 * error, or return the number of bytes
2705					 * consumed.  It will return 0 if the
2706					 * type couldn't be determined, and we
2707					 * should fall through to the normal
2708					 * trace method.
2709					 */
2710					if (n < 0)
2711						return (-1);
2712
2713					if (n > 0)
2714						goto nextrec;
2715				}
2716			}
2717
2718nofmt:
2719			if (act == DTRACEACT_PRINTA) {
2720				dt_print_aggdata_t pd;
2721				dtrace_aggvarid_t *aggvars;
2722				int j, naggvars = 0;
2723				size_t size = ((epd->dtepd_nrecs - i) *
2724				    sizeof (dtrace_aggvarid_t));
2725
2726				if ((aggvars = dt_alloc(dtp, size)) == NULL)
2727					return (-1);
2728
2729				/*
2730				 * This might be a printa() with multiple
2731				 * aggregation variables.  We need to scan
2732				 * forward through the records until we find
2733				 * a record from a different statement.
2734				 */
2735				for (j = i; j < epd->dtepd_nrecs; j++) {
2736					dtrace_recdesc_t *nrec;
2737					caddr_t naddr;
2738
2739					nrec = &epd->dtepd_rec[j];
2740
2741					if (nrec->dtrd_uarg != rec->dtrd_uarg)
2742						break;
2743
2744					if (nrec->dtrd_action != act) {
2745						return (dt_set_errno(dtp,
2746						    EDT_BADAGG));
2747					}
2748
2749					naddr = buf->dtbd_data + offs +
2750					    nrec->dtrd_offset;
2751
2752					aggvars[naggvars++] =
2753					    /* LINTED - alignment */
2754					    *((dtrace_aggvarid_t *)naddr);
2755				}
2756
2757				i = j - 1;
2758				bzero(&pd, sizeof (pd));
2759				pd.dtpa_dtp = dtp;
2760				pd.dtpa_fp = fp;
2761
2762				assert(naggvars >= 1);
2763
2764				if (naggvars == 1) {
2765					pd.dtpa_id = aggvars[0];
2766					dt_free(dtp, aggvars);
2767
2768					if (dt_printf(dtp, fp, "\n") < 0 ||
2769					    dtrace_aggregate_walk_sorted(dtp,
2770					    dt_print_agg, &pd) < 0)
2771						return (-1);
2772					goto nextrec;
2773				}
2774
2775				if (dt_printf(dtp, fp, "\n") < 0 ||
2776				    dtrace_aggregate_walk_joined(dtp, aggvars,
2777				    naggvars, dt_print_aggs, &pd) < 0) {
2778					dt_free(dtp, aggvars);
2779					return (-1);
2780				}
2781
2782				dt_free(dtp, aggvars);
2783				goto nextrec;
2784			}
2785
2786			if (act == DTRACEACT_TRACEMEM) {
2787				if (tracememsize == 0 ||
2788				    tracememsize > rec->dtrd_size) {
2789					tracememsize = rec->dtrd_size;
2790				}
2791
2792				n = dt_print_bytes(dtp, fp, addr,
2793				    tracememsize, -33, quiet, 1);
2794
2795				tracememsize = 0;
2796
2797				if (n < 0)
2798					return (-1);
2799
2800				goto nextrec;
2801			}
2802
2803			switch (rec->dtrd_size) {
2804			case sizeof (uint64_t):
2805				n = dt_printf(dtp, fp,
2806				    quiet ? "%lld" : " %16lld",
2807				    /* LINTED - alignment */
2808				    *((unsigned long long *)addr));
2809				break;
2810			case sizeof (uint32_t):
2811				n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2812				    /* LINTED - alignment */
2813				    *((uint32_t *)addr));
2814				break;
2815			case sizeof (uint16_t):
2816				n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2817				    /* LINTED - alignment */
2818				    *((uint16_t *)addr));
2819				break;
2820			case sizeof (uint8_t):
2821				n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2822				    *((uint8_t *)addr));
2823				break;
2824			default:
2825				n = dt_print_bytes(dtp, fp, addr,
2826				    rec->dtrd_size, -33, quiet, 0);
2827				break;
2828			}
2829
2830			if (n < 0)
2831				return (-1); /* errno is set for us */
2832
2833nextrec:
2834			if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2835				return (-1); /* errno is set for us */
2836		}
2837
2838		/*
2839		 * Call the record callback with a NULL record to indicate
2840		 * that we're done processing this EPID.
2841		 */
2842		rval = (*rfunc)(&data, NULL, arg);
2843nextepid:
2844		offs += epd->dtepd_size;
2845		dtp->dt_last_epid = id;
2846		if (just_one) {
2847			buf->dtbd_oldest = offs;
2848			break;
2849		}
2850	}
2851
2852	dtp->dt_flow = data.dtpda_flow;
2853	dtp->dt_indent = data.dtpda_indent;
2854	dtp->dt_prefix = data.dtpda_prefix;
2855
2856	if ((drops = buf->dtbd_drops) == 0)
2857		return (0);
2858
2859	/*
2860	 * Explicitly zero the drops to prevent us from processing them again.
2861	 */
2862	buf->dtbd_drops = 0;
2863
2864	return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2865}
2866
2867/*
2868 * Reduce memory usage by shrinking the buffer if it's no more than half full.
2869 * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2870 * only 4-byte aligned.
2871 */
2872static void
2873dt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize)
2874{
2875	uint64_t used = buf->dtbd_size - buf->dtbd_oldest;
2876	if (used < cursize / 2) {
2877		int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2878		char *newdata = dt_alloc(dtp, used + misalign);
2879		if (newdata == NULL)
2880			return;
2881		bzero(newdata, misalign);
2882		bcopy(buf->dtbd_data + buf->dtbd_oldest,
2883		    newdata + misalign, used);
2884		dt_free(dtp, buf->dtbd_data);
2885		buf->dtbd_oldest = misalign;
2886		buf->dtbd_size = used + misalign;
2887		buf->dtbd_data = newdata;
2888	}
2889}
2890
2891/*
2892 * If the ring buffer has wrapped, the data is not in order.  Rearrange it
2893 * so that it is.  Note, we need to preserve the alignment of the data at
2894 * dtbd_oldest, which is only 4-byte aligned.
2895 */
2896static int
2897dt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2898{
2899	int misalign;
2900	char *newdata, *ndp;
2901
2902	if (buf->dtbd_oldest == 0)
2903		return (0);
2904
2905	misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2906	newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign);
2907
2908	if (newdata == NULL)
2909		return (-1);
2910
2911	assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1)));
2912
2913	bzero(ndp, misalign);
2914	ndp += misalign;
2915
2916	bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp,
2917	    buf->dtbd_size - buf->dtbd_oldest);
2918	ndp += buf->dtbd_size - buf->dtbd_oldest;
2919
2920	bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest);
2921
2922	dt_free(dtp, buf->dtbd_data);
2923	buf->dtbd_oldest = 0;
2924	buf->dtbd_data = newdata;
2925	buf->dtbd_size += misalign;
2926
2927	return (0);
2928}
2929
2930static void
2931dt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2932{
2933	dt_free(dtp, buf->dtbd_data);
2934	dt_free(dtp, buf);
2935}
2936
2937/*
2938 * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2939 * data, or NULL if there is no data for this CPU.
2940 * Returns -1 on failure and sets dt_errno.
2941 */
2942static int
2943dt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp)
2944{
2945	dtrace_optval_t size;
2946	dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf));
2947	int error;
2948
2949	if (buf == NULL)
2950		return (-1);
2951
2952	(void) dtrace_getopt(dtp, "bufsize", &size);
2953	buf->dtbd_data = dt_alloc(dtp, size);
2954	if (buf->dtbd_data == NULL) {
2955		dt_free(dtp, buf);
2956		return (-1);
2957	}
2958	buf->dtbd_size = size;
2959	buf->dtbd_cpu = cpu;
2960
2961#if defined(sun)
2962	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2963#else
2964	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2965#endif
2966		dt_put_buf(dtp, buf);
2967		/*
2968		 * If we failed with ENOENT, it may be because the
2969		 * CPU was unconfigured -- this is okay.  Any other
2970		 * error, however, is unexpected.
2971		 */
2972		if (errno == ENOENT) {
2973			*bufp = NULL;
2974			return (0);
2975		}
2976
2977		return (dt_set_errno(dtp, errno));
2978	}
2979
2980	error = dt_unring_buf(dtp, buf);
2981	if (error != 0) {
2982		dt_put_buf(dtp, buf);
2983		return (error);
2984	}
2985	dt_realloc_buf(dtp, buf, size);
2986
2987	*bufp = buf;
2988	return (0);
2989}
2990
2991typedef struct dt_begin {
2992	dtrace_consume_probe_f *dtbgn_probefunc;
2993	dtrace_consume_rec_f *dtbgn_recfunc;
2994	void *dtbgn_arg;
2995	dtrace_handle_err_f *dtbgn_errhdlr;
2996	void *dtbgn_errarg;
2997	int dtbgn_beginonly;
2998} dt_begin_t;
2999
3000static int
3001dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
3002{
3003	dt_begin_t *begin = arg;
3004	dtrace_probedesc_t *pd = data->dtpda_pdesc;
3005
3006	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3007	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3008
3009	if (begin->dtbgn_beginonly) {
3010		if (!(r1 && r2))
3011			return (DTRACE_CONSUME_NEXT);
3012	} else {
3013		if (r1 && r2)
3014			return (DTRACE_CONSUME_NEXT);
3015	}
3016
3017	/*
3018	 * We have a record that we're interested in.  Now call the underlying
3019	 * probe function...
3020	 */
3021	return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
3022}
3023
3024static int
3025dt_consume_begin_record(const dtrace_probedata_t *data,
3026    const dtrace_recdesc_t *rec, void *arg)
3027{
3028	dt_begin_t *begin = arg;
3029
3030	return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
3031}
3032
3033static int
3034dt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
3035{
3036	dt_begin_t *begin = (dt_begin_t *)arg;
3037	dtrace_probedesc_t *pd = data->dteda_pdesc;
3038
3039	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3040	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3041
3042	if (begin->dtbgn_beginonly) {
3043		if (!(r1 && r2))
3044			return (DTRACE_HANDLE_OK);
3045	} else {
3046		if (r1 && r2)
3047			return (DTRACE_HANDLE_OK);
3048	}
3049
3050	return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
3051}
3052
3053static int
3054dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp,
3055    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3056{
3057	/*
3058	 * There's this idea that the BEGIN probe should be processed before
3059	 * everything else, and that the END probe should be processed after
3060	 * anything else.  In the common case, this is pretty easy to deal
3061	 * with.  However, a situation may arise where the BEGIN enabling and
3062	 * END enabling are on the same CPU, and some enabling in the middle
3063	 * occurred on a different CPU.  To deal with this (blech!) we need to
3064	 * consume the BEGIN buffer up until the end of the BEGIN probe, and
3065	 * then set it aside.  We will then process every other CPU, and then
3066	 * we'll return to the BEGIN CPU and process the rest of the data
3067	 * (which will inevitably include the END probe, if any).  Making this
3068	 * even more complicated (!) is the library's ERROR enabling.  Because
3069	 * this enabling is processed before we even get into the consume call
3070	 * back, any ERROR firing would result in the library's ERROR enabling
3071	 * being processed twice -- once in our first pass (for BEGIN probes),
3072	 * and again in our second pass (for everything but BEGIN probes).  To
3073	 * deal with this, we interpose on the ERROR handler to assure that we
3074	 * only process ERROR enablings induced by BEGIN enablings in the
3075	 * first pass, and that we only process ERROR enablings _not_ induced
3076	 * by BEGIN enablings in the second pass.
3077	 */
3078
3079	dt_begin_t begin;
3080	processorid_t cpu = dtp->dt_beganon;
3081	int rval, i;
3082	static int max_ncpus;
3083	dtrace_bufdesc_t *buf;
3084
3085	dtp->dt_beganon = -1;
3086
3087	if (dt_get_buf(dtp, cpu, &buf) != 0)
3088		return (-1);
3089	if (buf == NULL)
3090		return (0);
3091
3092	if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
3093		/*
3094		 * This is the simple case.  We're either not stopped, or if
3095		 * we are, we actually processed any END probes on another
3096		 * CPU.  We can simply consume this buffer and return.
3097		 */
3098		rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3099		    pf, rf, arg);
3100		dt_put_buf(dtp, buf);
3101		return (rval);
3102	}
3103
3104	begin.dtbgn_probefunc = pf;
3105	begin.dtbgn_recfunc = rf;
3106	begin.dtbgn_arg = arg;
3107	begin.dtbgn_beginonly = 1;
3108
3109	/*
3110	 * We need to interpose on the ERROR handler to be sure that we
3111	 * only process ERRORs induced by BEGIN.
3112	 */
3113	begin.dtbgn_errhdlr = dtp->dt_errhdlr;
3114	begin.dtbgn_errarg = dtp->dt_errarg;
3115	dtp->dt_errhdlr = dt_consume_begin_error;
3116	dtp->dt_errarg = &begin;
3117
3118	rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3119	    dt_consume_begin_probe, dt_consume_begin_record, &begin);
3120
3121	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3122	dtp->dt_errarg = begin.dtbgn_errarg;
3123
3124	if (rval != 0) {
3125		dt_put_buf(dtp, buf);
3126		return (rval);
3127	}
3128
3129	if (max_ncpus == 0)
3130		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3131
3132	for (i = 0; i < max_ncpus; i++) {
3133		dtrace_bufdesc_t *nbuf;
3134		if (i == cpu)
3135			continue;
3136
3137		if (dt_get_buf(dtp, i, &nbuf) != 0) {
3138			dt_put_buf(dtp, buf);
3139			return (-1);
3140		}
3141		if (nbuf == NULL)
3142			continue;
3143
3144		rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE,
3145		    pf, rf, arg);
3146		dt_put_buf(dtp, nbuf);
3147		if (rval != 0) {
3148			dt_put_buf(dtp, buf);
3149			return (rval);
3150		}
3151	}
3152
3153	/*
3154	 * Okay -- we're done with the other buffers.  Now we want to
3155	 * reconsume the first buffer -- but this time we're looking for
3156	 * everything _but_ BEGIN.  And of course, in order to only consume
3157	 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
3158	 * ERROR interposition function...
3159	 */
3160	begin.dtbgn_beginonly = 0;
3161
3162	assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
3163	assert(begin.dtbgn_errarg == dtp->dt_errarg);
3164	dtp->dt_errhdlr = dt_consume_begin_error;
3165	dtp->dt_errarg = &begin;
3166
3167	rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3168	    dt_consume_begin_probe, dt_consume_begin_record, &begin);
3169
3170	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3171	dtp->dt_errarg = begin.dtbgn_errarg;
3172
3173	return (rval);
3174}
3175
3176/* ARGSUSED */
3177static uint64_t
3178dt_buf_oldest(void *elem, void *arg)
3179{
3180	dtrace_bufdesc_t *buf = elem;
3181	size_t offs = buf->dtbd_oldest;
3182
3183	while (offs < buf->dtbd_size) {
3184		dtrace_rechdr_t *dtrh =
3185		    /* LINTED - alignment */
3186		    (dtrace_rechdr_t *)(buf->dtbd_data + offs);
3187		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
3188			offs += sizeof (dtrace_epid_t);
3189		} else {
3190			return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh));
3191		}
3192	}
3193
3194	/* There are no records left; use the time the buffer was retrieved. */
3195	return (buf->dtbd_timestamp);
3196}
3197
3198int
3199dtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
3200    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3201{
3202	dtrace_optval_t size;
3203	static int max_ncpus;
3204	int i, rval;
3205	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
3206	hrtime_t now = gethrtime();
3207
3208	if (dtp->dt_lastswitch != 0) {
3209		if (now - dtp->dt_lastswitch < interval)
3210			return (0);
3211
3212		dtp->dt_lastswitch += interval;
3213	} else {
3214		dtp->dt_lastswitch = now;
3215	}
3216
3217	if (!dtp->dt_active)
3218		return (dt_set_errno(dtp, EINVAL));
3219
3220	if (max_ncpus == 0)
3221		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3222
3223	if (pf == NULL)
3224		pf = (dtrace_consume_probe_f *)dt_nullprobe;
3225
3226	if (rf == NULL)
3227		rf = (dtrace_consume_rec_f *)dt_nullrec;
3228
3229	if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) {
3230		/*
3231		 * The output will not be in the order it was traced.  Rather,
3232		 * we will consume all of the data from each CPU's buffer in
3233		 * turn.  We apply special handling for the records from BEGIN
3234		 * and END probes so that they are consumed first and last,
3235		 * respectively.
3236		 *
3237		 * If we have just begun, we want to first process the CPU that
3238		 * executed the BEGIN probe (if any).
3239		 */
3240		if (dtp->dt_active && dtp->dt_beganon != -1 &&
3241		    (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0)
3242			return (rval);
3243
3244		for (i = 0; i < max_ncpus; i++) {
3245			dtrace_bufdesc_t *buf;
3246
3247			/*
3248			 * If we have stopped, we want to process the CPU on
3249			 * which the END probe was processed only _after_ we
3250			 * have processed everything else.
3251			 */
3252			if (dtp->dt_stopped && (i == dtp->dt_endedon))
3253				continue;
3254
3255			if (dt_get_buf(dtp, i, &buf) != 0)
3256				return (-1);
3257			if (buf == NULL)
3258				continue;
3259
3260			dtp->dt_flow = 0;
3261			dtp->dt_indent = 0;
3262			dtp->dt_prefix = NULL;
3263			rval = dt_consume_cpu(dtp, fp, i,
3264			    buf, B_FALSE, pf, rf, arg);
3265			dt_put_buf(dtp, buf);
3266			if (rval != 0)
3267				return (rval);
3268		}
3269		if (dtp->dt_stopped) {
3270			dtrace_bufdesc_t *buf;
3271
3272			if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0)
3273				return (-1);
3274			if (buf == NULL)
3275				return (0);
3276
3277			rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon,
3278			    buf, B_FALSE, pf, rf, arg);
3279			dt_put_buf(dtp, buf);
3280			return (rval);
3281		}
3282	} else {
3283		/*
3284		 * The output will be in the order it was traced (or for
3285		 * speculations, when it was committed).  We retrieve a buffer
3286		 * from each CPU and put it into a priority queue, which sorts
3287		 * based on the first entry in the buffer.  This is sufficient
3288		 * because entries within a buffer are already sorted.
3289		 *
3290		 * We then consume records one at a time, always consuming the
3291		 * oldest record, as determined by the priority queue.  When
3292		 * we reach the end of the time covered by these buffers,
3293		 * we need to stop and retrieve more records on the next pass.
3294		 * The kernel tells us the time covered by each buffer, in
3295		 * dtbd_timestamp.  The first buffer's timestamp tells us the
3296		 * time covered by all buffers, as subsequently retrieved
3297		 * buffers will cover to a more recent time.
3298		 */
3299
3300		uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t));
3301		uint64_t first_timestamp = 0;
3302		uint_t cookie = 0;
3303		dtrace_bufdesc_t *buf;
3304
3305		bzero(drops, max_ncpus * sizeof (uint64_t));
3306
3307		if (dtp->dt_bufq == NULL) {
3308			dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2,
3309			    dt_buf_oldest, NULL);
3310			if (dtp->dt_bufq == NULL) /* ENOMEM */
3311				return (-1);
3312		}
3313
3314		/* Retrieve data from each CPU. */
3315		(void) dtrace_getopt(dtp, "bufsize", &size);
3316		for (i = 0; i < max_ncpus; i++) {
3317			dtrace_bufdesc_t *buf;
3318
3319			if (dt_get_buf(dtp, i, &buf) != 0)
3320				return (-1);
3321			if (buf != NULL) {
3322				if (first_timestamp == 0)
3323					first_timestamp = buf->dtbd_timestamp;
3324				assert(buf->dtbd_timestamp >= first_timestamp);
3325
3326				dt_pq_insert(dtp->dt_bufq, buf);
3327				drops[i] = buf->dtbd_drops;
3328				buf->dtbd_drops = 0;
3329			}
3330		}
3331
3332		/* Consume records. */
3333		for (;;) {
3334			dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq);
3335			uint64_t timestamp;
3336
3337			if (buf == NULL)
3338				break;
3339
3340			timestamp = dt_buf_oldest(buf, dtp);
3341			assert(timestamp >= dtp->dt_last_timestamp);
3342			dtp->dt_last_timestamp = timestamp;
3343
3344			if (timestamp == buf->dtbd_timestamp) {
3345				/*
3346				 * We've reached the end of the time covered
3347				 * by this buffer.  If this is the oldest
3348				 * buffer, we must do another pass
3349				 * to retrieve more data.
3350				 */
3351				dt_put_buf(dtp, buf);
3352				if (timestamp == first_timestamp &&
3353				    !dtp->dt_stopped)
3354					break;
3355				continue;
3356			}
3357
3358			if ((rval = dt_consume_cpu(dtp, fp,
3359			    buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0)
3360				return (rval);
3361			dt_pq_insert(dtp->dt_bufq, buf);
3362		}
3363
3364		/* Consume drops. */
3365		for (i = 0; i < max_ncpus; i++) {
3366			if (drops[i] != 0) {
3367				int error = dt_handle_cpudrop(dtp, i,
3368				    DTRACEDROP_PRINCIPAL, drops[i]);
3369				if (error != 0)
3370					return (error);
3371			}
3372		}
3373
3374		/*
3375		 * Reduce memory usage by re-allocating smaller buffers
3376		 * for the "remnants".
3377		 */
3378		while (buf = dt_pq_walk(dtp->dt_bufq, &cookie))
3379			dt_realloc_buf(dtp, buf, buf->dtbd_size);
3380	}
3381
3382	return (0);
3383}
3384