Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_printf.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.23* Copyright (c) 2013, Joyent, Inc. All rights reserved.24* Copyright (c) 2013 by Delphix. All rights reserved.25*/2627#ifdef illumos28#include <sys/sysmacros.h>29#else30#define ABS(a) ((a) < 0 ? -(a) : (a))31#endif32#include <string.h>33#include <strings.h>34#include <stdlib.h>35#ifdef illumos36#include <alloca.h>37#endif38#include <assert.h>39#include <ctype.h>40#include <errno.h>41#include <limits.h>42#include <sys/socket.h>43#include <netdb.h>44#include <netinet/in.h>45#include <arpa/inet.h>46#include <sys/byteorder.h>47#include <dt_printf.h>48#include <dt_string.h>49#include <dt_impl.h>5051#ifndef NS_IN6ADDRSZ52#define NS_IN6ADDRSZ 1653#endif5455#ifndef NS_INADDRSZ56#define NS_INADDRSZ 457#endif5859/*ARGSUSED*/60static int61pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)62{63return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));64}6566/*ARGSUSED*/67static int68pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)69{70return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||71dt_node_is_symaddr(dnp));72}7374/*ARGSUSED*/75static int76pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)77{78dtrace_hdl_t *dtp = pfv->pfv_dtp;79dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");8081if (dt_node_is_usymaddr(dnp))82return (1);8384if (idp == NULL || idp->di_id == 0)85return (0);8687return (dt_node_is_pointer(dnp) || dt_node_is_integer(dnp));88}8990/*ARGSUSED*/91static int92pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)93{94return (dt_node_is_stack(dnp));95}9697/*ARGSUSED*/98static int99pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)100{101return (dt_node_is_integer(dnp) &&102dt_node_type_size(dnp) == sizeof (uint64_t));103}104105/*ARGSUSED*/106static int107pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)108{109ctf_file_t *ctfp;110ctf_encoding_t e;111ctf_arinfo_t r;112ctf_id_t base;113uint_t kind;114115if (dt_node_is_string(dnp))116return (1);117118ctfp = dnp->dn_ctfp;119base = ctf_type_resolve(ctfp, dnp->dn_type);120kind = ctf_type_kind(ctfp, base);121122return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&123(base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&124ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e));125}126127/*ARGSUSED*/128static int129pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)130{131ctf_file_t *ctfp = dnp->dn_ctfp;132ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);133uint_t kind = ctf_type_kind(ctfp, base);134135ctf_encoding_t e;136ctf_arinfo_t r;137138return (kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&139(base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&140ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&141ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32);142}143144/*ARGSUSED*/145static int146pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)147{148return (dt_node_is_integer(dnp) &&149dt_node_type_size(dnp) <= sizeof (int));150}151152/*ARGSUSED*/153static int154pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)155{156return (dt_node_is_float(dnp));157}158159/*ARGSUSED*/160static int161pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)162{163return (dt_node_is_integer(dnp));164}165166/*ARGSUSED*/167static int168pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)169{170if (dnp->dn_flags & DT_NF_SIGNED)171pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'i';172else173pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';174175return (dt_node_is_integer(dnp));176}177178/*ARGSUSED*/179static int180pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)181{182ctf_file_t *ctfp = dnp->dn_ctfp;183ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);184char n[DT_TYPE_NAMELEN];185186return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (187strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||188strcmp(n, "unsigned short") == 0));189}190191/*ARGSUSED*/192static int193pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)194{195ctf_file_t *ctfp = dnp->dn_ctfp;196ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);197char n[DT_TYPE_NAMELEN];198199return (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL && (200strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||201strcmp(n, "unsigned long") == 0));202}203204/*ARGSUSED*/205static int206pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)207{208ctf_file_t *ctfp = dnp->dn_ctfp;209ctf_id_t type = dnp->dn_type;210char n[DT_TYPE_NAMELEN];211212if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,213sizeof (n)) != NULL && (strcmp(n, "long long") == 0 ||214strcmp(n, "signed long long") == 0 ||215strcmp(n, "unsigned long long") == 0))216return (1);217218/*219* If the type used for %llx or %llX is not an [unsigned] long long, we220* also permit it to be a [u]int64_t or any typedef thereof. We know221* that these typedefs are guaranteed to work with %ll[xX] in either222* compilation environment even though they alias to "long" in LP64.223*/224while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {225if (ctf_type_name(ctfp, type, n, sizeof (n)) != NULL &&226(strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))227return (1);228229type = ctf_type_reference(ctfp, type);230}231232return (0);233}234235/*ARGSUSED*/236static int237pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)238{239return (ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,240dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype));241}242243/*ARGSUSED*/244static int245pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,246const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t unormal)247{248int64_t normal = (int64_t)unormal;249int32_t n = (int32_t)normal;250251switch (size) {252case sizeof (int8_t):253return (dt_printf(dtp, fp, format,254(int32_t)*((int8_t *)addr) / n));255case sizeof (int16_t):256return (dt_printf(dtp, fp, format,257(int32_t)*((int16_t *)addr) / n));258case sizeof (int32_t):259return (dt_printf(dtp, fp, format,260*((int32_t *)addr) / n));261case sizeof (int64_t):262return (dt_printf(dtp, fp, format,263*((int64_t *)addr) / normal));264default:265return (dt_set_errno(dtp, EDT_DMISMATCH));266}267}268269/*ARGSUSED*/270static int271pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,272const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)273{274uint32_t n = (uint32_t)normal;275276switch (size) {277case sizeof (uint8_t):278return (dt_printf(dtp, fp, format,279(uint32_t)*((uint8_t *)addr) / n));280case sizeof (uint16_t):281return (dt_printf(dtp, fp, format,282(uint32_t)*((uint16_t *)addr) / n));283case sizeof (uint32_t):284return (dt_printf(dtp, fp, format,285*((uint32_t *)addr) / n));286case sizeof (uint64_t):287return (dt_printf(dtp, fp, format,288*((uint64_t *)addr) / normal));289default:290return (dt_set_errno(dtp, EDT_DMISMATCH));291}292}293294static int295pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,296const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)297{298if (pfd->pfd_flags & DT_PFCONV_SIGNED)299return (pfprint_sint(dtp, fp, format, pfd, addr, size, normal));300else301return (pfprint_uint(dtp, fp, format, pfd, addr, size, normal));302}303304/*ARGSUSED*/305static int306pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,307const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)308{309double n = (double)normal;310long double ldn = (long double)normal;311312switch (size) {313case sizeof (float):314return (dt_printf(dtp, fp, format,315(double)*((float *)addr) / n));316case sizeof (double):317return (dt_printf(dtp, fp, format,318*((double *)addr) / n));319#if !defined(__arm__) && !defined(__powerpc__) && !defined(__riscv)320case sizeof (long double):321return (dt_printf(dtp, fp, format,322*((long double *)addr) / ldn));323#endif324default:325return (dt_set_errno(dtp, EDT_DMISMATCH));326}327}328329/*ARGSUSED*/330static int331pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,332const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)333{334char *s;335int n, len = 256;336uint64_t val;337338switch (size) {339case sizeof (uint32_t):340val = *((uint32_t *)addr);341break;342case sizeof (uint64_t):343val = *((uint64_t *)addr);344break;345default:346return (dt_set_errno(dtp, EDT_DMISMATCH));347}348349do {350n = len;351s = alloca(n);352} while ((len = dtrace_addr2str(dtp, val, s, n)) > n);353354return (dt_printf(dtp, fp, format, s));355}356357/*ARGSUSED*/358static int359pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,360const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)361{362return (dt_print_mod(dtp, fp, format, (caddr_t)addr));363}364365/*ARGSUSED*/366static int367pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,368const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)369{370return (dt_print_umod(dtp, fp, format, (caddr_t)addr));371}372373/*ARGSUSED*/374static int375pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,376const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)377{378char *s;379int n, len = 256;380uint64_t val, pid = 0;381382dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");383384switch (size) {385case sizeof (uint32_t):386val = (u_longlong_t)*((uint32_t *)addr);387break;388case sizeof (uint64_t):389val = (u_longlong_t)*((uint64_t *)addr);390break;391case sizeof (uint64_t) * 2:392pid = ((uint64_t *)(uintptr_t)addr)[0];393val = ((uint64_t *)(uintptr_t)addr)[1];394break;395default:396return (dt_set_errno(dtp, EDT_DMISMATCH));397}398399if (pid == 0 && dtp->dt_vector == NULL && idp != NULL)400pid = idp->di_id;401402do {403n = len;404s = alloca(n);405} while ((len = dtrace_uaddr2str(dtp, pid, val, s, n)) > n);406407return (dt_printf(dtp, fp, format, s));408}409410/*ARGSUSED*/411static int412pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,413const dt_pfargd_t *pfd, const void *vaddr, size_t size, uint64_t normal)414{415int width;416dtrace_optval_t saved = dtp->dt_options[DTRACEOPT_STACKINDENT];417const dtrace_recdesc_t *rec = pfd->pfd_rec;418caddr_t addr = (caddr_t)vaddr;419int err = 0;420421/*422* We have stashed the value of the STACKINDENT option, and we will423* now override it for the purposes of formatting the stack. If the424* field has been specified as left-aligned (i.e. (%-#), we set the425* indentation to be the width. This is a slightly odd semantic, but426* it's useful functionality -- and it's slightly odd to begin with to427* be using a single format specifier to be formatting multiple lines428* of text...429*/430if (pfd->pfd_dynwidth < 0) {431assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);432width = -pfd->pfd_dynwidth;433} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {434width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;435} else {436width = 0;437}438439dtp->dt_options[DTRACEOPT_STACKINDENT] = width;440441switch (rec->dtrd_action) {442case DTRACEACT_USTACK:443case DTRACEACT_JSTACK:444err = dt_print_ustack(dtp, fp, format, addr, rec->dtrd_arg);445break;446447case DTRACEACT_STACK:448err = dt_print_stack(dtp, fp, format, addr, rec->dtrd_arg,449rec->dtrd_size / rec->dtrd_arg);450break;451452default:453assert(0);454}455456dtp->dt_options[DTRACEOPT_STACKINDENT] = saved;457458return (err);459}460461/*ARGSUSED*/462static int463pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,464const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)465{466char src[32], buf[32], *dst = buf;467hrtime_t time = *((uint64_t *)addr);468time_t sec = (time_t)(time / NANOSEC);469int i;470471/*472* ctime(3C) returns a string of the form "Dec 3 17:20:00 1973\n\0".473* Below, we turn this into the canonical adb/mdb /[yY] format,474* "1973 Dec 3 17:20:00".475*/476#ifdef illumos477(void) ctime_r(&sec, src, sizeof (src));478#else479(void) ctime_r(&sec, src);480#endif481482/*483* Place the 4-digit year at the head of the string...484*/485for (i = 20; i < 24; i++)486*dst++ = src[i];487488/*489* ...and follow it with the remainder (month, day, hh:mm:ss).490*/491for (i = 3; i < 19; i++)492*dst++ = src[i];493494*dst = '\0';495return (dt_printf(dtp, fp, format, buf));496}497498/*499* This prints the time in RFC 822 standard form. This is useful for emitting500* notions of time that are consumed by standard tools (e.g., as part of an501* RSS feed).502*/503/*ARGSUSED*/504static int505pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,506const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)507{508hrtime_t time = *((uint64_t *)addr);509time_t sec = (time_t)(time / NANOSEC);510struct tm tm;511char buf[64];512513(void) localtime_r(&sec, &tm);514(void) strftime(buf, sizeof (buf), "%a, %d %b %G %T %Z", &tm);515return (dt_printf(dtp, fp, format, buf));516}517518/*ARGSUSED*/519static int520pfprint_port(dtrace_hdl_t *dtp, FILE *fp, const char *format,521const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)522{523uint16_t port = htons(*((uint16_t *)addr));524char buf[256];525struct servent *sv, res;526527#ifdef illumos528if ((sv = getservbyport_r(port, NULL, &res, buf, sizeof (buf))) != NULL)529#else530if (getservbyport_r(port, NULL, &res, buf, sizeof (buf), &sv) > 0)531#endif532return (dt_printf(dtp, fp, format, sv->s_name));533534(void) snprintf(buf, sizeof (buf), "%d", *((uint16_t *)addr));535return (dt_printf(dtp, fp, format, buf));536}537538/*ARGSUSED*/539static int540pfprint_inetaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,541const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)542{543char *s = alloca(size + 1);544struct hostent *host, res;545char inetaddr[NS_IN6ADDRSZ];546char buf[1024];547int e;548549bcopy(addr, s, size);550s[size] = '\0';551552if (strchr(s, ':') == NULL && inet_pton(AF_INET, s, inetaddr) != -1) {553#ifdef illumos554if ((host = gethostbyaddr_r(inetaddr, NS_INADDRSZ,555AF_INET, &res, buf, sizeof (buf), &e)) != NULL)556#else557if (gethostbyaddr_r(inetaddr, NS_INADDRSZ,558AF_INET, &res, buf, sizeof (buf), &host, &e) > 0)559#endif560return (dt_printf(dtp, fp, format, host->h_name));561} else if (inet_pton(AF_INET6, s, inetaddr) != -1) {562if ((host = getipnodebyaddr(inetaddr, NS_IN6ADDRSZ,563AF_INET6, &e)) != NULL)564return (dt_printf(dtp, fp, format, host->h_name));565}566567return (dt_printf(dtp, fp, format, s));568}569570/*ARGSUSED*/571static int572pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,573const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)574{575char *s = alloca(size + 1);576577bcopy(addr, s, size);578s[size] = '\0';579return (dt_printf(dtp, fp, format, s));580}581582/*ARGSUSED*/583static int584pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,585const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)586{587wchar_t *ws = alloca(size + sizeof (wchar_t));588589bcopy(addr, ws, size);590ws[size / sizeof (wchar_t)] = L'\0';591return (dt_printf(dtp, fp, format, ws));592}593594/*ARGSUSED*/595static int596pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,597const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)598{599char *s;600int n;601602if ((s = strchr2esc(addr, size)) == NULL)603return (dt_set_errno(dtp, EDT_NOMEM));604605n = dt_printf(dtp, fp, format, s);606free(s);607return (n);608}609610static int611pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,612const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)613{614char c;615616switch (size) {617case sizeof (int8_t):618c = *(int8_t *)addr;619break;620case sizeof (int16_t):621c = *(int16_t *)addr;622break;623case sizeof (int32_t):624c = *(int32_t *)addr;625break;626default:627return (dt_set_errno(dtp, EDT_DMISMATCH));628}629630return (pfprint_estr(dtp, fp, format, pfd, &c, 1, normal));631}632633/*ARGSUSED*/634static int635pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,636const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)637{638return (dt_printf(dtp, fp, "%%"));639}640641static const char pfproto_xint[] = "char, short, int, long, or long long";642static const char pfproto_csi[] = "char, short, or int";643static const char pfproto_fp[] = "float, double, or long double";644static const char pfproto_addr[] = "pointer or integer";645static const char pfproto_uaddr[] =646"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";647static const char pfproto_cstr[] = "char [] or string (or use stringof)";648static const char pfproto_wstr[] = "wchar_t []";649650/*651* Printf format conversion dictionary. This table should match the set of652* conversions offered by printf(3C), as well as some additional extensions.653* The second parameter is an ASCII string which is either an actual type654* name we should look up (if pfcheck_type is specified), or just a descriptive655* string of the types expected for use in error messages.656*/657static const dt_pfconv_t _dtrace_conversions[] = {658{ "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },659{ "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },660{ "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },661{ "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },662{ "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },663{ "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },664{ "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },665{ "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },666{ "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },667{ "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },668{ "hd", "d", "short", pfcheck_type, pfprint_sint },669{ "hi", "i", "short", pfcheck_type, pfprint_sint },670{ "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },671{ "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },672{ "hx", "x", "short", pfcheck_xshort, pfprint_uint },673{ "hX", "X", "short", pfcheck_xshort, pfprint_uint },674{ "i", "i", pfproto_xint, pfcheck_xint, pfprint_sint },675{ "I", "s", pfproto_cstr, pfcheck_str, pfprint_inetaddr },676{ "k", "s", "stack", pfcheck_stack, pfprint_stack },677{ "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */678{ "ld", "d", "long", pfcheck_type, pfprint_sint },679{ "li", "i", "long", pfcheck_type, pfprint_sint },680{ "lo", "o", "unsigned long", pfcheck_type, pfprint_uint },681{ "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },682{ "ls", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },683{ "lx", "x", "long", pfcheck_xlong, pfprint_uint },684{ "lX", "X", "long", pfcheck_xlong, pfprint_uint },685{ "lld", "d", "long long", pfcheck_type, pfprint_sint },686{ "lli", "i", "long long", pfcheck_type, pfprint_sint },687{ "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },688{ "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },689{ "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },690{ "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },691{ "Le", "e", "long double", pfcheck_type, pfprint_fp },692{ "LE", "E", "long double", pfcheck_type, pfprint_fp },693{ "Lf", "f", "long double", pfcheck_type, pfprint_fp },694{ "Lg", "g", "long double", pfcheck_type, pfprint_fp },695{ "LG", "G", "long double", pfcheck_type, pfprint_fp },696{ "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },697{ "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },698{ "P", "s", "uint16_t", pfcheck_type, pfprint_port },699{ "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },700{ "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },701{ "T", "s", "int64_t", pfcheck_time, pfprint_time822 },702{ "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },703#ifdef illumos704{ "wc", "wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */705{ "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },706#else707{ "wc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */708{ "ws", "ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },709#endif710{ "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },711{ "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },712{ "Y", "s", "int64_t", pfcheck_time, pfprint_time },713{ "%", "%", "void", pfcheck_type, pfprint_pct },714{ NULL, NULL, NULL, NULL, NULL }715};716717int718dt_pfdict_create(dtrace_hdl_t *dtp)719{720uint_t n = _dtrace_strbuckets;721const dt_pfconv_t *pfd;722dt_pfdict_t *pdi;723724if ((pdi = malloc(sizeof (dt_pfdict_t))) == NULL ||725(pdi->pdi_buckets = malloc(sizeof (dt_pfconv_t *) * n)) == NULL) {726free(pdi);727return (dt_set_errno(dtp, EDT_NOMEM));728}729730dtp->dt_pfdict = pdi;731bzero(pdi->pdi_buckets, sizeof (dt_pfconv_t *) * n);732pdi->pdi_nbuckets = n;733734for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {735dtrace_typeinfo_t dtt;736dt_pfconv_t *pfc;737uint_t h;738739if ((pfc = malloc(sizeof (dt_pfconv_t))) == NULL) {740dt_pfdict_destroy(dtp);741return (dt_set_errno(dtp, EDT_NOMEM));742}743744bcopy(pfd, pfc, sizeof (dt_pfconv_t));745h = dt_strtab_hash(pfc->pfc_name, NULL) % n;746pfc->pfc_next = pdi->pdi_buckets[h];747pdi->pdi_buckets[h] = pfc;748749dtt.dtt_ctfp = NULL;750dtt.dtt_type = CTF_ERR;751752/*753* The "D" container or its parent must contain a definition of754* any type referenced by a printf conversion. If none can be755* found, we fail to initialize the printf dictionary.756*/757if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(758dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {759dt_pfdict_destroy(dtp);760return (dt_set_errno(dtp, EDT_NOCONV));761}762763pfc->pfc_dctfp = dtt.dtt_ctfp;764pfc->pfc_dtype = dtt.dtt_type;765766/*767* The "C" container may contain an alternate definition of an768* explicit conversion type. If it does, use it; otherwise769* just set pfc_ctype to pfc_dtype so it is always valid.770*/771if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(772dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {773pfc->pfc_cctfp = dtt.dtt_ctfp;774pfc->pfc_ctype = dtt.dtt_type;775} else {776pfc->pfc_cctfp = pfc->pfc_dctfp;777pfc->pfc_ctype = pfc->pfc_dtype;778}779780if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||781pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {782dt_pfdict_destroy(dtp);783return (dt_set_errno(dtp, EDT_BADCONV));784}785786dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);787}788789return (0);790}791792void793dt_pfdict_destroy(dtrace_hdl_t *dtp)794{795dt_pfdict_t *pdi = dtp->dt_pfdict;796dt_pfconv_t *pfc, *nfc;797uint_t i;798799if (pdi == NULL)800return;801802for (i = 0; i < pdi->pdi_nbuckets; i++) {803for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {804nfc = pfc->pfc_next;805free(pfc);806}807}808809free(pdi->pdi_buckets);810free(pdi);811dtp->dt_pfdict = NULL;812}813814static const dt_pfconv_t *815dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)816{817dt_pfdict_t *pdi = dtp->dt_pfdict;818uint_t h = dt_strtab_hash(name, NULL) % pdi->pdi_nbuckets;819const dt_pfconv_t *pfc;820821for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {822if (strcmp(pfc->pfc_name, name) == 0)823break;824}825826return (pfc);827}828829static dt_pfargv_t *830dt_printf_error(dtrace_hdl_t *dtp, int err)831{832if (yypcb != NULL)833longjmp(yypcb->pcb_jmpbuf, err);834835(void) dt_set_errno(dtp, err);836return (NULL);837}838839dt_pfargv_t *840dt_printf_create(dtrace_hdl_t *dtp, const char *s)841{842dt_pfargd_t *pfd, *nfd = NULL;843dt_pfargv_t *pfv;844const char *p, *q;845char *format;846847if ((pfv = malloc(sizeof (dt_pfargv_t))) == NULL ||848(format = strdup(s)) == NULL) {849free(pfv);850return (dt_printf_error(dtp, EDT_NOMEM));851}852853pfv->pfv_format = format;854pfv->pfv_argv = NULL;855pfv->pfv_argc = 0;856pfv->pfv_flags = 0;857pfv->pfv_dtp = dtp;858859for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {860uint_t namelen = 0;861int digits = 0;862int dot = 0;863864char name[8];865char c;866int n;867868if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {869dt_printf_destroy(pfv);870return (dt_printf_error(dtp, EDT_NOMEM));871}872873if (pfv->pfv_argv != NULL)874nfd->pfd_next = pfd;875else876pfv->pfv_argv = pfd;877878bzero(pfd, sizeof (dt_pfargd_t));879pfv->pfv_argc++;880nfd = pfd;881882if (p > q) {883pfd->pfd_preflen = (size_t)(p - q);884pfd->pfd_prefix = q;885}886887fmt_switch:888switch (c = *++p) {889case '0': case '1': case '2': case '3': case '4':890case '5': case '6': case '7': case '8': case '9':891if (dot == 0 && digits == 0 && c == '0') {892pfd->pfd_flags |= DT_PFCONV_ZPAD;893pfd->pfd_flags &= ~DT_PFCONV_LEFT;894goto fmt_switch;895}896897for (n = 0; isdigit(c); c = *++p)898n = n * 10 + c - '0';899900if (dot)901pfd->pfd_prec = n;902else903pfd->pfd_width = n;904905p--;906digits++;907goto fmt_switch;908909case '#':910pfd->pfd_flags |= DT_PFCONV_ALT;911goto fmt_switch;912913case '*':914n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;915916if (pfd->pfd_flags & n) {917yywarn("format conversion #%u has more than "918"one '*' specified for the output %s\n",919pfv->pfv_argc, n ? "precision" : "width");920921dt_printf_destroy(pfv);922return (dt_printf_error(dtp, EDT_COMPILER));923}924925pfd->pfd_flags |= n;926goto fmt_switch;927928case '+':929pfd->pfd_flags |= DT_PFCONV_SPOS;930goto fmt_switch;931932case '-':933pfd->pfd_flags |= DT_PFCONV_LEFT;934pfd->pfd_flags &= ~DT_PFCONV_ZPAD;935goto fmt_switch;936937case '.':938if (dot++ != 0) {939yywarn("format conversion #%u has more than "940"one '.' specified\n", pfv->pfv_argc);941942dt_printf_destroy(pfv);943return (dt_printf_error(dtp, EDT_COMPILER));944}945digits = 0;946goto fmt_switch;947948case '?':949if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)950pfd->pfd_width = 16;951else952pfd->pfd_width = 8;953goto fmt_switch;954955case '@':956pfd->pfd_flags |= DT_PFCONV_AGG;957goto fmt_switch;958959case '\'':960pfd->pfd_flags |= DT_PFCONV_GROUP;961goto fmt_switch;962963case ' ':964pfd->pfd_flags |= DT_PFCONV_SPACE;965goto fmt_switch;966967case '$':968yywarn("format conversion #%u uses unsupported "969"positional format (%%n$)\n", pfv->pfv_argc);970971dt_printf_destroy(pfv);972return (dt_printf_error(dtp, EDT_COMPILER));973974case '%':975if (p[-1] == '%')976goto default_lbl; /* if %% then use "%" conv */977978yywarn("format conversion #%u cannot be combined "979"with other format flags: %%%%\n", pfv->pfv_argc);980981dt_printf_destroy(pfv);982return (dt_printf_error(dtp, EDT_COMPILER));983984case '\0':985yywarn("format conversion #%u name expected before "986"end of format string\n", pfv->pfv_argc);987988dt_printf_destroy(pfv);989return (dt_printf_error(dtp, EDT_COMPILER));990991case 'h':992case 'l':993case 'L':994case 'w':995if (namelen < sizeof (name) - 2)996name[namelen++] = c;997goto fmt_switch;998999default_lbl:1000default:1001name[namelen++] = c;1002name[namelen] = '\0';1003}10041005pfd->pfd_conv = dt_pfdict_lookup(dtp, name);10061007if (pfd->pfd_conv == NULL) {1008yywarn("format conversion #%u is undefined: %%%s\n",1009pfv->pfv_argc, name);1010dt_printf_destroy(pfv);1011return (dt_printf_error(dtp, EDT_COMPILER));1012}1013}10141015if (*q != '\0' || *format == '\0') {1016if ((pfd = malloc(sizeof (dt_pfargd_t))) == NULL) {1017dt_printf_destroy(pfv);1018return (dt_printf_error(dtp, EDT_NOMEM));1019}10201021if (pfv->pfv_argv != NULL)1022nfd->pfd_next = pfd;1023else1024pfv->pfv_argv = pfd;10251026bzero(pfd, sizeof (dt_pfargd_t));1027pfv->pfv_argc++;10281029pfd->pfd_prefix = q;1030pfd->pfd_preflen = strlen(q);1031}10321033return (pfv);1034}10351036void1037dt_printf_destroy(dt_pfargv_t *pfv)1038{1039dt_pfargd_t *pfd, *nfd;10401041for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {1042nfd = pfd->pfd_next;1043free(pfd);1044}10451046free(pfv->pfv_format);1047free(pfv);1048}10491050void1051dt_printf_validate(dt_pfargv_t *pfv, uint_t flags,1052dt_ident_t *idp, int foff, dtrace_actkind_t kind, dt_node_t *dnp)1053{1054dt_pfargd_t *pfd = pfv->pfv_argv;1055const char *func = idp->di_name;10561057char n[DT_TYPE_NAMELEN];1058dtrace_typeinfo_t dtt;1059const char *aggtype;1060dt_node_t aggnode;1061int i, j;10621063if (pfv->pfv_format[0] == '\0') {1064xyerror(D_PRINTF_FMT_EMPTY,1065"%s( ) format string is empty\n", func);1066}10671068pfv->pfv_flags = flags;10691070/*1071* We fake up a parse node representing the type that can be used with1072* an aggregation result conversion, which -- for all but count() --1073* is a signed quantity.1074*/1075if (kind != DTRACEAGG_COUNT)1076aggtype = "int64_t";1077else1078aggtype = "uint64_t";10791080if (dt_type_lookup(aggtype, &dtt) != 0)1081xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n", aggtype);10821083bzero(&aggnode, sizeof (aggnode));1084dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);10851086for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {1087const dt_pfconv_t *pfc = pfd->pfd_conv;1088const char *dyns[2];1089int dync = 0;10901091char vname[64];1092dt_node_t *vnp;10931094if (pfc == NULL)1095continue; /* no checking if argd is just a prefix */10961097if (pfc->pfc_print == &pfprint_pct) {1098(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);1099continue;1100}11011102if (pfd->pfd_flags & DT_PFCONV_DYNPREC)1103dyns[dync++] = ".*";1104if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)1105dyns[dync++] = "*";11061107for (; dync != 0; dync--) {1108if (dnp == NULL) {1109xyerror(D_PRINTF_DYN_PROTO,1110"%s( ) prototype mismatch: conversion "1111"#%d (%%%s) is missing a corresponding "1112"\"%s\" argument\n", func, i + 1,1113pfc->pfc_name, dyns[dync - 1]);1114}11151116if (dt_node_is_integer(dnp) == 0) {1117xyerror(D_PRINTF_DYN_TYPE,1118"%s( ) argument #%d is incompatible "1119"with conversion #%d prototype:\n"1120"\tconversion: %% %s %s\n"1121"\t prototype: int\n\t argument: %s\n",1122func, j + foff + 1, i + 1,1123dyns[dync - 1], pfc->pfc_name,1124dt_node_type_name(dnp, n, sizeof (n)));1125}11261127dnp = dnp->dn_list;1128j++;1129}11301131/*1132* If this conversion is consuming the aggregation data, set1133* the value node pointer (vnp) to a fake node based on the1134* aggregating function result type. Otherwise assign vnp to1135* the next parse node in the argument list, if there is one.1136*/1137if (pfd->pfd_flags & DT_PFCONV_AGG) {1138if (!(flags & DT_PRINTF_AGGREGATION)) {1139xyerror(D_PRINTF_AGG_CONV,1140"%%@ conversion requires an aggregation"1141" and is not for use with %s( )\n", func);1142}1143(void) strlcpy(vname, "aggregating action",1144sizeof (vname));1145vnp = &aggnode;1146} else if (dnp == NULL) {1147xyerror(D_PRINTF_ARG_PROTO,1148"%s( ) prototype mismatch: conversion #%d (%%"1149"%s) is missing a corresponding value argument\n",1150func, i + 1, pfc->pfc_name);1151} else {1152(void) snprintf(vname, sizeof (vname),1153"argument #%d", j + foff + 1);1154vnp = dnp;1155dnp = dnp->dn_list;1156j++;1157}11581159/*1160* Fill in the proposed final format string by prepending any1161* size-related prefixes to the pfconv's format string. The1162* pfc_check() function below may optionally modify the format1163* as part of validating the type of the input argument.1164*/1165if (pfc->pfc_print == &pfprint_sint ||1166pfc->pfc_print == &pfprint_uint ||1167pfc->pfc_print == &pfprint_dint) {1168if (dt_node_type_size(vnp) == sizeof (uint64_t))1169(void) strcpy(pfd->pfd_fmt, "ll");1170} else if (pfc->pfc_print == &pfprint_fp) {1171if (dt_node_type_size(vnp) == sizeof (long double))1172(void) strcpy(pfd->pfd_fmt, "L");1173}11741175(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);11761177/*1178* Validate the format conversion against the value node type.1179* If the conversion is good, create the descriptor format1180* string by concatenating together any required printf(3C)1181* size prefixes with the conversion's native format string.1182*/1183if (pfc->pfc_check(pfv, pfd, vnp) == 0) {1184xyerror(D_PRINTF_ARG_TYPE,1185"%s( ) %s is incompatible with "1186"conversion #%d prototype:\n\tconversion: %%%s\n"1187"\t prototype: %s\n\t argument: %s\n", func,1188vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,1189dt_node_type_name(vnp, n, sizeof (n)));1190}1191}11921193if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL) {1194xyerror(D_PRINTF_ARG_EXTRA,1195"%s( ) prototype mismatch: only %d arguments "1196"required by this format string\n", func, j);1197}1198}11991200void1201dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)1202{1203dt_ident_t *lid, *rid;1204dt_node_t *lproto, *rproto;1205int largc, rargc, argn;1206char n1[DT_TYPE_NAMELEN];1207char n2[DT_TYPE_NAMELEN];12081209assert(lhs->dn_kind == DT_NODE_AGG);1210assert(rhs->dn_kind == DT_NODE_AGG);12111212lid = lhs->dn_ident;1213rid = rhs->dn_ident;12141215lproto = ((dt_idsig_t *)lid->di_data)->dis_args;1216rproto = ((dt_idsig_t *)rid->di_data)->dis_args;12171218/*1219* First, get an argument count on each side. These must match.1220*/1221for (largc = 0; lproto != NULL; lproto = lproto->dn_list)1222largc++;12231224for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)1225rargc++;12261227if (largc != rargc) {1228xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "1229"matching key signatures: @%s has %d key%s, @%s has %d "1230"key%s", lid->di_name, rid->di_name,1231lid->di_name, largc, largc == 1 ? "" : "s",1232rid->di_name, rargc, rargc == 1 ? "" : "s");1233}12341235/*1236* Now iterate over the keys to verify that each type matches.1237*/1238lproto = ((dt_idsig_t *)lid->di_data)->dis_args;1239rproto = ((dt_idsig_t *)rid->di_data)->dis_args;12401241for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,1242rproto = rproto->dn_list) {1243assert(rproto != NULL);12441245if (dt_node_is_argcompat(lproto, rproto))1246continue;12471248xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "1249"incompatible with @%s:\n%9s key #%d: %s\n"1250"%9s key #%d: %s\n",1251rid->di_name, argn, lid->di_name, lid->di_name, argn,1252dt_node_type_name(lproto, n1, sizeof (n1)), rid->di_name,1253argn, dt_node_type_name(rproto, n2, sizeof (n2)));1254}1255}12561257static int1258dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,1259uint_t nrecs, const void *buf, size_t len, int *ip)1260{1261uintptr_t addr;12621263if (nrecs == 0)1264return (dt_set_errno(dtp, EDT_DMISMATCH));12651266addr = (uintptr_t)buf + recp->dtrd_offset;12671268if (addr + sizeof (int) > (uintptr_t)buf + len)1269return (dt_set_errno(dtp, EDT_DOFFSET));12701271if (addr & (recp->dtrd_alignment - 1))1272return (dt_set_errno(dtp, EDT_DALIGN));12731274switch (recp->dtrd_size) {1275case sizeof (int8_t):1276*ip = (int)*((int8_t *)addr);1277break;1278case sizeof (int16_t):1279*ip = (int)*((int16_t *)addr);1280break;1281case sizeof (int32_t):1282*ip = (int)*((int32_t *)addr);1283break;1284case sizeof (int64_t):1285*ip = (int)*((int64_t *)addr);1286break;1287default:1288return (dt_set_errno(dtp, EDT_DMISMATCH));1289}12901291return (0);1292}12931294/*ARGSUSED*/1295static int1296pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,1297const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)1298{1299const uint64_t *data = addr;13001301if (size != sizeof (uint64_t) * 2)1302return (dt_set_errno(dtp, EDT_DMISMATCH));13031304return (dt_printf(dtp, fp, format,1305data[0] ? data[1] / normal / data[0] : 0));1306}13071308/*ARGSUSED*/1309static int1310pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,1311const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)1312{1313const uint64_t *data = addr;13141315if (size != sizeof (uint64_t) * 4)1316return (dt_set_errno(dtp, EDT_DMISMATCH));13171318return (dt_printf(dtp, fp, format,1319dt_stddev((uint64_t *)data, normal)));1320}13211322/*ARGSUSED*/1323static int1324pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,1325const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)1326{1327return (dt_print_quantize(dtp, fp, addr, size, normal));1328}13291330/*ARGSUSED*/1331static int1332pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,1333const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)1334{1335return (dt_print_lquantize(dtp, fp, addr, size, normal));1336}13371338/*ARGSUSED*/1339static int1340pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,1341const dt_pfargd_t *pfd, const void *addr, size_t size, uint64_t normal)1342{1343return (dt_print_llquantize(dtp, fp, addr, size, normal));1344}13451346static int1347dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,1348const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,1349size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)1350{1351dt_pfargd_t *pfd = pfv->pfv_argv;1352const dtrace_recdesc_t *recp = recs;1353const dtrace_aggdata_t *aggdata;1354dtrace_aggdesc_t *agg;1355caddr_t lim = (caddr_t)buf + len, limit;1356char format[64] = "%";1357size_t ret;1358int i, aggrec, curagg = -1;1359uint64_t normal;13601361/*1362* If we are formatting an aggregation, set 'aggrec' to the index of1363* the final record description (the aggregation result) so we can use1364* this record index with any conversion where DT_PFCONV_AGG is set.1365* (The actual aggregation used will vary as we increment through the1366* aggregation variables that we have been passed.) Finally, we1367* decrement nrecs to prevent this record from being used with any1368* other conversion.1369*/1370if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {1371assert(aggsdata != NULL);1372assert(naggvars > 0);13731374if (nrecs == 0)1375return (dt_set_errno(dtp, EDT_DMISMATCH));13761377curagg = naggvars > 1 ? 1 : 0;1378aggdata = aggsdata[0];1379aggrec = aggdata->dtada_desc->dtagd_nrecs - 1;1380nrecs--;1381}13821383for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {1384const dt_pfconv_t *pfc = pfd->pfd_conv;1385int width = pfd->pfd_width;1386int prec = pfd->pfd_prec;1387int rval;13881389const char *start;1390char *f = format + 1; /* skip initial '%' */1391size_t fmtsz = sizeof(format) - 1;1392const dtrace_recdesc_t *rec;1393dt_pfprint_f *func;1394caddr_t addr;1395size_t size;1396uint32_t flags;13971398if (pfd->pfd_preflen != 0) {1399char *tmp = alloca(pfd->pfd_preflen + 1);14001401bcopy(pfd->pfd_prefix, tmp, pfd->pfd_preflen);1402tmp[pfd->pfd_preflen] = '\0';14031404if ((rval = dt_printf(dtp, fp, tmp)) < 0)1405return (rval);14061407if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {1408/*1409* For printa(), we flush the buffer after each1410* prefix, setting the flags to indicate that1411* this is part of the printa() format string.1412*/1413flags = DTRACE_BUFDATA_AGGFORMAT;14141415if (pfc == NULL && i == pfv->pfv_argc - 1)1416flags |= DTRACE_BUFDATA_AGGLAST;14171418if (dt_buffered_flush(dtp, NULL, NULL,1419aggdata, flags) < 0)1420return (-1);1421}1422}14231424if (pfc == NULL) {1425if (pfv->pfv_argc == 1)1426return (nrecs != 0);1427continue;1428}14291430/*1431* If the conversion is %%, just invoke the print callback1432* with no data record and continue; it consumes no record.1433*/1434if (pfc->pfc_print == &pfprint_pct) {1435if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1) >= 0)1436continue;1437return (-1); /* errno is set for us */1438}14391440if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {1441if (dt_printf_getint(dtp, recp++, nrecs--, buf,1442len, &width) == -1)1443return (-1); /* errno is set for us */1444pfd->pfd_dynwidth = width;1445} else {1446pfd->pfd_dynwidth = 0;1447}14481449if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) && dt_printf_getint(1450dtp, recp++, nrecs--, buf, len, &prec) == -1)1451return (-1); /* errno is set for us */14521453if (pfd->pfd_flags & DT_PFCONV_AGG) {1454/*1455* This should be impossible -- the compiler shouldn't1456* create a DT_PFCONV_AGG conversion without an1457* aggregation present. Still, we'd rather fail1458* gracefully than blow up...1459*/1460if (aggsdata == NULL)1461return (dt_set_errno(dtp, EDT_DMISMATCH));14621463aggdata = aggsdata[curagg];1464agg = aggdata->dtada_desc;14651466/*1467* We increment the current aggregation variable, but1468* not beyond the number of aggregation variables that1469* we're printing. This has the (desired) effect that1470* DT_PFCONV_AGG conversions beyond the number of1471* aggregation variables (re-)convert the aggregation1472* value of the last aggregation variable.1473*/1474if (curagg < naggvars - 1)1475curagg++;14761477rec = &agg->dtagd_rec[aggrec];1478addr = aggdata->dtada_data + rec->dtrd_offset;1479limit = addr + aggdata->dtada_size;1480normal = aggdata->dtada_normal;1481flags = DTRACE_BUFDATA_AGGVAL;1482} else {1483if (nrecs == 0)1484return (dt_set_errno(dtp, EDT_DMISMATCH));14851486if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {1487/*1488* When printing aggregation keys, we always1489* set the aggdata to be the representative1490* (zeroth) aggregation. The aggdata isn't1491* actually used here in this case, but it is1492* passed to the buffer handler and must1493* therefore still be correct.1494*/1495aggdata = aggsdata[0];1496flags = DTRACE_BUFDATA_AGGKEY;1497}14981499rec = recp++;1500nrecs--;1501addr = (caddr_t)buf + rec->dtrd_offset;1502limit = lim;1503normal = 1;1504}15051506size = rec->dtrd_size;15071508if (addr + size > limit) {1509dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",1510(void *)addr, rec->dtrd_size, (void *)lim);1511return (dt_set_errno(dtp, EDT_DOFFSET));1512}15131514if (rec->dtrd_alignment != 0 &&1515((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {1516dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",1517(void *)addr, rec->dtrd_size, rec->dtrd_alignment);1518return (dt_set_errno(dtp, EDT_DALIGN));1519}15201521switch (rec->dtrd_action) {1522case DTRACEAGG_AVG:1523func = pfprint_average;1524break;1525case DTRACEAGG_STDDEV:1526func = pfprint_stddev;1527break;1528case DTRACEAGG_QUANTIZE:1529func = pfprint_quantize;1530break;1531case DTRACEAGG_LQUANTIZE:1532func = pfprint_lquantize;1533break;1534case DTRACEAGG_LLQUANTIZE:1535func = pfprint_llquantize;1536break;1537case DTRACEACT_MOD:1538func = pfprint_mod;1539break;1540case DTRACEACT_UMOD:1541func = pfprint_umod;1542break;1543default:1544func = pfc->pfc_print;1545break;1546}15471548start = f;1549if (pfd->pfd_flags & DT_PFCONV_ALT)1550*f++ = '#';1551if (pfd->pfd_flags & DT_PFCONV_ZPAD)1552*f++ = '0';1553if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))1554*f++ = '-';1555if (pfd->pfd_flags & DT_PFCONV_SPOS)1556*f++ = '+';1557if (pfd->pfd_flags & DT_PFCONV_GROUP)1558*f++ = '\'';1559if (pfd->pfd_flags & DT_PFCONV_SPACE)1560*f++ = ' ';1561fmtsz -= f - start;15621563/*1564* If we're printing a stack and DT_PFCONV_LEFT is set, we1565* don't add the width to the format string. See the block1566* comment in pfprint_stack() for a description of the1567* behavior in this case.1568*/1569if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))1570width = 0;15711572if (width != 0) {1573ret = snprintf(f, fmtsz, "%d", ABS(width));1574f += ret;1575fmtsz = MAX(0, fmtsz - ret);1576}15771578if (prec > 0) {1579ret = snprintf(f, fmtsz, ".%d", prec);1580f += ret;1581fmtsz = MAX(0, fmtsz - ret);1582}15831584if (strlcpy(f, pfd->pfd_fmt, fmtsz) >= fmtsz)1585return (dt_set_errno(dtp, EDT_COMPILER));1586pfd->pfd_rec = rec;15871588if (func(dtp, fp, format, pfd, addr, size, normal) < 0)1589return (-1); /* errno is set for us */15901591if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {1592/*1593* For printa(), we flush the buffer after each tuple1594* element, inidicating that this is the last record1595* as appropriate.1596*/1597if (i == pfv->pfv_argc - 1)1598flags |= DTRACE_BUFDATA_AGGLAST;15991600if (dt_buffered_flush(dtp, NULL,1601rec, aggdata, flags) < 0)1602return (-1);1603}1604}16051606return ((int)(recp - recs));1607}16081609int1610dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,1611const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)1612{1613dtrace_optval_t size;1614int rval;16151616rval = dtrace_getopt(dtp, "strsize", &size);1617assert(rval == 0);1618assert(dtp->dt_sprintf_buflen == 0);16191620if (dtp->dt_sprintf_buf != NULL)1621free(dtp->dt_sprintf_buf);16221623if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)1624return (dt_set_errno(dtp, EDT_NOMEM));16251626bzero(dtp->dt_sprintf_buf, size);1627dtp->dt_sprintf_buflen = size;1628rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,1629NULL, 0);1630dtp->dt_sprintf_buflen = 0;16311632if (rval == -1)1633free(dtp->dt_sprintf_buf);16341635return (rval);1636}16371638/*ARGSUSED*/1639int1640dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,1641const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,1642uint_t nrecs, const void *buf, size_t len)1643{1644int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);16451646if (rval == -1)1647return (rval);16481649/*1650* Before we execute the specified command, flush fp to assure that1651* any prior dt_printf()'s appear before the output of the command1652* not after it.1653*/1654(void) fflush(fp);16551656if (system(dtp->dt_sprintf_buf) == -1)1657return (dt_set_errno(dtp, errno));16581659return (rval);1660}16611662int1663dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,1664const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,1665uint_t nrecs, const void *buf, size_t len)1666{1667char selfbuf[40], restorebuf[40], *filename;1668FILE *nfp;1669int rval, errval;1670dt_pfargv_t *pfv = fmtdata;1671dt_pfargd_t *pfd = pfv->pfv_argv;16721673rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);16741675if (rval == -1 || fp == NULL)1676return (rval);16771678#ifdef illumos1679if (pfd->pfd_preflen != 0 &&1680strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {1681/*1682* The only way to have the format string set to the value1683* DT_FREOPEN_RESTORE is via the empty freopen() string --1684* denoting that we should restore the old stdout.1685*/1686assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);16871688if (dtp->dt_stdout_fd == -1) {1689/*1690* We could complain here by generating an error,1691* but it seems like overkill: it seems that calling1692* freopen() to restore stdout when freopen() has1693* never before been called should just be a no-op,1694* so we just return in this case.1695*/1696return (rval);1697}16981699(void) snprintf(restorebuf, sizeof (restorebuf),1700"/dev/fd/%d", dtp->dt_stdout_fd);1701filename = restorebuf;1702} else {1703filename = dtp->dt_sprintf_buf;1704}17051706/*1707* freopen(3C) will always close the specified stream and underlying1708* file descriptor -- even if the specified file can't be opened.1709* Even for the semantic cesspool that is standard I/O, this is1710* surprisingly brain-dead behavior: it means that any failure to1711* open the specified file destroys the specified stream in the1712* process -- which is particularly relevant when the specified stream1713* happens (or rather, happened) to be stdout. This could be resolved1714* were there an "fdreopen()" equivalent of freopen() that allowed one1715* to pass a file descriptor instead of the name of a file, but there1716* is no such thing. However, we can effect this ourselves by first1717* fopen()'ing the desired file, and then (assuming that that works),1718* freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying1719* file descriptor for the fopen()'d file. This way, if the fopen()1720* fails, we can fail the operation without destroying stdout.1721*/1722if ((nfp = fopen(filename, "aF")) == NULL) {1723char *msg = strerror(errno);1724char *faultstr;1725int len = 80;17261727len += strlen(msg) + strlen(filename);1728faultstr = alloca(len);17291730(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",1731filename, strerror(errno));17321733if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)1734return (rval);17351736return (errval);1737}17381739(void) snprintf(selfbuf, sizeof (selfbuf), "/dev/fd/%d", fileno(nfp));17401741if (dtp->dt_stdout_fd == -1) {1742/*1743* If this is the first time that we're calling freopen(),1744* we're going to stash away the file descriptor for stdout.1745* We don't expect the dup(2) to fail, so if it does we must1746* return failure.1747*/1748if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {1749(void) fclose(nfp);1750return (dt_set_errno(dtp, errno));1751}1752}17531754if (freopen(selfbuf, "aF", fp) == NULL) {1755(void) fclose(nfp);1756return (dt_set_errno(dtp, errno));1757}17581759(void) fclose(nfp);1760#else /* !illumos */1761/*1762* The 'standard output' (which is not necessarily stdout)1763* treatment on FreeBSD is implemented differently than on1764* Solaris because FreeBSD's freopen() will attempt to re-use1765* the current file descriptor, causing the previous file to1766* be closed and thereby preventing it from be re-activated1767* later.1768*1769* For FreeBSD we use the concept of setting an output file1770* pointer in the DTrace handle if a dtrace_freopen() has1771* enabled another output file and we leave the caller's1772* file pointer untouched. If it was actually stdout, then1773* stdout remains open. If it was another file, then that1774* file remains open. While a dtrace_freopen() has activated1775* another file, we keep a pointer to that which we use in1776* the output functions by preference and only use the caller's1777* file pointer if no dtrace_freopen() call has been made.1778*1779* The check to see if we're re-activating the caller's1780* output file is much the same as on Solaris.1781*/1782if (pfd->pfd_preflen != 0 &&1783strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {1784/*1785* The only way to have the format string set to the value1786* DT_FREOPEN_RESTORE is via the empty freopen() string --1787* denoting that we should restore the old stdout.1788*/1789assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);17901791if (dtp->dt_freopen_fp == NULL) {1792/*1793* We could complain here by generating an error,1794* but it seems like overkill: it seems that calling1795* freopen() to restore stdout when freopen() has1796* never before been called should just be a no-op,1797* so we just return in this case.1798*/1799return (rval);1800}18011802/*1803* At this point, to re-active the original output file,1804* on FreeBSD we only code the current file that this1805* function opened previously.1806*/1807(void) fclose(dtp->dt_freopen_fp);1808dtp->dt_freopen_fp = NULL;18091810return (rval);1811}18121813if ((nfp = fopen(dtp->dt_sprintf_buf, "a")) == NULL) {1814char *msg = strerror(errno);1815char *faultstr;1816int len = 80;18171818len += strlen(msg) + strlen(dtp->dt_sprintf_buf);1819faultstr = alloca(len);18201821(void) snprintf(faultstr, len, "couldn't freopen() \"%s\": %s",1822dtp->dt_sprintf_buf, strerror(errno));18231824if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)1825return (rval);18261827return (errval);1828}18291830if (dtp->dt_freopen_fp != NULL)1831(void) fclose(dtp->dt_freopen_fp);18321833/* Remember that the output has been redirected to the new file. */1834dtp->dt_freopen_fp = nfp;1835#endif /* illumos */18361837return (rval);1838}18391840/*ARGSUSED*/1841int1842dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,1843const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,1844uint_t nrecs, const void *buf, size_t len)1845{1846return (dt_printf_format(dtp, fp, fmtdata,1847recp, nrecs, buf, len, NULL, 0));1848}18491850void *1851dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)1852{1853dt_pfargv_t *pfv = dt_printf_create(dtp, s);1854dt_pfargd_t *pfd;1855int i;18561857if (pfv == NULL)1858return (NULL); /* errno has been set for us */18591860pfd = pfv->pfv_argv;18611862for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {1863const dt_pfconv_t *pfc = pfd->pfd_conv;18641865if (pfc == NULL)1866continue;18671868/*1869* If the output format is not %s then we assume that we have1870* been given a correctly-sized format string, so we copy the1871* true format name including the size modifier. If the output1872* format is %s, then either the input format is %s as well or1873* it is one of our custom formats (e.g. pfprint_addr), so we1874* must set pfd_fmt to be the output format conversion "s".1875*/1876if (strcmp(pfc->pfc_ofmt, "s") != 0)1877(void) strcat(pfd->pfd_fmt, pfc->pfc_name);1878else1879(void) strcat(pfd->pfd_fmt, pfc->pfc_ofmt);1880}18811882return (pfv);1883}18841885void *1886dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)1887{1888dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);18891890if (pfv == NULL)1891return (NULL); /* errno has been set for us */18921893pfv->pfv_flags |= DT_PRINTF_AGGREGATION;18941895return (pfv);1896}18971898/*ARGSUSED*/1899size_t1900dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)1901{1902dt_pfargv_t *pfv = fmtdata;1903dt_pfargd_t *pfd = pfv->pfv_argv;19041905/*1906* An upper bound on the string length is the length of the original1907* format string, plus three times the number of conversions (each1908* conversion could add up an additional "ll" and/or pfd_width digit1909* in the case of converting %? to %16) plus one for a terminating \0.1910*/1911size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;1912char *format = alloca(formatlen);1913char *f = format;1914int i, j;19151916for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {1917const dt_pfconv_t *pfc = pfd->pfd_conv;1918const char *str;1919int width = pfd->pfd_width;1920int prec = pfd->pfd_prec;19211922if (pfd->pfd_preflen != 0) {1923for (j = 0; j < pfd->pfd_preflen; j++)1924*f++ = pfd->pfd_prefix[j];1925}19261927if (pfc == NULL)1928continue;19291930*f++ = '%';19311932if (pfd->pfd_flags & DT_PFCONV_ALT)1933*f++ = '#';1934if (pfd->pfd_flags & DT_PFCONV_ZPAD)1935*f++ = '0';1936if (pfd->pfd_flags & DT_PFCONV_LEFT)1937*f++ = '-';1938if (pfd->pfd_flags & DT_PFCONV_SPOS)1939*f++ = '+';1940if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)1941*f++ = '*';1942if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {1943*f++ = '.';1944*f++ = '*';1945}1946if (pfd->pfd_flags & DT_PFCONV_GROUP)1947*f++ = '\'';1948if (pfd->pfd_flags & DT_PFCONV_SPACE)1949*f++ = ' ';1950if (pfd->pfd_flags & DT_PFCONV_AGG)1951*f++ = '@';19521953if (width != 0)1954f += snprintf(f, sizeof (format), "%d", width);19551956if (prec != 0)1957f += snprintf(f, sizeof (format), ".%d", prec);19581959/*1960* If the output format is %s, then either %s is the underlying1961* conversion or the conversion is one of our customized ones,1962* e.g. pfprint_addr. In these cases, put the original string1963* name of the conversion (pfc_name) into the pickled format1964* string rather than the derived conversion (pfd_fmt).1965*/1966if (strcmp(pfc->pfc_ofmt, "s") == 0)1967str = pfc->pfc_name;1968else1969str = pfd->pfd_fmt;19701971for (j = 0; str[j] != '\0'; j++)1972*f++ = str[j];1973}19741975*f = '\0'; /* insert nul byte; do not count in return value */19761977assert(f < format + formatlen);1978(void) strncpy(s, format, len);19791980return ((size_t)(f - format));1981}19821983static int1984dt_fprinta(const dtrace_aggdata_t *adp, void *arg)1985{1986const dtrace_aggdesc_t *agg = adp->dtada_desc;1987const dtrace_recdesc_t *recp = &agg->dtagd_rec[0];1988uint_t nrecs = agg->dtagd_nrecs;1989dt_pfwalk_t *pfw = arg;1990dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;1991int id;19921993if (dt_printf_getint(dtp, recp++, nrecs--,1994adp->dtada_data, adp->dtada_size, &id) != 0 || pfw->pfw_aid != id)1995return (0); /* no aggregation id or id does not match */19961997if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,1998recp, nrecs, adp->dtada_data, adp->dtada_size, &adp, 1) == -1)1999return (pfw->pfw_err = dtp->dt_errno);20002001/*2002* Cast away the const to set the bit indicating that this aggregation2003* has been printed.2004*/2005((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;20062007return (0);2008}20092010static int2011dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)2012{2013const dtrace_aggdata_t *aggdata = aggsdata[0];2014const dtrace_aggdesc_t *agg = aggdata->dtada_desc;2015const dtrace_recdesc_t *rec = &agg->dtagd_rec[1];2016uint_t nrecs = agg->dtagd_nrecs - 1;2017dt_pfwalk_t *pfw = arg;2018dtrace_hdl_t *dtp = pfw->pfw_argv->pfv_dtp;2019int i;20202021if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv,2022rec, nrecs, aggdata->dtada_data, aggdata->dtada_size,2023aggsdata, naggvars) == -1)2024return (pfw->pfw_err = dtp->dt_errno);20252026/*2027* For each aggregation, indicate that it has been printed, casting2028* away the const as necessary.2029*/2030for (i = 1; i < naggvars; i++) {2031agg = aggsdata[i]->dtada_desc;2032((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;2033}20342035return (0);2036}2037/*ARGSUSED*/2038int2039dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,2040const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,2041uint_t nrecs, const void *buf, size_t len)2042{2043dt_pfwalk_t pfw;2044int i, naggvars = 0;2045dtrace_aggvarid_t *aggvars;20462047aggvars = alloca(nrecs * sizeof (dtrace_aggvarid_t));20482049/*2050* This might be a printa() with multiple aggregation variables. We2051* need to scan forward through the records until we find a record from2052* a different statement.2053*/2054for (i = 0; i < nrecs; i++) {2055const dtrace_recdesc_t *nrec = &recs[i];20562057if (nrec->dtrd_uarg != recs->dtrd_uarg)2058break;20592060if (nrec->dtrd_action != recs->dtrd_action)2061return (dt_set_errno(dtp, EDT_BADAGG));20622063aggvars[naggvars++] =2064/* LINTED - alignment */2065*((dtrace_aggvarid_t *)((caddr_t)buf + nrec->dtrd_offset));2066}20672068if (naggvars == 0)2069return (dt_set_errno(dtp, EDT_BADAGG));20702071pfw.pfw_argv = fmtdata;2072pfw.pfw_fp = fp;2073pfw.pfw_err = 0;20742075if (naggvars == 1) {2076pfw.pfw_aid = aggvars[0];20772078if (dtrace_aggregate_walk_sorted(dtp,2079dt_fprinta, &pfw) == -1 || pfw.pfw_err != 0)2080return (-1); /* errno is set for us */2081} else {2082if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,2083dt_fprintas, &pfw) == -1 || pfw.pfw_err != 0)2084return (-1); /* errno is set for us */2085}20862087return (i);2088}208920902091