10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
511237SJonathan.Haslam@Sun.COM * Common Development and Distribution License (the "License").
611237SJonathan.Haslam@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
2111466SRoger.Faulkner@Sun.COM
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/sysmacros.h>
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <stddef.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <alloca.h>
370Sstevel@tonic-gate #include <assert.h>
380Sstevel@tonic-gate #include <libgen.h>
39457Sbmc #include <limits.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <dt_impl.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate static const struct {
440Sstevel@tonic-gate size_t dtps_offset;
450Sstevel@tonic-gate size_t dtps_len;
460Sstevel@tonic-gate } dtrace_probespecs[] = {
470Sstevel@tonic-gate { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN },
480Sstevel@tonic-gate { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN },
490Sstevel@tonic-gate { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN },
500Sstevel@tonic-gate { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN }
510Sstevel@tonic-gate };
520Sstevel@tonic-gate
530Sstevel@tonic-gate int
dtrace_xstr2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,int argc,char * const argv[],dtrace_probedesc_t * pdp)540Sstevel@tonic-gate dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
550Sstevel@tonic-gate const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
560Sstevel@tonic-gate {
5711237SJonathan.Haslam@Sun.COM size_t off, len, vlen, wlen;
5811237SJonathan.Haslam@Sun.COM const char *p, *q, *v, *w;
590Sstevel@tonic-gate
600Sstevel@tonic-gate char buf[32]; /* for id_t as %d (see below) */
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
630Sstevel@tonic-gate return (dt_set_errno(dtp, EINVAL));
640Sstevel@tonic-gate
650Sstevel@tonic-gate bzero(pdp, sizeof (dtrace_probedesc_t));
660Sstevel@tonic-gate p = s + strlen(s) - 1;
670Sstevel@tonic-gate
680Sstevel@tonic-gate do {
690Sstevel@tonic-gate for (len = 0; p >= s && *p != ':'; len++)
700Sstevel@tonic-gate p--; /* move backward until we find a delimiter */
710Sstevel@tonic-gate
720Sstevel@tonic-gate q = p + 1;
730Sstevel@tonic-gate vlen = 0;
7411237SJonathan.Haslam@Sun.COM w = NULL;
7511237SJonathan.Haslam@Sun.COM wlen = 0;
760Sstevel@tonic-gate
770Sstevel@tonic-gate if ((v = strchr(q, '$')) != NULL && v < q + len) {
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate * Set vlen to the length of the variable name and then
800Sstevel@tonic-gate * reset len to the length of the text prior to '$'. If
810Sstevel@tonic-gate * the name begins with a digit, interpret it using the
820Sstevel@tonic-gate * the argv[] array. Otherwise we look in dt_macros.
830Sstevel@tonic-gate * For the moment, all dt_macros variables are of type
840Sstevel@tonic-gate * id_t (see dtrace_update() for more details on that).
850Sstevel@tonic-gate */
860Sstevel@tonic-gate vlen = (size_t)(q + len - v);
870Sstevel@tonic-gate len = (size_t)(v - q);
880Sstevel@tonic-gate
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate * If the variable string begins with $$, skip past the
910Sstevel@tonic-gate * leading dollar sign since $ and $$ are equivalent
920Sstevel@tonic-gate * macro reference operators in a probe description.
930Sstevel@tonic-gate */
940Sstevel@tonic-gate if (vlen > 2 && v[1] == '$') {
950Sstevel@tonic-gate vlen--;
960Sstevel@tonic-gate v++;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (isdigit(v[1])) {
1000Sstevel@tonic-gate long i;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate errno = 0;
10311237SJonathan.Haslam@Sun.COM i = strtol(v + 1, (char **)&w, 10);
1040Sstevel@tonic-gate
10511237SJonathan.Haslam@Sun.COM wlen = vlen - (w - v);
10611237SJonathan.Haslam@Sun.COM
10711237SJonathan.Haslam@Sun.COM if (i < 0 || i >= argc || errno != 0)
1080Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADSPCV));
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate v = argv[i];
1110Sstevel@tonic-gate vlen = strlen(v);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (yypcb != NULL && yypcb->pcb_sargv == argv)
1140Sstevel@tonic-gate yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate } else if (vlen > 1) {
1170Sstevel@tonic-gate char *vstr = alloca(vlen);
1180Sstevel@tonic-gate dt_ident_t *idp;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate (void) strncpy(vstr, v + 1, vlen - 1);
1210Sstevel@tonic-gate vstr[vlen - 1] = '\0';
1220Sstevel@tonic-gate idp = dt_idhash_lookup(dtp->dt_macros, vstr);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (idp == NULL)
1250Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADSPCV));
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate v = buf;
1280Sstevel@tonic-gate vlen = snprintf(buf, 32, "%d", idp->di_id);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate } else
1310Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADSPCV));
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (spec == DTRACE_PROBESPEC_NONE)
1350Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADSPEC));
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate if (len + vlen >= dtrace_probespecs[spec].dtps_len)
1380Sstevel@tonic-gate return (dt_set_errno(dtp, ENAMETOOLONG));
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate off = dtrace_probespecs[spec--].dtps_offset;
1410Sstevel@tonic-gate bcopy(q, (char *)pdp + off, len);
1420Sstevel@tonic-gate bcopy(v, (char *)pdp + off + len, vlen);
14311237SJonathan.Haslam@Sun.COM bcopy(w, (char *)pdp + off + len + vlen, wlen);
1440Sstevel@tonic-gate } while (--p >= s);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate pdp->dtpd_id = DTRACE_IDNONE;
1470Sstevel@tonic-gate return (0);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate int
dtrace_str2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,dtrace_probedesc_t * pdp)1510Sstevel@tonic-gate dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
1520Sstevel@tonic-gate const char *s, dtrace_probedesc_t *pdp)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate int
dtrace_id2desc(dtrace_hdl_t * dtp,dtrace_id_t id,dtrace_probedesc_t * pdp)1580Sstevel@tonic-gate dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate bzero(pdp, sizeof (dtrace_probedesc_t));
1610Sstevel@tonic-gate pdp->dtpd_id = id;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
1640Sstevel@tonic-gate pdp->dtpd_id != id)
1650Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_BADID));
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate return (0);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate char *
dtrace_desc2str(const dtrace_probedesc_t * pdp,char * buf,size_t len)1710Sstevel@tonic-gate dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate if (pdp->dtpd_id == 0) {
1740Sstevel@tonic-gate (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
1750Sstevel@tonic-gate pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
1760Sstevel@tonic-gate } else
1770Sstevel@tonic-gate (void) snprintf(buf, len, "%u", pdp->dtpd_id);
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate return (buf);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate char *
dtrace_attr2str(dtrace_attribute_t attr,char * buf,size_t len)1830Sstevel@tonic-gate dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate const char *name = dtrace_stability_name(attr.dtat_name);
1860Sstevel@tonic-gate const char *data = dtrace_stability_name(attr.dtat_data);
1870Sstevel@tonic-gate const char *class = dtrace_class_name(attr.dtat_class);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (name == NULL || data == NULL || class == NULL)
1900Sstevel@tonic-gate return (NULL); /* one or more invalid attributes */
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate (void) snprintf(buf, len, "%s/%s/%s", name, data, class);
1930Sstevel@tonic-gate return (buf);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate static char *
dt_getstrattr(char * p,char ** qp)1970Sstevel@tonic-gate dt_getstrattr(char *p, char **qp)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate char *q;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (*p == '\0')
2020Sstevel@tonic-gate return (NULL);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if ((q = strchr(p, '/')) == NULL)
2050Sstevel@tonic-gate q = p + strlen(p);
2060Sstevel@tonic-gate else
2070Sstevel@tonic-gate *q++ = '\0';
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate *qp = q;
2100Sstevel@tonic-gate return (p);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate int
dtrace_str2attr(const char * str,dtrace_attribute_t * attr)2140Sstevel@tonic-gate dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate dtrace_stability_t s;
2170Sstevel@tonic-gate dtrace_class_t c;
2180Sstevel@tonic-gate char *p, *q;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (str == NULL || attr == NULL)
2210Sstevel@tonic-gate return (-1); /* invalid function arguments */
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate *attr = _dtrace_maxattr;
224*13093SRoger.Faulkner@Oracle.COM p = strdupa(str);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if ((p = dt_getstrattr(p, &q)) == NULL)
2270Sstevel@tonic-gate return (0);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2300Sstevel@tonic-gate if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2310Sstevel@tonic-gate attr->dtat_name = s;
2320Sstevel@tonic-gate break;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (s > DTRACE_STABILITY_MAX)
2370Sstevel@tonic-gate return (-1);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if ((p = dt_getstrattr(q, &q)) == NULL)
2400Sstevel@tonic-gate return (0);
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2430Sstevel@tonic-gate if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2440Sstevel@tonic-gate attr->dtat_data = s;
2450Sstevel@tonic-gate break;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (s > DTRACE_STABILITY_MAX)
2500Sstevel@tonic-gate return (-1);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if ((p = dt_getstrattr(q, &q)) == NULL)
2530Sstevel@tonic-gate return (0);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
2560Sstevel@tonic-gate if (strcasecmp(p, dtrace_class_name(c)) == 0) {
2570Sstevel@tonic-gate attr->dtat_class = c;
2580Sstevel@tonic-gate break;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
2630Sstevel@tonic-gate return (-1);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate const char *
dtrace_stability_name(dtrace_stability_t s)2690Sstevel@tonic-gate dtrace_stability_name(dtrace_stability_t s)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate switch (s) {
2720Sstevel@tonic-gate case DTRACE_STABILITY_INTERNAL: return ("Internal");
2730Sstevel@tonic-gate case DTRACE_STABILITY_PRIVATE: return ("Private");
2740Sstevel@tonic-gate case DTRACE_STABILITY_OBSOLETE: return ("Obsolete");
2750Sstevel@tonic-gate case DTRACE_STABILITY_EXTERNAL: return ("External");
2760Sstevel@tonic-gate case DTRACE_STABILITY_UNSTABLE: return ("Unstable");
2770Sstevel@tonic-gate case DTRACE_STABILITY_EVOLVING: return ("Evolving");
2780Sstevel@tonic-gate case DTRACE_STABILITY_STABLE: return ("Stable");
2790Sstevel@tonic-gate case DTRACE_STABILITY_STANDARD: return ("Standard");
2800Sstevel@tonic-gate default: return (NULL);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate const char *
dtrace_class_name(dtrace_class_t c)2850Sstevel@tonic-gate dtrace_class_name(dtrace_class_t c)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate switch (c) {
2880Sstevel@tonic-gate case DTRACE_CLASS_UNKNOWN: return ("Unknown");
2890Sstevel@tonic-gate case DTRACE_CLASS_CPU: return ("CPU");
2900Sstevel@tonic-gate case DTRACE_CLASS_PLATFORM: return ("Platform");
2910Sstevel@tonic-gate case DTRACE_CLASS_GROUP: return ("Group");
2920Sstevel@tonic-gate case DTRACE_CLASS_ISA: return ("ISA");
2930Sstevel@tonic-gate case DTRACE_CLASS_COMMON: return ("Common");
2940Sstevel@tonic-gate default: return (NULL);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate dtrace_attribute_t
dt_attr_min(dtrace_attribute_t a1,dtrace_attribute_t a2)2990Sstevel@tonic-gate dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate dtrace_attribute_t am;
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
3040Sstevel@tonic-gate am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
3050Sstevel@tonic-gate am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate return (am);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate dtrace_attribute_t
dt_attr_max(dtrace_attribute_t a1,dtrace_attribute_t a2)3110Sstevel@tonic-gate dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate dtrace_attribute_t am;
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
3160Sstevel@tonic-gate am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
3170Sstevel@tonic-gate am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate return (am);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate * Compare two attributes and return an integer value in the following ranges:
3240Sstevel@tonic-gate *
3250Sstevel@tonic-gate * <0 if any of a1's attributes are less than a2's attributes
3260Sstevel@tonic-gate * =0 if all of a1's attributes are equal to a2's attributes
3270Sstevel@tonic-gate * >0 if all of a1's attributes are greater than or equal to a2's attributes
3280Sstevel@tonic-gate *
3290Sstevel@tonic-gate * To implement this function efficiently, we subtract a2's attributes from
3300Sstevel@tonic-gate * a1's to obtain a negative result if an a1 attribute is less than its a2
3310Sstevel@tonic-gate * counterpart. We then OR the intermediate results together, relying on the
3320Sstevel@tonic-gate * twos-complement property that if any result is negative, the bitwise union
3330Sstevel@tonic-gate * will also be negative since the highest bit will be set in the result.
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate int
dt_attr_cmp(dtrace_attribute_t a1,dtrace_attribute_t a2)3360Sstevel@tonic-gate dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate return (((int)a1.dtat_name - a2.dtat_name) |
3390Sstevel@tonic-gate ((int)a1.dtat_data - a2.dtat_data) |
3400Sstevel@tonic-gate ((int)a1.dtat_class - a2.dtat_class));
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate char *
dt_attr_str(dtrace_attribute_t a,char * buf,size_t len)3440Sstevel@tonic-gate dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate static const char stability[] = "ipoxuesS";
3470Sstevel@tonic-gate static const char class[] = "uCpgIc";
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate if (a.dtat_name < sizeof (stability) &&
3500Sstevel@tonic-gate a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
3510Sstevel@tonic-gate (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
3520Sstevel@tonic-gate stability[a.dtat_data], class[a.dtat_class]);
3530Sstevel@tonic-gate } else {
3540Sstevel@tonic-gate (void) snprintf(buf, len, "[%u/%u/%u]",
3550Sstevel@tonic-gate a.dtat_name, a.dtat_data, a.dtat_class);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate return (buf);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate char *
dt_version_num2str(dt_version_t v,char * buf,size_t len)3620Sstevel@tonic-gate dt_version_num2str(dt_version_t v, char *buf, size_t len)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate uint_t M = DT_VERSION_MAJOR(v);
3650Sstevel@tonic-gate uint_t m = DT_VERSION_MINOR(v);
3660Sstevel@tonic-gate uint_t u = DT_VERSION_MICRO(v);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate if (u == 0)
3690Sstevel@tonic-gate (void) snprintf(buf, len, "%u.%u", M, m);
3700Sstevel@tonic-gate else
3710Sstevel@tonic-gate (void) snprintf(buf, len, "%u.%u.%u", M, m, u);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate return (buf);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate int
dt_version_str2num(const char * s,dt_version_t * vp)3770Sstevel@tonic-gate dt_version_str2num(const char *s, dt_version_t *vp)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate int i = 0, n[3] = { 0, 0, 0 };
3800Sstevel@tonic-gate char c;
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate while ((c = *s++) != '\0') {
3830Sstevel@tonic-gate if (isdigit(c))
3840Sstevel@tonic-gate n[i] = n[i] * 10 + c - '0';
3850Sstevel@tonic-gate else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
3860Sstevel@tonic-gate return (-1);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate if (n[0] > DT_VERSION_MAJMAX ||
3900Sstevel@tonic-gate n[1] > DT_VERSION_MINMAX ||
3910Sstevel@tonic-gate n[2] > DT_VERSION_MICMAX)
3920Sstevel@tonic-gate return (-1);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (vp != NULL)
3950Sstevel@tonic-gate *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate return (0);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate int
dt_version_defined(dt_version_t v)4010Sstevel@tonic-gate dt_version_defined(dt_version_t v)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate int i;
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate for (i = 0; _dtrace_versions[i] != 0; i++) {
4060Sstevel@tonic-gate if (_dtrace_versions[i] == v)
4070Sstevel@tonic-gate return (1);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate return (0);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate char *
dt_cpp_add_arg(dtrace_hdl_t * dtp,const char * str)4140Sstevel@tonic-gate dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate char *arg;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
4190Sstevel@tonic-gate int olds = dtp->dt_cpp_args;
4200Sstevel@tonic-gate int news = olds * 2;
4210Sstevel@tonic-gate char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate if (argv == NULL)
4240Sstevel@tonic-gate return (NULL);
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate bzero(&argv[olds], sizeof (char *) * olds);
4270Sstevel@tonic-gate dtp->dt_cpp_argv = argv;
4280Sstevel@tonic-gate dtp->dt_cpp_args = news;
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate if ((arg = strdup(str)) == NULL)
4320Sstevel@tonic-gate return (NULL);
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
4350Sstevel@tonic-gate dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
4360Sstevel@tonic-gate return (arg);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate char *
dt_cpp_pop_arg(dtrace_hdl_t * dtp)4400Sstevel@tonic-gate dt_cpp_pop_arg(dtrace_hdl_t *dtp)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate char *arg;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate if (dtp->dt_cpp_argc <= 1)
4450Sstevel@tonic-gate return (NULL); /* dt_cpp_argv[0] cannot be popped */
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
4480Sstevel@tonic-gate dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate return (arg);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /*PRINTFLIKE1*/
4540Sstevel@tonic-gate void
dt_dprintf(const char * format,...)4550Sstevel@tonic-gate dt_dprintf(const char *format, ...)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate if (_dtrace_debug) {
4580Sstevel@tonic-gate va_list alist;
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate va_start(alist, format);
4610Sstevel@tonic-gate (void) fputs("libdtrace DEBUG: ", stderr);
4620Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
4630Sstevel@tonic-gate va_end(alist);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate int
dt_ioctl(dtrace_hdl_t * dtp,int val,void * arg)4680Sstevel@tonic-gate dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector;
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate if (v != NULL)
4730Sstevel@tonic-gate return (v->dtv_ioctl(dtp->dt_varg, val, arg));
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (dtp->dt_fd >= 0)
4760Sstevel@tonic-gate return (ioctl(dtp->dt_fd, val, arg));
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate errno = EBADF;
4790Sstevel@tonic-gate return (-1);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate int
dt_status(dtrace_hdl_t * dtp,processorid_t cpu)4830Sstevel@tonic-gate dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector;
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate if (v == NULL)
4880Sstevel@tonic-gate return (p_online(cpu, P_STATUS));
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate return (v->dtv_status(dtp->dt_varg, cpu));
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate long
dt_sysconf(dtrace_hdl_t * dtp,int name)4940Sstevel@tonic-gate dt_sysconf(dtrace_hdl_t *dtp, int name)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate if (v == NULL)
4990Sstevel@tonic-gate return (sysconf(name));
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate return (v->dtv_sysconf(dtp->dt_varg, name));
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate * Wrapper around write(2) to handle partial writes. For maximum safety of
5060Sstevel@tonic-gate * output files and proper error reporting, we continuing writing in the
5070Sstevel@tonic-gate * face of partial writes until write(2) fails or 'buf' is completely written.
5080Sstevel@tonic-gate * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
5090Sstevel@tonic-gate */
5100Sstevel@tonic-gate ssize_t
dt_write(dtrace_hdl_t * dtp,int fd,const void * buf,size_t n)5110Sstevel@tonic-gate dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate ssize_t resid = n;
5140Sstevel@tonic-gate ssize_t len;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate while (resid != 0) {
5170Sstevel@tonic-gate if ((len = write(fd, buf, resid)) <= 0)
5180Sstevel@tonic-gate break;
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate resid -= len;
5210Sstevel@tonic-gate buf = (char *)buf + len;
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate if (resid == n && n != 0)
5250Sstevel@tonic-gate return (dt_set_errno(dtp, errno));
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate return (n - resid);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate * This function handles all output from libdtrace, as well as the
5320Sstevel@tonic-gate * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then
5330Sstevel@tonic-gate * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
5340Sstevel@tonic-gate * specified buffer and return. Otherwise, if output is buffered (denoted by
5350Sstevel@tonic-gate * a NULL fp), we sprintf the desired output into the buffered buffer
5360Sstevel@tonic-gate * (expanding the buffer if required). If we don't satisfy either of these
5370Sstevel@tonic-gate * conditions (that is, if we are to actually generate output), then we call
5380Sstevel@tonic-gate * fprintf with the specified fp. In this case, we need to deal with one of
5390Sstevel@tonic-gate * the more annoying peculiarities of libc's printf routines: any failed
5400Sstevel@tonic-gate * write persistently sets an error flag inside the FILE causing every
5410Sstevel@tonic-gate * subsequent write to fail, but only the caller that initiated the error gets
5420Sstevel@tonic-gate * the errno. Since libdtrace clients often intercept SIGINT, this case is
5430Sstevel@tonic-gate * particularly frustrating since we don't want the EINTR on one attempt to
5440Sstevel@tonic-gate * write to the output file to preclude later attempts to write. This
5450Sstevel@tonic-gate * function therefore does a clearerr() if any error occurred, and saves the
5460Sstevel@tonic-gate * errno for the caller inside the specified dtrace_hdl_t.
5470Sstevel@tonic-gate */
5480Sstevel@tonic-gate /*PRINTFLIKE3*/
5490Sstevel@tonic-gate int
dt_printf(dtrace_hdl_t * dtp,FILE * fp,const char * format,...)5500Sstevel@tonic-gate dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate va_list ap;
5530Sstevel@tonic-gate int n;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate va_start(ap, format);
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate if (dtp->dt_sprintf_buflen != 0) {
5580Sstevel@tonic-gate int len;
5590Sstevel@tonic-gate char *buf;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate assert(dtp->dt_sprintf_buf != NULL);
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
5640Sstevel@tonic-gate len = dtp->dt_sprintf_buflen - len;
5650Sstevel@tonic-gate assert(len >= 0);
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate if ((n = vsnprintf(buf, len, format, ap)) < 0)
5680Sstevel@tonic-gate n = dt_set_errno(dtp, errno);
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate va_end(ap);
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate return (n);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate if (fp == NULL) {
5760Sstevel@tonic-gate int needed, rval;
5770Sstevel@tonic-gate size_t avail;
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate * It's not legal to use buffered ouput if there is not a
5810Sstevel@tonic-gate * handler for buffered output.
5820Sstevel@tonic-gate */
5830Sstevel@tonic-gate if (dtp->dt_bufhdlr == NULL) {
5840Sstevel@tonic-gate va_end(ap);
5850Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOBUFFERED));
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (dtp->dt_buffered_buf == NULL) {
5890Sstevel@tonic-gate assert(dtp->dt_buffered_size == 0);
5900Sstevel@tonic-gate dtp->dt_buffered_size = 1;
5910Sstevel@tonic-gate dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate if (dtp->dt_buffered_buf == NULL) {
5940Sstevel@tonic-gate va_end(ap);
5950Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM));
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate dtp->dt_buffered_offs = 0;
5990Sstevel@tonic-gate dtp->dt_buffered_buf[0] = '\0';
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
6030Sstevel@tonic-gate rval = dt_set_errno(dtp, errno);
6040Sstevel@tonic-gate va_end(ap);
6050Sstevel@tonic-gate return (rval);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate if (needed == 0) {
6090Sstevel@tonic-gate va_end(ap);
6100Sstevel@tonic-gate return (0);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate for (;;) {
6140Sstevel@tonic-gate char *newbuf;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
6170Sstevel@tonic-gate avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate if (needed + 1 < avail)
6200Sstevel@tonic-gate break;
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate if ((newbuf = realloc(dtp->dt_buffered_buf,
6230Sstevel@tonic-gate dtp->dt_buffered_size << 1)) == NULL) {
6240Sstevel@tonic-gate va_end(ap);
6250Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM));
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate dtp->dt_buffered_buf = newbuf;
6290Sstevel@tonic-gate dtp->dt_buffered_size <<= 1;
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
6330Sstevel@tonic-gate avail, format, ap) < 0) {
6340Sstevel@tonic-gate rval = dt_set_errno(dtp, errno);
6350Sstevel@tonic-gate va_end(ap);
6360Sstevel@tonic-gate return (rval);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate dtp->dt_buffered_offs += needed;
6400Sstevel@tonic-gate assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
6410Sstevel@tonic-gate return (0);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate n = vfprintf(fp, format, ap);
6450Sstevel@tonic-gate va_end(ap);
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate if (n < 0) {
6480Sstevel@tonic-gate clearerr(fp);
6490Sstevel@tonic-gate return (dt_set_errno(dtp, errno));
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate return (n);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate 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)6560Sstevel@tonic-gate dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
6571017Sbmc const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate dtrace_bufdata_t data;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate if (dtp->dt_buffered_offs == 0)
6620Sstevel@tonic-gate return (0);
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate data.dtbda_handle = dtp;
6650Sstevel@tonic-gate data.dtbda_buffered = dtp->dt_buffered_buf;
6660Sstevel@tonic-gate data.dtbda_probe = pdata;
6670Sstevel@tonic-gate data.dtbda_recdesc = rec;
6680Sstevel@tonic-gate data.dtbda_aggdata = agg;
6691017Sbmc data.dtbda_flags = flags;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
6720Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_DIRABORT));
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate dtp->dt_buffered_offs = 0;
6750Sstevel@tonic-gate dtp->dt_buffered_buf[0] = '\0';
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate return (0);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate void
dt_buffered_destroy(dtrace_hdl_t * dtp)6810Sstevel@tonic-gate dt_buffered_destroy(dtrace_hdl_t *dtp)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate free(dtp->dt_buffered_buf);
6840Sstevel@tonic-gate dtp->dt_buffered_buf = NULL;
6850Sstevel@tonic-gate dtp->dt_buffered_offs = 0;
6860Sstevel@tonic-gate dtp->dt_buffered_size = 0;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate void *
dt_zalloc(dtrace_hdl_t * dtp,size_t size)6900Sstevel@tonic-gate dt_zalloc(dtrace_hdl_t *dtp, size_t size)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate void *data;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate if ((data = malloc(size)) == NULL)
6950Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM);
6960Sstevel@tonic-gate else
6970Sstevel@tonic-gate bzero(data, size);
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate return (data);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate void *
dt_alloc(dtrace_hdl_t * dtp,size_t size)7030Sstevel@tonic-gate dt_alloc(dtrace_hdl_t *dtp, size_t size)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate void *data;
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if ((data = malloc(size)) == NULL)
7080Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate return (data);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate void
dt_free(dtrace_hdl_t * dtp,void * data)7140Sstevel@tonic-gate dt_free(dtrace_hdl_t *dtp, void *data)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate assert(dtp != NULL); /* ensure sane use of this interface */
7170Sstevel@tonic-gate free(data);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate
720265Smws void
dt_difo_free(dtrace_hdl_t * dtp,dtrace_difo_t * dp)721265Smws dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
722265Smws {
723265Smws if (dp == NULL)
724265Smws return; /* simplify caller code */
725265Smws
726265Smws dt_free(dtp, dp->dtdo_buf);
727265Smws dt_free(dtp, dp->dtdo_inttab);
728265Smws dt_free(dtp, dp->dtdo_strtab);
729265Smws dt_free(dtp, dp->dtdo_vartab);
730265Smws dt_free(dtp, dp->dtdo_kreltab);
731265Smws dt_free(dtp, dp->dtdo_ureltab);
732265Smws dt_free(dtp, dp->dtdo_xlmtab);
733265Smws
734265Smws dt_free(dtp, dp);
735265Smws }
736265Smws
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
7390Sstevel@tonic-gate * implements the behavior that an empty pattern matches any string.
7400Sstevel@tonic-gate */
7410Sstevel@tonic-gate int
dt_gmatch(const char * s,const char * p)7420Sstevel@tonic-gate dt_gmatch(const char *s, const char *p)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate return (p == NULL || *p == '\0' || gmatch(s, p));
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate char *
dt_basename(char * str)7480Sstevel@tonic-gate dt_basename(char *str)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate char *last = strrchr(str, '/');
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (last == NULL)
7530Sstevel@tonic-gate return (str);
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate return (last + 1);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate
758265Smws /*
759265Smws * dt_popc() is a fast implementation of population count. The algorithm is
760265Smws * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
761265Smws */
762265Smws ulong_t
dt_popc(ulong_t x)763265Smws dt_popc(ulong_t x)
764265Smws {
765265Smws #ifdef _ILP32
766265Smws x = x - ((x >> 1) & 0x55555555UL);
767265Smws x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
768265Smws x = (x + (x >> 4)) & 0x0F0F0F0FUL;
769265Smws x = x + (x >> 8);
770265Smws x = x + (x >> 16);
771265Smws return (x & 0x3F);
772265Smws #endif
773265Smws #ifdef _LP64
774265Smws x = x - ((x >> 1) & 0x5555555555555555ULL);
775265Smws x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
776265Smws x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
777265Smws x = x + (x >> 8);
778265Smws x = x + (x >> 16);
779265Smws x = x + (x >> 32);
780265Smws return (x & 0x7F);
781265Smws #endif
782265Smws }
783265Smws
784265Smws /*
785265Smws * dt_popcb() is a bitmap-based version of population count that returns the
786265Smws * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
787265Smws */
788265Smws ulong_t
dt_popcb(const ulong_t * bp,ulong_t n)789265Smws dt_popcb(const ulong_t *bp, ulong_t n)
790265Smws {
791265Smws ulong_t maxb = n & BT_ULMASK;
792265Smws ulong_t maxw = n >> BT_ULSHIFT;
793265Smws ulong_t w, popc = 0;
794265Smws
795265Smws if (n == 0)
796265Smws return (0);
797265Smws
798265Smws for (w = 0; w < maxw; w++)
799265Smws popc += dt_popc(bp[w]);
800265Smws
801265Smws return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
802265Smws }
803265Smws
804457Sbmc static int
dt_string2str(char * s,char * str,int nbytes)805457Sbmc dt_string2str(char *s, char *str, int nbytes)
806457Sbmc {
807457Sbmc int len = strlen(s);
808457Sbmc
809457Sbmc if (nbytes == 0) {
810457Sbmc /*
811457Sbmc * Like snprintf(3C), we don't check the value of str if the
812457Sbmc * number of bytes is 0.
813457Sbmc */
814457Sbmc return (len);
815457Sbmc }
816457Sbmc
817457Sbmc if (nbytes <= len) {
818457Sbmc (void) strncpy(str, s, nbytes - 1);
819457Sbmc /*
820457Sbmc * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
821457Sbmc * that the string is null-terminated.
822457Sbmc */
823457Sbmc str[nbytes - 1] = '\0';
824457Sbmc } else {
825457Sbmc (void) strcpy(str, s);
826457Sbmc }
827457Sbmc
828457Sbmc return (len);
829457Sbmc }
830457Sbmc
831457Sbmc int
dtrace_addr2str(dtrace_hdl_t * dtp,uint64_t addr,char * str,int nbytes)832457Sbmc dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
833457Sbmc {
834457Sbmc dtrace_syminfo_t dts;
835457Sbmc GElf_Sym sym;
836457Sbmc
837457Sbmc size_t n = 20; /* for 0x%llx\0 */
838457Sbmc char *s;
839457Sbmc int err;
840457Sbmc
841457Sbmc if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
842457Sbmc n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
843457Sbmc
844457Sbmc s = alloca(n);
845457Sbmc
846457Sbmc if (err == 0 && addr != sym.st_value) {
847457Sbmc (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
848457Sbmc dts.dts_name, (u_longlong_t)addr - sym.st_value);
849457Sbmc } else if (err == 0) {
850457Sbmc (void) snprintf(s, n, "%s`%s",
851457Sbmc dts.dts_object, dts.dts_name);
852457Sbmc } else {
853457Sbmc /*
854457Sbmc * We'll repeat the lookup, but this time we'll specify a NULL
855457Sbmc * GElf_Sym -- indicating that we're only interested in the
856457Sbmc * containing module.
857457Sbmc */
858457Sbmc if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
859457Sbmc (void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
860457Sbmc (u_longlong_t)addr);
861457Sbmc } else {
862457Sbmc (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
863457Sbmc }
864457Sbmc }
865457Sbmc
866457Sbmc return (dt_string2str(s, str, nbytes));
867457Sbmc }
868457Sbmc
869457Sbmc int
dtrace_uaddr2str(dtrace_hdl_t * dtp,pid_t pid,uint64_t addr,char * str,int nbytes)870457Sbmc dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
871457Sbmc uint64_t addr, char *str, int nbytes)
872457Sbmc {
873457Sbmc char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
874457Sbmc struct ps_prochandle *P = NULL;
875457Sbmc GElf_Sym sym;
876457Sbmc char *obj;
877457Sbmc
878457Sbmc if (pid != 0)
879457Sbmc P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
880457Sbmc
881457Sbmc if (P == NULL) {
882457Sbmc (void) snprintf(c, sizeof (c), "0x%llx", addr);
883457Sbmc return (dt_string2str(c, str, nbytes));
884457Sbmc }
885457Sbmc
886457Sbmc dt_proc_lock(dtp, P);
887457Sbmc
888457Sbmc if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
889457Sbmc (void) Pobjname(P, addr, objname, sizeof (objname));
890457Sbmc
891457Sbmc obj = dt_basename(objname);
892457Sbmc
893457Sbmc if (addr > sym.st_value) {
894457Sbmc (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
895457Sbmc name, (u_longlong_t)(addr - sym.st_value));
896457Sbmc } else {
897457Sbmc (void) snprintf(c, sizeof (c), "%s`%s", obj, name);
898457Sbmc }
899457Sbmc } else if (Pobjname(P, addr, objname, sizeof (objname)) != NULL) {
900457Sbmc (void) snprintf(c, sizeof (c), "%s`0x%llx",
901457Sbmc dt_basename(objname), addr);
902457Sbmc } else {
903457Sbmc (void) snprintf(c, sizeof (c), "0x%llx", addr);
904457Sbmc }
905457Sbmc
906457Sbmc dt_proc_unlock(dtp, P);
907457Sbmc dt_proc_release(dtp, P);
908457Sbmc
909457Sbmc return (dt_string2str(c, str, nbytes));
910457Sbmc }
911