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