dt_consume.c revision 269524
1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb/*
22210767Srpaulo * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23178479Sjb * Use is subject to license terms.
24178479Sjb */
25178479Sjb
26237624Spfg/*
27267942Srpaulo * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28250574Smarkj * Copyright (c) 2012 by Delphix. All rights reserved.
29237624Spfg */
30237624Spfg
31178479Sjb#include <stdlib.h>
32178479Sjb#include <strings.h>
33178479Sjb#include <errno.h>
34178479Sjb#include <unistd.h>
35178479Sjb#include <limits.h>
36178479Sjb#include <assert.h>
37178479Sjb#include <ctype.h>
38178576Sjb#if defined(sun)
39178479Sjb#include <alloca.h>
40178576Sjb#endif
41178479Sjb#include <dt_impl.h>
42250574Smarkj#include <dt_pq.h>
43211554Srpaulo#if !defined(sun)
44211554Srpaulo#include <libproc_compat.h>
45211554Srpaulo#endif
46178479Sjb
47178479Sjb#define	DT_MASK_LO 0x00000000FFFFFFFFULL
48178479Sjb
49178479Sjb/*
50178479Sjb * We declare this here because (1) we need it and (2) we want to avoid a
51178479Sjb * dependency on libm in libdtrace.
52178479Sjb */
53178479Sjbstatic long double
54178479Sjbdt_fabsl(long double x)
55178479Sjb{
56178479Sjb	if (x < 0)
57178479Sjb		return (-x);
58178479Sjb
59178479Sjb	return (x);
60178479Sjb}
61178479Sjb
62267942Srpaulostatic int
63267942Srpaulodt_ndigits(long long val)
64267942Srpaulo{
65267942Srpaulo	int rval = 1;
66267942Srpaulo	long long cmp = 10;
67267942Srpaulo
68267942Srpaulo	if (val < 0) {
69267942Srpaulo		val = val == INT64_MIN ? INT64_MAX : -val;
70267942Srpaulo		rval++;
71267942Srpaulo	}
72267942Srpaulo
73267942Srpaulo	while (val > cmp && cmp > 0) {
74267942Srpaulo		rval++;
75267942Srpaulo		cmp *= 10;
76267942Srpaulo	}
77267942Srpaulo
78267942Srpaulo	return (rval < 4 ? 4 : rval);
79267942Srpaulo}
80267942Srpaulo
81178479Sjb/*
82178479Sjb * 128-bit arithmetic functions needed to support the stddev() aggregating
83178479Sjb * action.
84178479Sjb */
85178479Sjbstatic int
86178479Sjbdt_gt_128(uint64_t *a, uint64_t *b)
87178479Sjb{
88178479Sjb	return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
89178479Sjb}
90178479Sjb
91178479Sjbstatic int
92178479Sjbdt_ge_128(uint64_t *a, uint64_t *b)
93178479Sjb{
94178479Sjb	return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
95178479Sjb}
96178479Sjb
97178479Sjbstatic int
98178479Sjbdt_le_128(uint64_t *a, uint64_t *b)
99178479Sjb{
100178479Sjb	return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
101178479Sjb}
102178479Sjb
103178479Sjb/*
104178479Sjb * Shift the 128-bit value in a by b. If b is positive, shift left.
105178479Sjb * If b is negative, shift right.
106178479Sjb */
107178479Sjbstatic void
108178479Sjbdt_shift_128(uint64_t *a, int b)
109178479Sjb{
110178479Sjb	uint64_t mask;
111178479Sjb
112178479Sjb	if (b == 0)
113178479Sjb		return;
114178479Sjb
115178479Sjb	if (b < 0) {
116178479Sjb		b = -b;
117178479Sjb		if (b >= 64) {
118178479Sjb			a[0] = a[1] >> (b - 64);
119178479Sjb			a[1] = 0;
120178479Sjb		} else {
121178479Sjb			a[0] >>= b;
122178479Sjb			mask = 1LL << (64 - b);
123178479Sjb			mask -= 1;
124178479Sjb			a[0] |= ((a[1] & mask) << (64 - b));
125178479Sjb			a[1] >>= b;
126178479Sjb		}
127178479Sjb	} else {
128178479Sjb		if (b >= 64) {
129178479Sjb			a[1] = a[0] << (b - 64);
130178479Sjb			a[0] = 0;
131178479Sjb		} else {
132178479Sjb			a[1] <<= b;
133178479Sjb			mask = a[0] >> (64 - b);
134178479Sjb			a[1] |= mask;
135178479Sjb			a[0] <<= b;
136178479Sjb		}
137178479Sjb	}
138178479Sjb}
139178479Sjb
140178479Sjbstatic int
141178479Sjbdt_nbits_128(uint64_t *a)
142178479Sjb{
143178479Sjb	int nbits = 0;
144178479Sjb	uint64_t tmp[2];
145178479Sjb	uint64_t zero[2] = { 0, 0 };
146178479Sjb
147178479Sjb	tmp[0] = a[0];
148178479Sjb	tmp[1] = a[1];
149178479Sjb
150178479Sjb	dt_shift_128(tmp, -1);
151178479Sjb	while (dt_gt_128(tmp, zero)) {
152178479Sjb		dt_shift_128(tmp, -1);
153178479Sjb		nbits++;
154178479Sjb	}
155178479Sjb
156178479Sjb	return (nbits);
157178479Sjb}
158178479Sjb
159178479Sjbstatic void
160178479Sjbdt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
161178479Sjb{
162178479Sjb	uint64_t result[2];
163178479Sjb
164178479Sjb	result[0] = minuend[0] - subtrahend[0];
165178479Sjb	result[1] = minuend[1] - subtrahend[1] -
166178479Sjb	    (minuend[0] < subtrahend[0] ? 1 : 0);
167178479Sjb
168178479Sjb	difference[0] = result[0];
169178479Sjb	difference[1] = result[1];
170178479Sjb}
171178479Sjb
172178479Sjbstatic void
173178479Sjbdt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
174178479Sjb{
175178479Sjb	uint64_t result[2];
176178479Sjb
177178479Sjb	result[0] = addend1[0] + addend2[0];
178178479Sjb	result[1] = addend1[1] + addend2[1] +
179178479Sjb	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
180178479Sjb
181178479Sjb	sum[0] = result[0];
182178479Sjb	sum[1] = result[1];
183178479Sjb}
184178479Sjb
185178479Sjb/*
186178479Sjb * The basic idea is to break the 2 64-bit values into 4 32-bit values,
187178479Sjb * use native multiplication on those, and then re-combine into the
188178479Sjb * resulting 128-bit value.
189178479Sjb *
190178479Sjb * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
191178479Sjb *     hi1 * hi2 << 64 +
192178479Sjb *     hi1 * lo2 << 32 +
193178479Sjb *     hi2 * lo1 << 32 +
194178479Sjb *     lo1 * lo2
195178479Sjb */
196178479Sjbstatic void
197178479Sjbdt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
198178479Sjb{
199178479Sjb	uint64_t hi1, hi2, lo1, lo2;
200178479Sjb	uint64_t tmp[2];
201178479Sjb
202178479Sjb	hi1 = factor1 >> 32;
203178479Sjb	hi2 = factor2 >> 32;
204178479Sjb
205178479Sjb	lo1 = factor1 & DT_MASK_LO;
206178479Sjb	lo2 = factor2 & DT_MASK_LO;
207178479Sjb
208178479Sjb	product[0] = lo1 * lo2;
209178479Sjb	product[1] = hi1 * hi2;
210178479Sjb
211178479Sjb	tmp[0] = hi1 * lo2;
212178479Sjb	tmp[1] = 0;
213178479Sjb	dt_shift_128(tmp, 32);
214178479Sjb	dt_add_128(product, tmp, product);
215178479Sjb
216178479Sjb	tmp[0] = hi2 * lo1;
217178479Sjb	tmp[1] = 0;
218178479Sjb	dt_shift_128(tmp, 32);
219178479Sjb	dt_add_128(product, tmp, product);
220178479Sjb}
221178479Sjb
222178479Sjb/*
223178479Sjb * This is long-hand division.
224178479Sjb *
225178479Sjb * We initialize subtrahend by shifting divisor left as far as possible. We
226178479Sjb * loop, comparing subtrahend to dividend:  if subtrahend is smaller, we
227178479Sjb * subtract and set the appropriate bit in the result.  We then shift
228178479Sjb * subtrahend right by one bit for the next comparison.
229178479Sjb */
230178479Sjbstatic void
231178479Sjbdt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
232178479Sjb{
233178479Sjb	uint64_t result[2] = { 0, 0 };
234178479Sjb	uint64_t remainder[2];
235178479Sjb	uint64_t subtrahend[2];
236178479Sjb	uint64_t divisor_128[2];
237178479Sjb	uint64_t mask[2] = { 1, 0 };
238178479Sjb	int log = 0;
239178479Sjb
240178479Sjb	assert(divisor != 0);
241178479Sjb
242178479Sjb	divisor_128[0] = divisor;
243178479Sjb	divisor_128[1] = 0;
244178479Sjb
245178479Sjb	remainder[0] = dividend[0];
246178479Sjb	remainder[1] = dividend[1];
247178479Sjb
248178479Sjb	subtrahend[0] = divisor;
249178479Sjb	subtrahend[1] = 0;
250178479Sjb
251178479Sjb	while (divisor > 0) {
252178479Sjb		log++;
253178479Sjb		divisor >>= 1;
254178479Sjb	}
255178479Sjb
256178479Sjb	dt_shift_128(subtrahend, 128 - log);
257178479Sjb	dt_shift_128(mask, 128 - log);
258178479Sjb
259178479Sjb	while (dt_ge_128(remainder, divisor_128)) {
260178479Sjb		if (dt_ge_128(remainder, subtrahend)) {
261178479Sjb			dt_subtract_128(remainder, subtrahend, remainder);
262178479Sjb			result[0] |= mask[0];
263178479Sjb			result[1] |= mask[1];
264178479Sjb		}
265178479Sjb
266178479Sjb		dt_shift_128(subtrahend, -1);
267178479Sjb		dt_shift_128(mask, -1);
268178479Sjb	}
269178479Sjb
270178479Sjb	quotient[0] = result[0];
271178479Sjb	quotient[1] = result[1];
272178479Sjb}
273178479Sjb
274178479Sjb/*
275178479Sjb * This is the long-hand method of calculating a square root.
276178479Sjb * The algorithm is as follows:
277178479Sjb *
278178479Sjb * 1. Group the digits by 2 from the right.
279178479Sjb * 2. Over the leftmost group, find the largest single-digit number
280178479Sjb *    whose square is less than that group.
281178479Sjb * 3. Subtract the result of the previous step (2 or 4, depending) and
282178479Sjb *    bring down the next two-digit group.
283178479Sjb * 4. For the result R we have so far, find the largest single-digit number
284178479Sjb *    x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
285178479Sjb *    (Note that this is doubling R and performing a decimal left-shift by 1
286178479Sjb *    and searching for the appropriate decimal to fill the one's place.)
287178479Sjb *    The value x is the next digit in the square root.
288178479Sjb * Repeat steps 3 and 4 until the desired precision is reached.  (We're
289178479Sjb * dealing with integers, so the above is sufficient.)
290178479Sjb *
291178479Sjb * In decimal, the square root of 582,734 would be calculated as so:
292178479Sjb *
293178479Sjb *     __7__6__3
294178479Sjb *    | 58 27 34
295178479Sjb *     -49       (7^2 == 49 => 7 is the first digit in the square root)
296178479Sjb *      --
297178479Sjb *       9 27    (Subtract and bring down the next group.)
298178479Sjb * 146   8 76    (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
299178479Sjb *      -----     the square root)
300178479Sjb *         51 34 (Subtract and bring down the next group.)
301178479Sjb * 1523    45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
302178479Sjb *         -----  the square root)
303178479Sjb *          5 65 (remainder)
304178479Sjb *
305178479Sjb * The above algorithm applies similarly in binary, but note that the
306178479Sjb * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
307178479Sjb * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
308178479Sjb * preceding difference?
309178479Sjb *
310178479Sjb * In binary, the square root of 11011011 would be calculated as so:
311178479Sjb *
312178479Sjb *     __1__1__1__0
313178479Sjb *    | 11 01 10 11
314178479Sjb *      01          (0 << 2 + 1 == 1 < 11 => this bit is 1)
315178479Sjb *      --
316178479Sjb *      10 01 10 11
317178479Sjb * 101   1 01       (1 << 2 + 1 == 101 < 1001 => next bit is 1)
318178479Sjb *      -----
319178479Sjb *       1 00 10 11
320178479Sjb * 1101    11 01    (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
321178479Sjb *       -------
322178479Sjb *          1 01 11
323178479Sjb * 11101    1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
324178479Sjb *
325178479Sjb */
326178479Sjbstatic uint64_t
327178479Sjbdt_sqrt_128(uint64_t *square)
328178479Sjb{
329178479Sjb	uint64_t result[2] = { 0, 0 };
330178479Sjb	uint64_t diff[2] = { 0, 0 };
331178479Sjb	uint64_t one[2] = { 1, 0 };
332178479Sjb	uint64_t next_pair[2];
333178479Sjb	uint64_t next_try[2];
334178479Sjb	uint64_t bit_pairs, pair_shift;
335178479Sjb	int i;
336178479Sjb
337178479Sjb	bit_pairs = dt_nbits_128(square) / 2;
338178479Sjb	pair_shift = bit_pairs * 2;
339178479Sjb
340178479Sjb	for (i = 0; i <= bit_pairs; i++) {
341178479Sjb		/*
342178479Sjb		 * Bring down the next pair of bits.
343178479Sjb		 */
344178479Sjb		next_pair[0] = square[0];
345178479Sjb		next_pair[1] = square[1];
346178479Sjb		dt_shift_128(next_pair, -pair_shift);
347178479Sjb		next_pair[0] &= 0x3;
348178479Sjb		next_pair[1] = 0;
349178479Sjb
350178479Sjb		dt_shift_128(diff, 2);
351178479Sjb		dt_add_128(diff, next_pair, diff);
352178479Sjb
353178479Sjb		/*
354178479Sjb		 * next_try = R << 2 + 1
355178479Sjb		 */
356178479Sjb		next_try[0] = result[0];
357178479Sjb		next_try[1] = result[1];
358178479Sjb		dt_shift_128(next_try, 2);
359178479Sjb		dt_add_128(next_try, one, next_try);
360178479Sjb
361178479Sjb		if (dt_le_128(next_try, diff)) {
362178479Sjb			dt_subtract_128(diff, next_try, diff);
363178479Sjb			dt_shift_128(result, 1);
364178479Sjb			dt_add_128(result, one, result);
365178479Sjb		} else {
366178479Sjb			dt_shift_128(result, 1);
367178479Sjb		}
368178479Sjb
369178479Sjb		pair_shift -= 2;
370178479Sjb	}
371178479Sjb
372178479Sjb	assert(result[1] == 0);
373178479Sjb
374178479Sjb	return (result[0]);
375178479Sjb}
376178479Sjb
377178479Sjbuint64_t
378178479Sjbdt_stddev(uint64_t *data, uint64_t normal)
379178479Sjb{
380178479Sjb	uint64_t avg_of_squares[2];
381178479Sjb	uint64_t square_of_avg[2];
382178479Sjb	int64_t norm_avg;
383178479Sjb	uint64_t diff[2];
384178479Sjb
385178479Sjb	/*
386178479Sjb	 * The standard approximation for standard deviation is
387178479Sjb	 * sqrt(average(x**2) - average(x)**2), i.e. the square root
388178479Sjb	 * of the average of the squares minus the square of the average.
389178479Sjb	 */
390178479Sjb	dt_divide_128(data + 2, normal, avg_of_squares);
391178479Sjb	dt_divide_128(avg_of_squares, data[0], avg_of_squares);
392178479Sjb
393178479Sjb	norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
394178479Sjb
395178479Sjb	if (norm_avg < 0)
396178479Sjb		norm_avg = -norm_avg;
397178479Sjb
398178479Sjb	dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
399178479Sjb
400178479Sjb	dt_subtract_128(avg_of_squares, square_of_avg, diff);
401178479Sjb
402178479Sjb	return (dt_sqrt_128(diff));
403178479Sjb}
404178479Sjb
405178479Sjbstatic int
406178479Sjbdt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
407178479Sjb    dtrace_bufdesc_t *buf, size_t offs)
408178479Sjb{
409178479Sjb	dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
410178479Sjb	dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
411178479Sjb	char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
412178479Sjb	dtrace_flowkind_t flow = DTRACEFLOW_NONE;
413178479Sjb	const char *str = NULL;
414178479Sjb	static const char *e_str[2] = { " -> ", " => " };
415178479Sjb	static const char *r_str[2] = { " <- ", " <= " };
416178479Sjb	static const char *ent = "entry", *ret = "return";
417178479Sjb	static int entlen = 0, retlen = 0;
418178479Sjb	dtrace_epid_t next, id = epd->dtepd_epid;
419178479Sjb	int rval;
420178479Sjb
421178479Sjb	if (entlen == 0) {
422178479Sjb		assert(retlen == 0);
423178479Sjb		entlen = strlen(ent);
424178479Sjb		retlen = strlen(ret);
425178479Sjb	}
426178479Sjb
427178479Sjb	/*
428178479Sjb	 * If the name of the probe is "entry" or ends with "-entry", we
429178479Sjb	 * treat it as an entry; if it is "return" or ends with "-return",
430178479Sjb	 * we treat it as a return.  (This allows application-provided probes
431178479Sjb	 * like "method-entry" or "function-entry" to participate in flow
432178479Sjb	 * indentation -- without accidentally misinterpreting popular probe
433178479Sjb	 * names like "carpentry", "gentry" or "Coventry".)
434178479Sjb	 */
435178479Sjb	if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
436178479Sjb	    (sub == n || sub[-1] == '-')) {
437178479Sjb		flow = DTRACEFLOW_ENTRY;
438178479Sjb		str = e_str[strcmp(p, "syscall") == 0];
439178479Sjb	} else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
440178479Sjb	    (sub == n || sub[-1] == '-')) {
441178479Sjb		flow = DTRACEFLOW_RETURN;
442178479Sjb		str = r_str[strcmp(p, "syscall") == 0];
443178479Sjb	}
444178479Sjb
445178479Sjb	/*
446178479Sjb	 * If we're going to indent this, we need to check the ID of our last
447178479Sjb	 * call.  If we're looking at the same probe ID but a different EPID,
448178479Sjb	 * we _don't_ want to indent.  (Yes, there are some minor holes in
449178479Sjb	 * this scheme -- it's a heuristic.)
450178479Sjb	 */
451178479Sjb	if (flow == DTRACEFLOW_ENTRY) {
452178479Sjb		if ((last != DTRACE_EPIDNONE && id != last &&
453178479Sjb		    pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
454178479Sjb			flow = DTRACEFLOW_NONE;
455178479Sjb	}
456178479Sjb
457178479Sjb	/*
458178479Sjb	 * If we're going to unindent this, it's more difficult to see if
459178479Sjb	 * we don't actually want to unindent it -- we need to look at the
460178479Sjb	 * _next_ EPID.
461178479Sjb	 */
462178479Sjb	if (flow == DTRACEFLOW_RETURN) {
463178479Sjb		offs += epd->dtepd_size;
464178479Sjb
465178479Sjb		do {
466250574Smarkj			if (offs >= buf->dtbd_size)
467250574Smarkj				goto out;
468178479Sjb
469178479Sjb			next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
470178479Sjb
471178479Sjb			if (next == DTRACE_EPIDNONE)
472178479Sjb				offs += sizeof (id);
473178479Sjb		} while (next == DTRACE_EPIDNONE);
474178479Sjb
475178479Sjb		if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
476178479Sjb			return (rval);
477178479Sjb
478178479Sjb		if (next != id && npd->dtpd_id == pd->dtpd_id)
479178479Sjb			flow = DTRACEFLOW_NONE;
480178479Sjb	}
481178479Sjb
482178479Sjbout:
483178479Sjb	if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
484178479Sjb		data->dtpda_prefix = str;
485178479Sjb	} else {
486178479Sjb		data->dtpda_prefix = "| ";
487178479Sjb	}
488178479Sjb
489178479Sjb	if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
490178479Sjb		data->dtpda_indent -= 2;
491178479Sjb
492178479Sjb	data->dtpda_flow = flow;
493178479Sjb
494178479Sjb	return (0);
495178479Sjb}
496178479Sjb
497178479Sjbstatic int
498178479Sjbdt_nullprobe()
499178479Sjb{
500178479Sjb	return (DTRACE_CONSUME_THIS);
501178479Sjb}
502178479Sjb
503178479Sjbstatic int
504178479Sjbdt_nullrec()
505178479Sjb{
506178479Sjb	return (DTRACE_CONSUME_NEXT);
507178479Sjb}
508178479Sjb
509267942Srpaulostatic void
510267942Srpaulodt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total)
511267942Srpaulo{
512267942Srpaulo	long double val = dt_fabsl((long double)datum);
513267942Srpaulo
514267942Srpaulo	if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) {
515267942Srpaulo		*total += val;
516267942Srpaulo		return;
517267942Srpaulo	}
518267942Srpaulo
519267942Srpaulo	/*
520267942Srpaulo	 * If we're zooming in on an aggregation, we want the height of the
521267942Srpaulo	 * highest value to be approximately 95% of total bar height -- so we
522267942Srpaulo	 * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
523267942Srpaulo	 * our highest value.
524267942Srpaulo	 */
525267942Srpaulo	val *= 1 / DTRACE_AGGZOOM_MAX;
526267942Srpaulo
527267942Srpaulo	if (*total < val)
528267942Srpaulo		*total = val;
529267942Srpaulo}
530267942Srpaulo
531267942Srpaulostatic int
532267942Srpaulodt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width)
533267942Srpaulo{
534267942Srpaulo	return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n",
535267942Srpaulo	    width ? width : 16, width ? "key" : "value",
536267942Srpaulo	    "------------- Distribution -------------", "count"));
537267942Srpaulo}
538267942Srpaulo
539267942Srpaulostatic int
540267942Srpaulodt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width,
541267942Srpaulo    const dtrace_aggdata_t *aggdata, dtrace_actkind_t action)
542267942Srpaulo{
543267942Srpaulo	int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin;
544267942Srpaulo	int minwidth, maxwidth, i;
545267942Srpaulo
546267942Srpaulo	assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE);
547267942Srpaulo
548267942Srpaulo	if (action == DTRACEAGG_QUANTIZE) {
549267942Srpaulo		if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
550267942Srpaulo			min--;
551267942Srpaulo
552267942Srpaulo		if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
553267942Srpaulo			max++;
554267942Srpaulo
555267942Srpaulo		minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min));
556267942Srpaulo		maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max));
557267942Srpaulo	} else {
558267942Srpaulo		maxwidth = 8;
559267942Srpaulo		minwidth = maxwidth - 1;
560267942Srpaulo		max++;
561267942Srpaulo	}
562267942Srpaulo
563267942Srpaulo	if (dt_printf(dtp, fp, "\n%*s %*s .",
564267942Srpaulo	    width, width > 0 ? "key" : "", minwidth, "min") < 0)
565267942Srpaulo		return (-1);
566267942Srpaulo
567267942Srpaulo	for (i = min; i <= max; i++) {
568267942Srpaulo		if (dt_printf(dtp, fp, "-") < 0)
569267942Srpaulo			return (-1);
570267942Srpaulo	}
571267942Srpaulo
572267942Srpaulo	return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max"));
573267942Srpaulo}
574267942Srpaulo
575267942Srpaulo/*
576267942Srpaulo * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
577267942Srpaulo * inclusive) to represent aggregations via UTF-8 -- which are expressed via
578267942Srpaulo * 3-byte UTF-8 sequences.
579267942Srpaulo */
580267942Srpaulo#define	DTRACE_AGGUTF8_FULL	0x2588
581267942Srpaulo#define	DTRACE_AGGUTF8_BASE	0x258f
582267942Srpaulo#define	DTRACE_AGGUTF8_LEVELS	8
583267942Srpaulo
584267942Srpaulo#define	DTRACE_AGGUTF8_BYTE0(val)	(0xe0 | ((val) >> 12))
585267942Srpaulo#define	DTRACE_AGGUTF8_BYTE1(val)	(0x80 | (((val) >> 6) & 0x3f))
586267942Srpaulo#define	DTRACE_AGGUTF8_BYTE2(val)	(0x80 | ((val) & 0x3f))
587267942Srpaulo
588267942Srpaulostatic int
589267942Srpaulodt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
590267942Srpaulo    uint64_t normal, long double total)
591267942Srpaulo{
592267942Srpaulo	uint_t len = 40, i, whole, partial;
593267942Srpaulo	long double f = (dt_fabsl((long double)val) * len) / total;
594267942Srpaulo	const char *spaces = "                                        ";
595267942Srpaulo
596267942Srpaulo	whole = (uint_t)f;
597267942Srpaulo	partial = (uint_t)((f - (long double)(uint_t)f) *
598267942Srpaulo	    (long double)DTRACE_AGGUTF8_LEVELS);
599267942Srpaulo
600267942Srpaulo	if (dt_printf(dtp, fp, "|") < 0)
601267942Srpaulo		return (-1);
602267942Srpaulo
603267942Srpaulo	for (i = 0; i < whole; i++) {
604267942Srpaulo		if (dt_printf(dtp, fp, "%c%c%c",
605267942Srpaulo		    DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL),
606267942Srpaulo		    DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL),
607267942Srpaulo		    DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0)
608267942Srpaulo			return (-1);
609267942Srpaulo	}
610267942Srpaulo
611267942Srpaulo	if (partial != 0) {
612267942Srpaulo		partial = DTRACE_AGGUTF8_BASE - (partial - 1);
613267942Srpaulo
614267942Srpaulo		if (dt_printf(dtp, fp, "%c%c%c",
615267942Srpaulo		    DTRACE_AGGUTF8_BYTE0(partial),
616267942Srpaulo		    DTRACE_AGGUTF8_BYTE1(partial),
617267942Srpaulo		    DTRACE_AGGUTF8_BYTE2(partial)) < 0)
618267942Srpaulo			return (-1);
619267942Srpaulo
620267942Srpaulo		i++;
621267942Srpaulo	}
622267942Srpaulo
623267942Srpaulo	return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i,
624267942Srpaulo	    (long long)val / normal));
625267942Srpaulo}
626267942Srpaulo
627267942Srpaulostatic int
628178479Sjbdt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
629178479Sjb    uint64_t normal, long double total, char positives, char negatives)
630178479Sjb{
631178479Sjb	long double f;
632178479Sjb	uint_t depth, len = 40;
633178479Sjb
634178479Sjb	const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
635178479Sjb	const char *spaces = "                                        ";
636178479Sjb
637178479Sjb	assert(strlen(ats) == len && strlen(spaces) == len);
638178479Sjb	assert(!(total == 0 && (positives || negatives)));
639178479Sjb	assert(!(val < 0 && !negatives));
640178479Sjb	assert(!(val > 0 && !positives));
641178479Sjb	assert(!(val != 0 && total == 0));
642178479Sjb
643178479Sjb	if (!negatives) {
644178479Sjb		if (positives) {
645267942Srpaulo			if (dtp->dt_encoding == DT_ENCODING_UTF8) {
646267942Srpaulo				return (dt_print_quantline_utf8(dtp, fp, val,
647267942Srpaulo				    normal, total));
648267942Srpaulo			}
649267942Srpaulo
650178479Sjb			f = (dt_fabsl((long double)val) * len) / total;
651178479Sjb			depth = (uint_t)(f + 0.5);
652178479Sjb		} else {
653178479Sjb			depth = 0;
654178479Sjb		}
655178479Sjb
656178479Sjb		return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
657178479Sjb		    spaces + depth, (long long)val / normal));
658178479Sjb	}
659178479Sjb
660178479Sjb	if (!positives) {
661178479Sjb		f = (dt_fabsl((long double)val) * len) / total;
662178479Sjb		depth = (uint_t)(f + 0.5);
663178479Sjb
664178479Sjb		return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
665178479Sjb		    ats + len - depth, (long long)val / normal));
666178479Sjb	}
667178479Sjb
668178479Sjb	/*
669178479Sjb	 * If we're here, we have both positive and negative bucket values.
670178479Sjb	 * To express this graphically, we're going to generate both positive
671178479Sjb	 * and negative bars separated by a centerline.  These bars are half
672178479Sjb	 * the size of normal quantize()/lquantize() bars, so we divide the
673178479Sjb	 * length in half before calculating the bar length.
674178479Sjb	 */
675178479Sjb	len /= 2;
676178479Sjb	ats = &ats[len];
677178479Sjb	spaces = &spaces[len];
678178479Sjb
679178479Sjb	f = (dt_fabsl((long double)val) * len) / total;
680178479Sjb	depth = (uint_t)(f + 0.5);
681178479Sjb
682178479Sjb	if (val <= 0) {
683178479Sjb		return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
684178479Sjb		    ats + len - depth, len, "", (long long)val / normal));
685178479Sjb	} else {
686178479Sjb		return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
687178479Sjb		    ats + len - depth, spaces + depth,
688178479Sjb		    (long long)val / normal));
689178479Sjb	}
690178479Sjb}
691178479Sjb
692267942Srpaulo/*
693267942Srpaulo * As with UTF-8 printing of aggregations, we use a subset of the Unicode
694267942Srpaulo * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
695267942Srpaulo * aggregation.
696267942Srpaulo */
697267942Srpaulo#define	DTRACE_AGGPACK_BASE	0x2581
698267942Srpaulo#define	DTRACE_AGGPACK_LEVELS	8
699267942Srpaulo
700267942Srpaulostatic int
701267942Srpaulodt_print_packed(dtrace_hdl_t *dtp, FILE *fp,
702267942Srpaulo    long double datum, long double total)
703267942Srpaulo{
704267942Srpaulo	static boolean_t utf8_checked = B_FALSE;
705267942Srpaulo	static boolean_t utf8;
706267942Srpaulo	char *ascii = "__xxxxXX";
707267942Srpaulo	char *neg = "vvvvVV";
708267942Srpaulo	unsigned int len;
709267942Srpaulo	long double val;
710267942Srpaulo
711267942Srpaulo	if (!utf8_checked) {
712267942Srpaulo		char *term;
713267942Srpaulo
714267942Srpaulo		/*
715267942Srpaulo		 * We want to determine if we can reasonably emit UTF-8 for our
716267942Srpaulo		 * packed aggregation.  To do this, we will check for terminals
717267942Srpaulo		 * that are known to be primitive to emit UTF-8 on these.
718267942Srpaulo		 */
719267942Srpaulo		utf8_checked = B_TRUE;
720267942Srpaulo
721267942Srpaulo		if (dtp->dt_encoding == DT_ENCODING_ASCII) {
722267942Srpaulo			utf8 = B_FALSE;
723267942Srpaulo		} else if (dtp->dt_encoding == DT_ENCODING_UTF8) {
724267942Srpaulo			utf8 = B_TRUE;
725267942Srpaulo		} else if ((term = getenv("TERM")) != NULL &&
726267942Srpaulo		    (strcmp(term, "sun") == 0 ||
727267942Srpaulo		    strcmp(term, "sun-color") == 0) ||
728267942Srpaulo		    strcmp(term, "dumb") == 0) {
729267942Srpaulo			utf8 = B_FALSE;
730267942Srpaulo		} else {
731267942Srpaulo			utf8 = B_TRUE;
732267942Srpaulo		}
733267942Srpaulo	}
734267942Srpaulo
735267942Srpaulo	if (datum == 0)
736267942Srpaulo		return (dt_printf(dtp, fp, " "));
737267942Srpaulo
738267942Srpaulo	if (datum < 0) {
739267942Srpaulo		len = strlen(neg);
740267942Srpaulo		val = dt_fabsl(datum * (len - 1)) / total;
741267942Srpaulo		return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)]));
742267942Srpaulo	}
743267942Srpaulo
744267942Srpaulo	if (utf8) {
745267942Srpaulo		int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum *
746267942Srpaulo		    (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5);
747267942Srpaulo
748267942Srpaulo		return (dt_printf(dtp, fp, "%c%c%c",
749267942Srpaulo		    DTRACE_AGGUTF8_BYTE0(block),
750267942Srpaulo		    DTRACE_AGGUTF8_BYTE1(block),
751267942Srpaulo		    DTRACE_AGGUTF8_BYTE2(block)));
752267942Srpaulo	}
753267942Srpaulo
754267942Srpaulo	len = strlen(ascii);
755267942Srpaulo	val = (datum * (len - 1)) / total;
756267942Srpaulo	return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)]));
757267942Srpaulo}
758267942Srpaulo
759178479Sjbint
760178479Sjbdt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
761178479Sjb    size_t size, uint64_t normal)
762178479Sjb{
763178479Sjb	const int64_t *data = addr;
764178479Sjb	int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
765178479Sjb	long double total = 0;
766178479Sjb	char positives = 0, negatives = 0;
767178479Sjb
768178479Sjb	if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
769178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
770178479Sjb
771178479Sjb	while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
772178479Sjb		first_bin++;
773178479Sjb
774178479Sjb	if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
775178479Sjb		/*
776267942Srpaulo		 * There isn't any data.  This is possible if the aggregation
777267942Srpaulo		 * has been clear()'d or if negative increment values have been
778267942Srpaulo		 * used.  Regardless, we'll print the buckets around 0.
779178479Sjb		 */
780178479Sjb		first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
781178479Sjb		last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
782178479Sjb	} else {
783178479Sjb		if (first_bin > 0)
784178479Sjb			first_bin--;
785178479Sjb
786178479Sjb		while (last_bin > 0 && data[last_bin] == 0)
787178479Sjb			last_bin--;
788178479Sjb
789178479Sjb		if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
790178479Sjb			last_bin++;
791178479Sjb	}
792178479Sjb
793178479Sjb	for (i = first_bin; i <= last_bin; i++) {
794178479Sjb		positives |= (data[i] > 0);
795178479Sjb		negatives |= (data[i] < 0);
796267942Srpaulo		dt_quantize_total(dtp, data[i], &total);
797178479Sjb	}
798178479Sjb
799267942Srpaulo	if (dt_print_quanthdr(dtp, fp, 0) < 0)
800178479Sjb		return (-1);
801178479Sjb
802178479Sjb	for (i = first_bin; i <= last_bin; i++) {
803178479Sjb		if (dt_printf(dtp, fp, "%16lld ",
804178479Sjb		    (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
805178479Sjb			return (-1);
806178479Sjb
807178479Sjb		if (dt_print_quantline(dtp, fp, data[i], normal, total,
808178479Sjb		    positives, negatives) < 0)
809178479Sjb			return (-1);
810178479Sjb	}
811178479Sjb
812178479Sjb	return (0);
813178479Sjb}
814178479Sjb
815178479Sjbint
816267942Srpaulodt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
817267942Srpaulo    size_t size, const dtrace_aggdata_t *aggdata)
818267942Srpaulo{
819267942Srpaulo	const int64_t *data = addr;
820267942Srpaulo	long double total = 0, count = 0;
821267942Srpaulo	int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i;
822267942Srpaulo	int64_t minval, maxval;
823267942Srpaulo
824267942Srpaulo	if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
825267942Srpaulo		return (dt_set_errno(dtp, EDT_DMISMATCH));
826267942Srpaulo
827267942Srpaulo	if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
828267942Srpaulo		min--;
829267942Srpaulo
830267942Srpaulo	if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
831267942Srpaulo		max++;
832267942Srpaulo
833267942Srpaulo	minval = DTRACE_QUANTIZE_BUCKETVAL(min);
834267942Srpaulo	maxval = DTRACE_QUANTIZE_BUCKETVAL(max);
835267942Srpaulo
836267942Srpaulo	if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval),
837267942Srpaulo	    (long long)minval) < 0)
838267942Srpaulo		return (-1);
839267942Srpaulo
840267942Srpaulo	for (i = min; i <= max; i++) {
841267942Srpaulo		dt_quantize_total(dtp, data[i], &total);
842267942Srpaulo		count += data[i];
843267942Srpaulo	}
844267942Srpaulo
845267942Srpaulo	for (i = min; i <= max; i++) {
846267942Srpaulo		if (dt_print_packed(dtp, fp, data[i], total) < 0)
847267942Srpaulo			return (-1);
848267942Srpaulo	}
849267942Srpaulo
850267942Srpaulo	if (dt_printf(dtp, fp, ": %*lld | %lld\n",
851267942Srpaulo	    -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0)
852267942Srpaulo		return (-1);
853267942Srpaulo
854267942Srpaulo	return (0);
855267942Srpaulo}
856267942Srpaulo
857267942Srpauloint
858178479Sjbdt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
859178479Sjb    size_t size, uint64_t normal)
860178479Sjb{
861178479Sjb	const int64_t *data = addr;
862178479Sjb	int i, first_bin, last_bin, base;
863178479Sjb	uint64_t arg;
864178479Sjb	long double total = 0;
865178479Sjb	uint16_t step, levels;
866178479Sjb	char positives = 0, negatives = 0;
867178479Sjb
868178479Sjb	if (size < sizeof (uint64_t))
869178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
870178479Sjb
871178479Sjb	arg = *data++;
872178479Sjb	size -= sizeof (uint64_t);
873178479Sjb
874178479Sjb	base = DTRACE_LQUANTIZE_BASE(arg);
875178479Sjb	step = DTRACE_LQUANTIZE_STEP(arg);
876178479Sjb	levels = DTRACE_LQUANTIZE_LEVELS(arg);
877178479Sjb
878178479Sjb	first_bin = 0;
879178479Sjb	last_bin = levels + 1;
880178479Sjb
881178479Sjb	if (size != sizeof (uint64_t) * (levels + 2))
882178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
883178479Sjb
884178479Sjb	while (first_bin <= levels + 1 && data[first_bin] == 0)
885178479Sjb		first_bin++;
886178479Sjb
887178479Sjb	if (first_bin > levels + 1) {
888178479Sjb		first_bin = 0;
889178479Sjb		last_bin = 2;
890178479Sjb	} else {
891178479Sjb		if (first_bin > 0)
892178479Sjb			first_bin--;
893178479Sjb
894178479Sjb		while (last_bin > 0 && data[last_bin] == 0)
895178479Sjb			last_bin--;
896178479Sjb
897178479Sjb		if (last_bin < levels + 1)
898178479Sjb			last_bin++;
899178479Sjb	}
900178479Sjb
901178479Sjb	for (i = first_bin; i <= last_bin; i++) {
902178479Sjb		positives |= (data[i] > 0);
903178479Sjb		negatives |= (data[i] < 0);
904267942Srpaulo		dt_quantize_total(dtp, data[i], &total);
905178479Sjb	}
906178479Sjb
907178479Sjb	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
908178479Sjb	    "------------- Distribution -------------", "count") < 0)
909178479Sjb		return (-1);
910178479Sjb
911178479Sjb	for (i = first_bin; i <= last_bin; i++) {
912178479Sjb		char c[32];
913178479Sjb		int err;
914178479Sjb
915178479Sjb		if (i == 0) {
916267942Srpaulo			(void) snprintf(c, sizeof (c), "< %d", base);
917178479Sjb			err = dt_printf(dtp, fp, "%16s ", c);
918178479Sjb		} else if (i == levels + 1) {
919178479Sjb			(void) snprintf(c, sizeof (c), ">= %d",
920178479Sjb			    base + (levels * step));
921178479Sjb			err = dt_printf(dtp, fp, "%16s ", c);
922178479Sjb		} else {
923178479Sjb			err = dt_printf(dtp, fp, "%16d ",
924178479Sjb			    base + (i - 1) * step);
925178479Sjb		}
926178479Sjb
927178479Sjb		if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
928178479Sjb		    total, positives, negatives) < 0)
929178479Sjb			return (-1);
930178479Sjb	}
931178479Sjb
932178479Sjb	return (0);
933178479Sjb}
934178479Sjb
935267942Srpaulo/*ARGSUSED*/
936237624Spfgint
937267942Srpaulodt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
938267942Srpaulo    size_t size, const dtrace_aggdata_t *aggdata)
939267942Srpaulo{
940267942Srpaulo	const int64_t *data = addr;
941267942Srpaulo	long double total = 0, count = 0;
942267942Srpaulo	int min, max, base, err;
943267942Srpaulo	uint64_t arg;
944267942Srpaulo	uint16_t step, levels;
945267942Srpaulo	char c[32];
946267942Srpaulo	unsigned int i;
947267942Srpaulo
948267942Srpaulo	if (size < sizeof (uint64_t))
949267942Srpaulo		return (dt_set_errno(dtp, EDT_DMISMATCH));
950267942Srpaulo
951267942Srpaulo	arg = *data++;
952267942Srpaulo	size -= sizeof (uint64_t);
953267942Srpaulo
954267942Srpaulo	base = DTRACE_LQUANTIZE_BASE(arg);
955267942Srpaulo	step = DTRACE_LQUANTIZE_STEP(arg);
956267942Srpaulo	levels = DTRACE_LQUANTIZE_LEVELS(arg);
957267942Srpaulo
958267942Srpaulo	if (size != sizeof (uint64_t) * (levels + 2))
959267942Srpaulo		return (dt_set_errno(dtp, EDT_DMISMATCH));
960267942Srpaulo
961267942Srpaulo	min = 0;
962267942Srpaulo	max = levels + 1;
963267942Srpaulo
964267942Srpaulo	if (min == 0) {
965267942Srpaulo		(void) snprintf(c, sizeof (c), "< %d", base);
966267942Srpaulo		err = dt_printf(dtp, fp, "%8s :", c);
967267942Srpaulo	} else {
968267942Srpaulo		err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step);
969267942Srpaulo	}
970267942Srpaulo
971267942Srpaulo	if (err < 0)
972267942Srpaulo		return (-1);
973267942Srpaulo
974267942Srpaulo	for (i = min; i <= max; i++) {
975267942Srpaulo		dt_quantize_total(dtp, data[i], &total);
976267942Srpaulo		count += data[i];
977267942Srpaulo	}
978267942Srpaulo
979267942Srpaulo	for (i = min; i <= max; i++) {
980267942Srpaulo		if (dt_print_packed(dtp, fp, data[i], total) < 0)
981267942Srpaulo			return (-1);
982267942Srpaulo	}
983267942Srpaulo
984267942Srpaulo	(void) snprintf(c, sizeof (c), ">= %d", base + (levels * step));
985267942Srpaulo	return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count));
986267942Srpaulo}
987267942Srpaulo
988267942Srpauloint
989237624Spfgdt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
990237624Spfg    size_t size, uint64_t normal)
991237624Spfg{
992237624Spfg	int i, first_bin, last_bin, bin = 1, order, levels;
993237624Spfg	uint16_t factor, low, high, nsteps;
994237624Spfg	const int64_t *data = addr;
995237624Spfg	int64_t value = 1, next, step;
996237624Spfg	char positives = 0, negatives = 0;
997237624Spfg	long double total = 0;
998237624Spfg	uint64_t arg;
999237624Spfg	char c[32];
1000237624Spfg
1001237624Spfg	if (size < sizeof (uint64_t))
1002237624Spfg		return (dt_set_errno(dtp, EDT_DMISMATCH));
1003237624Spfg
1004237624Spfg	arg = *data++;
1005237624Spfg	size -= sizeof (uint64_t);
1006237624Spfg
1007237624Spfg	factor = DTRACE_LLQUANTIZE_FACTOR(arg);
1008237624Spfg	low = DTRACE_LLQUANTIZE_LOW(arg);
1009237624Spfg	high = DTRACE_LLQUANTIZE_HIGH(arg);
1010237624Spfg	nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
1011237624Spfg
1012237624Spfg	/*
1013237624Spfg	 * We don't expect to be handed invalid llquantize() parameters here,
1014237624Spfg	 * but sanity check them (to a degree) nonetheless.
1015237624Spfg	 */
1016237624Spfg	if (size > INT32_MAX || factor < 2 || low >= high ||
1017237624Spfg	    nsteps == 0 || factor > nsteps)
1018237624Spfg		return (dt_set_errno(dtp, EDT_DMISMATCH));
1019237624Spfg
1020237624Spfg	levels = (int)size / sizeof (uint64_t);
1021237624Spfg
1022237624Spfg	first_bin = 0;
1023237624Spfg	last_bin = levels - 1;
1024237624Spfg
1025237624Spfg	while (first_bin < levels && data[first_bin] == 0)
1026237624Spfg		first_bin++;
1027237624Spfg
1028237624Spfg	if (first_bin == levels) {
1029237624Spfg		first_bin = 0;
1030237624Spfg		last_bin = 1;
1031237624Spfg	} else {
1032237624Spfg		if (first_bin > 0)
1033237624Spfg			first_bin--;
1034237624Spfg
1035237624Spfg		while (last_bin > 0 && data[last_bin] == 0)
1036237624Spfg			last_bin--;
1037237624Spfg
1038237624Spfg		if (last_bin < levels - 1)
1039237624Spfg			last_bin++;
1040237624Spfg	}
1041237624Spfg
1042237624Spfg	for (i = first_bin; i <= last_bin; i++) {
1043237624Spfg		positives |= (data[i] > 0);
1044237624Spfg		negatives |= (data[i] < 0);
1045267942Srpaulo		dt_quantize_total(dtp, data[i], &total);
1046237624Spfg	}
1047237624Spfg
1048237624Spfg	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
1049237624Spfg	    "------------- Distribution -------------", "count") < 0)
1050237624Spfg		return (-1);
1051237624Spfg
1052237624Spfg	for (order = 0; order < low; order++)
1053237624Spfg		value *= factor;
1054237624Spfg
1055237624Spfg	next = value * factor;
1056237624Spfg	step = next > nsteps ? next / nsteps : 1;
1057237624Spfg
1058237624Spfg	if (first_bin == 0) {
1059237716Spfg		(void) snprintf(c, sizeof (c), "< %lld", (long long)value);
1060237624Spfg
1061237624Spfg		if (dt_printf(dtp, fp, "%16s ", c) < 0)
1062237624Spfg			return (-1);
1063237624Spfg
1064237624Spfg		if (dt_print_quantline(dtp, fp, data[0], normal,
1065237624Spfg		    total, positives, negatives) < 0)
1066237624Spfg			return (-1);
1067237624Spfg	}
1068237624Spfg
1069237624Spfg	while (order <= high) {
1070237624Spfg		if (bin >= first_bin && bin <= last_bin) {
1071237624Spfg			if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
1072237624Spfg				return (-1);
1073237624Spfg
1074237624Spfg			if (dt_print_quantline(dtp, fp, data[bin],
1075237624Spfg			    normal, total, positives, negatives) < 0)
1076237624Spfg				return (-1);
1077237624Spfg		}
1078237624Spfg
1079237624Spfg		assert(value < next);
1080237624Spfg		bin++;
1081237624Spfg
1082237624Spfg		if ((value += step) != next)
1083237624Spfg			continue;
1084237624Spfg
1085237624Spfg		next = value * factor;
1086237624Spfg		step = next > nsteps ? next / nsteps : 1;
1087237624Spfg		order++;
1088237624Spfg	}
1089237624Spfg
1090237624Spfg	if (last_bin < bin)
1091237624Spfg		return (0);
1092237624Spfg
1093237624Spfg	assert(last_bin == bin);
1094238071Sdim	(void) snprintf(c, sizeof (c), ">= %lld", (long long)value);
1095237624Spfg
1096237624Spfg	if (dt_printf(dtp, fp, "%16s ", c) < 0)
1097237624Spfg		return (-1);
1098237624Spfg
1099237624Spfg	return (dt_print_quantline(dtp, fp, data[bin], normal,
1100237624Spfg	    total, positives, negatives));
1101237624Spfg}
1102237624Spfg
1103178479Sjb/*ARGSUSED*/
1104178479Sjbstatic int
1105178479Sjbdt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1106178479Sjb    size_t size, uint64_t normal)
1107178479Sjb{
1108178479Sjb	/* LINTED - alignment */
1109178479Sjb	int64_t *data = (int64_t *)addr;
1110178479Sjb
1111178479Sjb	return (dt_printf(dtp, fp, " %16lld", data[0] ?
1112178479Sjb	    (long long)(data[1] / (int64_t)normal / data[0]) : 0));
1113178479Sjb}
1114178479Sjb
1115178479Sjb/*ARGSUSED*/
1116178479Sjbstatic int
1117178479Sjbdt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1118178479Sjb    size_t size, uint64_t normal)
1119178479Sjb{
1120178479Sjb	/* LINTED - alignment */
1121178479Sjb	uint64_t *data = (uint64_t *)addr;
1122178479Sjb
1123178479Sjb	return (dt_printf(dtp, fp, " %16llu", data[0] ?
1124178479Sjb	    (unsigned long long) dt_stddev(data, normal) : 0));
1125178479Sjb}
1126178479Sjb
1127178479Sjb/*ARGSUSED*/
1128267942Srpaulostatic int
1129178479Sjbdt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1130248690Spfg    size_t nbytes, int width, int quiet, int forceraw)
1131178479Sjb{
1132178479Sjb	/*
1133178479Sjb	 * If the byte stream is a series of printable characters, followed by
1134178479Sjb	 * a terminating byte, we print it out as a string.  Otherwise, we
1135178479Sjb	 * assume that it's something else and just print the bytes.
1136178479Sjb	 */
1137178479Sjb	int i, j, margin = 5;
1138178479Sjb	char *c = (char *)addr;
1139178479Sjb
1140178479Sjb	if (nbytes == 0)
1141178479Sjb		return (0);
1142178479Sjb
1143248690Spfg	if (forceraw)
1144178479Sjb		goto raw;
1145178479Sjb
1146248690Spfg	if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
1147248690Spfg		goto raw;
1148248690Spfg
1149178479Sjb	for (i = 0; i < nbytes; i++) {
1150178479Sjb		/*
1151178479Sjb		 * We define a "printable character" to be one for which
1152178479Sjb		 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
1153178479Sjb		 * or a character which is either backspace or the bell.
1154178479Sjb		 * Backspace and the bell are regrettably special because
1155178479Sjb		 * they fail the first two tests -- and yet they are entirely
1156178479Sjb		 * printable.  These are the only two control characters that
1157178479Sjb		 * have meaning for the terminal and for which isprint(3C) and
1158178479Sjb		 * isspace(3C) return 0.
1159178479Sjb		 */
1160178479Sjb		if (isprint(c[i]) || isspace(c[i]) ||
1161178479Sjb		    c[i] == '\b' || c[i] == '\a')
1162178479Sjb			continue;
1163178479Sjb
1164178479Sjb		if (c[i] == '\0' && i > 0) {
1165178479Sjb			/*
1166178479Sjb			 * This looks like it might be a string.  Before we
1167178479Sjb			 * assume that it is indeed a string, check the
1168178479Sjb			 * remainder of the byte range; if it contains
1169178479Sjb			 * additional non-nul characters, we'll assume that
1170178479Sjb			 * it's a binary stream that just happens to look like
1171178479Sjb			 * a string, and we'll print out the individual bytes.
1172178479Sjb			 */
1173178479Sjb			for (j = i + 1; j < nbytes; j++) {
1174178479Sjb				if (c[j] != '\0')
1175178479Sjb					break;
1176178479Sjb			}
1177178479Sjb
1178178479Sjb			if (j != nbytes)
1179178479Sjb				break;
1180178479Sjb
1181267942Srpaulo			if (quiet) {
1182178479Sjb				return (dt_printf(dtp, fp, "%s", c));
1183267942Srpaulo			} else {
1184267942Srpaulo				return (dt_printf(dtp, fp, " %s%*s",
1185267942Srpaulo				    width < 0 ? " " : "", width, c));
1186267942Srpaulo			}
1187178479Sjb		}
1188178479Sjb
1189178479Sjb		break;
1190178479Sjb	}
1191178479Sjb
1192178479Sjb	if (i == nbytes) {
1193178479Sjb		/*
1194178479Sjb		 * The byte range is all printable characters, but there is
1195178479Sjb		 * no trailing nul byte.  We'll assume that it's a string and
1196178479Sjb		 * print it as such.
1197178479Sjb		 */
1198178479Sjb		char *s = alloca(nbytes + 1);
1199178479Sjb		bcopy(c, s, nbytes);
1200178479Sjb		s[nbytes] = '\0';
1201178479Sjb		return (dt_printf(dtp, fp, "  %-*s", width, s));
1202178479Sjb	}
1203178479Sjb
1204178479Sjbraw:
1205178479Sjb	if (dt_printf(dtp, fp, "\n%*s      ", margin, "") < 0)
1206178479Sjb		return (-1);
1207178479Sjb
1208178479Sjb	for (i = 0; i < 16; i++)
1209178479Sjb		if (dt_printf(dtp, fp, "  %c", "0123456789abcdef"[i]) < 0)
1210178479Sjb			return (-1);
1211178479Sjb
1212178479Sjb	if (dt_printf(dtp, fp, "  0123456789abcdef\n") < 0)
1213178479Sjb		return (-1);
1214178479Sjb
1215178479Sjb
1216178479Sjb	for (i = 0; i < nbytes; i += 16) {
1217178479Sjb		if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
1218178479Sjb			return (-1);
1219178479Sjb
1220178479Sjb		for (j = i; j < i + 16 && j < nbytes; j++) {
1221178479Sjb			if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
1222178479Sjb				return (-1);
1223178479Sjb		}
1224178479Sjb
1225178479Sjb		while (j++ % 16) {
1226178479Sjb			if (dt_printf(dtp, fp, "   ") < 0)
1227178479Sjb				return (-1);
1228178479Sjb		}
1229178479Sjb
1230178479Sjb		if (dt_printf(dtp, fp, "  ") < 0)
1231178479Sjb			return (-1);
1232178479Sjb
1233178479Sjb		for (j = i; j < i + 16 && j < nbytes; j++) {
1234178479Sjb			if (dt_printf(dtp, fp, "%c",
1235178479Sjb			    c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
1236178479Sjb				return (-1);
1237178479Sjb		}
1238178479Sjb
1239178479Sjb		if (dt_printf(dtp, fp, "\n") < 0)
1240178479Sjb			return (-1);
1241178479Sjb	}
1242178479Sjb
1243178479Sjb	return (0);
1244178479Sjb}
1245178479Sjb
1246178479Sjbint
1247178479Sjbdt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1248178479Sjb    caddr_t addr, int depth, int size)
1249178479Sjb{
1250178479Sjb	dtrace_syminfo_t dts;
1251178479Sjb	GElf_Sym sym;
1252178479Sjb	int i, indent;
1253178479Sjb	char c[PATH_MAX * 2];
1254178479Sjb	uint64_t pc;
1255178479Sjb
1256178479Sjb	if (dt_printf(dtp, fp, "\n") < 0)
1257178479Sjb		return (-1);
1258178479Sjb
1259178479Sjb	if (format == NULL)
1260178479Sjb		format = "%s";
1261178479Sjb
1262178479Sjb	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1263178479Sjb		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1264178479Sjb	else
1265178479Sjb		indent = _dtrace_stkindent;
1266178479Sjb
1267178479Sjb	for (i = 0; i < depth; i++) {
1268178479Sjb		switch (size) {
1269178479Sjb		case sizeof (uint32_t):
1270178479Sjb			/* LINTED - alignment */
1271178479Sjb			pc = *((uint32_t *)addr);
1272178479Sjb			break;
1273178479Sjb
1274178479Sjb		case sizeof (uint64_t):
1275178479Sjb			/* LINTED - alignment */
1276178479Sjb			pc = *((uint64_t *)addr);
1277178479Sjb			break;
1278178479Sjb
1279178479Sjb		default:
1280178479Sjb			return (dt_set_errno(dtp, EDT_BADSTACKPC));
1281178479Sjb		}
1282178479Sjb
1283178576Sjb		if (pc == 0)
1284178479Sjb			break;
1285178479Sjb
1286178479Sjb		addr += size;
1287178479Sjb
1288178479Sjb		if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
1289178479Sjb			return (-1);
1290178479Sjb
1291178479Sjb		if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1292178479Sjb			if (pc > sym.st_value) {
1293178479Sjb				(void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
1294178479Sjb				    dts.dts_object, dts.dts_name,
1295228579Sdim				    (u_longlong_t)(pc - sym.st_value));
1296178479Sjb			} else {
1297178479Sjb				(void) snprintf(c, sizeof (c), "%s`%s",
1298178479Sjb				    dts.dts_object, dts.dts_name);
1299178479Sjb			}
1300178479Sjb		} else {
1301178479Sjb			/*
1302178479Sjb			 * We'll repeat the lookup, but this time we'll specify
1303178479Sjb			 * a NULL GElf_Sym -- indicating that we're only
1304178479Sjb			 * interested in the containing module.
1305178479Sjb			 */
1306178479Sjb			if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1307178479Sjb				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1308228579Sdim				    dts.dts_object, (u_longlong_t)pc);
1309178479Sjb			} else {
1310228579Sdim				(void) snprintf(c, sizeof (c), "0x%llx",
1311228579Sdim				    (u_longlong_t)pc);
1312178479Sjb			}
1313178479Sjb		}
1314178479Sjb
1315178479Sjb		if (dt_printf(dtp, fp, format, c) < 0)
1316178479Sjb			return (-1);
1317178479Sjb
1318178479Sjb		if (dt_printf(dtp, fp, "\n") < 0)
1319178479Sjb			return (-1);
1320178479Sjb	}
1321178479Sjb
1322178479Sjb	return (0);
1323178479Sjb}
1324178479Sjb
1325178479Sjbint
1326178479Sjbdt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327178479Sjb    caddr_t addr, uint64_t arg)
1328178479Sjb{
1329178479Sjb	/* LINTED - alignment */
1330178479Sjb	uint64_t *pc = (uint64_t *)addr;
1331178479Sjb	uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1332178479Sjb	uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1333178479Sjb	const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1334178479Sjb	const char *str = strsize ? strbase : NULL;
1335178479Sjb	int err = 0;
1336178479Sjb
1337178479Sjb	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1338178479Sjb	struct ps_prochandle *P;
1339178479Sjb	GElf_Sym sym;
1340178479Sjb	int i, indent;
1341178479Sjb	pid_t pid;
1342178479Sjb
1343178479Sjb	if (depth == 0)
1344178479Sjb		return (0);
1345178479Sjb
1346178479Sjb	pid = (pid_t)*pc++;
1347178479Sjb
1348178479Sjb	if (dt_printf(dtp, fp, "\n") < 0)
1349178479Sjb		return (-1);
1350178479Sjb
1351178479Sjb	if (format == NULL)
1352178479Sjb		format = "%s";
1353178479Sjb
1354178479Sjb	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1355178479Sjb		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1356178479Sjb	else
1357178479Sjb		indent = _dtrace_stkindent;
1358178479Sjb
1359178479Sjb	/*
1360178479Sjb	 * Ultimately, we need to add an entry point in the library vector for
1361178479Sjb	 * determining <symbol, offset> from <pid, address>.  For now, if
1362178479Sjb	 * this is a vector open, we just print the raw address or string.
1363178479Sjb	 */
1364178479Sjb	if (dtp->dt_vector == NULL)
1365178479Sjb		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1366178479Sjb	else
1367178479Sjb		P = NULL;
1368178479Sjb
1369178479Sjb	if (P != NULL)
1370178479Sjb		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1371178479Sjb
1372178576Sjb	for (i = 0; i < depth && pc[i] != 0; i++) {
1373178479Sjb		const prmap_t *map;
1374178479Sjb
1375178479Sjb		if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1376178479Sjb			break;
1377178479Sjb
1378178479Sjb		if (P != NULL && Plookup_by_addr(P, pc[i],
1379178479Sjb		    name, sizeof (name), &sym) == 0) {
1380178479Sjb			(void) Pobjname(P, pc[i], objname, sizeof (objname));
1381178479Sjb
1382178479Sjb			if (pc[i] > sym.st_value) {
1383178479Sjb				(void) snprintf(c, sizeof (c),
1384178479Sjb				    "%s`%s+0x%llx", dt_basename(objname), name,
1385178479Sjb				    (u_longlong_t)(pc[i] - sym.st_value));
1386178479Sjb			} else {
1387178479Sjb				(void) snprintf(c, sizeof (c),
1388178479Sjb				    "%s`%s", dt_basename(objname), name);
1389178479Sjb			}
1390178479Sjb		} else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1391178479Sjb		    (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1392178479Sjb		    (map->pr_mflags & MA_WRITE)))) {
1393178479Sjb			/*
1394178479Sjb			 * If the current string pointer in the string table
1395178479Sjb			 * does not point to an empty string _and_ the program
1396178479Sjb			 * counter falls in a writable region, we'll use the
1397178479Sjb			 * string from the string table instead of the raw
1398178479Sjb			 * address.  This last condition is necessary because
1399178479Sjb			 * some (broken) ustack helpers will return a string
1400178479Sjb			 * even for a program counter that they can't
1401178479Sjb			 * identify.  If we have a string for a program
1402178479Sjb			 * counter that falls in a segment that isn't
1403178479Sjb			 * writable, we assume that we have fallen into this
1404178479Sjb			 * case and we refuse to use the string.
1405178479Sjb			 */
1406178479Sjb			(void) snprintf(c, sizeof (c), "%s", str);
1407178479Sjb		} else {
1408178479Sjb			if (P != NULL && Pobjname(P, pc[i], objname,
1409178576Sjb			    sizeof (objname)) != 0) {
1410178479Sjb				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1411178479Sjb				    dt_basename(objname), (u_longlong_t)pc[i]);
1412178479Sjb			} else {
1413178479Sjb				(void) snprintf(c, sizeof (c), "0x%llx",
1414178479Sjb				    (u_longlong_t)pc[i]);
1415178479Sjb			}
1416178479Sjb		}
1417178479Sjb
1418178479Sjb		if ((err = dt_printf(dtp, fp, format, c)) < 0)
1419178479Sjb			break;
1420178479Sjb
1421178479Sjb		if ((err = dt_printf(dtp, fp, "\n")) < 0)
1422178479Sjb			break;
1423178479Sjb
1424178479Sjb		if (str != NULL && str[0] == '@') {
1425178479Sjb			/*
1426178479Sjb			 * If the first character of the string is an "at" sign,
1427178479Sjb			 * then the string is inferred to be an annotation --
1428178479Sjb			 * and it is printed out beneath the frame and offset
1429178479Sjb			 * with brackets.
1430178479Sjb			 */
1431178479Sjb			if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1432178479Sjb				break;
1433178479Sjb
1434178479Sjb			(void) snprintf(c, sizeof (c), "  [ %s ]", &str[1]);
1435178479Sjb
1436178479Sjb			if ((err = dt_printf(dtp, fp, format, c)) < 0)
1437178479Sjb				break;
1438178479Sjb
1439178479Sjb			if ((err = dt_printf(dtp, fp, "\n")) < 0)
1440178479Sjb				break;
1441178479Sjb		}
1442178479Sjb
1443178479Sjb		if (str != NULL) {
1444178479Sjb			str += strlen(str) + 1;
1445178479Sjb			if (str - strbase >= strsize)
1446178479Sjb				str = NULL;
1447178479Sjb		}
1448178479Sjb	}
1449178479Sjb
1450178479Sjb	if (P != NULL) {
1451178479Sjb		dt_proc_unlock(dtp, P);
1452178479Sjb		dt_proc_release(dtp, P);
1453178479Sjb	}
1454178479Sjb
1455178479Sjb	return (err);
1456178479Sjb}
1457178479Sjb
1458178479Sjbstatic int
1459178479Sjbdt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1460178479Sjb{
1461178479Sjb	/* LINTED - alignment */
1462178479Sjb	uint64_t pid = ((uint64_t *)addr)[0];
1463178479Sjb	/* LINTED - alignment */
1464178479Sjb	uint64_t pc = ((uint64_t *)addr)[1];
1465178479Sjb	const char *format = "  %-50s";
1466178479Sjb	char *s;
1467178479Sjb	int n, len = 256;
1468178479Sjb
1469178479Sjb	if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1470178479Sjb		struct ps_prochandle *P;
1471178479Sjb
1472178479Sjb		if ((P = dt_proc_grab(dtp, pid,
1473178479Sjb		    PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1474178479Sjb			GElf_Sym sym;
1475178479Sjb
1476178479Sjb			dt_proc_lock(dtp, P);
1477178479Sjb
1478178479Sjb			if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1479178479Sjb				pc = sym.st_value;
1480178479Sjb
1481178479Sjb			dt_proc_unlock(dtp, P);
1482178479Sjb			dt_proc_release(dtp, P);
1483178479Sjb		}
1484178479Sjb	}
1485178479Sjb
1486178479Sjb	do {
1487178479Sjb		n = len;
1488178479Sjb		s = alloca(n);
1489210767Srpaulo	} while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1490178479Sjb
1491178479Sjb	return (dt_printf(dtp, fp, format, s));
1492178479Sjb}
1493178479Sjb
1494178479Sjbint
1495178479Sjbdt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1496178479Sjb{
1497178479Sjb	/* LINTED - alignment */
1498178479Sjb	uint64_t pid = ((uint64_t *)addr)[0];
1499178479Sjb	/* LINTED - alignment */
1500178479Sjb	uint64_t pc = ((uint64_t *)addr)[1];
1501178479Sjb	int err = 0;
1502178479Sjb
1503178479Sjb	char objname[PATH_MAX], c[PATH_MAX * 2];
1504178479Sjb	struct ps_prochandle *P;
1505178479Sjb
1506178479Sjb	if (format == NULL)
1507178479Sjb		format = "  %-50s";
1508178479Sjb
1509178479Sjb	/*
1510178479Sjb	 * See the comment in dt_print_ustack() for the rationale for
1511178479Sjb	 * printing raw addresses in the vectored case.
1512178479Sjb	 */
1513178479Sjb	if (dtp->dt_vector == NULL)
1514178479Sjb		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1515178479Sjb	else
1516178479Sjb		P = NULL;
1517178479Sjb
1518178479Sjb	if (P != NULL)
1519178479Sjb		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1520178479Sjb
1521178576Sjb	if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) {
1522178479Sjb		(void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1523178479Sjb	} else {
1524178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1525178479Sjb	}
1526178479Sjb
1527178479Sjb	err = dt_printf(dtp, fp, format, c);
1528178479Sjb
1529178479Sjb	if (P != NULL) {
1530178479Sjb		dt_proc_unlock(dtp, P);
1531178479Sjb		dt_proc_release(dtp, P);
1532178479Sjb	}
1533178479Sjb
1534178479Sjb	return (err);
1535178479Sjb}
1536178479Sjb
1537178576Sjbint
1538178576Sjbdt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1539178576Sjb{
1540178576Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1541178576Sjb	size_t nbytes = *((uintptr_t *) addr);
1542178576Sjb
1543178576Sjb	return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t),
1544178576Sjb	    nbytes, 50, quiet, 1));
1545178576Sjb}
1546178576Sjb
1547178576Sjbtypedef struct dt_type_cbdata {
1548178576Sjb	dtrace_hdl_t		*dtp;
1549178576Sjb	dtrace_typeinfo_t	dtt;
1550178576Sjb	caddr_t			addr;
1551178576Sjb	caddr_t			addrend;
1552178576Sjb	const char		*name;
1553178576Sjb	int			f_type;
1554178576Sjb	int			indent;
1555178576Sjb	int			type_width;
1556178576Sjb	int			name_width;
1557178576Sjb	FILE			*fp;
1558178576Sjb} dt_type_cbdata_t;
1559178576Sjb
1560178576Sjbstatic int	dt_print_type_data(dt_type_cbdata_t *, ctf_id_t);
1561178576Sjb
1562178479Sjbstatic int
1563178576Sjbdt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1564178576Sjb{
1565178576Sjb	dt_type_cbdata_t cbdata;
1566178576Sjb	dt_type_cbdata_t *cbdatap = arg;
1567178576Sjb	ssize_t ssz;
1568178576Sjb
1569178576Sjb	if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0)
1570178576Sjb		return (0);
1571178576Sjb
1572178576Sjb	off /= 8;
1573178576Sjb
1574178576Sjb	cbdata = *cbdatap;
1575178576Sjb	cbdata.name = name;
1576178576Sjb	cbdata.addr += off;
1577178576Sjb	cbdata.addrend = cbdata.addr + ssz;
1578178576Sjb
1579178576Sjb	return (dt_print_type_data(&cbdata, type));
1580178576Sjb}
1581178576Sjb
1582178576Sjbstatic int
1583178576Sjbdt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg)
1584178576Sjb{
1585178576Sjb	char buf[DT_TYPE_NAMELEN];
1586178576Sjb	char *p;
1587178576Sjb	dt_type_cbdata_t *cbdatap = arg;
1588178576Sjb	size_t sz = strlen(name);
1589178576Sjb
1590178576Sjb	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1591178576Sjb
1592178576Sjb	if ((p = strchr(buf, '[')) != NULL)
1593178576Sjb		p[-1] = '\0';
1594178576Sjb	else
1595178576Sjb		p = "";
1596178576Sjb
1597178576Sjb	sz += strlen(p);
1598178576Sjb
1599178576Sjb	if (sz > cbdatap->name_width)
1600178576Sjb		cbdatap->name_width = sz;
1601178576Sjb
1602178576Sjb	sz = strlen(buf);
1603178576Sjb
1604178576Sjb	if (sz > cbdatap->type_width)
1605178576Sjb		cbdatap->type_width = sz;
1606178576Sjb
1607178576Sjb	return (0);
1608178576Sjb}
1609178576Sjb
1610178576Sjbstatic int
1611178576Sjbdt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type)
1612178576Sjb{
1613178576Sjb	caddr_t addr = cbdatap->addr;
1614178576Sjb	caddr_t addrend = cbdatap->addrend;
1615178576Sjb	char buf[DT_TYPE_NAMELEN];
1616178576Sjb	char *p;
1617178576Sjb	int cnt = 0;
1618178576Sjb	uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type);
1619178576Sjb	ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type);
1620178576Sjb
1621178576Sjb	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1622178576Sjb
1623178576Sjb	if ((p = strchr(buf, '[')) != NULL)
1624178576Sjb		p[-1] = '\0';
1625178576Sjb	else
1626178576Sjb		p = "";
1627178576Sjb
1628178576Sjb	if (cbdatap->f_type) {
1629178576Sjb		int type_width = roundup(cbdatap->type_width + 1, 4);
1630178576Sjb		int name_width = roundup(cbdatap->name_width + 1, 4);
1631178576Sjb
1632178576Sjb		name_width -= strlen(cbdatap->name);
1633178576Sjb
1634178576Sjb		dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s	= ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p);
1635178576Sjb	}
1636178576Sjb
1637178576Sjb	while (addr < addrend) {
1638178576Sjb		dt_type_cbdata_t cbdata;
1639178576Sjb		ctf_arinfo_t arinfo;
1640178576Sjb		ctf_encoding_t cte;
1641178576Sjb		uintptr_t *up;
1642178576Sjb		void *vp = addr;
1643178576Sjb		cbdata = *cbdatap;
1644178576Sjb		cbdata.name = "";
1645178576Sjb		cbdata.addr = addr;
1646178576Sjb		cbdata.addrend = addr + ssz;
1647178576Sjb		cbdata.f_type = 0;
1648178576Sjb		cbdata.indent++;
1649178576Sjb		cbdata.type_width = 0;
1650178576Sjb		cbdata.name_width = 0;
1651178576Sjb
1652178576Sjb		if (cnt > 0)
1653178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,"");
1654178576Sjb
1655178576Sjb		switch (kind) {
1656178576Sjb		case CTF_K_INTEGER:
1657178576Sjb			if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0)
1658178576Sjb				return (-1);
1659178576Sjb			if ((cte.cte_format & CTF_INT_SIGNED) != 0)
1660178576Sjb				switch (cte.cte_bits) {
1661178576Sjb				case 8:
1662178576Sjb					if (isprint(*((char *) vp)))
1663178576Sjb						dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp));
1664178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp));
1665178576Sjb					break;
1666178576Sjb				case 16:
1667178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp));
1668178576Sjb					break;
1669178576Sjb				case 32:
1670178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp));
1671178576Sjb					break;
1672178576Sjb				case 64:
1673178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp));
1674178576Sjb					break;
1675178576Sjb				default:
1676178576Sjb					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);
1677178576Sjb					break;
1678178576Sjb				}
1679178576Sjb			else
1680178576Sjb				switch (cte.cte_bits) {
1681178576Sjb				case 8:
1682178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff);
1683178576Sjb					break;
1684178576Sjb				case 16:
1685178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp));
1686178576Sjb					break;
1687178576Sjb				case 32:
1688178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp));
1689178576Sjb					break;
1690178576Sjb				case 64:
1691178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp));
1692178576Sjb					break;
1693178576Sjb				default:
1694178576Sjb					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);
1695178576Sjb					break;
1696178576Sjb				}
1697178576Sjb			break;
1698178576Sjb		case CTF_K_FLOAT:
1699178576Sjb			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);
1700178576Sjb			break;
1701178576Sjb		case CTF_K_POINTER:
1702178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr));
1703178576Sjb			break;
1704178576Sjb		case CTF_K_ARRAY:
1705178576Sjb			if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0)
1706178576Sjb				return (-1);
1707178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,"");
1708178576Sjb			dt_print_type_data(&cbdata, arinfo.ctr_contents);
1709178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1710178576Sjb			break;
1711178576Sjb		case CTF_K_FUNCTION:
1712178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n");
1713178576Sjb			break;
1714178576Sjb		case CTF_K_STRUCT:
1715178576Sjb			cbdata.f_type = 1;
1716178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1717178576Sjb			    dt_print_type_width, &cbdata) != 0)
1718178576Sjb				return (-1);
1719178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1720178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1721178576Sjb			    dt_print_type_member, &cbdata) != 0)
1722178576Sjb				return (-1);
1723178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1724178576Sjb			break;
1725178576Sjb		case CTF_K_UNION:
1726178576Sjb			cbdata.f_type = 1;
1727178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1728178576Sjb			    dt_print_type_width, &cbdata) != 0)
1729178576Sjb				return (-1);
1730178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1731178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1732178576Sjb			    dt_print_type_member, &cbdata) != 0)
1733178576Sjb				return (-1);
1734178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1735178576Sjb			break;
1736178576Sjb		case CTF_K_ENUM:
1737178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp)));
1738178576Sjb			break;
1739178576Sjb		case CTF_K_TYPEDEF:
1740178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1741178576Sjb			break;
1742178576Sjb		case CTF_K_VOLATILE:
1743178576Sjb			if (cbdatap->f_type)
1744178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "volatile ");
1745178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1746178576Sjb			break;
1747178576Sjb		case CTF_K_CONST:
1748178576Sjb			if (cbdatap->f_type)
1749178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "const ");
1750178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1751178576Sjb			break;
1752178576Sjb		case CTF_K_RESTRICT:
1753178576Sjb			if (cbdatap->f_type)
1754178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "restrict ");
1755178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1756178576Sjb			break;
1757178576Sjb		default:
1758178576Sjb			break;
1759178576Sjb		}
1760178576Sjb
1761178576Sjb		addr += ssz;
1762178576Sjb		cnt++;
1763178576Sjb	}
1764178576Sjb
1765178576Sjb	return (0);
1766178576Sjb}
1767178576Sjb
1768178576Sjbstatic int
1769178576Sjbdt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1770178576Sjb{
1771178576Sjb	caddr_t addrend;
1772178576Sjb	char *p;
1773178576Sjb	dtrace_typeinfo_t dtt;
1774178576Sjb	dt_type_cbdata_t cbdata;
1775178576Sjb	int num = 0;
1776178576Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1777178576Sjb	ssize_t ssz;
1778178576Sjb
1779178576Sjb	if (!quiet)
1780178576Sjb		dt_printf(dtp, fp, "\n");
1781178576Sjb
1782178576Sjb	/* Get the total number of bytes of data buffered. */
1783178576Sjb	size_t nbytes = *((uintptr_t *) addr);
1784178576Sjb	addr += sizeof(uintptr_t);
1785178576Sjb
1786178576Sjb	/*
1787178576Sjb	 * Get the size of the type so that we can check that it matches
1788178576Sjb	 * the CTF data we look up and so that we can figure out how many
1789178576Sjb	 * type elements are buffered.
1790178576Sjb	 */
1791178576Sjb	size_t typs = *((uintptr_t *) addr);
1792178576Sjb	addr += sizeof(uintptr_t);
1793178576Sjb
1794178576Sjb	/*
1795178576Sjb	 * Point to the type string in the buffer. Get it's string
1796178576Sjb	 * length and round it up to become the offset to the start
1797178576Sjb	 * of the buffered type data which we would like to be aligned
1798178576Sjb	 * for easy access.
1799178576Sjb	 */
1800178576Sjb	char *strp = (char *) addr;
1801178576Sjb	int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t));
1802178576Sjb
1803178576Sjb	/*
1804178576Sjb	 * The type string might have a format such as 'int [20]'.
1805178576Sjb	 * Check if there is an array dimension present.
1806178576Sjb	 */
1807178576Sjb	if ((p = strchr(strp, '[')) != NULL) {
1808178576Sjb		/* Strip off the array dimension. */
1809178576Sjb		*p++ = '\0';
1810178576Sjb
1811178576Sjb		for (; *p != '\0' && *p != ']'; p++)
1812178576Sjb			num = num * 10 + *p - '0';
1813178576Sjb	} else
1814178576Sjb		/* No array dimension, so default. */
1815178576Sjb		num = 1;
1816178576Sjb
1817178576Sjb	/* Lookup the CTF type from the type string. */
1818178576Sjb	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, strp, &dtt) < 0)
1819178576Sjb		return (-1);
1820178576Sjb
1821178576Sjb	/* Offset the buffer address to the start of the data... */
1822178576Sjb	addr += offset;
1823178576Sjb
1824178576Sjb	ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1825178576Sjb
1826178576Sjb	if (typs != ssz) {
1827178576Sjb		printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz);
1828178576Sjb		return (-1);
1829178576Sjb	}
1830178576Sjb
1831178576Sjb	cbdata.dtp = dtp;
1832178576Sjb	cbdata.dtt = dtt;
1833178576Sjb	cbdata.name = "";
1834178576Sjb	cbdata.addr = addr;
1835178576Sjb	cbdata.addrend = addr + nbytes;
1836178576Sjb	cbdata.indent = 1;
1837178576Sjb	cbdata.f_type = 1;
1838178576Sjb	cbdata.type_width = 0;
1839178576Sjb	cbdata.name_width = 0;
1840178576Sjb	cbdata.fp = fp;
1841178576Sjb
1842178576Sjb	return (dt_print_type_data(&cbdata, dtt.dtt_type));
1843178576Sjb}
1844178576Sjb
1845178576Sjbstatic int
1846178479Sjbdt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1847178479Sjb{
1848178479Sjb	/* LINTED - alignment */
1849178479Sjb	uint64_t pc = *((uint64_t *)addr);
1850178479Sjb	dtrace_syminfo_t dts;
1851178479Sjb	GElf_Sym sym;
1852178479Sjb	char c[PATH_MAX * 2];
1853178479Sjb
1854178479Sjb	if (format == NULL)
1855178479Sjb		format = "  %-50s";
1856178479Sjb
1857178479Sjb	if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1858178479Sjb		(void) snprintf(c, sizeof (c), "%s`%s",
1859178479Sjb		    dts.dts_object, dts.dts_name);
1860178479Sjb	} else {
1861178479Sjb		/*
1862178479Sjb		 * We'll repeat the lookup, but this time we'll specify a
1863178479Sjb		 * NULL GElf_Sym -- indicating that we're only interested in
1864178479Sjb		 * the containing module.
1865178479Sjb		 */
1866178479Sjb		if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1867178479Sjb			(void) snprintf(c, sizeof (c), "%s`0x%llx",
1868178479Sjb			    dts.dts_object, (u_longlong_t)pc);
1869178479Sjb		} else {
1870178479Sjb			(void) snprintf(c, sizeof (c), "0x%llx",
1871178479Sjb			    (u_longlong_t)pc);
1872178479Sjb		}
1873178479Sjb	}
1874178479Sjb
1875178479Sjb	if (dt_printf(dtp, fp, format, c) < 0)
1876178479Sjb		return (-1);
1877178479Sjb
1878178479Sjb	return (0);
1879178479Sjb}
1880178479Sjb
1881178479Sjbint
1882178479Sjbdt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1883178479Sjb{
1884178479Sjb	/* LINTED - alignment */
1885178479Sjb	uint64_t pc = *((uint64_t *)addr);
1886178479Sjb	dtrace_syminfo_t dts;
1887178479Sjb	char c[PATH_MAX * 2];
1888178479Sjb
1889178479Sjb	if (format == NULL)
1890178479Sjb		format = "  %-50s";
1891178479Sjb
1892178479Sjb	if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1893178479Sjb		(void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1894178479Sjb	} else {
1895178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1896178479Sjb	}
1897178479Sjb
1898178479Sjb	if (dt_printf(dtp, fp, format, c) < 0)
1899178479Sjb		return (-1);
1900178479Sjb
1901178479Sjb	return (0);
1902178479Sjb}
1903178479Sjb
1904178479Sjbtypedef struct dt_normal {
1905178479Sjb	dtrace_aggvarid_t dtnd_id;
1906178479Sjb	uint64_t dtnd_normal;
1907178479Sjb} dt_normal_t;
1908178479Sjb
1909178479Sjbstatic int
1910178479Sjbdt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1911178479Sjb{
1912178479Sjb	dt_normal_t *normal = arg;
1913178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1914178479Sjb	dtrace_aggvarid_t id = normal->dtnd_id;
1915178479Sjb
1916178479Sjb	if (agg->dtagd_nrecs == 0)
1917178479Sjb		return (DTRACE_AGGWALK_NEXT);
1918178479Sjb
1919178479Sjb	if (agg->dtagd_varid != id)
1920178479Sjb		return (DTRACE_AGGWALK_NEXT);
1921178479Sjb
1922178479Sjb	((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1923178479Sjb	return (DTRACE_AGGWALK_NORMALIZE);
1924178479Sjb}
1925178479Sjb
1926178479Sjbstatic int
1927178479Sjbdt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1928178479Sjb{
1929178479Sjb	dt_normal_t normal;
1930178479Sjb	caddr_t addr;
1931178479Sjb
1932178479Sjb	/*
1933178479Sjb	 * We (should) have two records:  the aggregation ID followed by the
1934178479Sjb	 * normalization value.
1935178479Sjb	 */
1936178479Sjb	addr = base + rec->dtrd_offset;
1937178479Sjb
1938178479Sjb	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1939178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1940178479Sjb
1941178479Sjb	/* LINTED - alignment */
1942178479Sjb	normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1943178479Sjb	rec++;
1944178479Sjb
1945178479Sjb	if (rec->dtrd_action != DTRACEACT_LIBACT)
1946178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1947178479Sjb
1948178479Sjb	if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1949178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1950178479Sjb
1951178479Sjb	addr = base + rec->dtrd_offset;
1952178479Sjb
1953178479Sjb	switch (rec->dtrd_size) {
1954178479Sjb	case sizeof (uint64_t):
1955178479Sjb		/* LINTED - alignment */
1956178479Sjb		normal.dtnd_normal = *((uint64_t *)addr);
1957178479Sjb		break;
1958178479Sjb	case sizeof (uint32_t):
1959178479Sjb		/* LINTED - alignment */
1960178479Sjb		normal.dtnd_normal = *((uint32_t *)addr);
1961178479Sjb		break;
1962178479Sjb	case sizeof (uint16_t):
1963178479Sjb		/* LINTED - alignment */
1964178479Sjb		normal.dtnd_normal = *((uint16_t *)addr);
1965178479Sjb		break;
1966178479Sjb	case sizeof (uint8_t):
1967178479Sjb		normal.dtnd_normal = *((uint8_t *)addr);
1968178479Sjb		break;
1969178479Sjb	default:
1970178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1971178479Sjb	}
1972178479Sjb
1973178479Sjb	(void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1974178479Sjb
1975178479Sjb	return (0);
1976178479Sjb}
1977178479Sjb
1978178479Sjbstatic int
1979178479Sjbdt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1980178479Sjb{
1981178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1982178479Sjb	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1983178479Sjb
1984178479Sjb	if (agg->dtagd_nrecs == 0)
1985178479Sjb		return (DTRACE_AGGWALK_NEXT);
1986178479Sjb
1987178479Sjb	if (agg->dtagd_varid != id)
1988178479Sjb		return (DTRACE_AGGWALK_NEXT);
1989178479Sjb
1990178479Sjb	return (DTRACE_AGGWALK_DENORMALIZE);
1991178479Sjb}
1992178479Sjb
1993178479Sjbstatic int
1994178479Sjbdt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1995178479Sjb{
1996178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1997178479Sjb	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1998178479Sjb
1999178479Sjb	if (agg->dtagd_nrecs == 0)
2000178479Sjb		return (DTRACE_AGGWALK_NEXT);
2001178479Sjb
2002178479Sjb	if (agg->dtagd_varid != id)
2003178479Sjb		return (DTRACE_AGGWALK_NEXT);
2004178479Sjb
2005178479Sjb	return (DTRACE_AGGWALK_CLEAR);
2006178479Sjb}
2007178479Sjb
2008178479Sjbtypedef struct dt_trunc {
2009178479Sjb	dtrace_aggvarid_t dttd_id;
2010178479Sjb	uint64_t dttd_remaining;
2011178479Sjb} dt_trunc_t;
2012178479Sjb
2013178479Sjbstatic int
2014178479Sjbdt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
2015178479Sjb{
2016178479Sjb	dt_trunc_t *trunc = arg;
2017178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2018178479Sjb	dtrace_aggvarid_t id = trunc->dttd_id;
2019178479Sjb
2020178479Sjb	if (agg->dtagd_nrecs == 0)
2021178479Sjb		return (DTRACE_AGGWALK_NEXT);
2022178479Sjb
2023178479Sjb	if (agg->dtagd_varid != id)
2024178479Sjb		return (DTRACE_AGGWALK_NEXT);
2025178479Sjb
2026178479Sjb	if (trunc->dttd_remaining == 0)
2027178479Sjb		return (DTRACE_AGGWALK_REMOVE);
2028178479Sjb
2029178479Sjb	trunc->dttd_remaining--;
2030178479Sjb	return (DTRACE_AGGWALK_NEXT);
2031178479Sjb}
2032178479Sjb
2033178479Sjbstatic int
2034178479Sjbdt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
2035178479Sjb{
2036178479Sjb	dt_trunc_t trunc;
2037178479Sjb	caddr_t addr;
2038178479Sjb	int64_t remaining;
2039178479Sjb	int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
2040178479Sjb
2041178479Sjb	/*
2042178479Sjb	 * We (should) have two records:  the aggregation ID followed by the
2043178479Sjb	 * number of aggregation entries after which the aggregation is to be
2044178479Sjb	 * truncated.
2045178479Sjb	 */
2046178479Sjb	addr = base + rec->dtrd_offset;
2047178479Sjb
2048178479Sjb	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
2049178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
2050178479Sjb
2051178479Sjb	/* LINTED - alignment */
2052178479Sjb	trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
2053178479Sjb	rec++;
2054178479Sjb
2055178479Sjb	if (rec->dtrd_action != DTRACEACT_LIBACT)
2056178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
2057178479Sjb
2058178479Sjb	if (rec->dtrd_arg != DT_ACT_TRUNC)
2059178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
2060178479Sjb
2061178479Sjb	addr = base + rec->dtrd_offset;
2062178479Sjb
2063178479Sjb	switch (rec->dtrd_size) {
2064178479Sjb	case sizeof (uint64_t):
2065178479Sjb		/* LINTED - alignment */
2066178479Sjb		remaining = *((int64_t *)addr);
2067178479Sjb		break;
2068178479Sjb	case sizeof (uint32_t):
2069178479Sjb		/* LINTED - alignment */
2070178479Sjb		remaining = *((int32_t *)addr);
2071178479Sjb		break;
2072178479Sjb	case sizeof (uint16_t):
2073178479Sjb		/* LINTED - alignment */
2074178479Sjb		remaining = *((int16_t *)addr);
2075178479Sjb		break;
2076178479Sjb	case sizeof (uint8_t):
2077178479Sjb		remaining = *((int8_t *)addr);
2078178479Sjb		break;
2079178479Sjb	default:
2080178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
2081178479Sjb	}
2082178479Sjb
2083178479Sjb	if (remaining < 0) {
2084178479Sjb		func = dtrace_aggregate_walk_valsorted;
2085178479Sjb		remaining = -remaining;
2086178479Sjb	} else {
2087178479Sjb		func = dtrace_aggregate_walk_valrevsorted;
2088178479Sjb	}
2089178479Sjb
2090178479Sjb	assert(remaining >= 0);
2091178479Sjb	trunc.dttd_remaining = remaining;
2092178479Sjb
2093178479Sjb	(void) func(dtp, dt_trunc_agg, &trunc);
2094178479Sjb
2095178479Sjb	return (0);
2096178479Sjb}
2097178479Sjb
2098178479Sjbstatic int
2099178479Sjbdt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
2100267942Srpaulo    caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata,
2101267942Srpaulo    uint64_t normal, dt_print_aggdata_t *pd)
2102178479Sjb{
2103267942Srpaulo	int err, width;
2104178479Sjb	dtrace_actkind_t act = rec->dtrd_action;
2105267942Srpaulo	boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack;
2106267942Srpaulo	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2107178479Sjb
2108267942Srpaulo	static struct {
2109267942Srpaulo		size_t size;
2110267942Srpaulo		int width;
2111267942Srpaulo		int packedwidth;
2112267942Srpaulo	} *fmt, fmttab[] = {
2113267942Srpaulo		{ sizeof (uint8_t),	3,	3 },
2114267942Srpaulo		{ sizeof (uint16_t),	5,	5 },
2115267942Srpaulo		{ sizeof (uint32_t),	8,	8 },
2116267942Srpaulo		{ sizeof (uint64_t),	16,	16 },
2117267942Srpaulo		{ 0,			-50,	16 }
2118267942Srpaulo	};
2119267942Srpaulo
2120267942Srpaulo	if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) {
2121267942Srpaulo		dtrace_recdesc_t *r;
2122267942Srpaulo
2123267942Srpaulo		width = 0;
2124267942Srpaulo
2125267942Srpaulo		/*
2126267942Srpaulo		 * To print our quantization header for either an agghist or
2127267942Srpaulo		 * aggpack aggregation, we need to iterate through all of our
2128267942Srpaulo		 * of our records to determine their width.
2129267942Srpaulo		 */
2130267942Srpaulo		for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) {
2131267942Srpaulo			for (fmt = fmttab; fmt->size &&
2132267942Srpaulo			    fmt->size != r->dtrd_size; fmt++)
2133267942Srpaulo				continue;
2134267942Srpaulo
2135267942Srpaulo			width += fmt->packedwidth + 1;
2136267942Srpaulo		}
2137267942Srpaulo
2138267942Srpaulo		if (pd->dtpa_agghist) {
2139267942Srpaulo			if (dt_print_quanthdr(dtp, fp, width) < 0)
2140267942Srpaulo				return (-1);
2141267942Srpaulo		} else {
2142267942Srpaulo			if (dt_print_quanthdr_packed(dtp, fp,
2143267942Srpaulo			    width, aggdata, r->dtrd_action) < 0)
2144267942Srpaulo				return (-1);
2145267942Srpaulo		}
2146267942Srpaulo
2147267942Srpaulo		pd->dtpa_agghisthdr = agg->dtagd_varid;
2148267942Srpaulo	}
2149267942Srpaulo
2150267942Srpaulo	if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) {
2151267942Srpaulo		char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES;
2152267942Srpaulo		char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES;
2153267942Srpaulo		int64_t val;
2154267942Srpaulo
2155267942Srpaulo		assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT);
2156267942Srpaulo		val = (long long)*((uint64_t *)addr);
2157267942Srpaulo
2158267942Srpaulo		if (dt_printf(dtp, fp, " ") < 0)
2159267942Srpaulo			return (-1);
2160267942Srpaulo
2161267942Srpaulo		return (dt_print_quantline(dtp, fp, val, normal,
2162267942Srpaulo		    aggdata->dtada_total, positives, negatives));
2163267942Srpaulo	}
2164267942Srpaulo
2165267942Srpaulo	if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) {
2166267942Srpaulo		switch (act) {
2167267942Srpaulo		case DTRACEAGG_QUANTIZE:
2168267942Srpaulo			return (dt_print_quantize_packed(dtp,
2169267942Srpaulo			    fp, addr, size, aggdata));
2170267942Srpaulo		case DTRACEAGG_LQUANTIZE:
2171267942Srpaulo			return (dt_print_lquantize_packed(dtp,
2172267942Srpaulo			    fp, addr, size, aggdata));
2173267942Srpaulo		default:
2174267942Srpaulo			break;
2175267942Srpaulo		}
2176267942Srpaulo	}
2177267942Srpaulo
2178178479Sjb	switch (act) {
2179178479Sjb	case DTRACEACT_STACK:
2180178479Sjb		return (dt_print_stack(dtp, fp, NULL, addr,
2181178479Sjb		    rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
2182178479Sjb
2183178479Sjb	case DTRACEACT_USTACK:
2184178479Sjb	case DTRACEACT_JSTACK:
2185178479Sjb		return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
2186178479Sjb
2187178479Sjb	case DTRACEACT_USYM:
2188178479Sjb	case DTRACEACT_UADDR:
2189178479Sjb		return (dt_print_usym(dtp, fp, addr, act));
2190178479Sjb
2191178479Sjb	case DTRACEACT_UMOD:
2192178479Sjb		return (dt_print_umod(dtp, fp, NULL, addr));
2193178479Sjb
2194178479Sjb	case DTRACEACT_SYM:
2195178479Sjb		return (dt_print_sym(dtp, fp, NULL, addr));
2196178479Sjb
2197178479Sjb	case DTRACEACT_MOD:
2198178479Sjb		return (dt_print_mod(dtp, fp, NULL, addr));
2199178479Sjb
2200178479Sjb	case DTRACEAGG_QUANTIZE:
2201178479Sjb		return (dt_print_quantize(dtp, fp, addr, size, normal));
2202178479Sjb
2203178479Sjb	case DTRACEAGG_LQUANTIZE:
2204178479Sjb		return (dt_print_lquantize(dtp, fp, addr, size, normal));
2205178479Sjb
2206237624Spfg	case DTRACEAGG_LLQUANTIZE:
2207237624Spfg		return (dt_print_llquantize(dtp, fp, addr, size, normal));
2208237624Spfg
2209178479Sjb	case DTRACEAGG_AVG:
2210178479Sjb		return (dt_print_average(dtp, fp, addr, size, normal));
2211178479Sjb
2212178479Sjb	case DTRACEAGG_STDDEV:
2213178479Sjb		return (dt_print_stddev(dtp, fp, addr, size, normal));
2214178479Sjb
2215178479Sjb	default:
2216178479Sjb		break;
2217178479Sjb	}
2218178479Sjb
2219267942Srpaulo	for (fmt = fmttab; fmt->size && fmt->size != size; fmt++)
2220267942Srpaulo		continue;
2221267942Srpaulo
2222267942Srpaulo	width = packed ? fmt->packedwidth : fmt->width;
2223267942Srpaulo
2224178479Sjb	switch (size) {
2225178479Sjb	case sizeof (uint64_t):
2226267942Srpaulo		err = dt_printf(dtp, fp, " %*lld", width,
2227178479Sjb		    /* LINTED - alignment */
2228178479Sjb		    (long long)*((uint64_t *)addr) / normal);
2229178479Sjb		break;
2230178479Sjb	case sizeof (uint32_t):
2231178479Sjb		/* LINTED - alignment */
2232267942Srpaulo		err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) /
2233178479Sjb		    (uint32_t)normal);
2234178479Sjb		break;
2235178479Sjb	case sizeof (uint16_t):
2236178479Sjb		/* LINTED - alignment */
2237267942Srpaulo		err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) /
2238178479Sjb		    (uint32_t)normal);
2239178479Sjb		break;
2240178479Sjb	case sizeof (uint8_t):
2241267942Srpaulo		err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) /
2242178479Sjb		    (uint32_t)normal);
2243178479Sjb		break;
2244178479Sjb	default:
2245267942Srpaulo		err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0);
2246178479Sjb		break;
2247178479Sjb	}
2248178479Sjb
2249178479Sjb	return (err);
2250178479Sjb}
2251178479Sjb
2252178479Sjbint
2253178479Sjbdt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
2254178479Sjb{
2255178479Sjb	int i, aggact = 0;
2256178479Sjb	dt_print_aggdata_t *pd = arg;
2257178479Sjb	const dtrace_aggdata_t *aggdata = aggsdata[0];
2258178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2259178479Sjb	FILE *fp = pd->dtpa_fp;
2260178479Sjb	dtrace_hdl_t *dtp = pd->dtpa_dtp;
2261178479Sjb	dtrace_recdesc_t *rec;
2262178479Sjb	dtrace_actkind_t act;
2263178479Sjb	caddr_t addr;
2264178479Sjb	size_t size;
2265178479Sjb
2266267942Srpaulo	pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL);
2267267942Srpaulo	pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN);
2268267942Srpaulo
2269178479Sjb	/*
2270178479Sjb	 * Iterate over each record description in the key, printing the traced
2271178479Sjb	 * data, skipping the first datum (the tuple member created by the
2272178479Sjb	 * compiler).
2273178479Sjb	 */
2274178479Sjb	for (i = 1; i < agg->dtagd_nrecs; i++) {
2275178479Sjb		rec = &agg->dtagd_rec[i];
2276178479Sjb		act = rec->dtrd_action;
2277178479Sjb		addr = aggdata->dtada_data + rec->dtrd_offset;
2278178479Sjb		size = rec->dtrd_size;
2279178479Sjb
2280178479Sjb		if (DTRACEACT_ISAGG(act)) {
2281178479Sjb			aggact = i;
2282178479Sjb			break;
2283178479Sjb		}
2284178479Sjb
2285267942Srpaulo		if (dt_print_datum(dtp, fp, rec, addr,
2286267942Srpaulo		    size, aggdata, 1, pd) < 0)
2287178479Sjb			return (-1);
2288178479Sjb
2289178479Sjb		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2290178479Sjb		    DTRACE_BUFDATA_AGGKEY) < 0)
2291178479Sjb			return (-1);
2292178479Sjb	}
2293178479Sjb
2294178479Sjb	assert(aggact != 0);
2295178479Sjb
2296178479Sjb	for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
2297178479Sjb		uint64_t normal;
2298178479Sjb
2299178479Sjb		aggdata = aggsdata[i];
2300178479Sjb		agg = aggdata->dtada_desc;
2301178479Sjb		rec = &agg->dtagd_rec[aggact];
2302178479Sjb		act = rec->dtrd_action;
2303178479Sjb		addr = aggdata->dtada_data + rec->dtrd_offset;
2304178479Sjb		size = rec->dtrd_size;
2305178479Sjb
2306178479Sjb		assert(DTRACEACT_ISAGG(act));
2307178479Sjb		normal = aggdata->dtada_normal;
2308178479Sjb
2309267942Srpaulo		if (dt_print_datum(dtp, fp, rec, addr,
2310267942Srpaulo		    size, aggdata, normal, pd) < 0)
2311178479Sjb			return (-1);
2312178479Sjb
2313178479Sjb		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
2314178479Sjb		    DTRACE_BUFDATA_AGGVAL) < 0)
2315178479Sjb			return (-1);
2316178479Sjb
2317178479Sjb		if (!pd->dtpa_allunprint)
2318178479Sjb			agg->dtagd_flags |= DTRACE_AGD_PRINTED;
2319178479Sjb	}
2320178479Sjb
2321267942Srpaulo	if (!pd->dtpa_agghist && !pd->dtpa_aggpack) {
2322267942Srpaulo		if (dt_printf(dtp, fp, "\n") < 0)
2323267942Srpaulo			return (-1);
2324267942Srpaulo	}
2325178479Sjb
2326178479Sjb	if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
2327178479Sjb	    DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
2328178479Sjb		return (-1);
2329178479Sjb
2330178479Sjb	return (0);
2331178479Sjb}
2332178479Sjb
2333178479Sjbint
2334178479Sjbdt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
2335178479Sjb{
2336178479Sjb	dt_print_aggdata_t *pd = arg;
2337178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2338178479Sjb	dtrace_aggvarid_t aggvarid = pd->dtpa_id;
2339178479Sjb
2340178479Sjb	if (pd->dtpa_allunprint) {
2341178479Sjb		if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
2342178479Sjb			return (0);
2343178479Sjb	} else {
2344178479Sjb		/*
2345178479Sjb		 * If we're not printing all unprinted aggregations, then the
2346178479Sjb		 * aggregation variable ID denotes a specific aggregation
2347178479Sjb		 * variable that we should print -- skip any other aggregations
2348178479Sjb		 * that we encounter.
2349178479Sjb		 */
2350178479Sjb		if (agg->dtagd_nrecs == 0)
2351178479Sjb			return (0);
2352178479Sjb
2353178479Sjb		if (aggvarid != agg->dtagd_varid)
2354178479Sjb			return (0);
2355178479Sjb	}
2356178479Sjb
2357178479Sjb	return (dt_print_aggs(&aggdata, 1, arg));
2358178479Sjb}
2359178479Sjb
2360178479Sjbint
2361178479Sjbdt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
2362178479Sjb    const char *option, const char *value)
2363178479Sjb{
2364178479Sjb	int len, rval;
2365178479Sjb	char *msg;
2366178479Sjb	const char *errstr;
2367178479Sjb	dtrace_setoptdata_t optdata;
2368178479Sjb
2369178479Sjb	bzero(&optdata, sizeof (optdata));
2370178479Sjb	(void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
2371178479Sjb
2372178479Sjb	if (dtrace_setopt(dtp, option, value) == 0) {
2373178479Sjb		(void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
2374178479Sjb		optdata.dtsda_probe = data;
2375178479Sjb		optdata.dtsda_option = option;
2376178479Sjb		optdata.dtsda_handle = dtp;
2377178479Sjb
2378178479Sjb		if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
2379178479Sjb			return (rval);
2380178479Sjb
2381178479Sjb		return (0);
2382178479Sjb	}
2383178479Sjb
2384178479Sjb	errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
2385178479Sjb	len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2386178479Sjb	msg = alloca(len);
2387178479Sjb
2388178479Sjb	(void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2389178479Sjb	    option, value, errstr);
2390178479Sjb
2391178479Sjb	if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2392178479Sjb		return (0);
2393178479Sjb
2394178479Sjb	return (rval);
2395178479Sjb}
2396178479Sjb
2397178479Sjbstatic int
2398250574Smarkjdt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu,
2399250574Smarkj    dtrace_bufdesc_t *buf, boolean_t just_one,
2400178479Sjb    dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2401178479Sjb{
2402178479Sjb	dtrace_epid_t id;
2403250574Smarkj	size_t offs;
2404178479Sjb	int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2405178479Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2406178479Sjb	int rval, i, n;
2407248690Spfg	uint64_t tracememsize = 0;
2408178479Sjb	dtrace_probedata_t data;
2409178479Sjb	uint64_t drops;
2410178479Sjb
2411178479Sjb	bzero(&data, sizeof (data));
2412178479Sjb	data.dtpda_handle = dtp;
2413178479Sjb	data.dtpda_cpu = cpu;
2414253725Spfg	data.dtpda_flow = dtp->dt_flow;
2415253725Spfg	data.dtpda_indent = dtp->dt_indent;
2416253725Spfg	data.dtpda_prefix = dtp->dt_prefix;
2417178479Sjb
2418250574Smarkj	for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) {
2419178479Sjb		dtrace_eprobedesc_t *epd;
2420178479Sjb
2421178479Sjb		/*
2422178479Sjb		 * We're guaranteed to have an ID.
2423178479Sjb		 */
2424178479Sjb		id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2425178479Sjb
2426178479Sjb		if (id == DTRACE_EPIDNONE) {
2427178479Sjb			/*
2428178479Sjb			 * This is filler to assure proper alignment of the
2429178479Sjb			 * next record; we simply ignore it.
2430178479Sjb			 */
2431178479Sjb			offs += sizeof (id);
2432178479Sjb			continue;
2433178479Sjb		}
2434178479Sjb
2435178479Sjb		if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2436178479Sjb		    &data.dtpda_pdesc)) != 0)
2437178479Sjb			return (rval);
2438178479Sjb
2439178479Sjb		epd = data.dtpda_edesc;
2440178479Sjb		data.dtpda_data = buf->dtbd_data + offs;
2441178479Sjb
2442178479Sjb		if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2443178479Sjb			rval = dt_handle(dtp, &data);
2444178479Sjb
2445178479Sjb			if (rval == DTRACE_CONSUME_NEXT)
2446178479Sjb				goto nextepid;
2447178479Sjb
2448178479Sjb			if (rval == DTRACE_CONSUME_ERROR)
2449178479Sjb				return (-1);
2450178479Sjb		}
2451178479Sjb
2452178479Sjb		if (flow)
2453250574Smarkj			(void) dt_flowindent(dtp, &data, dtp->dt_last_epid,
2454250574Smarkj			    buf, offs);
2455178479Sjb
2456178479Sjb		rval = (*efunc)(&data, arg);
2457178479Sjb
2458178479Sjb		if (flow) {
2459178479Sjb			if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2460178479Sjb				data.dtpda_indent += 2;
2461178479Sjb		}
2462178479Sjb
2463178479Sjb		if (rval == DTRACE_CONSUME_NEXT)
2464178479Sjb			goto nextepid;
2465178479Sjb
2466178479Sjb		if (rval == DTRACE_CONSUME_ABORT)
2467178479Sjb			return (dt_set_errno(dtp, EDT_DIRABORT));
2468178479Sjb
2469178479Sjb		if (rval != DTRACE_CONSUME_THIS)
2470178479Sjb			return (dt_set_errno(dtp, EDT_BADRVAL));
2471178479Sjb
2472178479Sjb		for (i = 0; i < epd->dtepd_nrecs; i++) {
2473250574Smarkj			caddr_t addr;
2474178479Sjb			dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2475178479Sjb			dtrace_actkind_t act = rec->dtrd_action;
2476178479Sjb
2477178479Sjb			data.dtpda_data = buf->dtbd_data + offs +
2478178479Sjb			    rec->dtrd_offset;
2479178479Sjb			addr = data.dtpda_data;
2480178479Sjb
2481178479Sjb			if (act == DTRACEACT_LIBACT) {
2482178479Sjb				uint64_t arg = rec->dtrd_arg;
2483178479Sjb				dtrace_aggvarid_t id;
2484178479Sjb
2485178479Sjb				switch (arg) {
2486178479Sjb				case DT_ACT_CLEAR:
2487178479Sjb					/* LINTED - alignment */
2488178479Sjb					id = *((dtrace_aggvarid_t *)addr);
2489178479Sjb					(void) dtrace_aggregate_walk(dtp,
2490178479Sjb					    dt_clear_agg, &id);
2491178479Sjb					continue;
2492178479Sjb
2493178479Sjb				case DT_ACT_DENORMALIZE:
2494178479Sjb					/* LINTED - alignment */
2495178479Sjb					id = *((dtrace_aggvarid_t *)addr);
2496178479Sjb					(void) dtrace_aggregate_walk(dtp,
2497178479Sjb					    dt_denormalize_agg, &id);
2498178479Sjb					continue;
2499178479Sjb
2500178479Sjb				case DT_ACT_FTRUNCATE:
2501178479Sjb					if (fp == NULL)
2502178479Sjb						continue;
2503178479Sjb
2504178479Sjb					(void) fflush(fp);
2505178479Sjb					(void) ftruncate(fileno(fp), 0);
2506178479Sjb					(void) fseeko(fp, 0, SEEK_SET);
2507178479Sjb					continue;
2508178479Sjb
2509178479Sjb				case DT_ACT_NORMALIZE:
2510178479Sjb					if (i == epd->dtepd_nrecs - 1)
2511178479Sjb						return (dt_set_errno(dtp,
2512178479Sjb						    EDT_BADNORMAL));
2513178479Sjb
2514178479Sjb					if (dt_normalize(dtp,
2515178479Sjb					    buf->dtbd_data + offs, rec) != 0)
2516178479Sjb						return (-1);
2517178479Sjb
2518178479Sjb					i++;
2519178479Sjb					continue;
2520178479Sjb
2521178479Sjb				case DT_ACT_SETOPT: {
2522178479Sjb					uint64_t *opts = dtp->dt_options;
2523178479Sjb					dtrace_recdesc_t *valrec;
2524178479Sjb					uint32_t valsize;
2525178479Sjb					caddr_t val;
2526178479Sjb					int rv;
2527178479Sjb
2528178479Sjb					if (i == epd->dtepd_nrecs - 1) {
2529178479Sjb						return (dt_set_errno(dtp,
2530178479Sjb						    EDT_BADSETOPT));
2531178479Sjb					}
2532178479Sjb
2533178479Sjb					valrec = &epd->dtepd_rec[++i];
2534178479Sjb					valsize = valrec->dtrd_size;
2535178479Sjb
2536178479Sjb					if (valrec->dtrd_action != act ||
2537178479Sjb					    valrec->dtrd_arg != arg) {
2538178479Sjb						return (dt_set_errno(dtp,
2539178479Sjb						    EDT_BADSETOPT));
2540178479Sjb					}
2541178479Sjb
2542178479Sjb					if (valsize > sizeof (uint64_t)) {
2543178479Sjb						val = buf->dtbd_data + offs +
2544178479Sjb						    valrec->dtrd_offset;
2545178479Sjb					} else {
2546178479Sjb						val = "1";
2547178479Sjb					}
2548178479Sjb
2549178479Sjb					rv = dt_setopt(dtp, &data, addr, val);
2550178479Sjb
2551178479Sjb					if (rv != 0)
2552178479Sjb						return (-1);
2553178479Sjb
2554178479Sjb					flow = (opts[DTRACEOPT_FLOWINDENT] !=
2555178479Sjb					    DTRACEOPT_UNSET);
2556178479Sjb					quiet = (opts[DTRACEOPT_QUIET] !=
2557178479Sjb					    DTRACEOPT_UNSET);
2558178479Sjb
2559178479Sjb					continue;
2560178479Sjb				}
2561178479Sjb
2562178479Sjb				case DT_ACT_TRUNC:
2563178479Sjb					if (i == epd->dtepd_nrecs - 1)
2564178479Sjb						return (dt_set_errno(dtp,
2565178479Sjb						    EDT_BADTRUNC));
2566178479Sjb
2567178479Sjb					if (dt_trunc(dtp,
2568178479Sjb					    buf->dtbd_data + offs, rec) != 0)
2569178479Sjb						return (-1);
2570178479Sjb
2571178479Sjb					i++;
2572178479Sjb					continue;
2573178479Sjb
2574178479Sjb				default:
2575178479Sjb					continue;
2576178479Sjb				}
2577178479Sjb			}
2578178479Sjb
2579248690Spfg			if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
2580248690Spfg			    rec->dtrd_size == sizeof (uint64_t)) {
2581248708Spfg			    	/* LINTED - alignment */
2582248690Spfg				tracememsize = *((unsigned long long *)addr);
2583248690Spfg				continue;
2584248690Spfg			}
2585248690Spfg
2586178479Sjb			rval = (*rfunc)(&data, rec, arg);
2587178479Sjb
2588178479Sjb			if (rval == DTRACE_CONSUME_NEXT)
2589178479Sjb				continue;
2590178479Sjb
2591178479Sjb			if (rval == DTRACE_CONSUME_ABORT)
2592178479Sjb				return (dt_set_errno(dtp, EDT_DIRABORT));
2593178479Sjb
2594178479Sjb			if (rval != DTRACE_CONSUME_THIS)
2595178479Sjb				return (dt_set_errno(dtp, EDT_BADRVAL));
2596178479Sjb
2597178479Sjb			if (act == DTRACEACT_STACK) {
2598178479Sjb				int depth = rec->dtrd_arg;
2599178479Sjb
2600178479Sjb				if (dt_print_stack(dtp, fp, NULL, addr, depth,
2601178479Sjb				    rec->dtrd_size / depth) < 0)
2602178479Sjb					return (-1);
2603178479Sjb				goto nextrec;
2604178479Sjb			}
2605178479Sjb
2606178479Sjb			if (act == DTRACEACT_USTACK ||
2607178479Sjb			    act == DTRACEACT_JSTACK) {
2608178479Sjb				if (dt_print_ustack(dtp, fp, NULL,
2609178479Sjb				    addr, rec->dtrd_arg) < 0)
2610178479Sjb					return (-1);
2611178479Sjb				goto nextrec;
2612178479Sjb			}
2613178479Sjb
2614178479Sjb			if (act == DTRACEACT_SYM) {
2615178479Sjb				if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2616178479Sjb					return (-1);
2617178479Sjb				goto nextrec;
2618178479Sjb			}
2619178479Sjb
2620178479Sjb			if (act == DTRACEACT_MOD) {
2621178479Sjb				if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2622178479Sjb					return (-1);
2623178479Sjb				goto nextrec;
2624178479Sjb			}
2625178479Sjb
2626178479Sjb			if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2627178479Sjb				if (dt_print_usym(dtp, fp, addr, act) < 0)
2628178479Sjb					return (-1);
2629178479Sjb				goto nextrec;
2630178479Sjb			}
2631178479Sjb
2632178479Sjb			if (act == DTRACEACT_UMOD) {
2633178479Sjb				if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2634178479Sjb					return (-1);
2635178479Sjb				goto nextrec;
2636178479Sjb			}
2637178479Sjb
2638178576Sjb			if (act == DTRACEACT_PRINTM) {
2639178576Sjb				if (dt_print_memory(dtp, fp, addr) < 0)
2640178576Sjb					return (-1);
2641178576Sjb				goto nextrec;
2642178576Sjb			}
2643178576Sjb
2644178576Sjb			if (act == DTRACEACT_PRINTT) {
2645178576Sjb				if (dt_print_type(dtp, fp, addr) < 0)
2646178576Sjb					return (-1);
2647178576Sjb				goto nextrec;
2648178576Sjb			}
2649178576Sjb
2650178479Sjb			if (DTRACEACT_ISPRINTFLIKE(act)) {
2651178479Sjb				void *fmtdata;
2652178479Sjb				int (*func)(dtrace_hdl_t *, FILE *, void *,
2653178479Sjb				    const dtrace_probedata_t *,
2654178479Sjb				    const dtrace_recdesc_t *, uint_t,
2655178479Sjb				    const void *buf, size_t);
2656178479Sjb
2657178479Sjb				if ((fmtdata = dt_format_lookup(dtp,
2658178479Sjb				    rec->dtrd_format)) == NULL)
2659178479Sjb					goto nofmt;
2660178479Sjb
2661178479Sjb				switch (act) {
2662178479Sjb				case DTRACEACT_PRINTF:
2663178479Sjb					func = dtrace_fprintf;
2664178479Sjb					break;
2665178479Sjb				case DTRACEACT_PRINTA:
2666178479Sjb					func = dtrace_fprinta;
2667178479Sjb					break;
2668178479Sjb				case DTRACEACT_SYSTEM:
2669178479Sjb					func = dtrace_system;
2670178479Sjb					break;
2671178479Sjb				case DTRACEACT_FREOPEN:
2672178479Sjb					func = dtrace_freopen;
2673178479Sjb					break;
2674178479Sjb				}
2675178479Sjb
2676178479Sjb				n = (*func)(dtp, fp, fmtdata, &data,
2677178479Sjb				    rec, epd->dtepd_nrecs - i,
2678178479Sjb				    (uchar_t *)buf->dtbd_data + offs,
2679178479Sjb				    buf->dtbd_size - offs);
2680178479Sjb
2681178479Sjb				if (n < 0)
2682178479Sjb					return (-1); /* errno is set for us */
2683178479Sjb
2684178479Sjb				if (n > 0)
2685178479Sjb					i += n - 1;
2686178479Sjb				goto nextrec;
2687178479Sjb			}
2688178479Sjb
2689248708Spfg			/*
2690248708Spfg			 * If this is a DIF expression, and the record has a
2691248708Spfg			 * format set, this indicates we have a CTF type name
2692248708Spfg			 * associated with the data and we should try to print
2693248708Spfg			 * it out by type.
2694248708Spfg			 */
2695248708Spfg			if (act == DTRACEACT_DIFEXPR) {
2696248708Spfg				const char *strdata = dt_strdata_lookup(dtp,
2697248708Spfg				    rec->dtrd_format);
2698248708Spfg				if (strdata != NULL) {
2699248708Spfg					n = dtrace_print(dtp, fp, strdata,
2700248708Spfg					    addr, rec->dtrd_size);
2701248708Spfg
2702248708Spfg					/*
2703248708Spfg					 * dtrace_print() will return -1 on
2704248708Spfg					 * error, or return the number of bytes
2705248708Spfg					 * consumed.  It will return 0 if the
2706248708Spfg					 * type couldn't be determined, and we
2707248708Spfg					 * should fall through to the normal
2708248708Spfg					 * trace method.
2709248708Spfg					 */
2710248708Spfg					if (n < 0)
2711248708Spfg						return (-1);
2712248708Spfg
2713248708Spfg					if (n > 0)
2714248708Spfg						goto nextrec;
2715248708Spfg				}
2716248708Spfg			}
2717248708Spfg
2718178479Sjbnofmt:
2719178479Sjb			if (act == DTRACEACT_PRINTA) {
2720178479Sjb				dt_print_aggdata_t pd;
2721178479Sjb				dtrace_aggvarid_t *aggvars;
2722178479Sjb				int j, naggvars = 0;
2723178479Sjb				size_t size = ((epd->dtepd_nrecs - i) *
2724178479Sjb				    sizeof (dtrace_aggvarid_t));
2725178479Sjb
2726178479Sjb				if ((aggvars = dt_alloc(dtp, size)) == NULL)
2727178479Sjb					return (-1);
2728178479Sjb
2729178479Sjb				/*
2730178479Sjb				 * This might be a printa() with multiple
2731178479Sjb				 * aggregation variables.  We need to scan
2732178479Sjb				 * forward through the records until we find
2733178479Sjb				 * a record from a different statement.
2734178479Sjb				 */
2735178479Sjb				for (j = i; j < epd->dtepd_nrecs; j++) {
2736178479Sjb					dtrace_recdesc_t *nrec;
2737178479Sjb					caddr_t naddr;
2738178479Sjb
2739178479Sjb					nrec = &epd->dtepd_rec[j];
2740178479Sjb
2741178479Sjb					if (nrec->dtrd_uarg != rec->dtrd_uarg)
2742178479Sjb						break;
2743178479Sjb
2744178479Sjb					if (nrec->dtrd_action != act) {
2745178479Sjb						return (dt_set_errno(dtp,
2746178479Sjb						    EDT_BADAGG));
2747178479Sjb					}
2748178479Sjb
2749178479Sjb					naddr = buf->dtbd_data + offs +
2750178479Sjb					    nrec->dtrd_offset;
2751178479Sjb
2752178479Sjb					aggvars[naggvars++] =
2753178479Sjb					    /* LINTED - alignment */
2754178479Sjb					    *((dtrace_aggvarid_t *)naddr);
2755178479Sjb				}
2756178479Sjb
2757178479Sjb				i = j - 1;
2758178479Sjb				bzero(&pd, sizeof (pd));
2759178479Sjb				pd.dtpa_dtp = dtp;
2760178479Sjb				pd.dtpa_fp = fp;
2761178479Sjb
2762178479Sjb				assert(naggvars >= 1);
2763178479Sjb
2764178479Sjb				if (naggvars == 1) {
2765178479Sjb					pd.dtpa_id = aggvars[0];
2766178479Sjb					dt_free(dtp, aggvars);
2767178479Sjb
2768178479Sjb					if (dt_printf(dtp, fp, "\n") < 0 ||
2769178479Sjb					    dtrace_aggregate_walk_sorted(dtp,
2770178479Sjb					    dt_print_agg, &pd) < 0)
2771178479Sjb						return (-1);
2772178479Sjb					goto nextrec;
2773178479Sjb				}
2774178479Sjb
2775178479Sjb				if (dt_printf(dtp, fp, "\n") < 0 ||
2776178479Sjb				    dtrace_aggregate_walk_joined(dtp, aggvars,
2777178479Sjb				    naggvars, dt_print_aggs, &pd) < 0) {
2778178479Sjb					dt_free(dtp, aggvars);
2779178479Sjb					return (-1);
2780178479Sjb				}
2781178479Sjb
2782178479Sjb				dt_free(dtp, aggvars);
2783178479Sjb				goto nextrec;
2784178479Sjb			}
2785178479Sjb
2786248690Spfg			if (act == DTRACEACT_TRACEMEM) {
2787248690Spfg				if (tracememsize == 0 ||
2788248690Spfg				    tracememsize > rec->dtrd_size) {
2789248690Spfg					tracememsize = rec->dtrd_size;
2790248690Spfg				}
2791248690Spfg
2792248690Spfg				n = dt_print_bytes(dtp, fp, addr,
2793267942Srpaulo				    tracememsize, -33, quiet, 1);
2794248690Spfg
2795248690Spfg				tracememsize = 0;
2796248690Spfg
2797248690Spfg				if (n < 0)
2798248690Spfg					return (-1);
2799248690Spfg
2800248690Spfg				goto nextrec;
2801248690Spfg			}
2802248690Spfg
2803178479Sjb			switch (rec->dtrd_size) {
2804178479Sjb			case sizeof (uint64_t):
2805178479Sjb				n = dt_printf(dtp, fp,
2806178479Sjb				    quiet ? "%lld" : " %16lld",
2807178479Sjb				    /* LINTED - alignment */
2808178479Sjb				    *((unsigned long long *)addr));
2809178479Sjb				break;
2810178479Sjb			case sizeof (uint32_t):
2811178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2812178479Sjb				    /* LINTED - alignment */
2813178479Sjb				    *((uint32_t *)addr));
2814178479Sjb				break;
2815178479Sjb			case sizeof (uint16_t):
2816178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2817178479Sjb				    /* LINTED - alignment */
2818178479Sjb				    *((uint16_t *)addr));
2819178479Sjb				break;
2820178479Sjb			case sizeof (uint8_t):
2821178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2822178479Sjb				    *((uint8_t *)addr));
2823178479Sjb				break;
2824178479Sjb			default:
2825178479Sjb				n = dt_print_bytes(dtp, fp, addr,
2826267942Srpaulo				    rec->dtrd_size, -33, quiet, 0);
2827178479Sjb				break;
2828178479Sjb			}
2829178479Sjb
2830178479Sjb			if (n < 0)
2831178479Sjb				return (-1); /* errno is set for us */
2832178479Sjb
2833178479Sjbnextrec:
2834178479Sjb			if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2835178479Sjb				return (-1); /* errno is set for us */
2836178479Sjb		}
2837178479Sjb
2838178479Sjb		/*
2839178479Sjb		 * Call the record callback with a NULL record to indicate
2840178479Sjb		 * that we're done processing this EPID.
2841178479Sjb		 */
2842178479Sjb		rval = (*rfunc)(&data, NULL, arg);
2843178479Sjbnextepid:
2844178479Sjb		offs += epd->dtepd_size;
2845250574Smarkj		dtp->dt_last_epid = id;
2846250574Smarkj		if (just_one) {
2847250574Smarkj			buf->dtbd_oldest = offs;
2848250574Smarkj			break;
2849250574Smarkj		}
2850178479Sjb	}
2851178479Sjb
2852250574Smarkj	dtp->dt_flow = data.dtpda_flow;
2853250574Smarkj	dtp->dt_indent = data.dtpda_indent;
2854250574Smarkj	dtp->dt_prefix = data.dtpda_prefix;
2855178479Sjb
2856178479Sjb	if ((drops = buf->dtbd_drops) == 0)
2857178479Sjb		return (0);
2858178479Sjb
2859178479Sjb	/*
2860178479Sjb	 * Explicitly zero the drops to prevent us from processing them again.
2861178479Sjb	 */
2862178479Sjb	buf->dtbd_drops = 0;
2863178479Sjb
2864178479Sjb	return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2865178479Sjb}
2866178479Sjb
2867250574Smarkj/*
2868250574Smarkj * Reduce memory usage by shrinking the buffer if it's no more than half full.
2869250574Smarkj * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2870250574Smarkj * only 4-byte aligned.
2871250574Smarkj */
2872250574Smarkjstatic void
2873250574Smarkjdt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize)
2874250574Smarkj{
2875250574Smarkj	uint64_t used = buf->dtbd_size - buf->dtbd_oldest;
2876250574Smarkj	if (used < cursize / 2) {
2877250574Smarkj		int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2878250574Smarkj		char *newdata = dt_alloc(dtp, used + misalign);
2879250574Smarkj		if (newdata == NULL)
2880250574Smarkj			return;
2881250574Smarkj		bzero(newdata, misalign);
2882250574Smarkj		bcopy(buf->dtbd_data + buf->dtbd_oldest,
2883250574Smarkj		    newdata + misalign, used);
2884250574Smarkj		dt_free(dtp, buf->dtbd_data);
2885250574Smarkj		buf->dtbd_oldest = misalign;
2886250574Smarkj		buf->dtbd_size = used + misalign;
2887250574Smarkj		buf->dtbd_data = newdata;
2888250574Smarkj	}
2889250574Smarkj}
2890250574Smarkj
2891250574Smarkj/*
2892250574Smarkj * If the ring buffer has wrapped, the data is not in order.  Rearrange it
2893250574Smarkj * so that it is.  Note, we need to preserve the alignment of the data at
2894250574Smarkj * dtbd_oldest, which is only 4-byte aligned.
2895250574Smarkj */
2896250574Smarkjstatic int
2897250574Smarkjdt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2898250574Smarkj{
2899250574Smarkj	int misalign;
2900250574Smarkj	char *newdata, *ndp;
2901250574Smarkj
2902250574Smarkj	if (buf->dtbd_oldest == 0)
2903250574Smarkj		return (0);
2904250574Smarkj
2905250574Smarkj	misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2906250574Smarkj	newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign);
2907250574Smarkj
2908250574Smarkj	if (newdata == NULL)
2909250574Smarkj		return (-1);
2910250574Smarkj
2911250574Smarkj	assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1)));
2912250574Smarkj
2913250574Smarkj	bzero(ndp, misalign);
2914250574Smarkj	ndp += misalign;
2915250574Smarkj
2916250574Smarkj	bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp,
2917250574Smarkj	    buf->dtbd_size - buf->dtbd_oldest);
2918250574Smarkj	ndp += buf->dtbd_size - buf->dtbd_oldest;
2919250574Smarkj
2920250574Smarkj	bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest);
2921250574Smarkj
2922250574Smarkj	dt_free(dtp, buf->dtbd_data);
2923250574Smarkj	buf->dtbd_oldest = 0;
2924250574Smarkj	buf->dtbd_data = newdata;
2925250574Smarkj	buf->dtbd_size += misalign;
2926250574Smarkj
2927250574Smarkj	return (0);
2928250574Smarkj}
2929250574Smarkj
2930250574Smarkjstatic void
2931250574Smarkjdt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2932250574Smarkj{
2933250574Smarkj	dt_free(dtp, buf->dtbd_data);
2934250574Smarkj	dt_free(dtp, buf);
2935250574Smarkj}
2936250574Smarkj
2937250574Smarkj/*
2938250574Smarkj * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2939250574Smarkj * data, or NULL if there is no data for this CPU.
2940250574Smarkj * Returns -1 on failure and sets dt_errno.
2941250574Smarkj */
2942250574Smarkjstatic int
2943250574Smarkjdt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp)
2944250574Smarkj{
2945250574Smarkj	dtrace_optval_t size;
2946250574Smarkj	dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf));
2947269524Smarkj	int error, rval;
2948250574Smarkj
2949250574Smarkj	if (buf == NULL)
2950250574Smarkj		return (-1);
2951250574Smarkj
2952250574Smarkj	(void) dtrace_getopt(dtp, "bufsize", &size);
2953250574Smarkj	buf->dtbd_data = dt_alloc(dtp, size);
2954250574Smarkj	if (buf->dtbd_data == NULL) {
2955250574Smarkj		dt_free(dtp, buf);
2956250574Smarkj		return (-1);
2957250574Smarkj	}
2958250574Smarkj	buf->dtbd_size = size;
2959250574Smarkj	buf->dtbd_cpu = cpu;
2960250574Smarkj
2961250574Smarkj#if defined(sun)
2962250574Smarkj	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2963250574Smarkj#else
2964250574Smarkj	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2965250574Smarkj#endif
2966250574Smarkj		/*
2967250574Smarkj		 * If we failed with ENOENT, it may be because the
2968250574Smarkj		 * CPU was unconfigured -- this is okay.  Any other
2969250574Smarkj		 * error, however, is unexpected.
2970250574Smarkj		 */
2971250574Smarkj		if (errno == ENOENT) {
2972250574Smarkj			*bufp = NULL;
2973269524Smarkj			rval = 0;
2974269524Smarkj		} else
2975269524Smarkj			rval = dt_set_errno(dtp, errno);
2976250574Smarkj
2977269524Smarkj		dt_put_buf(dtp, buf);
2978269524Smarkj		return (rval);
2979250574Smarkj	}
2980250574Smarkj
2981250574Smarkj	error = dt_unring_buf(dtp, buf);
2982250574Smarkj	if (error != 0) {
2983250574Smarkj		dt_put_buf(dtp, buf);
2984250574Smarkj		return (error);
2985250574Smarkj	}
2986250574Smarkj	dt_realloc_buf(dtp, buf, size);
2987250574Smarkj
2988250574Smarkj	*bufp = buf;
2989250574Smarkj	return (0);
2990250574Smarkj}
2991250574Smarkj
2992178479Sjbtypedef struct dt_begin {
2993178479Sjb	dtrace_consume_probe_f *dtbgn_probefunc;
2994178479Sjb	dtrace_consume_rec_f *dtbgn_recfunc;
2995178479Sjb	void *dtbgn_arg;
2996178479Sjb	dtrace_handle_err_f *dtbgn_errhdlr;
2997178479Sjb	void *dtbgn_errarg;
2998178479Sjb	int dtbgn_beginonly;
2999178479Sjb} dt_begin_t;
3000178479Sjb
3001178479Sjbstatic int
3002178479Sjbdt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
3003178479Sjb{
3004253725Spfg	dt_begin_t *begin = arg;
3005178479Sjb	dtrace_probedesc_t *pd = data->dtpda_pdesc;
3006178479Sjb
3007178479Sjb	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3008178479Sjb	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3009178479Sjb
3010178479Sjb	if (begin->dtbgn_beginonly) {
3011178479Sjb		if (!(r1 && r2))
3012178479Sjb			return (DTRACE_CONSUME_NEXT);
3013178479Sjb	} else {
3014178479Sjb		if (r1 && r2)
3015178479Sjb			return (DTRACE_CONSUME_NEXT);
3016178479Sjb	}
3017178479Sjb
3018178479Sjb	/*
3019178479Sjb	 * We have a record that we're interested in.  Now call the underlying
3020178479Sjb	 * probe function...
3021178479Sjb	 */
3022178479Sjb	return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
3023178479Sjb}
3024178479Sjb
3025178479Sjbstatic int
3026178479Sjbdt_consume_begin_record(const dtrace_probedata_t *data,
3027178479Sjb    const dtrace_recdesc_t *rec, void *arg)
3028178479Sjb{
3029253725Spfg	dt_begin_t *begin = arg;
3030178479Sjb
3031178479Sjb	return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
3032178479Sjb}
3033178479Sjb
3034178479Sjbstatic int
3035178479Sjbdt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
3036178479Sjb{
3037178479Sjb	dt_begin_t *begin = (dt_begin_t *)arg;
3038178479Sjb	dtrace_probedesc_t *pd = data->dteda_pdesc;
3039178479Sjb
3040178479Sjb	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
3041178479Sjb	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
3042178479Sjb
3043178479Sjb	if (begin->dtbgn_beginonly) {
3044178479Sjb		if (!(r1 && r2))
3045178479Sjb			return (DTRACE_HANDLE_OK);
3046178479Sjb	} else {
3047178479Sjb		if (r1 && r2)
3048178479Sjb			return (DTRACE_HANDLE_OK);
3049178479Sjb	}
3050178479Sjb
3051178479Sjb	return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
3052178479Sjb}
3053178479Sjb
3054178479Sjbstatic int
3055250574Smarkjdt_consume_begin(dtrace_hdl_t *dtp, FILE *fp,
3056178479Sjb    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3057178479Sjb{
3058178479Sjb	/*
3059178479Sjb	 * There's this idea that the BEGIN probe should be processed before
3060178479Sjb	 * everything else, and that the END probe should be processed after
3061178479Sjb	 * anything else.  In the common case, this is pretty easy to deal
3062178479Sjb	 * with.  However, a situation may arise where the BEGIN enabling and
3063178479Sjb	 * END enabling are on the same CPU, and some enabling in the middle
3064178479Sjb	 * occurred on a different CPU.  To deal with this (blech!) we need to
3065178479Sjb	 * consume the BEGIN buffer up until the end of the BEGIN probe, and
3066178479Sjb	 * then set it aside.  We will then process every other CPU, and then
3067178479Sjb	 * we'll return to the BEGIN CPU and process the rest of the data
3068178479Sjb	 * (which will inevitably include the END probe, if any).  Making this
3069178479Sjb	 * even more complicated (!) is the library's ERROR enabling.  Because
3070178479Sjb	 * this enabling is processed before we even get into the consume call
3071178479Sjb	 * back, any ERROR firing would result in the library's ERROR enabling
3072178479Sjb	 * being processed twice -- once in our first pass (for BEGIN probes),
3073178479Sjb	 * and again in our second pass (for everything but BEGIN probes).  To
3074178479Sjb	 * deal with this, we interpose on the ERROR handler to assure that we
3075178479Sjb	 * only process ERROR enablings induced by BEGIN enablings in the
3076178479Sjb	 * first pass, and that we only process ERROR enablings _not_ induced
3077178479Sjb	 * by BEGIN enablings in the second pass.
3078178479Sjb	 */
3079250574Smarkj
3080178479Sjb	dt_begin_t begin;
3081178479Sjb	processorid_t cpu = dtp->dt_beganon;
3082178479Sjb	int rval, i;
3083178479Sjb	static int max_ncpus;
3084250574Smarkj	dtrace_bufdesc_t *buf;
3085178479Sjb
3086178479Sjb	dtp->dt_beganon = -1;
3087178479Sjb
3088250574Smarkj	if (dt_get_buf(dtp, cpu, &buf) != 0)
3089250574Smarkj		return (-1);
3090250574Smarkj	if (buf == NULL)
3091250574Smarkj		return (0);
3092178479Sjb
3093178479Sjb	if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
3094178479Sjb		/*
3095178479Sjb		 * This is the simple case.  We're either not stopped, or if
3096178479Sjb		 * we are, we actually processed any END probes on another
3097178479Sjb		 * CPU.  We can simply consume this buffer and return.
3098178479Sjb		 */
3099250574Smarkj		rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3100250574Smarkj		    pf, rf, arg);
3101250574Smarkj		dt_put_buf(dtp, buf);
3102250574Smarkj		return (rval);
3103178479Sjb	}
3104178479Sjb
3105178479Sjb	begin.dtbgn_probefunc = pf;
3106178479Sjb	begin.dtbgn_recfunc = rf;
3107178479Sjb	begin.dtbgn_arg = arg;
3108178479Sjb	begin.dtbgn_beginonly = 1;
3109178479Sjb
3110178479Sjb	/*
3111178479Sjb	 * We need to interpose on the ERROR handler to be sure that we
3112178479Sjb	 * only process ERRORs induced by BEGIN.
3113178479Sjb	 */
3114178479Sjb	begin.dtbgn_errhdlr = dtp->dt_errhdlr;
3115178479Sjb	begin.dtbgn_errarg = dtp->dt_errarg;
3116178479Sjb	dtp->dt_errhdlr = dt_consume_begin_error;
3117178479Sjb	dtp->dt_errarg = &begin;
3118178479Sjb
3119250574Smarkj	rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3120250574Smarkj	    dt_consume_begin_probe, dt_consume_begin_record, &begin);
3121178479Sjb
3122178479Sjb	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3123178479Sjb	dtp->dt_errarg = begin.dtbgn_errarg;
3124178479Sjb
3125250574Smarkj	if (rval != 0) {
3126250574Smarkj		dt_put_buf(dtp, buf);
3127178479Sjb		return (rval);
3128250574Smarkj	}
3129178479Sjb
3130178479Sjb	if (max_ncpus == 0)
3131178479Sjb		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3132178479Sjb
3133178479Sjb	for (i = 0; i < max_ncpus; i++) {
3134250574Smarkj		dtrace_bufdesc_t *nbuf;
3135178479Sjb		if (i == cpu)
3136178479Sjb			continue;
3137178479Sjb
3138250574Smarkj		if (dt_get_buf(dtp, i, &nbuf) != 0) {
3139250574Smarkj			dt_put_buf(dtp, buf);
3140250574Smarkj			return (-1);
3141178479Sjb		}
3142250574Smarkj		if (nbuf == NULL)
3143250574Smarkj			continue;
3144178479Sjb
3145250574Smarkj		rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE,
3146250574Smarkj		    pf, rf, arg);
3147250574Smarkj		dt_put_buf(dtp, nbuf);
3148250574Smarkj		if (rval != 0) {
3149250574Smarkj			dt_put_buf(dtp, buf);
3150178479Sjb			return (rval);
3151178479Sjb		}
3152178479Sjb	}
3153178479Sjb
3154178479Sjb	/*
3155178479Sjb	 * Okay -- we're done with the other buffers.  Now we want to
3156178479Sjb	 * reconsume the first buffer -- but this time we're looking for
3157178479Sjb	 * everything _but_ BEGIN.  And of course, in order to only consume
3158178479Sjb	 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
3159178479Sjb	 * ERROR interposition function...
3160178479Sjb	 */
3161178479Sjb	begin.dtbgn_beginonly = 0;
3162178479Sjb
3163178479Sjb	assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
3164178479Sjb	assert(begin.dtbgn_errarg == dtp->dt_errarg);
3165178479Sjb	dtp->dt_errhdlr = dt_consume_begin_error;
3166178479Sjb	dtp->dt_errarg = &begin;
3167178479Sjb
3168250574Smarkj	rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
3169250574Smarkj	    dt_consume_begin_probe, dt_consume_begin_record, &begin);
3170178479Sjb
3171178479Sjb	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
3172178479Sjb	dtp->dt_errarg = begin.dtbgn_errarg;
3173178479Sjb
3174178479Sjb	return (rval);
3175178479Sjb}
3176178479Sjb
3177250574Smarkj/* ARGSUSED */
3178250574Smarkjstatic uint64_t
3179250574Smarkjdt_buf_oldest(void *elem, void *arg)
3180250574Smarkj{
3181250574Smarkj	dtrace_bufdesc_t *buf = elem;
3182250574Smarkj	size_t offs = buf->dtbd_oldest;
3183250574Smarkj
3184250574Smarkj	while (offs < buf->dtbd_size) {
3185250574Smarkj		dtrace_rechdr_t *dtrh =
3186250574Smarkj		    /* LINTED - alignment */
3187250574Smarkj		    (dtrace_rechdr_t *)(buf->dtbd_data + offs);
3188250574Smarkj		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
3189250574Smarkj			offs += sizeof (dtrace_epid_t);
3190250574Smarkj		} else {
3191250574Smarkj			return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh));
3192250574Smarkj		}
3193250574Smarkj	}
3194250574Smarkj
3195250574Smarkj	/* There are no records left; use the time the buffer was retrieved. */
3196250574Smarkj	return (buf->dtbd_timestamp);
3197250574Smarkj}
3198250574Smarkj
3199178479Sjbint
3200178479Sjbdtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
3201178479Sjb    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
3202178479Sjb{
3203178479Sjb	dtrace_optval_t size;
3204178479Sjb	static int max_ncpus;
3205178479Sjb	int i, rval;
3206178479Sjb	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
3207178479Sjb	hrtime_t now = gethrtime();
3208178479Sjb
3209178479Sjb	if (dtp->dt_lastswitch != 0) {
3210178479Sjb		if (now - dtp->dt_lastswitch < interval)
3211178479Sjb			return (0);
3212178479Sjb
3213178479Sjb		dtp->dt_lastswitch += interval;
3214178479Sjb	} else {
3215178479Sjb		dtp->dt_lastswitch = now;
3216178479Sjb	}
3217178479Sjb
3218178479Sjb	if (!dtp->dt_active)
3219178479Sjb		return (dt_set_errno(dtp, EINVAL));
3220178479Sjb
3221178479Sjb	if (max_ncpus == 0)
3222178479Sjb		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
3223178479Sjb
3224178479Sjb	if (pf == NULL)
3225178479Sjb		pf = (dtrace_consume_probe_f *)dt_nullprobe;
3226178479Sjb
3227178479Sjb	if (rf == NULL)
3228178479Sjb		rf = (dtrace_consume_rec_f *)dt_nullrec;
3229178479Sjb
3230250574Smarkj	if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) {
3231250574Smarkj		/*
3232250574Smarkj		 * The output will not be in the order it was traced.  Rather,
3233250574Smarkj		 * we will consume all of the data from each CPU's buffer in
3234250574Smarkj		 * turn.  We apply special handling for the records from BEGIN
3235250574Smarkj		 * and END probes so that they are consumed first and last,
3236250574Smarkj		 * respectively.
3237250574Smarkj		 *
3238250574Smarkj		 * If we have just begun, we want to first process the CPU that
3239250574Smarkj		 * executed the BEGIN probe (if any).
3240250574Smarkj		 */
3241250574Smarkj		if (dtp->dt_active && dtp->dt_beganon != -1 &&
3242250574Smarkj		    (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0)
3243249573Spfg			return (rval);
3244249573Spfg
3245250574Smarkj		for (i = 0; i < max_ncpus; i++) {
3246250574Smarkj			dtrace_bufdesc_t *buf;
3247249573Spfg
3248178479Sjb			/*
3249250574Smarkj			 * If we have stopped, we want to process the CPU on
3250250574Smarkj			 * which the END probe was processed only _after_ we
3251250574Smarkj			 * have processed everything else.
3252178479Sjb			 */
3253250574Smarkj			if (dtp->dt_stopped && (i == dtp->dt_endedon))
3254178479Sjb				continue;
3255178479Sjb
3256250574Smarkj			if (dt_get_buf(dtp, i, &buf) != 0)
3257250574Smarkj				return (-1);
3258250574Smarkj			if (buf == NULL)
3259250574Smarkj				continue;
3260250574Smarkj
3261250574Smarkj			dtp->dt_flow = 0;
3262250574Smarkj			dtp->dt_indent = 0;
3263250574Smarkj			dtp->dt_prefix = NULL;
3264250574Smarkj			rval = dt_consume_cpu(dtp, fp, i,
3265250574Smarkj			    buf, B_FALSE, pf, rf, arg);
3266250574Smarkj			dt_put_buf(dtp, buf);
3267250574Smarkj			if (rval != 0)
3268250574Smarkj				return (rval);
3269178479Sjb		}
3270250574Smarkj		if (dtp->dt_stopped) {
3271250574Smarkj			dtrace_bufdesc_t *buf;
3272178479Sjb
3273250574Smarkj			if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0)
3274250574Smarkj				return (-1);
3275250574Smarkj			if (buf == NULL)
3276250574Smarkj				return (0);
3277250574Smarkj
3278250574Smarkj			rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon,
3279250574Smarkj			    buf, B_FALSE, pf, rf, arg);
3280250574Smarkj			dt_put_buf(dtp, buf);
3281178479Sjb			return (rval);
3282250574Smarkj		}
3283250574Smarkj	} else {
3284250574Smarkj		/*
3285250574Smarkj		 * The output will be in the order it was traced (or for
3286250574Smarkj		 * speculations, when it was committed).  We retrieve a buffer
3287250574Smarkj		 * from each CPU and put it into a priority queue, which sorts
3288250574Smarkj		 * based on the first entry in the buffer.  This is sufficient
3289250574Smarkj		 * because entries within a buffer are already sorted.
3290250574Smarkj		 *
3291250574Smarkj		 * We then consume records one at a time, always consuming the
3292250574Smarkj		 * oldest record, as determined by the priority queue.  When
3293250574Smarkj		 * we reach the end of the time covered by these buffers,
3294250574Smarkj		 * we need to stop and retrieve more records on the next pass.
3295250574Smarkj		 * The kernel tells us the time covered by each buffer, in
3296250574Smarkj		 * dtbd_timestamp.  The first buffer's timestamp tells us the
3297250574Smarkj		 * time covered by all buffers, as subsequently retrieved
3298250574Smarkj		 * buffers will cover to a more recent time.
3299250574Smarkj		 */
3300178479Sjb
3301250574Smarkj		uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t));
3302250574Smarkj		uint64_t first_timestamp = 0;
3303250574Smarkj		uint_t cookie = 0;
3304250574Smarkj		dtrace_bufdesc_t *buf;
3305178479Sjb
3306250574Smarkj		bzero(drops, max_ncpus * sizeof (uint64_t));
3307178479Sjb
3308250574Smarkj		if (dtp->dt_bufq == NULL) {
3309250574Smarkj			dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2,
3310250574Smarkj			    dt_buf_oldest, NULL);
3311250574Smarkj			if (dtp->dt_bufq == NULL) /* ENOMEM */
3312250574Smarkj				return (-1);
3313250574Smarkj		}
3314250574Smarkj
3315250574Smarkj		/* Retrieve data from each CPU. */
3316250574Smarkj		(void) dtrace_getopt(dtp, "bufsize", &size);
3317250574Smarkj		for (i = 0; i < max_ncpus; i++) {
3318250574Smarkj			dtrace_bufdesc_t *buf;
3319250574Smarkj
3320250574Smarkj			if (dt_get_buf(dtp, i, &buf) != 0)
3321250574Smarkj				return (-1);
3322250574Smarkj			if (buf != NULL) {
3323250574Smarkj				if (first_timestamp == 0)
3324250574Smarkj					first_timestamp = buf->dtbd_timestamp;
3325250574Smarkj				assert(buf->dtbd_timestamp >= first_timestamp);
3326250574Smarkj
3327250574Smarkj				dt_pq_insert(dtp->dt_bufq, buf);
3328250574Smarkj				drops[i] = buf->dtbd_drops;
3329250574Smarkj				buf->dtbd_drops = 0;
3330250574Smarkj			}
3331250574Smarkj		}
3332250574Smarkj
3333250574Smarkj		/* Consume records. */
3334250574Smarkj		for (;;) {
3335250574Smarkj			dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq);
3336250574Smarkj			uint64_t timestamp;
3337250574Smarkj
3338250574Smarkj			if (buf == NULL)
3339250574Smarkj				break;
3340250574Smarkj
3341250574Smarkj			timestamp = dt_buf_oldest(buf, dtp);
3342250574Smarkj			assert(timestamp >= dtp->dt_last_timestamp);
3343250574Smarkj			dtp->dt_last_timestamp = timestamp;
3344250574Smarkj
3345250574Smarkj			if (timestamp == buf->dtbd_timestamp) {
3346250574Smarkj				/*
3347250574Smarkj				 * We've reached the end of the time covered
3348250574Smarkj				 * by this buffer.  If this is the oldest
3349250574Smarkj				 * buffer, we must do another pass
3350250574Smarkj				 * to retrieve more data.
3351250574Smarkj				 */
3352250574Smarkj				dt_put_buf(dtp, buf);
3353250574Smarkj				if (timestamp == first_timestamp &&
3354250574Smarkj				    !dtp->dt_stopped)
3355250574Smarkj					break;
3356250574Smarkj				continue;
3357250574Smarkj			}
3358250574Smarkj
3359250574Smarkj			if ((rval = dt_consume_cpu(dtp, fp,
3360250574Smarkj			    buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0)
3361250574Smarkj				return (rval);
3362250574Smarkj			dt_pq_insert(dtp->dt_bufq, buf);
3363250574Smarkj		}
3364250574Smarkj
3365250574Smarkj		/* Consume drops. */
3366250574Smarkj		for (i = 0; i < max_ncpus; i++) {
3367250574Smarkj			if (drops[i] != 0) {
3368250574Smarkj				int error = dt_handle_cpudrop(dtp, i,
3369250574Smarkj				    DTRACEDROP_PRINCIPAL, drops[i]);
3370250574Smarkj				if (error != 0)
3371250574Smarkj					return (error);
3372250574Smarkj			}
3373250574Smarkj		}
3374250574Smarkj
3375178479Sjb		/*
3376250574Smarkj		 * Reduce memory usage by re-allocating smaller buffers
3377250574Smarkj		 * for the "remnants".
3378178479Sjb		 */
3379250574Smarkj		while (buf = dt_pq_walk(dtp->dt_bufq, &cookie))
3380250574Smarkj			dt_realloc_buf(dtp, buf, buf->dtbd_size);
3381178479Sjb	}
3382178479Sjb
3383250574Smarkj	return (0);
3384178479Sjb}
3385