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