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