1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25  * Copyright (c) 2011 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <sys/types.h>
  29 #include <sys/modctl.h>
  30 #include <sys/systeminfo.h>
  31 #include <sys/resource.h>
  32 
  33 #include <libelf.h>
  34 #include <strings.h>
  35 #include <alloca.h>
  36 #include <limits.h>
  37 #include <unistd.h>
  38 #include <stdlib.h>
  39 #include <stdio.h>
  40 #include <fcntl.h>
  41 #include <errno.h>
  42 #include <assert.h>
  43 
  44 #define _POSIX_PTHREAD_SEMANTICS
  45 #include <dirent.h>
  46 #undef  _POSIX_PTHREAD_SEMANTICS
  47 
  48 #include <dt_impl.h>
  49 #include <dt_program.h>
  50 #include <dt_module.h>
  51 #include <dt_printf.h>
  52 #include <dt_string.h>
  53 #include <dt_provider.h>
  54 
  55 /*
  56  * Stability and versioning definitions.  These #defines are used in the tables
  57  * of identifiers below to fill in the attribute and version fields associated
  58  * with each identifier.  The DT_ATTR_* macros are a convenience to permit more
  59  * concise declarations of common attributes such as Stable/Stable/Common.  The
  60  * DT_VERS_* macros declare the encoded integer values of all versions used so
  61  * far.  DT_VERS_LATEST must correspond to the latest version value among all
  62  * versions exported by the D compiler.  DT_VERS_STRING must be an ASCII string
  63  * that contains DT_VERS_LATEST within it along with any suffixes (e.g. Beta).
  64  * You must update DT_VERS_LATEST and DT_VERS_STRING when adding a new version,
  65  * and then add the new version to the _dtrace_versions[] array declared below.
  66  * Refer to the Solaris Dynamic Tracing Guide Stability and Versioning chapters
  67  * respectively for an explanation of these DTrace features and their values.
  68  *
  69  * NOTE: Although the DTrace versioning scheme supports the labeling and
  70  *       introduction of incompatible changes (e.g. dropping an interface in a
  71  *       major release), the libdtrace code does not currently support this.
  72  *       All versions are assumed to strictly inherit from one another.  If
  73  *       we ever need to provide divergent interfaces, this will need work.
  74  */
  75 #define DT_ATTR_STABCMN { DTRACE_STABILITY_STABLE, \
  76         DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }
  77 
  78 #define DT_ATTR_EVOLCMN { DTRACE_STABILITY_EVOLVING, \
  79         DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON \
  80 }
  81 
  82 /*
  83  * The version number should be increased for every customer visible release
  84  * of Solaris. The major number should be incremented when a fundamental
  85  * change has been made that would affect all consumers, and would reflect
  86  * sweeping changes to DTrace or the D language. The minor number should be
  87  * incremented when a change is introduced that could break scripts that had
  88  * previously worked; for example, adding a new built-in variable could break
  89  * a script which was already using that identifier. The micro number should
  90  * be changed when introducing functionality changes or major bug fixes that
  91  * do not affect backward compatibility -- this is merely to make capabilities
  92  * easily determined from the version number. Minor bugs do not require any
  93  * modification to the version number.
  94  */
  95 #define DT_VERS_1_0     DT_VERSION_NUMBER(1, 0, 0)
  96 #define DT_VERS_1_1     DT_VERSION_NUMBER(1, 1, 0)
  97 #define DT_VERS_1_2     DT_VERSION_NUMBER(1, 2, 0)
  98 #define DT_VERS_1_2_1   DT_VERSION_NUMBER(1, 2, 1)
  99 #define DT_VERS_1_2_2   DT_VERSION_NUMBER(1, 2, 2)
 100 #define DT_VERS_1_3     DT_VERSION_NUMBER(1, 3, 0)
 101 #define DT_VERS_1_4     DT_VERSION_NUMBER(1, 4, 0)
 102 #define DT_VERS_1_4_1   DT_VERSION_NUMBER(1, 4, 1)
 103 #define DT_VERS_1_5     DT_VERSION_NUMBER(1, 5, 0)
 104 #define DT_VERS_1_6     DT_VERSION_NUMBER(1, 6, 0)
 105 #define DT_VERS_1_6_1   DT_VERSION_NUMBER(1, 6, 1)
 106 #define DT_VERS_1_6_2   DT_VERSION_NUMBER(1, 6, 2)
 107 #define DT_VERS_1_6_3   DT_VERSION_NUMBER(1, 6, 3)
 108 #define DT_VERS_1_7     DT_VERSION_NUMBER(1, 7, 0)
 109 #define DT_VERS_1_7_1   DT_VERSION_NUMBER(1, 7, 1)
 110 #define DT_VERS_1_8     DT_VERSION_NUMBER(1, 8, 0)
 111 #define DT_VERS_1_8_1   DT_VERSION_NUMBER(1, 8, 1)
 112 #define DT_VERS_1_9     DT_VERSION_NUMBER(1, 9, 0)
 113 #define DT_VERS_1_10    DT_VERSION_NUMBER(1, 10, 0)
 114 #define DT_VERS_LATEST  DT_VERS_1_10
 115 #define DT_VERS_STRING  "Sun D 1.10"
 116 
 117 const dt_version_t _dtrace_versions[] = {
 118         DT_VERS_1_0,    /* D API 1.0.0 (PSARC 2001/466) Solaris 10 FCS */
 119         DT_VERS_1_1,    /* D API 1.1.0 Solaris Express 6/05 */
 120         DT_VERS_1_2,    /* D API 1.2.0 Solaris 10 Update 1 */
 121         DT_VERS_1_2_1,  /* D API 1.2.1 Solaris Express 4/06 */
 122         DT_VERS_1_2_2,  /* D API 1.2.2 Solaris Express 6/06 */
 123         DT_VERS_1_3,    /* D API 1.3 Solaris Express 10/06 */
 124         DT_VERS_1_4,    /* D API 1.4 Solaris Express 2/07 */
 125         DT_VERS_1_4_1,  /* D API 1.4.1 Solaris Express 4/07 */
 126         DT_VERS_1_5,    /* D API 1.5 Solaris Express 7/07 */
 127         DT_VERS_1_6,    /* D API 1.6 */
 128         DT_VERS_1_6_1,  /* D API 1.6.1 */
 129         DT_VERS_1_6_2,  /* D API 1.6.2 */
 130         DT_VERS_1_6_3,  /* D API 1.6.3 */
 131         DT_VERS_1_7,    /* D API 1.7 */
 132         DT_VERS_1_7_1,  /* D API 1.7.1 */
 133         DT_VERS_1_8,    /* D API 1.8 */
 134         DT_VERS_1_8_1,  /* D API 1.8.1 */
 135         DT_VERS_1_9,    /* D API 1.9 */
 136         DT_VERS_1_10,   /* D API 1.10 */
 137         0
 138 };
 139 
 140 /*
 141  * Table of global identifiers.  This is used to populate the global identifier
 142  * hash when a new dtrace client open occurs.  For more info see dt_ident.h.
 143  * The global identifiers that represent functions use the dt_idops_func ops
 144  * and specify the private data pointer as a prototype string which is parsed
 145  * when the identifier is first encountered.  These prototypes look like ANSI
 146  * C function prototypes except that the special symbol "@" can be used as a
 147  * wildcard to represent a single parameter of any type (i.e. any dt_node_t).
 148  * The standard "..." notation can also be used to represent varargs.  An empty
 149  * parameter list is taken to mean void (that is, no arguments are permitted).
 150  * A parameter enclosed in square brackets (e.g. "[int]") denotes an optional
 151  * argument.
 152  */
 153 static const dt_ident_t _dtrace_globals[] = {
 154 { "alloca", DT_IDENT_FUNC, 0, DIF_SUBR_ALLOCA, DT_ATTR_STABCMN, DT_VERS_1_0,
 155         &dt_idops_func, "void *(size_t)" },
 156 { "arg0", DT_IDENT_SCALAR, 0, DIF_VAR_ARG0, DT_ATTR_STABCMN, DT_VERS_1_0,
 157         &dt_idops_type, "int64_t" },
 158 { "arg1", DT_IDENT_SCALAR, 0, DIF_VAR_ARG1, DT_ATTR_STABCMN, DT_VERS_1_0,
 159         &dt_idops_type, "int64_t" },
 160 { "arg2", DT_IDENT_SCALAR, 0, DIF_VAR_ARG2, DT_ATTR_STABCMN, DT_VERS_1_0,
 161         &dt_idops_type, "int64_t" },
 162 { "arg3", DT_IDENT_SCALAR, 0, DIF_VAR_ARG3, DT_ATTR_STABCMN, DT_VERS_1_0,
 163         &dt_idops_type, "int64_t" },
 164 { "arg4", DT_IDENT_SCALAR, 0, DIF_VAR_ARG4, DT_ATTR_STABCMN, DT_VERS_1_0,
 165         &dt_idops_type, "int64_t" },
 166 { "arg5", DT_IDENT_SCALAR, 0, DIF_VAR_ARG5, DT_ATTR_STABCMN, DT_VERS_1_0,
 167         &dt_idops_type, "int64_t" },
 168 { "arg6", DT_IDENT_SCALAR, 0, DIF_VAR_ARG6, DT_ATTR_STABCMN, DT_VERS_1_0,
 169         &dt_idops_type, "int64_t" },
 170 { "arg7", DT_IDENT_SCALAR, 0, DIF_VAR_ARG7, DT_ATTR_STABCMN, DT_VERS_1_0,
 171         &dt_idops_type, "int64_t" },
 172 { "arg8", DT_IDENT_SCALAR, 0, DIF_VAR_ARG8, DT_ATTR_STABCMN, DT_VERS_1_0,
 173         &dt_idops_type, "int64_t" },
 174 { "arg9", DT_IDENT_SCALAR, 0, DIF_VAR_ARG9, DT_ATTR_STABCMN, DT_VERS_1_0,
 175         &dt_idops_type, "int64_t" },
 176 { "args", DT_IDENT_ARRAY, 0, DIF_VAR_ARGS, DT_ATTR_STABCMN, DT_VERS_1_0,
 177         &dt_idops_args, NULL },
 178 { "avg", DT_IDENT_AGGFUNC, 0, DTRACEAGG_AVG, DT_ATTR_STABCMN, DT_VERS_1_0,
 179         &dt_idops_func, "void(@)" },
 180 { "basename", DT_IDENT_FUNC, 0, DIF_SUBR_BASENAME, DT_ATTR_STABCMN, DT_VERS_1_0,
 181         &dt_idops_func, "string(const char *)" },
 182 { "bcopy", DT_IDENT_FUNC, 0, DIF_SUBR_BCOPY, DT_ATTR_STABCMN, DT_VERS_1_0,
 183         &dt_idops_func, "void(void *, void *, size_t)" },
 184 { "breakpoint", DT_IDENT_ACTFUNC, 0, DT_ACT_BREAKPOINT,
 185         DT_ATTR_STABCMN, DT_VERS_1_0,
 186         &dt_idops_func, "void()" },
 187 { "caller", DT_IDENT_SCALAR, 0, DIF_VAR_CALLER, DT_ATTR_STABCMN, DT_VERS_1_0,
 188         &dt_idops_type, "uintptr_t" },
 189 { "chill", DT_IDENT_ACTFUNC, 0, DT_ACT_CHILL, DT_ATTR_STABCMN, DT_VERS_1_0,
 190         &dt_idops_func, "void(int)" },
 191 { "cleanpath", DT_IDENT_FUNC, 0, DIF_SUBR_CLEANPATH, DT_ATTR_STABCMN,
 192         DT_VERS_1_0, &dt_idops_func, "string(const char *)" },
 193 { "clear", DT_IDENT_ACTFUNC, 0, DT_ACT_CLEAR, DT_ATTR_STABCMN, DT_VERS_1_0,
 194         &dt_idops_func, "void(...)" },
 195 { "commit", DT_IDENT_ACTFUNC, 0, DT_ACT_COMMIT, DT_ATTR_STABCMN, DT_VERS_1_0,
 196         &dt_idops_func, "void(int)" },
 197 { "copyin", DT_IDENT_FUNC, 0, DIF_SUBR_COPYIN, DT_ATTR_STABCMN, DT_VERS_1_0,
 198         &dt_idops_func, "void *(uintptr_t, size_t)" },
 199 { "copyinstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINSTR,
 200         DT_ATTR_STABCMN, DT_VERS_1_0,
 201         &dt_idops_func, "string(uintptr_t, [size_t])" },
 202 { "copyinto", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINTO, DT_ATTR_STABCMN,
 203         DT_VERS_1_0, &dt_idops_func, "void(uintptr_t, size_t, void *)" },
 204 { "copyout", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUT, DT_ATTR_STABCMN, DT_VERS_1_0,
 205         &dt_idops_func, "void(void *, uintptr_t, size_t)" },
 206 { "copyoutstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUTSTR,
 207         DT_ATTR_STABCMN, DT_VERS_1_0,
 208         &dt_idops_func, "void(char *, uintptr_t, size_t)" },
 209 { "count", DT_IDENT_AGGFUNC, 0, DTRACEAGG_COUNT, DT_ATTR_STABCMN, DT_VERS_1_0,
 210         &dt_idops_func, "void()" },
 211 { "curthread", DT_IDENT_SCALAR, 0, DIF_VAR_CURTHREAD,
 212         { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_PRIVATE,
 213         DTRACE_CLASS_COMMON }, DT_VERS_1_0,
 214         &dt_idops_type, "genunix`kthread_t *" },
 215 { "ddi_pathname", DT_IDENT_FUNC, 0, DIF_SUBR_DDI_PATHNAME,
 216         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 217         &dt_idops_func, "string(void *, int64_t)" },
 218 { "denormalize", DT_IDENT_ACTFUNC, 0, DT_ACT_DENORMALIZE, DT_ATTR_STABCMN,
 219         DT_VERS_1_0, &dt_idops_func, "void(...)" },
 220 { "dirname", DT_IDENT_FUNC, 0, DIF_SUBR_DIRNAME, DT_ATTR_STABCMN, DT_VERS_1_0,
 221         &dt_idops_func, "string(const char *)" },
 222 { "discard", DT_IDENT_ACTFUNC, 0, DT_ACT_DISCARD, DT_ATTR_STABCMN, DT_VERS_1_0,
 223         &dt_idops_func, "void(int)" },
 224 { "epid", DT_IDENT_SCALAR, 0, DIF_VAR_EPID, DT_ATTR_STABCMN, DT_VERS_1_0,
 225         &dt_idops_type, "uint_t" },
 226 { "errno", DT_IDENT_SCALAR, 0, DIF_VAR_ERRNO, DT_ATTR_STABCMN, DT_VERS_1_0,
 227         &dt_idops_type, "int" },
 228 { "execname", DT_IDENT_SCALAR, 0, DIF_VAR_EXECNAME,
 229         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 230 { "exit", DT_IDENT_ACTFUNC, 0, DT_ACT_EXIT, DT_ATTR_STABCMN, DT_VERS_1_0,
 231         &dt_idops_func, "void(int)" },
 232 { "freopen", DT_IDENT_ACTFUNC, 0, DT_ACT_FREOPEN, DT_ATTR_STABCMN,
 233         DT_VERS_1_1, &dt_idops_func, "void(@, ...)" },
 234 { "ftruncate", DT_IDENT_ACTFUNC, 0, DT_ACT_FTRUNCATE, DT_ATTR_STABCMN,
 235         DT_VERS_1_0, &dt_idops_func, "void()" },
 236 { "func", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
 237         DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
 238 { "getmajor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMAJOR,
 239         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 240         &dt_idops_func, "genunix`major_t(genunix`dev_t)" },
 241 { "getminor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMINOR,
 242         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 243         &dt_idops_func, "genunix`minor_t(genunix`dev_t)" },
 244 { "htonl", DT_IDENT_FUNC, 0, DIF_SUBR_HTONL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 245         &dt_idops_func, "uint32_t(uint32_t)" },
 246 { "htonll", DT_IDENT_FUNC, 0, DIF_SUBR_HTONLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 247         &dt_idops_func, "uint64_t(uint64_t)" },
 248 { "htons", DT_IDENT_FUNC, 0, DIF_SUBR_HTONS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 249         &dt_idops_func, "uint16_t(uint16_t)" },
 250 { "getf", DT_IDENT_FUNC, 0, DIF_SUBR_GETF, DT_ATTR_STABCMN, DT_VERS_1_10,
 251         &dt_idops_func, "file_t *(int)" },
 252 { "gid", DT_IDENT_SCALAR, 0, DIF_VAR_GID, DT_ATTR_STABCMN, DT_VERS_1_0,
 253         &dt_idops_type, "gid_t" },
 254 { "id", DT_IDENT_SCALAR, 0, DIF_VAR_ID, DT_ATTR_STABCMN, DT_VERS_1_0,
 255         &dt_idops_type, "uint_t" },
 256 { "index", DT_IDENT_FUNC, 0, DIF_SUBR_INDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
 257         &dt_idops_func, "int(const char *, const char *, [int])" },
 258 { "inet_ntoa", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA, DT_ATTR_STABCMN,
 259         DT_VERS_1_5, &dt_idops_func, "string(ipaddr_t *)" },
 260 { "inet_ntoa6", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA6, DT_ATTR_STABCMN,
 261         DT_VERS_1_5, &dt_idops_func, "string(in6_addr_t *)" },
 262 { "inet_ntop", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOP, DT_ATTR_STABCMN,
 263         DT_VERS_1_5, &dt_idops_func, "string(int, void *)" },
 264 { "ipl", DT_IDENT_SCALAR, 0, DIF_VAR_IPL, DT_ATTR_STABCMN, DT_VERS_1_0,
 265         &dt_idops_type, "uint_t" },
 266 { "jstack", DT_IDENT_ACTFUNC, 0, DT_ACT_JSTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
 267         &dt_idops_func, "stack(...)" },
 268 { "lltostr", DT_IDENT_FUNC, 0, DIF_SUBR_LLTOSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
 269         &dt_idops_func, "string(int64_t, [int])" },
 270 { "llquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LLQUANTIZE, DT_ATTR_STABCMN,
 271         DT_VERS_1_7, &dt_idops_func,
 272         "void(@, int32_t, int32_t, int32_t, int32_t, ...)" },
 273 { "lquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LQUANTIZE,
 274         DT_ATTR_STABCMN, DT_VERS_1_0,
 275         &dt_idops_func, "void(@, int32_t, int32_t, ...)" },
 276 { "max", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MAX, DT_ATTR_STABCMN, DT_VERS_1_0,
 277         &dt_idops_func, "void(@)" },
 278 { "min", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MIN, DT_ATTR_STABCMN, DT_VERS_1_0,
 279         &dt_idops_func, "void(@)" },
 280 { "mod", DT_IDENT_ACTFUNC, 0, DT_ACT_MOD, DT_ATTR_STABCMN,
 281         DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
 282 { "msgdsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGDSIZE,
 283         DT_ATTR_STABCMN, DT_VERS_1_0,
 284         &dt_idops_func, "size_t(mblk_t *)" },
 285 { "msgsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGSIZE,
 286         DT_ATTR_STABCMN, DT_VERS_1_0,
 287         &dt_idops_func, "size_t(mblk_t *)" },
 288 { "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
 289         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 290         &dt_idops_func, "int(genunix`kmutex_t *)" },
 291 { "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
 292         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 293         &dt_idops_func, "genunix`kthread_t *(genunix`kmutex_t *)" },
 294 { "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
 295         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 296         &dt_idops_func, "int(genunix`kmutex_t *)" },
 297 { "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
 298         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 299         &dt_idops_func, "int(genunix`kmutex_t *)" },
 300 { "ntohl", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 301         &dt_idops_func, "uint32_t(uint32_t)" },
 302 { "ntohll", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 303         &dt_idops_func, "uint64_t(uint64_t)" },
 304 { "ntohs", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
 305         &dt_idops_func, "uint16_t(uint16_t)" },
 306 { "normalize", DT_IDENT_ACTFUNC, 0, DT_ACT_NORMALIZE, DT_ATTR_STABCMN,
 307         DT_VERS_1_0, &dt_idops_func, "void(...)" },
 308 { "panic", DT_IDENT_ACTFUNC, 0, DT_ACT_PANIC, DT_ATTR_STABCMN, DT_VERS_1_0,
 309         &dt_idops_func, "void()" },
 310 { "pid", DT_IDENT_SCALAR, 0, DIF_VAR_PID, DT_ATTR_STABCMN, DT_VERS_1_0,
 311         &dt_idops_type, "pid_t" },
 312 { "ppid", DT_IDENT_SCALAR, 0, DIF_VAR_PPID, DT_ATTR_STABCMN, DT_VERS_1_0,
 313         &dt_idops_type, "pid_t" },
 314 { "print", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINT, DT_ATTR_STABCMN, DT_VERS_1_9,
 315         &dt_idops_func, "void(@)" },
 316 { "printa", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTA, DT_ATTR_STABCMN, DT_VERS_1_0,
 317         &dt_idops_func, "void(@, ...)" },
 318 { "printf", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTF, DT_ATTR_STABCMN, DT_VERS_1_0,
 319         &dt_idops_func, "void(@, ...)" },
 320 { "probefunc", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEFUNC,
 321         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 322 { "probemod", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEMOD,
 323         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 324 { "probename", DT_IDENT_SCALAR, 0, DIF_VAR_PROBENAME,
 325         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 326 { "probeprov", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEPROV,
 327         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 328 { "progenyof", DT_IDENT_FUNC, 0, DIF_SUBR_PROGENYOF,
 329         DT_ATTR_STABCMN, DT_VERS_1_0,
 330         &dt_idops_func, "int(pid_t)" },
 331 { "quantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_QUANTIZE,
 332         DT_ATTR_STABCMN, DT_VERS_1_0,
 333         &dt_idops_func, "void(@, ...)" },
 334 { "raise", DT_IDENT_ACTFUNC, 0, DT_ACT_RAISE, DT_ATTR_STABCMN, DT_VERS_1_0,
 335         &dt_idops_func, "void(int)" },
 336 { "rand", DT_IDENT_FUNC, 0, DIF_SUBR_RAND, DT_ATTR_STABCMN, DT_VERS_1_0,
 337         &dt_idops_func, "int()" },
 338 { "rindex", DT_IDENT_FUNC, 0, DIF_SUBR_RINDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
 339         &dt_idops_func, "int(const char *, const char *, [int])" },
 340 { "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
 341         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 342         &dt_idops_func, "int(genunix`krwlock_t *)" },
 343 { "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
 344         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 345         &dt_idops_func, "int(genunix`krwlock_t *)" },
 346 { "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
 347         DT_ATTR_EVOLCMN, DT_VERS_1_0,
 348         &dt_idops_func, "int(genunix`krwlock_t *)" },
 349 { "self", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
 350         &dt_idops_type, "void" },
 351 { "setopt", DT_IDENT_ACTFUNC, 0, DT_ACT_SETOPT, DT_ATTR_STABCMN,
 352         DT_VERS_1_2, &dt_idops_func, "void(const char *, [const char *])" },
 353 { "speculate", DT_IDENT_ACTFUNC, 0, DT_ACT_SPECULATE,
 354         DT_ATTR_STABCMN, DT_VERS_1_0,
 355         &dt_idops_func, "void(int)" },
 356 { "speculation", DT_IDENT_FUNC, 0, DIF_SUBR_SPECULATION,
 357         DT_ATTR_STABCMN, DT_VERS_1_0,
 358         &dt_idops_func, "int()" },
 359 { "stack", DT_IDENT_ACTFUNC, 0, DT_ACT_STACK, DT_ATTR_STABCMN, DT_VERS_1_0,
 360         &dt_idops_func, "stack(...)" },
 361 { "stackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_STACKDEPTH,
 362         DT_ATTR_STABCMN, DT_VERS_1_0,
 363         &dt_idops_type, "uint32_t" },
 364 { "stddev", DT_IDENT_AGGFUNC, 0, DTRACEAGG_STDDEV, DT_ATTR_STABCMN,
 365         DT_VERS_1_6, &dt_idops_func, "void(@)" },
 366 { "stop", DT_IDENT_ACTFUNC, 0, DT_ACT_STOP, DT_ATTR_STABCMN, DT_VERS_1_0,
 367         &dt_idops_func, "void()" },
 368 { "strchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
 369         &dt_idops_func, "string(const char *, char)" },
 370 { "strlen", DT_IDENT_FUNC, 0, DIF_SUBR_STRLEN, DT_ATTR_STABCMN, DT_VERS_1_0,
 371         &dt_idops_func, "size_t(const char *)" },
 372 { "strjoin", DT_IDENT_FUNC, 0, DIF_SUBR_STRJOIN, DT_ATTR_STABCMN, DT_VERS_1_0,
 373         &dt_idops_func, "string(const char *, const char *)" },
 374 { "strrchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
 375         &dt_idops_func, "string(const char *, char)" },
 376 { "strstr", DT_IDENT_FUNC, 0, DIF_SUBR_STRSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
 377         &dt_idops_func, "string(const char *, const char *)" },
 378 { "strtok", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOK, DT_ATTR_STABCMN, DT_VERS_1_1,
 379         &dt_idops_func, "string(const char *, const char *)" },
 380 { "substr", DT_IDENT_FUNC, 0, DIF_SUBR_SUBSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
 381         &dt_idops_func, "string(const char *, int, [int])" },
 382 { "sum", DT_IDENT_AGGFUNC, 0, DTRACEAGG_SUM, DT_ATTR_STABCMN, DT_VERS_1_0,
 383         &dt_idops_func, "void(@)" },
 384 { "sym", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
 385         DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
 386 { "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
 387         &dt_idops_func, "void(@, ...)" },
 388 { "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
 389         &dt_idops_type, "void" },
 390 { "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
 391         &dt_idops_type, "id_t" },
 392 { "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
 393         DT_ATTR_STABCMN, DT_VERS_1_0,
 394         &dt_idops_type, "uint64_t" },
 395 { "tolower", DT_IDENT_FUNC, 0, DIF_SUBR_TOLOWER, DT_ATTR_STABCMN, DT_VERS_1_8,
 396         &dt_idops_func, "string(const char *)" },
 397 { "toupper", DT_IDENT_FUNC, 0, DIF_SUBR_TOUPPER, DT_ATTR_STABCMN, DT_VERS_1_8,
 398         &dt_idops_func, "string(const char *)" },
 399 { "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
 400         &dt_idops_func, "void(@)" },
 401 { "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
 402         DT_ATTR_STABCMN, DT_VERS_1_0,
 403         &dt_idops_func, "void(@, size_t, ...)" },
 404 { "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
 405         DT_VERS_1_0, &dt_idops_func, "void(...)" },
 406 { "uaddr", DT_IDENT_ACTFUNC, 0, DT_ACT_UADDR, DT_ATTR_STABCMN,
 407         DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
 408 { "ucaller", DT_IDENT_SCALAR, 0, DIF_VAR_UCALLER, DT_ATTR_STABCMN,
 409         DT_VERS_1_2, &dt_idops_type, "uint64_t" },
 410 { "ufunc", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
 411         DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
 412 { "uid", DT_IDENT_SCALAR, 0, DIF_VAR_UID, DT_ATTR_STABCMN, DT_VERS_1_0,
 413         &dt_idops_type, "uid_t" },
 414 { "umod", DT_IDENT_ACTFUNC, 0, DT_ACT_UMOD, DT_ATTR_STABCMN,
 415         DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
 416 { "uregs", DT_IDENT_ARRAY, 0, DIF_VAR_UREGS, DT_ATTR_STABCMN, DT_VERS_1_0,
 417         &dt_idops_regs, NULL },
 418 { "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
 419         &dt_idops_func, "stack(...)" },
 420 { "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
 421         DT_ATTR_STABCMN, DT_VERS_1_2,
 422         &dt_idops_type, "uint32_t" },
 423 { "usym", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
 424         DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
 425 { "vmregs", DT_IDENT_ARRAY, 0, DIF_VAR_VMREGS, DT_ATTR_STABCMN, DT_VERS_1_7,
 426         &dt_idops_regs, NULL },
 427 { "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
 428         DT_ATTR_STABCMN, DT_VERS_1_0,
 429         &dt_idops_type, "uint64_t" },
 430 { "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
 431         DT_ATTR_STABCMN, DT_VERS_1_0,
 432         &dt_idops_type, "int64_t" },
 433 { "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
 434         DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
 435 { NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
 436 };
 437 
 438 /*
 439  * Tables of ILP32 intrinsic integer and floating-point type templates to use
 440  * to populate the dynamic "C" CTF type container.
 441  */
 442 static const dt_intrinsic_t _dtrace_intrinsics_32[] = {
 443 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
 444 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 445 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
 446 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 447 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
 448 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 449 { "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 450 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 451 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 452 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
 453 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 454 { "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 455 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 456 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 457 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
 458 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
 459 { "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
 460 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
 461 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
 462 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
 463 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
 464 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
 465 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
 466 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
 467 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
 468 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
 469 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
 470 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
 471 { NULL, { 0, 0, 0 }, 0 }
 472 };
 473 
 474 /*
 475  * Tables of LP64 intrinsic integer and floating-point type templates to use
 476  * to populate the dynamic "C" CTF type container.
 477  */
 478 static const dt_intrinsic_t _dtrace_intrinsics_64[] = {
 479 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
 480 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 481 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
 482 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 483 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
 484 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 485 { "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 486 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 487 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 488 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
 489 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
 490 { "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 491 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
 492 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
 493 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
 494 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
 495 { "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
 496 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
 497 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
 498 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
 499 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
 500 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
 501 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
 502 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
 503 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
 504 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
 505 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
 506 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
 507 { NULL, { 0, 0, 0 }, 0 }
 508 };
 509 
 510 /*
 511  * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
 512  * These aliases ensure that D definitions can use typical <sys/types.h> names.
 513  */
 514 static const dt_typedef_t _dtrace_typedefs_32[] = {
 515 { "char", "int8_t" },
 516 { "short", "int16_t" },
 517 { "int", "int32_t" },
 518 { "long long", "int64_t" },
 519 { "int", "intptr_t" },
 520 { "int", "ssize_t" },
 521 { "unsigned char", "uint8_t" },
 522 { "unsigned short", "uint16_t" },
 523 { "unsigned", "uint32_t" },
 524 { "unsigned long long", "uint64_t" },
 525 { "unsigned char", "uchar_t" },
 526 { "unsigned short", "ushort_t" },
 527 { "unsigned", "uint_t" },
 528 { "unsigned long", "ulong_t" },
 529 { "unsigned long long", "u_longlong_t" },
 530 { "int", "ptrdiff_t" },
 531 { "unsigned", "uintptr_t" },
 532 { "unsigned", "size_t" },
 533 { "long", "id_t" },
 534 { "long", "pid_t" },
 535 { NULL, NULL }
 536 };
 537 
 538 /*
 539  * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
 540  * These aliases ensure that D definitions can use typical <sys/types.h> names.
 541  */
 542 static const dt_typedef_t _dtrace_typedefs_64[] = {
 543 { "char", "int8_t" },
 544 { "short", "int16_t" },
 545 { "int", "int32_t" },
 546 { "long", "int64_t" },
 547 { "long", "intptr_t" },
 548 { "long", "ssize_t" },
 549 { "unsigned char", "uint8_t" },
 550 { "unsigned short", "uint16_t" },
 551 { "unsigned", "uint32_t" },
 552 { "unsigned long", "uint64_t" },
 553 { "unsigned char", "uchar_t" },
 554 { "unsigned short", "ushort_t" },
 555 { "unsigned", "uint_t" },
 556 { "unsigned long", "ulong_t" },
 557 { "unsigned long long", "u_longlong_t" },
 558 { "long", "ptrdiff_t" },
 559 { "unsigned long", "uintptr_t" },
 560 { "unsigned long", "size_t" },
 561 { "int", "id_t" },
 562 { "int", "pid_t" },
 563 { NULL, NULL }
 564 };
 565 
 566 /*
 567  * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
 568  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
 569  */
 570 static const dt_intdesc_t _dtrace_ints_32[] = {
 571 { "int", NULL, CTF_ERR, 0x7fffffffULL },
 572 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
 573 { "long", NULL, CTF_ERR, 0x7fffffffULL },
 574 { "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
 575 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
 576 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
 577 };
 578 
 579 /*
 580  * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
 581  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
 582  */
 583 static const dt_intdesc_t _dtrace_ints_64[] = {
 584 { "int", NULL, CTF_ERR, 0x7fffffffULL },
 585 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
 586 { "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
 587 { "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
 588 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
 589 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
 590 };
 591 
 592 /*
 593  * Table of macro variable templates used to populate the macro identifier hash
 594  * when a new dtrace client open occurs.  Values are set by dtrace_update().
 595  */
 596 static const dt_ident_t _dtrace_macros[] = {
 597 { "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 598 { "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 599 { "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 600 { "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 601 { "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 602 { "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 603 { "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 604 { "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 605 { "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 606 { "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 607 { "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
 608 { NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
 609 };
 610 
 611 /*
 612  * Hard-wired definition string to be compiled and cached every time a new
 613  * DTrace library handle is initialized.  This string should only be used to
 614  * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
 615  */
 616 static const char _dtrace_hardwire[] = "\
 617 inline long NULL = 0; \n\
 618 #pragma D binding \"1.0\" NULL\n\
 619 ";
 620 
 621 /*
 622  * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
 623  * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
 624  * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
 625  * relying on the fact that when running dtrace(1M), isaexec will invoke the
 626  * binary with the same bitness as the kernel, which is what we want by default
 627  * when generating our DIF.  The user can override the choice using oflags.
 628  */
 629 static const dtrace_conf_t _dtrace_conf = {
 630         DIF_VERSION,            /* dtc_difversion */
 631         DIF_DIR_NREGS,          /* dtc_difintregs */
 632         DIF_DTR_NREGS,          /* dtc_diftupregs */
 633         CTF_MODEL_NATIVE        /* dtc_ctfmodel */
 634 };
 635 
 636 const dtrace_attribute_t _dtrace_maxattr = {
 637         DTRACE_STABILITY_MAX,
 638         DTRACE_STABILITY_MAX,
 639         DTRACE_CLASS_MAX
 640 };
 641 
 642 const dtrace_attribute_t _dtrace_defattr = {
 643         DTRACE_STABILITY_STABLE,
 644         DTRACE_STABILITY_STABLE,
 645         DTRACE_CLASS_COMMON
 646 };
 647 
 648 const dtrace_attribute_t _dtrace_symattr = {
 649         DTRACE_STABILITY_PRIVATE,
 650         DTRACE_STABILITY_PRIVATE,
 651         DTRACE_CLASS_UNKNOWN
 652 };
 653 
 654 const dtrace_attribute_t _dtrace_typattr = {
 655         DTRACE_STABILITY_PRIVATE,
 656         DTRACE_STABILITY_PRIVATE,
 657         DTRACE_CLASS_UNKNOWN
 658 };
 659 
 660 const dtrace_attribute_t _dtrace_prvattr = {
 661         DTRACE_STABILITY_PRIVATE,
 662         DTRACE_STABILITY_PRIVATE,
 663         DTRACE_CLASS_UNKNOWN
 664 };
 665 
 666 const dtrace_pattr_t _dtrace_prvdesc = {
 667 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 668 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 669 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 670 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 671 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
 672 };
 673 
 674 const char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
 675 const char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
 676 
 677 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
 678 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
 679 
 680 int _dtrace_strbuckets = 211;   /* default number of hash buckets (prime) */
 681 int _dtrace_intbuckets = 256;   /* default number of integer buckets (Pof2) */
 682 uint_t _dtrace_strsize = 256;   /* default size of string intrinsic type */
 683 uint_t _dtrace_stkindent = 14;  /* default whitespace indent for stack/ustack */
 684 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
 685 uint_t _dtrace_pidlrulim = 8;   /* default number of pid handles to cache */
 686 size_t _dtrace_bufsize = 512;   /* default dt_buf_create() size */
 687 int _dtrace_argmax = 32;        /* default maximum number of probe arguments */
 688 
 689 int _dtrace_debug = 0;          /* debug messages enabled (off) */
 690 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
 691 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
 692 
 693 typedef struct dt_fdlist {
 694         int *df_fds;            /* array of provider driver file descriptors */
 695         uint_t df_ents;         /* number of valid elements in df_fds[] */
 696         uint_t df_size;         /* size of df_fds[] */
 697 } dt_fdlist_t;
 698 
 699 #pragma init(_dtrace_init)
 700 void
 701 _dtrace_init(void)
 702 {
 703         _dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
 704 
 705         for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
 706                 if (rd_init(_dtrace_rdvers) == RD_OK)
 707                         break;
 708         }
 709 }
 710 
 711 static dtrace_hdl_t *
 712 set_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
 713 {
 714         if (dtp != NULL)
 715                 dtrace_close(dtp);
 716         if (errp != NULL)
 717                 *errp = err;
 718         return (NULL);
 719 }
 720 
 721 static void
 722 dt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
 723 {
 724         dt_provmod_t *prov;
 725         char path[PATH_MAX];
 726         struct dirent *dp, *ep;
 727         DIR *dirp;
 728         int fd;
 729 
 730         if ((dirp = opendir(_dtrace_provdir)) == NULL)
 731                 return; /* failed to open directory; just skip it */
 732 
 733         ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
 734         bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
 735 
 736         while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
 737                 if (dp->d_name[0] == '.')
 738                         continue; /* skip "." and ".." */
 739 
 740                 if (dfp->df_ents == dfp->df_size) {
 741                         uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
 742                         int *fds = realloc(dfp->df_fds, size * sizeof (int));
 743 
 744                         if (fds == NULL)
 745                                 break; /* skip the rest of this directory */
 746 
 747                         dfp->df_fds = fds;
 748                         dfp->df_size = size;
 749                 }
 750 
 751                 (void) snprintf(path, sizeof (path), "%s/%s",
 752                     _dtrace_provdir, dp->d_name);
 753 
 754                 if ((fd = open(path, O_RDONLY)) == -1)
 755                         continue; /* failed to open driver; just skip it */
 756 
 757                 if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
 758                     (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
 759                         free(prov);
 760                         (void) close(fd);
 761                         break;
 762                 }
 763 
 764                 (void) strcpy(prov->dp_name, dp->d_name);
 765                 prov->dp_next = *provmod;
 766                 *provmod = prov;
 767 
 768                 dt_dprintf("opened provider %s\n", dp->d_name);
 769                 dfp->df_fds[dfp->df_ents++] = fd;
 770         }
 771 
 772         (void) closedir(dirp);
 773 }
 774 
 775 static void
 776 dt_provmod_destroy(dt_provmod_t **provmod)
 777 {
 778         dt_provmod_t *next, *current;
 779 
 780         for (current = *provmod; current != NULL; current = next) {
 781                 next = current->dp_next;
 782                 free(current->dp_name);
 783                 free(current);
 784         }
 785 
 786         *provmod = NULL;
 787 }
 788 
 789 static const char *
 790 dt_get_sysinfo(int cmd, char *buf, size_t len)
 791 {
 792         ssize_t rv = sysinfo(cmd, buf, len);
 793         char *p = buf;
 794 
 795         if (rv < 0 || rv > len)
 796                 (void) snprintf(buf, len, "%s", "Unknown");
 797 
 798         while ((p = strchr(p, '.')) != NULL)
 799                 *p++ = '_';
 800 
 801         return (buf);
 802 }
 803 
 804 static dtrace_hdl_t *
 805 dt_vopen(int version, int flags, int *errp,
 806     const dtrace_vector_t *vector, void *arg)
 807 {
 808         dtrace_hdl_t *dtp = NULL;
 809         int dtfd = -1, ftfd = -1, fterr = 0;
 810         dtrace_prog_t *pgp;
 811         dt_module_t *dmp;
 812         dt_provmod_t *provmod = NULL;
 813         int i, err;
 814         struct rlimit rl;
 815 
 816         const dt_intrinsic_t *dinp;
 817         const dt_typedef_t *dtyp;
 818         const dt_ident_t *idp;
 819 
 820         dtrace_typeinfo_t dtt;
 821         ctf_funcinfo_t ctc;
 822         ctf_arinfo_t ctr;
 823 
 824         dt_fdlist_t df = { NULL, 0, 0 };
 825 
 826         char isadef[32], utsdef[32];
 827         char s1[64], s2[64];
 828 
 829         if (version <= 0)
 830                 return (set_open_errno(dtp, errp, EINVAL));
 831 
 832         if (version > DTRACE_VERSION)
 833                 return (set_open_errno(dtp, errp, EDT_VERSION));
 834 
 835         if (version < DTRACE_VERSION) {
 836                 /*
 837                  * Currently, increasing the library version number is used to
 838                  * denote a binary incompatible change.  That is, a consumer
 839                  * of the library cannot run on a version of the library with
 840                  * a higher DTRACE_VERSION number than the consumer compiled
 841                  * against.  Once the library API has been committed to,
 842                  * backwards binary compatibility will be required; at that
 843                  * time, this check should change to return EDT_OVERSION only
 844                  * if the specified version number is less than the version
 845                  * number at the time of interface commitment.
 846                  */
 847                 return (set_open_errno(dtp, errp, EDT_OVERSION));
 848         }
 849 
 850         if (flags & ~DTRACE_O_MASK)
 851                 return (set_open_errno(dtp, errp, EINVAL));
 852 
 853         if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
 854                 return (set_open_errno(dtp, errp, EINVAL));
 855 
 856         if (vector == NULL && arg != NULL)
 857                 return (set_open_errno(dtp, errp, EINVAL));
 858 
 859         if (elf_version(EV_CURRENT) == EV_NONE)
 860                 return (set_open_errno(dtp, errp, EDT_ELFVERSION));
 861 
 862         if (vector != NULL || (flags & DTRACE_O_NODEV))
 863                 goto alloc; /* do not attempt to open dtrace device */
 864 
 865         /*
 866          * Before we get going, crank our limit on file descriptors up to the
 867          * hard limit.  This is to allow for the fact that libproc keeps file
 868          * descriptors to objects open for the lifetime of the proc handle;
 869          * without raising our hard limit, we would have an acceptably small
 870          * bound on the number of processes that we could concurrently
 871          * instrument with the pid provider.
 872          */
 873         if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
 874                 rl.rlim_cur = rl.rlim_max;
 875                 (void) setrlimit(RLIMIT_NOFILE, &rl);
 876         }
 877 
 878         /*
 879          * Get the device path of each of the providers.  We hold them open
 880          * in the df.df_fds list until we open the DTrace driver itself,
 881          * allowing us to see all of the probes provided on this system.  Once
 882          * we have the DTrace driver open, we can safely close all the providers
 883          * now that they have registered with the framework.
 884          */
 885         dt_provmod_open(&provmod, &df);
 886 
 887         dtfd = open("/dev/dtrace/dtrace", O_RDWR);
 888         err = errno; /* save errno from opening dtfd */
 889 
 890         ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
 891         fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
 892 
 893         while (df.df_ents-- != 0)
 894                 (void) close(df.df_fds[df.df_ents]);
 895 
 896         free(df.df_fds);
 897 
 898         /*
 899          * If we failed to open the dtrace device, fail dtrace_open().
 900          * We convert some kernel errnos to custom libdtrace errnos to
 901          * improve the resulting message from the usual strerror().
 902          */
 903         if (dtfd == -1) {
 904                 dt_provmod_destroy(&provmod);
 905                 switch (err) {
 906                 case ENOENT:
 907                         err = EDT_NOENT;
 908                         break;
 909                 case EBUSY:
 910                         err = EDT_BUSY;
 911                         break;
 912                 case EACCES:
 913                         err = EDT_ACCESS;
 914                         break;
 915                 }
 916                 return (set_open_errno(dtp, errp, err));
 917         }
 918 
 919         (void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
 920         (void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
 921 
 922 alloc:
 923         if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
 924                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 925 
 926         bzero(dtp, sizeof (dtrace_hdl_t));
 927         dtp->dt_oflags = flags;
 928         dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
 929         dtp->dt_linkmode = DT_LINK_KERNEL;
 930         dtp->dt_linktype = DT_LTYP_ELF;
 931         dtp->dt_xlatemode = DT_XL_STATIC;
 932         dtp->dt_stdcmode = DT_STDC_XA;
 933         dtp->dt_version = version;
 934         dtp->dt_fd = dtfd;
 935         dtp->dt_ftfd = ftfd;
 936         dtp->dt_fterr = fterr;
 937         dtp->dt_cdefs_fd = -1;
 938         dtp->dt_ddefs_fd = -1;
 939         dtp->dt_stdout_fd = -1;
 940         dtp->dt_modbuckets = _dtrace_strbuckets;
 941         dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
 942         dtp->dt_provbuckets = _dtrace_strbuckets;
 943         dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
 944         dt_proc_hash_create(dtp);
 945         dtp->dt_vmax = DT_VERS_LATEST;
 946         dtp->dt_cpp_path = strdup(_dtrace_defcpp);
 947         dtp->dt_cpp_argv = malloc(sizeof (char *));
 948         dtp->dt_cpp_argc = 1;
 949         dtp->dt_cpp_args = 1;
 950         dtp->dt_ld_path = strdup(_dtrace_defld);
 951         dtp->dt_provmod = provmod;
 952         dtp->dt_vector = vector;
 953         dtp->dt_varg = arg;
 954         dt_dof_init(dtp);
 955         (void) uname(&dtp->dt_uts);
 956 
 957         if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
 958             dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
 959             dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
 960                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 961 
 962         for (i = 0; i < DTRACEOPT_MAX; i++)
 963                 dtp->dt_options[i] = DTRACEOPT_UNSET;
 964 
 965         dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
 966 
 967         (void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
 968             (uint_t)(sizeof (void *) * NBBY));
 969 
 970         (void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
 971             dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
 972             dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
 973 
 974         if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
 975             dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
 976             dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
 977             dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
 978             dt_cpp_add_arg(dtp, isadef) == NULL ||
 979             dt_cpp_add_arg(dtp, utsdef) == NULL)
 980                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 981 
 982         if (flags & DTRACE_O_NODEV)
 983                 bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
 984         else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
 985                 return (set_open_errno(dtp, errp, errno));
 986 
 987         if (flags & DTRACE_O_LP64)
 988                 dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
 989         else if (flags & DTRACE_O_ILP32)
 990                 dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
 991 
 992 #ifdef __sparc
 993         /*
 994          * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
 995          * and __sparcv9 is defined if we are doing a 64-bit compile.
 996          */
 997         if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
 998                 return (set_open_errno(dtp, errp, EDT_NOMEM));
 999 
1000         if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
1001             dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
1002                 return (set_open_errno(dtp, errp, EDT_NOMEM));
1003 #endif
1004 
1005 #ifdef __x86
1006         /*
1007          * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
1008          * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
1009          * they are defined exclusive of one another (see PSARC 2004/619).
1010          */
1011         if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1012                 if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
1013                         return (set_open_errno(dtp, errp, EDT_NOMEM));
1014         } else {
1015                 if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
1016                         return (set_open_errno(dtp, errp, EDT_NOMEM));
1017         }
1018 #endif
1019 
1020         if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1021                 return (set_open_errno(dtp, errp, EDT_DIFVERS));
1022 
1023         if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1024                 bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1025         else
1026                 bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1027 
1028         dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1029         dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1030             DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1031 
1032         dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1033             DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1034 
1035         dtp->dt_tls = dt_idhash_create("thread local", NULL,
1036             DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1037 
1038         if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1039             dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1040                 return (set_open_errno(dtp, errp, EDT_NOMEM));
1041 
1042         /*
1043          * Populate the dt_macros identifier hash table by hand: we can't use
1044          * the dt_idhash_populate() mechanism because we're not yet compiling
1045          * and dtrace_update() needs to immediately reference these idents.
1046          */
1047         for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1048                 if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1049                     idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1050                     idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1051                     idp->di_iarg, 0) == NULL)
1052                         return (set_open_errno(dtp, errp, EDT_NOMEM));
1053         }
1054 
1055         /*
1056          * Update the module list using /system/object and load the values for
1057          * the macro variable definitions according to the current process.
1058          */
1059         dtrace_update(dtp);
1060 
1061         /*
1062          * Select the intrinsics and typedefs we want based on the data model.
1063          * The intrinsics are under "C".  The typedefs are added under "D".
1064          */
1065         if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1066                 dinp = _dtrace_intrinsics_32;
1067                 dtyp = _dtrace_typedefs_32;
1068         } else {
1069                 dinp = _dtrace_intrinsics_64;
1070                 dtyp = _dtrace_typedefs_64;
1071         }
1072 
1073         /*
1074          * Create a dynamic CTF container under the "C" scope for intrinsic
1075          * types and types defined in ANSI-C header files that are included.
1076          */
1077         if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1078                 return (set_open_errno(dtp, errp, EDT_NOMEM));
1079 
1080         if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1081                 return (set_open_errno(dtp, errp, EDT_CTF));
1082 
1083         dt_dprintf("created CTF container for %s (%p)\n",
1084             dmp->dm_name, (void *)dmp->dm_ctfp);
1085 
1086         (void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1087         ctf_setspecific(dmp->dm_ctfp, dmp);
1088 
1089         dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1090         dmp->dm_modid = -1; /* no module ID */
1091 
1092         /*
1093          * Fill the dynamic "C" CTF container with all of the intrinsic
1094          * integer and floating-point types appropriate for this data model.
1095          */
1096         for (; dinp->din_name != NULL; dinp++) {
1097                 if (dinp->din_kind == CTF_K_INTEGER) {
1098                         err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1099                             dinp->din_name, &dinp->din_data);
1100                 } else {
1101                         err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1102                             dinp->din_name, &dinp->din_data);
1103                 }
1104 
1105                 if (err == CTF_ERR) {
1106                         dt_dprintf("failed to add %s to C container: %s\n",
1107                             dinp->din_name, ctf_errmsg(
1108                             ctf_errno(dmp->dm_ctfp)));
1109                         return (set_open_errno(dtp, errp, EDT_CTF));
1110                 }
1111         }
1112 
1113         if (ctf_update(dmp->dm_ctfp) != 0) {
1114                 dt_dprintf("failed to update C container: %s\n",
1115                     ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1116                 return (set_open_errno(dtp, errp, EDT_CTF));
1117         }
1118 
1119         /*
1120          * Add intrinsic pointer types that are needed to initialize printf
1121          * format dictionary types (see table in dt_printf.c).
1122          */
1123         (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1124             ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1125 
1126         (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1127             ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1128 
1129         (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1130             ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1131 
1132         if (ctf_update(dmp->dm_ctfp) != 0) {
1133                 dt_dprintf("failed to update C container: %s\n",
1134                     ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1135                 return (set_open_errno(dtp, errp, EDT_CTF));
1136         }
1137 
1138         /*
1139          * Create a dynamic CTF container under the "D" scope for types that
1140          * are defined by the D program itself or on-the-fly by the D compiler.
1141          * The "D" CTF container is a child of the "C" CTF container.
1142          */
1143         if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1144                 return (set_open_errno(dtp, errp, EDT_NOMEM));
1145 
1146         if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1147                 return (set_open_errno(dtp, errp, EDT_CTF));
1148 
1149         dt_dprintf("created CTF container for %s (%p)\n",
1150             dmp->dm_name, (void *)dmp->dm_ctfp);
1151 
1152         (void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1153         ctf_setspecific(dmp->dm_ctfp, dmp);
1154 
1155         dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1156         dmp->dm_modid = -1; /* no module ID */
1157 
1158         if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1159                 dt_dprintf("failed to import D parent container: %s\n",
1160                     ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1161                 return (set_open_errno(dtp, errp, EDT_CTF));
1162         }
1163 
1164         /*
1165          * Fill the dynamic "D" CTF container with all of the built-in typedefs
1166          * that we need to use for our D variable and function definitions.
1167          * This ensures that basic inttypes.h names are always available to us.
1168          */
1169         for (; dtyp->dty_src != NULL; dtyp++) {
1170                 if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1171                     dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1172                     dtyp->dty_src)) == CTF_ERR) {
1173                         dt_dprintf("failed to add typedef %s %s to D "
1174                             "container: %s", dtyp->dty_src, dtyp->dty_dst,
1175                             ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1176                         return (set_open_errno(dtp, errp, EDT_CTF));
1177                 }
1178         }
1179 
1180         /*
1181          * Insert a CTF ID corresponding to a pointer to a type of kind
1182          * CTF_K_FUNCTION we can use in the compiler for function pointers.
1183          * CTF treats all function pointers as "int (*)()" so we only need one.
1184          */
1185         ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1186         ctc.ctc_argc = 0;
1187         ctc.ctc_flags = 0;
1188 
1189         dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
1190             CTF_ADD_ROOT, &ctc, NULL);
1191 
1192         dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
1193             CTF_ADD_ROOT, dtp->dt_type_func);
1194 
1195         /*
1196          * We also insert CTF definitions for the special D intrinsic types
1197          * string and <DYN> into the D container.  The string type is added
1198          * as a typedef of char[n].  The <DYN> type is an alias for void.
1199          * We compare types to these special CTF ids throughout the compiler.
1200          */
1201         ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1202         ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1203         ctr.ctr_nelems = _dtrace_strsize;
1204 
1205         dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1206             "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1207 
1208         dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1209             "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1210 
1211         dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1212             "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1213 
1214         dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1215             "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1216 
1217         dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1218             "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1219 
1220         if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1221             dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1222             dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1223             dtp->dt_type_usymaddr == CTF_ERR) {
1224                 dt_dprintf("failed to add intrinsic to D container: %s\n",
1225                     ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1226                 return (set_open_errno(dtp, errp, EDT_CTF));
1227         }
1228 
1229         if (ctf_update(dmp->dm_ctfp) != 0) {
1230                 dt_dprintf("failed update D container: %s\n",
1231                     ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1232                 return (set_open_errno(dtp, errp, EDT_CTF));
1233         }
1234 
1235         /*
1236          * Initialize the integer description table used to convert integer
1237          * constants to the appropriate types.  Refer to the comments above
1238          * dt_node_int() for a complete description of how this table is used.
1239          */
1240         for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1241                 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1242                     dtp->dt_ints[i].did_name, &dtt) != 0) {
1243                         dt_dprintf("failed to lookup integer type %s: %s\n",
1244                             dtp->dt_ints[i].did_name,
1245                             dtrace_errmsg(dtp, dtrace_errno(dtp)));
1246                         return (set_open_errno(dtp, errp, dtp->dt_errno));
1247                 }
1248                 dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1249                 dtp->dt_ints[i].did_type = dtt.dtt_type;
1250         }
1251 
1252         /*
1253          * Now that we've created the "C" and "D" containers, move them to the
1254          * start of the module list so that these types and symbols are found
1255          * first (for stability) when iterating through the module list.
1256          */
1257         dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1258         dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1259 
1260         dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1261         dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1262 
1263         if (dt_pfdict_create(dtp) == -1)
1264                 return (set_open_errno(dtp, errp, dtp->dt_errno));
1265 
1266         /*
1267          * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1268          * because without /dev/dtrace open, we will not be able to load the
1269          * names and attributes of any providers or probes from the kernel.
1270          */
1271         if (flags & DTRACE_O_NODEV)
1272                 dtp->dt_cflags |= DTRACE_C_ZDEFS;
1273 
1274         /*
1275          * Load hard-wired inlines into the definition cache by calling the
1276          * compiler on the raw definition string defined above.
1277          */
1278         if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1279             DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1280                 dt_dprintf("failed to load hard-wired definitions: %s\n",
1281                     dtrace_errmsg(dtp, dtrace_errno(dtp)));
1282                 return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1283         }
1284 
1285         dt_program_destroy(dtp, pgp);
1286 
1287         /*
1288          * Set up the default DTrace library path.  Once set, the next call to
1289          * dt_compile() will compile all the libraries.  We intentionally defer
1290          * library processing to improve overhead for clients that don't ever
1291          * compile, and to provide better error reporting (because the full
1292          * reporting of compiler errors requires dtrace_open() to succeed).
1293          */
1294         if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1295                 return (set_open_errno(dtp, errp, dtp->dt_errno));
1296 
1297         return (dtp);
1298 }
1299 
1300 dtrace_hdl_t *
1301 dtrace_open(int version, int flags, int *errp)
1302 {
1303         return (dt_vopen(version, flags, errp, NULL, NULL));
1304 }
1305 
1306 dtrace_hdl_t *
1307 dtrace_vopen(int version, int flags, int *errp,
1308     const dtrace_vector_t *vector, void *arg)
1309 {
1310         return (dt_vopen(version, flags, errp, vector, arg));
1311 }
1312 
1313 void
1314 dtrace_close(dtrace_hdl_t *dtp)
1315 {
1316         dt_ident_t *idp, *ndp;
1317         dt_module_t *dmp;
1318         dt_provider_t *pvp;
1319         dtrace_prog_t *pgp;
1320         dt_xlator_t *dxp;
1321         dt_dirpath_t *dirp;
1322         int i;
1323 
1324         if (dtp->dt_procs != NULL)
1325                 dt_proc_hash_destroy(dtp);
1326 
1327         while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1328                 dt_program_destroy(dtp, pgp);
1329 
1330         while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1331                 dt_xlator_destroy(dtp, dxp);
1332 
1333         dt_free(dtp, dtp->dt_xlatormap);
1334 
1335         for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1336                 ndp = idp->di_next;
1337                 dt_ident_destroy(idp);
1338         }
1339 
1340         if (dtp->dt_macros != NULL)
1341                 dt_idhash_destroy(dtp->dt_macros);
1342         if (dtp->dt_aggs != NULL)
1343                 dt_idhash_destroy(dtp->dt_aggs);
1344         if (dtp->dt_globals != NULL)
1345                 dt_idhash_destroy(dtp->dt_globals);
1346         if (dtp->dt_tls != NULL)
1347                 dt_idhash_destroy(dtp->dt_tls);
1348 
1349         while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1350                 dt_module_destroy(dtp, dmp);
1351 
1352         while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1353                 dt_provider_destroy(dtp, pvp);
1354 
1355         if (dtp->dt_fd != -1)
1356                 (void) close(dtp->dt_fd);
1357         if (dtp->dt_ftfd != -1)
1358                 (void) close(dtp->dt_ftfd);
1359         if (dtp->dt_cdefs_fd != -1)
1360                 (void) close(dtp->dt_cdefs_fd);
1361         if (dtp->dt_ddefs_fd != -1)
1362                 (void) close(dtp->dt_ddefs_fd);
1363         if (dtp->dt_stdout_fd != -1)
1364                 (void) close(dtp->dt_stdout_fd);
1365 
1366         dt_epid_destroy(dtp);
1367         dt_aggid_destroy(dtp);
1368         dt_format_destroy(dtp);
1369         dt_strdata_destroy(dtp);
1370         dt_buffered_destroy(dtp);
1371         dt_aggregate_destroy(dtp);
1372         free(dtp->dt_buf.dtbd_data);
1373         dt_pfdict_destroy(dtp);
1374         dt_provmod_destroy(&dtp->dt_provmod);
1375         dt_dof_fini(dtp);
1376 
1377         for (i = 1; i < dtp->dt_cpp_argc; i++)
1378                 free(dtp->dt_cpp_argv[i]);
1379 
1380         while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1381                 dt_list_delete(&dtp->dt_lib_path, dirp);
1382                 free(dirp->dir_path);
1383                 free(dirp);
1384         }
1385 
1386         free(dtp->dt_cpp_argv);
1387         free(dtp->dt_cpp_path);
1388         free(dtp->dt_ld_path);
1389 
1390         free(dtp->dt_mods);
1391         free(dtp->dt_provs);
1392         free(dtp);
1393 }
1394 
1395 int
1396 dtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1397 {
1398         dt_provmod_t *prov;
1399         int i = 0;
1400 
1401         for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1402                 if (i < nmods)
1403                         mods[i] = prov->dp_name;
1404         }
1405 
1406         return (i);
1407 }
1408 
1409 int
1410 dtrace_ctlfd(dtrace_hdl_t *dtp)
1411 {
1412         return (dtp->dt_fd);
1413 }