dt_subr.c revision 223262
1178479Sjb/* 2178479Sjb * CDDL HEADER START 3178479Sjb * 4178479Sjb * The contents of this file are subject to the terms of the 5210767Srpaulo * Common Development and Distribution License (the "License"). 6210767Srpaulo * You may not use this file except in compliance with the License. 7178479Sjb * 8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178479Sjb * or http://www.opensolaris.org/os/licensing. 10178479Sjb * See the License for the specific language governing permissions 11178479Sjb * and limitations under the License. 12178479Sjb * 13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178479Sjb * If applicable, add the following below this CDDL HEADER, with the 16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178479Sjb * 19178479Sjb * CDDL HEADER END 20178479Sjb */ 21210767Srpaulo 22178479Sjb/* 23210767Srpaulo * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24178479Sjb * Use is subject to license terms. 25178479Sjb */ 26178479Sjb 27178556Sjb#if defined(sun) 28178479Sjb#include <sys/sysmacros.h> 29178556Sjb#endif 30178479Sjb 31178479Sjb#include <strings.h> 32178479Sjb#include <unistd.h> 33178479Sjb#include <stdarg.h> 34178479Sjb#include <stddef.h> 35178479Sjb#include <stdlib.h> 36178479Sjb#include <stdio.h> 37178479Sjb#include <errno.h> 38178479Sjb#include <ctype.h> 39178556Sjb#if defined(sun) 40178479Sjb#include <alloca.h> 41178556Sjb#else 42178556Sjb#include <sys/sysctl.h> 43211554Srpaulo#include <libproc_compat.h> 44178556Sjb#endif 45178479Sjb#include <assert.h> 46178479Sjb#include <libgen.h> 47178479Sjb#include <limits.h> 48223262Sbenl#include <stdint.h> 49178479Sjb 50178479Sjb#include <dt_impl.h> 51178479Sjb 52178479Sjbstatic const struct { 53178479Sjb size_t dtps_offset; 54178479Sjb size_t dtps_len; 55178479Sjb} dtrace_probespecs[] = { 56178479Sjb { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN }, 57178479Sjb { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN }, 58178479Sjb { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN }, 59178479Sjb { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN } 60178479Sjb}; 61178479Sjb 62178479Sjbint 63178479Sjbdtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec, 64178479Sjb const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp) 65178479Sjb{ 66210767Srpaulo size_t off, len, vlen, wlen; 67210767Srpaulo const char *p, *q, *v, *w; 68178479Sjb 69178479Sjb char buf[32]; /* for id_t as %d (see below) */ 70178479Sjb 71178479Sjb if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME) 72178479Sjb return (dt_set_errno(dtp, EINVAL)); 73178479Sjb 74178479Sjb bzero(pdp, sizeof (dtrace_probedesc_t)); 75178479Sjb p = s + strlen(s) - 1; 76178479Sjb 77178479Sjb do { 78178479Sjb for (len = 0; p >= s && *p != ':'; len++) 79178479Sjb p--; /* move backward until we find a delimiter */ 80178479Sjb 81178479Sjb q = p + 1; 82178479Sjb vlen = 0; 83210767Srpaulo w = NULL; 84210767Srpaulo wlen = 0; 85178479Sjb 86178479Sjb if ((v = strchr(q, '$')) != NULL && v < q + len) { 87178479Sjb /* 88178479Sjb * Set vlen to the length of the variable name and then 89178479Sjb * reset len to the length of the text prior to '$'. If 90178479Sjb * the name begins with a digit, interpret it using the 91178479Sjb * the argv[] array. Otherwise we look in dt_macros. 92178479Sjb * For the moment, all dt_macros variables are of type 93178479Sjb * id_t (see dtrace_update() for more details on that). 94178479Sjb */ 95178479Sjb vlen = (size_t)(q + len - v); 96178479Sjb len = (size_t)(v - q); 97178479Sjb 98178479Sjb /* 99178479Sjb * If the variable string begins with $$, skip past the 100178479Sjb * leading dollar sign since $ and $$ are equivalent 101178479Sjb * macro reference operators in a probe description. 102178479Sjb */ 103178479Sjb if (vlen > 2 && v[1] == '$') { 104178479Sjb vlen--; 105178479Sjb v++; 106178479Sjb } 107178479Sjb 108178479Sjb if (isdigit(v[1])) { 109178479Sjb long i; 110178479Sjb 111178479Sjb errno = 0; 112210767Srpaulo i = strtol(v + 1, (char **)&w, 10); 113178479Sjb 114210767Srpaulo wlen = vlen - (w - v); 115210767Srpaulo 116210767Srpaulo if (i < 0 || i >= argc || errno != 0) 117178479Sjb return (dt_set_errno(dtp, EDT_BADSPCV)); 118178479Sjb 119178479Sjb v = argv[i]; 120178479Sjb vlen = strlen(v); 121178479Sjb 122178479Sjb if (yypcb != NULL && yypcb->pcb_sargv == argv) 123178479Sjb yypcb->pcb_sflagv[i] |= DT_IDFLG_REF; 124178479Sjb 125178479Sjb } else if (vlen > 1) { 126178479Sjb char *vstr = alloca(vlen); 127178479Sjb dt_ident_t *idp; 128178479Sjb 129178479Sjb (void) strncpy(vstr, v + 1, vlen - 1); 130178479Sjb vstr[vlen - 1] = '\0'; 131178479Sjb idp = dt_idhash_lookup(dtp->dt_macros, vstr); 132178479Sjb 133178479Sjb if (idp == NULL) 134178479Sjb return (dt_set_errno(dtp, EDT_BADSPCV)); 135178479Sjb 136178479Sjb v = buf; 137178479Sjb vlen = snprintf(buf, 32, "%d", idp->di_id); 138178479Sjb 139178479Sjb } else 140178479Sjb return (dt_set_errno(dtp, EDT_BADSPCV)); 141178479Sjb } 142178479Sjb 143178479Sjb if (spec == DTRACE_PROBESPEC_NONE) 144178479Sjb return (dt_set_errno(dtp, EDT_BADSPEC)); 145178479Sjb 146178479Sjb if (len + vlen >= dtrace_probespecs[spec].dtps_len) 147178479Sjb return (dt_set_errno(dtp, ENAMETOOLONG)); 148178479Sjb 149178479Sjb off = dtrace_probespecs[spec--].dtps_offset; 150178479Sjb bcopy(q, (char *)pdp + off, len); 151178479Sjb bcopy(v, (char *)pdp + off + len, vlen); 152210767Srpaulo bcopy(w, (char *)pdp + off + len + vlen, wlen); 153178479Sjb } while (--p >= s); 154178479Sjb 155178479Sjb pdp->dtpd_id = DTRACE_IDNONE; 156178479Sjb return (0); 157178479Sjb} 158178479Sjb 159178479Sjbint 160178479Sjbdtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec, 161178479Sjb const char *s, dtrace_probedesc_t *pdp) 162178479Sjb{ 163178479Sjb return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp)); 164178479Sjb} 165178479Sjb 166178479Sjbint 167178479Sjbdtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp) 168178479Sjb{ 169178479Sjb bzero(pdp, sizeof (dtrace_probedesc_t)); 170178479Sjb pdp->dtpd_id = id; 171178479Sjb 172178479Sjb if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 || 173178479Sjb pdp->dtpd_id != id) 174178479Sjb return (dt_set_errno(dtp, EDT_BADID)); 175178479Sjb 176178479Sjb return (0); 177178479Sjb} 178178479Sjb 179178479Sjbchar * 180178479Sjbdtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len) 181178479Sjb{ 182178479Sjb if (pdp->dtpd_id == 0) { 183178479Sjb (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider, 184178479Sjb pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name); 185178479Sjb } else 186178479Sjb (void) snprintf(buf, len, "%u", pdp->dtpd_id); 187178479Sjb 188178479Sjb return (buf); 189178479Sjb} 190178479Sjb 191178479Sjbchar * 192178479Sjbdtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len) 193178479Sjb{ 194178479Sjb const char *name = dtrace_stability_name(attr.dtat_name); 195178479Sjb const char *data = dtrace_stability_name(attr.dtat_data); 196178479Sjb const char *class = dtrace_class_name(attr.dtat_class); 197178479Sjb 198178479Sjb if (name == NULL || data == NULL || class == NULL) 199178479Sjb return (NULL); /* one or more invalid attributes */ 200178479Sjb 201178479Sjb (void) snprintf(buf, len, "%s/%s/%s", name, data, class); 202178479Sjb return (buf); 203178479Sjb} 204178479Sjb 205178479Sjbstatic char * 206178479Sjbdt_getstrattr(char *p, char **qp) 207178479Sjb{ 208178479Sjb char *q; 209178479Sjb 210178479Sjb if (*p == '\0') 211178479Sjb return (NULL); 212178479Sjb 213178479Sjb if ((q = strchr(p, '/')) == NULL) 214178479Sjb q = p + strlen(p); 215178479Sjb else 216178479Sjb *q++ = '\0'; 217178479Sjb 218178479Sjb *qp = q; 219178479Sjb return (p); 220178479Sjb} 221178479Sjb 222178479Sjbint 223178479Sjbdtrace_str2attr(const char *str, dtrace_attribute_t *attr) 224178479Sjb{ 225178479Sjb dtrace_stability_t s; 226178479Sjb dtrace_class_t c; 227178479Sjb char *p, *q; 228178479Sjb 229178479Sjb if (str == NULL || attr == NULL) 230178479Sjb return (-1); /* invalid function arguments */ 231178479Sjb 232178479Sjb *attr = _dtrace_maxattr; 233178479Sjb p = alloca(strlen(str) + 1); 234178479Sjb (void) strcpy(p, str); 235178479Sjb 236178479Sjb if ((p = dt_getstrattr(p, &q)) == NULL) 237178479Sjb return (0); 238178479Sjb 239178479Sjb for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 240178479Sjb if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 241178479Sjb attr->dtat_name = s; 242178479Sjb break; 243178479Sjb } 244178479Sjb } 245178479Sjb 246178479Sjb if (s > DTRACE_STABILITY_MAX) 247178479Sjb return (-1); 248178479Sjb 249178479Sjb if ((p = dt_getstrattr(q, &q)) == NULL) 250178479Sjb return (0); 251178479Sjb 252178479Sjb for (s = 0; s <= DTRACE_STABILITY_MAX; s++) { 253178479Sjb if (strcasecmp(p, dtrace_stability_name(s)) == 0) { 254178479Sjb attr->dtat_data = s; 255178479Sjb break; 256178479Sjb } 257178479Sjb } 258178479Sjb 259178479Sjb if (s > DTRACE_STABILITY_MAX) 260178479Sjb return (-1); 261178479Sjb 262178479Sjb if ((p = dt_getstrattr(q, &q)) == NULL) 263178479Sjb return (0); 264178479Sjb 265178479Sjb for (c = 0; c <= DTRACE_CLASS_MAX; c++) { 266178479Sjb if (strcasecmp(p, dtrace_class_name(c)) == 0) { 267178479Sjb attr->dtat_class = c; 268178479Sjb break; 269178479Sjb } 270178479Sjb } 271178479Sjb 272178479Sjb if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL) 273178479Sjb return (-1); 274178479Sjb 275178479Sjb return (0); 276178479Sjb} 277178479Sjb 278178479Sjbconst char * 279178479Sjbdtrace_stability_name(dtrace_stability_t s) 280178479Sjb{ 281178479Sjb switch (s) { 282178479Sjb case DTRACE_STABILITY_INTERNAL: return ("Internal"); 283178479Sjb case DTRACE_STABILITY_PRIVATE: return ("Private"); 284178479Sjb case DTRACE_STABILITY_OBSOLETE: return ("Obsolete"); 285178479Sjb case DTRACE_STABILITY_EXTERNAL: return ("External"); 286178479Sjb case DTRACE_STABILITY_UNSTABLE: return ("Unstable"); 287178479Sjb case DTRACE_STABILITY_EVOLVING: return ("Evolving"); 288178479Sjb case DTRACE_STABILITY_STABLE: return ("Stable"); 289178479Sjb case DTRACE_STABILITY_STANDARD: return ("Standard"); 290178479Sjb default: return (NULL); 291178479Sjb } 292178479Sjb} 293178479Sjb 294178479Sjbconst char * 295178479Sjbdtrace_class_name(dtrace_class_t c) 296178479Sjb{ 297178479Sjb switch (c) { 298178479Sjb case DTRACE_CLASS_UNKNOWN: return ("Unknown"); 299178479Sjb case DTRACE_CLASS_CPU: return ("CPU"); 300178479Sjb case DTRACE_CLASS_PLATFORM: return ("Platform"); 301178479Sjb case DTRACE_CLASS_GROUP: return ("Group"); 302178479Sjb case DTRACE_CLASS_ISA: return ("ISA"); 303178479Sjb case DTRACE_CLASS_COMMON: return ("Common"); 304178479Sjb default: return (NULL); 305178479Sjb } 306178479Sjb} 307178479Sjb 308178479Sjbdtrace_attribute_t 309178479Sjbdt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2) 310178479Sjb{ 311178479Sjb dtrace_attribute_t am; 312178479Sjb 313178479Sjb am.dtat_name = MIN(a1.dtat_name, a2.dtat_name); 314178479Sjb am.dtat_data = MIN(a1.dtat_data, a2.dtat_data); 315178479Sjb am.dtat_class = MIN(a1.dtat_class, a2.dtat_class); 316178479Sjb 317178479Sjb return (am); 318178479Sjb} 319178479Sjb 320178479Sjbdtrace_attribute_t 321178479Sjbdt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2) 322178479Sjb{ 323178479Sjb dtrace_attribute_t am; 324178479Sjb 325178479Sjb am.dtat_name = MAX(a1.dtat_name, a2.dtat_name); 326178479Sjb am.dtat_data = MAX(a1.dtat_data, a2.dtat_data); 327178479Sjb am.dtat_class = MAX(a1.dtat_class, a2.dtat_class); 328178479Sjb 329178479Sjb return (am); 330178479Sjb} 331178479Sjb 332178479Sjb/* 333178479Sjb * Compare two attributes and return an integer value in the following ranges: 334178479Sjb * 335178479Sjb * <0 if any of a1's attributes are less than a2's attributes 336178479Sjb * =0 if all of a1's attributes are equal to a2's attributes 337178479Sjb * >0 if all of a1's attributes are greater than or equal to a2's attributes 338178479Sjb * 339178479Sjb * To implement this function efficiently, we subtract a2's attributes from 340178479Sjb * a1's to obtain a negative result if an a1 attribute is less than its a2 341178479Sjb * counterpart. We then OR the intermediate results together, relying on the 342178479Sjb * twos-complement property that if any result is negative, the bitwise union 343178479Sjb * will also be negative since the highest bit will be set in the result. 344178479Sjb */ 345178479Sjbint 346178479Sjbdt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2) 347178479Sjb{ 348178479Sjb return (((int)a1.dtat_name - a2.dtat_name) | 349178479Sjb ((int)a1.dtat_data - a2.dtat_data) | 350178479Sjb ((int)a1.dtat_class - a2.dtat_class)); 351178479Sjb} 352178479Sjb 353178479Sjbchar * 354178479Sjbdt_attr_str(dtrace_attribute_t a, char *buf, size_t len) 355178479Sjb{ 356178479Sjb static const char stability[] = "ipoxuesS"; 357178479Sjb static const char class[] = "uCpgIc"; 358178479Sjb 359178479Sjb if (a.dtat_name < sizeof (stability) && 360178479Sjb a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) { 361178479Sjb (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name], 362178479Sjb stability[a.dtat_data], class[a.dtat_class]); 363178479Sjb } else { 364178479Sjb (void) snprintf(buf, len, "[%u/%u/%u]", 365178479Sjb a.dtat_name, a.dtat_data, a.dtat_class); 366178479Sjb } 367178479Sjb 368178479Sjb return (buf); 369178479Sjb} 370178479Sjb 371178479Sjbchar * 372178479Sjbdt_version_num2str(dt_version_t v, char *buf, size_t len) 373178479Sjb{ 374178479Sjb uint_t M = DT_VERSION_MAJOR(v); 375178479Sjb uint_t m = DT_VERSION_MINOR(v); 376178479Sjb uint_t u = DT_VERSION_MICRO(v); 377178479Sjb 378178479Sjb if (u == 0) 379178479Sjb (void) snprintf(buf, len, "%u.%u", M, m); 380178479Sjb else 381178479Sjb (void) snprintf(buf, len, "%u.%u.%u", M, m, u); 382178479Sjb 383178479Sjb return (buf); 384178479Sjb} 385178479Sjb 386178479Sjbint 387178479Sjbdt_version_str2num(const char *s, dt_version_t *vp) 388178479Sjb{ 389178479Sjb int i = 0, n[3] = { 0, 0, 0 }; 390178479Sjb char c; 391178479Sjb 392178479Sjb while ((c = *s++) != '\0') { 393178479Sjb if (isdigit(c)) 394178479Sjb n[i] = n[i] * 10 + c - '0'; 395178479Sjb else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1) 396178479Sjb return (-1); 397178479Sjb } 398178479Sjb 399178479Sjb if (n[0] > DT_VERSION_MAJMAX || 400178479Sjb n[1] > DT_VERSION_MINMAX || 401178479Sjb n[2] > DT_VERSION_MICMAX) 402178479Sjb return (-1); 403178479Sjb 404178479Sjb if (vp != NULL) 405178479Sjb *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]); 406178479Sjb 407178479Sjb return (0); 408178479Sjb} 409178479Sjb 410178479Sjbint 411178479Sjbdt_version_defined(dt_version_t v) 412178479Sjb{ 413178479Sjb int i; 414178479Sjb 415178479Sjb for (i = 0; _dtrace_versions[i] != 0; i++) { 416178479Sjb if (_dtrace_versions[i] == v) 417178479Sjb return (1); 418178479Sjb } 419178479Sjb 420178479Sjb return (0); 421178479Sjb} 422178479Sjb 423178479Sjbchar * 424178479Sjbdt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str) 425178479Sjb{ 426178479Sjb char *arg; 427178479Sjb 428178479Sjb if (dtp->dt_cpp_argc == dtp->dt_cpp_args) { 429178479Sjb int olds = dtp->dt_cpp_args; 430178479Sjb int news = olds * 2; 431178479Sjb char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news); 432178479Sjb 433178479Sjb if (argv == NULL) 434178479Sjb return (NULL); 435178479Sjb 436178479Sjb bzero(&argv[olds], sizeof (char *) * olds); 437178479Sjb dtp->dt_cpp_argv = argv; 438178479Sjb dtp->dt_cpp_args = news; 439178479Sjb } 440178479Sjb 441178479Sjb if ((arg = strdup(str)) == NULL) 442178479Sjb return (NULL); 443178479Sjb 444178479Sjb assert(dtp->dt_cpp_argc < dtp->dt_cpp_args); 445178479Sjb dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg; 446178479Sjb return (arg); 447178479Sjb} 448178479Sjb 449178479Sjbchar * 450178479Sjbdt_cpp_pop_arg(dtrace_hdl_t *dtp) 451178479Sjb{ 452178479Sjb char *arg; 453178479Sjb 454178479Sjb if (dtp->dt_cpp_argc <= 1) 455178479Sjb return (NULL); /* dt_cpp_argv[0] cannot be popped */ 456178479Sjb 457178479Sjb arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc]; 458178479Sjb dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL; 459178479Sjb 460178479Sjb return (arg); 461178479Sjb} 462178479Sjb 463178479Sjb/*PRINTFLIKE1*/ 464178479Sjbvoid 465178479Sjbdt_dprintf(const char *format, ...) 466178479Sjb{ 467178479Sjb if (_dtrace_debug) { 468178479Sjb va_list alist; 469178479Sjb 470178479Sjb va_start(alist, format); 471178479Sjb (void) fputs("libdtrace DEBUG: ", stderr); 472178479Sjb (void) vfprintf(stderr, format, alist); 473178479Sjb va_end(alist); 474178479Sjb } 475178479Sjb} 476178479Sjb 477178479Sjbint 478178556Sjb#if defined(sun) 479178479Sjbdt_ioctl(dtrace_hdl_t *dtp, int val, void *arg) 480178556Sjb#else 481178556Sjbdt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg) 482178556Sjb#endif 483178479Sjb{ 484178479Sjb const dtrace_vector_t *v = dtp->dt_vector; 485178479Sjb 486178556Sjb#if !defined(sun) 487178556Sjb /* Avoid sign extension. */ 488178556Sjb val &= 0xffffffff; 489178556Sjb#endif 490178556Sjb 491178479Sjb if (v != NULL) 492178479Sjb return (v->dtv_ioctl(dtp->dt_varg, val, arg)); 493178479Sjb 494178479Sjb if (dtp->dt_fd >= 0) 495178479Sjb return (ioctl(dtp->dt_fd, val, arg)); 496178479Sjb 497178479Sjb errno = EBADF; 498178479Sjb return (-1); 499178479Sjb} 500178479Sjb 501178479Sjbint 502178479Sjbdt_status(dtrace_hdl_t *dtp, processorid_t cpu) 503178479Sjb{ 504178479Sjb const dtrace_vector_t *v = dtp->dt_vector; 505178479Sjb 506178556Sjb if (v == NULL) { 507178556Sjb#if defined(sun) 508178479Sjb return (p_online(cpu, P_STATUS)); 509178556Sjb#else 510178556Sjb int maxid = 0; 511178556Sjb size_t len = sizeof(maxid); 512178556Sjb if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0) 513178556Sjb return (cpu == 0 ? 1 : -1); 514178556Sjb else 515178556Sjb return (cpu <= maxid ? 1 : -1); 516178556Sjb#endif 517178556Sjb } 518178479Sjb 519178479Sjb return (v->dtv_status(dtp->dt_varg, cpu)); 520178479Sjb} 521178479Sjb 522178479Sjblong 523178479Sjbdt_sysconf(dtrace_hdl_t *dtp, int name) 524178479Sjb{ 525178479Sjb const dtrace_vector_t *v = dtp->dt_vector; 526178479Sjb 527178479Sjb if (v == NULL) 528178479Sjb return (sysconf(name)); 529178479Sjb 530178479Sjb return (v->dtv_sysconf(dtp->dt_varg, name)); 531178479Sjb} 532178479Sjb 533178479Sjb/* 534178479Sjb * Wrapper around write(2) to handle partial writes. For maximum safety of 535178479Sjb * output files and proper error reporting, we continuing writing in the 536178479Sjb * face of partial writes until write(2) fails or 'buf' is completely written. 537178479Sjb * We also record any errno in the specified dtrace_hdl_t as well as 'errno'. 538178479Sjb */ 539178479Sjbssize_t 540178479Sjbdt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n) 541178479Sjb{ 542178479Sjb ssize_t resid = n; 543178479Sjb ssize_t len; 544178479Sjb 545178479Sjb while (resid != 0) { 546178479Sjb if ((len = write(fd, buf, resid)) <= 0) 547178479Sjb break; 548178479Sjb 549178479Sjb resid -= len; 550178479Sjb buf = (char *)buf + len; 551178479Sjb } 552178479Sjb 553178479Sjb if (resid == n && n != 0) 554178479Sjb return (dt_set_errno(dtp, errno)); 555178479Sjb 556178479Sjb return (n - resid); 557178479Sjb} 558178479Sjb 559178479Sjb/* 560178479Sjb * This function handles all output from libdtrace, as well as the 561178479Sjb * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then 562178479Sjb * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the 563178479Sjb * specified buffer and return. Otherwise, if output is buffered (denoted by 564178479Sjb * a NULL fp), we sprintf the desired output into the buffered buffer 565178479Sjb * (expanding the buffer if required). If we don't satisfy either of these 566178479Sjb * conditions (that is, if we are to actually generate output), then we call 567178479Sjb * fprintf with the specified fp. In this case, we need to deal with one of 568178479Sjb * the more annoying peculiarities of libc's printf routines: any failed 569178479Sjb * write persistently sets an error flag inside the FILE causing every 570178479Sjb * subsequent write to fail, but only the caller that initiated the error gets 571178479Sjb * the errno. Since libdtrace clients often intercept SIGINT, this case is 572178479Sjb * particularly frustrating since we don't want the EINTR on one attempt to 573178479Sjb * write to the output file to preclude later attempts to write. This 574178479Sjb * function therefore does a clearerr() if any error occurred, and saves the 575178479Sjb * errno for the caller inside the specified dtrace_hdl_t. 576178479Sjb */ 577178479Sjb/*PRINTFLIKE3*/ 578178479Sjbint 579178479Sjbdt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...) 580178479Sjb{ 581178479Sjb va_list ap; 582178479Sjb int n; 583178479Sjb 584178556Sjb#if !defined(sun) 585178556Sjb /* 586178556Sjb * On FreeBSD, check if output is currently being re-directed 587178556Sjb * to another file. If so, output to that file instead of the 588178556Sjb * one the caller has specified. 589178556Sjb */ 590178556Sjb if (dtp->dt_freopen_fp != NULL) 591178556Sjb fp = dtp->dt_freopen_fp; 592178556Sjb#endif 593178556Sjb 594178479Sjb va_start(ap, format); 595178479Sjb 596178479Sjb if (dtp->dt_sprintf_buflen != 0) { 597178479Sjb int len; 598178479Sjb char *buf; 599178479Sjb 600178479Sjb assert(dtp->dt_sprintf_buf != NULL); 601178479Sjb 602178479Sjb buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)]; 603178479Sjb len = dtp->dt_sprintf_buflen - len; 604178479Sjb assert(len >= 0); 605178479Sjb 606178479Sjb if ((n = vsnprintf(buf, len, format, ap)) < 0) 607178479Sjb n = dt_set_errno(dtp, errno); 608178479Sjb 609178479Sjb va_end(ap); 610178479Sjb 611178479Sjb return (n); 612178479Sjb } 613178479Sjb 614178479Sjb if (fp == NULL) { 615178479Sjb int needed, rval; 616178479Sjb size_t avail; 617178479Sjb 618178479Sjb /* 619178479Sjb * It's not legal to use buffered ouput if there is not a 620178479Sjb * handler for buffered output. 621178479Sjb */ 622178479Sjb if (dtp->dt_bufhdlr == NULL) { 623178479Sjb va_end(ap); 624178479Sjb return (dt_set_errno(dtp, EDT_NOBUFFERED)); 625178479Sjb } 626178479Sjb 627178479Sjb if (dtp->dt_buffered_buf == NULL) { 628178479Sjb assert(dtp->dt_buffered_size == 0); 629178479Sjb dtp->dt_buffered_size = 1; 630178479Sjb dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size); 631178479Sjb 632178479Sjb if (dtp->dt_buffered_buf == NULL) { 633178479Sjb va_end(ap); 634178479Sjb return (dt_set_errno(dtp, EDT_NOMEM)); 635178479Sjb } 636178479Sjb 637178479Sjb dtp->dt_buffered_offs = 0; 638178479Sjb dtp->dt_buffered_buf[0] = '\0'; 639178479Sjb } 640178479Sjb 641178479Sjb if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) { 642178479Sjb rval = dt_set_errno(dtp, errno); 643178479Sjb va_end(ap); 644178479Sjb return (rval); 645178479Sjb } 646178479Sjb 647178479Sjb if (needed == 0) { 648178479Sjb va_end(ap); 649178479Sjb return (0); 650178479Sjb } 651178479Sjb 652178479Sjb for (;;) { 653178479Sjb char *newbuf; 654178479Sjb 655178479Sjb assert(dtp->dt_buffered_offs < dtp->dt_buffered_size); 656178479Sjb avail = dtp->dt_buffered_size - dtp->dt_buffered_offs; 657178479Sjb 658178479Sjb if (needed + 1 < avail) 659178479Sjb break; 660178479Sjb 661178479Sjb if ((newbuf = realloc(dtp->dt_buffered_buf, 662178479Sjb dtp->dt_buffered_size << 1)) == NULL) { 663178479Sjb va_end(ap); 664178479Sjb return (dt_set_errno(dtp, EDT_NOMEM)); 665178479Sjb } 666178479Sjb 667178479Sjb dtp->dt_buffered_buf = newbuf; 668178479Sjb dtp->dt_buffered_size <<= 1; 669178479Sjb } 670178479Sjb 671178479Sjb if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs], 672178479Sjb avail, format, ap) < 0) { 673178479Sjb rval = dt_set_errno(dtp, errno); 674178479Sjb va_end(ap); 675178479Sjb return (rval); 676178479Sjb } 677178479Sjb 678178479Sjb dtp->dt_buffered_offs += needed; 679178479Sjb assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0'); 680178479Sjb return (0); 681178479Sjb } 682178479Sjb 683178479Sjb n = vfprintf(fp, format, ap); 684178556Sjb fflush(fp); 685178479Sjb va_end(ap); 686178479Sjb 687178479Sjb if (n < 0) { 688178479Sjb clearerr(fp); 689178479Sjb return (dt_set_errno(dtp, errno)); 690178479Sjb } 691178479Sjb 692178479Sjb return (n); 693178479Sjb} 694178479Sjb 695178479Sjbint 696178479Sjbdt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata, 697178479Sjb const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags) 698178479Sjb{ 699178479Sjb dtrace_bufdata_t data; 700178479Sjb 701178479Sjb if (dtp->dt_buffered_offs == 0) 702178479Sjb return (0); 703178479Sjb 704178479Sjb data.dtbda_handle = dtp; 705178479Sjb data.dtbda_buffered = dtp->dt_buffered_buf; 706178479Sjb data.dtbda_probe = pdata; 707178479Sjb data.dtbda_recdesc = rec; 708178479Sjb data.dtbda_aggdata = agg; 709178479Sjb data.dtbda_flags = flags; 710178479Sjb 711178479Sjb if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT) 712178479Sjb return (dt_set_errno(dtp, EDT_DIRABORT)); 713178479Sjb 714178479Sjb dtp->dt_buffered_offs = 0; 715178479Sjb dtp->dt_buffered_buf[0] = '\0'; 716178479Sjb 717178479Sjb return (0); 718178479Sjb} 719178479Sjb 720178479Sjbvoid 721178479Sjbdt_buffered_destroy(dtrace_hdl_t *dtp) 722178479Sjb{ 723178479Sjb free(dtp->dt_buffered_buf); 724178479Sjb dtp->dt_buffered_buf = NULL; 725178479Sjb dtp->dt_buffered_offs = 0; 726178479Sjb dtp->dt_buffered_size = 0; 727178479Sjb} 728178479Sjb 729178479Sjbvoid * 730178479Sjbdt_zalloc(dtrace_hdl_t *dtp, size_t size) 731178479Sjb{ 732178479Sjb void *data; 733178479Sjb 734178556Sjb if (size > 16 * 1024 * 1024) { 735178556Sjb (void) dt_set_errno(dtp, EDT_NOMEM); 736178556Sjb return (NULL); 737178556Sjb } 738178556Sjb 739178479Sjb if ((data = malloc(size)) == NULL) 740178479Sjb (void) dt_set_errno(dtp, EDT_NOMEM); 741178479Sjb else 742178479Sjb bzero(data, size); 743178479Sjb 744178479Sjb return (data); 745178479Sjb} 746178479Sjb 747178479Sjbvoid * 748178479Sjbdt_alloc(dtrace_hdl_t *dtp, size_t size) 749178479Sjb{ 750178479Sjb void *data; 751178479Sjb 752178556Sjb if (size > 16 * 1024 * 1024) { 753178556Sjb (void) dt_set_errno(dtp, EDT_NOMEM); 754178556Sjb return (NULL); 755178556Sjb } 756178556Sjb 757178479Sjb if ((data = malloc(size)) == NULL) 758178479Sjb (void) dt_set_errno(dtp, EDT_NOMEM); 759178479Sjb 760178479Sjb return (data); 761178479Sjb} 762178479Sjb 763178479Sjbvoid 764178479Sjbdt_free(dtrace_hdl_t *dtp, void *data) 765178479Sjb{ 766178479Sjb assert(dtp != NULL); /* ensure sane use of this interface */ 767178479Sjb free(data); 768178479Sjb} 769178479Sjb 770178479Sjbvoid 771178479Sjbdt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp) 772178479Sjb{ 773178479Sjb if (dp == NULL) 774178479Sjb return; /* simplify caller code */ 775178479Sjb 776178479Sjb dt_free(dtp, dp->dtdo_buf); 777178479Sjb dt_free(dtp, dp->dtdo_inttab); 778178479Sjb dt_free(dtp, dp->dtdo_strtab); 779178479Sjb dt_free(dtp, dp->dtdo_vartab); 780178479Sjb dt_free(dtp, dp->dtdo_kreltab); 781178479Sjb dt_free(dtp, dp->dtdo_ureltab); 782178479Sjb dt_free(dtp, dp->dtdo_xlmtab); 783178479Sjb 784178479Sjb dt_free(dtp, dp); 785178479Sjb} 786178479Sjb 787178479Sjb/* 788178479Sjb * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also 789178479Sjb * implements the behavior that an empty pattern matches any string. 790178479Sjb */ 791178479Sjbint 792178479Sjbdt_gmatch(const char *s, const char *p) 793178479Sjb{ 794178479Sjb return (p == NULL || *p == '\0' || gmatch(s, p)); 795178479Sjb} 796178479Sjb 797178479Sjbchar * 798178479Sjbdt_basename(char *str) 799178479Sjb{ 800178479Sjb char *last = strrchr(str, '/'); 801178479Sjb 802178479Sjb if (last == NULL) 803178479Sjb return (str); 804178479Sjb 805178479Sjb return (last + 1); 806178479Sjb} 807178479Sjb 808178479Sjb/* 809178479Sjb * dt_popc() is a fast implementation of population count. The algorithm is 810178479Sjb * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added. 811178479Sjb */ 812178479Sjbulong_t 813178479Sjbdt_popc(ulong_t x) 814178479Sjb{ 815223262Sbenl#if defined(_ILP32) 816178479Sjb x = x - ((x >> 1) & 0x55555555UL); 817178479Sjb x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL); 818178479Sjb x = (x + (x >> 4)) & 0x0F0F0F0FUL; 819178479Sjb x = x + (x >> 8); 820178479Sjb x = x + (x >> 16); 821178479Sjb return (x & 0x3F); 822223262Sbenl#elif defined(_LP64) 823178479Sjb x = x - ((x >> 1) & 0x5555555555555555ULL); 824178479Sjb x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); 825178479Sjb x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL; 826178479Sjb x = x + (x >> 8); 827178479Sjb x = x + (x >> 16); 828178479Sjb x = x + (x >> 32); 829178479Sjb return (x & 0x7F); 830223262Sbenl#else 831223262Sbenl# warning need td_popc() implementation 832178479Sjb#endif 833178479Sjb} 834178479Sjb 835178479Sjb/* 836178479Sjb * dt_popcb() is a bitmap-based version of population count that returns the 837178479Sjb * number of one bits in the specified bitmap 'bp' at bit positions below 'n'. 838178479Sjb */ 839178479Sjbulong_t 840178479Sjbdt_popcb(const ulong_t *bp, ulong_t n) 841178479Sjb{ 842178479Sjb ulong_t maxb = n & BT_ULMASK; 843178479Sjb ulong_t maxw = n >> BT_ULSHIFT; 844178479Sjb ulong_t w, popc = 0; 845178479Sjb 846178479Sjb if (n == 0) 847178479Sjb return (0); 848178479Sjb 849178479Sjb for (w = 0; w < maxw; w++) 850178479Sjb popc += dt_popc(bp[w]); 851178479Sjb 852178479Sjb return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1))); 853178479Sjb} 854178479Sjb 855178556Sjb#if defined(sun) 856178479Sjbstruct _rwlock; 857178479Sjbstruct _lwp_mutex; 858178479Sjb 859178479Sjbint 860178479Sjbdt_rw_read_held(pthread_rwlock_t *lock) 861178479Sjb{ 862178479Sjb extern int _rw_read_held(struct _rwlock *); 863178479Sjb return (_rw_read_held((struct _rwlock *)lock)); 864178479Sjb} 865178479Sjb 866178479Sjbint 867178479Sjbdt_rw_write_held(pthread_rwlock_t *lock) 868178479Sjb{ 869178479Sjb extern int _rw_write_held(struct _rwlock *); 870178479Sjb return (_rw_write_held((struct _rwlock *)lock)); 871178479Sjb} 872178556Sjb#endif 873178479Sjb 874178479Sjbint 875178479Sjbdt_mutex_held(pthread_mutex_t *lock) 876178479Sjb{ 877178556Sjb#if defined(sun) 878178479Sjb extern int _mutex_held(struct _lwp_mutex *); 879178479Sjb return (_mutex_held((struct _lwp_mutex *)lock)); 880178556Sjb#else 881178556Sjb return (1); 882178556Sjb#endif 883178479Sjb} 884178479Sjb 885178479Sjbstatic int 886178479Sjbdt_string2str(char *s, char *str, int nbytes) 887178479Sjb{ 888178479Sjb int len = strlen(s); 889178479Sjb 890178479Sjb if (nbytes == 0) { 891178479Sjb /* 892178479Sjb * Like snprintf(3C), we don't check the value of str if the 893178479Sjb * number of bytes is 0. 894178479Sjb */ 895178479Sjb return (len); 896178479Sjb } 897178479Sjb 898178479Sjb if (nbytes <= len) { 899178479Sjb (void) strncpy(str, s, nbytes - 1); 900178479Sjb /* 901178479Sjb * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee 902178479Sjb * that the string is null-terminated. 903178479Sjb */ 904178479Sjb str[nbytes - 1] = '\0'; 905178479Sjb } else { 906178479Sjb (void) strcpy(str, s); 907178479Sjb } 908178479Sjb 909178479Sjb return (len); 910178479Sjb} 911178479Sjb 912178479Sjbint 913178479Sjbdtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes) 914178479Sjb{ 915178479Sjb dtrace_syminfo_t dts; 916178479Sjb GElf_Sym sym; 917178479Sjb 918178479Sjb size_t n = 20; /* for 0x%llx\0 */ 919178479Sjb char *s; 920178479Sjb int err; 921178479Sjb 922178479Sjb if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0) 923178479Sjb n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */ 924178479Sjb 925178479Sjb s = alloca(n); 926178479Sjb 927178479Sjb if (err == 0 && addr != sym.st_value) { 928178479Sjb (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object, 929178479Sjb dts.dts_name, (u_longlong_t)addr - sym.st_value); 930178479Sjb } else if (err == 0) { 931178479Sjb (void) snprintf(s, n, "%s`%s", 932178479Sjb dts.dts_object, dts.dts_name); 933178479Sjb } else { 934178479Sjb /* 935178479Sjb * We'll repeat the lookup, but this time we'll specify a NULL 936178479Sjb * GElf_Sym -- indicating that we're only interested in the 937178479Sjb * containing module. 938178479Sjb */ 939178479Sjb if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) { 940178479Sjb (void) snprintf(s, n, "%s`0x%llx", dts.dts_object, 941178479Sjb (u_longlong_t)addr); 942178479Sjb } else { 943178479Sjb (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr); 944178479Sjb } 945178479Sjb } 946178479Sjb 947178479Sjb return (dt_string2str(s, str, nbytes)); 948178479Sjb} 949178479Sjb 950178479Sjbint 951178479Sjbdtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid, 952178479Sjb uint64_t addr, char *str, int nbytes) 953178479Sjb{ 954178479Sjb char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 955178479Sjb struct ps_prochandle *P = NULL; 956178479Sjb GElf_Sym sym; 957178479Sjb char *obj; 958178479Sjb 959178479Sjb if (pid != 0) 960178479Sjb P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 961178479Sjb 962178479Sjb if (P == NULL) { 963223262Sbenl (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr); 964178479Sjb return (dt_string2str(c, str, nbytes)); 965178479Sjb } 966178479Sjb 967178479Sjb dt_proc_lock(dtp, P); 968178479Sjb 969178479Sjb if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) { 970178479Sjb (void) Pobjname(P, addr, objname, sizeof (objname)); 971178479Sjb 972178479Sjb obj = dt_basename(objname); 973178479Sjb 974178479Sjb if (addr > sym.st_value) { 975178479Sjb (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj, 976178479Sjb name, (u_longlong_t)(addr - sym.st_value)); 977178479Sjb } else { 978178479Sjb (void) snprintf(c, sizeof (c), "%s`%s", obj, name); 979178479Sjb } 980178556Sjb } else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) { 981223262Sbenl (void) snprintf(c, sizeof (c), "%s`0x%jx", 982223262Sbenl dt_basename(objname), (uintmax_t)addr); 983178479Sjb } else { 984223262Sbenl (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr); 985178479Sjb } 986178479Sjb 987178479Sjb dt_proc_unlock(dtp, P); 988178479Sjb dt_proc_release(dtp, P); 989178479Sjb 990178479Sjb return (dt_string2str(c, str, nbytes)); 991178479Sjb} 992