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