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