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