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