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