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 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/sysmacros.h>
32 #define ABS(a) ((a) < 0 ? -(a) : (a))
45 #include <dt_printf.h>
46 #include <dt_string.h>
51 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
53 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
58 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
60 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
61 dt_node_is_symaddr(dnp));
66 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
68 dtrace_hdl_t *dtp = pfv->pfv_dtp;
69 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
71 if (dt_node_is_usymaddr(dnp))
74 if (idp == NULL || idp->di_id == 0)
77 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
82 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
84 return (dt_node_is_stack(dnp));
89 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
91 return (dt_node_is_integer(dnp) &&
92 dt_node_type_size(dnp) == sizeof (uint64_t));
97 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
105 if (dt_node_is_string(dnp))
109 base = ctf_type_resolve(ctfp, dnp->dn_type);
110 kind = ctf_type_kind(ctfp, base);
112 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
113 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
114 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
119 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
121 ctf_file_t *ctfp = dnp->dn_ctfp;
122 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
123 uint_t kind = ctf_type_kind(ctfp, base);
128 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
129 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
130 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
131 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
136 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
138 return (dt_node_is_integer(dnp) &&
139 dt_node_type_size(dnp) <= sizeof (int));
144 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
146 return (dt_node_is_float(dnp));
151 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
153 return (dt_node_is_integer(dnp));
158 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
160 if (dnp->dn_flags & DT_NF_SIGNED)
161 pfd->pfd_flags |= DT_PFCONV_SIGNED;
163 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
165 return (dt_node_is_integer(dnp));
170 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
172 ctf_file_t *ctfp = dnp->dn_ctfp;
173 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
174 char n[DT_TYPE_NAMELEN];
176 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
177 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
178 strcmp(n, "unsigned short") == 0));
183 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
185 ctf_file_t *ctfp = dnp->dn_ctfp;
186 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
187 char n[DT_TYPE_NAMELEN];
189 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
190 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
191 strcmp(n, "unsigned long") == 0));
196 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
198 ctf_file_t *ctfp = dnp->dn_ctfp;
199 ctf_id_t type = dnp->dn_type;
200 char n[DT_TYPE_NAMELEN];
202 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
203 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
204 strcmp(n, "signed long long") == 0 ||
205 strcmp(n, "unsigned long long") == 0))
209 * If the type used for %llx or %llX is not an [unsigned] long long, we
210 * also permit it to be a [u]int64_t or any typedef thereof. We know
211 * that these typedefs are guaranteed to work with %ll[xX] in either
212 * compilation environment even though they alias to "long" in LP64.
214 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
215 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
216 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
219 type = ctf_type_reference(ctfp, type);
227 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
229 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
230 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
235 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
236 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
238 int64_t normal = (int64_t)unormal;
239 int32_t n = (int32_t)normal;
242 case sizeof (int8_t):
243 return (dt_printf(dtp, fp, format,
244 (int32_t)*((int8_t *)addr) / n));
245 case sizeof (int16_t):
246 return (dt_printf(dtp, fp, format,
247 (int32_t)*((int16_t *)addr) / n));
248 case sizeof (int32_t):
249 return (dt_printf(dtp, fp, format,
250 *((int32_t *)addr) / n));
251 case sizeof (int64_t):
252 return (dt_printf(dtp, fp, format,
253 *((int64_t *)addr) / normal));
255 return (dt_set_errno(dtp, EDT_DMISMATCH));
261 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
262 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
264 uint32_t n = (uint32_t)normal;
267 case sizeof (uint8_t):
268 return (dt_printf(dtp, fp, format,
269 (uint32_t)*((uint8_t *)addr) / n));
270 case sizeof (uint16_t):
271 return (dt_printf(dtp, fp, format,
272 (uint32_t)*((uint16_t *)addr) / n));
273 case sizeof (uint32_t):
274 return (dt_printf(dtp, fp, format,
275 *((uint32_t *)addr) / n));
276 case sizeof (uint64_t):
277 return (dt_printf(dtp, fp, format,
278 *((uint64_t *)addr) / normal));
280 return (dt_set_errno(dtp, EDT_DMISMATCH));
285 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
286 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
288 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
289 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
291 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
296 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
297 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
299 double n = (double)normal;
300 long double ldn = (long double)normal;
304 return (dt_printf(dtp, fp, format,
305 (double)*((float *)addr) / n));
306 case sizeof (double):
307 return (dt_printf(dtp, fp, format,
308 *((double *)addr) / n));
309 #if !defined(__arm__) && !defined(__powerpc__)
310 case sizeof (long double):
311 return (dt_printf(dtp, fp, format,
312 *((long double *)addr) / ldn));
315 return (dt_set_errno(dtp, EDT_DMISMATCH));
321 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
322 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
329 case sizeof (uint32_t):
330 val = *((uint32_t *)addr);
332 case sizeof (uint64_t):
333 val = *((uint64_t *)addr);
336 return (dt_set_errno(dtp, EDT_DMISMATCH));
342 } while ((len = dtrace_addr2str(dtp, val, s, n)) >= n);
344 return (dt_printf(dtp, fp, format, s));
349 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
350 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
352 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
357 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
358 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
360 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
365 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
366 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
370 uint64_t val, pid = 0;
372 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
375 case sizeof (uint32_t):
376 val = (u_longlong_t)*((uint32_t *)addr);
378 case sizeof (uint64_t):
379 val = (u_longlong_t)*((uint64_t *)addr);
381 case sizeof (uint64_t) * 2:
382 pid = ((uint64_t *)(uintptr_t)addr)[0];
383 val = ((uint64_t *)(uintptr_t)addr)[1];
386 return (dt_set_errno(dtp, EDT_DMISMATCH));
389 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
395 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) >= n);
397 return (dt_printf(dtp, fp, format, s));
402 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
403 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
406 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
407 const dtrace_recdesc_t *rec = pfd->pfd_rec;
408 caddr_t addr = (caddr_t)vaddr;
412 * We have stashed the value of the STACKINDENT option, and we will
413 * now override it for the purposes of formatting the stack. If the
414 * field has been specified as left-aligned (i.e. (%-#), we set the
415 * indentation to be the width. This is a slightly odd semantic, but
416 * it's useful functionality -- and it's slightly odd to begin with to
417 * be using a single format specifier to be formatting multiple lines
420 if (pfd->pfd_dynwidth < 0) {
421 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
422 width = -pfd->pfd_dynwidth;
423 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
424 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
429 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
431 switch (rec->dtrd_action) {
432 case DTRACEACT_USTACK:
433 case DTRACEACT_JSTACK:
434 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
437 case DTRACEACT_STACK:
438 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
439 rec->dtrd_size / rec->dtrd_arg);
446 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
453 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
454 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
456 char src[32], buf[32], *dst = buf;
457 hrtime_t time = *((uint64_t *)addr);
458 time_t sec = (time_t)(time / NANOSEC);
462 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
463 * Below, we turn this into the canonical adb/mdb /[yY] format,
464 * "1973 Dec 3 17:20:00".
467 (void) ctime_r(&sec, src, sizeof (src));
469 (void) ctime_r(&sec, src);
473 * Place the 4-digit year at the head of the string...
475 for (i = 20; i < 24; i++)
479 * ...and follow it with the remainder (month, day, hh:mm:ss).
481 for (i = 3; i < 19; i++)
485 return (dt_printf(dtp, fp, format, buf));
489 * This prints the time in RFC 822 standard form. This is useful for emitting
490 * notions of time that are consumed by standard tools (e.g., as part of an
495 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
496 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
498 hrtime_t time = *((uint64_t *)addr);
499 time_t sec = (time_t)(time / NANOSEC);
503 (void) localtime_r(&sec, &tm);
504 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
505 return (dt_printf(dtp, fp, format, buf));
510 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
511 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
513 char *s = alloca(size + 1);
515 bcopy(addr, s, size);
517 return (dt_printf(dtp, fp, format, s));
522 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
523 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
525 wchar_t *ws = alloca(size + sizeof (wchar_t));
527 bcopy(addr, ws, size);
528 ws[size / sizeof (wchar_t)] = L'\0';
529 return (dt_printf(dtp, fp, format, ws));
534 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
535 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
540 if ((s = strchr2esc(addr, size)) == NULL)
541 return (dt_set_errno(dtp, EDT_NOMEM));
543 n = dt_printf(dtp, fp, format, s);
549 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
550 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
555 case sizeof (int8_t):
558 case sizeof (int16_t):
559 c = *(int16_t *)addr;
561 case sizeof (int32_t):
562 c = *(int32_t *)addr;
565 return (dt_set_errno(dtp, EDT_DMISMATCH));
568 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
573 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
574 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
576 return (dt_printf(dtp, fp, "%%"));
579 static const char pfproto_xint[] = "char, short, int, long, or long long";
580 static const char pfproto_csi[] = "char, short, or int";
581 static const char pfproto_fp[] = "float, double, or long double";
582 static const char pfproto_addr[] = "pointer or integer";
583 static const char pfproto_uaddr[] =
584 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
585 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
586 static const char pfproto_wstr[] = "wchar_t []";
589 * Printf format conversion dictionary. This table should match the set of
590 * conversions offered by printf(3C), as well as some additional extensions.
591 * The second parameter is an ASCII string which is either an actual type
592 * name we should look up (if pfcheck_type is specified), or just a descriptive
593 * string of the types expected for use in error messages.
595 static const dt_pfconv_t _dtrace_conversions[] = {
596 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
597 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
598 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
599 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
600 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
601 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
602 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
603 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
604 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
605 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
606 { "hd", "d", "short", pfcheck_type, pfprint_sint },
607 { "hi", "i", "short", pfcheck_type, pfprint_sint },
608 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
609 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
610 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
611 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
612 { "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
613 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
614 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
615 { "ld", "d", "long", pfcheck_type, pfprint_sint },
616 { "li", "i", "long", pfcheck_type, pfprint_sint },
617 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
618 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
619 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
620 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
621 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
622 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
623 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
624 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
625 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
626 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
627 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
628 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
629 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
630 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
631 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
632 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
633 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
634 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
635 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
636 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
637 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
638 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
639 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
640 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
641 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
642 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
643 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
644 { "%", "%", "void", pfcheck_type, pfprint_pct },
645 { NULL, NULL, NULL, NULL, NULL }
649 dt_pfdict_create(dtrace_hdl_t *dtp)
651 uint_t n = _dtrace_strbuckets;
652 const dt_pfconv_t *pfd;
655 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
656 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
658 return (dt_set_errno(dtp, EDT_NOMEM));
661 dtp->dt_pfdict = pdi;
662 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
663 pdi->pdi_nbuckets = n;
665 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
666 dtrace_typeinfo_t dtt;
670 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
671 dt_pfdict_destroy(dtp);
672 return (dt_set_errno(dtp, EDT_NOMEM));
675 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
676 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
677 pfc->pfc_next = pdi->pdi_buckets[h];
678 pdi->pdi_buckets[h] = pfc;
681 dtt.dtt_type = CTF_ERR;
684 * The "D" container or its parent must contain a definition of
685 * any type referenced by a printf conversion. If none can be
686 * found, we fail to initialize the printf dictionary.
688 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
689 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
690 dt_pfdict_destroy(dtp);
691 return (dt_set_errno(dtp, EDT_NOCONV));
694 pfc->pfc_dctfp = dtt.dtt_ctfp;
695 pfc->pfc_dtype = dtt.dtt_type;
698 * The "C" container may contain an alternate definition of an
699 * explicit conversion type. If it does, use it; otherwise
700 * just set pfc_ctype to pfc_dtype so it is always valid.
702 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
703 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
704 pfc->pfc_cctfp = dtt.dtt_ctfp;
705 pfc->pfc_ctype = dtt.dtt_type;
707 pfc->pfc_cctfp = pfc->pfc_dctfp;
708 pfc->pfc_ctype = pfc->pfc_dtype;
711 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
712 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
713 dt_pfdict_destroy(dtp);
714 return (dt_set_errno(dtp, EDT_BADCONV));
717 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
724 dt_pfdict_destroy(dtrace_hdl_t *dtp)
726 dt_pfdict_t *pdi = dtp->dt_pfdict;
727 dt_pfconv_t *pfc, *nfc;
733 for (i = 0; i < pdi->pdi_nbuckets; i++) {
734 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
740 free(pdi->pdi_buckets);
742 dtp->dt_pfdict = NULL;
745 static const dt_pfconv_t *
746 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
748 dt_pfdict_t *pdi = dtp->dt_pfdict;
749 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
750 const dt_pfconv_t *pfc;
752 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
753 if (strcmp(pfc->pfc_name, name) == 0)
761 dt_printf_error(dtrace_hdl_t *dtp, int err)
764 longjmp(yypcb->pcb_jmpbuf, err);
766 (void) dt_set_errno(dtp, err);
771 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
773 dt_pfargd_t *pfd, *nfd = NULL;
778 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
779 (format = strdup(s)) == NULL) {
781 return (dt_printf_error(dtp, EDT_NOMEM));
784 pfv->pfv_format = format;
785 pfv->pfv_argv = NULL;
790 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
799 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
800 dt_printf_destroy(pfv);
801 return (dt_printf_error(dtp, EDT_NOMEM));
804 if (pfv->pfv_argv != NULL)
809 bzero(pfd, sizeof (dt_pfargd_t));
814 pfd->pfd_preflen = (size_t)(p - q);
820 case '0': case '1': case '2': case '3': case '4':
821 case '5': case '6': case '7': case '8': case '9':
822 if (dot == 0 && digits == 0 && c == '0') {
823 pfd->pfd_flags |= DT_PFCONV_ZPAD;
824 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
828 for (n = 0; isdigit(c); c = *++p)
829 n = n * 10 + c - '0';
841 pfd->pfd_flags |= DT_PFCONV_ALT;
845 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
847 if (pfd->pfd_flags & n) {
848 yywarn("format conversion #%u has more than "
849 "one '*' specified for the output %s\n",
850 pfv->pfv_argc, n ? "precision" : "width");
852 dt_printf_destroy(pfv);
853 return (dt_printf_error(dtp, EDT_COMPILER));
860 pfd->pfd_flags |= DT_PFCONV_SPOS;
864 pfd->pfd_flags |= DT_PFCONV_LEFT;
865 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
870 yywarn("format conversion #%u has more than "
871 "one '.' specified\n", pfv->pfv_argc);
873 dt_printf_destroy(pfv);
874 return (dt_printf_error(dtp, EDT_COMPILER));
880 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
887 pfd->pfd_flags |= DT_PFCONV_AGG;
891 pfd->pfd_flags |= DT_PFCONV_GROUP;
895 pfd->pfd_flags |= DT_PFCONV_SPACE;
899 yywarn("format conversion #%u uses unsupported "
900 "positional format (%%n$)\n", pfv->pfv_argc);
902 dt_printf_destroy(pfv);
903 return (dt_printf_error(dtp, EDT_COMPILER));
907 goto default_lbl; /* if %% then use "%" conv */
909 yywarn("format conversion #%u cannot be combined "
910 "with other format flags: %%%%\n", pfv->pfv_argc);
912 dt_printf_destroy(pfv);
913 return (dt_printf_error(dtp, EDT_COMPILER));
916 yywarn("format conversion #%u name expected before "
917 "end of format string\n", pfv->pfv_argc);
919 dt_printf_destroy(pfv);
920 return (dt_printf_error(dtp, EDT_COMPILER));
926 if (namelen < sizeof (name) - 2)
933 name[namelen] = '\0';
936 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
938 if (pfd->pfd_conv == NULL) {
939 yywarn("format conversion #%u is undefined: %%%s\n",
940 pfv->pfv_argc, name);
941 dt_printf_destroy(pfv);
942 return (dt_printf_error(dtp, EDT_COMPILER));
946 if (*q != '\0' || *format == '\0') {
947 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
948 dt_printf_destroy(pfv);
949 return (dt_printf_error(dtp, EDT_NOMEM));
952 if (pfv->pfv_argv != NULL)
957 bzero(pfd, sizeof (dt_pfargd_t));
961 pfd->pfd_preflen = strlen(q);
968 dt_printf_destroy(dt_pfargv_t *pfv)
970 dt_pfargd_t *pfd, *nfd;
972 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
977 free(pfv->pfv_format);
982 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
983 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
985 dt_pfargd_t *pfd = pfv->pfv_argv;
986 const char *func = idp->di_name;
988 char n[DT_TYPE_NAMELEN];
989 dtrace_typeinfo_t dtt;
994 if (pfv->pfv_format[0] == '\0') {
995 xyerror(D_PRINTF_FMT_EMPTY,
996 "%s( ) format string is empty\n", func);
999 pfv->pfv_flags = flags;
1002 * We fake up a parse node representing the type that can be used with
1003 * an aggregation result conversion, which -- for all but count() --
1004 * is a signed quantity.
1006 if (kind != DTRACEAGG_COUNT)
1007 aggtype = "int64_t";
1009 aggtype = "uint64_t";
1011 if (dt_type_lookup(aggtype, &dtt) != 0)
1012 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1014 bzero(&aggnode, sizeof (aggnode));
1015 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
1017 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1018 const dt_pfconv_t *pfc = pfd->pfd_conv;
1019 const char *dyns[2];
1026 continue; /* no checking if argd is just a prefix */
1028 if (pfc->pfc_print == &pfprint_pct) {
1029 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1033 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1034 dyns[dync++] = ".*";
1035 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1038 for (; dync != 0; dync--) {
1040 xyerror(D_PRINTF_DYN_PROTO,
1041 "%s( ) prototype mismatch: conversion "
1042 "#%d (%%%s) is missing a corresponding "
1043 "\"%s\" argument\n", func, i + 1,
1044 pfc->pfc_name, dyns[dync - 1]);
1047 if (dt_node_is_integer(dnp) == 0) {
1048 xyerror(D_PRINTF_DYN_TYPE,
1049 "%s( ) argument #%d is incompatible "
1050 "with conversion #%d prototype:\n"
1051 "\tconversion: %% %s %s\n"
1052 "\t prototype: int\n\t argument: %s\n",
1053 func, j + foff + 1, i + 1,
1054 dyns[dync - 1], pfc->pfc_name,
1055 dt_node_type_name(dnp, n, sizeof (n)));
1063 * If this conversion is consuming the aggregation data, set
1064 * the value node pointer (vnp) to a fake node based on the
1065 * aggregating function result type. Otherwise assign vnp to
1066 * the next parse node in the argument list, if there is one.
1068 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1069 if (!(flags & DT_PRINTF_AGGREGATION)) {
1070 xyerror(D_PRINTF_AGG_CONV,
1071 "%%@ conversion requires an aggregation"
1072 " and is not for use with %s( )\n", func);
1074 (void) strlcpy(vname, "aggregating action",
1077 } else if (dnp == NULL) {
1078 xyerror(D_PRINTF_ARG_PROTO,
1079 "%s( ) prototype mismatch: conversion #%d (%%"
1080 "%s) is missing a corresponding value argument\n",
1081 func, i + 1, pfc->pfc_name);
1083 (void) snprintf(vname, sizeof (vname),
1084 "argument #%d", j + foff + 1);
1091 * Fill in the proposed final format string by prepending any
1092 * size-related prefixes to the pfconv's format string. The
1093 * pfc_check() function below may optionally modify the format
1094 * as part of validating the type of the input argument.
1096 if (pfc->pfc_print == &pfprint_sint ||
1097 pfc->pfc_print == &pfprint_uint ||
1098 pfc->pfc_print == &pfprint_dint) {
1099 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1100 (void) strcpy(pfd->pfd_fmt, "ll");
1101 } else if (pfc->pfc_print == &pfprint_fp) {
1102 if (dt_node_type_size(vnp) == sizeof (long double))
1103 (void) strcpy(pfd->pfd_fmt, "L");
1106 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1109 * Validate the format conversion against the value node type.
1110 * If the conversion is good, create the descriptor format
1111 * string by concatenating together any required printf(3C)
1112 * size prefixes with the conversion's native format string.
1114 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1115 xyerror(D_PRINTF_ARG_TYPE,
1116 "%s( ) %s is incompatible with "
1117 "conversion #%d prototype:\n\tconversion: %%%s\n"
1118 "\t prototype: %s\n\t argument: %s\n", func,
1119 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1120 dt_node_type_name(vnp, n, sizeof (n)));
1124 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1125 xyerror(D_PRINTF_ARG_EXTRA,
1126 "%s( ) prototype mismatch: only %d arguments "
1127 "required by this format string\n", func, j);
1132 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1134 dt_ident_t *lid, *rid;
1135 dt_node_t *lproto, *rproto;
1136 int largc, rargc, argn;
1137 char n1[DT_TYPE_NAMELEN];
1138 char n2[DT_TYPE_NAMELEN];
1140 assert(lhs->dn_kind == DT_NODE_AGG);
1141 assert(rhs->dn_kind == DT_NODE_AGG);
1143 lid = lhs->dn_ident;
1144 rid = rhs->dn_ident;
1146 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1147 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1150 * First, get an argument count on each side. These must match.
1152 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1155 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1158 if (largc != rargc) {
1159 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1160 "matching key signatures: @%s has %d key%s, @%s has %d "
1161 "key%s", lid->di_name, rid->di_name,
1162 lid->di_name, largc, largc == 1 ? "" : "s",
1163 rid->di_name, rargc, rargc == 1 ? "" : "s");
1167 * Now iterate over the keys to verify that each type matches.
1169 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1170 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1172 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1173 rproto = rproto->dn_list) {
1174 assert(rproto != NULL);
1176 if (dt_node_is_argcompat(lproto, rproto))
1179 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1180 "incompatible with @%s:\n%9s key #%d: %s\n"
1181 "%9s key #%d: %s\n",
1182 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1183 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1184 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1189 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1190 uint_t nrecs, const void *buf, size_t len, int *ip)
1195 return (dt_set_errno(dtp, EDT_DMISMATCH));
1197 addr = (uintptr_t)buf + recp->dtrd_offset;
1199 if (addr + sizeof (int) > (uintptr_t)buf + len)
1200 return (dt_set_errno(dtp, EDT_DOFFSET));
1202 if (addr & (recp->dtrd_alignment - 1))
1203 return (dt_set_errno(dtp, EDT_DALIGN));
1205 switch (recp->dtrd_size) {
1206 case sizeof (int8_t):
1207 *ip = (int)*((int8_t *)addr);
1209 case sizeof (int16_t):
1210 *ip = (int)*((int16_t *)addr);
1212 case sizeof (int32_t):
1213 *ip = (int)*((int32_t *)addr);
1215 case sizeof (int64_t):
1216 *ip = (int)*((int64_t *)addr);
1219 return (dt_set_errno(dtp, EDT_DMISMATCH));
1227 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1228 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1230 const uint64_t *data = addr;
1232 if (size != sizeof (uint64_t) * 2)
1233 return (dt_set_errno(dtp, EDT_DMISMATCH));
1235 return (dt_printf(dtp, fp, format,
1236 data[0] ? data[1] / normal / data[0] : 0));
1241 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1242 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1244 return (dt_print_quantize(dtp, fp, addr, size, normal));
1249 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1250 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1252 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1256 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1257 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1258 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1260 dt_pfargd_t *pfd = pfv->pfv_argv;
1261 const dtrace_recdesc_t *recp = recs;
1262 const dtrace_aggdata_t *aggdata;
1263 dtrace_aggdesc_t *agg;
1264 caddr_t lim = (caddr_t)buf + len, limit;
1265 char format[64] = "%";
1266 int i, aggrec, curagg = -1;
1270 * If we are formatting an aggregation, set 'aggrec' to the index of
1271 * the final record description (the aggregation result) so we can use
1272 * this record index with any conversion where DT_PFCONV_AGG is set.
1273 * (The actual aggregation used will vary as we increment through the
1274 * aggregation variables that we have been passed.) Finally, we
1275 * decrement nrecs to prevent this record from being used with any
1278 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1279 assert(aggsdata != NULL);
1280 assert(naggvars > 0);
1283 return (dt_set_errno(dtp, EDT_DMISMATCH));
1285 curagg = naggvars > 1 ? 1 : 0;
1286 aggdata = aggsdata[0];
1287 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1291 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1292 const dt_pfconv_t *pfc = pfd->pfd_conv;
1293 int width = pfd->pfd_width;
1294 int prec = pfd->pfd_prec;
1297 char *f = format + 1; /* skip initial '%' */
1298 const dtrace_recdesc_t *rec;
1304 if (pfd->pfd_preflen != 0) {
1305 char *tmp = alloca(pfd->pfd_preflen + 1);
1307 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1308 tmp[pfd->pfd_preflen] = '\0';
1310 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1313 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1315 * For printa(), we flush the buffer after each
1316 * prefix, setting the flags to indicate that
1317 * this is part of the printa() format string.
1319 flags = DTRACE_BUFDATA_AGGFORMAT;
1321 if (pfc == NULL && i == pfv->pfv_argc - 1)
1322 flags |= DTRACE_BUFDATA_AGGLAST;
1324 if (dt_buffered_flush(dtp, NULL, NULL,
1325 aggdata, flags) < 0)
1331 if (pfv->pfv_argc == 1)
1332 return (nrecs != 0);
1337 * If the conversion is %%, just invoke the print callback
1338 * with no data record and continue; it consumes no record.
1340 if (pfc->pfc_print == &pfprint_pct) {
1341 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1343 return (-1); /* errno is set for us */
1346 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1347 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1349 return (-1); /* errno is set for us */
1350 pfd->pfd_dynwidth = width;
1352 pfd->pfd_dynwidth = 0;
1355 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1356 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1357 return (-1); /* errno is set for us */
1359 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1361 * This should be impossible -- the compiler shouldn't
1362 * create a DT_PFCONV_AGG conversion without an
1363 * aggregation present. Still, we'd rather fail
1364 * gracefully than blow up...
1366 if (aggsdata == NULL)
1367 return (dt_set_errno(dtp, EDT_DMISMATCH));
1369 aggdata = aggsdata[curagg];
1370 agg = aggdata->dtada_desc;
1373 * We increment the current aggregation variable, but
1374 * not beyond the number of aggregation variables that
1375 * we're printing. This has the (desired) effect that
1376 * DT_PFCONV_AGG conversions beyond the number of
1377 * aggregation variables (re-)convert the aggregation
1378 * value of the last aggregation variable.
1380 if (curagg < naggvars - 1)
1383 rec = &agg->dtagd_rec[aggrec];
1384 addr = aggdata->dtada_data + rec->dtrd_offset;
1385 limit = addr + aggdata->dtada_size;
1386 normal = aggdata->dtada_normal;
1387 flags = DTRACE_BUFDATA_AGGVAL;
1390 return (dt_set_errno(dtp, EDT_DMISMATCH));
1392 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1394 * When printing aggregation keys, we always
1395 * set the aggdata to be the representative
1396 * (zeroth) aggregation. The aggdata isn't
1397 * actually used here in this case, but it is
1398 * passed to the buffer handler and must
1399 * therefore still be correct.
1401 aggdata = aggsdata[0];
1402 flags = DTRACE_BUFDATA_AGGKEY;
1407 addr = (caddr_t)buf + rec->dtrd_offset;
1412 size = rec->dtrd_size;
1414 if (addr + size > limit) {
1415 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1416 (void *)addr, rec->dtrd_size, (void *)lim);
1417 return (dt_set_errno(dtp, EDT_DOFFSET));
1420 if (rec->dtrd_alignment != 0 &&
1421 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1422 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1423 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1424 return (dt_set_errno(dtp, EDT_DALIGN));
1427 switch (rec->dtrd_action) {
1429 func = pfprint_average;
1431 case DTRACEAGG_QUANTIZE:
1432 func = pfprint_quantize;
1434 case DTRACEAGG_LQUANTIZE:
1435 func = pfprint_lquantize;
1440 case DTRACEACT_UMOD:
1441 func = pfprint_umod;
1444 func = pfc->pfc_print;
1448 if (pfd->pfd_flags & DT_PFCONV_ALT)
1450 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1452 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1454 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1456 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1458 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1462 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1463 * don't add the width to the format string. See the block
1464 * comment in pfprint_stack() for a description of the
1465 * behavior in this case.
1467 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1471 f += snprintf(f, sizeof (format), "%d", ABS(width));
1474 f += snprintf(f, sizeof (format), ".%d", prec);
1476 (void) strcpy(f, pfd->pfd_fmt);
1479 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1480 return (-1); /* errno is set for us */
1482 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1484 * For printa(), we flush the buffer after each tuple
1485 * element, inidicating that this is the last record
1488 if (i == pfv->pfv_argc - 1)
1489 flags |= DTRACE_BUFDATA_AGGLAST;
1491 if (dt_buffered_flush(dtp, NULL,
1492 rec, aggdata, flags) < 0)
1497 return ((int)(recp - recs));
1501 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1502 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1504 dtrace_optval_t size;
1507 rval = dtrace_getopt(dtp, "strsize", &size);
1509 assert(dtp->dt_sprintf_buflen == 0);
1511 if (dtp->dt_sprintf_buf != NULL)
1512 free(dtp->dt_sprintf_buf);
1514 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1515 return (dt_set_errno(dtp, EDT_NOMEM));
1517 bzero(dtp->dt_sprintf_buf, size);
1518 dtp->dt_sprintf_buflen = size;
1519 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1521 dtp->dt_sprintf_buflen = 0;
1524 free(dtp->dt_sprintf_buf);
1531 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1532 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1533 uint_t nrecs, const void *buf, size_t len)
1535 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1541 * Before we execute the specified command, flush fp to assure that
1542 * any prior dt_printf()'s appear before the output of the command
1547 if (system(dtp->dt_sprintf_buf) == -1)
1548 return (dt_set_errno(dtp, errno));
1554 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1555 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1556 uint_t nrecs, const void *buf, size_t len)
1558 char selfbuf[40], restorebuf[40], *filename;
1561 dt_pfargv_t *pfv = fmtdata;
1562 dt_pfargd_t *pfd = pfv->pfv_argv;
1564 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1566 if (rval == -1 || fp == NULL)
1570 if (pfd->pfd_preflen != 0 &&
1571 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1573 * The only way to have the format string set to the value
1574 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1575 * denoting that we should restore the old stdout.
1577 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1579 if (dtp->dt_stdout_fd == -1) {
1581 * We could complain here by generating an error,
1582 * but it seems like overkill: it seems that calling
1583 * freopen() to restore stdout when freopen() has
1584 * never before been called should just be a no-op,
1585 * so we just return in this case.
1590 (void) snprintf(restorebuf, sizeof (restorebuf),
1591 "/dev/fd/%d", dtp->dt_stdout_fd);
1592 filename = restorebuf;
1594 filename = dtp->dt_sprintf_buf;
1598 * freopen(3C) will always close the specified stream and underlying
1599 * file descriptor -- even if the specified file can't be opened.
1600 * Even for the semantic cesspool that is standard I/O, this is
1601 * surprisingly brain-dead behavior: it means that any failure to
1602 * open the specified file destroys the specified stream in the
1603 * process -- which is particularly relevant when the specified stream
1604 * happens (or rather, happened) to be stdout. This could be resolved
1605 * were there an "fdreopen()" equivalent of freopen() that allowed one
1606 * to pass a file descriptor instead of the name of a file, but there
1607 * is no such thing. However, we can effect this ourselves by first
1608 * fopen()'ing the desired file, and then (assuming that that works),
1609 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1610 * file descriptor for the fopen()'d file. This way, if the fopen()
1611 * fails, we can fail the operation without destroying stdout.
1613 if ((nfp = fopen(filename, "aF")) == NULL) {
1614 char *msg = strerror(errno);
1618 len += strlen(msg) + strlen(filename);
1619 faultstr = alloca(len);
1621 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1622 filename, strerror(errno));
1624 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1630 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1632 if (dtp->dt_stdout_fd == -1) {
1634 * If this is the first time that we're calling freopen(),
1635 * we're going to stash away the file descriptor for stdout.
1636 * We don't expect the dup(2) to fail, so if it does we must
1639 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1641 return (dt_set_errno(dtp, errno));
1645 if (freopen(selfbuf, "aF", fp) == NULL) {
1647 return (dt_set_errno(dtp, errno));
1653 * The 'standard output' (which is not necessarily stdout)
1654 * treatment on FreeBSD is implemented differently than on
1655 * Solaris because FreeBSD's freopen() will attempt to re-use
1656 * the current file descriptor, causing the previous file to
1657 * be closed and thereby preventing it from be re-activated
1660 * For FreeBSD we use the concept of setting an output file
1661 * pointer in the DTrace handle if a dtrace_freopen() has
1662 * enabled another output file and we leave the caller's
1663 * file pointer untouched. If it was actually stdout, then
1664 * stdout remains open. If it was another file, then that
1665 * file remains open. While a dtrace_freopen() has activated
1666 * another file, we keep a pointer to that which we use in
1667 * the output functions by preference and only use the caller's
1668 * file pointer if no dtrace_freopen() call has been made.
1670 * The check to see if we're re-activating the caller's
1671 * output file is much the same as on Solaris.
1673 if (pfd->pfd_preflen != 0 &&
1674 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1676 * The only way to have the format string set to the value
1677 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1678 * denoting that we should restore the old stdout.
1680 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1682 if (dtp->dt_freopen_fp == NULL) {
1684 * We could complain here by generating an error,
1685 * but it seems like overkill: it seems that calling
1686 * freopen() to restore stdout when freopen() has
1687 * never before been called should just be a no-op,
1688 * so we just return in this case.
1694 * At this point, to re-active the original output file,
1695 * on FreeBSD we only code the current file that this
1696 * function opened previously.
1698 (void) fclose(dtp->dt_freopen_fp);
1699 dtp->dt_freopen_fp = NULL;
1704 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1705 char *msg = strerror(errno);
1709 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1710 faultstr = alloca(len);
1712 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1713 dtp->dt_sprintf_buf, strerror(errno));
1715 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1721 if (dtp->dt_freopen_fp != NULL)
1722 (void) fclose(dtp->dt_freopen_fp);
1724 /* Remember that the output has been redirected to the new file. */
1725 dtp->dt_freopen_fp = nfp;
1733 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1734 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1735 uint_t nrecs, const void *buf, size_t len)
1737 return (dt_printf_format(dtp, fp, fmtdata,
1738 recp, nrecs, buf, len, NULL, 0));
1742 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1744 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1749 return (NULL); /* errno has been set for us */
1751 pfd = pfv->pfv_argv;
1753 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1754 const dt_pfconv_t *pfc = pfd->pfd_conv;
1760 * If the output format is not %s then we assume that we have
1761 * been given a correctly-sized format string, so we copy the
1762 * true format name including the size modifier. If the output
1763 * format is %s, then either the input format is %s as well or
1764 * it is one of our custom formats (e.g. pfprint_addr), so we
1765 * must set pfd_fmt to be the output format conversion "s".
1767 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1768 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1770 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1777 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1779 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1782 return (NULL); /* errno has been set for us */
1784 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1791 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1793 dt_pfargv_t *pfv = fmtdata;
1794 dt_pfargd_t *pfd = pfv->pfv_argv;
1797 * An upper bound on the string length is the length of the original
1798 * format string, plus three times the number of conversions (each
1799 * conversion could add up an additional "ll" and/or pfd_width digit
1800 * in the case of converting %? to %16) plus one for a terminating \0.
1802 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1803 char *format = alloca(formatlen);
1807 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1808 const dt_pfconv_t *pfc = pfd->pfd_conv;
1810 int width = pfd->pfd_width;
1811 int prec = pfd->pfd_prec;
1813 if (pfd->pfd_preflen != 0) {
1814 for (j = 0; j < pfd->pfd_preflen; j++)
1815 *f++ = pfd->pfd_prefix[j];
1823 if (pfd->pfd_flags & DT_PFCONV_ALT)
1825 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1827 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1829 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1831 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1833 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1837 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1839 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1841 if (pfd->pfd_flags & DT_PFCONV_AGG)
1845 f += snprintf(f, sizeof (format), "%d", width);
1848 f += snprintf(f, sizeof (format), ".%d", prec);
1851 * If the output format is %s, then either %s is the underlying
1852 * conversion or the conversion is one of our customized ones,
1853 * e.g. pfprint_addr. In these cases, put the original string
1854 * name of the conversion (pfc_name) into the pickled format
1855 * string rather than the derived conversion (pfd_fmt).
1857 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1858 str = pfc->pfc_name;
1862 for (j = 0; str[j] != '\0'; j++)
1866 *f = '\0'; /* insert nul byte; do not count in return value */
1868 assert(f < format + formatlen);
1869 (void) strncpy(s, format, len);
1871 return ((size_t)(f - format));
1875 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1877 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1878 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1879 uint_t nrecs = agg->dtagd_nrecs;
1880 dt_pfwalk_t *pfw = arg;
1881 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1884 if (dt_printf_getint(dtp, recp++, nrecs--,
1885 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1886 return (0); /* no aggregation id or id does not match */
1888 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1889 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1890 return (pfw->pfw_err = dtp->dt_errno);
1893 * Cast away the const to set the bit indicating that this aggregation
1896 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1902 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1904 const dtrace_aggdata_t *aggdata = aggsdata[0];
1905 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1906 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1907 uint_t nrecs = agg->dtagd_nrecs - 1;
1908 dt_pfwalk_t *pfw = arg;
1909 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1912 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1913 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
1914 aggsdata, naggvars) == -1)
1915 return (pfw->pfw_err = dtp->dt_errno);
1918 * For each aggregation, indicate that it has been printed, casting
1919 * away the const as necessary.
1921 for (i = 1; i < naggvars; i++) {
1922 agg = aggsdata[i]->dtada_desc;
1923 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1930 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1931 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
1932 uint_t nrecs, const void *buf, size_t len)
1935 int i, naggvars = 0;
1936 dtrace_aggvarid_t *aggvars;
1938 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
1941 * This might be a printa() with multiple aggregation variables. We
1942 * need to scan forward through the records until we find a record from
1943 * a different statement.
1945 for (i = 0; i < nrecs; i++) {
1946 const dtrace_recdesc_t *nrec = &recs[i];
1948 if (nrec->dtrd_uarg != recs->dtrd_uarg)
1951 if (nrec->dtrd_action != recs->dtrd_action)
1952 return (dt_set_errno(dtp, EDT_BADAGG));
1954 aggvars[naggvars++] =
1955 /* LINTED - alignment */
1956 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
1960 return (dt_set_errno(dtp, EDT_BADAGG));
1962 pfw.pfw_argv = fmtdata;
1966 if (naggvars == 1) {
1967 pfw.pfw_aid = aggvars[0];
1969 if (dtrace_aggregate_walk_sorted(dtp,
1970 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
1971 return (-1); /* errno is set for us */
1973 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
1974 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
1975 return (-1); /* errno is set for us */