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