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