xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_printf.c (revision 491:cf5fd4822860)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <alloca.h>
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <dt_printf.h>
390Sstevel@tonic-gate #include <dt_string.h>
400Sstevel@tonic-gate #include <dt_impl.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*ARGSUSED*/
430Sstevel@tonic-gate static int
44457Sbmc pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*ARGSUSED*/
500Sstevel@tonic-gate static int
51457Sbmc pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
52457Sbmc {
53457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
54457Sbmc 	    dt_node_is_symaddr(dnp));
55457Sbmc }
56457Sbmc 
57457Sbmc /*ARGSUSED*/
58457Sbmc static int
59457Sbmc pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
60457Sbmc {
61457Sbmc 	dtrace_hdl_t *dtp = pfv->pfv_dtp;
62457Sbmc 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
63457Sbmc 
64457Sbmc 	if (dt_node_is_usymaddr(dnp))
65457Sbmc 		return (1);
66457Sbmc 
67457Sbmc 	if (idp == NULL || idp->di_id == 0)
68457Sbmc 		return (0);
69457Sbmc 
70457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
71457Sbmc }
72457Sbmc 
73457Sbmc /*ARGSUSED*/
74457Sbmc static int
75457Sbmc pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
76457Sbmc {
77457Sbmc 	return (dt_node_is_stack(dnp));
78457Sbmc }
79457Sbmc 
80457Sbmc /*ARGSUSED*/
81457Sbmc static int
82457Sbmc pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	ctf_file_t *ctfp;
850Sstevel@tonic-gate 	ctf_encoding_t e;
860Sstevel@tonic-gate 	ctf_arinfo_t r;
870Sstevel@tonic-gate 	ctf_id_t base;
880Sstevel@tonic-gate 	uint_t kind;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	if (dt_node_is_string(dnp))
910Sstevel@tonic-gate 		return (1);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	ctfp = dnp->dn_ctfp;
940Sstevel@tonic-gate 	base = ctf_type_resolve(ctfp, dnp->dn_type);
950Sstevel@tonic-gate 	kind = ctf_type_kind(ctfp, base);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
980Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
990Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*ARGSUSED*/
1030Sstevel@tonic-gate static int
104457Sbmc pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1070Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
1080Sstevel@tonic-gate 	uint_t kind = ctf_type_kind(ctfp, base);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	ctf_encoding_t e;
1110Sstevel@tonic-gate 	ctf_arinfo_t r;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1140Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1150Sstevel@tonic-gate 	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
1160Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*ARGSUSED*/
1200Sstevel@tonic-gate static int
121457Sbmc pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	return (dt_node_is_integer(dnp) &&
1240Sstevel@tonic-gate 	    dt_node_type_size(dnp) <= sizeof (int));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*ARGSUSED*/
1280Sstevel@tonic-gate static int
129457Sbmc pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	return (dt_node_is_float(dnp));
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*ARGSUSED*/
1350Sstevel@tonic-gate static int
136457Sbmc pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
141457Sbmc /*ARGSUSED*/
1420Sstevel@tonic-gate static int
143457Sbmc pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	if (dnp->dn_flags & DT_NF_SIGNED)
1460Sstevel@tonic-gate 		pfd->pfd_flags |= DT_PFCONV_SIGNED;
1470Sstevel@tonic-gate 	else
1480Sstevel@tonic-gate 		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*ARGSUSED*/
1540Sstevel@tonic-gate static int
155457Sbmc pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1580Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1590Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1620Sstevel@tonic-gate 	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
1630Sstevel@tonic-gate 	    strcmp(n, "unsigned short") == 0));
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*ARGSUSED*/
1670Sstevel@tonic-gate static int
168457Sbmc pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1710Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1720Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1750Sstevel@tonic-gate 	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
1760Sstevel@tonic-gate 	    strcmp(n, "unsigned long") == 0));
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*ARGSUSED*/
1800Sstevel@tonic-gate static int
181457Sbmc pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1840Sstevel@tonic-gate 	ctf_id_t type = dnp->dn_type;
1850Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
1880Sstevel@tonic-gate 	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
1890Sstevel@tonic-gate 	    strcmp(n, "signed long long") == 0 ||
1900Sstevel@tonic-gate 	    strcmp(n, "unsigned long long") == 0))
1910Sstevel@tonic-gate 		return (1);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * If the type used for %llx or %llX is not an [unsigned] long long, we
1950Sstevel@tonic-gate 	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
1960Sstevel@tonic-gate 	 * that these typedefs are guaranteed to work with %ll[xX] in either
1970Sstevel@tonic-gate 	 * compilation environment even though they alias to "long" in LP64.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
2000Sstevel@tonic-gate 		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
2010Sstevel@tonic-gate 		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
2020Sstevel@tonic-gate 			return (1);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		type = ctf_type_reference(ctfp, type);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (0);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
210457Sbmc /*ARGSUSED*/
2110Sstevel@tonic-gate static int
212457Sbmc pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
2150Sstevel@tonic-gate 	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*ARGSUSED*/
2190Sstevel@tonic-gate static int
2200Sstevel@tonic-gate pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2210Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	int64_t normal = (int64_t)unormal;
2240Sstevel@tonic-gate 	int32_t n = (int32_t)normal;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	switch (size) {
2270Sstevel@tonic-gate 	case sizeof (int8_t):
2280Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2290Sstevel@tonic-gate 		    (int32_t)*((int8_t *)addr) / n));
2300Sstevel@tonic-gate 	case sizeof (int16_t):
2310Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2320Sstevel@tonic-gate 		    (int32_t)*((int16_t *)addr) / n));
2330Sstevel@tonic-gate 	case sizeof (int32_t):
2340Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2350Sstevel@tonic-gate 		    *((int32_t *)addr) / n));
2360Sstevel@tonic-gate 	case sizeof (int64_t):
2370Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2380Sstevel@tonic-gate 		    *((int64_t *)addr) / normal));
2390Sstevel@tonic-gate 	default:
2400Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*ARGSUSED*/
2450Sstevel@tonic-gate static int
2460Sstevel@tonic-gate pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2470Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	uint32_t n = (uint32_t)normal;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	switch (size) {
2520Sstevel@tonic-gate 	case sizeof (uint8_t):
2530Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2540Sstevel@tonic-gate 		    (uint32_t)*((uint8_t *)addr) / n));
2550Sstevel@tonic-gate 	case sizeof (uint16_t):
2560Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2570Sstevel@tonic-gate 		    (uint32_t)*((uint16_t *)addr) / n));
2580Sstevel@tonic-gate 	case sizeof (uint32_t):
2590Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2600Sstevel@tonic-gate 		    *((uint32_t *)addr) / n));
2610Sstevel@tonic-gate 	case sizeof (uint64_t):
2620Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2630Sstevel@tonic-gate 		    *((uint64_t *)addr) / normal));
2640Sstevel@tonic-gate 	default:
2650Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static int
2700Sstevel@tonic-gate pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2710Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
2740Sstevel@tonic-gate 		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
2750Sstevel@tonic-gate 	else
2760Sstevel@tonic-gate 		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*ARGSUSED*/
2800Sstevel@tonic-gate static int
2810Sstevel@tonic-gate pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2820Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	double n = (double)normal;
2850Sstevel@tonic-gate 	long double ldn = (long double)normal;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	switch (size) {
2880Sstevel@tonic-gate 	case sizeof (float):
2890Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2900Sstevel@tonic-gate 		    (double)*((float *)addr) / n));
2910Sstevel@tonic-gate 	case sizeof (double):
2920Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2930Sstevel@tonic-gate 		    *((double *)addr) / n));
2940Sstevel@tonic-gate 	case sizeof (long double):
2950Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2960Sstevel@tonic-gate 		    *((long double *)addr) / ldn));
2970Sstevel@tonic-gate 	default:
2980Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*ARGSUSED*/
3030Sstevel@tonic-gate static int
3040Sstevel@tonic-gate pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3050Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	char *s;
308457Sbmc 	int n, len = 256;
309457Sbmc 	uint64_t val;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	switch (size) {
3120Sstevel@tonic-gate 	case sizeof (uint32_t):
3130Sstevel@tonic-gate 		val = *((uint32_t *)addr);
3140Sstevel@tonic-gate 		break;
3150Sstevel@tonic-gate 	case sizeof (uint64_t):
3160Sstevel@tonic-gate 		val = *((uint64_t *)addr);
3170Sstevel@tonic-gate 		break;
3180Sstevel@tonic-gate 	default:
3190Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
322457Sbmc 	do {
323457Sbmc 		n = len;
324457Sbmc 		s = alloca(n);
325457Sbmc 	} while ((len = dtrace_addr2str(dtp, val, s, n)) >= n);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*ARGSUSED*/
3310Sstevel@tonic-gate static int
332457Sbmc pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
333457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
334457Sbmc {
335457Sbmc 	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
336457Sbmc }
337457Sbmc 
338457Sbmc /*ARGSUSED*/
339457Sbmc static int
340457Sbmc pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
341457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
342457Sbmc {
343457Sbmc 	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
344457Sbmc }
345457Sbmc 
346457Sbmc /*ARGSUSED*/
347457Sbmc static int
3480Sstevel@tonic-gate pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3490Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3500Sstevel@tonic-gate {
351457Sbmc 	char *s;
352457Sbmc 	int n, len = 256;
353457Sbmc 	uint64_t val, pid = 0;
354457Sbmc 
3550Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	switch (size) {
3580Sstevel@tonic-gate 	case sizeof (uint32_t):
3590Sstevel@tonic-gate 		val = (u_longlong_t)*((uint32_t *)addr);
3600Sstevel@tonic-gate 		break;
3610Sstevel@tonic-gate 	case sizeof (uint64_t):
3620Sstevel@tonic-gate 		val = (u_longlong_t)*((uint64_t *)addr);
3630Sstevel@tonic-gate 		break;
364457Sbmc 	case sizeof (uint64_t) * 2:
365457Sbmc 		pid = ((uint64_t *)(uintptr_t)addr)[0];
366457Sbmc 		val = ((uint64_t *)(uintptr_t)addr)[1];
367457Sbmc 		break;
3680Sstevel@tonic-gate 	default:
3690Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
372457Sbmc 	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
373457Sbmc 		pid = idp->di_id;
3740Sstevel@tonic-gate 
375457Sbmc 	do {
376457Sbmc 		n = len;
377457Sbmc 		s = alloca(n);
378457Sbmc 	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) >= n);
3790Sstevel@tonic-gate 
380457Sbmc 	return (dt_printf(dtp, fp, format, s));
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*ARGSUSED*/
3840Sstevel@tonic-gate static int
3850Sstevel@tonic-gate pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3860Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
3870Sstevel@tonic-gate {
388457Sbmc 	int width;
3890Sstevel@tonic-gate 	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
3900Sstevel@tonic-gate 	const dtrace_recdesc_t *rec = pfd->pfd_rec;
3910Sstevel@tonic-gate 	caddr_t addr = (caddr_t)vaddr;
3920Sstevel@tonic-gate 	int err = 0;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * We have stashed the value of the STACKINDENT option, and we will
3960Sstevel@tonic-gate 	 * now override it for the purposes of formatting the stack.  If the
3970Sstevel@tonic-gate 	 * field has been specified as left-aligned (i.e. (%-#), we set the
3980Sstevel@tonic-gate 	 * indentation to be the width.  This is a slightly odd semantic, but
3990Sstevel@tonic-gate 	 * it's useful functionality -- and it's slightly odd to begin with to
4000Sstevel@tonic-gate 	 * be using a single format specifier to be formatting multiple lines
4010Sstevel@tonic-gate 	 * of text...
4020Sstevel@tonic-gate 	 */
4030Sstevel@tonic-gate 	if (pfd->pfd_dynwidth < 0) {
4040Sstevel@tonic-gate 		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
4050Sstevel@tonic-gate 		width = -pfd->pfd_dynwidth;
4060Sstevel@tonic-gate 	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
4070Sstevel@tonic-gate 		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
4080Sstevel@tonic-gate 	} else {
4090Sstevel@tonic-gate 		width = 0;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	switch (rec->dtrd_action) {
4150Sstevel@tonic-gate 	case DTRACEACT_USTACK:
4160Sstevel@tonic-gate 	case DTRACEACT_JSTACK:
4170Sstevel@tonic-gate 		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
4180Sstevel@tonic-gate 		break;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	case DTRACEACT_STACK:
421457Sbmc 		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
422457Sbmc 		    rec->dtrd_size / rec->dtrd_arg);
4230Sstevel@tonic-gate 		break;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	default:
4260Sstevel@tonic-gate 		assert(0);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	return (err);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*ARGSUSED*/
4350Sstevel@tonic-gate static int
4360Sstevel@tonic-gate pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4370Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	char src[32], buf[32], *dst = buf;
4400Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4410Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4420Sstevel@tonic-gate 	int i;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
4460Sstevel@tonic-gate 	 * Below, we turn this into the canonical adb/mdb /[yY] format,
4470Sstevel@tonic-gate 	 * "1973 Dec  3 17:20:00".
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	(void) ctime_r(&sec, src, sizeof (src));
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/*
4520Sstevel@tonic-gate 	 * Place the 4-digit year at the head of the string...
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	for (i = 20; i < 24; i++)
4550Sstevel@tonic-gate 		*dst++ = src[i];
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * ...and follow it with the remainder (month, day, hh:mm:ss).
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	for (i = 3; i < 19; i++)
4610Sstevel@tonic-gate 		*dst++ = src[i];
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	*dst = '\0';
4640Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * This prints the time in RFC 822 standard form.  This is useful for emitting
4690Sstevel@tonic-gate  * notions of time that are consumed by standard tools (e.g., as part of an
4700Sstevel@tonic-gate  * RSS feed).
4710Sstevel@tonic-gate  */
4720Sstevel@tonic-gate /*ARGSUSED*/
4730Sstevel@tonic-gate static int
4740Sstevel@tonic-gate pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4750Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4780Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4790Sstevel@tonic-gate 	struct tm tm;
4800Sstevel@tonic-gate 	char buf[64];
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	(void) localtime_r(&sec, &tm);
4830Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
4840Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /*ARGSUSED*/
4880Sstevel@tonic-gate static int
4890Sstevel@tonic-gate pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4900Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	char *s = alloca(size + 1);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	bcopy(addr, s, size);
4950Sstevel@tonic-gate 	s[size] = '\0';
4960Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*ARGSUSED*/
5000Sstevel@tonic-gate static int
5010Sstevel@tonic-gate pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5020Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	wchar_t *ws = alloca(size + sizeof (wchar_t));
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	bcopy(addr, ws, size);
5070Sstevel@tonic-gate 	ws[size / sizeof (wchar_t)] = L'\0';
5080Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, ws));
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /*ARGSUSED*/
5120Sstevel@tonic-gate static int
5130Sstevel@tonic-gate pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5140Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	char *s;
5170Sstevel@tonic-gate 	int n;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if ((s = strchr2esc(addr, size)) == NULL)
5200Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	n = dt_printf(dtp, fp, format, s);
5230Sstevel@tonic-gate 	free(s);
5240Sstevel@tonic-gate 	return (n);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate static int
5280Sstevel@tonic-gate pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5290Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	char c;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	switch (size) {
5340Sstevel@tonic-gate 	case sizeof (int8_t):
5350Sstevel@tonic-gate 		c = *(int8_t *)addr;
5360Sstevel@tonic-gate 		break;
5370Sstevel@tonic-gate 	case sizeof (int16_t):
5380Sstevel@tonic-gate 		c = *(int16_t *)addr;
5390Sstevel@tonic-gate 		break;
5400Sstevel@tonic-gate 	case sizeof (int32_t):
5410Sstevel@tonic-gate 		c = *(int32_t *)addr;
5420Sstevel@tonic-gate 		break;
5430Sstevel@tonic-gate 	default:
5440Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*ARGSUSED*/
5510Sstevel@tonic-gate static int
5520Sstevel@tonic-gate pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5530Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	return (dt_printf(dtp, fp, "%%"));
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate static const char pfproto_xint[] = "char, short, int, long, or long long";
5590Sstevel@tonic-gate static const char pfproto_csi[] = "char, short, or int";
5600Sstevel@tonic-gate static const char pfproto_fp[] = "float, double, or long double";
5610Sstevel@tonic-gate static const char pfproto_addr[] = "pointer or integer";
562457Sbmc static const char pfproto_uaddr[] =
563457Sbmc 	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
5640Sstevel@tonic-gate static const char pfproto_cstr[] = "char [] or string (or use stringof)";
5650Sstevel@tonic-gate static const char pfproto_wstr[] = "wchar_t []";
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * Printf format conversion dictionary.  This table should match the set of
5690Sstevel@tonic-gate  * conversions offered by printf(3C), as well as some additional extensions.
5700Sstevel@tonic-gate  * The second parameter is an ASCII string which is either an actual type
5710Sstevel@tonic-gate  * name we should look up (if pfcheck_type is specified), or just a descriptive
5720Sstevel@tonic-gate  * string of the types expected for use in error messages.
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate static const dt_pfconv_t _dtrace_conversions[] = {
575457Sbmc { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
576457Sbmc { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
5770Sstevel@tonic-gate { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
5780Sstevel@tonic-gate { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
5790Sstevel@tonic-gate { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
5800Sstevel@tonic-gate { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
5810Sstevel@tonic-gate { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
5820Sstevel@tonic-gate { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
5830Sstevel@tonic-gate { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
5840Sstevel@tonic-gate { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
5850Sstevel@tonic-gate { "hd", "d", "short", pfcheck_type, pfprint_sint },
5860Sstevel@tonic-gate { "hi", "i", "short", pfcheck_type, pfprint_sint },
5870Sstevel@tonic-gate { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
5880Sstevel@tonic-gate { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
5890Sstevel@tonic-gate { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
5900Sstevel@tonic-gate { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
5910Sstevel@tonic-gate { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
592457Sbmc { "k", "s", "stack", pfcheck_stack, pfprint_stack },
5930Sstevel@tonic-gate { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
5940Sstevel@tonic-gate { "ld",	"d", "long", pfcheck_type, pfprint_sint },
5950Sstevel@tonic-gate { "li",	"i", "long", pfcheck_type, pfprint_sint },
5960Sstevel@tonic-gate { "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
5970Sstevel@tonic-gate { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
5980Sstevel@tonic-gate { "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
5990Sstevel@tonic-gate { "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
6000Sstevel@tonic-gate { "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
6010Sstevel@tonic-gate { "lld", "d", "long long", pfcheck_type, pfprint_sint },
6020Sstevel@tonic-gate { "lli", "i", "long long", pfcheck_type, pfprint_sint },
6030Sstevel@tonic-gate { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
6040Sstevel@tonic-gate { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
6050Sstevel@tonic-gate { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
6060Sstevel@tonic-gate { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
6070Sstevel@tonic-gate { "Le",	"e", "long double", pfcheck_type, pfprint_fp },
6080Sstevel@tonic-gate { "LE",	"E", "long double", pfcheck_type, pfprint_fp },
6090Sstevel@tonic-gate { "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
6100Sstevel@tonic-gate { "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
6110Sstevel@tonic-gate { "LG",	"G", "long double", pfcheck_type, pfprint_fp },
6120Sstevel@tonic-gate { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
6130Sstevel@tonic-gate { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
6140Sstevel@tonic-gate { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
6150Sstevel@tonic-gate { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
6160Sstevel@tonic-gate { "T", "s", "uint64_t", pfcheck_type, pfprint_time822 },
6170Sstevel@tonic-gate { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
6180Sstevel@tonic-gate { "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
6190Sstevel@tonic-gate { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6200Sstevel@tonic-gate { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
6210Sstevel@tonic-gate { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
6220Sstevel@tonic-gate { "Y", "s", "uint64_t", pfcheck_type, pfprint_time },
6230Sstevel@tonic-gate { "%", "%", "void", pfcheck_type, pfprint_pct },
6240Sstevel@tonic-gate { NULL, NULL, NULL, NULL, NULL }
6250Sstevel@tonic-gate };
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate int
6280Sstevel@tonic-gate dt_pfdict_create(dtrace_hdl_t *dtp)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	uint_t n = _dtrace_strbuckets;
6310Sstevel@tonic-gate 	const dt_pfconv_t *pfd;
6320Sstevel@tonic-gate 	dt_pfdict_t *pdi;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
6350Sstevel@tonic-gate 	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
6360Sstevel@tonic-gate 		free(pdi);
6370Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	dtp->dt_pfdict = pdi;
6410Sstevel@tonic-gate 	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
6420Sstevel@tonic-gate 	pdi->pdi_nbuckets = n;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
6450Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
6460Sstevel@tonic-gate 		dt_pfconv_t *pfc;
6470Sstevel@tonic-gate 		uint_t h;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
6500Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6510Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
6550Sstevel@tonic-gate 		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
6560Sstevel@tonic-gate 		pfc->pfc_next = pdi->pdi_buckets[h];
6570Sstevel@tonic-gate 		pdi->pdi_buckets[h] = pfc;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		dtt.dtt_ctfp = NULL;
6600Sstevel@tonic-gate 		dtt.dtt_type = CTF_ERR;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		/*
6630Sstevel@tonic-gate 		 * The "D" container or its parent must contain a definition of
6640Sstevel@tonic-gate 		 * any type referenced by a printf conversion.  If none can be
6650Sstevel@tonic-gate 		 * found, we fail to initialize the printf dictionary.
6660Sstevel@tonic-gate 		 */
6670Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6680Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
6690Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6700Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOCONV));
6710Sstevel@tonic-gate 		}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		pfc->pfc_dctfp = dtt.dtt_ctfp;
6740Sstevel@tonic-gate 		pfc->pfc_dtype = dtt.dtt_type;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		/*
6770Sstevel@tonic-gate 		 * The "C" container may contain an alternate definition of an
6780Sstevel@tonic-gate 		 * explicit conversion type.  If it does, use it; otherwise
6790Sstevel@tonic-gate 		 * just set pfc_ctype to pfc_dtype so it is always valid.
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
6820Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
6830Sstevel@tonic-gate 			pfc->pfc_cctfp = dtt.dtt_ctfp;
6840Sstevel@tonic-gate 			pfc->pfc_ctype = dtt.dtt_type;
6850Sstevel@tonic-gate 		} else {
6860Sstevel@tonic-gate 			pfc->pfc_cctfp = pfc->pfc_dctfp;
6870Sstevel@tonic-gate 			pfc->pfc_ctype = pfc->pfc_dtype;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
6910Sstevel@tonic-gate 		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
6920Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
6930Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADCONV));
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	return (0);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate void
7030Sstevel@tonic-gate dt_pfdict_destroy(dtrace_hdl_t *dtp)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7060Sstevel@tonic-gate 	dt_pfconv_t *pfc, *nfc;
7070Sstevel@tonic-gate 	uint_t i;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (pdi == NULL)
7100Sstevel@tonic-gate 		return;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	for (i = 0; i < pdi->pdi_nbuckets; i++) {
7130Sstevel@tonic-gate 		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
7140Sstevel@tonic-gate 			nfc = pfc->pfc_next;
7150Sstevel@tonic-gate 			free(pfc);
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	free(pdi->pdi_buckets);
7200Sstevel@tonic-gate 	free(pdi);
7210Sstevel@tonic-gate 	dtp->dt_pfdict = NULL;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate static const dt_pfconv_t *
7250Sstevel@tonic-gate dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7280Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
7290Sstevel@tonic-gate 	const dt_pfconv_t *pfc;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
7320Sstevel@tonic-gate 		if (strcmp(pfc->pfc_name, name) == 0)
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	return (pfc);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate static dt_pfargv_t *
7400Sstevel@tonic-gate dt_printf_error(dtrace_hdl_t *dtp, int err)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	if (yypcb != NULL)
7430Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, err);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
7460Sstevel@tonic-gate 	return (NULL);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate dt_pfargv_t *
7500Sstevel@tonic-gate dt_printf_create(dtrace_hdl_t *dtp, const char *s)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd = NULL;
7530Sstevel@tonic-gate 	dt_pfargv_t *pfv;
7540Sstevel@tonic-gate 	const char *p, *q;
7550Sstevel@tonic-gate 	char *format;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
7580Sstevel@tonic-gate 	    (format = strdup(s)) == NULL) {
7590Sstevel@tonic-gate 		free(pfv);
7600Sstevel@tonic-gate 		return (dt_printf_error(dtp, EDT_NOMEM));
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	pfv->pfv_format = format;
7640Sstevel@tonic-gate 	pfv->pfv_argv = NULL;
7650Sstevel@tonic-gate 	pfv->pfv_argc = 0;
7660Sstevel@tonic-gate 	pfv->pfv_flags = 0;
767457Sbmc 	pfv->pfv_dtp = dtp;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
7700Sstevel@tonic-gate 		uint_t namelen = 0;
7710Sstevel@tonic-gate 		int digits = 0;
7720Sstevel@tonic-gate 		int dot = 0;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		char name[8];
7750Sstevel@tonic-gate 		char c;
7760Sstevel@tonic-gate 		int n;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
7790Sstevel@tonic-gate 			dt_printf_destroy(pfv);
7800Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
7840Sstevel@tonic-gate 			nfd->pfd_next = pfd;
7850Sstevel@tonic-gate 		else
7860Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
7890Sstevel@tonic-gate 		pfv->pfv_argc++;
7900Sstevel@tonic-gate 		nfd = pfd;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		if (p > q) {
7930Sstevel@tonic-gate 			pfd->pfd_preflen = (size_t)(p - q);
7940Sstevel@tonic-gate 			pfd->pfd_prefix = q;
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		fmt_switch:
7980Sstevel@tonic-gate 		switch (c = *++p) {
7990Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
8000Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
8010Sstevel@tonic-gate 			if (dot == 0 && digits == 0 && c == '0') {
8020Sstevel@tonic-gate 				pfd->pfd_flags |= DT_PFCONV_ZPAD;
8030Sstevel@tonic-gate 				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
8040Sstevel@tonic-gate 				goto fmt_switch;
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 			for (n = 0; isdigit(c); c = *++p)
8080Sstevel@tonic-gate 				n = n * 10 + c - '0';
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 			if (dot)
8110Sstevel@tonic-gate 				pfd->pfd_prec = n;
8120Sstevel@tonic-gate 			else
8130Sstevel@tonic-gate 				pfd->pfd_width = n;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 			p--;
8160Sstevel@tonic-gate 			digits++;
8170Sstevel@tonic-gate 			goto fmt_switch;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		case '#':
8200Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_ALT;
8210Sstevel@tonic-gate 			goto fmt_switch;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 		case '*':
8240Sstevel@tonic-gate 			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 			if (pfd->pfd_flags & n) {
8270Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8280Sstevel@tonic-gate 				    "one '*' specified for the output %s\n",
8290Sstevel@tonic-gate 				    pfv->pfv_argc, n ? "precision" : "width");
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8320Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8330Sstevel@tonic-gate 			}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 			pfd->pfd_flags |= n;
8360Sstevel@tonic-gate 			goto fmt_switch;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		case '+':
8390Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPOS;
8400Sstevel@tonic-gate 			goto fmt_switch;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 		case '-':
8430Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_LEFT;
8440Sstevel@tonic-gate 			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
8450Sstevel@tonic-gate 			goto fmt_switch;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 		case '.':
8480Sstevel@tonic-gate 			if (dot++ != 0) {
8490Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8500Sstevel@tonic-gate 				    "one '.' specified\n", pfv->pfv_argc);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8530Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8540Sstevel@tonic-gate 			}
8550Sstevel@tonic-gate 			digits = 0;
8560Sstevel@tonic-gate 			goto fmt_switch;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		case '?':
8590Sstevel@tonic-gate 			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
8600Sstevel@tonic-gate 				pfd->pfd_width = 16;
8610Sstevel@tonic-gate 			else
8620Sstevel@tonic-gate 				pfd->pfd_width = 8;
8630Sstevel@tonic-gate 			goto fmt_switch;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 		case '@':
8660Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_AGG;
8670Sstevel@tonic-gate 			goto fmt_switch;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		case '\'':
8700Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_GROUP;
8710Sstevel@tonic-gate 			goto fmt_switch;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		case ' ':
8740Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPACE;
8750Sstevel@tonic-gate 			goto fmt_switch;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		case '$':
8780Sstevel@tonic-gate 			yywarn("format conversion #%u uses unsupported "
8790Sstevel@tonic-gate 			    "positional format (%%n$)\n", pfv->pfv_argc);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8820Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 		case '%':
8850Sstevel@tonic-gate 			if (p[-1] == '%')
8860Sstevel@tonic-gate 				goto default_lbl; /* if %% then use "%" conv */
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			yywarn("format conversion #%u cannot be combined "
8890Sstevel@tonic-gate 			    "with other format flags: %%%%\n", pfv->pfv_argc);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8920Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		case '\0':
8950Sstevel@tonic-gate 			yywarn("format conversion #%u name expected before "
8960Sstevel@tonic-gate 			    "end of format string\n", pfv->pfv_argc);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8990Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		case 'h':
9020Sstevel@tonic-gate 		case 'l':
9030Sstevel@tonic-gate 		case 'L':
9040Sstevel@tonic-gate 		case 'w':
9050Sstevel@tonic-gate 			if (namelen < sizeof (name) - 2)
9060Sstevel@tonic-gate 				name[namelen++] = c;
9070Sstevel@tonic-gate 			goto fmt_switch;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		default_lbl:
9100Sstevel@tonic-gate 		default:
9110Sstevel@tonic-gate 			name[namelen++] = c;
9120Sstevel@tonic-gate 			name[namelen] = '\0';
9130Sstevel@tonic-gate 		}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		if (pfd->pfd_conv == NULL) {
9180Sstevel@tonic-gate 			yywarn("format conversion #%u is undefined: %%%s\n",
9190Sstevel@tonic-gate 			    pfv->pfv_argc, name);
9200Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9210Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9220Sstevel@tonic-gate 		}
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	if (*q != '\0' || *format == '\0') {
9260Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
9270Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9280Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
9320Sstevel@tonic-gate 			nfd->pfd_next = pfd;
9330Sstevel@tonic-gate 		else
9340Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
9370Sstevel@tonic-gate 		pfv->pfv_argc++;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		pfd->pfd_prefix = q;
9400Sstevel@tonic-gate 		pfd->pfd_preflen = strlen(q);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	return (pfv);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate void
9470Sstevel@tonic-gate dt_printf_destroy(dt_pfargv_t *pfv)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
9520Sstevel@tonic-gate 		nfd = pfd->pfd_next;
9530Sstevel@tonic-gate 		free(pfd);
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	free(pfv->pfv_format);
9570Sstevel@tonic-gate 	free(pfv);
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate void
9610Sstevel@tonic-gate dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
9620Sstevel@tonic-gate     dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
9650Sstevel@tonic-gate 	const char *func = idp->di_name;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
9680Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
9690Sstevel@tonic-gate 	const char *aggtype;
9700Sstevel@tonic-gate 	dt_node_t aggnode;
9710Sstevel@tonic-gate 	int i, j;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (pfv->pfv_format[0] == '\0') {
9740Sstevel@tonic-gate 		xyerror(D_PRINTF_FMT_EMPTY,
9750Sstevel@tonic-gate 		    "%s( ) format string is empty\n", func);
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 
978457Sbmc 	pfv->pfv_flags = flags;
979457Sbmc 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * We fake up a parse node representing the type that can be used with
982*491Sbmc 	 * an aggregation result conversion, which -- for all but count() --
983*491Sbmc 	 * is a signed quantity.
9840Sstevel@tonic-gate 	 */
985*491Sbmc 	if (kind != DTRACEAGG_COUNT)
9860Sstevel@tonic-gate 		aggtype = "int64_t";
9870Sstevel@tonic-gate 	else
9880Sstevel@tonic-gate 		aggtype = "uint64_t";
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if (dt_type_lookup(aggtype, &dtt) != 0)
9910Sstevel@tonic-gate 		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	bzero(&aggnode, sizeof (aggnode));
9940Sstevel@tonic-gate 	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
9970Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
9980Sstevel@tonic-gate 		const char *dyns[2];
9990Sstevel@tonic-gate 		int dync = 0;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 		char vname[64];
10020Sstevel@tonic-gate 		dt_node_t *vnp;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 		if (pfc == NULL)
10050Sstevel@tonic-gate 			continue; /* no checking if argd is just a prefix */
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
10080Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10090Sstevel@tonic-gate 			continue;
10100Sstevel@tonic-gate 		}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
10130Sstevel@tonic-gate 			dyns[dync++] = ".*";
10140Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
10150Sstevel@tonic-gate 			dyns[dync++] = "*";
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 		for (; dync != 0; dync--) {
10180Sstevel@tonic-gate 			if (dnp == NULL) {
10190Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_PROTO,
10200Sstevel@tonic-gate 				    "%s( ) prototype mismatch: conversion "
10210Sstevel@tonic-gate 				    "#%d (%%%s) is missing a corresponding "
10220Sstevel@tonic-gate 				    "\"%s\" argument\n", func, i + 1,
10230Sstevel@tonic-gate 				    pfc->pfc_name, dyns[dync - 1]);
10240Sstevel@tonic-gate 			}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 			if (dt_node_is_integer(dnp) == 0) {
10270Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_TYPE,
10280Sstevel@tonic-gate 				    "%s( ) argument #%d is incompatible "
10290Sstevel@tonic-gate 				    "with conversion #%d prototype:\n"
10300Sstevel@tonic-gate 				    "\tconversion: %% %s %s\n"
10310Sstevel@tonic-gate 				    "\t prototype: int\n\t  argument: %s\n",
10320Sstevel@tonic-gate 				    func, j + foff + 1, i + 1,
10330Sstevel@tonic-gate 				    dyns[dync - 1], pfc->pfc_name,
10340Sstevel@tonic-gate 				    dt_node_type_name(dnp, n, sizeof (n)));
10350Sstevel@tonic-gate 			}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 			dnp = dnp->dn_list;
10380Sstevel@tonic-gate 			j++;
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 		/*
10420Sstevel@tonic-gate 		 * If this conversion is consuming the aggregation data, set
10430Sstevel@tonic-gate 		 * the value node pointer (vnp) to a fake node based on the
10440Sstevel@tonic-gate 		 * aggregating function result type.  Otherwise assign vnp to
10450Sstevel@tonic-gate 		 * the next parse node in the argument list, if there is one.
10460Sstevel@tonic-gate 		 */
10470Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
10480Sstevel@tonic-gate 			if (!(flags & DT_PRINTF_AGGREGATION)) {
10490Sstevel@tonic-gate 				xyerror(D_PRINTF_AGG_CONV,
10500Sstevel@tonic-gate 				    "%%@ conversion requires an aggregation"
10510Sstevel@tonic-gate 				    " and is not for use with %s( )\n", func);
10520Sstevel@tonic-gate 			}
10530Sstevel@tonic-gate 			(void) strlcpy(vname, "aggregating action",
10540Sstevel@tonic-gate 			    sizeof (vname));
10550Sstevel@tonic-gate 			vnp = &aggnode;
10560Sstevel@tonic-gate 		} else if (dnp == NULL) {
10570Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_PROTO,
10580Sstevel@tonic-gate 			    "%s( ) prototype mismatch: conversion #%d (%%"
10590Sstevel@tonic-gate 			    "%s) is missing a corresponding value argument\n",
10600Sstevel@tonic-gate 			    func, i + 1, pfc->pfc_name);
10610Sstevel@tonic-gate 		} else {
10620Sstevel@tonic-gate 			(void) snprintf(vname, sizeof (vname),
10630Sstevel@tonic-gate 			    "argument #%d", j + foff + 1);
10640Sstevel@tonic-gate 			vnp = dnp;
10650Sstevel@tonic-gate 			dnp = dnp->dn_list;
10660Sstevel@tonic-gate 			j++;
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		/*
10700Sstevel@tonic-gate 		 * Fill in the proposed final format string by prepending any
10710Sstevel@tonic-gate 		 * size-related prefixes to the pfconv's format string.  The
10720Sstevel@tonic-gate 		 * pfc_check() function below may optionally modify the format
10730Sstevel@tonic-gate 		 * as part of validating the type of the input argument.
10740Sstevel@tonic-gate 		 */
10750Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_sint ||
10760Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_uint ||
10770Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_dint) {
10780Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (uint64_t))
10790Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "ll");
10800Sstevel@tonic-gate 		} else if (pfc->pfc_print == &pfprint_fp) {
10810Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (long double))
10820Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "L");
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 		/*
10880Sstevel@tonic-gate 		 * Validate the format conversion against the value node type.
10890Sstevel@tonic-gate 		 * If the conversion is good, create the descriptor format
10900Sstevel@tonic-gate 		 * string by concatenating together any required printf(3C)
10910Sstevel@tonic-gate 		 * size prefixes with the conversion's native format string.
10920Sstevel@tonic-gate 		 */
1093457Sbmc 		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
10940Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_TYPE,
10950Sstevel@tonic-gate 			    "%s( ) %s is incompatible with "
10960Sstevel@tonic-gate 			    "conversion #%d prototype:\n\tconversion: %%%s\n"
10970Sstevel@tonic-gate 			    "\t prototype: %s\n\t  argument: %s\n", func,
10980Sstevel@tonic-gate 			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
10990Sstevel@tonic-gate 			    dt_node_type_name(vnp, n, sizeof (n)));
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
11040Sstevel@tonic-gate 		xyerror(D_PRINTF_ARG_EXTRA,
11050Sstevel@tonic-gate 		    "%s( ) prototype mismatch: only %d arguments "
11060Sstevel@tonic-gate 		    "required by this format string\n", func, j);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate static int
11110Sstevel@tonic-gate dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
11120Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len, int *ip)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	uintptr_t addr;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (nrecs == 0)
11170Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	addr = (uintptr_t)buf + recp->dtrd_offset;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (addr + sizeof (int) > (uintptr_t)buf + len)
11220Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DOFFSET));
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	if (addr & (recp->dtrd_alignment - 1))
11250Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DALIGN));
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	switch (recp->dtrd_size) {
11280Sstevel@tonic-gate 	case sizeof (int8_t):
11290Sstevel@tonic-gate 		*ip = (int)*((int8_t *)addr);
11300Sstevel@tonic-gate 		break;
11310Sstevel@tonic-gate 	case sizeof (int16_t):
11320Sstevel@tonic-gate 		*ip = (int)*((int16_t *)addr);
11330Sstevel@tonic-gate 		break;
11340Sstevel@tonic-gate 	case sizeof (int32_t):
11350Sstevel@tonic-gate 		*ip = (int)*((int32_t *)addr);
11360Sstevel@tonic-gate 		break;
11370Sstevel@tonic-gate 	case sizeof (int64_t):
11380Sstevel@tonic-gate 		*ip = (int)*((int64_t *)addr);
11390Sstevel@tonic-gate 		break;
11400Sstevel@tonic-gate 	default:
11410Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	return (0);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*ARGSUSED*/
11480Sstevel@tonic-gate static int
11490Sstevel@tonic-gate pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
11500Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
11510Sstevel@tonic-gate {
11520Sstevel@tonic-gate 	const uint64_t *data = addr;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	if (size != sizeof (uint64_t) * 2)
11550Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format,
11580Sstevel@tonic-gate 	    data[0] ? data[1] / normal / data[0] : 0));
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate /*ARGSUSED*/
11620Sstevel@tonic-gate static int
11630Sstevel@tonic-gate pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
11640Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	return (dt_print_quantize(dtp, fp, addr, size, normal));
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*ARGSUSED*/
11700Sstevel@tonic-gate static int
11710Sstevel@tonic-gate pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
11720Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	return (dt_print_lquantize(dtp, fp, addr, size, normal));
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate static int
11780Sstevel@tonic-gate dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
11790Sstevel@tonic-gate     const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1180457Sbmc     size_t len, const dtrace_aggdata_t *adp)
11810Sstevel@tonic-gate {
11820Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
11830Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = recs;
11840Sstevel@tonic-gate 	const dtrace_recdesc_t *aggr = NULL;
11850Sstevel@tonic-gate 	uchar_t *lim = (uchar_t *)buf + len;
11860Sstevel@tonic-gate 	char format[64] = "%";
1187457Sbmc 	uint64_t normal = adp ? adp->dtada_normal : 1;
11880Sstevel@tonic-gate 	int i;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/*
11910Sstevel@tonic-gate 	 * If we are formatting an aggregation, set 'aggr' to the final record
11920Sstevel@tonic-gate 	 * description (the aggregation result) so we can use this record with
11930Sstevel@tonic-gate 	 * any conversion where DT_PFCONV_AGG is set.  We then decrement nrecs
11940Sstevel@tonic-gate 	 * to prevent this record from being used with any other conversion.
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1197457Sbmc 		assert(adp != NULL);
11980Sstevel@tonic-gate 		if (nrecs == 0)
11990Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DMISMATCH));
12000Sstevel@tonic-gate 		aggr = recp + nrecs - 1;
12010Sstevel@tonic-gate 		nrecs--;
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
12050Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
12060Sstevel@tonic-gate 		int width = pfd->pfd_width;
12070Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
12080Sstevel@tonic-gate 		int rval;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 		char *f = format + 1; /* skip initial '%' */
12110Sstevel@tonic-gate 		const dtrace_recdesc_t *rec;
12120Sstevel@tonic-gate 		dt_pfprint_f *func;
12130Sstevel@tonic-gate 		uchar_t *addr;
12140Sstevel@tonic-gate 		size_t size;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
12170Sstevel@tonic-gate 			char *tmp = alloca(pfd->pfd_preflen + 1);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
12200Sstevel@tonic-gate 			tmp[pfd->pfd_preflen] = '\0';
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
12230Sstevel@tonic-gate 				return (rval);
1224457Sbmc 
1225457Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1226457Sbmc 				/*
1227457Sbmc 				 * For printa(), we flush the buffer after each
1228457Sbmc 				 * prefix, setting the record to NULL to
1229457Sbmc 				 * indicate that this does not correspond to
1230457Sbmc 				 * a particular tuple element, but is rather
1231457Sbmc 				 * part of the format string.
1232457Sbmc 				 */
1233457Sbmc 				if (dt_buffered_flush(dtp, NULL, NULL, adp) < 0)
1234457Sbmc 					return (-1);
1235457Sbmc 			}
12360Sstevel@tonic-gate 		}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 		if (pfc == NULL) {
12390Sstevel@tonic-gate 			if (pfv->pfv_argc == 1)
12400Sstevel@tonic-gate 				return (nrecs != 0);
12410Sstevel@tonic-gate 			continue;
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 		/*
12450Sstevel@tonic-gate 		 * If the conversion is %%, just invoke the print callback
12460Sstevel@tonic-gate 		 * with no data record and continue; it consumes no record.
12470Sstevel@tonic-gate 		 */
12480Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
12490Sstevel@tonic-gate 			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
12500Sstevel@tonic-gate 				continue;
12510Sstevel@tonic-gate 			return (-1); /* errno is set for us */
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
12550Sstevel@tonic-gate 			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
12560Sstevel@tonic-gate 			    len, &width) == -1)
12570Sstevel@tonic-gate 				return (-1); /* errno is set for us */
12580Sstevel@tonic-gate 			pfd->pfd_dynwidth = width;
12590Sstevel@tonic-gate 		} else {
12600Sstevel@tonic-gate 			pfd->pfd_dynwidth = 0;
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
12640Sstevel@tonic-gate 		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
12650Sstevel@tonic-gate 			return (-1); /* errno is set for us */
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
12680Sstevel@tonic-gate 			if (aggr == NULL)
12690Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
12700Sstevel@tonic-gate 			rec = aggr;
12710Sstevel@tonic-gate 		} else {
12720Sstevel@tonic-gate 			if (nrecs == 0)
12730Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
12740Sstevel@tonic-gate 			rec = recp++;
12750Sstevel@tonic-gate 			nrecs--;
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		addr = (uchar_t *)buf + rec->dtrd_offset;
12790Sstevel@tonic-gate 		size = rec->dtrd_size;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		if (addr + size > lim) {
12820Sstevel@tonic-gate 			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
12830Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, (void *)lim);
12840Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DOFFSET));
12850Sstevel@tonic-gate 		}
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 		if (rec->dtrd_alignment != 0 &&
12880Sstevel@tonic-gate 		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
12890Sstevel@tonic-gate 			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
12900Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
12910Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DALIGN));
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		switch (rec->dtrd_action) {
12950Sstevel@tonic-gate 		case DTRACEAGG_AVG:
12960Sstevel@tonic-gate 			func = pfprint_average;
12970Sstevel@tonic-gate 			break;
12980Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
12990Sstevel@tonic-gate 			func = pfprint_quantize;
13000Sstevel@tonic-gate 			break;
13010Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
13020Sstevel@tonic-gate 			func = pfprint_lquantize;
13030Sstevel@tonic-gate 			break;
1304457Sbmc 		case DTRACEACT_MOD:
1305457Sbmc 			func = pfprint_mod;
1306457Sbmc 			break;
1307457Sbmc 		case DTRACEACT_UMOD:
1308457Sbmc 			func = pfprint_umod;
1309457Sbmc 			break;
13100Sstevel@tonic-gate 		default:
13110Sstevel@tonic-gate 			func = pfc->pfc_print;
13120Sstevel@tonic-gate 			break;
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
13160Sstevel@tonic-gate 			*f++ = '#';
13170Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
13180Sstevel@tonic-gate 			*f++ = '0';
13190Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
13200Sstevel@tonic-gate 			*f++ = '-';
13210Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
13220Sstevel@tonic-gate 			*f++ = '+';
13230Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
13240Sstevel@tonic-gate 			*f++ = '\'';
13250Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
13260Sstevel@tonic-gate 			*f++ = ' ';
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 		/*
13290Sstevel@tonic-gate 		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
13300Sstevel@tonic-gate 		 * don't add the width to the format string.  See the block
13310Sstevel@tonic-gate 		 * comment in pfprint_stack() for a description of the
13320Sstevel@tonic-gate 		 * behavior in this case.
13330Sstevel@tonic-gate 		 */
13340Sstevel@tonic-gate 		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
13350Sstevel@tonic-gate 			width = 0;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		if (width != 0)
13380Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), "%d", width);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		if (prec != 0)
13410Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		(void) strcpy(f, pfd->pfd_fmt);
13440Sstevel@tonic-gate 		pfd->pfd_rec = rec;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 		if (func(dtp, fp, format, pfd, addr, size,
13470Sstevel@tonic-gate 		    rec == aggr ? normal : 1) < 0)
13480Sstevel@tonic-gate 			return (-1); /* errno is set for us */
1349457Sbmc 
1350457Sbmc 		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1351457Sbmc 			/*
1352457Sbmc 			 * For printa(), we flush the buffer after each tuple
1353457Sbmc 			 * element.
1354457Sbmc 			 */
1355457Sbmc 			if (dt_buffered_flush(dtp, NULL, rec, adp) < 0)
1356457Sbmc 				return (-1);
1357457Sbmc 		}
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	return ((int)(recp - recs));
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate int
13640Sstevel@tonic-gate dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
13650Sstevel@tonic-gate     const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
13660Sstevel@tonic-gate {
13670Sstevel@tonic-gate 	dtrace_optval_t size;
13680Sstevel@tonic-gate 	int rval;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "strsize", &size);
13710Sstevel@tonic-gate 	assert(rval == 0);
13720Sstevel@tonic-gate 	assert(dtp->dt_sprintf_buflen == 0);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	if (dtp->dt_sprintf_buf != NULL)
13750Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
13780Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	bzero(dtp->dt_sprintf_buf, size);
13810Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = size;
1382457Sbmc 	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len, NULL);
13830Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = 0;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	if (rval == -1)
13860Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	return (rval);
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate /*ARGSUSED*/
13920Sstevel@tonic-gate int
13930Sstevel@tonic-gate dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
13940Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
13950Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
13960Sstevel@tonic-gate {
13970Sstevel@tonic-gate 	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	if (rval == -1)
14000Sstevel@tonic-gate 		return (rval);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	/*
14030Sstevel@tonic-gate 	 * Before we execute the specified command, flush fp to assure that
14040Sstevel@tonic-gate 	 * any prior dt_printf()'s appear before the output of the command
14050Sstevel@tonic-gate 	 * not after it.
14060Sstevel@tonic-gate 	 */
14070Sstevel@tonic-gate 	(void) fflush(fp);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	if (system(dtp->dt_sprintf_buf) == -1)
14100Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	return (rval);
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate int
14160Sstevel@tonic-gate dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
14170Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
14180Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	char selfbuf[40], restorebuf[40], *filename;
14210Sstevel@tonic-gate 	FILE *nfp;
14220Sstevel@tonic-gate 	int rval, errval;
14230Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
14240Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	if (rval == -1 || fp == NULL)
14290Sstevel@tonic-gate 		return (rval);
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	if (pfd->pfd_preflen != 0 &&
14320Sstevel@tonic-gate 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
14330Sstevel@tonic-gate 		/*
14340Sstevel@tonic-gate 		 * The only way to have the format string set to the value
14350Sstevel@tonic-gate 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
14360Sstevel@tonic-gate 		 * denoting that we should restore the old stdout.
14370Sstevel@tonic-gate 		 */
14380Sstevel@tonic-gate 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 		if (dtp->dt_stdout_fd == -1) {
14410Sstevel@tonic-gate 			/*
14420Sstevel@tonic-gate 			 * We could complain here by generating an error,
14430Sstevel@tonic-gate 			 * but it seems like overkill:  it seems that calling
14440Sstevel@tonic-gate 			 * freopen() to restore stdout when freopen() has
14450Sstevel@tonic-gate 			 * never before been called should just be a no-op,
14460Sstevel@tonic-gate 			 * so we just return in this case.
14470Sstevel@tonic-gate 			 */
14480Sstevel@tonic-gate 			return (rval);
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 		(void) snprintf(restorebuf, sizeof (restorebuf),
14520Sstevel@tonic-gate 		    "/dev/fd/%d", dtp->dt_stdout_fd);
14530Sstevel@tonic-gate 		filename = restorebuf;
14540Sstevel@tonic-gate 	} else {
14550Sstevel@tonic-gate 		filename = dtp->dt_sprintf_buf;
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	/*
14590Sstevel@tonic-gate 	 * freopen(3C) will always close the specified stream and underlying
14600Sstevel@tonic-gate 	 * file descriptor -- even if the specified file can't be opened.
14610Sstevel@tonic-gate 	 * Even for the semantic cesspool that is standard I/O, this is
14620Sstevel@tonic-gate 	 * surprisingly brain-dead behavior:  it means that any failure to
14630Sstevel@tonic-gate 	 * open the specified file destroys the specified stream in the
14640Sstevel@tonic-gate 	 * process -- which is particularly relevant when the specified stream
14650Sstevel@tonic-gate 	 * happens (or rather, happened) to be stdout.  This could be resolved
14660Sstevel@tonic-gate 	 * were there an "fdreopen()" equivalent of freopen() that allowed one
14670Sstevel@tonic-gate 	 * to pass a file descriptor instead of the name of a file, but there
14680Sstevel@tonic-gate 	 * is no such thing.  However, we can effect this ourselves by first
14690Sstevel@tonic-gate 	 * fopen()'ing the desired file, and then (assuming that that works),
14700Sstevel@tonic-gate 	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
14710Sstevel@tonic-gate 	 * file descriptor for the fopen()'d file.  This way, if the fopen()
14720Sstevel@tonic-gate 	 * fails, we can fail the operation without destroying stdout.
14730Sstevel@tonic-gate 	 */
14740Sstevel@tonic-gate 	if ((nfp = fopen(filename, "aw")) == NULL) {
14750Sstevel@tonic-gate 		char *msg = strerror(errno), *faultstr;
14760Sstevel@tonic-gate 		int len = 80;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		len += strlen(msg) + strlen(filename);
14790Sstevel@tonic-gate 		faultstr = alloca(len);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
14820Sstevel@tonic-gate 		    filename, strerror(errno));
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
14850Sstevel@tonic-gate 			return (rval);
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		return (errval);
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	if (dtp->dt_stdout_fd == -1) {
14930Sstevel@tonic-gate 		/*
14940Sstevel@tonic-gate 		 * If this is the first time that we're calling freopen(),
14950Sstevel@tonic-gate 		 * we're going to stash away the file descriptor for stdout.
14960Sstevel@tonic-gate 		 * We don't expect the dup(2) to fail, so if it does we must
14970Sstevel@tonic-gate 		 * return failure.
14980Sstevel@tonic-gate 		 */
14990Sstevel@tonic-gate 		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
15000Sstevel@tonic-gate 			(void) fclose(nfp);
15010Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	if (freopen(selfbuf, "aw", fp) == NULL) {
15060Sstevel@tonic-gate 		(void) fclose(nfp);
15070Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	(void) fclose(nfp);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	return (rval);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /*ARGSUSED*/
15160Sstevel@tonic-gate int
15170Sstevel@tonic-gate dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15180Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15190Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15200Sstevel@tonic-gate {
1521457Sbmc 	return (dt_printf_format(dtp, fp, fmtdata,
1522457Sbmc 	    recp, nrecs, buf, len, NULL));
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate void *
15260Sstevel@tonic-gate dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
15290Sstevel@tonic-gate 	dt_pfargd_t *pfd;
15300Sstevel@tonic-gate 	int i;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if (pfv == NULL)
15330Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	pfd = pfv->pfv_argv;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
15380Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 		if (pfc == NULL)
15410Sstevel@tonic-gate 			continue;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 		/*
15440Sstevel@tonic-gate 		 * If the output format is not %s then we assume that we have
15450Sstevel@tonic-gate 		 * been given a correctly-sized format string, so we copy the
15460Sstevel@tonic-gate 		 * true format name including the size modifier.  If the output
15470Sstevel@tonic-gate 		 * format is %s, then either the input format is %s as well or
15480Sstevel@tonic-gate 		 * it is one of our custom formats (e.g. pfprint_addr), so we
15490Sstevel@tonic-gate 		 * must set pfd_fmt to be the output format conversion "s".
15500Sstevel@tonic-gate 		 */
15510Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") != 0)
15520Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
15530Sstevel@tonic-gate 		else
15540Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (pfv);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate void *
15610Sstevel@tonic-gate dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if (pfv == NULL)
15660Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	return (pfv);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate /*ARGSUSED*/
15740Sstevel@tonic-gate size_t
15750Sstevel@tonic-gate dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
15760Sstevel@tonic-gate {
15770Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
15780Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/*
15810Sstevel@tonic-gate 	 * An upper bound on the string length is the length of the original
15820Sstevel@tonic-gate 	 * format string, plus three times the number of conversions (each
15830Sstevel@tonic-gate 	 * conversion could add up an additional "ll" and/or pfd_width digit
15840Sstevel@tonic-gate 	 * in the case of converting %? to %16) plus one for a terminating \0.
15850Sstevel@tonic-gate 	 */
15860Sstevel@tonic-gate 	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
15870Sstevel@tonic-gate 	char *format = alloca(formatlen);
15880Sstevel@tonic-gate 	char *f = format;
15890Sstevel@tonic-gate 	int i, j;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
15920Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
15930Sstevel@tonic-gate 		const char *str;
15940Sstevel@tonic-gate 		int width = pfd->pfd_width;
15950Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
15980Sstevel@tonic-gate 			for (j = 0; j < pfd->pfd_preflen; j++)
15990Sstevel@tonic-gate 				*f++ = pfd->pfd_prefix[j];
16000Sstevel@tonic-gate 		}
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 		if (pfc == NULL)
16030Sstevel@tonic-gate 			continue;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 		*f++ = '%';
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
16080Sstevel@tonic-gate 			*f++ = '#';
16090Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
16100Sstevel@tonic-gate 			*f++ = '0';
16110Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
16120Sstevel@tonic-gate 			*f++ = '-';
16130Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
16140Sstevel@tonic-gate 			*f++ = '+';
16150Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
16160Sstevel@tonic-gate 			*f++ = '*';
16170Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
16180Sstevel@tonic-gate 			*f++ = '.';
16190Sstevel@tonic-gate 			*f++ = '*';
16200Sstevel@tonic-gate 		}
16210Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
16220Sstevel@tonic-gate 			*f++ = '\'';
16230Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
16240Sstevel@tonic-gate 			*f++ = ' ';
16250Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG)
16260Sstevel@tonic-gate 			*f++ = '@';
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 		if (width != 0)
16290Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), "%d", width);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		if (prec != 0)
16320Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 		/*
16350Sstevel@tonic-gate 		 * If the output format is %s, then either %s is the underlying
16360Sstevel@tonic-gate 		 * conversion or the conversion is one of our customized ones,
16370Sstevel@tonic-gate 		 * e.g. pfprint_addr.  In these cases, put the original string
16380Sstevel@tonic-gate 		 * name of the conversion (pfc_name) into the pickled format
16390Sstevel@tonic-gate 		 * string rather than the derived conversion (pfd_fmt).
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") == 0)
16420Sstevel@tonic-gate 			str = pfc->pfc_name;
16430Sstevel@tonic-gate 		else
16440Sstevel@tonic-gate 			str = pfd->pfd_fmt;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 		for (j = 0; str[j] != '\0'; j++)
16470Sstevel@tonic-gate 			*f++ = str[j];
16480Sstevel@tonic-gate 	}
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	*f = '\0'; /* insert nul byte; do not count in return value */
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	assert(f < format + formatlen);
16530Sstevel@tonic-gate 	(void) strncpy(s, format, len);
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	return ((size_t)(f - format));
16560Sstevel@tonic-gate }
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate static int
1659457Sbmc dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
16600Sstevel@tonic-gate {
1661457Sbmc 	const dtrace_aggdesc_t *agg = adp->dtada_desc;
16620Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
16630Sstevel@tonic-gate 	uint_t nrecs = agg->dtagd_nrecs;
16640Sstevel@tonic-gate 	dt_pfwalk_t *pfw = arg;
1665457Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
16660Sstevel@tonic-gate 	int id;
16670Sstevel@tonic-gate 
1668457Sbmc 	if (dt_printf_getint(dtp, recp++, nrecs--,
16690Sstevel@tonic-gate 	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
16700Sstevel@tonic-gate 		return (0); /* no aggregation id or id does not match */
16710Sstevel@tonic-gate 
1672457Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1673457Sbmc 	    recp, nrecs, adp->dtada_data, adp->dtada_size, adp) == -1)
1674457Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
16750Sstevel@tonic-gate 
1676457Sbmc 	/*
1677457Sbmc 	 * Cast away the const to set the bit indicating that this aggregation
1678457Sbmc 	 * has been printed.
1679457Sbmc 	 */
1680457Sbmc 	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	return (0);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate /*ARGSUSED*/
16860Sstevel@tonic-gate int
16870Sstevel@tonic-gate dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
16880Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
16890Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
16900Sstevel@tonic-gate {
16910Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = recs;
16920Sstevel@tonic-gate 	dt_pfwalk_t pfw;
16930Sstevel@tonic-gate 	int id;
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	if (dt_printf_getint(dtp, recp++, nrecs--, buf, len, &id) == -1)
16960Sstevel@tonic-gate 		return (-1); /* errno is set for us */
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	pfw.pfw_argv = fmtdata;
16990Sstevel@tonic-gate 	pfw.pfw_aid = id;
17000Sstevel@tonic-gate 	pfw.pfw_fp = fp;
17010Sstevel@tonic-gate 	pfw.pfw_err = 0;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	if (dtrace_aggregate_walk_valsorted(dtp, dt_fprinta, &pfw) == -1 ||
17040Sstevel@tonic-gate 	    pfw.pfw_err != 0)
17050Sstevel@tonic-gate 		return (-1); /* errno is set for us */
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	return ((int)(recp - recs));
17080Sstevel@tonic-gate }
1709