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