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 5*11237SJonathan.Haslam@Sun.COM * Common Development and Distribution License (the "License"). 6*11237SJonathan.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 */ 210Sstevel@tonic-gate /* 22*11237SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 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 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 { 57*11237SJonathan.Haslam@Sun.COM size_t off, len, vlen, wlen; 58*11237SJonathan.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; 74*11237SJonathan.Haslam@Sun.COM w = NULL; 75*11237SJonathan.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; 103*11237SJonathan.Haslam@Sun.COM i = strtol(v + 1, (char **)&w, 10); 1040Sstevel@tonic-gate 105*11237SJonathan.Haslam@Sun.COM wlen = vlen - (w - v); 106*11237SJonathan.Haslam@Sun.COM 107*11237SJonathan.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); 143*11237SJonathan.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 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 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 * 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 * 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 * 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 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; 2240Sstevel@tonic-gate p = alloca(strlen(str) + 1); 2250Sstevel@tonic-gate (void) strcpy(p, str); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate if ((p = dt_getstrattr(p, &q)) == NULL) 2280Sstevel@tonic-gate return (0); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 2310Sstevel@tonic-gate if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 2320Sstevel@tonic-gate attr->dtat_name = s; 2330Sstevel@tonic-gate break; 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (s > DTRACE_STABILITY_MAX) 2380Sstevel@tonic-gate return (-1); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate if ((p = dt_getstrattr(q, &q)) == NULL) 2410Sstevel@tonic-gate return (0); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 2440Sstevel@tonic-gate if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 2450Sstevel@tonic-gate attr->dtat_data = s; 2460Sstevel@tonic-gate break; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate if (s > DTRACE_STABILITY_MAX) 2510Sstevel@tonic-gate return (-1); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if ((p = dt_getstrattr(q, &q)) == NULL) 2540Sstevel@tonic-gate return (0); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate for (c = 0; c <= DTRACE_CLASS_MAX; c++) { 2570Sstevel@tonic-gate if (strcasecmp(p, dtrace_class_name(c)) == 0) { 2580Sstevel@tonic-gate attr->dtat_class = c; 2590Sstevel@tonic-gate break; 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL) 2640Sstevel@tonic-gate return (-1); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate return (0); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate const char * 2700Sstevel@tonic-gate dtrace_stability_name(dtrace_stability_t s) 2710Sstevel@tonic-gate { 2720Sstevel@tonic-gate switch (s) { 2730Sstevel@tonic-gate case DTRACE_STABILITY_INTERNAL: return ("Internal"); 2740Sstevel@tonic-gate case DTRACE_STABILITY_PRIVATE: return ("Private"); 2750Sstevel@tonic-gate case DTRACE_STABILITY_OBSOLETE: return ("Obsolete"); 2760Sstevel@tonic-gate case DTRACE_STABILITY_EXTERNAL: return ("External"); 2770Sstevel@tonic-gate case DTRACE_STABILITY_UNSTABLE: return ("Unstable"); 2780Sstevel@tonic-gate case DTRACE_STABILITY_EVOLVING: return ("Evolving"); 2790Sstevel@tonic-gate case DTRACE_STABILITY_STABLE: return ("Stable"); 2800Sstevel@tonic-gate case DTRACE_STABILITY_STANDARD: return ("Standard"); 2810Sstevel@tonic-gate default: return (NULL); 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate const char * 2860Sstevel@tonic-gate dtrace_class_name(dtrace_class_t c) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate switch (c) { 2890Sstevel@tonic-gate case DTRACE_CLASS_UNKNOWN: return ("Unknown"); 2900Sstevel@tonic-gate case DTRACE_CLASS_CPU: return ("CPU"); 2910Sstevel@tonic-gate case DTRACE_CLASS_PLATFORM: return ("Platform"); 2920Sstevel@tonic-gate case DTRACE_CLASS_GROUP: return ("Group"); 2930Sstevel@tonic-gate case DTRACE_CLASS_ISA: return ("ISA"); 2940Sstevel@tonic-gate case DTRACE_CLASS_COMMON: return ("Common"); 2950Sstevel@tonic-gate default: return (NULL); 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate dtrace_attribute_t 3000Sstevel@tonic-gate dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2) 3010Sstevel@tonic-gate { 3020Sstevel@tonic-gate dtrace_attribute_t am; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate am.dtat_name = MIN(a1.dtat_name, a2.dtat_name); 3050Sstevel@tonic-gate am.dtat_data = MIN(a1.dtat_data, a2.dtat_data); 3060Sstevel@tonic-gate am.dtat_class = MIN(a1.dtat_class, a2.dtat_class); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate return (am); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate dtrace_attribute_t 3120Sstevel@tonic-gate dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate dtrace_attribute_t am; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate am.dtat_name = MAX(a1.dtat_name, a2.dtat_name); 3170Sstevel@tonic-gate am.dtat_data = MAX(a1.dtat_data, a2.dtat_data); 3180Sstevel@tonic-gate am.dtat_class = MAX(a1.dtat_class, a2.dtat_class); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate return (am); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * Compare two attributes and return an integer value in the following ranges: 3250Sstevel@tonic-gate * 3260Sstevel@tonic-gate * <0 if any of a1's attributes are less than a2's attributes 3270Sstevel@tonic-gate * =0 if all of a1's attributes are equal to a2's attributes 3280Sstevel@tonic-gate * >0 if all of a1's attributes are greater than or equal to a2's attributes 3290Sstevel@tonic-gate * 3300Sstevel@tonic-gate * To implement this function efficiently, we subtract a2's attributes from 3310Sstevel@tonic-gate * a1's to obtain a negative result if an a1 attribute is less than its a2 3320Sstevel@tonic-gate * counterpart. We then OR the intermediate results together, relying on the 3330Sstevel@tonic-gate * twos-complement property that if any result is negative, the bitwise union 3340Sstevel@tonic-gate * will also be negative since the highest bit will be set in the result. 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate int 3370Sstevel@tonic-gate dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate return (((int)a1.dtat_name - a2.dtat_name) | 3400Sstevel@tonic-gate ((int)a1.dtat_data - a2.dtat_data) | 3410Sstevel@tonic-gate ((int)a1.dtat_class - a2.dtat_class)); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate char * 3450Sstevel@tonic-gate dt_attr_str(dtrace_attribute_t a, char *buf, size_t len) 3460Sstevel@tonic-gate { 3470Sstevel@tonic-gate static const char stability[] = "ipoxuesS"; 3480Sstevel@tonic-gate static const char class[] = "uCpgIc"; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (a.dtat_name < sizeof (stability) && 3510Sstevel@tonic-gate a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) { 3520Sstevel@tonic-gate (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name], 3530Sstevel@tonic-gate stability[a.dtat_data], class[a.dtat_class]); 3540Sstevel@tonic-gate } else { 3550Sstevel@tonic-gate (void) snprintf(buf, len, "[%u/%u/%u]", 3560Sstevel@tonic-gate a.dtat_name, a.dtat_data, a.dtat_class); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate return (buf); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate char * 3630Sstevel@tonic-gate dt_version_num2str(dt_version_t v, char *buf, size_t len) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate uint_t M = DT_VERSION_MAJOR(v); 3660Sstevel@tonic-gate uint_t m = DT_VERSION_MINOR(v); 3670Sstevel@tonic-gate uint_t u = DT_VERSION_MICRO(v); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate if (u == 0) 3700Sstevel@tonic-gate (void) snprintf(buf, len, "%u.%u", M, m); 3710Sstevel@tonic-gate else 3720Sstevel@tonic-gate (void) snprintf(buf, len, "%u.%u.%u", M, m, u); 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate return (buf); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate int 3780Sstevel@tonic-gate dt_version_str2num(const char *s, dt_version_t *vp) 3790Sstevel@tonic-gate { 3800Sstevel@tonic-gate int i = 0, n[3] = { 0, 0, 0 }; 3810Sstevel@tonic-gate char c; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate while ((c = *s++) != '\0') { 3840Sstevel@tonic-gate if (isdigit(c)) 3850Sstevel@tonic-gate n[i] = n[i] * 10 + c - '0'; 3860Sstevel@tonic-gate else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1) 3870Sstevel@tonic-gate return (-1); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate if (n[0] > DT_VERSION_MAJMAX || 3910Sstevel@tonic-gate n[1] > DT_VERSION_MINMAX || 3920Sstevel@tonic-gate n[2] > DT_VERSION_MICMAX) 3930Sstevel@tonic-gate return (-1); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate if (vp != NULL) 3960Sstevel@tonic-gate *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate return (0); 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate int 4020Sstevel@tonic-gate dt_version_defined(dt_version_t v) 4030Sstevel@tonic-gate { 4040Sstevel@tonic-gate int i; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate for (i = 0; _dtrace_versions[i] != 0; i++) { 4070Sstevel@tonic-gate if (_dtrace_versions[i] == v) 4080Sstevel@tonic-gate return (1); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate return (0); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate char * 4150Sstevel@tonic-gate dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate char *arg; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate if (dtp->dt_cpp_argc == dtp->dt_cpp_args) { 4200Sstevel@tonic-gate int olds = dtp->dt_cpp_args; 4210Sstevel@tonic-gate int news = olds * 2; 4220Sstevel@tonic-gate char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate if (argv == NULL) 4250Sstevel@tonic-gate return (NULL); 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate bzero(&argv[olds], sizeof (char *) * olds); 4280Sstevel@tonic-gate dtp->dt_cpp_argv = argv; 4290Sstevel@tonic-gate dtp->dt_cpp_args = news; 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if ((arg = strdup(str)) == NULL) 4330Sstevel@tonic-gate return (NULL); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate assert(dtp->dt_cpp_argc < dtp->dt_cpp_args); 4360Sstevel@tonic-gate dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg; 4370Sstevel@tonic-gate return (arg); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate char * 4410Sstevel@tonic-gate dt_cpp_pop_arg(dtrace_hdl_t *dtp) 4420Sstevel@tonic-gate { 4430Sstevel@tonic-gate char *arg; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate if (dtp->dt_cpp_argc <= 1) 4460Sstevel@tonic-gate return (NULL); /* dt_cpp_argv[0] cannot be popped */ 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc]; 4490Sstevel@tonic-gate dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate return (arg); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate /*PRINTFLIKE1*/ 4550Sstevel@tonic-gate void 4560Sstevel@tonic-gate dt_dprintf(const char *format, ...) 4570Sstevel@tonic-gate { 4580Sstevel@tonic-gate if (_dtrace_debug) { 4590Sstevel@tonic-gate va_list alist; 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate va_start(alist, format); 4620Sstevel@tonic-gate (void) fputs("libdtrace DEBUG: ", stderr); 4630Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 4640Sstevel@tonic-gate va_end(alist); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate int 4690Sstevel@tonic-gate dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg) 4700Sstevel@tonic-gate { 4710Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if (v != NULL) 4740Sstevel@tonic-gate return (v->dtv_ioctl(dtp->dt_varg, val, arg)); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate if (dtp->dt_fd >= 0) 4770Sstevel@tonic-gate return (ioctl(dtp->dt_fd, val, arg)); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate errno = EBADF; 4800Sstevel@tonic-gate return (-1); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate int 4840Sstevel@tonic-gate dt_status(dtrace_hdl_t *dtp, processorid_t cpu) 4850Sstevel@tonic-gate { 4860Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate if (v == NULL) 4890Sstevel@tonic-gate return (p_online(cpu, P_STATUS)); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate return (v->dtv_status(dtp->dt_varg, cpu)); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate long 4950Sstevel@tonic-gate dt_sysconf(dtrace_hdl_t *dtp, int name) 4960Sstevel@tonic-gate { 4970Sstevel@tonic-gate const dtrace_vector_t *v = dtp->dt_vector; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate if (v == NULL) 5000Sstevel@tonic-gate return (sysconf(name)); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate return (v->dtv_sysconf(dtp->dt_varg, name)); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Wrapper around write(2) to handle partial writes. For maximum safety of 5070Sstevel@tonic-gate * output files and proper error reporting, we continuing writing in the 5080Sstevel@tonic-gate * face of partial writes until write(2) fails or 'buf' is completely written. 5090Sstevel@tonic-gate * We also record any errno in the specified dtrace_hdl_t as well as 'errno'. 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate ssize_t 5120Sstevel@tonic-gate dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n) 5130Sstevel@tonic-gate { 5140Sstevel@tonic-gate ssize_t resid = n; 5150Sstevel@tonic-gate ssize_t len; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate while (resid != 0) { 5180Sstevel@tonic-gate if ((len = write(fd, buf, resid)) <= 0) 5190Sstevel@tonic-gate break; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate resid -= len; 5220Sstevel@tonic-gate buf = (char *)buf + len; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate if (resid == n && n != 0) 5260Sstevel@tonic-gate return (dt_set_errno(dtp, errno)); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate return (n - resid); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * This function handles all output from libdtrace, as well as the 5330Sstevel@tonic-gate * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then 5340Sstevel@tonic-gate * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the 5350Sstevel@tonic-gate * specified buffer and return. Otherwise, if output is buffered (denoted by 5360Sstevel@tonic-gate * a NULL fp), we sprintf the desired output into the buffered buffer 5370Sstevel@tonic-gate * (expanding the buffer if required). If we don't satisfy either of these 5380Sstevel@tonic-gate * conditions (that is, if we are to actually generate output), then we call 5390Sstevel@tonic-gate * fprintf with the specified fp. In this case, we need to deal with one of 5400Sstevel@tonic-gate * the more annoying peculiarities of libc's printf routines: any failed 5410Sstevel@tonic-gate * write persistently sets an error flag inside the FILE causing every 5420Sstevel@tonic-gate * subsequent write to fail, but only the caller that initiated the error gets 5430Sstevel@tonic-gate * the errno. Since libdtrace clients often intercept SIGINT, this case is 5440Sstevel@tonic-gate * particularly frustrating since we don't want the EINTR on one attempt to 5450Sstevel@tonic-gate * write to the output file to preclude later attempts to write. This 5460Sstevel@tonic-gate * function therefore does a clearerr() if any error occurred, and saves the 5470Sstevel@tonic-gate * errno for the caller inside the specified dtrace_hdl_t. 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate /*PRINTFLIKE3*/ 5500Sstevel@tonic-gate int 5510Sstevel@tonic-gate dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...) 5520Sstevel@tonic-gate { 5530Sstevel@tonic-gate va_list ap; 5540Sstevel@tonic-gate int n; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate va_start(ap, format); 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate if (dtp->dt_sprintf_buflen != 0) { 5590Sstevel@tonic-gate int len; 5600Sstevel@tonic-gate char *buf; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate assert(dtp->dt_sprintf_buf != NULL); 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)]; 5650Sstevel@tonic-gate len = dtp->dt_sprintf_buflen - len; 5660Sstevel@tonic-gate assert(len >= 0); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if ((n = vsnprintf(buf, len, format, ap)) < 0) 5690Sstevel@tonic-gate n = dt_set_errno(dtp, errno); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate va_end(ap); 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate return (n); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate if (fp == NULL) { 5770Sstevel@tonic-gate int needed, rval; 5780Sstevel@tonic-gate size_t avail; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate /* 5810Sstevel@tonic-gate * It's not legal to use buffered ouput if there is not a 5820Sstevel@tonic-gate * handler for buffered output. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate if (dtp->dt_bufhdlr == NULL) { 5850Sstevel@tonic-gate va_end(ap); 5860Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOBUFFERED)); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate if (dtp->dt_buffered_buf == NULL) { 5900Sstevel@tonic-gate assert(dtp->dt_buffered_size == 0); 5910Sstevel@tonic-gate dtp->dt_buffered_size = 1; 5920Sstevel@tonic-gate dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size); 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (dtp->dt_buffered_buf == NULL) { 5950Sstevel@tonic-gate va_end(ap); 5960Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate dtp->dt_buffered_offs = 0; 6000Sstevel@tonic-gate dtp->dt_buffered_buf[0] = '\0'; 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) { 6040Sstevel@tonic-gate rval = dt_set_errno(dtp, errno); 6050Sstevel@tonic-gate va_end(ap); 6060Sstevel@tonic-gate return (rval); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate if (needed == 0) { 6100Sstevel@tonic-gate va_end(ap); 6110Sstevel@tonic-gate return (0); 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate for (;;) { 6150Sstevel@tonic-gate char *newbuf; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate assert(dtp->dt_buffered_offs < dtp->dt_buffered_size); 6180Sstevel@tonic-gate avail = dtp->dt_buffered_size - dtp->dt_buffered_offs; 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate if (needed + 1 < avail) 6210Sstevel@tonic-gate break; 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate if ((newbuf = realloc(dtp->dt_buffered_buf, 6240Sstevel@tonic-gate dtp->dt_buffered_size << 1)) == NULL) { 6250Sstevel@tonic-gate va_end(ap); 6260Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate dtp->dt_buffered_buf = newbuf; 6300Sstevel@tonic-gate dtp->dt_buffered_size <<= 1; 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs], 6340Sstevel@tonic-gate avail, format, ap) < 0) { 6350Sstevel@tonic-gate rval = dt_set_errno(dtp, errno); 6360Sstevel@tonic-gate va_end(ap); 6370Sstevel@tonic-gate return (rval); 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate dtp->dt_buffered_offs += needed; 6410Sstevel@tonic-gate assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0'); 6420Sstevel@tonic-gate return (0); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate n = vfprintf(fp, format, ap); 6460Sstevel@tonic-gate va_end(ap); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (n < 0) { 6490Sstevel@tonic-gate clearerr(fp); 6500Sstevel@tonic-gate return (dt_set_errno(dtp, errno)); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate return (n); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate int 6570Sstevel@tonic-gate dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata, 6581017Sbmc const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags) 6590Sstevel@tonic-gate { 6600Sstevel@tonic-gate dtrace_bufdata_t data; 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate if (dtp->dt_buffered_offs == 0) 6630Sstevel@tonic-gate return (0); 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate data.dtbda_handle = dtp; 6660Sstevel@tonic-gate data.dtbda_buffered = dtp->dt_buffered_buf; 6670Sstevel@tonic-gate data.dtbda_probe = pdata; 6680Sstevel@tonic-gate data.dtbda_recdesc = rec; 6690Sstevel@tonic-gate data.dtbda_aggdata = agg; 6701017Sbmc data.dtbda_flags = flags; 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT) 6730Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_DIRABORT)); 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate dtp->dt_buffered_offs = 0; 6760Sstevel@tonic-gate dtp->dt_buffered_buf[0] = '\0'; 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate return (0); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate void 6820Sstevel@tonic-gate dt_buffered_destroy(dtrace_hdl_t *dtp) 6830Sstevel@tonic-gate { 6840Sstevel@tonic-gate free(dtp->dt_buffered_buf); 6850Sstevel@tonic-gate dtp->dt_buffered_buf = NULL; 6860Sstevel@tonic-gate dtp->dt_buffered_offs = 0; 6870Sstevel@tonic-gate dtp->dt_buffered_size = 0; 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate void * 6910Sstevel@tonic-gate dt_zalloc(dtrace_hdl_t *dtp, size_t size) 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate void *data; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if ((data = malloc(size)) == NULL) 6960Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 6970Sstevel@tonic-gate else 6980Sstevel@tonic-gate bzero(data, size); 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate return (data); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate void * 7040Sstevel@tonic-gate dt_alloc(dtrace_hdl_t *dtp, size_t size) 7050Sstevel@tonic-gate { 7060Sstevel@tonic-gate void *data; 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate if ((data = malloc(size)) == NULL) 7090Sstevel@tonic-gate (void) dt_set_errno(dtp, EDT_NOMEM); 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate return (data); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate void 7150Sstevel@tonic-gate dt_free(dtrace_hdl_t *dtp, void *data) 7160Sstevel@tonic-gate { 7170Sstevel@tonic-gate assert(dtp != NULL); /* ensure sane use of this interface */ 7180Sstevel@tonic-gate free(data); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate 721265Smws void 722265Smws dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp) 723265Smws { 724265Smws if (dp == NULL) 725265Smws return; /* simplify caller code */ 726265Smws 727265Smws dt_free(dtp, dp->dtdo_buf); 728265Smws dt_free(dtp, dp->dtdo_inttab); 729265Smws dt_free(dtp, dp->dtdo_strtab); 730265Smws dt_free(dtp, dp->dtdo_vartab); 731265Smws dt_free(dtp, dp->dtdo_kreltab); 732265Smws dt_free(dtp, dp->dtdo_ureltab); 733265Smws dt_free(dtp, dp->dtdo_xlmtab); 734265Smws 735265Smws dt_free(dtp, dp); 736265Smws } 737265Smws 7380Sstevel@tonic-gate /* 7390Sstevel@tonic-gate * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also 7400Sstevel@tonic-gate * implements the behavior that an empty pattern matches any string. 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate int 7430Sstevel@tonic-gate dt_gmatch(const char *s, const char *p) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate return (p == NULL || *p == '\0' || gmatch(s, p)); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate char * 7490Sstevel@tonic-gate dt_basename(char *str) 7500Sstevel@tonic-gate { 7510Sstevel@tonic-gate char *last = strrchr(str, '/'); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate if (last == NULL) 7540Sstevel@tonic-gate return (str); 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate return (last + 1); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 759265Smws /* 760265Smws * dt_popc() is a fast implementation of population count. The algorithm is 761265Smws * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added. 762265Smws */ 763265Smws ulong_t 764265Smws dt_popc(ulong_t x) 765265Smws { 766265Smws #ifdef _ILP32 767265Smws x = x - ((x >> 1) & 0x55555555UL); 768265Smws x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL); 769265Smws x = (x + (x >> 4)) & 0x0F0F0F0FUL; 770265Smws x = x + (x >> 8); 771265Smws x = x + (x >> 16); 772265Smws return (x & 0x3F); 773265Smws #endif 774265Smws #ifdef _LP64 775265Smws x = x - ((x >> 1) & 0x5555555555555555ULL); 776265Smws x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); 777265Smws x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL; 778265Smws x = x + (x >> 8); 779265Smws x = x + (x >> 16); 780265Smws x = x + (x >> 32); 781265Smws return (x & 0x7F); 782265Smws #endif 783265Smws } 784265Smws 785265Smws /* 786265Smws * dt_popcb() is a bitmap-based version of population count that returns the 787265Smws * number of one bits in the specified bitmap 'bp' at bit positions below 'n'. 788265Smws */ 789265Smws ulong_t 790265Smws dt_popcb(const ulong_t *bp, ulong_t n) 791265Smws { 792265Smws ulong_t maxb = n & BT_ULMASK; 793265Smws ulong_t maxw = n >> BT_ULSHIFT; 794265Smws ulong_t w, popc = 0; 795265Smws 796265Smws if (n == 0) 797265Smws return (0); 798265Smws 799265Smws for (w = 0; w < maxw; w++) 800265Smws popc += dt_popc(bp[w]); 801265Smws 802265Smws return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1))); 803265Smws } 804265Smws 8050Sstevel@tonic-gate struct _rwlock; 8060Sstevel@tonic-gate struct _lwp_mutex; 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate int 8090Sstevel@tonic-gate dt_rw_read_held(pthread_rwlock_t *lock) 8100Sstevel@tonic-gate { 8110Sstevel@tonic-gate extern int _rw_read_held(struct _rwlock *); 8120Sstevel@tonic-gate return (_rw_read_held((struct _rwlock *)lock)); 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate int 8160Sstevel@tonic-gate dt_rw_write_held(pthread_rwlock_t *lock) 8170Sstevel@tonic-gate { 8180Sstevel@tonic-gate extern int _rw_write_held(struct _rwlock *); 8190Sstevel@tonic-gate return (_rw_write_held((struct _rwlock *)lock)); 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate int 8230Sstevel@tonic-gate dt_mutex_held(pthread_mutex_t *lock) 8240Sstevel@tonic-gate { 8250Sstevel@tonic-gate extern int _mutex_held(struct _lwp_mutex *); 8260Sstevel@tonic-gate return (_mutex_held((struct _lwp_mutex *)lock)); 8270Sstevel@tonic-gate } 828457Sbmc 829457Sbmc static int 830457Sbmc dt_string2str(char *s, char *str, int nbytes) 831457Sbmc { 832457Sbmc int len = strlen(s); 833457Sbmc 834457Sbmc if (nbytes == 0) { 835457Sbmc /* 836457Sbmc * Like snprintf(3C), we don't check the value of str if the 837457Sbmc * number of bytes is 0. 838457Sbmc */ 839457Sbmc return (len); 840457Sbmc } 841457Sbmc 842457Sbmc if (nbytes <= len) { 843457Sbmc (void) strncpy(str, s, nbytes - 1); 844457Sbmc /* 845457Sbmc * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee 846457Sbmc * that the string is null-terminated. 847457Sbmc */ 848457Sbmc str[nbytes - 1] = '\0'; 849457Sbmc } else { 850457Sbmc (void) strcpy(str, s); 851457Sbmc } 852457Sbmc 853457Sbmc return (len); 854457Sbmc } 855457Sbmc 856457Sbmc int 857457Sbmc dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes) 858457Sbmc { 859457Sbmc dtrace_syminfo_t dts; 860457Sbmc GElf_Sym sym; 861457Sbmc 862457Sbmc size_t n = 20; /* for 0x%llx\0 */ 863457Sbmc char *s; 864457Sbmc int err; 865457Sbmc 866457Sbmc if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0) 867457Sbmc n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */ 868457Sbmc 869457Sbmc s = alloca(n); 870457Sbmc 871457Sbmc if (err == 0 && addr != sym.st_value) { 872457Sbmc (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object, 873457Sbmc dts.dts_name, (u_longlong_t)addr - sym.st_value); 874457Sbmc } else if (err == 0) { 875457Sbmc (void) snprintf(s, n, "%s`%s", 876457Sbmc dts.dts_object, dts.dts_name); 877457Sbmc } else { 878457Sbmc /* 879457Sbmc * We'll repeat the lookup, but this time we'll specify a NULL 880457Sbmc * GElf_Sym -- indicating that we're only interested in the 881457Sbmc * containing module. 882457Sbmc */ 883457Sbmc if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) { 884457Sbmc (void) snprintf(s, n, "%s`0x%llx", dts.dts_object, 885457Sbmc (u_longlong_t)addr); 886457Sbmc } else { 887457Sbmc (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr); 888457Sbmc } 889457Sbmc } 890457Sbmc 891457Sbmc return (dt_string2str(s, str, nbytes)); 892457Sbmc } 893457Sbmc 894457Sbmc int 895457Sbmc dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid, 896457Sbmc uint64_t addr, char *str, int nbytes) 897457Sbmc { 898457Sbmc char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 899457Sbmc struct ps_prochandle *P = NULL; 900457Sbmc GElf_Sym sym; 901457Sbmc char *obj; 902457Sbmc 903457Sbmc if (pid != 0) 904457Sbmc P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 905457Sbmc 906457Sbmc if (P == NULL) { 907457Sbmc (void) snprintf(c, sizeof (c), "0x%llx", addr); 908457Sbmc return (dt_string2str(c, str, nbytes)); 909457Sbmc } 910457Sbmc 911457Sbmc dt_proc_lock(dtp, P); 912457Sbmc 913457Sbmc if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) { 914457Sbmc (void) Pobjname(P, addr, objname, sizeof (objname)); 915457Sbmc 916457Sbmc obj = dt_basename(objname); 917457Sbmc 918457Sbmc if (addr > sym.st_value) { 919457Sbmc (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj, 920457Sbmc name, (u_longlong_t)(addr - sym.st_value)); 921457Sbmc } else { 922457Sbmc (void) snprintf(c, sizeof (c), "%s`%s", obj, name); 923457Sbmc } 924457Sbmc } else if (Pobjname(P, addr, objname, sizeof (objname)) != NULL) { 925457Sbmc (void) snprintf(c, sizeof (c), "%s`0x%llx", 926457Sbmc dt_basename(objname), addr); 927457Sbmc } else { 928457Sbmc (void) snprintf(c, sizeof (c), "0x%llx", addr); 929457Sbmc } 930457Sbmc 931457Sbmc dt_proc_unlock(dtp, P); 932457Sbmc dt_proc_release(dtp, P); 933457Sbmc 934457Sbmc return (dt_string2str(c, str, nbytes)); 935457Sbmc } 936