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