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