4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
29 #include <sys/sysmacros.h>
31 #define ABS(a) ((a) < 0 ? -(a) : (a))
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <arpa/nameser.h>
49 #include <dt_printf.h>
50 #include <dt_string.h>
55 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
57 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
62 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
64 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
65 dt_node_is_symaddr(dnp));
70 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
72 dtrace_hdl_t *dtp = pfv->pfv_dtp;
73 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
75 if (dt_node_is_usymaddr(dnp))
78 if (idp == NULL || idp->di_id == 0)
81 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
86 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
88 return (dt_node_is_stack(dnp));
93 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
95 return (dt_node_is_integer(dnp) &&
96 dt_node_type_size(dnp) == sizeof (uint64_t));
101 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
109 if (dt_node_is_string(dnp))
113 base = ctf_type_resolve(ctfp, dnp->dn_type);
114 kind = ctf_type_kind(ctfp, base);
116 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
117 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
118 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
123 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
125 ctf_file_t *ctfp = dnp->dn_ctfp;
126 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
127 uint_t kind = ctf_type_kind(ctfp, base);
132 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
133 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
134 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
135 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
140 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
142 return (dt_node_is_integer(dnp) &&
143 dt_node_type_size(dnp) <= sizeof (int));
148 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
150 return (dt_node_is_float(dnp));
155 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
157 return (dt_node_is_integer(dnp));
162 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
164 if (dnp->dn_flags & DT_NF_SIGNED)
165 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'i';
167 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
169 return (dt_node_is_integer(dnp));
174 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
176 ctf_file_t *ctfp = dnp->dn_ctfp;
177 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
178 char n[DT_TYPE_NAMELEN];
180 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
181 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
182 strcmp(n, "unsigned short") == 0));
187 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
189 ctf_file_t *ctfp = dnp->dn_ctfp;
190 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
191 char n[DT_TYPE_NAMELEN];
193 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
194 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
195 strcmp(n, "unsigned long") == 0));
200 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
202 ctf_file_t *ctfp = dnp->dn_ctfp;
203 ctf_id_t type = dnp->dn_type;
204 char n[DT_TYPE_NAMELEN];
206 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
207 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
208 strcmp(n, "signed long long") == 0 ||
209 strcmp(n, "unsigned long long") == 0))
213 * If the type used for %llx or %llX is not an [unsigned] long long, we
214 * also permit it to be a [u]int64_t or any typedef thereof. We know
215 * that these typedefs are guaranteed to work with %ll[xX] in either
216 * compilation environment even though they alias to "long" in LP64.
218 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
219 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
220 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
223 type = ctf_type_reference(ctfp, type);
231 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
233 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
234 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
239 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
240 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
242 int64_t normal = (int64_t)unormal;
243 int32_t n = (int32_t)normal;
246 case sizeof (int8_t):
247 return (dt_printf(dtp, fp, format,
248 (int32_t)*((int8_t *)addr) / n));
249 case sizeof (int16_t):
250 return (dt_printf(dtp, fp, format,
251 (int32_t)*((int16_t *)addr) / n));
252 case sizeof (int32_t):
253 return (dt_printf(dtp, fp, format,
254 *((int32_t *)addr) / n));
255 case sizeof (int64_t):
256 return (dt_printf(dtp, fp, format,
257 *((int64_t *)addr) / normal));
259 return (dt_set_errno(dtp, EDT_DMISMATCH));
265 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
266 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
268 uint32_t n = (uint32_t)normal;
271 case sizeof (uint8_t):
272 return (dt_printf(dtp, fp, format,
273 (uint32_t)*((uint8_t *)addr) / n));
274 case sizeof (uint16_t):
275 return (dt_printf(dtp, fp, format,
276 (uint32_t)*((uint16_t *)addr) / n));
277 case sizeof (uint32_t):
278 return (dt_printf(dtp, fp, format,
279 *((uint32_t *)addr) / n));
280 case sizeof (uint64_t):
281 return (dt_printf(dtp, fp, format,
282 *((uint64_t *)addr) / normal));
284 return (dt_set_errno(dtp, EDT_DMISMATCH));
289 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
290 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
292 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
293 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
295 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
300 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
301 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
303 double n = (double)normal;
304 long double ldn = (long double)normal;
308 return (dt_printf(dtp, fp, format,
309 (double)*((float *)addr) / n));
310 case sizeof (double):
311 return (dt_printf(dtp, fp, format,
312 *((double *)addr) / n));
313 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
314 case sizeof (long double):
315 return (dt_printf(dtp, fp, format,
316 *((long double *)addr) / ldn));
319 return (dt_set_errno(dtp, EDT_DMISMATCH));
325 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
326 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
333 case sizeof (uint32_t):
334 val = *((uint32_t *)addr);
336 case sizeof (uint64_t):
337 val = *((uint64_t *)addr);
340 return (dt_set_errno(dtp, EDT_DMISMATCH));
346 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
348 return (dt_printf(dtp, fp, format, s));
353 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
354 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
356 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
361 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
362 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
364 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
369 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
370 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
374 uint64_t val, pid = 0;
376 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
379 case sizeof (uint32_t):
380 val = (u_longlong_t)*((uint32_t *)addr);
382 case sizeof (uint64_t):
383 val = (u_longlong_t)*((uint64_t *)addr);
385 case sizeof (uint64_t) * 2:
386 pid = ((uint64_t *)(uintptr_t)addr)[0];
387 val = ((uint64_t *)(uintptr_t)addr)[1];
390 return (dt_set_errno(dtp, EDT_DMISMATCH));
393 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
399 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
401 return (dt_printf(dtp, fp, format, s));
406 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
407 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
410 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
411 const dtrace_recdesc_t *rec = pfd->pfd_rec;
412 caddr_t addr = (caddr_t)vaddr;
416 * We have stashed the value of the STACKINDENT option, and we will
417 * now override it for the purposes of formatting the stack. If the
418 * field has been specified as left-aligned (i.e. (%-#), we set the
419 * indentation to be the width. This is a slightly odd semantic, but
420 * it's useful functionality -- and it's slightly odd to begin with to
421 * be using a single format specifier to be formatting multiple lines
424 if (pfd->pfd_dynwidth < 0) {
425 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
426 width = -pfd->pfd_dynwidth;
427 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
428 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
433 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
435 switch (rec->dtrd_action) {
436 case DTRACEACT_USTACK:
437 case DTRACEACT_JSTACK:
438 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
441 case DTRACEACT_STACK:
442 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
443 rec->dtrd_size / rec->dtrd_arg);
450 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
457 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
458 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
460 char src[32], buf[32], *dst = buf;
461 hrtime_t time = *((uint64_t *)addr);
462 time_t sec = (time_t)(time / NANOSEC);
466 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
467 * Below, we turn this into the canonical adb/mdb /[yY] format,
468 * "1973 Dec 3 17:20:00".
471 (void) ctime_r(&sec, src, sizeof (src));
473 (void) ctime_r(&sec, src);
477 * Place the 4-digit year at the head of the string...
479 for (i = 20; i < 24; i++)
483 * ...and follow it with the remainder (month, day, hh:mm:ss).
485 for (i = 3; i < 19; i++)
489 return (dt_printf(dtp, fp, format, buf));
493 * This prints the time in RFC 822 standard form. This is useful for emitting
494 * notions of time that are consumed by standard tools (e.g., as part of an
499 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
500 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
502 hrtime_t time = *((uint64_t *)addr);
503 time_t sec = (time_t)(time / NANOSEC);
507 (void) localtime_r(&sec, &tm);
508 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
509 return (dt_printf(dtp, fp, format, buf));
514 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
515 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
517 uint16_t port = htons(*((uint16_t *)addr));
519 struct servent *sv, res;
522 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
524 if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
526 return (dt_printf(dtp, fp, format, sv->s_name));
528 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
529 return (dt_printf(dtp, fp, format, buf));
534 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
535 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
537 char *s = alloca(size + 1);
538 struct hostent *host, res;
539 char inetaddr[NS_IN6ADDRSZ];
543 bcopy(addr, s, size);
546 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
548 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
549 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
551 if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
552 AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
554 return (dt_printf(dtp, fp, format, host->h_name));
555 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
556 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
557 AF_INET6, &e)) != NULL)
558 return (dt_printf(dtp, fp, format, host->h_name));
561 return (dt_printf(dtp, fp, format, s));
566 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
567 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
569 char *s = alloca(size + 1);
571 bcopy(addr, s, size);
573 return (dt_printf(dtp, fp, format, s));
578 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
579 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
581 wchar_t *ws = alloca(size + sizeof (wchar_t));
583 bcopy(addr, ws, size);
584 ws[size / sizeof (wchar_t)] = L'\0';
585 return (dt_printf(dtp, fp, format, ws));
590 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
591 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
596 if ((s = strchr2esc(addr, size)) == NULL)
597 return (dt_set_errno(dtp, EDT_NOMEM));
599 n = dt_printf(dtp, fp, format, s);
605 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
606 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
611 case sizeof (int8_t):
614 case sizeof (int16_t):
615 c = *(int16_t *)addr;
617 case sizeof (int32_t):
618 c = *(int32_t *)addr;
621 return (dt_set_errno(dtp, EDT_DMISMATCH));
624 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
629 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
630 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
632 return (dt_printf(dtp, fp, "%%"));
635 static const char pfproto_xint[] = "char, short, int, long, or long long";
636 static const char pfproto_csi[] = "char, short, or int";
637 static const char pfproto_fp[] = "float, double, or long double";
638 static const char pfproto_addr[] = "pointer or integer";
639 static const char pfproto_uaddr[] =
640 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
641 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
642 static const char pfproto_wstr[] = "wchar_t []";
645 * Printf format conversion dictionary. This table should match the set of
646 * conversions offered by printf(3C), as well as some additional extensions.
647 * The second parameter is an ASCII string which is either an actual type
648 * name we should look up (if pfcheck_type is specified), or just a descriptive
649 * string of the types expected for use in error messages.
651 static const dt_pfconv_t _dtrace_conversions[] = {
652 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
653 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
654 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
655 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
656 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
657 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
658 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
659 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
660 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
661 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
662 { "hd", "d", "short", pfcheck_type, pfprint_sint },
663 { "hi", "i", "short", pfcheck_type, pfprint_sint },
664 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
665 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
666 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
667 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
668 { "i", "i", pfproto_xint, pfcheck_xint, pfprint_sint },
669 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
670 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
671 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
672 { "ld", "d", "long", pfcheck_type, pfprint_sint },
673 { "li", "i", "long", pfcheck_type, pfprint_sint },
674 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
675 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
676 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
677 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
678 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
679 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
680 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
681 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
682 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
683 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
684 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
685 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
686 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
687 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
688 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
689 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
690 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
691 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
692 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
693 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
694 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
695 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
696 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
698 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
699 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
701 { "wc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
702 { "ws", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
704 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
705 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
706 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
707 { "%", "%", "void", pfcheck_type, pfprint_pct },
708 { NULL, NULL, NULL, NULL, NULL }
712 dt_pfdict_create(dtrace_hdl_t *dtp)
714 uint_t n = _dtrace_strbuckets;
715 const dt_pfconv_t *pfd;
718 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
719 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
721 return (dt_set_errno(dtp, EDT_NOMEM));
724 dtp->dt_pfdict = pdi;
725 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
726 pdi->pdi_nbuckets = n;
728 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
729 dtrace_typeinfo_t dtt;
733 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
734 dt_pfdict_destroy(dtp);
735 return (dt_set_errno(dtp, EDT_NOMEM));
738 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
739 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
740 pfc->pfc_next = pdi->pdi_buckets[h];
741 pdi->pdi_buckets[h] = pfc;
744 dtt.dtt_type = CTF_ERR;
747 * The "D" container or its parent must contain a definition of
748 * any type referenced by a printf conversion. If none can be
749 * found, we fail to initialize the printf dictionary.
751 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
752 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
753 dt_pfdict_destroy(dtp);
754 return (dt_set_errno(dtp, EDT_NOCONV));
757 pfc->pfc_dctfp = dtt.dtt_ctfp;
758 pfc->pfc_dtype = dtt.dtt_type;
761 * The "C" container may contain an alternate definition of an
762 * explicit conversion type. If it does, use it; otherwise
763 * just set pfc_ctype to pfc_dtype so it is always valid.
765 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
766 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
767 pfc->pfc_cctfp = dtt.dtt_ctfp;
768 pfc->pfc_ctype = dtt.dtt_type;
770 pfc->pfc_cctfp = pfc->pfc_dctfp;
771 pfc->pfc_ctype = pfc->pfc_dtype;
774 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
775 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
776 dt_pfdict_destroy(dtp);
777 return (dt_set_errno(dtp, EDT_BADCONV));
780 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
787 dt_pfdict_destroy(dtrace_hdl_t *dtp)
789 dt_pfdict_t *pdi = dtp->dt_pfdict;
790 dt_pfconv_t *pfc, *nfc;
796 for (i = 0; i < pdi->pdi_nbuckets; i++) {
797 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
803 free(pdi->pdi_buckets);
805 dtp->dt_pfdict = NULL;
808 static const dt_pfconv_t *
809 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
811 dt_pfdict_t *pdi = dtp->dt_pfdict;
812 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
813 const dt_pfconv_t *pfc;
815 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
816 if (strcmp(pfc->pfc_name, name) == 0)
824 dt_printf_error(dtrace_hdl_t *dtp, int err)
827 longjmp(yypcb->pcb_jmpbuf, err);
829 (void) dt_set_errno(dtp, err);
834 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
836 dt_pfargd_t *pfd, *nfd = NULL;
841 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
842 (format = strdup(s)) == NULL) {
844 return (dt_printf_error(dtp, EDT_NOMEM));
847 pfv->pfv_format = format;
848 pfv->pfv_argv = NULL;
853 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
862 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
863 dt_printf_destroy(pfv);
864 return (dt_printf_error(dtp, EDT_NOMEM));
867 if (pfv->pfv_argv != NULL)
872 bzero(pfd, sizeof (dt_pfargd_t));
877 pfd->pfd_preflen = (size_t)(p - q);
883 case '0': case '1': case '2': case '3': case '4':
884 case '5': case '6': case '7': case '8': case '9':
885 if (dot == 0 && digits == 0 && c == '0') {
886 pfd->pfd_flags |= DT_PFCONV_ZPAD;
887 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
891 for (n = 0; isdigit(c); c = *++p)
892 n = n * 10 + c - '0';
904 pfd->pfd_flags |= DT_PFCONV_ALT;
908 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
910 if (pfd->pfd_flags & n) {
911 yywarn("format conversion #%u has more than "
912 "one '*' specified for the output %s\n",
913 pfv->pfv_argc, n ? "precision" : "width");
915 dt_printf_destroy(pfv);
916 return (dt_printf_error(dtp, EDT_COMPILER));
923 pfd->pfd_flags |= DT_PFCONV_SPOS;
927 pfd->pfd_flags |= DT_PFCONV_LEFT;
928 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
933 yywarn("format conversion #%u has more than "
934 "one '.' specified\n", pfv->pfv_argc);
936 dt_printf_destroy(pfv);
937 return (dt_printf_error(dtp, EDT_COMPILER));
943 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
950 pfd->pfd_flags |= DT_PFCONV_AGG;
954 pfd->pfd_flags |= DT_PFCONV_GROUP;
958 pfd->pfd_flags |= DT_PFCONV_SPACE;
962 yywarn("format conversion #%u uses unsupported "
963 "positional format (%%n$)\n", pfv->pfv_argc);
965 dt_printf_destroy(pfv);
966 return (dt_printf_error(dtp, EDT_COMPILER));
970 goto default_lbl; /* if %% then use "%" conv */
972 yywarn("format conversion #%u cannot be combined "
973 "with other format flags: %%%%\n", pfv->pfv_argc);
975 dt_printf_destroy(pfv);
976 return (dt_printf_error(dtp, EDT_COMPILER));
979 yywarn("format conversion #%u name expected before "
980 "end of format string\n", pfv->pfv_argc);
982 dt_printf_destroy(pfv);
983 return (dt_printf_error(dtp, EDT_COMPILER));
989 if (namelen < sizeof (name) - 2)
996 name[namelen] = '\0';
999 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
1001 if (pfd->pfd_conv == NULL) {
1002 yywarn("format conversion #%u is undefined: %%%s\n",
1003 pfv->pfv_argc, name);
1004 dt_printf_destroy(pfv);
1005 return (dt_printf_error(dtp, EDT_COMPILER));
1009 if (*q != '\0' || *format == '\0') {
1010 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1011 dt_printf_destroy(pfv);
1012 return (dt_printf_error(dtp, EDT_NOMEM));
1015 if (pfv->pfv_argv != NULL)
1016 nfd->pfd_next = pfd;
1018 pfv->pfv_argv = pfd;
1020 bzero(pfd, sizeof (dt_pfargd_t));
1023 pfd->pfd_prefix = q;
1024 pfd->pfd_preflen = strlen(q);
1031 dt_printf_destroy(dt_pfargv_t *pfv)
1033 dt_pfargd_t *pfd, *nfd;
1035 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1036 nfd = pfd->pfd_next;
1040 free(pfv->pfv_format);
1045 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1046 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1048 dt_pfargd_t *pfd = pfv->pfv_argv;
1049 const char *func = idp->di_name;
1051 char n[DT_TYPE_NAMELEN];
1052 dtrace_typeinfo_t dtt;
1053 const char *aggtype;
1057 if (pfv->pfv_format[0] == '\0') {
1058 xyerror(D_PRINTF_FMT_EMPTY,
1059 "%s( ) format string is empty\n", func);
1062 pfv->pfv_flags = flags;
1065 * We fake up a parse node representing the type that can be used with
1066 * an aggregation result conversion, which -- for all but count() --
1067 * is a signed quantity.
1069 if (kind != DTRACEAGG_COUNT)
1070 aggtype = "int64_t";
1072 aggtype = "uint64_t";
1074 if (dt_type_lookup(aggtype, &dtt) != 0)
1075 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1077 bzero(&aggnode, sizeof (aggnode));
1078 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
1080 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1081 const dt_pfconv_t *pfc = pfd->pfd_conv;
1082 const char *dyns[2];
1089 continue; /* no checking if argd is just a prefix */
1091 if (pfc->pfc_print == &pfprint_pct) {
1092 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1096 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1097 dyns[dync++] = ".*";
1098 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1101 for (; dync != 0; dync--) {
1103 xyerror(D_PRINTF_DYN_PROTO,
1104 "%s( ) prototype mismatch: conversion "
1105 "#%d (%%%s) is missing a corresponding "
1106 "\"%s\" argument\n", func, i + 1,
1107 pfc->pfc_name, dyns[dync - 1]);
1110 if (dt_node_is_integer(dnp) == 0) {
1111 xyerror(D_PRINTF_DYN_TYPE,
1112 "%s( ) argument #%d is incompatible "
1113 "with conversion #%d prototype:\n"
1114 "\tconversion: %% %s %s\n"
1115 "\t prototype: int\n\t argument: %s\n",
1116 func, j + foff + 1, i + 1,
1117 dyns[dync - 1], pfc->pfc_name,
1118 dt_node_type_name(dnp, n, sizeof (n)));
1126 * If this conversion is consuming the aggregation data, set
1127 * the value node pointer (vnp) to a fake node based on the
1128 * aggregating function result type. Otherwise assign vnp to
1129 * the next parse node in the argument list, if there is one.
1131 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1132 if (!(flags & DT_PRINTF_AGGREGATION)) {
1133 xyerror(D_PRINTF_AGG_CONV,
1134 "%%@ conversion requires an aggregation"
1135 " and is not for use with %s( )\n", func);
1137 (void) strlcpy(vname, "aggregating action",
1140 } else if (dnp == NULL) {
1141 xyerror(D_PRINTF_ARG_PROTO,
1142 "%s( ) prototype mismatch: conversion #%d (%%"
1143 "%s) is missing a corresponding value argument\n",
1144 func, i + 1, pfc->pfc_name);
1146 (void) snprintf(vname, sizeof (vname),
1147 "argument #%d", j + foff + 1);
1154 * Fill in the proposed final format string by prepending any
1155 * size-related prefixes to the pfconv's format string. The
1156 * pfc_check() function below may optionally modify the format
1157 * as part of validating the type of the input argument.
1159 if (pfc->pfc_print == &pfprint_sint ||
1160 pfc->pfc_print == &pfprint_uint ||
1161 pfc->pfc_print == &pfprint_dint) {
1162 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1163 (void) strcpy(pfd->pfd_fmt, "ll");
1164 } else if (pfc->pfc_print == &pfprint_fp) {
1165 if (dt_node_type_size(vnp) == sizeof (long double))
1166 (void) strcpy(pfd->pfd_fmt, "L");
1169 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1172 * Validate the format conversion against the value node type.
1173 * If the conversion is good, create the descriptor format
1174 * string by concatenating together any required printf(3C)
1175 * size prefixes with the conversion's native format string.
1177 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1178 xyerror(D_PRINTF_ARG_TYPE,
1179 "%s( ) %s is incompatible with "
1180 "conversion #%d prototype:\n\tconversion: %%%s\n"
1181 "\t prototype: %s\n\t argument: %s\n", func,
1182 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1183 dt_node_type_name(vnp, n, sizeof (n)));
1187 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1188 xyerror(D_PRINTF_ARG_EXTRA,
1189 "%s( ) prototype mismatch: only %d arguments "
1190 "required by this format string\n", func, j);
1195 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1197 dt_ident_t *lid, *rid;
1198 dt_node_t *lproto, *rproto;
1199 int largc, rargc, argn;
1200 char n1[DT_TYPE_NAMELEN];
1201 char n2[DT_TYPE_NAMELEN];
1203 assert(lhs->dn_kind == DT_NODE_AGG);
1204 assert(rhs->dn_kind == DT_NODE_AGG);
1206 lid = lhs->dn_ident;
1207 rid = rhs->dn_ident;
1209 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1210 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1213 * First, get an argument count on each side. These must match.
1215 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1218 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1221 if (largc != rargc) {
1222 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1223 "matching key signatures: @%s has %d key%s, @%s has %d "
1224 "key%s", lid->di_name, rid->di_name,
1225 lid->di_name, largc, largc == 1 ? "" : "s",
1226 rid->di_name, rargc, rargc == 1 ? "" : "s");
1230 * Now iterate over the keys to verify that each type matches.
1232 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1233 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1235 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1236 rproto = rproto->dn_list) {
1237 assert(rproto != NULL);
1239 if (dt_node_is_argcompat(lproto, rproto))
1242 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1243 "incompatible with @%s:\n%9s key #%d: %s\n"
1244 "%9s key #%d: %s\n",
1245 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1246 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1247 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1252 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1253 uint_t nrecs, const void *buf, size_t len, int *ip)
1258 return (dt_set_errno(dtp, EDT_DMISMATCH));
1260 addr = (uintptr_t)buf + recp->dtrd_offset;
1262 if (addr + sizeof (int) > (uintptr_t)buf + len)
1263 return (dt_set_errno(dtp, EDT_DOFFSET));
1265 if (addr & (recp->dtrd_alignment - 1))
1266 return (dt_set_errno(dtp, EDT_DALIGN));
1268 switch (recp->dtrd_size) {
1269 case sizeof (int8_t):
1270 *ip = (int)*((int8_t *)addr);
1272 case sizeof (int16_t):
1273 *ip = (int)*((int16_t *)addr);
1275 case sizeof (int32_t):
1276 *ip = (int)*((int32_t *)addr);
1278 case sizeof (int64_t):
1279 *ip = (int)*((int64_t *)addr);
1282 return (dt_set_errno(dtp, EDT_DMISMATCH));
1290 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1291 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1293 const uint64_t *data = addr;
1295 if (size != sizeof (uint64_t) * 2)
1296 return (dt_set_errno(dtp, EDT_DMISMATCH));
1298 return (dt_printf(dtp, fp, format,
1299 data[0] ? data[1] / normal / data[0] : 0));
1304 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1305 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1307 const uint64_t *data = addr;
1309 if (size != sizeof (uint64_t) * 4)
1310 return (dt_set_errno(dtp, EDT_DMISMATCH));
1312 return (dt_printf(dtp, fp, format,
1313 dt_stddev((uint64_t *)data, normal)));
1318 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1319 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1321 return (dt_print_quantize(dtp, fp, addr, size, normal));
1326 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1329 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1334 pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1335 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1337 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1341 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1342 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1343 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1345 dt_pfargd_t *pfd = pfv->pfv_argv;
1346 const dtrace_recdesc_t *recp = recs;
1347 const dtrace_aggdata_t *aggdata;
1348 dtrace_aggdesc_t *agg;
1349 caddr_t lim = (caddr_t)buf + len, limit;
1350 char format[64] = "%";
1351 int i, aggrec, curagg = -1;
1355 * If we are formatting an aggregation, set 'aggrec' to the index of
1356 * the final record description (the aggregation result) so we can use
1357 * this record index with any conversion where DT_PFCONV_AGG is set.
1358 * (The actual aggregation used will vary as we increment through the
1359 * aggregation variables that we have been passed.) Finally, we
1360 * decrement nrecs to prevent this record from being used with any
1363 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1364 assert(aggsdata != NULL);
1365 assert(naggvars > 0);
1368 return (dt_set_errno(dtp, EDT_DMISMATCH));
1370 curagg = naggvars > 1 ? 1 : 0;
1371 aggdata = aggsdata[0];
1372 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1376 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1377 const dt_pfconv_t *pfc = pfd->pfd_conv;
1378 int width = pfd->pfd_width;
1379 int prec = pfd->pfd_prec;
1382 char *f = format + 1; /* skip initial '%' */
1383 const dtrace_recdesc_t *rec;
1389 if (pfd->pfd_preflen != 0) {
1390 char *tmp = alloca(pfd->pfd_preflen + 1);
1392 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1393 tmp[pfd->pfd_preflen] = '\0';
1395 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1398 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1400 * For printa(), we flush the buffer after each
1401 * prefix, setting the flags to indicate that
1402 * this is part of the printa() format string.
1404 flags = DTRACE_BUFDATA_AGGFORMAT;
1406 if (pfc == NULL && i == pfv->pfv_argc - 1)
1407 flags |= DTRACE_BUFDATA_AGGLAST;
1409 if (dt_buffered_flush(dtp, NULL, NULL,
1410 aggdata, flags) < 0)
1416 if (pfv->pfv_argc == 1)
1417 return (nrecs != 0);
1422 * If the conversion is %%, just invoke the print callback
1423 * with no data record and continue; it consumes no record.
1425 if (pfc->pfc_print == &pfprint_pct) {
1426 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1428 return (-1); /* errno is set for us */
1431 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1432 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1434 return (-1); /* errno is set for us */
1435 pfd->pfd_dynwidth = width;
1437 pfd->pfd_dynwidth = 0;
1440 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1441 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1442 return (-1); /* errno is set for us */
1444 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1446 * This should be impossible -- the compiler shouldn't
1447 * create a DT_PFCONV_AGG conversion without an
1448 * aggregation present. Still, we'd rather fail
1449 * gracefully than blow up...
1451 if (aggsdata == NULL)
1452 return (dt_set_errno(dtp, EDT_DMISMATCH));
1454 aggdata = aggsdata[curagg];
1455 agg = aggdata->dtada_desc;
1458 * We increment the current aggregation variable, but
1459 * not beyond the number of aggregation variables that
1460 * we're printing. This has the (desired) effect that
1461 * DT_PFCONV_AGG conversions beyond the number of
1462 * aggregation variables (re-)convert the aggregation
1463 * value of the last aggregation variable.
1465 if (curagg < naggvars - 1)
1468 rec = &agg->dtagd_rec[aggrec];
1469 addr = aggdata->dtada_data + rec->dtrd_offset;
1470 limit = addr + aggdata->dtada_size;
1471 normal = aggdata->dtada_normal;
1472 flags = DTRACE_BUFDATA_AGGVAL;
1475 return (dt_set_errno(dtp, EDT_DMISMATCH));
1477 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1479 * When printing aggregation keys, we always
1480 * set the aggdata to be the representative
1481 * (zeroth) aggregation. The aggdata isn't
1482 * actually used here in this case, but it is
1483 * passed to the buffer handler and must
1484 * therefore still be correct.
1486 aggdata = aggsdata[0];
1487 flags = DTRACE_BUFDATA_AGGKEY;
1492 addr = (caddr_t)buf + rec->dtrd_offset;
1497 size = rec->dtrd_size;
1499 if (addr + size > limit) {
1500 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1501 (void *)addr, rec->dtrd_size, (void *)lim);
1502 return (dt_set_errno(dtp, EDT_DOFFSET));
1505 if (rec->dtrd_alignment != 0 &&
1506 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1507 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1508 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1509 return (dt_set_errno(dtp, EDT_DALIGN));
1512 switch (rec->dtrd_action) {
1514 func = pfprint_average;
1516 case DTRACEAGG_STDDEV:
1517 func = pfprint_stddev;
1519 case DTRACEAGG_QUANTIZE:
1520 func = pfprint_quantize;
1522 case DTRACEAGG_LQUANTIZE:
1523 func = pfprint_lquantize;
1525 case DTRACEAGG_LLQUANTIZE:
1526 func = pfprint_llquantize;
1531 case DTRACEACT_UMOD:
1532 func = pfprint_umod;
1535 func = pfc->pfc_print;
1539 if (pfd->pfd_flags & DT_PFCONV_ALT)
1541 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1543 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1545 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1547 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1549 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1553 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1554 * don't add the width to the format string. See the block
1555 * comment in pfprint_stack() for a description of the
1556 * behavior in this case.
1558 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1562 f += snprintf(f, sizeof (format), "%d", ABS(width));
1565 f += snprintf(f, sizeof (format), ".%d", prec);
1567 (void) strcpy(f, pfd->pfd_fmt);
1570 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1571 return (-1); /* errno is set for us */
1573 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1575 * For printa(), we flush the buffer after each tuple
1576 * element, inidicating that this is the last record
1579 if (i == pfv->pfv_argc - 1)
1580 flags |= DTRACE_BUFDATA_AGGLAST;
1582 if (dt_buffered_flush(dtp, NULL,
1583 rec, aggdata, flags) < 0)
1588 return ((int)(recp - recs));
1592 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1593 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1595 dtrace_optval_t size;
1598 rval = dtrace_getopt(dtp, "strsize", &size);
1600 assert(dtp->dt_sprintf_buflen == 0);
1602 if (dtp->dt_sprintf_buf != NULL)
1603 free(dtp->dt_sprintf_buf);
1605 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1606 return (dt_set_errno(dtp, EDT_NOMEM));
1608 bzero(dtp->dt_sprintf_buf, size);
1609 dtp->dt_sprintf_buflen = size;
1610 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1612 dtp->dt_sprintf_buflen = 0;
1615 free(dtp->dt_sprintf_buf);
1622 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1623 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1624 uint_t nrecs, const void *buf, size_t len)
1626 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1632 * Before we execute the specified command, flush fp to assure that
1633 * any prior dt_printf()'s appear before the output of the command
1638 if (system(dtp->dt_sprintf_buf) == -1)
1639 return (dt_set_errno(dtp, errno));
1645 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1646 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1647 uint_t nrecs, const void *buf, size_t len)
1649 char selfbuf[40], restorebuf[40], *filename;
1652 dt_pfargv_t *pfv = fmtdata;
1653 dt_pfargd_t *pfd = pfv->pfv_argv;
1655 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1657 if (rval == -1 || fp == NULL)
1661 if (pfd->pfd_preflen != 0 &&
1662 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1664 * The only way to have the format string set to the value
1665 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1666 * denoting that we should restore the old stdout.
1668 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1670 if (dtp->dt_stdout_fd == -1) {
1672 * We could complain here by generating an error,
1673 * but it seems like overkill: it seems that calling
1674 * freopen() to restore stdout when freopen() has
1675 * never before been called should just be a no-op,
1676 * so we just return in this case.
1681 (void) snprintf(restorebuf, sizeof (restorebuf),
1682 "/dev/fd/%d", dtp->dt_stdout_fd);
1683 filename = restorebuf;
1685 filename = dtp->dt_sprintf_buf;
1689 * freopen(3C) will always close the specified stream and underlying
1690 * file descriptor -- even if the specified file can't be opened.
1691 * Even for the semantic cesspool that is standard I/O, this is
1692 * surprisingly brain-dead behavior: it means that any failure to
1693 * open the specified file destroys the specified stream in the
1694 * process -- which is particularly relevant when the specified stream
1695 * happens (or rather, happened) to be stdout. This could be resolved
1696 * were there an "fdreopen()" equivalent of freopen() that allowed one
1697 * to pass a file descriptor instead of the name of a file, but there
1698 * is no such thing. However, we can effect this ourselves by first
1699 * fopen()'ing the desired file, and then (assuming that that works),
1700 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1701 * file descriptor for the fopen()'d file. This way, if the fopen()
1702 * fails, we can fail the operation without destroying stdout.
1704 if ((nfp = fopen(filename, "aF")) == NULL) {
1705 char *msg = strerror(errno);
1709 len += strlen(msg) + strlen(filename);
1710 faultstr = alloca(len);
1712 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1713 filename, strerror(errno));
1715 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1721 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1723 if (dtp->dt_stdout_fd == -1) {
1725 * If this is the first time that we're calling freopen(),
1726 * we're going to stash away the file descriptor for stdout.
1727 * We don't expect the dup(2) to fail, so if it does we must
1730 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1732 return (dt_set_errno(dtp, errno));
1736 if (freopen(selfbuf, "aF", fp) == NULL) {
1738 return (dt_set_errno(dtp, errno));
1744 * The 'standard output' (which is not necessarily stdout)
1745 * treatment on FreeBSD is implemented differently than on
1746 * Solaris because FreeBSD's freopen() will attempt to re-use
1747 * the current file descriptor, causing the previous file to
1748 * be closed and thereby preventing it from be re-activated
1751 * For FreeBSD we use the concept of setting an output file
1752 * pointer in the DTrace handle if a dtrace_freopen() has
1753 * enabled another output file and we leave the caller's
1754 * file pointer untouched. If it was actually stdout, then
1755 * stdout remains open. If it was another file, then that
1756 * file remains open. While a dtrace_freopen() has activated
1757 * another file, we keep a pointer to that which we use in
1758 * the output functions by preference and only use the caller's
1759 * file pointer if no dtrace_freopen() call has been made.
1761 * The check to see if we're re-activating the caller's
1762 * output file is much the same as on Solaris.
1764 if (pfd->pfd_preflen != 0 &&
1765 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1767 * The only way to have the format string set to the value
1768 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1769 * denoting that we should restore the old stdout.
1771 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1773 if (dtp->dt_freopen_fp == NULL) {
1775 * We could complain here by generating an error,
1776 * but it seems like overkill: it seems that calling
1777 * freopen() to restore stdout when freopen() has
1778 * never before been called should just be a no-op,
1779 * so we just return in this case.
1785 * At this point, to re-active the original output file,
1786 * on FreeBSD we only code the current file that this
1787 * function opened previously.
1789 (void) fclose(dtp->dt_freopen_fp);
1790 dtp->dt_freopen_fp = NULL;
1795 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1796 char *msg = strerror(errno);
1800 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1801 faultstr = alloca(len);
1803 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1804 dtp->dt_sprintf_buf, strerror(errno));
1806 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1812 if (dtp->dt_freopen_fp != NULL)
1813 (void) fclose(dtp->dt_freopen_fp);
1815 /* Remember that the output has been redirected to the new file. */
1816 dtp->dt_freopen_fp = nfp;
1824 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1825 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1826 uint_t nrecs, const void *buf, size_t len)
1828 return (dt_printf_format(dtp, fp, fmtdata,
1829 recp, nrecs, buf, len, NULL, 0));
1833 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1835 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1840 return (NULL); /* errno has been set for us */
1842 pfd = pfv->pfv_argv;
1844 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1845 const dt_pfconv_t *pfc = pfd->pfd_conv;
1851 * If the output format is not %s then we assume that we have
1852 * been given a correctly-sized format string, so we copy the
1853 * true format name including the size modifier. If the output
1854 * format is %s, then either the input format is %s as well or
1855 * it is one of our custom formats (e.g. pfprint_addr), so we
1856 * must set pfd_fmt to be the output format conversion "s".
1858 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1859 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1861 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1868 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1870 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1873 return (NULL); /* errno has been set for us */
1875 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1882 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1884 dt_pfargv_t *pfv = fmtdata;
1885 dt_pfargd_t *pfd = pfv->pfv_argv;
1888 * An upper bound on the string length is the length of the original
1889 * format string, plus three times the number of conversions (each
1890 * conversion could add up an additional "ll" and/or pfd_width digit
1891 * in the case of converting %? to %16) plus one for a terminating \0.
1893 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1894 char *format = alloca(formatlen);
1898 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1899 const dt_pfconv_t *pfc = pfd->pfd_conv;
1901 int width = pfd->pfd_width;
1902 int prec = pfd->pfd_prec;
1904 if (pfd->pfd_preflen != 0) {
1905 for (j = 0; j < pfd->pfd_preflen; j++)
1906 *f++ = pfd->pfd_prefix[j];
1914 if (pfd->pfd_flags & DT_PFCONV_ALT)
1916 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1918 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1920 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1922 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1924 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1928 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1930 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1932 if (pfd->pfd_flags & DT_PFCONV_AGG)
1936 f += snprintf(f, sizeof (format), "%d", width);
1939 f += snprintf(f, sizeof (format), ".%d", prec);
1942 * If the output format is %s, then either %s is the underlying
1943 * conversion or the conversion is one of our customized ones,
1944 * e.g. pfprint_addr. In these cases, put the original string
1945 * name of the conversion (pfc_name) into the pickled format
1946 * string rather than the derived conversion (pfd_fmt).
1948 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1949 str = pfc->pfc_name;
1953 for (j = 0; str[j] != '\0'; j++)
1957 *f = '\0'; /* insert nul byte; do not count in return value */
1959 assert(f < format + formatlen);
1960 (void) strncpy(s, format, len);
1962 return ((size_t)(f - format));
1966 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1968 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1969 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1970 uint_t nrecs = agg->dtagd_nrecs;
1971 dt_pfwalk_t *pfw = arg;
1972 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1975 if (dt_printf_getint(dtp, recp++, nrecs--,
1976 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1977 return (0); /* no aggregation id or id does not match */
1979 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1980 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1981 return (pfw->pfw_err = dtp->dt_errno);
1984 * Cast away the const to set the bit indicating that this aggregation
1987 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1993 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1995 const dtrace_aggdata_t *aggdata = aggsdata[0];
1996 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1997 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1998 uint_t nrecs = agg->dtagd_nrecs - 1;
1999 dt_pfwalk_t *pfw = arg;
2000 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
2003 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
2004 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
2005 aggsdata, naggvars) == -1)
2006 return (pfw->pfw_err = dtp->dt_errno);
2009 * For each aggregation, indicate that it has been printed, casting
2010 * away the const as necessary.
2012 for (i = 1; i < naggvars; i++) {
2013 agg = aggsdata[i]->dtada_desc;
2014 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2021 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2022 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2023 uint_t nrecs, const void *buf, size_t len)
2026 int i, naggvars = 0;
2027 dtrace_aggvarid_t *aggvars;
2029 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2032 * This might be a printa() with multiple aggregation variables. We
2033 * need to scan forward through the records until we find a record from
2034 * a different statement.
2036 for (i = 0; i < nrecs; i++) {
2037 const dtrace_recdesc_t *nrec = &recs[i];
2039 if (nrec->dtrd_uarg != recs->dtrd_uarg)
2042 if (nrec->dtrd_action != recs->dtrd_action)
2043 return (dt_set_errno(dtp, EDT_BADAGG));
2045 aggvars[naggvars++] =
2046 /* LINTED - alignment */
2047 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2051 return (dt_set_errno(dtp, EDT_BADAGG));
2053 pfw.pfw_argv = fmtdata;
2057 if (naggvars == 1) {
2058 pfw.pfw_aid = aggvars[0];
2060 if (dtrace_aggregate_walk_sorted(dtp,
2061 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2062 return (-1); /* errno is set for us */
2064 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2065 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2066 return (-1); /* errno is set for us */