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