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