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