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