dt_consume.c revision 248708
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/*
27237624Spfg * Copyright (c) 2011, Joyent, Inc. All rights reserved.
28248708Spfg * Copyright (c) 2011 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>
42211554Srpaulo#if !defined(sun)
43211554Srpaulo#include <libproc_compat.h>
44211554Srpaulo#endif
45178479Sjb
46178479Sjb#define	DT_MASK_LO 0x00000000FFFFFFFFULL
47178479Sjb
48178479Sjb/*
49178479Sjb * We declare this here because (1) we need it and (2) we want to avoid a
50178479Sjb * dependency on libm in libdtrace.
51178479Sjb */
52178479Sjbstatic long double
53178479Sjbdt_fabsl(long double x)
54178479Sjb{
55178479Sjb	if (x < 0)
56178479Sjb		return (-x);
57178479Sjb
58178479Sjb	return (x);
59178479Sjb}
60178479Sjb
61178479Sjb/*
62178479Sjb * 128-bit arithmetic functions needed to support the stddev() aggregating
63178479Sjb * action.
64178479Sjb */
65178479Sjbstatic int
66178479Sjbdt_gt_128(uint64_t *a, uint64_t *b)
67178479Sjb{
68178479Sjb	return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
69178479Sjb}
70178479Sjb
71178479Sjbstatic int
72178479Sjbdt_ge_128(uint64_t *a, uint64_t *b)
73178479Sjb{
74178479Sjb	return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
75178479Sjb}
76178479Sjb
77178479Sjbstatic int
78178479Sjbdt_le_128(uint64_t *a, uint64_t *b)
79178479Sjb{
80178479Sjb	return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
81178479Sjb}
82178479Sjb
83178479Sjb/*
84178479Sjb * Shift the 128-bit value in a by b. If b is positive, shift left.
85178479Sjb * If b is negative, shift right.
86178479Sjb */
87178479Sjbstatic void
88178479Sjbdt_shift_128(uint64_t *a, int b)
89178479Sjb{
90178479Sjb	uint64_t mask;
91178479Sjb
92178479Sjb	if (b == 0)
93178479Sjb		return;
94178479Sjb
95178479Sjb	if (b < 0) {
96178479Sjb		b = -b;
97178479Sjb		if (b >= 64) {
98178479Sjb			a[0] = a[1] >> (b - 64);
99178479Sjb			a[1] = 0;
100178479Sjb		} else {
101178479Sjb			a[0] >>= b;
102178479Sjb			mask = 1LL << (64 - b);
103178479Sjb			mask -= 1;
104178479Sjb			a[0] |= ((a[1] & mask) << (64 - b));
105178479Sjb			a[1] >>= b;
106178479Sjb		}
107178479Sjb	} else {
108178479Sjb		if (b >= 64) {
109178479Sjb			a[1] = a[0] << (b - 64);
110178479Sjb			a[0] = 0;
111178479Sjb		} else {
112178479Sjb			a[1] <<= b;
113178479Sjb			mask = a[0] >> (64 - b);
114178479Sjb			a[1] |= mask;
115178479Sjb			a[0] <<= b;
116178479Sjb		}
117178479Sjb	}
118178479Sjb}
119178479Sjb
120178479Sjbstatic int
121178479Sjbdt_nbits_128(uint64_t *a)
122178479Sjb{
123178479Sjb	int nbits = 0;
124178479Sjb	uint64_t tmp[2];
125178479Sjb	uint64_t zero[2] = { 0, 0 };
126178479Sjb
127178479Sjb	tmp[0] = a[0];
128178479Sjb	tmp[1] = a[1];
129178479Sjb
130178479Sjb	dt_shift_128(tmp, -1);
131178479Sjb	while (dt_gt_128(tmp, zero)) {
132178479Sjb		dt_shift_128(tmp, -1);
133178479Sjb		nbits++;
134178479Sjb	}
135178479Sjb
136178479Sjb	return (nbits);
137178479Sjb}
138178479Sjb
139178479Sjbstatic void
140178479Sjbdt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
141178479Sjb{
142178479Sjb	uint64_t result[2];
143178479Sjb
144178479Sjb	result[0] = minuend[0] - subtrahend[0];
145178479Sjb	result[1] = minuend[1] - subtrahend[1] -
146178479Sjb	    (minuend[0] < subtrahend[0] ? 1 : 0);
147178479Sjb
148178479Sjb	difference[0] = result[0];
149178479Sjb	difference[1] = result[1];
150178479Sjb}
151178479Sjb
152178479Sjbstatic void
153178479Sjbdt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
154178479Sjb{
155178479Sjb	uint64_t result[2];
156178479Sjb
157178479Sjb	result[0] = addend1[0] + addend2[0];
158178479Sjb	result[1] = addend1[1] + addend2[1] +
159178479Sjb	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
160178479Sjb
161178479Sjb	sum[0] = result[0];
162178479Sjb	sum[1] = result[1];
163178479Sjb}
164178479Sjb
165178479Sjb/*
166178479Sjb * The basic idea is to break the 2 64-bit values into 4 32-bit values,
167178479Sjb * use native multiplication on those, and then re-combine into the
168178479Sjb * resulting 128-bit value.
169178479Sjb *
170178479Sjb * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
171178479Sjb *     hi1 * hi2 << 64 +
172178479Sjb *     hi1 * lo2 << 32 +
173178479Sjb *     hi2 * lo1 << 32 +
174178479Sjb *     lo1 * lo2
175178479Sjb */
176178479Sjbstatic void
177178479Sjbdt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
178178479Sjb{
179178479Sjb	uint64_t hi1, hi2, lo1, lo2;
180178479Sjb	uint64_t tmp[2];
181178479Sjb
182178479Sjb	hi1 = factor1 >> 32;
183178479Sjb	hi2 = factor2 >> 32;
184178479Sjb
185178479Sjb	lo1 = factor1 & DT_MASK_LO;
186178479Sjb	lo2 = factor2 & DT_MASK_LO;
187178479Sjb
188178479Sjb	product[0] = lo1 * lo2;
189178479Sjb	product[1] = hi1 * hi2;
190178479Sjb
191178479Sjb	tmp[0] = hi1 * lo2;
192178479Sjb	tmp[1] = 0;
193178479Sjb	dt_shift_128(tmp, 32);
194178479Sjb	dt_add_128(product, tmp, product);
195178479Sjb
196178479Sjb	tmp[0] = hi2 * lo1;
197178479Sjb	tmp[1] = 0;
198178479Sjb	dt_shift_128(tmp, 32);
199178479Sjb	dt_add_128(product, tmp, product);
200178479Sjb}
201178479Sjb
202178479Sjb/*
203178479Sjb * This is long-hand division.
204178479Sjb *
205178479Sjb * We initialize subtrahend by shifting divisor left as far as possible. We
206178479Sjb * loop, comparing subtrahend to dividend:  if subtrahend is smaller, we
207178479Sjb * subtract and set the appropriate bit in the result.  We then shift
208178479Sjb * subtrahend right by one bit for the next comparison.
209178479Sjb */
210178479Sjbstatic void
211178479Sjbdt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
212178479Sjb{
213178479Sjb	uint64_t result[2] = { 0, 0 };
214178479Sjb	uint64_t remainder[2];
215178479Sjb	uint64_t subtrahend[2];
216178479Sjb	uint64_t divisor_128[2];
217178479Sjb	uint64_t mask[2] = { 1, 0 };
218178479Sjb	int log = 0;
219178479Sjb
220178479Sjb	assert(divisor != 0);
221178479Sjb
222178479Sjb	divisor_128[0] = divisor;
223178479Sjb	divisor_128[1] = 0;
224178479Sjb
225178479Sjb	remainder[0] = dividend[0];
226178479Sjb	remainder[1] = dividend[1];
227178479Sjb
228178479Sjb	subtrahend[0] = divisor;
229178479Sjb	subtrahend[1] = 0;
230178479Sjb
231178479Sjb	while (divisor > 0) {
232178479Sjb		log++;
233178479Sjb		divisor >>= 1;
234178479Sjb	}
235178479Sjb
236178479Sjb	dt_shift_128(subtrahend, 128 - log);
237178479Sjb	dt_shift_128(mask, 128 - log);
238178479Sjb
239178479Sjb	while (dt_ge_128(remainder, divisor_128)) {
240178479Sjb		if (dt_ge_128(remainder, subtrahend)) {
241178479Sjb			dt_subtract_128(remainder, subtrahend, remainder);
242178479Sjb			result[0] |= mask[0];
243178479Sjb			result[1] |= mask[1];
244178479Sjb		}
245178479Sjb
246178479Sjb		dt_shift_128(subtrahend, -1);
247178479Sjb		dt_shift_128(mask, -1);
248178479Sjb	}
249178479Sjb
250178479Sjb	quotient[0] = result[0];
251178479Sjb	quotient[1] = result[1];
252178479Sjb}
253178479Sjb
254178479Sjb/*
255178479Sjb * This is the long-hand method of calculating a square root.
256178479Sjb * The algorithm is as follows:
257178479Sjb *
258178479Sjb * 1. Group the digits by 2 from the right.
259178479Sjb * 2. Over the leftmost group, find the largest single-digit number
260178479Sjb *    whose square is less than that group.
261178479Sjb * 3. Subtract the result of the previous step (2 or 4, depending) and
262178479Sjb *    bring down the next two-digit group.
263178479Sjb * 4. For the result R we have so far, find the largest single-digit number
264178479Sjb *    x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
265178479Sjb *    (Note that this is doubling R and performing a decimal left-shift by 1
266178479Sjb *    and searching for the appropriate decimal to fill the one's place.)
267178479Sjb *    The value x is the next digit in the square root.
268178479Sjb * Repeat steps 3 and 4 until the desired precision is reached.  (We're
269178479Sjb * dealing with integers, so the above is sufficient.)
270178479Sjb *
271178479Sjb * In decimal, the square root of 582,734 would be calculated as so:
272178479Sjb *
273178479Sjb *     __7__6__3
274178479Sjb *    | 58 27 34
275178479Sjb *     -49       (7^2 == 49 => 7 is the first digit in the square root)
276178479Sjb *      --
277178479Sjb *       9 27    (Subtract and bring down the next group.)
278178479Sjb * 146   8 76    (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
279178479Sjb *      -----     the square root)
280178479Sjb *         51 34 (Subtract and bring down the next group.)
281178479Sjb * 1523    45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
282178479Sjb *         -----  the square root)
283178479Sjb *          5 65 (remainder)
284178479Sjb *
285178479Sjb * The above algorithm applies similarly in binary, but note that the
286178479Sjb * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
287178479Sjb * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
288178479Sjb * preceding difference?
289178479Sjb *
290178479Sjb * In binary, the square root of 11011011 would be calculated as so:
291178479Sjb *
292178479Sjb *     __1__1__1__0
293178479Sjb *    | 11 01 10 11
294178479Sjb *      01          (0 << 2 + 1 == 1 < 11 => this bit is 1)
295178479Sjb *      --
296178479Sjb *      10 01 10 11
297178479Sjb * 101   1 01       (1 << 2 + 1 == 101 < 1001 => next bit is 1)
298178479Sjb *      -----
299178479Sjb *       1 00 10 11
300178479Sjb * 1101    11 01    (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
301178479Sjb *       -------
302178479Sjb *          1 01 11
303178479Sjb * 11101    1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
304178479Sjb *
305178479Sjb */
306178479Sjbstatic uint64_t
307178479Sjbdt_sqrt_128(uint64_t *square)
308178479Sjb{
309178479Sjb	uint64_t result[2] = { 0, 0 };
310178479Sjb	uint64_t diff[2] = { 0, 0 };
311178479Sjb	uint64_t one[2] = { 1, 0 };
312178479Sjb	uint64_t next_pair[2];
313178479Sjb	uint64_t next_try[2];
314178479Sjb	uint64_t bit_pairs, pair_shift;
315178479Sjb	int i;
316178479Sjb
317178479Sjb	bit_pairs = dt_nbits_128(square) / 2;
318178479Sjb	pair_shift = bit_pairs * 2;
319178479Sjb
320178479Sjb	for (i = 0; i <= bit_pairs; i++) {
321178479Sjb		/*
322178479Sjb		 * Bring down the next pair of bits.
323178479Sjb		 */
324178479Sjb		next_pair[0] = square[0];
325178479Sjb		next_pair[1] = square[1];
326178479Sjb		dt_shift_128(next_pair, -pair_shift);
327178479Sjb		next_pair[0] &= 0x3;
328178479Sjb		next_pair[1] = 0;
329178479Sjb
330178479Sjb		dt_shift_128(diff, 2);
331178479Sjb		dt_add_128(diff, next_pair, diff);
332178479Sjb
333178479Sjb		/*
334178479Sjb		 * next_try = R << 2 + 1
335178479Sjb		 */
336178479Sjb		next_try[0] = result[0];
337178479Sjb		next_try[1] = result[1];
338178479Sjb		dt_shift_128(next_try, 2);
339178479Sjb		dt_add_128(next_try, one, next_try);
340178479Sjb
341178479Sjb		if (dt_le_128(next_try, diff)) {
342178479Sjb			dt_subtract_128(diff, next_try, diff);
343178479Sjb			dt_shift_128(result, 1);
344178479Sjb			dt_add_128(result, one, result);
345178479Sjb		} else {
346178479Sjb			dt_shift_128(result, 1);
347178479Sjb		}
348178479Sjb
349178479Sjb		pair_shift -= 2;
350178479Sjb	}
351178479Sjb
352178479Sjb	assert(result[1] == 0);
353178479Sjb
354178479Sjb	return (result[0]);
355178479Sjb}
356178479Sjb
357178479Sjbuint64_t
358178479Sjbdt_stddev(uint64_t *data, uint64_t normal)
359178479Sjb{
360178479Sjb	uint64_t avg_of_squares[2];
361178479Sjb	uint64_t square_of_avg[2];
362178479Sjb	int64_t norm_avg;
363178479Sjb	uint64_t diff[2];
364178479Sjb
365178479Sjb	/*
366178479Sjb	 * The standard approximation for standard deviation is
367178479Sjb	 * sqrt(average(x**2) - average(x)**2), i.e. the square root
368178479Sjb	 * of the average of the squares minus the square of the average.
369178479Sjb	 */
370178479Sjb	dt_divide_128(data + 2, normal, avg_of_squares);
371178479Sjb	dt_divide_128(avg_of_squares, data[0], avg_of_squares);
372178479Sjb
373178479Sjb	norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
374178479Sjb
375178479Sjb	if (norm_avg < 0)
376178479Sjb		norm_avg = -norm_avg;
377178479Sjb
378178479Sjb	dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
379178479Sjb
380178479Sjb	dt_subtract_128(avg_of_squares, square_of_avg, diff);
381178479Sjb
382178479Sjb	return (dt_sqrt_128(diff));
383178479Sjb}
384178479Sjb
385178479Sjbstatic int
386178479Sjbdt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
387178479Sjb    dtrace_bufdesc_t *buf, size_t offs)
388178479Sjb{
389178479Sjb	dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
390178479Sjb	dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
391178479Sjb	char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
392178479Sjb	dtrace_flowkind_t flow = DTRACEFLOW_NONE;
393178479Sjb	const char *str = NULL;
394178479Sjb	static const char *e_str[2] = { " -> ", " => " };
395178479Sjb	static const char *r_str[2] = { " <- ", " <= " };
396178479Sjb	static const char *ent = "entry", *ret = "return";
397178479Sjb	static int entlen = 0, retlen = 0;
398178479Sjb	dtrace_epid_t next, id = epd->dtepd_epid;
399178479Sjb	int rval;
400178479Sjb
401178479Sjb	if (entlen == 0) {
402178479Sjb		assert(retlen == 0);
403178479Sjb		entlen = strlen(ent);
404178479Sjb		retlen = strlen(ret);
405178479Sjb	}
406178479Sjb
407178479Sjb	/*
408178479Sjb	 * If the name of the probe is "entry" or ends with "-entry", we
409178479Sjb	 * treat it as an entry; if it is "return" or ends with "-return",
410178479Sjb	 * we treat it as a return.  (This allows application-provided probes
411178479Sjb	 * like "method-entry" or "function-entry" to participate in flow
412178479Sjb	 * indentation -- without accidentally misinterpreting popular probe
413178479Sjb	 * names like "carpentry", "gentry" or "Coventry".)
414178479Sjb	 */
415178479Sjb	if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
416178479Sjb	    (sub == n || sub[-1] == '-')) {
417178479Sjb		flow = DTRACEFLOW_ENTRY;
418178479Sjb		str = e_str[strcmp(p, "syscall") == 0];
419178479Sjb	} else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
420178479Sjb	    (sub == n || sub[-1] == '-')) {
421178479Sjb		flow = DTRACEFLOW_RETURN;
422178479Sjb		str = r_str[strcmp(p, "syscall") == 0];
423178479Sjb	}
424178479Sjb
425178479Sjb	/*
426178479Sjb	 * If we're going to indent this, we need to check the ID of our last
427178479Sjb	 * call.  If we're looking at the same probe ID but a different EPID,
428178479Sjb	 * we _don't_ want to indent.  (Yes, there are some minor holes in
429178479Sjb	 * this scheme -- it's a heuristic.)
430178479Sjb	 */
431178479Sjb	if (flow == DTRACEFLOW_ENTRY) {
432178479Sjb		if ((last != DTRACE_EPIDNONE && id != last &&
433178479Sjb		    pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
434178479Sjb			flow = DTRACEFLOW_NONE;
435178479Sjb	}
436178479Sjb
437178479Sjb	/*
438178479Sjb	 * If we're going to unindent this, it's more difficult to see if
439178479Sjb	 * we don't actually want to unindent it -- we need to look at the
440178479Sjb	 * _next_ EPID.
441178479Sjb	 */
442178479Sjb	if (flow == DTRACEFLOW_RETURN) {
443178479Sjb		offs += epd->dtepd_size;
444178479Sjb
445178479Sjb		do {
446178479Sjb			if (offs >= buf->dtbd_size) {
447178479Sjb				/*
448178479Sjb				 * We're at the end -- maybe.  If the oldest
449178479Sjb				 * record is non-zero, we need to wrap.
450178479Sjb				 */
451178479Sjb				if (buf->dtbd_oldest != 0) {
452178479Sjb					offs = 0;
453178479Sjb				} else {
454178479Sjb					goto out;
455178479Sjb				}
456178479Sjb			}
457178479Sjb
458178479Sjb			next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
459178479Sjb
460178479Sjb			if (next == DTRACE_EPIDNONE)
461178479Sjb				offs += sizeof (id);
462178479Sjb		} while (next == DTRACE_EPIDNONE);
463178479Sjb
464178479Sjb		if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
465178479Sjb			return (rval);
466178479Sjb
467178479Sjb		if (next != id && npd->dtpd_id == pd->dtpd_id)
468178479Sjb			flow = DTRACEFLOW_NONE;
469178479Sjb	}
470178479Sjb
471178479Sjbout:
472178479Sjb	if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
473178479Sjb		data->dtpda_prefix = str;
474178479Sjb	} else {
475178479Sjb		data->dtpda_prefix = "| ";
476178479Sjb	}
477178479Sjb
478178479Sjb	if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
479178479Sjb		data->dtpda_indent -= 2;
480178479Sjb
481178479Sjb	data->dtpda_flow = flow;
482178479Sjb
483178479Sjb	return (0);
484178479Sjb}
485178479Sjb
486178479Sjbstatic int
487178479Sjbdt_nullprobe()
488178479Sjb{
489178479Sjb	return (DTRACE_CONSUME_THIS);
490178479Sjb}
491178479Sjb
492178479Sjbstatic int
493178479Sjbdt_nullrec()
494178479Sjb{
495178479Sjb	return (DTRACE_CONSUME_NEXT);
496178479Sjb}
497178479Sjb
498178479Sjbint
499178479Sjbdt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
500178479Sjb    uint64_t normal, long double total, char positives, char negatives)
501178479Sjb{
502178479Sjb	long double f;
503178479Sjb	uint_t depth, len = 40;
504178479Sjb
505178479Sjb	const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
506178479Sjb	const char *spaces = "                                        ";
507178479Sjb
508178479Sjb	assert(strlen(ats) == len && strlen(spaces) == len);
509178479Sjb	assert(!(total == 0 && (positives || negatives)));
510178479Sjb	assert(!(val < 0 && !negatives));
511178479Sjb	assert(!(val > 0 && !positives));
512178479Sjb	assert(!(val != 0 && total == 0));
513178479Sjb
514178479Sjb	if (!negatives) {
515178479Sjb		if (positives) {
516178479Sjb			f = (dt_fabsl((long double)val) * len) / total;
517178479Sjb			depth = (uint_t)(f + 0.5);
518178479Sjb		} else {
519178479Sjb			depth = 0;
520178479Sjb		}
521178479Sjb
522178479Sjb		return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
523178479Sjb		    spaces + depth, (long long)val / normal));
524178479Sjb	}
525178479Sjb
526178479Sjb	if (!positives) {
527178479Sjb		f = (dt_fabsl((long double)val) * len) / total;
528178479Sjb		depth = (uint_t)(f + 0.5);
529178479Sjb
530178479Sjb		return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
531178479Sjb		    ats + len - depth, (long long)val / normal));
532178479Sjb	}
533178479Sjb
534178479Sjb	/*
535178479Sjb	 * If we're here, we have both positive and negative bucket values.
536178479Sjb	 * To express this graphically, we're going to generate both positive
537178479Sjb	 * and negative bars separated by a centerline.  These bars are half
538178479Sjb	 * the size of normal quantize()/lquantize() bars, so we divide the
539178479Sjb	 * length in half before calculating the bar length.
540178479Sjb	 */
541178479Sjb	len /= 2;
542178479Sjb	ats = &ats[len];
543178479Sjb	spaces = &spaces[len];
544178479Sjb
545178479Sjb	f = (dt_fabsl((long double)val) * len) / total;
546178479Sjb	depth = (uint_t)(f + 0.5);
547178479Sjb
548178479Sjb	if (val <= 0) {
549178479Sjb		return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
550178479Sjb		    ats + len - depth, len, "", (long long)val / normal));
551178479Sjb	} else {
552178479Sjb		return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
553178479Sjb		    ats + len - depth, spaces + depth,
554178479Sjb		    (long long)val / normal));
555178479Sjb	}
556178479Sjb}
557178479Sjb
558178479Sjbint
559178479Sjbdt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
560178479Sjb    size_t size, uint64_t normal)
561178479Sjb{
562178479Sjb	const int64_t *data = addr;
563178479Sjb	int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
564178479Sjb	long double total = 0;
565178479Sjb	char positives = 0, negatives = 0;
566178479Sjb
567178479Sjb	if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
568178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
569178479Sjb
570178479Sjb	while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
571178479Sjb		first_bin++;
572178479Sjb
573178479Sjb	if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
574178479Sjb		/*
575178479Sjb		 * There isn't any data.  This is possible if (and only if)
576178479Sjb		 * negative increment values have been used.  In this case,
577178479Sjb		 * we'll print the buckets around 0.
578178479Sjb		 */
579178479Sjb		first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
580178479Sjb		last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
581178479Sjb	} else {
582178479Sjb		if (first_bin > 0)
583178479Sjb			first_bin--;
584178479Sjb
585178479Sjb		while (last_bin > 0 && data[last_bin] == 0)
586178479Sjb			last_bin--;
587178479Sjb
588178479Sjb		if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
589178479Sjb			last_bin++;
590178479Sjb	}
591178479Sjb
592178479Sjb	for (i = first_bin; i <= last_bin; i++) {
593178479Sjb		positives |= (data[i] > 0);
594178479Sjb		negatives |= (data[i] < 0);
595178479Sjb		total += dt_fabsl((long double)data[i]);
596178479Sjb	}
597178479Sjb
598178479Sjb	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
599178479Sjb	    "------------- Distribution -------------", "count") < 0)
600178479Sjb		return (-1);
601178479Sjb
602178479Sjb	for (i = first_bin; i <= last_bin; i++) {
603178479Sjb		if (dt_printf(dtp, fp, "%16lld ",
604178479Sjb		    (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
605178479Sjb			return (-1);
606178479Sjb
607178479Sjb		if (dt_print_quantline(dtp, fp, data[i], normal, total,
608178479Sjb		    positives, negatives) < 0)
609178479Sjb			return (-1);
610178479Sjb	}
611178479Sjb
612178479Sjb	return (0);
613178479Sjb}
614178479Sjb
615178479Sjbint
616178479Sjbdt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
617178479Sjb    size_t size, uint64_t normal)
618178479Sjb{
619178479Sjb	const int64_t *data = addr;
620178479Sjb	int i, first_bin, last_bin, base;
621178479Sjb	uint64_t arg;
622178479Sjb	long double total = 0;
623178479Sjb	uint16_t step, levels;
624178479Sjb	char positives = 0, negatives = 0;
625178479Sjb
626178479Sjb	if (size < sizeof (uint64_t))
627178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
628178479Sjb
629178479Sjb	arg = *data++;
630178479Sjb	size -= sizeof (uint64_t);
631178479Sjb
632178479Sjb	base = DTRACE_LQUANTIZE_BASE(arg);
633178479Sjb	step = DTRACE_LQUANTIZE_STEP(arg);
634178479Sjb	levels = DTRACE_LQUANTIZE_LEVELS(arg);
635178479Sjb
636178479Sjb	first_bin = 0;
637178479Sjb	last_bin = levels + 1;
638178479Sjb
639178479Sjb	if (size != sizeof (uint64_t) * (levels + 2))
640178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
641178479Sjb
642178479Sjb	while (first_bin <= levels + 1 && data[first_bin] == 0)
643178479Sjb		first_bin++;
644178479Sjb
645178479Sjb	if (first_bin > levels + 1) {
646178479Sjb		first_bin = 0;
647178479Sjb		last_bin = 2;
648178479Sjb	} else {
649178479Sjb		if (first_bin > 0)
650178479Sjb			first_bin--;
651178479Sjb
652178479Sjb		while (last_bin > 0 && data[last_bin] == 0)
653178479Sjb			last_bin--;
654178479Sjb
655178479Sjb		if (last_bin < levels + 1)
656178479Sjb			last_bin++;
657178479Sjb	}
658178479Sjb
659178479Sjb	for (i = first_bin; i <= last_bin; i++) {
660178479Sjb		positives |= (data[i] > 0);
661178479Sjb		negatives |= (data[i] < 0);
662178479Sjb		total += dt_fabsl((long double)data[i]);
663178479Sjb	}
664178479Sjb
665178479Sjb	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
666178479Sjb	    "------------- Distribution -------------", "count") < 0)
667178479Sjb		return (-1);
668178479Sjb
669178479Sjb	for (i = first_bin; i <= last_bin; i++) {
670178479Sjb		char c[32];
671178479Sjb		int err;
672178479Sjb
673178479Sjb		if (i == 0) {
674178479Sjb			(void) snprintf(c, sizeof (c), "< %d",
675178479Sjb			    base / (uint32_t)normal);
676178479Sjb			err = dt_printf(dtp, fp, "%16s ", c);
677178479Sjb		} else if (i == levels + 1) {
678178479Sjb			(void) snprintf(c, sizeof (c), ">= %d",
679178479Sjb			    base + (levels * step));
680178479Sjb			err = dt_printf(dtp, fp, "%16s ", c);
681178479Sjb		} else {
682178479Sjb			err = dt_printf(dtp, fp, "%16d ",
683178479Sjb			    base + (i - 1) * step);
684178479Sjb		}
685178479Sjb
686178479Sjb		if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
687178479Sjb		    total, positives, negatives) < 0)
688178479Sjb			return (-1);
689178479Sjb	}
690178479Sjb
691178479Sjb	return (0);
692178479Sjb}
693178479Sjb
694237624Spfgint
695237624Spfgdt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
696237624Spfg    size_t size, uint64_t normal)
697237624Spfg{
698237624Spfg	int i, first_bin, last_bin, bin = 1, order, levels;
699237624Spfg	uint16_t factor, low, high, nsteps;
700237624Spfg	const int64_t *data = addr;
701237624Spfg	int64_t value = 1, next, step;
702237624Spfg	char positives = 0, negatives = 0;
703237624Spfg	long double total = 0;
704237624Spfg	uint64_t arg;
705237624Spfg	char c[32];
706237624Spfg
707237624Spfg	if (size < sizeof (uint64_t))
708237624Spfg		return (dt_set_errno(dtp, EDT_DMISMATCH));
709237624Spfg
710237624Spfg	arg = *data++;
711237624Spfg	size -= sizeof (uint64_t);
712237624Spfg
713237624Spfg	factor = DTRACE_LLQUANTIZE_FACTOR(arg);
714237624Spfg	low = DTRACE_LLQUANTIZE_LOW(arg);
715237624Spfg	high = DTRACE_LLQUANTIZE_HIGH(arg);
716237624Spfg	nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
717237624Spfg
718237624Spfg	/*
719237624Spfg	 * We don't expect to be handed invalid llquantize() parameters here,
720237624Spfg	 * but sanity check them (to a degree) nonetheless.
721237624Spfg	 */
722237624Spfg	if (size > INT32_MAX || factor < 2 || low >= high ||
723237624Spfg	    nsteps == 0 || factor > nsteps)
724237624Spfg		return (dt_set_errno(dtp, EDT_DMISMATCH));
725237624Spfg
726237624Spfg	levels = (int)size / sizeof (uint64_t);
727237624Spfg
728237624Spfg	first_bin = 0;
729237624Spfg	last_bin = levels - 1;
730237624Spfg
731237624Spfg	while (first_bin < levels && data[first_bin] == 0)
732237624Spfg		first_bin++;
733237624Spfg
734237624Spfg	if (first_bin == levels) {
735237624Spfg		first_bin = 0;
736237624Spfg		last_bin = 1;
737237624Spfg	} else {
738237624Spfg		if (first_bin > 0)
739237624Spfg			first_bin--;
740237624Spfg
741237624Spfg		while (last_bin > 0 && data[last_bin] == 0)
742237624Spfg			last_bin--;
743237624Spfg
744237624Spfg		if (last_bin < levels - 1)
745237624Spfg			last_bin++;
746237624Spfg	}
747237624Spfg
748237624Spfg	for (i = first_bin; i <= last_bin; i++) {
749237624Spfg		positives |= (data[i] > 0);
750237624Spfg		negatives |= (data[i] < 0);
751237624Spfg		total += dt_fabsl((long double)data[i]);
752237624Spfg	}
753237624Spfg
754237624Spfg	if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
755237624Spfg	    "------------- Distribution -------------", "count") < 0)
756237624Spfg		return (-1);
757237624Spfg
758237624Spfg	for (order = 0; order < low; order++)
759237624Spfg		value *= factor;
760237624Spfg
761237624Spfg	next = value * factor;
762237624Spfg	step = next > nsteps ? next / nsteps : 1;
763237624Spfg
764237624Spfg	if (first_bin == 0) {
765237716Spfg		(void) snprintf(c, sizeof (c), "< %lld", (long long)value);
766237624Spfg
767237624Spfg		if (dt_printf(dtp, fp, "%16s ", c) < 0)
768237624Spfg			return (-1);
769237624Spfg
770237624Spfg		if (dt_print_quantline(dtp, fp, data[0], normal,
771237624Spfg		    total, positives, negatives) < 0)
772237624Spfg			return (-1);
773237624Spfg	}
774237624Spfg
775237624Spfg	while (order <= high) {
776237624Spfg		if (bin >= first_bin && bin <= last_bin) {
777237624Spfg			if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
778237624Spfg				return (-1);
779237624Spfg
780237624Spfg			if (dt_print_quantline(dtp, fp, data[bin],
781237624Spfg			    normal, total, positives, negatives) < 0)
782237624Spfg				return (-1);
783237624Spfg		}
784237624Spfg
785237624Spfg		assert(value < next);
786237624Spfg		bin++;
787237624Spfg
788237624Spfg		if ((value += step) != next)
789237624Spfg			continue;
790237624Spfg
791237624Spfg		next = value * factor;
792237624Spfg		step = next > nsteps ? next / nsteps : 1;
793237624Spfg		order++;
794237624Spfg	}
795237624Spfg
796237624Spfg	if (last_bin < bin)
797237624Spfg		return (0);
798237624Spfg
799237624Spfg	assert(last_bin == bin);
800238071Sdim	(void) snprintf(c, sizeof (c), ">= %lld", (long long)value);
801237624Spfg
802237624Spfg	if (dt_printf(dtp, fp, "%16s ", c) < 0)
803237624Spfg		return (-1);
804237624Spfg
805237624Spfg	return (dt_print_quantline(dtp, fp, data[bin], normal,
806237624Spfg	    total, positives, negatives));
807237624Spfg}
808237624Spfg
809178479Sjb/*ARGSUSED*/
810178479Sjbstatic int
811178479Sjbdt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
812178479Sjb    size_t size, uint64_t normal)
813178479Sjb{
814178479Sjb	/* LINTED - alignment */
815178479Sjb	int64_t *data = (int64_t *)addr;
816178479Sjb
817178479Sjb	return (dt_printf(dtp, fp, " %16lld", data[0] ?
818178479Sjb	    (long long)(data[1] / (int64_t)normal / data[0]) : 0));
819178479Sjb}
820178479Sjb
821178479Sjb/*ARGSUSED*/
822178479Sjbstatic int
823178479Sjbdt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
824178479Sjb    size_t size, uint64_t normal)
825178479Sjb{
826178479Sjb	/* LINTED - alignment */
827178479Sjb	uint64_t *data = (uint64_t *)addr;
828178479Sjb
829178479Sjb	return (dt_printf(dtp, fp, " %16llu", data[0] ?
830178479Sjb	    (unsigned long long) dt_stddev(data, normal) : 0));
831178479Sjb}
832178479Sjb
833178479Sjb/*ARGSUSED*/
834178479Sjbint
835178479Sjbdt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
836248690Spfg    size_t nbytes, int width, int quiet, int forceraw)
837178479Sjb{
838178479Sjb	/*
839178479Sjb	 * If the byte stream is a series of printable characters, followed by
840178479Sjb	 * a terminating byte, we print it out as a string.  Otherwise, we
841178479Sjb	 * assume that it's something else and just print the bytes.
842178479Sjb	 */
843178479Sjb	int i, j, margin = 5;
844178479Sjb	char *c = (char *)addr;
845178479Sjb
846178479Sjb	if (nbytes == 0)
847178479Sjb		return (0);
848178479Sjb
849248690Spfg	if (forceraw)
850178479Sjb		goto raw;
851178479Sjb
852248690Spfg	if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
853248690Spfg		goto raw;
854248690Spfg
855178479Sjb	for (i = 0; i < nbytes; i++) {
856178479Sjb		/*
857178479Sjb		 * We define a "printable character" to be one for which
858178479Sjb		 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
859178479Sjb		 * or a character which is either backspace or the bell.
860178479Sjb		 * Backspace and the bell are regrettably special because
861178479Sjb		 * they fail the first two tests -- and yet they are entirely
862178479Sjb		 * printable.  These are the only two control characters that
863178479Sjb		 * have meaning for the terminal and for which isprint(3C) and
864178479Sjb		 * isspace(3C) return 0.
865178479Sjb		 */
866178479Sjb		if (isprint(c[i]) || isspace(c[i]) ||
867178479Sjb		    c[i] == '\b' || c[i] == '\a')
868178479Sjb			continue;
869178479Sjb
870178479Sjb		if (c[i] == '\0' && i > 0) {
871178479Sjb			/*
872178479Sjb			 * This looks like it might be a string.  Before we
873178479Sjb			 * assume that it is indeed a string, check the
874178479Sjb			 * remainder of the byte range; if it contains
875178479Sjb			 * additional non-nul characters, we'll assume that
876178479Sjb			 * it's a binary stream that just happens to look like
877178479Sjb			 * a string, and we'll print out the individual bytes.
878178479Sjb			 */
879178479Sjb			for (j = i + 1; j < nbytes; j++) {
880178479Sjb				if (c[j] != '\0')
881178479Sjb					break;
882178479Sjb			}
883178479Sjb
884178479Sjb			if (j != nbytes)
885178479Sjb				break;
886178479Sjb
887178479Sjb			if (quiet)
888178479Sjb				return (dt_printf(dtp, fp, "%s", c));
889178479Sjb			else
890178479Sjb				return (dt_printf(dtp, fp, "  %-*s", width, c));
891178479Sjb		}
892178479Sjb
893178479Sjb		break;
894178479Sjb	}
895178479Sjb
896178479Sjb	if (i == nbytes) {
897178479Sjb		/*
898178479Sjb		 * The byte range is all printable characters, but there is
899178479Sjb		 * no trailing nul byte.  We'll assume that it's a string and
900178479Sjb		 * print it as such.
901178479Sjb		 */
902178479Sjb		char *s = alloca(nbytes + 1);
903178479Sjb		bcopy(c, s, nbytes);
904178479Sjb		s[nbytes] = '\0';
905178479Sjb		return (dt_printf(dtp, fp, "  %-*s", width, s));
906178479Sjb	}
907178479Sjb
908178479Sjbraw:
909178479Sjb	if (dt_printf(dtp, fp, "\n%*s      ", margin, "") < 0)
910178479Sjb		return (-1);
911178479Sjb
912178479Sjb	for (i = 0; i < 16; i++)
913178479Sjb		if (dt_printf(dtp, fp, "  %c", "0123456789abcdef"[i]) < 0)
914178479Sjb			return (-1);
915178479Sjb
916178479Sjb	if (dt_printf(dtp, fp, "  0123456789abcdef\n") < 0)
917178479Sjb		return (-1);
918178479Sjb
919178479Sjb
920178479Sjb	for (i = 0; i < nbytes; i += 16) {
921178479Sjb		if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
922178479Sjb			return (-1);
923178479Sjb
924178479Sjb		for (j = i; j < i + 16 && j < nbytes; j++) {
925178479Sjb			if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
926178479Sjb				return (-1);
927178479Sjb		}
928178479Sjb
929178479Sjb		while (j++ % 16) {
930178479Sjb			if (dt_printf(dtp, fp, "   ") < 0)
931178479Sjb				return (-1);
932178479Sjb		}
933178479Sjb
934178479Sjb		if (dt_printf(dtp, fp, "  ") < 0)
935178479Sjb			return (-1);
936178479Sjb
937178479Sjb		for (j = i; j < i + 16 && j < nbytes; j++) {
938178479Sjb			if (dt_printf(dtp, fp, "%c",
939178479Sjb			    c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
940178479Sjb				return (-1);
941178479Sjb		}
942178479Sjb
943178479Sjb		if (dt_printf(dtp, fp, "\n") < 0)
944178479Sjb			return (-1);
945178479Sjb	}
946178479Sjb
947178479Sjb	return (0);
948178479Sjb}
949178479Sjb
950178479Sjbint
951178479Sjbdt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
952178479Sjb    caddr_t addr, int depth, int size)
953178479Sjb{
954178479Sjb	dtrace_syminfo_t dts;
955178479Sjb	GElf_Sym sym;
956178479Sjb	int i, indent;
957178479Sjb	char c[PATH_MAX * 2];
958178479Sjb	uint64_t pc;
959178479Sjb
960178479Sjb	if (dt_printf(dtp, fp, "\n") < 0)
961178479Sjb		return (-1);
962178479Sjb
963178479Sjb	if (format == NULL)
964178479Sjb		format = "%s";
965178479Sjb
966178479Sjb	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
967178479Sjb		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
968178479Sjb	else
969178479Sjb		indent = _dtrace_stkindent;
970178479Sjb
971178479Sjb	for (i = 0; i < depth; i++) {
972178479Sjb		switch (size) {
973178479Sjb		case sizeof (uint32_t):
974178479Sjb			/* LINTED - alignment */
975178479Sjb			pc = *((uint32_t *)addr);
976178479Sjb			break;
977178479Sjb
978178479Sjb		case sizeof (uint64_t):
979178479Sjb			/* LINTED - alignment */
980178479Sjb			pc = *((uint64_t *)addr);
981178479Sjb			break;
982178479Sjb
983178479Sjb		default:
984178479Sjb			return (dt_set_errno(dtp, EDT_BADSTACKPC));
985178479Sjb		}
986178479Sjb
987178576Sjb		if (pc == 0)
988178479Sjb			break;
989178479Sjb
990178479Sjb		addr += size;
991178479Sjb
992178479Sjb		if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
993178479Sjb			return (-1);
994178479Sjb
995178479Sjb		if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
996178479Sjb			if (pc > sym.st_value) {
997178479Sjb				(void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
998178479Sjb				    dts.dts_object, dts.dts_name,
999228579Sdim				    (u_longlong_t)(pc - sym.st_value));
1000178479Sjb			} else {
1001178479Sjb				(void) snprintf(c, sizeof (c), "%s`%s",
1002178479Sjb				    dts.dts_object, dts.dts_name);
1003178479Sjb			}
1004178479Sjb		} else {
1005178479Sjb			/*
1006178479Sjb			 * We'll repeat the lookup, but this time we'll specify
1007178479Sjb			 * a NULL GElf_Sym -- indicating that we're only
1008178479Sjb			 * interested in the containing module.
1009178479Sjb			 */
1010178479Sjb			if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1011178479Sjb				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1012228579Sdim				    dts.dts_object, (u_longlong_t)pc);
1013178479Sjb			} else {
1014228579Sdim				(void) snprintf(c, sizeof (c), "0x%llx",
1015228579Sdim				    (u_longlong_t)pc);
1016178479Sjb			}
1017178479Sjb		}
1018178479Sjb
1019178479Sjb		if (dt_printf(dtp, fp, format, c) < 0)
1020178479Sjb			return (-1);
1021178479Sjb
1022178479Sjb		if (dt_printf(dtp, fp, "\n") < 0)
1023178479Sjb			return (-1);
1024178479Sjb	}
1025178479Sjb
1026178479Sjb	return (0);
1027178479Sjb}
1028178479Sjb
1029178479Sjbint
1030178479Sjbdt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1031178479Sjb    caddr_t addr, uint64_t arg)
1032178479Sjb{
1033178479Sjb	/* LINTED - alignment */
1034178479Sjb	uint64_t *pc = (uint64_t *)addr;
1035178479Sjb	uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1036178479Sjb	uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1037178479Sjb	const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1038178479Sjb	const char *str = strsize ? strbase : NULL;
1039178479Sjb	int err = 0;
1040178479Sjb
1041178479Sjb	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1042178479Sjb	struct ps_prochandle *P;
1043178479Sjb	GElf_Sym sym;
1044178479Sjb	int i, indent;
1045178479Sjb	pid_t pid;
1046178479Sjb
1047178479Sjb	if (depth == 0)
1048178479Sjb		return (0);
1049178479Sjb
1050178479Sjb	pid = (pid_t)*pc++;
1051178479Sjb
1052178479Sjb	if (dt_printf(dtp, fp, "\n") < 0)
1053178479Sjb		return (-1);
1054178479Sjb
1055178479Sjb	if (format == NULL)
1056178479Sjb		format = "%s";
1057178479Sjb
1058178479Sjb	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1059178479Sjb		indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1060178479Sjb	else
1061178479Sjb		indent = _dtrace_stkindent;
1062178479Sjb
1063178479Sjb	/*
1064178479Sjb	 * Ultimately, we need to add an entry point in the library vector for
1065178479Sjb	 * determining <symbol, offset> from <pid, address>.  For now, if
1066178479Sjb	 * this is a vector open, we just print the raw address or string.
1067178479Sjb	 */
1068178479Sjb	if (dtp->dt_vector == NULL)
1069178479Sjb		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1070178479Sjb	else
1071178479Sjb		P = NULL;
1072178479Sjb
1073178479Sjb	if (P != NULL)
1074178479Sjb		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1075178479Sjb
1076178576Sjb	for (i = 0; i < depth && pc[i] != 0; i++) {
1077178479Sjb		const prmap_t *map;
1078178479Sjb
1079178479Sjb		if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1080178479Sjb			break;
1081178479Sjb
1082178479Sjb		if (P != NULL && Plookup_by_addr(P, pc[i],
1083178479Sjb		    name, sizeof (name), &sym) == 0) {
1084178479Sjb			(void) Pobjname(P, pc[i], objname, sizeof (objname));
1085178479Sjb
1086178479Sjb			if (pc[i] > sym.st_value) {
1087178479Sjb				(void) snprintf(c, sizeof (c),
1088178479Sjb				    "%s`%s+0x%llx", dt_basename(objname), name,
1089178479Sjb				    (u_longlong_t)(pc[i] - sym.st_value));
1090178479Sjb			} else {
1091178479Sjb				(void) snprintf(c, sizeof (c),
1092178479Sjb				    "%s`%s", dt_basename(objname), name);
1093178479Sjb			}
1094178479Sjb		} else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1095178479Sjb		    (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1096178479Sjb		    (map->pr_mflags & MA_WRITE)))) {
1097178479Sjb			/*
1098178479Sjb			 * If the current string pointer in the string table
1099178479Sjb			 * does not point to an empty string _and_ the program
1100178479Sjb			 * counter falls in a writable region, we'll use the
1101178479Sjb			 * string from the string table instead of the raw
1102178479Sjb			 * address.  This last condition is necessary because
1103178479Sjb			 * some (broken) ustack helpers will return a string
1104178479Sjb			 * even for a program counter that they can't
1105178479Sjb			 * identify.  If we have a string for a program
1106178479Sjb			 * counter that falls in a segment that isn't
1107178479Sjb			 * writable, we assume that we have fallen into this
1108178479Sjb			 * case and we refuse to use the string.
1109178479Sjb			 */
1110178479Sjb			(void) snprintf(c, sizeof (c), "%s", str);
1111178479Sjb		} else {
1112178479Sjb			if (P != NULL && Pobjname(P, pc[i], objname,
1113178576Sjb			    sizeof (objname)) != 0) {
1114178479Sjb				(void) snprintf(c, sizeof (c), "%s`0x%llx",
1115178479Sjb				    dt_basename(objname), (u_longlong_t)pc[i]);
1116178479Sjb			} else {
1117178479Sjb				(void) snprintf(c, sizeof (c), "0x%llx",
1118178479Sjb				    (u_longlong_t)pc[i]);
1119178479Sjb			}
1120178479Sjb		}
1121178479Sjb
1122178479Sjb		if ((err = dt_printf(dtp, fp, format, c)) < 0)
1123178479Sjb			break;
1124178479Sjb
1125178479Sjb		if ((err = dt_printf(dtp, fp, "\n")) < 0)
1126178479Sjb			break;
1127178479Sjb
1128178479Sjb		if (str != NULL && str[0] == '@') {
1129178479Sjb			/*
1130178479Sjb			 * If the first character of the string is an "at" sign,
1131178479Sjb			 * then the string is inferred to be an annotation --
1132178479Sjb			 * and it is printed out beneath the frame and offset
1133178479Sjb			 * with brackets.
1134178479Sjb			 */
1135178479Sjb			if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1136178479Sjb				break;
1137178479Sjb
1138178479Sjb			(void) snprintf(c, sizeof (c), "  [ %s ]", &str[1]);
1139178479Sjb
1140178479Sjb			if ((err = dt_printf(dtp, fp, format, c)) < 0)
1141178479Sjb				break;
1142178479Sjb
1143178479Sjb			if ((err = dt_printf(dtp, fp, "\n")) < 0)
1144178479Sjb				break;
1145178479Sjb		}
1146178479Sjb
1147178479Sjb		if (str != NULL) {
1148178479Sjb			str += strlen(str) + 1;
1149178479Sjb			if (str - strbase >= strsize)
1150178479Sjb				str = NULL;
1151178479Sjb		}
1152178479Sjb	}
1153178479Sjb
1154178479Sjb	if (P != NULL) {
1155178479Sjb		dt_proc_unlock(dtp, P);
1156178479Sjb		dt_proc_release(dtp, P);
1157178479Sjb	}
1158178479Sjb
1159178479Sjb	return (err);
1160178479Sjb}
1161178479Sjb
1162178479Sjbstatic int
1163178479Sjbdt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1164178479Sjb{
1165178479Sjb	/* LINTED - alignment */
1166178479Sjb	uint64_t pid = ((uint64_t *)addr)[0];
1167178479Sjb	/* LINTED - alignment */
1168178479Sjb	uint64_t pc = ((uint64_t *)addr)[1];
1169178479Sjb	const char *format = "  %-50s";
1170178479Sjb	char *s;
1171178479Sjb	int n, len = 256;
1172178479Sjb
1173178479Sjb	if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1174178479Sjb		struct ps_prochandle *P;
1175178479Sjb
1176178479Sjb		if ((P = dt_proc_grab(dtp, pid,
1177178479Sjb		    PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1178178479Sjb			GElf_Sym sym;
1179178479Sjb
1180178479Sjb			dt_proc_lock(dtp, P);
1181178479Sjb
1182178479Sjb			if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1183178479Sjb				pc = sym.st_value;
1184178479Sjb
1185178479Sjb			dt_proc_unlock(dtp, P);
1186178479Sjb			dt_proc_release(dtp, P);
1187178479Sjb		}
1188178479Sjb	}
1189178479Sjb
1190178479Sjb	do {
1191178479Sjb		n = len;
1192178479Sjb		s = alloca(n);
1193210767Srpaulo	} while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1194178479Sjb
1195178479Sjb	return (dt_printf(dtp, fp, format, s));
1196178479Sjb}
1197178479Sjb
1198178479Sjbint
1199178479Sjbdt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1200178479Sjb{
1201178479Sjb	/* LINTED - alignment */
1202178479Sjb	uint64_t pid = ((uint64_t *)addr)[0];
1203178479Sjb	/* LINTED - alignment */
1204178479Sjb	uint64_t pc = ((uint64_t *)addr)[1];
1205178479Sjb	int err = 0;
1206178479Sjb
1207178479Sjb	char objname[PATH_MAX], c[PATH_MAX * 2];
1208178479Sjb	struct ps_prochandle *P;
1209178479Sjb
1210178479Sjb	if (format == NULL)
1211178479Sjb		format = "  %-50s";
1212178479Sjb
1213178479Sjb	/*
1214178479Sjb	 * See the comment in dt_print_ustack() for the rationale for
1215178479Sjb	 * printing raw addresses in the vectored case.
1216178479Sjb	 */
1217178479Sjb	if (dtp->dt_vector == NULL)
1218178479Sjb		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1219178479Sjb	else
1220178479Sjb		P = NULL;
1221178479Sjb
1222178479Sjb	if (P != NULL)
1223178479Sjb		dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1224178479Sjb
1225178576Sjb	if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) {
1226178479Sjb		(void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1227178479Sjb	} else {
1228178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1229178479Sjb	}
1230178479Sjb
1231178479Sjb	err = dt_printf(dtp, fp, format, c);
1232178479Sjb
1233178479Sjb	if (P != NULL) {
1234178479Sjb		dt_proc_unlock(dtp, P);
1235178479Sjb		dt_proc_release(dtp, P);
1236178479Sjb	}
1237178479Sjb
1238178479Sjb	return (err);
1239178479Sjb}
1240178479Sjb
1241178576Sjbint
1242178576Sjbdt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1243178576Sjb{
1244178576Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1245178576Sjb	size_t nbytes = *((uintptr_t *) addr);
1246178576Sjb
1247178576Sjb	return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t),
1248178576Sjb	    nbytes, 50, quiet, 1));
1249178576Sjb}
1250178576Sjb
1251178576Sjbtypedef struct dt_type_cbdata {
1252178576Sjb	dtrace_hdl_t		*dtp;
1253178576Sjb	dtrace_typeinfo_t	dtt;
1254178576Sjb	caddr_t			addr;
1255178576Sjb	caddr_t			addrend;
1256178576Sjb	const char		*name;
1257178576Sjb	int			f_type;
1258178576Sjb	int			indent;
1259178576Sjb	int			type_width;
1260178576Sjb	int			name_width;
1261178576Sjb	FILE			*fp;
1262178576Sjb} dt_type_cbdata_t;
1263178576Sjb
1264178576Sjbstatic int	dt_print_type_data(dt_type_cbdata_t *, ctf_id_t);
1265178576Sjb
1266178479Sjbstatic int
1267178576Sjbdt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg)
1268178576Sjb{
1269178576Sjb	dt_type_cbdata_t cbdata;
1270178576Sjb	dt_type_cbdata_t *cbdatap = arg;
1271178576Sjb	ssize_t ssz;
1272178576Sjb
1273178576Sjb	if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0)
1274178576Sjb		return (0);
1275178576Sjb
1276178576Sjb	off /= 8;
1277178576Sjb
1278178576Sjb	cbdata = *cbdatap;
1279178576Sjb	cbdata.name = name;
1280178576Sjb	cbdata.addr += off;
1281178576Sjb	cbdata.addrend = cbdata.addr + ssz;
1282178576Sjb
1283178576Sjb	return (dt_print_type_data(&cbdata, type));
1284178576Sjb}
1285178576Sjb
1286178576Sjbstatic int
1287178576Sjbdt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg)
1288178576Sjb{
1289178576Sjb	char buf[DT_TYPE_NAMELEN];
1290178576Sjb	char *p;
1291178576Sjb	dt_type_cbdata_t *cbdatap = arg;
1292178576Sjb	size_t sz = strlen(name);
1293178576Sjb
1294178576Sjb	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1295178576Sjb
1296178576Sjb	if ((p = strchr(buf, '[')) != NULL)
1297178576Sjb		p[-1] = '\0';
1298178576Sjb	else
1299178576Sjb		p = "";
1300178576Sjb
1301178576Sjb	sz += strlen(p);
1302178576Sjb
1303178576Sjb	if (sz > cbdatap->name_width)
1304178576Sjb		cbdatap->name_width = sz;
1305178576Sjb
1306178576Sjb	sz = strlen(buf);
1307178576Sjb
1308178576Sjb	if (sz > cbdatap->type_width)
1309178576Sjb		cbdatap->type_width = sz;
1310178576Sjb
1311178576Sjb	return (0);
1312178576Sjb}
1313178576Sjb
1314178576Sjbstatic int
1315178576Sjbdt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type)
1316178576Sjb{
1317178576Sjb	caddr_t addr = cbdatap->addr;
1318178576Sjb	caddr_t addrend = cbdatap->addrend;
1319178576Sjb	char buf[DT_TYPE_NAMELEN];
1320178576Sjb	char *p;
1321178576Sjb	int cnt = 0;
1322178576Sjb	uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type);
1323178576Sjb	ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type);
1324178576Sjb
1325178576Sjb	ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf));
1326178576Sjb
1327178576Sjb	if ((p = strchr(buf, '[')) != NULL)
1328178576Sjb		p[-1] = '\0';
1329178576Sjb	else
1330178576Sjb		p = "";
1331178576Sjb
1332178576Sjb	if (cbdatap->f_type) {
1333178576Sjb		int type_width = roundup(cbdatap->type_width + 1, 4);
1334178576Sjb		int name_width = roundup(cbdatap->name_width + 1, 4);
1335178576Sjb
1336178576Sjb		name_width -= strlen(cbdatap->name);
1337178576Sjb
1338178576Sjb		dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s	= ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p);
1339178576Sjb	}
1340178576Sjb
1341178576Sjb	while (addr < addrend) {
1342178576Sjb		dt_type_cbdata_t cbdata;
1343178576Sjb		ctf_arinfo_t arinfo;
1344178576Sjb		ctf_encoding_t cte;
1345178576Sjb		uintptr_t *up;
1346178576Sjb		void *vp = addr;
1347178576Sjb		cbdata = *cbdatap;
1348178576Sjb		cbdata.name = "";
1349178576Sjb		cbdata.addr = addr;
1350178576Sjb		cbdata.addrend = addr + ssz;
1351178576Sjb		cbdata.f_type = 0;
1352178576Sjb		cbdata.indent++;
1353178576Sjb		cbdata.type_width = 0;
1354178576Sjb		cbdata.name_width = 0;
1355178576Sjb
1356178576Sjb		if (cnt > 0)
1357178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,"");
1358178576Sjb
1359178576Sjb		switch (kind) {
1360178576Sjb		case CTF_K_INTEGER:
1361178576Sjb			if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0)
1362178576Sjb				return (-1);
1363178576Sjb			if ((cte.cte_format & CTF_INT_SIGNED) != 0)
1364178576Sjb				switch (cte.cte_bits) {
1365178576Sjb				case 8:
1366178576Sjb					if (isprint(*((char *) vp)))
1367178576Sjb						dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp));
1368178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp));
1369178576Sjb					break;
1370178576Sjb				case 16:
1371178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp));
1372178576Sjb					break;
1373178576Sjb				case 32:
1374178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp));
1375178576Sjb					break;
1376178576Sjb				case 64:
1377178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp));
1378178576Sjb					break;
1379178576Sjb				default:
1380178576Sjb					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);
1381178576Sjb					break;
1382178576Sjb				}
1383178576Sjb			else
1384178576Sjb				switch (cte.cte_bits) {
1385178576Sjb				case 8:
1386178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff);
1387178576Sjb					break;
1388178576Sjb				case 16:
1389178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp));
1390178576Sjb					break;
1391178576Sjb				case 32:
1392178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp));
1393178576Sjb					break;
1394178576Sjb				case 64:
1395178576Sjb					dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp));
1396178576Sjb					break;
1397178576Sjb				default:
1398178576Sjb					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);
1399178576Sjb					break;
1400178576Sjb				}
1401178576Sjb			break;
1402178576Sjb		case CTF_K_FLOAT:
1403178576Sjb			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);
1404178576Sjb			break;
1405178576Sjb		case CTF_K_POINTER:
1406178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr));
1407178576Sjb			break;
1408178576Sjb		case CTF_K_ARRAY:
1409178576Sjb			if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0)
1410178576Sjb				return (-1);
1411178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,"");
1412178576Sjb			dt_print_type_data(&cbdata, arinfo.ctr_contents);
1413178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1414178576Sjb			break;
1415178576Sjb		case CTF_K_FUNCTION:
1416178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n");
1417178576Sjb			break;
1418178576Sjb		case CTF_K_STRUCT:
1419178576Sjb			cbdata.f_type = 1;
1420178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1421178576Sjb			    dt_print_type_width, &cbdata) != 0)
1422178576Sjb				return (-1);
1423178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1424178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1425178576Sjb			    dt_print_type_member, &cbdata) != 0)
1426178576Sjb				return (-1);
1427178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1428178576Sjb			break;
1429178576Sjb		case CTF_K_UNION:
1430178576Sjb			cbdata.f_type = 1;
1431178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1432178576Sjb			    dt_print_type_width, &cbdata) != 0)
1433178576Sjb				return (-1);
1434178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "{\n");
1435178576Sjb			if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type,
1436178576Sjb			    dt_print_type_member, &cbdata) != 0)
1437178576Sjb				return (-1);
1438178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,"");
1439178576Sjb			break;
1440178576Sjb		case CTF_K_ENUM:
1441178576Sjb			dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp)));
1442178576Sjb			break;
1443178576Sjb		case CTF_K_TYPEDEF:
1444178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1445178576Sjb			break;
1446178576Sjb		case CTF_K_VOLATILE:
1447178576Sjb			if (cbdatap->f_type)
1448178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "volatile ");
1449178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1450178576Sjb			break;
1451178576Sjb		case CTF_K_CONST:
1452178576Sjb			if (cbdatap->f_type)
1453178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "const ");
1454178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1455178576Sjb			break;
1456178576Sjb		case CTF_K_RESTRICT:
1457178576Sjb			if (cbdatap->f_type)
1458178576Sjb				dt_printf(cbdatap->dtp, cbdatap->fp, "restrict ");
1459178576Sjb			dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type));
1460178576Sjb			break;
1461178576Sjb		default:
1462178576Sjb			break;
1463178576Sjb		}
1464178576Sjb
1465178576Sjb		addr += ssz;
1466178576Sjb		cnt++;
1467178576Sjb	}
1468178576Sjb
1469178576Sjb	return (0);
1470178576Sjb}
1471178576Sjb
1472178576Sjbstatic int
1473178576Sjbdt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr)
1474178576Sjb{
1475178576Sjb	caddr_t addrend;
1476178576Sjb	char *p;
1477178576Sjb	dtrace_typeinfo_t dtt;
1478178576Sjb	dt_type_cbdata_t cbdata;
1479178576Sjb	int num = 0;
1480178576Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1481178576Sjb	ssize_t ssz;
1482178576Sjb
1483178576Sjb	if (!quiet)
1484178576Sjb		dt_printf(dtp, fp, "\n");
1485178576Sjb
1486178576Sjb	/* Get the total number of bytes of data buffered. */
1487178576Sjb	size_t nbytes = *((uintptr_t *) addr);
1488178576Sjb	addr += sizeof(uintptr_t);
1489178576Sjb
1490178576Sjb	/*
1491178576Sjb	 * Get the size of the type so that we can check that it matches
1492178576Sjb	 * the CTF data we look up and so that we can figure out how many
1493178576Sjb	 * type elements are buffered.
1494178576Sjb	 */
1495178576Sjb	size_t typs = *((uintptr_t *) addr);
1496178576Sjb	addr += sizeof(uintptr_t);
1497178576Sjb
1498178576Sjb	/*
1499178576Sjb	 * Point to the type string in the buffer. Get it's string
1500178576Sjb	 * length and round it up to become the offset to the start
1501178576Sjb	 * of the buffered type data which we would like to be aligned
1502178576Sjb	 * for easy access.
1503178576Sjb	 */
1504178576Sjb	char *strp = (char *) addr;
1505178576Sjb	int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t));
1506178576Sjb
1507178576Sjb	/*
1508178576Sjb	 * The type string might have a format such as 'int [20]'.
1509178576Sjb	 * Check if there is an array dimension present.
1510178576Sjb	 */
1511178576Sjb	if ((p = strchr(strp, '[')) != NULL) {
1512178576Sjb		/* Strip off the array dimension. */
1513178576Sjb		*p++ = '\0';
1514178576Sjb
1515178576Sjb		for (; *p != '\0' && *p != ']'; p++)
1516178576Sjb			num = num * 10 + *p - '0';
1517178576Sjb	} else
1518178576Sjb		/* No array dimension, so default. */
1519178576Sjb		num = 1;
1520178576Sjb
1521178576Sjb	/* Lookup the CTF type from the type string. */
1522178576Sjb	if (dtrace_lookup_by_type(dtp,  DTRACE_OBJ_EVERY, strp, &dtt) < 0)
1523178576Sjb		return (-1);
1524178576Sjb
1525178576Sjb	/* Offset the buffer address to the start of the data... */
1526178576Sjb	addr += offset;
1527178576Sjb
1528178576Sjb	ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type);
1529178576Sjb
1530178576Sjb	if (typs != ssz) {
1531178576Sjb		printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz);
1532178576Sjb		return (-1);
1533178576Sjb	}
1534178576Sjb
1535178576Sjb	cbdata.dtp = dtp;
1536178576Sjb	cbdata.dtt = dtt;
1537178576Sjb	cbdata.name = "";
1538178576Sjb	cbdata.addr = addr;
1539178576Sjb	cbdata.addrend = addr + nbytes;
1540178576Sjb	cbdata.indent = 1;
1541178576Sjb	cbdata.f_type = 1;
1542178576Sjb	cbdata.type_width = 0;
1543178576Sjb	cbdata.name_width = 0;
1544178576Sjb	cbdata.fp = fp;
1545178576Sjb
1546178576Sjb	return (dt_print_type_data(&cbdata, dtt.dtt_type));
1547178576Sjb}
1548178576Sjb
1549178576Sjbstatic int
1550178479Sjbdt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1551178479Sjb{
1552178479Sjb	/* LINTED - alignment */
1553178479Sjb	uint64_t pc = *((uint64_t *)addr);
1554178479Sjb	dtrace_syminfo_t dts;
1555178479Sjb	GElf_Sym sym;
1556178479Sjb	char c[PATH_MAX * 2];
1557178479Sjb
1558178479Sjb	if (format == NULL)
1559178479Sjb		format = "  %-50s";
1560178479Sjb
1561178479Sjb	if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1562178479Sjb		(void) snprintf(c, sizeof (c), "%s`%s",
1563178479Sjb		    dts.dts_object, dts.dts_name);
1564178479Sjb	} else {
1565178479Sjb		/*
1566178479Sjb		 * We'll repeat the lookup, but this time we'll specify a
1567178479Sjb		 * NULL GElf_Sym -- indicating that we're only interested in
1568178479Sjb		 * the containing module.
1569178479Sjb		 */
1570178479Sjb		if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1571178479Sjb			(void) snprintf(c, sizeof (c), "%s`0x%llx",
1572178479Sjb			    dts.dts_object, (u_longlong_t)pc);
1573178479Sjb		} else {
1574178479Sjb			(void) snprintf(c, sizeof (c), "0x%llx",
1575178479Sjb			    (u_longlong_t)pc);
1576178479Sjb		}
1577178479Sjb	}
1578178479Sjb
1579178479Sjb	if (dt_printf(dtp, fp, format, c) < 0)
1580178479Sjb		return (-1);
1581178479Sjb
1582178479Sjb	return (0);
1583178479Sjb}
1584178479Sjb
1585178479Sjbint
1586178479Sjbdt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1587178479Sjb{
1588178479Sjb	/* LINTED - alignment */
1589178479Sjb	uint64_t pc = *((uint64_t *)addr);
1590178479Sjb	dtrace_syminfo_t dts;
1591178479Sjb	char c[PATH_MAX * 2];
1592178479Sjb
1593178479Sjb	if (format == NULL)
1594178479Sjb		format = "  %-50s";
1595178479Sjb
1596178479Sjb	if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1597178479Sjb		(void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1598178479Sjb	} else {
1599178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1600178479Sjb	}
1601178479Sjb
1602178479Sjb	if (dt_printf(dtp, fp, format, c) < 0)
1603178479Sjb		return (-1);
1604178479Sjb
1605178479Sjb	return (0);
1606178479Sjb}
1607178479Sjb
1608178479Sjbtypedef struct dt_normal {
1609178479Sjb	dtrace_aggvarid_t dtnd_id;
1610178479Sjb	uint64_t dtnd_normal;
1611178479Sjb} dt_normal_t;
1612178479Sjb
1613178479Sjbstatic int
1614178479Sjbdt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1615178479Sjb{
1616178479Sjb	dt_normal_t *normal = arg;
1617178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1618178479Sjb	dtrace_aggvarid_t id = normal->dtnd_id;
1619178479Sjb
1620178479Sjb	if (agg->dtagd_nrecs == 0)
1621178479Sjb		return (DTRACE_AGGWALK_NEXT);
1622178479Sjb
1623178479Sjb	if (agg->dtagd_varid != id)
1624178479Sjb		return (DTRACE_AGGWALK_NEXT);
1625178479Sjb
1626178479Sjb	((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1627178479Sjb	return (DTRACE_AGGWALK_NORMALIZE);
1628178479Sjb}
1629178479Sjb
1630178479Sjbstatic int
1631178479Sjbdt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1632178479Sjb{
1633178479Sjb	dt_normal_t normal;
1634178479Sjb	caddr_t addr;
1635178479Sjb
1636178479Sjb	/*
1637178479Sjb	 * We (should) have two records:  the aggregation ID followed by the
1638178479Sjb	 * normalization value.
1639178479Sjb	 */
1640178479Sjb	addr = base + rec->dtrd_offset;
1641178479Sjb
1642178479Sjb	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1643178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1644178479Sjb
1645178479Sjb	/* LINTED - alignment */
1646178479Sjb	normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1647178479Sjb	rec++;
1648178479Sjb
1649178479Sjb	if (rec->dtrd_action != DTRACEACT_LIBACT)
1650178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1651178479Sjb
1652178479Sjb	if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1653178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1654178479Sjb
1655178479Sjb	addr = base + rec->dtrd_offset;
1656178479Sjb
1657178479Sjb	switch (rec->dtrd_size) {
1658178479Sjb	case sizeof (uint64_t):
1659178479Sjb		/* LINTED - alignment */
1660178479Sjb		normal.dtnd_normal = *((uint64_t *)addr);
1661178479Sjb		break;
1662178479Sjb	case sizeof (uint32_t):
1663178479Sjb		/* LINTED - alignment */
1664178479Sjb		normal.dtnd_normal = *((uint32_t *)addr);
1665178479Sjb		break;
1666178479Sjb	case sizeof (uint16_t):
1667178479Sjb		/* LINTED - alignment */
1668178479Sjb		normal.dtnd_normal = *((uint16_t *)addr);
1669178479Sjb		break;
1670178479Sjb	case sizeof (uint8_t):
1671178479Sjb		normal.dtnd_normal = *((uint8_t *)addr);
1672178479Sjb		break;
1673178479Sjb	default:
1674178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1675178479Sjb	}
1676178479Sjb
1677178479Sjb	(void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1678178479Sjb
1679178479Sjb	return (0);
1680178479Sjb}
1681178479Sjb
1682178479Sjbstatic int
1683178479Sjbdt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1684178479Sjb{
1685178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1686178479Sjb	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1687178479Sjb
1688178479Sjb	if (agg->dtagd_nrecs == 0)
1689178479Sjb		return (DTRACE_AGGWALK_NEXT);
1690178479Sjb
1691178479Sjb	if (agg->dtagd_varid != id)
1692178479Sjb		return (DTRACE_AGGWALK_NEXT);
1693178479Sjb
1694178479Sjb	return (DTRACE_AGGWALK_DENORMALIZE);
1695178479Sjb}
1696178479Sjb
1697178479Sjbstatic int
1698178479Sjbdt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1699178479Sjb{
1700178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1701178479Sjb	dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1702178479Sjb
1703178479Sjb	if (agg->dtagd_nrecs == 0)
1704178479Sjb		return (DTRACE_AGGWALK_NEXT);
1705178479Sjb
1706178479Sjb	if (agg->dtagd_varid != id)
1707178479Sjb		return (DTRACE_AGGWALK_NEXT);
1708178479Sjb
1709178479Sjb	return (DTRACE_AGGWALK_CLEAR);
1710178479Sjb}
1711178479Sjb
1712178479Sjbtypedef struct dt_trunc {
1713178479Sjb	dtrace_aggvarid_t dttd_id;
1714178479Sjb	uint64_t dttd_remaining;
1715178479Sjb} dt_trunc_t;
1716178479Sjb
1717178479Sjbstatic int
1718178479Sjbdt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
1719178479Sjb{
1720178479Sjb	dt_trunc_t *trunc = arg;
1721178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1722178479Sjb	dtrace_aggvarid_t id = trunc->dttd_id;
1723178479Sjb
1724178479Sjb	if (agg->dtagd_nrecs == 0)
1725178479Sjb		return (DTRACE_AGGWALK_NEXT);
1726178479Sjb
1727178479Sjb	if (agg->dtagd_varid != id)
1728178479Sjb		return (DTRACE_AGGWALK_NEXT);
1729178479Sjb
1730178479Sjb	if (trunc->dttd_remaining == 0)
1731178479Sjb		return (DTRACE_AGGWALK_REMOVE);
1732178479Sjb
1733178479Sjb	trunc->dttd_remaining--;
1734178479Sjb	return (DTRACE_AGGWALK_NEXT);
1735178479Sjb}
1736178479Sjb
1737178479Sjbstatic int
1738178479Sjbdt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1739178479Sjb{
1740178479Sjb	dt_trunc_t trunc;
1741178479Sjb	caddr_t addr;
1742178479Sjb	int64_t remaining;
1743178479Sjb	int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
1744178479Sjb
1745178479Sjb	/*
1746178479Sjb	 * We (should) have two records:  the aggregation ID followed by the
1747178479Sjb	 * number of aggregation entries after which the aggregation is to be
1748178479Sjb	 * truncated.
1749178479Sjb	 */
1750178479Sjb	addr = base + rec->dtrd_offset;
1751178479Sjb
1752178479Sjb	if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1753178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
1754178479Sjb
1755178479Sjb	/* LINTED - alignment */
1756178479Sjb	trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
1757178479Sjb	rec++;
1758178479Sjb
1759178479Sjb	if (rec->dtrd_action != DTRACEACT_LIBACT)
1760178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
1761178479Sjb
1762178479Sjb	if (rec->dtrd_arg != DT_ACT_TRUNC)
1763178479Sjb		return (dt_set_errno(dtp, EDT_BADTRUNC));
1764178479Sjb
1765178479Sjb	addr = base + rec->dtrd_offset;
1766178479Sjb
1767178479Sjb	switch (rec->dtrd_size) {
1768178479Sjb	case sizeof (uint64_t):
1769178479Sjb		/* LINTED - alignment */
1770178479Sjb		remaining = *((int64_t *)addr);
1771178479Sjb		break;
1772178479Sjb	case sizeof (uint32_t):
1773178479Sjb		/* LINTED - alignment */
1774178479Sjb		remaining = *((int32_t *)addr);
1775178479Sjb		break;
1776178479Sjb	case sizeof (uint16_t):
1777178479Sjb		/* LINTED - alignment */
1778178479Sjb		remaining = *((int16_t *)addr);
1779178479Sjb		break;
1780178479Sjb	case sizeof (uint8_t):
1781178479Sjb		remaining = *((int8_t *)addr);
1782178479Sjb		break;
1783178479Sjb	default:
1784178479Sjb		return (dt_set_errno(dtp, EDT_BADNORMAL));
1785178479Sjb	}
1786178479Sjb
1787178479Sjb	if (remaining < 0) {
1788178479Sjb		func = dtrace_aggregate_walk_valsorted;
1789178479Sjb		remaining = -remaining;
1790178479Sjb	} else {
1791178479Sjb		func = dtrace_aggregate_walk_valrevsorted;
1792178479Sjb	}
1793178479Sjb
1794178479Sjb	assert(remaining >= 0);
1795178479Sjb	trunc.dttd_remaining = remaining;
1796178479Sjb
1797178479Sjb	(void) func(dtp, dt_trunc_agg, &trunc);
1798178479Sjb
1799178479Sjb	return (0);
1800178479Sjb}
1801178479Sjb
1802178479Sjbstatic int
1803178479Sjbdt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
1804178479Sjb    caddr_t addr, size_t size, uint64_t normal)
1805178479Sjb{
1806178479Sjb	int err;
1807178479Sjb	dtrace_actkind_t act = rec->dtrd_action;
1808178479Sjb
1809178479Sjb	switch (act) {
1810178479Sjb	case DTRACEACT_STACK:
1811178479Sjb		return (dt_print_stack(dtp, fp, NULL, addr,
1812178479Sjb		    rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
1813178479Sjb
1814178479Sjb	case DTRACEACT_USTACK:
1815178479Sjb	case DTRACEACT_JSTACK:
1816178479Sjb		return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
1817178479Sjb
1818178479Sjb	case DTRACEACT_USYM:
1819178479Sjb	case DTRACEACT_UADDR:
1820178479Sjb		return (dt_print_usym(dtp, fp, addr, act));
1821178479Sjb
1822178479Sjb	case DTRACEACT_UMOD:
1823178479Sjb		return (dt_print_umod(dtp, fp, NULL, addr));
1824178479Sjb
1825178479Sjb	case DTRACEACT_SYM:
1826178479Sjb		return (dt_print_sym(dtp, fp, NULL, addr));
1827178479Sjb
1828178479Sjb	case DTRACEACT_MOD:
1829178479Sjb		return (dt_print_mod(dtp, fp, NULL, addr));
1830178479Sjb
1831178479Sjb	case DTRACEAGG_QUANTIZE:
1832178479Sjb		return (dt_print_quantize(dtp, fp, addr, size, normal));
1833178479Sjb
1834178479Sjb	case DTRACEAGG_LQUANTIZE:
1835178479Sjb		return (dt_print_lquantize(dtp, fp, addr, size, normal));
1836178479Sjb
1837237624Spfg	case DTRACEAGG_LLQUANTIZE:
1838237624Spfg		return (dt_print_llquantize(dtp, fp, addr, size, normal));
1839237624Spfg
1840178479Sjb	case DTRACEAGG_AVG:
1841178479Sjb		return (dt_print_average(dtp, fp, addr, size, normal));
1842178479Sjb
1843178479Sjb	case DTRACEAGG_STDDEV:
1844178479Sjb		return (dt_print_stddev(dtp, fp, addr, size, normal));
1845178479Sjb
1846178479Sjb	default:
1847178479Sjb		break;
1848178479Sjb	}
1849178479Sjb
1850178479Sjb	switch (size) {
1851178479Sjb	case sizeof (uint64_t):
1852178479Sjb		err = dt_printf(dtp, fp, " %16lld",
1853178479Sjb		    /* LINTED - alignment */
1854178479Sjb		    (long long)*((uint64_t *)addr) / normal);
1855178479Sjb		break;
1856178479Sjb	case sizeof (uint32_t):
1857178479Sjb		/* LINTED - alignment */
1858178479Sjb		err = dt_printf(dtp, fp, " %8d", *((uint32_t *)addr) /
1859178479Sjb		    (uint32_t)normal);
1860178479Sjb		break;
1861178479Sjb	case sizeof (uint16_t):
1862178479Sjb		/* LINTED - alignment */
1863178479Sjb		err = dt_printf(dtp, fp, " %5d", *((uint16_t *)addr) /
1864178479Sjb		    (uint32_t)normal);
1865178479Sjb		break;
1866178479Sjb	case sizeof (uint8_t):
1867178479Sjb		err = dt_printf(dtp, fp, " %3d", *((uint8_t *)addr) /
1868178479Sjb		    (uint32_t)normal);
1869178479Sjb		break;
1870178479Sjb	default:
1871178576Sjb		err = dt_print_bytes(dtp, fp, addr, size, 50, 0, 0);
1872178479Sjb		break;
1873178479Sjb	}
1874178479Sjb
1875178479Sjb	return (err);
1876178479Sjb}
1877178479Sjb
1878178479Sjbint
1879178479Sjbdt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1880178479Sjb{
1881178479Sjb	int i, aggact = 0;
1882178479Sjb	dt_print_aggdata_t *pd = arg;
1883178479Sjb	const dtrace_aggdata_t *aggdata = aggsdata[0];
1884178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1885178479Sjb	FILE *fp = pd->dtpa_fp;
1886178479Sjb	dtrace_hdl_t *dtp = pd->dtpa_dtp;
1887178479Sjb	dtrace_recdesc_t *rec;
1888178479Sjb	dtrace_actkind_t act;
1889178479Sjb	caddr_t addr;
1890178479Sjb	size_t size;
1891178479Sjb
1892178479Sjb	/*
1893178479Sjb	 * Iterate over each record description in the key, printing the traced
1894178479Sjb	 * data, skipping the first datum (the tuple member created by the
1895178479Sjb	 * compiler).
1896178479Sjb	 */
1897178479Sjb	for (i = 1; i < agg->dtagd_nrecs; i++) {
1898178479Sjb		rec = &agg->dtagd_rec[i];
1899178479Sjb		act = rec->dtrd_action;
1900178479Sjb		addr = aggdata->dtada_data + rec->dtrd_offset;
1901178479Sjb		size = rec->dtrd_size;
1902178479Sjb
1903178479Sjb		if (DTRACEACT_ISAGG(act)) {
1904178479Sjb			aggact = i;
1905178479Sjb			break;
1906178479Sjb		}
1907178479Sjb
1908178479Sjb		if (dt_print_datum(dtp, fp, rec, addr, size, 1) < 0)
1909178479Sjb			return (-1);
1910178479Sjb
1911178479Sjb		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1912178479Sjb		    DTRACE_BUFDATA_AGGKEY) < 0)
1913178479Sjb			return (-1);
1914178479Sjb	}
1915178479Sjb
1916178479Sjb	assert(aggact != 0);
1917178479Sjb
1918178479Sjb	for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
1919178479Sjb		uint64_t normal;
1920178479Sjb
1921178479Sjb		aggdata = aggsdata[i];
1922178479Sjb		agg = aggdata->dtada_desc;
1923178479Sjb		rec = &agg->dtagd_rec[aggact];
1924178479Sjb		act = rec->dtrd_action;
1925178479Sjb		addr = aggdata->dtada_data + rec->dtrd_offset;
1926178479Sjb		size = rec->dtrd_size;
1927178479Sjb
1928178479Sjb		assert(DTRACEACT_ISAGG(act));
1929178479Sjb		normal = aggdata->dtada_normal;
1930178479Sjb
1931178479Sjb		if (dt_print_datum(dtp, fp, rec, addr, size, normal) < 0)
1932178479Sjb			return (-1);
1933178479Sjb
1934178479Sjb		if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1935178479Sjb		    DTRACE_BUFDATA_AGGVAL) < 0)
1936178479Sjb			return (-1);
1937178479Sjb
1938178479Sjb		if (!pd->dtpa_allunprint)
1939178479Sjb			agg->dtagd_flags |= DTRACE_AGD_PRINTED;
1940178479Sjb	}
1941178479Sjb
1942178479Sjb	if (dt_printf(dtp, fp, "\n") < 0)
1943178479Sjb		return (-1);
1944178479Sjb
1945178479Sjb	if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
1946178479Sjb	    DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
1947178479Sjb		return (-1);
1948178479Sjb
1949178479Sjb	return (0);
1950178479Sjb}
1951178479Sjb
1952178479Sjbint
1953178479Sjbdt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
1954178479Sjb{
1955178479Sjb	dt_print_aggdata_t *pd = arg;
1956178479Sjb	dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1957178479Sjb	dtrace_aggvarid_t aggvarid = pd->dtpa_id;
1958178479Sjb
1959178479Sjb	if (pd->dtpa_allunprint) {
1960178479Sjb		if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
1961178479Sjb			return (0);
1962178479Sjb	} else {
1963178479Sjb		/*
1964178479Sjb		 * If we're not printing all unprinted aggregations, then the
1965178479Sjb		 * aggregation variable ID denotes a specific aggregation
1966178479Sjb		 * variable that we should print -- skip any other aggregations
1967178479Sjb		 * that we encounter.
1968178479Sjb		 */
1969178479Sjb		if (agg->dtagd_nrecs == 0)
1970178479Sjb			return (0);
1971178479Sjb
1972178479Sjb		if (aggvarid != agg->dtagd_varid)
1973178479Sjb			return (0);
1974178479Sjb	}
1975178479Sjb
1976178479Sjb	return (dt_print_aggs(&aggdata, 1, arg));
1977178479Sjb}
1978178479Sjb
1979178479Sjbint
1980178479Sjbdt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
1981178479Sjb    const char *option, const char *value)
1982178479Sjb{
1983178479Sjb	int len, rval;
1984178479Sjb	char *msg;
1985178479Sjb	const char *errstr;
1986178479Sjb	dtrace_setoptdata_t optdata;
1987178479Sjb
1988178479Sjb	bzero(&optdata, sizeof (optdata));
1989178479Sjb	(void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
1990178479Sjb
1991178479Sjb	if (dtrace_setopt(dtp, option, value) == 0) {
1992178479Sjb		(void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
1993178479Sjb		optdata.dtsda_probe = data;
1994178479Sjb		optdata.dtsda_option = option;
1995178479Sjb		optdata.dtsda_handle = dtp;
1996178479Sjb
1997178479Sjb		if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
1998178479Sjb			return (rval);
1999178479Sjb
2000178479Sjb		return (0);
2001178479Sjb	}
2002178479Sjb
2003178479Sjb	errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
2004178479Sjb	len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2005178479Sjb	msg = alloca(len);
2006178479Sjb
2007178479Sjb	(void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2008178479Sjb	    option, value, errstr);
2009178479Sjb
2010178479Sjb	if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2011178479Sjb		return (0);
2012178479Sjb
2013178479Sjb	return (rval);
2014178479Sjb}
2015178479Sjb
2016178479Sjbstatic int
2017178479Sjbdt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu, dtrace_bufdesc_t *buf,
2018178479Sjb    dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2019178479Sjb{
2020178479Sjb	dtrace_epid_t id;
2021178479Sjb	size_t offs, start = buf->dtbd_oldest, end = buf->dtbd_size;
2022178479Sjb	int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2023178479Sjb	int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2024178479Sjb	int rval, i, n;
2025178479Sjb	dtrace_epid_t last = DTRACE_EPIDNONE;
2026248690Spfg	uint64_t tracememsize = 0;
2027178479Sjb	dtrace_probedata_t data;
2028178479Sjb	uint64_t drops;
2029178479Sjb	caddr_t addr;
2030178479Sjb
2031178479Sjb	bzero(&data, sizeof (data));
2032178479Sjb	data.dtpda_handle = dtp;
2033178479Sjb	data.dtpda_cpu = cpu;
2034178479Sjb
2035178479Sjbagain:
2036178479Sjb	for (offs = start; offs < end; ) {
2037178479Sjb		dtrace_eprobedesc_t *epd;
2038178479Sjb
2039178479Sjb		/*
2040178479Sjb		 * We're guaranteed to have an ID.
2041178479Sjb		 */
2042178479Sjb		id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2043178479Sjb
2044178479Sjb		if (id == DTRACE_EPIDNONE) {
2045178479Sjb			/*
2046178479Sjb			 * This is filler to assure proper alignment of the
2047178479Sjb			 * next record; we simply ignore it.
2048178479Sjb			 */
2049178479Sjb			offs += sizeof (id);
2050178479Sjb			continue;
2051178479Sjb		}
2052178479Sjb
2053178479Sjb		if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2054178479Sjb		    &data.dtpda_pdesc)) != 0)
2055178479Sjb			return (rval);
2056178479Sjb
2057178479Sjb		epd = data.dtpda_edesc;
2058178479Sjb		data.dtpda_data = buf->dtbd_data + offs;
2059178479Sjb
2060178479Sjb		if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2061178479Sjb			rval = dt_handle(dtp, &data);
2062178479Sjb
2063178479Sjb			if (rval == DTRACE_CONSUME_NEXT)
2064178479Sjb				goto nextepid;
2065178479Sjb
2066178479Sjb			if (rval == DTRACE_CONSUME_ERROR)
2067178479Sjb				return (-1);
2068178479Sjb		}
2069178479Sjb
2070178479Sjb		if (flow)
2071178479Sjb			(void) dt_flowindent(dtp, &data, last, buf, offs);
2072178479Sjb
2073178479Sjb		rval = (*efunc)(&data, arg);
2074178479Sjb
2075178479Sjb		if (flow) {
2076178479Sjb			if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2077178479Sjb				data.dtpda_indent += 2;
2078178479Sjb		}
2079178479Sjb
2080178479Sjb		if (rval == DTRACE_CONSUME_NEXT)
2081178479Sjb			goto nextepid;
2082178479Sjb
2083178479Sjb		if (rval == DTRACE_CONSUME_ABORT)
2084178479Sjb			return (dt_set_errno(dtp, EDT_DIRABORT));
2085178479Sjb
2086178479Sjb		if (rval != DTRACE_CONSUME_THIS)
2087178479Sjb			return (dt_set_errno(dtp, EDT_BADRVAL));
2088178479Sjb
2089178479Sjb		for (i = 0; i < epd->dtepd_nrecs; i++) {
2090178479Sjb			dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2091178479Sjb			dtrace_actkind_t act = rec->dtrd_action;
2092178479Sjb
2093178479Sjb			data.dtpda_data = buf->dtbd_data + offs +
2094178479Sjb			    rec->dtrd_offset;
2095178479Sjb			addr = data.dtpda_data;
2096178479Sjb
2097178479Sjb			if (act == DTRACEACT_LIBACT) {
2098178479Sjb				uint64_t arg = rec->dtrd_arg;
2099178479Sjb				dtrace_aggvarid_t id;
2100178479Sjb
2101178479Sjb				switch (arg) {
2102178479Sjb				case DT_ACT_CLEAR:
2103178479Sjb					/* LINTED - alignment */
2104178479Sjb					id = *((dtrace_aggvarid_t *)addr);
2105178479Sjb					(void) dtrace_aggregate_walk(dtp,
2106178479Sjb					    dt_clear_agg, &id);
2107178479Sjb					continue;
2108178479Sjb
2109178479Sjb				case DT_ACT_DENORMALIZE:
2110178479Sjb					/* LINTED - alignment */
2111178479Sjb					id = *((dtrace_aggvarid_t *)addr);
2112178479Sjb					(void) dtrace_aggregate_walk(dtp,
2113178479Sjb					    dt_denormalize_agg, &id);
2114178479Sjb					continue;
2115178479Sjb
2116178479Sjb				case DT_ACT_FTRUNCATE:
2117178479Sjb					if (fp == NULL)
2118178479Sjb						continue;
2119178479Sjb
2120178479Sjb					(void) fflush(fp);
2121178479Sjb					(void) ftruncate(fileno(fp), 0);
2122178479Sjb					(void) fseeko(fp, 0, SEEK_SET);
2123178479Sjb					continue;
2124178479Sjb
2125178479Sjb				case DT_ACT_NORMALIZE:
2126178479Sjb					if (i == epd->dtepd_nrecs - 1)
2127178479Sjb						return (dt_set_errno(dtp,
2128178479Sjb						    EDT_BADNORMAL));
2129178479Sjb
2130178479Sjb					if (dt_normalize(dtp,
2131178479Sjb					    buf->dtbd_data + offs, rec) != 0)
2132178479Sjb						return (-1);
2133178479Sjb
2134178479Sjb					i++;
2135178479Sjb					continue;
2136178479Sjb
2137178479Sjb				case DT_ACT_SETOPT: {
2138178479Sjb					uint64_t *opts = dtp->dt_options;
2139178479Sjb					dtrace_recdesc_t *valrec;
2140178479Sjb					uint32_t valsize;
2141178479Sjb					caddr_t val;
2142178479Sjb					int rv;
2143178479Sjb
2144178479Sjb					if (i == epd->dtepd_nrecs - 1) {
2145178479Sjb						return (dt_set_errno(dtp,
2146178479Sjb						    EDT_BADSETOPT));
2147178479Sjb					}
2148178479Sjb
2149178479Sjb					valrec = &epd->dtepd_rec[++i];
2150178479Sjb					valsize = valrec->dtrd_size;
2151178479Sjb
2152178479Sjb					if (valrec->dtrd_action != act ||
2153178479Sjb					    valrec->dtrd_arg != arg) {
2154178479Sjb						return (dt_set_errno(dtp,
2155178479Sjb						    EDT_BADSETOPT));
2156178479Sjb					}
2157178479Sjb
2158178479Sjb					if (valsize > sizeof (uint64_t)) {
2159178479Sjb						val = buf->dtbd_data + offs +
2160178479Sjb						    valrec->dtrd_offset;
2161178479Sjb					} else {
2162178479Sjb						val = "1";
2163178479Sjb					}
2164178479Sjb
2165178479Sjb					rv = dt_setopt(dtp, &data, addr, val);
2166178479Sjb
2167178479Sjb					if (rv != 0)
2168178479Sjb						return (-1);
2169178479Sjb
2170178479Sjb					flow = (opts[DTRACEOPT_FLOWINDENT] !=
2171178479Sjb					    DTRACEOPT_UNSET);
2172178479Sjb					quiet = (opts[DTRACEOPT_QUIET] !=
2173178479Sjb					    DTRACEOPT_UNSET);
2174178479Sjb
2175178479Sjb					continue;
2176178479Sjb				}
2177178479Sjb
2178178479Sjb				case DT_ACT_TRUNC:
2179178479Sjb					if (i == epd->dtepd_nrecs - 1)
2180178479Sjb						return (dt_set_errno(dtp,
2181178479Sjb						    EDT_BADTRUNC));
2182178479Sjb
2183178479Sjb					if (dt_trunc(dtp,
2184178479Sjb					    buf->dtbd_data + offs, rec) != 0)
2185178479Sjb						return (-1);
2186178479Sjb
2187178479Sjb					i++;
2188178479Sjb					continue;
2189178479Sjb
2190178479Sjb				default:
2191178479Sjb					continue;
2192178479Sjb				}
2193178479Sjb			}
2194178479Sjb
2195248690Spfg			if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
2196248690Spfg			    rec->dtrd_size == sizeof (uint64_t)) {
2197248708Spfg			    	/* LINTED - alignment */
2198248690Spfg				tracememsize = *((unsigned long long *)addr);
2199248690Spfg				continue;
2200248690Spfg			}
2201248690Spfg
2202178479Sjb			rval = (*rfunc)(&data, rec, arg);
2203178479Sjb
2204178479Sjb			if (rval == DTRACE_CONSUME_NEXT)
2205178479Sjb				continue;
2206178479Sjb
2207178479Sjb			if (rval == DTRACE_CONSUME_ABORT)
2208178479Sjb				return (dt_set_errno(dtp, EDT_DIRABORT));
2209178479Sjb
2210178479Sjb			if (rval != DTRACE_CONSUME_THIS)
2211178479Sjb				return (dt_set_errno(dtp, EDT_BADRVAL));
2212178479Sjb
2213178479Sjb			if (act == DTRACEACT_STACK) {
2214178479Sjb				int depth = rec->dtrd_arg;
2215178479Sjb
2216178479Sjb				if (dt_print_stack(dtp, fp, NULL, addr, depth,
2217178479Sjb				    rec->dtrd_size / depth) < 0)
2218178479Sjb					return (-1);
2219178479Sjb				goto nextrec;
2220178479Sjb			}
2221178479Sjb
2222178479Sjb			if (act == DTRACEACT_USTACK ||
2223178479Sjb			    act == DTRACEACT_JSTACK) {
2224178479Sjb				if (dt_print_ustack(dtp, fp, NULL,
2225178479Sjb				    addr, rec->dtrd_arg) < 0)
2226178479Sjb					return (-1);
2227178479Sjb				goto nextrec;
2228178479Sjb			}
2229178479Sjb
2230178479Sjb			if (act == DTRACEACT_SYM) {
2231178479Sjb				if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2232178479Sjb					return (-1);
2233178479Sjb				goto nextrec;
2234178479Sjb			}
2235178479Sjb
2236178479Sjb			if (act == DTRACEACT_MOD) {
2237178479Sjb				if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2238178479Sjb					return (-1);
2239178479Sjb				goto nextrec;
2240178479Sjb			}
2241178479Sjb
2242178479Sjb			if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2243178479Sjb				if (dt_print_usym(dtp, fp, addr, act) < 0)
2244178479Sjb					return (-1);
2245178479Sjb				goto nextrec;
2246178479Sjb			}
2247178479Sjb
2248178479Sjb			if (act == DTRACEACT_UMOD) {
2249178479Sjb				if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2250178479Sjb					return (-1);
2251178479Sjb				goto nextrec;
2252178479Sjb			}
2253178479Sjb
2254178576Sjb			if (act == DTRACEACT_PRINTM) {
2255178576Sjb				if (dt_print_memory(dtp, fp, addr) < 0)
2256178576Sjb					return (-1);
2257178576Sjb				goto nextrec;
2258178576Sjb			}
2259178576Sjb
2260178576Sjb			if (act == DTRACEACT_PRINTT) {
2261178576Sjb				if (dt_print_type(dtp, fp, addr) < 0)
2262178576Sjb					return (-1);
2263178576Sjb				goto nextrec;
2264178576Sjb			}
2265178576Sjb
2266178479Sjb			if (DTRACEACT_ISPRINTFLIKE(act)) {
2267178479Sjb				void *fmtdata;
2268178479Sjb				int (*func)(dtrace_hdl_t *, FILE *, void *,
2269178479Sjb				    const dtrace_probedata_t *,
2270178479Sjb				    const dtrace_recdesc_t *, uint_t,
2271178479Sjb				    const void *buf, size_t);
2272178479Sjb
2273178479Sjb				if ((fmtdata = dt_format_lookup(dtp,
2274178479Sjb				    rec->dtrd_format)) == NULL)
2275178479Sjb					goto nofmt;
2276178479Sjb
2277178479Sjb				switch (act) {
2278178479Sjb				case DTRACEACT_PRINTF:
2279178479Sjb					func = dtrace_fprintf;
2280178479Sjb					break;
2281178479Sjb				case DTRACEACT_PRINTA:
2282178479Sjb					func = dtrace_fprinta;
2283178479Sjb					break;
2284178479Sjb				case DTRACEACT_SYSTEM:
2285178479Sjb					func = dtrace_system;
2286178479Sjb					break;
2287178479Sjb				case DTRACEACT_FREOPEN:
2288178479Sjb					func = dtrace_freopen;
2289178479Sjb					break;
2290178479Sjb				}
2291178479Sjb
2292178479Sjb				n = (*func)(dtp, fp, fmtdata, &data,
2293178479Sjb				    rec, epd->dtepd_nrecs - i,
2294178479Sjb				    (uchar_t *)buf->dtbd_data + offs,
2295178479Sjb				    buf->dtbd_size - offs);
2296178479Sjb
2297178479Sjb				if (n < 0)
2298178479Sjb					return (-1); /* errno is set for us */
2299178479Sjb
2300178479Sjb				if (n > 0)
2301178479Sjb					i += n - 1;
2302178479Sjb				goto nextrec;
2303178479Sjb			}
2304178479Sjb
2305248708Spfg			/*
2306248708Spfg			 * If this is a DIF expression, and the record has a
2307248708Spfg			 * format set, this indicates we have a CTF type name
2308248708Spfg			 * associated with the data and we should try to print
2309248708Spfg			 * it out by type.
2310248708Spfg			 */
2311248708Spfg			if (act == DTRACEACT_DIFEXPR) {
2312248708Spfg				const char *strdata = dt_strdata_lookup(dtp,
2313248708Spfg				    rec->dtrd_format);
2314248708Spfg				if (strdata != NULL) {
2315248708Spfg					n = dtrace_print(dtp, fp, strdata,
2316248708Spfg					    addr, rec->dtrd_size);
2317248708Spfg
2318248708Spfg					/*
2319248708Spfg					 * dtrace_print() will return -1 on
2320248708Spfg					 * error, or return the number of bytes
2321248708Spfg					 * consumed.  It will return 0 if the
2322248708Spfg					 * type couldn't be determined, and we
2323248708Spfg					 * should fall through to the normal
2324248708Spfg					 * trace method.
2325248708Spfg					 */
2326248708Spfg					if (n < 0)
2327248708Spfg						return (-1);
2328248708Spfg
2329248708Spfg					if (n > 0)
2330248708Spfg						goto nextrec;
2331248708Spfg				}
2332248708Spfg			}
2333248708Spfg
2334178479Sjbnofmt:
2335178479Sjb			if (act == DTRACEACT_PRINTA) {
2336178479Sjb				dt_print_aggdata_t pd;
2337178479Sjb				dtrace_aggvarid_t *aggvars;
2338178479Sjb				int j, naggvars = 0;
2339178479Sjb				size_t size = ((epd->dtepd_nrecs - i) *
2340178479Sjb				    sizeof (dtrace_aggvarid_t));
2341178479Sjb
2342178479Sjb				if ((aggvars = dt_alloc(dtp, size)) == NULL)
2343178479Sjb					return (-1);
2344178479Sjb
2345178479Sjb				/*
2346178479Sjb				 * This might be a printa() with multiple
2347178479Sjb				 * aggregation variables.  We need to scan
2348178479Sjb				 * forward through the records until we find
2349178479Sjb				 * a record from a different statement.
2350178479Sjb				 */
2351178479Sjb				for (j = i; j < epd->dtepd_nrecs; j++) {
2352178479Sjb					dtrace_recdesc_t *nrec;
2353178479Sjb					caddr_t naddr;
2354178479Sjb
2355178479Sjb					nrec = &epd->dtepd_rec[j];
2356178479Sjb
2357178479Sjb					if (nrec->dtrd_uarg != rec->dtrd_uarg)
2358178479Sjb						break;
2359178479Sjb
2360178479Sjb					if (nrec->dtrd_action != act) {
2361178479Sjb						return (dt_set_errno(dtp,
2362178479Sjb						    EDT_BADAGG));
2363178479Sjb					}
2364178479Sjb
2365178479Sjb					naddr = buf->dtbd_data + offs +
2366178479Sjb					    nrec->dtrd_offset;
2367178479Sjb
2368178479Sjb					aggvars[naggvars++] =
2369178479Sjb					    /* LINTED - alignment */
2370178479Sjb					    *((dtrace_aggvarid_t *)naddr);
2371178479Sjb				}
2372178479Sjb
2373178479Sjb				i = j - 1;
2374178479Sjb				bzero(&pd, sizeof (pd));
2375178479Sjb				pd.dtpa_dtp = dtp;
2376178479Sjb				pd.dtpa_fp = fp;
2377178479Sjb
2378178479Sjb				assert(naggvars >= 1);
2379178479Sjb
2380178479Sjb				if (naggvars == 1) {
2381178479Sjb					pd.dtpa_id = aggvars[0];
2382178479Sjb					dt_free(dtp, aggvars);
2383178479Sjb
2384178479Sjb					if (dt_printf(dtp, fp, "\n") < 0 ||
2385178479Sjb					    dtrace_aggregate_walk_sorted(dtp,
2386178479Sjb					    dt_print_agg, &pd) < 0)
2387178479Sjb						return (-1);
2388178479Sjb					goto nextrec;
2389178479Sjb				}
2390178479Sjb
2391178479Sjb				if (dt_printf(dtp, fp, "\n") < 0 ||
2392178479Sjb				    dtrace_aggregate_walk_joined(dtp, aggvars,
2393178479Sjb				    naggvars, dt_print_aggs, &pd) < 0) {
2394178479Sjb					dt_free(dtp, aggvars);
2395178479Sjb					return (-1);
2396178479Sjb				}
2397178479Sjb
2398178479Sjb				dt_free(dtp, aggvars);
2399178479Sjb				goto nextrec;
2400178479Sjb			}
2401178479Sjb
2402248690Spfg			if (act == DTRACEACT_TRACEMEM) {
2403248690Spfg				if (tracememsize == 0 ||
2404248690Spfg				    tracememsize > rec->dtrd_size) {
2405248690Spfg					tracememsize = rec->dtrd_size;
2406248690Spfg				}
2407248690Spfg
2408248690Spfg				n = dt_print_bytes(dtp, fp, addr,
2409248690Spfg				    tracememsize, 33, quiet, 1);
2410248690Spfg
2411248690Spfg				tracememsize = 0;
2412248690Spfg
2413248690Spfg				if (n < 0)
2414248690Spfg					return (-1);
2415248690Spfg
2416248690Spfg				goto nextrec;
2417248690Spfg			}
2418248690Spfg
2419178479Sjb			switch (rec->dtrd_size) {
2420178479Sjb			case sizeof (uint64_t):
2421178479Sjb				n = dt_printf(dtp, fp,
2422178479Sjb				    quiet ? "%lld" : " %16lld",
2423178479Sjb				    /* LINTED - alignment */
2424178479Sjb				    *((unsigned long long *)addr));
2425178479Sjb				break;
2426178479Sjb			case sizeof (uint32_t):
2427178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2428178479Sjb				    /* LINTED - alignment */
2429178479Sjb				    *((uint32_t *)addr));
2430178479Sjb				break;
2431178479Sjb			case sizeof (uint16_t):
2432178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2433178479Sjb				    /* LINTED - alignment */
2434178479Sjb				    *((uint16_t *)addr));
2435178479Sjb				break;
2436178479Sjb			case sizeof (uint8_t):
2437178479Sjb				n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2438178479Sjb				    *((uint8_t *)addr));
2439178479Sjb				break;
2440178479Sjb			default:
2441178479Sjb				n = dt_print_bytes(dtp, fp, addr,
2442178576Sjb				    rec->dtrd_size, 33, quiet, 0);
2443178479Sjb				break;
2444178479Sjb			}
2445178479Sjb
2446178479Sjb			if (n < 0)
2447178479Sjb				return (-1); /* errno is set for us */
2448178479Sjb
2449178479Sjbnextrec:
2450178479Sjb			if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2451178479Sjb				return (-1); /* errno is set for us */
2452178479Sjb		}
2453178479Sjb
2454178479Sjb		/*
2455178479Sjb		 * Call the record callback with a NULL record to indicate
2456178479Sjb		 * that we're done processing this EPID.
2457178479Sjb		 */
2458178479Sjb		rval = (*rfunc)(&data, NULL, arg);
2459178479Sjbnextepid:
2460178479Sjb		offs += epd->dtepd_size;
2461178479Sjb		last = id;
2462178479Sjb	}
2463178479Sjb
2464178479Sjb	if (buf->dtbd_oldest != 0 && start == buf->dtbd_oldest) {
2465178479Sjb		end = buf->dtbd_oldest;
2466178479Sjb		start = 0;
2467178479Sjb		goto again;
2468178479Sjb	}
2469178479Sjb
2470178479Sjb	if ((drops = buf->dtbd_drops) == 0)
2471178479Sjb		return (0);
2472178479Sjb
2473178479Sjb	/*
2474178479Sjb	 * Explicitly zero the drops to prevent us from processing them again.
2475178479Sjb	 */
2476178479Sjb	buf->dtbd_drops = 0;
2477178479Sjb
2478178479Sjb	return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2479178479Sjb}
2480178479Sjb
2481178479Sjbtypedef struct dt_begin {
2482178479Sjb	dtrace_consume_probe_f *dtbgn_probefunc;
2483178479Sjb	dtrace_consume_rec_f *dtbgn_recfunc;
2484178479Sjb	void *dtbgn_arg;
2485178479Sjb	dtrace_handle_err_f *dtbgn_errhdlr;
2486178479Sjb	void *dtbgn_errarg;
2487178479Sjb	int dtbgn_beginonly;
2488178479Sjb} dt_begin_t;
2489178479Sjb
2490178479Sjbstatic int
2491178479Sjbdt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
2492178479Sjb{
2493178479Sjb	dt_begin_t *begin = (dt_begin_t *)arg;
2494178479Sjb	dtrace_probedesc_t *pd = data->dtpda_pdesc;
2495178479Sjb
2496178479Sjb	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2497178479Sjb	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2498178479Sjb
2499178479Sjb	if (begin->dtbgn_beginonly) {
2500178479Sjb		if (!(r1 && r2))
2501178479Sjb			return (DTRACE_CONSUME_NEXT);
2502178479Sjb	} else {
2503178479Sjb		if (r1 && r2)
2504178479Sjb			return (DTRACE_CONSUME_NEXT);
2505178479Sjb	}
2506178479Sjb
2507178479Sjb	/*
2508178479Sjb	 * We have a record that we're interested in.  Now call the underlying
2509178479Sjb	 * probe function...
2510178479Sjb	 */
2511178479Sjb	return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
2512178479Sjb}
2513178479Sjb
2514178479Sjbstatic int
2515178479Sjbdt_consume_begin_record(const dtrace_probedata_t *data,
2516178479Sjb    const dtrace_recdesc_t *rec, void *arg)
2517178479Sjb{
2518178479Sjb	dt_begin_t *begin = (dt_begin_t *)arg;
2519178479Sjb
2520178479Sjb	return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
2521178479Sjb}
2522178479Sjb
2523178479Sjbstatic int
2524178479Sjbdt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
2525178479Sjb{
2526178479Sjb	dt_begin_t *begin = (dt_begin_t *)arg;
2527178479Sjb	dtrace_probedesc_t *pd = data->dteda_pdesc;
2528178479Sjb
2529178479Sjb	int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2530178479Sjb	int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2531178479Sjb
2532178479Sjb	if (begin->dtbgn_beginonly) {
2533178479Sjb		if (!(r1 && r2))
2534178479Sjb			return (DTRACE_HANDLE_OK);
2535178479Sjb	} else {
2536178479Sjb		if (r1 && r2)
2537178479Sjb			return (DTRACE_HANDLE_OK);
2538178479Sjb	}
2539178479Sjb
2540178479Sjb	return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
2541178479Sjb}
2542178479Sjb
2543178479Sjbstatic int
2544178479Sjbdt_consume_begin(dtrace_hdl_t *dtp, FILE *fp, dtrace_bufdesc_t *buf,
2545178479Sjb    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2546178479Sjb{
2547178479Sjb	/*
2548178479Sjb	 * There's this idea that the BEGIN probe should be processed before
2549178479Sjb	 * everything else, and that the END probe should be processed after
2550178479Sjb	 * anything else.  In the common case, this is pretty easy to deal
2551178479Sjb	 * with.  However, a situation may arise where the BEGIN enabling and
2552178479Sjb	 * END enabling are on the same CPU, and some enabling in the middle
2553178479Sjb	 * occurred on a different CPU.  To deal with this (blech!) we need to
2554178479Sjb	 * consume the BEGIN buffer up until the end of the BEGIN probe, and
2555178479Sjb	 * then set it aside.  We will then process every other CPU, and then
2556178479Sjb	 * we'll return to the BEGIN CPU and process the rest of the data
2557178479Sjb	 * (which will inevitably include the END probe, if any).  Making this
2558178479Sjb	 * even more complicated (!) is the library's ERROR enabling.  Because
2559178479Sjb	 * this enabling is processed before we even get into the consume call
2560178479Sjb	 * back, any ERROR firing would result in the library's ERROR enabling
2561178479Sjb	 * being processed twice -- once in our first pass (for BEGIN probes),
2562178479Sjb	 * and again in our second pass (for everything but BEGIN probes).  To
2563178479Sjb	 * deal with this, we interpose on the ERROR handler to assure that we
2564178479Sjb	 * only process ERROR enablings induced by BEGIN enablings in the
2565178479Sjb	 * first pass, and that we only process ERROR enablings _not_ induced
2566178479Sjb	 * by BEGIN enablings in the second pass.
2567178479Sjb	 */
2568178479Sjb	dt_begin_t begin;
2569178479Sjb	processorid_t cpu = dtp->dt_beganon;
2570178479Sjb	dtrace_bufdesc_t nbuf;
2571178576Sjb#if !defined(sun)
2572178576Sjb	dtrace_bufdesc_t *pbuf;
2573178576Sjb#endif
2574178479Sjb	int rval, i;
2575178479Sjb	static int max_ncpus;
2576178479Sjb	dtrace_optval_t size;
2577178479Sjb
2578178479Sjb	dtp->dt_beganon = -1;
2579178479Sjb
2580178576Sjb#if defined(sun)
2581178479Sjb	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2582178576Sjb#else
2583178576Sjb	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2584178576Sjb#endif
2585178479Sjb		/*
2586178479Sjb		 * We really don't expect this to fail, but it is at least
2587178479Sjb		 * technically possible for this to fail with ENOENT.  In this
2588178479Sjb		 * case, we just drive on...
2589178479Sjb		 */
2590178479Sjb		if (errno == ENOENT)
2591178479Sjb			return (0);
2592178479Sjb
2593178479Sjb		return (dt_set_errno(dtp, errno));
2594178479Sjb	}
2595178479Sjb
2596178479Sjb	if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
2597178479Sjb		/*
2598178479Sjb		 * This is the simple case.  We're either not stopped, or if
2599178479Sjb		 * we are, we actually processed any END probes on another
2600178479Sjb		 * CPU.  We can simply consume this buffer and return.
2601178479Sjb		 */
2602178479Sjb		return (dt_consume_cpu(dtp, fp, cpu, buf, pf, rf, arg));
2603178479Sjb	}
2604178479Sjb
2605178479Sjb	begin.dtbgn_probefunc = pf;
2606178479Sjb	begin.dtbgn_recfunc = rf;
2607178479Sjb	begin.dtbgn_arg = arg;
2608178479Sjb	begin.dtbgn_beginonly = 1;
2609178479Sjb
2610178479Sjb	/*
2611178479Sjb	 * We need to interpose on the ERROR handler to be sure that we
2612178479Sjb	 * only process ERRORs induced by BEGIN.
2613178479Sjb	 */
2614178479Sjb	begin.dtbgn_errhdlr = dtp->dt_errhdlr;
2615178479Sjb	begin.dtbgn_errarg = dtp->dt_errarg;
2616178479Sjb	dtp->dt_errhdlr = dt_consume_begin_error;
2617178479Sjb	dtp->dt_errarg = &begin;
2618178479Sjb
2619178479Sjb	rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe,
2620178479Sjb	    dt_consume_begin_record, &begin);
2621178479Sjb
2622178479Sjb	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2623178479Sjb	dtp->dt_errarg = begin.dtbgn_errarg;
2624178479Sjb
2625178479Sjb	if (rval != 0)
2626178479Sjb		return (rval);
2627178479Sjb
2628178479Sjb	/*
2629178479Sjb	 * Now allocate a new buffer.  We'll use this to deal with every other
2630178479Sjb	 * CPU.
2631178479Sjb	 */
2632178479Sjb	bzero(&nbuf, sizeof (dtrace_bufdesc_t));
2633178479Sjb	(void) dtrace_getopt(dtp, "bufsize", &size);
2634178479Sjb	if ((nbuf.dtbd_data = malloc(size)) == NULL)
2635178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
2636178479Sjb
2637178479Sjb	if (max_ncpus == 0)
2638178479Sjb		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2639178479Sjb
2640178479Sjb	for (i = 0; i < max_ncpus; i++) {
2641178479Sjb		nbuf.dtbd_cpu = i;
2642178479Sjb
2643178479Sjb		if (i == cpu)
2644178479Sjb			continue;
2645178479Sjb
2646178576Sjb#if defined(sun)
2647178479Sjb		if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &nbuf) == -1) {
2648178576Sjb#else
2649178576Sjb		pbuf = &nbuf;
2650178576Sjb		if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &pbuf) == -1) {
2651178576Sjb#endif
2652178479Sjb			/*
2653178479Sjb			 * If we failed with ENOENT, it may be because the
2654178479Sjb			 * CPU was unconfigured -- this is okay.  Any other
2655178479Sjb			 * error, however, is unexpected.
2656178479Sjb			 */
2657178479Sjb			if (errno == ENOENT)
2658178479Sjb				continue;
2659178479Sjb
2660178479Sjb			free(nbuf.dtbd_data);
2661178479Sjb
2662178479Sjb			return (dt_set_errno(dtp, errno));
2663178479Sjb		}
2664178479Sjb
2665178479Sjb		if ((rval = dt_consume_cpu(dtp, fp,
2666178479Sjb		    i, &nbuf, pf, rf, arg)) != 0) {
2667178479Sjb			free(nbuf.dtbd_data);
2668178479Sjb			return (rval);
2669178479Sjb		}
2670178479Sjb	}
2671178479Sjb
2672178479Sjb	free(nbuf.dtbd_data);
2673178479Sjb
2674178479Sjb	/*
2675178479Sjb	 * Okay -- we're done with the other buffers.  Now we want to
2676178479Sjb	 * reconsume the first buffer -- but this time we're looking for
2677178479Sjb	 * everything _but_ BEGIN.  And of course, in order to only consume
2678178479Sjb	 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
2679178479Sjb	 * ERROR interposition function...
2680178479Sjb	 */
2681178479Sjb	begin.dtbgn_beginonly = 0;
2682178479Sjb
2683178479Sjb	assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
2684178479Sjb	assert(begin.dtbgn_errarg == dtp->dt_errarg);
2685178479Sjb	dtp->dt_errhdlr = dt_consume_begin_error;
2686178479Sjb	dtp->dt_errarg = &begin;
2687178479Sjb
2688178479Sjb	rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe,
2689178479Sjb	    dt_consume_begin_record, &begin);
2690178479Sjb
2691178479Sjb	dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2692178479Sjb	dtp->dt_errarg = begin.dtbgn_errarg;
2693178479Sjb
2694178479Sjb	return (rval);
2695178479Sjb}
2696178479Sjb
2697178479Sjbint
2698178479Sjbdtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
2699178479Sjb    dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2700178479Sjb{
2701178479Sjb	dtrace_bufdesc_t *buf = &dtp->dt_buf;
2702178479Sjb	dtrace_optval_t size;
2703178479Sjb	static int max_ncpus;
2704178479Sjb	int i, rval;
2705178479Sjb	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
2706178479Sjb	hrtime_t now = gethrtime();
2707178479Sjb
2708178479Sjb	if (dtp->dt_lastswitch != 0) {
2709178479Sjb		if (now - dtp->dt_lastswitch < interval)
2710178479Sjb			return (0);
2711178479Sjb
2712178479Sjb		dtp->dt_lastswitch += interval;
2713178479Sjb	} else {
2714178479Sjb		dtp->dt_lastswitch = now;
2715178479Sjb	}
2716178479Sjb
2717178479Sjb	if (!dtp->dt_active)
2718178479Sjb		return (dt_set_errno(dtp, EINVAL));
2719178479Sjb
2720178479Sjb	if (max_ncpus == 0)
2721178479Sjb		max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2722178479Sjb
2723178479Sjb	if (pf == NULL)
2724178479Sjb		pf = (dtrace_consume_probe_f *)dt_nullprobe;
2725178479Sjb
2726178479Sjb	if (rf == NULL)
2727178479Sjb		rf = (dtrace_consume_rec_f *)dt_nullrec;
2728178479Sjb
2729178479Sjb	if (buf->dtbd_data == NULL) {
2730178479Sjb		(void) dtrace_getopt(dtp, "bufsize", &size);
2731178479Sjb		if ((buf->dtbd_data = malloc(size)) == NULL)
2732178479Sjb			return (dt_set_errno(dtp, EDT_NOMEM));
2733178479Sjb
2734178479Sjb		buf->dtbd_size = size;
2735178479Sjb	}
2736178479Sjb
2737178479Sjb	/*
2738178479Sjb	 * If we have just begun, we want to first process the CPU that
2739178479Sjb	 * executed the BEGIN probe (if any).
2740178479Sjb	 */
2741178479Sjb	if (dtp->dt_active && dtp->dt_beganon != -1) {
2742178479Sjb		buf->dtbd_cpu = dtp->dt_beganon;
2743178479Sjb		if ((rval = dt_consume_begin(dtp, fp, buf, pf, rf, arg)) != 0)
2744178479Sjb			return (rval);
2745178479Sjb	}
2746178479Sjb
2747178479Sjb	for (i = 0; i < max_ncpus; i++) {
2748178479Sjb		buf->dtbd_cpu = i;
2749178479Sjb
2750178479Sjb		/*
2751178479Sjb		 * If we have stopped, we want to process the CPU on which the
2752178479Sjb		 * END probe was processed only _after_ we have processed
2753178479Sjb		 * everything else.
2754178479Sjb		 */
2755178479Sjb		if (dtp->dt_stopped && (i == dtp->dt_endedon))
2756178479Sjb			continue;
2757178479Sjb
2758178576Sjb#if defined(sun)
2759178479Sjb		if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2760178576Sjb#else
2761178576Sjb		if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2762178576Sjb#endif
2763178479Sjb			/*
2764178479Sjb			 * If we failed with ENOENT, it may be because the
2765178479Sjb			 * CPU was unconfigured -- this is okay.  Any other
2766178479Sjb			 * error, however, is unexpected.
2767178479Sjb			 */
2768178479Sjb			if (errno == ENOENT)
2769178479Sjb				continue;
2770178479Sjb
2771178479Sjb			return (dt_set_errno(dtp, errno));
2772178479Sjb		}
2773178479Sjb
2774178479Sjb		if ((rval = dt_consume_cpu(dtp, fp, i, buf, pf, rf, arg)) != 0)
2775178479Sjb			return (rval);
2776178479Sjb	}
2777178479Sjb
2778178479Sjb	if (!dtp->dt_stopped)
2779178479Sjb		return (0);
2780178479Sjb
2781178479Sjb	buf->dtbd_cpu = dtp->dt_endedon;
2782178479Sjb
2783178576Sjb#if defined(sun)
2784178479Sjb	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2785178576Sjb#else
2786178576Sjb	if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) {
2787178576Sjb#endif
2788178479Sjb		/*
2789178479Sjb		 * This _really_ shouldn't fail, but it is strictly speaking
2790178479Sjb		 * possible for this to return ENOENT if the CPU that called
2791178479Sjb		 * the END enabling somehow managed to become unconfigured.
2792178479Sjb		 * It's unclear how the user can possibly expect anything
2793178479Sjb		 * rational to happen in this case -- the state has been thrown
2794178479Sjb		 * out along with the unconfigured CPU -- so we'll just drive
2795178479Sjb		 * on...
2796178479Sjb		 */
2797178479Sjb		if (errno == ENOENT)
2798178479Sjb			return (0);
2799178479Sjb
2800178479Sjb		return (dt_set_errno(dtp, errno));
2801178479Sjb	}
2802178479Sjb
2803178479Sjb	return (dt_consume_cpu(dtp, fp, dtp->dt_endedon, buf, pf, rf, arg));
2804178479Sjb}
2805