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