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.
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.
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]
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.
29 #include <sys/sysmacros.h>
31 #define ABS(a) ((a) < 0 ? -(a) : (a))
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/byteorder.h>
48 #include <dt_printf.h>
49 #include <dt_string.h>
53 #define NS_IN6ADDRSZ 16
62 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
64 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
69 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
71 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
72 dt_node_is_symaddr(dnp));
77 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
79 dtrace_hdl_t *dtp = pfv->pfv_dtp;
80 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
82 if (dt_node_is_usymaddr(dnp))
85 if (idp == NULL || idp->di_id == 0)
88 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
93 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
95 return (dt_node_is_stack(dnp));
100 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
102 return (dt_node_is_integer(dnp) &&
103 dt_node_type_size(dnp) == sizeof (uint64_t));
108 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
116 if (dt_node_is_string(dnp))
120 base = ctf_type_resolve(ctfp, dnp->dn_type);
121 kind = ctf_type_kind(ctfp, base);
123 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
124 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
125 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
130 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
132 ctf_file_t *ctfp = dnp->dn_ctfp;
133 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
134 uint_t kind = ctf_type_kind(ctfp, base);
139 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
140 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
141 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
142 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
147 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
149 return (dt_node_is_integer(dnp) &&
150 dt_node_type_size(dnp) <= sizeof (int));
155 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
157 return (dt_node_is_float(dnp));
162 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
164 return (dt_node_is_integer(dnp));
169 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
171 if (dnp->dn_flags & DT_NF_SIGNED)
172 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'i';
174 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
176 return (dt_node_is_integer(dnp));
181 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
183 ctf_file_t *ctfp = dnp->dn_ctfp;
184 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
185 char n[DT_TYPE_NAMELEN];
187 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
188 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
189 strcmp(n, "unsigned short") == 0));
194 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
196 ctf_file_t *ctfp = dnp->dn_ctfp;
197 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
198 char n[DT_TYPE_NAMELEN];
200 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
201 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
202 strcmp(n, "unsigned long") == 0));
207 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
209 ctf_file_t *ctfp = dnp->dn_ctfp;
210 ctf_id_t type = dnp->dn_type;
211 char n[DT_TYPE_NAMELEN];
213 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
214 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
215 strcmp(n, "signed long long") == 0 ||
216 strcmp(n, "unsigned long long") == 0))
220 * If the type used for %llx or %llX is not an [unsigned] long long, we
221 * also permit it to be a [u]int64_t or any typedef thereof. We know
222 * that these typedefs are guaranteed to work with %ll[xX] in either
223 * compilation environment even though they alias to "long" in LP64.
225 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
226 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
227 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
230 type = ctf_type_reference(ctfp, type);
238 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
240 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
241 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
246 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
247 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
249 int64_t normal = (int64_t)unormal;
250 int32_t n = (int32_t)normal;
253 case sizeof (int8_t):
254 return (dt_printf(dtp, fp, format,
255 (int32_t)*((int8_t *)addr) / n));
256 case sizeof (int16_t):
257 return (dt_printf(dtp, fp, format,
258 (int32_t)*((int16_t *)addr) / n));
259 case sizeof (int32_t):
260 return (dt_printf(dtp, fp, format,
261 *((int32_t *)addr) / n));
262 case sizeof (int64_t):
263 return (dt_printf(dtp, fp, format,
264 *((int64_t *)addr) / normal));
266 return (dt_set_errno(dtp, EDT_DMISMATCH));
272 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
273 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
275 uint32_t n = (uint32_t)normal;
278 case sizeof (uint8_t):
279 return (dt_printf(dtp, fp, format,
280 (uint32_t)*((uint8_t *)addr) / n));
281 case sizeof (uint16_t):
282 return (dt_printf(dtp, fp, format,
283 (uint32_t)*((uint16_t *)addr) / n));
284 case sizeof (uint32_t):
285 return (dt_printf(dtp, fp, format,
286 *((uint32_t *)addr) / n));
287 case sizeof (uint64_t):
288 return (dt_printf(dtp, fp, format,
289 *((uint64_t *)addr) / normal));
291 return (dt_set_errno(dtp, EDT_DMISMATCH));
296 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
297 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
299 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
300 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
302 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
307 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
308 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
310 double n = (double)normal;
311 long double ldn = (long double)normal;
315 return (dt_printf(dtp, fp, format,
316 (double)*((float *)addr) / n));
317 case sizeof (double):
318 return (dt_printf(dtp, fp, format,
319 *((double *)addr) / n));
320 #if !defined(__arm__) && !defined(__powerpc__) && \
321 !defined(__mips__) && !defined(__riscv)
322 case sizeof (long double):
323 return (dt_printf(dtp, fp, format,
324 *((long double *)addr) / ldn));
327 return (dt_set_errno(dtp, EDT_DMISMATCH));
333 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
334 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
341 case sizeof (uint32_t):
342 val = *((uint32_t *)addr);
344 case sizeof (uint64_t):
345 val = *((uint64_t *)addr);
348 return (dt_set_errno(dtp, EDT_DMISMATCH));
354 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
356 return (dt_printf(dtp, fp, format, s));
361 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
362 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
364 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
369 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
370 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
372 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
377 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
378 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
382 uint64_t val, pid = 0;
384 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
387 case sizeof (uint32_t):
388 val = (u_longlong_t)*((uint32_t *)addr);
390 case sizeof (uint64_t):
391 val = (u_longlong_t)*((uint64_t *)addr);
393 case sizeof (uint64_t) * 2:
394 pid = ((uint64_t *)(uintptr_t)addr)[0];
395 val = ((uint64_t *)(uintptr_t)addr)[1];
398 return (dt_set_errno(dtp, EDT_DMISMATCH));
401 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
407 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
409 return (dt_printf(dtp, fp, format, s));
414 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
415 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
418 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
419 const dtrace_recdesc_t *rec = pfd->pfd_rec;
420 caddr_t addr = (caddr_t)vaddr;
424 * We have stashed the value of the STACKINDENT option, and we will
425 * now override it for the purposes of formatting the stack. If the
426 * field has been specified as left-aligned (i.e. (%-#), we set the
427 * indentation to be the width. This is a slightly odd semantic, but
428 * it's useful functionality -- and it's slightly odd to begin with to
429 * be using a single format specifier to be formatting multiple lines
432 if (pfd->pfd_dynwidth < 0) {
433 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
434 width = -pfd->pfd_dynwidth;
435 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
436 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
441 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
443 switch (rec->dtrd_action) {
444 case DTRACEACT_USTACK:
445 case DTRACEACT_JSTACK:
446 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
449 case DTRACEACT_STACK:
450 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
451 rec->dtrd_size / rec->dtrd_arg);
458 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
465 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
466 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
468 char src[32], buf[32], *dst = buf;
469 hrtime_t time = *((uint64_t *)addr);
470 time_t sec = (time_t)(time / NANOSEC);
474 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
475 * Below, we turn this into the canonical adb/mdb /[yY] format,
476 * "1973 Dec 3 17:20:00".
479 (void) ctime_r(&sec, src, sizeof (src));
481 (void) ctime_r(&sec, src);
485 * Place the 4-digit year at the head of the string...
487 for (i = 20; i < 24; i++)
491 * ...and follow it with the remainder (month, day, hh:mm:ss).
493 for (i = 3; i < 19; i++)
497 return (dt_printf(dtp, fp, format, buf));
501 * This prints the time in RFC 822 standard form. This is useful for emitting
502 * notions of time that are consumed by standard tools (e.g., as part of an
507 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
508 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
510 hrtime_t time = *((uint64_t *)addr);
511 time_t sec = (time_t)(time / NANOSEC);
515 (void) localtime_r(&sec, &tm);
516 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
517 return (dt_printf(dtp, fp, format, buf));
522 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
523 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
525 uint16_t port = htons(*((uint16_t *)addr));
527 struct servent *sv, res;
530 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
532 if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
534 return (dt_printf(dtp, fp, format, sv->s_name));
536 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
537 return (dt_printf(dtp, fp, format, buf));
542 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
543 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
545 char *s = alloca(size + 1);
546 struct hostent *host, res;
547 char inetaddr[NS_IN6ADDRSZ];
551 bcopy(addr, s, size);
554 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
556 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
557 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
559 if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
560 AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
562 return (dt_printf(dtp, fp, format, host->h_name));
563 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
564 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
565 AF_INET6, &e)) != NULL)
566 return (dt_printf(dtp, fp, format, host->h_name));
569 return (dt_printf(dtp, fp, format, s));
574 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
575 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
577 char *s = alloca(size + 1);
579 bcopy(addr, s, size);
581 return (dt_printf(dtp, fp, format, s));
586 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
587 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
589 wchar_t *ws = alloca(size + sizeof (wchar_t));
591 bcopy(addr, ws, size);
592 ws[size / sizeof (wchar_t)] = L'\0';
593 return (dt_printf(dtp, fp, format, ws));
598 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
599 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
604 if ((s = strchr2esc(addr, size)) == NULL)
605 return (dt_set_errno(dtp, EDT_NOMEM));
607 n = dt_printf(dtp, fp, format, s);
613 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
614 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
619 case sizeof (int8_t):
622 case sizeof (int16_t):
623 c = *(int16_t *)addr;
625 case sizeof (int32_t):
626 c = *(int32_t *)addr;
629 return (dt_set_errno(dtp, EDT_DMISMATCH));
632 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
637 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
638 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
640 return (dt_printf(dtp, fp, "%%"));
643 static const char pfproto_xint[] = "char, short, int, long, or long long";
644 static const char pfproto_csi[] = "char, short, or int";
645 static const char pfproto_fp[] = "float, double, or long double";
646 static const char pfproto_addr[] = "pointer or integer";
647 static const char pfproto_uaddr[] =
648 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
649 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
650 static const char pfproto_wstr[] = "wchar_t []";
653 * Printf format conversion dictionary. This table should match the set of
654 * conversions offered by printf(3C), as well as some additional extensions.
655 * The second parameter is an ASCII string which is either an actual type
656 * name we should look up (if pfcheck_type is specified), or just a descriptive
657 * string of the types expected for use in error messages.
659 static const dt_pfconv_t _dtrace_conversions[] = {
660 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
661 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
662 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
663 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
664 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
665 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
666 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
667 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
668 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
669 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
670 { "hd", "d", "short", pfcheck_type, pfprint_sint },
671 { "hi", "i", "short", pfcheck_type, pfprint_sint },
672 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
673 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
674 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
675 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
676 { "i", "i", pfproto_xint, pfcheck_xint, pfprint_sint },
677 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
678 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
679 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
680 { "ld", "d", "long", pfcheck_type, pfprint_sint },
681 { "li", "i", "long", pfcheck_type, pfprint_sint },
682 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
683 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
684 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
685 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
686 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
687 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
688 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
689 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
690 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
691 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
692 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
693 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
694 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
695 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
696 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
697 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
698 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
699 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
700 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
701 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
702 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
703 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
704 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
706 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
707 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
709 { "wc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
710 { "ws", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
712 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
713 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
714 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
715 { "%", "%", "void", pfcheck_type, pfprint_pct },
716 { NULL, NULL, NULL, NULL, NULL }
720 dt_pfdict_create(dtrace_hdl_t *dtp)
722 uint_t n = _dtrace_strbuckets;
723 const dt_pfconv_t *pfd;
726 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
727 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
729 return (dt_set_errno(dtp, EDT_NOMEM));
732 dtp->dt_pfdict = pdi;
733 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
734 pdi->pdi_nbuckets = n;
736 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
737 dtrace_typeinfo_t dtt;
741 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
742 dt_pfdict_destroy(dtp);
743 return (dt_set_errno(dtp, EDT_NOMEM));
746 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
747 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
748 pfc->pfc_next = pdi->pdi_buckets[h];
749 pdi->pdi_buckets[h] = pfc;
752 dtt.dtt_type = CTF_ERR;
755 * The "D" container or its parent must contain a definition of
756 * any type referenced by a printf conversion. If none can be
757 * found, we fail to initialize the printf dictionary.
759 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
760 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
761 dt_pfdict_destroy(dtp);
762 return (dt_set_errno(dtp, EDT_NOCONV));
765 pfc->pfc_dctfp = dtt.dtt_ctfp;
766 pfc->pfc_dtype = dtt.dtt_type;
769 * The "C" container may contain an alternate definition of an
770 * explicit conversion type. If it does, use it; otherwise
771 * just set pfc_ctype to pfc_dtype so it is always valid.
773 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
774 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
775 pfc->pfc_cctfp = dtt.dtt_ctfp;
776 pfc->pfc_ctype = dtt.dtt_type;
778 pfc->pfc_cctfp = pfc->pfc_dctfp;
779 pfc->pfc_ctype = pfc->pfc_dtype;
782 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
783 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
784 dt_pfdict_destroy(dtp);
785 return (dt_set_errno(dtp, EDT_BADCONV));
788 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
795 dt_pfdict_destroy(dtrace_hdl_t *dtp)
797 dt_pfdict_t *pdi = dtp->dt_pfdict;
798 dt_pfconv_t *pfc, *nfc;
804 for (i = 0; i < pdi->pdi_nbuckets; i++) {
805 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
811 free(pdi->pdi_buckets);
813 dtp->dt_pfdict = NULL;
816 static const dt_pfconv_t *
817 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
819 dt_pfdict_t *pdi = dtp->dt_pfdict;
820 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
821 const dt_pfconv_t *pfc;
823 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
824 if (strcmp(pfc->pfc_name, name) == 0)
832 dt_printf_error(dtrace_hdl_t *dtp, int err)
835 longjmp(yypcb->pcb_jmpbuf, err);
837 (void) dt_set_errno(dtp, err);
842 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
844 dt_pfargd_t *pfd, *nfd = NULL;
849 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
850 (format = strdup(s)) == NULL) {
852 return (dt_printf_error(dtp, EDT_NOMEM));
855 pfv->pfv_format = format;
856 pfv->pfv_argv = NULL;
861 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
870 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
871 dt_printf_destroy(pfv);
872 return (dt_printf_error(dtp, EDT_NOMEM));
875 if (pfv->pfv_argv != NULL)
880 bzero(pfd, sizeof (dt_pfargd_t));
885 pfd->pfd_preflen = (size_t)(p - q);
891 case '0': case '1': case '2': case '3': case '4':
892 case '5': case '6': case '7': case '8': case '9':
893 if (dot == 0 && digits == 0 && c == '0') {
894 pfd->pfd_flags |= DT_PFCONV_ZPAD;
895 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
899 for (n = 0; isdigit(c); c = *++p)
900 n = n * 10 + c - '0';
912 pfd->pfd_flags |= DT_PFCONV_ALT;
916 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
918 if (pfd->pfd_flags & n) {
919 yywarn("format conversion #%u has more than "
920 "one '*' specified for the output %s\n",
921 pfv->pfv_argc, n ? "precision" : "width");
923 dt_printf_destroy(pfv);
924 return (dt_printf_error(dtp, EDT_COMPILER));
931 pfd->pfd_flags |= DT_PFCONV_SPOS;
935 pfd->pfd_flags |= DT_PFCONV_LEFT;
936 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
941 yywarn("format conversion #%u has more than "
942 "one '.' specified\n", pfv->pfv_argc);
944 dt_printf_destroy(pfv);
945 return (dt_printf_error(dtp, EDT_COMPILER));
951 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
958 pfd->pfd_flags |= DT_PFCONV_AGG;
962 pfd->pfd_flags |= DT_PFCONV_GROUP;
966 pfd->pfd_flags |= DT_PFCONV_SPACE;
970 yywarn("format conversion #%u uses unsupported "
971 "positional format (%%n$)\n", pfv->pfv_argc);
973 dt_printf_destroy(pfv);
974 return (dt_printf_error(dtp, EDT_COMPILER));
978 goto default_lbl; /* if %% then use "%" conv */
980 yywarn("format conversion #%u cannot be combined "
981 "with other format flags: %%%%\n", pfv->pfv_argc);
983 dt_printf_destroy(pfv);
984 return (dt_printf_error(dtp, EDT_COMPILER));
987 yywarn("format conversion #%u name expected before "
988 "end of format string\n", pfv->pfv_argc);
990 dt_printf_destroy(pfv);
991 return (dt_printf_error(dtp, EDT_COMPILER));
997 if (namelen < sizeof (name) - 2)
1003 name[namelen++] = c;
1004 name[namelen] = '\0';
1007 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
1009 if (pfd->pfd_conv == NULL) {
1010 yywarn("format conversion #%u is undefined: %%%s\n",
1011 pfv->pfv_argc, name);
1012 dt_printf_destroy(pfv);
1013 return (dt_printf_error(dtp, EDT_COMPILER));
1017 if (*q != '\0' || *format == '\0') {
1018 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1019 dt_printf_destroy(pfv);
1020 return (dt_printf_error(dtp, EDT_NOMEM));
1023 if (pfv->pfv_argv != NULL)
1024 nfd->pfd_next = pfd;
1026 pfv->pfv_argv = pfd;
1028 bzero(pfd, sizeof (dt_pfargd_t));
1031 pfd->pfd_prefix = q;
1032 pfd->pfd_preflen = strlen(q);
1039 dt_printf_destroy(dt_pfargv_t *pfv)
1041 dt_pfargd_t *pfd, *nfd;
1043 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1044 nfd = pfd->pfd_next;
1048 free(pfv->pfv_format);
1053 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1054 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1056 dt_pfargd_t *pfd = pfv->pfv_argv;
1057 const char *func = idp->di_name;
1059 char n[DT_TYPE_NAMELEN];
1060 dtrace_typeinfo_t dtt;
1061 const char *aggtype;
1065 if (pfv->pfv_format[0] == '\0') {
1066 xyerror(D_PRINTF_FMT_EMPTY,
1067 "%s( ) format string is empty\n", func);
1070 pfv->pfv_flags = flags;
1073 * We fake up a parse node representing the type that can be used with
1074 * an aggregation result conversion, which -- for all but count() --
1075 * is a signed quantity.
1077 if (kind != DTRACEAGG_COUNT)
1078 aggtype = "int64_t";
1080 aggtype = "uint64_t";
1082 if (dt_type_lookup(aggtype, &dtt) != 0)
1083 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1085 bzero(&aggnode, sizeof (aggnode));
1086 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
1088 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1089 const dt_pfconv_t *pfc = pfd->pfd_conv;
1090 const char *dyns[2];
1097 continue; /* no checking if argd is just a prefix */
1099 if (pfc->pfc_print == &pfprint_pct) {
1100 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1104 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1105 dyns[dync++] = ".*";
1106 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1109 for (; dync != 0; dync--) {
1111 xyerror(D_PRINTF_DYN_PROTO,
1112 "%s( ) prototype mismatch: conversion "
1113 "#%d (%%%s) is missing a corresponding "
1114 "\"%s\" argument\n", func, i + 1,
1115 pfc->pfc_name, dyns[dync - 1]);
1118 if (dt_node_is_integer(dnp) == 0) {
1119 xyerror(D_PRINTF_DYN_TYPE,
1120 "%s( ) argument #%d is incompatible "
1121 "with conversion #%d prototype:\n"
1122 "\tconversion: %% %s %s\n"
1123 "\t prototype: int\n\t argument: %s\n",
1124 func, j + foff + 1, i + 1,
1125 dyns[dync - 1], pfc->pfc_name,
1126 dt_node_type_name(dnp, n, sizeof (n)));
1134 * If this conversion is consuming the aggregation data, set
1135 * the value node pointer (vnp) to a fake node based on the
1136 * aggregating function result type. Otherwise assign vnp to
1137 * the next parse node in the argument list, if there is one.
1139 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1140 if (!(flags & DT_PRINTF_AGGREGATION)) {
1141 xyerror(D_PRINTF_AGG_CONV,
1142 "%%@ conversion requires an aggregation"
1143 " and is not for use with %s( )\n", func);
1145 (void) strlcpy(vname, "aggregating action",
1148 } else if (dnp == NULL) {
1149 xyerror(D_PRINTF_ARG_PROTO,
1150 "%s( ) prototype mismatch: conversion #%d (%%"
1151 "%s) is missing a corresponding value argument\n",
1152 func, i + 1, pfc->pfc_name);
1154 (void) snprintf(vname, sizeof (vname),
1155 "argument #%d", j + foff + 1);
1162 * Fill in the proposed final format string by prepending any
1163 * size-related prefixes to the pfconv's format string. The
1164 * pfc_check() function below may optionally modify the format
1165 * as part of validating the type of the input argument.
1167 if (pfc->pfc_print == &pfprint_sint ||
1168 pfc->pfc_print == &pfprint_uint ||
1169 pfc->pfc_print == &pfprint_dint) {
1170 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1171 (void) strcpy(pfd->pfd_fmt, "ll");
1172 } else if (pfc->pfc_print == &pfprint_fp) {
1173 if (dt_node_type_size(vnp) == sizeof (long double))
1174 (void) strcpy(pfd->pfd_fmt, "L");
1177 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1180 * Validate the format conversion against the value node type.
1181 * If the conversion is good, create the descriptor format
1182 * string by concatenating together any required printf(3C)
1183 * size prefixes with the conversion's native format string.
1185 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1186 xyerror(D_PRINTF_ARG_TYPE,
1187 "%s( ) %s is incompatible with "
1188 "conversion #%d prototype:\n\tconversion: %%%s\n"
1189 "\t prototype: %s\n\t argument: %s\n", func,
1190 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1191 dt_node_type_name(vnp, n, sizeof (n)));
1195 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1196 xyerror(D_PRINTF_ARG_EXTRA,
1197 "%s( ) prototype mismatch: only %d arguments "
1198 "required by this format string\n", func, j);
1203 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1205 dt_ident_t *lid, *rid;
1206 dt_node_t *lproto, *rproto;
1207 int largc, rargc, argn;
1208 char n1[DT_TYPE_NAMELEN];
1209 char n2[DT_TYPE_NAMELEN];
1211 assert(lhs->dn_kind == DT_NODE_AGG);
1212 assert(rhs->dn_kind == DT_NODE_AGG);
1214 lid = lhs->dn_ident;
1215 rid = rhs->dn_ident;
1217 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1218 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1221 * First, get an argument count on each side. These must match.
1223 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1226 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1229 if (largc != rargc) {
1230 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1231 "matching key signatures: @%s has %d key%s, @%s has %d "
1232 "key%s", lid->di_name, rid->di_name,
1233 lid->di_name, largc, largc == 1 ? "" : "s",
1234 rid->di_name, rargc, rargc == 1 ? "" : "s");
1238 * Now iterate over the keys to verify that each type matches.
1240 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1241 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1243 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1244 rproto = rproto->dn_list) {
1245 assert(rproto != NULL);
1247 if (dt_node_is_argcompat(lproto, rproto))
1250 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1251 "incompatible with @%s:\n%9s key #%d: %s\n"
1252 "%9s key #%d: %s\n",
1253 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1254 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1255 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1260 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1261 uint_t nrecs, const void *buf, size_t len, int *ip)
1266 return (dt_set_errno(dtp, EDT_DMISMATCH));
1268 addr = (uintptr_t)buf + recp->dtrd_offset;
1270 if (addr + sizeof (int) > (uintptr_t)buf + len)
1271 return (dt_set_errno(dtp, EDT_DOFFSET));
1273 if (addr & (recp->dtrd_alignment - 1))
1274 return (dt_set_errno(dtp, EDT_DALIGN));
1276 switch (recp->dtrd_size) {
1277 case sizeof (int8_t):
1278 *ip = (int)*((int8_t *)addr);
1280 case sizeof (int16_t):
1281 *ip = (int)*((int16_t *)addr);
1283 case sizeof (int32_t):
1284 *ip = (int)*((int32_t *)addr);
1286 case sizeof (int64_t):
1287 *ip = (int)*((int64_t *)addr);
1290 return (dt_set_errno(dtp, EDT_DMISMATCH));
1298 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1299 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1301 const uint64_t *data = addr;
1303 if (size != sizeof (uint64_t) * 2)
1304 return (dt_set_errno(dtp, EDT_DMISMATCH));
1306 return (dt_printf(dtp, fp, format,
1307 data[0] ? data[1] / normal / data[0] : 0));
1312 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1313 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1315 const uint64_t *data = addr;
1317 if (size != sizeof (uint64_t) * 4)
1318 return (dt_set_errno(dtp, EDT_DMISMATCH));
1320 return (dt_printf(dtp, fp, format,
1321 dt_stddev((uint64_t *)data, normal)));
1326 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1329 return (dt_print_quantize(dtp, fp, addr, size, normal));
1334 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1335 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1337 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1342 pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1343 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1345 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1349 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1350 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1351 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1353 dt_pfargd_t *pfd = pfv->pfv_argv;
1354 const dtrace_recdesc_t *recp = recs;
1355 const dtrace_aggdata_t *aggdata;
1356 dtrace_aggdesc_t *agg;
1357 caddr_t lim = (caddr_t)buf + len, limit;
1358 char format[64] = "%";
1360 int i, aggrec, curagg = -1;
1364 * If we are formatting an aggregation, set 'aggrec' to the index of
1365 * the final record description (the aggregation result) so we can use
1366 * this record index with any conversion where DT_PFCONV_AGG is set.
1367 * (The actual aggregation used will vary as we increment through the
1368 * aggregation variables that we have been passed.) Finally, we
1369 * decrement nrecs to prevent this record from being used with any
1372 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1373 assert(aggsdata != NULL);
1374 assert(naggvars > 0);
1377 return (dt_set_errno(dtp, EDT_DMISMATCH));
1379 curagg = naggvars > 1 ? 1 : 0;
1380 aggdata = aggsdata[0];
1381 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1385 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1386 const dt_pfconv_t *pfc = pfd->pfd_conv;
1387 int width = pfd->pfd_width;
1388 int prec = pfd->pfd_prec;
1392 char *f = format + 1; /* skip initial '%' */
1393 size_t fmtsz = sizeof(format) - 1;
1394 const dtrace_recdesc_t *rec;
1400 if (pfd->pfd_preflen != 0) {
1401 char *tmp = alloca(pfd->pfd_preflen + 1);
1403 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1404 tmp[pfd->pfd_preflen] = '\0';
1406 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1409 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1411 * For printa(), we flush the buffer after each
1412 * prefix, setting the flags to indicate that
1413 * this is part of the printa() format string.
1415 flags = DTRACE_BUFDATA_AGGFORMAT;
1417 if (pfc == NULL && i == pfv->pfv_argc - 1)
1418 flags |= DTRACE_BUFDATA_AGGLAST;
1420 if (dt_buffered_flush(dtp, NULL, NULL,
1421 aggdata, flags) < 0)
1427 if (pfv->pfv_argc == 1)
1428 return (nrecs != 0);
1433 * If the conversion is %%, just invoke the print callback
1434 * with no data record and continue; it consumes no record.
1436 if (pfc->pfc_print == &pfprint_pct) {
1437 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1439 return (-1); /* errno is set for us */
1442 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1443 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1445 return (-1); /* errno is set for us */
1446 pfd->pfd_dynwidth = width;
1448 pfd->pfd_dynwidth = 0;
1451 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1452 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1453 return (-1); /* errno is set for us */
1455 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1457 * This should be impossible -- the compiler shouldn't
1458 * create a DT_PFCONV_AGG conversion without an
1459 * aggregation present. Still, we'd rather fail
1460 * gracefully than blow up...
1462 if (aggsdata == NULL)
1463 return (dt_set_errno(dtp, EDT_DMISMATCH));
1465 aggdata = aggsdata[curagg];
1466 agg = aggdata->dtada_desc;
1469 * We increment the current aggregation variable, but
1470 * not beyond the number of aggregation variables that
1471 * we're printing. This has the (desired) effect that
1472 * DT_PFCONV_AGG conversions beyond the number of
1473 * aggregation variables (re-)convert the aggregation
1474 * value of the last aggregation variable.
1476 if (curagg < naggvars - 1)
1479 rec = &agg->dtagd_rec[aggrec];
1480 addr = aggdata->dtada_data + rec->dtrd_offset;
1481 limit = addr + aggdata->dtada_size;
1482 normal = aggdata->dtada_normal;
1483 flags = DTRACE_BUFDATA_AGGVAL;
1486 return (dt_set_errno(dtp, EDT_DMISMATCH));
1488 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1490 * When printing aggregation keys, we always
1491 * set the aggdata to be the representative
1492 * (zeroth) aggregation. The aggdata isn't
1493 * actually used here in this case, but it is
1494 * passed to the buffer handler and must
1495 * therefore still be correct.
1497 aggdata = aggsdata[0];
1498 flags = DTRACE_BUFDATA_AGGKEY;
1503 addr = (caddr_t)buf + rec->dtrd_offset;
1508 size = rec->dtrd_size;
1510 if (addr + size > limit) {
1511 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1512 (void *)addr, rec->dtrd_size, (void *)lim);
1513 return (dt_set_errno(dtp, EDT_DOFFSET));
1516 if (rec->dtrd_alignment != 0 &&
1517 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1518 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1519 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1520 return (dt_set_errno(dtp, EDT_DALIGN));
1523 switch (rec->dtrd_action) {
1525 func = pfprint_average;
1527 case DTRACEAGG_STDDEV:
1528 func = pfprint_stddev;
1530 case DTRACEAGG_QUANTIZE:
1531 func = pfprint_quantize;
1533 case DTRACEAGG_LQUANTIZE:
1534 func = pfprint_lquantize;
1536 case DTRACEAGG_LLQUANTIZE:
1537 func = pfprint_llquantize;
1542 case DTRACEACT_UMOD:
1543 func = pfprint_umod;
1546 func = pfc->pfc_print;
1551 if (pfd->pfd_flags & DT_PFCONV_ALT)
1553 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1555 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1557 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1559 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1561 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1566 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1567 * don't add the width to the format string. See the block
1568 * comment in pfprint_stack() for a description of the
1569 * behavior in this case.
1571 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1575 ret = snprintf(f, fmtsz, "%d", ABS(width));
1577 fmtsz = MAX(0, fmtsz - ret);
1581 ret = snprintf(f, fmtsz, ".%d", prec);
1583 fmtsz = MAX(0, fmtsz - ret);
1586 if (strlcpy(f, pfd->pfd_fmt, fmtsz) >= fmtsz)
1587 return (dt_set_errno(dtp, EDT_COMPILER));
1590 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1591 return (-1); /* errno is set for us */
1593 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1595 * For printa(), we flush the buffer after each tuple
1596 * element, inidicating that this is the last record
1599 if (i == pfv->pfv_argc - 1)
1600 flags |= DTRACE_BUFDATA_AGGLAST;
1602 if (dt_buffered_flush(dtp, NULL,
1603 rec, aggdata, flags) < 0)
1608 return ((int)(recp - recs));
1612 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1613 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1615 dtrace_optval_t size;
1618 rval = dtrace_getopt(dtp, "strsize", &size);
1620 assert(dtp->dt_sprintf_buflen == 0);
1622 if (dtp->dt_sprintf_buf != NULL)
1623 free(dtp->dt_sprintf_buf);
1625 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1626 return (dt_set_errno(dtp, EDT_NOMEM));
1628 bzero(dtp->dt_sprintf_buf, size);
1629 dtp->dt_sprintf_buflen = size;
1630 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1632 dtp->dt_sprintf_buflen = 0;
1635 free(dtp->dt_sprintf_buf);
1642 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1643 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1644 uint_t nrecs, const void *buf, size_t len)
1646 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1652 * Before we execute the specified command, flush fp to assure that
1653 * any prior dt_printf()'s appear before the output of the command
1658 if (system(dtp->dt_sprintf_buf) == -1)
1659 return (dt_set_errno(dtp, errno));
1665 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1666 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1667 uint_t nrecs, const void *buf, size_t len)
1669 char selfbuf[40], restorebuf[40], *filename;
1672 dt_pfargv_t *pfv = fmtdata;
1673 dt_pfargd_t *pfd = pfv->pfv_argv;
1675 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1677 if (rval == -1 || fp == NULL)
1681 if (pfd->pfd_preflen != 0 &&
1682 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1684 * The only way to have the format string set to the value
1685 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1686 * denoting that we should restore the old stdout.
1688 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1690 if (dtp->dt_stdout_fd == -1) {
1692 * We could complain here by generating an error,
1693 * but it seems like overkill: it seems that calling
1694 * freopen() to restore stdout when freopen() has
1695 * never before been called should just be a no-op,
1696 * so we just return in this case.
1701 (void) snprintf(restorebuf, sizeof (restorebuf),
1702 "/dev/fd/%d", dtp->dt_stdout_fd);
1703 filename = restorebuf;
1705 filename = dtp->dt_sprintf_buf;
1709 * freopen(3C) will always close the specified stream and underlying
1710 * file descriptor -- even if the specified file can't be opened.
1711 * Even for the semantic cesspool that is standard I/O, this is
1712 * surprisingly brain-dead behavior: it means that any failure to
1713 * open the specified file destroys the specified stream in the
1714 * process -- which is particularly relevant when the specified stream
1715 * happens (or rather, happened) to be stdout. This could be resolved
1716 * were there an "fdreopen()" equivalent of freopen() that allowed one
1717 * to pass a file descriptor instead of the name of a file, but there
1718 * is no such thing. However, we can effect this ourselves by first
1719 * fopen()'ing the desired file, and then (assuming that that works),
1720 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1721 * file descriptor for the fopen()'d file. This way, if the fopen()
1722 * fails, we can fail the operation without destroying stdout.
1724 if ((nfp = fopen(filename, "aF")) == NULL) {
1725 char *msg = strerror(errno);
1729 len += strlen(msg) + strlen(filename);
1730 faultstr = alloca(len);
1732 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1733 filename, strerror(errno));
1735 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1741 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1743 if (dtp->dt_stdout_fd == -1) {
1745 * If this is the first time that we're calling freopen(),
1746 * we're going to stash away the file descriptor for stdout.
1747 * We don't expect the dup(2) to fail, so if it does we must
1750 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1752 return (dt_set_errno(dtp, errno));
1756 if (freopen(selfbuf, "aF", fp) == NULL) {
1758 return (dt_set_errno(dtp, errno));
1762 #else /* !illumos */
1764 * The 'standard output' (which is not necessarily stdout)
1765 * treatment on FreeBSD is implemented differently than on
1766 * Solaris because FreeBSD's freopen() will attempt to re-use
1767 * the current file descriptor, causing the previous file to
1768 * be closed and thereby preventing it from be re-activated
1771 * For FreeBSD we use the concept of setting an output file
1772 * pointer in the DTrace handle if a dtrace_freopen() has
1773 * enabled another output file and we leave the caller's
1774 * file pointer untouched. If it was actually stdout, then
1775 * stdout remains open. If it was another file, then that
1776 * file remains open. While a dtrace_freopen() has activated
1777 * another file, we keep a pointer to that which we use in
1778 * the output functions by preference and only use the caller's
1779 * file pointer if no dtrace_freopen() call has been made.
1781 * The check to see if we're re-activating the caller's
1782 * output file is much the same as on Solaris.
1784 if (pfd->pfd_preflen != 0 &&
1785 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1787 * The only way to have the format string set to the value
1788 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1789 * denoting that we should restore the old stdout.
1791 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1793 if (dtp->dt_freopen_fp == NULL) {
1795 * We could complain here by generating an error,
1796 * but it seems like overkill: it seems that calling
1797 * freopen() to restore stdout when freopen() has
1798 * never before been called should just be a no-op,
1799 * so we just return in this case.
1805 * At this point, to re-active the original output file,
1806 * on FreeBSD we only code the current file that this
1807 * function opened previously.
1809 (void) fclose(dtp->dt_freopen_fp);
1810 dtp->dt_freopen_fp = NULL;
1815 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1816 char *msg = strerror(errno);
1820 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1821 faultstr = alloca(len);
1823 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1824 dtp->dt_sprintf_buf, strerror(errno));
1826 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1832 if (dtp->dt_freopen_fp != NULL)
1833 (void) fclose(dtp->dt_freopen_fp);
1835 /* Remember that the output has been redirected to the new file. */
1836 dtp->dt_freopen_fp = nfp;
1837 #endif /* illumos */
1844 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1845 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1846 uint_t nrecs, const void *buf, size_t len)
1848 return (dt_printf_format(dtp, fp, fmtdata,
1849 recp, nrecs, buf, len, NULL, 0));
1853 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1855 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1860 return (NULL); /* errno has been set for us */
1862 pfd = pfv->pfv_argv;
1864 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1865 const dt_pfconv_t *pfc = pfd->pfd_conv;
1871 * If the output format is not %s then we assume that we have
1872 * been given a correctly-sized format string, so we copy the
1873 * true format name including the size modifier. If the output
1874 * format is %s, then either the input format is %s as well or
1875 * it is one of our custom formats (e.g. pfprint_addr), so we
1876 * must set pfd_fmt to be the output format conversion "s".
1878 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1879 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1881 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1888 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1890 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1893 return (NULL); /* errno has been set for us */
1895 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1902 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1904 dt_pfargv_t *pfv = fmtdata;
1905 dt_pfargd_t *pfd = pfv->pfv_argv;
1908 * An upper bound on the string length is the length of the original
1909 * format string, plus three times the number of conversions (each
1910 * conversion could add up an additional "ll" and/or pfd_width digit
1911 * in the case of converting %? to %16) plus one for a terminating \0.
1913 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1914 char *format = alloca(formatlen);
1918 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1919 const dt_pfconv_t *pfc = pfd->pfd_conv;
1921 int width = pfd->pfd_width;
1922 int prec = pfd->pfd_prec;
1924 if (pfd->pfd_preflen != 0) {
1925 for (j = 0; j < pfd->pfd_preflen; j++)
1926 *f++ = pfd->pfd_prefix[j];
1934 if (pfd->pfd_flags & DT_PFCONV_ALT)
1936 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1938 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1940 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1942 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1944 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1948 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1950 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1952 if (pfd->pfd_flags & DT_PFCONV_AGG)
1956 f += snprintf(f, sizeof (format), "%d", width);
1959 f += snprintf(f, sizeof (format), ".%d", prec);
1962 * If the output format is %s, then either %s is the underlying
1963 * conversion or the conversion is one of our customized ones,
1964 * e.g. pfprint_addr. In these cases, put the original string
1965 * name of the conversion (pfc_name) into the pickled format
1966 * string rather than the derived conversion (pfd_fmt).
1968 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1969 str = pfc->pfc_name;
1973 for (j = 0; str[j] != '\0'; j++)
1977 *f = '\0'; /* insert nul byte; do not count in return value */
1979 assert(f < format + formatlen);
1980 (void) strncpy(s, format, len);
1982 return ((size_t)(f - format));
1986 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1988 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1989 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1990 uint_t nrecs = agg->dtagd_nrecs;
1991 dt_pfwalk_t *pfw = arg;
1992 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1995 if (dt_printf_getint(dtp, recp++, nrecs--,
1996 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1997 return (0); /* no aggregation id or id does not match */
1999 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
2000 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
2001 return (pfw->pfw_err = dtp->dt_errno);
2004 * Cast away the const to set the bit indicating that this aggregation
2007 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2013 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
2015 const dtrace_aggdata_t *aggdata = aggsdata[0];
2016 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2017 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
2018 uint_t nrecs = agg->dtagd_nrecs - 1;
2019 dt_pfwalk_t *pfw = arg;
2020 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
2023 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
2024 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
2025 aggsdata, naggvars) == -1)
2026 return (pfw->pfw_err = dtp->dt_errno);
2029 * For each aggregation, indicate that it has been printed, casting
2030 * away the const as necessary.
2032 for (i = 1; i < naggvars; i++) {
2033 agg = aggsdata[i]->dtada_desc;
2034 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2041 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2042 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2043 uint_t nrecs, const void *buf, size_t len)
2046 int i, naggvars = 0;
2047 dtrace_aggvarid_t *aggvars;
2049 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2052 * This might be a printa() with multiple aggregation variables. We
2053 * need to scan forward through the records until we find a record from
2054 * a different statement.
2056 for (i = 0; i < nrecs; i++) {
2057 const dtrace_recdesc_t *nrec = &recs[i];
2059 if (nrec->dtrd_uarg != recs->dtrd_uarg)
2062 if (nrec->dtrd_action != recs->dtrd_action)
2063 return (dt_set_errno(dtp, EDT_BADAGG));
2065 aggvars[naggvars++] =
2066 /* LINTED - alignment */
2067 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2071 return (dt_set_errno(dtp, EDT_BADAGG));
2073 pfw.pfw_argv = fmtdata;
2077 if (naggvars == 1) {
2078 pfw.pfw_aid = aggvars[0];
2080 if (dtrace_aggregate_walk_sorted(dtp,
2081 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2082 return (-1); /* errno is set for us */
2084 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2085 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2086 return (-1); /* errno is set for us */