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