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.
28 #include <sys/sysmacros.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <arpa/nameser.h>
42 #include <dt_printf.h>
43 #include <dt_string.h>
48 pfcheck_addr(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
50 return (dt_node_is_pointer(dnp
) || dt_node_is_integer(dnp
));
55 pfcheck_kaddr(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
57 return (dt_node_is_pointer(dnp
) || dt_node_is_integer(dnp
) ||
58 dt_node_is_symaddr(dnp
));
63 pfcheck_uaddr(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
65 dtrace_hdl_t
*dtp
= pfv
->pfv_dtp
;
66 dt_ident_t
*idp
= dt_idhash_lookup(dtp
->dt_macros
, "target");
68 if (dt_node_is_usymaddr(dnp
))
71 if (idp
== NULL
|| idp
->di_id
== 0)
74 return (dt_node_is_pointer(dnp
) || dt_node_is_integer(dnp
));
79 pfcheck_stack(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
81 return (dt_node_is_stack(dnp
));
86 pfcheck_time(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
88 return (dt_node_is_integer(dnp
) &&
89 dt_node_type_size(dnp
) == sizeof (uint64_t));
94 pfcheck_str(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
102 if (dt_node_is_string(dnp
))
106 base
= ctf_type_resolve(ctfp
, dnp
->dn_type
);
107 kind
= ctf_type_kind(ctfp
, base
);
109 return (kind
== CTF_K_ARRAY
&& ctf_array_info(ctfp
, base
, &r
) == 0 &&
110 (base
= ctf_type_resolve(ctfp
, r
.ctr_contents
)) != CTF_ERR
&&
111 ctf_type_encoding(ctfp
, base
, &e
) == 0 && IS_CHAR(e
));
116 pfcheck_wstr(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
118 ctf_file_t
*ctfp
= dnp
->dn_ctfp
;
119 ctf_id_t base
= ctf_type_resolve(ctfp
, dnp
->dn_type
);
120 uint_t kind
= ctf_type_kind(ctfp
, base
);
125 return (kind
== CTF_K_ARRAY
&& ctf_array_info(ctfp
, base
, &r
) == 0 &&
126 (base
= ctf_type_resolve(ctfp
, r
.ctr_contents
)) != CTF_ERR
&&
127 ctf_type_kind(ctfp
, base
) == CTF_K_INTEGER
&&
128 ctf_type_encoding(ctfp
, base
, &e
) == 0 && e
.cte_bits
== 32);
133 pfcheck_csi(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
135 return (dt_node_is_integer(dnp
) &&
136 dt_node_type_size(dnp
) <= sizeof (int));
141 pfcheck_fp(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
143 return (dt_node_is_float(dnp
));
148 pfcheck_xint(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
150 return (dt_node_is_integer(dnp
));
155 pfcheck_dint(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
157 if (dnp
->dn_flags
& DT_NF_SIGNED
)
158 pfd
->pfd_fmt
[strlen(pfd
->pfd_fmt
) - 1] = 'i';
160 pfd
->pfd_fmt
[strlen(pfd
->pfd_fmt
) - 1] = 'u';
162 return (dt_node_is_integer(dnp
));
167 pfcheck_xshort(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
169 ctf_file_t
*ctfp
= dnp
->dn_ctfp
;
170 ctf_id_t type
= ctf_type_resolve(ctfp
, dnp
->dn_type
);
171 char n
[DT_TYPE_NAMELEN
];
173 return (ctf_type_name(ctfp
, type
, n
, sizeof (n
)) != NULL
&& (
174 strcmp(n
, "short") == 0 || strcmp(n
, "signed short") == 0 ||
175 strcmp(n
, "unsigned short") == 0));
180 pfcheck_xlong(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
182 ctf_file_t
*ctfp
= dnp
->dn_ctfp
;
183 ctf_id_t type
= ctf_type_resolve(ctfp
, dnp
->dn_type
);
184 char n
[DT_TYPE_NAMELEN
];
186 return (ctf_type_name(ctfp
, type
, n
, sizeof (n
)) != NULL
&& (
187 strcmp(n
, "long") == 0 || strcmp(n
, "signed long") == 0 ||
188 strcmp(n
, "unsigned long") == 0));
193 pfcheck_xlonglong(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
195 ctf_file_t
*ctfp
= dnp
->dn_ctfp
;
196 ctf_id_t type
= dnp
->dn_type
;
197 char n
[DT_TYPE_NAMELEN
];
199 if (ctf_type_name(ctfp
, ctf_type_resolve(ctfp
, type
), n
,
200 sizeof (n
)) != NULL
&& (strcmp(n
, "long long") == 0 ||
201 strcmp(n
, "signed long long") == 0 ||
202 strcmp(n
, "unsigned long long") == 0))
206 * If the type used for %llx or %llX is not an [unsigned] long long, we
207 * also permit it to be a [u]int64_t or any typedef thereof. We know
208 * that these typedefs are guaranteed to work with %ll[xX] in either
209 * compilation environment even though they alias to "long" in LP64.
211 while (ctf_type_kind(ctfp
, type
) == CTF_K_TYPEDEF
) {
212 if (ctf_type_name(ctfp
, type
, n
, sizeof (n
)) != NULL
&&
213 (strcmp(n
, "int64_t") == 0 || strcmp(n
, "uint64_t") == 0))
216 type
= ctf_type_reference(ctfp
, type
);
224 pfcheck_type(dt_pfargv_t
*pfv
, dt_pfargd_t
*pfd
, dt_node_t
*dnp
)
226 return (ctf_type_compat(dnp
->dn_ctfp
, ctf_type_resolve(dnp
->dn_ctfp
,
227 dnp
->dn_type
), pfd
->pfd_conv
->pfc_dctfp
, pfd
->pfd_conv
->pfc_dtype
));
232 pfprint_sint(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
233 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t unormal
)
235 int64_t normal
= (int64_t)unormal
;
236 int32_t n
= (int32_t)normal
;
239 case sizeof (int8_t):
240 return (dt_printf(dtp
, fp
, format
,
241 (int32_t)*((int8_t *)addr
) / n
));
242 case sizeof (int16_t):
243 return (dt_printf(dtp
, fp
, format
,
244 (int32_t)*((int16_t *)addr
) / n
));
245 case sizeof (int32_t):
246 return (dt_printf(dtp
, fp
, format
,
247 *((int32_t *)addr
) / n
));
248 case sizeof (int64_t):
249 return (dt_printf(dtp
, fp
, format
,
250 *((int64_t *)addr
) / normal
));
252 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
258 pfprint_uint(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
259 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
261 uint32_t n
= (uint32_t)normal
;
264 case sizeof (uint8_t):
265 return (dt_printf(dtp
, fp
, format
,
266 (uint32_t)*((uint8_t *)addr
) / n
));
267 case sizeof (uint16_t):
268 return (dt_printf(dtp
, fp
, format
,
269 (uint32_t)*((uint16_t *)addr
) / n
));
270 case sizeof (uint32_t):
271 return (dt_printf(dtp
, fp
, format
,
272 *((uint32_t *)addr
) / n
));
273 case sizeof (uint64_t):
274 return (dt_printf(dtp
, fp
, format
,
275 *((uint64_t *)addr
) / normal
));
277 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
282 pfprint_dint(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
283 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
285 if (pfd
->pfd_flags
& DT_PFCONV_SIGNED
)
286 return (pfprint_sint(dtp
, fp
, format
, pfd
, addr
, size
, normal
));
288 return (pfprint_uint(dtp
, fp
, format
, pfd
, addr
, size
, normal
));
293 pfprint_fp(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
294 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
296 double n
= (double)normal
;
297 long double ldn
= (long double)normal
;
301 return (dt_printf(dtp
, fp
, format
,
302 (double)*((float *)addr
) / n
));
303 case sizeof (double):
304 return (dt_printf(dtp
, fp
, format
,
305 *((double *)addr
) / n
));
306 case sizeof (long double):
307 return (dt_printf(dtp
, fp
, format
,
308 *((long double *)addr
) / ldn
));
310 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
316 pfprint_addr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
317 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
324 case sizeof (uint32_t):
325 val
= *((uint32_t *)addr
);
327 case sizeof (uint64_t):
328 val
= *((uint64_t *)addr
);
331 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
337 } while ((len
= dtrace_addr2str(dtp
, val
, s
, n
)) > n
);
339 return (dt_printf(dtp
, fp
, format
, s
));
344 pfprint_mod(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
345 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
347 return (dt_print_mod(dtp
, fp
, format
, (caddr_t
)addr
));
352 pfprint_umod(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
353 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
355 return (dt_print_umod(dtp
, fp
, format
, (caddr_t
)addr
));
360 pfprint_uaddr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
361 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
365 uint64_t val
, pid
= 0;
367 dt_ident_t
*idp
= dt_idhash_lookup(dtp
->dt_macros
, "target");
370 case sizeof (uint32_t):
371 val
= (u_longlong_t
)*((uint32_t *)addr
);
373 case sizeof (uint64_t):
374 val
= (u_longlong_t
)*((uint64_t *)addr
);
376 case sizeof (uint64_t) * 2:
377 pid
= ((uint64_t *)(uintptr_t)addr
)[0];
378 val
= ((uint64_t *)(uintptr_t)addr
)[1];
381 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
384 if (pid
== 0 && dtp
->dt_vector
== NULL
&& idp
!= NULL
)
390 } while ((len
= dtrace_uaddr2str(dtp
, pid
, val
, s
, n
)) > n
);
392 return (dt_printf(dtp
, fp
, format
, s
));
397 pfprint_stack(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
398 const dt_pfargd_t
*pfd
, const void *vaddr
, size_t size
, uint64_t normal
)
401 dtrace_optval_t saved
= dtp
->dt_options
[DTRACEOPT_STACKINDENT
];
402 const dtrace_recdesc_t
*rec
= pfd
->pfd_rec
;
403 caddr_t addr
= (caddr_t
)vaddr
;
407 * We have stashed the value of the STACKINDENT option, and we will
408 * now override it for the purposes of formatting the stack. If the
409 * field has been specified as left-aligned (i.e. (%-#), we set the
410 * indentation to be the width. This is a slightly odd semantic, but
411 * it's useful functionality -- and it's slightly odd to begin with to
412 * be using a single format specifier to be formatting multiple lines
415 if (pfd
->pfd_dynwidth
< 0) {
416 assert(pfd
->pfd_flags
& DT_PFCONV_DYNWIDTH
);
417 width
= -pfd
->pfd_dynwidth
;
418 } else if (pfd
->pfd_flags
& DT_PFCONV_LEFT
) {
419 width
= pfd
->pfd_dynwidth
? pfd
->pfd_dynwidth
: pfd
->pfd_width
;
424 dtp
->dt_options
[DTRACEOPT_STACKINDENT
] = width
;
426 switch (rec
->dtrd_action
) {
427 case DTRACEACT_USTACK
:
428 case DTRACEACT_JSTACK
:
429 err
= dt_print_ustack(dtp
, fp
, format
, addr
, rec
->dtrd_arg
);
432 case DTRACEACT_STACK
:
433 err
= dt_print_stack(dtp
, fp
, format
, addr
, rec
->dtrd_arg
,
434 rec
->dtrd_size
/ rec
->dtrd_arg
);
441 dtp
->dt_options
[DTRACEOPT_STACKINDENT
] = saved
;
448 pfprint_time(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
449 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
451 char src
[32], buf
[32], *dst
= buf
;
452 hrtime_t time
= *((uint64_t *)addr
);
453 time_t sec
= (time_t)(time
/ NANOSEC
);
457 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
458 * Below, we turn this into the canonical adb/mdb /[yY] format,
459 * "1973 Dec 3 17:20:00".
461 (void) ctime_r(&sec
, src
, sizeof (src
));
464 * Place the 4-digit year at the head of the string...
466 for (i
= 20; i
< 24; i
++)
470 * ...and follow it with the remainder (month, day, hh:mm:ss).
472 for (i
= 3; i
< 19; i
++)
476 return (dt_printf(dtp
, fp
, format
, buf
));
480 * This prints the time in RFC 822 standard form. This is useful for emitting
481 * notions of time that are consumed by standard tools (e.g., as part of an
486 pfprint_time822(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
487 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
489 hrtime_t time
= *((uint64_t *)addr
);
490 time_t sec
= (time_t)(time
/ NANOSEC
);
494 (void) localtime_r(&sec
, &tm
);
495 (void) strftime(buf
, sizeof (buf
), "%a, %d %b %G %T %Z", &tm
);
496 return (dt_printf(dtp
, fp
, format
, buf
));
501 pfprint_port(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
502 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
504 uint16_t port
= htons(*((uint16_t *)addr
));
506 struct servent
*sv
, res
;
508 if ((sv
= getservbyport_r(port
, NULL
, &res
, buf
, sizeof (buf
))) != NULL
)
509 return (dt_printf(dtp
, fp
, format
, sv
->s_name
));
511 (void) snprintf(buf
, sizeof (buf
), "%d", *((uint16_t *)addr
));
512 return (dt_printf(dtp
, fp
, format
, buf
));
517 pfprint_inetaddr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
518 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
520 char *s
= alloca(size
+ 1);
521 struct hostent
*host
, res
;
522 char inetaddr
[NS_IN6ADDRSZ
];
526 bcopy(addr
, s
, size
);
529 if (strchr(s
, ':') == NULL
&& inet_pton(AF_INET
, s
, inetaddr
) != -1) {
530 if ((host
= gethostbyaddr_r(inetaddr
, NS_INADDRSZ
,
531 AF_INET
, &res
, buf
, sizeof (buf
), &e
)) != NULL
)
532 return (dt_printf(dtp
, fp
, format
, host
->h_name
));
533 } else if (inet_pton(AF_INET6
, s
, inetaddr
) != -1) {
534 if ((host
= getipnodebyaddr(inetaddr
, NS_IN6ADDRSZ
,
535 AF_INET6
, &e
)) != NULL
)
536 return (dt_printf(dtp
, fp
, format
, host
->h_name
));
539 return (dt_printf(dtp
, fp
, format
, s
));
544 pfprint_cstr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
545 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
547 char *s
= alloca(size
+ 1);
549 bcopy(addr
, s
, size
);
551 return (dt_printf(dtp
, fp
, format
, s
));
556 pfprint_wstr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
557 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
559 wchar_t *ws
= alloca(size
+ sizeof (wchar_t));
561 bcopy(addr
, ws
, size
);
562 ws
[size
/ sizeof (wchar_t)] = L
'\0';
563 return (dt_printf(dtp
, fp
, format
, ws
));
568 pfprint_estr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
569 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
574 if ((s
= strchr2esc(addr
, size
)) == NULL
)
575 return (dt_set_errno(dtp
, EDT_NOMEM
));
577 n
= dt_printf(dtp
, fp
, format
, s
);
583 pfprint_echr(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
584 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
589 case sizeof (int8_t):
592 case sizeof (int16_t):
593 c
= *(int16_t *)addr
;
595 case sizeof (int32_t):
596 c
= *(int32_t *)addr
;
599 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
602 return (pfprint_estr(dtp
, fp
, format
, pfd
, &c
, 1, normal
));
607 pfprint_pct(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
608 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
610 return (dt_printf(dtp
, fp
, "%%"));
613 static const char pfproto_xint
[] = "char, short, int, long, or long long";
614 static const char pfproto_csi
[] = "char, short, or int";
615 static const char pfproto_fp
[] = "float, double, or long double";
616 static const char pfproto_addr
[] = "pointer or integer";
617 static const char pfproto_uaddr
[] =
618 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
619 static const char pfproto_cstr
[] = "char [] or string (or use stringof)";
620 static const char pfproto_wstr
[] = "wchar_t []";
623 * Printf format conversion dictionary. This table should match the set of
624 * conversions offered by printf(3C), as well as some additional extensions.
625 * The second parameter is an ASCII string which is either an actual type
626 * name we should look up (if pfcheck_type is specified), or just a descriptive
627 * string of the types expected for use in error messages.
629 static const dt_pfconv_t _dtrace_conversions
[] = {
630 { "a", "s", pfproto_addr
, pfcheck_kaddr
, pfprint_addr
},
631 { "A", "s", pfproto_uaddr
, pfcheck_uaddr
, pfprint_uaddr
},
632 { "c", "c", pfproto_csi
, pfcheck_csi
, pfprint_sint
},
633 { "C", "s", pfproto_csi
, pfcheck_csi
, pfprint_echr
},
634 { "d", "d", pfproto_xint
, pfcheck_dint
, pfprint_dint
},
635 { "e", "e", pfproto_fp
, pfcheck_fp
, pfprint_fp
},
636 { "E", "E", pfproto_fp
, pfcheck_fp
, pfprint_fp
},
637 { "f", "f", pfproto_fp
, pfcheck_fp
, pfprint_fp
},
638 { "g", "g", pfproto_fp
, pfcheck_fp
, pfprint_fp
},
639 { "G", "G", pfproto_fp
, pfcheck_fp
, pfprint_fp
},
640 { "hd", "d", "short", pfcheck_type
, pfprint_sint
},
641 { "hi", "i", "short", pfcheck_type
, pfprint_sint
},
642 { "ho", "o", "unsigned short", pfcheck_type
, pfprint_uint
},
643 { "hu", "u", "unsigned short", pfcheck_type
, pfprint_uint
},
644 { "hx", "x", "short", pfcheck_xshort
, pfprint_uint
},
645 { "hX", "X", "short", pfcheck_xshort
, pfprint_uint
},
646 { "i", "i", pfproto_xint
, pfcheck_xint
, pfprint_sint
},
647 { "I", "s", pfproto_cstr
, pfcheck_str
, pfprint_inetaddr
},
648 { "k", "s", "stack", pfcheck_stack
, pfprint_stack
},
649 { "lc", "lc", "int", pfcheck_type
, pfprint_sint
}, /* a.k.a. wint_t */
650 { "ld", "d", "long", pfcheck_type
, pfprint_sint
},
651 { "li", "i", "long", pfcheck_type
, pfprint_sint
},
652 { "lo", "o", "unsigned long", pfcheck_type
, pfprint_uint
},
653 { "lu", "u", "unsigned long", pfcheck_type
, pfprint_uint
},
654 { "ls", "ls", pfproto_wstr
, pfcheck_wstr
, pfprint_wstr
},
655 { "lx", "x", "long", pfcheck_xlong
, pfprint_uint
},
656 { "lX", "X", "long", pfcheck_xlong
, pfprint_uint
},
657 { "lld", "d", "long long", pfcheck_type
, pfprint_sint
},
658 { "lli", "i", "long long", pfcheck_type
, pfprint_sint
},
659 { "llo", "o", "unsigned long long", pfcheck_type
, pfprint_uint
},
660 { "llu", "u", "unsigned long long", pfcheck_type
, pfprint_uint
},
661 { "llx", "x", "long long", pfcheck_xlonglong
, pfprint_uint
},
662 { "llX", "X", "long long", pfcheck_xlonglong
, pfprint_uint
},
663 { "Le", "e", "long double", pfcheck_type
, pfprint_fp
},
664 { "LE", "E", "long double", pfcheck_type
, pfprint_fp
},
665 { "Lf", "f", "long double", pfcheck_type
, pfprint_fp
},
666 { "Lg", "g", "long double", pfcheck_type
, pfprint_fp
},
667 { "LG", "G", "long double", pfcheck_type
, pfprint_fp
},
668 { "o", "o", pfproto_xint
, pfcheck_xint
, pfprint_uint
},
669 { "p", "x", pfproto_addr
, pfcheck_addr
, pfprint_uint
},
670 { "P", "s", "uint16_t", pfcheck_type
, pfprint_port
},
671 { "s", "s", "char [] or string (or use stringof)", pfcheck_str
, pfprint_cstr
},
672 { "S", "s", pfproto_cstr
, pfcheck_str
, pfprint_estr
},
673 { "T", "s", "int64_t", pfcheck_time
, pfprint_time822
},
674 { "u", "u", pfproto_xint
, pfcheck_xint
, pfprint_uint
},
675 { "wc", "wc", "int", pfcheck_type
, pfprint_sint
}, /* a.k.a. wchar_t */
676 { "ws", "ws", pfproto_wstr
, pfcheck_wstr
, pfprint_wstr
},
677 { "x", "x", pfproto_xint
, pfcheck_xint
, pfprint_uint
},
678 { "X", "X", pfproto_xint
, pfcheck_xint
, pfprint_uint
},
679 { "Y", "s", "int64_t", pfcheck_time
, pfprint_time
},
680 { "%", "%", "void", pfcheck_type
, pfprint_pct
},
681 { NULL
, NULL
, NULL
, NULL
, NULL
}
685 dt_pfdict_create(dtrace_hdl_t
*dtp
)
687 uint_t n
= _dtrace_strbuckets
;
688 const dt_pfconv_t
*pfd
;
691 if ((pdi
= malloc(sizeof (dt_pfdict_t
))) == NULL
||
692 (pdi
->pdi_buckets
= malloc(sizeof (dt_pfconv_t
*) * n
)) == NULL
) {
694 return (dt_set_errno(dtp
, EDT_NOMEM
));
697 dtp
->dt_pfdict
= pdi
;
698 bzero(pdi
->pdi_buckets
, sizeof (dt_pfconv_t
*) * n
);
699 pdi
->pdi_nbuckets
= n
;
701 for (pfd
= _dtrace_conversions
; pfd
->pfc_name
!= NULL
; pfd
++) {
702 dtrace_typeinfo_t dtt
;
706 if ((pfc
= malloc(sizeof (dt_pfconv_t
))) == NULL
) {
707 dt_pfdict_destroy(dtp
);
708 return (dt_set_errno(dtp
, EDT_NOMEM
));
711 bcopy(pfd
, pfc
, sizeof (dt_pfconv_t
));
712 h
= dt_strtab_hash(pfc
->pfc_name
, NULL
) % n
;
713 pfc
->pfc_next
= pdi
->pdi_buckets
[h
];
714 pdi
->pdi_buckets
[h
] = pfc
;
717 dtt
.dtt_type
= CTF_ERR
;
720 * The "D" container or its parent must contain a definition of
721 * any type referenced by a printf conversion. If none can be
722 * found, we fail to initialize the printf dictionary.
724 if (pfc
->pfc_check
== &pfcheck_type
&& dtrace_lookup_by_type(
725 dtp
, DTRACE_OBJ_DDEFS
, pfc
->pfc_tstr
, &dtt
) != 0) {
726 dt_pfdict_destroy(dtp
);
727 return (dt_set_errno(dtp
, EDT_NOCONV
));
730 pfc
->pfc_dctfp
= dtt
.dtt_ctfp
;
731 pfc
->pfc_dtype
= dtt
.dtt_type
;
734 * The "C" container may contain an alternate definition of an
735 * explicit conversion type. If it does, use it; otherwise
736 * just set pfc_ctype to pfc_dtype so it is always valid.
738 if (pfc
->pfc_check
== &pfcheck_type
&& dtrace_lookup_by_type(
739 dtp
, DTRACE_OBJ_CDEFS
, pfc
->pfc_tstr
, &dtt
) == 0) {
740 pfc
->pfc_cctfp
= dtt
.dtt_ctfp
;
741 pfc
->pfc_ctype
= dtt
.dtt_type
;
743 pfc
->pfc_cctfp
= pfc
->pfc_dctfp
;
744 pfc
->pfc_ctype
= pfc
->pfc_dtype
;
747 if (pfc
->pfc_check
== NULL
|| pfc
->pfc_print
== NULL
||
748 pfc
->pfc_ofmt
== NULL
|| pfc
->pfc_tstr
== NULL
) {
749 dt_pfdict_destroy(dtp
);
750 return (dt_set_errno(dtp
, EDT_BADCONV
));
753 dt_dprintf("loaded printf conversion %%%s\n", pfc
->pfc_name
);
760 dt_pfdict_destroy(dtrace_hdl_t
*dtp
)
762 dt_pfdict_t
*pdi
= dtp
->dt_pfdict
;
763 dt_pfconv_t
*pfc
, *nfc
;
769 for (i
= 0; i
< pdi
->pdi_nbuckets
; i
++) {
770 for (pfc
= pdi
->pdi_buckets
[i
]; pfc
!= NULL
; pfc
= nfc
) {
776 free(pdi
->pdi_buckets
);
778 dtp
->dt_pfdict
= NULL
;
781 static const dt_pfconv_t
*
782 dt_pfdict_lookup(dtrace_hdl_t
*dtp
, const char *name
)
784 dt_pfdict_t
*pdi
= dtp
->dt_pfdict
;
785 uint_t h
= dt_strtab_hash(name
, NULL
) % pdi
->pdi_nbuckets
;
786 const dt_pfconv_t
*pfc
;
788 for (pfc
= pdi
->pdi_buckets
[h
]; pfc
!= NULL
; pfc
= pfc
->pfc_next
) {
789 if (strcmp(pfc
->pfc_name
, name
) == 0)
797 dt_printf_error(dtrace_hdl_t
*dtp
, int err
)
800 longjmp(yypcb
->pcb_jmpbuf
, err
);
802 (void) dt_set_errno(dtp
, err
);
807 dt_printf_create(dtrace_hdl_t
*dtp
, const char *s
)
809 dt_pfargd_t
*pfd
, *nfd
= NULL
;
814 if ((pfv
= malloc(sizeof (dt_pfargv_t
))) == NULL
||
815 (format
= strdup(s
)) == NULL
) {
817 return (dt_printf_error(dtp
, EDT_NOMEM
));
820 pfv
->pfv_format
= format
;
821 pfv
->pfv_argv
= NULL
;
826 for (q
= format
; (p
= strchr(q
, '%')) != NULL
; q
= *p
? p
+ 1 : p
) {
835 if ((pfd
= malloc(sizeof (dt_pfargd_t
))) == NULL
) {
836 dt_printf_destroy(pfv
);
837 return (dt_printf_error(dtp
, EDT_NOMEM
));
840 if (pfv
->pfv_argv
!= NULL
)
845 bzero(pfd
, sizeof (dt_pfargd_t
));
850 pfd
->pfd_preflen
= (size_t)(p
- q
);
856 case '0': case '1': case '2': case '3': case '4':
857 case '5': case '6': case '7': case '8': case '9':
858 if (dot
== 0 && digits
== 0 && c
== '0') {
859 pfd
->pfd_flags
|= DT_PFCONV_ZPAD
;
860 pfd
->pfd_flags
&= ~DT_PFCONV_LEFT
;
864 for (n
= 0; isdigit(c
); c
= *++p
)
865 n
= n
* 10 + c
- '0';
877 pfd
->pfd_flags
|= DT_PFCONV_ALT
;
881 n
= dot
? DT_PFCONV_DYNPREC
: DT_PFCONV_DYNWIDTH
;
883 if (pfd
->pfd_flags
& n
) {
884 yywarn("format conversion #%u has more than "
885 "one '*' specified for the output %s\n",
886 pfv
->pfv_argc
, n
? "precision" : "width");
888 dt_printf_destroy(pfv
);
889 return (dt_printf_error(dtp
, EDT_COMPILER
));
896 pfd
->pfd_flags
|= DT_PFCONV_SPOS
;
900 pfd
->pfd_flags
|= DT_PFCONV_LEFT
;
901 pfd
->pfd_flags
&= ~DT_PFCONV_ZPAD
;
906 yywarn("format conversion #%u has more than "
907 "one '.' specified\n", pfv
->pfv_argc
);
909 dt_printf_destroy(pfv
);
910 return (dt_printf_error(dtp
, EDT_COMPILER
));
916 if (dtp
->dt_conf
.dtc_ctfmodel
== CTF_MODEL_LP64
)
923 pfd
->pfd_flags
|= DT_PFCONV_AGG
;
927 pfd
->pfd_flags
|= DT_PFCONV_GROUP
;
931 pfd
->pfd_flags
|= DT_PFCONV_SPACE
;
935 yywarn("format conversion #%u uses unsupported "
936 "positional format (%%n$)\n", pfv
->pfv_argc
);
938 dt_printf_destroy(pfv
);
939 return (dt_printf_error(dtp
, EDT_COMPILER
));
943 goto default_lbl
; /* if %% then use "%" conv */
945 yywarn("format conversion #%u cannot be combined "
946 "with other format flags: %%%%\n", pfv
->pfv_argc
);
948 dt_printf_destroy(pfv
);
949 return (dt_printf_error(dtp
, EDT_COMPILER
));
952 yywarn("format conversion #%u name expected before "
953 "end of format string\n", pfv
->pfv_argc
);
955 dt_printf_destroy(pfv
);
956 return (dt_printf_error(dtp
, EDT_COMPILER
));
962 if (namelen
< sizeof (name
) - 2)
969 name
[namelen
] = '\0';
972 pfd
->pfd_conv
= dt_pfdict_lookup(dtp
, name
);
974 if (pfd
->pfd_conv
== NULL
) {
975 yywarn("format conversion #%u is undefined: %%%s\n",
976 pfv
->pfv_argc
, name
);
977 dt_printf_destroy(pfv
);
978 return (dt_printf_error(dtp
, EDT_COMPILER
));
982 if (*q
!= '\0' || *format
== '\0') {
983 if ((pfd
= malloc(sizeof (dt_pfargd_t
))) == NULL
) {
984 dt_printf_destroy(pfv
);
985 return (dt_printf_error(dtp
, EDT_NOMEM
));
988 if (pfv
->pfv_argv
!= NULL
)
993 bzero(pfd
, sizeof (dt_pfargd_t
));
997 pfd
->pfd_preflen
= strlen(q
);
1004 dt_printf_destroy(dt_pfargv_t
*pfv
)
1006 dt_pfargd_t
*pfd
, *nfd
;
1008 for (pfd
= pfv
->pfv_argv
; pfd
!= NULL
; pfd
= nfd
) {
1009 nfd
= pfd
->pfd_next
;
1013 free(pfv
->pfv_format
);
1018 dt_printf_validate(dt_pfargv_t
*pfv
, uint_t flags
,
1019 dt_ident_t
*idp
, int foff
, dtrace_actkind_t kind
, dt_node_t
*dnp
)
1021 dt_pfargd_t
*pfd
= pfv
->pfv_argv
;
1022 const char *func
= idp
->di_name
;
1024 char n
[DT_TYPE_NAMELEN
];
1025 dtrace_typeinfo_t dtt
;
1026 const char *aggtype
;
1030 if (pfv
->pfv_format
[0] == '\0') {
1031 xyerror(D_PRINTF_FMT_EMPTY
,
1032 "%s( ) format string is empty\n", func
);
1035 pfv
->pfv_flags
= flags
;
1038 * We fake up a parse node representing the type that can be used with
1039 * an aggregation result conversion, which -- for all but count() --
1040 * is a signed quantity.
1042 if (kind
!= DTRACEAGG_COUNT
)
1043 aggtype
= "int64_t";
1045 aggtype
= "uint64_t";
1047 if (dt_type_lookup(aggtype
, &dtt
) != 0)
1048 xyerror(D_TYPE_ERR
, "failed to lookup agg type %s\n", aggtype
);
1050 bzero(&aggnode
, sizeof (aggnode
));
1051 dt_node_type_assign(&aggnode
, dtt
.dtt_ctfp
, dtt
.dtt_type
, B_FALSE
);
1053 for (i
= 0, j
= 0; i
< pfv
->pfv_argc
; i
++, pfd
= pfd
->pfd_next
) {
1054 const dt_pfconv_t
*pfc
= pfd
->pfd_conv
;
1055 const char *dyns
[2];
1062 continue; /* no checking if argd is just a prefix */
1064 if (pfc
->pfc_print
== &pfprint_pct
) {
1065 (void) strcat(pfd
->pfd_fmt
, pfc
->pfc_ofmt
);
1069 if (pfd
->pfd_flags
& DT_PFCONV_DYNPREC
)
1070 dyns
[dync
++] = ".*";
1071 if (pfd
->pfd_flags
& DT_PFCONV_DYNWIDTH
)
1074 for (; dync
!= 0; dync
--) {
1076 xyerror(D_PRINTF_DYN_PROTO
,
1077 "%s( ) prototype mismatch: conversion "
1078 "#%d (%%%s) is missing a corresponding "
1079 "\"%s\" argument\n", func
, i
+ 1,
1080 pfc
->pfc_name
, dyns
[dync
- 1]);
1083 if (dt_node_is_integer(dnp
) == 0) {
1084 xyerror(D_PRINTF_DYN_TYPE
,
1085 "%s( ) argument #%d is incompatible "
1086 "with conversion #%d prototype:\n"
1087 "\tconversion: %% %s %s\n"
1088 "\t prototype: int\n\t argument: %s\n",
1089 func
, j
+ foff
+ 1, i
+ 1,
1090 dyns
[dync
- 1], pfc
->pfc_name
,
1091 dt_node_type_name(dnp
, n
, sizeof (n
)));
1099 * If this conversion is consuming the aggregation data, set
1100 * the value node pointer (vnp) to a fake node based on the
1101 * aggregating function result type. Otherwise assign vnp to
1102 * the next parse node in the argument list, if there is one.
1104 if (pfd
->pfd_flags
& DT_PFCONV_AGG
) {
1105 if (!(flags
& DT_PRINTF_AGGREGATION
)) {
1106 xyerror(D_PRINTF_AGG_CONV
,
1107 "%%@ conversion requires an aggregation"
1108 " and is not for use with %s( )\n", func
);
1110 (void) strlcpy(vname
, "aggregating action",
1113 } else if (dnp
== NULL
) {
1114 xyerror(D_PRINTF_ARG_PROTO
,
1115 "%s( ) prototype mismatch: conversion #%d (%%"
1116 "%s) is missing a corresponding value argument\n",
1117 func
, i
+ 1, pfc
->pfc_name
);
1119 (void) snprintf(vname
, sizeof (vname
),
1120 "argument #%d", j
+ foff
+ 1);
1127 * Fill in the proposed final format string by prepending any
1128 * size-related prefixes to the pfconv's format string. The
1129 * pfc_check() function below may optionally modify the format
1130 * as part of validating the type of the input argument.
1132 if (pfc
->pfc_print
== &pfprint_sint
||
1133 pfc
->pfc_print
== &pfprint_uint
||
1134 pfc
->pfc_print
== &pfprint_dint
) {
1135 if (dt_node_type_size(vnp
) == sizeof (uint64_t))
1136 (void) strcpy(pfd
->pfd_fmt
, "ll");
1137 } else if (pfc
->pfc_print
== &pfprint_fp
) {
1138 if (dt_node_type_size(vnp
) == sizeof (long double))
1139 (void) strcpy(pfd
->pfd_fmt
, "L");
1142 (void) strcat(pfd
->pfd_fmt
, pfc
->pfc_ofmt
);
1145 * Validate the format conversion against the value node type.
1146 * If the conversion is good, create the descriptor format
1147 * string by concatenating together any required printf(3C)
1148 * size prefixes with the conversion's native format string.
1150 if (pfc
->pfc_check(pfv
, pfd
, vnp
) == 0) {
1151 xyerror(D_PRINTF_ARG_TYPE
,
1152 "%s( ) %s is incompatible with "
1153 "conversion #%d prototype:\n\tconversion: %%%s\n"
1154 "\t prototype: %s\n\t argument: %s\n", func
,
1155 vname
, i
+ 1, pfc
->pfc_name
, pfc
->pfc_tstr
,
1156 dt_node_type_name(vnp
, n
, sizeof (n
)));
1160 if ((flags
& DT_PRINTF_EXACTLEN
) && dnp
!= NULL
) {
1161 xyerror(D_PRINTF_ARG_EXTRA
,
1162 "%s( ) prototype mismatch: only %d arguments "
1163 "required by this format string\n", func
, j
);
1168 dt_printa_validate(dt_node_t
*lhs
, dt_node_t
*rhs
)
1170 dt_ident_t
*lid
, *rid
;
1171 dt_node_t
*lproto
, *rproto
;
1172 int largc
, rargc
, argn
;
1173 char n1
[DT_TYPE_NAMELEN
];
1174 char n2
[DT_TYPE_NAMELEN
];
1176 assert(lhs
->dn_kind
== DT_NODE_AGG
);
1177 assert(rhs
->dn_kind
== DT_NODE_AGG
);
1179 lid
= lhs
->dn_ident
;
1180 rid
= rhs
->dn_ident
;
1182 lproto
= ((dt_idsig_t
*)lid
->di_data
)->dis_args
;
1183 rproto
= ((dt_idsig_t
*)rid
->di_data
)->dis_args
;
1186 * First, get an argument count on each side. These must match.
1188 for (largc
= 0; lproto
!= NULL
; lproto
= lproto
->dn_list
)
1191 for (rargc
= 0; rproto
!= NULL
; rproto
= rproto
->dn_list
)
1194 if (largc
!= rargc
) {
1195 xyerror(D_PRINTA_AGGKEY
, "printa( ): @%s and @%s do not have "
1196 "matching key signatures: @%s has %d key%s, @%s has %d "
1197 "key%s", lid
->di_name
, rid
->di_name
,
1198 lid
->di_name
, largc
, largc
== 1 ? "" : "s",
1199 rid
->di_name
, rargc
, rargc
== 1 ? "" : "s");
1203 * Now iterate over the keys to verify that each type matches.
1205 lproto
= ((dt_idsig_t
*)lid
->di_data
)->dis_args
;
1206 rproto
= ((dt_idsig_t
*)rid
->di_data
)->dis_args
;
1208 for (argn
= 1; lproto
!= NULL
; argn
++, lproto
= lproto
->dn_list
,
1209 rproto
= rproto
->dn_list
) {
1210 assert(rproto
!= NULL
);
1212 if (dt_node_is_argcompat(lproto
, rproto
))
1215 xyerror(D_PRINTA_AGGPROTO
, "printa( ): @%s[ ] key #%d is "
1216 "incompatible with @%s:\n%9s key #%d: %s\n"
1217 "%9s key #%d: %s\n",
1218 rid
->di_name
, argn
, lid
->di_name
, lid
->di_name
, argn
,
1219 dt_node_type_name(lproto
, n1
, sizeof (n1
)), rid
->di_name
,
1220 argn
, dt_node_type_name(rproto
, n2
, sizeof (n2
)));
1225 dt_printf_getint(dtrace_hdl_t
*dtp
, const dtrace_recdesc_t
*recp
,
1226 uint_t nrecs
, const void *buf
, size_t len
, int *ip
)
1231 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1233 addr
= (uintptr_t)buf
+ recp
->dtrd_offset
;
1235 if (addr
+ sizeof (int) > (uintptr_t)buf
+ len
)
1236 return (dt_set_errno(dtp
, EDT_DOFFSET
));
1238 if (addr
& (recp
->dtrd_alignment
- 1))
1239 return (dt_set_errno(dtp
, EDT_DALIGN
));
1241 switch (recp
->dtrd_size
) {
1242 case sizeof (int8_t):
1243 *ip
= (int)*((int8_t *)addr
);
1245 case sizeof (int16_t):
1246 *ip
= (int)*((int16_t *)addr
);
1248 case sizeof (int32_t):
1249 *ip
= (int)*((int32_t *)addr
);
1251 case sizeof (int64_t):
1252 *ip
= (int)*((int64_t *)addr
);
1255 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1263 pfprint_average(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1264 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
1266 const uint64_t *data
= addr
;
1268 if (size
!= sizeof (uint64_t) * 2)
1269 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1271 return (dt_printf(dtp
, fp
, format
,
1272 data
[0] ? data
[1] / normal
/ data
[0] : 0));
1277 pfprint_stddev(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1278 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
1280 const uint64_t *data
= addr
;
1282 if (size
!= sizeof (uint64_t) * 4)
1283 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1285 return (dt_printf(dtp
, fp
, format
,
1286 dt_stddev((uint64_t *)data
, normal
)));
1291 pfprint_quantize(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1292 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
1294 return (dt_print_quantize(dtp
, fp
, addr
, size
, normal
));
1299 pfprint_lquantize(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1300 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
1302 return (dt_print_lquantize(dtp
, fp
, addr
, size
, normal
));
1307 pfprint_llquantize(dtrace_hdl_t
*dtp
, FILE *fp
, const char *format
,
1308 const dt_pfargd_t
*pfd
, const void *addr
, size_t size
, uint64_t normal
)
1310 return (dt_print_llquantize(dtp
, fp
, addr
, size
, normal
));
1314 dt_printf_format(dtrace_hdl_t
*dtp
, FILE *fp
, const dt_pfargv_t
*pfv
,
1315 const dtrace_recdesc_t
*recs
, uint_t nrecs
, const void *buf
,
1316 size_t len
, const dtrace_aggdata_t
**aggsdata
, int naggvars
)
1318 dt_pfargd_t
*pfd
= pfv
->pfv_argv
;
1319 const dtrace_recdesc_t
*recp
= recs
;
1320 const dtrace_aggdata_t
*aggdata
;
1321 dtrace_aggdesc_t
*agg
;
1322 caddr_t lim
= (caddr_t
)buf
+ len
, limit
;
1323 char format
[64] = "%";
1324 int i
, aggrec
, curagg
= -1;
1328 * If we are formatting an aggregation, set 'aggrec' to the index of
1329 * the final record description (the aggregation result) so we can use
1330 * this record index with any conversion where DT_PFCONV_AGG is set.
1331 * (The actual aggregation used will vary as we increment through the
1332 * aggregation variables that we have been passed.) Finally, we
1333 * decrement nrecs to prevent this record from being used with any
1336 if (pfv
->pfv_flags
& DT_PRINTF_AGGREGATION
) {
1337 assert(aggsdata
!= NULL
);
1338 assert(naggvars
> 0);
1341 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1343 curagg
= naggvars
> 1 ? 1 : 0;
1344 aggdata
= aggsdata
[0];
1345 aggrec
= aggdata
->dtada_desc
->dtagd_nrecs
- 1;
1349 for (i
= 0; i
< pfv
->pfv_argc
; i
++, pfd
= pfd
->pfd_next
) {
1350 const dt_pfconv_t
*pfc
= pfd
->pfd_conv
;
1351 int width
= pfd
->pfd_width
;
1352 int prec
= pfd
->pfd_prec
;
1355 char *f
= format
+ 1; /* skip initial '%' */
1356 const dtrace_recdesc_t
*rec
;
1362 if (pfd
->pfd_preflen
!= 0) {
1363 char *tmp
= alloca(pfd
->pfd_preflen
+ 1);
1365 bcopy(pfd
->pfd_prefix
, tmp
, pfd
->pfd_preflen
);
1366 tmp
[pfd
->pfd_preflen
] = '\0';
1368 if ((rval
= dt_printf(dtp
, fp
, tmp
)) < 0)
1371 if (pfv
->pfv_flags
& DT_PRINTF_AGGREGATION
) {
1373 * For printa(), we flush the buffer after each
1374 * prefix, setting the flags to indicate that
1375 * this is part of the printa() format string.
1377 flags
= DTRACE_BUFDATA_AGGFORMAT
;
1379 if (pfc
== NULL
&& i
== pfv
->pfv_argc
- 1)
1380 flags
|= DTRACE_BUFDATA_AGGLAST
;
1382 if (dt_buffered_flush(dtp
, NULL
, NULL
,
1383 aggdata
, flags
) < 0)
1389 if (pfv
->pfv_argc
== 1)
1390 return (nrecs
!= 0);
1395 * If the conversion is %%, just invoke the print callback
1396 * with no data record and continue; it consumes no record.
1398 if (pfc
->pfc_print
== &pfprint_pct
) {
1399 if (pfc
->pfc_print(dtp
, fp
, NULL
, pfd
, NULL
, 0, 1) >= 0)
1401 return (-1); /* errno is set for us */
1404 if (pfd
->pfd_flags
& DT_PFCONV_DYNWIDTH
) {
1405 if (dt_printf_getint(dtp
, recp
++, nrecs
--, buf
,
1407 return (-1); /* errno is set for us */
1408 pfd
->pfd_dynwidth
= width
;
1410 pfd
->pfd_dynwidth
= 0;
1413 if ((pfd
->pfd_flags
& DT_PFCONV_DYNPREC
) && dt_printf_getint(
1414 dtp
, recp
++, nrecs
--, buf
, len
, &prec
) == -1)
1415 return (-1); /* errno is set for us */
1417 if (pfd
->pfd_flags
& DT_PFCONV_AGG
) {
1419 * This should be impossible -- the compiler shouldn't
1420 * create a DT_PFCONV_AGG conversion without an
1421 * aggregation present. Still, we'd rather fail
1422 * gracefully than blow up...
1424 if (aggsdata
== NULL
)
1425 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1427 aggdata
= aggsdata
[curagg
];
1428 agg
= aggdata
->dtada_desc
;
1431 * We increment the current aggregation variable, but
1432 * not beyond the number of aggregation variables that
1433 * we're printing. This has the (desired) effect that
1434 * DT_PFCONV_AGG conversions beyond the number of
1435 * aggregation variables (re-)convert the aggregation
1436 * value of the last aggregation variable.
1438 if (curagg
< naggvars
- 1)
1441 rec
= &agg
->dtagd_rec
[aggrec
];
1442 addr
= aggdata
->dtada_data
+ rec
->dtrd_offset
;
1443 limit
= addr
+ aggdata
->dtada_size
;
1444 normal
= aggdata
->dtada_normal
;
1445 flags
= DTRACE_BUFDATA_AGGVAL
;
1448 return (dt_set_errno(dtp
, EDT_DMISMATCH
));
1450 if (pfv
->pfv_flags
& DT_PRINTF_AGGREGATION
) {
1452 * When printing aggregation keys, we always
1453 * set the aggdata to be the representative
1454 * (zeroth) aggregation. The aggdata isn't
1455 * actually used here in this case, but it is
1456 * passed to the buffer handler and must
1457 * therefore still be correct.
1459 aggdata
= aggsdata
[0];
1460 flags
= DTRACE_BUFDATA_AGGKEY
;
1465 addr
= (caddr_t
)buf
+ rec
->dtrd_offset
;
1470 size
= rec
->dtrd_size
;
1472 if (addr
+ size
> limit
) {
1473 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1474 (void *)addr
, rec
->dtrd_size
, (void *)lim
);
1475 return (dt_set_errno(dtp
, EDT_DOFFSET
));
1478 if (rec
->dtrd_alignment
!= 0 &&
1479 ((uintptr_t)addr
& (rec
->dtrd_alignment
- 1)) != 0) {
1480 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1481 (void *)addr
, rec
->dtrd_size
, rec
->dtrd_alignment
);
1482 return (dt_set_errno(dtp
, EDT_DALIGN
));
1485 switch (rec
->dtrd_action
) {
1487 func
= pfprint_average
;
1489 case DTRACEAGG_STDDEV
:
1490 func
= pfprint_stddev
;
1492 case DTRACEAGG_QUANTIZE
:
1493 func
= pfprint_quantize
;
1495 case DTRACEAGG_LQUANTIZE
:
1496 func
= pfprint_lquantize
;
1498 case DTRACEAGG_LLQUANTIZE
:
1499 func
= pfprint_llquantize
;
1504 case DTRACEACT_UMOD
:
1505 func
= pfprint_umod
;
1508 func
= pfc
->pfc_print
;
1512 if (pfd
->pfd_flags
& DT_PFCONV_ALT
)
1514 if (pfd
->pfd_flags
& DT_PFCONV_ZPAD
)
1516 if (width
< 0 || (pfd
->pfd_flags
& DT_PFCONV_LEFT
))
1518 if (pfd
->pfd_flags
& DT_PFCONV_SPOS
)
1520 if (pfd
->pfd_flags
& DT_PFCONV_GROUP
)
1522 if (pfd
->pfd_flags
& DT_PFCONV_SPACE
)
1526 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1527 * don't add the width to the format string. See the block
1528 * comment in pfprint_stack() for a description of the
1529 * behavior in this case.
1531 if (func
== pfprint_stack
&& (pfd
->pfd_flags
& DT_PFCONV_LEFT
))
1535 f
+= snprintf(f
, sizeof (format
), "%d", ABS(width
));
1538 f
+= snprintf(f
, sizeof (format
), ".%d", prec
);
1540 (void) strcpy(f
, pfd
->pfd_fmt
);
1543 if (func(dtp
, fp
, format
, pfd
, addr
, size
, normal
) < 0)
1544 return (-1); /* errno is set for us */
1546 if (pfv
->pfv_flags
& DT_PRINTF_AGGREGATION
) {
1548 * For printa(), we flush the buffer after each tuple
1549 * element, inidicating that this is the last record
1552 if (i
== pfv
->pfv_argc
- 1)
1553 flags
|= DTRACE_BUFDATA_AGGLAST
;
1555 if (dt_buffered_flush(dtp
, NULL
,
1556 rec
, aggdata
, flags
) < 0)
1561 return ((int)(recp
- recs
));
1565 dtrace_sprintf(dtrace_hdl_t
*dtp
, FILE *fp
, void *fmtdata
,
1566 const dtrace_recdesc_t
*recp
, uint_t nrecs
, const void *buf
, size_t len
)
1568 dtrace_optval_t size
;
1571 rval
= dtrace_getopt(dtp
, "strsize", &size
);
1573 assert(dtp
->dt_sprintf_buflen
== 0);
1575 if (dtp
->dt_sprintf_buf
!= NULL
)
1576 free(dtp
->dt_sprintf_buf
);
1578 if ((dtp
->dt_sprintf_buf
= malloc(size
)) == NULL
)
1579 return (dt_set_errno(dtp
, EDT_NOMEM
));
1581 bzero(dtp
->dt_sprintf_buf
, size
);
1582 dtp
->dt_sprintf_buflen
= size
;
1583 rval
= dt_printf_format(dtp
, fp
, fmtdata
, recp
, nrecs
, buf
, len
,
1585 dtp
->dt_sprintf_buflen
= 0;
1588 free(dtp
->dt_sprintf_buf
);
1595 dtrace_system(dtrace_hdl_t
*dtp
, FILE *fp
, void *fmtdata
,
1596 const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*recp
,
1597 uint_t nrecs
, const void *buf
, size_t len
)
1599 int rval
= dtrace_sprintf(dtp
, fp
, fmtdata
, recp
, nrecs
, buf
, len
);
1605 * Before we execute the specified command, flush fp to assure that
1606 * any prior dt_printf()'s appear before the output of the command
1611 if (system(dtp
->dt_sprintf_buf
) == -1)
1612 return (dt_set_errno(dtp
, errno
));
1618 dtrace_freopen(dtrace_hdl_t
*dtp
, FILE *fp
, void *fmtdata
,
1619 const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*recp
,
1620 uint_t nrecs
, const void *buf
, size_t len
)
1622 char selfbuf
[40], restorebuf
[40], *filename
;
1625 dt_pfargv_t
*pfv
= fmtdata
;
1626 dt_pfargd_t
*pfd
= pfv
->pfv_argv
;
1628 rval
= dtrace_sprintf(dtp
, fp
, fmtdata
, recp
, nrecs
, buf
, len
);
1630 if (rval
== -1 || fp
== NULL
)
1633 if (pfd
->pfd_preflen
!= 0 &&
1634 strcmp(pfd
->pfd_prefix
, DT_FREOPEN_RESTORE
) == 0) {
1636 * The only way to have the format string set to the value
1637 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1638 * denoting that we should restore the old stdout.
1640 assert(strcmp(dtp
->dt_sprintf_buf
, DT_FREOPEN_RESTORE
) == 0);
1642 if (dtp
->dt_stdout_fd
== -1) {
1644 * We could complain here by generating an error,
1645 * but it seems like overkill: it seems that calling
1646 * freopen() to restore stdout when freopen() has
1647 * never before been called should just be a no-op,
1648 * so we just return in this case.
1653 (void) snprintf(restorebuf
, sizeof (restorebuf
),
1654 "/dev/fd/%d", dtp
->dt_stdout_fd
);
1655 filename
= restorebuf
;
1657 filename
= dtp
->dt_sprintf_buf
;
1661 * freopen(3C) will always close the specified stream and underlying
1662 * file descriptor -- even if the specified file can't be opened.
1663 * Even for the semantic cesspool that is standard I/O, this is
1664 * surprisingly brain-dead behavior: it means that any failure to
1665 * open the specified file destroys the specified stream in the
1666 * process -- which is particularly relevant when the specified stream
1667 * happens (or rather, happened) to be stdout. This could be resolved
1668 * were there an "fdreopen()" equivalent of freopen() that allowed one
1669 * to pass a file descriptor instead of the name of a file, but there
1670 * is no such thing. However, we can effect this ourselves by first
1671 * fopen()'ing the desired file, and then (assuming that that works),
1672 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1673 * file descriptor for the fopen()'d file. This way, if the fopen()
1674 * fails, we can fail the operation without destroying stdout.
1676 if ((nfp
= fopen(filename
, "aF")) == NULL
) {
1677 char *msg
= strerror(errno
), *faultstr
;
1680 len
+= strlen(msg
) + strlen(filename
);
1681 faultstr
= alloca(len
);
1683 (void) snprintf(faultstr
, len
, "couldn't freopen() \"%s\": %s",
1684 filename
, strerror(errno
));
1686 if ((errval
= dt_handle_liberr(dtp
, data
, faultstr
)) == 0)
1692 (void) snprintf(selfbuf
, sizeof (selfbuf
), "/dev/fd/%d", fileno(nfp
));
1694 if (dtp
->dt_stdout_fd
== -1) {
1696 * If this is the first time that we're calling freopen(),
1697 * we're going to stash away the file descriptor for stdout.
1698 * We don't expect the dup(2) to fail, so if it does we must
1701 if ((dtp
->dt_stdout_fd
= dup(fileno(fp
))) == -1) {
1703 return (dt_set_errno(dtp
, errno
));
1707 if (freopen(selfbuf
, "aF", fp
) == NULL
) {
1709 return (dt_set_errno(dtp
, errno
));
1719 dtrace_fprintf(dtrace_hdl_t
*dtp
, FILE *fp
, void *fmtdata
,
1720 const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*recp
,
1721 uint_t nrecs
, const void *buf
, size_t len
)
1723 return (dt_printf_format(dtp
, fp
, fmtdata
,
1724 recp
, nrecs
, buf
, len
, NULL
, 0));
1728 dtrace_printf_create(dtrace_hdl_t
*dtp
, const char *s
)
1730 dt_pfargv_t
*pfv
= dt_printf_create(dtp
, s
);
1735 return (NULL
); /* errno has been set for us */
1737 pfd
= pfv
->pfv_argv
;
1739 for (i
= 0; i
< pfv
->pfv_argc
; i
++, pfd
= pfd
->pfd_next
) {
1740 const dt_pfconv_t
*pfc
= pfd
->pfd_conv
;
1746 * If the output format is not %s then we assume that we have
1747 * been given a correctly-sized format string, so we copy the
1748 * true format name including the size modifier. If the output
1749 * format is %s, then either the input format is %s as well or
1750 * it is one of our custom formats (e.g. pfprint_addr), so we
1751 * must set pfd_fmt to be the output format conversion "s".
1753 if (strcmp(pfc
->pfc_ofmt
, "s") != 0)
1754 (void) strcat(pfd
->pfd_fmt
, pfc
->pfc_name
);
1756 (void) strcat(pfd
->pfd_fmt
, pfc
->pfc_ofmt
);
1763 dtrace_printa_create(dtrace_hdl_t
*dtp
, const char *s
)
1765 dt_pfargv_t
*pfv
= dtrace_printf_create(dtp
, s
);
1768 return (NULL
); /* errno has been set for us */
1770 pfv
->pfv_flags
|= DT_PRINTF_AGGREGATION
;
1777 dtrace_printf_format(dtrace_hdl_t
*dtp
, void *fmtdata
, char *s
, size_t len
)
1779 dt_pfargv_t
*pfv
= fmtdata
;
1780 dt_pfargd_t
*pfd
= pfv
->pfv_argv
;
1783 * An upper bound on the string length is the length of the original
1784 * format string, plus three times the number of conversions (each
1785 * conversion could add up an additional "ll" and/or pfd_width digit
1786 * in the case of converting %? to %16) plus one for a terminating \0.
1788 size_t formatlen
= strlen(pfv
->pfv_format
) + 3 * pfv
->pfv_argc
+ 1;
1789 char *format
= alloca(formatlen
);
1793 for (i
= 0; i
< pfv
->pfv_argc
; i
++, pfd
= pfd
->pfd_next
) {
1794 const dt_pfconv_t
*pfc
= pfd
->pfd_conv
;
1796 int width
= pfd
->pfd_width
;
1797 int prec
= pfd
->pfd_prec
;
1799 if (pfd
->pfd_preflen
!= 0) {
1800 for (j
= 0; j
< pfd
->pfd_preflen
; j
++)
1801 *f
++ = pfd
->pfd_prefix
[j
];
1809 if (pfd
->pfd_flags
& DT_PFCONV_ALT
)
1811 if (pfd
->pfd_flags
& DT_PFCONV_ZPAD
)
1813 if (pfd
->pfd_flags
& DT_PFCONV_LEFT
)
1815 if (pfd
->pfd_flags
& DT_PFCONV_SPOS
)
1817 if (pfd
->pfd_flags
& DT_PFCONV_DYNWIDTH
)
1819 if (pfd
->pfd_flags
& DT_PFCONV_DYNPREC
) {
1823 if (pfd
->pfd_flags
& DT_PFCONV_GROUP
)
1825 if (pfd
->pfd_flags
& DT_PFCONV_SPACE
)
1827 if (pfd
->pfd_flags
& DT_PFCONV_AGG
)
1831 f
+= snprintf(f
, sizeof (format
), "%d", width
);
1834 f
+= snprintf(f
, sizeof (format
), ".%d", prec
);
1837 * If the output format is %s, then either %s is the underlying
1838 * conversion or the conversion is one of our customized ones,
1839 * e.g. pfprint_addr. In these cases, put the original string
1840 * name of the conversion (pfc_name) into the pickled format
1841 * string rather than the derived conversion (pfd_fmt).
1843 if (strcmp(pfc
->pfc_ofmt
, "s") == 0)
1844 str
= pfc
->pfc_name
;
1848 for (j
= 0; str
[j
] != '\0'; j
++)
1852 *f
= '\0'; /* insert nul byte; do not count in return value */
1854 assert(f
< format
+ formatlen
);
1855 (void) strncpy(s
, format
, len
);
1857 return ((size_t)(f
- format
));
1861 dt_fprinta(const dtrace_aggdata_t
*adp
, void *arg
)
1863 const dtrace_aggdesc_t
*agg
= adp
->dtada_desc
;
1864 const dtrace_recdesc_t
*recp
= &agg
->dtagd_rec
[0];
1865 uint_t nrecs
= agg
->dtagd_nrecs
;
1866 dt_pfwalk_t
*pfw
= arg
;
1867 dtrace_hdl_t
*dtp
= pfw
->pfw_argv
->pfv_dtp
;
1870 if (dt_printf_getint(dtp
, recp
++, nrecs
--,
1871 adp
->dtada_data
, adp
->dtada_size
, &id
) != 0 || pfw
->pfw_aid
!= id
)
1872 return (0); /* no aggregation id or id does not match */
1874 if (dt_printf_format(dtp
, pfw
->pfw_fp
, pfw
->pfw_argv
,
1875 recp
, nrecs
, adp
->dtada_data
, adp
->dtada_size
, &adp
, 1) == -1)
1876 return (pfw
->pfw_err
= dtp
->dt_errno
);
1879 * Cast away the const to set the bit indicating that this aggregation
1882 ((dtrace_aggdesc_t
*)agg
)->dtagd_flags
|= DTRACE_AGD_PRINTED
;
1888 dt_fprintas(const dtrace_aggdata_t
**aggsdata
, int naggvars
, void *arg
)
1890 const dtrace_aggdata_t
*aggdata
= aggsdata
[0];
1891 const dtrace_aggdesc_t
*agg
= aggdata
->dtada_desc
;
1892 const dtrace_recdesc_t
*rec
= &agg
->dtagd_rec
[1];
1893 uint_t nrecs
= agg
->dtagd_nrecs
- 1;
1894 dt_pfwalk_t
*pfw
= arg
;
1895 dtrace_hdl_t
*dtp
= pfw
->pfw_argv
->pfv_dtp
;
1898 if (dt_printf_format(dtp
, pfw
->pfw_fp
, pfw
->pfw_argv
,
1899 rec
, nrecs
, aggdata
->dtada_data
, aggdata
->dtada_size
,
1900 aggsdata
, naggvars
) == -1)
1901 return (pfw
->pfw_err
= dtp
->dt_errno
);
1904 * For each aggregation, indicate that it has been printed, casting
1905 * away the const as necessary.
1907 for (i
= 1; i
< naggvars
; i
++) {
1908 agg
= aggsdata
[i
]->dtada_desc
;
1909 ((dtrace_aggdesc_t
*)agg
)->dtagd_flags
|= DTRACE_AGD_PRINTED
;
1916 dtrace_fprinta(dtrace_hdl_t
*dtp
, FILE *fp
, void *fmtdata
,
1917 const dtrace_probedata_t
*data
, const dtrace_recdesc_t
*recs
,
1918 uint_t nrecs
, const void *buf
, size_t len
)
1921 int i
, naggvars
= 0;
1922 dtrace_aggvarid_t
*aggvars
;
1924 aggvars
= alloca(nrecs
* sizeof (dtrace_aggvarid_t
));
1927 * This might be a printa() with multiple aggregation variables. We
1928 * need to scan forward through the records until we find a record from
1929 * a different statement.
1931 for (i
= 0; i
< nrecs
; i
++) {
1932 const dtrace_recdesc_t
*nrec
= &recs
[i
];
1934 if (nrec
->dtrd_uarg
!= recs
->dtrd_uarg
)
1937 if (nrec
->dtrd_action
!= recs
->dtrd_action
)
1938 return (dt_set_errno(dtp
, EDT_BADAGG
));
1940 aggvars
[naggvars
++] =
1941 /* LINTED - alignment */
1942 *((dtrace_aggvarid_t
*)((caddr_t
)buf
+ nrec
->dtrd_offset
));
1946 return (dt_set_errno(dtp
, EDT_BADAGG
));
1948 pfw
.pfw_argv
= fmtdata
;
1952 if (naggvars
== 1) {
1953 pfw
.pfw_aid
= aggvars
[0];
1955 if (dtrace_aggregate_walk_sorted(dtp
,
1956 dt_fprinta
, &pfw
) == -1 || pfw
.pfw_err
!= 0)
1957 return (-1); /* errno is set for us */
1959 if (dtrace_aggregate_walk_joined(dtp
, aggvars
, naggvars
,
1960 dt_fprintas
, &pfw
) == -1 || pfw
.pfw_err
!= 0)
1961 return (-1); /* errno is set for us */