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.
27 #include <sys/sysmacros.h>
29 #define ABS(a) ((a) < 0 ? -(a) : (a))
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <arpa/nameser.h>
47 #include <dt_printf.h>
48 #include <dt_string.h>
53 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
55 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
60 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
62 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
63 dt_node_is_symaddr(dnp));
68 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
70 dtrace_hdl_t *dtp = pfv->pfv_dtp;
71 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
73 if (dt_node_is_usymaddr(dnp))
76 if (idp == NULL || idp->di_id == 0)
79 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
84 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
86 return (dt_node_is_stack(dnp));
91 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
93 return (dt_node_is_integer(dnp) &&
94 dt_node_type_size(dnp) == sizeof (uint64_t));
99 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
107 if (dt_node_is_string(dnp))
111 base = ctf_type_resolve(ctfp, dnp->dn_type);
112 kind = ctf_type_kind(ctfp, base);
114 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
115 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
116 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
121 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
123 ctf_file_t *ctfp = dnp->dn_ctfp;
124 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
125 uint_t kind = ctf_type_kind(ctfp, base);
130 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
131 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
132 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
133 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
138 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
140 return (dt_node_is_integer(dnp) &&
141 dt_node_type_size(dnp) <= sizeof (int));
146 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
148 return (dt_node_is_float(dnp));
153 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
155 return (dt_node_is_integer(dnp));
160 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
162 if (dnp->dn_flags & DT_NF_SIGNED)
163 pfd->pfd_flags |= DT_PFCONV_SIGNED;
165 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
167 return (dt_node_is_integer(dnp));
172 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
174 ctf_file_t *ctfp = dnp->dn_ctfp;
175 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
176 char n[DT_TYPE_NAMELEN];
178 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
179 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
180 strcmp(n, "unsigned short") == 0));
185 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
187 ctf_file_t *ctfp = dnp->dn_ctfp;
188 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
189 char n[DT_TYPE_NAMELEN];
191 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
192 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
193 strcmp(n, "unsigned long") == 0));
198 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
200 ctf_file_t *ctfp = dnp->dn_ctfp;
201 ctf_id_t type = dnp->dn_type;
202 char n[DT_TYPE_NAMELEN];
204 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
205 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
206 strcmp(n, "signed long long") == 0 ||
207 strcmp(n, "unsigned long long") == 0))
211 * If the type used for %llx or %llX is not an [unsigned] long long, we
212 * also permit it to be a [u]int64_t or any typedef thereof. We know
213 * that these typedefs are guaranteed to work with %ll[xX] in either
214 * compilation environment even though they alias to "long" in LP64.
216 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
217 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
218 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
221 type = ctf_type_reference(ctfp, type);
229 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
231 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
232 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
237 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
238 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
240 int64_t normal = (int64_t)unormal;
241 int32_t n = (int32_t)normal;
244 case sizeof (int8_t):
245 return (dt_printf(dtp, fp, format,
246 (int32_t)*((int8_t *)addr) / n));
247 case sizeof (int16_t):
248 return (dt_printf(dtp, fp, format,
249 (int32_t)*((int16_t *)addr) / n));
250 case sizeof (int32_t):
251 return (dt_printf(dtp, fp, format,
252 *((int32_t *)addr) / n));
253 case sizeof (int64_t):
254 return (dt_printf(dtp, fp, format,
255 *((int64_t *)addr) / normal));
257 return (dt_set_errno(dtp, EDT_DMISMATCH));
263 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
264 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
266 uint32_t n = (uint32_t)normal;
269 case sizeof (uint8_t):
270 return (dt_printf(dtp, fp, format,
271 (uint32_t)*((uint8_t *)addr) / n));
272 case sizeof (uint16_t):
273 return (dt_printf(dtp, fp, format,
274 (uint32_t)*((uint16_t *)addr) / n));
275 case sizeof (uint32_t):
276 return (dt_printf(dtp, fp, format,
277 *((uint32_t *)addr) / n));
278 case sizeof (uint64_t):
279 return (dt_printf(dtp, fp, format,
280 *((uint64_t *)addr) / normal));
282 return (dt_set_errno(dtp, EDT_DMISMATCH));
287 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
288 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
290 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
291 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
293 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
298 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
299 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
301 double n = (double)normal;
302 long double ldn = (long double)normal;
306 return (dt_printf(dtp, fp, format,
307 (double)*((float *)addr) / n));
308 case sizeof (double):
309 return (dt_printf(dtp, fp, format,
310 *((double *)addr) / n));
311 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
312 case sizeof (long double):
313 return (dt_printf(dtp, fp, format,
314 *((long double *)addr) / ldn));
317 return (dt_set_errno(dtp, EDT_DMISMATCH));
323 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
324 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
331 case sizeof (uint32_t):
332 val = *((uint32_t *)addr);
334 case sizeof (uint64_t):
335 val = *((uint64_t *)addr);
338 return (dt_set_errno(dtp, EDT_DMISMATCH));
344 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
346 return (dt_printf(dtp, fp, format, s));
351 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
352 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
354 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
359 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
360 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
362 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
367 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
368 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
372 uint64_t val, pid = 0;
374 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
377 case sizeof (uint32_t):
378 val = (u_longlong_t)*((uint32_t *)addr);
380 case sizeof (uint64_t):
381 val = (u_longlong_t)*((uint64_t *)addr);
383 case sizeof (uint64_t) * 2:
384 pid = ((uint64_t *)(uintptr_t)addr)[0];
385 val = ((uint64_t *)(uintptr_t)addr)[1];
388 return (dt_set_errno(dtp, EDT_DMISMATCH));
391 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
397 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
399 return (dt_printf(dtp, fp, format, s));
404 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
405 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
408 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
409 const dtrace_recdesc_t *rec = pfd->pfd_rec;
410 caddr_t addr = (caddr_t)vaddr;
414 * We have stashed the value of the STACKINDENT option, and we will
415 * now override it for the purposes of formatting the stack. If the
416 * field has been specified as left-aligned (i.e. (%-#), we set the
417 * indentation to be the width. This is a slightly odd semantic, but
418 * it's useful functionality -- and it's slightly odd to begin with to
419 * be using a single format specifier to be formatting multiple lines
422 if (pfd->pfd_dynwidth < 0) {
423 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
424 width = -pfd->pfd_dynwidth;
425 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
426 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
431 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
433 switch (rec->dtrd_action) {
434 case DTRACEACT_USTACK:
435 case DTRACEACT_JSTACK:
436 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
439 case DTRACEACT_STACK:
440 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
441 rec->dtrd_size / rec->dtrd_arg);
448 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
455 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
456 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
458 char src[32], buf[32], *dst = buf;
459 hrtime_t time = *((uint64_t *)addr);
460 time_t sec = (time_t)(time / NANOSEC);
464 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
465 * Below, we turn this into the canonical adb/mdb /[yY] format,
466 * "1973 Dec 3 17:20:00".
469 (void) ctime_r(&sec, src, sizeof (src));
471 (void) ctime_r(&sec, src);
475 * Place the 4-digit year at the head of the string...
477 for (i = 20; i < 24; i++)
481 * ...and follow it with the remainder (month, day, hh:mm:ss).
483 for (i = 3; i < 19; i++)
487 return (dt_printf(dtp, fp, format, buf));
491 * This prints the time in RFC 822 standard form. This is useful for emitting
492 * notions of time that are consumed by standard tools (e.g., as part of an
497 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
498 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
500 hrtime_t time = *((uint64_t *)addr);
501 time_t sec = (time_t)(time / NANOSEC);
505 (void) localtime_r(&sec, &tm);
506 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
507 return (dt_printf(dtp, fp, format, buf));
512 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
513 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
515 uint16_t port = htons(*((uint16_t *)addr));
517 struct servent *sv, res;
520 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
522 if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
524 return (dt_printf(dtp, fp, format, sv->s_name));
526 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
527 return (dt_printf(dtp, fp, format, buf));
532 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
533 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
535 char *s = alloca(size + 1);
536 struct hostent *host, res;
537 char inetaddr[NS_IN6ADDRSZ];
541 bcopy(addr, s, size);
544 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
546 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
547 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
549 if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
550 AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
552 return (dt_printf(dtp, fp, format, host->h_name));
553 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
554 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
555 AF_INET6, &e)) != NULL)
556 return (dt_printf(dtp, fp, format, host->h_name));
559 return (dt_printf(dtp, fp, format, s));
564 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
565 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
567 char *s = alloca(size + 1);
569 bcopy(addr, s, size);
571 return (dt_printf(dtp, fp, format, s));
576 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
577 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
579 wchar_t *ws = alloca(size + sizeof (wchar_t));
581 bcopy(addr, ws, size);
582 ws[size / sizeof (wchar_t)] = L'\0';
583 return (dt_printf(dtp, fp, format, ws));
588 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
589 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
594 if ((s = strchr2esc(addr, size)) == NULL)
595 return (dt_set_errno(dtp, EDT_NOMEM));
597 n = dt_printf(dtp, fp, format, s);
603 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
604 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
609 case sizeof (int8_t):
612 case sizeof (int16_t):
613 c = *(int16_t *)addr;
615 case sizeof (int32_t):
616 c = *(int32_t *)addr;
619 return (dt_set_errno(dtp, EDT_DMISMATCH));
622 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
627 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
628 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
630 return (dt_printf(dtp, fp, "%%"));
633 static const char pfproto_xint[] = "char, short, int, long, or long long";
634 static const char pfproto_csi[] = "char, short, or int";
635 static const char pfproto_fp[] = "float, double, or long double";
636 static const char pfproto_addr[] = "pointer or integer";
637 static const char pfproto_uaddr[] =
638 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
639 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
640 static const char pfproto_wstr[] = "wchar_t []";
643 * Printf format conversion dictionary. This table should match the set of
644 * conversions offered by printf(3C), as well as some additional extensions.
645 * The second parameter is an ASCII string which is either an actual type
646 * name we should look up (if pfcheck_type is specified), or just a descriptive
647 * string of the types expected for use in error messages.
649 static const dt_pfconv_t _dtrace_conversions[] = {
650 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
651 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
652 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
653 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
654 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
655 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
656 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
657 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
658 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
659 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
660 { "hd", "d", "short", pfcheck_type, pfprint_sint },
661 { "hi", "i", "short", pfcheck_type, pfprint_sint },
662 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
663 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
664 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
665 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
666 { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
667 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
668 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
669 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
670 { "ld", "d", "long", pfcheck_type, pfprint_sint },
671 { "li", "i", "long", pfcheck_type, pfprint_sint },
672 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
673 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
674 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
675 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
676 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
677 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
678 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
679 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
680 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
681 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
682 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
683 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
684 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
685 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
686 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
687 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
688 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
689 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
690 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
691 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
692 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
693 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
694 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
695 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
696 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
697 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
698 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
699 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
700 { "%", "%", "void", pfcheck_type, pfprint_pct },
701 { NULL, NULL, NULL, NULL, NULL }
705 dt_pfdict_create(dtrace_hdl_t *dtp)
707 uint_t n = _dtrace_strbuckets;
708 const dt_pfconv_t *pfd;
711 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
712 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
714 return (dt_set_errno(dtp, EDT_NOMEM));
717 dtp->dt_pfdict = pdi;
718 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
719 pdi->pdi_nbuckets = n;
721 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
722 dtrace_typeinfo_t dtt;
726 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
727 dt_pfdict_destroy(dtp);
728 return (dt_set_errno(dtp, EDT_NOMEM));
731 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
732 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
733 pfc->pfc_next = pdi->pdi_buckets[h];
734 pdi->pdi_buckets[h] = pfc;
737 dtt.dtt_type = CTF_ERR;
740 * The "D" container or its parent must contain a definition of
741 * any type referenced by a printf conversion. If none can be
742 * found, we fail to initialize the printf dictionary.
744 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
745 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
746 dt_pfdict_destroy(dtp);
747 return (dt_set_errno(dtp, EDT_NOCONV));
750 pfc->pfc_dctfp = dtt.dtt_ctfp;
751 pfc->pfc_dtype = dtt.dtt_type;
754 * The "C" container may contain an alternate definition of an
755 * explicit conversion type. If it does, use it; otherwise
756 * just set pfc_ctype to pfc_dtype so it is always valid.
758 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
759 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
760 pfc->pfc_cctfp = dtt.dtt_ctfp;
761 pfc->pfc_ctype = dtt.dtt_type;
763 pfc->pfc_cctfp = pfc->pfc_dctfp;
764 pfc->pfc_ctype = pfc->pfc_dtype;
767 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
768 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
769 dt_pfdict_destroy(dtp);
770 return (dt_set_errno(dtp, EDT_BADCONV));
773 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
780 dt_pfdict_destroy(dtrace_hdl_t *dtp)
782 dt_pfdict_t *pdi = dtp->dt_pfdict;
783 dt_pfconv_t *pfc, *nfc;
789 for (i = 0; i < pdi->pdi_nbuckets; i++) {
790 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
796 free(pdi->pdi_buckets);
798 dtp->dt_pfdict = NULL;
801 static const dt_pfconv_t *
802 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
804 dt_pfdict_t *pdi = dtp->dt_pfdict;
805 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
806 const dt_pfconv_t *pfc;
808 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
809 if (strcmp(pfc->pfc_name, name) == 0)
817 dt_printf_error(dtrace_hdl_t *dtp, int err)
820 longjmp(yypcb->pcb_jmpbuf, err);
822 (void) dt_set_errno(dtp, err);
827 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
829 dt_pfargd_t *pfd, *nfd = NULL;
834 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
835 (format = strdup(s)) == NULL) {
837 return (dt_printf_error(dtp, EDT_NOMEM));
840 pfv->pfv_format = format;
841 pfv->pfv_argv = NULL;
846 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
855 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
856 dt_printf_destroy(pfv);
857 return (dt_printf_error(dtp, EDT_NOMEM));
860 if (pfv->pfv_argv != NULL)
865 bzero(pfd, sizeof (dt_pfargd_t));
870 pfd->pfd_preflen = (size_t)(p - q);
876 case '0': case '1': case '2': case '3': case '4':
877 case '5': case '6': case '7': case '8': case '9':
878 if (dot == 0 && digits == 0 && c == '0') {
879 pfd->pfd_flags |= DT_PFCONV_ZPAD;
880 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
884 for (n = 0; isdigit(c); c = *++p)
885 n = n * 10 + c - '0';
897 pfd->pfd_flags |= DT_PFCONV_ALT;
901 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
903 if (pfd->pfd_flags & n) {
904 yywarn("format conversion #%u has more than "
905 "one '*' specified for the output %s\n",
906 pfv->pfv_argc, n ? "precision" : "width");
908 dt_printf_destroy(pfv);
909 return (dt_printf_error(dtp, EDT_COMPILER));
916 pfd->pfd_flags |= DT_PFCONV_SPOS;
920 pfd->pfd_flags |= DT_PFCONV_LEFT;
921 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
926 yywarn("format conversion #%u has more than "
927 "one '.' specified\n", pfv->pfv_argc);
929 dt_printf_destroy(pfv);
930 return (dt_printf_error(dtp, EDT_COMPILER));
936 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
943 pfd->pfd_flags |= DT_PFCONV_AGG;
947 pfd->pfd_flags |= DT_PFCONV_GROUP;
951 pfd->pfd_flags |= DT_PFCONV_SPACE;
955 yywarn("format conversion #%u uses unsupported "
956 "positional format (%%n$)\n", pfv->pfv_argc);
958 dt_printf_destroy(pfv);
959 return (dt_printf_error(dtp, EDT_COMPILER));
963 goto default_lbl; /* if %% then use "%" conv */
965 yywarn("format conversion #%u cannot be combined "
966 "with other format flags: %%%%\n", pfv->pfv_argc);
968 dt_printf_destroy(pfv);
969 return (dt_printf_error(dtp, EDT_COMPILER));
972 yywarn("format conversion #%u name expected before "
973 "end of format string\n", pfv->pfv_argc);
975 dt_printf_destroy(pfv);
976 return (dt_printf_error(dtp, EDT_COMPILER));
982 if (namelen < sizeof (name) - 2)
989 name[namelen] = '\0';
992 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
994 if (pfd->pfd_conv == NULL) {
995 yywarn("format conversion #%u is undefined: %%%s\n",
996 pfv->pfv_argc, name);
997 dt_printf_destroy(pfv);
998 return (dt_printf_error(dtp, EDT_COMPILER));
1002 if (*q != '\0' || *format == '\0') {
1003 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1004 dt_printf_destroy(pfv);
1005 return (dt_printf_error(dtp, EDT_NOMEM));
1008 if (pfv->pfv_argv != NULL)
1009 nfd->pfd_next = pfd;
1011 pfv->pfv_argv = pfd;
1013 bzero(pfd, sizeof (dt_pfargd_t));
1016 pfd->pfd_prefix = q;
1017 pfd->pfd_preflen = strlen(q);
1024 dt_printf_destroy(dt_pfargv_t *pfv)
1026 dt_pfargd_t *pfd, *nfd;
1028 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1029 nfd = pfd->pfd_next;
1033 free(pfv->pfv_format);
1038 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1039 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1041 dt_pfargd_t *pfd = pfv->pfv_argv;
1042 const char *func = idp->di_name;
1044 char n[DT_TYPE_NAMELEN];
1045 dtrace_typeinfo_t dtt;
1046 const char *aggtype;
1050 if (pfv->pfv_format[0] == '\0') {
1051 xyerror(D_PRINTF_FMT_EMPTY,
1052 "%s( ) format string is empty\n", func);
1055 pfv->pfv_flags = flags;
1058 * We fake up a parse node representing the type that can be used with
1059 * an aggregation result conversion, which -- for all but count() --
1060 * is a signed quantity.
1062 if (kind != DTRACEAGG_COUNT)
1063 aggtype = "int64_t";
1065 aggtype = "uint64_t";
1067 if (dt_type_lookup(aggtype, &dtt) != 0)
1068 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1070 bzero(&aggnode, sizeof (aggnode));
1071 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
1073 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1074 const dt_pfconv_t *pfc = pfd->pfd_conv;
1075 const char *dyns[2];
1082 continue; /* no checking if argd is just a prefix */
1084 if (pfc->pfc_print == &pfprint_pct) {
1085 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1089 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1090 dyns[dync++] = ".*";
1091 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1094 for (; dync != 0; dync--) {
1096 xyerror(D_PRINTF_DYN_PROTO,
1097 "%s( ) prototype mismatch: conversion "
1098 "#%d (%%%s) is missing a corresponding "
1099 "\"%s\" argument\n", func, i + 1,
1100 pfc->pfc_name, dyns[dync - 1]);
1103 if (dt_node_is_integer(dnp) == 0) {
1104 xyerror(D_PRINTF_DYN_TYPE,
1105 "%s( ) argument #%d is incompatible "
1106 "with conversion #%d prototype:\n"
1107 "\tconversion: %% %s %s\n"
1108 "\t prototype: int\n\t argument: %s\n",
1109 func, j + foff + 1, i + 1,
1110 dyns[dync - 1], pfc->pfc_name,
1111 dt_node_type_name(dnp, n, sizeof (n)));
1119 * If this conversion is consuming the aggregation data, set
1120 * the value node pointer (vnp) to a fake node based on the
1121 * aggregating function result type. Otherwise assign vnp to
1122 * the next parse node in the argument list, if there is one.
1124 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1125 if (!(flags & DT_PRINTF_AGGREGATION)) {
1126 xyerror(D_PRINTF_AGG_CONV,
1127 "%%@ conversion requires an aggregation"
1128 " and is not for use with %s( )\n", func);
1130 (void) strlcpy(vname, "aggregating action",
1133 } else if (dnp == NULL) {
1134 xyerror(D_PRINTF_ARG_PROTO,
1135 "%s( ) prototype mismatch: conversion #%d (%%"
1136 "%s) is missing a corresponding value argument\n",
1137 func, i + 1, pfc->pfc_name);
1139 (void) snprintf(vname, sizeof (vname),
1140 "argument #%d", j + foff + 1);
1147 * Fill in the proposed final format string by prepending any
1148 * size-related prefixes to the pfconv's format string. The
1149 * pfc_check() function below may optionally modify the format
1150 * as part of validating the type of the input argument.
1152 if (pfc->pfc_print == &pfprint_sint ||
1153 pfc->pfc_print == &pfprint_uint ||
1154 pfc->pfc_print == &pfprint_dint) {
1155 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1156 (void) strcpy(pfd->pfd_fmt, "ll");
1157 } else if (pfc->pfc_print == &pfprint_fp) {
1158 if (dt_node_type_size(vnp) == sizeof (long double))
1159 (void) strcpy(pfd->pfd_fmt, "L");
1162 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1165 * Validate the format conversion against the value node type.
1166 * If the conversion is good, create the descriptor format
1167 * string by concatenating together any required printf(3C)
1168 * size prefixes with the conversion's native format string.
1170 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1171 xyerror(D_PRINTF_ARG_TYPE,
1172 "%s( ) %s is incompatible with "
1173 "conversion #%d prototype:\n\tconversion: %%%s\n"
1174 "\t prototype: %s\n\t argument: %s\n", func,
1175 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1176 dt_node_type_name(vnp, n, sizeof (n)));
1180 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1181 xyerror(D_PRINTF_ARG_EXTRA,
1182 "%s( ) prototype mismatch: only %d arguments "
1183 "required by this format string\n", func, j);
1188 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1190 dt_ident_t *lid, *rid;
1191 dt_node_t *lproto, *rproto;
1192 int largc, rargc, argn;
1193 char n1[DT_TYPE_NAMELEN];
1194 char n2[DT_TYPE_NAMELEN];
1196 assert(lhs->dn_kind == DT_NODE_AGG);
1197 assert(rhs->dn_kind == DT_NODE_AGG);
1199 lid = lhs->dn_ident;
1200 rid = rhs->dn_ident;
1202 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1203 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1206 * First, get an argument count on each side. These must match.
1208 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1211 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1214 if (largc != rargc) {
1215 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1216 "matching key signatures: @%s has %d key%s, @%s has %d "
1217 "key%s", lid->di_name, rid->di_name,
1218 lid->di_name, largc, largc == 1 ? "" : "s",
1219 rid->di_name, rargc, rargc == 1 ? "" : "s");
1223 * Now iterate over the keys to verify that each type matches.
1225 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1226 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1228 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1229 rproto = rproto->dn_list) {
1230 assert(rproto != NULL);
1232 if (dt_node_is_argcompat(lproto, rproto))
1235 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1236 "incompatible with @%s:\n%9s key #%d: %s\n"
1237 "%9s key #%d: %s\n",
1238 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1239 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1240 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1245 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1246 uint_t nrecs, const void *buf, size_t len, int *ip)
1251 return (dt_set_errno(dtp, EDT_DMISMATCH));
1253 addr = (uintptr_t)buf + recp->dtrd_offset;
1255 if (addr + sizeof (int) > (uintptr_t)buf + len)
1256 return (dt_set_errno(dtp, EDT_DOFFSET));
1258 if (addr & (recp->dtrd_alignment - 1))
1259 return (dt_set_errno(dtp, EDT_DALIGN));
1261 switch (recp->dtrd_size) {
1262 case sizeof (int8_t):
1263 *ip = (int)*((int8_t *)addr);
1265 case sizeof (int16_t):
1266 *ip = (int)*((int16_t *)addr);
1268 case sizeof (int32_t):
1269 *ip = (int)*((int32_t *)addr);
1271 case sizeof (int64_t):
1272 *ip = (int)*((int64_t *)addr);
1275 return (dt_set_errno(dtp, EDT_DMISMATCH));
1283 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1284 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1286 const uint64_t *data = addr;
1288 if (size != sizeof (uint64_t) * 2)
1289 return (dt_set_errno(dtp, EDT_DMISMATCH));
1291 return (dt_printf(dtp, fp, format,
1292 data[0] ? data[1] / normal / data[0] : 0));
1297 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1298 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1300 const uint64_t *data = addr;
1302 if (size != sizeof (uint64_t) * 4)
1303 return (dt_set_errno(dtp, EDT_DMISMATCH));
1305 return (dt_printf(dtp, fp, format,
1306 dt_stddev((uint64_t *)data, normal)));
1311 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1312 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1314 return (dt_print_quantize(dtp, fp, addr, size, normal));
1319 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1320 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1322 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1326 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1327 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1328 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1330 dt_pfargd_t *pfd = pfv->pfv_argv;
1331 const dtrace_recdesc_t *recp = recs;
1332 const dtrace_aggdata_t *aggdata;
1333 dtrace_aggdesc_t *agg;
1334 caddr_t lim = (caddr_t)buf + len, limit;
1335 char format[64] = "%";
1336 int i, aggrec, curagg = -1;
1340 * If we are formatting an aggregation, set 'aggrec' to the index of
1341 * the final record description (the aggregation result) so we can use
1342 * this record index with any conversion where DT_PFCONV_AGG is set.
1343 * (The actual aggregation used will vary as we increment through the
1344 * aggregation variables that we have been passed.) Finally, we
1345 * decrement nrecs to prevent this record from being used with any
1348 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1349 assert(aggsdata != NULL);
1350 assert(naggvars > 0);
1353 return (dt_set_errno(dtp, EDT_DMISMATCH));
1355 curagg = naggvars > 1 ? 1 : 0;
1356 aggdata = aggsdata[0];
1357 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1361 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1362 const dt_pfconv_t *pfc = pfd->pfd_conv;
1363 int width = pfd->pfd_width;
1364 int prec = pfd->pfd_prec;
1367 char *f = format + 1; /* skip initial '%' */
1368 const dtrace_recdesc_t *rec;
1374 if (pfd->pfd_preflen != 0) {
1375 char *tmp = alloca(pfd->pfd_preflen + 1);
1377 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1378 tmp[pfd->pfd_preflen] = '\0';
1380 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1383 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1385 * For printa(), we flush the buffer after each
1386 * prefix, setting the flags to indicate that
1387 * this is part of the printa() format string.
1389 flags = DTRACE_BUFDATA_AGGFORMAT;
1391 if (pfc == NULL && i == pfv->pfv_argc - 1)
1392 flags |= DTRACE_BUFDATA_AGGLAST;
1394 if (dt_buffered_flush(dtp, NULL, NULL,
1395 aggdata, flags) < 0)
1401 if (pfv->pfv_argc == 1)
1402 return (nrecs != 0);
1407 * If the conversion is %%, just invoke the print callback
1408 * with no data record and continue; it consumes no record.
1410 if (pfc->pfc_print == &pfprint_pct) {
1411 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1413 return (-1); /* errno is set for us */
1416 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1417 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1419 return (-1); /* errno is set for us */
1420 pfd->pfd_dynwidth = width;
1422 pfd->pfd_dynwidth = 0;
1425 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1426 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1427 return (-1); /* errno is set for us */
1429 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1431 * This should be impossible -- the compiler shouldn't
1432 * create a DT_PFCONV_AGG conversion without an
1433 * aggregation present. Still, we'd rather fail
1434 * gracefully than blow up...
1436 if (aggsdata == NULL)
1437 return (dt_set_errno(dtp, EDT_DMISMATCH));
1439 aggdata = aggsdata[curagg];
1440 agg = aggdata->dtada_desc;
1443 * We increment the current aggregation variable, but
1444 * not beyond the number of aggregation variables that
1445 * we're printing. This has the (desired) effect that
1446 * DT_PFCONV_AGG conversions beyond the number of
1447 * aggregation variables (re-)convert the aggregation
1448 * value of the last aggregation variable.
1450 if (curagg < naggvars - 1)
1453 rec = &agg->dtagd_rec[aggrec];
1454 addr = aggdata->dtada_data + rec->dtrd_offset;
1455 limit = addr + aggdata->dtada_size;
1456 normal = aggdata->dtada_normal;
1457 flags = DTRACE_BUFDATA_AGGVAL;
1460 return (dt_set_errno(dtp, EDT_DMISMATCH));
1462 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1464 * When printing aggregation keys, we always
1465 * set the aggdata to be the representative
1466 * (zeroth) aggregation. The aggdata isn't
1467 * actually used here in this case, but it is
1468 * passed to the buffer handler and must
1469 * therefore still be correct.
1471 aggdata = aggsdata[0];
1472 flags = DTRACE_BUFDATA_AGGKEY;
1477 addr = (caddr_t)buf + rec->dtrd_offset;
1482 size = rec->dtrd_size;
1484 if (addr + size > limit) {
1485 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1486 (void *)addr, rec->dtrd_size, (void *)lim);
1487 return (dt_set_errno(dtp, EDT_DOFFSET));
1490 if (rec->dtrd_alignment != 0 &&
1491 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1492 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1493 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1494 return (dt_set_errno(dtp, EDT_DALIGN));
1497 switch (rec->dtrd_action) {
1499 func = pfprint_average;
1501 case DTRACEAGG_STDDEV:
1502 func = pfprint_stddev;
1504 case DTRACEAGG_QUANTIZE:
1505 func = pfprint_quantize;
1507 case DTRACEAGG_LQUANTIZE:
1508 func = pfprint_lquantize;
1513 case DTRACEACT_UMOD:
1514 func = pfprint_umod;
1517 func = pfc->pfc_print;
1521 if (pfd->pfd_flags & DT_PFCONV_ALT)
1523 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1525 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1527 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1529 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1531 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1535 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1536 * don't add the width to the format string. See the block
1537 * comment in pfprint_stack() for a description of the
1538 * behavior in this case.
1540 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1544 f += snprintf(f, sizeof (format), "%d", ABS(width));
1547 f += snprintf(f, sizeof (format), ".%d", prec);
1549 (void) strcpy(f, pfd->pfd_fmt);
1552 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1553 return (-1); /* errno is set for us */
1555 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1557 * For printa(), we flush the buffer after each tuple
1558 * element, inidicating that this is the last record
1561 if (i == pfv->pfv_argc - 1)
1562 flags |= DTRACE_BUFDATA_AGGLAST;
1564 if (dt_buffered_flush(dtp, NULL,
1565 rec, aggdata, flags) < 0)
1570 return ((int)(recp - recs));
1574 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1575 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1577 dtrace_optval_t size;
1580 rval = dtrace_getopt(dtp, "strsize", &size);
1582 assert(dtp->dt_sprintf_buflen == 0);
1584 if (dtp->dt_sprintf_buf != NULL)
1585 free(dtp->dt_sprintf_buf);
1587 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1588 return (dt_set_errno(dtp, EDT_NOMEM));
1590 bzero(dtp->dt_sprintf_buf, size);
1591 dtp->dt_sprintf_buflen = size;
1592 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1594 dtp->dt_sprintf_buflen = 0;
1597 free(dtp->dt_sprintf_buf);
1604 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1605 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1606 uint_t nrecs, const void *buf, size_t len)
1608 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1614 * Before we execute the specified command, flush fp to assure that
1615 * any prior dt_printf()'s appear before the output of the command
1620 if (system(dtp->dt_sprintf_buf) == -1)
1621 return (dt_set_errno(dtp, errno));
1627 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1628 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1629 uint_t nrecs, const void *buf, size_t len)
1631 char selfbuf[40], restorebuf[40], *filename;
1634 dt_pfargv_t *pfv = fmtdata;
1635 dt_pfargd_t *pfd = pfv->pfv_argv;
1637 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1639 if (rval == -1 || fp == NULL)
1643 if (pfd->pfd_preflen != 0 &&
1644 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1646 * The only way to have the format string set to the value
1647 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1648 * denoting that we should restore the old stdout.
1650 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1652 if (dtp->dt_stdout_fd == -1) {
1654 * We could complain here by generating an error,
1655 * but it seems like overkill: it seems that calling
1656 * freopen() to restore stdout when freopen() has
1657 * never before been called should just be a no-op,
1658 * so we just return in this case.
1663 (void) snprintf(restorebuf, sizeof (restorebuf),
1664 "/dev/fd/%d", dtp->dt_stdout_fd);
1665 filename = restorebuf;
1667 filename = dtp->dt_sprintf_buf;
1671 * freopen(3C) will always close the specified stream and underlying
1672 * file descriptor -- even if the specified file can't be opened.
1673 * Even for the semantic cesspool that is standard I/O, this is
1674 * surprisingly brain-dead behavior: it means that any failure to
1675 * open the specified file destroys the specified stream in the
1676 * process -- which is particularly relevant when the specified stream
1677 * happens (or rather, happened) to be stdout. This could be resolved
1678 * were there an "fdreopen()" equivalent of freopen() that allowed one
1679 * to pass a file descriptor instead of the name of a file, but there
1680 * is no such thing. However, we can effect this ourselves by first
1681 * fopen()'ing the desired file, and then (assuming that that works),
1682 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1683 * file descriptor for the fopen()'d file. This way, if the fopen()
1684 * fails, we can fail the operation without destroying stdout.
1686 if ((nfp = fopen(filename, "aF")) == NULL) {
1687 char *msg = strerror(errno);
1691 len += strlen(msg) + strlen(filename);
1692 faultstr = alloca(len);
1694 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1695 filename, strerror(errno));
1697 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1703 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1705 if (dtp->dt_stdout_fd == -1) {
1707 * If this is the first time that we're calling freopen(),
1708 * we're going to stash away the file descriptor for stdout.
1709 * We don't expect the dup(2) to fail, so if it does we must
1712 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1714 return (dt_set_errno(dtp, errno));
1718 if (freopen(selfbuf, "aF", fp) == NULL) {
1720 return (dt_set_errno(dtp, errno));
1726 * The 'standard output' (which is not necessarily stdout)
1727 * treatment on FreeBSD is implemented differently than on
1728 * Solaris because FreeBSD's freopen() will attempt to re-use
1729 * the current file descriptor, causing the previous file to
1730 * be closed and thereby preventing it from be re-activated
1733 * For FreeBSD we use the concept of setting an output file
1734 * pointer in the DTrace handle if a dtrace_freopen() has
1735 * enabled another output file and we leave the caller's
1736 * file pointer untouched. If it was actually stdout, then
1737 * stdout remains open. If it was another file, then that
1738 * file remains open. While a dtrace_freopen() has activated
1739 * another file, we keep a pointer to that which we use in
1740 * the output functions by preference and only use the caller's
1741 * file pointer if no dtrace_freopen() call has been made.
1743 * The check to see if we're re-activating the caller's
1744 * output file is much the same as on Solaris.
1746 if (pfd->pfd_preflen != 0 &&
1747 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1749 * The only way to have the format string set to the value
1750 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1751 * denoting that we should restore the old stdout.
1753 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1755 if (dtp->dt_freopen_fp == NULL) {
1757 * We could complain here by generating an error,
1758 * but it seems like overkill: it seems that calling
1759 * freopen() to restore stdout when freopen() has
1760 * never before been called should just be a no-op,
1761 * so we just return in this case.
1767 * At this point, to re-active the original output file,
1768 * on FreeBSD we only code the current file that this
1769 * function opened previously.
1771 (void) fclose(dtp->dt_freopen_fp);
1772 dtp->dt_freopen_fp = NULL;
1777 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1778 char *msg = strerror(errno);
1782 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1783 faultstr = alloca(len);
1785 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1786 dtp->dt_sprintf_buf, strerror(errno));
1788 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1794 if (dtp->dt_freopen_fp != NULL)
1795 (void) fclose(dtp->dt_freopen_fp);
1797 /* Remember that the output has been redirected to the new file. */
1798 dtp->dt_freopen_fp = nfp;
1806 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1807 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1808 uint_t nrecs, const void *buf, size_t len)
1810 return (dt_printf_format(dtp, fp, fmtdata,
1811 recp, nrecs, buf, len, NULL, 0));
1815 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1817 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1822 return (NULL); /* errno has been set for us */
1824 pfd = pfv->pfv_argv;
1826 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1827 const dt_pfconv_t *pfc = pfd->pfd_conv;
1833 * If the output format is not %s then we assume that we have
1834 * been given a correctly-sized format string, so we copy the
1835 * true format name including the size modifier. If the output
1836 * format is %s, then either the input format is %s as well or
1837 * it is one of our custom formats (e.g. pfprint_addr), so we
1838 * must set pfd_fmt to be the output format conversion "s".
1840 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1841 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1843 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1850 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1852 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1855 return (NULL); /* errno has been set for us */
1857 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1864 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1866 dt_pfargv_t *pfv = fmtdata;
1867 dt_pfargd_t *pfd = pfv->pfv_argv;
1870 * An upper bound on the string length is the length of the original
1871 * format string, plus three times the number of conversions (each
1872 * conversion could add up an additional "ll" and/or pfd_width digit
1873 * in the case of converting %? to %16) plus one for a terminating \0.
1875 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1876 char *format = alloca(formatlen);
1880 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1881 const dt_pfconv_t *pfc = pfd->pfd_conv;
1883 int width = pfd->pfd_width;
1884 int prec = pfd->pfd_prec;
1886 if (pfd->pfd_preflen != 0) {
1887 for (j = 0; j < pfd->pfd_preflen; j++)
1888 *f++ = pfd->pfd_prefix[j];
1896 if (pfd->pfd_flags & DT_PFCONV_ALT)
1898 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1900 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1902 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1904 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1906 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1910 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1912 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1914 if (pfd->pfd_flags & DT_PFCONV_AGG)
1918 f += snprintf(f, sizeof (format), "%d", width);
1921 f += snprintf(f, sizeof (format), ".%d", prec);
1924 * If the output format is %s, then either %s is the underlying
1925 * conversion or the conversion is one of our customized ones,
1926 * e.g. pfprint_addr. In these cases, put the original string
1927 * name of the conversion (pfc_name) into the pickled format
1928 * string rather than the derived conversion (pfd_fmt).
1930 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1931 str = pfc->pfc_name;
1935 for (j = 0; str[j] != '\0'; j++)
1939 *f = '\0'; /* insert nul byte; do not count in return value */
1941 assert(f < format + formatlen);
1942 (void) strncpy(s, format, len);
1944 return ((size_t)(f - format));
1948 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1950 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1951 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1952 uint_t nrecs = agg->dtagd_nrecs;
1953 dt_pfwalk_t *pfw = arg;
1954 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1957 if (dt_printf_getint(dtp, recp++, nrecs--,
1958 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1959 return (0); /* no aggregation id or id does not match */
1961 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1962 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1963 return (pfw->pfw_err = dtp->dt_errno);
1966 * Cast away the const to set the bit indicating that this aggregation
1969 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1975 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1977 const dtrace_aggdata_t *aggdata = aggsdata[0];
1978 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1979 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1980 uint_t nrecs = agg->dtagd_nrecs - 1;
1981 dt_pfwalk_t *pfw = arg;
1982 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1985 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1986 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
1987 aggsdata, naggvars) == -1)
1988 return (pfw->pfw_err = dtp->dt_errno);
1991 * For each aggregation, indicate that it has been printed, casting
1992 * away the const as necessary.
1994 for (i = 1; i < naggvars; i++) {
1995 agg = aggsdata[i]->dtada_desc;
1996 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2003 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2004 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2005 uint_t nrecs, const void *buf, size_t len)
2008 int i, naggvars = 0;
2009 dtrace_aggvarid_t *aggvars;
2011 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2014 * This might be a printa() with multiple aggregation variables. We
2015 * need to scan forward through the records until we find a record from
2016 * a different statement.
2018 for (i = 0; i < nrecs; i++) {
2019 const dtrace_recdesc_t *nrec = &recs[i];
2021 if (nrec->dtrd_uarg != recs->dtrd_uarg)
2024 if (nrec->dtrd_action != recs->dtrd_action)
2025 return (dt_set_errno(dtp, EDT_BADAGG));
2027 aggvars[naggvars++] =
2028 /* LINTED - alignment */
2029 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2033 return (dt_set_errno(dtp, EDT_BADAGG));
2035 pfw.pfw_argv = fmtdata;
2039 if (naggvars == 1) {
2040 pfw.pfw_aid = aggvars[0];
2042 if (dtrace_aggregate_walk_sorted(dtp,
2043 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2044 return (-1); /* errno is set for us */
2046 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2047 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2048 return (-1); /* errno is set for us */