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