xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/common/dt_printf.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #if defined(sun)
28 #include <sys/sysmacros.h>
29 #else
30 #define	ABS(a)		((a) < 0 ? -(a) : (a))
31 #endif
32 #include <string.h>
33 #include <strings.h>
34 #include <stdlib.h>
35 #if defined(sun)
36 #include <alloca.h>
37 #endif
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <limits.h>
42 
43 #include <dt_printf.h>
44 #include <dt_string.h>
45 #include <dt_impl.h>
46 
47 /*ARGSUSED*/
48 static int
49 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
50 {
51 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
52 }
53 
54 /*ARGSUSED*/
55 static int
56 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
57 {
58 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
59 	    dt_node_is_symaddr(dnp));
60 }
61 
62 /*ARGSUSED*/
63 static int
64 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
65 {
66 	dtrace_hdl_t *dtp = pfv->pfv_dtp;
67 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
68 
69 	if (dt_node_is_usymaddr(dnp))
70 		return (1);
71 
72 	if (idp == NULL || idp->di_id == 0)
73 		return (0);
74 
75 	return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
76 }
77 
78 /*ARGSUSED*/
79 static int
80 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
81 {
82 	return (dt_node_is_stack(dnp));
83 }
84 
85 /*ARGSUSED*/
86 static int
87 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
88 {
89 	return (dt_node_is_integer(dnp) &&
90 	    dt_node_type_size(dnp) == sizeof (uint64_t));
91 }
92 
93 /*ARGSUSED*/
94 static int
95 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
96 {
97 	ctf_file_t *ctfp;
98 	ctf_encoding_t e;
99 	ctf_arinfo_t r;
100 	ctf_id_t base;
101 	uint_t kind;
102 
103 	if (dt_node_is_string(dnp))
104 		return (1);
105 
106 	ctfp = dnp->dn_ctfp;
107 	base = ctf_type_resolve(ctfp, dnp->dn_type);
108 	kind = ctf_type_kind(ctfp, base);
109 
110 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
111 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
112 	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
113 }
114 
115 /*ARGSUSED*/
116 static int
117 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
118 {
119 	ctf_file_t *ctfp = dnp->dn_ctfp;
120 	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
121 	uint_t kind = ctf_type_kind(ctfp, base);
122 
123 	ctf_encoding_t e;
124 	ctf_arinfo_t r;
125 
126 	return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
127 	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
128 	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
129 	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
130 }
131 
132 /*ARGSUSED*/
133 static int
134 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
135 {
136 	return (dt_node_is_integer(dnp) &&
137 	    dt_node_type_size(dnp) <= sizeof (int));
138 }
139 
140 /*ARGSUSED*/
141 static int
142 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
143 {
144 	return (dt_node_is_float(dnp));
145 }
146 
147 /*ARGSUSED*/
148 static int
149 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
150 {
151 	return (dt_node_is_integer(dnp));
152 }
153 
154 /*ARGSUSED*/
155 static int
156 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
157 {
158 	if (dnp->dn_flags & DT_NF_SIGNED)
159 		pfd->pfd_flags |= DT_PFCONV_SIGNED;
160 	else
161 		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
162 
163 	return (dt_node_is_integer(dnp));
164 }
165 
166 /*ARGSUSED*/
167 static int
168 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
169 {
170 	ctf_file_t *ctfp = dnp->dn_ctfp;
171 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
172 	char n[DT_TYPE_NAMELEN];
173 
174 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
175 	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
176 	    strcmp(n, "unsigned short") == 0));
177 }
178 
179 /*ARGSUSED*/
180 static int
181 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
182 {
183 	ctf_file_t *ctfp = dnp->dn_ctfp;
184 	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
185 	char n[DT_TYPE_NAMELEN];
186 
187 	return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
188 	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
189 	    strcmp(n, "unsigned long") == 0));
190 }
191 
192 /*ARGSUSED*/
193 static int
194 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
195 {
196 	ctf_file_t *ctfp = dnp->dn_ctfp;
197 	ctf_id_t type = dnp->dn_type;
198 	char n[DT_TYPE_NAMELEN];
199 
200 	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
201 	    sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
202 	    strcmp(n, "signed long long") == 0 ||
203 	    strcmp(n, "unsigned long long") == 0))
204 		return (1);
205 
206 	/*
207 	 * If the type used for %llx or %llX is not an [unsigned] long long, we
208 	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
209 	 * that these typedefs are guaranteed to work with %ll[xX] in either
210 	 * compilation environment even though they alias to "long" in LP64.
211 	 */
212 	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
213 		if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
214 		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
215 			return (1);
216 
217 		type = ctf_type_reference(ctfp, type);
218 	}
219 
220 	return (0);
221 }
222 
223 /*ARGSUSED*/
224 static int
225 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
226 {
227 	return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
228 	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
229 }
230 
231 /*ARGSUSED*/
232 static int
233 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
234     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
235 {
236 	int64_t normal = (int64_t)unormal;
237 	int32_t n = (int32_t)normal;
238 
239 	switch (size) {
240 	case sizeof (int8_t):
241 		return (dt_printf(dtp, fp, format,
242 		    (int32_t)*((int8_t *)addr) / n));
243 	case sizeof (int16_t):
244 		return (dt_printf(dtp, fp, format,
245 		    (int32_t)*((int16_t *)addr) / n));
246 	case sizeof (int32_t):
247 		return (dt_printf(dtp, fp, format,
248 		    *((int32_t *)addr) / n));
249 	case sizeof (int64_t):
250 		return (dt_printf(dtp, fp, format,
251 		    *((int64_t *)addr) / normal));
252 	default:
253 		return (dt_set_errno(dtp, EDT_DMISMATCH));
254 	}
255 }
256 
257 /*ARGSUSED*/
258 static int
259 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
260     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
261 {
262 	uint32_t n = (uint32_t)normal;
263 
264 	switch (size) {
265 	case sizeof (uint8_t):
266 		return (dt_printf(dtp, fp, format,
267 		    (uint32_t)*((uint8_t *)addr) / n));
268 	case sizeof (uint16_t):
269 		return (dt_printf(dtp, fp, format,
270 		    (uint32_t)*((uint16_t *)addr) / n));
271 	case sizeof (uint32_t):
272 		return (dt_printf(dtp, fp, format,
273 		    *((uint32_t *)addr) / n));
274 	case sizeof (uint64_t):
275 		return (dt_printf(dtp, fp, format,
276 		    *((uint64_t *)addr) / normal));
277 	default:
278 		return (dt_set_errno(dtp, EDT_DMISMATCH));
279 	}
280 }
281 
282 static int
283 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
284     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
285 {
286 	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
287 		return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
288 	else
289 		return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
290 }
291 
292 /*ARGSUSED*/
293 static int
294 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
295     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
296 {
297 	double n = (double)normal;
298 	long double ldn = (long double)normal;
299 
300 	switch (size) {
301 	case sizeof (float):
302 		return (dt_printf(dtp, fp, format,
303 		    (double)*((float *)addr) / n));
304 	case sizeof (double):
305 		return (dt_printf(dtp, fp, format,
306 		    *((double *)addr) / n));
307 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
308 	case sizeof (long double):
309 		return (dt_printf(dtp, fp, format,
310 		    *((long double *)addr) / ldn));
311 #endif
312 	default:
313 		return (dt_set_errno(dtp, EDT_DMISMATCH));
314 	}
315 }
316 
317 /*ARGSUSED*/
318 static int
319 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
320     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
321 {
322 	char *s;
323 	int n, len = 256;
324 	uint64_t val;
325 
326 	switch (size) {
327 	case sizeof (uint32_t):
328 		val = *((uint32_t *)addr);
329 		break;
330 	case sizeof (uint64_t):
331 		val = *((uint64_t *)addr);
332 		break;
333 	default:
334 		return (dt_set_errno(dtp, EDT_DMISMATCH));
335 	}
336 
337 	do {
338 		n = len;
339 		s = alloca(n);
340 	} while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
341 
342 	return (dt_printf(dtp, fp, format, s));
343 }
344 
345 /*ARGSUSED*/
346 static int
347 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
348     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
349 {
350 	return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
351 }
352 
353 /*ARGSUSED*/
354 static int
355 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
356     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
357 {
358 	return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
359 }
360 
361 /*ARGSUSED*/
362 static int
363 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
364     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
365 {
366 	char *s;
367 	int n, len = 256;
368 	uint64_t val, pid = 0;
369 
370 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
371 
372 	switch (size) {
373 	case sizeof (uint32_t):
374 		val = (u_longlong_t)*((uint32_t *)addr);
375 		break;
376 	case sizeof (uint64_t):
377 		val = (u_longlong_t)*((uint64_t *)addr);
378 		break;
379 	case sizeof (uint64_t) * 2:
380 		pid = ((uint64_t *)(uintptr_t)addr)[0];
381 		val = ((uint64_t *)(uintptr_t)addr)[1];
382 		break;
383 	default:
384 		return (dt_set_errno(dtp, EDT_DMISMATCH));
385 	}
386 
387 	if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
388 		pid = idp->di_id;
389 
390 	do {
391 		n = len;
392 		s = alloca(n);
393 	} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
394 
395 	return (dt_printf(dtp, fp, format, s));
396 }
397 
398 /*ARGSUSED*/
399 static int
400 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
401     const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
402 {
403 	int width;
404 	dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
405 	const dtrace_recdesc_t *rec = pfd->pfd_rec;
406 	caddr_t addr = (caddr_t)vaddr;
407 	int err = 0;
408 
409 	/*
410 	 * We have stashed the value of the STACKINDENT option, and we will
411 	 * now override it for the purposes of formatting the stack.  If the
412 	 * field has been specified as left-aligned (i.e. (%-#), we set the
413 	 * indentation to be the width.  This is a slightly odd semantic, but
414 	 * it's useful functionality -- and it's slightly odd to begin with to
415 	 * be using a single format specifier to be formatting multiple lines
416 	 * of text...
417 	 */
418 	if (pfd->pfd_dynwidth < 0) {
419 		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
420 		width = -pfd->pfd_dynwidth;
421 	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
422 		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
423 	} else {
424 		width = 0;
425 	}
426 
427 	dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
428 
429 	switch (rec->dtrd_action) {
430 	case DTRACEACT_USTACK:
431 	case DTRACEACT_JSTACK:
432 		err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
433 		break;
434 
435 	case DTRACEACT_STACK:
436 		err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
437 		    rec->dtrd_size / rec->dtrd_arg);
438 		break;
439 
440 	default:
441 		assert(0);
442 	}
443 
444 	dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
445 
446 	return (err);
447 }
448 
449 /*ARGSUSED*/
450 static int
451 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
452     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
453 {
454 	char src[32], buf[32], *dst = buf;
455 	hrtime_t time = *((uint64_t *)addr);
456 	time_t sec = (time_t)(time / NANOSEC);
457 	int i;
458 
459 	/*
460 	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
461 	 * Below, we turn this into the canonical adb/mdb /[yY] format,
462 	 * "1973 Dec  3 17:20:00".
463 	 */
464 #if defined(sun)
465 	(void) ctime_r(&sec, src, sizeof (src));
466 #else
467 	(void) ctime_r(&sec, src);
468 #endif
469 
470 	/*
471 	 * Place the 4-digit year at the head of the string...
472 	 */
473 	for (i = 20; i < 24; i++)
474 		*dst++ = src[i];
475 
476 	/*
477 	 * ...and follow it with the remainder (month, day, hh:mm:ss).
478 	 */
479 	for (i = 3; i < 19; i++)
480 		*dst++ = src[i];
481 
482 	*dst = '\0';
483 	return (dt_printf(dtp, fp, format, buf));
484 }
485 
486 /*
487  * This prints the time in RFC 822 standard form.  This is useful for emitting
488  * notions of time that are consumed by standard tools (e.g., as part of an
489  * RSS feed).
490  */
491 /*ARGSUSED*/
492 static int
493 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
494     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
495 {
496 	hrtime_t time = *((uint64_t *)addr);
497 	time_t sec = (time_t)(time / NANOSEC);
498 	struct tm tm;
499 	char buf[64];
500 
501 	(void) localtime_r(&sec, &tm);
502 	(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
503 	return (dt_printf(dtp, fp, format, buf));
504 }
505 
506 /*ARGSUSED*/
507 static int
508 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
509     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
510 {
511 	char *s = alloca(size + 1);
512 
513 	bcopy(addr, s, size);
514 	s[size] = '\0';
515 	return (dt_printf(dtp, fp, format, s));
516 }
517 
518 /*ARGSUSED*/
519 static int
520 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
521     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
522 {
523 	wchar_t *ws = alloca(size + sizeof (wchar_t));
524 
525 	bcopy(addr, ws, size);
526 	ws[size / sizeof (wchar_t)] = L'\0';
527 	return (dt_printf(dtp, fp, format, ws));
528 }
529 
530 /*ARGSUSED*/
531 static int
532 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
533     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
534 {
535 	char *s;
536 	int n;
537 
538 	if ((s = strchr2esc(addr, size)) == NULL)
539 		return (dt_set_errno(dtp, EDT_NOMEM));
540 
541 	n = dt_printf(dtp, fp, format, s);
542 	free(s);
543 	return (n);
544 }
545 
546 static int
547 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
548     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
549 {
550 	char c;
551 
552 	switch (size) {
553 	case sizeof (int8_t):
554 		c = *(int8_t *)addr;
555 		break;
556 	case sizeof (int16_t):
557 		c = *(int16_t *)addr;
558 		break;
559 	case sizeof (int32_t):
560 		c = *(int32_t *)addr;
561 		break;
562 	default:
563 		return (dt_set_errno(dtp, EDT_DMISMATCH));
564 	}
565 
566 	return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
567 }
568 
569 /*ARGSUSED*/
570 static int
571 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
572     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
573 {
574 	return (dt_printf(dtp, fp, "%%"));
575 }
576 
577 static const char pfproto_xint[] = "char, short, int, long, or long long";
578 static const char pfproto_csi[] = "char, short, or int";
579 static const char pfproto_fp[] = "float, double, or long double";
580 static const char pfproto_addr[] = "pointer or integer";
581 static const char pfproto_uaddr[] =
582 	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
583 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
584 static const char pfproto_wstr[] = "wchar_t []";
585 
586 /*
587  * Printf format conversion dictionary.  This table should match the set of
588  * conversions offered by printf(3C), as well as some additional extensions.
589  * The second parameter is an ASCII string which is either an actual type
590  * name we should look up (if pfcheck_type is specified), or just a descriptive
591  * string of the types expected for use in error messages.
592  */
593 static const dt_pfconv_t _dtrace_conversions[] = {
594 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
595 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
596 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
597 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
598 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
599 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
600 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
601 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
602 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
603 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
604 { "hd", "d", "short", pfcheck_type, pfprint_sint },
605 { "hi", "i", "short", pfcheck_type, pfprint_sint },
606 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
607 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
608 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
609 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
610 { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
611 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
612 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
613 { "ld",	"d", "long", pfcheck_type, pfprint_sint },
614 { "li",	"i", "long", pfcheck_type, pfprint_sint },
615 { "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
616 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
617 { "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
618 { "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
619 { "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
620 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
621 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
622 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
623 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
624 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
625 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
626 { "Le",	"e", "long double", pfcheck_type, pfprint_fp },
627 { "LE",	"E", "long double", pfcheck_type, pfprint_fp },
628 { "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
629 { "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
630 { "LG",	"G", "long double", pfcheck_type, pfprint_fp },
631 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
632 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
633 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
634 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
635 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
636 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
637 { "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
638 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
639 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
640 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
641 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
642 { "%", "%", "void", pfcheck_type, pfprint_pct },
643 { NULL, NULL, NULL, NULL, NULL }
644 };
645 
646 int
647 dt_pfdict_create(dtrace_hdl_t *dtp)
648 {
649 	uint_t n = _dtrace_strbuckets;
650 	const dt_pfconv_t *pfd;
651 	dt_pfdict_t *pdi;
652 
653 	if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
654 	    (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
655 		free(pdi);
656 		return (dt_set_errno(dtp, EDT_NOMEM));
657 	}
658 
659 	dtp->dt_pfdict = pdi;
660 	bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
661 	pdi->pdi_nbuckets = n;
662 
663 	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
664 		dtrace_typeinfo_t dtt;
665 		dt_pfconv_t *pfc;
666 		uint_t h;
667 
668 		if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
669 			dt_pfdict_destroy(dtp);
670 			return (dt_set_errno(dtp, EDT_NOMEM));
671 		}
672 
673 		bcopy(pfd, pfc, sizeof (dt_pfconv_t));
674 		h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
675 		pfc->pfc_next = pdi->pdi_buckets[h];
676 		pdi->pdi_buckets[h] = pfc;
677 
678 		dtt.dtt_ctfp = NULL;
679 		dtt.dtt_type = CTF_ERR;
680 
681 		/*
682 		 * The "D" container or its parent must contain a definition of
683 		 * any type referenced by a printf conversion.  If none can be
684 		 * found, we fail to initialize the printf dictionary.
685 		 */
686 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
687 		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
688 			dt_pfdict_destroy(dtp);
689 			return (dt_set_errno(dtp, EDT_NOCONV));
690 		}
691 
692 		pfc->pfc_dctfp = dtt.dtt_ctfp;
693 		pfc->pfc_dtype = dtt.dtt_type;
694 
695 		/*
696 		 * The "C" container may contain an alternate definition of an
697 		 * explicit conversion type.  If it does, use it; otherwise
698 		 * just set pfc_ctype to pfc_dtype so it is always valid.
699 		 */
700 		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
701 		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
702 			pfc->pfc_cctfp = dtt.dtt_ctfp;
703 			pfc->pfc_ctype = dtt.dtt_type;
704 		} else {
705 			pfc->pfc_cctfp = pfc->pfc_dctfp;
706 			pfc->pfc_ctype = pfc->pfc_dtype;
707 		}
708 
709 		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
710 		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
711 			dt_pfdict_destroy(dtp);
712 			return (dt_set_errno(dtp, EDT_BADCONV));
713 		}
714 
715 		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
716 	}
717 
718 	return (0);
719 }
720 
721 void
722 dt_pfdict_destroy(dtrace_hdl_t *dtp)
723 {
724 	dt_pfdict_t *pdi = dtp->dt_pfdict;
725 	dt_pfconv_t *pfc, *nfc;
726 	uint_t i;
727 
728 	if (pdi == NULL)
729 		return;
730 
731 	for (i = 0; i < pdi->pdi_nbuckets; i++) {
732 		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
733 			nfc = pfc->pfc_next;
734 			free(pfc);
735 		}
736 	}
737 
738 	free(pdi->pdi_buckets);
739 	free(pdi);
740 	dtp->dt_pfdict = NULL;
741 }
742 
743 static const dt_pfconv_t *
744 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
745 {
746 	dt_pfdict_t *pdi = dtp->dt_pfdict;
747 	uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
748 	const dt_pfconv_t *pfc;
749 
750 	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
751 		if (strcmp(pfc->pfc_name, name) == 0)
752 			break;
753 	}
754 
755 	return (pfc);
756 }
757 
758 static dt_pfargv_t *
759 dt_printf_error(dtrace_hdl_t *dtp, int err)
760 {
761 	if (yypcb != NULL)
762 		longjmp(yypcb->pcb_jmpbuf, err);
763 
764 	(void) dt_set_errno(dtp, err);
765 	return (NULL);
766 }
767 
768 dt_pfargv_t *
769 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
770 {
771 	dt_pfargd_t *pfd, *nfd = NULL;
772 	dt_pfargv_t *pfv;
773 	const char *p, *q;
774 	char *format;
775 
776 	if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
777 	    (format = strdup(s)) == NULL) {
778 		free(pfv);
779 		return (dt_printf_error(dtp, EDT_NOMEM));
780 	}
781 
782 	pfv->pfv_format = format;
783 	pfv->pfv_argv = NULL;
784 	pfv->pfv_argc = 0;
785 	pfv->pfv_flags = 0;
786 	pfv->pfv_dtp = dtp;
787 
788 	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
789 		uint_t namelen = 0;
790 		int digits = 0;
791 		int dot = 0;
792 
793 		char name[8];
794 		char c;
795 		int n;
796 
797 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
798 			dt_printf_destroy(pfv);
799 			return (dt_printf_error(dtp, EDT_NOMEM));
800 		}
801 
802 		if (pfv->pfv_argv != NULL)
803 			nfd->pfd_next = pfd;
804 		else
805 			pfv->pfv_argv = pfd;
806 
807 		bzero(pfd, sizeof (dt_pfargd_t));
808 		pfv->pfv_argc++;
809 		nfd = pfd;
810 
811 		if (p > q) {
812 			pfd->pfd_preflen = (size_t)(p - q);
813 			pfd->pfd_prefix = q;
814 		}
815 
816 		fmt_switch:
817 		switch (c = *++p) {
818 		case '0': case '1': case '2': case '3': case '4':
819 		case '5': case '6': case '7': case '8': case '9':
820 			if (dot == 0 && digits == 0 && c == '0') {
821 				pfd->pfd_flags |= DT_PFCONV_ZPAD;
822 				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
823 				goto fmt_switch;
824 			}
825 
826 			for (n = 0; isdigit(c); c = *++p)
827 				n = n * 10 + c - '0';
828 
829 			if (dot)
830 				pfd->pfd_prec = n;
831 			else
832 				pfd->pfd_width = n;
833 
834 			p--;
835 			digits++;
836 			goto fmt_switch;
837 
838 		case '#':
839 			pfd->pfd_flags |= DT_PFCONV_ALT;
840 			goto fmt_switch;
841 
842 		case '*':
843 			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
844 
845 			if (pfd->pfd_flags & n) {
846 				yywarn("format conversion #%u has more than "
847 				    "one '*' specified for the output %s\n",
848 				    pfv->pfv_argc, n ? "precision" : "width");
849 
850 				dt_printf_destroy(pfv);
851 				return (dt_printf_error(dtp, EDT_COMPILER));
852 			}
853 
854 			pfd->pfd_flags |= n;
855 			goto fmt_switch;
856 
857 		case '+':
858 			pfd->pfd_flags |= DT_PFCONV_SPOS;
859 			goto fmt_switch;
860 
861 		case '-':
862 			pfd->pfd_flags |= DT_PFCONV_LEFT;
863 			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
864 			goto fmt_switch;
865 
866 		case '.':
867 			if (dot++ != 0) {
868 				yywarn("format conversion #%u has more than "
869 				    "one '.' specified\n", pfv->pfv_argc);
870 
871 				dt_printf_destroy(pfv);
872 				return (dt_printf_error(dtp, EDT_COMPILER));
873 			}
874 			digits = 0;
875 			goto fmt_switch;
876 
877 		case '?':
878 			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
879 				pfd->pfd_width = 16;
880 			else
881 				pfd->pfd_width = 8;
882 			goto fmt_switch;
883 
884 		case '@':
885 			pfd->pfd_flags |= DT_PFCONV_AGG;
886 			goto fmt_switch;
887 
888 		case '\'':
889 			pfd->pfd_flags |= DT_PFCONV_GROUP;
890 			goto fmt_switch;
891 
892 		case ' ':
893 			pfd->pfd_flags |= DT_PFCONV_SPACE;
894 			goto fmt_switch;
895 
896 		case '$':
897 			yywarn("format conversion #%u uses unsupported "
898 			    "positional format (%%n$)\n", pfv->pfv_argc);
899 
900 			dt_printf_destroy(pfv);
901 			return (dt_printf_error(dtp, EDT_COMPILER));
902 
903 		case '%':
904 			if (p[-1] == '%')
905 				goto default_lbl; /* if %% then use "%" conv */
906 
907 			yywarn("format conversion #%u cannot be combined "
908 			    "with other format flags: %%%%\n", pfv->pfv_argc);
909 
910 			dt_printf_destroy(pfv);
911 			return (dt_printf_error(dtp, EDT_COMPILER));
912 
913 		case '\0':
914 			yywarn("format conversion #%u name expected before "
915 			    "end of format string\n", pfv->pfv_argc);
916 
917 			dt_printf_destroy(pfv);
918 			return (dt_printf_error(dtp, EDT_COMPILER));
919 
920 		case 'h':
921 		case 'l':
922 		case 'L':
923 		case 'w':
924 			if (namelen < sizeof (name) - 2)
925 				name[namelen++] = c;
926 			goto fmt_switch;
927 
928 		default_lbl:
929 		default:
930 			name[namelen++] = c;
931 			name[namelen] = '\0';
932 		}
933 
934 		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
935 
936 		if (pfd->pfd_conv == NULL) {
937 			yywarn("format conversion #%u is undefined: %%%s\n",
938 			    pfv->pfv_argc, name);
939 			dt_printf_destroy(pfv);
940 			return (dt_printf_error(dtp, EDT_COMPILER));
941 		}
942 	}
943 
944 	if (*q != '\0' || *format == '\0') {
945 		if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
946 			dt_printf_destroy(pfv);
947 			return (dt_printf_error(dtp, EDT_NOMEM));
948 		}
949 
950 		if (pfv->pfv_argv != NULL)
951 			nfd->pfd_next = pfd;
952 		else
953 			pfv->pfv_argv = pfd;
954 
955 		bzero(pfd, sizeof (dt_pfargd_t));
956 		pfv->pfv_argc++;
957 
958 		pfd->pfd_prefix = q;
959 		pfd->pfd_preflen = strlen(q);
960 	}
961 
962 	return (pfv);
963 }
964 
965 void
966 dt_printf_destroy(dt_pfargv_t *pfv)
967 {
968 	dt_pfargd_t *pfd, *nfd;
969 
970 	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
971 		nfd = pfd->pfd_next;
972 		free(pfd);
973 	}
974 
975 	free(pfv->pfv_format);
976 	free(pfv);
977 }
978 
979 void
980 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
981     dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
982 {
983 	dt_pfargd_t *pfd = pfv->pfv_argv;
984 	const char *func = idp->di_name;
985 
986 	char n[DT_TYPE_NAMELEN];
987 	dtrace_typeinfo_t dtt;
988 	const char *aggtype;
989 	dt_node_t aggnode;
990 	int i, j;
991 
992 	if (pfv->pfv_format[0] == '\0') {
993 		xyerror(D_PRINTF_FMT_EMPTY,
994 		    "%s( ) format string is empty\n", func);
995 	}
996 
997 	pfv->pfv_flags = flags;
998 
999 	/*
1000 	 * We fake up a parse node representing the type that can be used with
1001 	 * an aggregation result conversion, which -- for all but count() --
1002 	 * is a signed quantity.
1003 	 */
1004 	if (kind != DTRACEAGG_COUNT)
1005 		aggtype = "int64_t";
1006 	else
1007 		aggtype = "uint64_t";
1008 
1009 	if (dt_type_lookup(aggtype, &dtt) != 0)
1010 		xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1011 
1012 	bzero(&aggnode, sizeof (aggnode));
1013 	dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
1014 
1015 	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1016 		const dt_pfconv_t *pfc = pfd->pfd_conv;
1017 		const char *dyns[2];
1018 		int dync = 0;
1019 
1020 		char vname[64];
1021 		dt_node_t *vnp;
1022 
1023 		if (pfc == NULL)
1024 			continue; /* no checking if argd is just a prefix */
1025 
1026 		if (pfc->pfc_print == &pfprint_pct) {
1027 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1028 			continue;
1029 		}
1030 
1031 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1032 			dyns[dync++] = ".*";
1033 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1034 			dyns[dync++] = "*";
1035 
1036 		for (; dync != 0; dync--) {
1037 			if (dnp == NULL) {
1038 				xyerror(D_PRINTF_DYN_PROTO,
1039 				    "%s( ) prototype mismatch: conversion "
1040 				    "#%d (%%%s) is missing a corresponding "
1041 				    "\"%s\" argument\n", func, i + 1,
1042 				    pfc->pfc_name, dyns[dync - 1]);
1043 			}
1044 
1045 			if (dt_node_is_integer(dnp) == 0) {
1046 				xyerror(D_PRINTF_DYN_TYPE,
1047 				    "%s( ) argument #%d is incompatible "
1048 				    "with conversion #%d prototype:\n"
1049 				    "\tconversion: %% %s %s\n"
1050 				    "\t prototype: int\n\t  argument: %s\n",
1051 				    func, j + foff + 1, i + 1,
1052 				    dyns[dync - 1], pfc->pfc_name,
1053 				    dt_node_type_name(dnp, n, sizeof (n)));
1054 			}
1055 
1056 			dnp = dnp->dn_list;
1057 			j++;
1058 		}
1059 
1060 		/*
1061 		 * If this conversion is consuming the aggregation data, set
1062 		 * the value node pointer (vnp) to a fake node based on the
1063 		 * aggregating function result type.  Otherwise assign vnp to
1064 		 * the next parse node in the argument list, if there is one.
1065 		 */
1066 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
1067 			if (!(flags & DT_PRINTF_AGGREGATION)) {
1068 				xyerror(D_PRINTF_AGG_CONV,
1069 				    "%%@ conversion requires an aggregation"
1070 				    " and is not for use with %s( )\n", func);
1071 			}
1072 			(void) strlcpy(vname, "aggregating action",
1073 			    sizeof (vname));
1074 			vnp = &aggnode;
1075 		} else if (dnp == NULL) {
1076 			xyerror(D_PRINTF_ARG_PROTO,
1077 			    "%s( ) prototype mismatch: conversion #%d (%%"
1078 			    "%s) is missing a corresponding value argument\n",
1079 			    func, i + 1, pfc->pfc_name);
1080 		} else {
1081 			(void) snprintf(vname, sizeof (vname),
1082 			    "argument #%d", j + foff + 1);
1083 			vnp = dnp;
1084 			dnp = dnp->dn_list;
1085 			j++;
1086 		}
1087 
1088 		/*
1089 		 * Fill in the proposed final format string by prepending any
1090 		 * size-related prefixes to the pfconv's format string.  The
1091 		 * pfc_check() function below may optionally modify the format
1092 		 * as part of validating the type of the input argument.
1093 		 */
1094 		if (pfc->pfc_print == &pfprint_sint ||
1095 		    pfc->pfc_print == &pfprint_uint ||
1096 		    pfc->pfc_print == &pfprint_dint) {
1097 			if (dt_node_type_size(vnp) == sizeof (uint64_t))
1098 				(void) strcpy(pfd->pfd_fmt, "ll");
1099 		} else if (pfc->pfc_print == &pfprint_fp) {
1100 			if (dt_node_type_size(vnp) == sizeof (long double))
1101 				(void) strcpy(pfd->pfd_fmt, "L");
1102 		}
1103 
1104 		(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1105 
1106 		/*
1107 		 * Validate the format conversion against the value node type.
1108 		 * If the conversion is good, create the descriptor format
1109 		 * string by concatenating together any required printf(3C)
1110 		 * size prefixes with the conversion's native format string.
1111 		 */
1112 		if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1113 			xyerror(D_PRINTF_ARG_TYPE,
1114 			    "%s( ) %s is incompatible with "
1115 			    "conversion #%d prototype:\n\tconversion: %%%s\n"
1116 			    "\t prototype: %s\n\t  argument: %s\n", func,
1117 			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1118 			    dt_node_type_name(vnp, n, sizeof (n)));
1119 		}
1120 	}
1121 
1122 	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1123 		xyerror(D_PRINTF_ARG_EXTRA,
1124 		    "%s( ) prototype mismatch: only %d arguments "
1125 		    "required by this format string\n", func, j);
1126 	}
1127 }
1128 
1129 void
1130 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1131 {
1132 	dt_ident_t *lid, *rid;
1133 	dt_node_t *lproto, *rproto;
1134 	int largc, rargc, argn;
1135 	char n1[DT_TYPE_NAMELEN];
1136 	char n2[DT_TYPE_NAMELEN];
1137 
1138 	assert(lhs->dn_kind == DT_NODE_AGG);
1139 	assert(rhs->dn_kind == DT_NODE_AGG);
1140 
1141 	lid = lhs->dn_ident;
1142 	rid = rhs->dn_ident;
1143 
1144 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1145 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1146 
1147 	/*
1148 	 * First, get an argument count on each side.  These must match.
1149 	 */
1150 	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1151 		largc++;
1152 
1153 	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1154 		rargc++;
1155 
1156 	if (largc != rargc) {
1157 		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1158 		    "matching key signatures: @%s has %d key%s, @%s has %d "
1159 		    "key%s", lid->di_name, rid->di_name,
1160 		    lid->di_name, largc, largc == 1 ? "" : "s",
1161 		    rid->di_name, rargc, rargc == 1 ? "" : "s");
1162 	}
1163 
1164 	/*
1165 	 * Now iterate over the keys to verify that each type matches.
1166 	 */
1167 	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1168 	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1169 
1170 	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1171 	    rproto = rproto->dn_list) {
1172 		assert(rproto != NULL);
1173 
1174 		if (dt_node_is_argcompat(lproto, rproto))
1175 			continue;
1176 
1177 		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1178 		    "incompatible with @%s:\n%9s key #%d: %s\n"
1179 		    "%9s key #%d: %s\n",
1180 		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
1181 		    dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1182 		    argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1183 	}
1184 }
1185 
1186 static int
1187 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1188     uint_t nrecs, const void *buf, size_t len, int *ip)
1189 {
1190 	uintptr_t addr;
1191 
1192 	if (nrecs == 0)
1193 		return (dt_set_errno(dtp, EDT_DMISMATCH));
1194 
1195 	addr = (uintptr_t)buf + recp->dtrd_offset;
1196 
1197 	if (addr + sizeof (int) > (uintptr_t)buf + len)
1198 		return (dt_set_errno(dtp, EDT_DOFFSET));
1199 
1200 	if (addr & (recp->dtrd_alignment - 1))
1201 		return (dt_set_errno(dtp, EDT_DALIGN));
1202 
1203 	switch (recp->dtrd_size) {
1204 	case sizeof (int8_t):
1205 		*ip = (int)*((int8_t *)addr);
1206 		break;
1207 	case sizeof (int16_t):
1208 		*ip = (int)*((int16_t *)addr);
1209 		break;
1210 	case sizeof (int32_t):
1211 		*ip = (int)*((int32_t *)addr);
1212 		break;
1213 	case sizeof (int64_t):
1214 		*ip = (int)*((int64_t *)addr);
1215 		break;
1216 	default:
1217 		return (dt_set_errno(dtp, EDT_DMISMATCH));
1218 	}
1219 
1220 	return (0);
1221 }
1222 
1223 /*ARGSUSED*/
1224 static int
1225 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1226     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1227 {
1228 	const uint64_t *data = addr;
1229 
1230 	if (size != sizeof (uint64_t) * 2)
1231 		return (dt_set_errno(dtp, EDT_DMISMATCH));
1232 
1233 	return (dt_printf(dtp, fp, format,
1234 	    data[0] ? data[1] / normal / data[0] : 0));
1235 }
1236 
1237 /*ARGSUSED*/
1238 static int
1239 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1240     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1241 {
1242 	const uint64_t *data = addr;
1243 
1244 	if (size != sizeof (uint64_t) * 4)
1245 		return (dt_set_errno(dtp, EDT_DMISMATCH));
1246 
1247 	return (dt_printf(dtp, fp, format,
1248 	    dt_stddev((uint64_t *)data, normal)));
1249 }
1250 
1251 /*ARGSUSED*/
1252 static int
1253 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1254     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1255 {
1256 	return (dt_print_quantize(dtp, fp, addr, size, normal));
1257 }
1258 
1259 /*ARGSUSED*/
1260 static int
1261 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1262     const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1263 {
1264 	return (dt_print_lquantize(dtp, fp, addr, size, normal));
1265 }
1266 
1267 static int
1268 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1269     const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1270     size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1271 {
1272 	dt_pfargd_t *pfd = pfv->pfv_argv;
1273 	const dtrace_recdesc_t *recp = recs;
1274 	const dtrace_aggdata_t *aggdata;
1275 	dtrace_aggdesc_t *agg;
1276 	caddr_t lim = (caddr_t)buf + len, limit;
1277 	char format[64] = "%";
1278 	int i, aggrec, curagg = -1;
1279 	uint64_t normal;
1280 
1281 	/*
1282 	 * If we are formatting an aggregation, set 'aggrec' to the index of
1283 	 * the final record description (the aggregation result) so we can use
1284 	 * this record index with any conversion where DT_PFCONV_AGG is set.
1285 	 * (The actual aggregation used will vary as we increment through the
1286 	 * aggregation variables that we have been passed.)  Finally, we
1287 	 * decrement nrecs to prevent this record from being used with any
1288 	 * other conversion.
1289 	 */
1290 	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1291 		assert(aggsdata != NULL);
1292 		assert(naggvars > 0);
1293 
1294 		if (nrecs == 0)
1295 			return (dt_set_errno(dtp, EDT_DMISMATCH));
1296 
1297 		curagg = naggvars > 1 ? 1 : 0;
1298 		aggdata = aggsdata[0];
1299 		aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1300 		nrecs--;
1301 	}
1302 
1303 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1304 		const dt_pfconv_t *pfc = pfd->pfd_conv;
1305 		int width = pfd->pfd_width;
1306 		int prec = pfd->pfd_prec;
1307 		int rval;
1308 
1309 		char *f = format + 1; /* skip initial '%' */
1310 		const dtrace_recdesc_t *rec;
1311 		dt_pfprint_f *func;
1312 		caddr_t addr;
1313 		size_t size;
1314 		uint32_t flags;
1315 
1316 		if (pfd->pfd_preflen != 0) {
1317 			char *tmp = alloca(pfd->pfd_preflen + 1);
1318 
1319 			bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1320 			tmp[pfd->pfd_preflen] = '\0';
1321 
1322 			if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1323 				return (rval);
1324 
1325 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1326 				/*
1327 				 * For printa(), we flush the buffer after each
1328 				 * prefix, setting the flags to indicate that
1329 				 * this is part of the printa() format string.
1330 				 */
1331 				flags = DTRACE_BUFDATA_AGGFORMAT;
1332 
1333 				if (pfc == NULL && i == pfv->pfv_argc - 1)
1334 					flags |= DTRACE_BUFDATA_AGGLAST;
1335 
1336 				if (dt_buffered_flush(dtp, NULL, NULL,
1337 				    aggdata, flags) < 0)
1338 					return (-1);
1339 			}
1340 		}
1341 
1342 		if (pfc == NULL) {
1343 			if (pfv->pfv_argc == 1)
1344 				return (nrecs != 0);
1345 			continue;
1346 		}
1347 
1348 		/*
1349 		 * If the conversion is %%, just invoke the print callback
1350 		 * with no data record and continue; it consumes no record.
1351 		 */
1352 		if (pfc->pfc_print == &pfprint_pct) {
1353 			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1354 				continue;
1355 			return (-1); /* errno is set for us */
1356 		}
1357 
1358 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1359 			if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1360 			    len, &width) == -1)
1361 				return (-1); /* errno is set for us */
1362 			pfd->pfd_dynwidth = width;
1363 		} else {
1364 			pfd->pfd_dynwidth = 0;
1365 		}
1366 
1367 		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1368 		    dtp, recp++, nrecs--, buf, len, &prec) == -1)
1369 			return (-1); /* errno is set for us */
1370 
1371 		if (pfd->pfd_flags & DT_PFCONV_AGG) {
1372 			/*
1373 			 * This should be impossible -- the compiler shouldn't
1374 			 * create a DT_PFCONV_AGG conversion without an
1375 			 * aggregation present.  Still, we'd rather fail
1376 			 * gracefully than blow up...
1377 			 */
1378 			if (aggsdata == NULL)
1379 				return (dt_set_errno(dtp, EDT_DMISMATCH));
1380 
1381 			aggdata = aggsdata[curagg];
1382 			agg = aggdata->dtada_desc;
1383 
1384 			/*
1385 			 * We increment the current aggregation variable, but
1386 			 * not beyond the number of aggregation variables that
1387 			 * we're printing. This has the (desired) effect that
1388 			 * DT_PFCONV_AGG conversions beyond the number of
1389 			 * aggregation variables (re-)convert the aggregation
1390 			 * value of the last aggregation variable.
1391 			 */
1392 			if (curagg < naggvars - 1)
1393 				curagg++;
1394 
1395 			rec = &agg->dtagd_rec[aggrec];
1396 			addr = aggdata->dtada_data + rec->dtrd_offset;
1397 			limit = addr + aggdata->dtada_size;
1398 			normal = aggdata->dtada_normal;
1399 			flags = DTRACE_BUFDATA_AGGVAL;
1400 		} else {
1401 			if (nrecs == 0)
1402 				return (dt_set_errno(dtp, EDT_DMISMATCH));
1403 
1404 			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1405 				/*
1406 				 * When printing aggregation keys, we always
1407 				 * set the aggdata to be the representative
1408 				 * (zeroth) aggregation.  The aggdata isn't
1409 				 * actually used here in this case, but it is
1410 				 * passed to the buffer handler and must
1411 				 * therefore still be correct.
1412 				 */
1413 				aggdata = aggsdata[0];
1414 				flags = DTRACE_BUFDATA_AGGKEY;
1415 			}
1416 
1417 			rec = recp++;
1418 			nrecs--;
1419 			addr = (caddr_t)buf + rec->dtrd_offset;
1420 			limit = lim;
1421 			normal = 1;
1422 		}
1423 
1424 		size = rec->dtrd_size;
1425 
1426 		if (addr + size > limit) {
1427 			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1428 			    (void *)addr, rec->dtrd_size, (void *)lim);
1429 			return (dt_set_errno(dtp, EDT_DOFFSET));
1430 		}
1431 
1432 		if (rec->dtrd_alignment != 0 &&
1433 		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1434 			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1435 			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1436 			return (dt_set_errno(dtp, EDT_DALIGN));
1437 		}
1438 
1439 		switch (rec->dtrd_action) {
1440 		case DTRACEAGG_AVG:
1441 			func = pfprint_average;
1442 			break;
1443 		case DTRACEAGG_STDDEV:
1444 			func = pfprint_stddev;
1445 			break;
1446 		case DTRACEAGG_QUANTIZE:
1447 			func = pfprint_quantize;
1448 			break;
1449 		case DTRACEAGG_LQUANTIZE:
1450 			func = pfprint_lquantize;
1451 			break;
1452 		case DTRACEACT_MOD:
1453 			func = pfprint_mod;
1454 			break;
1455 		case DTRACEACT_UMOD:
1456 			func = pfprint_umod;
1457 			break;
1458 		default:
1459 			func = pfc->pfc_print;
1460 			break;
1461 		}
1462 
1463 		if (pfd->pfd_flags & DT_PFCONV_ALT)
1464 			*f++ = '#';
1465 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1466 			*f++ = '0';
1467 		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1468 			*f++ = '-';
1469 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
1470 			*f++ = '+';
1471 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
1472 			*f++ = '\'';
1473 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
1474 			*f++ = ' ';
1475 
1476 		/*
1477 		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1478 		 * don't add the width to the format string.  See the block
1479 		 * comment in pfprint_stack() for a description of the
1480 		 * behavior in this case.
1481 		 */
1482 		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1483 			width = 0;
1484 
1485 		if (width != 0)
1486 			f += snprintf(f, sizeof (format), "%d", ABS(width));
1487 
1488 		if (prec > 0)
1489 			f += snprintf(f, sizeof (format), ".%d", prec);
1490 
1491 		(void) strcpy(f, pfd->pfd_fmt);
1492 		pfd->pfd_rec = rec;
1493 
1494 		if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1495 			return (-1); /* errno is set for us */
1496 
1497 		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1498 			/*
1499 			 * For printa(), we flush the buffer after each tuple
1500 			 * element, inidicating that this is the last record
1501 			 * as appropriate.
1502 			 */
1503 			if (i == pfv->pfv_argc - 1)
1504 				flags |= DTRACE_BUFDATA_AGGLAST;
1505 
1506 			if (dt_buffered_flush(dtp, NULL,
1507 			    rec, aggdata, flags) < 0)
1508 				return (-1);
1509 		}
1510 	}
1511 
1512 	return ((int)(recp - recs));
1513 }
1514 
1515 int
1516 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1517     const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1518 {
1519 	dtrace_optval_t size;
1520 	int rval;
1521 
1522 	rval = dtrace_getopt(dtp, "strsize", &size);
1523 	assert(rval == 0);
1524 	assert(dtp->dt_sprintf_buflen == 0);
1525 
1526 	if (dtp->dt_sprintf_buf != NULL)
1527 		free(dtp->dt_sprintf_buf);
1528 
1529 	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1530 		return (dt_set_errno(dtp, EDT_NOMEM));
1531 
1532 	bzero(dtp->dt_sprintf_buf, size);
1533 	dtp->dt_sprintf_buflen = size;
1534 	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1535 	    NULL, 0);
1536 	dtp->dt_sprintf_buflen = 0;
1537 
1538 	if (rval == -1)
1539 		free(dtp->dt_sprintf_buf);
1540 
1541 	return (rval);
1542 }
1543 
1544 /*ARGSUSED*/
1545 int
1546 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1547     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1548     uint_t nrecs, const void *buf, size_t len)
1549 {
1550 	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1551 
1552 	if (rval == -1)
1553 		return (rval);
1554 
1555 	/*
1556 	 * Before we execute the specified command, flush fp to assure that
1557 	 * any prior dt_printf()'s appear before the output of the command
1558 	 * not after it.
1559 	 */
1560 	(void) fflush(fp);
1561 
1562 	if (system(dtp->dt_sprintf_buf) == -1)
1563 		return (dt_set_errno(dtp, errno));
1564 
1565 	return (rval);
1566 }
1567 
1568 int
1569 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1570     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1571     uint_t nrecs, const void *buf, size_t len)
1572 {
1573 	char selfbuf[40], restorebuf[40], *filename;
1574 	FILE *nfp;
1575 	int rval, errval;
1576 	dt_pfargv_t *pfv = fmtdata;
1577 	dt_pfargd_t *pfd = pfv->pfv_argv;
1578 
1579 	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1580 
1581 	if (rval == -1 || fp == NULL)
1582 		return (rval);
1583 
1584 #if defined(sun)
1585 	if (pfd->pfd_preflen != 0 &&
1586 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1587 		/*
1588 		 * The only way to have the format string set to the value
1589 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1590 		 * denoting that we should restore the old stdout.
1591 		 */
1592 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1593 
1594 		if (dtp->dt_stdout_fd == -1) {
1595 			/*
1596 			 * We could complain here by generating an error,
1597 			 * but it seems like overkill:  it seems that calling
1598 			 * freopen() to restore stdout when freopen() has
1599 			 * never before been called should just be a no-op,
1600 			 * so we just return in this case.
1601 			 */
1602 			return (rval);
1603 		}
1604 
1605 		(void) snprintf(restorebuf, sizeof (restorebuf),
1606 		    "/dev/fd/%d", dtp->dt_stdout_fd);
1607 		filename = restorebuf;
1608 	} else {
1609 		filename = dtp->dt_sprintf_buf;
1610 	}
1611 
1612 	/*
1613 	 * freopen(3C) will always close the specified stream and underlying
1614 	 * file descriptor -- even if the specified file can't be opened.
1615 	 * Even for the semantic cesspool that is standard I/O, this is
1616 	 * surprisingly brain-dead behavior:  it means that any failure to
1617 	 * open the specified file destroys the specified stream in the
1618 	 * process -- which is particularly relevant when the specified stream
1619 	 * happens (or rather, happened) to be stdout.  This could be resolved
1620 	 * were there an "fdreopen()" equivalent of freopen() that allowed one
1621 	 * to pass a file descriptor instead of the name of a file, but there
1622 	 * is no such thing.  However, we can effect this ourselves by first
1623 	 * fopen()'ing the desired file, and then (assuming that that works),
1624 	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1625 	 * file descriptor for the fopen()'d file.  This way, if the fopen()
1626 	 * fails, we can fail the operation without destroying stdout.
1627 	 */
1628 	if ((nfp = fopen(filename, "aF")) == NULL) {
1629 		char *msg = strerror(errno);
1630 		char *faultstr;
1631 		int len = 80;
1632 
1633 		len += strlen(msg) + strlen(filename);
1634 		faultstr = alloca(len);
1635 
1636 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1637 		    filename, strerror(errno));
1638 
1639 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1640 			return (rval);
1641 
1642 		return (errval);
1643 	}
1644 
1645 	(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1646 
1647 	if (dtp->dt_stdout_fd == -1) {
1648 		/*
1649 		 * If this is the first time that we're calling freopen(),
1650 		 * we're going to stash away the file descriptor for stdout.
1651 		 * We don't expect the dup(2) to fail, so if it does we must
1652 		 * return failure.
1653 		 */
1654 		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1655 			(void) fclose(nfp);
1656 			return (dt_set_errno(dtp, errno));
1657 		}
1658 	}
1659 
1660 	if (freopen(selfbuf, "aF", fp) == NULL) {
1661 		(void) fclose(nfp);
1662 		return (dt_set_errno(dtp, errno));
1663 	}
1664 
1665 	(void) fclose(nfp);
1666 #else
1667 	/*
1668 	 * The 'standard output' (which is not necessarily stdout)
1669 	 * treatment on FreeBSD is implemented differently than on
1670 	 * Solaris because FreeBSD's freopen() will attempt to re-use
1671 	 * the current file descriptor, causing the previous file to
1672 	 * be closed and thereby preventing it from be re-activated
1673 	 * later.
1674 	 *
1675 	 * For FreeBSD we use the concept of setting an output file
1676 	 * pointer in the DTrace handle if a dtrace_freopen() has
1677 	 * enabled another output file and we leave the caller's
1678 	 * file pointer untouched. If it was actually stdout, then
1679 	 * stdout remains open. If it was another file, then that
1680 	 * file remains open. While a dtrace_freopen() has activated
1681 	 * another file, we keep a pointer to that which we use in
1682 	 * the output functions by preference and only use the caller's
1683 	 * file pointer if no dtrace_freopen() call has been made.
1684 	 *
1685 	 * The check to see if we're re-activating the caller's
1686 	 * output file is much the same as on Solaris.
1687 	 */
1688 	if (pfd->pfd_preflen != 0 &&
1689 	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1690 		/*
1691 		 * The only way to have the format string set to the value
1692 		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1693 		 * denoting that we should restore the old stdout.
1694 		 */
1695 		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1696 
1697 		if (dtp->dt_freopen_fp == NULL) {
1698 			/*
1699 			 * We could complain here by generating an error,
1700 			 * but it seems like overkill:  it seems that calling
1701 			 * freopen() to restore stdout when freopen() has
1702 			 * never before been called should just be a no-op,
1703 			 * so we just return in this case.
1704 			 */
1705 			return (rval);
1706 		}
1707 
1708 		/*
1709 		 * At this point, to re-active the original output file,
1710 		 * on FreeBSD we only code the current file that this
1711 		 * function opened previously.
1712 		 */
1713 		(void) fclose(dtp->dt_freopen_fp);
1714 		dtp->dt_freopen_fp = NULL;
1715 
1716 		return (rval);
1717 	}
1718 
1719 	if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1720 		char *msg = strerror(errno);
1721 		char *faultstr;
1722 		int len = 80;
1723 
1724 		len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1725 		faultstr = alloca(len);
1726 
1727 		(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1728 		    dtp->dt_sprintf_buf, strerror(errno));
1729 
1730 		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1731 			return (rval);
1732 
1733 		return (errval);
1734 	}
1735 
1736 	if (dtp->dt_freopen_fp != NULL)
1737 		(void) fclose(dtp->dt_freopen_fp);
1738 
1739 	/* Remember that the output has been redirected to the new file. */
1740 	dtp->dt_freopen_fp = nfp;
1741 #endif
1742 
1743 	return (rval);
1744 }
1745 
1746 /*ARGSUSED*/
1747 int
1748 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1749     const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1750     uint_t nrecs, const void *buf, size_t len)
1751 {
1752 	return (dt_printf_format(dtp, fp, fmtdata,
1753 	    recp, nrecs, buf, len, NULL, 0));
1754 }
1755 
1756 void *
1757 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1758 {
1759 	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1760 	dt_pfargd_t *pfd;
1761 	int i;
1762 
1763 	if (pfv == NULL)
1764 		return (NULL);		/* errno has been set for us */
1765 
1766 	pfd = pfv->pfv_argv;
1767 
1768 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1769 		const dt_pfconv_t *pfc = pfd->pfd_conv;
1770 
1771 		if (pfc == NULL)
1772 			continue;
1773 
1774 		/*
1775 		 * If the output format is not %s then we assume that we have
1776 		 * been given a correctly-sized format string, so we copy the
1777 		 * true format name including the size modifier.  If the output
1778 		 * format is %s, then either the input format is %s as well or
1779 		 * it is one of our custom formats (e.g. pfprint_addr), so we
1780 		 * must set pfd_fmt to be the output format conversion "s".
1781 		 */
1782 		if (strcmp(pfc->pfc_ofmt, "s") != 0)
1783 			(void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1784 		else
1785 			(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1786 	}
1787 
1788 	return (pfv);
1789 }
1790 
1791 void *
1792 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1793 {
1794 	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1795 
1796 	if (pfv == NULL)
1797 		return (NULL);		/* errno has been set for us */
1798 
1799 	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1800 
1801 	return (pfv);
1802 }
1803 
1804 /*ARGSUSED*/
1805 size_t
1806 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1807 {
1808 	dt_pfargv_t *pfv = fmtdata;
1809 	dt_pfargd_t *pfd = pfv->pfv_argv;
1810 
1811 	/*
1812 	 * An upper bound on the string length is the length of the original
1813 	 * format string, plus three times the number of conversions (each
1814 	 * conversion could add up an additional "ll" and/or pfd_width digit
1815 	 * in the case of converting %? to %16) plus one for a terminating \0.
1816 	 */
1817 	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1818 	char *format = alloca(formatlen);
1819 	char *f = format;
1820 	int i, j;
1821 
1822 	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1823 		const dt_pfconv_t *pfc = pfd->pfd_conv;
1824 		const char *str;
1825 		int width = pfd->pfd_width;
1826 		int prec = pfd->pfd_prec;
1827 
1828 		if (pfd->pfd_preflen != 0) {
1829 			for (j = 0; j < pfd->pfd_preflen; j++)
1830 				*f++ = pfd->pfd_prefix[j];
1831 		}
1832 
1833 		if (pfc == NULL)
1834 			continue;
1835 
1836 		*f++ = '%';
1837 
1838 		if (pfd->pfd_flags & DT_PFCONV_ALT)
1839 			*f++ = '#';
1840 		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1841 			*f++ = '0';
1842 		if (pfd->pfd_flags & DT_PFCONV_LEFT)
1843 			*f++ = '-';
1844 		if (pfd->pfd_flags & DT_PFCONV_SPOS)
1845 			*f++ = '+';
1846 		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1847 			*f++ = '*';
1848 		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1849 			*f++ = '.';
1850 			*f++ = '*';
1851 		}
1852 		if (pfd->pfd_flags & DT_PFCONV_GROUP)
1853 			*f++ = '\'';
1854 		if (pfd->pfd_flags & DT_PFCONV_SPACE)
1855 			*f++ = ' ';
1856 		if (pfd->pfd_flags & DT_PFCONV_AGG)
1857 			*f++ = '@';
1858 
1859 		if (width != 0)
1860 			f += snprintf(f, sizeof (format), "%d", width);
1861 
1862 		if (prec != 0)
1863 			f += snprintf(f, sizeof (format), ".%d", prec);
1864 
1865 		/*
1866 		 * If the output format is %s, then either %s is the underlying
1867 		 * conversion or the conversion is one of our customized ones,
1868 		 * e.g. pfprint_addr.  In these cases, put the original string
1869 		 * name of the conversion (pfc_name) into the pickled format
1870 		 * string rather than the derived conversion (pfd_fmt).
1871 		 */
1872 		if (strcmp(pfc->pfc_ofmt, "s") == 0)
1873 			str = pfc->pfc_name;
1874 		else
1875 			str = pfd->pfd_fmt;
1876 
1877 		for (j = 0; str[j] != '\0'; j++)
1878 			*f++ = str[j];
1879 	}
1880 
1881 	*f = '\0'; /* insert nul byte; do not count in return value */
1882 
1883 	assert(f < format + formatlen);
1884 	(void) strncpy(s, format, len);
1885 
1886 	return ((size_t)(f - format));
1887 }
1888 
1889 static int
1890 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1891 {
1892 	const dtrace_aggdesc_t *agg = adp->dtada_desc;
1893 	const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1894 	uint_t nrecs = agg->dtagd_nrecs;
1895 	dt_pfwalk_t *pfw = arg;
1896 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1897 	int id;
1898 
1899 	if (dt_printf_getint(dtp, recp++, nrecs--,
1900 	    adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1901 		return (0); /* no aggregation id or id does not match */
1902 
1903 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1904 	    recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1905 		return (pfw->pfw_err = dtp->dt_errno);
1906 
1907 	/*
1908 	 * Cast away the const to set the bit indicating that this aggregation
1909 	 * has been printed.
1910 	 */
1911 	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1912 
1913 	return (0);
1914 }
1915 
1916 static int
1917 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1918 {
1919 	const dtrace_aggdata_t *aggdata = aggsdata[0];
1920 	const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1921 	const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1922 	uint_t nrecs = agg->dtagd_nrecs - 1;
1923 	dt_pfwalk_t *pfw = arg;
1924 	dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1925 	int i;
1926 
1927 	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1928 	    rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
1929 	    aggsdata, naggvars) == -1)
1930 		return (pfw->pfw_err = dtp->dt_errno);
1931 
1932 	/*
1933 	 * For each aggregation, indicate that it has been printed, casting
1934 	 * away the const as necessary.
1935 	 */
1936 	for (i = 1; i < naggvars; i++) {
1937 		agg = aggsdata[i]->dtada_desc;
1938 		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1939 	}
1940 
1941 	return (0);
1942 }
1943 /*ARGSUSED*/
1944 int
1945 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1946     const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
1947     uint_t nrecs, const void *buf, size_t len)
1948 {
1949 	dt_pfwalk_t pfw;
1950 	int i, naggvars = 0;
1951 	dtrace_aggvarid_t *aggvars;
1952 
1953 	aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
1954 
1955 	/*
1956 	 * This might be a printa() with multiple aggregation variables.  We
1957 	 * need to scan forward through the records until we find a record from
1958 	 * a different statement.
1959 	 */
1960 	for (i = 0; i < nrecs; i++) {
1961 		const dtrace_recdesc_t *nrec = &recs[i];
1962 
1963 		if (nrec->dtrd_uarg != recs->dtrd_uarg)
1964 			break;
1965 
1966 		if (nrec->dtrd_action != recs->dtrd_action)
1967 			return (dt_set_errno(dtp, EDT_BADAGG));
1968 
1969 		aggvars[naggvars++] =
1970 		    /* LINTED - alignment */
1971 		    *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
1972 	}
1973 
1974 	if (naggvars == 0)
1975 		return (dt_set_errno(dtp, EDT_BADAGG));
1976 
1977 	pfw.pfw_argv = fmtdata;
1978 	pfw.pfw_fp = fp;
1979 	pfw.pfw_err = 0;
1980 
1981 	if (naggvars == 1) {
1982 		pfw.pfw_aid = aggvars[0];
1983 
1984 		if (dtrace_aggregate_walk_sorted(dtp,
1985 		    dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
1986 			return (-1); /* errno is set for us */
1987 	} else {
1988 		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
1989 		    dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
1990 			return (-1); /* errno is set for us */
1991 	}
1992 
1993 	return (i);
1994 }
1995