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