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