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.
26 #include <sys/sysmacros.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
40 #include <dt_printf.h>
41 #include <dt_string.h>
46 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
48 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
53 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
55 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
56 dt_node_is_symaddr(dnp));
61 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
63 dtrace_hdl_t *dtp = pfv->pfv_dtp;
64 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
66 if (dt_node_is_usymaddr(dnp))
69 if (idp == NULL || idp->di_id == 0)
72 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
77 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
79 return (dt_node_is_stack(dnp));
84 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
86 return (dt_node_is_integer(dnp) &&
87 dt_node_type_size(dnp) == sizeof (uint64_t));
92 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
100 if (dt_node_is_string(dnp))
104 base = ctf_type_resolve(ctfp, dnp->dn_type);
105 kind = ctf_type_kind(ctfp, base);
107 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
108 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
109 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
114 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
116 ctf_file_t *ctfp = dnp->dn_ctfp;
117 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
118 uint_t kind = ctf_type_kind(ctfp, base);
123 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
124 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
125 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
126 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
131 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
133 return (dt_node_is_integer(dnp) &&
134 dt_node_type_size(dnp) <= sizeof (int));
139 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
141 return (dt_node_is_float(dnp));
146 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
148 return (dt_node_is_integer(dnp));
153 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
155 if (dnp->dn_flags & DT_NF_SIGNED)
156 pfd->pfd_flags |= DT_PFCONV_SIGNED;
158 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
160 return (dt_node_is_integer(dnp));
165 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
167 ctf_file_t *ctfp = dnp->dn_ctfp;
168 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
169 char n[DT_TYPE_NAMELEN];
171 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
172 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
173 strcmp(n, "unsigned short") == 0));
178 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
180 ctf_file_t *ctfp = dnp->dn_ctfp;
181 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
182 char n[DT_TYPE_NAMELEN];
184 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
185 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
186 strcmp(n, "unsigned long") == 0));
191 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
193 ctf_file_t *ctfp = dnp->dn_ctfp;
194 ctf_id_t type = dnp->dn_type;
195 char n[DT_TYPE_NAMELEN];
197 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
198 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
199 strcmp(n, "signed long long") == 0 ||
200 strcmp(n, "unsigned long long") == 0))
204 * If the type used for %llx or %llX is not an [unsigned] long long, we
205 * also permit it to be a [u]int64_t or any typedef thereof. We know
206 * that these typedefs are guaranteed to work with %ll[xX] in either
207 * compilation environment even though they alias to "long" in LP64.
209 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
210 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
211 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
214 type = ctf_type_reference(ctfp, type);
222 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
224 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
225 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
230 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
231 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
233 int64_t normal = (int64_t)unormal;
234 int32_t n = (int32_t)normal;
237 case sizeof (int8_t):
238 return (dt_printf(dtp, fp, format,
239 (int32_t)*((int8_t *)addr) / n));
240 case sizeof (int16_t):
241 return (dt_printf(dtp, fp, format,
242 (int32_t)*((int16_t *)addr) / n));
243 case sizeof (int32_t):
244 return (dt_printf(dtp, fp, format,
245 *((int32_t *)addr) / n));
246 case sizeof (int64_t):
247 return (dt_printf(dtp, fp, format,
248 *((int64_t *)addr) / normal));
250 return (dt_set_errno(dtp, EDT_DMISMATCH));
256 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
257 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
259 uint32_t n = (uint32_t)normal;
262 case sizeof (uint8_t):
263 return (dt_printf(dtp, fp, format,
264 (uint32_t)*((uint8_t *)addr) / n));
265 case sizeof (uint16_t):
266 return (dt_printf(dtp, fp, format,
267 (uint32_t)*((uint16_t *)addr) / n));
268 case sizeof (uint32_t):
269 return (dt_printf(dtp, fp, format,
270 *((uint32_t *)addr) / n));
271 case sizeof (uint64_t):
272 return (dt_printf(dtp, fp, format,
273 *((uint64_t *)addr) / normal));
275 return (dt_set_errno(dtp, EDT_DMISMATCH));
280 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
281 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
283 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
284 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
286 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
291 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
292 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
294 double n = (double)normal;
295 long double ldn = (long double)normal;
299 return (dt_printf(dtp, fp, format,
300 (double)*((float *)addr) / n));
301 case sizeof (double):
302 return (dt_printf(dtp, fp, format,
303 *((double *)addr) / n));
304 case sizeof (long double):
305 return (dt_printf(dtp, fp, format,
306 *((long double *)addr) / ldn));
308 return (dt_set_errno(dtp, EDT_DMISMATCH));
314 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
315 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
322 case sizeof (uint32_t):
323 val = *((uint32_t *)addr);
325 case sizeof (uint64_t):
326 val = *((uint64_t *)addr);
329 return (dt_set_errno(dtp, EDT_DMISMATCH));
335 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
337 return (dt_printf(dtp, fp, format, s));
342 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
343 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
345 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
350 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
351 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
353 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
358 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
359 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
363 uint64_t val, pid = 0;
365 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
368 case sizeof (uint32_t):
369 val = (u_longlong_t)*((uint32_t *)addr);
371 case sizeof (uint64_t):
372 val = (u_longlong_t)*((uint64_t *)addr);
374 case sizeof (uint64_t) * 2:
375 pid = ((uint64_t *)(uintptr_t)addr)[0];
376 val = ((uint64_t *)(uintptr_t)addr)[1];
379 return (dt_set_errno(dtp, EDT_DMISMATCH));
382 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
388 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
390 return (dt_printf(dtp, fp, format, s));
395 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
396 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
399 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
400 const dtrace_recdesc_t *rec = pfd->pfd_rec;
401 caddr_t addr = (caddr_t)vaddr;
405 * We have stashed the value of the STACKINDENT option, and we will
406 * now override it for the purposes of formatting the stack. If the
407 * field has been specified as left-aligned (i.e. (%-#), we set the
408 * indentation to be the width. This is a slightly odd semantic, but
409 * it's useful functionality -- and it's slightly odd to begin with to
410 * be using a single format specifier to be formatting multiple lines
413 if (pfd->pfd_dynwidth < 0) {
414 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
415 width = -pfd->pfd_dynwidth;
416 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
417 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
422 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
424 switch (rec->dtrd_action) {
425 case DTRACEACT_USTACK:
426 case DTRACEACT_JSTACK:
427 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
430 case DTRACEACT_STACK:
431 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
432 rec->dtrd_size / rec->dtrd_arg);
439 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
446 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
447 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
449 char src[32], buf[32], *dst = buf;
450 hrtime_t time = *((uint64_t *)addr);
451 time_t sec = (time_t)(time / NANOSEC);
455 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
456 * Below, we turn this into the canonical adb/mdb /[yY] format,
457 * "1973 Dec 3 17:20:00".
459 (void) ctime_r(&sec, src, sizeof (src));
462 * Place the 4-digit year at the head of the string...
464 for (i = 20; i < 24; i++)
468 * ...and follow it with the remainder (month, day, hh:mm:ss).
470 for (i = 3; i < 19; i++)
474 return (dt_printf(dtp, fp, format, buf));
478 * This prints the time in RFC 822 standard form. This is useful for emitting
479 * notions of time that are consumed by standard tools (e.g., as part of an
484 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
485 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
487 hrtime_t time = *((uint64_t *)addr);
488 time_t sec = (time_t)(time / NANOSEC);
492 (void) localtime_r(&sec, &tm);
493 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
494 return (dt_printf(dtp, fp, format, buf));
499 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
500 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
502 uint16_t port = htons(*((uint16_t *)addr));
504 struct servent *sv, res;
506 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
507 return (dt_printf(dtp, fp, format, sv->s_name));
509 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
510 return (dt_printf(dtp, fp, format, buf));
515 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
516 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
518 char *s = alloca(size + 1);
519 struct hostent *host, res;
520 char inetaddr[NS_IN6ADDRSZ];
524 bcopy(addr, s, size);
527 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
528 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
529 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
530 return (dt_printf(dtp, fp, format, host->h_name));
531 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
532 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
533 AF_INET6, &e)) != NULL)
534 return (dt_printf(dtp, fp, format, host->h_name));
537 return (dt_printf(dtp, fp, format, s));
542 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
543 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
545 char *s = alloca(size + 1);
547 bcopy(addr, s, size);
549 return (dt_printf(dtp, fp, format, s));
554 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
555 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
557 wchar_t *ws = alloca(size + sizeof (wchar_t));
559 bcopy(addr, ws, size);
560 ws[size / sizeof (wchar_t)] = L'\0';
561 return (dt_printf(dtp, fp, format, ws));
566 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
567 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
572 if ((s = strchr2esc(addr, size)) == NULL)
573 return (dt_set_errno(dtp, EDT_NOMEM));
575 n = dt_printf(dtp, fp, format, s);
581 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
582 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
587 case sizeof (int8_t):
590 case sizeof (int16_t):
591 c = *(int16_t *)addr;
593 case sizeof (int32_t):
594 c = *(int32_t *)addr;
597 return (dt_set_errno(dtp, EDT_DMISMATCH));
600 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
605 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
606 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
608 return (dt_printf(dtp, fp, "%%"));
611 static const char pfproto_xint[] = "char, short, int, long, or long long";
612 static const char pfproto_csi[] = "char, short, or int";
613 static const char pfproto_fp[] = "float, double, or long double";
614 static const char pfproto_addr[] = "pointer or integer";
615 static const char pfproto_uaddr[] =
616 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
617 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
618 static const char pfproto_wstr[] = "wchar_t []";
621 * Printf format conversion dictionary. This table should match the set of
622 * conversions offered by printf(3C), as well as some additional extensions.
623 * The second parameter is an ASCII string which is either an actual type
624 * name we should look up (if pfcheck_type is specified), or just a descriptive
625 * string of the types expected for use in error messages.
627 static const dt_pfconv_t _dtrace_conversions[] = {
628 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
629 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
630 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
631 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
632 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
633 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
634 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
635 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
636 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
637 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
638 { "hd", "d", "short", pfcheck_type, pfprint_sint },
639 { "hi", "i", "short", pfcheck_type, pfprint_sint },
640 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
641 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
642 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
643 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
644 { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
645 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
646 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
647 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
648 { "ld", "d", "long", pfcheck_type, pfprint_sint },
649 { "li", "i", "long", pfcheck_type, pfprint_sint },
650 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
651 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
652 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
653 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
654 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
655 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
656 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
657 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
658 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
659 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
660 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
661 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
662 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
663 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
664 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
665 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
666 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
667 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
668 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
669 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
670 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
671 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
672 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
673 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
674 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
675 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
676 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
677 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
678 { "%", "%", "void", pfcheck_type, pfprint_pct },
679 { NULL, NULL, NULL, NULL, NULL }
683 dt_pfdict_create(dtrace_hdl_t *dtp)
685 uint_t n = _dtrace_strbuckets;
686 const dt_pfconv_t *pfd;
689 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
690 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
692 return (dt_set_errno(dtp, EDT_NOMEM));
695 dtp->dt_pfdict = pdi;
696 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
697 pdi->pdi_nbuckets = n;
699 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
700 dtrace_typeinfo_t dtt;
704 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
705 dt_pfdict_destroy(dtp);
706 return (dt_set_errno(dtp, EDT_NOMEM));
709 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
710 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
711 pfc->pfc_next = pdi->pdi_buckets[h];
712 pdi->pdi_buckets[h] = pfc;
715 dtt.dtt_type = CTF_ERR;
718 * The "D" container or its parent must contain a definition of
719 * any type referenced by a printf conversion. If none can be
720 * found, we fail to initialize the printf dictionary.
722 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
723 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
724 dt_pfdict_destroy(dtp);
725 return (dt_set_errno(dtp, EDT_NOCONV));
728 pfc->pfc_dctfp = dtt.dtt_ctfp;
729 pfc->pfc_dtype = dtt.dtt_type;
732 * The "C" container may contain an alternate definition of an
733 * explicit conversion type. If it does, use it; otherwise
734 * just set pfc_ctype to pfc_dtype so it is always valid.
736 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
737 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
738 pfc->pfc_cctfp = dtt.dtt_ctfp;
739 pfc->pfc_ctype = dtt.dtt_type;
741 pfc->pfc_cctfp = pfc->pfc_dctfp;
742 pfc->pfc_ctype = pfc->pfc_dtype;
745 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
746 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
747 dt_pfdict_destroy(dtp);
748 return (dt_set_errno(dtp, EDT_BADCONV));
751 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
758 dt_pfdict_destroy(dtrace_hdl_t *dtp)
760 dt_pfdict_t *pdi = dtp->dt_pfdict;
761 dt_pfconv_t *pfc, *nfc;
767 for (i = 0; i < pdi->pdi_nbuckets; i++) {
768 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
774 free(pdi->pdi_buckets);
776 dtp->dt_pfdict = NULL;
779 static const dt_pfconv_t *
780 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
782 dt_pfdict_t *pdi = dtp->dt_pfdict;
783 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
784 const dt_pfconv_t *pfc;
786 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
787 if (strcmp(pfc->pfc_name, name) == 0)
795 dt_printf_error(dtrace_hdl_t *dtp, int err)
798 longjmp(yypcb->pcb_jmpbuf, err);
800 (void) dt_set_errno(dtp, err);
805 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
807 dt_pfargd_t *pfd, *nfd = NULL;
812 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
813 (format = strdup(s)) == NULL) {
815 return (dt_printf_error(dtp, EDT_NOMEM));
818 pfv->pfv_format = format;
819 pfv->pfv_argv = NULL;
824 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
833 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
834 dt_printf_destroy(pfv);
835 return (dt_printf_error(dtp, EDT_NOMEM));
838 if (pfv->pfv_argv != NULL)
843 bzero(pfd, sizeof (dt_pfargd_t));
848 pfd->pfd_preflen = (size_t)(p - q);
854 case '0': case '1': case '2': case '3': case '4':
855 case '5': case '6': case '7': case '8': case '9':
856 if (dot == 0 && digits == 0 && c == '0') {
857 pfd->pfd_flags |= DT_PFCONV_ZPAD;
858 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
862 for (n = 0; isdigit(c); c = *++p)
863 n = n * 10 + c - '0';
875 pfd->pfd_flags |= DT_PFCONV_ALT;
879 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
881 if (pfd->pfd_flags & n) {
882 yywarn("format conversion #%u has more than "
883 "one '*' specified for the output %s\n",
884 pfv->pfv_argc, n ? "precision" : "width");
886 dt_printf_destroy(pfv);
887 return (dt_printf_error(dtp, EDT_COMPILER));
894 pfd->pfd_flags |= DT_PFCONV_SPOS;
898 pfd->pfd_flags |= DT_PFCONV_LEFT;
899 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
904 yywarn("format conversion #%u has more than "
905 "one '.' specified\n", pfv->pfv_argc);
907 dt_printf_destroy(pfv);
908 return (dt_printf_error(dtp, EDT_COMPILER));
914 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
921 pfd->pfd_flags |= DT_PFCONV_AGG;
925 pfd->pfd_flags |= DT_PFCONV_GROUP;
929 pfd->pfd_flags |= DT_PFCONV_SPACE;
933 yywarn("format conversion #%u uses unsupported "
934 "positional format (%%n$)\n", pfv->pfv_argc);
936 dt_printf_destroy(pfv);
937 return (dt_printf_error(dtp, EDT_COMPILER));
941 goto default_lbl; /* if %% then use "%" conv */
943 yywarn("format conversion #%u cannot be combined "
944 "with other format flags: %%%%\n", pfv->pfv_argc);
946 dt_printf_destroy(pfv);
947 return (dt_printf_error(dtp, EDT_COMPILER));
950 yywarn("format conversion #%u name expected before "
951 "end of format string\n", pfv->pfv_argc);
953 dt_printf_destroy(pfv);
954 return (dt_printf_error(dtp, EDT_COMPILER));
960 if (namelen < sizeof (name) - 2)
967 name[namelen] = '\0';
970 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
972 if (pfd->pfd_conv == NULL) {
973 yywarn("format conversion #%u is undefined: %%%s\n",
974 pfv->pfv_argc, name);
975 dt_printf_destroy(pfv);
976 return (dt_printf_error(dtp, EDT_COMPILER));
980 if (*q != '\0' || *format == '\0') {
981 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
982 dt_printf_destroy(pfv);
983 return (dt_printf_error(dtp, EDT_NOMEM));
986 if (pfv->pfv_argv != NULL)
991 bzero(pfd, sizeof (dt_pfargd_t));
995 pfd->pfd_preflen = strlen(q);
1002 dt_printf_destroy(dt_pfargv_t *pfv)
1004 dt_pfargd_t *pfd, *nfd;
1006 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1007 nfd = pfd->pfd_next;
1011 free(pfv->pfv_format);
1016 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1017 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1019 dt_pfargd_t *pfd = pfv->pfv_argv;
1020 const char *func = idp->di_name;
1022 char n[DT_TYPE_NAMELEN];
1023 dtrace_typeinfo_t dtt;
1024 const char *aggtype;
1028 if (pfv->pfv_format[0] == '\0') {
1029 xyerror(D_PRINTF_FMT_EMPTY,
1030 "%s( ) format string is empty\n", func);
1033 pfv->pfv_flags = flags;
1036 * We fake up a parse node representing the type that can be used with
1037 * an aggregation result conversion, which -- for all but count() --
1038 * is a signed quantity.
1040 if (kind != DTRACEAGG_COUNT)
1041 aggtype = "int64_t";
1043 aggtype = "uint64_t";
1045 if (dt_type_lookup(aggtype, &dtt) != 0)
1046 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1048 bzero(&aggnode, sizeof (aggnode));
1049 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
1051 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1052 const dt_pfconv_t *pfc = pfd->pfd_conv;
1053 const char *dyns[2];
1060 continue; /* no checking if argd is just a prefix */
1062 if (pfc->pfc_print == &pfprint_pct) {
1063 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1067 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1068 dyns[dync++] = ".*";
1069 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1072 for (; dync != 0; dync--) {
1074 xyerror(D_PRINTF_DYN_PROTO,
1075 "%s( ) prototype mismatch: conversion "
1076 "#%d (%%%s) is missing a corresponding "
1077 "\"%s\" argument\n", func, i + 1,
1078 pfc->pfc_name, dyns[dync - 1]);
1081 if (dt_node_is_integer(dnp) == 0) {
1082 xyerror(D_PRINTF_DYN_TYPE,
1083 "%s( ) argument #%d is incompatible "
1084 "with conversion #%d prototype:\n"
1085 "\tconversion: %% %s %s\n"
1086 "\t prototype: int\n\t argument: %s\n",
1087 func, j + foff + 1, i + 1,
1088 dyns[dync - 1], pfc->pfc_name,
1089 dt_node_type_name(dnp, n, sizeof (n)));
1097 * If this conversion is consuming the aggregation data, set
1098 * the value node pointer (vnp) to a fake node based on the
1099 * aggregating function result type. Otherwise assign vnp to
1100 * the next parse node in the argument list, if there is one.
1102 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1103 if (!(flags & DT_PRINTF_AGGREGATION)) {
1104 xyerror(D_PRINTF_AGG_CONV,
1105 "%%@ conversion requires an aggregation"
1106 " and is not for use with %s( )\n", func);
1108 (void) strlcpy(vname, "aggregating action",
1111 } else if (dnp == NULL) {
1112 xyerror(D_PRINTF_ARG_PROTO,
1113 "%s( ) prototype mismatch: conversion #%d (%%"
1114 "%s) is missing a corresponding value argument\n",
1115 func, i + 1, pfc->pfc_name);
1117 (void) snprintf(vname, sizeof (vname),
1118 "argument #%d", j + foff + 1);
1125 * Fill in the proposed final format string by prepending any
1126 * size-related prefixes to the pfconv's format string. The
1127 * pfc_check() function below may optionally modify the format
1128 * as part of validating the type of the input argument.
1130 if (pfc->pfc_print == &pfprint_sint ||
1131 pfc->pfc_print == &pfprint_uint ||
1132 pfc->pfc_print == &pfprint_dint) {
1133 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1134 (void) strcpy(pfd->pfd_fmt, "ll");
1135 } else if (pfc->pfc_print == &pfprint_fp) {
1136 if (dt_node_type_size(vnp) == sizeof (long double))
1137 (void) strcpy(pfd->pfd_fmt, "L");
1140 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1143 * Validate the format conversion against the value node type.
1144 * If the conversion is good, create the descriptor format
1145 * string by concatenating together any required printf(3C)
1146 * size prefixes with the conversion's native format string.
1148 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1149 xyerror(D_PRINTF_ARG_TYPE,
1150 "%s( ) %s is incompatible with "
1151 "conversion #%d prototype:\n\tconversion: %%%s\n"
1152 "\t prototype: %s\n\t argument: %s\n", func,
1153 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1154 dt_node_type_name(vnp, n, sizeof (n)));
1158 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1159 xyerror(D_PRINTF_ARG_EXTRA,
1160 "%s( ) prototype mismatch: only %d arguments "
1161 "required by this format string\n", func, j);
1166 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1168 dt_ident_t *lid, *rid;
1169 dt_node_t *lproto, *rproto;
1170 int largc, rargc, argn;
1171 char n1[DT_TYPE_NAMELEN];
1172 char n2[DT_TYPE_NAMELEN];
1174 assert(lhs->dn_kind == DT_NODE_AGG);
1175 assert(rhs->dn_kind == DT_NODE_AGG);
1177 lid = lhs->dn_ident;
1178 rid = rhs->dn_ident;
1180 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1181 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1184 * First, get an argument count on each side. These must match.
1186 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1189 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1192 if (largc != rargc) {
1193 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1194 "matching key signatures: @%s has %d key%s, @%s has %d "
1195 "key%s", lid->di_name, rid->di_name,
1196 lid->di_name, largc, largc == 1 ? "" : "s",
1197 rid->di_name, rargc, rargc == 1 ? "" : "s");
1201 * Now iterate over the keys to verify that each type matches.
1203 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1204 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1206 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1207 rproto = rproto->dn_list) {
1208 assert(rproto != NULL);
1210 if (dt_node_is_argcompat(lproto, rproto))
1213 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1214 "incompatible with @%s:\n%9s key #%d: %s\n"
1215 "%9s key #%d: %s\n",
1216 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1217 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1218 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1223 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1224 uint_t nrecs, const void *buf, size_t len, int *ip)
1229 return (dt_set_errno(dtp, EDT_DMISMATCH));
1231 addr = (uintptr_t)buf + recp->dtrd_offset;
1233 if (addr + sizeof (int) > (uintptr_t)buf + len)
1234 return (dt_set_errno(dtp, EDT_DOFFSET));
1236 if (addr & (recp->dtrd_alignment - 1))
1237 return (dt_set_errno(dtp, EDT_DALIGN));
1239 switch (recp->dtrd_size) {
1240 case sizeof (int8_t):
1241 *ip = (int)*((int8_t *)addr);
1243 case sizeof (int16_t):
1244 *ip = (int)*((int16_t *)addr);
1246 case sizeof (int32_t):
1247 *ip = (int)*((int32_t *)addr);
1249 case sizeof (int64_t):
1250 *ip = (int)*((int64_t *)addr);
1253 return (dt_set_errno(dtp, EDT_DMISMATCH));
1261 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1262 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1264 const uint64_t *data = addr;
1266 if (size != sizeof (uint64_t) * 2)
1267 return (dt_set_errno(dtp, EDT_DMISMATCH));
1269 return (dt_printf(dtp, fp, format,
1270 data[0] ? data[1] / normal / data[0] : 0));
1275 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1276 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1278 const uint64_t *data = addr;
1280 if (size != sizeof (uint64_t) * 4)
1281 return (dt_set_errno(dtp, EDT_DMISMATCH));
1283 return (dt_printf(dtp, fp, format,
1284 dt_stddev((uint64_t *)data, normal)));
1289 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1290 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1292 return (dt_print_quantize(dtp, fp, addr, size, normal));
1297 pfprint_lquantize(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 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1304 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1305 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1306 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1308 dt_pfargd_t *pfd = pfv->pfv_argv;
1309 const dtrace_recdesc_t *recp = recs;
1310 const dtrace_aggdata_t *aggdata;
1311 dtrace_aggdesc_t *agg;
1312 caddr_t lim = (caddr_t)buf + len, limit;
1313 char format[64] = "%";
1314 int i, aggrec, curagg = -1;
1318 * If we are formatting an aggregation, set 'aggrec' to the index of
1319 * the final record description (the aggregation result) so we can use
1320 * this record index with any conversion where DT_PFCONV_AGG is set.
1321 * (The actual aggregation used will vary as we increment through the
1322 * aggregation variables that we have been passed.) Finally, we
1323 * decrement nrecs to prevent this record from being used with any
1326 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1327 assert(aggsdata != NULL);
1328 assert(naggvars > 0);
1331 return (dt_set_errno(dtp, EDT_DMISMATCH));
1333 curagg = naggvars > 1 ? 1 : 0;
1334 aggdata = aggsdata[0];
1335 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1339 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1340 const dt_pfconv_t *pfc = pfd->pfd_conv;
1341 int width = pfd->pfd_width;
1342 int prec = pfd->pfd_prec;
1345 char *f = format + 1; /* skip initial '%' */
1346 const dtrace_recdesc_t *rec;
1352 if (pfd->pfd_preflen != 0) {
1353 char *tmp = alloca(pfd->pfd_preflen + 1);
1355 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1356 tmp[pfd->pfd_preflen] = '\0';
1358 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1361 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1363 * For printa(), we flush the buffer after each
1364 * prefix, setting the flags to indicate that
1365 * this is part of the printa() format string.
1367 flags = DTRACE_BUFDATA_AGGFORMAT;
1369 if (pfc == NULL && i == pfv->pfv_argc - 1)
1370 flags |= DTRACE_BUFDATA_AGGLAST;
1372 if (dt_buffered_flush(dtp, NULL, NULL,
1373 aggdata, flags) < 0)
1379 if (pfv->pfv_argc == 1)
1380 return (nrecs != 0);
1385 * If the conversion is %%, just invoke the print callback
1386 * with no data record and continue; it consumes no record.
1388 if (pfc->pfc_print == &pfprint_pct) {
1389 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1391 return (-1); /* errno is set for us */
1394 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1395 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1397 return (-1); /* errno is set for us */
1398 pfd->pfd_dynwidth = width;
1400 pfd->pfd_dynwidth = 0;
1403 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1404 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1405 return (-1); /* errno is set for us */
1407 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1409 * This should be impossible -- the compiler shouldn't
1410 * create a DT_PFCONV_AGG conversion without an
1411 * aggregation present. Still, we'd rather fail
1412 * gracefully than blow up...
1414 if (aggsdata == NULL)
1415 return (dt_set_errno(dtp, EDT_DMISMATCH));
1417 aggdata = aggsdata[curagg];
1418 agg = aggdata->dtada_desc;
1421 * We increment the current aggregation variable, but
1422 * not beyond the number of aggregation variables that
1423 * we're printing. This has the (desired) effect that
1424 * DT_PFCONV_AGG conversions beyond the number of
1425 * aggregation variables (re-)convert the aggregation
1426 * value of the last aggregation variable.
1428 if (curagg < naggvars - 1)
1431 rec = &agg->dtagd_rec[aggrec];
1432 addr = aggdata->dtada_data + rec->dtrd_offset;
1433 limit = addr + aggdata->dtada_size;
1434 normal = aggdata->dtada_normal;
1435 flags = DTRACE_BUFDATA_AGGVAL;
1438 return (dt_set_errno(dtp, EDT_DMISMATCH));
1440 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1442 * When printing aggregation keys, we always
1443 * set the aggdata to be the representative
1444 * (zeroth) aggregation. The aggdata isn't
1445 * actually used here in this case, but it is
1446 * passed to the buffer handler and must
1447 * therefore still be correct.
1449 aggdata = aggsdata[0];
1450 flags = DTRACE_BUFDATA_AGGKEY;
1455 addr = (caddr_t)buf + rec->dtrd_offset;
1460 size = rec->dtrd_size;
1462 if (addr + size > limit) {
1463 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1464 (void *)addr, rec->dtrd_size, (void *)lim);
1465 return (dt_set_errno(dtp, EDT_DOFFSET));
1468 if (rec->dtrd_alignment != 0 &&
1469 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1470 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1471 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1472 return (dt_set_errno(dtp, EDT_DALIGN));
1475 switch (rec->dtrd_action) {
1477 func = pfprint_average;
1479 case DTRACEAGG_STDDEV:
1480 func = pfprint_stddev;
1482 case DTRACEAGG_QUANTIZE:
1483 func = pfprint_quantize;
1485 case DTRACEAGG_LQUANTIZE:
1486 func = pfprint_lquantize;
1491 case DTRACEACT_UMOD:
1492 func = pfprint_umod;
1495 func = pfc->pfc_print;
1499 if (pfd->pfd_flags & DT_PFCONV_ALT)
1501 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1503 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1505 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1507 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1509 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1513 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1514 * don't add the width to the format string. See the block
1515 * comment in pfprint_stack() for a description of the
1516 * behavior in this case.
1518 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1522 f += snprintf(f, sizeof (format), "%d", ABS(width));
1525 f += snprintf(f, sizeof (format), ".%d", prec);
1527 (void) strcpy(f, pfd->pfd_fmt);
1530 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1531 return (-1); /* errno is set for us */
1533 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1535 * For printa(), we flush the buffer after each tuple
1536 * element, inidicating that this is the last record
1539 if (i == pfv->pfv_argc - 1)
1540 flags |= DTRACE_BUFDATA_AGGLAST;
1542 if (dt_buffered_flush(dtp, NULL,
1543 rec, aggdata, flags) < 0)
1548 return ((int)(recp - recs));
1552 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1553 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1555 dtrace_optval_t size;
1558 rval = dtrace_getopt(dtp, "strsize", &size);
1560 assert(dtp->dt_sprintf_buflen == 0);
1562 if (dtp->dt_sprintf_buf != NULL)
1563 free(dtp->dt_sprintf_buf);
1565 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1566 return (dt_set_errno(dtp, EDT_NOMEM));
1568 bzero(dtp->dt_sprintf_buf, size);
1569 dtp->dt_sprintf_buflen = size;
1570 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1572 dtp->dt_sprintf_buflen = 0;
1575 free(dtp->dt_sprintf_buf);
1582 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1583 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1584 uint_t nrecs, const void *buf, size_t len)
1586 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1592 * Before we execute the specified command, flush fp to assure that
1593 * any prior dt_printf()'s appear before the output of the command
1598 if (system(dtp->dt_sprintf_buf) == -1)
1599 return (dt_set_errno(dtp, errno));
1605 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1606 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1607 uint_t nrecs, const void *buf, size_t len)
1609 char selfbuf[40], restorebuf[40], *filename;
1612 dt_pfargv_t *pfv = fmtdata;
1613 dt_pfargd_t *pfd = pfv->pfv_argv;
1615 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1617 if (rval == -1 || fp == NULL)
1620 if (pfd->pfd_preflen != 0 &&
1621 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1623 * The only way to have the format string set to the value
1624 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1625 * denoting that we should restore the old stdout.
1627 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1629 if (dtp->dt_stdout_fd == -1) {
1631 * We could complain here by generating an error,
1632 * but it seems like overkill: it seems that calling
1633 * freopen() to restore stdout when freopen() has
1634 * never before been called should just be a no-op,
1635 * so we just return in this case.
1640 (void) snprintf(restorebuf, sizeof (restorebuf),
1641 "/dev/fd/%d", dtp->dt_stdout_fd);
1642 filename = restorebuf;
1644 filename = dtp->dt_sprintf_buf;
1648 * freopen(3C) will always close the specified stream and underlying
1649 * file descriptor -- even if the specified file can't be opened.
1650 * Even for the semantic cesspool that is standard I/O, this is
1651 * surprisingly brain-dead behavior: it means that any failure to
1652 * open the specified file destroys the specified stream in the
1653 * process -- which is particularly relevant when the specified stream
1654 * happens (or rather, happened) to be stdout. This could be resolved
1655 * were there an "fdreopen()" equivalent of freopen() that allowed one
1656 * to pass a file descriptor instead of the name of a file, but there
1657 * is no such thing. However, we can effect this ourselves by first
1658 * fopen()'ing the desired file, and then (assuming that that works),
1659 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1660 * file descriptor for the fopen()'d file. This way, if the fopen()
1661 * fails, we can fail the operation without destroying stdout.
1663 if ((nfp = fopen(filename, "aF")) == NULL) {
1664 char *msg = strerror(errno), *faultstr;
1667 len += strlen(msg) + strlen(filename);
1668 faultstr = alloca(len);
1670 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1671 filename, strerror(errno));
1673 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1679 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1681 if (dtp->dt_stdout_fd == -1) {
1683 * If this is the first time that we're calling freopen(),
1684 * we're going to stash away the file descriptor for stdout.
1685 * We don't expect the dup(2) to fail, so if it does we must
1688 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1690 return (dt_set_errno(dtp, errno));
1694 if (freopen(selfbuf, "aF", fp) == NULL) {
1696 return (dt_set_errno(dtp, errno));
1706 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1707 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1708 uint_t nrecs, const void *buf, size_t len)
1710 return (dt_printf_format(dtp, fp, fmtdata,
1711 recp, nrecs, buf, len, NULL, 0));
1715 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1717 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1722 return (NULL); /* errno has been set for us */
1724 pfd = pfv->pfv_argv;
1726 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1727 const dt_pfconv_t *pfc = pfd->pfd_conv;
1733 * If the output format is not %s then we assume that we have
1734 * been given a correctly-sized format string, so we copy the
1735 * true format name including the size modifier. If the output
1736 * format is %s, then either the input format is %s as well or
1737 * it is one of our custom formats (e.g. pfprint_addr), so we
1738 * must set pfd_fmt to be the output format conversion "s".
1740 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1741 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1743 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1750 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1752 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1755 return (NULL); /* errno has been set for us */
1757 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1764 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1766 dt_pfargv_t *pfv = fmtdata;
1767 dt_pfargd_t *pfd = pfv->pfv_argv;
1770 * An upper bound on the string length is the length of the original
1771 * format string, plus three times the number of conversions (each
1772 * conversion could add up an additional "ll" and/or pfd_width digit
1773 * in the case of converting %? to %16) plus one for a terminating \0.
1775 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1776 char *format = alloca(formatlen);
1780 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1781 const dt_pfconv_t *pfc = pfd->pfd_conv;
1783 int width = pfd->pfd_width;
1784 int prec = pfd->pfd_prec;
1786 if (pfd->pfd_preflen != 0) {
1787 for (j = 0; j < pfd->pfd_preflen; j++)
1788 *f++ = pfd->pfd_prefix[j];
1796 if (pfd->pfd_flags & DT_PFCONV_ALT)
1798 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1800 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1802 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1804 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1806 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1810 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1812 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1814 if (pfd->pfd_flags & DT_PFCONV_AGG)
1818 f += snprintf(f, sizeof (format), "%d", width);
1821 f += snprintf(f, sizeof (format), ".%d", prec);
1824 * If the output format is %s, then either %s is the underlying
1825 * conversion or the conversion is one of our customized ones,
1826 * e.g. pfprint_addr. In these cases, put the original string
1827 * name of the conversion (pfc_name) into the pickled format
1828 * string rather than the derived conversion (pfd_fmt).
1830 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1831 str = pfc->pfc_name;
1835 for (j = 0; str[j] != '\0'; j++)
1839 *f = '\0'; /* insert nul byte; do not count in return value */
1841 assert(f < format + formatlen);
1842 (void) strncpy(s, format, len);
1844 return ((size_t)(f - format));
1848 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1850 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1851 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1852 uint_t nrecs = agg->dtagd_nrecs;
1853 dt_pfwalk_t *pfw = arg;
1854 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1857 if (dt_printf_getint(dtp, recp++, nrecs--,
1858 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1859 return (0); /* no aggregation id or id does not match */
1861 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1862 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1863 return (pfw->pfw_err = dtp->dt_errno);
1866 * Cast away the const to set the bit indicating that this aggregation
1869 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1875 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1877 const dtrace_aggdata_t *aggdata = aggsdata[0];
1878 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1879 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1880 uint_t nrecs = agg->dtagd_nrecs - 1;
1881 dt_pfwalk_t *pfw = arg;
1882 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1885 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1886 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
1887 aggsdata, naggvars) == -1)
1888 return (pfw->pfw_err = dtp->dt_errno);
1891 * For each aggregation, indicate that it has been printed, casting
1892 * away the const as necessary.
1894 for (i = 1; i < naggvars; i++) {
1895 agg = aggsdata[i]->dtada_desc;
1896 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1903 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1904 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
1905 uint_t nrecs, const void *buf, size_t len)
1908 int i, naggvars = 0;
1909 dtrace_aggvarid_t *aggvars;
1911 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
1914 * This might be a printa() with multiple aggregation variables. We
1915 * need to scan forward through the records until we find a record from
1916 * a different statement.
1918 for (i = 0; i < nrecs; i++) {
1919 const dtrace_recdesc_t *nrec = &recs[i];
1921 if (nrec->dtrd_uarg != recs->dtrd_uarg)
1924 if (nrec->dtrd_action != recs->dtrd_action)
1925 return (dt_set_errno(dtp, EDT_BADAGG));
1927 aggvars[naggvars++] =
1928 /* LINTED - alignment */
1929 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
1933 return (dt_set_errno(dtp, EDT_BADAGG));
1935 pfw.pfw_argv = fmtdata;
1939 if (naggvars == 1) {
1940 pfw.pfw_aid = aggvars[0];
1942 if (dtrace_aggregate_walk_sorted(dtp,
1943 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
1944 return (-1); /* errno is set for us */
1946 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
1947 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
1948 return (-1); /* errno is set for us */