1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb
22178479Sjb/*
23210767Srpaulo * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24268578Srpaulo * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25268578Srpaulo * Copyright (c) 2013 by Delphix. All rights reserved.
26178479Sjb */
27178479Sjb
28297077Smav#ifdef illumos
29178479Sjb#include <sys/sysmacros.h>
30178552Sjb#else
31178552Sjb#define	ABS(a)		((a) < 0 ? -(a) : (a))
32178552Sjb#endif
33178552Sjb#include <string.h>
34178479Sjb#include <strings.h>
35178479Sjb#include <stdlib.h>
36297077Smav#ifdef illumos
37178479Sjb#include <alloca.h>
38178552Sjb#endif
39178479Sjb#include <assert.h>
40178479Sjb#include <ctype.h>
41178479Sjb#include <errno.h>
42178479Sjb#include <limits.h>
43210767Srpaulo#include <sys/socket.h>
44210767Srpaulo#include <netdb.h>
45210767Srpaulo#include <netinet/in.h>
46210767Srpaulo#include <arpa/inet.h>
47210767Srpaulo#include <arpa/nameser.h>
48178479Sjb
49178479Sjb#include <dt_printf.h>
50178479Sjb#include <dt_string.h>
51178479Sjb#include <dt_impl.h>
52178479Sjb
53178479Sjb/*ARGSUSED*/
54178479Sjbstatic int
55178479Sjbpfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
56178479Sjb{
57178479Sjb	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
58178479Sjb}
59178479Sjb
60178479Sjb/*ARGSUSED*/
61178479Sjbstatic int
62178479Sjbpfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
63178479Sjb{
64178479Sjb	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
65178479Sjb	    dt_node_is_symaddr(dnp));
66178479Sjb}
67178479Sjb
68178479Sjb/*ARGSUSED*/
69178479Sjbstatic int
70178479Sjbpfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
71178479Sjb{
72178479Sjb	dtrace_hdl_t *dtp = pfv->pfv_dtp;
73178479Sjb	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
74178479Sjb
75178479Sjb	if (dt_node_is_usymaddr(dnp))
76178479Sjb		return (1);
77178479Sjb
78178479Sjb	if (idp == NULL || idp->di_id == 0)
79178479Sjb		return (0);
80178479Sjb
81178479Sjb	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
82178479Sjb}
83178479Sjb
84178479Sjb/*ARGSUSED*/
85178479Sjbstatic int
86178479Sjbpfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
87178479Sjb{
88178479Sjb	return (dt_node_is_stack(dnp));
89178479Sjb}
90178479Sjb
91178479Sjb/*ARGSUSED*/
92178479Sjbstatic int
93178479Sjbpfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
94178479Sjb{
95178479Sjb	return (dt_node_is_integer(dnp) &&
96178479Sjb	    dt_node_type_size(dnp) == sizeof (uint64_t));
97178479Sjb}
98178479Sjb
99178479Sjb/*ARGSUSED*/
100178479Sjbstatic int
101178479Sjbpfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
102178479Sjb{
103178479Sjb	ctf_file_t *ctfp;
104178479Sjb	ctf_encoding_t e;
105178479Sjb	ctf_arinfo_t r;
106178479Sjb	ctf_id_t base;
107178479Sjb	uint_t kind;
108178479Sjb
109178479Sjb	if (dt_node_is_string(dnp))
110178479Sjb		return (1);
111178479Sjb
112178479Sjb	ctfp = dnp->dn_ctfp;
113178479Sjb	base = ctf_type_resolve(ctfp, dnp->dn_type);
114178479Sjb	kind = ctf_type_kind(ctfp, base);
115178479Sjb
116178479Sjb	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
117178479Sjb	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
118178479Sjb	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
119178479Sjb}
120178479Sjb
121178479Sjb/*ARGSUSED*/
122178479Sjbstatic int
123178479Sjbpfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
124178479Sjb{
125178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
126178479Sjb	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
127178479Sjb	uint_t kind = ctf_type_kind(ctfp, base);
128178479Sjb
129178479Sjb	ctf_encoding_t e;
130178479Sjb	ctf_arinfo_t r;
131178479Sjb
132178479Sjb	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
133178479Sjb	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
134178479Sjb	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
135178479Sjb	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
136178479Sjb}
137178479Sjb
138178479Sjb/*ARGSUSED*/
139178479Sjbstatic int
140178479Sjbpfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
141178479Sjb{
142178479Sjb	return (dt_node_is_integer(dnp) &&
143178479Sjb	    dt_node_type_size(dnp) <= sizeof (int));
144178479Sjb}
145178479Sjb
146178479Sjb/*ARGSUSED*/
147178479Sjbstatic int
148178479Sjbpfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
149178479Sjb{
150178479Sjb	return (dt_node_is_float(dnp));
151178479Sjb}
152178479Sjb
153178479Sjb/*ARGSUSED*/
154178479Sjbstatic int
155178479Sjbpfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
156178479Sjb{
157178479Sjb	return (dt_node_is_integer(dnp));
158178479Sjb}
159178479Sjb
160178479Sjb/*ARGSUSED*/
161178479Sjbstatic int
162178479Sjbpfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
163178479Sjb{
164178479Sjb	if (dnp->dn_flags & DT_NF_SIGNED)
165253725Spfg		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'i';
166178479Sjb	else
167178479Sjb		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
168178479Sjb
169178479Sjb	return (dt_node_is_integer(dnp));
170178479Sjb}
171178479Sjb
172178479Sjb/*ARGSUSED*/
173178479Sjbstatic int
174178479Sjbpfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
175178479Sjb{
176178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
177178479Sjb	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
178178479Sjb	char n[DT_TYPE_NAMELEN];
179178479Sjb
180178479Sjb	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
181178479Sjb	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
182178479Sjb	    strcmp(n, "unsigned short") == 0));
183178479Sjb}
184178479Sjb
185178479Sjb/*ARGSUSED*/
186178479Sjbstatic int
187178479Sjbpfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
188178479Sjb{
189178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
190178479Sjb	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
191178479Sjb	char n[DT_TYPE_NAMELEN];
192178479Sjb
193178479Sjb	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
194178479Sjb	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
195178479Sjb	    strcmp(n, "unsigned long") == 0));
196178479Sjb}
197178479Sjb
198178479Sjb/*ARGSUSED*/
199178479Sjbstatic int
200178479Sjbpfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
201178479Sjb{
202178479Sjb	ctf_file_t *ctfp = dnp->dn_ctfp;
203178479Sjb	ctf_id_t type = dnp->dn_type;
204178479Sjb	char n[DT_TYPE_NAMELEN];
205178479Sjb
206178479Sjb	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
207178479Sjb	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
208178479Sjb	    strcmp(n, "signed long long") == 0 ||
209178479Sjb	    strcmp(n, "unsigned long long") == 0))
210178479Sjb		return (1);
211178479Sjb
212178479Sjb	/*
213178479Sjb	 * If the type used for %llx or %llX is not an [unsigned] long long, we
214178479Sjb	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
215178479Sjb	 * that these typedefs are guaranteed to work with %ll[xX] in either
216178479Sjb	 * compilation environment even though they alias to "long" in LP64.
217178479Sjb	 */
218178479Sjb	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
219178479Sjb		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
220178479Sjb		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
221178479Sjb			return (1);
222178479Sjb
223178479Sjb		type = ctf_type_reference(ctfp, type);
224178479Sjb	}
225178479Sjb
226178479Sjb	return (0);
227178479Sjb}
228178479Sjb
229178479Sjb/*ARGSUSED*/
230178479Sjbstatic int
231178479Sjbpfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
232178479Sjb{
233178479Sjb	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
234178479Sjb	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
235178479Sjb}
236178479Sjb
237178479Sjb/*ARGSUSED*/
238178479Sjbstatic int
239178479Sjbpfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
240178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
241178479Sjb{
242178479Sjb	int64_t normal = (int64_t)unormal;
243178479Sjb	int32_t n = (int32_t)normal;
244178479Sjb
245178479Sjb	switch (size) {
246178479Sjb	case sizeof (int8_t):
247178479Sjb		return (dt_printf(dtp, fp, format,
248178479Sjb		    (int32_t)*((int8_t *)addr) / n));
249178479Sjb	case sizeof (int16_t):
250178479Sjb		return (dt_printf(dtp, fp, format,
251178479Sjb		    (int32_t)*((int16_t *)addr) / n));
252178479Sjb	case sizeof (int32_t):
253178479Sjb		return (dt_printf(dtp, fp, format,
254178479Sjb		    *((int32_t *)addr) / n));
255178479Sjb	case sizeof (int64_t):
256178479Sjb		return (dt_printf(dtp, fp, format,
257178479Sjb		    *((int64_t *)addr) / normal));
258178479Sjb	default:
259178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
260178479Sjb	}
261178479Sjb}
262178479Sjb
263178479Sjb/*ARGSUSED*/
264178479Sjbstatic int
265178479Sjbpfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
266178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
267178479Sjb{
268178479Sjb	uint32_t n = (uint32_t)normal;
269178479Sjb
270178479Sjb	switch (size) {
271178479Sjb	case sizeof (uint8_t):
272178479Sjb		return (dt_printf(dtp, fp, format,
273178479Sjb		    (uint32_t)*((uint8_t *)addr) / n));
274178479Sjb	case sizeof (uint16_t):
275178479Sjb		return (dt_printf(dtp, fp, format,
276178479Sjb		    (uint32_t)*((uint16_t *)addr) / n));
277178479Sjb	case sizeof (uint32_t):
278178479Sjb		return (dt_printf(dtp, fp, format,
279178479Sjb		    *((uint32_t *)addr) / n));
280178479Sjb	case sizeof (uint64_t):
281178479Sjb		return (dt_printf(dtp, fp, format,
282178479Sjb		    *((uint64_t *)addr) / normal));
283178479Sjb	default:
284178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
285178479Sjb	}
286178479Sjb}
287178479Sjb
288178479Sjbstatic int
289178479Sjbpfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
290178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
291178479Sjb{
292178479Sjb	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
293178479Sjb		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
294178479Sjb	else
295178479Sjb		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
296178479Sjb}
297178479Sjb
298178479Sjb/*ARGSUSED*/
299178479Sjbstatic int
300178479Sjbpfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
301178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
302178479Sjb{
303178479Sjb	double n = (double)normal;
304178479Sjb	long double ldn = (long double)normal;
305178479Sjb
306178479Sjb	switch (size) {
307178479Sjb	case sizeof (float):
308178479Sjb		return (dt_printf(dtp, fp, format,
309178479Sjb		    (double)*((float *)addr) / n));
310178479Sjb	case sizeof (double):
311178479Sjb		return (dt_printf(dtp, fp, format,
312178479Sjb		    *((double *)addr) / n));
313183153Simp#if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
314178479Sjb	case sizeof (long double):
315178479Sjb		return (dt_printf(dtp, fp, format,
316178479Sjb		    *((long double *)addr) / ldn));
317178552Sjb#endif
318178479Sjb	default:
319178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
320178479Sjb	}
321178479Sjb}
322178479Sjb
323178479Sjb/*ARGSUSED*/
324178479Sjbstatic int
325178479Sjbpfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
326178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
327178479Sjb{
328178479Sjb	char *s;
329178479Sjb	int n, len = 256;
330178479Sjb	uint64_t val;
331178479Sjb
332178479Sjb	switch (size) {
333178479Sjb	case sizeof (uint32_t):
334178479Sjb		val = *((uint32_t *)addr);
335178479Sjb		break;
336178479Sjb	case sizeof (uint64_t):
337178479Sjb		val = *((uint64_t *)addr);
338178479Sjb		break;
339178479Sjb	default:
340178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
341178479Sjb	}
342178479Sjb
343178479Sjb	do {
344178479Sjb		n = len;
345178479Sjb		s = alloca(n);
346210767Srpaulo	} while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
347178479Sjb
348178479Sjb	return (dt_printf(dtp, fp, format, s));
349178479Sjb}
350178479Sjb
351178479Sjb/*ARGSUSED*/
352178479Sjbstatic int
353178479Sjbpfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
354178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
355178479Sjb{
356178479Sjb	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
357178479Sjb}
358178479Sjb
359178479Sjb/*ARGSUSED*/
360178479Sjbstatic int
361178479Sjbpfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
362178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
363178479Sjb{
364178479Sjb	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
365178479Sjb}
366178479Sjb
367178479Sjb/*ARGSUSED*/
368178479Sjbstatic int
369178479Sjbpfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
370178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
371178479Sjb{
372178479Sjb	char *s;
373178479Sjb	int n, len = 256;
374178479Sjb	uint64_t val, pid = 0;
375178479Sjb
376178479Sjb	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
377178479Sjb
378178479Sjb	switch (size) {
379178479Sjb	case sizeof (uint32_t):
380178479Sjb		val = (u_longlong_t)*((uint32_t *)addr);
381178479Sjb		break;
382178479Sjb	case sizeof (uint64_t):
383178479Sjb		val = (u_longlong_t)*((uint64_t *)addr);
384178479Sjb		break;
385178479Sjb	case sizeof (uint64_t) * 2:
386178479Sjb		pid = ((uint64_t *)(uintptr_t)addr)[0];
387178479Sjb		val = ((uint64_t *)(uintptr_t)addr)[1];
388178479Sjb		break;
389178479Sjb	default:
390178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
391178479Sjb	}
392178479Sjb
393178479Sjb	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
394178479Sjb		pid = idp->di_id;
395178479Sjb
396178479Sjb	do {
397178479Sjb		n = len;
398178479Sjb		s = alloca(n);
399210767Srpaulo	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
400178479Sjb
401178479Sjb	return (dt_printf(dtp, fp, format, s));
402178479Sjb}
403178479Sjb
404178479Sjb/*ARGSUSED*/
405178479Sjbstatic int
406178479Sjbpfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
407178479Sjb    const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
408178479Sjb{
409178479Sjb	int width;
410178479Sjb	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
411178479Sjb	const dtrace_recdesc_t *rec = pfd->pfd_rec;
412178479Sjb	caddr_t addr = (caddr_t)vaddr;
413178479Sjb	int err = 0;
414178479Sjb
415178479Sjb	/*
416178479Sjb	 * We have stashed the value of the STACKINDENT option, and we will
417178479Sjb	 * now override it for the purposes of formatting the stack.  If the
418178479Sjb	 * field has been specified as left-aligned (i.e. (%-#), we set the
419178479Sjb	 * indentation to be the width.  This is a slightly odd semantic, but
420178479Sjb	 * it's useful functionality -- and it's slightly odd to begin with to
421178479Sjb	 * be using a single format specifier to be formatting multiple lines
422178479Sjb	 * of text...
423178479Sjb	 */
424178479Sjb	if (pfd->pfd_dynwidth < 0) {
425178479Sjb		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
426178479Sjb		width = -pfd->pfd_dynwidth;
427178479Sjb	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
428178479Sjb		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
429178479Sjb	} else {
430178479Sjb		width = 0;
431178479Sjb	}
432178479Sjb
433178479Sjb	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
434178479Sjb
435178479Sjb	switch (rec->dtrd_action) {
436178479Sjb	case DTRACEACT_USTACK:
437178479Sjb	case DTRACEACT_JSTACK:
438178479Sjb		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
439178479Sjb		break;
440178479Sjb
441178479Sjb	case DTRACEACT_STACK:
442178479Sjb		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
443178479Sjb		    rec->dtrd_size / rec->dtrd_arg);
444178479Sjb		break;
445178479Sjb
446178479Sjb	default:
447178479Sjb		assert(0);
448178479Sjb	}
449178479Sjb
450178479Sjb	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
451178479Sjb
452178479Sjb	return (err);
453178479Sjb}
454178479Sjb
455178479Sjb/*ARGSUSED*/
456178479Sjbstatic int
457178479Sjbpfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
458178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
459178479Sjb{
460178479Sjb	char src[32], buf[32], *dst = buf;
461178479Sjb	hrtime_t time = *((uint64_t *)addr);
462178479Sjb	time_t sec = (time_t)(time / NANOSEC);
463178479Sjb	int i;
464178479Sjb
465178479Sjb	/*
466178479Sjb	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
467178479Sjb	 * Below, we turn this into the canonical adb/mdb /[yY] format,
468178479Sjb	 * "1973 Dec  3 17:20:00".
469178479Sjb	 */
470297077Smav#ifdef illumos
471178479Sjb	(void) ctime_r(&sec, src, sizeof (src));
472178552Sjb#else
473178552Sjb	(void) ctime_r(&sec, src);
474178552Sjb#endif
475178479Sjb
476178479Sjb	/*
477178479Sjb	 * Place the 4-digit year at the head of the string...
478178479Sjb	 */
479178479Sjb	for (i = 20; i < 24; i++)
480178479Sjb		*dst++ = src[i];
481178479Sjb
482178479Sjb	/*
483178479Sjb	 * ...and follow it with the remainder (month, day, hh:mm:ss).
484178479Sjb	 */
485178479Sjb	for (i = 3; i < 19; i++)
486178479Sjb		*dst++ = src[i];
487178479Sjb
488178479Sjb	*dst = '\0';
489178479Sjb	return (dt_printf(dtp, fp, format, buf));
490178479Sjb}
491178479Sjb
492178479Sjb/*
493178479Sjb * This prints the time in RFC 822 standard form.  This is useful for emitting
494178479Sjb * notions of time that are consumed by standard tools (e.g., as part of an
495178479Sjb * RSS feed).
496178479Sjb */
497178479Sjb/*ARGSUSED*/
498178479Sjbstatic int
499178479Sjbpfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
500178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
501178479Sjb{
502178479Sjb	hrtime_t time = *((uint64_t *)addr);
503178479Sjb	time_t sec = (time_t)(time / NANOSEC);
504178479Sjb	struct tm tm;
505178479Sjb	char buf[64];
506178479Sjb
507178479Sjb	(void) localtime_r(&sec, &tm);
508178479Sjb	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
509178479Sjb	return (dt_printf(dtp, fp, format, buf));
510178479Sjb}
511178479Sjb
512178479Sjb/*ARGSUSED*/
513178479Sjbstatic int
514210767Srpaulopfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
515210767Srpaulo    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
516210767Srpaulo{
517210767Srpaulo	uint16_t port = htons(*((uint16_t *)addr));
518210767Srpaulo	char buf[256];
519210767Srpaulo	struct servent *sv, res;
520210767Srpaulo
521297077Smav#ifdef illumos
522210767Srpaulo	if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
523210767Srpaulo#else
524210767Srpaulo	if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
525210767Srpaulo#endif
526210767Srpaulo		return (dt_printf(dtp, fp, format, sv->s_name));
527210767Srpaulo
528210767Srpaulo	(void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
529210767Srpaulo	return (dt_printf(dtp, fp, format, buf));
530210767Srpaulo}
531210767Srpaulo
532210767Srpaulo/*ARGSUSED*/
533210767Srpaulostatic int
534210767Srpaulopfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
535210767Srpaulo    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
536210767Srpaulo{
537210767Srpaulo	char *s = alloca(size + 1);
538210767Srpaulo	struct hostent *host, res;
539210767Srpaulo	char inetaddr[NS_IN6ADDRSZ];
540210767Srpaulo	char buf[1024];
541210767Srpaulo	int e;
542210767Srpaulo
543210767Srpaulo	bcopy(addr, s, size);
544210767Srpaulo	s[size] = '\0';
545210767Srpaulo
546210767Srpaulo	if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
547297077Smav#ifdef illumos
548210767Srpaulo		if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
549210767Srpaulo		    AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
550210767Srpaulo#else
551210767Srpaulo		if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
552210767Srpaulo		    AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
553210767Srpaulo#endif
554210767Srpaulo			return (dt_printf(dtp, fp, format, host->h_name));
555210767Srpaulo	} else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
556210767Srpaulo		if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
557210767Srpaulo		    AF_INET6, &e)) != NULL)
558210767Srpaulo			return (dt_printf(dtp, fp, format, host->h_name));
559210767Srpaulo	}
560210767Srpaulo
561210767Srpaulo	return (dt_printf(dtp, fp, format, s));
562210767Srpaulo}
563210767Srpaulo
564210767Srpaulo/*ARGSUSED*/
565210767Srpaulostatic int
566178479Sjbpfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
567178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
568178479Sjb{
569178479Sjb	char *s = alloca(size + 1);
570178479Sjb
571178479Sjb	bcopy(addr, s, size);
572178479Sjb	s[size] = '\0';
573178479Sjb	return (dt_printf(dtp, fp, format, s));
574178479Sjb}
575178479Sjb
576178479Sjb/*ARGSUSED*/
577178479Sjbstatic int
578178479Sjbpfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
579178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
580178479Sjb{
581178479Sjb	wchar_t *ws = alloca(size + sizeof (wchar_t));
582178479Sjb
583178479Sjb	bcopy(addr, ws, size);
584178479Sjb	ws[size / sizeof (wchar_t)] = L'\0';
585178479Sjb	return (dt_printf(dtp, fp, format, ws));
586178479Sjb}
587178479Sjb
588178479Sjb/*ARGSUSED*/
589178479Sjbstatic int
590178479Sjbpfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
591178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
592178479Sjb{
593178479Sjb	char *s;
594178479Sjb	int n;
595178479Sjb
596178479Sjb	if ((s = strchr2esc(addr, size)) == NULL)
597178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
598178479Sjb
599178479Sjb	n = dt_printf(dtp, fp, format, s);
600178479Sjb	free(s);
601178479Sjb	return (n);
602178479Sjb}
603178479Sjb
604178479Sjbstatic int
605178479Sjbpfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
606178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
607178479Sjb{
608178479Sjb	char c;
609178479Sjb
610178479Sjb	switch (size) {
611178479Sjb	case sizeof (int8_t):
612178479Sjb		c = *(int8_t *)addr;
613178479Sjb		break;
614178479Sjb	case sizeof (int16_t):
615178479Sjb		c = *(int16_t *)addr;
616178479Sjb		break;
617178479Sjb	case sizeof (int32_t):
618178479Sjb		c = *(int32_t *)addr;
619178479Sjb		break;
620178479Sjb	default:
621178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
622178479Sjb	}
623178479Sjb
624178479Sjb	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
625178479Sjb}
626178479Sjb
627178479Sjb/*ARGSUSED*/
628178479Sjbstatic int
629178479Sjbpfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
630178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
631178479Sjb{
632178479Sjb	return (dt_printf(dtp, fp, "%%"));
633178479Sjb}
634178479Sjb
635178479Sjbstatic const char pfproto_xint[] = "char, short, int, long, or long long";
636178479Sjbstatic const char pfproto_csi[] = "char, short, or int";
637178479Sjbstatic const char pfproto_fp[] = "float, double, or long double";
638178479Sjbstatic const char pfproto_addr[] = "pointer or integer";
639178479Sjbstatic const char pfproto_uaddr[] =
640178479Sjb	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
641178479Sjbstatic const char pfproto_cstr[] = "char [] or string (or use stringof)";
642178479Sjbstatic const char pfproto_wstr[] = "wchar_t []";
643178479Sjb
644178479Sjb/*
645178479Sjb * Printf format conversion dictionary.  This table should match the set of
646178479Sjb * conversions offered by printf(3C), as well as some additional extensions.
647178479Sjb * The second parameter is an ASCII string which is either an actual type
648178479Sjb * name we should look up (if pfcheck_type is specified), or just a descriptive
649178479Sjb * string of the types expected for use in error messages.
650178479Sjb */
651178479Sjbstatic const dt_pfconv_t _dtrace_conversions[] = {
652178479Sjb{ "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
653178479Sjb{ "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
654178479Sjb{ "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
655178479Sjb{ "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
656178479Sjb{ "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
657178479Sjb{ "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
658178479Sjb{ "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
659178479Sjb{ "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
660178479Sjb{ "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
661178479Sjb{ "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
662178479Sjb{ "hd", "d", "short", pfcheck_type, pfprint_sint },
663178479Sjb{ "hi", "i", "short", pfcheck_type, pfprint_sint },
664178479Sjb{ "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
665178479Sjb{ "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
666178479Sjb{ "hx", "x", "short", pfcheck_xshort, pfprint_uint },
667178479Sjb{ "hX", "X", "short", pfcheck_xshort, pfprint_uint },
668253725Spfg{ "i", "i", pfproto_xint, pfcheck_xint, pfprint_sint },
669210767Srpaulo{ "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
670178479Sjb{ "k", "s", "stack", pfcheck_stack, pfprint_stack },
671178479Sjb{ "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
672178479Sjb{ "ld",	"d", "long", pfcheck_type, pfprint_sint },
673178479Sjb{ "li",	"i", "long", pfcheck_type, pfprint_sint },
674178479Sjb{ "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
675178479Sjb{ "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
676178479Sjb{ "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
677178479Sjb{ "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
678178479Sjb{ "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
679178479Sjb{ "lld", "d", "long long", pfcheck_type, pfprint_sint },
680178479Sjb{ "lli", "i", "long long", pfcheck_type, pfprint_sint },
681178479Sjb{ "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
682178479Sjb{ "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
683178479Sjb{ "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
684178479Sjb{ "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
685178479Sjb{ "Le",	"e", "long double", pfcheck_type, pfprint_fp },
686178479Sjb{ "LE",	"E", "long double", pfcheck_type, pfprint_fp },
687178479Sjb{ "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
688178479Sjb{ "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
689178479Sjb{ "LG",	"G", "long double", pfcheck_type, pfprint_fp },
690178479Sjb{ "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
691178479Sjb{ "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
692210767Srpaulo{ "P", "s", "uint16_t", pfcheck_type, pfprint_port },
693178479Sjb{ "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
694178479Sjb{ "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
695178479Sjb{ "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
696178479Sjb{ "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
697297077Smav#ifdef illumos
698178479Sjb{ "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
699178479Sjb{ "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
700269263Smarkj#else
701269263Smarkj{ "wc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
702269263Smarkj{ "ws", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
703269263Smarkj#endif
704178479Sjb{ "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
705178479Sjb{ "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
706178479Sjb{ "Y", "s", "int64_t", pfcheck_time, pfprint_time },
707178479Sjb{ "%", "%", "void", pfcheck_type, pfprint_pct },
708178479Sjb{ NULL, NULL, NULL, NULL, NULL }
709178479Sjb};
710178479Sjb
711178479Sjbint
712178479Sjbdt_pfdict_create(dtrace_hdl_t *dtp)
713178479Sjb{
714178479Sjb	uint_t n = _dtrace_strbuckets;
715178479Sjb	const dt_pfconv_t *pfd;
716178479Sjb	dt_pfdict_t *pdi;
717178479Sjb
718178479Sjb	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
719178479Sjb	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
720178479Sjb		free(pdi);
721178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
722178479Sjb	}
723178479Sjb
724178479Sjb	dtp->dt_pfdict = pdi;
725178479Sjb	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
726178479Sjb	pdi->pdi_nbuckets = n;
727178479Sjb
728178479Sjb	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
729178479Sjb		dtrace_typeinfo_t dtt;
730178479Sjb		dt_pfconv_t *pfc;
731178479Sjb		uint_t h;
732178479Sjb
733178479Sjb		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
734178479Sjb			dt_pfdict_destroy(dtp);
735178479Sjb			return (dt_set_errno(dtp, EDT_NOMEM));
736178479Sjb		}
737178479Sjb
738178479Sjb		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
739178479Sjb		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
740178479Sjb		pfc->pfc_next = pdi->pdi_buckets[h];
741178479Sjb		pdi->pdi_buckets[h] = pfc;
742178479Sjb
743178479Sjb		dtt.dtt_ctfp = NULL;
744178479Sjb		dtt.dtt_type = CTF_ERR;
745178479Sjb
746178479Sjb		/*
747178479Sjb		 * The "D" container or its parent must contain a definition of
748178479Sjb		 * any type referenced by a printf conversion.  If none can be
749178479Sjb		 * found, we fail to initialize the printf dictionary.
750178479Sjb		 */
751178479Sjb		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
752178479Sjb		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
753178479Sjb			dt_pfdict_destroy(dtp);
754178479Sjb			return (dt_set_errno(dtp, EDT_NOCONV));
755178479Sjb		}
756178479Sjb
757178479Sjb		pfc->pfc_dctfp = dtt.dtt_ctfp;
758178479Sjb		pfc->pfc_dtype = dtt.dtt_type;
759178479Sjb
760178479Sjb		/*
761178479Sjb		 * The "C" container may contain an alternate definition of an
762178479Sjb		 * explicit conversion type.  If it does, use it; otherwise
763178479Sjb		 * just set pfc_ctype to pfc_dtype so it is always valid.
764178479Sjb		 */
765178479Sjb		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
766178479Sjb		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
767178479Sjb			pfc->pfc_cctfp = dtt.dtt_ctfp;
768178479Sjb			pfc->pfc_ctype = dtt.dtt_type;
769178479Sjb		} else {
770178479Sjb			pfc->pfc_cctfp = pfc->pfc_dctfp;
771178479Sjb			pfc->pfc_ctype = pfc->pfc_dtype;
772178479Sjb		}
773178479Sjb
774178479Sjb		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
775178479Sjb		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
776178479Sjb			dt_pfdict_destroy(dtp);
777178479Sjb			return (dt_set_errno(dtp, EDT_BADCONV));
778178479Sjb		}
779178479Sjb
780178479Sjb		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
781178479Sjb	}
782178479Sjb
783178479Sjb	return (0);
784178479Sjb}
785178479Sjb
786178479Sjbvoid
787178479Sjbdt_pfdict_destroy(dtrace_hdl_t *dtp)
788178479Sjb{
789178479Sjb	dt_pfdict_t *pdi = dtp->dt_pfdict;
790178479Sjb	dt_pfconv_t *pfc, *nfc;
791178479Sjb	uint_t i;
792178479Sjb
793178479Sjb	if (pdi == NULL)
794178479Sjb		return;
795178479Sjb
796178479Sjb	for (i = 0; i < pdi->pdi_nbuckets; i++) {
797178479Sjb		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
798178479Sjb			nfc = pfc->pfc_next;
799178479Sjb			free(pfc);
800178479Sjb		}
801178479Sjb	}
802178479Sjb
803178479Sjb	free(pdi->pdi_buckets);
804178479Sjb	free(pdi);
805178479Sjb	dtp->dt_pfdict = NULL;
806178479Sjb}
807178479Sjb
808178479Sjbstatic const dt_pfconv_t *
809178479Sjbdt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
810178479Sjb{
811178479Sjb	dt_pfdict_t *pdi = dtp->dt_pfdict;
812178479Sjb	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
813178479Sjb	const dt_pfconv_t *pfc;
814178479Sjb
815178479Sjb	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
816178479Sjb		if (strcmp(pfc->pfc_name, name) == 0)
817178479Sjb			break;
818178479Sjb	}
819178479Sjb
820178479Sjb	return (pfc);
821178479Sjb}
822178479Sjb
823178479Sjbstatic dt_pfargv_t *
824178479Sjbdt_printf_error(dtrace_hdl_t *dtp, int err)
825178479Sjb{
826178479Sjb	if (yypcb != NULL)
827178479Sjb		longjmp(yypcb->pcb_jmpbuf, err);
828178479Sjb
829178479Sjb	(void) dt_set_errno(dtp, err);
830178479Sjb	return (NULL);
831178479Sjb}
832178479Sjb
833178479Sjbdt_pfargv_t *
834178479Sjbdt_printf_create(dtrace_hdl_t *dtp, const char *s)
835178479Sjb{
836178479Sjb	dt_pfargd_t *pfd, *nfd = NULL;
837178479Sjb	dt_pfargv_t *pfv;
838178479Sjb	const char *p, *q;
839178479Sjb	char *format;
840178479Sjb
841178479Sjb	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
842178479Sjb	    (format = strdup(s)) == NULL) {
843178479Sjb		free(pfv);
844178479Sjb		return (dt_printf_error(dtp, EDT_NOMEM));
845178479Sjb	}
846178479Sjb
847178479Sjb	pfv->pfv_format = format;
848178479Sjb	pfv->pfv_argv = NULL;
849178479Sjb	pfv->pfv_argc = 0;
850178479Sjb	pfv->pfv_flags = 0;
851178479Sjb	pfv->pfv_dtp = dtp;
852178479Sjb
853178479Sjb	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
854178479Sjb		uint_t namelen = 0;
855178479Sjb		int digits = 0;
856178479Sjb		int dot = 0;
857178479Sjb
858178479Sjb		char name[8];
859178479Sjb		char c;
860178479Sjb		int n;
861178479Sjb
862178479Sjb		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
863178479Sjb			dt_printf_destroy(pfv);
864178479Sjb			return (dt_printf_error(dtp, EDT_NOMEM));
865178479Sjb		}
866178479Sjb
867178479Sjb		if (pfv->pfv_argv != NULL)
868178479Sjb			nfd->pfd_next = pfd;
869178479Sjb		else
870178479Sjb			pfv->pfv_argv = pfd;
871178479Sjb
872178479Sjb		bzero(pfd, sizeof (dt_pfargd_t));
873178479Sjb		pfv->pfv_argc++;
874178479Sjb		nfd = pfd;
875178479Sjb
876178479Sjb		if (p > q) {
877178479Sjb			pfd->pfd_preflen = (size_t)(p - q);
878178479Sjb			pfd->pfd_prefix = q;
879178479Sjb		}
880178479Sjb
881178479Sjb		fmt_switch:
882178479Sjb		switch (c = *++p) {
883178479Sjb		case '0': case '1': case '2': case '3': case '4':
884178479Sjb		case '5': case '6': case '7': case '8': case '9':
885178479Sjb			if (dot == 0 && digits == 0 && c == '0') {
886178479Sjb				pfd->pfd_flags |= DT_PFCONV_ZPAD;
887178479Sjb				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
888178479Sjb				goto fmt_switch;
889178479Sjb			}
890178479Sjb
891178479Sjb			for (n = 0; isdigit(c); c = *++p)
892178479Sjb				n = n * 10 + c - '0';
893178479Sjb
894178479Sjb			if (dot)
895178479Sjb				pfd->pfd_prec = n;
896178479Sjb			else
897178479Sjb				pfd->pfd_width = n;
898178479Sjb
899178479Sjb			p--;
900178479Sjb			digits++;
901178479Sjb			goto fmt_switch;
902178479Sjb
903178479Sjb		case '#':
904178479Sjb			pfd->pfd_flags |= DT_PFCONV_ALT;
905178479Sjb			goto fmt_switch;
906178479Sjb
907178479Sjb		case '*':
908178479Sjb			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
909178479Sjb
910178479Sjb			if (pfd->pfd_flags & n) {
911178479Sjb				yywarn("format conversion #%u has more than "
912178479Sjb				    "one '*' specified for the output %s\n",
913178479Sjb				    pfv->pfv_argc, n ? "precision" : "width");
914178479Sjb
915178479Sjb				dt_printf_destroy(pfv);
916178479Sjb				return (dt_printf_error(dtp, EDT_COMPILER));
917178479Sjb			}
918178479Sjb
919178479Sjb			pfd->pfd_flags |= n;
920178479Sjb			goto fmt_switch;
921178479Sjb
922178479Sjb		case '+':
923178479Sjb			pfd->pfd_flags |= DT_PFCONV_SPOS;
924178479Sjb			goto fmt_switch;
925178479Sjb
926178479Sjb		case '-':
927178479Sjb			pfd->pfd_flags |= DT_PFCONV_LEFT;
928178479Sjb			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
929178479Sjb			goto fmt_switch;
930178479Sjb
931178479Sjb		case '.':
932178479Sjb			if (dot++ != 0) {
933178479Sjb				yywarn("format conversion #%u has more than "
934178479Sjb				    "one '.' specified\n", pfv->pfv_argc);
935178479Sjb
936178479Sjb				dt_printf_destroy(pfv);
937178479Sjb				return (dt_printf_error(dtp, EDT_COMPILER));
938178479Sjb			}
939178479Sjb			digits = 0;
940178479Sjb			goto fmt_switch;
941178479Sjb
942178479Sjb		case '?':
943178479Sjb			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
944178479Sjb				pfd->pfd_width = 16;
945178479Sjb			else
946178479Sjb				pfd->pfd_width = 8;
947178479Sjb			goto fmt_switch;
948178479Sjb
949178479Sjb		case '@':
950178479Sjb			pfd->pfd_flags |= DT_PFCONV_AGG;
951178479Sjb			goto fmt_switch;
952178479Sjb
953178479Sjb		case '\'':
954178479Sjb			pfd->pfd_flags |= DT_PFCONV_GROUP;
955178479Sjb			goto fmt_switch;
956178479Sjb
957178479Sjb		case ' ':
958178479Sjb			pfd->pfd_flags |= DT_PFCONV_SPACE;
959178479Sjb			goto fmt_switch;
960178479Sjb
961178479Sjb		case '$':
962178479Sjb			yywarn("format conversion #%u uses unsupported "
963178479Sjb			    "positional format (%%n$)\n", pfv->pfv_argc);
964178479Sjb
965178479Sjb			dt_printf_destroy(pfv);
966178479Sjb			return (dt_printf_error(dtp, EDT_COMPILER));
967178479Sjb
968178479Sjb		case '%':
969178479Sjb			if (p[-1] == '%')
970178479Sjb				goto default_lbl; /* if %% then use "%" conv */
971178479Sjb
972178479Sjb			yywarn("format conversion #%u cannot be combined "
973178479Sjb			    "with other format flags: %%%%\n", pfv->pfv_argc);
974178479Sjb
975178479Sjb			dt_printf_destroy(pfv);
976178479Sjb			return (dt_printf_error(dtp, EDT_COMPILER));
977178479Sjb
978178479Sjb		case '\0':
979178479Sjb			yywarn("format conversion #%u name expected before "
980178479Sjb			    "end of format string\n", pfv->pfv_argc);
981178479Sjb
982178479Sjb			dt_printf_destroy(pfv);
983178479Sjb			return (dt_printf_error(dtp, EDT_COMPILER));
984178479Sjb
985178479Sjb		case 'h':
986178479Sjb		case 'l':
987178479Sjb		case 'L':
988178479Sjb		case 'w':
989178479Sjb			if (namelen < sizeof (name) - 2)
990178479Sjb				name[namelen++] = c;
991178479Sjb			goto fmt_switch;
992178479Sjb
993178479Sjb		default_lbl:
994178479Sjb		default:
995178479Sjb			name[namelen++] = c;
996178479Sjb			name[namelen] = '\0';
997178479Sjb		}
998178479Sjb
999178479Sjb		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
1000178479Sjb
1001178479Sjb		if (pfd->pfd_conv == NULL) {
1002178479Sjb			yywarn("format conversion #%u is undefined: %%%s\n",
1003178479Sjb			    pfv->pfv_argc, name);
1004178479Sjb			dt_printf_destroy(pfv);
1005178479Sjb			return (dt_printf_error(dtp, EDT_COMPILER));
1006178479Sjb		}
1007178479Sjb	}
1008178479Sjb
1009178479Sjb	if (*q != '\0' || *format == '\0') {
1010178479Sjb		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1011178479Sjb			dt_printf_destroy(pfv);
1012178479Sjb			return (dt_printf_error(dtp, EDT_NOMEM));
1013178479Sjb		}
1014178479Sjb
1015178479Sjb		if (pfv->pfv_argv != NULL)
1016178479Sjb			nfd->pfd_next = pfd;
1017178479Sjb		else
1018178479Sjb			pfv->pfv_argv = pfd;
1019178479Sjb
1020178479Sjb		bzero(pfd, sizeof (dt_pfargd_t));
1021178479Sjb		pfv->pfv_argc++;
1022178479Sjb
1023178479Sjb		pfd->pfd_prefix = q;
1024178479Sjb		pfd->pfd_preflen = strlen(q);
1025178479Sjb	}
1026178479Sjb
1027178479Sjb	return (pfv);
1028178479Sjb}
1029178479Sjb
1030178479Sjbvoid
1031178479Sjbdt_printf_destroy(dt_pfargv_t *pfv)
1032178479Sjb{
1033178479Sjb	dt_pfargd_t *pfd, *nfd;
1034178479Sjb
1035178479Sjb	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1036178479Sjb		nfd = pfd->pfd_next;
1037178479Sjb		free(pfd);
1038178479Sjb	}
1039178479Sjb
1040178479Sjb	free(pfv->pfv_format);
1041178479Sjb	free(pfv);
1042178479Sjb}
1043178479Sjb
1044178479Sjbvoid
1045178479Sjbdt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1046178479Sjb    dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1047178479Sjb{
1048178479Sjb	dt_pfargd_t *pfd = pfv->pfv_argv;
1049178479Sjb	const char *func = idp->di_name;
1050178479Sjb
1051178479Sjb	char n[DT_TYPE_NAMELEN];
1052178479Sjb	dtrace_typeinfo_t dtt;
1053178479Sjb	const char *aggtype;
1054178479Sjb	dt_node_t aggnode;
1055178479Sjb	int i, j;
1056178479Sjb
1057178479Sjb	if (pfv->pfv_format[0] == '\0') {
1058178479Sjb		xyerror(D_PRINTF_FMT_EMPTY,
1059178479Sjb		    "%s( ) format string is empty\n", func);
1060178479Sjb	}
1061178479Sjb
1062178479Sjb	pfv->pfv_flags = flags;
1063178479Sjb
1064178479Sjb	/*
1065178479Sjb	 * We fake up a parse node representing the type that can be used with
1066178479Sjb	 * an aggregation result conversion, which -- for all but count() --
1067178479Sjb	 * is a signed quantity.
1068178479Sjb	 */
1069178479Sjb	if (kind != DTRACEAGG_COUNT)
1070178479Sjb		aggtype = "int64_t";
1071178479Sjb	else
1072178479Sjb		aggtype = "uint64_t";
1073178479Sjb
1074178479Sjb	if (dt_type_lookup(aggtype, &dtt) != 0)
1075178479Sjb		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1076178479Sjb
1077178479Sjb	bzero(&aggnode, sizeof (aggnode));
1078268578Srpaulo	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
1079178479Sjb
1080178479Sjb	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1081178479Sjb		const dt_pfconv_t *pfc = pfd->pfd_conv;
1082178479Sjb		const char *dyns[2];
1083178479Sjb		int dync = 0;
1084178479Sjb
1085178479Sjb		char vname[64];
1086178479Sjb		dt_node_t *vnp;
1087178479Sjb
1088178479Sjb		if (pfc == NULL)
1089178479Sjb			continue; /* no checking if argd is just a prefix */
1090178479Sjb
1091178479Sjb		if (pfc->pfc_print == &pfprint_pct) {
1092178479Sjb			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1093178479Sjb			continue;
1094178479Sjb		}
1095178479Sjb
1096178479Sjb		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1097178479Sjb			dyns[dync++] = ".*";
1098178479Sjb		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1099178479Sjb			dyns[dync++] = "*";
1100178479Sjb
1101178479Sjb		for (; dync != 0; dync--) {
1102178479Sjb			if (dnp == NULL) {
1103178479Sjb				xyerror(D_PRINTF_DYN_PROTO,
1104178479Sjb				    "%s( ) prototype mismatch: conversion "
1105178479Sjb				    "#%d (%%%s) is missing a corresponding "
1106178479Sjb				    "\"%s\" argument\n", func, i + 1,
1107178479Sjb				    pfc->pfc_name, dyns[dync - 1]);
1108178479Sjb			}
1109178479Sjb
1110178479Sjb			if (dt_node_is_integer(dnp) == 0) {
1111178479Sjb				xyerror(D_PRINTF_DYN_TYPE,
1112178479Sjb				    "%s( ) argument #%d is incompatible "
1113178479Sjb				    "with conversion #%d prototype:\n"
1114178479Sjb				    "\tconversion: %% %s %s\n"
1115178479Sjb				    "\t prototype: int\n\t  argument: %s\n",
1116178479Sjb				    func, j + foff + 1, i + 1,
1117178479Sjb				    dyns[dync - 1], pfc->pfc_name,
1118178479Sjb				    dt_node_type_name(dnp, n, sizeof (n)));
1119178479Sjb			}
1120178479Sjb
1121178479Sjb			dnp = dnp->dn_list;
1122178479Sjb			j++;
1123178479Sjb		}
1124178479Sjb
1125178479Sjb		/*
1126178479Sjb		 * If this conversion is consuming the aggregation data, set
1127178479Sjb		 * the value node pointer (vnp) to a fake node based on the
1128178479Sjb		 * aggregating function result type.  Otherwise assign vnp to
1129178479Sjb		 * the next parse node in the argument list, if there is one.
1130178479Sjb		 */
1131178479Sjb		if (pfd->pfd_flags & DT_PFCONV_AGG) {
1132178479Sjb			if (!(flags & DT_PRINTF_AGGREGATION)) {
1133178479Sjb				xyerror(D_PRINTF_AGG_CONV,
1134178479Sjb				    "%%@ conversion requires an aggregation"
1135178479Sjb				    " and is not for use with %s( )\n", func);
1136178479Sjb			}
1137178479Sjb			(void) strlcpy(vname, "aggregating action",
1138178479Sjb			    sizeof (vname));
1139178479Sjb			vnp = &aggnode;
1140178479Sjb		} else if (dnp == NULL) {
1141178479Sjb			xyerror(D_PRINTF_ARG_PROTO,
1142178479Sjb			    "%s( ) prototype mismatch: conversion #%d (%%"
1143178479Sjb			    "%s) is missing a corresponding value argument\n",
1144178479Sjb			    func, i + 1, pfc->pfc_name);
1145178479Sjb		} else {
1146178479Sjb			(void) snprintf(vname, sizeof (vname),
1147178479Sjb			    "argument #%d", j + foff + 1);
1148178479Sjb			vnp = dnp;
1149178479Sjb			dnp = dnp->dn_list;
1150178479Sjb			j++;
1151178479Sjb		}
1152178479Sjb
1153178479Sjb		/*
1154178479Sjb		 * Fill in the proposed final format string by prepending any
1155178479Sjb		 * size-related prefixes to the pfconv's format string.  The
1156178479Sjb		 * pfc_check() function below may optionally modify the format
1157178479Sjb		 * as part of validating the type of the input argument.
1158178479Sjb		 */
1159178479Sjb		if (pfc->pfc_print == &pfprint_sint ||
1160178479Sjb		    pfc->pfc_print == &pfprint_uint ||
1161178479Sjb		    pfc->pfc_print == &pfprint_dint) {
1162178479Sjb			if (dt_node_type_size(vnp) == sizeof (uint64_t))
1163178479Sjb				(void) strcpy(pfd->pfd_fmt, "ll");
1164178479Sjb		} else if (pfc->pfc_print == &pfprint_fp) {
1165178479Sjb			if (dt_node_type_size(vnp) == sizeof (long double))
1166178479Sjb				(void) strcpy(pfd->pfd_fmt, "L");
1167178479Sjb		}
1168178479Sjb
1169178479Sjb		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1170178479Sjb
1171178479Sjb		/*
1172178479Sjb		 * Validate the format conversion against the value node type.
1173178479Sjb		 * If the conversion is good, create the descriptor format
1174178479Sjb		 * string by concatenating together any required printf(3C)
1175178479Sjb		 * size prefixes with the conversion's native format string.
1176178479Sjb		 */
1177178479Sjb		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1178178479Sjb			xyerror(D_PRINTF_ARG_TYPE,
1179178479Sjb			    "%s( ) %s is incompatible with "
1180178479Sjb			    "conversion #%d prototype:\n\tconversion: %%%s\n"
1181178479Sjb			    "\t prototype: %s\n\t  argument: %s\n", func,
1182178479Sjb			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1183178479Sjb			    dt_node_type_name(vnp, n, sizeof (n)));
1184178479Sjb		}
1185178479Sjb	}
1186178479Sjb
1187178479Sjb	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1188178479Sjb		xyerror(D_PRINTF_ARG_EXTRA,
1189178479Sjb		    "%s( ) prototype mismatch: only %d arguments "
1190178479Sjb		    "required by this format string\n", func, j);
1191178479Sjb	}
1192178479Sjb}
1193178479Sjb
1194178479Sjbvoid
1195178479Sjbdt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1196178479Sjb{
1197178479Sjb	dt_ident_t *lid, *rid;
1198178479Sjb	dt_node_t *lproto, *rproto;
1199178479Sjb	int largc, rargc, argn;
1200178479Sjb	char n1[DT_TYPE_NAMELEN];
1201178479Sjb	char n2[DT_TYPE_NAMELEN];
1202178479Sjb
1203178479Sjb	assert(lhs->dn_kind == DT_NODE_AGG);
1204178479Sjb	assert(rhs->dn_kind == DT_NODE_AGG);
1205178479Sjb
1206178479Sjb	lid = lhs->dn_ident;
1207178479Sjb	rid = rhs->dn_ident;
1208178479Sjb
1209178479Sjb	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1210178479Sjb	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1211178479Sjb
1212178479Sjb	/*
1213178479Sjb	 * First, get an argument count on each side.  These must match.
1214178479Sjb	 */
1215178479Sjb	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1216178479Sjb		largc++;
1217178479Sjb
1218178479Sjb	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1219178479Sjb		rargc++;
1220178479Sjb
1221178479Sjb	if (largc != rargc) {
1222178479Sjb		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1223178479Sjb		    "matching key signatures: @%s has %d key%s, @%s has %d "
1224178479Sjb		    "key%s", lid->di_name, rid->di_name,
1225178479Sjb		    lid->di_name, largc, largc == 1 ? "" : "s",
1226178479Sjb		    rid->di_name, rargc, rargc == 1 ? "" : "s");
1227178479Sjb	}
1228178479Sjb
1229178479Sjb	/*
1230178479Sjb	 * Now iterate over the keys to verify that each type matches.
1231178479Sjb	 */
1232178479Sjb	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1233178479Sjb	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1234178479Sjb
1235178479Sjb	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1236178479Sjb	    rproto = rproto->dn_list) {
1237178479Sjb		assert(rproto != NULL);
1238178479Sjb
1239178479Sjb		if (dt_node_is_argcompat(lproto, rproto))
1240178479Sjb			continue;
1241178479Sjb
1242178479Sjb		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1243178479Sjb		    "incompatible with @%s:\n%9s key #%d: %s\n"
1244178479Sjb		    "%9s key #%d: %s\n",
1245178479Sjb		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
1246178479Sjb		    dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1247178479Sjb		    argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1248178479Sjb	}
1249178479Sjb}
1250178479Sjb
1251178479Sjbstatic int
1252178479Sjbdt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1253178479Sjb    uint_t nrecs, const void *buf, size_t len, int *ip)
1254178479Sjb{
1255178479Sjb	uintptr_t addr;
1256178479Sjb
1257178479Sjb	if (nrecs == 0)
1258178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
1259178479Sjb
1260178479Sjb	addr = (uintptr_t)buf + recp->dtrd_offset;
1261178479Sjb
1262178479Sjb	if (addr + sizeof (int) > (uintptr_t)buf + len)
1263178479Sjb		return (dt_set_errno(dtp, EDT_DOFFSET));
1264178479Sjb
1265178479Sjb	if (addr & (recp->dtrd_alignment - 1))
1266178479Sjb		return (dt_set_errno(dtp, EDT_DALIGN));
1267178479Sjb
1268178479Sjb	switch (recp->dtrd_size) {
1269178479Sjb	case sizeof (int8_t):
1270178479Sjb		*ip = (int)*((int8_t *)addr);
1271178479Sjb		break;
1272178479Sjb	case sizeof (int16_t):
1273178479Sjb		*ip = (int)*((int16_t *)addr);
1274178479Sjb		break;
1275178479Sjb	case sizeof (int32_t):
1276178479Sjb		*ip = (int)*((int32_t *)addr);
1277178479Sjb		break;
1278178479Sjb	case sizeof (int64_t):
1279178479Sjb		*ip = (int)*((int64_t *)addr);
1280178479Sjb		break;
1281178479Sjb	default:
1282178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
1283178479Sjb	}
1284178479Sjb
1285178479Sjb	return (0);
1286178479Sjb}
1287178479Sjb
1288178479Sjb/*ARGSUSED*/
1289178479Sjbstatic int
1290178479Sjbpfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1291178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1292178479Sjb{
1293178479Sjb	const uint64_t *data = addr;
1294178479Sjb
1295178479Sjb	if (size != sizeof (uint64_t) * 2)
1296178479Sjb		return (dt_set_errno(dtp, EDT_DMISMATCH));
1297178479Sjb
1298178479Sjb	return (dt_printf(dtp, fp, format,
1299178479Sjb	    data[0] ? data[1] / normal / data[0] : 0));
1300178479Sjb}
1301178479Sjb
1302178479Sjb/*ARGSUSED*/
1303178479Sjbstatic int
1304210767Srpaulopfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1305210767Srpaulo    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1306210767Srpaulo{
1307210767Srpaulo	const uint64_t *data = addr;
1308210767Srpaulo
1309210767Srpaulo	if (size != sizeof (uint64_t) * 4)
1310210767Srpaulo		return (dt_set_errno(dtp, EDT_DMISMATCH));
1311210767Srpaulo
1312210767Srpaulo	return (dt_printf(dtp, fp, format,
1313210767Srpaulo	    dt_stddev((uint64_t *)data, normal)));
1314210767Srpaulo}
1315210767Srpaulo
1316210767Srpaulo/*ARGSUSED*/
1317210767Srpaulostatic int
1318178479Sjbpfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1319178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1320178479Sjb{
1321178479Sjb	return (dt_print_quantize(dtp, fp, addr, size, normal));
1322178479Sjb}
1323178479Sjb
1324178479Sjb/*ARGSUSED*/
1325178479Sjbstatic int
1326178479Sjbpfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327178479Sjb    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1328178479Sjb{
1329178479Sjb	return (dt_print_lquantize(dtp, fp, addr, size, normal));
1330178479Sjb}
1331178479Sjb
1332237624Spfg/*ARGSUSED*/
1333178479Sjbstatic int
1334237624Spfgpfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1335237624Spfg    const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1336237624Spfg{
1337237624Spfg	return (dt_print_llquantize(dtp, fp, addr, size, normal));
1338237624Spfg}
1339237624Spfg
1340237624Spfgstatic int
1341178479Sjbdt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1342178479Sjb    const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1343178479Sjb    size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1344178479Sjb{
1345178479Sjb	dt_pfargd_t *pfd = pfv->pfv_argv;
1346178479Sjb	const dtrace_recdesc_t *recp = recs;
1347178479Sjb	const dtrace_aggdata_t *aggdata;
1348178479Sjb	dtrace_aggdesc_t *agg;
1349178479Sjb	caddr_t lim = (caddr_t)buf + len, limit;
1350178479Sjb	char format[64] = "%";
1351287226Smarkj	size_t ret;
1352178479Sjb	int i, aggrec, curagg = -1;
1353178479Sjb	uint64_t normal;
1354178479Sjb
1355178479Sjb	/*
1356178479Sjb	 * If we are formatting an aggregation, set 'aggrec' to the index of
1357178479Sjb	 * the final record description (the aggregation result) so we can use
1358178479Sjb	 * this record index with any conversion where DT_PFCONV_AGG is set.
1359178479Sjb	 * (The actual aggregation used will vary as we increment through the
1360178479Sjb	 * aggregation variables that we have been passed.)  Finally, we
1361178479Sjb	 * decrement nrecs to prevent this record from being used with any
1362178479Sjb	 * other conversion.
1363178479Sjb	 */
1364178479Sjb	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1365178479Sjb		assert(aggsdata != NULL);
1366178479Sjb		assert(naggvars > 0);
1367178479Sjb
1368178479Sjb		if (nrecs == 0)
1369178479Sjb			return (dt_set_errno(dtp, EDT_DMISMATCH));
1370178479Sjb
1371178479Sjb		curagg = naggvars > 1 ? 1 : 0;
1372178479Sjb		aggdata = aggsdata[0];
1373178479Sjb		aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1374178479Sjb		nrecs--;
1375178479Sjb	}
1376178479Sjb
1377178479Sjb	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1378178479Sjb		const dt_pfconv_t *pfc = pfd->pfd_conv;
1379178479Sjb		int width = pfd->pfd_width;
1380178479Sjb		int prec = pfd->pfd_prec;
1381178479Sjb		int rval;
1382178479Sjb
1383287226Smarkj		const char *start;
1384178479Sjb		char *f = format + 1; /* skip initial '%' */
1385287226Smarkj		size_t fmtsz = sizeof(format) - 1;
1386178479Sjb		const dtrace_recdesc_t *rec;
1387178479Sjb		dt_pfprint_f *func;
1388178479Sjb		caddr_t addr;
1389178479Sjb		size_t size;
1390178479Sjb		uint32_t flags;
1391178479Sjb
1392178479Sjb		if (pfd->pfd_preflen != 0) {
1393178479Sjb			char *tmp = alloca(pfd->pfd_preflen + 1);
1394178479Sjb
1395178479Sjb			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1396178479Sjb			tmp[pfd->pfd_preflen] = '\0';
1397178479Sjb
1398178479Sjb			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1399178479Sjb				return (rval);
1400178479Sjb
1401178479Sjb			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1402178479Sjb				/*
1403178479Sjb				 * For printa(), we flush the buffer after each
1404178479Sjb				 * prefix, setting the flags to indicate that
1405178479Sjb				 * this is part of the printa() format string.
1406178479Sjb				 */
1407178479Sjb				flags = DTRACE_BUFDATA_AGGFORMAT;
1408178479Sjb
1409178479Sjb				if (pfc == NULL && i == pfv->pfv_argc - 1)
1410178479Sjb					flags |= DTRACE_BUFDATA_AGGLAST;
1411178479Sjb
1412178479Sjb				if (dt_buffered_flush(dtp, NULL, NULL,
1413178479Sjb				    aggdata, flags) < 0)
1414178479Sjb					return (-1);
1415178479Sjb			}
1416178479Sjb		}
1417178479Sjb
1418178479Sjb		if (pfc == NULL) {
1419178479Sjb			if (pfv->pfv_argc == 1)
1420178479Sjb				return (nrecs != 0);
1421178479Sjb			continue;
1422178479Sjb		}
1423178479Sjb
1424178479Sjb		/*
1425178479Sjb		 * If the conversion is %%, just invoke the print callback
1426178479Sjb		 * with no data record and continue; it consumes no record.
1427178479Sjb		 */
1428178479Sjb		if (pfc->pfc_print == &pfprint_pct) {
1429178479Sjb			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1430178479Sjb				continue;
1431178479Sjb			return (-1); /* errno is set for us */
1432178479Sjb		}
1433178479Sjb
1434178479Sjb		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1435178479Sjb			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1436178479Sjb			    len, &width) == -1)
1437178479Sjb				return (-1); /* errno is set for us */
1438178479Sjb			pfd->pfd_dynwidth = width;
1439178479Sjb		} else {
1440178479Sjb			pfd->pfd_dynwidth = 0;
1441178479Sjb		}
1442178479Sjb
1443178479Sjb		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1444178479Sjb		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
1445178479Sjb			return (-1); /* errno is set for us */
1446178479Sjb
1447178479Sjb		if (pfd->pfd_flags & DT_PFCONV_AGG) {
1448178479Sjb			/*
1449178479Sjb			 * This should be impossible -- the compiler shouldn't
1450178479Sjb			 * create a DT_PFCONV_AGG conversion without an
1451178479Sjb			 * aggregation present.  Still, we'd rather fail
1452178479Sjb			 * gracefully than blow up...
1453178479Sjb			 */
1454178479Sjb			if (aggsdata == NULL)
1455178479Sjb				return (dt_set_errno(dtp, EDT_DMISMATCH));
1456178479Sjb
1457178479Sjb			aggdata = aggsdata[curagg];
1458178479Sjb			agg = aggdata->dtada_desc;
1459178479Sjb
1460178479Sjb			/*
1461178479Sjb			 * We increment the current aggregation variable, but
1462178479Sjb			 * not beyond the number of aggregation variables that
1463178479Sjb			 * we're printing. This has the (desired) effect that
1464178479Sjb			 * DT_PFCONV_AGG conversions beyond the number of
1465178479Sjb			 * aggregation variables (re-)convert the aggregation
1466178479Sjb			 * value of the last aggregation variable.
1467178479Sjb			 */
1468178479Sjb			if (curagg < naggvars - 1)
1469178479Sjb				curagg++;
1470178479Sjb
1471178479Sjb			rec = &agg->dtagd_rec[aggrec];
1472178479Sjb			addr = aggdata->dtada_data + rec->dtrd_offset;
1473178479Sjb			limit = addr + aggdata->dtada_size;
1474178479Sjb			normal = aggdata->dtada_normal;
1475178479Sjb			flags = DTRACE_BUFDATA_AGGVAL;
1476178479Sjb		} else {
1477178479Sjb			if (nrecs == 0)
1478178479Sjb				return (dt_set_errno(dtp, EDT_DMISMATCH));
1479178479Sjb
1480178479Sjb			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1481178479Sjb				/*
1482178479Sjb				 * When printing aggregation keys, we always
1483178479Sjb				 * set the aggdata to be the representative
1484178479Sjb				 * (zeroth) aggregation.  The aggdata isn't
1485178479Sjb				 * actually used here in this case, but it is
1486178479Sjb				 * passed to the buffer handler and must
1487178479Sjb				 * therefore still be correct.
1488178479Sjb				 */
1489178479Sjb				aggdata = aggsdata[0];
1490178479Sjb				flags = DTRACE_BUFDATA_AGGKEY;
1491178479Sjb			}
1492178479Sjb
1493178479Sjb			rec = recp++;
1494178479Sjb			nrecs--;
1495178479Sjb			addr = (caddr_t)buf + rec->dtrd_offset;
1496178479Sjb			limit = lim;
1497178479Sjb			normal = 1;
1498178479Sjb		}
1499178479Sjb
1500178479Sjb		size = rec->dtrd_size;
1501178479Sjb
1502178479Sjb		if (addr + size > limit) {
1503178479Sjb			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1504178479Sjb			    (void *)addr, rec->dtrd_size, (void *)lim);
1505178479Sjb			return (dt_set_errno(dtp, EDT_DOFFSET));
1506178479Sjb		}
1507178479Sjb
1508178479Sjb		if (rec->dtrd_alignment != 0 &&
1509178479Sjb		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1510178479Sjb			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1511178479Sjb			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1512178479Sjb			return (dt_set_errno(dtp, EDT_DALIGN));
1513178479Sjb		}
1514178479Sjb
1515178479Sjb		switch (rec->dtrd_action) {
1516178479Sjb		case DTRACEAGG_AVG:
1517178479Sjb			func = pfprint_average;
1518178479Sjb			break;
1519210767Srpaulo		case DTRACEAGG_STDDEV:
1520210767Srpaulo			func = pfprint_stddev;
1521210767Srpaulo			break;
1522178479Sjb		case DTRACEAGG_QUANTIZE:
1523178479Sjb			func = pfprint_quantize;
1524178479Sjb			break;
1525178479Sjb		case DTRACEAGG_LQUANTIZE:
1526178479Sjb			func = pfprint_lquantize;
1527178479Sjb			break;
1528237624Spfg		case DTRACEAGG_LLQUANTIZE:
1529237624Spfg			func = pfprint_llquantize;
1530237624Spfg			break;
1531178479Sjb		case DTRACEACT_MOD:
1532178479Sjb			func = pfprint_mod;
1533178479Sjb			break;
1534178479Sjb		case DTRACEACT_UMOD:
1535178479Sjb			func = pfprint_umod;
1536178479Sjb			break;
1537178479Sjb		default:
1538178479Sjb			func = pfc->pfc_print;
1539178479Sjb			break;
1540178479Sjb		}
1541178479Sjb
1542287226Smarkj		start = f;
1543178479Sjb		if (pfd->pfd_flags & DT_PFCONV_ALT)
1544178479Sjb			*f++ = '#';
1545178479Sjb		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1546178479Sjb			*f++ = '0';
1547178479Sjb		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1548178479Sjb			*f++ = '-';
1549178479Sjb		if (pfd->pfd_flags & DT_PFCONV_SPOS)
1550178479Sjb			*f++ = '+';
1551178479Sjb		if (pfd->pfd_flags & DT_PFCONV_GROUP)
1552178479Sjb			*f++ = '\'';
1553178479Sjb		if (pfd->pfd_flags & DT_PFCONV_SPACE)
1554178479Sjb			*f++ = ' ';
1555287226Smarkj		fmtsz -= f - start;
1556178479Sjb
1557178479Sjb		/*
1558178479Sjb		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1559178479Sjb		 * don't add the width to the format string.  See the block
1560178479Sjb		 * comment in pfprint_stack() for a description of the
1561178479Sjb		 * behavior in this case.
1562178479Sjb		 */
1563178479Sjb		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1564178479Sjb			width = 0;
1565178479Sjb
1566287226Smarkj		if (width != 0) {
1567287226Smarkj			ret = snprintf(f, fmtsz, "%d", ABS(width));
1568287226Smarkj			f += ret;
1569287226Smarkj			fmtsz = MAX(0, fmtsz - ret);
1570287226Smarkj		}
1571178479Sjb
1572287226Smarkj		if (prec > 0) {
1573287226Smarkj			ret = snprintf(f, fmtsz, ".%d", prec);
1574287226Smarkj			f += ret;
1575287226Smarkj			fmtsz = MAX(0, fmtsz - ret);
1576287226Smarkj		}
1577178479Sjb
1578287226Smarkj		if (strlcpy(f, pfd->pfd_fmt, fmtsz) >= fmtsz)
1579287226Smarkj			return (dt_set_errno(dtp, EDT_COMPILER));
1580178479Sjb		pfd->pfd_rec = rec;
1581178479Sjb
1582178479Sjb		if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1583178479Sjb			return (-1); /* errno is set for us */
1584178479Sjb
1585178479Sjb		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1586178479Sjb			/*
1587178479Sjb			 * For printa(), we flush the buffer after each tuple
1588178479Sjb			 * element, inidicating that this is the last record
1589178479Sjb			 * as appropriate.
1590178479Sjb			 */
1591178479Sjb			if (i == pfv->pfv_argc - 1)
1592178479Sjb				flags |= DTRACE_BUFDATA_AGGLAST;
1593178479Sjb
1594178479Sjb			if (dt_buffered_flush(dtp, NULL,
1595178479Sjb			    rec, aggdata, flags) < 0)
1596178479Sjb				return (-1);
1597178479Sjb		}
1598178479Sjb	}
1599178479Sjb
1600178479Sjb	return ((int)(recp - recs));
1601178479Sjb}
1602178479Sjb
1603178479Sjbint
1604178479Sjbdtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1605178479Sjb    const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1606178479Sjb{
1607178479Sjb	dtrace_optval_t size;
1608178479Sjb	int rval;
1609178479Sjb
1610178479Sjb	rval = dtrace_getopt(dtp, "strsize", &size);
1611178479Sjb	assert(rval == 0);
1612178479Sjb	assert(dtp->dt_sprintf_buflen == 0);
1613178479Sjb
1614178479Sjb	if (dtp->dt_sprintf_buf != NULL)
1615178479Sjb		free(dtp->dt_sprintf_buf);
1616178479Sjb
1617178479Sjb	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1618178479Sjb		return (dt_set_errno(dtp, EDT_NOMEM));
1619178479Sjb
1620178479Sjb	bzero(dtp->dt_sprintf_buf, size);
1621178479Sjb	dtp->dt_sprintf_buflen = size;
1622178479Sjb	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1623178479Sjb	    NULL, 0);
1624178479Sjb	dtp->dt_sprintf_buflen = 0;
1625178479Sjb
1626178479Sjb	if (rval == -1)
1627178479Sjb		free(dtp->dt_sprintf_buf);
1628178479Sjb
1629178479Sjb	return (rval);
1630178479Sjb}
1631178479Sjb
1632178479Sjb/*ARGSUSED*/
1633178479Sjbint
1634178479Sjbdtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1635178479Sjb    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1636178479Sjb    uint_t nrecs, const void *buf, size_t len)
1637178479Sjb{
1638178479Sjb	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1639178479Sjb
1640178479Sjb	if (rval == -1)
1641178479Sjb		return (rval);
1642178479Sjb
1643178479Sjb	/*
1644178479Sjb	 * Before we execute the specified command, flush fp to assure that
1645178479Sjb	 * any prior dt_printf()'s appear before the output of the command
1646178479Sjb	 * not after it.
1647178479Sjb	 */
1648178479Sjb	(void) fflush(fp);
1649178479Sjb
1650178479Sjb	if (system(dtp->dt_sprintf_buf) == -1)
1651178479Sjb		return (dt_set_errno(dtp, errno));
1652178479Sjb
1653178479Sjb	return (rval);
1654178479Sjb}
1655178479Sjb
1656178479Sjbint
1657178479Sjbdtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1658178479Sjb    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1659178479Sjb    uint_t nrecs, const void *buf, size_t len)
1660178479Sjb{
1661178479Sjb	char selfbuf[40], restorebuf[40], *filename;
1662178479Sjb	FILE *nfp;
1663178479Sjb	int rval, errval;
1664178479Sjb	dt_pfargv_t *pfv = fmtdata;
1665178479Sjb	dt_pfargd_t *pfd = pfv->pfv_argv;
1666178479Sjb
1667178479Sjb	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1668178479Sjb
1669178479Sjb	if (rval == -1 || fp == NULL)
1670178479Sjb		return (rval);
1671178479Sjb
1672297077Smav#ifdef illumos
1673178479Sjb	if (pfd->pfd_preflen != 0 &&
1674178479Sjb	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1675178479Sjb		/*
1676178479Sjb		 * The only way to have the format string set to the value
1677178479Sjb		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1678178479Sjb		 * denoting that we should restore the old stdout.
1679178479Sjb		 */
1680178479Sjb		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1681178479Sjb
1682178479Sjb		if (dtp->dt_stdout_fd == -1) {
1683178479Sjb			/*
1684178479Sjb			 * We could complain here by generating an error,
1685178479Sjb			 * but it seems like overkill:  it seems that calling
1686178479Sjb			 * freopen() to restore stdout when freopen() has
1687178479Sjb			 * never before been called should just be a no-op,
1688178479Sjb			 * so we just return in this case.
1689178479Sjb			 */
1690178479Sjb			return (rval);
1691178479Sjb		}
1692178479Sjb
1693178479Sjb		(void) snprintf(restorebuf, sizeof (restorebuf),
1694178479Sjb		    "/dev/fd/%d", dtp->dt_stdout_fd);
1695178479Sjb		filename = restorebuf;
1696178479Sjb	} else {
1697178479Sjb		filename = dtp->dt_sprintf_buf;
1698178479Sjb	}
1699178479Sjb
1700178479Sjb	/*
1701178479Sjb	 * freopen(3C) will always close the specified stream and underlying
1702178479Sjb	 * file descriptor -- even if the specified file can't be opened.
1703178479Sjb	 * Even for the semantic cesspool that is standard I/O, this is
1704178479Sjb	 * surprisingly brain-dead behavior:  it means that any failure to
1705178479Sjb	 * open the specified file destroys the specified stream in the
1706178479Sjb	 * process -- which is particularly relevant when the specified stream
1707178479Sjb	 * happens (or rather, happened) to be stdout.  This could be resolved
1708178479Sjb	 * were there an "fdreopen()" equivalent of freopen() that allowed one
1709178479Sjb	 * to pass a file descriptor instead of the name of a file, but there
1710178479Sjb	 * is no such thing.  However, we can effect this ourselves by first
1711178479Sjb	 * fopen()'ing the desired file, and then (assuming that that works),
1712178479Sjb	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1713178479Sjb	 * file descriptor for the fopen()'d file.  This way, if the fopen()
1714178479Sjb	 * fails, we can fail the operation without destroying stdout.
1715178479Sjb	 */
1716178479Sjb	if ((nfp = fopen(filename, "aF")) == NULL) {
1717178552Sjb		char *msg = strerror(errno);
1718178552Sjb		char *faultstr;
1719178479Sjb		int len = 80;
1720178479Sjb
1721178479Sjb		len += strlen(msg) + strlen(filename);
1722178479Sjb		faultstr = alloca(len);
1723178479Sjb
1724178479Sjb		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1725178479Sjb		    filename, strerror(errno));
1726178479Sjb
1727178479Sjb		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1728178479Sjb			return (rval);
1729178479Sjb
1730178479Sjb		return (errval);
1731178479Sjb	}
1732178479Sjb
1733178479Sjb	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1734178479Sjb
1735178479Sjb	if (dtp->dt_stdout_fd == -1) {
1736178479Sjb		/*
1737178479Sjb		 * If this is the first time that we're calling freopen(),
1738178479Sjb		 * we're going to stash away the file descriptor for stdout.
1739178479Sjb		 * We don't expect the dup(2) to fail, so if it does we must
1740178479Sjb		 * return failure.
1741178479Sjb		 */
1742178479Sjb		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1743178479Sjb			(void) fclose(nfp);
1744178479Sjb			return (dt_set_errno(dtp, errno));
1745178479Sjb		}
1746178479Sjb	}
1747178479Sjb
1748178479Sjb	if (freopen(selfbuf, "aF", fp) == NULL) {
1749178479Sjb		(void) fclose(nfp);
1750178479Sjb		return (dt_set_errno(dtp, errno));
1751178479Sjb	}
1752178479Sjb
1753178479Sjb	(void) fclose(nfp);
1754297077Smav#else	/* !illumos */
1755178552Sjb	/*
1756178552Sjb	 * The 'standard output' (which is not necessarily stdout)
1757178552Sjb	 * treatment on FreeBSD is implemented differently than on
1758178552Sjb	 * Solaris because FreeBSD's freopen() will attempt to re-use
1759178552Sjb	 * the current file descriptor, causing the previous file to
1760178552Sjb	 * be closed and thereby preventing it from be re-activated
1761178552Sjb	 * later.
1762178552Sjb	 *
1763178552Sjb	 * For FreeBSD we use the concept of setting an output file
1764178552Sjb	 * pointer in the DTrace handle if a dtrace_freopen() has
1765178552Sjb	 * enabled another output file and we leave the caller's
1766178552Sjb	 * file pointer untouched. If it was actually stdout, then
1767178552Sjb	 * stdout remains open. If it was another file, then that
1768178552Sjb	 * file remains open. While a dtrace_freopen() has activated
1769178552Sjb	 * another file, we keep a pointer to that which we use in
1770178552Sjb	 * the output functions by preference and only use the caller's
1771178552Sjb	 * file pointer if no dtrace_freopen() call has been made.
1772178552Sjb	 *
1773178552Sjb	 * The check to see if we're re-activating the caller's
1774178552Sjb	 * output file is much the same as on Solaris.
1775178552Sjb	 */
1776178552Sjb	if (pfd->pfd_preflen != 0 &&
1777178552Sjb	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1778178552Sjb		/*
1779178552Sjb		 * The only way to have the format string set to the value
1780178552Sjb		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1781178552Sjb		 * denoting that we should restore the old stdout.
1782178552Sjb		 */
1783178552Sjb		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1784178479Sjb
1785178552Sjb		if (dtp->dt_freopen_fp == NULL) {
1786178552Sjb			/*
1787178552Sjb			 * We could complain here by generating an error,
1788178552Sjb			 * but it seems like overkill:  it seems that calling
1789178552Sjb			 * freopen() to restore stdout when freopen() has
1790178552Sjb			 * never before been called should just be a no-op,
1791178552Sjb			 * so we just return in this case.
1792178552Sjb			 */
1793178552Sjb			return (rval);
1794178552Sjb		}
1795178552Sjb
1796178552Sjb		/*
1797178552Sjb		 * At this point, to re-active the original output file,
1798178552Sjb		 * on FreeBSD we only code the current file that this
1799178552Sjb		 * function opened previously.
1800178552Sjb		 */
1801178552Sjb		(void) fclose(dtp->dt_freopen_fp);
1802178552Sjb		dtp->dt_freopen_fp = NULL;
1803178552Sjb
1804178552Sjb		return (rval);
1805178552Sjb	}
1806178552Sjb
1807178552Sjb	if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1808178552Sjb		char *msg = strerror(errno);
1809178552Sjb		char *faultstr;
1810178552Sjb		int len = 80;
1811178552Sjb
1812178552Sjb		len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1813178552Sjb		faultstr = alloca(len);
1814178552Sjb
1815178552Sjb		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1816178552Sjb		    dtp->dt_sprintf_buf, strerror(errno));
1817178552Sjb
1818178552Sjb		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1819178552Sjb			return (rval);
1820178552Sjb
1821178552Sjb		return (errval);
1822178552Sjb	}
1823178552Sjb
1824178552Sjb	if (dtp->dt_freopen_fp != NULL)
1825178552Sjb		(void) fclose(dtp->dt_freopen_fp);
1826178552Sjb
1827178552Sjb	/* Remember that the output has been redirected to the new file. */
1828178552Sjb	dtp->dt_freopen_fp = nfp;
1829297077Smav#endif	/* illumos */
1830178552Sjb
1831178479Sjb	return (rval);
1832178479Sjb}
1833178479Sjb
1834178479Sjb/*ARGSUSED*/
1835178479Sjbint
1836178479Sjbdtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1837178479Sjb    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1838178479Sjb    uint_t nrecs, const void *buf, size_t len)
1839178479Sjb{
1840178479Sjb	return (dt_printf_format(dtp, fp, fmtdata,
1841178479Sjb	    recp, nrecs, buf, len, NULL, 0));
1842178479Sjb}
1843178479Sjb
1844178479Sjbvoid *
1845178479Sjbdtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1846178479Sjb{
1847178479Sjb	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1848178479Sjb	dt_pfargd_t *pfd;
1849178479Sjb	int i;
1850178479Sjb
1851178479Sjb	if (pfv == NULL)
1852178479Sjb		return (NULL);		/* errno has been set for us */
1853178479Sjb
1854178479Sjb	pfd = pfv->pfv_argv;
1855178479Sjb
1856178479Sjb	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1857178479Sjb		const dt_pfconv_t *pfc = pfd->pfd_conv;
1858178479Sjb
1859178479Sjb		if (pfc == NULL)
1860178479Sjb			continue;
1861178479Sjb
1862178479Sjb		/*
1863178479Sjb		 * If the output format is not %s then we assume that we have
1864178479Sjb		 * been given a correctly-sized format string, so we copy the
1865178479Sjb		 * true format name including the size modifier.  If the output
1866178479Sjb		 * format is %s, then either the input format is %s as well or
1867178479Sjb		 * it is one of our custom formats (e.g. pfprint_addr), so we
1868178479Sjb		 * must set pfd_fmt to be the output format conversion "s".
1869178479Sjb		 */
1870178479Sjb		if (strcmp(pfc->pfc_ofmt, "s") != 0)
1871178479Sjb			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1872178479Sjb		else
1873178479Sjb			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1874178479Sjb	}
1875178479Sjb
1876178479Sjb	return (pfv);
1877178479Sjb}
1878178479Sjb
1879178479Sjbvoid *
1880178479Sjbdtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1881178479Sjb{
1882178479Sjb	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1883178479Sjb
1884178479Sjb	if (pfv == NULL)
1885178479Sjb		return (NULL);		/* errno has been set for us */
1886178479Sjb
1887178479Sjb	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1888178479Sjb
1889178479Sjb	return (pfv);
1890178479Sjb}
1891178479Sjb
1892178479Sjb/*ARGSUSED*/
1893178479Sjbsize_t
1894178479Sjbdtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1895178479Sjb{
1896178479Sjb	dt_pfargv_t *pfv = fmtdata;
1897178479Sjb	dt_pfargd_t *pfd = pfv->pfv_argv;
1898178479Sjb
1899178479Sjb	/*
1900178479Sjb	 * An upper bound on the string length is the length of the original
1901178479Sjb	 * format string, plus three times the number of conversions (each
1902178479Sjb	 * conversion could add up an additional "ll" and/or pfd_width digit
1903178479Sjb	 * in the case of converting %? to %16) plus one for a terminating \0.
1904178479Sjb	 */
1905178479Sjb	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1906178479Sjb	char *format = alloca(formatlen);
1907178479Sjb	char *f = format;
1908178479Sjb	int i, j;
1909178479Sjb
1910178479Sjb	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1911178479Sjb		const dt_pfconv_t *pfc = pfd->pfd_conv;
1912178479Sjb		const char *str;
1913178479Sjb		int width = pfd->pfd_width;
1914178479Sjb		int prec = pfd->pfd_prec;
1915178479Sjb
1916178479Sjb		if (pfd->pfd_preflen != 0) {
1917178479Sjb			for (j = 0; j < pfd->pfd_preflen; j++)
1918178479Sjb				*f++ = pfd->pfd_prefix[j];
1919178479Sjb		}
1920178479Sjb
1921178479Sjb		if (pfc == NULL)
1922178479Sjb			continue;
1923178479Sjb
1924178479Sjb		*f++ = '%';
1925178479Sjb
1926178479Sjb		if (pfd->pfd_flags & DT_PFCONV_ALT)
1927178479Sjb			*f++ = '#';
1928178479Sjb		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1929178479Sjb			*f++ = '0';
1930178479Sjb		if (pfd->pfd_flags & DT_PFCONV_LEFT)
1931178479Sjb			*f++ = '-';
1932178479Sjb		if (pfd->pfd_flags & DT_PFCONV_SPOS)
1933178479Sjb			*f++ = '+';
1934178479Sjb		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1935178479Sjb			*f++ = '*';
1936178479Sjb		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1937178479Sjb			*f++ = '.';
1938178479Sjb			*f++ = '*';
1939178479Sjb		}
1940178479Sjb		if (pfd->pfd_flags & DT_PFCONV_GROUP)
1941178479Sjb			*f++ = '\'';
1942178479Sjb		if (pfd->pfd_flags & DT_PFCONV_SPACE)
1943178479Sjb			*f++ = ' ';
1944178479Sjb		if (pfd->pfd_flags & DT_PFCONV_AGG)
1945178479Sjb			*f++ = '@';
1946178479Sjb
1947178479Sjb		if (width != 0)
1948178479Sjb			f += snprintf(f, sizeof (format), "%d", width);
1949178479Sjb
1950178479Sjb		if (prec != 0)
1951178479Sjb			f += snprintf(f, sizeof (format), ".%d", prec);
1952178479Sjb
1953178479Sjb		/*
1954178479Sjb		 * If the output format is %s, then either %s is the underlying
1955178479Sjb		 * conversion or the conversion is one of our customized ones,
1956178479Sjb		 * e.g. pfprint_addr.  In these cases, put the original string
1957178479Sjb		 * name of the conversion (pfc_name) into the pickled format
1958178479Sjb		 * string rather than the derived conversion (pfd_fmt).
1959178479Sjb		 */
1960178479Sjb		if (strcmp(pfc->pfc_ofmt, "s") == 0)
1961178479Sjb			str = pfc->pfc_name;
1962178479Sjb		else
1963178479Sjb			str = pfd->pfd_fmt;
1964178479Sjb
1965178479Sjb		for (j = 0; str[j] != '\0'; j++)
1966178479Sjb			*f++ = str[j];
1967178479Sjb	}
1968178479Sjb
1969178479Sjb	*f = '\0'; /* insert nul byte; do not count in return value */
1970178479Sjb
1971178479Sjb	assert(f < format + formatlen);
1972178479Sjb	(void) strncpy(s, format, len);
1973178479Sjb
1974178479Sjb	return ((size_t)(f - format));
1975178479Sjb}
1976178479Sjb
1977178479Sjbstatic int
1978178479Sjbdt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1979178479Sjb{
1980178479Sjb	const dtrace_aggdesc_t *agg = adp->dtada_desc;
1981178479Sjb	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1982178479Sjb	uint_t nrecs = agg->dtagd_nrecs;
1983178479Sjb	dt_pfwalk_t *pfw = arg;
1984178479Sjb	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1985178479Sjb	int id;
1986178479Sjb
1987178479Sjb	if (dt_printf_getint(dtp, recp++, nrecs--,
1988178479Sjb	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1989178479Sjb		return (0); /* no aggregation id or id does not match */
1990178479Sjb
1991178479Sjb	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1992178479Sjb	    recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1993178479Sjb		return (pfw->pfw_err = dtp->dt_errno);
1994178479Sjb
1995178479Sjb	/*
1996178479Sjb	 * Cast away the const to set the bit indicating that this aggregation
1997178479Sjb	 * has been printed.
1998178479Sjb	 */
1999178479Sjb	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2000178479Sjb
2001178479Sjb	return (0);
2002178479Sjb}
2003178479Sjb
2004178479Sjbstatic int
2005178479Sjbdt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
2006178479Sjb{
2007178479Sjb	const dtrace_aggdata_t *aggdata = aggsdata[0];
2008178479Sjb	const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2009178479Sjb	const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
2010178479Sjb	uint_t nrecs = agg->dtagd_nrecs - 1;
2011178479Sjb	dt_pfwalk_t *pfw = arg;
2012178479Sjb	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
2013178479Sjb	int i;
2014178479Sjb
2015178479Sjb	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
2016178479Sjb	    rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
2017178479Sjb	    aggsdata, naggvars) == -1)
2018178479Sjb		return (pfw->pfw_err = dtp->dt_errno);
2019178479Sjb
2020178479Sjb	/*
2021178479Sjb	 * For each aggregation, indicate that it has been printed, casting
2022178479Sjb	 * away the const as necessary.
2023178479Sjb	 */
2024178479Sjb	for (i = 1; i < naggvars; i++) {
2025178479Sjb		agg = aggsdata[i]->dtada_desc;
2026178479Sjb		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2027178479Sjb	}
2028178479Sjb
2029178479Sjb	return (0);
2030178479Sjb}
2031178479Sjb/*ARGSUSED*/
2032178479Sjbint
2033178479Sjbdtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2034178479Sjb    const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2035178479Sjb    uint_t nrecs, const void *buf, size_t len)
2036178479Sjb{
2037178479Sjb	dt_pfwalk_t pfw;
2038178479Sjb	int i, naggvars = 0;
2039178479Sjb	dtrace_aggvarid_t *aggvars;
2040178479Sjb
2041178479Sjb	aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2042178479Sjb
2043178479Sjb	/*
2044178479Sjb	 * This might be a printa() with multiple aggregation variables.  We
2045178479Sjb	 * need to scan forward through the records until we find a record from
2046178479Sjb	 * a different statement.
2047178479Sjb	 */
2048178479Sjb	for (i = 0; i < nrecs; i++) {
2049178479Sjb		const dtrace_recdesc_t *nrec = &recs[i];
2050178479Sjb
2051178479Sjb		if (nrec->dtrd_uarg != recs->dtrd_uarg)
2052178479Sjb			break;
2053178479Sjb
2054178479Sjb		if (nrec->dtrd_action != recs->dtrd_action)
2055178479Sjb			return (dt_set_errno(dtp, EDT_BADAGG));
2056178479Sjb
2057178479Sjb		aggvars[naggvars++] =
2058178479Sjb		    /* LINTED - alignment */
2059178479Sjb		    *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2060178479Sjb	}
2061178479Sjb
2062178479Sjb	if (naggvars == 0)
2063178479Sjb		return (dt_set_errno(dtp, EDT_BADAGG));
2064178479Sjb
2065178479Sjb	pfw.pfw_argv = fmtdata;
2066178479Sjb	pfw.pfw_fp = fp;
2067178479Sjb	pfw.pfw_err = 0;
2068178479Sjb
2069178479Sjb	if (naggvars == 1) {
2070178479Sjb		pfw.pfw_aid = aggvars[0];
2071178479Sjb
2072178479Sjb		if (dtrace_aggregate_walk_sorted(dtp,
2073178479Sjb		    dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2074178479Sjb			return (-1); /* errno is set for us */
2075178479Sjb	} else {
2076178479Sjb		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2077178479Sjb		    dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2078178479Sjb			return (-1); /* errno is set for us */
2079178479Sjb	}
2080178479Sjb
2081178479Sjb	return (i);
2082178479Sjb}
2083