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