xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_printf.c (revision 9531:dc8924ef7839)
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
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211222Smws 
220Sstevel@tonic-gate /*
23*9531Srafael.vanoni@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/sysmacros.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <alloca.h>
310Sstevel@tonic-gate #include <assert.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <dt_printf.h>
370Sstevel@tonic-gate #include <dt_string.h>
380Sstevel@tonic-gate #include <dt_impl.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*ARGSUSED*/
410Sstevel@tonic-gate static int
42457Sbmc pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
450Sstevel@tonic-gate }
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*ARGSUSED*/
480Sstevel@tonic-gate static int
49457Sbmc pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
50457Sbmc {
51457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
52457Sbmc 	    dt_node_is_symaddr(dnp));
53457Sbmc }
54457Sbmc 
55457Sbmc /*ARGSUSED*/
56457Sbmc static int
57457Sbmc pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
58457Sbmc {
59457Sbmc 	dtrace_hdl_t *dtp = pfv->pfv_dtp;
60457Sbmc 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
61457Sbmc 
62457Sbmc 	if (dt_node_is_usymaddr(dnp))
63457Sbmc 		return (1);
64457Sbmc 
65457Sbmc 	if (idp == NULL || idp->di_id == 0)
66457Sbmc 		return (0);
67457Sbmc 
68457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
69457Sbmc }
70457Sbmc 
71457Sbmc /*ARGSUSED*/
72457Sbmc static int
73457Sbmc pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
74457Sbmc {
75457Sbmc 	return (dt_node_is_stack(dnp));
76457Sbmc }
77457Sbmc 
78457Sbmc /*ARGSUSED*/
79457Sbmc static int
801017Sbmc pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
811017Sbmc {
821017Sbmc 	return (dt_node_is_integer(dnp) &&
831017Sbmc 	    dt_node_type_size(dnp) == sizeof (uint64_t));
841017Sbmc }
851017Sbmc 
861017Sbmc /*ARGSUSED*/
871017Sbmc static int
88457Sbmc pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	ctf_file_t *ctfp;
910Sstevel@tonic-gate 	ctf_encoding_t e;
920Sstevel@tonic-gate 	ctf_arinfo_t r;
930Sstevel@tonic-gate 	ctf_id_t base;
940Sstevel@tonic-gate 	uint_t kind;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (dt_node_is_string(dnp))
970Sstevel@tonic-gate 		return (1);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	ctfp = dnp->dn_ctfp;
1000Sstevel@tonic-gate 	base = ctf_type_resolve(ctfp, dnp->dn_type);
1010Sstevel@tonic-gate 	kind = ctf_type_kind(ctfp, base);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1040Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1050Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*ARGSUSED*/
1090Sstevel@tonic-gate static int
110457Sbmc pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1130Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
1140Sstevel@tonic-gate 	uint_t kind = ctf_type_kind(ctfp, base);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	ctf_encoding_t e;
1170Sstevel@tonic-gate 	ctf_arinfo_t r;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1200Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1210Sstevel@tonic-gate 	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
1220Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*ARGSUSED*/
1260Sstevel@tonic-gate static int
127457Sbmc pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	return (dt_node_is_integer(dnp) &&
1300Sstevel@tonic-gate 	    dt_node_type_size(dnp) <= sizeof (int));
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*ARGSUSED*/
1340Sstevel@tonic-gate static int
135457Sbmc pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	return (dt_node_is_float(dnp));
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*ARGSUSED*/
1410Sstevel@tonic-gate static int
142457Sbmc pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
147457Sbmc /*ARGSUSED*/
1480Sstevel@tonic-gate static int
149457Sbmc pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	if (dnp->dn_flags & DT_NF_SIGNED)
1520Sstevel@tonic-gate 		pfd->pfd_flags |= DT_PFCONV_SIGNED;
1530Sstevel@tonic-gate 	else
1540Sstevel@tonic-gate 		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*ARGSUSED*/
1600Sstevel@tonic-gate static int
161457Sbmc pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1640Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1650Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1680Sstevel@tonic-gate 	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
1690Sstevel@tonic-gate 	    strcmp(n, "unsigned short") == 0));
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*ARGSUSED*/
1730Sstevel@tonic-gate static int
174457Sbmc pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1770Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1780Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1810Sstevel@tonic-gate 	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
1820Sstevel@tonic-gate 	    strcmp(n, "unsigned long") == 0));
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*ARGSUSED*/
1860Sstevel@tonic-gate static int
187457Sbmc pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1900Sstevel@tonic-gate 	ctf_id_t type = dnp->dn_type;
1910Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
1940Sstevel@tonic-gate 	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
1950Sstevel@tonic-gate 	    strcmp(n, "signed long long") == 0 ||
1960Sstevel@tonic-gate 	    strcmp(n, "unsigned long long") == 0))
1970Sstevel@tonic-gate 		return (1);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * If the type used for %llx or %llX is not an [unsigned] long long, we
2010Sstevel@tonic-gate 	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
2020Sstevel@tonic-gate 	 * that these typedefs are guaranteed to work with %ll[xX] in either
2030Sstevel@tonic-gate 	 * compilation environment even though they alias to "long" in LP64.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
2060Sstevel@tonic-gate 		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
2070Sstevel@tonic-gate 		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
2080Sstevel@tonic-gate 			return (1);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		type = ctf_type_reference(ctfp, type);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	return (0);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
216457Sbmc /*ARGSUSED*/
2170Sstevel@tonic-gate static int
218457Sbmc pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
2210Sstevel@tonic-gate 	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*ARGSUSED*/
2250Sstevel@tonic-gate static int
2260Sstevel@tonic-gate pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2270Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	int64_t normal = (int64_t)unormal;
2300Sstevel@tonic-gate 	int32_t n = (int32_t)normal;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	switch (size) {
2330Sstevel@tonic-gate 	case sizeof (int8_t):
2340Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2350Sstevel@tonic-gate 		    (int32_t)*((int8_t *)addr) / n));
2360Sstevel@tonic-gate 	case sizeof (int16_t):
2370Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2380Sstevel@tonic-gate 		    (int32_t)*((int16_t *)addr) / n));
2390Sstevel@tonic-gate 	case sizeof (int32_t):
2400Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2410Sstevel@tonic-gate 		    *((int32_t *)addr) / n));
2420Sstevel@tonic-gate 	case sizeof (int64_t):
2430Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2440Sstevel@tonic-gate 		    *((int64_t *)addr) / normal));
2450Sstevel@tonic-gate 	default:
2460Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*ARGSUSED*/
2510Sstevel@tonic-gate static int
2520Sstevel@tonic-gate pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2530Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	uint32_t n = (uint32_t)normal;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	switch (size) {
2580Sstevel@tonic-gate 	case sizeof (uint8_t):
2590Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2600Sstevel@tonic-gate 		    (uint32_t)*((uint8_t *)addr) / n));
2610Sstevel@tonic-gate 	case sizeof (uint16_t):
2620Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2630Sstevel@tonic-gate 		    (uint32_t)*((uint16_t *)addr) / n));
2640Sstevel@tonic-gate 	case sizeof (uint32_t):
2650Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2660Sstevel@tonic-gate 		    *((uint32_t *)addr) / n));
2670Sstevel@tonic-gate 	case sizeof (uint64_t):
2680Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2690Sstevel@tonic-gate 		    *((uint64_t *)addr) / normal));
2700Sstevel@tonic-gate 	default:
2710Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static int
2760Sstevel@tonic-gate pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2770Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
2800Sstevel@tonic-gate 		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
2810Sstevel@tonic-gate 	else
2820Sstevel@tonic-gate 		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*ARGSUSED*/
2860Sstevel@tonic-gate static int
2870Sstevel@tonic-gate pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2880Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	double n = (double)normal;
2910Sstevel@tonic-gate 	long double ldn = (long double)normal;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	switch (size) {
2940Sstevel@tonic-gate 	case sizeof (float):
2950Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2960Sstevel@tonic-gate 		    (double)*((float *)addr) / n));
2970Sstevel@tonic-gate 	case sizeof (double):
2980Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2990Sstevel@tonic-gate 		    *((double *)addr) / n));
3000Sstevel@tonic-gate 	case sizeof (long double):
3010Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3020Sstevel@tonic-gate 		    *((long double *)addr) / ldn));
3030Sstevel@tonic-gate 	default:
3040Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /*ARGSUSED*/
3090Sstevel@tonic-gate static int
3100Sstevel@tonic-gate pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3110Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	char *s;
314457Sbmc 	int n, len = 256;
315457Sbmc 	uint64_t val;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	switch (size) {
3180Sstevel@tonic-gate 	case sizeof (uint32_t):
3190Sstevel@tonic-gate 		val = *((uint32_t *)addr);
3200Sstevel@tonic-gate 		break;
3210Sstevel@tonic-gate 	case sizeof (uint64_t):
3220Sstevel@tonic-gate 		val = *((uint64_t *)addr);
3230Sstevel@tonic-gate 		break;
3240Sstevel@tonic-gate 	default:
3250Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
328457Sbmc 	do {
329457Sbmc 		n = len;
330457Sbmc 		s = alloca(n);
331457Sbmc 	} while ((len = dtrace_addr2str(dtp, val, s, n)) >= n);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*ARGSUSED*/
3370Sstevel@tonic-gate static int
338457Sbmc pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
339457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
340457Sbmc {
341457Sbmc 	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
342457Sbmc }
343457Sbmc 
344457Sbmc /*ARGSUSED*/
345457Sbmc static int
346457Sbmc pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
347457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
348457Sbmc {
349457Sbmc 	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
350457Sbmc }
351457Sbmc 
352457Sbmc /*ARGSUSED*/
353457Sbmc static int
3540Sstevel@tonic-gate pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3550Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3560Sstevel@tonic-gate {
357457Sbmc 	char *s;
358457Sbmc 	int n, len = 256;
359457Sbmc 	uint64_t val, pid = 0;
360457Sbmc 
3610Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	switch (size) {
3640Sstevel@tonic-gate 	case sizeof (uint32_t):
3650Sstevel@tonic-gate 		val = (u_longlong_t)*((uint32_t *)addr);
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	case sizeof (uint64_t):
3680Sstevel@tonic-gate 		val = (u_longlong_t)*((uint64_t *)addr);
3690Sstevel@tonic-gate 		break;
370457Sbmc 	case sizeof (uint64_t) * 2:
371457Sbmc 		pid = ((uint64_t *)(uintptr_t)addr)[0];
372457Sbmc 		val = ((uint64_t *)(uintptr_t)addr)[1];
373457Sbmc 		break;
3740Sstevel@tonic-gate 	default:
3750Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
378457Sbmc 	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
379457Sbmc 		pid = idp->di_id;
3800Sstevel@tonic-gate 
381457Sbmc 	do {
382457Sbmc 		n = len;
383457Sbmc 		s = alloca(n);
384457Sbmc 	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) >= n);
3850Sstevel@tonic-gate 
386457Sbmc 	return (dt_printf(dtp, fp, format, s));
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*ARGSUSED*/
3900Sstevel@tonic-gate static int
3910Sstevel@tonic-gate pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3920Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
3930Sstevel@tonic-gate {
394457Sbmc 	int width;
3950Sstevel@tonic-gate 	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
3960Sstevel@tonic-gate 	const dtrace_recdesc_t *rec = pfd->pfd_rec;
3970Sstevel@tonic-gate 	caddr_t addr = (caddr_t)vaddr;
3980Sstevel@tonic-gate 	int err = 0;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * We have stashed the value of the STACKINDENT option, and we will
4020Sstevel@tonic-gate 	 * now override it for the purposes of formatting the stack.  If the
4030Sstevel@tonic-gate 	 * field has been specified as left-aligned (i.e. (%-#), we set the
4040Sstevel@tonic-gate 	 * indentation to be the width.  This is a slightly odd semantic, but
4050Sstevel@tonic-gate 	 * it's useful functionality -- and it's slightly odd to begin with to
4060Sstevel@tonic-gate 	 * be using a single format specifier to be formatting multiple lines
4070Sstevel@tonic-gate 	 * of text...
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	if (pfd->pfd_dynwidth < 0) {
4100Sstevel@tonic-gate 		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
4110Sstevel@tonic-gate 		width = -pfd->pfd_dynwidth;
4120Sstevel@tonic-gate 	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
4130Sstevel@tonic-gate 		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
4140Sstevel@tonic-gate 	} else {
4150Sstevel@tonic-gate 		width = 0;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	switch (rec->dtrd_action) {
4210Sstevel@tonic-gate 	case DTRACEACT_USTACK:
4220Sstevel@tonic-gate 	case DTRACEACT_JSTACK:
4230Sstevel@tonic-gate 		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
4240Sstevel@tonic-gate 		break;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	case DTRACEACT_STACK:
427457Sbmc 		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
428457Sbmc 		    rec->dtrd_size / rec->dtrd_arg);
4290Sstevel@tonic-gate 		break;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	default:
4320Sstevel@tonic-gate 		assert(0);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	return (err);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*ARGSUSED*/
4410Sstevel@tonic-gate static int
4420Sstevel@tonic-gate pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4430Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	char src[32], buf[32], *dst = buf;
4460Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4470Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4480Sstevel@tonic-gate 	int i;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
4520Sstevel@tonic-gate 	 * Below, we turn this into the canonical adb/mdb /[yY] format,
4530Sstevel@tonic-gate 	 * "1973 Dec  3 17:20:00".
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	(void) ctime_r(&sec, src, sizeof (src));
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * Place the 4-digit year at the head of the string...
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	for (i = 20; i < 24; i++)
4610Sstevel@tonic-gate 		*dst++ = src[i];
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/*
4640Sstevel@tonic-gate 	 * ...and follow it with the remainder (month, day, hh:mm:ss).
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	for (i = 3; i < 19; i++)
4670Sstevel@tonic-gate 		*dst++ = src[i];
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	*dst = '\0';
4700Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate  * This prints the time in RFC 822 standard form.  This is useful for emitting
4750Sstevel@tonic-gate  * notions of time that are consumed by standard tools (e.g., as part of an
4760Sstevel@tonic-gate  * RSS feed).
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate /*ARGSUSED*/
4790Sstevel@tonic-gate static int
4800Sstevel@tonic-gate pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4810Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4840Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4850Sstevel@tonic-gate 	struct tm tm;
4860Sstevel@tonic-gate 	char buf[64];
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	(void) localtime_r(&sec, &tm);
4890Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
4900Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*ARGSUSED*/
4940Sstevel@tonic-gate static int
4950Sstevel@tonic-gate pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4960Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	char *s = alloca(size + 1);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	bcopy(addr, s, size);
5010Sstevel@tonic-gate 	s[size] = '\0';
5020Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*ARGSUSED*/
5060Sstevel@tonic-gate static int
5070Sstevel@tonic-gate pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5080Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	wchar_t *ws = alloca(size + sizeof (wchar_t));
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	bcopy(addr, ws, size);
5130Sstevel@tonic-gate 	ws[size / sizeof (wchar_t)] = L'\0';
5140Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, ws));
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*ARGSUSED*/
5180Sstevel@tonic-gate static int
5190Sstevel@tonic-gate pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5200Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	char *s;
5230Sstevel@tonic-gate 	int n;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if ((s = strchr2esc(addr, size)) == NULL)
5260Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	n = dt_printf(dtp, fp, format, s);
5290Sstevel@tonic-gate 	free(s);
5300Sstevel@tonic-gate 	return (n);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate static int
5340Sstevel@tonic-gate pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5350Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	char c;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	switch (size) {
5400Sstevel@tonic-gate 	case sizeof (int8_t):
5410Sstevel@tonic-gate 		c = *(int8_t *)addr;
5420Sstevel@tonic-gate 		break;
5430Sstevel@tonic-gate 	case sizeof (int16_t):
5440Sstevel@tonic-gate 		c = *(int16_t *)addr;
5450Sstevel@tonic-gate 		break;
5460Sstevel@tonic-gate 	case sizeof (int32_t):
5470Sstevel@tonic-gate 		c = *(int32_t *)addr;
5480Sstevel@tonic-gate 		break;
5490Sstevel@tonic-gate 	default:
5500Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*ARGSUSED*/
5570Sstevel@tonic-gate static int
5580Sstevel@tonic-gate pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5590Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	return (dt_printf(dtp, fp, "%%"));
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate static const char pfproto_xint[] = "char, short, int, long, or long long";
5650Sstevel@tonic-gate static const char pfproto_csi[] = "char, short, or int";
5660Sstevel@tonic-gate static const char pfproto_fp[] = "float, double, or long double";
5670Sstevel@tonic-gate static const char pfproto_addr[] = "pointer or integer";
568457Sbmc static const char pfproto_uaddr[] =
569457Sbmc 	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
5700Sstevel@tonic-gate static const char pfproto_cstr[] = "char [] or string (or use stringof)";
5710Sstevel@tonic-gate static const char pfproto_wstr[] = "wchar_t []";
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate  * Printf format conversion dictionary.  This table should match the set of
5750Sstevel@tonic-gate  * conversions offered by printf(3C), as well as some additional extensions.
5760Sstevel@tonic-gate  * The second parameter is an ASCII string which is either an actual type
5770Sstevel@tonic-gate  * name we should look up (if pfcheck_type is specified), or just a descriptive
5780Sstevel@tonic-gate  * string of the types expected for use in error messages.
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate static const dt_pfconv_t _dtrace_conversions[] = {
581457Sbmc { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
582457Sbmc { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
5830Sstevel@tonic-gate { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
5840Sstevel@tonic-gate { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
5850Sstevel@tonic-gate { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
5860Sstevel@tonic-gate { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
5870Sstevel@tonic-gate { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
5880Sstevel@tonic-gate { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
5890Sstevel@tonic-gate { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
5900Sstevel@tonic-gate { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
5910Sstevel@tonic-gate { "hd", "d", "short", pfcheck_type, pfprint_sint },
5920Sstevel@tonic-gate { "hi", "i", "short", pfcheck_type, pfprint_sint },
5930Sstevel@tonic-gate { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
5940Sstevel@tonic-gate { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
5950Sstevel@tonic-gate { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
5960Sstevel@tonic-gate { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
5970Sstevel@tonic-gate { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
598457Sbmc { "k", "s", "stack", pfcheck_stack, pfprint_stack },
5990Sstevel@tonic-gate { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
6000Sstevel@tonic-gate { "ld",	"d", "long", pfcheck_type, pfprint_sint },
6010Sstevel@tonic-gate { "li",	"i", "long", pfcheck_type, pfprint_sint },
6020Sstevel@tonic-gate { "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
6030Sstevel@tonic-gate { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
6040Sstevel@tonic-gate { "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6050Sstevel@tonic-gate { "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
6060Sstevel@tonic-gate { "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
6070Sstevel@tonic-gate { "lld", "d", "long long", pfcheck_type, pfprint_sint },
6080Sstevel@tonic-gate { "lli", "i", "long long", pfcheck_type, pfprint_sint },
6090Sstevel@tonic-gate { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
6100Sstevel@tonic-gate { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
6110Sstevel@tonic-gate { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
6120Sstevel@tonic-gate { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
6130Sstevel@tonic-gate { "Le",	"e", "long double", pfcheck_type, pfprint_fp },
6140Sstevel@tonic-gate { "LE",	"E", "long double", pfcheck_type, pfprint_fp },
6150Sstevel@tonic-gate { "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
6160Sstevel@tonic-gate { "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
6170Sstevel@tonic-gate { "LG",	"G", "long double", pfcheck_type, pfprint_fp },
6180Sstevel@tonic-gate { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
6190Sstevel@tonic-gate { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
6200Sstevel@tonic-gate { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
6210Sstevel@tonic-gate { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
6221017Sbmc { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
6230Sstevel@tonic-gate { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
6240Sstevel@tonic-gate { "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
6250Sstevel@tonic-gate { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6260Sstevel@tonic-gate { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
6270Sstevel@tonic-gate { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
6281017Sbmc { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
6290Sstevel@tonic-gate { "%", "%", "void", pfcheck_type, pfprint_pct },
6300Sstevel@tonic-gate { NULL, NULL, NULL, NULL, NULL }
6310Sstevel@tonic-gate };
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate int
6340Sstevel@tonic-gate dt_pfdict_create(dtrace_hdl_t *dtp)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	uint_t n = _dtrace_strbuckets;
6370Sstevel@tonic-gate 	const dt_pfconv_t *pfd;
6380Sstevel@tonic-gate 	dt_pfdict_t *pdi;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
6410Sstevel@tonic-gate 	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
6420Sstevel@tonic-gate 		free(pdi);
6430Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	dtp->dt_pfdict = pdi;
6470Sstevel@tonic-gate 	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
6480Sstevel@tonic-gate 	pdi->pdi_nbuckets = n;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
6510Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
6520Sstevel@tonic-gate 		dt_pfconv_t *pfc;
6530Sstevel@tonic-gate 		uint_t h;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
6560Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6570Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
6610Sstevel@tonic-gate 		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
6620Sstevel@tonic-gate 		pfc->pfc_next = pdi->pdi_buckets[h];
6630Sstevel@tonic-gate 		pdi->pdi_buckets[h] = pfc;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		dtt.dtt_ctfp = NULL;
6660Sstevel@tonic-gate 		dtt.dtt_type = CTF_ERR;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/*
6690Sstevel@tonic-gate 		 * The "D" container or its parent must contain a definition of
6700Sstevel@tonic-gate 		 * any type referenced by a printf conversion.  If none can be
6710Sstevel@tonic-gate 		 * found, we fail to initialize the printf dictionary.
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6740Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
6750Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6760Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOCONV));
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		pfc->pfc_dctfp = dtt.dtt_ctfp;
6800Sstevel@tonic-gate 		pfc->pfc_dtype = dtt.dtt_type;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * The "C" container may contain an alternate definition of an
6840Sstevel@tonic-gate 		 * explicit conversion type.  If it does, use it; otherwise
6850Sstevel@tonic-gate 		 * just set pfc_ctype to pfc_dtype so it is always valid.
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6880Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
6890Sstevel@tonic-gate 			pfc->pfc_cctfp = dtt.dtt_ctfp;
6900Sstevel@tonic-gate 			pfc->pfc_ctype = dtt.dtt_type;
6910Sstevel@tonic-gate 		} else {
6920Sstevel@tonic-gate 			pfc->pfc_cctfp = pfc->pfc_dctfp;
6930Sstevel@tonic-gate 			pfc->pfc_ctype = pfc->pfc_dtype;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
6970Sstevel@tonic-gate 		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
6980Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6990Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADCONV));
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (0);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate void
7090Sstevel@tonic-gate dt_pfdict_destroy(dtrace_hdl_t *dtp)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7120Sstevel@tonic-gate 	dt_pfconv_t *pfc, *nfc;
7130Sstevel@tonic-gate 	uint_t i;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (pdi == NULL)
7160Sstevel@tonic-gate 		return;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	for (i = 0; i < pdi->pdi_nbuckets; i++) {
7190Sstevel@tonic-gate 		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
7200Sstevel@tonic-gate 			nfc = pfc->pfc_next;
7210Sstevel@tonic-gate 			free(pfc);
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	free(pdi->pdi_buckets);
7260Sstevel@tonic-gate 	free(pdi);
7270Sstevel@tonic-gate 	dtp->dt_pfdict = NULL;
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static const dt_pfconv_t *
7310Sstevel@tonic-gate dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7340Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
7350Sstevel@tonic-gate 	const dt_pfconv_t *pfc;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
7380Sstevel@tonic-gate 		if (strcmp(pfc->pfc_name, name) == 0)
7390Sstevel@tonic-gate 			break;
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	return (pfc);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate static dt_pfargv_t *
7460Sstevel@tonic-gate dt_printf_error(dtrace_hdl_t *dtp, int err)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	if (yypcb != NULL)
7490Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, err);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
7520Sstevel@tonic-gate 	return (NULL);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate dt_pfargv_t *
7560Sstevel@tonic-gate dt_printf_create(dtrace_hdl_t *dtp, const char *s)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd = NULL;
7590Sstevel@tonic-gate 	dt_pfargv_t *pfv;
7600Sstevel@tonic-gate 	const char *p, *q;
7610Sstevel@tonic-gate 	char *format;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
7640Sstevel@tonic-gate 	    (format = strdup(s)) == NULL) {
7650Sstevel@tonic-gate 		free(pfv);
7660Sstevel@tonic-gate 		return (dt_printf_error(dtp, EDT_NOMEM));
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	pfv->pfv_format = format;
7700Sstevel@tonic-gate 	pfv->pfv_argv = NULL;
7710Sstevel@tonic-gate 	pfv->pfv_argc = 0;
7720Sstevel@tonic-gate 	pfv->pfv_flags = 0;
773457Sbmc 	pfv->pfv_dtp = dtp;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
7760Sstevel@tonic-gate 		uint_t namelen = 0;
7770Sstevel@tonic-gate 		int digits = 0;
7780Sstevel@tonic-gate 		int dot = 0;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		char name[8];
7810Sstevel@tonic-gate 		char c;
7820Sstevel@tonic-gate 		int n;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
7850Sstevel@tonic-gate 			dt_printf_destroy(pfv);
7860Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
7900Sstevel@tonic-gate 			nfd->pfd_next = pfd;
7910Sstevel@tonic-gate 		else
7920Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
7950Sstevel@tonic-gate 		pfv->pfv_argc++;
7960Sstevel@tonic-gate 		nfd = pfd;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		if (p > q) {
7990Sstevel@tonic-gate 			pfd->pfd_preflen = (size_t)(p - q);
8000Sstevel@tonic-gate 			pfd->pfd_prefix = q;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		fmt_switch:
8040Sstevel@tonic-gate 		switch (c = *++p) {
8050Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
8060Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
8070Sstevel@tonic-gate 			if (dot == 0 && digits == 0 && c == '0') {
8080Sstevel@tonic-gate 				pfd->pfd_flags |= DT_PFCONV_ZPAD;
8090Sstevel@tonic-gate 				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
8100Sstevel@tonic-gate 				goto fmt_switch;
8110Sstevel@tonic-gate 			}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 			for (n = 0; isdigit(c); c = *++p)
8140Sstevel@tonic-gate 				n = n * 10 + c - '0';
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 			if (dot)
8170Sstevel@tonic-gate 				pfd->pfd_prec = n;
8180Sstevel@tonic-gate 			else
8190Sstevel@tonic-gate 				pfd->pfd_width = n;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 			p--;
8220Sstevel@tonic-gate 			digits++;
8230Sstevel@tonic-gate 			goto fmt_switch;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		case '#':
8260Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_ALT;
8270Sstevel@tonic-gate 			goto fmt_switch;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		case '*':
8300Sstevel@tonic-gate 			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 			if (pfd->pfd_flags & n) {
8330Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8340Sstevel@tonic-gate 				    "one '*' specified for the output %s\n",
8350Sstevel@tonic-gate 				    pfv->pfv_argc, n ? "precision" : "width");
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8380Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8390Sstevel@tonic-gate 			}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 			pfd->pfd_flags |= n;
8420Sstevel@tonic-gate 			goto fmt_switch;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		case '+':
8450Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPOS;
8460Sstevel@tonic-gate 			goto fmt_switch;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		case '-':
8490Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_LEFT;
8500Sstevel@tonic-gate 			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
8510Sstevel@tonic-gate 			goto fmt_switch;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 		case '.':
8540Sstevel@tonic-gate 			if (dot++ != 0) {
8550Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8560Sstevel@tonic-gate 				    "one '.' specified\n", pfv->pfv_argc);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8590Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 			digits = 0;
8620Sstevel@tonic-gate 			goto fmt_switch;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		case '?':
8650Sstevel@tonic-gate 			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
8660Sstevel@tonic-gate 				pfd->pfd_width = 16;
8670Sstevel@tonic-gate 			else
8680Sstevel@tonic-gate 				pfd->pfd_width = 8;
8690Sstevel@tonic-gate 			goto fmt_switch;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		case '@':
8720Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_AGG;
8730Sstevel@tonic-gate 			goto fmt_switch;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		case '\'':
8760Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_GROUP;
8770Sstevel@tonic-gate 			goto fmt_switch;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		case ' ':
8800Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPACE;
8810Sstevel@tonic-gate 			goto fmt_switch;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		case '$':
8840Sstevel@tonic-gate 			yywarn("format conversion #%u uses unsupported "
8850Sstevel@tonic-gate 			    "positional format (%%n$)\n", pfv->pfv_argc);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8880Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		case '%':
8910Sstevel@tonic-gate 			if (p[-1] == '%')
8920Sstevel@tonic-gate 				goto default_lbl; /* if %% then use "%" conv */
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 			yywarn("format conversion #%u cannot be combined "
8950Sstevel@tonic-gate 			    "with other format flags: %%%%\n", pfv->pfv_argc);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8980Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		case '\0':
9010Sstevel@tonic-gate 			yywarn("format conversion #%u name expected before "
9020Sstevel@tonic-gate 			    "end of format string\n", pfv->pfv_argc);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9050Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 		case 'h':
9080Sstevel@tonic-gate 		case 'l':
9090Sstevel@tonic-gate 		case 'L':
9100Sstevel@tonic-gate 		case 'w':
9110Sstevel@tonic-gate 			if (namelen < sizeof (name) - 2)
9120Sstevel@tonic-gate 				name[namelen++] = c;
9130Sstevel@tonic-gate 			goto fmt_switch;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		default_lbl:
9160Sstevel@tonic-gate 		default:
9170Sstevel@tonic-gate 			name[namelen++] = c;
9180Sstevel@tonic-gate 			name[namelen] = '\0';
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		if (pfd->pfd_conv == NULL) {
9240Sstevel@tonic-gate 			yywarn("format conversion #%u is undefined: %%%s\n",
9250Sstevel@tonic-gate 			    pfv->pfv_argc, name);
9260Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9270Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (*q != '\0' || *format == '\0') {
9320Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
9330Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9340Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
9380Sstevel@tonic-gate 			nfd->pfd_next = pfd;
9390Sstevel@tonic-gate 		else
9400Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
9430Sstevel@tonic-gate 		pfv->pfv_argc++;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		pfd->pfd_prefix = q;
9460Sstevel@tonic-gate 		pfd->pfd_preflen = strlen(q);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	return (pfv);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate void
9530Sstevel@tonic-gate dt_printf_destroy(dt_pfargv_t *pfv)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
9580Sstevel@tonic-gate 		nfd = pfd->pfd_next;
9590Sstevel@tonic-gate 		free(pfd);
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	free(pfv->pfv_format);
9630Sstevel@tonic-gate 	free(pfv);
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate void
9670Sstevel@tonic-gate dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
9680Sstevel@tonic-gate     dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
9710Sstevel@tonic-gate 	const char *func = idp->di_name;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
9740Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
9750Sstevel@tonic-gate 	const char *aggtype;
9760Sstevel@tonic-gate 	dt_node_t aggnode;
9770Sstevel@tonic-gate 	int i, j;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	if (pfv->pfv_format[0] == '\0') {
9800Sstevel@tonic-gate 		xyerror(D_PRINTF_FMT_EMPTY,
9810Sstevel@tonic-gate 		    "%s( ) format string is empty\n", func);
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 
984457Sbmc 	pfv->pfv_flags = flags;
985457Sbmc 
9860Sstevel@tonic-gate 	/*
9870Sstevel@tonic-gate 	 * We fake up a parse node representing the type that can be used with
988491Sbmc 	 * an aggregation result conversion, which -- for all but count() --
989491Sbmc 	 * is a signed quantity.
9900Sstevel@tonic-gate 	 */
991491Sbmc 	if (kind != DTRACEAGG_COUNT)
9920Sstevel@tonic-gate 		aggtype = "int64_t";
9930Sstevel@tonic-gate 	else
9940Sstevel@tonic-gate 		aggtype = "uint64_t";
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if (dt_type_lookup(aggtype, &dtt) != 0)
9970Sstevel@tonic-gate 		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	bzero(&aggnode, sizeof (aggnode));
10000Sstevel@tonic-gate 	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
10030Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
10040Sstevel@tonic-gate 		const char *dyns[2];
10050Sstevel@tonic-gate 		int dync = 0;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		char vname[64];
10080Sstevel@tonic-gate 		dt_node_t *vnp;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 		if (pfc == NULL)
10110Sstevel@tonic-gate 			continue; /* no checking if argd is just a prefix */
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
10140Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10150Sstevel@tonic-gate 			continue;
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
10190Sstevel@tonic-gate 			dyns[dync++] = ".*";
10200Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
10210Sstevel@tonic-gate 			dyns[dync++] = "*";
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		for (; dync != 0; dync--) {
10240Sstevel@tonic-gate 			if (dnp == NULL) {
10250Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_PROTO,
10260Sstevel@tonic-gate 				    "%s( ) prototype mismatch: conversion "
10270Sstevel@tonic-gate 				    "#%d (%%%s) is missing a corresponding "
10280Sstevel@tonic-gate 				    "\"%s\" argument\n", func, i + 1,
10290Sstevel@tonic-gate 				    pfc->pfc_name, dyns[dync - 1]);
10300Sstevel@tonic-gate 			}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 			if (dt_node_is_integer(dnp) == 0) {
10330Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_TYPE,
10340Sstevel@tonic-gate 				    "%s( ) argument #%d is incompatible "
10350Sstevel@tonic-gate 				    "with conversion #%d prototype:\n"
10360Sstevel@tonic-gate 				    "\tconversion: %% %s %s\n"
10370Sstevel@tonic-gate 				    "\t prototype: int\n\t  argument: %s\n",
10380Sstevel@tonic-gate 				    func, j + foff + 1, i + 1,
10390Sstevel@tonic-gate 				    dyns[dync - 1], pfc->pfc_name,
10400Sstevel@tonic-gate 				    dt_node_type_name(dnp, n, sizeof (n)));
10410Sstevel@tonic-gate 			}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			dnp = dnp->dn_list;
10440Sstevel@tonic-gate 			j++;
10450Sstevel@tonic-gate 		}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		/*
10480Sstevel@tonic-gate 		 * If this conversion is consuming the aggregation data, set
10490Sstevel@tonic-gate 		 * the value node pointer (vnp) to a fake node based on the
10500Sstevel@tonic-gate 		 * aggregating function result type.  Otherwise assign vnp to
10510Sstevel@tonic-gate 		 * the next parse node in the argument list, if there is one.
10520Sstevel@tonic-gate 		 */
10530Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
10540Sstevel@tonic-gate 			if (!(flags & DT_PRINTF_AGGREGATION)) {
10550Sstevel@tonic-gate 				xyerror(D_PRINTF_AGG_CONV,
10560Sstevel@tonic-gate 				    "%%@ conversion requires an aggregation"
10570Sstevel@tonic-gate 				    " and is not for use with %s( )\n", func);
10580Sstevel@tonic-gate 			}
10590Sstevel@tonic-gate 			(void) strlcpy(vname, "aggregating action",
10600Sstevel@tonic-gate 			    sizeof (vname));
10610Sstevel@tonic-gate 			vnp = &aggnode;
10620Sstevel@tonic-gate 		} else if (dnp == NULL) {
10630Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_PROTO,
10640Sstevel@tonic-gate 			    "%s( ) prototype mismatch: conversion #%d (%%"
10650Sstevel@tonic-gate 			    "%s) is missing a corresponding value argument\n",
10660Sstevel@tonic-gate 			    func, i + 1, pfc->pfc_name);
10670Sstevel@tonic-gate 		} else {
10680Sstevel@tonic-gate 			(void) snprintf(vname, sizeof (vname),
10690Sstevel@tonic-gate 			    "argument #%d", j + foff + 1);
10700Sstevel@tonic-gate 			vnp = dnp;
10710Sstevel@tonic-gate 			dnp = dnp->dn_list;
10720Sstevel@tonic-gate 			j++;
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		/*
10760Sstevel@tonic-gate 		 * Fill in the proposed final format string by prepending any
10770Sstevel@tonic-gate 		 * size-related prefixes to the pfconv's format string.  The
10780Sstevel@tonic-gate 		 * pfc_check() function below may optionally modify the format
10790Sstevel@tonic-gate 		 * as part of validating the type of the input argument.
10800Sstevel@tonic-gate 		 */
10810Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_sint ||
10820Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_uint ||
10830Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_dint) {
10840Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (uint64_t))
10850Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "ll");
10860Sstevel@tonic-gate 		} else if (pfc->pfc_print == &pfprint_fp) {
10870Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (long double))
10880Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "L");
10890Sstevel@tonic-gate 		}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		/*
10940Sstevel@tonic-gate 		 * Validate the format conversion against the value node type.
10950Sstevel@tonic-gate 		 * If the conversion is good, create the descriptor format
10960Sstevel@tonic-gate 		 * string by concatenating together any required printf(3C)
10970Sstevel@tonic-gate 		 * size prefixes with the conversion's native format string.
10980Sstevel@tonic-gate 		 */
1099457Sbmc 		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
11000Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_TYPE,
11010Sstevel@tonic-gate 			    "%s( ) %s is incompatible with "
11020Sstevel@tonic-gate 			    "conversion #%d prototype:\n\tconversion: %%%s\n"
11030Sstevel@tonic-gate 			    "\t prototype: %s\n\t  argument: %s\n", func,
11040Sstevel@tonic-gate 			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
11050Sstevel@tonic-gate 			    dt_node_type_name(vnp, n, sizeof (n)));
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
11100Sstevel@tonic-gate 		xyerror(D_PRINTF_ARG_EXTRA,
11110Sstevel@tonic-gate 		    "%s( ) prototype mismatch: only %d arguments "
11120Sstevel@tonic-gate 		    "required by this format string\n", func, j);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate 
11161017Sbmc void
11171017Sbmc dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
11181017Sbmc {
11191017Sbmc 	dt_ident_t *lid, *rid;
11201017Sbmc 	dt_node_t *lproto, *rproto;
11211017Sbmc 	int largc, rargc, argn;
11221017Sbmc 	char n1[DT_TYPE_NAMELEN];
11231017Sbmc 	char n2[DT_TYPE_NAMELEN];
11241017Sbmc 
11251017Sbmc 	assert(lhs->dn_kind == DT_NODE_AGG);
11261017Sbmc 	assert(rhs->dn_kind == DT_NODE_AGG);
11271017Sbmc 
11281017Sbmc 	lid = lhs->dn_ident;
11291017Sbmc 	rid = rhs->dn_ident;
11301017Sbmc 
11311017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
11321017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
11331017Sbmc 
11341017Sbmc 	/*
11351017Sbmc 	 * First, get an argument count on each side.  These must match.
11361017Sbmc 	 */
11371017Sbmc 	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
11381017Sbmc 		largc++;
11391017Sbmc 
11401017Sbmc 	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
11411017Sbmc 		rargc++;
11421017Sbmc 
11431017Sbmc 	if (largc != rargc) {
11441017Sbmc 		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
11451017Sbmc 		    "matching key signatures: @%s has %d key%s, @%s has %d "
11461017Sbmc 		    "key%s", lid->di_name, rid->di_name,
11471017Sbmc 		    lid->di_name, largc, largc == 1 ? "" : "s",
11481017Sbmc 		    rid->di_name, rargc, rargc == 1 ? "" : "s");
11491017Sbmc 	}
11501017Sbmc 
11511017Sbmc 	/*
11521017Sbmc 	 * Now iterate over the keys to verify that each type matches.
11531017Sbmc 	 */
11541017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
11551017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
11561017Sbmc 
11571017Sbmc 	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
11581017Sbmc 	    rproto = rproto->dn_list) {
11591017Sbmc 		assert(rproto != NULL);
11601017Sbmc 
11611017Sbmc 		if (dt_node_is_argcompat(lproto, rproto))
11621017Sbmc 			continue;
11631017Sbmc 
11641017Sbmc 		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
11651017Sbmc 		    "incompatible with @%s:\n%9s key #%d: %s\n"
11661017Sbmc 		    "%9s key #%d: %s\n",
11671017Sbmc 		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
11681017Sbmc 		    dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
11691017Sbmc 		    argn, dt_node_type_name(rproto, n2, sizeof (n2)));
11701017Sbmc 	}
11711017Sbmc }
11721017Sbmc 
11730Sstevel@tonic-gate static int
11740Sstevel@tonic-gate dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
11750Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len, int *ip)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	uintptr_t addr;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	if (nrecs == 0)
11800Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	addr = (uintptr_t)buf + recp->dtrd_offset;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (addr + sizeof (int) > (uintptr_t)buf + len)
11850Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DOFFSET));
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (addr & (recp->dtrd_alignment - 1))
11880Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DALIGN));
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	switch (recp->dtrd_size) {
11910Sstevel@tonic-gate 	case sizeof (int8_t):
11920Sstevel@tonic-gate 		*ip = (int)*((int8_t *)addr);
11930Sstevel@tonic-gate 		break;
11940Sstevel@tonic-gate 	case sizeof (int16_t):
11950Sstevel@tonic-gate 		*ip = (int)*((int16_t *)addr);
11960Sstevel@tonic-gate 		break;
11970Sstevel@tonic-gate 	case sizeof (int32_t):
11980Sstevel@tonic-gate 		*ip = (int)*((int32_t *)addr);
11990Sstevel@tonic-gate 		break;
12000Sstevel@tonic-gate 	case sizeof (int64_t):
12010Sstevel@tonic-gate 		*ip = (int)*((int64_t *)addr);
12020Sstevel@tonic-gate 		break;
12030Sstevel@tonic-gate 	default:
12040Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	return (0);
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate /*ARGSUSED*/
12110Sstevel@tonic-gate static int
12120Sstevel@tonic-gate pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12130Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	const uint64_t *data = addr;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	if (size != sizeof (uint64_t) * 2)
12180Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format,
12210Sstevel@tonic-gate 	    data[0] ? data[1] / normal / data[0] : 0));
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*ARGSUSED*/
12250Sstevel@tonic-gate static int
1226*9531Srafael.vanoni@sun.com pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1227*9531Srafael.vanoni@sun.com     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1228*9531Srafael.vanoni@sun.com {
1229*9531Srafael.vanoni@sun.com 	const uint64_t *data = addr;
1230*9531Srafael.vanoni@sun.com 
1231*9531Srafael.vanoni@sun.com 	if (size != sizeof (uint64_t) * 4)
1232*9531Srafael.vanoni@sun.com 		return (dt_set_errno(dtp, EDT_DMISMATCH));
1233*9531Srafael.vanoni@sun.com 
1234*9531Srafael.vanoni@sun.com 	return (dt_printf(dtp, fp, format,
1235*9531Srafael.vanoni@sun.com 	    dt_stddev((uint64_t *)data, normal)));
1236*9531Srafael.vanoni@sun.com }
1237*9531Srafael.vanoni@sun.com 
1238*9531Srafael.vanoni@sun.com /*ARGSUSED*/
1239*9531Srafael.vanoni@sun.com static int
12400Sstevel@tonic-gate pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12410Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	return (dt_print_quantize(dtp, fp, addr, size, normal));
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate /*ARGSUSED*/
12470Sstevel@tonic-gate static int
12480Sstevel@tonic-gate pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12490Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	return (dt_print_lquantize(dtp, fp, addr, size, normal));
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate static int
12550Sstevel@tonic-gate dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
12560Sstevel@tonic-gate     const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
12571017Sbmc     size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
12600Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = recs;
12611017Sbmc 	const dtrace_aggdata_t *aggdata;
12621017Sbmc 	dtrace_aggdesc_t *agg;
12631017Sbmc 	caddr_t lim = (caddr_t)buf + len, limit;
12640Sstevel@tonic-gate 	char format[64] = "%";
12651017Sbmc 	int i, aggrec, curagg = -1;
12661017Sbmc 	uint64_t normal;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	/*
12691017Sbmc 	 * If we are formatting an aggregation, set 'aggrec' to the index of
12701017Sbmc 	 * the final record description (the aggregation result) so we can use
12711017Sbmc 	 * this record index with any conversion where DT_PFCONV_AGG is set.
12721017Sbmc 	 * (The actual aggregation used will vary as we increment through the
12731017Sbmc 	 * aggregation variables that we have been passed.)  Finally, we
12741017Sbmc 	 * decrement nrecs to prevent this record from being used with any
12751017Sbmc 	 * other conversion.
12760Sstevel@tonic-gate 	 */
12770Sstevel@tonic-gate 	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
12781017Sbmc 		assert(aggsdata != NULL);
12791017Sbmc 		assert(naggvars > 0);
12801017Sbmc 
12810Sstevel@tonic-gate 		if (nrecs == 0)
12820Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DMISMATCH));
12831017Sbmc 
12841017Sbmc 		curagg = naggvars > 1 ? 1 : 0;
12851017Sbmc 		aggdata = aggsdata[0];
12861017Sbmc 		aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
12870Sstevel@tonic-gate 		nrecs--;
12880Sstevel@tonic-gate 	}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
12910Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
12920Sstevel@tonic-gate 		int width = pfd->pfd_width;
12930Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
12940Sstevel@tonic-gate 		int rval;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		char *f = format + 1; /* skip initial '%' */
12970Sstevel@tonic-gate 		const dtrace_recdesc_t *rec;
12980Sstevel@tonic-gate 		dt_pfprint_f *func;
12991017Sbmc 		caddr_t addr;
13000Sstevel@tonic-gate 		size_t size;
13011017Sbmc 		uint32_t flags;
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
13040Sstevel@tonic-gate 			char *tmp = alloca(pfd->pfd_preflen + 1);
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
13070Sstevel@tonic-gate 			tmp[pfd->pfd_preflen] = '\0';
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
13100Sstevel@tonic-gate 				return (rval);
1311457Sbmc 
1312457Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1313457Sbmc 				/*
1314457Sbmc 				 * For printa(), we flush the buffer after each
13151017Sbmc 				 * prefix, setting the flags to indicate that
13161017Sbmc 				 * this is part of the printa() format string.
1317457Sbmc 				 */
13181017Sbmc 				flags = DTRACE_BUFDATA_AGGFORMAT;
13191017Sbmc 
13201017Sbmc 				if (pfc == NULL && i == pfv->pfv_argc - 1)
13211017Sbmc 					flags |= DTRACE_BUFDATA_AGGLAST;
13221017Sbmc 
13231017Sbmc 				if (dt_buffered_flush(dtp, NULL, NULL,
13241017Sbmc 				    aggdata, flags) < 0)
1325457Sbmc 					return (-1);
1326457Sbmc 			}
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		if (pfc == NULL) {
13300Sstevel@tonic-gate 			if (pfv->pfv_argc == 1)
13310Sstevel@tonic-gate 				return (nrecs != 0);
13320Sstevel@tonic-gate 			continue;
13330Sstevel@tonic-gate 		}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 		/*
13360Sstevel@tonic-gate 		 * If the conversion is %%, just invoke the print callback
13370Sstevel@tonic-gate 		 * with no data record and continue; it consumes no record.
13380Sstevel@tonic-gate 		 */
13390Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
13400Sstevel@tonic-gate 			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
13410Sstevel@tonic-gate 				continue;
13420Sstevel@tonic-gate 			return (-1); /* errno is set for us */
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
13460Sstevel@tonic-gate 			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
13470Sstevel@tonic-gate 			    len, &width) == -1)
13480Sstevel@tonic-gate 				return (-1); /* errno is set for us */
13490Sstevel@tonic-gate 			pfd->pfd_dynwidth = width;
13500Sstevel@tonic-gate 		} else {
13510Sstevel@tonic-gate 			pfd->pfd_dynwidth = 0;
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
13550Sstevel@tonic-gate 		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
13560Sstevel@tonic-gate 			return (-1); /* errno is set for us */
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
13591017Sbmc 			/*
13601017Sbmc 			 * This should be impossible -- the compiler shouldn't
13611017Sbmc 			 * create a DT_PFCONV_AGG conversion without an
13621017Sbmc 			 * aggregation present.  Still, we'd rather fail
13631017Sbmc 			 * gracefully than blow up...
13641017Sbmc 			 */
13651017Sbmc 			if (aggsdata == NULL)
13660Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
13671017Sbmc 
13681017Sbmc 			aggdata = aggsdata[curagg];
13691017Sbmc 			agg = aggdata->dtada_desc;
13701017Sbmc 
13711017Sbmc 			/*
13721017Sbmc 			 * We increment the current aggregation variable, but
13731017Sbmc 			 * not beyond the number of aggregation variables that
13741017Sbmc 			 * we're printing. This has the (desired) effect that
13751017Sbmc 			 * DT_PFCONV_AGG conversions beyond the number of
13761017Sbmc 			 * aggregation variables (re-)convert the aggregation
13771017Sbmc 			 * value of the last aggregation variable.
13781017Sbmc 			 */
13791017Sbmc 			if (curagg < naggvars - 1)
13801017Sbmc 				curagg++;
13811017Sbmc 
13821017Sbmc 			rec = &agg->dtagd_rec[aggrec];
13831017Sbmc 			addr = aggdata->dtada_data + rec->dtrd_offset;
13841017Sbmc 			limit = addr + aggdata->dtada_size;
13851017Sbmc 			normal = aggdata->dtada_normal;
13861017Sbmc 			flags = DTRACE_BUFDATA_AGGVAL;
13870Sstevel@tonic-gate 		} else {
13880Sstevel@tonic-gate 			if (nrecs == 0)
13890Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
13901028Sbmc 
13911028Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
13921028Sbmc 				/*
13931028Sbmc 				 * When printing aggregation keys, we always
13941028Sbmc 				 * set the aggdata to be the representative
13951028Sbmc 				 * (zeroth) aggregation.  The aggdata isn't
13961028Sbmc 				 * actually used here in this case, but it is
13971028Sbmc 				 * passed to the buffer handler and must
13981028Sbmc 				 * therefore still be correct.
13991028Sbmc 				 */
14001028Sbmc 				aggdata = aggsdata[0];
14011028Sbmc 				flags = DTRACE_BUFDATA_AGGKEY;
14021028Sbmc 			}
14031028Sbmc 
14040Sstevel@tonic-gate 			rec = recp++;
14050Sstevel@tonic-gate 			nrecs--;
14061017Sbmc 			addr = (caddr_t)buf + rec->dtrd_offset;
14071017Sbmc 			limit = lim;
14081017Sbmc 			normal = 1;
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 		size = rec->dtrd_size;
14120Sstevel@tonic-gate 
14131017Sbmc 		if (addr + size > limit) {
14140Sstevel@tonic-gate 			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
14150Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, (void *)lim);
14160Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DOFFSET));
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		if (rec->dtrd_alignment != 0 &&
14200Sstevel@tonic-gate 		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
14210Sstevel@tonic-gate 			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
14220Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
14230Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DALIGN));
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		switch (rec->dtrd_action) {
14270Sstevel@tonic-gate 		case DTRACEAGG_AVG:
14280Sstevel@tonic-gate 			func = pfprint_average;
14290Sstevel@tonic-gate 			break;
1430*9531Srafael.vanoni@sun.com 		case DTRACEAGG_STDDEV:
1431*9531Srafael.vanoni@sun.com 			func = pfprint_stddev;
1432*9531Srafael.vanoni@sun.com 			break;
14330Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
14340Sstevel@tonic-gate 			func = pfprint_quantize;
14350Sstevel@tonic-gate 			break;
14360Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
14370Sstevel@tonic-gate 			func = pfprint_lquantize;
14380Sstevel@tonic-gate 			break;
1439457Sbmc 		case DTRACEACT_MOD:
1440457Sbmc 			func = pfprint_mod;
1441457Sbmc 			break;
1442457Sbmc 		case DTRACEACT_UMOD:
1443457Sbmc 			func = pfprint_umod;
1444457Sbmc 			break;
14450Sstevel@tonic-gate 		default:
14460Sstevel@tonic-gate 			func = pfc->pfc_print;
14470Sstevel@tonic-gate 			break;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
14510Sstevel@tonic-gate 			*f++ = '#';
14520Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
14530Sstevel@tonic-gate 			*f++ = '0';
14541222Smws 		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
14550Sstevel@tonic-gate 			*f++ = '-';
14560Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
14570Sstevel@tonic-gate 			*f++ = '+';
14580Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
14590Sstevel@tonic-gate 			*f++ = '\'';
14600Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
14610Sstevel@tonic-gate 			*f++ = ' ';
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 		/*
14640Sstevel@tonic-gate 		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
14650Sstevel@tonic-gate 		 * don't add the width to the format string.  See the block
14660Sstevel@tonic-gate 		 * comment in pfprint_stack() for a description of the
14670Sstevel@tonic-gate 		 * behavior in this case.
14680Sstevel@tonic-gate 		 */
14690Sstevel@tonic-gate 		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
14700Sstevel@tonic-gate 			width = 0;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 		if (width != 0)
14731222Smws 			f += snprintf(f, sizeof (format), "%d", ABS(width));
14740Sstevel@tonic-gate 
14751222Smws 		if (prec > 0)
14760Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		(void) strcpy(f, pfd->pfd_fmt);
14790Sstevel@tonic-gate 		pfd->pfd_rec = rec;
14800Sstevel@tonic-gate 
14811017Sbmc 		if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
14820Sstevel@tonic-gate 			return (-1); /* errno is set for us */
1483457Sbmc 
1484457Sbmc 		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1485457Sbmc 			/*
1486457Sbmc 			 * For printa(), we flush the buffer after each tuple
14871017Sbmc 			 * element, inidicating that this is the last record
14881017Sbmc 			 * as appropriate.
1489457Sbmc 			 */
14901017Sbmc 			if (i == pfv->pfv_argc - 1)
14911017Sbmc 				flags |= DTRACE_BUFDATA_AGGLAST;
14921017Sbmc 
14931017Sbmc 			if (dt_buffered_flush(dtp, NULL,
14941017Sbmc 			    rec, aggdata, flags) < 0)
1495457Sbmc 				return (-1);
1496457Sbmc 		}
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	return ((int)(recp - recs));
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate int
15030Sstevel@tonic-gate dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15040Sstevel@tonic-gate     const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	dtrace_optval_t size;
15070Sstevel@tonic-gate 	int rval;
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "strsize", &size);
15100Sstevel@tonic-gate 	assert(rval == 0);
15110Sstevel@tonic-gate 	assert(dtp->dt_sprintf_buflen == 0);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if (dtp->dt_sprintf_buf != NULL)
15140Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
15170Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	bzero(dtp->dt_sprintf_buf, size);
15200Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = size;
15211017Sbmc 	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
15221017Sbmc 	    NULL, 0);
15230Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = 0;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	if (rval == -1)
15260Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	return (rval);
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate /*ARGSUSED*/
15320Sstevel@tonic-gate int
15330Sstevel@tonic-gate dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15340Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15350Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15360Sstevel@tonic-gate {
15370Sstevel@tonic-gate 	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (rval == -1)
15400Sstevel@tonic-gate 		return (rval);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * Before we execute the specified command, flush fp to assure that
15440Sstevel@tonic-gate 	 * any prior dt_printf()'s appear before the output of the command
15450Sstevel@tonic-gate 	 * not after it.
15460Sstevel@tonic-gate 	 */
15470Sstevel@tonic-gate 	(void) fflush(fp);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if (system(dtp->dt_sprintf_buf) == -1)
15500Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	return (rval);
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate int
15560Sstevel@tonic-gate dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15570Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15580Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15590Sstevel@tonic-gate {
15600Sstevel@tonic-gate 	char selfbuf[40], restorebuf[40], *filename;
15610Sstevel@tonic-gate 	FILE *nfp;
15620Sstevel@tonic-gate 	int rval, errval;
15630Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
15640Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	if (rval == -1 || fp == NULL)
15690Sstevel@tonic-gate 		return (rval);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	if (pfd->pfd_preflen != 0 &&
15720Sstevel@tonic-gate 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
15730Sstevel@tonic-gate 		/*
15740Sstevel@tonic-gate 		 * The only way to have the format string set to the value
15750Sstevel@tonic-gate 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
15760Sstevel@tonic-gate 		 * denoting that we should restore the old stdout.
15770Sstevel@tonic-gate 		 */
15780Sstevel@tonic-gate 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 		if (dtp->dt_stdout_fd == -1) {
15810Sstevel@tonic-gate 			/*
15820Sstevel@tonic-gate 			 * We could complain here by generating an error,
15830Sstevel@tonic-gate 			 * but it seems like overkill:  it seems that calling
15840Sstevel@tonic-gate 			 * freopen() to restore stdout when freopen() has
15850Sstevel@tonic-gate 			 * never before been called should just be a no-op,
15860Sstevel@tonic-gate 			 * so we just return in this case.
15870Sstevel@tonic-gate 			 */
15880Sstevel@tonic-gate 			return (rval);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 		(void) snprintf(restorebuf, sizeof (restorebuf),
15920Sstevel@tonic-gate 		    "/dev/fd/%d", dtp->dt_stdout_fd);
15930Sstevel@tonic-gate 		filename = restorebuf;
15940Sstevel@tonic-gate 	} else {
15950Sstevel@tonic-gate 		filename = dtp->dt_sprintf_buf;
15960Sstevel@tonic-gate 	}
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	/*
15990Sstevel@tonic-gate 	 * freopen(3C) will always close the specified stream and underlying
16000Sstevel@tonic-gate 	 * file descriptor -- even if the specified file can't be opened.
16010Sstevel@tonic-gate 	 * Even for the semantic cesspool that is standard I/O, this is
16020Sstevel@tonic-gate 	 * surprisingly brain-dead behavior:  it means that any failure to
16030Sstevel@tonic-gate 	 * open the specified file destroys the specified stream in the
16040Sstevel@tonic-gate 	 * process -- which is particularly relevant when the specified stream
16050Sstevel@tonic-gate 	 * happens (or rather, happened) to be stdout.  This could be resolved
16060Sstevel@tonic-gate 	 * were there an "fdreopen()" equivalent of freopen() that allowed one
16070Sstevel@tonic-gate 	 * to pass a file descriptor instead of the name of a file, but there
16080Sstevel@tonic-gate 	 * is no such thing.  However, we can effect this ourselves by first
16090Sstevel@tonic-gate 	 * fopen()'ing the desired file, and then (assuming that that works),
16100Sstevel@tonic-gate 	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
16110Sstevel@tonic-gate 	 * file descriptor for the fopen()'d file.  This way, if the fopen()
16120Sstevel@tonic-gate 	 * fails, we can fail the operation without destroying stdout.
16130Sstevel@tonic-gate 	 */
16141914Scasper 	if ((nfp = fopen(filename, "aF")) == NULL) {
16150Sstevel@tonic-gate 		char *msg = strerror(errno), *faultstr;
16160Sstevel@tonic-gate 		int len = 80;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		len += strlen(msg) + strlen(filename);
16190Sstevel@tonic-gate 		faultstr = alloca(len);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
16220Sstevel@tonic-gate 		    filename, strerror(errno));
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
16250Sstevel@tonic-gate 			return (rval);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 		return (errval);
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if (dtp->dt_stdout_fd == -1) {
16330Sstevel@tonic-gate 		/*
16340Sstevel@tonic-gate 		 * If this is the first time that we're calling freopen(),
16350Sstevel@tonic-gate 		 * we're going to stash away the file descriptor for stdout.
16360Sstevel@tonic-gate 		 * We don't expect the dup(2) to fail, so if it does we must
16370Sstevel@tonic-gate 		 * return failure.
16380Sstevel@tonic-gate 		 */
16390Sstevel@tonic-gate 		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
16400Sstevel@tonic-gate 			(void) fclose(nfp);
16410Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
16420Sstevel@tonic-gate 		}
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate 
16451914Scasper 	if (freopen(selfbuf, "aF", fp) == NULL) {
16460Sstevel@tonic-gate 		(void) fclose(nfp);
16470Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
16480Sstevel@tonic-gate 	}
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	(void) fclose(nfp);
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	return (rval);
16530Sstevel@tonic-gate }
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate /*ARGSUSED*/
16560Sstevel@tonic-gate int
16570Sstevel@tonic-gate dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
16580Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
16590Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
16600Sstevel@tonic-gate {
1661457Sbmc 	return (dt_printf_format(dtp, fp, fmtdata,
16621017Sbmc 	    recp, nrecs, buf, len, NULL, 0));
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate void *
16660Sstevel@tonic-gate dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
16670Sstevel@tonic-gate {
16680Sstevel@tonic-gate 	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
16690Sstevel@tonic-gate 	dt_pfargd_t *pfd;
16700Sstevel@tonic-gate 	int i;
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	if (pfv == NULL)
16730Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	pfd = pfv->pfv_argv;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
16780Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		if (pfc == NULL)
16810Sstevel@tonic-gate 			continue;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 		/*
16840Sstevel@tonic-gate 		 * If the output format is not %s then we assume that we have
16850Sstevel@tonic-gate 		 * been given a correctly-sized format string, so we copy the
16860Sstevel@tonic-gate 		 * true format name including the size modifier.  If the output
16870Sstevel@tonic-gate 		 * format is %s, then either the input format is %s as well or
16880Sstevel@tonic-gate 		 * it is one of our custom formats (e.g. pfprint_addr), so we
16890Sstevel@tonic-gate 		 * must set pfd_fmt to be the output format conversion "s".
16900Sstevel@tonic-gate 		 */
16910Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") != 0)
16920Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
16930Sstevel@tonic-gate 		else
16940Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	return (pfv);
16980Sstevel@tonic-gate }
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate void *
17010Sstevel@tonic-gate dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	if (pfv == NULL)
17060Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	return (pfv);
17110Sstevel@tonic-gate }
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate /*ARGSUSED*/
17140Sstevel@tonic-gate size_t
17150Sstevel@tonic-gate dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
17180Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	/*
17210Sstevel@tonic-gate 	 * An upper bound on the string length is the length of the original
17220Sstevel@tonic-gate 	 * format string, plus three times the number of conversions (each
17230Sstevel@tonic-gate 	 * conversion could add up an additional "ll" and/or pfd_width digit
17240Sstevel@tonic-gate 	 * in the case of converting %? to %16) plus one for a terminating \0.
17250Sstevel@tonic-gate 	 */
17260Sstevel@tonic-gate 	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
17270Sstevel@tonic-gate 	char *format = alloca(formatlen);
17280Sstevel@tonic-gate 	char *f = format;
17290Sstevel@tonic-gate 	int i, j;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
17320Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
17330Sstevel@tonic-gate 		const char *str;
17340Sstevel@tonic-gate 		int width = pfd->pfd_width;
17350Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
17380Sstevel@tonic-gate 			for (j = 0; j < pfd->pfd_preflen; j++)
17390Sstevel@tonic-gate 				*f++ = pfd->pfd_prefix[j];
17400Sstevel@tonic-gate 		}
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 		if (pfc == NULL)
17430Sstevel@tonic-gate 			continue;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		*f++ = '%';
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
17480Sstevel@tonic-gate 			*f++ = '#';
17490Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
17500Sstevel@tonic-gate 			*f++ = '0';
17510Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
17520Sstevel@tonic-gate 			*f++ = '-';
17530Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
17540Sstevel@tonic-gate 			*f++ = '+';
17550Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
17560Sstevel@tonic-gate 			*f++ = '*';
17570Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
17580Sstevel@tonic-gate 			*f++ = '.';
17590Sstevel@tonic-gate 			*f++ = '*';
17600Sstevel@tonic-gate 		}
17610Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
17620Sstevel@tonic-gate 			*f++ = '\'';
17630Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
17640Sstevel@tonic-gate 			*f++ = ' ';
17650Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG)
17660Sstevel@tonic-gate 			*f++ = '@';
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 		if (width != 0)
17690Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), "%d", width);
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 		if (prec != 0)
17720Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 		/*
17750Sstevel@tonic-gate 		 * If the output format is %s, then either %s is the underlying
17760Sstevel@tonic-gate 		 * conversion or the conversion is one of our customized ones,
17770Sstevel@tonic-gate 		 * e.g. pfprint_addr.  In these cases, put the original string
17780Sstevel@tonic-gate 		 * name of the conversion (pfc_name) into the pickled format
17790Sstevel@tonic-gate 		 * string rather than the derived conversion (pfd_fmt).
17800Sstevel@tonic-gate 		 */
17810Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") == 0)
17820Sstevel@tonic-gate 			str = pfc->pfc_name;
17830Sstevel@tonic-gate 		else
17840Sstevel@tonic-gate 			str = pfd->pfd_fmt;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		for (j = 0; str[j] != '\0'; j++)
17870Sstevel@tonic-gate 			*f++ = str[j];
17880Sstevel@tonic-gate 	}
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	*f = '\0'; /* insert nul byte; do not count in return value */
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	assert(f < format + formatlen);
17930Sstevel@tonic-gate 	(void) strncpy(s, format, len);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	return ((size_t)(f - format));
17960Sstevel@tonic-gate }
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate static int
1799457Sbmc dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
18000Sstevel@tonic-gate {
1801457Sbmc 	const dtrace_aggdesc_t *agg = adp->dtada_desc;
18020Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
18030Sstevel@tonic-gate 	uint_t nrecs = agg->dtagd_nrecs;
18040Sstevel@tonic-gate 	dt_pfwalk_t *pfw = arg;
1805457Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
18060Sstevel@tonic-gate 	int id;
18070Sstevel@tonic-gate 
1808457Sbmc 	if (dt_printf_getint(dtp, recp++, nrecs--,
18090Sstevel@tonic-gate 	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
18100Sstevel@tonic-gate 		return (0); /* no aggregation id or id does not match */
18110Sstevel@tonic-gate 
1812457Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
18131017Sbmc 	    recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1814457Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18150Sstevel@tonic-gate 
1816457Sbmc 	/*
1817457Sbmc 	 * Cast away the const to set the bit indicating that this aggregation
1818457Sbmc 	 * has been printed.
1819457Sbmc 	 */
1820457Sbmc 	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	return (0);
18230Sstevel@tonic-gate }
18240Sstevel@tonic-gate 
18251017Sbmc static int
18261017Sbmc dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
18271017Sbmc {
18281017Sbmc 	const dtrace_aggdata_t *aggdata = aggsdata[0];
18291017Sbmc 	const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
18301017Sbmc 	const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
18311017Sbmc 	uint_t nrecs = agg->dtagd_nrecs - 1;
18321017Sbmc 	dt_pfwalk_t *pfw = arg;
18331017Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
18341017Sbmc 	int i;
18351017Sbmc 
18361017Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
18371017Sbmc 	    rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
18381017Sbmc 	    aggsdata, naggvars) == -1)
18391017Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18401017Sbmc 
18411017Sbmc 	/*
18421017Sbmc 	 * For each aggregation, indicate that it has been printed, casting
18431017Sbmc 	 * away the const as necessary.
18441017Sbmc 	 */
18451017Sbmc 	for (i = 1; i < naggvars; i++) {
18461017Sbmc 		agg = aggsdata[i]->dtada_desc;
18471017Sbmc 		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18481017Sbmc 	}
18491017Sbmc 
18501017Sbmc 	return (0);
18511017Sbmc }
18520Sstevel@tonic-gate /*ARGSUSED*/
18530Sstevel@tonic-gate int
18540Sstevel@tonic-gate dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
18550Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
18560Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	dt_pfwalk_t pfw;
18591017Sbmc 	int i, naggvars = 0;
18601017Sbmc 	dtrace_aggvarid_t *aggvars;
18611017Sbmc 
18621017Sbmc 	aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
18631017Sbmc 
18641017Sbmc 	/*
18651017Sbmc 	 * This might be a printa() with multiple aggregation variables.  We
18661017Sbmc 	 * need to scan forward through the records until we find a record from
18671017Sbmc 	 * a different statement.
18681017Sbmc 	 */
18691017Sbmc 	for (i = 0; i < nrecs; i++) {
18701017Sbmc 		const dtrace_recdesc_t *nrec = &recs[i];
18710Sstevel@tonic-gate 
18721017Sbmc 		if (nrec->dtrd_uarg != recs->dtrd_uarg)
18731017Sbmc 			break;
18741017Sbmc 
18751017Sbmc 		if (nrec->dtrd_action != recs->dtrd_action)
18761017Sbmc 			return (dt_set_errno(dtp, EDT_BADAGG));
18771017Sbmc 
18781017Sbmc 		aggvars[naggvars++] =
18791017Sbmc 		    /* LINTED - alignment */
18801017Sbmc 		    *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
18811017Sbmc 	}
18821017Sbmc 
18831017Sbmc 	if (naggvars == 0)
18841017Sbmc 		return (dt_set_errno(dtp, EDT_BADAGG));
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	pfw.pfw_argv = fmtdata;
18870Sstevel@tonic-gate 	pfw.pfw_fp = fp;
18880Sstevel@tonic-gate 	pfw.pfw_err = 0;
18890Sstevel@tonic-gate 
18901017Sbmc 	if (naggvars == 1) {
18911017Sbmc 		pfw.pfw_aid = aggvars[0];
18920Sstevel@tonic-gate 
18931017Sbmc 		if (dtrace_aggregate_walk_sorted(dtp,
18941017Sbmc 		    dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
18951017Sbmc 			return (-1); /* errno is set for us */
18961017Sbmc 	} else {
18971017Sbmc 		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
18981017Sbmc 		    dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
18991017Sbmc 			return (-1); /* errno is set for us */
19001017Sbmc 	}
19011017Sbmc 
19021017Sbmc 	return (i);
19030Sstevel@tonic-gate }
1904