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