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