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