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