1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
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.
26 */
27
28 #if defined(sun)
29 #include <sys/sysmacros.h>
30 #else
31 #define ABS(a) ((a) < 0 ? -(a) : (a))
32 #endif
33 #include <string.h>
34 #include <strings.h>
35 #include <stdlib.h>
36 #if defined(sun)
37 #include <alloca.h>
38 #endif
39 #include <assert.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <sys/socket.h>
44 #include <netdb.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <arpa/nameser.h>
48
49 #include <dt_printf.h>
50 #include <dt_string.h>
51 #include <dt_impl.h>
52
53 /*ARGSUSED*/
54 static int
pfcheck_addr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)55 pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
56 {
57 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
58 }
59
60 /*ARGSUSED*/
61 static int
pfcheck_kaddr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)62 pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
63 {
64 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
65 dt_node_is_symaddr(dnp));
66 }
67
68 /*ARGSUSED*/
69 static int
pfcheck_uaddr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)70 pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
71 {
72 dtrace_hdl_t *dtp = pfv->pfv_dtp;
73 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
74
75 if (dt_node_is_usymaddr(dnp))
76 return (1);
77
78 if (idp == NULL || idp->di_id == 0)
79 return (0);
80
81 return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));
82 }
83
84 /*ARGSUSED*/
85 static int
pfcheck_stack(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)86 pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
87 {
88 return (dt_node_is_stack(dnp));
89 }
90
91 /*ARGSUSED*/
92 static int
pfcheck_time(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)93 pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
94 {
95 return (dt_node_is_integer(dnp) &&
96 dt_node_type_size(dnp) == sizeof (uint64_t));
97 }
98
99 /*ARGSUSED*/
100 static int
pfcheck_str(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)101 pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
102 {
103 ctf_file_t *ctfp;
104 ctf_encoding_t e;
105 ctf_arinfo_t r;
106 ctf_id_t base;
107 uint_t kind;
108
109 if (dt_node_is_string(dnp))
110 return (1);
111
112 ctfp = dnp->dn_ctfp;
113 base = ctf_type_resolve(ctfp, dnp->dn_type);
114 kind = ctf_type_kind(ctfp, base);
115
116 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
117 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
118 ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));
119 }
120
121 /*ARGSUSED*/
122 static int
pfcheck_wstr(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)123 pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
124 {
125 ctf_file_t *ctfp = dnp->dn_ctfp;
126 ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
127 uint_t kind = ctf_type_kind(ctfp, base);
128
129 ctf_encoding_t e;
130 ctf_arinfo_t r;
131
132 return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
133 (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
134 ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
135 ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);
136 }
137
138 /*ARGSUSED*/
139 static int
pfcheck_csi(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)140 pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
141 {
142 return (dt_node_is_integer(dnp) &&
143 dt_node_type_size(dnp) <= sizeof (int));
144 }
145
146 /*ARGSUSED*/
147 static int
pfcheck_fp(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)148 pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
149 {
150 return (dt_node_is_float(dnp));
151 }
152
153 /*ARGSUSED*/
154 static int
pfcheck_xint(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)155 pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
156 {
157 return (dt_node_is_integer(dnp));
158 }
159
160 /*ARGSUSED*/
161 static int
pfcheck_dint(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)162 pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
163 {
164 if (dnp->dn_flags & DT_NF_SIGNED)
165 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'i';
166 else
167 pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';
168
169 return (dt_node_is_integer(dnp));
170 }
171
172 /*ARGSUSED*/
173 static int
pfcheck_xshort(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)174 pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
175 {
176 ctf_file_t *ctfp = dnp->dn_ctfp;
177 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
178 char n[DT_TYPE_NAMELEN];
179
180 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
181 strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
182 strcmp(n, "unsigned short") == 0));
183 }
184
185 /*ARGSUSED*/
186 static int
pfcheck_xlong(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)187 pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
188 {
189 ctf_file_t *ctfp = dnp->dn_ctfp;
190 ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
191 char n[DT_TYPE_NAMELEN];
192
193 return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (
194 strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
195 strcmp(n, "unsigned long") == 0));
196 }
197
198 /*ARGSUSED*/
199 static int
pfcheck_xlonglong(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)200 pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
201 {
202 ctf_file_t *ctfp = dnp->dn_ctfp;
203 ctf_id_t type = dnp->dn_type;
204 char n[DT_TYPE_NAMELEN];
205
206 if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
207 sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||
208 strcmp(n, "signed long long") == 0 ||
209 strcmp(n, "unsigned long long") == 0))
210 return (1);
211
212 /*
213 * If the type used for %llx or %llX is not an [unsigned] long long, we
214 * also permit it to be a [u]int64_t or any typedef thereof. We know
215 * that these typedefs are guaranteed to work with %ll[xX] in either
216 * compilation environment even though they alias to "long" in LP64.
217 */
218 while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
219 if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&
220 (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
221 return (1);
222
223 type = ctf_type_reference(ctfp, type);
224 }
225
226 return (0);
227 }
228
229 /*ARGSUSED*/
230 static int
pfcheck_type(dt_pfargv_t * pfv,dt_pfargd_t * pfd,dt_node_t * dnp)231 pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
232 {
233 return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
234 dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));
235 }
236
237 /*ARGSUSED*/
238 static int
pfprint_sint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t unormal)239 pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
240 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)
241 {
242 int64_t normal = (int64_t)unormal;
243 int32_t n = (int32_t)normal;
244
245 switch (size) {
246 case sizeof (int8_t):
247 return (dt_printf(dtp, fp, format,
248 (int32_t)*((int8_t *)addr) / n));
249 case sizeof (int16_t):
250 return (dt_printf(dtp, fp, format,
251 (int32_t)*((int16_t *)addr) / n));
252 case sizeof (int32_t):
253 return (dt_printf(dtp, fp, format,
254 *((int32_t *)addr) / n));
255 case sizeof (int64_t):
256 return (dt_printf(dtp, fp, format,
257 *((int64_t *)addr) / normal));
258 default:
259 return (dt_set_errno(dtp, EDT_DMISMATCH));
260 }
261 }
262
263 /*ARGSUSED*/
264 static int
pfprint_uint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)265 pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
266 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
267 {
268 uint32_t n = (uint32_t)normal;
269
270 switch (size) {
271 case sizeof (uint8_t):
272 return (dt_printf(dtp, fp, format,
273 (uint32_t)*((uint8_t *)addr) / n));
274 case sizeof (uint16_t):
275 return (dt_printf(dtp, fp, format,
276 (uint32_t)*((uint16_t *)addr) / n));
277 case sizeof (uint32_t):
278 return (dt_printf(dtp, fp, format,
279 *((uint32_t *)addr) / n));
280 case sizeof (uint64_t):
281 return (dt_printf(dtp, fp, format,
282 *((uint64_t *)addr) / normal));
283 default:
284 return (dt_set_errno(dtp, EDT_DMISMATCH));
285 }
286 }
287
288 static int
pfprint_dint(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)289 pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
290 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
291 {
292 if (pfd->pfd_flags & DT_PFCONV_SIGNED)
293 return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));
294 else
295 return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));
296 }
297
298 /*ARGSUSED*/
299 static int
pfprint_fp(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)300 pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
301 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
302 {
303 double n = (double)normal;
304 long double ldn = (long double)normal;
305
306 switch (size) {
307 case sizeof (float):
308 return (dt_printf(dtp, fp, format,
309 (double)*((float *)addr) / n));
310 case sizeof (double):
311 return (dt_printf(dtp, fp, format,
312 *((double *)addr) / n));
313 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__mips__)
314 case sizeof (long double):
315 return (dt_printf(dtp, fp, format,
316 *((long double *)addr) / ldn));
317 #endif
318 default:
319 return (dt_set_errno(dtp, EDT_DMISMATCH));
320 }
321 }
322
323 /*ARGSUSED*/
324 static int
pfprint_addr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)325 pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
326 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
327 {
328 char *s;
329 int n, len = 256;
330 uint64_t val;
331
332 switch (size) {
333 case sizeof (uint32_t):
334 val = *((uint32_t *)addr);
335 break;
336 case sizeof (uint64_t):
337 val = *((uint64_t *)addr);
338 break;
339 default:
340 return (dt_set_errno(dtp, EDT_DMISMATCH));
341 }
342
343 do {
344 n = len;
345 s = alloca(n);
346 } while ((len = dtrace_addr2str(dtp, val, s, n)) > n);
347
348 return (dt_printf(dtp, fp, format, s));
349 }
350
351 /*ARGSUSED*/
352 static int
pfprint_mod(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)353 pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
354 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
355 {
356 return (dt_print_mod(dtp, fp, format, (caddr_t)addr));
357 }
358
359 /*ARGSUSED*/
360 static int
pfprint_umod(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)361 pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
362 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
363 {
364 return (dt_print_umod(dtp, fp, format, (caddr_t)addr));
365 }
366
367 /*ARGSUSED*/
368 static int
pfprint_uaddr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)369 pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
370 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
371 {
372 char *s;
373 int n, len = 256;
374 uint64_t val, pid = 0;
375
376 dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
377
378 switch (size) {
379 case sizeof (uint32_t):
380 val = (u_longlong_t)*((uint32_t *)addr);
381 break;
382 case sizeof (uint64_t):
383 val = (u_longlong_t)*((uint64_t *)addr);
384 break;
385 case sizeof (uint64_t) * 2:
386 pid = ((uint64_t *)(uintptr_t)addr)[0];
387 val = ((uint64_t *)(uintptr_t)addr)[1];
388 break;
389 default:
390 return (dt_set_errno(dtp, EDT_DMISMATCH));
391 }
392
393 if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)
394 pid = idp->di_id;
395
396 do {
397 n = len;
398 s = alloca(n);
399 } while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);
400
401 return (dt_printf(dtp, fp, format, s));
402 }
403
404 /*ARGSUSED*/
405 static int
pfprint_stack(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * vaddr,size_t size,uint64_t normal)406 pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
407 const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)
408 {
409 int width;
410 dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];
411 const dtrace_recdesc_t *rec = pfd->pfd_rec;
412 caddr_t addr = (caddr_t)vaddr;
413 int err = 0;
414
415 /*
416 * We have stashed the value of the STACKINDENT option, and we will
417 * now override it for the purposes of formatting the stack. If the
418 * field has been specified as left-aligned (i.e. (%-#), we set the
419 * indentation to be the width. This is a slightly odd semantic, but
420 * it's useful functionality -- and it's slightly odd to begin with to
421 * be using a single format specifier to be formatting multiple lines
422 * of text...
423 */
424 if (pfd->pfd_dynwidth < 0) {
425 assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
426 width = -pfd->pfd_dynwidth;
427 } else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
428 width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
429 } else {
430 width = 0;
431 }
432
433 dtp->dt_options[DTRACEOPT_STACKINDENT] = width;
434
435 switch (rec->dtrd_action) {
436 case DTRACEACT_USTACK:
437 case DTRACEACT_JSTACK:
438 err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);
439 break;
440
441 case DTRACEACT_STACK:
442 err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,
443 rec->dtrd_size / rec->dtrd_arg);
444 break;
445
446 default:
447 assert(0);
448 }
449
450 dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;
451
452 return (err);
453 }
454
455 /*ARGSUSED*/
456 static int
pfprint_time(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)457 pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
458 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
459 {
460 char src[32], buf[32], *dst = buf;
461 hrtime_t time = *((uint64_t *)addr);
462 time_t sec = (time_t)(time / NANOSEC);
463 int i;
464
465 /*
466 * ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".
467 * Below, we turn this into the canonical adb/mdb /[yY] format,
468 * "1973 Dec 3 17:20:00".
469 */
470 #if defined(sun)
471 (void) ctime_r(&sec, src, sizeof (src));
472 #else
473 (void) ctime_r(&sec, src);
474 #endif
475
476 /*
477 * Place the 4-digit year at the head of the string...
478 */
479 for (i = 20; i < 24; i++)
480 *dst++ = src[i];
481
482 /*
483 * ...and follow it with the remainder (month, day, hh:mm:ss).
484 */
485 for (i = 3; i < 19; i++)
486 *dst++ = src[i];
487
488 *dst = '\0';
489 return (dt_printf(dtp, fp, format, buf));
490 }
491
492 /*
493 * This prints the time in RFC 822 standard form. This is useful for emitting
494 * notions of time that are consumed by standard tools (e.g., as part of an
495 * RSS feed).
496 */
497 /*ARGSUSED*/
498 static int
pfprint_time822(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)499 pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
500 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
501 {
502 hrtime_t time = *((uint64_t *)addr);
503 time_t sec = (time_t)(time / NANOSEC);
504 struct tm tm;
505 char buf[64];
506
507 (void) localtime_r(&sec, &tm);
508 (void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);
509 return (dt_printf(dtp, fp, format, buf));
510 }
511
512 /*ARGSUSED*/
513 static int
pfprint_port(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)514 pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,
515 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
516 {
517 uint16_t port = htons(*((uint16_t *)addr));
518 char buf[256];
519 struct servent *sv, res;
520
521 #if defined(sun)
522 if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)
523 #else
524 if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)
525 #endif
526 return (dt_printf(dtp, fp, format, sv->s_name));
527
528 (void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));
529 return (dt_printf(dtp, fp, format, buf));
530 }
531
532 /*ARGSUSED*/
533 static int
pfprint_inetaddr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)534 pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
535 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
536 {
537 char *s = alloca(size + 1);
538 struct hostent *host, res;
539 char inetaddr[NS_IN6ADDRSZ];
540 char buf[1024];
541 int e;
542
543 bcopy(addr, s, size);
544 s[size] = '\0';
545
546 if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {
547 #if defined(sun)
548 if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,
549 AF_INET, &res, buf, sizeof (buf), &e)) != NULL)
550 #else
551 if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,
552 AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)
553 #endif
554 return (dt_printf(dtp, fp, format, host->h_name));
555 } else if (inet_pton(AF_INET6, s, inetaddr) != -1) {
556 if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,
557 AF_INET6, &e)) != NULL)
558 return (dt_printf(dtp, fp, format, host->h_name));
559 }
560
561 return (dt_printf(dtp, fp, format, s));
562 }
563
564 /*ARGSUSED*/
565 static int
pfprint_cstr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)566 pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
567 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
568 {
569 char *s = alloca(size + 1);
570
571 bcopy(addr, s, size);
572 s[size] = '\0';
573 return (dt_printf(dtp, fp, format, s));
574 }
575
576 /*ARGSUSED*/
577 static int
pfprint_wstr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)578 pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
579 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
580 {
581 wchar_t *ws = alloca(size + sizeof (wchar_t));
582
583 bcopy(addr, ws, size);
584 ws[size / sizeof (wchar_t)] = L'\0';
585 return (dt_printf(dtp, fp, format, ws));
586 }
587
588 /*ARGSUSED*/
589 static int
pfprint_estr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)590 pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
591 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
592 {
593 char *s;
594 int n;
595
596 if ((s = strchr2esc(addr, size)) == NULL)
597 return (dt_set_errno(dtp, EDT_NOMEM));
598
599 n = dt_printf(dtp, fp, format, s);
600 free(s);
601 return (n);
602 }
603
604 static int
pfprint_echr(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)605 pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
606 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
607 {
608 char c;
609
610 switch (size) {
611 case sizeof (int8_t):
612 c = *(int8_t *)addr;
613 break;
614 case sizeof (int16_t):
615 c = *(int16_t *)addr;
616 break;
617 case sizeof (int32_t):
618 c = *(int32_t *)addr;
619 break;
620 default:
621 return (dt_set_errno(dtp, EDT_DMISMATCH));
622 }
623
624 return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));
625 }
626
627 /*ARGSUSED*/
628 static int
pfprint_pct(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)629 pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
630 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
631 {
632 return (dt_printf(dtp, fp, "%%"));
633 }
634
635 static const char pfproto_xint[] = "char, short, int, long, or long long";
636 static const char pfproto_csi[] = "char, short, or int";
637 static const char pfproto_fp[] = "float, double, or long double";
638 static const char pfproto_addr[] = "pointer or integer";
639 static const char pfproto_uaddr[] =
640 "pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
641 static const char pfproto_cstr[] = "char [] or string (or use stringof)";
642 static const char pfproto_wstr[] = "wchar_t []";
643
644 /*
645 * Printf format conversion dictionary. This table should match the set of
646 * conversions offered by printf(3C), as well as some additional extensions.
647 * The second parameter is an ASCII string which is either an actual type
648 * name we should look up (if pfcheck_type is specified), or just a descriptive
649 * string of the types expected for use in error messages.
650 */
651 static const dt_pfconv_t _dtrace_conversions[] = {
652 { "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
653 { "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
654 { "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
655 { "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
656 { "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
657 { "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
658 { "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
659 { "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
660 { "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
661 { "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
662 { "hd", "d", "short", pfcheck_type, pfprint_sint },
663 { "hi", "i", "short", pfcheck_type, pfprint_sint },
664 { "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
665 { "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
666 { "hx", "x", "short", pfcheck_xshort, pfprint_uint },
667 { "hX", "X", "short", pfcheck_xshort, pfprint_uint },
668 { "i", "i", pfproto_xint, pfcheck_xint, pfprint_sint },
669 { "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },
670 { "k", "s", "stack", pfcheck_stack, pfprint_stack },
671 { "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
672 { "ld", "d", "long", pfcheck_type, pfprint_sint },
673 { "li", "i", "long", pfcheck_type, pfprint_sint },
674 { "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },
675 { "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
676 { "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
677 { "lx", "x", "long", pfcheck_xlong, pfprint_uint },
678 { "lX", "X", "long", pfcheck_xlong, pfprint_uint },
679 { "lld", "d", "long long", pfcheck_type, pfprint_sint },
680 { "lli", "i", "long long", pfcheck_type, pfprint_sint },
681 { "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
682 { "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
683 { "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
684 { "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
685 { "Le", "e", "long double", pfcheck_type, pfprint_fp },
686 { "LE", "E", "long double", pfcheck_type, pfprint_fp },
687 { "Lf", "f", "long double", pfcheck_type, pfprint_fp },
688 { "Lg", "g", "long double", pfcheck_type, pfprint_fp },
689 { "LG", "G", "long double", pfcheck_type, pfprint_fp },
690 { "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
691 { "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
692 { "P", "s", "uint16_t", pfcheck_type, pfprint_port },
693 { "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
694 { "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
695 { "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
696 { "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
697 #if defined(sun)
698 { "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
699 { "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
700 #else
701 { "wc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
702 { "ws", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
703 #endif
704 { "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
705 { "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
706 { "Y", "s", "int64_t", pfcheck_time, pfprint_time },
707 { "%", "%", "void", pfcheck_type, pfprint_pct },
708 { NULL, NULL, NULL, NULL, NULL }
709 };
710
711 int
dt_pfdict_create(dtrace_hdl_t * dtp)712 dt_pfdict_create(dtrace_hdl_t *dtp)
713 {
714 uint_t n = _dtrace_strbuckets;
715 const dt_pfconv_t *pfd;
716 dt_pfdict_t *pdi;
717
718 if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||
719 (pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {
720 free(pdi);
721 return (dt_set_errno(dtp, EDT_NOMEM));
722 }
723
724 dtp->dt_pfdict = pdi;
725 bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);
726 pdi->pdi_nbuckets = n;
727
728 for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
729 dtrace_typeinfo_t dtt;
730 dt_pfconv_t *pfc;
731 uint_t h;
732
733 if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {
734 dt_pfdict_destroy(dtp);
735 return (dt_set_errno(dtp, EDT_NOMEM));
736 }
737
738 bcopy(pfd, pfc, sizeof (dt_pfconv_t));
739 h = dt_strtab_hash(pfc->pfc_name, NULL) % n;
740 pfc->pfc_next = pdi->pdi_buckets[h];
741 pdi->pdi_buckets[h] = pfc;
742
743 dtt.dtt_ctfp = NULL;
744 dtt.dtt_type = CTF_ERR;
745
746 /*
747 * The "D" container or its parent must contain a definition of
748 * any type referenced by a printf conversion. If none can be
749 * found, we fail to initialize the printf dictionary.
750 */
751 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
752 dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
753 dt_pfdict_destroy(dtp);
754 return (dt_set_errno(dtp, EDT_NOCONV));
755 }
756
757 pfc->pfc_dctfp = dtt.dtt_ctfp;
758 pfc->pfc_dtype = dtt.dtt_type;
759
760 /*
761 * The "C" container may contain an alternate definition of an
762 * explicit conversion type. If it does, use it; otherwise
763 * just set pfc_ctype to pfc_dtype so it is always valid.
764 */
765 if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
766 dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
767 pfc->pfc_cctfp = dtt.dtt_ctfp;
768 pfc->pfc_ctype = dtt.dtt_type;
769 } else {
770 pfc->pfc_cctfp = pfc->pfc_dctfp;
771 pfc->pfc_ctype = pfc->pfc_dtype;
772 }
773
774 if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
775 pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
776 dt_pfdict_destroy(dtp);
777 return (dt_set_errno(dtp, EDT_BADCONV));
778 }
779
780 dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
781 }
782
783 return (0);
784 }
785
786 void
dt_pfdict_destroy(dtrace_hdl_t * dtp)787 dt_pfdict_destroy(dtrace_hdl_t *dtp)
788 {
789 dt_pfdict_t *pdi = dtp->dt_pfdict;
790 dt_pfconv_t *pfc, *nfc;
791 uint_t i;
792
793 if (pdi == NULL)
794 return;
795
796 for (i = 0; i < pdi->pdi_nbuckets; i++) {
797 for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
798 nfc = pfc->pfc_next;
799 free(pfc);
800 }
801 }
802
803 free(pdi->pdi_buckets);
804 free(pdi);
805 dtp->dt_pfdict = NULL;
806 }
807
808 static const dt_pfconv_t *
dt_pfdict_lookup(dtrace_hdl_t * dtp,const char * name)809 dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
810 {
811 dt_pfdict_t *pdi = dtp->dt_pfdict;
812 uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;
813 const dt_pfconv_t *pfc;
814
815 for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
816 if (strcmp(pfc->pfc_name, name) == 0)
817 break;
818 }
819
820 return (pfc);
821 }
822
823 static dt_pfargv_t *
dt_printf_error(dtrace_hdl_t * dtp,int err)824 dt_printf_error(dtrace_hdl_t *dtp, int err)
825 {
826 if (yypcb != NULL)
827 longjmp(yypcb->pcb_jmpbuf, err);
828
829 (void) dt_set_errno(dtp, err);
830 return (NULL);
831 }
832
833 dt_pfargv_t *
dt_printf_create(dtrace_hdl_t * dtp,const char * s)834 dt_printf_create(dtrace_hdl_t *dtp, const char *s)
835 {
836 dt_pfargd_t *pfd, *nfd = NULL;
837 dt_pfargv_t *pfv;
838 const char *p, *q;
839 char *format;
840
841 if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||
842 (format = strdup(s)) == NULL) {
843 free(pfv);
844 return (dt_printf_error(dtp, EDT_NOMEM));
845 }
846
847 pfv->pfv_format = format;
848 pfv->pfv_argv = NULL;
849 pfv->pfv_argc = 0;
850 pfv->pfv_flags = 0;
851 pfv->pfv_dtp = dtp;
852
853 for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
854 uint_t namelen = 0;
855 int digits = 0;
856 int dot = 0;
857
858 char name[8];
859 char c;
860 int n;
861
862 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
863 dt_printf_destroy(pfv);
864 return (dt_printf_error(dtp, EDT_NOMEM));
865 }
866
867 if (pfv->pfv_argv != NULL)
868 nfd->pfd_next = pfd;
869 else
870 pfv->pfv_argv = pfd;
871
872 bzero(pfd, sizeof (dt_pfargd_t));
873 pfv->pfv_argc++;
874 nfd = pfd;
875
876 if (p > q) {
877 pfd->pfd_preflen = (size_t)(p - q);
878 pfd->pfd_prefix = q;
879 }
880
881 fmt_switch:
882 switch (c = *++p) {
883 case '0': case '1': case '2': case '3': case '4':
884 case '5': case '6': case '7': case '8': case '9':
885 if (dot == 0 && digits == 0 && c == '0') {
886 pfd->pfd_flags |= DT_PFCONV_ZPAD;
887 pfd->pfd_flags &= ~DT_PFCONV_LEFT;
888 goto fmt_switch;
889 }
890
891 for (n = 0; isdigit(c); c = *++p)
892 n = n * 10 + c - '0';
893
894 if (dot)
895 pfd->pfd_prec = n;
896 else
897 pfd->pfd_width = n;
898
899 p--;
900 digits++;
901 goto fmt_switch;
902
903 case '#':
904 pfd->pfd_flags |= DT_PFCONV_ALT;
905 goto fmt_switch;
906
907 case '*':
908 n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;
909
910 if (pfd->pfd_flags & n) {
911 yywarn("format conversion #%u has more than "
912 "one '*' specified for the output %s\n",
913 pfv->pfv_argc, n ? "precision" : "width");
914
915 dt_printf_destroy(pfv);
916 return (dt_printf_error(dtp, EDT_COMPILER));
917 }
918
919 pfd->pfd_flags |= n;
920 goto fmt_switch;
921
922 case '+':
923 pfd->pfd_flags |= DT_PFCONV_SPOS;
924 goto fmt_switch;
925
926 case '-':
927 pfd->pfd_flags |= DT_PFCONV_LEFT;
928 pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
929 goto fmt_switch;
930
931 case '.':
932 if (dot++ != 0) {
933 yywarn("format conversion #%u has more than "
934 "one '.' specified\n", pfv->pfv_argc);
935
936 dt_printf_destroy(pfv);
937 return (dt_printf_error(dtp, EDT_COMPILER));
938 }
939 digits = 0;
940 goto fmt_switch;
941
942 case '?':
943 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
944 pfd->pfd_width = 16;
945 else
946 pfd->pfd_width = 8;
947 goto fmt_switch;
948
949 case '@':
950 pfd->pfd_flags |= DT_PFCONV_AGG;
951 goto fmt_switch;
952
953 case '\'':
954 pfd->pfd_flags |= DT_PFCONV_GROUP;
955 goto fmt_switch;
956
957 case ' ':
958 pfd->pfd_flags |= DT_PFCONV_SPACE;
959 goto fmt_switch;
960
961 case '$':
962 yywarn("format conversion #%u uses unsupported "
963 "positional format (%%n$)\n", pfv->pfv_argc);
964
965 dt_printf_destroy(pfv);
966 return (dt_printf_error(dtp, EDT_COMPILER));
967
968 case '%':
969 if (p[-1] == '%')
970 goto default_lbl; /* if %% then use "%" conv */
971
972 yywarn("format conversion #%u cannot be combined "
973 "with other format flags: %%%%\n", pfv->pfv_argc);
974
975 dt_printf_destroy(pfv);
976 return (dt_printf_error(dtp, EDT_COMPILER));
977
978 case '\0':
979 yywarn("format conversion #%u name expected before "
980 "end of format string\n", pfv->pfv_argc);
981
982 dt_printf_destroy(pfv);
983 return (dt_printf_error(dtp, EDT_COMPILER));
984
985 case 'h':
986 case 'l':
987 case 'L':
988 case 'w':
989 if (namelen < sizeof (name) - 2)
990 name[namelen++] = c;
991 goto fmt_switch;
992
993 default_lbl:
994 default:
995 name[namelen++] = c;
996 name[namelen] = '\0';
997 }
998
999 pfd->pfd_conv = dt_pfdict_lookup(dtp, name);
1000
1001 if (pfd->pfd_conv == NULL) {
1002 yywarn("format conversion #%u is undefined: %%%s\n",
1003 pfv->pfv_argc, name);
1004 dt_printf_destroy(pfv);
1005 return (dt_printf_error(dtp, EDT_COMPILER));
1006 }
1007 }
1008
1009 if (*q != '\0' || *format == '\0') {
1010 if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {
1011 dt_printf_destroy(pfv);
1012 return (dt_printf_error(dtp, EDT_NOMEM));
1013 }
1014
1015 if (pfv->pfv_argv != NULL)
1016 nfd->pfd_next = pfd;
1017 else
1018 pfv->pfv_argv = pfd;
1019
1020 bzero(pfd, sizeof (dt_pfargd_t));
1021 pfv->pfv_argc++;
1022
1023 pfd->pfd_prefix = q;
1024 pfd->pfd_preflen = strlen(q);
1025 }
1026
1027 return (pfv);
1028 }
1029
1030 void
dt_printf_destroy(dt_pfargv_t * pfv)1031 dt_printf_destroy(dt_pfargv_t *pfv)
1032 {
1033 dt_pfargd_t *pfd, *nfd;
1034
1035 for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
1036 nfd = pfd->pfd_next;
1037 free(pfd);
1038 }
1039
1040 free(pfv->pfv_format);
1041 free(pfv);
1042 }
1043
1044 void
dt_printf_validate(dt_pfargv_t * pfv,uint_t flags,dt_ident_t * idp,int foff,dtrace_actkind_t kind,dt_node_t * dnp)1045 dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,
1046 dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)
1047 {
1048 dt_pfargd_t *pfd = pfv->pfv_argv;
1049 const char *func = idp->di_name;
1050
1051 char n[DT_TYPE_NAMELEN];
1052 dtrace_typeinfo_t dtt;
1053 const char *aggtype;
1054 dt_node_t aggnode;
1055 int i, j;
1056
1057 if (pfv->pfv_format[0] == '\0') {
1058 xyerror(D_PRINTF_FMT_EMPTY,
1059 "%s( ) format string is empty\n", func);
1060 }
1061
1062 pfv->pfv_flags = flags;
1063
1064 /*
1065 * We fake up a parse node representing the type that can be used with
1066 * an aggregation result conversion, which -- for all but count() --
1067 * is a signed quantity.
1068 */
1069 if (kind != DTRACEAGG_COUNT)
1070 aggtype = "int64_t";
1071 else
1072 aggtype = "uint64_t";
1073
1074 if (dt_type_lookup(aggtype, &dtt) != 0)
1075 xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);
1076
1077 bzero(&aggnode, sizeof (aggnode));
1078 dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
1079
1080 for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1081 const dt_pfconv_t *pfc = pfd->pfd_conv;
1082 const char *dyns[2];
1083 int dync = 0;
1084
1085 char vname[64];
1086 dt_node_t *vnp;
1087
1088 if (pfc == NULL)
1089 continue; /* no checking if argd is just a prefix */
1090
1091 if (pfc->pfc_print == &pfprint_pct) {
1092 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1093 continue;
1094 }
1095
1096 if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
1097 dyns[dync++] = ".*";
1098 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1099 dyns[dync++] = "*";
1100
1101 for (; dync != 0; dync--) {
1102 if (dnp == NULL) {
1103 xyerror(D_PRINTF_DYN_PROTO,
1104 "%s( ) prototype mismatch: conversion "
1105 "#%d (%%%s) is missing a corresponding "
1106 "\"%s\" argument\n", func, i + 1,
1107 pfc->pfc_name, dyns[dync - 1]);
1108 }
1109
1110 if (dt_node_is_integer(dnp) == 0) {
1111 xyerror(D_PRINTF_DYN_TYPE,
1112 "%s( ) argument #%d is incompatible "
1113 "with conversion #%d prototype:\n"
1114 "\tconversion: %% %s %s\n"
1115 "\t prototype: int\n\t argument: %s\n",
1116 func, j + foff + 1, i + 1,
1117 dyns[dync - 1], pfc->pfc_name,
1118 dt_node_type_name(dnp, n, sizeof (n)));
1119 }
1120
1121 dnp = dnp->dn_list;
1122 j++;
1123 }
1124
1125 /*
1126 * If this conversion is consuming the aggregation data, set
1127 * the value node pointer (vnp) to a fake node based on the
1128 * aggregating function result type. Otherwise assign vnp to
1129 * the next parse node in the argument list, if there is one.
1130 */
1131 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1132 if (!(flags & DT_PRINTF_AGGREGATION)) {
1133 xyerror(D_PRINTF_AGG_CONV,
1134 "%%@ conversion requires an aggregation"
1135 " and is not for use with %s( )\n", func);
1136 }
1137 (void) strlcpy(vname, "aggregating action",
1138 sizeof (vname));
1139 vnp = &aggnode;
1140 } else if (dnp == NULL) {
1141 xyerror(D_PRINTF_ARG_PROTO,
1142 "%s( ) prototype mismatch: conversion #%d (%%"
1143 "%s) is missing a corresponding value argument\n",
1144 func, i + 1, pfc->pfc_name);
1145 } else {
1146 (void) snprintf(vname, sizeof (vname),
1147 "argument #%d", j + foff + 1);
1148 vnp = dnp;
1149 dnp = dnp->dn_list;
1150 j++;
1151 }
1152
1153 /*
1154 * Fill in the proposed final format string by prepending any
1155 * size-related prefixes to the pfconv's format string. The
1156 * pfc_check() function below may optionally modify the format
1157 * as part of validating the type of the input argument.
1158 */
1159 if (pfc->pfc_print == &pfprint_sint ||
1160 pfc->pfc_print == &pfprint_uint ||
1161 pfc->pfc_print == &pfprint_dint) {
1162 if (dt_node_type_size(vnp) == sizeof (uint64_t))
1163 (void) strcpy(pfd->pfd_fmt, "ll");
1164 } else if (pfc->pfc_print == &pfprint_fp) {
1165 if (dt_node_type_size(vnp) == sizeof (long double))
1166 (void) strcpy(pfd->pfd_fmt, "L");
1167 }
1168
1169 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1170
1171 /*
1172 * Validate the format conversion against the value node type.
1173 * If the conversion is good, create the descriptor format
1174 * string by concatenating together any required printf(3C)
1175 * size prefixes with the conversion's native format string.
1176 */
1177 if (pfc->pfc_check(pfv, pfd, vnp) == 0) {
1178 xyerror(D_PRINTF_ARG_TYPE,
1179 "%s( ) %s is incompatible with "
1180 "conversion #%d prototype:\n\tconversion: %%%s\n"
1181 "\t prototype: %s\n\t argument: %s\n", func,
1182 vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
1183 dt_node_type_name(vnp, n, sizeof (n)));
1184 }
1185 }
1186
1187 if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {
1188 xyerror(D_PRINTF_ARG_EXTRA,
1189 "%s( ) prototype mismatch: only %d arguments "
1190 "required by this format string\n", func, j);
1191 }
1192 }
1193
1194 void
dt_printa_validate(dt_node_t * lhs,dt_node_t * rhs)1195 dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
1196 {
1197 dt_ident_t *lid, *rid;
1198 dt_node_t *lproto, *rproto;
1199 int largc, rargc, argn;
1200 char n1[DT_TYPE_NAMELEN];
1201 char n2[DT_TYPE_NAMELEN];
1202
1203 assert(lhs->dn_kind == DT_NODE_AGG);
1204 assert(rhs->dn_kind == DT_NODE_AGG);
1205
1206 lid = lhs->dn_ident;
1207 rid = rhs->dn_ident;
1208
1209 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1210 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1211
1212 /*
1213 * First, get an argument count on each side. These must match.
1214 */
1215 for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
1216 largc++;
1217
1218 for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
1219 rargc++;
1220
1221 if (largc != rargc) {
1222 xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
1223 "matching key signatures: @%s has %d key%s, @%s has %d "
1224 "key%s", lid->di_name, rid->di_name,
1225 lid->di_name, largc, largc == 1 ? "" : "s",
1226 rid->di_name, rargc, rargc == 1 ? "" : "s");
1227 }
1228
1229 /*
1230 * Now iterate over the keys to verify that each type matches.
1231 */
1232 lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
1233 rproto = ((dt_idsig_t *)rid->di_data)->dis_args;
1234
1235 for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
1236 rproto = rproto->dn_list) {
1237 assert(rproto != NULL);
1238
1239 if (dt_node_is_argcompat(lproto, rproto))
1240 continue;
1241
1242 xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
1243 "incompatible with @%s:\n%9s key #%d: %s\n"
1244 "%9s key #%d: %s\n",
1245 rid->di_name, argn, lid->di_name, lid->di_name, argn,
1246 dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,
1247 argn, dt_node_type_name(rproto, n2, sizeof (n2)));
1248 }
1249 }
1250
1251 static int
dt_printf_getint(dtrace_hdl_t * dtp,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len,int * ip)1252 dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
1253 uint_t nrecs, const void *buf, size_t len, int *ip)
1254 {
1255 uintptr_t addr;
1256
1257 if (nrecs == 0)
1258 return (dt_set_errno(dtp, EDT_DMISMATCH));
1259
1260 addr = (uintptr_t)buf + recp->dtrd_offset;
1261
1262 if (addr + sizeof (int) > (uintptr_t)buf + len)
1263 return (dt_set_errno(dtp, EDT_DOFFSET));
1264
1265 if (addr & (recp->dtrd_alignment - 1))
1266 return (dt_set_errno(dtp, EDT_DALIGN));
1267
1268 switch (recp->dtrd_size) {
1269 case sizeof (int8_t):
1270 *ip = (int)*((int8_t *)addr);
1271 break;
1272 case sizeof (int16_t):
1273 *ip = (int)*((int16_t *)addr);
1274 break;
1275 case sizeof (int32_t):
1276 *ip = (int)*((int32_t *)addr);
1277 break;
1278 case sizeof (int64_t):
1279 *ip = (int)*((int64_t *)addr);
1280 break;
1281 default:
1282 return (dt_set_errno(dtp, EDT_DMISMATCH));
1283 }
1284
1285 return (0);
1286 }
1287
1288 /*ARGSUSED*/
1289 static int
pfprint_average(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)1290 pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1291 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1292 {
1293 const uint64_t *data = addr;
1294
1295 if (size != sizeof (uint64_t) * 2)
1296 return (dt_set_errno(dtp, EDT_DMISMATCH));
1297
1298 return (dt_printf(dtp, fp, format,
1299 data[0] ? data[1] / normal / data[0] : 0));
1300 }
1301
1302 /*ARGSUSED*/
1303 static int
pfprint_stddev(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)1304 pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1305 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1306 {
1307 const uint64_t *data = addr;
1308
1309 if (size != sizeof (uint64_t) * 4)
1310 return (dt_set_errno(dtp, EDT_DMISMATCH));
1311
1312 return (dt_printf(dtp, fp, format,
1313 dt_stddev((uint64_t *)data, normal)));
1314 }
1315
1316 /*ARGSUSED*/
1317 static int
pfprint_quantize(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)1318 pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1319 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1320 {
1321 return (dt_print_quantize(dtp, fp, addr, size, normal));
1322 }
1323
1324 /*ARGSUSED*/
1325 static int
pfprint_lquantize(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)1326 pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1327 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1328 {
1329 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1330 }
1331
1332 /*ARGSUSED*/
1333 static int
pfprint_llquantize(dtrace_hdl_t * dtp,FILE * fp,const char * format,const dt_pfargd_t * pfd,const void * addr,size_t size,uint64_t normal)1334 pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1335 const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)
1336 {
1337 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1338 }
1339
1340 static int
dt_printf_format(dtrace_hdl_t * dtp,FILE * fp,const dt_pfargv_t * pfv,const dtrace_recdesc_t * recs,uint_t nrecs,const void * buf,size_t len,const dtrace_aggdata_t ** aggsdata,int naggvars)1341 dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
1342 const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
1343 size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
1344 {
1345 dt_pfargd_t *pfd = pfv->pfv_argv;
1346 const dtrace_recdesc_t *recp = recs;
1347 const dtrace_aggdata_t *aggdata;
1348 dtrace_aggdesc_t *agg;
1349 caddr_t lim = (caddr_t)buf + len, limit;
1350 char format[64] = "%";
1351 int i, aggrec, curagg = -1;
1352 uint64_t normal;
1353
1354 /*
1355 * If we are formatting an aggregation, set 'aggrec' to the index of
1356 * the final record description (the aggregation result) so we can use
1357 * this record index with any conversion where DT_PFCONV_AGG is set.
1358 * (The actual aggregation used will vary as we increment through the
1359 * aggregation variables that we have been passed.) Finally, we
1360 * decrement nrecs to prevent this record from being used with any
1361 * other conversion.
1362 */
1363 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1364 assert(aggsdata != NULL);
1365 assert(naggvars > 0);
1366
1367 if (nrecs == 0)
1368 return (dt_set_errno(dtp, EDT_DMISMATCH));
1369
1370 curagg = naggvars > 1 ? 1 : 0;
1371 aggdata = aggsdata[0];
1372 aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;
1373 nrecs--;
1374 }
1375
1376 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1377 const dt_pfconv_t *pfc = pfd->pfd_conv;
1378 int width = pfd->pfd_width;
1379 int prec = pfd->pfd_prec;
1380 int rval;
1381
1382 char *f = format + 1; /* skip initial '%' */
1383 const dtrace_recdesc_t *rec;
1384 dt_pfprint_f *func;
1385 caddr_t addr;
1386 size_t size;
1387 uint32_t flags;
1388
1389 if (pfd->pfd_preflen != 0) {
1390 char *tmp = alloca(pfd->pfd_preflen + 1);
1391
1392 bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);
1393 tmp[pfd->pfd_preflen] = '\0';
1394
1395 if ((rval = dt_printf(dtp, fp, tmp)) < 0)
1396 return (rval);
1397
1398 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1399 /*
1400 * For printa(), we flush the buffer after each
1401 * prefix, setting the flags to indicate that
1402 * this is part of the printa() format string.
1403 */
1404 flags = DTRACE_BUFDATA_AGGFORMAT;
1405
1406 if (pfc == NULL && i == pfv->pfv_argc - 1)
1407 flags |= DTRACE_BUFDATA_AGGLAST;
1408
1409 if (dt_buffered_flush(dtp, NULL, NULL,
1410 aggdata, flags) < 0)
1411 return (-1);
1412 }
1413 }
1414
1415 if (pfc == NULL) {
1416 if (pfv->pfv_argc == 1)
1417 return (nrecs != 0);
1418 continue;
1419 }
1420
1421 /*
1422 * If the conversion is %%, just invoke the print callback
1423 * with no data record and continue; it consumes no record.
1424 */
1425 if (pfc->pfc_print == &pfprint_pct) {
1426 if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)
1427 continue;
1428 return (-1); /* errno is set for us */
1429 }
1430
1431 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
1432 if (dt_printf_getint(dtp, recp++, nrecs--, buf,
1433 len, &width) == -1)
1434 return (-1); /* errno is set for us */
1435 pfd->pfd_dynwidth = width;
1436 } else {
1437 pfd->pfd_dynwidth = 0;
1438 }
1439
1440 if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(
1441 dtp, recp++, nrecs--, buf, len, &prec) == -1)
1442 return (-1); /* errno is set for us */
1443
1444 if (pfd->pfd_flags & DT_PFCONV_AGG) {
1445 /*
1446 * This should be impossible -- the compiler shouldn't
1447 * create a DT_PFCONV_AGG conversion without an
1448 * aggregation present. Still, we'd rather fail
1449 * gracefully than blow up...
1450 */
1451 if (aggsdata == NULL)
1452 return (dt_set_errno(dtp, EDT_DMISMATCH));
1453
1454 aggdata = aggsdata[curagg];
1455 agg = aggdata->dtada_desc;
1456
1457 /*
1458 * We increment the current aggregation variable, but
1459 * not beyond the number of aggregation variables that
1460 * we're printing. This has the (desired) effect that
1461 * DT_PFCONV_AGG conversions beyond the number of
1462 * aggregation variables (re-)convert the aggregation
1463 * value of the last aggregation variable.
1464 */
1465 if (curagg < naggvars - 1)
1466 curagg++;
1467
1468 rec = &agg->dtagd_rec[aggrec];
1469 addr = aggdata->dtada_data + rec->dtrd_offset;
1470 limit = addr + aggdata->dtada_size;
1471 normal = aggdata->dtada_normal;
1472 flags = DTRACE_BUFDATA_AGGVAL;
1473 } else {
1474 if (nrecs == 0)
1475 return (dt_set_errno(dtp, EDT_DMISMATCH));
1476
1477 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1478 /*
1479 * When printing aggregation keys, we always
1480 * set the aggdata to be the representative
1481 * (zeroth) aggregation. The aggdata isn't
1482 * actually used here in this case, but it is
1483 * passed to the buffer handler and must
1484 * therefore still be correct.
1485 */
1486 aggdata = aggsdata[0];
1487 flags = DTRACE_BUFDATA_AGGKEY;
1488 }
1489
1490 rec = recp++;
1491 nrecs--;
1492 addr = (caddr_t)buf + rec->dtrd_offset;
1493 limit = lim;
1494 normal = 1;
1495 }
1496
1497 size = rec->dtrd_size;
1498
1499 if (addr + size > limit) {
1500 dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
1501 (void *)addr, rec->dtrd_size, (void *)lim);
1502 return (dt_set_errno(dtp, EDT_DOFFSET));
1503 }
1504
1505 if (rec->dtrd_alignment != 0 &&
1506 ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
1507 dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
1508 (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
1509 return (dt_set_errno(dtp, EDT_DALIGN));
1510 }
1511
1512 switch (rec->dtrd_action) {
1513 case DTRACEAGG_AVG:
1514 func = pfprint_average;
1515 break;
1516 case DTRACEAGG_STDDEV:
1517 func = pfprint_stddev;
1518 break;
1519 case DTRACEAGG_QUANTIZE:
1520 func = pfprint_quantize;
1521 break;
1522 case DTRACEAGG_LQUANTIZE:
1523 func = pfprint_lquantize;
1524 break;
1525 case DTRACEAGG_LLQUANTIZE:
1526 func = pfprint_llquantize;
1527 break;
1528 case DTRACEACT_MOD:
1529 func = pfprint_mod;
1530 break;
1531 case DTRACEACT_UMOD:
1532 func = pfprint_umod;
1533 break;
1534 default:
1535 func = pfc->pfc_print;
1536 break;
1537 }
1538
1539 if (pfd->pfd_flags & DT_PFCONV_ALT)
1540 *f++ = '#';
1541 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1542 *f++ = '0';
1543 if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
1544 *f++ = '-';
1545 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1546 *f++ = '+';
1547 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1548 *f++ = '\'';
1549 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1550 *f++ = ' ';
1551
1552 /*
1553 * If we're printing a stack and DT_PFCONV_LEFT is set, we
1554 * don't add the width to the format string. See the block
1555 * comment in pfprint_stack() for a description of the
1556 * behavior in this case.
1557 */
1558 if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
1559 width = 0;
1560
1561 if (width != 0)
1562 f += snprintf(f, sizeof (format), "%d", ABS(width));
1563
1564 if (prec > 0)
1565 f += snprintf(f, sizeof (format), ".%d", prec);
1566
1567 (void) strcpy(f, pfd->pfd_fmt);
1568 pfd->pfd_rec = rec;
1569
1570 if (func(dtp, fp, format, pfd, addr, size, normal) < 0)
1571 return (-1); /* errno is set for us */
1572
1573 if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
1574 /*
1575 * For printa(), we flush the buffer after each tuple
1576 * element, inidicating that this is the last record
1577 * as appropriate.
1578 */
1579 if (i == pfv->pfv_argc - 1)
1580 flags |= DTRACE_BUFDATA_AGGLAST;
1581
1582 if (dt_buffered_flush(dtp, NULL,
1583 rec, aggdata, flags) < 0)
1584 return (-1);
1585 }
1586 }
1587
1588 return ((int)(recp - recs));
1589 }
1590
1591 int
dtrace_sprintf(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)1592 dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1593 const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
1594 {
1595 dtrace_optval_t size;
1596 int rval;
1597
1598 rval = dtrace_getopt(dtp, "strsize", &size);
1599 assert(rval == 0);
1600 assert(dtp->dt_sprintf_buflen == 0);
1601
1602 if (dtp->dt_sprintf_buf != NULL)
1603 free(dtp->dt_sprintf_buf);
1604
1605 if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
1606 return (dt_set_errno(dtp, EDT_NOMEM));
1607
1608 bzero(dtp->dt_sprintf_buf, size);
1609 dtp->dt_sprintf_buflen = size;
1610 rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
1611 NULL, 0);
1612 dtp->dt_sprintf_buflen = 0;
1613
1614 if (rval == -1)
1615 free(dtp->dt_sprintf_buf);
1616
1617 return (rval);
1618 }
1619
1620 /*ARGSUSED*/
1621 int
dtrace_system(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)1622 dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1623 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1624 uint_t nrecs, const void *buf, size_t len)
1625 {
1626 int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1627
1628 if (rval == -1)
1629 return (rval);
1630
1631 /*
1632 * Before we execute the specified command, flush fp to assure that
1633 * any prior dt_printf()'s appear before the output of the command
1634 * not after it.
1635 */
1636 (void) fflush(fp);
1637
1638 if (system(dtp->dt_sprintf_buf) == -1)
1639 return (dt_set_errno(dtp, errno));
1640
1641 return (rval);
1642 }
1643
1644 int
dtrace_freopen(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)1645 dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1646 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1647 uint_t nrecs, const void *buf, size_t len)
1648 {
1649 char selfbuf[40], restorebuf[40], *filename;
1650 FILE *nfp;
1651 int rval, errval;
1652 dt_pfargv_t *pfv = fmtdata;
1653 dt_pfargd_t *pfd = pfv->pfv_argv;
1654
1655 rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);
1656
1657 if (rval == -1 || fp == NULL)
1658 return (rval);
1659
1660 #if defined(sun)
1661 if (pfd->pfd_preflen != 0 &&
1662 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1663 /*
1664 * The only way to have the format string set to the value
1665 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1666 * denoting that we should restore the old stdout.
1667 */
1668 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1669
1670 if (dtp->dt_stdout_fd == -1) {
1671 /*
1672 * We could complain here by generating an error,
1673 * but it seems like overkill: it seems that calling
1674 * freopen() to restore stdout when freopen() has
1675 * never before been called should just be a no-op,
1676 * so we just return in this case.
1677 */
1678 return (rval);
1679 }
1680
1681 (void) snprintf(restorebuf, sizeof (restorebuf),
1682 "/dev/fd/%d", dtp->dt_stdout_fd);
1683 filename = restorebuf;
1684 } else {
1685 filename = dtp->dt_sprintf_buf;
1686 }
1687
1688 /*
1689 * freopen(3C) will always close the specified stream and underlying
1690 * file descriptor -- even if the specified file can't be opened.
1691 * Even for the semantic cesspool that is standard I/O, this is
1692 * surprisingly brain-dead behavior: it means that any failure to
1693 * open the specified file destroys the specified stream in the
1694 * process -- which is particularly relevant when the specified stream
1695 * happens (or rather, happened) to be stdout. This could be resolved
1696 * were there an "fdreopen()" equivalent of freopen() that allowed one
1697 * to pass a file descriptor instead of the name of a file, but there
1698 * is no such thing. However, we can effect this ourselves by first
1699 * fopen()'ing the desired file, and then (assuming that that works),
1700 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
1701 * file descriptor for the fopen()'d file. This way, if the fopen()
1702 * fails, we can fail the operation without destroying stdout.
1703 */
1704 if ((nfp = fopen(filename, "aF")) == NULL) {
1705 char *msg = strerror(errno);
1706 char *faultstr;
1707 int len = 80;
1708
1709 len += strlen(msg) + strlen(filename);
1710 faultstr = alloca(len);
1711
1712 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1713 filename, strerror(errno));
1714
1715 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1716 return (rval);
1717
1718 return (errval);
1719 }
1720
1721 (void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));
1722
1723 if (dtp->dt_stdout_fd == -1) {
1724 /*
1725 * If this is the first time that we're calling freopen(),
1726 * we're going to stash away the file descriptor for stdout.
1727 * We don't expect the dup(2) to fail, so if it does we must
1728 * return failure.
1729 */
1730 if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
1731 (void) fclose(nfp);
1732 return (dt_set_errno(dtp, errno));
1733 }
1734 }
1735
1736 if (freopen(selfbuf, "aF", fp) == NULL) {
1737 (void) fclose(nfp);
1738 return (dt_set_errno(dtp, errno));
1739 }
1740
1741 (void) fclose(nfp);
1742 #else
1743 /*
1744 * The 'standard output' (which is not necessarily stdout)
1745 * treatment on FreeBSD is implemented differently than on
1746 * Solaris because FreeBSD's freopen() will attempt to re-use
1747 * the current file descriptor, causing the previous file to
1748 * be closed and thereby preventing it from be re-activated
1749 * later.
1750 *
1751 * For FreeBSD we use the concept of setting an output file
1752 * pointer in the DTrace handle if a dtrace_freopen() has
1753 * enabled another output file and we leave the caller's
1754 * file pointer untouched. If it was actually stdout, then
1755 * stdout remains open. If it was another file, then that
1756 * file remains open. While a dtrace_freopen() has activated
1757 * another file, we keep a pointer to that which we use in
1758 * the output functions by preference and only use the caller's
1759 * file pointer if no dtrace_freopen() call has been made.
1760 *
1761 * The check to see if we're re-activating the caller's
1762 * output file is much the same as on Solaris.
1763 */
1764 if (pfd->pfd_preflen != 0 &&
1765 strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
1766 /*
1767 * The only way to have the format string set to the value
1768 * DT_FREOPEN_RESTORE is via the empty freopen() string --
1769 * denoting that we should restore the old stdout.
1770 */
1771 assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);
1772
1773 if (dtp->dt_freopen_fp == NULL) {
1774 /*
1775 * We could complain here by generating an error,
1776 * but it seems like overkill: it seems that calling
1777 * freopen() to restore stdout when freopen() has
1778 * never before been called should just be a no-op,
1779 * so we just return in this case.
1780 */
1781 return (rval);
1782 }
1783
1784 /*
1785 * At this point, to re-active the original output file,
1786 * on FreeBSD we only code the current file that this
1787 * function opened previously.
1788 */
1789 (void) fclose(dtp->dt_freopen_fp);
1790 dtp->dt_freopen_fp = NULL;
1791
1792 return (rval);
1793 }
1794
1795 if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {
1796 char *msg = strerror(errno);
1797 char *faultstr;
1798 int len = 80;
1799
1800 len += strlen(msg) + strlen(dtp->dt_sprintf_buf);
1801 faultstr = alloca(len);
1802
1803 (void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",
1804 dtp->dt_sprintf_buf, strerror(errno));
1805
1806 if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
1807 return (rval);
1808
1809 return (errval);
1810 }
1811
1812 if (dtp->dt_freopen_fp != NULL)
1813 (void) fclose(dtp->dt_freopen_fp);
1814
1815 /* Remember that the output has been redirected to the new file. */
1816 dtp->dt_freopen_fp = nfp;
1817 #endif
1818
1819 return (rval);
1820 }
1821
1822 /*ARGSUSED*/
1823 int
dtrace_fprintf(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recp,uint_t nrecs,const void * buf,size_t len)1824 dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
1825 const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
1826 uint_t nrecs, const void *buf, size_t len)
1827 {
1828 return (dt_printf_format(dtp, fp, fmtdata,
1829 recp, nrecs, buf, len, NULL, 0));
1830 }
1831
1832 void *
dtrace_printf_create(dtrace_hdl_t * dtp,const char * s)1833 dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
1834 {
1835 dt_pfargv_t *pfv = dt_printf_create(dtp, s);
1836 dt_pfargd_t *pfd;
1837 int i;
1838
1839 if (pfv == NULL)
1840 return (NULL); /* errno has been set for us */
1841
1842 pfd = pfv->pfv_argv;
1843
1844 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1845 const dt_pfconv_t *pfc = pfd->pfd_conv;
1846
1847 if (pfc == NULL)
1848 continue;
1849
1850 /*
1851 * If the output format is not %s then we assume that we have
1852 * been given a correctly-sized format string, so we copy the
1853 * true format name including the size modifier. If the output
1854 * format is %s, then either the input format is %s as well or
1855 * it is one of our custom formats (e.g. pfprint_addr), so we
1856 * must set pfd_fmt to be the output format conversion "s".
1857 */
1858 if (strcmp(pfc->pfc_ofmt, "s") != 0)
1859 (void) strcat(pfd->pfd_fmt, pfc->pfc_name);
1860 else
1861 (void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
1862 }
1863
1864 return (pfv);
1865 }
1866
1867 void *
dtrace_printa_create(dtrace_hdl_t * dtp,const char * s)1868 dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
1869 {
1870 dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);
1871
1872 if (pfv == NULL)
1873 return (NULL); /* errno has been set for us */
1874
1875 pfv->pfv_flags |= DT_PRINTF_AGGREGATION;
1876
1877 return (pfv);
1878 }
1879
1880 /*ARGSUSED*/
1881 size_t
dtrace_printf_format(dtrace_hdl_t * dtp,void * fmtdata,char * s,size_t len)1882 dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
1883 {
1884 dt_pfargv_t *pfv = fmtdata;
1885 dt_pfargd_t *pfd = pfv->pfv_argv;
1886
1887 /*
1888 * An upper bound on the string length is the length of the original
1889 * format string, plus three times the number of conversions (each
1890 * conversion could add up an additional "ll" and/or pfd_width digit
1891 * in the case of converting %? to %16) plus one for a terminating \0.
1892 */
1893 size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
1894 char *format = alloca(formatlen);
1895 char *f = format;
1896 int i, j;
1897
1898 for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
1899 const dt_pfconv_t *pfc = pfd->pfd_conv;
1900 const char *str;
1901 int width = pfd->pfd_width;
1902 int prec = pfd->pfd_prec;
1903
1904 if (pfd->pfd_preflen != 0) {
1905 for (j = 0; j < pfd->pfd_preflen; j++)
1906 *f++ = pfd->pfd_prefix[j];
1907 }
1908
1909 if (pfc == NULL)
1910 continue;
1911
1912 *f++ = '%';
1913
1914 if (pfd->pfd_flags & DT_PFCONV_ALT)
1915 *f++ = '#';
1916 if (pfd->pfd_flags & DT_PFCONV_ZPAD)
1917 *f++ = '0';
1918 if (pfd->pfd_flags & DT_PFCONV_LEFT)
1919 *f++ = '-';
1920 if (pfd->pfd_flags & DT_PFCONV_SPOS)
1921 *f++ = '+';
1922 if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
1923 *f++ = '*';
1924 if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
1925 *f++ = '.';
1926 *f++ = '*';
1927 }
1928 if (pfd->pfd_flags & DT_PFCONV_GROUP)
1929 *f++ = '\'';
1930 if (pfd->pfd_flags & DT_PFCONV_SPACE)
1931 *f++ = ' ';
1932 if (pfd->pfd_flags & DT_PFCONV_AGG)
1933 *f++ = '@';
1934
1935 if (width != 0)
1936 f += snprintf(f, sizeof (format), "%d", width);
1937
1938 if (prec != 0)
1939 f += snprintf(f, sizeof (format), ".%d", prec);
1940
1941 /*
1942 * If the output format is %s, then either %s is the underlying
1943 * conversion or the conversion is one of our customized ones,
1944 * e.g. pfprint_addr. In these cases, put the original string
1945 * name of the conversion (pfc_name) into the pickled format
1946 * string rather than the derived conversion (pfd_fmt).
1947 */
1948 if (strcmp(pfc->pfc_ofmt, "s") == 0)
1949 str = pfc->pfc_name;
1950 else
1951 str = pfd->pfd_fmt;
1952
1953 for (j = 0; str[j] != '\0'; j++)
1954 *f++ = str[j];
1955 }
1956
1957 *f = '\0'; /* insert nul byte; do not count in return value */
1958
1959 assert(f < format + formatlen);
1960 (void) strncpy(s, format, len);
1961
1962 return ((size_t)(f - format));
1963 }
1964
1965 static int
dt_fprinta(const dtrace_aggdata_t * adp,void * arg)1966 dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
1967 {
1968 const dtrace_aggdesc_t *agg = adp->dtada_desc;
1969 const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];
1970 uint_t nrecs = agg->dtagd_nrecs;
1971 dt_pfwalk_t *pfw = arg;
1972 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
1973 int id;
1974
1975 if (dt_printf_getint(dtp, recp++, nrecs--,
1976 adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)
1977 return (0); /* no aggregation id or id does not match */
1978
1979 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
1980 recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)
1981 return (pfw->pfw_err = dtp->dt_errno);
1982
1983 /*
1984 * Cast away the const to set the bit indicating that this aggregation
1985 * has been printed.
1986 */
1987 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
1988
1989 return (0);
1990 }
1991
1992 static int
dt_fprintas(const dtrace_aggdata_t ** aggsdata,int naggvars,void * arg)1993 dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1994 {
1995 const dtrace_aggdata_t *aggdata = aggsdata[0];
1996 const dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1997 const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];
1998 uint_t nrecs = agg->dtagd_nrecs - 1;
1999 dt_pfwalk_t *pfw = arg;
2000 dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;
2001 int i;
2002
2003 if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,
2004 rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,
2005 aggsdata, naggvars) == -1)
2006 return (pfw->pfw_err = dtp->dt_errno);
2007
2008 /*
2009 * For each aggregation, indicate that it has been printed, casting
2010 * away the const as necessary.
2011 */
2012 for (i = 1; i < naggvars; i++) {
2013 agg = aggsdata[i]->dtada_desc;
2014 ((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
2015 }
2016
2017 return (0);
2018 }
2019 /*ARGSUSED*/
2020 int
dtrace_fprinta(dtrace_hdl_t * dtp,FILE * fp,void * fmtdata,const dtrace_probedata_t * data,const dtrace_recdesc_t * recs,uint_t nrecs,const void * buf,size_t len)2021 dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
2022 const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
2023 uint_t nrecs, const void *buf, size_t len)
2024 {
2025 dt_pfwalk_t pfw;
2026 int i, naggvars = 0;
2027 dtrace_aggvarid_t *aggvars;
2028
2029 aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));
2030
2031 /*
2032 * This might be a printa() with multiple aggregation variables. We
2033 * need to scan forward through the records until we find a record from
2034 * a different statement.
2035 */
2036 for (i = 0; i < nrecs; i++) {
2037 const dtrace_recdesc_t *nrec = &recs[i];
2038
2039 if (nrec->dtrd_uarg != recs->dtrd_uarg)
2040 break;
2041
2042 if (nrec->dtrd_action != recs->dtrd_action)
2043 return (dt_set_errno(dtp, EDT_BADAGG));
2044
2045 aggvars[naggvars++] =
2046 /* LINTED - alignment */
2047 *((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));
2048 }
2049
2050 if (naggvars == 0)
2051 return (dt_set_errno(dtp, EDT_BADAGG));
2052
2053 pfw.pfw_argv = fmtdata;
2054 pfw.pfw_fp = fp;
2055 pfw.pfw_err = 0;
2056
2057 if (naggvars == 1) {
2058 pfw.pfw_aid = aggvars[0];
2059
2060 if (dtrace_aggregate_walk_sorted(dtp,
2061 dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)
2062 return (-1); /* errno is set for us */
2063 } else {
2064 if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
2065 dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)
2066 return (-1); /* errno is set for us */
2067 }
2068
2069 return (i);
2070 }
2071