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