xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_printf.c (revision 1222:6c8cd7eac61b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1222Smws 
230Sstevel@tonic-gate /*
24*1222Smws  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <strings.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <alloca.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <dt_printf.h>
400Sstevel@tonic-gate #include <dt_string.h>
410Sstevel@tonic-gate #include <dt_impl.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*ARGSUSED*/
440Sstevel@tonic-gate static int
45457Sbmc pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
480Sstevel@tonic-gate }
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*ARGSUSED*/
510Sstevel@tonic-gate static int
52457Sbmc pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
53457Sbmc {
54457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
55457Sbmc 	    dt_node_is_symaddr(dnp));
56457Sbmc }
57457Sbmc 
58457Sbmc /*ARGSUSED*/
59457Sbmc static int
60457Sbmc pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
61457Sbmc {
62457Sbmc 	dtrace_hdl_t *dtp = pfv->pfv_dtp;
63457Sbmc 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
64457Sbmc 
65457Sbmc 	if (dt_node_is_usymaddr(dnp))
66457Sbmc 		return (1);
67457Sbmc 
68457Sbmc 	if (idp == NULL || idp->di_id == 0)
69457Sbmc 		return (0);
70457Sbmc 
71457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
72457Sbmc }
73457Sbmc 
74457Sbmc /*ARGSUSED*/
75457Sbmc static int
76457Sbmc pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
77457Sbmc {
78457Sbmc 	return (dt_node_is_stack(dnp));
79457Sbmc }
80457Sbmc 
81457Sbmc /*ARGSUSED*/
82457Sbmc static int
831017Sbmc pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
841017Sbmc {
851017Sbmc 	return (dt_node_is_integer(dnp) &&
861017Sbmc 	    dt_node_type_size(dnp) == sizeof (uint64_t));
871017Sbmc }
881017Sbmc 
891017Sbmc /*ARGSUSED*/
901017Sbmc static int
91457Sbmc pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	ctf_file_t *ctfp;
940Sstevel@tonic-gate 	ctf_encoding_t e;
950Sstevel@tonic-gate 	ctf_arinfo_t r;
960Sstevel@tonic-gate 	ctf_id_t base;
970Sstevel@tonic-gate 	uint_t kind;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (dt_node_is_string(dnp))
1000Sstevel@tonic-gate 		return (1);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	ctfp = dnp->dn_ctfp;
1030Sstevel@tonic-gate 	base = ctf_type_resolve(ctfp, dnp->dn_type);
1040Sstevel@tonic-gate 	kind = ctf_type_kind(ctfp, base);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1070Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1080Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*ARGSUSED*/
1120Sstevel@tonic-gate static int
113457Sbmc pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1160Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
1170Sstevel@tonic-gate 	uint_t kind = ctf_type_kind(ctfp, base);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	ctf_encoding_t e;
1200Sstevel@tonic-gate 	ctf_arinfo_t r;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1230Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1240Sstevel@tonic-gate 	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
1250Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*ARGSUSED*/
1290Sstevel@tonic-gate static int
130457Sbmc pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	return (dt_node_is_integer(dnp) &&
1330Sstevel@tonic-gate 	    dt_node_type_size(dnp) <= sizeof (int));
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*ARGSUSED*/
1370Sstevel@tonic-gate static int
138457Sbmc pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	return (dt_node_is_float(dnp));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*ARGSUSED*/
1440Sstevel@tonic-gate static int
145457Sbmc pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
150457Sbmc /*ARGSUSED*/
1510Sstevel@tonic-gate static int
152457Sbmc pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	if (dnp->dn_flags & DT_NF_SIGNED)
1550Sstevel@tonic-gate 		pfd->pfd_flags |= DT_PFCONV_SIGNED;
1560Sstevel@tonic-gate 	else
1570Sstevel@tonic-gate 		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*ARGSUSED*/
1630Sstevel@tonic-gate static int
164457Sbmc pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1670Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1680Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1710Sstevel@tonic-gate 	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
1720Sstevel@tonic-gate 	    strcmp(n, "unsigned short") == 0));
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*ARGSUSED*/
1760Sstevel@tonic-gate static int
177457Sbmc pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1800Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1810Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1840Sstevel@tonic-gate 	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
1850Sstevel@tonic-gate 	    strcmp(n, "unsigned long") == 0));
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*ARGSUSED*/
1890Sstevel@tonic-gate static int
190457Sbmc pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1930Sstevel@tonic-gate 	ctf_id_t type = dnp->dn_type;
1940Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
1970Sstevel@tonic-gate 	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
1980Sstevel@tonic-gate 	    strcmp(n, "signed long long") == 0 ||
1990Sstevel@tonic-gate 	    strcmp(n, "unsigned long long") == 0))
2000Sstevel@tonic-gate 		return (1);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * If the type used for %llx or %llX is not an [unsigned] long long, we
2040Sstevel@tonic-gate 	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
2050Sstevel@tonic-gate 	 * that these typedefs are guaranteed to work with %ll[xX] in either
2060Sstevel@tonic-gate 	 * compilation environment even though they alias to "long" in LP64.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
2090Sstevel@tonic-gate 		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
2100Sstevel@tonic-gate 		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
2110Sstevel@tonic-gate 			return (1);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		type = ctf_type_reference(ctfp, type);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	return (0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
219457Sbmc /*ARGSUSED*/
2200Sstevel@tonic-gate static int
221457Sbmc pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
2240Sstevel@tonic-gate 	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*ARGSUSED*/
2280Sstevel@tonic-gate static int
2290Sstevel@tonic-gate pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2300Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	int64_t normal = (int64_t)unormal;
2330Sstevel@tonic-gate 	int32_t n = (int32_t)normal;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	switch (size) {
2360Sstevel@tonic-gate 	case sizeof (int8_t):
2370Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2380Sstevel@tonic-gate 		    (int32_t)*((int8_t *)addr) / n));
2390Sstevel@tonic-gate 	case sizeof (int16_t):
2400Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2410Sstevel@tonic-gate 		    (int32_t)*((int16_t *)addr) / n));
2420Sstevel@tonic-gate 	case sizeof (int32_t):
2430Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2440Sstevel@tonic-gate 		    *((int32_t *)addr) / n));
2450Sstevel@tonic-gate 	case sizeof (int64_t):
2460Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2470Sstevel@tonic-gate 		    *((int64_t *)addr) / normal));
2480Sstevel@tonic-gate 	default:
2490Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /*ARGSUSED*/
2540Sstevel@tonic-gate static int
2550Sstevel@tonic-gate pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2560Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	uint32_t n = (uint32_t)normal;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	switch (size) {
2610Sstevel@tonic-gate 	case sizeof (uint8_t):
2620Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2630Sstevel@tonic-gate 		    (uint32_t)*((uint8_t *)addr) / n));
2640Sstevel@tonic-gate 	case sizeof (uint16_t):
2650Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2660Sstevel@tonic-gate 		    (uint32_t)*((uint16_t *)addr) / n));
2670Sstevel@tonic-gate 	case sizeof (uint32_t):
2680Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2690Sstevel@tonic-gate 		    *((uint32_t *)addr) / n));
2700Sstevel@tonic-gate 	case sizeof (uint64_t):
2710Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2720Sstevel@tonic-gate 		    *((uint64_t *)addr) / normal));
2730Sstevel@tonic-gate 	default:
2740Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate static int
2790Sstevel@tonic-gate pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2800Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
2830Sstevel@tonic-gate 		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
2840Sstevel@tonic-gate 	else
2850Sstevel@tonic-gate 		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*ARGSUSED*/
2890Sstevel@tonic-gate static int
2900Sstevel@tonic-gate pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2910Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	double n = (double)normal;
2940Sstevel@tonic-gate 	long double ldn = (long double)normal;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	switch (size) {
2970Sstevel@tonic-gate 	case sizeof (float):
2980Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2990Sstevel@tonic-gate 		    (double)*((float *)addr) / n));
3000Sstevel@tonic-gate 	case sizeof (double):
3010Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3020Sstevel@tonic-gate 		    *((double *)addr) / n));
3030Sstevel@tonic-gate 	case sizeof (long double):
3040Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3050Sstevel@tonic-gate 		    *((long double *)addr) / ldn));
3060Sstevel@tonic-gate 	default:
3070Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*ARGSUSED*/
3120Sstevel@tonic-gate static int
3130Sstevel@tonic-gate pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3140Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	char *s;
317457Sbmc 	int n, len = 256;
318457Sbmc 	uint64_t val;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	switch (size) {
3210Sstevel@tonic-gate 	case sizeof (uint32_t):
3220Sstevel@tonic-gate 		val = *((uint32_t *)addr);
3230Sstevel@tonic-gate 		break;
3240Sstevel@tonic-gate 	case sizeof (uint64_t):
3250Sstevel@tonic-gate 		val = *((uint64_t *)addr);
3260Sstevel@tonic-gate 		break;
3270Sstevel@tonic-gate 	default:
3280Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
331457Sbmc 	do {
332457Sbmc 		n = len;
333457Sbmc 		s = alloca(n);
334457Sbmc 	} while ((len = dtrace_addr2str(dtp, val, s, n)) >= n);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*ARGSUSED*/
3400Sstevel@tonic-gate static int
341457Sbmc pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
342457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
343457Sbmc {
344457Sbmc 	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
345457Sbmc }
346457Sbmc 
347457Sbmc /*ARGSUSED*/
348457Sbmc static int
349457Sbmc pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
350457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
351457Sbmc {
352457Sbmc 	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
353457Sbmc }
354457Sbmc 
355457Sbmc /*ARGSUSED*/
356457Sbmc static int
3570Sstevel@tonic-gate pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3580Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3590Sstevel@tonic-gate {
360457Sbmc 	char *s;
361457Sbmc 	int n, len = 256;
362457Sbmc 	uint64_t val, pid = 0;
363457Sbmc 
3640Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	switch (size) {
3670Sstevel@tonic-gate 	case sizeof (uint32_t):
3680Sstevel@tonic-gate 		val = (u_longlong_t)*((uint32_t *)addr);
3690Sstevel@tonic-gate 		break;
3700Sstevel@tonic-gate 	case sizeof (uint64_t):
3710Sstevel@tonic-gate 		val = (u_longlong_t)*((uint64_t *)addr);
3720Sstevel@tonic-gate 		break;
373457Sbmc 	case sizeof (uint64_t) * 2:
374457Sbmc 		pid = ((uint64_t *)(uintptr_t)addr)[0];
375457Sbmc 		val = ((uint64_t *)(uintptr_t)addr)[1];
376457Sbmc 		break;
3770Sstevel@tonic-gate 	default:
3780Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
381457Sbmc 	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
382457Sbmc 		pid = idp->di_id;
3830Sstevel@tonic-gate 
384457Sbmc 	do {
385457Sbmc 		n = len;
386457Sbmc 		s = alloca(n);
387457Sbmc 	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) >= n);
3880Sstevel@tonic-gate 
389457Sbmc 	return (dt_printf(dtp, fp, format, s));
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*ARGSUSED*/
3930Sstevel@tonic-gate static int
3940Sstevel@tonic-gate pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3950Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
3960Sstevel@tonic-gate {
397457Sbmc 	int width;
3980Sstevel@tonic-gate 	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
3990Sstevel@tonic-gate 	const dtrace_recdesc_t *rec = pfd->pfd_rec;
4000Sstevel@tonic-gate 	caddr_t addr = (caddr_t)vaddr;
4010Sstevel@tonic-gate 	int err = 0;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * We have stashed the value of the STACKINDENT option, and we will
4050Sstevel@tonic-gate 	 * now override it for the purposes of formatting the stack.  If the
4060Sstevel@tonic-gate 	 * field has been specified as left-aligned (i.e. (%-#), we set the
4070Sstevel@tonic-gate 	 * indentation to be the width.  This is a slightly odd semantic, but
4080Sstevel@tonic-gate 	 * it's useful functionality -- and it's slightly odd to begin with to
4090Sstevel@tonic-gate 	 * be using a single format specifier to be formatting multiple lines
4100Sstevel@tonic-gate 	 * of text...
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	if (pfd->pfd_dynwidth < 0) {
4130Sstevel@tonic-gate 		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
4140Sstevel@tonic-gate 		width = -pfd->pfd_dynwidth;
4150Sstevel@tonic-gate 	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
4160Sstevel@tonic-gate 		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
4170Sstevel@tonic-gate 	} else {
4180Sstevel@tonic-gate 		width = 0;
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	switch (rec->dtrd_action) {
4240Sstevel@tonic-gate 	case DTRACEACT_USTACK:
4250Sstevel@tonic-gate 	case DTRACEACT_JSTACK:
4260Sstevel@tonic-gate 		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
4270Sstevel@tonic-gate 		break;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	case DTRACEACT_STACK:
430457Sbmc 		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
431457Sbmc 		    rec->dtrd_size / rec->dtrd_arg);
4320Sstevel@tonic-gate 		break;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	default:
4350Sstevel@tonic-gate 		assert(0);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	return (err);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*ARGSUSED*/
4440Sstevel@tonic-gate static int
4450Sstevel@tonic-gate pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4460Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	char src[32], buf[32], *dst = buf;
4490Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4500Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4510Sstevel@tonic-gate 	int i;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
4550Sstevel@tonic-gate 	 * Below, we turn this into the canonical adb/mdb /[yY] format,
4560Sstevel@tonic-gate 	 * "1973 Dec  3 17:20:00".
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 	(void) ctime_r(&sec, src, sizeof (src));
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * Place the 4-digit year at the head of the string...
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	for (i = 20; i < 24; i++)
4640Sstevel@tonic-gate 		*dst++ = src[i];
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * ...and follow it with the remainder (month, day, hh:mm:ss).
4680Sstevel@tonic-gate 	 */
4690Sstevel@tonic-gate 	for (i = 3; i < 19; i++)
4700Sstevel@tonic-gate 		*dst++ = src[i];
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	*dst = '\0';
4730Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * This prints the time in RFC 822 standard form.  This is useful for emitting
4780Sstevel@tonic-gate  * notions of time that are consumed by standard tools (e.g., as part of an
4790Sstevel@tonic-gate  * RSS feed).
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate /*ARGSUSED*/
4820Sstevel@tonic-gate static int
4830Sstevel@tonic-gate pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4840Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4870Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4880Sstevel@tonic-gate 	struct tm tm;
4890Sstevel@tonic-gate 	char buf[64];
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	(void) localtime_r(&sec, &tm);
4920Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
4930Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*ARGSUSED*/
4970Sstevel@tonic-gate static int
4980Sstevel@tonic-gate pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4990Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	char *s = alloca(size + 1);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	bcopy(addr, s, size);
5040Sstevel@tonic-gate 	s[size] = '\0';
5050Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /*ARGSUSED*/
5090Sstevel@tonic-gate static int
5100Sstevel@tonic-gate pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5110Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	wchar_t *ws = alloca(size + sizeof (wchar_t));
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	bcopy(addr, ws, size);
5160Sstevel@tonic-gate 	ws[size / sizeof (wchar_t)] = L'\0';
5170Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, ws));
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*ARGSUSED*/
5210Sstevel@tonic-gate static int
5220Sstevel@tonic-gate pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5230Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 	char *s;
5260Sstevel@tonic-gate 	int n;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if ((s = strchr2esc(addr, size)) == NULL)
5290Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	n = dt_printf(dtp, fp, format, s);
5320Sstevel@tonic-gate 	free(s);
5330Sstevel@tonic-gate 	return (n);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate static int
5370Sstevel@tonic-gate pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5380Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	char c;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	switch (size) {
5430Sstevel@tonic-gate 	case sizeof (int8_t):
5440Sstevel@tonic-gate 		c = *(int8_t *)addr;
5450Sstevel@tonic-gate 		break;
5460Sstevel@tonic-gate 	case sizeof (int16_t):
5470Sstevel@tonic-gate 		c = *(int16_t *)addr;
5480Sstevel@tonic-gate 		break;
5490Sstevel@tonic-gate 	case sizeof (int32_t):
5500Sstevel@tonic-gate 		c = *(int32_t *)addr;
5510Sstevel@tonic-gate 		break;
5520Sstevel@tonic-gate 	default:
5530Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate /*ARGSUSED*/
5600Sstevel@tonic-gate static int
5610Sstevel@tonic-gate pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5620Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	return (dt_printf(dtp, fp, "%%"));
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate static const char pfproto_xint[] = "char, short, int, long, or long long";
5680Sstevel@tonic-gate static const char pfproto_csi[] = "char, short, or int";
5690Sstevel@tonic-gate static const char pfproto_fp[] = "float, double, or long double";
5700Sstevel@tonic-gate static const char pfproto_addr[] = "pointer or integer";
571457Sbmc static const char pfproto_uaddr[] =
572457Sbmc 	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
5730Sstevel@tonic-gate static const char pfproto_cstr[] = "char [] or string (or use stringof)";
5740Sstevel@tonic-gate static const char pfproto_wstr[] = "wchar_t []";
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate  * Printf format conversion dictionary.  This table should match the set of
5780Sstevel@tonic-gate  * conversions offered by printf(3C), as well as some additional extensions.
5790Sstevel@tonic-gate  * The second parameter is an ASCII string which is either an actual type
5800Sstevel@tonic-gate  * name we should look up (if pfcheck_type is specified), or just a descriptive
5810Sstevel@tonic-gate  * string of the types expected for use in error messages.
5820Sstevel@tonic-gate  */
5830Sstevel@tonic-gate static const dt_pfconv_t _dtrace_conversions[] = {
584457Sbmc { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
585457Sbmc { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
5860Sstevel@tonic-gate { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
5870Sstevel@tonic-gate { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
5880Sstevel@tonic-gate { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
5890Sstevel@tonic-gate { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
5900Sstevel@tonic-gate { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
5910Sstevel@tonic-gate { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
5920Sstevel@tonic-gate { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
5930Sstevel@tonic-gate { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
5940Sstevel@tonic-gate { "hd", "d", "short", pfcheck_type, pfprint_sint },
5950Sstevel@tonic-gate { "hi", "i", "short", pfcheck_type, pfprint_sint },
5960Sstevel@tonic-gate { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
5970Sstevel@tonic-gate { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
5980Sstevel@tonic-gate { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
5990Sstevel@tonic-gate { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
6000Sstevel@tonic-gate { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
601457Sbmc { "k", "s", "stack", pfcheck_stack, pfprint_stack },
6020Sstevel@tonic-gate { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
6030Sstevel@tonic-gate { "ld",	"d", "long", pfcheck_type, pfprint_sint },
6040Sstevel@tonic-gate { "li",	"i", "long", pfcheck_type, pfprint_sint },
6050Sstevel@tonic-gate { "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
6060Sstevel@tonic-gate { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
6070Sstevel@tonic-gate { "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6080Sstevel@tonic-gate { "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
6090Sstevel@tonic-gate { "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
6100Sstevel@tonic-gate { "lld", "d", "long long", pfcheck_type, pfprint_sint },
6110Sstevel@tonic-gate { "lli", "i", "long long", pfcheck_type, pfprint_sint },
6120Sstevel@tonic-gate { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
6130Sstevel@tonic-gate { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
6140Sstevel@tonic-gate { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
6150Sstevel@tonic-gate { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
6160Sstevel@tonic-gate { "Le",	"e", "long double", pfcheck_type, pfprint_fp },
6170Sstevel@tonic-gate { "LE",	"E", "long double", pfcheck_type, pfprint_fp },
6180Sstevel@tonic-gate { "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
6190Sstevel@tonic-gate { "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
6200Sstevel@tonic-gate { "LG",	"G", "long double", pfcheck_type, pfprint_fp },
6210Sstevel@tonic-gate { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
6220Sstevel@tonic-gate { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
6230Sstevel@tonic-gate { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
6240Sstevel@tonic-gate { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
6251017Sbmc { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
6260Sstevel@tonic-gate { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
6270Sstevel@tonic-gate { "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
6280Sstevel@tonic-gate { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6290Sstevel@tonic-gate { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
6300Sstevel@tonic-gate { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
6311017Sbmc { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
6320Sstevel@tonic-gate { "%", "%", "void", pfcheck_type, pfprint_pct },
6330Sstevel@tonic-gate { NULL, NULL, NULL, NULL, NULL }
6340Sstevel@tonic-gate };
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate int
6370Sstevel@tonic-gate dt_pfdict_create(dtrace_hdl_t *dtp)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	uint_t n = _dtrace_strbuckets;
6400Sstevel@tonic-gate 	const dt_pfconv_t *pfd;
6410Sstevel@tonic-gate 	dt_pfdict_t *pdi;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
6440Sstevel@tonic-gate 	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
6450Sstevel@tonic-gate 		free(pdi);
6460Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	dtp->dt_pfdict = pdi;
6500Sstevel@tonic-gate 	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
6510Sstevel@tonic-gate 	pdi->pdi_nbuckets = n;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
6540Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
6550Sstevel@tonic-gate 		dt_pfconv_t *pfc;
6560Sstevel@tonic-gate 		uint_t h;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
6590Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6600Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
6640Sstevel@tonic-gate 		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
6650Sstevel@tonic-gate 		pfc->pfc_next = pdi->pdi_buckets[h];
6660Sstevel@tonic-gate 		pdi->pdi_buckets[h] = pfc;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		dtt.dtt_ctfp = NULL;
6690Sstevel@tonic-gate 		dtt.dtt_type = CTF_ERR;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		/*
6720Sstevel@tonic-gate 		 * The "D" container or its parent must contain a definition of
6730Sstevel@tonic-gate 		 * any type referenced by a printf conversion.  If none can be
6740Sstevel@tonic-gate 		 * found, we fail to initialize the printf dictionary.
6750Sstevel@tonic-gate 		 */
6760Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6770Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
6780Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6790Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOCONV));
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		pfc->pfc_dctfp = dtt.dtt_ctfp;
6830Sstevel@tonic-gate 		pfc->pfc_dtype = dtt.dtt_type;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * The "C" container may contain an alternate definition of an
6870Sstevel@tonic-gate 		 * explicit conversion type.  If it does, use it; otherwise
6880Sstevel@tonic-gate 		 * just set pfc_ctype to pfc_dtype so it is always valid.
6890Sstevel@tonic-gate 		 */
6900Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6910Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
6920Sstevel@tonic-gate 			pfc->pfc_cctfp = dtt.dtt_ctfp;
6930Sstevel@tonic-gate 			pfc->pfc_ctype = dtt.dtt_type;
6940Sstevel@tonic-gate 		} else {
6950Sstevel@tonic-gate 			pfc->pfc_cctfp = pfc->pfc_dctfp;
6960Sstevel@tonic-gate 			pfc->pfc_ctype = pfc->pfc_dtype;
6970Sstevel@tonic-gate 		}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
7000Sstevel@tonic-gate 		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
7010Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
7020Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADCONV));
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (0);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate void
7120Sstevel@tonic-gate dt_pfdict_destroy(dtrace_hdl_t *dtp)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7150Sstevel@tonic-gate 	dt_pfconv_t *pfc, *nfc;
7160Sstevel@tonic-gate 	uint_t i;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (pdi == NULL)
7190Sstevel@tonic-gate 		return;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	for (i = 0; i < pdi->pdi_nbuckets; i++) {
7220Sstevel@tonic-gate 		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
7230Sstevel@tonic-gate 			nfc = pfc->pfc_next;
7240Sstevel@tonic-gate 			free(pfc);
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	free(pdi->pdi_buckets);
7290Sstevel@tonic-gate 	free(pdi);
7300Sstevel@tonic-gate 	dtp->dt_pfdict = NULL;
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate static const dt_pfconv_t *
7340Sstevel@tonic-gate dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7370Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
7380Sstevel@tonic-gate 	const dt_pfconv_t *pfc;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
7410Sstevel@tonic-gate 		if (strcmp(pfc->pfc_name, name) == 0)
7420Sstevel@tonic-gate 			break;
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	return (pfc);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate static dt_pfargv_t *
7490Sstevel@tonic-gate dt_printf_error(dtrace_hdl_t *dtp, int err)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	if (yypcb != NULL)
7520Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, err);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
7550Sstevel@tonic-gate 	return (NULL);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate dt_pfargv_t *
7590Sstevel@tonic-gate dt_printf_create(dtrace_hdl_t *dtp, const char *s)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd = NULL;
7620Sstevel@tonic-gate 	dt_pfargv_t *pfv;
7630Sstevel@tonic-gate 	const char *p, *q;
7640Sstevel@tonic-gate 	char *format;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
7670Sstevel@tonic-gate 	    (format = strdup(s)) == NULL) {
7680Sstevel@tonic-gate 		free(pfv);
7690Sstevel@tonic-gate 		return (dt_printf_error(dtp, EDT_NOMEM));
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	pfv->pfv_format = format;
7730Sstevel@tonic-gate 	pfv->pfv_argv = NULL;
7740Sstevel@tonic-gate 	pfv->pfv_argc = 0;
7750Sstevel@tonic-gate 	pfv->pfv_flags = 0;
776457Sbmc 	pfv->pfv_dtp = dtp;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
7790Sstevel@tonic-gate 		uint_t namelen = 0;
7800Sstevel@tonic-gate 		int digits = 0;
7810Sstevel@tonic-gate 		int dot = 0;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		char name[8];
7840Sstevel@tonic-gate 		char c;
7850Sstevel@tonic-gate 		int n;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
7880Sstevel@tonic-gate 			dt_printf_destroy(pfv);
7890Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
7930Sstevel@tonic-gate 			nfd->pfd_next = pfd;
7940Sstevel@tonic-gate 		else
7950Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
7980Sstevel@tonic-gate 		pfv->pfv_argc++;
7990Sstevel@tonic-gate 		nfd = pfd;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		if (p > q) {
8020Sstevel@tonic-gate 			pfd->pfd_preflen = (size_t)(p - q);
8030Sstevel@tonic-gate 			pfd->pfd_prefix = q;
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		fmt_switch:
8070Sstevel@tonic-gate 		switch (c = *++p) {
8080Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
8090Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
8100Sstevel@tonic-gate 			if (dot == 0 && digits == 0 && c == '0') {
8110Sstevel@tonic-gate 				pfd->pfd_flags |= DT_PFCONV_ZPAD;
8120Sstevel@tonic-gate 				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
8130Sstevel@tonic-gate 				goto fmt_switch;
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 			for (n = 0; isdigit(c); c = *++p)
8170Sstevel@tonic-gate 				n = n * 10 + c - '0';
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 			if (dot)
8200Sstevel@tonic-gate 				pfd->pfd_prec = n;
8210Sstevel@tonic-gate 			else
8220Sstevel@tonic-gate 				pfd->pfd_width = n;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 			p--;
8250Sstevel@tonic-gate 			digits++;
8260Sstevel@tonic-gate 			goto fmt_switch;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		case '#':
8290Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_ALT;
8300Sstevel@tonic-gate 			goto fmt_switch;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		case '*':
8330Sstevel@tonic-gate 			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 			if (pfd->pfd_flags & n) {
8360Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8370Sstevel@tonic-gate 				    "one '*' specified for the output %s\n",
8380Sstevel@tonic-gate 				    pfv->pfv_argc, n ? "precision" : "width");
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8410Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8420Sstevel@tonic-gate 			}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 			pfd->pfd_flags |= n;
8450Sstevel@tonic-gate 			goto fmt_switch;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 		case '+':
8480Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPOS;
8490Sstevel@tonic-gate 			goto fmt_switch;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		case '-':
8520Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_LEFT;
8530Sstevel@tonic-gate 			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
8540Sstevel@tonic-gate 			goto fmt_switch;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		case '.':
8570Sstevel@tonic-gate 			if (dot++ != 0) {
8580Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8590Sstevel@tonic-gate 				    "one '.' specified\n", pfv->pfv_argc);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8620Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 			digits = 0;
8650Sstevel@tonic-gate 			goto fmt_switch;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		case '?':
8680Sstevel@tonic-gate 			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
8690Sstevel@tonic-gate 				pfd->pfd_width = 16;
8700Sstevel@tonic-gate 			else
8710Sstevel@tonic-gate 				pfd->pfd_width = 8;
8720Sstevel@tonic-gate 			goto fmt_switch;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		case '@':
8750Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_AGG;
8760Sstevel@tonic-gate 			goto fmt_switch;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		case '\'':
8790Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_GROUP;
8800Sstevel@tonic-gate 			goto fmt_switch;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		case ' ':
8830Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPACE;
8840Sstevel@tonic-gate 			goto fmt_switch;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		case '$':
8870Sstevel@tonic-gate 			yywarn("format conversion #%u uses unsupported "
8880Sstevel@tonic-gate 			    "positional format (%%n$)\n", pfv->pfv_argc);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8910Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		case '%':
8940Sstevel@tonic-gate 			if (p[-1] == '%')
8950Sstevel@tonic-gate 				goto default_lbl; /* if %% then use "%" conv */
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			yywarn("format conversion #%u cannot be combined "
8980Sstevel@tonic-gate 			    "with other format flags: %%%%\n", pfv->pfv_argc);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9010Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 		case '\0':
9040Sstevel@tonic-gate 			yywarn("format conversion #%u name expected before "
9050Sstevel@tonic-gate 			    "end of format string\n", pfv->pfv_argc);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9080Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 		case 'h':
9110Sstevel@tonic-gate 		case 'l':
9120Sstevel@tonic-gate 		case 'L':
9130Sstevel@tonic-gate 		case 'w':
9140Sstevel@tonic-gate 			if (namelen < sizeof (name) - 2)
9150Sstevel@tonic-gate 				name[namelen++] = c;
9160Sstevel@tonic-gate 			goto fmt_switch;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		default_lbl:
9190Sstevel@tonic-gate 		default:
9200Sstevel@tonic-gate 			name[namelen++] = c;
9210Sstevel@tonic-gate 			name[namelen] = '\0';
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		if (pfd->pfd_conv == NULL) {
9270Sstevel@tonic-gate 			yywarn("format conversion #%u is undefined: %%%s\n",
9280Sstevel@tonic-gate 			    pfv->pfv_argc, name);
9290Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9300Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (*q != '\0' || *format == '\0') {
9350Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
9360Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9370Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
9410Sstevel@tonic-gate 			nfd->pfd_next = pfd;
9420Sstevel@tonic-gate 		else
9430Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
9460Sstevel@tonic-gate 		pfv->pfv_argc++;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		pfd->pfd_prefix = q;
9490Sstevel@tonic-gate 		pfd->pfd_preflen = strlen(q);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	return (pfv);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate void
9560Sstevel@tonic-gate dt_printf_destroy(dt_pfargv_t *pfv)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
9610Sstevel@tonic-gate 		nfd = pfd->pfd_next;
9620Sstevel@tonic-gate 		free(pfd);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	free(pfv->pfv_format);
9660Sstevel@tonic-gate 	free(pfv);
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate void
9700Sstevel@tonic-gate dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
9710Sstevel@tonic-gate     dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
9720Sstevel@tonic-gate {
9730Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
9740Sstevel@tonic-gate 	const char *func = idp->di_name;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
9770Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
9780Sstevel@tonic-gate 	const char *aggtype;
9790Sstevel@tonic-gate 	dt_node_t aggnode;
9800Sstevel@tonic-gate 	int i, j;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	if (pfv->pfv_format[0] == '\0') {
9830Sstevel@tonic-gate 		xyerror(D_PRINTF_FMT_EMPTY,
9840Sstevel@tonic-gate 		    "%s( ) format string is empty\n", func);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
987457Sbmc 	pfv->pfv_flags = flags;
988457Sbmc 
9890Sstevel@tonic-gate 	/*
9900Sstevel@tonic-gate 	 * We fake up a parse node representing the type that can be used with
991491Sbmc 	 * an aggregation result conversion, which -- for all but count() --
992491Sbmc 	 * is a signed quantity.
9930Sstevel@tonic-gate 	 */
994491Sbmc 	if (kind != DTRACEAGG_COUNT)
9950Sstevel@tonic-gate 		aggtype = "int64_t";
9960Sstevel@tonic-gate 	else
9970Sstevel@tonic-gate 		aggtype = "uint64_t";
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	if (dt_type_lookup(aggtype, &dtt) != 0)
10000Sstevel@tonic-gate 		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	bzero(&aggnode, sizeof (aggnode));
10030Sstevel@tonic-gate 	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
10060Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
10070Sstevel@tonic-gate 		const char *dyns[2];
10080Sstevel@tonic-gate 		int dync = 0;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 		char vname[64];
10110Sstevel@tonic-gate 		dt_node_t *vnp;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		if (pfc == NULL)
10140Sstevel@tonic-gate 			continue; /* no checking if argd is just a prefix */
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
10170Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10180Sstevel@tonic-gate 			continue;
10190Sstevel@tonic-gate 		}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
10220Sstevel@tonic-gate 			dyns[dync++] = ".*";
10230Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
10240Sstevel@tonic-gate 			dyns[dync++] = "*";
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		for (; dync != 0; dync--) {
10270Sstevel@tonic-gate 			if (dnp == NULL) {
10280Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_PROTO,
10290Sstevel@tonic-gate 				    "%s( ) prototype mismatch: conversion "
10300Sstevel@tonic-gate 				    "#%d (%%%s) is missing a corresponding "
10310Sstevel@tonic-gate 				    "\"%s\" argument\n", func, i + 1,
10320Sstevel@tonic-gate 				    pfc->pfc_name, dyns[dync - 1]);
10330Sstevel@tonic-gate 			}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 			if (dt_node_is_integer(dnp) == 0) {
10360Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_TYPE,
10370Sstevel@tonic-gate 				    "%s( ) argument #%d is incompatible "
10380Sstevel@tonic-gate 				    "with conversion #%d prototype:\n"
10390Sstevel@tonic-gate 				    "\tconversion: %% %s %s\n"
10400Sstevel@tonic-gate 				    "\t prototype: int\n\t  argument: %s\n",
10410Sstevel@tonic-gate 				    func, j + foff + 1, i + 1,
10420Sstevel@tonic-gate 				    dyns[dync - 1], pfc->pfc_name,
10430Sstevel@tonic-gate 				    dt_node_type_name(dnp, n, sizeof (n)));
10440Sstevel@tonic-gate 			}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 			dnp = dnp->dn_list;
10470Sstevel@tonic-gate 			j++;
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 		/*
10510Sstevel@tonic-gate 		 * If this conversion is consuming the aggregation data, set
10520Sstevel@tonic-gate 		 * the value node pointer (vnp) to a fake node based on the
10530Sstevel@tonic-gate 		 * aggregating function result type.  Otherwise assign vnp to
10540Sstevel@tonic-gate 		 * the next parse node in the argument list, if there is one.
10550Sstevel@tonic-gate 		 */
10560Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
10570Sstevel@tonic-gate 			if (!(flags & DT_PRINTF_AGGREGATION)) {
10580Sstevel@tonic-gate 				xyerror(D_PRINTF_AGG_CONV,
10590Sstevel@tonic-gate 				    "%%@ conversion requires an aggregation"
10600Sstevel@tonic-gate 				    " and is not for use with %s( )\n", func);
10610Sstevel@tonic-gate 			}
10620Sstevel@tonic-gate 			(void) strlcpy(vname, "aggregating action",
10630Sstevel@tonic-gate 			    sizeof (vname));
10640Sstevel@tonic-gate 			vnp = &aggnode;
10650Sstevel@tonic-gate 		} else if (dnp == NULL) {
10660Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_PROTO,
10670Sstevel@tonic-gate 			    "%s( ) prototype mismatch: conversion #%d (%%"
10680Sstevel@tonic-gate 			    "%s) is missing a corresponding value argument\n",
10690Sstevel@tonic-gate 			    func, i + 1, pfc->pfc_name);
10700Sstevel@tonic-gate 		} else {
10710Sstevel@tonic-gate 			(void) snprintf(vname, sizeof (vname),
10720Sstevel@tonic-gate 			    "argument #%d", j + foff + 1);
10730Sstevel@tonic-gate 			vnp = dnp;
10740Sstevel@tonic-gate 			dnp = dnp->dn_list;
10750Sstevel@tonic-gate 			j++;
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		/*
10790Sstevel@tonic-gate 		 * Fill in the proposed final format string by prepending any
10800Sstevel@tonic-gate 		 * size-related prefixes to the pfconv's format string.  The
10810Sstevel@tonic-gate 		 * pfc_check() function below may optionally modify the format
10820Sstevel@tonic-gate 		 * as part of validating the type of the input argument.
10830Sstevel@tonic-gate 		 */
10840Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_sint ||
10850Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_uint ||
10860Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_dint) {
10870Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (uint64_t))
10880Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "ll");
10890Sstevel@tonic-gate 		} else if (pfc->pfc_print == &pfprint_fp) {
10900Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (long double))
10910Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "L");
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		/*
10970Sstevel@tonic-gate 		 * Validate the format conversion against the value node type.
10980Sstevel@tonic-gate 		 * If the conversion is good, create the descriptor format
10990Sstevel@tonic-gate 		 * string by concatenating together any required printf(3C)
11000Sstevel@tonic-gate 		 * size prefixes with the conversion's native format string.
11010Sstevel@tonic-gate 		 */
1102457Sbmc 		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
11030Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_TYPE,
11040Sstevel@tonic-gate 			    "%s( ) %s is incompatible with "
11050Sstevel@tonic-gate 			    "conversion #%d prototype:\n\tconversion: %%%s\n"
11060Sstevel@tonic-gate 			    "\t prototype: %s\n\t  argument: %s\n", func,
11070Sstevel@tonic-gate 			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
11080Sstevel@tonic-gate 			    dt_node_type_name(vnp, n, sizeof (n)));
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
11130Sstevel@tonic-gate 		xyerror(D_PRINTF_ARG_EXTRA,
11140Sstevel@tonic-gate 		    "%s( ) prototype mismatch: only %d arguments "
11150Sstevel@tonic-gate 		    "required by this format string\n", func, j);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate 
11191017Sbmc void
11201017Sbmc dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
11211017Sbmc {
11221017Sbmc 	dt_ident_t *lid, *rid;
11231017Sbmc 	dt_node_t *lproto, *rproto;
11241017Sbmc 	int largc, rargc, argn;
11251017Sbmc 	char n1[DT_TYPE_NAMELEN];
11261017Sbmc 	char n2[DT_TYPE_NAMELEN];
11271017Sbmc 
11281017Sbmc 	assert(lhs->dn_kind == DT_NODE_AGG);
11291017Sbmc 	assert(rhs->dn_kind == DT_NODE_AGG);
11301017Sbmc 
11311017Sbmc 	lid = lhs->dn_ident;
11321017Sbmc 	rid = rhs->dn_ident;
11331017Sbmc 
11341017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
11351017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
11361017Sbmc 
11371017Sbmc 	/*
11381017Sbmc 	 * First, get an argument count on each side.  These must match.
11391017Sbmc 	 */
11401017Sbmc 	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
11411017Sbmc 		largc++;
11421017Sbmc 
11431017Sbmc 	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
11441017Sbmc 		rargc++;
11451017Sbmc 
11461017Sbmc 	if (largc != rargc) {
11471017Sbmc 		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
11481017Sbmc 		    "matching key signatures: @%s has %d key%s, @%s has %d "
11491017Sbmc 		    "key%s", lid->di_name, rid->di_name,
11501017Sbmc 		    lid->di_name, largc, largc == 1 ? "" : "s",
11511017Sbmc 		    rid->di_name, rargc, rargc == 1 ? "" : "s");
11521017Sbmc 	}
11531017Sbmc 
11541017Sbmc 	/*
11551017Sbmc 	 * Now iterate over the keys to verify that each type matches.
11561017Sbmc 	 */
11571017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
11581017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
11591017Sbmc 
11601017Sbmc 	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
11611017Sbmc 	    rproto = rproto->dn_list) {
11621017Sbmc 		assert(rproto != NULL);
11631017Sbmc 
11641017Sbmc 		if (dt_node_is_argcompat(lproto, rproto))
11651017Sbmc 			continue;
11661017Sbmc 
11671017Sbmc 		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
11681017Sbmc 		    "incompatible with @%s:\n%9s key #%d: %s\n"
11691017Sbmc 		    "%9s key #%d: %s\n",
11701017Sbmc 		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
11711017Sbmc 		    dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
11721017Sbmc 		    argn, dt_node_type_name(rproto, n2, sizeof (n2)));
11731017Sbmc 	}
11741017Sbmc }
11751017Sbmc 
11760Sstevel@tonic-gate static int
11770Sstevel@tonic-gate dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
11780Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len, int *ip)
11790Sstevel@tonic-gate {
11800Sstevel@tonic-gate 	uintptr_t addr;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (nrecs == 0)
11830Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	addr = (uintptr_t)buf + recp->dtrd_offset;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (addr + sizeof (int) > (uintptr_t)buf + len)
11880Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DOFFSET));
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (addr & (recp->dtrd_alignment - 1))
11910Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DALIGN));
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	switch (recp->dtrd_size) {
11940Sstevel@tonic-gate 	case sizeof (int8_t):
11950Sstevel@tonic-gate 		*ip = (int)*((int8_t *)addr);
11960Sstevel@tonic-gate 		break;
11970Sstevel@tonic-gate 	case sizeof (int16_t):
11980Sstevel@tonic-gate 		*ip = (int)*((int16_t *)addr);
11990Sstevel@tonic-gate 		break;
12000Sstevel@tonic-gate 	case sizeof (int32_t):
12010Sstevel@tonic-gate 		*ip = (int)*((int32_t *)addr);
12020Sstevel@tonic-gate 		break;
12030Sstevel@tonic-gate 	case sizeof (int64_t):
12040Sstevel@tonic-gate 		*ip = (int)*((int64_t *)addr);
12050Sstevel@tonic-gate 		break;
12060Sstevel@tonic-gate 	default:
12070Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	return (0);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate /*ARGSUSED*/
12140Sstevel@tonic-gate static int
12150Sstevel@tonic-gate pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12160Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12170Sstevel@tonic-gate {
12180Sstevel@tonic-gate 	const uint64_t *data = addr;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	if (size != sizeof (uint64_t) * 2)
12210Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format,
12240Sstevel@tonic-gate 	    data[0] ? data[1] / normal / data[0] : 0));
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*ARGSUSED*/
12280Sstevel@tonic-gate static int
12290Sstevel@tonic-gate pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12300Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate 	return (dt_print_quantize(dtp, fp, addr, size, normal));
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*ARGSUSED*/
12360Sstevel@tonic-gate static int
12370Sstevel@tonic-gate pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12380Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	return (dt_print_lquantize(dtp, fp, addr, size, normal));
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate static int
12440Sstevel@tonic-gate dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
12450Sstevel@tonic-gate     const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
12461017Sbmc     size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
12490Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = recs;
12501017Sbmc 	const dtrace_aggdata_t *aggdata;
12511017Sbmc 	dtrace_aggdesc_t *agg;
12521017Sbmc 	caddr_t lim = (caddr_t)buf + len, limit;
12530Sstevel@tonic-gate 	char format[64] = "%";
12541017Sbmc 	int i, aggrec, curagg = -1;
12551017Sbmc 	uint64_t normal;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/*
12581017Sbmc 	 * If we are formatting an aggregation, set 'aggrec' to the index of
12591017Sbmc 	 * the final record description (the aggregation result) so we can use
12601017Sbmc 	 * this record index with any conversion where DT_PFCONV_AGG is set.
12611017Sbmc 	 * (The actual aggregation used will vary as we increment through the
12621017Sbmc 	 * aggregation variables that we have been passed.)  Finally, we
12631017Sbmc 	 * decrement nrecs to prevent this record from being used with any
12641017Sbmc 	 * other conversion.
12650Sstevel@tonic-gate 	 */
12660Sstevel@tonic-gate 	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
12671017Sbmc 		assert(aggsdata != NULL);
12681017Sbmc 		assert(naggvars > 0);
12691017Sbmc 
12700Sstevel@tonic-gate 		if (nrecs == 0)
12710Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DMISMATCH));
12721017Sbmc 
12731017Sbmc 		curagg = naggvars > 1 ? 1 : 0;
12741017Sbmc 		aggdata = aggsdata[0];
12751017Sbmc 		aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
12760Sstevel@tonic-gate 		nrecs--;
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
12800Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
12810Sstevel@tonic-gate 		int width = pfd->pfd_width;
12820Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
12830Sstevel@tonic-gate 		int rval;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		char *f = format + 1; /* skip initial '%' */
12860Sstevel@tonic-gate 		const dtrace_recdesc_t *rec;
12870Sstevel@tonic-gate 		dt_pfprint_f *func;
12881017Sbmc 		caddr_t addr;
12890Sstevel@tonic-gate 		size_t size;
12901017Sbmc 		uint32_t flags;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
12930Sstevel@tonic-gate 			char *tmp = alloca(pfd->pfd_preflen + 1);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
12960Sstevel@tonic-gate 			tmp[pfd->pfd_preflen] = '\0';
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
12990Sstevel@tonic-gate 				return (rval);
1300457Sbmc 
1301457Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1302457Sbmc 				/*
1303457Sbmc 				 * For printa(), we flush the buffer after each
13041017Sbmc 				 * prefix, setting the flags to indicate that
13051017Sbmc 				 * this is part of the printa() format string.
1306457Sbmc 				 */
13071017Sbmc 				flags = DTRACE_BUFDATA_AGGFORMAT;
13081017Sbmc 
13091017Sbmc 				if (pfc == NULL && i == pfv->pfv_argc - 1)
13101017Sbmc 					flags |= DTRACE_BUFDATA_AGGLAST;
13111017Sbmc 
13121017Sbmc 				if (dt_buffered_flush(dtp, NULL, NULL,
13131017Sbmc 				    aggdata, flags) < 0)
1314457Sbmc 					return (-1);
1315457Sbmc 			}
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 		if (pfc == NULL) {
13190Sstevel@tonic-gate 			if (pfv->pfv_argc == 1)
13200Sstevel@tonic-gate 				return (nrecs != 0);
13210Sstevel@tonic-gate 			continue;
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 		/*
13250Sstevel@tonic-gate 		 * If the conversion is %%, just invoke the print callback
13260Sstevel@tonic-gate 		 * with no data record and continue; it consumes no record.
13270Sstevel@tonic-gate 		 */
13280Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
13290Sstevel@tonic-gate 			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
13300Sstevel@tonic-gate 				continue;
13310Sstevel@tonic-gate 			return (-1); /* errno is set for us */
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
13350Sstevel@tonic-gate 			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
13360Sstevel@tonic-gate 			    len, &width) == -1)
13370Sstevel@tonic-gate 				return (-1); /* errno is set for us */
13380Sstevel@tonic-gate 			pfd->pfd_dynwidth = width;
13390Sstevel@tonic-gate 		} else {
13400Sstevel@tonic-gate 			pfd->pfd_dynwidth = 0;
13410Sstevel@tonic-gate 		}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
13440Sstevel@tonic-gate 		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
13450Sstevel@tonic-gate 			return (-1); /* errno is set for us */
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
13481017Sbmc 			/*
13491017Sbmc 			 * This should be impossible -- the compiler shouldn't
13501017Sbmc 			 * create a DT_PFCONV_AGG conversion without an
13511017Sbmc 			 * aggregation present.  Still, we'd rather fail
13521017Sbmc 			 * gracefully than blow up...
13531017Sbmc 			 */
13541017Sbmc 			if (aggsdata == NULL)
13550Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
13561017Sbmc 
13571017Sbmc 			aggdata = aggsdata[curagg];
13581017Sbmc 			agg = aggdata->dtada_desc;
13591017Sbmc 
13601017Sbmc 			/*
13611017Sbmc 			 * We increment the current aggregation variable, but
13621017Sbmc 			 * not beyond the number of aggregation variables that
13631017Sbmc 			 * we're printing. This has the (desired) effect that
13641017Sbmc 			 * DT_PFCONV_AGG conversions beyond the number of
13651017Sbmc 			 * aggregation variables (re-)convert the aggregation
13661017Sbmc 			 * value of the last aggregation variable.
13671017Sbmc 			 */
13681017Sbmc 			if (curagg < naggvars - 1)
13691017Sbmc 				curagg++;
13701017Sbmc 
13711017Sbmc 			rec = &agg->dtagd_rec[aggrec];
13721017Sbmc 			addr = aggdata->dtada_data + rec->dtrd_offset;
13731017Sbmc 			limit = addr + aggdata->dtada_size;
13741017Sbmc 			normal = aggdata->dtada_normal;
13751017Sbmc 			flags = DTRACE_BUFDATA_AGGVAL;
13760Sstevel@tonic-gate 		} else {
13770Sstevel@tonic-gate 			if (nrecs == 0)
13780Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
13791028Sbmc 
13801028Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
13811028Sbmc 				/*
13821028Sbmc 				 * When printing aggregation keys, we always
13831028Sbmc 				 * set the aggdata to be the representative
13841028Sbmc 				 * (zeroth) aggregation.  The aggdata isn't
13851028Sbmc 				 * actually used here in this case, but it is
13861028Sbmc 				 * passed to the buffer handler and must
13871028Sbmc 				 * therefore still be correct.
13881028Sbmc 				 */
13891028Sbmc 				aggdata = aggsdata[0];
13901028Sbmc 				flags = DTRACE_BUFDATA_AGGKEY;
13911028Sbmc 			}
13921028Sbmc 
13930Sstevel@tonic-gate 			rec = recp++;
13940Sstevel@tonic-gate 			nrecs--;
13951017Sbmc 			addr = (caddr_t)buf + rec->dtrd_offset;
13961017Sbmc 			limit = lim;
13971017Sbmc 			normal = 1;
13980Sstevel@tonic-gate 		}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		size = rec->dtrd_size;
14010Sstevel@tonic-gate 
14021017Sbmc 		if (addr + size > limit) {
14030Sstevel@tonic-gate 			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
14040Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, (void *)lim);
14050Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DOFFSET));
14060Sstevel@tonic-gate 		}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 		if (rec->dtrd_alignment != 0 &&
14090Sstevel@tonic-gate 		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
14100Sstevel@tonic-gate 			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
14110Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
14120Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DALIGN));
14130Sstevel@tonic-gate 		}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 		switch (rec->dtrd_action) {
14160Sstevel@tonic-gate 		case DTRACEAGG_AVG:
14170Sstevel@tonic-gate 			func = pfprint_average;
14180Sstevel@tonic-gate 			break;
14190Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
14200Sstevel@tonic-gate 			func = pfprint_quantize;
14210Sstevel@tonic-gate 			break;
14220Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
14230Sstevel@tonic-gate 			func = pfprint_lquantize;
14240Sstevel@tonic-gate 			break;
1425457Sbmc 		case DTRACEACT_MOD:
1426457Sbmc 			func = pfprint_mod;
1427457Sbmc 			break;
1428457Sbmc 		case DTRACEACT_UMOD:
1429457Sbmc 			func = pfprint_umod;
1430457Sbmc 			break;
14310Sstevel@tonic-gate 		default:
14320Sstevel@tonic-gate 			func = pfc->pfc_print;
14330Sstevel@tonic-gate 			break;
14340Sstevel@tonic-gate 		}
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
14370Sstevel@tonic-gate 			*f++ = '#';
14380Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
14390Sstevel@tonic-gate 			*f++ = '0';
1440*1222Smws 		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
14410Sstevel@tonic-gate 			*f++ = '-';
14420Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
14430Sstevel@tonic-gate 			*f++ = '+';
14440Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
14450Sstevel@tonic-gate 			*f++ = '\'';
14460Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
14470Sstevel@tonic-gate 			*f++ = ' ';
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 		/*
14500Sstevel@tonic-gate 		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
14510Sstevel@tonic-gate 		 * don't add the width to the format string.  See the block
14520Sstevel@tonic-gate 		 * comment in pfprint_stack() for a description of the
14530Sstevel@tonic-gate 		 * behavior in this case.
14540Sstevel@tonic-gate 		 */
14550Sstevel@tonic-gate 		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
14560Sstevel@tonic-gate 			width = 0;
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 		if (width != 0)
1459*1222Smws 			f += snprintf(f, sizeof (format), "%d", ABS(width));
14600Sstevel@tonic-gate 
1461*1222Smws 		if (prec > 0)
14620Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		(void) strcpy(f, pfd->pfd_fmt);
14650Sstevel@tonic-gate 		pfd->pfd_rec = rec;
14660Sstevel@tonic-gate 
14671017Sbmc 		if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
14680Sstevel@tonic-gate 			return (-1); /* errno is set for us */
1469457Sbmc 
1470457Sbmc 		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1471457Sbmc 			/*
1472457Sbmc 			 * For printa(), we flush the buffer after each tuple
14731017Sbmc 			 * element, inidicating that this is the last record
14741017Sbmc 			 * as appropriate.
1475457Sbmc 			 */
14761017Sbmc 			if (i == pfv->pfv_argc - 1)
14771017Sbmc 				flags |= DTRACE_BUFDATA_AGGLAST;
14781017Sbmc 
14791017Sbmc 			if (dt_buffered_flush(dtp, NULL,
14801017Sbmc 			    rec, aggdata, flags) < 0)
1481457Sbmc 				return (-1);
1482457Sbmc 		}
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	return ((int)(recp - recs));
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate int
14890Sstevel@tonic-gate dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
14900Sstevel@tonic-gate     const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
14910Sstevel@tonic-gate {
14920Sstevel@tonic-gate 	dtrace_optval_t size;
14930Sstevel@tonic-gate 	int rval;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "strsize", &size);
14960Sstevel@tonic-gate 	assert(rval == 0);
14970Sstevel@tonic-gate 	assert(dtp->dt_sprintf_buflen == 0);
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	if (dtp->dt_sprintf_buf != NULL)
15000Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
15030Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	bzero(dtp->dt_sprintf_buf, size);
15060Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = size;
15071017Sbmc 	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
15081017Sbmc 	    NULL, 0);
15090Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = 0;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if (rval == -1)
15120Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	return (rval);
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate /*ARGSUSED*/
15180Sstevel@tonic-gate int
15190Sstevel@tonic-gate dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15200Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15210Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	if (rval == -1)
15260Sstevel@tonic-gate 		return (rval);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	/*
15290Sstevel@tonic-gate 	 * Before we execute the specified command, flush fp to assure that
15300Sstevel@tonic-gate 	 * any prior dt_printf()'s appear before the output of the command
15310Sstevel@tonic-gate 	 * not after it.
15320Sstevel@tonic-gate 	 */
15330Sstevel@tonic-gate 	(void) fflush(fp);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	if (system(dtp->dt_sprintf_buf) == -1)
15360Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	return (rval);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate int
15420Sstevel@tonic-gate dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15430Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15440Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15450Sstevel@tonic-gate {
15460Sstevel@tonic-gate 	char selfbuf[40], restorebuf[40], *filename;
15470Sstevel@tonic-gate 	FILE *nfp;
15480Sstevel@tonic-gate 	int rval, errval;
15490Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
15500Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	if (rval == -1 || fp == NULL)
15550Sstevel@tonic-gate 		return (rval);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (pfd->pfd_preflen != 0 &&
15580Sstevel@tonic-gate 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
15590Sstevel@tonic-gate 		/*
15600Sstevel@tonic-gate 		 * The only way to have the format string set to the value
15610Sstevel@tonic-gate 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
15620Sstevel@tonic-gate 		 * denoting that we should restore the old stdout.
15630Sstevel@tonic-gate 		 */
15640Sstevel@tonic-gate 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		if (dtp->dt_stdout_fd == -1) {
15670Sstevel@tonic-gate 			/*
15680Sstevel@tonic-gate 			 * We could complain here by generating an error,
15690Sstevel@tonic-gate 			 * but it seems like overkill:  it seems that calling
15700Sstevel@tonic-gate 			 * freopen() to restore stdout when freopen() has
15710Sstevel@tonic-gate 			 * never before been called should just be a no-op,
15720Sstevel@tonic-gate 			 * so we just return in this case.
15730Sstevel@tonic-gate 			 */
15740Sstevel@tonic-gate 			return (rval);
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		(void) snprintf(restorebuf, sizeof (restorebuf),
15780Sstevel@tonic-gate 		    "/dev/fd/%d", dtp->dt_stdout_fd);
15790Sstevel@tonic-gate 		filename = restorebuf;
15800Sstevel@tonic-gate 	} else {
15810Sstevel@tonic-gate 		filename = dtp->dt_sprintf_buf;
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * freopen(3C) will always close the specified stream and underlying
15860Sstevel@tonic-gate 	 * file descriptor -- even if the specified file can't be opened.
15870Sstevel@tonic-gate 	 * Even for the semantic cesspool that is standard I/O, this is
15880Sstevel@tonic-gate 	 * surprisingly brain-dead behavior:  it means that any failure to
15890Sstevel@tonic-gate 	 * open the specified file destroys the specified stream in the
15900Sstevel@tonic-gate 	 * process -- which is particularly relevant when the specified stream
15910Sstevel@tonic-gate 	 * happens (or rather, happened) to be stdout.  This could be resolved
15920Sstevel@tonic-gate 	 * were there an "fdreopen()" equivalent of freopen() that allowed one
15930Sstevel@tonic-gate 	 * to pass a file descriptor instead of the name of a file, but there
15940Sstevel@tonic-gate 	 * is no such thing.  However, we can effect this ourselves by first
15950Sstevel@tonic-gate 	 * fopen()'ing the desired file, and then (assuming that that works),
15960Sstevel@tonic-gate 	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
15970Sstevel@tonic-gate 	 * file descriptor for the fopen()'d file.  This way, if the fopen()
15980Sstevel@tonic-gate 	 * fails, we can fail the operation without destroying stdout.
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	if ((nfp = fopen(filename, "aw")) == NULL) {
16010Sstevel@tonic-gate 		char *msg = strerror(errno), *faultstr;
16020Sstevel@tonic-gate 		int len = 80;
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		len += strlen(msg) + strlen(filename);
16050Sstevel@tonic-gate 		faultstr = alloca(len);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
16080Sstevel@tonic-gate 		    filename, strerror(errno));
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
16110Sstevel@tonic-gate 			return (rval);
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 		return (errval);
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	if (dtp->dt_stdout_fd == -1) {
16190Sstevel@tonic-gate 		/*
16200Sstevel@tonic-gate 		 * If this is the first time that we're calling freopen(),
16210Sstevel@tonic-gate 		 * we're going to stash away the file descriptor for stdout.
16220Sstevel@tonic-gate 		 * We don't expect the dup(2) to fail, so if it does we must
16230Sstevel@tonic-gate 		 * return failure.
16240Sstevel@tonic-gate 		 */
16250Sstevel@tonic-gate 		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
16260Sstevel@tonic-gate 			(void) fclose(nfp);
16270Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	if (freopen(selfbuf, "aw", fp) == NULL) {
16320Sstevel@tonic-gate 		(void) fclose(nfp);
16330Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	(void) fclose(nfp);
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	return (rval);
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate /*ARGSUSED*/
16420Sstevel@tonic-gate int
16430Sstevel@tonic-gate dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
16440Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
16450Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
16460Sstevel@tonic-gate {
1647457Sbmc 	return (dt_printf_format(dtp, fp, fmtdata,
16481017Sbmc 	    recp, nrecs, buf, len, NULL, 0));
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate void *
16520Sstevel@tonic-gate dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
16550Sstevel@tonic-gate 	dt_pfargd_t *pfd;
16560Sstevel@tonic-gate 	int i;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (pfv == NULL)
16590Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	pfd = pfv->pfv_argv;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
16640Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 		if (pfc == NULL)
16670Sstevel@tonic-gate 			continue;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 		/*
16700Sstevel@tonic-gate 		 * If the output format is not %s then we assume that we have
16710Sstevel@tonic-gate 		 * been given a correctly-sized format string, so we copy the
16720Sstevel@tonic-gate 		 * true format name including the size modifier.  If the output
16730Sstevel@tonic-gate 		 * format is %s, then either the input format is %s as well or
16740Sstevel@tonic-gate 		 * it is one of our custom formats (e.g. pfprint_addr), so we
16750Sstevel@tonic-gate 		 * must set pfd_fmt to be the output format conversion "s".
16760Sstevel@tonic-gate 		 */
16770Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") != 0)
16780Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
16790Sstevel@tonic-gate 		else
16800Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	return (pfv);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate void *
16870Sstevel@tonic-gate dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	if (pfv == NULL)
16920Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	return (pfv);
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /*ARGSUSED*/
17000Sstevel@tonic-gate size_t
17010Sstevel@tonic-gate dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
17040Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	/*
17070Sstevel@tonic-gate 	 * An upper bound on the string length is the length of the original
17080Sstevel@tonic-gate 	 * format string, plus three times the number of conversions (each
17090Sstevel@tonic-gate 	 * conversion could add up an additional "ll" and/or pfd_width digit
17100Sstevel@tonic-gate 	 * in the case of converting %? to %16) plus one for a terminating \0.
17110Sstevel@tonic-gate 	 */
17120Sstevel@tonic-gate 	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
17130Sstevel@tonic-gate 	char *format = alloca(formatlen);
17140Sstevel@tonic-gate 	char *f = format;
17150Sstevel@tonic-gate 	int i, j;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
17180Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
17190Sstevel@tonic-gate 		const char *str;
17200Sstevel@tonic-gate 		int width = pfd->pfd_width;
17210Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
17240Sstevel@tonic-gate 			for (j = 0; j < pfd->pfd_preflen; j++)
17250Sstevel@tonic-gate 				*f++ = pfd->pfd_prefix[j];
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 		if (pfc == NULL)
17290Sstevel@tonic-gate 			continue;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 		*f++ = '%';
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
17340Sstevel@tonic-gate 			*f++ = '#';
17350Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
17360Sstevel@tonic-gate 			*f++ = '0';
17370Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
17380Sstevel@tonic-gate 			*f++ = '-';
17390Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
17400Sstevel@tonic-gate 			*f++ = '+';
17410Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
17420Sstevel@tonic-gate 			*f++ = '*';
17430Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
17440Sstevel@tonic-gate 			*f++ = '.';
17450Sstevel@tonic-gate 			*f++ = '*';
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
17480Sstevel@tonic-gate 			*f++ = '\'';
17490Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
17500Sstevel@tonic-gate 			*f++ = ' ';
17510Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG)
17520Sstevel@tonic-gate 			*f++ = '@';
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 		if (width != 0)
17550Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), "%d", width);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 		if (prec != 0)
17580Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 		/*
17610Sstevel@tonic-gate 		 * If the output format is %s, then either %s is the underlying
17620Sstevel@tonic-gate 		 * conversion or the conversion is one of our customized ones,
17630Sstevel@tonic-gate 		 * e.g. pfprint_addr.  In these cases, put the original string
17640Sstevel@tonic-gate 		 * name of the conversion (pfc_name) into the pickled format
17650Sstevel@tonic-gate 		 * string rather than the derived conversion (pfd_fmt).
17660Sstevel@tonic-gate 		 */
17670Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") == 0)
17680Sstevel@tonic-gate 			str = pfc->pfc_name;
17690Sstevel@tonic-gate 		else
17700Sstevel@tonic-gate 			str = pfd->pfd_fmt;
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 		for (j = 0; str[j] != '\0'; j++)
17730Sstevel@tonic-gate 			*f++ = str[j];
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	*f = '\0'; /* insert nul byte; do not count in return value */
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	assert(f < format + formatlen);
17790Sstevel@tonic-gate 	(void) strncpy(s, format, len);
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	return ((size_t)(f - format));
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate static int
1785457Sbmc dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
17860Sstevel@tonic-gate {
1787457Sbmc 	const dtrace_aggdesc_t *agg = adp->dtada_desc;
17880Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
17890Sstevel@tonic-gate 	uint_t nrecs = agg->dtagd_nrecs;
17900Sstevel@tonic-gate 	dt_pfwalk_t *pfw = arg;
1791457Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
17920Sstevel@tonic-gate 	int id;
17930Sstevel@tonic-gate 
1794457Sbmc 	if (dt_printf_getint(dtp, recp++, nrecs--,
17950Sstevel@tonic-gate 	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
17960Sstevel@tonic-gate 		return (0); /* no aggregation id or id does not match */
17970Sstevel@tonic-gate 
1798457Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
17991017Sbmc 	    recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1800457Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18010Sstevel@tonic-gate 
1802457Sbmc 	/*
1803457Sbmc 	 * Cast away the const to set the bit indicating that this aggregation
1804457Sbmc 	 * has been printed.
1805457Sbmc 	 */
1806457Sbmc 	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	return (0);
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate 
18111017Sbmc static int
18121017Sbmc dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
18131017Sbmc {
18141017Sbmc 	const dtrace_aggdata_t *aggdata = aggsdata[0];
18151017Sbmc 	const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
18161017Sbmc 	const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
18171017Sbmc 	uint_t nrecs = agg->dtagd_nrecs - 1;
18181017Sbmc 	dt_pfwalk_t *pfw = arg;
18191017Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
18201017Sbmc 	int i;
18211017Sbmc 
18221017Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
18231017Sbmc 	    rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
18241017Sbmc 	    aggsdata, naggvars) == -1)
18251017Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18261017Sbmc 
18271017Sbmc 	/*
18281017Sbmc 	 * For each aggregation, indicate that it has been printed, casting
18291017Sbmc 	 * away the const as necessary.
18301017Sbmc 	 */
18311017Sbmc 	for (i = 1; i < naggvars; i++) {
18321017Sbmc 		agg = aggsdata[i]->dtada_desc;
18331017Sbmc 		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18341017Sbmc 	}
18351017Sbmc 
18361017Sbmc 	return (0);
18371017Sbmc }
18380Sstevel@tonic-gate /*ARGSUSED*/
18390Sstevel@tonic-gate int
18400Sstevel@tonic-gate dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
18410Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
18420Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate 	dt_pfwalk_t pfw;
18451017Sbmc 	int i, naggvars = 0;
18461017Sbmc 	dtrace_aggvarid_t *aggvars;
18471017Sbmc 
18481017Sbmc 	aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
18491017Sbmc 
18501017Sbmc 	/*
18511017Sbmc 	 * This might be a printa() with multiple aggregation variables.  We
18521017Sbmc 	 * need to scan forward through the records until we find a record from
18531017Sbmc 	 * a different statement.
18541017Sbmc 	 */
18551017Sbmc 	for (i = 0; i < nrecs; i++) {
18561017Sbmc 		const dtrace_recdesc_t *nrec = &recs[i];
18570Sstevel@tonic-gate 
18581017Sbmc 		if (nrec->dtrd_uarg != recs->dtrd_uarg)
18591017Sbmc 			break;
18601017Sbmc 
18611017Sbmc 		if (nrec->dtrd_action != recs->dtrd_action)
18621017Sbmc 			return (dt_set_errno(dtp, EDT_BADAGG));
18631017Sbmc 
18641017Sbmc 		aggvars[naggvars++] =
18651017Sbmc 		    /* LINTED - alignment */
18661017Sbmc 		    *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
18671017Sbmc 	}
18681017Sbmc 
18691017Sbmc 	if (naggvars == 0)
18701017Sbmc 		return (dt_set_errno(dtp, EDT_BADAGG));
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	pfw.pfw_argv = fmtdata;
18730Sstevel@tonic-gate 	pfw.pfw_fp = fp;
18740Sstevel@tonic-gate 	pfw.pfw_err = 0;
18750Sstevel@tonic-gate 
18761017Sbmc 	if (naggvars == 1) {
18771017Sbmc 		pfw.pfw_aid = aggvars[0];
18780Sstevel@tonic-gate 
18791017Sbmc 		if (dtrace_aggregate_walk_sorted(dtp,
18801017Sbmc 		    dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
18811017Sbmc 			return (-1); /* errno is set for us */
18821017Sbmc 	} else {
18831017Sbmc 		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
18841017Sbmc 		    dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
18851017Sbmc 			return (-1); /* errno is set for us */
18861017Sbmc 	}
18871017Sbmc 
18881017Sbmc 	return (i);
18890Sstevel@tonic-gate }
1890