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