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