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