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) 2011, Joyent, Inc. All rights reserved.
28 #include <sys/sysmacros.h>
30 #define ABS(a) ((a) < 0 ? -(a) : (a))
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <arpa/nameser.h>
48 #include <dt_printf.h>
49 #include <dt_string.h>
54 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
56 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
61 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
63 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
64 dt_node_is_symaddr(dnp));
69 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
71 dtrace_hdl_t *dtp = pfv->pfv_dtp;
72 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
74 if (dt_node_is_usymaddr(dnp))
77 if (idp == NULL || idp->di_id == 0)
80 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
85 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
87 return (dt_node_is_stack(dnp));
92 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
94 return (dt_node_is_integer(dnp) &&
95 dt_node_type_size(dnp) == sizeof (uint64_t));
100 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
108 if (dt_node_is_string(dnp))
112 base = ctf_type_resolve(ctfp, dnp->dn_type);
113 kind = ctf_type_kind(ctfp, base);
115 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
116 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
117 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
122 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
124 ctf_file_t *ctfp = dnp->dn_ctfp;
125 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
126 uint_t kind = ctf_type_kind(ctfp, base);
131 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
132 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
133 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
134 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
139 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
141 return (dt_node_is_integer(dnp) &&
142 dt_node_type_size(dnp) <= sizeof (int));
147 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
149 return (dt_node_is_float(dnp));
154 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
156 return (dt_node_is_integer(dnp));
161 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
163 if (dnp->dn_flags & DT_NF_SIGNED)
164 pfd->pfd_flags |= DT_PFCONV_SIGNED;
166 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
168 return (dt_node_is_integer(dnp));
173 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
175 ctf_file_t *ctfp = dnp->dn_ctfp;
176 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
177 char n[DT_TYPE_NAMELEN];
179 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
180 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
181 strcmp(n, "unsigned short") == 0));
186 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
188 ctf_file_t *ctfp = dnp->dn_ctfp;
189 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
190 char n[DT_TYPE_NAMELEN];
192 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
193 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
194 strcmp(n, "unsigned long") == 0));
199 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
201 ctf_file_t *ctfp = dnp->dn_ctfp;
202 ctf_id_t type = dnp->dn_type;
203 char n[DT_TYPE_NAMELEN];
205 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
206 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
207 strcmp(n, "signed long long") == 0 ||
208 strcmp(n, "unsigned long long") == 0))
212 * If the type used for %llx or %llX is not an [unsigned] long long, we
213 * also permit it to be a [u]int64_t or any typedef thereof. We know
214 * that these typedefs are guaranteed to work with %ll[xX] in either
215 * compilation environment even though they alias to "long" in LP64.
217 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
218 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
219 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
222 type = ctf_type_reference(ctfp, type);
230 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
232 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
233 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
238 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
239 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
241 int64_t normal = (int64_t)unormal;
242 int32_t n = (int32_t)normal;
245 case sizeof (int8_t):
246 return (dt_printf(dtp, fp, format,
247 (int32_t)*((int8_t *)addr) / n));
248 case sizeof (int16_t):
249 return (dt_printf(dtp, fp, format,
250 (int32_t)*((int16_t *)addr) / n));
251 case sizeof (int32_t):
252 return (dt_printf(dtp, fp, format,
253 *((int32_t *)addr) / n));
254 case sizeof (int64_t):
255 return (dt_printf(dtp, fp, format,
256 *((int64_t *)addr) / normal));
258 return (dt_set_errno(dtp, EDT_DMISMATCH));
264 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
265 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
267 uint32_t n = (uint32_t)normal;
270 case sizeof (uint8_t):
271 return (dt_printf(dtp, fp, format,
272 (uint32_t)*((uint8_t *)addr) / n));
273 case sizeof (uint16_t):
274 return (dt_printf(dtp, fp, format,
275 (uint32_t)*((uint16_t *)addr) / n));
276 case sizeof (uint32_t):
277 return (dt_printf(dtp, fp, format,
278 *((uint32_t *)addr) / n));
279 case sizeof (uint64_t):
280 return (dt_printf(dtp, fp, format,
281 *((uint64_t *)addr) / normal));
283 return (dt_set_errno(dtp, EDT_DMISMATCH));
288 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
289 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
291 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
292 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
294 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
299 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
300 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
302 double n = (double)normal;
303 long double ldn = (long double)normal;
307 return (dt_printf(dtp, fp, format,
308 (double)*((float *)addr) / n));
309 case sizeof (double):
310 return (dt_printf(dtp, fp, format,
311 *((double *)addr) / n));
312 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
313 case sizeof (long double):
314 return (dt_printf(dtp, fp, format,
315 *((long double *)addr) / ldn));
318 return (dt_set_errno(dtp, EDT_DMISMATCH));
324 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
325 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
332 case sizeof (uint32_t):
333 val = *((uint32_t *)addr);
335 case sizeof (uint64_t):
336 val = *((uint64_t *)addr);
339 return (dt_set_errno(dtp, EDT_DMISMATCH));
345 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
347 return (dt_printf(dtp, fp, format, s));
352 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
353 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
355 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
360 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
361 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
363 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
368 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
369 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
373 uint64_t val, pid = 0;
375 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
378 case sizeof (uint32_t):
379 val = (u_longlong_t)*((uint32_t *)addr);
381 case sizeof (uint64_t):
382 val = (u_longlong_t)*((uint64_t *)addr);
384 case sizeof (uint64_t) * 2:
385 pid = ((uint64_t *)(uintptr_t)addr)[0];
386 val = ((uint64_t *)(uintptr_t)addr)[1];
389 return (dt_set_errno(dtp, EDT_DMISMATCH));
392 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
398 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
400 return (dt_printf(dtp, fp, format, s));
405 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
406 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
409 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
410 const dtrace_recdesc_t *rec = pfd->pfd_rec;
411 caddr_t addr = (caddr_t)vaddr;
415 * We have stashed the value of the STACKINDENT option, and we will
416 * now override it for the purposes of formatting the stack. If the
417 * field has been specified as left-aligned (i.e. (%-#), we set the
418 * indentation to be the width. This is a slightly odd semantic, but
419 * it's useful functionality -- and it's slightly odd to begin with to
420 * be using a single format specifier to be formatting multiple lines
423 if (pfd->pfd_dynwidth < 0) {
424 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
425 width = -pfd->pfd_dynwidth;
426 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
427 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
432 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
434 switch (rec->dtrd_action) {
435 case DTRACEACT_USTACK:
436 case DTRACEACT_JSTACK:
437 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
440 case DTRACEACT_STACK:
441 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
442 rec->dtrd_size / rec->dtrd_arg);
449 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
456 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
457 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
459 char src[32], buf[32], *dst = buf;
460 hrtime_t time = *((uint64_t *)addr);
461 time_t sec = (time_t)(time / NANOSEC);
465 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
466 * Below, we turn this into the canonical adb/mdb /[yY] format,
467 * "1973 Dec 3 17:20:00".
470 (void) ctime_r(&sec, src, sizeof (src));
472 (void) ctime_r(&sec, src);
476 * Place the 4-digit year at the head of the string...
478 for (i = 20; i < 24; i++)
482 * ...and follow it with the remainder (month, day, hh:mm:ss).
484 for (i = 3; i < 19; i++)
488 return (dt_printf(dtp, fp, format, buf));
492 * This prints the time in RFC 822 standard form. This is useful for emitting
493 * notions of time that are consumed by standard tools (e.g., as part of an
498 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
499 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
501 hrtime_t time = *((uint64_t *)addr);
502 time_t sec = (time_t)(time / NANOSEC);
506 (void) localtime_r(&sec, &tm);
507 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
508 return (dt_printf(dtp, fp, format, buf));
513 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
514 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
516 uint16_t port = htons(*((uint16_t *)addr));
518 struct servent *sv, res;
521 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
523 if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
525 return (dt_printf(dtp, fp, format, sv->s_name));
527 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
528 return (dt_printf(dtp, fp, format, buf));
533 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
534 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
536 char *s = alloca(size + 1);
537 struct hostent *host, res;
538 char inetaddr[NS_IN6ADDRSZ];
542 bcopy(addr, s, size);
545 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
547 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
548 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
550 if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
551 AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
553 return (dt_printf(dtp, fp, format, host->h_name));
554 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
555 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
556 AF_INET6, &e)) != NULL)
557 return (dt_printf(dtp, fp, format, host->h_name));
560 return (dt_printf(dtp, fp, format, s));
565 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
566 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
568 char *s = alloca(size + 1);
570 bcopy(addr, s, size);
572 return (dt_printf(dtp, fp, format, s));
577 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
578 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
580 wchar_t *ws = alloca(size + sizeof (wchar_t));
582 bcopy(addr, ws, size);
583 ws[size / sizeof (wchar_t)] = L'\0';
584 return (dt_printf(dtp, fp, format, ws));
589 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
590 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
595 if ((s = strchr2esc(addr, size)) == NULL)
596 return (dt_set_errno(dtp, EDT_NOMEM));
598 n = dt_printf(dtp, fp, format, s);
604 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
605 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
610 case sizeof (int8_t):
613 case sizeof (int16_t):
614 c = *(int16_t *)addr;
616 case sizeof (int32_t):
617 c = *(int32_t *)addr;
620 return (dt_set_errno(dtp, EDT_DMISMATCH));
623 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
628 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
629 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
631 return (dt_printf(dtp, fp, "%%"));
634 static const char pfproto_xint[] = "char, short, int, long, or long long";
635 static const char pfproto_csi[] = "char, short, or int";
636 static const char pfproto_fp[] = "float, double, or long double";
637 static const char pfproto_addr[] = "pointer or integer";
638 static const char pfproto_uaddr[] =
639 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
640 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
641 static const char pfproto_wstr[] = "wchar_t []";
644 * Printf format conversion dictionary. This table should match the set of
645 * conversions offered by printf(3C), as well as some additional extensions.
646 * The second parameter is an ASCII string which is either an actual type
647 * name we should look up (if pfcheck_type is specified), or just a descriptive
648 * string of the types expected for use in error messages.
650 static const dt_pfconv_t _dtrace_conversions[] = {
651 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
652 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
653 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
654 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
655 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
656 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
657 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
658 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
659 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
660 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
661 { "hd", "d", "short", pfcheck_type, pfprint_sint },
662 { "hi", "i", "short", pfcheck_type, pfprint_sint },
663 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
664 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
665 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
666 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
667 { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
668 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
669 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
670 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
671 { "ld", "d", "long", pfcheck_type, pfprint_sint },
672 { "li", "i", "long", pfcheck_type, pfprint_sint },
673 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
674 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
675 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
676 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
677 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
678 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
679 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
680 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
681 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
682 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
683 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
684 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
685 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
686 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
687 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
688 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
689 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
690 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
691 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
692 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
693 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
694 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
695 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
696 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
697 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
698 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
699 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
700 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
701 { "%", "%", "void", pfcheck_type, pfprint_pct },
702 { NULL, NULL, NULL, NULL, NULL }
706 dt_pfdict_create(dtrace_hdl_t *dtp)
708 uint_t n = _dtrace_strbuckets;
709 const dt_pfconv_t *pfd;
712 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
713 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
715 return (dt_set_errno(dtp, EDT_NOMEM));
718 dtp->dt_pfdict = pdi;
719 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
720 pdi->pdi_nbuckets = n;
722 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
723 dtrace_typeinfo_t dtt;
727 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
728 dt_pfdict_destroy(dtp);
729 return (dt_set_errno(dtp, EDT_NOMEM));
732 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
733 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
734 pfc->pfc_next = pdi->pdi_buckets[h];
735 pdi->pdi_buckets[h] = pfc;
738 dtt.dtt_type = CTF_ERR;
741 * The "D" container or its parent must contain a definition of
742 * any type referenced by a printf conversion. If none can be
743 * found, we fail to initialize the printf dictionary.
745 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
746 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
747 dt_pfdict_destroy(dtp);
748 return (dt_set_errno(dtp, EDT_NOCONV));
751 pfc->pfc_dctfp = dtt.dtt_ctfp;
752 pfc->pfc_dtype = dtt.dtt_type;
755 * The "C" container may contain an alternate definition of an
756 * explicit conversion type. If it does, use it; otherwise
757 * just set pfc_ctype to pfc_dtype so it is always valid.
759 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
760 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
761 pfc->pfc_cctfp = dtt.dtt_ctfp;
762 pfc->pfc_ctype = dtt.dtt_type;
764 pfc->pfc_cctfp = pfc->pfc_dctfp;
765 pfc->pfc_ctype = pfc->pfc_dtype;
768 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
769 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
770 dt_pfdict_destroy(dtp);
771 return (dt_set_errno(dtp, EDT_BADCONV));
774 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
781 dt_pfdict_destroy(dtrace_hdl_t *dtp)
783 dt_pfdict_t *pdi = dtp->dt_pfdict;
784 dt_pfconv_t *pfc, *nfc;
790 for (i = 0; i < pdi->pdi_nbuckets; i++) {
791 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
797 free(pdi->pdi_buckets);
799 dtp->dt_pfdict = NULL;
802 static const dt_pfconv_t *
803 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
805 dt_pfdict_t *pdi = dtp->dt_pfdict;
806 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
807 const dt_pfconv_t *pfc;
809 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
810 if (strcmp(pfc->pfc_name, name) == 0)
818 dt_printf_error(dtrace_hdl_t *dtp, int err)
821 longjmp(yypcb->pcb_jmpbuf, err);
823 (void) dt_set_errno(dtp, err);
828 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
830 dt_pfargd_t *pfd, *nfd = NULL;
835 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
836 (format = strdup(s)) == NULL) {
838 return (dt_printf_error(dtp, EDT_NOMEM));
841 pfv->pfv_format = format;
842 pfv->pfv_argv = NULL;
847 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
856 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
857 dt_printf_destroy(pfv);
858 return (dt_printf_error(dtp, EDT_NOMEM));
861 if (pfv->pfv_argv != NULL)
866 bzero(pfd, sizeof (dt_pfargd_t));
871 pfd->pfd_preflen = (size_t)(p - q);
877 case '0': case '1': case '2': case '3': case '4':
878 case '5': case '6': case '7': case '8': case '9':
879 if (dot == 0 && digits == 0 && c == '0') {
880 pfd->pfd_flags |= DT_PFCONV_ZPAD;
881 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
885 for (n = 0; isdigit(c); c = *++p)
886 n = n * 10 + c - '0';
898 pfd->pfd_flags |= DT_PFCONV_ALT;
902 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
904 if (pfd->pfd_flags & n) {
905 yywarn("format conversion #%u has more than "
906 "one '*' specified for the output %s\n",
907 pfv->pfv_argc, n ? "precision" : "width");
909 dt_printf_destroy(pfv);
910 return (dt_printf_error(dtp, EDT_COMPILER));
917 pfd->pfd_flags |= DT_PFCONV_SPOS;
921 pfd->pfd_flags |= DT_PFCONV_LEFT;
922 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
927 yywarn("format conversion #%u has more than "
928 "one '.' specified\n", pfv->pfv_argc);
930 dt_printf_destroy(pfv);
931 return (dt_printf_error(dtp, EDT_COMPILER));
937 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
944 pfd->pfd_flags |= DT_PFCONV_AGG;
948 pfd->pfd_flags |= DT_PFCONV_GROUP;
952 pfd->pfd_flags |= DT_PFCONV_SPACE;
956 yywarn("format conversion #%u uses unsupported "
957 "positional format (%%n$)\n", pfv->pfv_argc);
959 dt_printf_destroy(pfv);
960 return (dt_printf_error(dtp, EDT_COMPILER));
964 goto default_lbl; /* if %% then use "%" conv */
966 yywarn("format conversion #%u cannot be combined "
967 "with other format flags: %%%%\n", pfv->pfv_argc);
969 dt_printf_destroy(pfv);
970 return (dt_printf_error(dtp, EDT_COMPILER));
973 yywarn("format conversion #%u name expected before "
974 "end of format string\n", pfv->pfv_argc);
976 dt_printf_destroy(pfv);
977 return (dt_printf_error(dtp, EDT_COMPILER));
983 if (namelen < sizeof (name) - 2)
990 name[namelen] = '\0';
993 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
995 if (pfd->pfd_conv == NULL) {
996 yywarn("format conversion #%u is undefined: %%%s\n",
997 pfv->pfv_argc, name);
998 dt_printf_destroy(pfv);
999 return (dt_printf_error(dtp, EDT_COMPILER));
1003 if (*q != '\0' || *format == '\0') {
1004 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1005 dt_printf_destroy(pfv);
1006 return (dt_printf_error(dtp, EDT_NOMEM));
1009 if (pfv->pfv_argv != NULL)
1010 nfd->pfd_next = pfd;
1012 pfv->pfv_argv = pfd;
1014 bzero(pfd, sizeof (dt_pfargd_t));
1017 pfd->pfd_prefix = q;
1018 pfd->pfd_preflen = strlen(q);
1025 dt_printf_destroy(dt_pfargv_t *pfv)
1027 dt_pfargd_t *pfd, *nfd;
1029 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1030 nfd = pfd->pfd_next;
1034 free(pfv->pfv_format);
1039 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1040 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1042 dt_pfargd_t *pfd = pfv->pfv_argv;
1043 const char *func = idp->di_name;
1045 char n[DT_TYPE_NAMELEN];
1046 dtrace_typeinfo_t dtt;
1047 const char *aggtype;
1051 if (pfv->pfv_format[0] == '\0') {
1052 xyerror(D_PRINTF_FMT_EMPTY,
1053 "%s( ) format string is empty\n", func);
1056 pfv->pfv_flags = flags;
1059 * We fake up a parse node representing the type that can be used with
1060 * an aggregation result conversion, which -- for all but count() --
1061 * is a signed quantity.
1063 if (kind != DTRACEAGG_COUNT)
1064 aggtype = "int64_t";
1066 aggtype = "uint64_t";
1068 if (dt_type_lookup(aggtype, &dtt) != 0)
1069 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1071 bzero(&aggnode, sizeof (aggnode));
1072 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
1074 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1075 const dt_pfconv_t *pfc = pfd->pfd_conv;
1076 const char *dyns[2];
1083 continue; /* no checking if argd is just a prefix */
1085 if (pfc->pfc_print == &pfprint_pct) {
1086 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1090 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1091 dyns[dync++] = ".*";
1092 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1095 for (; dync != 0; dync--) {
1097 xyerror(D_PRINTF_DYN_PROTO,
1098 "%s( ) prototype mismatch: conversion "
1099 "#%d (%%%s) is missing a corresponding "
1100 "\"%s\" argument\n", func, i + 1,
1101 pfc->pfc_name, dyns[dync - 1]);
1104 if (dt_node_is_integer(dnp) == 0) {
1105 xyerror(D_PRINTF_DYN_TYPE,
1106 "%s( ) argument #%d is incompatible "
1107 "with conversion #%d prototype:\n"
1108 "\tconversion: %% %s %s\n"
1109 "\t prototype: int\n\t argument: %s\n",
1110 func, j + foff + 1, i + 1,
1111 dyns[dync - 1], pfc->pfc_name,
1112 dt_node_type_name(dnp, n, sizeof (n)));
1120 * If this conversion is consuming the aggregation data, set
1121 * the value node pointer (vnp) to a fake node based on the
1122 * aggregating function result type. Otherwise assign vnp to
1123 * the next parse node in the argument list, if there is one.
1125 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1126 if (!(flags & DT_PRINTF_AGGREGATION)) {
1127 xyerror(D_PRINTF_AGG_CONV,
1128 "%%@ conversion requires an aggregation"
1129 " and is not for use with %s( )\n", func);
1131 (void) strlcpy(vname, "aggregating action",
1134 } else if (dnp == NULL) {
1135 xyerror(D_PRINTF_ARG_PROTO,
1136 "%s( ) prototype mismatch: conversion #%d (%%"
1137 "%s) is missing a corresponding value argument\n",
1138 func, i + 1, pfc->pfc_name);
1140 (void) snprintf(vname, sizeof (vname),
1141 "argument #%d", j + foff + 1);
1148 * Fill in the proposed final format string by prepending any
1149 * size-related prefixes to the pfconv's format string. The
1150 * pfc_check() function below may optionally modify the format
1151 * as part of validating the type of the input argument.
1153 if (pfc->pfc_print == &pfprint_sint ||
1154 pfc->pfc_print == &pfprint_uint ||
1155 pfc->pfc_print == &pfprint_dint) {
1156 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1157 (void) strcpy(pfd->pfd_fmt, "ll");
1158 } else if (pfc->pfc_print == &pfprint_fp) {
1159 if (dt_node_type_size(vnp) == sizeof (long double))
1160 (void) strcpy(pfd->pfd_fmt, "L");
1163 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1166 * Validate the format conversion against the value node type.
1167 * If the conversion is good, create the descriptor format
1168 * string by concatenating together any required printf(3C)
1169 * size prefixes with the conversion's native format string.
1171 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1172 xyerror(D_PRINTF_ARG_TYPE,
1173 "%s( ) %s is incompatible with "
1174 "conversion #%d prototype:\n\tconversion: %%%s\n"
1175 "\t prototype: %s\n\t argument: %s\n", func,
1176 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1177 dt_node_type_name(vnp, n, sizeof (n)));
1181 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1182 xyerror(D_PRINTF_ARG_EXTRA,
1183 "%s( ) prototype mismatch: only %d arguments "
1184 "required by this format string\n", func, j);
1189 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1191 dt_ident_t *lid, *rid;
1192 dt_node_t *lproto, *rproto;
1193 int largc, rargc, argn;
1194 char n1[DT_TYPE_NAMELEN];
1195 char n2[DT_TYPE_NAMELEN];
1197 assert(lhs->dn_kind == DT_NODE_AGG);
1198 assert(rhs->dn_kind == DT_NODE_AGG);
1200 lid = lhs->dn_ident;
1201 rid = rhs->dn_ident;
1203 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1204 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1207 * First, get an argument count on each side. These must match.
1209 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1212 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1215 if (largc != rargc) {
1216 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1217 "matching key signatures: @%s has %d key%s, @%s has %d "
1218 "key%s", lid->di_name, rid->di_name,
1219 lid->di_name, largc, largc == 1 ? "" : "s",
1220 rid->di_name, rargc, rargc == 1 ? "" : "s");
1224 * Now iterate over the keys to verify that each type matches.
1226 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1227 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1229 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1230 rproto = rproto->dn_list) {
1231 assert(rproto != NULL);
1233 if (dt_node_is_argcompat(lproto, rproto))
1236 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1237 "incompatible with @%s:\n%9s key #%d: %s\n"
1238 "%9s key #%d: %s\n",
1239 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1240 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1241 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1246 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1247 uint_t nrecs, const void *buf, size_t len, int *ip)
1252 return (dt_set_errno(dtp, EDT_DMISMATCH));
1254 addr = (uintptr_t)buf + recp->dtrd_offset;
1256 if (addr + sizeof (int) > (uintptr_t)buf + len)
1257 return (dt_set_errno(dtp, EDT_DOFFSET));
1259 if (addr & (recp->dtrd_alignment - 1))
1260 return (dt_set_errno(dtp, EDT_DALIGN));
1262 switch (recp->dtrd_size) {
1263 case sizeof (int8_t):
1264 *ip = (int)*((int8_t *)addr);
1266 case sizeof (int16_t):
1267 *ip = (int)*((int16_t *)addr);
1269 case sizeof (int32_t):
1270 *ip = (int)*((int32_t *)addr);
1272 case sizeof (int64_t):
1273 *ip = (int)*((int64_t *)addr);
1276 return (dt_set_errno(dtp, EDT_DMISMATCH));
1284 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1285 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1287 const uint64_t *data = addr;
1289 if (size != sizeof (uint64_t) * 2)
1290 return (dt_set_errno(dtp, EDT_DMISMATCH));
1292 return (dt_printf(dtp, fp, format,
1293 data[0] ? data[1] / normal / data[0] : 0));
1298 pfprint_stddev(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) * 4)
1304 return (dt_set_errno(dtp, EDT_DMISMATCH));
1306 return (dt_printf(dtp, fp, format,
1307 dt_stddev((uint64_t *)data, normal)));
1312 pfprint_quantize(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 return (dt_print_quantize(dtp, fp, addr, size, normal));
1320 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1321 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1323 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1328 pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1329 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1331 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1335 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1336 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1337 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1339 dt_pfargd_t *pfd = pfv->pfv_argv;
1340 const dtrace_recdesc_t *recp = recs;
1341 const dtrace_aggdata_t *aggdata;
1342 dtrace_aggdesc_t *agg;
1343 caddr_t lim = (caddr_t)buf + len, limit;
1344 char format[64] = "%";
1345 int i, aggrec, curagg = -1;
1349 * If we are formatting an aggregation, set 'aggrec' to the index of
1350 * the final record description (the aggregation result) so we can use
1351 * this record index with any conversion where DT_PFCONV_AGG is set.
1352 * (The actual aggregation used will vary as we increment through the
1353 * aggregation variables that we have been passed.) Finally, we
1354 * decrement nrecs to prevent this record from being used with any
1357 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1358 assert(aggsdata != NULL);
1359 assert(naggvars > 0);
1362 return (dt_set_errno(dtp, EDT_DMISMATCH));
1364 curagg = naggvars > 1 ? 1 : 0;
1365 aggdata = aggsdata[0];
1366 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1370 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1371 const dt_pfconv_t *pfc = pfd->pfd_conv;
1372 int width = pfd->pfd_width;
1373 int prec = pfd->pfd_prec;
1376 char *f = format + 1; /* skip initial '%' */
1377 const dtrace_recdesc_t *rec;
1383 if (pfd->pfd_preflen != 0) {
1384 char *tmp = alloca(pfd->pfd_preflen + 1);
1386 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1387 tmp[pfd->pfd_preflen] = '\0';
1389 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1392 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1394 * For printa(), we flush the buffer after each
1395 * prefix, setting the flags to indicate that
1396 * this is part of the printa() format string.
1398 flags = DTRACE_BUFDATA_AGGFORMAT;
1400 if (pfc == NULL && i == pfv->pfv_argc - 1)
1401 flags |= DTRACE_BUFDATA_AGGLAST;
1403 if (dt_buffered_flush(dtp, NULL, NULL,
1404 aggdata, flags) < 0)
1410 if (pfv->pfv_argc == 1)
1411 return (nrecs != 0);
1416 * If the conversion is %%, just invoke the print callback
1417 * with no data record and continue; it consumes no record.
1419 if (pfc->pfc_print == &pfprint_pct) {
1420 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1422 return (-1); /* errno is set for us */
1425 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1426 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1428 return (-1); /* errno is set for us */
1429 pfd->pfd_dynwidth = width;
1431 pfd->pfd_dynwidth = 0;
1434 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1435 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1436 return (-1); /* errno is set for us */
1438 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1440 * This should be impossible -- the compiler shouldn't
1441 * create a DT_PFCONV_AGG conversion without an
1442 * aggregation present. Still, we'd rather fail
1443 * gracefully than blow up...
1445 if (aggsdata == NULL)
1446 return (dt_set_errno(dtp, EDT_DMISMATCH));
1448 aggdata = aggsdata[curagg];
1449 agg = aggdata->dtada_desc;
1452 * We increment the current aggregation variable, but
1453 * not beyond the number of aggregation variables that
1454 * we're printing. This has the (desired) effect that
1455 * DT_PFCONV_AGG conversions beyond the number of
1456 * aggregation variables (re-)convert the aggregation
1457 * value of the last aggregation variable.
1459 if (curagg < naggvars - 1)
1462 rec = &agg->dtagd_rec[aggrec];
1463 addr = aggdata->dtada_data + rec->dtrd_offset;
1464 limit = addr + aggdata->dtada_size;
1465 normal = aggdata->dtada_normal;
1466 flags = DTRACE_BUFDATA_AGGVAL;
1469 return (dt_set_errno(dtp, EDT_DMISMATCH));
1471 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1473 * When printing aggregation keys, we always
1474 * set the aggdata to be the representative
1475 * (zeroth) aggregation. The aggdata isn't
1476 * actually used here in this case, but it is
1477 * passed to the buffer handler and must
1478 * therefore still be correct.
1480 aggdata = aggsdata[0];
1481 flags = DTRACE_BUFDATA_AGGKEY;
1486 addr = (caddr_t)buf + rec->dtrd_offset;
1491 size = rec->dtrd_size;
1493 if (addr + size > limit) {
1494 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1495 (void *)addr, rec->dtrd_size, (void *)lim);
1496 return (dt_set_errno(dtp, EDT_DOFFSET));
1499 if (rec->dtrd_alignment != 0 &&
1500 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1501 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1502 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1503 return (dt_set_errno(dtp, EDT_DALIGN));
1506 switch (rec->dtrd_action) {
1508 func = pfprint_average;
1510 case DTRACEAGG_STDDEV:
1511 func = pfprint_stddev;
1513 case DTRACEAGG_QUANTIZE:
1514 func = pfprint_quantize;
1516 case DTRACEAGG_LQUANTIZE:
1517 func = pfprint_lquantize;
1519 case DTRACEAGG_LLQUANTIZE:
1520 func = pfprint_llquantize;
1525 case DTRACEACT_UMOD:
1526 func = pfprint_umod;
1529 func = pfc->pfc_print;
1533 if (pfd->pfd_flags & DT_PFCONV_ALT)
1535 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1537 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1539 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1541 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1543 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1547 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1548 * don't add the width to the format string. See the block
1549 * comment in pfprint_stack() for a description of the
1550 * behavior in this case.
1552 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1556 f += snprintf(f, sizeof (format), "%d", ABS(width));
1559 f += snprintf(f, sizeof (format), ".%d", prec);
1561 (void) strcpy(f, pfd->pfd_fmt);
1564 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1565 return (-1); /* errno is set for us */
1567 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1569 * For printa(), we flush the buffer after each tuple
1570 * element, inidicating that this is the last record
1573 if (i == pfv->pfv_argc - 1)
1574 flags |= DTRACE_BUFDATA_AGGLAST;
1576 if (dt_buffered_flush(dtp, NULL,
1577 rec, aggdata, flags) < 0)
1582 return ((int)(recp - recs));
1586 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1587 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1589 dtrace_optval_t size;
1592 rval = dtrace_getopt(dtp, "strsize", &size);
1594 assert(dtp->dt_sprintf_buflen == 0);
1596 if (dtp->dt_sprintf_buf != NULL)
1597 free(dtp->dt_sprintf_buf);
1599 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1600 return (dt_set_errno(dtp, EDT_NOMEM));
1602 bzero(dtp->dt_sprintf_buf, size);
1603 dtp->dt_sprintf_buflen = size;
1604 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1606 dtp->dt_sprintf_buflen = 0;
1609 free(dtp->dt_sprintf_buf);
1616 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1617 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1618 uint_t nrecs, const void *buf, size_t len)
1620 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1626 * Before we execute the specified command, flush fp to assure that
1627 * any prior dt_printf()'s appear before the output of the command
1632 if (system(dtp->dt_sprintf_buf) == -1)
1633 return (dt_set_errno(dtp, errno));
1639 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1640 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1641 uint_t nrecs, const void *buf, size_t len)
1643 char selfbuf[40], restorebuf[40], *filename;
1646 dt_pfargv_t *pfv = fmtdata;
1647 dt_pfargd_t *pfd = pfv->pfv_argv;
1649 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1651 if (rval == -1 || fp == NULL)
1655 if (pfd->pfd_preflen != 0 &&
1656 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1658 * The only way to have the format string set to the value
1659 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1660 * denoting that we should restore the old stdout.
1662 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1664 if (dtp->dt_stdout_fd == -1) {
1666 * We could complain here by generating an error,
1667 * but it seems like overkill: it seems that calling
1668 * freopen() to restore stdout when freopen() has
1669 * never before been called should just be a no-op,
1670 * so we just return in this case.
1675 (void) snprintf(restorebuf, sizeof (restorebuf),
1676 "/dev/fd/%d", dtp->dt_stdout_fd);
1677 filename = restorebuf;
1679 filename = dtp->dt_sprintf_buf;
1683 * freopen(3C) will always close the specified stream and underlying
1684 * file descriptor -- even if the specified file can't be opened.
1685 * Even for the semantic cesspool that is standard I/O, this is
1686 * surprisingly brain-dead behavior: it means that any failure to
1687 * open the specified file destroys the specified stream in the
1688 * process -- which is particularly relevant when the specified stream
1689 * happens (or rather, happened) to be stdout. This could be resolved
1690 * were there an "fdreopen()" equivalent of freopen() that allowed one
1691 * to pass a file descriptor instead of the name of a file, but there
1692 * is no such thing. However, we can effect this ourselves by first
1693 * fopen()'ing the desired file, and then (assuming that that works),
1694 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1695 * file descriptor for the fopen()'d file. This way, if the fopen()
1696 * fails, we can fail the operation without destroying stdout.
1698 if ((nfp = fopen(filename, "aF")) == NULL) {
1699 char *msg = strerror(errno);
1703 len += strlen(msg) + strlen(filename);
1704 faultstr = alloca(len);
1706 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1707 filename, strerror(errno));
1709 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1715 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1717 if (dtp->dt_stdout_fd == -1) {
1719 * If this is the first time that we're calling freopen(),
1720 * we're going to stash away the file descriptor for stdout.
1721 * We don't expect the dup(2) to fail, so if it does we must
1724 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1726 return (dt_set_errno(dtp, errno));
1730 if (freopen(selfbuf, "aF", fp) == NULL) {
1732 return (dt_set_errno(dtp, errno));
1738 * The 'standard output' (which is not necessarily stdout)
1739 * treatment on FreeBSD is implemented differently than on
1740 * Solaris because FreeBSD's freopen() will attempt to re-use
1741 * the current file descriptor, causing the previous file to
1742 * be closed and thereby preventing it from be re-activated
1745 * For FreeBSD we use the concept of setting an output file
1746 * pointer in the DTrace handle if a dtrace_freopen() has
1747 * enabled another output file and we leave the caller's
1748 * file pointer untouched. If it was actually stdout, then
1749 * stdout remains open. If it was another file, then that
1750 * file remains open. While a dtrace_freopen() has activated
1751 * another file, we keep a pointer to that which we use in
1752 * the output functions by preference and only use the caller's
1753 * file pointer if no dtrace_freopen() call has been made.
1755 * The check to see if we're re-activating the caller's
1756 * output file is much the same as on Solaris.
1758 if (pfd->pfd_preflen != 0 &&
1759 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1761 * The only way to have the format string set to the value
1762 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1763 * denoting that we should restore the old stdout.
1765 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1767 if (dtp->dt_freopen_fp == NULL) {
1769 * We could complain here by generating an error,
1770 * but it seems like overkill: it seems that calling
1771 * freopen() to restore stdout when freopen() has
1772 * never before been called should just be a no-op,
1773 * so we just return in this case.
1779 * At this point, to re-active the original output file,
1780 * on FreeBSD we only code the current file that this
1781 * function opened previously.
1783 (void) fclose(dtp->dt_freopen_fp);
1784 dtp->dt_freopen_fp = NULL;
1789 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1790 char *msg = strerror(errno);
1794 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1795 faultstr = alloca(len);
1797 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1798 dtp->dt_sprintf_buf, strerror(errno));
1800 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1806 if (dtp->dt_freopen_fp != NULL)
1807 (void) fclose(dtp->dt_freopen_fp);
1809 /* Remember that the output has been redirected to the new file. */
1810 dtp->dt_freopen_fp = nfp;
1818 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1819 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1820 uint_t nrecs, const void *buf, size_t len)
1822 return (dt_printf_format(dtp, fp, fmtdata,
1823 recp, nrecs, buf, len, NULL, 0));
1827 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1829 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1834 return (NULL); /* errno has been set for us */
1836 pfd = pfv->pfv_argv;
1838 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1839 const dt_pfconv_t *pfc = pfd->pfd_conv;
1845 * If the output format is not %s then we assume that we have
1846 * been given a correctly-sized format string, so we copy the
1847 * true format name including the size modifier. If the output
1848 * format is %s, then either the input format is %s as well or
1849 * it is one of our custom formats (e.g. pfprint_addr), so we
1850 * must set pfd_fmt to be the output format conversion "s".
1852 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1853 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1855 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1862 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1864 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1867 return (NULL); /* errno has been set for us */
1869 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1876 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1878 dt_pfargv_t *pfv = fmtdata;
1879 dt_pfargd_t *pfd = pfv->pfv_argv;
1882 * An upper bound on the string length is the length of the original
1883 * format string, plus three times the number of conversions (each
1884 * conversion could add up an additional "ll" and/or pfd_width digit
1885 * in the case of converting %? to %16) plus one for a terminating \0.
1887 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1888 char *format = alloca(formatlen);
1892 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1893 const dt_pfconv_t *pfc = pfd->pfd_conv;
1895 int width = pfd->pfd_width;
1896 int prec = pfd->pfd_prec;
1898 if (pfd->pfd_preflen != 0) {
1899 for (j = 0; j < pfd->pfd_preflen; j++)
1900 *f++ = pfd->pfd_prefix[j];
1908 if (pfd->pfd_flags & DT_PFCONV_ALT)
1910 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1912 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1914 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1916 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1918 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1922 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1924 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1926 if (pfd->pfd_flags & DT_PFCONV_AGG)
1930 f += snprintf(f, sizeof (format), "%d", width);
1933 f += snprintf(f, sizeof (format), ".%d", prec);
1936 * If the output format is %s, then either %s is the underlying
1937 * conversion or the conversion is one of our customized ones,
1938 * e.g. pfprint_addr. In these cases, put the original string
1939 * name of the conversion (pfc_name) into the pickled format
1940 * string rather than the derived conversion (pfd_fmt).
1942 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1943 str = pfc->pfc_name;
1947 for (j = 0; str[j] != '\0'; j++)
1951 *f = '\0'; /* insert nul byte; do not count in return value */
1953 assert(f < format + formatlen);
1954 (void) strncpy(s, format, len);
1956 return ((size_t)(f - format));
1960 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1962 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1963 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1964 uint_t nrecs = agg->dtagd_nrecs;
1965 dt_pfwalk_t *pfw = arg;
1966 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1969 if (dt_printf_getint(dtp, recp++, nrecs--,
1970 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1971 return (0); /* no aggregation id or id does not match */
1973 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1974 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1975 return (pfw->pfw_err = dtp->dt_errno);
1978 * Cast away the const to set the bit indicating that this aggregation
1981 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1987 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1989 const dtrace_aggdata_t *aggdata = aggsdata[0];
1990 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1991 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1992 uint_t nrecs = agg->dtagd_nrecs - 1;
1993 dt_pfwalk_t *pfw = arg;
1994 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1997 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1998 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
1999 aggsdata, naggvars) == -1)
2000 return (pfw->pfw_err = dtp->dt_errno);
2003 * For each aggregation, indicate that it has been printed, casting
2004 * away the const as necessary.
2006 for (i = 1; i < naggvars; i++) {
2007 agg = aggsdata[i]->dtada_desc;
2008 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2015 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2016 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2017 uint_t nrecs, const void *buf, size_t len)
2020 int i, naggvars = 0;
2021 dtrace_aggvarid_t *aggvars;
2023 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2026 * This might be a printa() with multiple aggregation variables. We
2027 * need to scan forward through the records until we find a record from
2028 * a different statement.
2030 for (i = 0; i < nrecs; i++) {
2031 const dtrace_recdesc_t *nrec = &recs[i];
2033 if (nrec->dtrd_uarg != recs->dtrd_uarg)
2036 if (nrec->dtrd_action != recs->dtrd_action)
2037 return (dt_set_errno(dtp, EDT_BADAGG));
2039 aggvars[naggvars++] =
2040 /* LINTED - alignment */
2041 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2045 return (dt_set_errno(dtp, EDT_BADAGG));
2047 pfw.pfw_argv = fmtdata;
2051 if (naggvars == 1) {
2052 pfw.pfw_aid = aggvars[0];
2054 if (dtrace_aggregate_walk_sorted(dtp,
2055 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2056 return (-1); /* errno is set for us */
2058 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2059 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2060 return (-1); /* errno is set for us */