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