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