xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_printf.c (revision 12902:3bb859a7330c)
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*12902SBryan.Cantrill@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/sysmacros.h>
270Sstevel@tonic-gate #include <strings.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <alloca.h>
300Sstevel@tonic-gate #include <assert.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <limits.h>
34*12902SBryan.Cantrill@Sun.COM #include <sys/socket.h>
35*12902SBryan.Cantrill@Sun.COM #include <netdb.h>
36*12902SBryan.Cantrill@Sun.COM #include <netinet/in.h>
37*12902SBryan.Cantrill@Sun.COM #include <arpa/inet.h>
38*12902SBryan.Cantrill@Sun.COM #include <arpa/nameser.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <dt_printf.h>
410Sstevel@tonic-gate #include <dt_string.h>
420Sstevel@tonic-gate #include <dt_impl.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*ARGSUSED*/
450Sstevel@tonic-gate static int
pfcheck_addr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)46457Sbmc pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
490Sstevel@tonic-gate }
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*ARGSUSED*/
520Sstevel@tonic-gate static int
pfcheck_kaddr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)53457Sbmc pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
54457Sbmc {
55457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
56457Sbmc 	    dt_node_is_symaddr(dnp));
57457Sbmc }
58457Sbmc 
59457Sbmc /*ARGSUSED*/
60457Sbmc static int
pfcheck_uaddr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)61457Sbmc pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
62457Sbmc {
63457Sbmc 	dtrace_hdl_t *dtp = pfv->pfv_dtp;
64457Sbmc 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
65457Sbmc 
66457Sbmc 	if (dt_node_is_usymaddr(dnp))
67457Sbmc 		return (1);
68457Sbmc 
69457Sbmc 	if (idp == NULL || idp->di_id == 0)
70457Sbmc 		return (0);
71457Sbmc 
72457Sbmc 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
73457Sbmc }
74457Sbmc 
75457Sbmc /*ARGSUSED*/
76457Sbmc static int
pfcheck_stack(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)77457Sbmc pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
78457Sbmc {
79457Sbmc 	return (dt_node_is_stack(dnp));
80457Sbmc }
81457Sbmc 
82457Sbmc /*ARGSUSED*/
83457Sbmc static int
pfcheck_time(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)841017Sbmc pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
851017Sbmc {
861017Sbmc 	return (dt_node_is_integer(dnp) &&
871017Sbmc 	    dt_node_type_size(dnp) == sizeof (uint64_t));
881017Sbmc }
891017Sbmc 
901017Sbmc /*ARGSUSED*/
911017Sbmc static int
pfcheck_str(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)92457Sbmc pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	ctf_file_t *ctfp;
950Sstevel@tonic-gate 	ctf_encoding_t e;
960Sstevel@tonic-gate 	ctf_arinfo_t r;
970Sstevel@tonic-gate 	ctf_id_t base;
980Sstevel@tonic-gate 	uint_t kind;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (dt_node_is_string(dnp))
1010Sstevel@tonic-gate 		return (1);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	ctfp = dnp->dn_ctfp;
1040Sstevel@tonic-gate 	base = ctf_type_resolve(ctfp, dnp->dn_type);
1050Sstevel@tonic-gate 	kind = ctf_type_kind(ctfp, base);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1080Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1090Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*ARGSUSED*/
1130Sstevel@tonic-gate static int
pfcheck_wstr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)114457Sbmc pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1170Sstevel@tonic-gate 	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
1180Sstevel@tonic-gate 	uint_t kind = ctf_type_kind(ctfp, base);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	ctf_encoding_t e;
1210Sstevel@tonic-gate 	ctf_arinfo_t r;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
1240Sstevel@tonic-gate 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
1250Sstevel@tonic-gate 	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
1260Sstevel@tonic-gate 	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*ARGSUSED*/
1300Sstevel@tonic-gate static int
pfcheck_csi(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)131457Sbmc pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	return (dt_node_is_integer(dnp) &&
1340Sstevel@tonic-gate 	    dt_node_type_size(dnp) <= sizeof (int));
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*ARGSUSED*/
1380Sstevel@tonic-gate static int
pfcheck_fp(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)139457Sbmc pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	return (dt_node_is_float(dnp));
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*ARGSUSED*/
1450Sstevel@tonic-gate static int
pfcheck_xint(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)146457Sbmc pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
151457Sbmc /*ARGSUSED*/
1520Sstevel@tonic-gate static int
pfcheck_dint(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)153457Sbmc pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	if (dnp->dn_flags & DT_NF_SIGNED)
1560Sstevel@tonic-gate 		pfd->pfd_flags |= DT_PFCONV_SIGNED;
1570Sstevel@tonic-gate 	else
1580Sstevel@tonic-gate 		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	return (dt_node_is_integer(dnp));
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*ARGSUSED*/
1640Sstevel@tonic-gate static int
pfcheck_xshort(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)165457Sbmc pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1680Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1690Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1720Sstevel@tonic-gate 	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
1730Sstevel@tonic-gate 	    strcmp(n, "unsigned short") == 0));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*ARGSUSED*/
1770Sstevel@tonic-gate static int
pfcheck_xlong(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)178457Sbmc pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1810Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
1820Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
1850Sstevel@tonic-gate 	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
1860Sstevel@tonic-gate 	    strcmp(n, "unsigned long") == 0));
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*ARGSUSED*/
1900Sstevel@tonic-gate static int
pfcheck_xlonglong(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)191457Sbmc pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	ctf_file_t *ctfp = dnp->dn_ctfp;
1940Sstevel@tonic-gate 	ctf_id_t type = dnp->dn_type;
1950Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
1980Sstevel@tonic-gate 	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
1990Sstevel@tonic-gate 	    strcmp(n, "signed long long") == 0 ||
2000Sstevel@tonic-gate 	    strcmp(n, "unsigned long long") == 0))
2010Sstevel@tonic-gate 		return (1);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * If the type used for %llx or %llX is not an [unsigned] long long, we
2050Sstevel@tonic-gate 	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
2060Sstevel@tonic-gate 	 * that these typedefs are guaranteed to work with %ll[xX] in either
2070Sstevel@tonic-gate 	 * compilation environment even though they alias to "long" in LP64.
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
2100Sstevel@tonic-gate 		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
2110Sstevel@tonic-gate 		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
2120Sstevel@tonic-gate 			return (1);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		type = ctf_type_reference(ctfp, type);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
220457Sbmc /*ARGSUSED*/
2210Sstevel@tonic-gate static int
pfcheck_type(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)222457Sbmc pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
2250Sstevel@tonic-gate 	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*ARGSUSED*/
2290Sstevel@tonic-gate static int
pfprint_sint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t unormal)2300Sstevel@tonic-gate pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2310Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	int64_t normal = (int64_t)unormal;
2340Sstevel@tonic-gate 	int32_t n = (int32_t)normal;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	switch (size) {
2370Sstevel@tonic-gate 	case sizeof (int8_t):
2380Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2390Sstevel@tonic-gate 		    (int32_t)*((int8_t *)addr) / n));
2400Sstevel@tonic-gate 	case sizeof (int16_t):
2410Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2420Sstevel@tonic-gate 		    (int32_t)*((int16_t *)addr) / n));
2430Sstevel@tonic-gate 	case sizeof (int32_t):
2440Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2450Sstevel@tonic-gate 		    *((int32_t *)addr) / n));
2460Sstevel@tonic-gate 	case sizeof (int64_t):
2470Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2480Sstevel@tonic-gate 		    *((int64_t *)addr) / normal));
2490Sstevel@tonic-gate 	default:
2500Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*ARGSUSED*/
2550Sstevel@tonic-gate static int
pfprint_uint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)2560Sstevel@tonic-gate pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2570Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	uint32_t n = (uint32_t)normal;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	switch (size) {
2620Sstevel@tonic-gate 	case sizeof (uint8_t):
2630Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2640Sstevel@tonic-gate 		    (uint32_t)*((uint8_t *)addr) / n));
2650Sstevel@tonic-gate 	case sizeof (uint16_t):
2660Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2670Sstevel@tonic-gate 		    (uint32_t)*((uint16_t *)addr) / n));
2680Sstevel@tonic-gate 	case sizeof (uint32_t):
2690Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2700Sstevel@tonic-gate 		    *((uint32_t *)addr) / n));
2710Sstevel@tonic-gate 	case sizeof (uint64_t):
2720Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
2730Sstevel@tonic-gate 		    *((uint64_t *)addr) / normal));
2740Sstevel@tonic-gate 	default:
2750Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static int
pfprint_dint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)2800Sstevel@tonic-gate pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2810Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
2840Sstevel@tonic-gate 		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
2850Sstevel@tonic-gate 	else
2860Sstevel@tonic-gate 		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*ARGSUSED*/
2900Sstevel@tonic-gate static int
pfprint_fp(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)2910Sstevel@tonic-gate pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
2920Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	double n = (double)normal;
2950Sstevel@tonic-gate 	long double ldn = (long double)normal;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	switch (size) {
2980Sstevel@tonic-gate 	case sizeof (float):
2990Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3000Sstevel@tonic-gate 		    (double)*((float *)addr) / n));
3010Sstevel@tonic-gate 	case sizeof (double):
3020Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3030Sstevel@tonic-gate 		    *((double *)addr) / n));
3040Sstevel@tonic-gate 	case sizeof (long double):
3050Sstevel@tonic-gate 		return (dt_printf(dtp, fp, format,
3060Sstevel@tonic-gate 		    *((long double *)addr) / ldn));
3070Sstevel@tonic-gate 	default:
3080Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*ARGSUSED*/
3130Sstevel@tonic-gate static int
pfprint_addr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)3140Sstevel@tonic-gate pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3150Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	char *s;
318457Sbmc 	int n, len = 256;
319457Sbmc 	uint64_t val;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	switch (size) {
3220Sstevel@tonic-gate 	case sizeof (uint32_t):
3230Sstevel@tonic-gate 		val = *((uint32_t *)addr);
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 	case sizeof (uint64_t):
3260Sstevel@tonic-gate 		val = *((uint64_t *)addr);
3270Sstevel@tonic-gate 		break;
3280Sstevel@tonic-gate 	default:
3290Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
332457Sbmc 	do {
333457Sbmc 		n = len;
334457Sbmc 		s = alloca(n);
33510791SJonathan.Haslam@Sun.COM 	} while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*ARGSUSED*/
3410Sstevel@tonic-gate static int
pfprint_mod(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)342457Sbmc pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
343457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
344457Sbmc {
345457Sbmc 	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
346457Sbmc }
347457Sbmc 
348457Sbmc /*ARGSUSED*/
349457Sbmc static int
pfprint_umod(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)350457Sbmc pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
351457Sbmc     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
352457Sbmc {
353457Sbmc 	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
354457Sbmc }
355457Sbmc 
356457Sbmc /*ARGSUSED*/
357457Sbmc static int
pfprint_uaddr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)3580Sstevel@tonic-gate pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3590Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
3600Sstevel@tonic-gate {
361457Sbmc 	char *s;
362457Sbmc 	int n, len = 256;
363457Sbmc 	uint64_t val, pid = 0;
364457Sbmc 
3650Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	switch (size) {
3680Sstevel@tonic-gate 	case sizeof (uint32_t):
3690Sstevel@tonic-gate 		val = (u_longlong_t)*((uint32_t *)addr);
3700Sstevel@tonic-gate 		break;
3710Sstevel@tonic-gate 	case sizeof (uint64_t):
3720Sstevel@tonic-gate 		val = (u_longlong_t)*((uint64_t *)addr);
3730Sstevel@tonic-gate 		break;
374457Sbmc 	case sizeof (uint64_t) * 2:
375457Sbmc 		pid = ((uint64_t *)(uintptr_t)addr)[0];
376457Sbmc 		val = ((uint64_t *)(uintptr_t)addr)[1];
377457Sbmc 		break;
3780Sstevel@tonic-gate 	default:
3790Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
382457Sbmc 	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
383457Sbmc 		pid = idp->di_id;
3840Sstevel@tonic-gate 
385457Sbmc 	do {
386457Sbmc 		n = len;
387457Sbmc 		s = alloca(n);
38810791SJonathan.Haslam@Sun.COM 	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
3890Sstevel@tonic-gate 
390457Sbmc 	return (dt_printf(dtp, fp, format, s));
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*ARGSUSED*/
3940Sstevel@tonic-gate static int
pfprint_stack(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * vaddr,size_t size,uint64_t normal)3950Sstevel@tonic-gate pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
3960Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
3970Sstevel@tonic-gate {
398457Sbmc 	int width;
3990Sstevel@tonic-gate 	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
4000Sstevel@tonic-gate 	const dtrace_recdesc_t *rec = pfd->pfd_rec;
4010Sstevel@tonic-gate 	caddr_t addr = (caddr_t)vaddr;
4020Sstevel@tonic-gate 	int err = 0;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/*
4050Sstevel@tonic-gate 	 * We have stashed the value of the STACKINDENT option, and we will
4060Sstevel@tonic-gate 	 * now override it for the purposes of formatting the stack.  If the
4070Sstevel@tonic-gate 	 * field has been specified as left-aligned (i.e. (%-#), we set the
4080Sstevel@tonic-gate 	 * indentation to be the width.  This is a slightly odd semantic, but
4090Sstevel@tonic-gate 	 * it's useful functionality -- and it's slightly odd to begin with to
4100Sstevel@tonic-gate 	 * be using a single format specifier to be formatting multiple lines
4110Sstevel@tonic-gate 	 * of text...
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	if (pfd->pfd_dynwidth < 0) {
4140Sstevel@tonic-gate 		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
4150Sstevel@tonic-gate 		width = -pfd->pfd_dynwidth;
4160Sstevel@tonic-gate 	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
4170Sstevel@tonic-gate 		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
4180Sstevel@tonic-gate 	} else {
4190Sstevel@tonic-gate 		width = 0;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	switch (rec->dtrd_action) {
4250Sstevel@tonic-gate 	case DTRACEACT_USTACK:
4260Sstevel@tonic-gate 	case DTRACEACT_JSTACK:
4270Sstevel@tonic-gate 		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
4280Sstevel@tonic-gate 		break;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	case DTRACEACT_STACK:
431457Sbmc 		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
432457Sbmc 		    rec->dtrd_size / rec->dtrd_arg);
4330Sstevel@tonic-gate 		break;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	default:
4360Sstevel@tonic-gate 		assert(0);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return (err);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate /*ARGSUSED*/
4450Sstevel@tonic-gate static int
pfprint_time(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)4460Sstevel@tonic-gate pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4470Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	char src[32], buf[32], *dst = buf;
4500Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4510Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4520Sstevel@tonic-gate 	int i;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/*
4550Sstevel@tonic-gate 	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
4560Sstevel@tonic-gate 	 * Below, we turn this into the canonical adb/mdb /[yY] format,
4570Sstevel@tonic-gate 	 * "1973 Dec  3 17:20:00".
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	(void) ctime_r(&sec, src, sizeof (src));
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/*
4620Sstevel@tonic-gate 	 * Place the 4-digit year at the head of the string...
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	for (i = 20; i < 24; i++)
4650Sstevel@tonic-gate 		*dst++ = src[i];
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * ...and follow it with the remainder (month, day, hh:mm:ss).
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 	for (i = 3; i < 19; i++)
4710Sstevel@tonic-gate 		*dst++ = src[i];
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	*dst = '\0';
4740Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate  * This prints the time in RFC 822 standard form.  This is useful for emitting
4790Sstevel@tonic-gate  * notions of time that are consumed by standard tools (e.g., as part of an
4800Sstevel@tonic-gate  * RSS feed).
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate /*ARGSUSED*/
4830Sstevel@tonic-gate static int
pfprint_time822(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)4840Sstevel@tonic-gate pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
4850Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	hrtime_t time = *((uint64_t *)addr);
4880Sstevel@tonic-gate 	time_t sec = (time_t)(time / NANOSEC);
4890Sstevel@tonic-gate 	struct tm tm;
4900Sstevel@tonic-gate 	char buf[64];
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	(void) localtime_r(&sec, &tm);
4930Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
4940Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, buf));
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*ARGSUSED*/
4980Sstevel@tonic-gate static int
pfprint_port(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)499*12902SBryan.Cantrill@Sun.COM pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
500*12902SBryan.Cantrill@Sun.COM     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
501*12902SBryan.Cantrill@Sun.COM {
502*12902SBryan.Cantrill@Sun.COM 	uint16_t port = htons(*((uint16_t *)addr));
503*12902SBryan.Cantrill@Sun.COM 	char buf[256];
504*12902SBryan.Cantrill@Sun.COM 	struct servent *sv, res;
505*12902SBryan.Cantrill@Sun.COM 
506*12902SBryan.Cantrill@Sun.COM 	if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
507*12902SBryan.Cantrill@Sun.COM 		return (dt_printf(dtp, fp, format, sv->s_name));
508*12902SBryan.Cantrill@Sun.COM 
509*12902SBryan.Cantrill@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
510*12902SBryan.Cantrill@Sun.COM 	return (dt_printf(dtp, fp, format, buf));
511*12902SBryan.Cantrill@Sun.COM }
512*12902SBryan.Cantrill@Sun.COM 
513*12902SBryan.Cantrill@Sun.COM /*ARGSUSED*/
514*12902SBryan.Cantrill@Sun.COM static int
pfprint_inetaddr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)515*12902SBryan.Cantrill@Sun.COM pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
516*12902SBryan.Cantrill@Sun.COM     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
517*12902SBryan.Cantrill@Sun.COM {
518*12902SBryan.Cantrill@Sun.COM 	char *s = alloca(size + 1);
519*12902SBryan.Cantrill@Sun.COM 	struct hostent *host, res;
520*12902SBryan.Cantrill@Sun.COM 	char inetaddr[NS_IN6ADDRSZ];
521*12902SBryan.Cantrill@Sun.COM 	char buf[1024];
522*12902SBryan.Cantrill@Sun.COM 	int e;
523*12902SBryan.Cantrill@Sun.COM 
524*12902SBryan.Cantrill@Sun.COM 	bcopy(addr, s, size);
525*12902SBryan.Cantrill@Sun.COM 	s[size] = '\0';
526*12902SBryan.Cantrill@Sun.COM 
527*12902SBryan.Cantrill@Sun.COM 	if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
528*12902SBryan.Cantrill@Sun.COM 		if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
529*12902SBryan.Cantrill@Sun.COM 		    AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
530*12902SBryan.Cantrill@Sun.COM 			return (dt_printf(dtp, fp, format, host->h_name));
531*12902SBryan.Cantrill@Sun.COM 	} else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
532*12902SBryan.Cantrill@Sun.COM 		if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
533*12902SBryan.Cantrill@Sun.COM 		    AF_INET6, &e)) != NULL)
534*12902SBryan.Cantrill@Sun.COM 			return (dt_printf(dtp, fp, format, host->h_name));
535*12902SBryan.Cantrill@Sun.COM 	}
536*12902SBryan.Cantrill@Sun.COM 
537*12902SBryan.Cantrill@Sun.COM 	return (dt_printf(dtp, fp, format, s));
538*12902SBryan.Cantrill@Sun.COM }
539*12902SBryan.Cantrill@Sun.COM 
540*12902SBryan.Cantrill@Sun.COM /*ARGSUSED*/
541*12902SBryan.Cantrill@Sun.COM static int
pfprint_cstr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)5420Sstevel@tonic-gate pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5430Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	char *s = alloca(size + 1);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	bcopy(addr, s, size);
5480Sstevel@tonic-gate 	s[size] = '\0';
5490Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, s));
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*ARGSUSED*/
5530Sstevel@tonic-gate static int
pfprint_wstr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)5540Sstevel@tonic-gate pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5550Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	wchar_t *ws = alloca(size + sizeof (wchar_t));
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	bcopy(addr, ws, size);
5600Sstevel@tonic-gate 	ws[size / sizeof (wchar_t)] = L'\0';
5610Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format, ws));
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*ARGSUSED*/
5650Sstevel@tonic-gate static int
pfprint_estr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)5660Sstevel@tonic-gate pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5670Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	char *s;
5700Sstevel@tonic-gate 	int n;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if ((s = strchr2esc(addr, size)) == NULL)
5730Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	n = dt_printf(dtp, fp, format, s);
5760Sstevel@tonic-gate 	free(s);
5770Sstevel@tonic-gate 	return (n);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate static int
pfprint_echr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)5810Sstevel@tonic-gate pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
5820Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	char c;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	switch (size) {
5870Sstevel@tonic-gate 	case sizeof (int8_t):
5880Sstevel@tonic-gate 		c = *(int8_t *)addr;
5890Sstevel@tonic-gate 		break;
5900Sstevel@tonic-gate 	case sizeof (int16_t):
5910Sstevel@tonic-gate 		c = *(int16_t *)addr;
5920Sstevel@tonic-gate 		break;
5930Sstevel@tonic-gate 	case sizeof (int32_t):
5940Sstevel@tonic-gate 		c = *(int32_t *)addr;
5950Sstevel@tonic-gate 		break;
5960Sstevel@tonic-gate 	default:
5970Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*ARGSUSED*/
6040Sstevel@tonic-gate static int
pfprint_pct(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)6050Sstevel@tonic-gate pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
6060Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	return (dt_printf(dtp, fp, "%%"));
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate static const char pfproto_xint[] = "char, short, int, long, or long long";
6120Sstevel@tonic-gate static const char pfproto_csi[] = "char, short, or int";
6130Sstevel@tonic-gate static const char pfproto_fp[] = "float, double, or long double";
6140Sstevel@tonic-gate static const char pfproto_addr[] = "pointer or integer";
615457Sbmc static const char pfproto_uaddr[] =
616457Sbmc 	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
6170Sstevel@tonic-gate static const char pfproto_cstr[] = "char [] or string (or use stringof)";
6180Sstevel@tonic-gate static const char pfproto_wstr[] = "wchar_t []";
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate  * Printf format conversion dictionary.  This table should match the set of
6220Sstevel@tonic-gate  * conversions offered by printf(3C), as well as some additional extensions.
6230Sstevel@tonic-gate  * The second parameter is an ASCII string which is either an actual type
6240Sstevel@tonic-gate  * name we should look up (if pfcheck_type is specified), or just a descriptive
6250Sstevel@tonic-gate  * string of the types expected for use in error messages.
6260Sstevel@tonic-gate  */
6270Sstevel@tonic-gate static const dt_pfconv_t _dtrace_conversions[] = {
628457Sbmc { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
629457Sbmc { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
6300Sstevel@tonic-gate { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
6310Sstevel@tonic-gate { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
6320Sstevel@tonic-gate { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
6330Sstevel@tonic-gate { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
6340Sstevel@tonic-gate { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
6350Sstevel@tonic-gate { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
6360Sstevel@tonic-gate { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
6370Sstevel@tonic-gate { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
6380Sstevel@tonic-gate { "hd", "d", "short", pfcheck_type, pfprint_sint },
6390Sstevel@tonic-gate { "hi", "i", "short", pfcheck_type, pfprint_sint },
6400Sstevel@tonic-gate { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
6410Sstevel@tonic-gate { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
6420Sstevel@tonic-gate { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
6430Sstevel@tonic-gate { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
6440Sstevel@tonic-gate { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
645*12902SBryan.Cantrill@Sun.COM { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
646457Sbmc { "k", "s", "stack", pfcheck_stack, pfprint_stack },
6470Sstevel@tonic-gate { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
6480Sstevel@tonic-gate { "ld",	"d", "long", pfcheck_type, pfprint_sint },
6490Sstevel@tonic-gate { "li",	"i", "long", pfcheck_type, pfprint_sint },
6500Sstevel@tonic-gate { "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
6510Sstevel@tonic-gate { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
6520Sstevel@tonic-gate { "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6530Sstevel@tonic-gate { "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
6540Sstevel@tonic-gate { "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
6550Sstevel@tonic-gate { "lld", "d", "long long", pfcheck_type, pfprint_sint },
6560Sstevel@tonic-gate { "lli", "i", "long long", pfcheck_type, pfprint_sint },
6570Sstevel@tonic-gate { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
6580Sstevel@tonic-gate { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
6590Sstevel@tonic-gate { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
6600Sstevel@tonic-gate { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
6610Sstevel@tonic-gate { "Le",	"e", "long double", pfcheck_type, pfprint_fp },
6620Sstevel@tonic-gate { "LE",	"E", "long double", pfcheck_type, pfprint_fp },
6630Sstevel@tonic-gate { "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
6640Sstevel@tonic-gate { "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
6650Sstevel@tonic-gate { "LG",	"G", "long double", pfcheck_type, pfprint_fp },
6660Sstevel@tonic-gate { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
6670Sstevel@tonic-gate { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
668*12902SBryan.Cantrill@Sun.COM { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
6690Sstevel@tonic-gate { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
6700Sstevel@tonic-gate { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
6711017Sbmc { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
6720Sstevel@tonic-gate { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
6730Sstevel@tonic-gate { "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
6740Sstevel@tonic-gate { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
6750Sstevel@tonic-gate { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
6760Sstevel@tonic-gate { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
6771017Sbmc { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
6780Sstevel@tonic-gate { "%", "%", "void", pfcheck_type, pfprint_pct },
6790Sstevel@tonic-gate { NULL, NULL, NULL, NULL, NULL }
6800Sstevel@tonic-gate };
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate int
dt_pfdict_create(dtrace_hdl_t * dtp)6830Sstevel@tonic-gate dt_pfdict_create(dtrace_hdl_t *dtp)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	uint_t n = _dtrace_strbuckets;
6860Sstevel@tonic-gate 	const dt_pfconv_t *pfd;
6870Sstevel@tonic-gate 	dt_pfdict_t *pdi;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
6900Sstevel@tonic-gate 	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
6910Sstevel@tonic-gate 		free(pdi);
6920Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	dtp->dt_pfdict = pdi;
6960Sstevel@tonic-gate 	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
6970Sstevel@tonic-gate 	pdi->pdi_nbuckets = n;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
7000Sstevel@tonic-gate 		dtrace_typeinfo_t dtt;
7010Sstevel@tonic-gate 		dt_pfconv_t *pfc;
7020Sstevel@tonic-gate 		uint_t h;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
7050Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
7060Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
7100Sstevel@tonic-gate 		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
7110Sstevel@tonic-gate 		pfc->pfc_next = pdi->pdi_buckets[h];
7120Sstevel@tonic-gate 		pdi->pdi_buckets[h] = pfc;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		dtt.dtt_ctfp = NULL;
7150Sstevel@tonic-gate 		dtt.dtt_type = CTF_ERR;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		/*
7180Sstevel@tonic-gate 		 * The "D" container or its parent must contain a definition of
7190Sstevel@tonic-gate 		 * any type referenced by a printf conversion.  If none can be
7200Sstevel@tonic-gate 		 * found, we fail to initialize the printf dictionary.
7210Sstevel@tonic-gate 		 */
7220Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
7230Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
7240Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
7250Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOCONV));
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		pfc->pfc_dctfp = dtt.dtt_ctfp;
7290Sstevel@tonic-gate 		pfc->pfc_dtype = dtt.dtt_type;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * The "C" container may contain an alternate definition of an
7330Sstevel@tonic-gate 		 * explicit conversion type.  If it does, use it; otherwise
7340Sstevel@tonic-gate 		 * just set pfc_ctype to pfc_dtype so it is always valid.
7350Sstevel@tonic-gate 		 */
7360Sstevel@tonic-gate 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
7370Sstevel@tonic-gate 		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
7380Sstevel@tonic-gate 			pfc->pfc_cctfp = dtt.dtt_ctfp;
7390Sstevel@tonic-gate 			pfc->pfc_ctype = dtt.dtt_type;
7400Sstevel@tonic-gate 		} else {
7410Sstevel@tonic-gate 			pfc->pfc_cctfp = pfc->pfc_dctfp;
7420Sstevel@tonic-gate 			pfc->pfc_ctype = pfc->pfc_dtype;
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
7460Sstevel@tonic-gate 		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
7470Sstevel@tonic-gate 			dt_pfdict_destroy(dtp);
7480Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADCONV));
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	return (0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate void
dt_pfdict_destroy(dtrace_hdl_t * dtp)7580Sstevel@tonic-gate dt_pfdict_destroy(dtrace_hdl_t *dtp)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7610Sstevel@tonic-gate 	dt_pfconv_t *pfc, *nfc;
7620Sstevel@tonic-gate 	uint_t i;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if (pdi == NULL)
7650Sstevel@tonic-gate 		return;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	for (i = 0; i < pdi->pdi_nbuckets; i++) {
7680Sstevel@tonic-gate 		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
7690Sstevel@tonic-gate 			nfc = pfc->pfc_next;
7700Sstevel@tonic-gate 			free(pfc);
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	free(pdi->pdi_buckets);
7750Sstevel@tonic-gate 	free(pdi);
7760Sstevel@tonic-gate 	dtp->dt_pfdict = NULL;
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate static const dt_pfconv_t *
dt_pfdict_lookup(dtrace_hdl_t * dtp,const char * name)7800Sstevel@tonic-gate dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 	dt_pfdict_t *pdi = dtp->dt_pfdict;
7830Sstevel@tonic-gate 	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
7840Sstevel@tonic-gate 	const dt_pfconv_t *pfc;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
7870Sstevel@tonic-gate 		if (strcmp(pfc->pfc_name, name) == 0)
7880Sstevel@tonic-gate 			break;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	return (pfc);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate static dt_pfargv_t *
dt_printf_error(dtrace_hdl_t * dtp,int err)7950Sstevel@tonic-gate dt_printf_error(dtrace_hdl_t *dtp, int err)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	if (yypcb != NULL)
7980Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, err);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	(void) dt_set_errno(dtp, err);
8010Sstevel@tonic-gate 	return (NULL);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate dt_pfargv_t *
dt_printf_create(dtrace_hdl_t * dtp,const char * s)8050Sstevel@tonic-gate dt_printf_create(dtrace_hdl_t *dtp, const char *s)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd = NULL;
8080Sstevel@tonic-gate 	dt_pfargv_t *pfv;
8090Sstevel@tonic-gate 	const char *p, *q;
8100Sstevel@tonic-gate 	char *format;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
8130Sstevel@tonic-gate 	    (format = strdup(s)) == NULL) {
8140Sstevel@tonic-gate 		free(pfv);
8150Sstevel@tonic-gate 		return (dt_printf_error(dtp, EDT_NOMEM));
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	pfv->pfv_format = format;
8190Sstevel@tonic-gate 	pfv->pfv_argv = NULL;
8200Sstevel@tonic-gate 	pfv->pfv_argc = 0;
8210Sstevel@tonic-gate 	pfv->pfv_flags = 0;
822457Sbmc 	pfv->pfv_dtp = dtp;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
8250Sstevel@tonic-gate 		uint_t namelen = 0;
8260Sstevel@tonic-gate 		int digits = 0;
8270Sstevel@tonic-gate 		int dot = 0;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		char name[8];
8300Sstevel@tonic-gate 		char c;
8310Sstevel@tonic-gate 		int n;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
8340Sstevel@tonic-gate 			dt_printf_destroy(pfv);
8350Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
8390Sstevel@tonic-gate 			nfd->pfd_next = pfd;
8400Sstevel@tonic-gate 		else
8410Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
8440Sstevel@tonic-gate 		pfv->pfv_argc++;
8450Sstevel@tonic-gate 		nfd = pfd;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 		if (p > q) {
8480Sstevel@tonic-gate 			pfd->pfd_preflen = (size_t)(p - q);
8490Sstevel@tonic-gate 			pfd->pfd_prefix = q;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		fmt_switch:
8530Sstevel@tonic-gate 		switch (c = *++p) {
8540Sstevel@tonic-gate 		case '0': case '1': case '2': case '3': case '4':
8550Sstevel@tonic-gate 		case '5': case '6': case '7': case '8': case '9':
8560Sstevel@tonic-gate 			if (dot == 0 && digits == 0 && c == '0') {
8570Sstevel@tonic-gate 				pfd->pfd_flags |= DT_PFCONV_ZPAD;
8580Sstevel@tonic-gate 				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
8590Sstevel@tonic-gate 				goto fmt_switch;
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 			for (n = 0; isdigit(c); c = *++p)
8630Sstevel@tonic-gate 				n = n * 10 + c - '0';
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 			if (dot)
8660Sstevel@tonic-gate 				pfd->pfd_prec = n;
8670Sstevel@tonic-gate 			else
8680Sstevel@tonic-gate 				pfd->pfd_width = n;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 			p--;
8710Sstevel@tonic-gate 			digits++;
8720Sstevel@tonic-gate 			goto fmt_switch;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		case '#':
8750Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_ALT;
8760Sstevel@tonic-gate 			goto fmt_switch;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		case '*':
8790Sstevel@tonic-gate 			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 			if (pfd->pfd_flags & n) {
8820Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
8830Sstevel@tonic-gate 				    "one '*' specified for the output %s\n",
8840Sstevel@tonic-gate 				    pfv->pfv_argc, n ? "precision" : "width");
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 				dt_printf_destroy(pfv);
8870Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
8880Sstevel@tonic-gate 			}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 			pfd->pfd_flags |= n;
8910Sstevel@tonic-gate 			goto fmt_switch;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		case '+':
8940Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPOS;
8950Sstevel@tonic-gate 			goto fmt_switch;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		case '-':
8980Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_LEFT;
8990Sstevel@tonic-gate 			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
9000Sstevel@tonic-gate 			goto fmt_switch;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		case '.':
9030Sstevel@tonic-gate 			if (dot++ != 0) {
9040Sstevel@tonic-gate 				yywarn("format conversion #%u has more than "
9050Sstevel@tonic-gate 				    "one '.' specified\n", pfv->pfv_argc);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 				dt_printf_destroy(pfv);
9080Sstevel@tonic-gate 				return (dt_printf_error(dtp, EDT_COMPILER));
9090Sstevel@tonic-gate 			}
9100Sstevel@tonic-gate 			digits = 0;
9110Sstevel@tonic-gate 			goto fmt_switch;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		case '?':
9140Sstevel@tonic-gate 			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
9150Sstevel@tonic-gate 				pfd->pfd_width = 16;
9160Sstevel@tonic-gate 			else
9170Sstevel@tonic-gate 				pfd->pfd_width = 8;
9180Sstevel@tonic-gate 			goto fmt_switch;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		case '@':
9210Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_AGG;
9220Sstevel@tonic-gate 			goto fmt_switch;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		case '\'':
9250Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_GROUP;
9260Sstevel@tonic-gate 			goto fmt_switch;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		case ' ':
9290Sstevel@tonic-gate 			pfd->pfd_flags |= DT_PFCONV_SPACE;
9300Sstevel@tonic-gate 			goto fmt_switch;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		case '$':
9330Sstevel@tonic-gate 			yywarn("format conversion #%u uses unsupported "
9340Sstevel@tonic-gate 			    "positional format (%%n$)\n", pfv->pfv_argc);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9370Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		case '%':
9400Sstevel@tonic-gate 			if (p[-1] == '%')
9410Sstevel@tonic-gate 				goto default_lbl; /* if %% then use "%" conv */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 			yywarn("format conversion #%u cannot be combined "
9440Sstevel@tonic-gate 			    "with other format flags: %%%%\n", pfv->pfv_argc);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9470Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 		case '\0':
9500Sstevel@tonic-gate 			yywarn("format conversion #%u name expected before "
9510Sstevel@tonic-gate 			    "end of format string\n", pfv->pfv_argc);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9540Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		case 'h':
9570Sstevel@tonic-gate 		case 'l':
9580Sstevel@tonic-gate 		case 'L':
9590Sstevel@tonic-gate 		case 'w':
9600Sstevel@tonic-gate 			if (namelen < sizeof (name) - 2)
9610Sstevel@tonic-gate 				name[namelen++] = c;
9620Sstevel@tonic-gate 			goto fmt_switch;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 		default_lbl:
9650Sstevel@tonic-gate 		default:
9660Sstevel@tonic-gate 			name[namelen++] = c;
9670Sstevel@tonic-gate 			name[namelen] = '\0';
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		if (pfd->pfd_conv == NULL) {
9730Sstevel@tonic-gate 			yywarn("format conversion #%u is undefined: %%%s\n",
9740Sstevel@tonic-gate 			    pfv->pfv_argc, name);
9750Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9760Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_COMPILER));
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (*q != '\0' || *format == '\0') {
9810Sstevel@tonic-gate 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
9820Sstevel@tonic-gate 			dt_printf_destroy(pfv);
9830Sstevel@tonic-gate 			return (dt_printf_error(dtp, EDT_NOMEM));
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		if (pfv->pfv_argv != NULL)
9870Sstevel@tonic-gate 			nfd->pfd_next = pfd;
9880Sstevel@tonic-gate 		else
9890Sstevel@tonic-gate 			pfv->pfv_argv = pfd;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		bzero(pfd, sizeof (dt_pfargd_t));
9920Sstevel@tonic-gate 		pfv->pfv_argc++;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		pfd->pfd_prefix = q;
9950Sstevel@tonic-gate 		pfd->pfd_preflen = strlen(q);
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	return (pfv);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate void
dt_printf_destroy(dt_pfargv_t * pfv)10020Sstevel@tonic-gate dt_printf_destroy(dt_pfargv_t *pfv)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	dt_pfargd_t *pfd, *nfd;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
10070Sstevel@tonic-gate 		nfd = pfd->pfd_next;
10080Sstevel@tonic-gate 		free(pfd);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	free(pfv->pfv_format);
10120Sstevel@tonic-gate 	free(pfv);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate void
dt_printf_validate(dt_pfargv_t * pfv,uint_t flags,dt_ident_t * idp,int foff,dtrace_actkind_t kind,dt_node_t * dnp)10160Sstevel@tonic-gate dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
10170Sstevel@tonic-gate     dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
10200Sstevel@tonic-gate 	const char *func = idp->di_name;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	char n[DT_TYPE_NAMELEN];
10230Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
10240Sstevel@tonic-gate 	const char *aggtype;
10250Sstevel@tonic-gate 	dt_node_t aggnode;
10260Sstevel@tonic-gate 	int i, j;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (pfv->pfv_format[0] == '\0') {
10290Sstevel@tonic-gate 		xyerror(D_PRINTF_FMT_EMPTY,
10300Sstevel@tonic-gate 		    "%s( ) format string is empty\n", func);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
1033457Sbmc 	pfv->pfv_flags = flags;
1034457Sbmc 
10350Sstevel@tonic-gate 	/*
10360Sstevel@tonic-gate 	 * We fake up a parse node representing the type that can be used with
1037491Sbmc 	 * an aggregation result conversion, which -- for all but count() --
1038491Sbmc 	 * is a signed quantity.
10390Sstevel@tonic-gate 	 */
1040491Sbmc 	if (kind != DTRACEAGG_COUNT)
10410Sstevel@tonic-gate 		aggtype = "int64_t";
10420Sstevel@tonic-gate 	else
10430Sstevel@tonic-gate 		aggtype = "uint64_t";
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	if (dt_type_lookup(aggtype, &dtt) != 0)
10460Sstevel@tonic-gate 		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	bzero(&aggnode, sizeof (aggnode));
10490Sstevel@tonic-gate 	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
10520Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
10530Sstevel@tonic-gate 		const char *dyns[2];
10540Sstevel@tonic-gate 		int dync = 0;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 		char vname[64];
10570Sstevel@tonic-gate 		dt_node_t *vnp;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 		if (pfc == NULL)
10600Sstevel@tonic-gate 			continue; /* no checking if argd is just a prefix */
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
10630Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
10640Sstevel@tonic-gate 			continue;
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
10680Sstevel@tonic-gate 			dyns[dync++] = ".*";
10690Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
10700Sstevel@tonic-gate 			dyns[dync++] = "*";
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 		for (; dync != 0; dync--) {
10730Sstevel@tonic-gate 			if (dnp == NULL) {
10740Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_PROTO,
10750Sstevel@tonic-gate 				    "%s( ) prototype mismatch: conversion "
10760Sstevel@tonic-gate 				    "#%d (%%%s) is missing a corresponding "
10770Sstevel@tonic-gate 				    "\"%s\" argument\n", func, i + 1,
10780Sstevel@tonic-gate 				    pfc->pfc_name, dyns[dync - 1]);
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 			if (dt_node_is_integer(dnp) == 0) {
10820Sstevel@tonic-gate 				xyerror(D_PRINTF_DYN_TYPE,
10830Sstevel@tonic-gate 				    "%s( ) argument #%d is incompatible "
10840Sstevel@tonic-gate 				    "with conversion #%d prototype:\n"
10850Sstevel@tonic-gate 				    "\tconversion: %% %s %s\n"
10860Sstevel@tonic-gate 				    "\t prototype: int\n\t  argument: %s\n",
10870Sstevel@tonic-gate 				    func, j + foff + 1, i + 1,
10880Sstevel@tonic-gate 				    dyns[dync - 1], pfc->pfc_name,
10890Sstevel@tonic-gate 				    dt_node_type_name(dnp, n, sizeof (n)));
10900Sstevel@tonic-gate 			}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 			dnp = dnp->dn_list;
10930Sstevel@tonic-gate 			j++;
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		/*
10970Sstevel@tonic-gate 		 * If this conversion is consuming the aggregation data, set
10980Sstevel@tonic-gate 		 * the value node pointer (vnp) to a fake node based on the
10990Sstevel@tonic-gate 		 * aggregating function result type.  Otherwise assign vnp to
11000Sstevel@tonic-gate 		 * the next parse node in the argument list, if there is one.
11010Sstevel@tonic-gate 		 */
11020Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
11030Sstevel@tonic-gate 			if (!(flags & DT_PRINTF_AGGREGATION)) {
11040Sstevel@tonic-gate 				xyerror(D_PRINTF_AGG_CONV,
11050Sstevel@tonic-gate 				    "%%@ conversion requires an aggregation"
11060Sstevel@tonic-gate 				    " and is not for use with %s( )\n", func);
11070Sstevel@tonic-gate 			}
11080Sstevel@tonic-gate 			(void) strlcpy(vname, "aggregating action",
11090Sstevel@tonic-gate 			    sizeof (vname));
11100Sstevel@tonic-gate 			vnp = &aggnode;
11110Sstevel@tonic-gate 		} else if (dnp == NULL) {
11120Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_PROTO,
11130Sstevel@tonic-gate 			    "%s( ) prototype mismatch: conversion #%d (%%"
11140Sstevel@tonic-gate 			    "%s) is missing a corresponding value argument\n",
11150Sstevel@tonic-gate 			    func, i + 1, pfc->pfc_name);
11160Sstevel@tonic-gate 		} else {
11170Sstevel@tonic-gate 			(void) snprintf(vname, sizeof (vname),
11180Sstevel@tonic-gate 			    "argument #%d", j + foff + 1);
11190Sstevel@tonic-gate 			vnp = dnp;
11200Sstevel@tonic-gate 			dnp = dnp->dn_list;
11210Sstevel@tonic-gate 			j++;
11220Sstevel@tonic-gate 		}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		/*
11250Sstevel@tonic-gate 		 * Fill in the proposed final format string by prepending any
11260Sstevel@tonic-gate 		 * size-related prefixes to the pfconv's format string.  The
11270Sstevel@tonic-gate 		 * pfc_check() function below may optionally modify the format
11280Sstevel@tonic-gate 		 * as part of validating the type of the input argument.
11290Sstevel@tonic-gate 		 */
11300Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_sint ||
11310Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_uint ||
11320Sstevel@tonic-gate 		    pfc->pfc_print == &pfprint_dint) {
11330Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (uint64_t))
11340Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "ll");
11350Sstevel@tonic-gate 		} else if (pfc->pfc_print == &pfprint_fp) {
11360Sstevel@tonic-gate 			if (dt_node_type_size(vnp) == sizeof (long double))
11370Sstevel@tonic-gate 				(void) strcpy(pfd->pfd_fmt, "L");
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		/*
11430Sstevel@tonic-gate 		 * Validate the format conversion against the value node type.
11440Sstevel@tonic-gate 		 * If the conversion is good, create the descriptor format
11450Sstevel@tonic-gate 		 * string by concatenating together any required printf(3C)
11460Sstevel@tonic-gate 		 * size prefixes with the conversion's native format string.
11470Sstevel@tonic-gate 		 */
1148457Sbmc 		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
11490Sstevel@tonic-gate 			xyerror(D_PRINTF_ARG_TYPE,
11500Sstevel@tonic-gate 			    "%s( ) %s is incompatible with "
11510Sstevel@tonic-gate 			    "conversion #%d prototype:\n\tconversion: %%%s\n"
11520Sstevel@tonic-gate 			    "\t prototype: %s\n\t  argument: %s\n", func,
11530Sstevel@tonic-gate 			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
11540Sstevel@tonic-gate 			    dt_node_type_name(vnp, n, sizeof (n)));
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
11590Sstevel@tonic-gate 		xyerror(D_PRINTF_ARG_EXTRA,
11600Sstevel@tonic-gate 		    "%s( ) prototype mismatch: only %d arguments "
11610Sstevel@tonic-gate 		    "required by this format string\n", func, j);
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11651017Sbmc void
dt_printa_validate(dt_node_t * lhs,dt_node_t * rhs)11661017Sbmc dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
11671017Sbmc {
11681017Sbmc 	dt_ident_t *lid, *rid;
11691017Sbmc 	dt_node_t *lproto, *rproto;
11701017Sbmc 	int largc, rargc, argn;
11711017Sbmc 	char n1[DT_TYPE_NAMELEN];
11721017Sbmc 	char n2[DT_TYPE_NAMELEN];
11731017Sbmc 
11741017Sbmc 	assert(lhs->dn_kind == DT_NODE_AGG);
11751017Sbmc 	assert(rhs->dn_kind == DT_NODE_AGG);
11761017Sbmc 
11771017Sbmc 	lid = lhs->dn_ident;
11781017Sbmc 	rid = rhs->dn_ident;
11791017Sbmc 
11801017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
11811017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
11821017Sbmc 
11831017Sbmc 	/*
11841017Sbmc 	 * First, get an argument count on each side.  These must match.
11851017Sbmc 	 */
11861017Sbmc 	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
11871017Sbmc 		largc++;
11881017Sbmc 
11891017Sbmc 	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
11901017Sbmc 		rargc++;
11911017Sbmc 
11921017Sbmc 	if (largc != rargc) {
11931017Sbmc 		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
11941017Sbmc 		    "matching key signatures: @%s has %d key%s, @%s has %d "
11951017Sbmc 		    "key%s", lid->di_name, rid->di_name,
11961017Sbmc 		    lid->di_name, largc, largc == 1 ? "" : "s",
11971017Sbmc 		    rid->di_name, rargc, rargc == 1 ? "" : "s");
11981017Sbmc 	}
11991017Sbmc 
12001017Sbmc 	/*
12011017Sbmc 	 * Now iterate over the keys to verify that each type matches.
12021017Sbmc 	 */
12031017Sbmc 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
12041017Sbmc 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
12051017Sbmc 
12061017Sbmc 	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
12071017Sbmc 	    rproto = rproto->dn_list) {
12081017Sbmc 		assert(rproto != NULL);
12091017Sbmc 
12101017Sbmc 		if (dt_node_is_argcompat(lproto, rproto))
12111017Sbmc 			continue;
12121017Sbmc 
12131017Sbmc 		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
12141017Sbmc 		    "incompatible with @%s:\n%9s key #%d: %s\n"
12151017Sbmc 		    "%9s key #%d: %s\n",
12161017Sbmc 		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
12171017Sbmc 		    dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
12181017Sbmc 		    argn, dt_node_type_name(rproto, n2, sizeof (n2)));
12191017Sbmc 	}
12201017Sbmc }
12211017Sbmc 
12220Sstevel@tonic-gate static int
dt_printf_getint(dtrace_hdl_t * dtp,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len,int * ip)12230Sstevel@tonic-gate dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
12240Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len, int *ip)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	uintptr_t addr;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	if (nrecs == 0)
12290Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	addr = (uintptr_t)buf + recp->dtrd_offset;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (addr + sizeof (int) > (uintptr_t)buf + len)
12340Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DOFFSET));
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (addr & (recp->dtrd_alignment - 1))
12370Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DALIGN));
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	switch (recp->dtrd_size) {
12400Sstevel@tonic-gate 	case sizeof (int8_t):
12410Sstevel@tonic-gate 		*ip = (int)*((int8_t *)addr);
12420Sstevel@tonic-gate 		break;
12430Sstevel@tonic-gate 	case sizeof (int16_t):
12440Sstevel@tonic-gate 		*ip = (int)*((int16_t *)addr);
12450Sstevel@tonic-gate 		break;
12460Sstevel@tonic-gate 	case sizeof (int32_t):
12470Sstevel@tonic-gate 		*ip = (int)*((int32_t *)addr);
12480Sstevel@tonic-gate 		break;
12490Sstevel@tonic-gate 	case sizeof (int64_t):
12500Sstevel@tonic-gate 		*ip = (int)*((int64_t *)addr);
12510Sstevel@tonic-gate 		break;
12520Sstevel@tonic-gate 	default:
12530Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	return (0);
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate /*ARGSUSED*/
12600Sstevel@tonic-gate static int
pfprint_average(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)12610Sstevel@tonic-gate pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12620Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate 	const uint64_t *data = addr;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (size != sizeof (uint64_t) * 2)
12670Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	return (dt_printf(dtp, fp, format,
12700Sstevel@tonic-gate 	    data[0] ? data[1] / normal / data[0] : 0));
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate /*ARGSUSED*/
12740Sstevel@tonic-gate static int
pfprint_stddev(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)12759531Srafael.vanoni@sun.com pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12769531Srafael.vanoni@sun.com     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12779531Srafael.vanoni@sun.com {
12789531Srafael.vanoni@sun.com 	const uint64_t *data = addr;
12799531Srafael.vanoni@sun.com 
12809531Srafael.vanoni@sun.com 	if (size != sizeof (uint64_t) * 4)
12819531Srafael.vanoni@sun.com 		return (dt_set_errno(dtp, EDT_DMISMATCH));
12829531Srafael.vanoni@sun.com 
12839531Srafael.vanoni@sun.com 	return (dt_printf(dtp, fp, format,
12849531Srafael.vanoni@sun.com 	    dt_stddev((uint64_t *)data, normal)));
12859531Srafael.vanoni@sun.com }
12869531Srafael.vanoni@sun.com 
12879531Srafael.vanoni@sun.com /*ARGSUSED*/
12889531Srafael.vanoni@sun.com static int
pfprint_quantize(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)12890Sstevel@tonic-gate pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12900Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	return (dt_print_quantize(dtp, fp, addr, size, normal));
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate /*ARGSUSED*/
12960Sstevel@tonic-gate static int
pfprint_lquantize(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)12970Sstevel@tonic-gate pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
12980Sstevel@tonic-gate     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	return (dt_print_lquantize(dtp, fp, addr, size, normal));
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate static int
dt_printf_format(dtrace_hdl_t * dtp,FILE * fp,const dt_pfargv_t * pfv,const dtrace_recdesc_t * recs,uint_t nrecs,const void * buf,size_t len,const dtrace_aggdata_t ** aggsdata,int naggvars)13040Sstevel@tonic-gate dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
13050Sstevel@tonic-gate     const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
13061017Sbmc     size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
13070Sstevel@tonic-gate {
13080Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
13090Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = recs;
13101017Sbmc 	const dtrace_aggdata_t *aggdata;
13111017Sbmc 	dtrace_aggdesc_t *agg;
13121017Sbmc 	caddr_t lim = (caddr_t)buf + len, limit;
13130Sstevel@tonic-gate 	char format[64] = "%";
13141017Sbmc 	int i, aggrec, curagg = -1;
13151017Sbmc 	uint64_t normal;
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/*
13181017Sbmc 	 * If we are formatting an aggregation, set 'aggrec' to the index of
13191017Sbmc 	 * the final record description (the aggregation result) so we can use
13201017Sbmc 	 * this record index with any conversion where DT_PFCONV_AGG is set.
13211017Sbmc 	 * (The actual aggregation used will vary as we increment through the
13221017Sbmc 	 * aggregation variables that we have been passed.)  Finally, we
13231017Sbmc 	 * decrement nrecs to prevent this record from being used with any
13241017Sbmc 	 * other conversion.
13250Sstevel@tonic-gate 	 */
13260Sstevel@tonic-gate 	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
13271017Sbmc 		assert(aggsdata != NULL);
13281017Sbmc 		assert(naggvars > 0);
13291017Sbmc 
13300Sstevel@tonic-gate 		if (nrecs == 0)
13310Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DMISMATCH));
13321017Sbmc 
13331017Sbmc 		curagg = naggvars > 1 ? 1 : 0;
13341017Sbmc 		aggdata = aggsdata[0];
13351017Sbmc 		aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
13360Sstevel@tonic-gate 		nrecs--;
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
13400Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
13410Sstevel@tonic-gate 		int width = pfd->pfd_width;
13420Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
13430Sstevel@tonic-gate 		int rval;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		char *f = format + 1; /* skip initial '%' */
13460Sstevel@tonic-gate 		const dtrace_recdesc_t *rec;
13470Sstevel@tonic-gate 		dt_pfprint_f *func;
13481017Sbmc 		caddr_t addr;
13490Sstevel@tonic-gate 		size_t size;
13501017Sbmc 		uint32_t flags;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
13530Sstevel@tonic-gate 			char *tmp = alloca(pfd->pfd_preflen + 1);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
13560Sstevel@tonic-gate 			tmp[pfd->pfd_preflen] = '\0';
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
13590Sstevel@tonic-gate 				return (rval);
1360457Sbmc 
1361457Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1362457Sbmc 				/*
1363457Sbmc 				 * For printa(), we flush the buffer after each
13641017Sbmc 				 * prefix, setting the flags to indicate that
13651017Sbmc 				 * this is part of the printa() format string.
1366457Sbmc 				 */
13671017Sbmc 				flags = DTRACE_BUFDATA_AGGFORMAT;
13681017Sbmc 
13691017Sbmc 				if (pfc == NULL && i == pfv->pfv_argc - 1)
13701017Sbmc 					flags |= DTRACE_BUFDATA_AGGLAST;
13711017Sbmc 
13721017Sbmc 				if (dt_buffered_flush(dtp, NULL, NULL,
13731017Sbmc 				    aggdata, flags) < 0)
1374457Sbmc 					return (-1);
1375457Sbmc 			}
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 		if (pfc == NULL) {
13790Sstevel@tonic-gate 			if (pfv->pfv_argc == 1)
13800Sstevel@tonic-gate 				return (nrecs != 0);
13810Sstevel@tonic-gate 			continue;
13820Sstevel@tonic-gate 		}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		/*
13850Sstevel@tonic-gate 		 * If the conversion is %%, just invoke the print callback
13860Sstevel@tonic-gate 		 * with no data record and continue; it consumes no record.
13870Sstevel@tonic-gate 		 */
13880Sstevel@tonic-gate 		if (pfc->pfc_print == &pfprint_pct) {
13890Sstevel@tonic-gate 			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
13900Sstevel@tonic-gate 				continue;
13910Sstevel@tonic-gate 			return (-1); /* errno is set for us */
13920Sstevel@tonic-gate 		}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
13950Sstevel@tonic-gate 			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
13960Sstevel@tonic-gate 			    len, &width) == -1)
13970Sstevel@tonic-gate 				return (-1); /* errno is set for us */
13980Sstevel@tonic-gate 			pfd->pfd_dynwidth = width;
13990Sstevel@tonic-gate 		} else {
14000Sstevel@tonic-gate 			pfd->pfd_dynwidth = 0;
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
14040Sstevel@tonic-gate 		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
14050Sstevel@tonic-gate 			return (-1); /* errno is set for us */
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
14081017Sbmc 			/*
14091017Sbmc 			 * This should be impossible -- the compiler shouldn't
14101017Sbmc 			 * create a DT_PFCONV_AGG conversion without an
14111017Sbmc 			 * aggregation present.  Still, we'd rather fail
14121017Sbmc 			 * gracefully than blow up...
14131017Sbmc 			 */
14141017Sbmc 			if (aggsdata == NULL)
14150Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
14161017Sbmc 
14171017Sbmc 			aggdata = aggsdata[curagg];
14181017Sbmc 			agg = aggdata->dtada_desc;
14191017Sbmc 
14201017Sbmc 			/*
14211017Sbmc 			 * We increment the current aggregation variable, but
14221017Sbmc 			 * not beyond the number of aggregation variables that
14231017Sbmc 			 * we're printing. This has the (desired) effect that
14241017Sbmc 			 * DT_PFCONV_AGG conversions beyond the number of
14251017Sbmc 			 * aggregation variables (re-)convert the aggregation
14261017Sbmc 			 * value of the last aggregation variable.
14271017Sbmc 			 */
14281017Sbmc 			if (curagg < naggvars - 1)
14291017Sbmc 				curagg++;
14301017Sbmc 
14311017Sbmc 			rec = &agg->dtagd_rec[aggrec];
14321017Sbmc 			addr = aggdata->dtada_data + rec->dtrd_offset;
14331017Sbmc 			limit = addr + aggdata->dtada_size;
14341017Sbmc 			normal = aggdata->dtada_normal;
14351017Sbmc 			flags = DTRACE_BUFDATA_AGGVAL;
14360Sstevel@tonic-gate 		} else {
14370Sstevel@tonic-gate 			if (nrecs == 0)
14380Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_DMISMATCH));
14391028Sbmc 
14401028Sbmc 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
14411028Sbmc 				/*
14421028Sbmc 				 * When printing aggregation keys, we always
14431028Sbmc 				 * set the aggdata to be the representative
14441028Sbmc 				 * (zeroth) aggregation.  The aggdata isn't
14451028Sbmc 				 * actually used here in this case, but it is
14461028Sbmc 				 * passed to the buffer handler and must
14471028Sbmc 				 * therefore still be correct.
14481028Sbmc 				 */
14491028Sbmc 				aggdata = aggsdata[0];
14501028Sbmc 				flags = DTRACE_BUFDATA_AGGKEY;
14511028Sbmc 			}
14521028Sbmc 
14530Sstevel@tonic-gate 			rec = recp++;
14540Sstevel@tonic-gate 			nrecs--;
14551017Sbmc 			addr = (caddr_t)buf + rec->dtrd_offset;
14561017Sbmc 			limit = lim;
14571017Sbmc 			normal = 1;
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 		size = rec->dtrd_size;
14610Sstevel@tonic-gate 
14621017Sbmc 		if (addr + size > limit) {
14630Sstevel@tonic-gate 			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
14640Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, (void *)lim);
14650Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DOFFSET));
14660Sstevel@tonic-gate 		}
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 		if (rec->dtrd_alignment != 0 &&
14690Sstevel@tonic-gate 		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
14700Sstevel@tonic-gate 			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
14710Sstevel@tonic-gate 			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
14720Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DALIGN));
14730Sstevel@tonic-gate 		}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		switch (rec->dtrd_action) {
14760Sstevel@tonic-gate 		case DTRACEAGG_AVG:
14770Sstevel@tonic-gate 			func = pfprint_average;
14780Sstevel@tonic-gate 			break;
14799531Srafael.vanoni@sun.com 		case DTRACEAGG_STDDEV:
14809531Srafael.vanoni@sun.com 			func = pfprint_stddev;
14819531Srafael.vanoni@sun.com 			break;
14820Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
14830Sstevel@tonic-gate 			func = pfprint_quantize;
14840Sstevel@tonic-gate 			break;
14850Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
14860Sstevel@tonic-gate 			func = pfprint_lquantize;
14870Sstevel@tonic-gate 			break;
1488457Sbmc 		case DTRACEACT_MOD:
1489457Sbmc 			func = pfprint_mod;
1490457Sbmc 			break;
1491457Sbmc 		case DTRACEACT_UMOD:
1492457Sbmc 			func = pfprint_umod;
1493457Sbmc 			break;
14940Sstevel@tonic-gate 		default:
14950Sstevel@tonic-gate 			func = pfc->pfc_print;
14960Sstevel@tonic-gate 			break;
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
15000Sstevel@tonic-gate 			*f++ = '#';
15010Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
15020Sstevel@tonic-gate 			*f++ = '0';
15031222Smws 		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
15040Sstevel@tonic-gate 			*f++ = '-';
15050Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
15060Sstevel@tonic-gate 			*f++ = '+';
15070Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
15080Sstevel@tonic-gate 			*f++ = '\'';
15090Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
15100Sstevel@tonic-gate 			*f++ = ' ';
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		/*
15130Sstevel@tonic-gate 		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
15140Sstevel@tonic-gate 		 * don't add the width to the format string.  See the block
15150Sstevel@tonic-gate 		 * comment in pfprint_stack() for a description of the
15160Sstevel@tonic-gate 		 * behavior in this case.
15170Sstevel@tonic-gate 		 */
15180Sstevel@tonic-gate 		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
15190Sstevel@tonic-gate 			width = 0;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 		if (width != 0)
15221222Smws 			f += snprintf(f, sizeof (format), "%d", ABS(width));
15230Sstevel@tonic-gate 
15241222Smws 		if (prec > 0)
15250Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 		(void) strcpy(f, pfd->pfd_fmt);
15280Sstevel@tonic-gate 		pfd->pfd_rec = rec;
15290Sstevel@tonic-gate 
15301017Sbmc 		if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
15310Sstevel@tonic-gate 			return (-1); /* errno is set for us */
1532457Sbmc 
1533457Sbmc 		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1534457Sbmc 			/*
1535457Sbmc 			 * For printa(), we flush the buffer after each tuple
15361017Sbmc 			 * element, inidicating that this is the last record
15371017Sbmc 			 * as appropriate.
1538457Sbmc 			 */
15391017Sbmc 			if (i == pfv->pfv_argc - 1)
15401017Sbmc 				flags |= DTRACE_BUFDATA_AGGLAST;
15411017Sbmc 
15421017Sbmc 			if (dt_buffered_flush(dtp, NULL,
15431017Sbmc 			    rec, aggdata, flags) < 0)
1544457Sbmc 				return (-1);
1545457Sbmc 		}
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	return ((int)(recp - recs));
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate int
dtrace_sprintf(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)15520Sstevel@tonic-gate dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15530Sstevel@tonic-gate     const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	dtrace_optval_t size;
15560Sstevel@tonic-gate 	int rval;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "strsize", &size);
15590Sstevel@tonic-gate 	assert(rval == 0);
15600Sstevel@tonic-gate 	assert(dtp->dt_sprintf_buflen == 0);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	if (dtp->dt_sprintf_buf != NULL)
15630Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
15660Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	bzero(dtp->dt_sprintf_buf, size);
15690Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = size;
15701017Sbmc 	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
15711017Sbmc 	    NULL, 0);
15720Sstevel@tonic-gate 	dtp->dt_sprintf_buflen = 0;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	if (rval == -1)
15750Sstevel@tonic-gate 		free(dtp->dt_sprintf_buf);
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	return (rval);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate /*ARGSUSED*/
15810Sstevel@tonic-gate int
dtrace_system(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)15820Sstevel@tonic-gate dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
15830Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
15840Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
15850Sstevel@tonic-gate {
15860Sstevel@tonic-gate 	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	if (rval == -1)
15890Sstevel@tonic-gate 		return (rval);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	/*
15920Sstevel@tonic-gate 	 * Before we execute the specified command, flush fp to assure that
15930Sstevel@tonic-gate 	 * any prior dt_printf()'s appear before the output of the command
15940Sstevel@tonic-gate 	 * not after it.
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	(void) fflush(fp);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	if (system(dtp->dt_sprintf_buf) == -1)
15990Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	return (rval);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate int
dtrace_freopen(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)16050Sstevel@tonic-gate dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
16060Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
16070Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate 	char selfbuf[40], restorebuf[40], *filename;
16100Sstevel@tonic-gate 	FILE *nfp;
16110Sstevel@tonic-gate 	int rval, errval;
16120Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
16130Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	if (rval == -1 || fp == NULL)
16180Sstevel@tonic-gate 		return (rval);
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	if (pfd->pfd_preflen != 0 &&
16210Sstevel@tonic-gate 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
16220Sstevel@tonic-gate 		/*
16230Sstevel@tonic-gate 		 * The only way to have the format string set to the value
16240Sstevel@tonic-gate 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
16250Sstevel@tonic-gate 		 * denoting that we should restore the old stdout.
16260Sstevel@tonic-gate 		 */
16270Sstevel@tonic-gate 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 		if (dtp->dt_stdout_fd == -1) {
16300Sstevel@tonic-gate 			/*
16310Sstevel@tonic-gate 			 * We could complain here by generating an error,
16320Sstevel@tonic-gate 			 * but it seems like overkill:  it seems that calling
16330Sstevel@tonic-gate 			 * freopen() to restore stdout when freopen() has
16340Sstevel@tonic-gate 			 * never before been called should just be a no-op,
16350Sstevel@tonic-gate 			 * so we just return in this case.
16360Sstevel@tonic-gate 			 */
16370Sstevel@tonic-gate 			return (rval);
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 		(void) snprintf(restorebuf, sizeof (restorebuf),
16410Sstevel@tonic-gate 		    "/dev/fd/%d", dtp->dt_stdout_fd);
16420Sstevel@tonic-gate 		filename = restorebuf;
16430Sstevel@tonic-gate 	} else {
16440Sstevel@tonic-gate 		filename = dtp->dt_sprintf_buf;
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * freopen(3C) will always close the specified stream and underlying
16490Sstevel@tonic-gate 	 * file descriptor -- even if the specified file can't be opened.
16500Sstevel@tonic-gate 	 * Even for the semantic cesspool that is standard I/O, this is
16510Sstevel@tonic-gate 	 * surprisingly brain-dead behavior:  it means that any failure to
16520Sstevel@tonic-gate 	 * open the specified file destroys the specified stream in the
16530Sstevel@tonic-gate 	 * process -- which is particularly relevant when the specified stream
16540Sstevel@tonic-gate 	 * happens (or rather, happened) to be stdout.  This could be resolved
16550Sstevel@tonic-gate 	 * were there an "fdreopen()" equivalent of freopen() that allowed one
16560Sstevel@tonic-gate 	 * to pass a file descriptor instead of the name of a file, but there
16570Sstevel@tonic-gate 	 * is no such thing.  However, we can effect this ourselves by first
16580Sstevel@tonic-gate 	 * fopen()'ing the desired file, and then (assuming that that works),
16590Sstevel@tonic-gate 	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
16600Sstevel@tonic-gate 	 * file descriptor for the fopen()'d file.  This way, if the fopen()
16610Sstevel@tonic-gate 	 * fails, we can fail the operation without destroying stdout.
16620Sstevel@tonic-gate 	 */
16631914Scasper 	if ((nfp = fopen(filename, "aF")) == NULL) {
16640Sstevel@tonic-gate 		char *msg = strerror(errno), *faultstr;
16650Sstevel@tonic-gate 		int len = 80;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		len += strlen(msg) + strlen(filename);
16680Sstevel@tonic-gate 		faultstr = alloca(len);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
16710Sstevel@tonic-gate 		    filename, strerror(errno));
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
16740Sstevel@tonic-gate 			return (rval);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 		return (errval);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (dtp->dt_stdout_fd == -1) {
16820Sstevel@tonic-gate 		/*
16830Sstevel@tonic-gate 		 * If this is the first time that we're calling freopen(),
16840Sstevel@tonic-gate 		 * we're going to stash away the file descriptor for stdout.
16850Sstevel@tonic-gate 		 * We don't expect the dup(2) to fail, so if it does we must
16860Sstevel@tonic-gate 		 * return failure.
16870Sstevel@tonic-gate 		 */
16880Sstevel@tonic-gate 		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
16890Sstevel@tonic-gate 			(void) fclose(nfp);
16900Sstevel@tonic-gate 			return (dt_set_errno(dtp, errno));
16910Sstevel@tonic-gate 		}
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 
16941914Scasper 	if (freopen(selfbuf, "aF", fp) == NULL) {
16950Sstevel@tonic-gate 		(void) fclose(nfp);
16960Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
16970Sstevel@tonic-gate 	}
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	(void) fclose(nfp);
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	return (rval);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*ARGSUSED*/
17050Sstevel@tonic-gate int
dtrace_fprintf(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)17060Sstevel@tonic-gate dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
17070Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
17080Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
17090Sstevel@tonic-gate {
1710457Sbmc 	return (dt_printf_format(dtp, fp, fmtdata,
17111017Sbmc 	    recp, nrecs, buf, len, NULL, 0));
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate void *
dtrace_printf_create(dtrace_hdl_t * dtp,const char * s)17150Sstevel@tonic-gate dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
17180Sstevel@tonic-gate 	dt_pfargd_t *pfd;
17190Sstevel@tonic-gate 	int i;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (pfv == NULL)
17220Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	pfd = pfv->pfv_argv;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
17270Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 		if (pfc == NULL)
17300Sstevel@tonic-gate 			continue;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 		/*
17330Sstevel@tonic-gate 		 * If the output format is not %s then we assume that we have
17340Sstevel@tonic-gate 		 * been given a correctly-sized format string, so we copy the
17350Sstevel@tonic-gate 		 * true format name including the size modifier.  If the output
17360Sstevel@tonic-gate 		 * format is %s, then either the input format is %s as well or
17370Sstevel@tonic-gate 		 * it is one of our custom formats (e.g. pfprint_addr), so we
17380Sstevel@tonic-gate 		 * must set pfd_fmt to be the output format conversion "s".
17390Sstevel@tonic-gate 		 */
17400Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") != 0)
17410Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
17420Sstevel@tonic-gate 		else
17430Sstevel@tonic-gate 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	return (pfv);
17470Sstevel@tonic-gate }
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate void *
dtrace_printa_create(dtrace_hdl_t * dtp,const char * s)17500Sstevel@tonic-gate dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
17510Sstevel@tonic-gate {
17520Sstevel@tonic-gate 	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	if (pfv == NULL)
17550Sstevel@tonic-gate 		return (NULL);		/* errno has been set for us */
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	return (pfv);
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate /*ARGSUSED*/
17630Sstevel@tonic-gate size_t
dtrace_printf_format(dtrace_hdl_t * dtp,void * fmtdata,char * s,size_t len)17640Sstevel@tonic-gate dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
17650Sstevel@tonic-gate {
17660Sstevel@tonic-gate 	dt_pfargv_t *pfv = fmtdata;
17670Sstevel@tonic-gate 	dt_pfargd_t *pfd = pfv->pfv_argv;
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	/*
17700Sstevel@tonic-gate 	 * An upper bound on the string length is the length of the original
17710Sstevel@tonic-gate 	 * format string, plus three times the number of conversions (each
17720Sstevel@tonic-gate 	 * conversion could add up an additional "ll" and/or pfd_width digit
17730Sstevel@tonic-gate 	 * in the case of converting %? to %16) plus one for a terminating \0.
17740Sstevel@tonic-gate 	 */
17750Sstevel@tonic-gate 	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
17760Sstevel@tonic-gate 	char *format = alloca(formatlen);
17770Sstevel@tonic-gate 	char *f = format;
17780Sstevel@tonic-gate 	int i, j;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
17810Sstevel@tonic-gate 		const dt_pfconv_t *pfc = pfd->pfd_conv;
17820Sstevel@tonic-gate 		const char *str;
17830Sstevel@tonic-gate 		int width = pfd->pfd_width;
17840Sstevel@tonic-gate 		int prec = pfd->pfd_prec;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		if (pfd->pfd_preflen != 0) {
17870Sstevel@tonic-gate 			for (j = 0; j < pfd->pfd_preflen; j++)
17880Sstevel@tonic-gate 				*f++ = pfd->pfd_prefix[j];
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		if (pfc == NULL)
17920Sstevel@tonic-gate 			continue;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 		*f++ = '%';
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ALT)
17970Sstevel@tonic-gate 			*f++ = '#';
17980Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
17990Sstevel@tonic-gate 			*f++ = '0';
18000Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
18010Sstevel@tonic-gate 			*f++ = '-';
18020Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
18030Sstevel@tonic-gate 			*f++ = '+';
18040Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
18050Sstevel@tonic-gate 			*f++ = '*';
18060Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
18070Sstevel@tonic-gate 			*f++ = '.';
18080Sstevel@tonic-gate 			*f++ = '*';
18090Sstevel@tonic-gate 		}
18100Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
18110Sstevel@tonic-gate 			*f++ = '\'';
18120Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
18130Sstevel@tonic-gate 			*f++ = ' ';
18140Sstevel@tonic-gate 		if (pfd->pfd_flags & DT_PFCONV_AGG)
18150Sstevel@tonic-gate 			*f++ = '@';
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 		if (width != 0)
18180Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), "%d", width);
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 		if (prec != 0)
18210Sstevel@tonic-gate 			f += snprintf(f, sizeof (format), ".%d", prec);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 		/*
18240Sstevel@tonic-gate 		 * If the output format is %s, then either %s is the underlying
18250Sstevel@tonic-gate 		 * conversion or the conversion is one of our customized ones,
18260Sstevel@tonic-gate 		 * e.g. pfprint_addr.  In these cases, put the original string
18270Sstevel@tonic-gate 		 * name of the conversion (pfc_name) into the pickled format
18280Sstevel@tonic-gate 		 * string rather than the derived conversion (pfd_fmt).
18290Sstevel@tonic-gate 		 */
18300Sstevel@tonic-gate 		if (strcmp(pfc->pfc_ofmt, "s") == 0)
18310Sstevel@tonic-gate 			str = pfc->pfc_name;
18320Sstevel@tonic-gate 		else
18330Sstevel@tonic-gate 			str = pfd->pfd_fmt;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 		for (j = 0; str[j] != '\0'; j++)
18360Sstevel@tonic-gate 			*f++ = str[j];
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	*f = '\0'; /* insert nul byte; do not count in return value */
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	assert(f < format + formatlen);
18420Sstevel@tonic-gate 	(void) strncpy(s, format, len);
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	return ((size_t)(f - format));
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate static int
dt_fprinta(const dtrace_aggdata_t * adp,void * arg)1848457Sbmc dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
18490Sstevel@tonic-gate {
1850457Sbmc 	const dtrace_aggdesc_t *agg = adp->dtada_desc;
18510Sstevel@tonic-gate 	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
18520Sstevel@tonic-gate 	uint_t nrecs = agg->dtagd_nrecs;
18530Sstevel@tonic-gate 	dt_pfwalk_t *pfw = arg;
1854457Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
18550Sstevel@tonic-gate 	int id;
18560Sstevel@tonic-gate 
1857457Sbmc 	if (dt_printf_getint(dtp, recp++, nrecs--,
18580Sstevel@tonic-gate 	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
18590Sstevel@tonic-gate 		return (0); /* no aggregation id or id does not match */
18600Sstevel@tonic-gate 
1861457Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
18621017Sbmc 	    recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1863457Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18640Sstevel@tonic-gate 
1865457Sbmc 	/*
1866457Sbmc 	 * Cast away the const to set the bit indicating that this aggregation
1867457Sbmc 	 * has been printed.
1868457Sbmc 	 */
1869457Sbmc 	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	return (0);
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate 
18741017Sbmc static int
dt_fprintas(const dtrace_aggdata_t ** aggsdata,int naggvars,void * arg)18751017Sbmc dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
18761017Sbmc {
18771017Sbmc 	const dtrace_aggdata_t *aggdata = aggsdata[0];
18781017Sbmc 	const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
18791017Sbmc 	const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
18801017Sbmc 	uint_t nrecs = agg->dtagd_nrecs - 1;
18811017Sbmc 	dt_pfwalk_t *pfw = arg;
18821017Sbmc 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
18831017Sbmc 	int i;
18841017Sbmc 
18851017Sbmc 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
18861017Sbmc 	    rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
18871017Sbmc 	    aggsdata, naggvars) == -1)
18881017Sbmc 		return (pfw->pfw_err = dtp->dt_errno);
18891017Sbmc 
18901017Sbmc 	/*
18911017Sbmc 	 * For each aggregation, indicate that it has been printed, casting
18921017Sbmc 	 * away the const as necessary.
18931017Sbmc 	 */
18941017Sbmc 	for (i = 1; i < naggvars; i++) {
18951017Sbmc 		agg = aggsdata[i]->dtada_desc;
18961017Sbmc 		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
18971017Sbmc 	}
18981017Sbmc 
18991017Sbmc 	return (0);
19001017Sbmc }
19010Sstevel@tonic-gate /*ARGSUSED*/
19020Sstevel@tonic-gate int
dtrace_fprinta(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recs,uint_t nrecs,const void * buf,size_t len)19030Sstevel@tonic-gate dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
19040Sstevel@tonic-gate     const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
19050Sstevel@tonic-gate     uint_t nrecs, const void *buf, size_t len)
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	dt_pfwalk_t pfw;
19081017Sbmc 	int i, naggvars = 0;
19091017Sbmc 	dtrace_aggvarid_t *aggvars;
19101017Sbmc 
19111017Sbmc 	aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
19121017Sbmc 
19131017Sbmc 	/*
19141017Sbmc 	 * This might be a printa() with multiple aggregation variables.  We
19151017Sbmc 	 * need to scan forward through the records until we find a record from
19161017Sbmc 	 * a different statement.
19171017Sbmc 	 */
19181017Sbmc 	for (i = 0; i < nrecs; i++) {
19191017Sbmc 		const dtrace_recdesc_t *nrec = &recs[i];
19200Sstevel@tonic-gate 
19211017Sbmc 		if (nrec->dtrd_uarg != recs->dtrd_uarg)
19221017Sbmc 			break;
19231017Sbmc 
19241017Sbmc 		if (nrec->dtrd_action != recs->dtrd_action)
19251017Sbmc 			return (dt_set_errno(dtp, EDT_BADAGG));
19261017Sbmc 
19271017Sbmc 		aggvars[naggvars++] =
19281017Sbmc 		    /* LINTED - alignment */
19291017Sbmc 		    *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
19301017Sbmc 	}
19311017Sbmc 
19321017Sbmc 	if (naggvars == 0)
19331017Sbmc 		return (dt_set_errno(dtp, EDT_BADAGG));
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	pfw.pfw_argv = fmtdata;
19360Sstevel@tonic-gate 	pfw.pfw_fp = fp;
19370Sstevel@tonic-gate 	pfw.pfw_err = 0;
19380Sstevel@tonic-gate 
19391017Sbmc 	if (naggvars == 1) {
19401017Sbmc 		pfw.pfw_aid = aggvars[0];
19410Sstevel@tonic-gate 
19421017Sbmc 		if (dtrace_aggregate_walk_sorted(dtp,
19431017Sbmc 		    dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
19441017Sbmc 			return (-1); /* errno is set for us */
19451017Sbmc 	} else {
19461017Sbmc 		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
19471017Sbmc 		    dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
19481017Sbmc 			return (-1); /* errno is set for us */
19491017Sbmc 	}
19501017Sbmc 
19511017Sbmc 	return (i);
19520Sstevel@tonic-gate }
1953