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