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