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