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