17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
558dbc507SJonathan Haslam  * Common Development and Distribution License (the "License").
658dbc507SJonathan Haslam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2153f3aea0SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
25*6eeafb34SRobert Mustacchi  * Copyright 2022 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <stddef.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <alloca.h>
397c478bd9Sstevel@tonic-gate #include <assert.h>
407c478bd9Sstevel@tonic-gate #include <libgen.h>
41a1b5e537Sbmc #include <limits.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <dt_impl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static const struct {
467c478bd9Sstevel@tonic-gate 	size_t dtps_offset;
477c478bd9Sstevel@tonic-gate 	size_t dtps_len;
487c478bd9Sstevel@tonic-gate } dtrace_probespecs[] = {
497c478bd9Sstevel@tonic-gate 	{ offsetof(dtrace_probedesc_t, dtpd_provider),	DTRACE_PROVNAMELEN },
507c478bd9Sstevel@tonic-gate 	{ offsetof(dtrace_probedesc_t, dtpd_mod),	DTRACE_MODNAMELEN },
517c478bd9Sstevel@tonic-gate 	{ offsetof(dtrace_probedesc_t, dtpd_func),	DTRACE_FUNCNAMELEN },
527c478bd9Sstevel@tonic-gate 	{ offsetof(dtrace_probedesc_t, dtpd_name),	DTRACE_NAMELEN }
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@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)567c478bd9Sstevel@tonic-gate dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
577c478bd9Sstevel@tonic-gate     const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
587c478bd9Sstevel@tonic-gate {
5958dbc507SJonathan Haslam 	size_t off, len, vlen, wlen;
6058dbc507SJonathan Haslam 	const char *p, *q, *v, *w;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	char buf[32]; /* for id_t as %d (see below) */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
657c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	bzero(pdp, sizeof (dtrace_probedesc_t));
687c478bd9Sstevel@tonic-gate 	p = s + strlen(s) - 1;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	do {
717c478bd9Sstevel@tonic-gate 		for (len = 0; p >= s && *p != ':'; len++)
727c478bd9Sstevel@tonic-gate 			p--; /* move backward until we find a delimiter */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		q = p + 1;
757c478bd9Sstevel@tonic-gate 		vlen = 0;
7658dbc507SJonathan Haslam 		w = NULL;
7758dbc507SJonathan Haslam 		wlen = 0;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		if ((v = strchr(q, '$')) != NULL && v < q + len) {
807c478bd9Sstevel@tonic-gate 			/*
817c478bd9Sstevel@tonic-gate 			 * Set vlen to the length of the variable name and then
827c478bd9Sstevel@tonic-gate 			 * reset len to the length of the text prior to '$'. If
837c478bd9Sstevel@tonic-gate 			 * the name begins with a digit, interpret it using the
847c478bd9Sstevel@tonic-gate 			 * the argv[] array.  Otherwise we look in dt_macros.
857c478bd9Sstevel@tonic-gate 			 * For the moment, all dt_macros variables are of type
867c478bd9Sstevel@tonic-gate 			 * id_t (see dtrace_update() for more details on that).
877c478bd9Sstevel@tonic-gate 			 */
887c478bd9Sstevel@tonic-gate 			vlen = (size_t)(q + len - v);
897c478bd9Sstevel@tonic-gate 			len = (size_t)(v - q);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 			/*
927c478bd9Sstevel@tonic-gate 			 * If the variable string begins with $$, skip past the
937c478bd9Sstevel@tonic-gate 			 * leading dollar sign since $ and $$ are equivalent
947c478bd9Sstevel@tonic-gate 			 * macro reference operators in a probe description.
957c478bd9Sstevel@tonic-gate 			 */
967c478bd9Sstevel@tonic-gate 			if (vlen > 2 && v[1] == '$') {
977c478bd9Sstevel@tonic-gate 				vlen--;
987c478bd9Sstevel@tonic-gate 				v++;
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			if (isdigit(v[1])) {
1027c478bd9Sstevel@tonic-gate 				long i;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 				errno = 0;
10558dbc507SJonathan Haslam 				i = strtol(v + 1, (char **)&w, 10);
1067c478bd9Sstevel@tonic-gate 
10758dbc507SJonathan Haslam 				wlen = vlen - (w - v);
10858dbc507SJonathan Haslam 
10958dbc507SJonathan Haslam 				if (i < 0 || i >= argc || errno != 0)
1107c478bd9Sstevel@tonic-gate 					return (dt_set_errno(dtp, EDT_BADSPCV));
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 				v = argv[i];
1137c478bd9Sstevel@tonic-gate 				vlen = strlen(v);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 				if (yypcb != NULL && yypcb->pcb_sargv == argv)
1167c478bd9Sstevel@tonic-gate 					yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 			} else if (vlen > 1) {
1197c478bd9Sstevel@tonic-gate 				char *vstr = alloca(vlen);
1207c478bd9Sstevel@tonic-gate 				dt_ident_t *idp;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 				(void) strncpy(vstr, v + 1, vlen - 1);
1237c478bd9Sstevel@tonic-gate 				vstr[vlen - 1] = '\0';
1247c478bd9Sstevel@tonic-gate 				idp = dt_idhash_lookup(dtp->dt_macros, vstr);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 				if (idp == NULL)
1277c478bd9Sstevel@tonic-gate 					return (dt_set_errno(dtp, EDT_BADSPCV));
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 				v = buf;
1307c478bd9Sstevel@tonic-gate 				vlen = snprintf(buf, 32, "%d", idp->di_id);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 			} else
1337c478bd9Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_BADSPCV));
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		if (spec == DTRACE_PROBESPEC_NONE)
1377c478bd9Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADSPEC));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		if (len + vlen >= dtrace_probespecs[spec].dtps_len)
1407c478bd9Sstevel@tonic-gate 			return (dt_set_errno(dtp, ENAMETOOLONG));
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		off = dtrace_probespecs[spec--].dtps_offset;
1437c478bd9Sstevel@tonic-gate 		bcopy(q, (char *)pdp + off, len);
1447c478bd9Sstevel@tonic-gate 		bcopy(v, (char *)pdp + off + len, vlen);
14558dbc507SJonathan Haslam 		bcopy(w, (char *)pdp + off + len + vlen, wlen);
1467c478bd9Sstevel@tonic-gate 	} while (--p >= s);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	pdp->dtpd_id = DTRACE_IDNONE;
1497c478bd9Sstevel@tonic-gate 	return (0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate int
dtrace_str2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,dtrace_probedesc_t * pdp)1537c478bd9Sstevel@tonic-gate dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
1547c478bd9Sstevel@tonic-gate     const char *s, dtrace_probedesc_t *pdp)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
dtrace_id2desc(dtrace_hdl_t * dtp,dtrace_id_t id,dtrace_probedesc_t * pdp)1607c478bd9Sstevel@tonic-gate dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	bzero(pdp, sizeof (dtrace_probedesc_t));
1637c478bd9Sstevel@tonic-gate 	pdp->dtpd_id = id;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
1667c478bd9Sstevel@tonic-gate 	    pdp->dtpd_id != id)
1677c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADID));
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (0);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate char *
dtrace_desc2str(const dtrace_probedesc_t * pdp,char * buf,size_t len)1737c478bd9Sstevel@tonic-gate dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	if (pdp->dtpd_id == 0) {
1767c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
1777c478bd9Sstevel@tonic-gate 		    pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
1787c478bd9Sstevel@tonic-gate 	} else
1797c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%u", pdp->dtpd_id);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return (buf);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate char *
dtrace_attr2str(dtrace_attribute_t attr,char * buf,size_t len)1857c478bd9Sstevel@tonic-gate dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	const char *name = dtrace_stability_name(attr.dtat_name);
1887c478bd9Sstevel@tonic-gate 	const char *data = dtrace_stability_name(attr.dtat_data);
1897c478bd9Sstevel@tonic-gate 	const char *class = dtrace_class_name(attr.dtat_class);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (name == NULL || data == NULL || class == NULL)
1927c478bd9Sstevel@tonic-gate 		return (NULL); /* one or more invalid attributes */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, len, "%s/%s/%s", name, data, class);
1957c478bd9Sstevel@tonic-gate 	return (buf);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static char *
dt_getstrattr(char * p,char ** qp)1997c478bd9Sstevel@tonic-gate dt_getstrattr(char *p, char **qp)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	char *q;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (*p == '\0')
2047c478bd9Sstevel@tonic-gate 		return (NULL);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if ((q = strchr(p, '/')) == NULL)
2077c478bd9Sstevel@tonic-gate 		q = p + strlen(p);
2087c478bd9Sstevel@tonic-gate 	else
2097c478bd9Sstevel@tonic-gate 		*q++ = '\0';
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	*qp = q;
2127c478bd9Sstevel@tonic-gate 	return (p);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate int
dtrace_str2attr(const char * str,dtrace_attribute_t * attr)2167c478bd9Sstevel@tonic-gate dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	dtrace_stability_t s;
2197c478bd9Sstevel@tonic-gate 	dtrace_class_t c;
2207c478bd9Sstevel@tonic-gate 	char *p, *q;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (str == NULL || attr == NULL)
2237c478bd9Sstevel@tonic-gate 		return (-1); /* invalid function arguments */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	*attr = _dtrace_maxattr;
22623a1cceaSRoger A. Faulkner 	p = strdupa(str);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if ((p = dt_getstrattr(p, &q)) == NULL)
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2327c478bd9Sstevel@tonic-gate 		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2337c478bd9Sstevel@tonic-gate 			attr->dtat_name = s;
2347c478bd9Sstevel@tonic-gate 			break;
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (s > DTRACE_STABILITY_MAX)
2397c478bd9Sstevel@tonic-gate 		return (-1);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if ((p = dt_getstrattr(q, &q)) == NULL)
2427c478bd9Sstevel@tonic-gate 		return (0);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
2457c478bd9Sstevel@tonic-gate 		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
2467c478bd9Sstevel@tonic-gate 			attr->dtat_data = s;
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (s > DTRACE_STABILITY_MAX)
2527c478bd9Sstevel@tonic-gate 		return (-1);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if ((p = dt_getstrattr(q, &q)) == NULL)
2557c478bd9Sstevel@tonic-gate 		return (0);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
2587c478bd9Sstevel@tonic-gate 		if (strcasecmp(p, dtrace_class_name(c)) == 0) {
2597c478bd9Sstevel@tonic-gate 			attr->dtat_class = c;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
2657c478bd9Sstevel@tonic-gate 		return (-1);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (0);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate const char *
dtrace_stability_name(dtrace_stability_t s)2717c478bd9Sstevel@tonic-gate dtrace_stability_name(dtrace_stability_t s)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	switch (s) {
2747c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_INTERNAL:	return ("Internal");
2757c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_PRIVATE:	return ("Private");
2767c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_OBSOLETE:	return ("Obsolete");
2777c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_EXTERNAL:	return ("External");
2787c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_UNSTABLE:	return ("Unstable");
2797c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_EVOLVING:	return ("Evolving");
2807c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_STABLE:	return ("Stable");
2817c478bd9Sstevel@tonic-gate 	case DTRACE_STABILITY_STANDARD:	return ("Standard");
2827c478bd9Sstevel@tonic-gate 	default:			return (NULL);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate const char *
dtrace_class_name(dtrace_class_t c)2877c478bd9Sstevel@tonic-gate dtrace_class_name(dtrace_class_t c)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	switch (c) {
2907c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_UNKNOWN:	return ("Unknown");
2917c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_CPU:		return ("CPU");
2927c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_PLATFORM:	return ("Platform");
2937c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_GROUP:	return ("Group");
2947c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_ISA:		return ("ISA");
2957c478bd9Sstevel@tonic-gate 	case DTRACE_CLASS_COMMON:	return ("Common");
2967c478bd9Sstevel@tonic-gate 	default:			return (NULL);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate dtrace_attribute_t
dt_attr_min(dtrace_attribute_t a1,dtrace_attribute_t a2)3017c478bd9Sstevel@tonic-gate dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	dtrace_attribute_t am;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
3067c478bd9Sstevel@tonic-gate 	am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
3077c478bd9Sstevel@tonic-gate 	am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (am);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate dtrace_attribute_t
dt_attr_max(dtrace_attribute_t a1,dtrace_attribute_t a2)3137c478bd9Sstevel@tonic-gate dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	dtrace_attribute_t am;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
3187c478bd9Sstevel@tonic-gate 	am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
3197c478bd9Sstevel@tonic-gate 	am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	return (am);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * Compare two attributes and return an integer value in the following ranges:
3267c478bd9Sstevel@tonic-gate  *
3277c478bd9Sstevel@tonic-gate  * <0 if any of a1's attributes are less than a2's attributes
3287c478bd9Sstevel@tonic-gate  * =0 if all of a1's attributes are equal to a2's attributes
3297c478bd9Sstevel@tonic-gate  * >0 if all of a1's attributes are greater than or equal to a2's attributes
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * To implement this function efficiently, we subtract a2's attributes from
3327c478bd9Sstevel@tonic-gate  * a1's to obtain a negative result if an a1 attribute is less than its a2
3337c478bd9Sstevel@tonic-gate  * counterpart.  We then OR the intermediate results together, relying on the
3347c478bd9Sstevel@tonic-gate  * twos-complement property that if any result is negative, the bitwise union
3357c478bd9Sstevel@tonic-gate  * will also be negative since the highest bit will be set in the result.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate int
dt_attr_cmp(dtrace_attribute_t a1,dtrace_attribute_t a2)3387c478bd9Sstevel@tonic-gate dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	return (((int)a1.dtat_name - a2.dtat_name) |
3417c478bd9Sstevel@tonic-gate 	    ((int)a1.dtat_data - a2.dtat_data) |
3427c478bd9Sstevel@tonic-gate 	    ((int)a1.dtat_class - a2.dtat_class));
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate char *
dt_attr_str(dtrace_attribute_t a,char * buf,size_t len)3467c478bd9Sstevel@tonic-gate dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	static const char stability[] = "ipoxuesS";
3497c478bd9Sstevel@tonic-gate 	static const char class[] = "uCpgIc";
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (a.dtat_name < sizeof (stability) &&
3527c478bd9Sstevel@tonic-gate 	    a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
3537c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
3547c478bd9Sstevel@tonic-gate 		    stability[a.dtat_data], class[a.dtat_class]);
3557c478bd9Sstevel@tonic-gate 	} else {
3567c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "[%u/%u/%u]",
3577c478bd9Sstevel@tonic-gate 		    a.dtat_name, a.dtat_data, a.dtat_class);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (buf);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate char *
dt_version_num2str(dt_version_t v,char * buf,size_t len)3647c478bd9Sstevel@tonic-gate dt_version_num2str(dt_version_t v, char *buf, size_t len)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	uint_t M = DT_VERSION_MAJOR(v);
3677c478bd9Sstevel@tonic-gate 	uint_t m = DT_VERSION_MINOR(v);
3687c478bd9Sstevel@tonic-gate 	uint_t u = DT_VERSION_MICRO(v);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (u == 0)
3717c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%u.%u", M, m);
3727c478bd9Sstevel@tonic-gate 	else
3737c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, len, "%u.%u.%u", M, m, u);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (buf);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate int
dt_version_str2num(const char * s,dt_version_t * vp)3797c478bd9Sstevel@tonic-gate dt_version_str2num(const char *s, dt_version_t *vp)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	int i = 0, n[3] = { 0, 0, 0 };
3827c478bd9Sstevel@tonic-gate 	char c;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0') {
3857c478bd9Sstevel@tonic-gate 		if (isdigit(c))
3867c478bd9Sstevel@tonic-gate 			n[i] = n[i] * 10 + c - '0';
3877c478bd9Sstevel@tonic-gate 		else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
3887c478bd9Sstevel@tonic-gate 			return (-1);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (n[0] > DT_VERSION_MAJMAX ||
3927c478bd9Sstevel@tonic-gate 	    n[1] > DT_VERSION_MINMAX ||
3937c478bd9Sstevel@tonic-gate 	    n[2] > DT_VERSION_MICMAX)
3947c478bd9Sstevel@tonic-gate 		return (-1);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (vp != NULL)
3977c478bd9Sstevel@tonic-gate 		*vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	return (0);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate int
dt_version_defined(dt_version_t v)4037c478bd9Sstevel@tonic-gate dt_version_defined(dt_version_t v)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	int i;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	for (i = 0; _dtrace_versions[i] != 0; i++) {
4087c478bd9Sstevel@tonic-gate 		if (_dtrace_versions[i] == v)
4097c478bd9Sstevel@tonic-gate 			return (1);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	return (0);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate char *
dt_cpp_add_arg(dtrace_hdl_t * dtp,const char * str)4167c478bd9Sstevel@tonic-gate dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	char *arg;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
4217c478bd9Sstevel@tonic-gate 		int olds = dtp->dt_cpp_args;
4227c478bd9Sstevel@tonic-gate 		int news = olds * 2;
4237c478bd9Sstevel@tonic-gate 		char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		if (argv == NULL)
4267c478bd9Sstevel@tonic-gate 			return (NULL);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		bzero(&argv[olds], sizeof (char *) * olds);
4297c478bd9Sstevel@tonic-gate 		dtp->dt_cpp_argv = argv;
4307c478bd9Sstevel@tonic-gate 		dtp->dt_cpp_args = news;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if ((arg = strdup(str)) == NULL)
4347c478bd9Sstevel@tonic-gate 		return (NULL);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
4377c478bd9Sstevel@tonic-gate 	dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
4387c478bd9Sstevel@tonic-gate 	return (arg);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate char *
dt_cpp_pop_arg(dtrace_hdl_t * dtp)4427c478bd9Sstevel@tonic-gate dt_cpp_pop_arg(dtrace_hdl_t *dtp)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	char *arg;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (dtp->dt_cpp_argc <= 1)
4477c478bd9Sstevel@tonic-gate 		return (NULL); /* dt_cpp_argv[0] cannot be popped */
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
4507c478bd9Sstevel@tonic-gate 	dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	return (arg);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
4567c478bd9Sstevel@tonic-gate void
dt_dprintf(const char * format,...)4577c478bd9Sstevel@tonic-gate dt_dprintf(const char *format, ...)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	if (_dtrace_debug) {
4607c478bd9Sstevel@tonic-gate 		va_list alist;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		va_start(alist, format);
4637c478bd9Sstevel@tonic-gate 		(void) fputs("libdtrace DEBUG: ", stderr);
4647c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, alist);
4657c478bd9Sstevel@tonic-gate 		va_end(alist);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate int
dt_ioctl(dtrace_hdl_t * dtp,int val,void * arg)4707c478bd9Sstevel@tonic-gate dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	const dtrace_vector_t *v = dtp->dt_vector;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (v != NULL)
4757c478bd9Sstevel@tonic-gate 		return (v->dtv_ioctl(dtp->dt_varg, val, arg));
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (dtp->dt_fd >= 0)
4787c478bd9Sstevel@tonic-gate 		return (ioctl(dtp->dt_fd, val, arg));
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	errno = EBADF;
4817c478bd9Sstevel@tonic-gate 	return (-1);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate int
dt_status(dtrace_hdl_t * dtp,processorid_t cpu)4857c478bd9Sstevel@tonic-gate dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	const dtrace_vector_t *v = dtp->dt_vector;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (v == NULL)
4907c478bd9Sstevel@tonic-gate 		return (p_online(cpu, P_STATUS));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	return (v->dtv_status(dtp->dt_varg, cpu));
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate long
dt_sysconf(dtrace_hdl_t * dtp,int name)4967c478bd9Sstevel@tonic-gate dt_sysconf(dtrace_hdl_t *dtp, int name)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	const dtrace_vector_t *v = dtp->dt_vector;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (v == NULL)
5017c478bd9Sstevel@tonic-gate 		return (sysconf(name));
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	return (v->dtv_sysconf(dtp->dt_varg, name));
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Wrapper around write(2) to handle partial writes.  For maximum safety of
5087c478bd9Sstevel@tonic-gate  * output files and proper error reporting, we continuing writing in the
5097c478bd9Sstevel@tonic-gate  * face of partial writes until write(2) fails or 'buf' is completely written.
5107c478bd9Sstevel@tonic-gate  * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate ssize_t
dt_write(dtrace_hdl_t * dtp,int fd,const void * buf,size_t n)5137c478bd9Sstevel@tonic-gate dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	ssize_t resid = n;
5167c478bd9Sstevel@tonic-gate 	ssize_t len;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	while (resid != 0) {
5197c478bd9Sstevel@tonic-gate 		if ((len = write(fd, buf, resid)) <= 0)
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		resid -= len;
5237c478bd9Sstevel@tonic-gate 		buf = (char *)buf + len;
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (resid == n && n != 0)
5277c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	return (n - resid);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate  * This function handles all output from libdtrace, as well as the
5347c478bd9Sstevel@tonic-gate  * dtrace_sprintf() case.  If we're here due to dtrace_sprintf(), then
5357c478bd9Sstevel@tonic-gate  * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
5367c478bd9Sstevel@tonic-gate  * specified buffer and return.  Otherwise, if output is buffered (denoted by
5377c478bd9Sstevel@tonic-gate  * a NULL fp), we sprintf the desired output into the buffered buffer
5387c478bd9Sstevel@tonic-gate  * (expanding the buffer if required).  If we don't satisfy either of these
5397c478bd9Sstevel@tonic-gate  * conditions (that is, if we are to actually generate output), then we call
5407c478bd9Sstevel@tonic-gate  * fprintf with the specified fp.  In this case, we need to deal with one of
5417c478bd9Sstevel@tonic-gate  * the more annoying peculiarities of libc's printf routines:  any failed
5427c478bd9Sstevel@tonic-gate  * write persistently sets an error flag inside the FILE causing every
5437c478bd9Sstevel@tonic-gate  * subsequent write to fail, but only the caller that initiated the error gets
5447c478bd9Sstevel@tonic-gate  * the errno.  Since libdtrace clients often intercept SIGINT, this case is
5457c478bd9Sstevel@tonic-gate  * particularly frustrating since we don't want the EINTR on one attempt to
5467c478bd9Sstevel@tonic-gate  * write to the output file to preclude later attempts to write.  This
5477c478bd9Sstevel@tonic-gate  * function therefore does a clearerr() if any error occurred, and saves the
5487c478bd9Sstevel@tonic-gate  * errno for the caller inside the specified dtrace_hdl_t.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
5517c478bd9Sstevel@tonic-gate int
dt_printf(dtrace_hdl_t * dtp,FILE * fp,const char * format,...)5527c478bd9Sstevel@tonic-gate dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	va_list ap;
5557c478bd9Sstevel@tonic-gate 	int n;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	va_start(ap, format);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (dtp->dt_sprintf_buflen != 0) {
5607c478bd9Sstevel@tonic-gate 		int len;
5617c478bd9Sstevel@tonic-gate 		char *buf;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		assert(dtp->dt_sprintf_buf != NULL);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
5667c478bd9Sstevel@tonic-gate 		len = dtp->dt_sprintf_buflen - len;
5677c478bd9Sstevel@tonic-gate 		assert(len >= 0);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		if ((n = vsnprintf(buf, len, format, ap)) < 0)
5707c478bd9Sstevel@tonic-gate 			n = dt_set_errno(dtp, errno);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		va_end(ap);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		return (n);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
5787c478bd9Sstevel@tonic-gate 		int needed, rval;
5797c478bd9Sstevel@tonic-gate 		size_t avail;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/*
582e5803b76SAdam H. Leventhal 		 * Using buffered output is not allowed if a handler has
583e5803b76SAdam H. Leventhal 		 * not been installed.
5847c478bd9Sstevel@tonic-gate 		 */
5857c478bd9Sstevel@tonic-gate 		if (dtp->dt_bufhdlr == NULL) {
5867c478bd9Sstevel@tonic-gate 			va_end(ap);
5877c478bd9Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOBUFFERED));
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		if (dtp->dt_buffered_buf == NULL) {
5917c478bd9Sstevel@tonic-gate 			assert(dtp->dt_buffered_size == 0);
5927c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_size = 1;
5937c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			if (dtp->dt_buffered_buf == NULL) {
5967c478bd9Sstevel@tonic-gate 				va_end(ap);
5977c478bd9Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_NOMEM));
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_offs = 0;
6017c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_buf[0] = '\0';
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
6057c478bd9Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
6067c478bd9Sstevel@tonic-gate 			va_end(ap);
6077c478bd9Sstevel@tonic-gate 			return (rval);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		if (needed == 0) {
6117c478bd9Sstevel@tonic-gate 			va_end(ap);
6127c478bd9Sstevel@tonic-gate 			return (0);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		for (;;) {
6167c478bd9Sstevel@tonic-gate 			char *newbuf;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
6197c478bd9Sstevel@tonic-gate 			avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			if (needed + 1 < avail)
6227c478bd9Sstevel@tonic-gate 				break;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			if ((newbuf = realloc(dtp->dt_buffered_buf,
6257c478bd9Sstevel@tonic-gate 			    dtp->dt_buffered_size << 1)) == NULL) {
6267c478bd9Sstevel@tonic-gate 				va_end(ap);
6277c478bd9Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_NOMEM));
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_buf = newbuf;
6317c478bd9Sstevel@tonic-gate 			dtp->dt_buffered_size <<= 1;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
6357c478bd9Sstevel@tonic-gate 		    avail, format, ap) < 0) {
6367c478bd9Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
6377c478bd9Sstevel@tonic-gate 			va_end(ap);
6387c478bd9Sstevel@tonic-gate 			return (rval);
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		dtp->dt_buffered_offs += needed;
6427c478bd9Sstevel@tonic-gate 		assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
6437c478bd9Sstevel@tonic-gate 		return (0);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	n = vfprintf(fp, format, ap);
6477c478bd9Sstevel@tonic-gate 	va_end(ap);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (n < 0) {
6507c478bd9Sstevel@tonic-gate 		clearerr(fp);
6517c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	return (n);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@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)6587c478bd9Sstevel@tonic-gate dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
65930ef842dSbmc     const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	dtrace_bufdata_t data;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (dtp->dt_buffered_offs == 0)
6647c478bd9Sstevel@tonic-gate 		return (0);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	data.dtbda_handle = dtp;
6677c478bd9Sstevel@tonic-gate 	data.dtbda_buffered = dtp->dt_buffered_buf;
6687c478bd9Sstevel@tonic-gate 	data.dtbda_probe = pdata;
6697c478bd9Sstevel@tonic-gate 	data.dtbda_recdesc = rec;
6707c478bd9Sstevel@tonic-gate 	data.dtbda_aggdata = agg;
67130ef842dSbmc 	data.dtbda_flags = flags;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
6747c478bd9Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DIRABORT));
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	dtp->dt_buffered_offs = 0;
6777c478bd9Sstevel@tonic-gate 	dtp->dt_buffered_buf[0] = '\0';
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate void
dt_buffered_destroy(dtrace_hdl_t * dtp)6837c478bd9Sstevel@tonic-gate dt_buffered_destroy(dtrace_hdl_t *dtp)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	free(dtp->dt_buffered_buf);
6867c478bd9Sstevel@tonic-gate 	dtp->dt_buffered_buf = NULL;
6877c478bd9Sstevel@tonic-gate 	dtp->dt_buffered_offs = 0;
6887c478bd9Sstevel@tonic-gate 	dtp->dt_buffered_size = 0;
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate void *
dt_zalloc(dtrace_hdl_t * dtp,size_t size)6927c478bd9Sstevel@tonic-gate dt_zalloc(dtrace_hdl_t *dtp, size_t size)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	void *data;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((data = malloc(size)) == NULL)
6977c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
6987c478bd9Sstevel@tonic-gate 	else
6997c478bd9Sstevel@tonic-gate 		bzero(data, size);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	return (data);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate void *
dt_alloc(dtrace_hdl_t * dtp,size_t size)7057c478bd9Sstevel@tonic-gate dt_alloc(dtrace_hdl_t *dtp, size_t size)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	void *data;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if ((data = malloc(size)) == NULL)
7107c478bd9Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	return (data);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate void
dt_free(dtrace_hdl_t * dtp,void * data)7167c478bd9Sstevel@tonic-gate dt_free(dtrace_hdl_t *dtp, void *data)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	assert(dtp != NULL); /* ensure sane use of this interface */
7197c478bd9Sstevel@tonic-gate 	free(data);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7221a7c1b72Smws void
dt_difo_free(dtrace_hdl_t * dtp,dtrace_difo_t * dp)7231a7c1b72Smws dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
7241a7c1b72Smws {
7251a7c1b72Smws 	if (dp == NULL)
7261a7c1b72Smws 		return; /* simplify caller code */
7271a7c1b72Smws 
7281a7c1b72Smws 	dt_free(dtp, dp->dtdo_buf);
7291a7c1b72Smws 	dt_free(dtp, dp->dtdo_inttab);
7301a7c1b72Smws 	dt_free(dtp, dp->dtdo_strtab);
7311a7c1b72Smws 	dt_free(dtp, dp->dtdo_vartab);
7321a7c1b72Smws 	dt_free(dtp, dp->dtdo_kreltab);
7331a7c1b72Smws 	dt_free(dtp, dp->dtdo_ureltab);
7341a7c1b72Smws 	dt_free(dtp, dp->dtdo_xlmtab);
7351a7c1b72Smws 
7361a7c1b72Smws 	dt_free(dtp, dp);
7371a7c1b72Smws }
7381a7c1b72Smws 
7397c478bd9Sstevel@tonic-gate /*
740bbf21555SRichard Lowe  * dt_gmatch() is similar to gmatch(3GEN) and dtrace(4D) globbing, but also
7417c478bd9Sstevel@tonic-gate  * implements the behavior that an empty pattern matches any string.
7427c478bd9Sstevel@tonic-gate  */
7437c478bd9Sstevel@tonic-gate int
dt_gmatch(const char * s,const char * p)7447c478bd9Sstevel@tonic-gate dt_gmatch(const char *s, const char *p)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	return (p == NULL || *p == '\0' || gmatch(s, p));
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate char *
dt_basename(char * str)7507c478bd9Sstevel@tonic-gate dt_basename(char *str)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	char *last = strrchr(str, '/');
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (last == NULL)
7557c478bd9Sstevel@tonic-gate 		return (str);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	return (last + 1);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7601a7c1b72Smws /*
7611a7c1b72Smws  * dt_popc() is a fast implementation of population count.  The algorithm is
7621a7c1b72Smws  * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
7631a7c1b72Smws  */
7641a7c1b72Smws ulong_t
dt_popc(ulong_t x)7651a7c1b72Smws dt_popc(ulong_t x)
7661a7c1b72Smws {
7671a7c1b72Smws #ifdef _ILP32
7681a7c1b72Smws 	x = x - ((x >> 1) & 0x55555555UL);
7691a7c1b72Smws 	x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
7701a7c1b72Smws 	x = (x + (x >> 4)) & 0x0F0F0F0FUL;
7711a7c1b72Smws 	x = x + (x >> 8);
7721a7c1b72Smws 	x = x + (x >> 16);
7731a7c1b72Smws 	return (x & 0x3F);
7741a7c1b72Smws #endif
7751a7c1b72Smws #ifdef _LP64
7761a7c1b72Smws 	x = x - ((x >> 1) & 0x5555555555555555ULL);
7771a7c1b72Smws 	x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7781a7c1b72Smws 	x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
7791a7c1b72Smws 	x = x + (x >> 8);
7801a7c1b72Smws 	x = x + (x >> 16);
7811a7c1b72Smws 	x = x + (x >> 32);
7821a7c1b72Smws 	return (x & 0x7F);
7831a7c1b72Smws #endif
7841a7c1b72Smws }
7851a7c1b72Smws 
7861a7c1b72Smws /*
7871a7c1b72Smws  * dt_popcb() is a bitmap-based version of population count that returns the
7881a7c1b72Smws  * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
7891a7c1b72Smws  */
7901a7c1b72Smws ulong_t
dt_popcb(const ulong_t * bp,ulong_t n)7911a7c1b72Smws dt_popcb(const ulong_t *bp, ulong_t n)
7921a7c1b72Smws {
7931a7c1b72Smws 	ulong_t maxb = n & BT_ULMASK;
7941a7c1b72Smws 	ulong_t maxw = n >> BT_ULSHIFT;
7951a7c1b72Smws 	ulong_t w, popc = 0;
7961a7c1b72Smws 
7971a7c1b72Smws 	if (n == 0)
7981a7c1b72Smws 		return (0);
7991a7c1b72Smws 
8001a7c1b72Smws 	for (w = 0; w < maxw; w++)
8011a7c1b72Smws 		popc += dt_popc(bp[w]);
8021a7c1b72Smws 
8031a7c1b72Smws 	return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
8041a7c1b72Smws }
8051a7c1b72Smws 
806a1b5e537Sbmc static int
dt_string2str(char * s,char * str,int nbytes)807a1b5e537Sbmc dt_string2str(char *s, char *str, int nbytes)
808a1b5e537Sbmc {
809a1b5e537Sbmc 	int len = strlen(s);
810a1b5e537Sbmc 
811a1b5e537Sbmc 	if (nbytes == 0) {
812a1b5e537Sbmc 		/*
813a1b5e537Sbmc 		 * Like snprintf(3C), we don't check the value of str if the
814a1b5e537Sbmc 		 * number of bytes is 0.
815a1b5e537Sbmc 		 */
816a1b5e537Sbmc 		return (len);
817a1b5e537Sbmc 	}
818a1b5e537Sbmc 
819a1b5e537Sbmc 	if (nbytes <= len) {
820a1b5e537Sbmc 		(void) strncpy(str, s, nbytes - 1);
821a1b5e537Sbmc 		/*
822a1b5e537Sbmc 		 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
823a1b5e537Sbmc 		 * that the string is null-terminated.
824a1b5e537Sbmc 		 */
825a1b5e537Sbmc 		str[nbytes - 1] = '\0';
826a1b5e537Sbmc 	} else {
827a1b5e537Sbmc 		(void) strcpy(str, s);
828a1b5e537Sbmc 	}
829a1b5e537Sbmc 
830a1b5e537Sbmc 	return (len);
831a1b5e537Sbmc }
832a1b5e537Sbmc 
833a1b5e537Sbmc int
dtrace_addr2str(dtrace_hdl_t * dtp,uint64_t addr,char * str,int nbytes)834a1b5e537Sbmc dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
835a1b5e537Sbmc {
836a1b5e537Sbmc 	dtrace_syminfo_t dts;
837a1b5e537Sbmc 	GElf_Sym sym;
838a1b5e537Sbmc 
839a1b5e537Sbmc 	size_t n = 20; /* for 0x%llx\0 */
840a1b5e537Sbmc 	char *s;
841a1b5e537Sbmc 	int err;
842a1b5e537Sbmc 
843a1b5e537Sbmc 	if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
844a1b5e537Sbmc 		n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
845a1b5e537Sbmc 
846a1b5e537Sbmc 	s = alloca(n);
847a1b5e537Sbmc 
848a1b5e537Sbmc 	if (err == 0 && addr != sym.st_value) {
849a1b5e537Sbmc 		(void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
850a1b5e537Sbmc 		    dts.dts_name, (u_longlong_t)addr - sym.st_value);
851a1b5e537Sbmc 	} else if (err == 0) {
852a1b5e537Sbmc 		(void) snprintf(s, n, "%s`%s",
853a1b5e537Sbmc 		    dts.dts_object, dts.dts_name);
854a1b5e537Sbmc 	} else {
855a1b5e537Sbmc 		/*
856a1b5e537Sbmc 		 * We'll repeat the lookup, but this time we'll specify a NULL
857a1b5e537Sbmc 		 * GElf_Sym -- indicating that we're only interested in the
858a1b5e537Sbmc 		 * containing module.
859a1b5e537Sbmc 		 */
860a1b5e537Sbmc 		if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
861a1b5e537Sbmc 			(void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
862a1b5e537Sbmc 			    (u_longlong_t)addr);
863a1b5e537Sbmc 		} else {
864a1b5e537Sbmc 			(void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
865a1b5e537Sbmc 		}
866a1b5e537Sbmc 	}
867a1b5e537Sbmc 
868a1b5e537Sbmc 	return (dt_string2str(s, str, nbytes));
869a1b5e537Sbmc }
870a1b5e537Sbmc 
871a1b5e537Sbmc int
dtrace_uaddr2str(dtrace_hdl_t * dtp,pid_t pid,uint64_t addr,char * str,int nbytes)872a1b5e537Sbmc dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
873a1b5e537Sbmc     uint64_t addr, char *str, int nbytes)
874a1b5e537Sbmc {
875a1b5e537Sbmc 	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
876a1b5e537Sbmc 	struct ps_prochandle *P = NULL;
877a1b5e537Sbmc 	GElf_Sym sym;
878a1b5e537Sbmc 	char *obj;
879a1b5e537Sbmc 
880a1b5e537Sbmc 	if (pid != 0)
881a1b5e537Sbmc 		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
882a1b5e537Sbmc 
883a1b5e537Sbmc 	if (P == NULL) {
884a1b5e537Sbmc 		(void) snprintf(c, sizeof (c), "0x%llx", addr);
885a1b5e537Sbmc 		return (dt_string2str(c, str, nbytes));
886a1b5e537Sbmc 	}
887a1b5e537Sbmc 
888a1b5e537Sbmc 	dt_proc_lock(dtp, P);
889a1b5e537Sbmc 
890a1b5e537Sbmc 	if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
891a1b5e537Sbmc 		(void) Pobjname(P, addr, objname, sizeof (objname));
892a1b5e537Sbmc 
893a1b5e537Sbmc 		obj = dt_basename(objname);
894a1b5e537Sbmc 
895a1b5e537Sbmc 		if (addr > sym.st_value) {
896a1b5e537Sbmc 			(void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
897a1b5e537Sbmc 			    name, (u_longlong_t)(addr - sym.st_value));
898a1b5e537Sbmc 		} else {
899a1b5e537Sbmc 			(void) snprintf(c, sizeof (c), "%s`%s", obj, name);
900a1b5e537Sbmc 		}
901a1b5e537Sbmc 	} else if (Pobjname(P, addr, objname, sizeof (objname)) != NULL) {
902a1b5e537Sbmc 		(void) snprintf(c, sizeof (c), "%s`0x%llx",
903a1b5e537Sbmc 		    dt_basename(objname), addr);
904a1b5e537Sbmc 	} else {
905a1b5e537Sbmc 		(void) snprintf(c, sizeof (c), "0x%llx", addr);
906a1b5e537Sbmc 	}
907a1b5e537Sbmc 
908a1b5e537Sbmc 	dt_proc_unlock(dtp, P);
909a1b5e537Sbmc 	dt_proc_release(dtp, P);
910a1b5e537Sbmc 
911a1b5e537Sbmc 	return (dt_string2str(c, str, nbytes));
912a1b5e537Sbmc }
913*6eeafb34SRobert Mustacchi 
914*6eeafb34SRobert Mustacchi /*
915*6eeafb34SRobert Mustacchi  * This is a shared implementation to determine if we should treat a type as a
916*6eeafb34SRobert Mustacchi  * bitfield. The parameters are the CTF encoding and the bit offset of the
917*6eeafb34SRobert Mustacchi  * integer. This also exists in mdb_print.c. We consider something a bitfield
918*6eeafb34SRobert Mustacchi  * if:
919*6eeafb34SRobert Mustacchi  *
920*6eeafb34SRobert Mustacchi  *  o The type is more than 8 bytes. This is a bit of a historical choice from
921*6eeafb34SRobert Mustacchi  *    mdb and is a stranger one. The normal integer handling code generally
922*6eeafb34SRobert Mustacchi  *    doesn't handle integers more than 64-bits in size. Of course neither does
923*6eeafb34SRobert Mustacchi  *    the bitfield code...
924*6eeafb34SRobert Mustacchi  *  o The bit count is not a multiple of 8.
925*6eeafb34SRobert Mustacchi  *  o The size in bytes is not a power of 2.
926*6eeafb34SRobert Mustacchi  *  o The offset is not a multiple of 8.
927*6eeafb34SRobert Mustacchi  */
928*6eeafb34SRobert Mustacchi boolean_t
dt_is_bitfield(const ctf_encoding_t * ep,ulong_t off)929*6eeafb34SRobert Mustacchi dt_is_bitfield(const ctf_encoding_t *ep, ulong_t off)
930*6eeafb34SRobert Mustacchi {
931*6eeafb34SRobert Mustacchi 	size_t bsize = ep->cte_bits / NBBY;
932*6eeafb34SRobert Mustacchi 	return (bsize > 8 || (ep->cte_bits % NBBY) != 0 ||
933*6eeafb34SRobert Mustacchi 	    (bsize & (bsize - 1)) != 0 || (off % NBBY) != 0);
934*6eeafb34SRobert Mustacchi }
935