16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell * CDDL HEADER START
36ff6d951SJohn Birrell *
46ff6d951SJohn Birrell * The contents of this file are subject to the terms of the
51670a1c2SRui Paulo * Common Development and Distribution License (the "License").
61670a1c2SRui Paulo * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell *
86ff6d951SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell * See the License for the specific language governing permissions
116ff6d951SJohn Birrell * and limitations under the License.
126ff6d951SJohn Birrell *
136ff6d951SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell *
196ff6d951SJohn Birrell * CDDL HEADER END
206ff6d951SJohn Birrell */
211670a1c2SRui Paulo
226ff6d951SJohn Birrell /*
231670a1c2SRui Paulo * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24a98ff317SPedro F. Giffuni * Copyright (c) 2012 by Delphix. All rights reserved.
256ff6d951SJohn Birrell * Use is subject to license terms.
266ff6d951SJohn Birrell */
276ff6d951SJohn Birrell
28bc96366cSSteven Hartland #ifdef illumos
296ff6d951SJohn Birrell #include <sys/sysmacros.h>
30b29602e4SJohn Birrell #endif
316d9bf094SDimitry Andric #include <sys/isa_defs.h>
326ff6d951SJohn Birrell
336ff6d951SJohn Birrell #include <strings.h>
346ff6d951SJohn Birrell #include <unistd.h>
356ff6d951SJohn Birrell #include <stdarg.h>
366ff6d951SJohn Birrell #include <stddef.h>
376ff6d951SJohn Birrell #include <stdlib.h>
386ff6d951SJohn Birrell #include <stdio.h>
396ff6d951SJohn Birrell #include <errno.h>
406ff6d951SJohn Birrell #include <ctype.h>
41bc96366cSSteven Hartland #ifdef illumos
426ff6d951SJohn Birrell #include <alloca.h>
43b29602e4SJohn Birrell #else
44b29602e4SJohn Birrell #include <sys/sysctl.h>
450f2bd1e8SRui Paulo #include <libproc_compat.h>
46b29602e4SJohn Birrell #endif
476ff6d951SJohn Birrell #include <assert.h>
486ff6d951SJohn Birrell #include <libgen.h>
496ff6d951SJohn Birrell #include <limits.h>
505f301949SBen Laurie #include <stdint.h>
516ff6d951SJohn Birrell
526ff6d951SJohn Birrell #include <dt_impl.h>
5393f27766SDomagoj Stolfa #include <dt_oformat.h>
546ff6d951SJohn Birrell
556ff6d951SJohn Birrell static const struct {
566ff6d951SJohn Birrell size_t dtps_offset;
576ff6d951SJohn Birrell size_t dtps_len;
586ff6d951SJohn Birrell } dtrace_probespecs[] = {
596ff6d951SJohn Birrell { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN },
606ff6d951SJohn Birrell { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN },
616ff6d951SJohn Birrell { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN },
626ff6d951SJohn Birrell { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN }
636ff6d951SJohn Birrell };
646ff6d951SJohn Birrell
656ff6d951SJohn Birrell int
dtrace_xstr2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,int argc,char * const argv[],dtrace_probedesc_t * pdp)666ff6d951SJohn Birrell dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
676ff6d951SJohn Birrell const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
686ff6d951SJohn Birrell {
691670a1c2SRui Paulo size_t off, len, vlen, wlen;
701670a1c2SRui Paulo const char *p, *q, *v, *w;
716ff6d951SJohn Birrell
726ff6d951SJohn Birrell char buf[32]; /* for id_t as %d (see below) */
736ff6d951SJohn Birrell
746ff6d951SJohn Birrell if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
756ff6d951SJohn Birrell return (dt_set_errno(dtp, EINVAL));
766ff6d951SJohn Birrell
776ff6d951SJohn Birrell bzero(pdp, sizeof (dtrace_probedesc_t));
786ff6d951SJohn Birrell p = s + strlen(s) - 1;
796ff6d951SJohn Birrell
806ff6d951SJohn Birrell do {
816ff6d951SJohn Birrell for (len = 0; p >= s && *p != ':'; len++)
826ff6d951SJohn Birrell p--; /* move backward until we find a delimiter */
836ff6d951SJohn Birrell
846ff6d951SJohn Birrell q = p + 1;
856ff6d951SJohn Birrell vlen = 0;
861670a1c2SRui Paulo w = NULL;
871670a1c2SRui Paulo wlen = 0;
886ff6d951SJohn Birrell
896ff6d951SJohn Birrell if ((v = strchr(q, '$')) != NULL && v < q + len) {
906ff6d951SJohn Birrell /*
916ff6d951SJohn Birrell * Set vlen to the length of the variable name and then
926ff6d951SJohn Birrell * reset len to the length of the text prior to '$'. If
936ff6d951SJohn Birrell * the name begins with a digit, interpret it using the
946ff6d951SJohn Birrell * the argv[] array. Otherwise we look in dt_macros.
956ff6d951SJohn Birrell * For the moment, all dt_macros variables are of type
966ff6d951SJohn Birrell * id_t (see dtrace_update() for more details on that).
976ff6d951SJohn Birrell */
986ff6d951SJohn Birrell vlen = (size_t)(q + len - v);
996ff6d951SJohn Birrell len = (size_t)(v - q);
1006ff6d951SJohn Birrell
1016ff6d951SJohn Birrell /*
1026ff6d951SJohn Birrell * If the variable string begins with $$, skip past the
1036ff6d951SJohn Birrell * leading dollar sign since $ and $$ are equivalent
1046ff6d951SJohn Birrell * macro reference operators in a probe description.
1056ff6d951SJohn Birrell */
1066ff6d951SJohn Birrell if (vlen > 2 && v[1] == '$') {
1076ff6d951SJohn Birrell vlen--;
1086ff6d951SJohn Birrell v++;
1096ff6d951SJohn Birrell }
1106ff6d951SJohn Birrell
1116ff6d951SJohn Birrell if (isdigit(v[1])) {
1126ff6d951SJohn Birrell long i;
1136ff6d951SJohn Birrell
1146ff6d951SJohn Birrell errno = 0;
1151670a1c2SRui Paulo i = strtol(v + 1, (char **)&w, 10);
1166ff6d951SJohn Birrell
1171670a1c2SRui Paulo wlen = vlen - (w - v);
1181670a1c2SRui Paulo
1191670a1c2SRui Paulo if (i < 0 || i >= argc || errno != 0)
1206ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_BADSPCV));
1216ff6d951SJohn Birrell
1226ff6d951SJohn Birrell v = argv[i];
1236ff6d951SJohn Birrell vlen = strlen(v);
1246ff6d951SJohn Birrell
1256ff6d951SJohn Birrell if (yypcb != NULL && yypcb->pcb_sargv == argv)
1266ff6d951SJohn Birrell yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
1276ff6d951SJohn Birrell
1286ff6d951SJohn Birrell } else if (vlen > 1) {
1296ff6d951SJohn Birrell char *vstr = alloca(vlen);
1306ff6d951SJohn Birrell dt_ident_t *idp;
1316ff6d951SJohn Birrell
1326ff6d951SJohn Birrell (void) strncpy(vstr, v + 1, vlen - 1);
1336ff6d951SJohn Birrell vstr[vlen - 1] = '\0';
1346ff6d951SJohn Birrell idp = dt_idhash_lookup(dtp->dt_macros, vstr);
1356ff6d951SJohn Birrell
1366ff6d951SJohn Birrell if (idp == NULL)
1376ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_BADSPCV));
1386ff6d951SJohn Birrell
1396ff6d951SJohn Birrell v = buf;
1406ff6d951SJohn Birrell vlen = snprintf(buf, 32, "%d", idp->di_id);
1416ff6d951SJohn Birrell
1426ff6d951SJohn Birrell } else
1436ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_BADSPCV));
1446ff6d951SJohn Birrell }
1456ff6d951SJohn Birrell
1466ff6d951SJohn Birrell if (spec == DTRACE_PROBESPEC_NONE)
1476ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_BADSPEC));
1486ff6d951SJohn Birrell
1496ff6d951SJohn Birrell if (len + vlen >= dtrace_probespecs[spec].dtps_len)
1506ff6d951SJohn Birrell return (dt_set_errno(dtp, ENAMETOOLONG));
1516ff6d951SJohn Birrell
1526ff6d951SJohn Birrell off = dtrace_probespecs[spec--].dtps_offset;
1536ff6d951SJohn Birrell bcopy(q, (char *)pdp + off, len);
1546ff6d951SJohn Birrell bcopy(v, (char *)pdp + off + len, vlen);
1551670a1c2SRui Paulo bcopy(w, (char *)pdp + off + len + vlen, wlen);
1566ff6d951SJohn Birrell } while (--p >= s);
1576ff6d951SJohn Birrell
1586ff6d951SJohn Birrell pdp->dtpd_id = DTRACE_IDNONE;
1596ff6d951SJohn Birrell return (0);
1606ff6d951SJohn Birrell }
1616ff6d951SJohn Birrell
1626ff6d951SJohn Birrell int
dtrace_str2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,dtrace_probedesc_t * pdp)1636ff6d951SJohn Birrell dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
1646ff6d951SJohn Birrell const char *s, dtrace_probedesc_t *pdp)
1656ff6d951SJohn Birrell {
1666ff6d951SJohn Birrell return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
1676ff6d951SJohn Birrell }
1686ff6d951SJohn Birrell
1696ff6d951SJohn Birrell int
dtrace_id2desc(dtrace_hdl_t * dtp,dtrace_id_t id,dtrace_probedesc_t * pdp)1706ff6d951SJohn Birrell dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
1716ff6d951SJohn Birrell {
1726ff6d951SJohn Birrell bzero(pdp, sizeof (dtrace_probedesc_t));
1736ff6d951SJohn Birrell pdp->dtpd_id = id;
1746ff6d951SJohn Birrell
1756ff6d951SJohn Birrell if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
1766ff6d951SJohn Birrell pdp->dtpd_id != id)
1776ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_BADID));
1786ff6d951SJohn Birrell
1796ff6d951SJohn Birrell return (0);
1806ff6d951SJohn Birrell }
1816ff6d951SJohn Birrell
1826ff6d951SJohn Birrell char *
dtrace_desc2str(const dtrace_probedesc_t * pdp,char * buf,size_t len)1836ff6d951SJohn Birrell dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
1846ff6d951SJohn Birrell {
1856ff6d951SJohn Birrell if (pdp->dtpd_id == 0) {
1866ff6d951SJohn Birrell (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
1876ff6d951SJohn Birrell pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
1886ff6d951SJohn Birrell } else
1896ff6d951SJohn Birrell (void) snprintf(buf, len, "%u", pdp->dtpd_id);
1906ff6d951SJohn Birrell
1916ff6d951SJohn Birrell return (buf);
1926ff6d951SJohn Birrell }
1936ff6d951SJohn Birrell
1946ff6d951SJohn Birrell char *
dtrace_attr2str(dtrace_attribute_t attr,char * buf,size_t len)1956ff6d951SJohn Birrell dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
1966ff6d951SJohn Birrell {
1976ff6d951SJohn Birrell const char *name = dtrace_stability_name(attr.dtat_name);
1986ff6d951SJohn Birrell const char *data = dtrace_stability_name(attr.dtat_data);
1996ff6d951SJohn Birrell const char *class = dtrace_class_name(attr.dtat_class);
2006ff6d951SJohn Birrell
2016ff6d951SJohn Birrell if (name == NULL || data == NULL || class == NULL)
2026ff6d951SJohn Birrell return (NULL); /* one or more invalid attributes */
2036ff6d951SJohn Birrell
2046ff6d951SJohn Birrell (void) snprintf(buf, len, "%s/%s/%s", name, data, class);
2056ff6d951SJohn Birrell return (buf);
2066ff6d951SJohn Birrell }
2076ff6d951SJohn Birrell
2086ff6d951SJohn Birrell static char *
dt_getstrattr(char * p,char ** qp)2096ff6d951SJohn Birrell dt_getstrattr(char *p, char **qp)
2106ff6d951SJohn Birrell {
2116ff6d951SJohn Birrell char *q;
2126ff6d951SJohn Birrell
2136ff6d951SJohn Birrell if (*p == '\0')
2146ff6d951SJohn Birrell return (NULL);
2156ff6d951SJohn Birrell
2166ff6d951SJohn Birrell if ((q = strchr(p, '/')) == NULL)
2176ff6d951SJohn Birrell q = p + strlen(p);
2186ff6d951SJohn Birrell else
2196ff6d951SJohn Birrell *q++ = '\0';
2206ff6d951SJohn Birrell
2216ff6d951SJohn Birrell *qp = q;
2226ff6d951SJohn Birrell return (p);
2236ff6d951SJohn Birrell }
2246ff6d951SJohn Birrell
2256ff6d951SJohn Birrell int
dtrace_str2attr(const char * str,dtrace_attribute_t * attr)2266ff6d951SJohn Birrell dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
2276ff6d951SJohn Birrell {
2286ff6d951SJohn Birrell dtrace_stability_t s;
2296ff6d951SJohn Birrell dtrace_class_t c;
2306ff6d951SJohn Birrell char *p, *q;
2316ff6d951SJohn Birrell
2326ff6d951SJohn Birrell if (str == NULL || attr == NULL)
2336ff6d951SJohn Birrell return (-1); /* invalid function arguments */
2346ff6d951SJohn Birrell
2356ff6d951SJohn Birrell *attr = _dtrace_maxattr;
2366ff6d951SJohn Birrell p = alloca(strlen(str) + 1);
2376ff6d951SJohn Birrell (void) strcpy(p, str);
2386ff6d951SJohn Birrell
2396ff6d951SJohn Birrell if ((p = dt_getstrattr(p, &q)) == NULL)
2406ff6d951SJohn Birrell return (0);
2416ff6d951SJohn Birrell
2426ff6d951SJohn Birrell for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2436ff6d951SJohn Birrell if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2446ff6d951SJohn Birrell attr->dtat_name = s;
2456ff6d951SJohn Birrell break;
2466ff6d951SJohn Birrell }
2476ff6d951SJohn Birrell }
2486ff6d951SJohn Birrell
2496ff6d951SJohn Birrell if (s > DTRACE_STABILITY_MAX)
2506ff6d951SJohn Birrell return (-1);
2516ff6d951SJohn Birrell
2526ff6d951SJohn Birrell if ((p = dt_getstrattr(q, &q)) == NULL)
2536ff6d951SJohn Birrell return (0);
2546ff6d951SJohn Birrell
2556ff6d951SJohn Birrell for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2566ff6d951SJohn Birrell if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2576ff6d951SJohn Birrell attr->dtat_data = s;
2586ff6d951SJohn Birrell break;
2596ff6d951SJohn Birrell }
2606ff6d951SJohn Birrell }
2616ff6d951SJohn Birrell
2626ff6d951SJohn Birrell if (s > DTRACE_STABILITY_MAX)
2636ff6d951SJohn Birrell return (-1);
2646ff6d951SJohn Birrell
2656ff6d951SJohn Birrell if ((p = dt_getstrattr(q, &q)) == NULL)
2666ff6d951SJohn Birrell return (0);
2676ff6d951SJohn Birrell
2686ff6d951SJohn Birrell for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
2696ff6d951SJohn Birrell if (strcasecmp(p, dtrace_class_name(c)) == 0) {
2706ff6d951SJohn Birrell attr->dtat_class = c;
2716ff6d951SJohn Birrell break;
2726ff6d951SJohn Birrell }
2736ff6d951SJohn Birrell }
2746ff6d951SJohn Birrell
2756ff6d951SJohn Birrell if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
2766ff6d951SJohn Birrell return (-1);
2776ff6d951SJohn Birrell
2786ff6d951SJohn Birrell return (0);
2796ff6d951SJohn Birrell }
2806ff6d951SJohn Birrell
2816ff6d951SJohn Birrell const char *
dtrace_stability_name(dtrace_stability_t s)2826ff6d951SJohn Birrell dtrace_stability_name(dtrace_stability_t s)
2836ff6d951SJohn Birrell {
2846ff6d951SJohn Birrell switch (s) {
2856ff6d951SJohn Birrell case DTRACE_STABILITY_INTERNAL: return ("Internal");
2866ff6d951SJohn Birrell case DTRACE_STABILITY_PRIVATE: return ("Private");
2876ff6d951SJohn Birrell case DTRACE_STABILITY_OBSOLETE: return ("Obsolete");
2886ff6d951SJohn Birrell case DTRACE_STABILITY_EXTERNAL: return ("External");
2896ff6d951SJohn Birrell case DTRACE_STABILITY_UNSTABLE: return ("Unstable");
2906ff6d951SJohn Birrell case DTRACE_STABILITY_EVOLVING: return ("Evolving");
2916ff6d951SJohn Birrell case DTRACE_STABILITY_STABLE: return ("Stable");
2926ff6d951SJohn Birrell case DTRACE_STABILITY_STANDARD: return ("Standard");
2936ff6d951SJohn Birrell default: return (NULL);
2946ff6d951SJohn Birrell }
2956ff6d951SJohn Birrell }
2966ff6d951SJohn Birrell
2976ff6d951SJohn Birrell const char *
dtrace_class_name(dtrace_class_t c)2986ff6d951SJohn Birrell dtrace_class_name(dtrace_class_t c)
2996ff6d951SJohn Birrell {
3006ff6d951SJohn Birrell switch (c) {
3016ff6d951SJohn Birrell case DTRACE_CLASS_UNKNOWN: return ("Unknown");
3026ff6d951SJohn Birrell case DTRACE_CLASS_CPU: return ("CPU");
3036ff6d951SJohn Birrell case DTRACE_CLASS_PLATFORM: return ("Platform");
3046ff6d951SJohn Birrell case DTRACE_CLASS_GROUP: return ("Group");
3056ff6d951SJohn Birrell case DTRACE_CLASS_ISA: return ("ISA");
3066ff6d951SJohn Birrell case DTRACE_CLASS_COMMON: return ("Common");
3076ff6d951SJohn Birrell default: return (NULL);
3086ff6d951SJohn Birrell }
3096ff6d951SJohn Birrell }
3106ff6d951SJohn Birrell
3116ff6d951SJohn Birrell dtrace_attribute_t
dt_attr_min(dtrace_attribute_t a1,dtrace_attribute_t a2)3126ff6d951SJohn Birrell dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
3136ff6d951SJohn Birrell {
3146ff6d951SJohn Birrell dtrace_attribute_t am;
3156ff6d951SJohn Birrell
3166ff6d951SJohn Birrell am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
3176ff6d951SJohn Birrell am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
3186ff6d951SJohn Birrell am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
3196ff6d951SJohn Birrell
3206ff6d951SJohn Birrell return (am);
3216ff6d951SJohn Birrell }
3226ff6d951SJohn Birrell
3236ff6d951SJohn Birrell dtrace_attribute_t
dt_attr_max(dtrace_attribute_t a1,dtrace_attribute_t a2)3246ff6d951SJohn Birrell dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
3256ff6d951SJohn Birrell {
3266ff6d951SJohn Birrell dtrace_attribute_t am;
3276ff6d951SJohn Birrell
3286ff6d951SJohn Birrell am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
3296ff6d951SJohn Birrell am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
3306ff6d951SJohn Birrell am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
3316ff6d951SJohn Birrell
3326ff6d951SJohn Birrell return (am);
3336ff6d951SJohn Birrell }
3346ff6d951SJohn Birrell
3356ff6d951SJohn Birrell /*
3366ff6d951SJohn Birrell * Compare two attributes and return an integer value in the following ranges:
3376ff6d951SJohn Birrell *
3386ff6d951SJohn Birrell * <0 if any of a1's attributes are less than a2's attributes
3396ff6d951SJohn Birrell * =0 if all of a1's attributes are equal to a2's attributes
3406ff6d951SJohn Birrell * >0 if all of a1's attributes are greater than or equal to a2's attributes
3416ff6d951SJohn Birrell *
3426ff6d951SJohn Birrell * To implement this function efficiently, we subtract a2's attributes from
3436ff6d951SJohn Birrell * a1's to obtain a negative result if an a1 attribute is less than its a2
3446ff6d951SJohn Birrell * counterpart. We then OR the intermediate results together, relying on the
3456ff6d951SJohn Birrell * twos-complement property that if any result is negative, the bitwise union
3466ff6d951SJohn Birrell * will also be negative since the highest bit will be set in the result.
3476ff6d951SJohn Birrell */
3486ff6d951SJohn Birrell int
dt_attr_cmp(dtrace_attribute_t a1,dtrace_attribute_t a2)3496ff6d951SJohn Birrell dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
3506ff6d951SJohn Birrell {
3516ff6d951SJohn Birrell return (((int)a1.dtat_name - a2.dtat_name) |
3526ff6d951SJohn Birrell ((int)a1.dtat_data - a2.dtat_data) |
3536ff6d951SJohn Birrell ((int)a1.dtat_class - a2.dtat_class));
3546ff6d951SJohn Birrell }
3556ff6d951SJohn Birrell
3566ff6d951SJohn Birrell char *
dt_attr_str(dtrace_attribute_t a,char * buf,size_t len)3576ff6d951SJohn Birrell dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
3586ff6d951SJohn Birrell {
3596ff6d951SJohn Birrell static const char stability[] = "ipoxuesS";
3606ff6d951SJohn Birrell static const char class[] = "uCpgIc";
3616ff6d951SJohn Birrell
3626ff6d951SJohn Birrell if (a.dtat_name < sizeof (stability) &&
3636ff6d951SJohn Birrell a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
3646ff6d951SJohn Birrell (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
3656ff6d951SJohn Birrell stability[a.dtat_data], class[a.dtat_class]);
3666ff6d951SJohn Birrell } else {
3676ff6d951SJohn Birrell (void) snprintf(buf, len, "[%u/%u/%u]",
3686ff6d951SJohn Birrell a.dtat_name, a.dtat_data, a.dtat_class);
3696ff6d951SJohn Birrell }
3706ff6d951SJohn Birrell
3716ff6d951SJohn Birrell return (buf);
3726ff6d951SJohn Birrell }
3736ff6d951SJohn Birrell
3746ff6d951SJohn Birrell char *
dt_version_num2str(dt_version_t v,char * buf,size_t len)3756ff6d951SJohn Birrell dt_version_num2str(dt_version_t v, char *buf, size_t len)
3766ff6d951SJohn Birrell {
3776ff6d951SJohn Birrell uint_t M = DT_VERSION_MAJOR(v);
3786ff6d951SJohn Birrell uint_t m = DT_VERSION_MINOR(v);
3796ff6d951SJohn Birrell uint_t u = DT_VERSION_MICRO(v);
3806ff6d951SJohn Birrell
3816ff6d951SJohn Birrell if (u == 0)
3826ff6d951SJohn Birrell (void) snprintf(buf, len, "%u.%u", M, m);
3836ff6d951SJohn Birrell else
3846ff6d951SJohn Birrell (void) snprintf(buf, len, "%u.%u.%u", M, m, u);
3856ff6d951SJohn Birrell
3866ff6d951SJohn Birrell return (buf);
3876ff6d951SJohn Birrell }
3886ff6d951SJohn Birrell
3896ff6d951SJohn Birrell int
dt_version_str2num(const char * s,dt_version_t * vp)3906ff6d951SJohn Birrell dt_version_str2num(const char *s, dt_version_t *vp)
3916ff6d951SJohn Birrell {
3926ff6d951SJohn Birrell int i = 0, n[3] = { 0, 0, 0 };
3936ff6d951SJohn Birrell char c;
3946ff6d951SJohn Birrell
3956ff6d951SJohn Birrell while ((c = *s++) != '\0') {
3966ff6d951SJohn Birrell if (isdigit(c))
3976ff6d951SJohn Birrell n[i] = n[i] * 10 + c - '0';
3986ff6d951SJohn Birrell else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
3996ff6d951SJohn Birrell return (-1);
4006ff6d951SJohn Birrell }
4016ff6d951SJohn Birrell
4026ff6d951SJohn Birrell if (n[0] > DT_VERSION_MAJMAX ||
4036ff6d951SJohn Birrell n[1] > DT_VERSION_MINMAX ||
4046ff6d951SJohn Birrell n[2] > DT_VERSION_MICMAX)
4056ff6d951SJohn Birrell return (-1);
4066ff6d951SJohn Birrell
4076ff6d951SJohn Birrell if (vp != NULL)
4086ff6d951SJohn Birrell *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
4096ff6d951SJohn Birrell
4106ff6d951SJohn Birrell return (0);
4116ff6d951SJohn Birrell }
4126ff6d951SJohn Birrell
4136ff6d951SJohn Birrell int
dt_version_defined(dt_version_t v)4146ff6d951SJohn Birrell dt_version_defined(dt_version_t v)
4156ff6d951SJohn Birrell {
4166ff6d951SJohn Birrell int i;
4176ff6d951SJohn Birrell
4186ff6d951SJohn Birrell for (i = 0; _dtrace_versions[i] != 0; i++) {
4196ff6d951SJohn Birrell if (_dtrace_versions[i] == v)
4206ff6d951SJohn Birrell return (1);
4216ff6d951SJohn Birrell }
4226ff6d951SJohn Birrell
4236ff6d951SJohn Birrell return (0);
4246ff6d951SJohn Birrell }
4256ff6d951SJohn Birrell
4266ff6d951SJohn Birrell char *
dt_cpp_add_arg(dtrace_hdl_t * dtp,const char * str)4276ff6d951SJohn Birrell dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
4286ff6d951SJohn Birrell {
4296ff6d951SJohn Birrell char *arg;
4306ff6d951SJohn Birrell
4316ff6d951SJohn Birrell if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
4326ff6d951SJohn Birrell int olds = dtp->dt_cpp_args;
4336ff6d951SJohn Birrell int news = olds * 2;
4346ff6d951SJohn Birrell char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
4356ff6d951SJohn Birrell
4366ff6d951SJohn Birrell if (argv == NULL)
4376ff6d951SJohn Birrell return (NULL);
4386ff6d951SJohn Birrell
4396ff6d951SJohn Birrell bzero(&argv[olds], sizeof (char *) * olds);
4406ff6d951SJohn Birrell dtp->dt_cpp_argv = argv;
4416ff6d951SJohn Birrell dtp->dt_cpp_args = news;
4426ff6d951SJohn Birrell }
4436ff6d951SJohn Birrell
4446ff6d951SJohn Birrell if ((arg = strdup(str)) == NULL)
4456ff6d951SJohn Birrell return (NULL);
4466ff6d951SJohn Birrell
4476ff6d951SJohn Birrell assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
4486ff6d951SJohn Birrell dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
4496ff6d951SJohn Birrell return (arg);
4506ff6d951SJohn Birrell }
4516ff6d951SJohn Birrell
4526ff6d951SJohn Birrell char *
dt_cpp_pop_arg(dtrace_hdl_t * dtp)4536ff6d951SJohn Birrell dt_cpp_pop_arg(dtrace_hdl_t *dtp)
4546ff6d951SJohn Birrell {
4556ff6d951SJohn Birrell char *arg;
4566ff6d951SJohn Birrell
4576ff6d951SJohn Birrell if (dtp->dt_cpp_argc <= 1)
4586ff6d951SJohn Birrell return (NULL); /* dt_cpp_argv[0] cannot be popped */
4596ff6d951SJohn Birrell
4606ff6d951SJohn Birrell arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
4616ff6d951SJohn Birrell dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
4626ff6d951SJohn Birrell
4636ff6d951SJohn Birrell return (arg);
4646ff6d951SJohn Birrell }
4656ff6d951SJohn Birrell
466*9a30c8d3SMark Johnston int
dt_cpu_maxid(dtrace_hdl_t * dtp)467*9a30c8d3SMark Johnston dt_cpu_maxid(dtrace_hdl_t *dtp)
468*9a30c8d3SMark Johnston {
469*9a30c8d3SMark Johnston size_t len;
470*9a30c8d3SMark Johnston u_int count;
471*9a30c8d3SMark Johnston int error;
472*9a30c8d3SMark Johnston
473*9a30c8d3SMark Johnston len = sizeof(count);
474*9a30c8d3SMark Johnston error = sysctlbyname("kern.smp.maxid", &count, &len, NULL, 0);
475*9a30c8d3SMark Johnston if (error != 0)
476*9a30c8d3SMark Johnston return (dt_set_errno(dtp, errno));
477*9a30c8d3SMark Johnston return (count);
478*9a30c8d3SMark Johnston }
479*9a30c8d3SMark Johnston
4806ff6d951SJohn Birrell /*PRINTFLIKE1*/
4816ff6d951SJohn Birrell void
dt_dprintf(const char * format,...)4826ff6d951SJohn Birrell dt_dprintf(const char *format, ...)
4836ff6d951SJohn Birrell {
4846ff6d951SJohn Birrell if (_dtrace_debug) {
4856ff6d951SJohn Birrell va_list alist;
4866ff6d951SJohn Birrell
4876ff6d951SJohn Birrell va_start(alist, format);
4886ff6d951SJohn Birrell (void) fputs("libdtrace DEBUG: ", stderr);
4896ff6d951SJohn Birrell (void) vfprintf(stderr, format, alist);
4906ff6d951SJohn Birrell va_end(alist);
4916ff6d951SJohn Birrell }
4926ff6d951SJohn Birrell }
4936ff6d951SJohn Birrell
4946ff6d951SJohn Birrell int
495bc96366cSSteven Hartland #ifdef illumos
dt_ioctl(dtrace_hdl_t * dtp,int val,void * arg)4966ff6d951SJohn Birrell dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
497b29602e4SJohn Birrell #else
498b29602e4SJohn Birrell dt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg)
499b29602e4SJohn Birrell #endif
5006ff6d951SJohn Birrell {
5016ff6d951SJohn Birrell const dtrace_vector_t *v = dtp->dt_vector;
5026ff6d951SJohn Birrell
503bc96366cSSteven Hartland #ifndef illumos
504b29602e4SJohn Birrell /* Avoid sign extension. */
505b29602e4SJohn Birrell val &= 0xffffffff;
506b29602e4SJohn Birrell #endif
507b29602e4SJohn Birrell
5086ff6d951SJohn Birrell if (v != NULL)
5096ff6d951SJohn Birrell return (v->dtv_ioctl(dtp->dt_varg, val, arg));
5106ff6d951SJohn Birrell
5116ff6d951SJohn Birrell if (dtp->dt_fd >= 0)
5126ff6d951SJohn Birrell return (ioctl(dtp->dt_fd, val, arg));
5136ff6d951SJohn Birrell
5146ff6d951SJohn Birrell errno = EBADF;
5156ff6d951SJohn Birrell return (-1);
5166ff6d951SJohn Birrell }
5176ff6d951SJohn Birrell
5186ff6d951SJohn Birrell int
dt_status(dtrace_hdl_t * dtp,processorid_t cpu)5196ff6d951SJohn Birrell dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
5206ff6d951SJohn Birrell {
5216ff6d951SJohn Birrell const dtrace_vector_t *v = dtp->dt_vector;
5226ff6d951SJohn Birrell
523b29602e4SJohn Birrell if (v == NULL) {
524bc96366cSSteven Hartland #ifdef illumos
5256ff6d951SJohn Birrell return (p_online(cpu, P_STATUS));
526b29602e4SJohn Birrell #else
527b29602e4SJohn Birrell int maxid = 0;
528b29602e4SJohn Birrell size_t len = sizeof(maxid);
529b29602e4SJohn Birrell if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0)
530b29602e4SJohn Birrell return (cpu == 0 ? 1 : -1);
531b29602e4SJohn Birrell else
532b29602e4SJohn Birrell return (cpu <= maxid ? 1 : -1);
533b29602e4SJohn Birrell #endif
534b29602e4SJohn Birrell }
5356ff6d951SJohn Birrell
5366ff6d951SJohn Birrell return (v->dtv_status(dtp->dt_varg, cpu));
5376ff6d951SJohn Birrell }
5386ff6d951SJohn Birrell
5396ff6d951SJohn Birrell long
dt_sysconf(dtrace_hdl_t * dtp,int name)5406ff6d951SJohn Birrell dt_sysconf(dtrace_hdl_t *dtp, int name)
5416ff6d951SJohn Birrell {
5426ff6d951SJohn Birrell const dtrace_vector_t *v = dtp->dt_vector;
5436ff6d951SJohn Birrell
5446ff6d951SJohn Birrell if (v == NULL)
5456ff6d951SJohn Birrell return (sysconf(name));
5466ff6d951SJohn Birrell
5476ff6d951SJohn Birrell return (v->dtv_sysconf(dtp->dt_varg, name));
5486ff6d951SJohn Birrell }
5496ff6d951SJohn Birrell
5506ff6d951SJohn Birrell /*
5516ff6d951SJohn Birrell * Wrapper around write(2) to handle partial writes. For maximum safety of
5526ff6d951SJohn Birrell * output files and proper error reporting, we continuing writing in the
5536ff6d951SJohn Birrell * face of partial writes until write(2) fails or 'buf' is completely written.
5546ff6d951SJohn Birrell * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
5556ff6d951SJohn Birrell */
5566ff6d951SJohn Birrell ssize_t
dt_write(dtrace_hdl_t * dtp,int fd,const void * buf,size_t n)5576ff6d951SJohn Birrell dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
5586ff6d951SJohn Birrell {
5596ff6d951SJohn Birrell ssize_t resid = n;
5606ff6d951SJohn Birrell ssize_t len;
5616ff6d951SJohn Birrell
5626ff6d951SJohn Birrell while (resid != 0) {
5636ff6d951SJohn Birrell if ((len = write(fd, buf, resid)) <= 0)
5646ff6d951SJohn Birrell break;
5656ff6d951SJohn Birrell
5666ff6d951SJohn Birrell resid -= len;
5676ff6d951SJohn Birrell buf = (char *)buf + len;
5686ff6d951SJohn Birrell }
5696ff6d951SJohn Birrell
5706ff6d951SJohn Birrell if (resid == n && n != 0)
5716ff6d951SJohn Birrell return (dt_set_errno(dtp, errno));
5726ff6d951SJohn Birrell
5736ff6d951SJohn Birrell return (n - resid);
5746ff6d951SJohn Birrell }
5756ff6d951SJohn Birrell
5766ff6d951SJohn Birrell /*
5776ff6d951SJohn Birrell * This function handles all output from libdtrace, as well as the
5786ff6d951SJohn Birrell * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then
5796ff6d951SJohn Birrell * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
5806ff6d951SJohn Birrell * specified buffer and return. Otherwise, if output is buffered (denoted by
5816ff6d951SJohn Birrell * a NULL fp), we sprintf the desired output into the buffered buffer
5826ff6d951SJohn Birrell * (expanding the buffer if required). If we don't satisfy either of these
5836ff6d951SJohn Birrell * conditions (that is, if we are to actually generate output), then we call
5846ff6d951SJohn Birrell * fprintf with the specified fp. In this case, we need to deal with one of
5856ff6d951SJohn Birrell * the more annoying peculiarities of libc's printf routines: any failed
5866ff6d951SJohn Birrell * write persistently sets an error flag inside the FILE causing every
5876ff6d951SJohn Birrell * subsequent write to fail, but only the caller that initiated the error gets
5886ff6d951SJohn Birrell * the errno. Since libdtrace clients often intercept SIGINT, this case is
5896ff6d951SJohn Birrell * particularly frustrating since we don't want the EINTR on one attempt to
5906ff6d951SJohn Birrell * write to the output file to preclude later attempts to write. This
5916ff6d951SJohn Birrell * function therefore does a clearerr() if any error occurred, and saves the
5926ff6d951SJohn Birrell * errno for the caller inside the specified dtrace_hdl_t.
5936ff6d951SJohn Birrell */
5946ff6d951SJohn Birrell /*PRINTFLIKE3*/
5956ff6d951SJohn Birrell int
dt_printf(dtrace_hdl_t * dtp,FILE * fp,const char * format,...)5966ff6d951SJohn Birrell dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
5976ff6d951SJohn Birrell {
5986ff6d951SJohn Birrell va_list ap;
599956c2a73SGeorge V. Neville-Neil va_list ap2;
6006ff6d951SJohn Birrell int n;
6016ff6d951SJohn Birrell
602bc96366cSSteven Hartland #ifndef illumos
603b29602e4SJohn Birrell /*
604b29602e4SJohn Birrell * On FreeBSD, check if output is currently being re-directed
605b29602e4SJohn Birrell * to another file. If so, output to that file instead of the
606b29602e4SJohn Birrell * one the caller has specified.
607b29602e4SJohn Birrell */
608b29602e4SJohn Birrell if (dtp->dt_freopen_fp != NULL)
609b29602e4SJohn Birrell fp = dtp->dt_freopen_fp;
610b29602e4SJohn Birrell #endif
611b29602e4SJohn Birrell
6126ff6d951SJohn Birrell va_start(ap, format);
6136ff6d951SJohn Birrell
6146ff6d951SJohn Birrell if (dtp->dt_sprintf_buflen != 0) {
6156ff6d951SJohn Birrell int len;
6166ff6d951SJohn Birrell char *buf;
6176ff6d951SJohn Birrell
6186ff6d951SJohn Birrell assert(dtp->dt_sprintf_buf != NULL);
6196ff6d951SJohn Birrell
6206ff6d951SJohn Birrell buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
6216ff6d951SJohn Birrell len = dtp->dt_sprintf_buflen - len;
6226ff6d951SJohn Birrell assert(len >= 0);
6236ff6d951SJohn Birrell
624956c2a73SGeorge V. Neville-Neil va_copy(ap2, ap);
625956c2a73SGeorge V. Neville-Neil if ((n = vsnprintf(buf, len, format, ap2)) < 0)
6266ff6d951SJohn Birrell n = dt_set_errno(dtp, errno);
6276ff6d951SJohn Birrell
628956c2a73SGeorge V. Neville-Neil va_end(ap2);
6296ff6d951SJohn Birrell va_end(ap);
6306ff6d951SJohn Birrell
6316ff6d951SJohn Birrell return (n);
6326ff6d951SJohn Birrell }
6336ff6d951SJohn Birrell
6346ff6d951SJohn Birrell if (fp == NULL) {
6356ff6d951SJohn Birrell int needed, rval;
6366ff6d951SJohn Birrell size_t avail;
6376ff6d951SJohn Birrell
6386ff6d951SJohn Birrell /*
639a98ff317SPedro F. Giffuni * Using buffered output is not allowed if a handler has
640a98ff317SPedro F. Giffuni * not been installed.
6416ff6d951SJohn Birrell */
6426ff6d951SJohn Birrell if (dtp->dt_bufhdlr == NULL) {
6436ff6d951SJohn Birrell va_end(ap);
6446ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_NOBUFFERED));
6456ff6d951SJohn Birrell }
6466ff6d951SJohn Birrell
6476ff6d951SJohn Birrell if (dtp->dt_buffered_buf == NULL) {
6486ff6d951SJohn Birrell assert(dtp->dt_buffered_size == 0);
6496ff6d951SJohn Birrell dtp->dt_buffered_size = 1;
6506ff6d951SJohn Birrell dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
6516ff6d951SJohn Birrell
6526ff6d951SJohn Birrell if (dtp->dt_buffered_buf == NULL) {
6536ff6d951SJohn Birrell va_end(ap);
6546ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_NOMEM));
6556ff6d951SJohn Birrell }
6566ff6d951SJohn Birrell
6576ff6d951SJohn Birrell dtp->dt_buffered_offs = 0;
6586ff6d951SJohn Birrell dtp->dt_buffered_buf[0] = '\0';
6596ff6d951SJohn Birrell }
6606ff6d951SJohn Birrell
661956c2a73SGeorge V. Neville-Neil va_copy(ap2, ap);
662956c2a73SGeorge V. Neville-Neil if ((needed = vsnprintf(NULL, 0, format, ap2)) < 0) {
6636ff6d951SJohn Birrell rval = dt_set_errno(dtp, errno);
664956c2a73SGeorge V. Neville-Neil va_end(ap2);
6656ff6d951SJohn Birrell va_end(ap);
6666ff6d951SJohn Birrell return (rval);
6676ff6d951SJohn Birrell }
668956c2a73SGeorge V. Neville-Neil va_end(ap2);
6696ff6d951SJohn Birrell
6706ff6d951SJohn Birrell if (needed == 0) {
6716ff6d951SJohn Birrell va_end(ap);
6726ff6d951SJohn Birrell return (0);
6736ff6d951SJohn Birrell }
6746ff6d951SJohn Birrell
6756ff6d951SJohn Birrell for (;;) {
6766ff6d951SJohn Birrell char *newbuf;
6776ff6d951SJohn Birrell
6786ff6d951SJohn Birrell assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
6796ff6d951SJohn Birrell avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
6806ff6d951SJohn Birrell
6816ff6d951SJohn Birrell if (needed + 1 < avail)
6826ff6d951SJohn Birrell break;
6836ff6d951SJohn Birrell
6846ff6d951SJohn Birrell if ((newbuf = realloc(dtp->dt_buffered_buf,
6856ff6d951SJohn Birrell dtp->dt_buffered_size << 1)) == NULL) {
6866ff6d951SJohn Birrell va_end(ap);
6876ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_NOMEM));
6886ff6d951SJohn Birrell }
6896ff6d951SJohn Birrell
6906ff6d951SJohn Birrell dtp->dt_buffered_buf = newbuf;
6916ff6d951SJohn Birrell dtp->dt_buffered_size <<= 1;
6926ff6d951SJohn Birrell }
6936ff6d951SJohn Birrell
694956c2a73SGeorge V. Neville-Neil va_copy(ap2, ap);
6956ff6d951SJohn Birrell if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
696956c2a73SGeorge V. Neville-Neil avail, format, ap2) < 0) {
6976ff6d951SJohn Birrell rval = dt_set_errno(dtp, errno);
698956c2a73SGeorge V. Neville-Neil va_end(ap2);
6996ff6d951SJohn Birrell va_end(ap);
7006ff6d951SJohn Birrell return (rval);
7016ff6d951SJohn Birrell }
702956c2a73SGeorge V. Neville-Neil va_end(ap2);
7036ff6d951SJohn Birrell
7046ff6d951SJohn Birrell dtp->dt_buffered_offs += needed;
7056ff6d951SJohn Birrell assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
706ba670ce0SKevin Lo va_end(ap);
7076ff6d951SJohn Birrell return (0);
7086ff6d951SJohn Birrell }
7096ff6d951SJohn Birrell
710956c2a73SGeorge V. Neville-Neil va_copy(ap2, ap);
711956c2a73SGeorge V. Neville-Neil n = vfprintf(fp, format, ap2);
712956c2a73SGeorge V. Neville-Neil va_end(ap2);
7136ff6d951SJohn Birrell va_end(ap);
7146ff6d951SJohn Birrell
7156ff6d951SJohn Birrell if (n < 0) {
7166ff6d951SJohn Birrell clearerr(fp);
7176ff6d951SJohn Birrell return (dt_set_errno(dtp, errno));
7186ff6d951SJohn Birrell }
7196ff6d951SJohn Birrell
7206ff6d951SJohn Birrell return (n);
7216ff6d951SJohn Birrell }
7226ff6d951SJohn Birrell
7236ff6d951SJohn Birrell int
dt_buffered_flush(dtrace_hdl_t * dtp,dtrace_probedata_t * pdata,const dtrace_recdesc_t * rec,const dtrace_aggdata_t * agg,uint32_t flags)7246ff6d951SJohn Birrell dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
7256ff6d951SJohn Birrell const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
7266ff6d951SJohn Birrell {
7276ff6d951SJohn Birrell dtrace_bufdata_t data;
7286ff6d951SJohn Birrell
7296ff6d951SJohn Birrell if (dtp->dt_buffered_offs == 0)
7306ff6d951SJohn Birrell return (0);
7316ff6d951SJohn Birrell
7326ff6d951SJohn Birrell data.dtbda_handle = dtp;
7336ff6d951SJohn Birrell data.dtbda_buffered = dtp->dt_buffered_buf;
7346ff6d951SJohn Birrell data.dtbda_probe = pdata;
7356ff6d951SJohn Birrell data.dtbda_recdesc = rec;
7366ff6d951SJohn Birrell data.dtbda_aggdata = agg;
7376ff6d951SJohn Birrell data.dtbda_flags = flags;
7386ff6d951SJohn Birrell
7396ff6d951SJohn Birrell if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
7406ff6d951SJohn Birrell return (dt_set_errno(dtp, EDT_DIRABORT));
7416ff6d951SJohn Birrell
7426ff6d951SJohn Birrell dtp->dt_buffered_offs = 0;
7436ff6d951SJohn Birrell dtp->dt_buffered_buf[0] = '\0';
7446ff6d951SJohn Birrell
7456ff6d951SJohn Birrell return (0);
7466ff6d951SJohn Birrell }
7476ff6d951SJohn Birrell
7486ff6d951SJohn Birrell void
dt_buffered_destroy(dtrace_hdl_t * dtp)7496ff6d951SJohn Birrell dt_buffered_destroy(dtrace_hdl_t *dtp)
7506ff6d951SJohn Birrell {
7516ff6d951SJohn Birrell free(dtp->dt_buffered_buf);
7526ff6d951SJohn Birrell dtp->dt_buffered_buf = NULL;
7536ff6d951SJohn Birrell dtp->dt_buffered_offs = 0;
7546ff6d951SJohn Birrell dtp->dt_buffered_size = 0;
7556ff6d951SJohn Birrell }
7566ff6d951SJohn Birrell
7576ff6d951SJohn Birrell void *
dt_zalloc(dtrace_hdl_t * dtp,size_t size)7586ff6d951SJohn Birrell dt_zalloc(dtrace_hdl_t *dtp, size_t size)
7596ff6d951SJohn Birrell {
7606ff6d951SJohn Birrell void *data;
7616ff6d951SJohn Birrell
7626ff6d951SJohn Birrell if ((data = malloc(size)) == NULL)
7636ff6d951SJohn Birrell (void) dt_set_errno(dtp, EDT_NOMEM);
7646ff6d951SJohn Birrell else
7656ff6d951SJohn Birrell bzero(data, size);
7666ff6d951SJohn Birrell
7676ff6d951SJohn Birrell return (data);
7686ff6d951SJohn Birrell }
7696ff6d951SJohn Birrell
7706ff6d951SJohn Birrell void *
dt_alloc(dtrace_hdl_t * dtp,size_t size)7716ff6d951SJohn Birrell dt_alloc(dtrace_hdl_t *dtp, size_t size)
7726ff6d951SJohn Birrell {
7736ff6d951SJohn Birrell void *data;
7746ff6d951SJohn Birrell
7756ff6d951SJohn Birrell if ((data = malloc(size)) == NULL)
7766ff6d951SJohn Birrell (void) dt_set_errno(dtp, EDT_NOMEM);
7776ff6d951SJohn Birrell
7786ff6d951SJohn Birrell return (data);
7796ff6d951SJohn Birrell }
7806ff6d951SJohn Birrell
7816ff6d951SJohn Birrell void
dt_free(dtrace_hdl_t * dtp,void * data)7826ff6d951SJohn Birrell dt_free(dtrace_hdl_t *dtp, void *data)
7836ff6d951SJohn Birrell {
7846ff6d951SJohn Birrell assert(dtp != NULL); /* ensure sane use of this interface */
7856ff6d951SJohn Birrell free(data);
7866ff6d951SJohn Birrell }
7876ff6d951SJohn Birrell
7886ff6d951SJohn Birrell void
dt_difo_free(dtrace_hdl_t * dtp,dtrace_difo_t * dp)7896ff6d951SJohn Birrell dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
7906ff6d951SJohn Birrell {
7916ff6d951SJohn Birrell if (dp == NULL)
7926ff6d951SJohn Birrell return; /* simplify caller code */
7936ff6d951SJohn Birrell
7946ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_buf);
7956ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_inttab);
7966ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_strtab);
7976ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_vartab);
7986ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_kreltab);
7996ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_ureltab);
8006ff6d951SJohn Birrell dt_free(dtp, dp->dtdo_xlmtab);
8016ff6d951SJohn Birrell
8026ff6d951SJohn Birrell dt_free(dtp, dp);
8036ff6d951SJohn Birrell }
8046ff6d951SJohn Birrell
8056ff6d951SJohn Birrell /*
8066ff6d951SJohn Birrell * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
8076ff6d951SJohn Birrell * implements the behavior that an empty pattern matches any string.
8086ff6d951SJohn Birrell */
8096ff6d951SJohn Birrell int
dt_gmatch(const char * s,const char * p)8106ff6d951SJohn Birrell dt_gmatch(const char *s, const char *p)
8116ff6d951SJohn Birrell {
8126ff6d951SJohn Birrell return (p == NULL || *p == '\0' || gmatch(s, p));
8136ff6d951SJohn Birrell }
8146ff6d951SJohn Birrell
8156ff6d951SJohn Birrell char *
dt_basename(char * str)8166ff6d951SJohn Birrell dt_basename(char *str)
8176ff6d951SJohn Birrell {
8186ff6d951SJohn Birrell char *last = strrchr(str, '/');
8196ff6d951SJohn Birrell
8206ff6d951SJohn Birrell if (last == NULL)
8216ff6d951SJohn Birrell return (str);
8226ff6d951SJohn Birrell
8236ff6d951SJohn Birrell return (last + 1);
8246ff6d951SJohn Birrell }
8256ff6d951SJohn Birrell
8266ff6d951SJohn Birrell /*
8276ff6d951SJohn Birrell * dt_popc() is a fast implementation of population count. The algorithm is
8286ff6d951SJohn Birrell * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
8296ff6d951SJohn Birrell */
8306ff6d951SJohn Birrell ulong_t
dt_popc(ulong_t x)8316ff6d951SJohn Birrell dt_popc(ulong_t x)
8326ff6d951SJohn Birrell {
8335f301949SBen Laurie #if defined(_ILP32)
8346ff6d951SJohn Birrell x = x - ((x >> 1) & 0x55555555UL);
8356ff6d951SJohn Birrell x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
8366ff6d951SJohn Birrell x = (x + (x >> 4)) & 0x0F0F0F0FUL;
8376ff6d951SJohn Birrell x = x + (x >> 8);
8386ff6d951SJohn Birrell x = x + (x >> 16);
8396ff6d951SJohn Birrell return (x & 0x3F);
8405f301949SBen Laurie #elif defined(_LP64)
8416ff6d951SJohn Birrell x = x - ((x >> 1) & 0x5555555555555555ULL);
8426ff6d951SJohn Birrell x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8436ff6d951SJohn Birrell x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
8446ff6d951SJohn Birrell x = x + (x >> 8);
8456ff6d951SJohn Birrell x = x + (x >> 16);
8466ff6d951SJohn Birrell x = x + (x >> 32);
8476ff6d951SJohn Birrell return (x & 0x7F);
8485f301949SBen Laurie #else
84986f222bbSSimon L. B. Nielsen /* This should be a #warning but for now ignore error. Err: "need td_popc() implementation" */
8506ff6d951SJohn Birrell #endif
8516ff6d951SJohn Birrell }
8526ff6d951SJohn Birrell
8536ff6d951SJohn Birrell /*
8546ff6d951SJohn Birrell * dt_popcb() is a bitmap-based version of population count that returns the
8556ff6d951SJohn Birrell * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
8566ff6d951SJohn Birrell */
8576ff6d951SJohn Birrell ulong_t
dt_popcb(const ulong_t * bp,ulong_t n)8586ff6d951SJohn Birrell dt_popcb(const ulong_t *bp, ulong_t n)
8596ff6d951SJohn Birrell {
8606ff6d951SJohn Birrell ulong_t maxb = n & BT_ULMASK;
8616ff6d951SJohn Birrell ulong_t maxw = n >> BT_ULSHIFT;
8626ff6d951SJohn Birrell ulong_t w, popc = 0;
8636ff6d951SJohn Birrell
8646ff6d951SJohn Birrell if (n == 0)
8656ff6d951SJohn Birrell return (0);
8666ff6d951SJohn Birrell
8676ff6d951SJohn Birrell for (w = 0; w < maxw; w++)
8686ff6d951SJohn Birrell popc += dt_popc(bp[w]);
8696ff6d951SJohn Birrell
8706ff6d951SJohn Birrell return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
8716ff6d951SJohn Birrell }
8726ff6d951SJohn Birrell
873bc96366cSSteven Hartland #ifdef illumos
8746ff6d951SJohn Birrell struct _rwlock;
8756ff6d951SJohn Birrell struct _lwp_mutex;
8766ff6d951SJohn Birrell
8776ff6d951SJohn Birrell int
dt_rw_read_held(pthread_rwlock_t * lock)8786ff6d951SJohn Birrell dt_rw_read_held(pthread_rwlock_t *lock)
8796ff6d951SJohn Birrell {
8806ff6d951SJohn Birrell extern int _rw_read_held(struct _rwlock *);
8816ff6d951SJohn Birrell return (_rw_read_held((struct _rwlock *)lock));
8826ff6d951SJohn Birrell }
8836ff6d951SJohn Birrell
8846ff6d951SJohn Birrell int
dt_rw_write_held(pthread_rwlock_t * lock)8856ff6d951SJohn Birrell dt_rw_write_held(pthread_rwlock_t *lock)
8866ff6d951SJohn Birrell {
8876ff6d951SJohn Birrell extern int _rw_write_held(struct _rwlock *);
8886ff6d951SJohn Birrell return (_rw_write_held((struct _rwlock *)lock));
8896ff6d951SJohn Birrell }
890b29602e4SJohn Birrell #endif
8916ff6d951SJohn Birrell
8926ff6d951SJohn Birrell int
dt_mutex_held(pthread_mutex_t * lock)8936ff6d951SJohn Birrell dt_mutex_held(pthread_mutex_t *lock)
8946ff6d951SJohn Birrell {
895bc96366cSSteven Hartland #ifdef illumos
8966ff6d951SJohn Birrell extern int _mutex_held(struct _lwp_mutex *);
8976ff6d951SJohn Birrell return (_mutex_held((struct _lwp_mutex *)lock));
898b29602e4SJohn Birrell #else
899b29602e4SJohn Birrell return (1);
900b29602e4SJohn Birrell #endif
9016ff6d951SJohn Birrell }
9026ff6d951SJohn Birrell
9036ff6d951SJohn Birrell static int
dt_string2str(char * s,char * str,int nbytes)9046ff6d951SJohn Birrell dt_string2str(char *s, char *str, int nbytes)
9056ff6d951SJohn Birrell {
9066ff6d951SJohn Birrell int len = strlen(s);
9076ff6d951SJohn Birrell
9086ff6d951SJohn Birrell if (nbytes == 0) {
9096ff6d951SJohn Birrell /*
9106ff6d951SJohn Birrell * Like snprintf(3C), we don't check the value of str if the
9116ff6d951SJohn Birrell * number of bytes is 0.
9126ff6d951SJohn Birrell */
9136ff6d951SJohn Birrell return (len);
9146ff6d951SJohn Birrell }
9156ff6d951SJohn Birrell
9166ff6d951SJohn Birrell if (nbytes <= len) {
9176ff6d951SJohn Birrell (void) strncpy(str, s, nbytes - 1);
9186ff6d951SJohn Birrell /*
9196ff6d951SJohn Birrell * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
9206ff6d951SJohn Birrell * that the string is null-terminated.
9216ff6d951SJohn Birrell */
9226ff6d951SJohn Birrell str[nbytes - 1] = '\0';
9236ff6d951SJohn Birrell } else {
9246ff6d951SJohn Birrell (void) strcpy(str, s);
9256ff6d951SJohn Birrell }
9266ff6d951SJohn Birrell
9276ff6d951SJohn Birrell return (len);
9286ff6d951SJohn Birrell }
9296ff6d951SJohn Birrell
9306ff6d951SJohn Birrell int
dtrace_addr2str(dtrace_hdl_t * dtp,uint64_t addr,char * str,int nbytes)9316ff6d951SJohn Birrell dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
9326ff6d951SJohn Birrell {
9336ff6d951SJohn Birrell dtrace_syminfo_t dts;
9346ff6d951SJohn Birrell GElf_Sym sym;
9356ff6d951SJohn Birrell
9366ff6d951SJohn Birrell size_t n = 20; /* for 0x%llx\0 */
9376ff6d951SJohn Birrell char *s;
9386ff6d951SJohn Birrell int err;
9396ff6d951SJohn Birrell
9406ff6d951SJohn Birrell if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
9416ff6d951SJohn Birrell n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
9426ff6d951SJohn Birrell
9436ff6d951SJohn Birrell s = alloca(n);
9446ff6d951SJohn Birrell
9456ff6d951SJohn Birrell if (err == 0 && addr != sym.st_value) {
9466ff6d951SJohn Birrell (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
9476ff6d951SJohn Birrell dts.dts_name, (u_longlong_t)addr - sym.st_value);
9486ff6d951SJohn Birrell } else if (err == 0) {
9496ff6d951SJohn Birrell (void) snprintf(s, n, "%s`%s",
9506ff6d951SJohn Birrell dts.dts_object, dts.dts_name);
9516ff6d951SJohn Birrell } else {
9526ff6d951SJohn Birrell /*
9536ff6d951SJohn Birrell * We'll repeat the lookup, but this time we'll specify a NULL
9546ff6d951SJohn Birrell * GElf_Sym -- indicating that we're only interested in the
9556ff6d951SJohn Birrell * containing module.
9566ff6d951SJohn Birrell */
9576ff6d951SJohn Birrell if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
9586ff6d951SJohn Birrell (void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
9596ff6d951SJohn Birrell (u_longlong_t)addr);
9606ff6d951SJohn Birrell } else {
9616ff6d951SJohn Birrell (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
9626ff6d951SJohn Birrell }
9636ff6d951SJohn Birrell }
9646ff6d951SJohn Birrell
9656ff6d951SJohn Birrell return (dt_string2str(s, str, nbytes));
9666ff6d951SJohn Birrell }
9676ff6d951SJohn Birrell
9686ff6d951SJohn Birrell int
dtrace_uaddr2str(dtrace_hdl_t * dtp,pid_t pid,uint64_t addr,char * str,int nbytes)9696ff6d951SJohn Birrell dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
9706ff6d951SJohn Birrell uint64_t addr, char *str, int nbytes)
9716ff6d951SJohn Birrell {
9726ff6d951SJohn Birrell char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
9736ff6d951SJohn Birrell struct ps_prochandle *P = NULL;
9746ff6d951SJohn Birrell GElf_Sym sym;
9756ff6d951SJohn Birrell char *obj;
9766ff6d951SJohn Birrell
9776ff6d951SJohn Birrell if (pid != 0)
9786ff6d951SJohn Birrell P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
9796ff6d951SJohn Birrell
9806ff6d951SJohn Birrell if (P == NULL) {
9815f301949SBen Laurie (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
9826ff6d951SJohn Birrell return (dt_string2str(c, str, nbytes));
9836ff6d951SJohn Birrell }
9846ff6d951SJohn Birrell
9856ff6d951SJohn Birrell dt_proc_lock(dtp, P);
9866ff6d951SJohn Birrell
9876ff6d951SJohn Birrell if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
9886ff6d951SJohn Birrell (void) Pobjname(P, addr, objname, sizeof (objname));
9896ff6d951SJohn Birrell
9906ff6d951SJohn Birrell obj = dt_basename(objname);
9916ff6d951SJohn Birrell
9926ff6d951SJohn Birrell if (addr > sym.st_value) {
9936ff6d951SJohn Birrell (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
9946ff6d951SJohn Birrell name, (u_longlong_t)(addr - sym.st_value));
9956ff6d951SJohn Birrell } else {
9966ff6d951SJohn Birrell (void) snprintf(c, sizeof (c), "%s`%s", obj, name);
9976ff6d951SJohn Birrell }
998b29602e4SJohn Birrell } else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
9995f301949SBen Laurie (void) snprintf(c, sizeof (c), "%s`0x%jx",
10005f301949SBen Laurie dt_basename(objname), (uintmax_t)addr);
10016ff6d951SJohn Birrell } else {
10025f301949SBen Laurie (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
10036ff6d951SJohn Birrell }
10046ff6d951SJohn Birrell
10056ff6d951SJohn Birrell dt_proc_unlock(dtp, P);
10066ff6d951SJohn Birrell dt_proc_release(dtp, P);
10076ff6d951SJohn Birrell
10086ff6d951SJohn Birrell return (dt_string2str(c, str, nbytes));
10096ff6d951SJohn Birrell }
101093f27766SDomagoj Stolfa
101193f27766SDomagoj Stolfa int
dtrace_oformat_configure(dtrace_hdl_t * dtp)101293f27766SDomagoj Stolfa dtrace_oformat_configure(dtrace_hdl_t *dtp)
101393f27766SDomagoj Stolfa {
101493f27766SDomagoj Stolfa
101593f27766SDomagoj Stolfa dtp->dt_oformat = xo_get_style(NULL) == XO_STYLE_TEXT ?
101693f27766SDomagoj Stolfa DTRACE_OFORMAT_TEXT :
101793f27766SDomagoj Stolfa DTRACE_OFORMAT_STRUCTURED;
101893f27766SDomagoj Stolfa xo_set_flags(NULL, XOF_DTRT);
101993f27766SDomagoj Stolfa return (0);
102093f27766SDomagoj Stolfa }
102193f27766SDomagoj Stolfa
102293f27766SDomagoj Stolfa int
dtrace_oformat(dtrace_hdl_t * dtp)102393f27766SDomagoj Stolfa dtrace_oformat(dtrace_hdl_t *dtp)
102493f27766SDomagoj Stolfa {
102593f27766SDomagoj Stolfa
102693f27766SDomagoj Stolfa return (dtp->dt_oformat != DTRACE_OFORMAT_TEXT);
102793f27766SDomagoj Stolfa }
102893f27766SDomagoj Stolfa
102993f27766SDomagoj Stolfa void
dtrace_set_outfp(const FILE * ofp)103093f27766SDomagoj Stolfa dtrace_set_outfp(const FILE *ofp)
103193f27766SDomagoj Stolfa {
103293f27766SDomagoj Stolfa
103393f27766SDomagoj Stolfa xo_set_file((FILE *)ofp);
103493f27766SDomagoj Stolfa }
103593f27766SDomagoj Stolfa
103693f27766SDomagoj Stolfa void
dtrace_oformat_setup(dtrace_hdl_t * dtp)103793f27766SDomagoj Stolfa dtrace_oformat_setup(dtrace_hdl_t *dtp)
103893f27766SDomagoj Stolfa {
103993f27766SDomagoj Stolfa
104093f27766SDomagoj Stolfa xo_open_container("dtrace");
104193f27766SDomagoj Stolfa xo_open_list("probes");
104293f27766SDomagoj Stolfa }
104393f27766SDomagoj Stolfa
104493f27766SDomagoj Stolfa void
dtrace_oformat_teardown(dtrace_hdl_t * dtp)104593f27766SDomagoj Stolfa dtrace_oformat_teardown(dtrace_hdl_t *dtp)
104693f27766SDomagoj Stolfa {
104793f27766SDomagoj Stolfa
104893f27766SDomagoj Stolfa xo_close_list("probes");
104993f27766SDomagoj Stolfa xo_close_container("dtrace");
105093f27766SDomagoj Stolfa }
1051