xref: /titanic_50/usr/src/lib/libdtrace/common/dt_open.c (revision ead9bb4b1be81d7bbf8ed86ee41d6c1e58b069a3)
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 { "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
407 	DT_ATTR_STABCMN, DT_VERS_1_0,
408 	&dt_idops_type, "uint64_t" },
409 { "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
410 	DT_ATTR_STABCMN, DT_VERS_1_0,
411 	&dt_idops_type, "int64_t" },
412 { "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
413 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
414 { NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
415 };
416 
417 /*
418  * Tables of ILP32 intrinsic integer and floating-point type templates to use
419  * to populate the dynamic "C" CTF type container.
420  */
421 static const dt_intrinsic_t _dtrace_intrinsics_32[] = {
422 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
423 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
424 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
425 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
426 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
427 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
428 { "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
429 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
430 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
431 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
432 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
433 { "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
434 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
435 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
436 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
437 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
438 { "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
439 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
440 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
441 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
442 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
443 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
444 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
445 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
446 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
447 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
448 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
449 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
450 { NULL, { 0, 0, 0 }, 0 }
451 };
452 
453 /*
454  * Tables of LP64 intrinsic integer and floating-point type templates to use
455  * to populate the dynamic "C" CTF type container.
456  */
457 static const dt_intrinsic_t _dtrace_intrinsics_64[] = {
458 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
459 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
460 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
461 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
462 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
463 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
464 { "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
465 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
466 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
467 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
468 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
469 { "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
470 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
471 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
472 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
473 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
474 { "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
475 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
476 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
477 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
478 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
479 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
480 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
481 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
482 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
483 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
484 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
485 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
486 { NULL, { 0, 0, 0 }, 0 }
487 };
488 
489 /*
490  * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
491  * These aliases ensure that D definitions can use typical <sys/types.h> names.
492  */
493 static const dt_typedef_t _dtrace_typedefs_32[] = {
494 { "char", "int8_t" },
495 { "short", "int16_t" },
496 { "int", "int32_t" },
497 { "long long", "int64_t" },
498 { "int", "intptr_t" },
499 { "int", "ssize_t" },
500 { "unsigned char", "uint8_t" },
501 { "unsigned short", "uint16_t" },
502 { "unsigned", "uint32_t" },
503 { "unsigned long long", "uint64_t" },
504 { "unsigned char", "uchar_t" },
505 { "unsigned short", "ushort_t" },
506 { "unsigned", "uint_t" },
507 { "unsigned long", "ulong_t" },
508 { "unsigned long long", "u_longlong_t" },
509 { "int", "ptrdiff_t" },
510 { "unsigned", "uintptr_t" },
511 { "unsigned", "size_t" },
512 { "long", "id_t" },
513 { "long", "pid_t" },
514 { NULL, NULL }
515 };
516 
517 /*
518  * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
519  * These aliases ensure that D definitions can use typical <sys/types.h> names.
520  */
521 static const dt_typedef_t _dtrace_typedefs_64[] = {
522 { "char", "int8_t" },
523 { "short", "int16_t" },
524 { "int", "int32_t" },
525 { "long", "int64_t" },
526 { "long", "intptr_t" },
527 { "long", "ssize_t" },
528 { "unsigned char", "uint8_t" },
529 { "unsigned short", "uint16_t" },
530 { "unsigned", "uint32_t" },
531 { "unsigned long", "uint64_t" },
532 { "unsigned char", "uchar_t" },
533 { "unsigned short", "ushort_t" },
534 { "unsigned", "uint_t" },
535 { "unsigned long", "ulong_t" },
536 { "unsigned long long", "u_longlong_t" },
537 { "long", "ptrdiff_t" },
538 { "unsigned long", "uintptr_t" },
539 { "unsigned long", "size_t" },
540 { "int", "id_t" },
541 { "int", "pid_t" },
542 { NULL, NULL }
543 };
544 
545 /*
546  * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
547  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
548  */
549 static const dt_intdesc_t _dtrace_ints_32[] = {
550 { "int", NULL, CTF_ERR, 0x7fffffffULL },
551 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
552 { "long", NULL, CTF_ERR, 0x7fffffffULL },
553 { "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
554 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
555 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
556 };
557 
558 /*
559  * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
560  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
561  */
562 static const dt_intdesc_t _dtrace_ints_64[] = {
563 { "int", NULL, CTF_ERR, 0x7fffffffULL },
564 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
565 { "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
566 { "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
567 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
568 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
569 };
570 
571 /*
572  * Table of macro variable templates used to populate the macro identifier hash
573  * when a new dtrace client open occurs.  Values are set by dtrace_update().
574  */
575 static const dt_ident_t _dtrace_macros[] = {
576 { "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
577 { "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
578 { "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
579 { "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
580 { "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
581 { "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
582 { "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
583 { "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
584 { "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
585 { "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
586 { "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
587 { NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
588 };
589 
590 /*
591  * Hard-wired definition string to be compiled and cached every time a new
592  * DTrace library handle is initialized.  This string should only be used to
593  * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
594  */
595 static const char _dtrace_hardwire[] = "\
596 inline long NULL = 0; \n\
597 #pragma D binding \"1.0\" NULL\n\
598 ";
599 
600 /*
601  * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
602  * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
603  * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
604  * relying on the fact that when running dtrace(1M), isaexec will invoke the
605  * binary with the same bitness as the kernel, which is what we want by default
606  * when generating our DIF.  The user can override the choice using oflags.
607  */
608 static const dtrace_conf_t _dtrace_conf = {
609 	DIF_VERSION,		/* dtc_difversion */
610 	DIF_DIR_NREGS,		/* dtc_difintregs */
611 	DIF_DTR_NREGS,		/* dtc_diftupregs */
612 	CTF_MODEL_NATIVE	/* dtc_ctfmodel */
613 };
614 
615 const dtrace_attribute_t _dtrace_maxattr = {
616 	DTRACE_STABILITY_MAX,
617 	DTRACE_STABILITY_MAX,
618 	DTRACE_CLASS_MAX
619 };
620 
621 const dtrace_attribute_t _dtrace_defattr = {
622 	DTRACE_STABILITY_STABLE,
623 	DTRACE_STABILITY_STABLE,
624 	DTRACE_CLASS_COMMON
625 };
626 
627 const dtrace_attribute_t _dtrace_symattr = {
628 	DTRACE_STABILITY_PRIVATE,
629 	DTRACE_STABILITY_PRIVATE,
630 	DTRACE_CLASS_UNKNOWN
631 };
632 
633 const dtrace_attribute_t _dtrace_typattr = {
634 	DTRACE_STABILITY_PRIVATE,
635 	DTRACE_STABILITY_PRIVATE,
636 	DTRACE_CLASS_UNKNOWN
637 };
638 
639 const dtrace_attribute_t _dtrace_prvattr = {
640 	DTRACE_STABILITY_PRIVATE,
641 	DTRACE_STABILITY_PRIVATE,
642 	DTRACE_CLASS_UNKNOWN
643 };
644 
645 const dtrace_pattr_t _dtrace_prvdesc = {
646 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
647 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
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 };
652 
653 const char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
654 const char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
655 
656 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
657 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
658 
659 int _dtrace_strbuckets = 211;	/* default number of hash buckets (prime) */
660 int _dtrace_intbuckets = 256;	/* default number of integer buckets (Pof2) */
661 uint_t _dtrace_strsize = 256;	/* default size of string intrinsic type */
662 uint_t _dtrace_stkindent = 14;	/* default whitespace indent for stack/ustack */
663 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
664 uint_t _dtrace_pidlrulim = 8;	/* default number of pid handles to cache */
665 size_t _dtrace_bufsize = 512;	/* default dt_buf_create() size */
666 int _dtrace_argmax = 32;	/* default maximum number of probe arguments */
667 
668 int _dtrace_debug = 0;		/* debug messages enabled (off) */
669 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
670 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
671 
672 typedef struct dt_fdlist {
673 	int *df_fds;		/* array of provider driver file descriptors */
674 	uint_t df_ents;		/* number of valid elements in df_fds[] */
675 	uint_t df_size;		/* size of df_fds[] */
676 } dt_fdlist_t;
677 
678 #pragma init(_dtrace_init)
679 void
680 _dtrace_init(void)
681 {
682 	_dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
683 
684 	for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
685 		if (rd_init(_dtrace_rdvers) == RD_OK)
686 			break;
687 	}
688 }
689 
690 static dtrace_hdl_t *
691 set_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
692 {
693 	if (dtp != NULL)
694 		dtrace_close(dtp);
695 	if (errp != NULL)
696 		*errp = err;
697 	return (NULL);
698 }
699 
700 static void
701 dt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
702 {
703 	dt_provmod_t *prov;
704 	char path[PATH_MAX];
705 	struct dirent *dp, *ep;
706 	DIR *dirp;
707 	int fd;
708 
709 	if ((dirp = opendir(_dtrace_provdir)) == NULL)
710 		return; /* failed to open directory; just skip it */
711 
712 	ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
713 	bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
714 
715 	while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
716 		if (dp->d_name[0] == '.')
717 			continue; /* skip "." and ".." */
718 
719 		if (dfp->df_ents == dfp->df_size) {
720 			uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
721 			int *fds = realloc(dfp->df_fds, size * sizeof (int));
722 
723 			if (fds == NULL)
724 				break; /* skip the rest of this directory */
725 
726 			dfp->df_fds = fds;
727 			dfp->df_size = size;
728 		}
729 
730 		(void) snprintf(path, sizeof (path), "%s/%s",
731 		    _dtrace_provdir, dp->d_name);
732 
733 		if ((fd = open(path, O_RDONLY)) == -1)
734 			continue; /* failed to open driver; just skip it */
735 
736 		if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
737 		    (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
738 			free(prov);
739 			(void) close(fd);
740 			break;
741 		}
742 
743 		(void) strcpy(prov->dp_name, dp->d_name);
744 		prov->dp_next = *provmod;
745 		*provmod = prov;
746 
747 		dt_dprintf("opened provider %s\n", dp->d_name);
748 		dfp->df_fds[dfp->df_ents++] = fd;
749 	}
750 
751 	(void) closedir(dirp);
752 }
753 
754 static void
755 dt_provmod_destroy(dt_provmod_t **provmod)
756 {
757 	dt_provmod_t *next, *current;
758 
759 	for (current = *provmod; current != NULL; current = next) {
760 		next = current->dp_next;
761 		free(current->dp_name);
762 		free(current);
763 	}
764 
765 	*provmod = NULL;
766 }
767 
768 static const char *
769 dt_get_sysinfo(int cmd, char *buf, size_t len)
770 {
771 	ssize_t rv = sysinfo(cmd, buf, len);
772 	char *p = buf;
773 
774 	if (rv < 0 || rv > len)
775 		(void) snprintf(buf, len, "%s", "Unknown");
776 
777 	while ((p = strchr(p, '.')) != NULL)
778 		*p++ = '_';
779 
780 	return (buf);
781 }
782 
783 static dtrace_hdl_t *
784 dt_vopen(int version, int flags, int *errp,
785     const dtrace_vector_t *vector, void *arg)
786 {
787 	dtrace_hdl_t *dtp = NULL;
788 	int dtfd = -1, ftfd = -1, fterr = 0;
789 	dtrace_prog_t *pgp;
790 	dt_module_t *dmp;
791 	dt_provmod_t *provmod = NULL;
792 	int i, err;
793 	struct rlimit rl;
794 
795 	const dt_intrinsic_t *dinp;
796 	const dt_typedef_t *dtyp;
797 	const dt_ident_t *idp;
798 
799 	dtrace_typeinfo_t dtt;
800 	ctf_funcinfo_t ctc;
801 	ctf_arinfo_t ctr;
802 
803 	dt_fdlist_t df = { NULL, 0, 0 };
804 
805 	char isadef[32], utsdef[32];
806 	char s1[64], s2[64];
807 
808 	if (version <= 0)
809 		return (set_open_errno(dtp, errp, EINVAL));
810 
811 	if (version > DTRACE_VERSION)
812 		return (set_open_errno(dtp, errp, EDT_VERSION));
813 
814 	if (version < DTRACE_VERSION) {
815 		/*
816 		 * Currently, increasing the library version number is used to
817 		 * denote a binary incompatible change.  That is, a consumer
818 		 * of the library cannot run on a version of the library with
819 		 * a higher DTRACE_VERSION number than the consumer compiled
820 		 * against.  Once the library API has been committed to,
821 		 * backwards binary compatibility will be required; at that
822 		 * time, this check should change to return EDT_OVERSION only
823 		 * if the specified version number is less than the version
824 		 * number at the time of interface commitment.
825 		 */
826 		return (set_open_errno(dtp, errp, EDT_OVERSION));
827 	}
828 
829 	if (flags & ~DTRACE_O_MASK)
830 		return (set_open_errno(dtp, errp, EINVAL));
831 
832 	if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
833 		return (set_open_errno(dtp, errp, EINVAL));
834 
835 	if (vector == NULL && arg != NULL)
836 		return (set_open_errno(dtp, errp, EINVAL));
837 
838 	if (elf_version(EV_CURRENT) == EV_NONE)
839 		return (set_open_errno(dtp, errp, EDT_ELFVERSION));
840 
841 	if (vector != NULL || (flags & DTRACE_O_NODEV))
842 		goto alloc; /* do not attempt to open dtrace device */
843 
844 	/*
845 	 * Before we get going, crank our limit on file descriptors up to the
846 	 * hard limit.  This is to allow for the fact that libproc keeps file
847 	 * descriptors to objects open for the lifetime of the proc handle;
848 	 * without raising our hard limit, we would have an acceptably small
849 	 * bound on the number of processes that we could concurrently
850 	 * instrument with the pid provider.
851 	 */
852 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
853 		rl.rlim_cur = rl.rlim_max;
854 		(void) setrlimit(RLIMIT_NOFILE, &rl);
855 	}
856 
857 	/*
858 	 * Get the device path of each of the providers.  We hold them open
859 	 * in the df.df_fds list until we open the DTrace driver itself,
860 	 * allowing us to see all of the probes provided on this system.  Once
861 	 * we have the DTrace driver open, we can safely close all the providers
862 	 * now that they have registered with the framework.
863 	 */
864 	dt_provmod_open(&provmod, &df);
865 
866 	dtfd = open("/dev/dtrace/dtrace", O_RDWR);
867 	err = errno; /* save errno from opening dtfd */
868 
869 	ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
870 	fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
871 
872 	while (df.df_ents-- != 0)
873 		(void) close(df.df_fds[df.df_ents]);
874 
875 	free(df.df_fds);
876 
877 	/*
878 	 * If we failed to open the dtrace device, fail dtrace_open().
879 	 * We convert some kernel errnos to custom libdtrace errnos to
880 	 * improve the resulting message from the usual strerror().
881 	 */
882 	if (dtfd == -1) {
883 		dt_provmod_destroy(&provmod);
884 		switch (err) {
885 		case ENOENT:
886 			err = EDT_NOENT;
887 			break;
888 		case EBUSY:
889 			err = EDT_BUSY;
890 			break;
891 		case EACCES:
892 			err = EDT_ACCESS;
893 			break;
894 		}
895 		return (set_open_errno(dtp, errp, err));
896 	}
897 
898 	(void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
899 	(void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
900 
901 alloc:
902 	if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
903 		return (set_open_errno(dtp, errp, EDT_NOMEM));
904 
905 	bzero(dtp, sizeof (dtrace_hdl_t));
906 	dtp->dt_oflags = flags;
907 	dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
908 	dtp->dt_linkmode = DT_LINK_KERNEL;
909 	dtp->dt_linktype = DT_LTYP_ELF;
910 	dtp->dt_xlatemode = DT_XL_STATIC;
911 	dtp->dt_stdcmode = DT_STDC_XA;
912 	dtp->dt_version = version;
913 	dtp->dt_fd = dtfd;
914 	dtp->dt_ftfd = ftfd;
915 	dtp->dt_fterr = fterr;
916 	dtp->dt_cdefs_fd = -1;
917 	dtp->dt_ddefs_fd = -1;
918 	dtp->dt_stdout_fd = -1;
919 	dtp->dt_modbuckets = _dtrace_strbuckets;
920 	dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
921 	dtp->dt_provbuckets = _dtrace_strbuckets;
922 	dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
923 	dt_proc_hash_create(dtp);
924 	dtp->dt_vmax = DT_VERS_LATEST;
925 	dtp->dt_cpp_path = strdup(_dtrace_defcpp);
926 	dtp->dt_cpp_argv = malloc(sizeof (char *));
927 	dtp->dt_cpp_argc = 1;
928 	dtp->dt_cpp_args = 1;
929 	dtp->dt_ld_path = strdup(_dtrace_defld);
930 	dtp->dt_provmod = provmod;
931 	dtp->dt_vector = vector;
932 	dtp->dt_varg = arg;
933 	dt_dof_init(dtp);
934 	(void) uname(&dtp->dt_uts);
935 
936 	if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
937 	    dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
938 	    dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
939 		return (set_open_errno(dtp, errp, EDT_NOMEM));
940 
941 	for (i = 0; i < DTRACEOPT_MAX; i++)
942 		dtp->dt_options[i] = DTRACEOPT_UNSET;
943 
944 	dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
945 
946 	(void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
947 	    (uint_t)(sizeof (void *) * NBBY));
948 
949 	(void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
950 	    dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
951 	    dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
952 
953 	if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
954 	    dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
955 	    dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
956 	    dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
957 	    dt_cpp_add_arg(dtp, isadef) == NULL ||
958 	    dt_cpp_add_arg(dtp, utsdef) == NULL)
959 		return (set_open_errno(dtp, errp, EDT_NOMEM));
960 
961 	if (flags & DTRACE_O_NODEV)
962 		bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
963 	else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
964 		return (set_open_errno(dtp, errp, errno));
965 
966 	if (flags & DTRACE_O_LP64)
967 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
968 	else if (flags & DTRACE_O_ILP32)
969 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
970 
971 #ifdef __sparc
972 	/*
973 	 * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
974 	 * and __sparcv9 is defined if we are doing a 64-bit compile.
975 	 */
976 	if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
977 		return (set_open_errno(dtp, errp, EDT_NOMEM));
978 
979 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
980 	    dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
981 		return (set_open_errno(dtp, errp, EDT_NOMEM));
982 #endif
983 
984 #ifdef __x86
985 	/*
986 	 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
987 	 * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
988 	 * they are defined exclusive of one another (see PSARC 2004/619).
989 	 */
990 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
991 		if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
992 			return (set_open_errno(dtp, errp, EDT_NOMEM));
993 	} else {
994 		if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
995 			return (set_open_errno(dtp, errp, EDT_NOMEM));
996 	}
997 #endif
998 
999 	if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1000 		return (set_open_errno(dtp, errp, EDT_DIFVERS));
1001 
1002 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1003 		bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1004 	else
1005 		bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1006 
1007 	dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1008 	dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1009 	    DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1010 
1011 	dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1012 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1013 
1014 	dtp->dt_tls = dt_idhash_create("thread local", NULL,
1015 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1016 
1017 	if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1018 	    dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1019 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1020 
1021 	/*
1022 	 * Populate the dt_macros identifier hash table by hand: we can't use
1023 	 * the dt_idhash_populate() mechanism because we're not yet compiling
1024 	 * and dtrace_update() needs to immediately reference these idents.
1025 	 */
1026 	for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1027 		if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1028 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1029 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1030 		    idp->di_iarg, 0) == NULL)
1031 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1032 	}
1033 
1034 	/*
1035 	 * Update the module list using /system/object and load the values for
1036 	 * the macro variable definitions according to the current process.
1037 	 */
1038 	dtrace_update(dtp);
1039 
1040 	/*
1041 	 * Select the intrinsics and typedefs we want based on the data model.
1042 	 * The intrinsics are under "C".  The typedefs are added under "D".
1043 	 */
1044 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1045 		dinp = _dtrace_intrinsics_32;
1046 		dtyp = _dtrace_typedefs_32;
1047 	} else {
1048 		dinp = _dtrace_intrinsics_64;
1049 		dtyp = _dtrace_typedefs_64;
1050 	}
1051 
1052 	/*
1053 	 * Create a dynamic CTF container under the "C" scope for intrinsic
1054 	 * types and types defined in ANSI-C header files that are included.
1055 	 */
1056 	if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1057 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1058 
1059 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1060 		return (set_open_errno(dtp, errp, EDT_CTF));
1061 
1062 	dt_dprintf("created CTF container for %s (%p)\n",
1063 	    dmp->dm_name, (void *)dmp->dm_ctfp);
1064 
1065 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1066 	ctf_setspecific(dmp->dm_ctfp, dmp);
1067 
1068 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1069 	dmp->dm_modid = -1; /* no module ID */
1070 
1071 	/*
1072 	 * Fill the dynamic "C" CTF container with all of the intrinsic
1073 	 * integer and floating-point types appropriate for this data model.
1074 	 */
1075 	for (; dinp->din_name != NULL; dinp++) {
1076 		if (dinp->din_kind == CTF_K_INTEGER) {
1077 			err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1078 			    dinp->din_name, &dinp->din_data);
1079 		} else {
1080 			err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1081 			    dinp->din_name, &dinp->din_data);
1082 		}
1083 
1084 		if (err == CTF_ERR) {
1085 			dt_dprintf("failed to add %s to C container: %s\n",
1086 			    dinp->din_name, ctf_errmsg(
1087 			    ctf_errno(dmp->dm_ctfp)));
1088 			return (set_open_errno(dtp, errp, EDT_CTF));
1089 		}
1090 	}
1091 
1092 	if (ctf_update(dmp->dm_ctfp) != 0) {
1093 		dt_dprintf("failed to update C container: %s\n",
1094 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1095 		return (set_open_errno(dtp, errp, EDT_CTF));
1096 	}
1097 
1098 	/*
1099 	 * Add intrinsic pointer types that are needed to initialize printf
1100 	 * format dictionary types (see table in dt_printf.c).
1101 	 */
1102 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1103 	    ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1104 
1105 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1106 	    ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1107 
1108 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1109 	    ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1110 
1111 	if (ctf_update(dmp->dm_ctfp) != 0) {
1112 		dt_dprintf("failed to update C container: %s\n",
1113 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1114 		return (set_open_errno(dtp, errp, EDT_CTF));
1115 	}
1116 
1117 	/*
1118 	 * Create a dynamic CTF container under the "D" scope for types that
1119 	 * are defined by the D program itself or on-the-fly by the D compiler.
1120 	 * The "D" CTF container is a child of the "C" CTF container.
1121 	 */
1122 	if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1123 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1124 
1125 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1126 		return (set_open_errno(dtp, errp, EDT_CTF));
1127 
1128 	dt_dprintf("created CTF container for %s (%p)\n",
1129 	    dmp->dm_name, (void *)dmp->dm_ctfp);
1130 
1131 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1132 	ctf_setspecific(dmp->dm_ctfp, dmp);
1133 
1134 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1135 	dmp->dm_modid = -1; /* no module ID */
1136 
1137 	if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1138 		dt_dprintf("failed to import D parent container: %s\n",
1139 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1140 		return (set_open_errno(dtp, errp, EDT_CTF));
1141 	}
1142 
1143 	/*
1144 	 * Fill the dynamic "D" CTF container with all of the built-in typedefs
1145 	 * that we need to use for our D variable and function definitions.
1146 	 * This ensures that basic inttypes.h names are always available to us.
1147 	 */
1148 	for (; dtyp->dty_src != NULL; dtyp++) {
1149 		if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1150 		    dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1151 		    dtyp->dty_src)) == CTF_ERR) {
1152 			dt_dprintf("failed to add typedef %s %s to D "
1153 			    "container: %s", dtyp->dty_src, dtyp->dty_dst,
1154 			    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1155 			return (set_open_errno(dtp, errp, EDT_CTF));
1156 		}
1157 	}
1158 
1159 	/*
1160 	 * Insert a CTF ID corresponding to a pointer to a type of kind
1161 	 * CTF_K_FUNCTION we can use in the compiler for function pointers.
1162 	 * CTF treats all function pointers as "int (*)()" so we only need one.
1163 	 */
1164 	ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1165 	ctc.ctc_argc = 0;
1166 	ctc.ctc_flags = 0;
1167 
1168 	dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
1169 	    CTF_ADD_ROOT, &ctc, NULL);
1170 
1171 	dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
1172 	    CTF_ADD_ROOT, dtp->dt_type_func);
1173 
1174 	/*
1175 	 * We also insert CTF definitions for the special D intrinsic types
1176 	 * string and <DYN> into the D container.  The string type is added
1177 	 * as a typedef of char[n].  The <DYN> type is an alias for void.
1178 	 * We compare types to these special CTF ids throughout the compiler.
1179 	 */
1180 	ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1181 	ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1182 	ctr.ctr_nelems = _dtrace_strsize;
1183 
1184 	dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1185 	    "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1186 
1187 	dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1188 	    "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1189 
1190 	dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1191 	    "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1192 
1193 	dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1194 	    "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1195 
1196 	dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1197 	    "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1198 
1199 	if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1200 	    dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1201 	    dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1202 	    dtp->dt_type_usymaddr == CTF_ERR) {
1203 		dt_dprintf("failed to add intrinsic to D container: %s\n",
1204 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1205 		return (set_open_errno(dtp, errp, EDT_CTF));
1206 	}
1207 
1208 	if (ctf_update(dmp->dm_ctfp) != 0) {
1209 		dt_dprintf("failed update D container: %s\n",
1210 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1211 		return (set_open_errno(dtp, errp, EDT_CTF));
1212 	}
1213 
1214 	/*
1215 	 * Initialize the integer description table used to convert integer
1216 	 * constants to the appropriate types.  Refer to the comments above
1217 	 * dt_node_int() for a complete description of how this table is used.
1218 	 */
1219 	for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1220 		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1221 		    dtp->dt_ints[i].did_name, &dtt) != 0) {
1222 			dt_dprintf("failed to lookup integer type %s: %s\n",
1223 			    dtp->dt_ints[i].did_name,
1224 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1225 			return (set_open_errno(dtp, errp, dtp->dt_errno));
1226 		}
1227 		dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1228 		dtp->dt_ints[i].did_type = dtt.dtt_type;
1229 	}
1230 
1231 	/*
1232 	 * Now that we've created the "C" and "D" containers, move them to the
1233 	 * start of the module list so that these types and symbols are found
1234 	 * first (for stability) when iterating through the module list.
1235 	 */
1236 	dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1237 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1238 
1239 	dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1240 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1241 
1242 	if (dt_pfdict_create(dtp) == -1)
1243 		return (set_open_errno(dtp, errp, dtp->dt_errno));
1244 
1245 	/*
1246 	 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1247 	 * because without /dev/dtrace open, we will not be able to load the
1248 	 * names and attributes of any providers or probes from the kernel.
1249 	 */
1250 	if (flags & DTRACE_O_NODEV)
1251 		dtp->dt_cflags |= DTRACE_C_ZDEFS;
1252 
1253 	/*
1254 	 * Load hard-wired inlines into the definition cache by calling the
1255 	 * compiler on the raw definition string defined above.
1256 	 */
1257 	if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1258 	    DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1259 		dt_dprintf("failed to load hard-wired definitions: %s\n",
1260 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1261 		return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1262 	}
1263 
1264 	dt_program_destroy(dtp, pgp);
1265 
1266 	/*
1267 	 * Set up the default DTrace library path.  Once set, the next call to
1268 	 * dt_compile() will compile all the libraries.  We intentionally defer
1269 	 * library processing to improve overhead for clients that don't ever
1270 	 * compile, and to provide better error reporting (because the full
1271 	 * reporting of compiler errors requires dtrace_open() to succeed).
1272 	 */
1273 	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1274 		return (set_open_errno(dtp, errp, dtp->dt_errno));
1275 
1276 	return (dtp);
1277 }
1278 
1279 dtrace_hdl_t *
1280 dtrace_open(int version, int flags, int *errp)
1281 {
1282 	return (dt_vopen(version, flags, errp, NULL, NULL));
1283 }
1284 
1285 dtrace_hdl_t *
1286 dtrace_vopen(int version, int flags, int *errp,
1287     const dtrace_vector_t *vector, void *arg)
1288 {
1289 	return (dt_vopen(version, flags, errp, vector, arg));
1290 }
1291 
1292 void
1293 dtrace_close(dtrace_hdl_t *dtp)
1294 {
1295 	dt_ident_t *idp, *ndp;
1296 	dt_module_t *dmp;
1297 	dt_provider_t *pvp;
1298 	dtrace_prog_t *pgp;
1299 	dt_xlator_t *dxp;
1300 	dt_dirpath_t *dirp;
1301 	int i;
1302 
1303 	if (dtp->dt_procs != NULL)
1304 		dt_proc_hash_destroy(dtp);
1305 
1306 	while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1307 		dt_program_destroy(dtp, pgp);
1308 
1309 	while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1310 		dt_xlator_destroy(dtp, dxp);
1311 
1312 	dt_free(dtp, dtp->dt_xlatormap);
1313 
1314 	for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1315 		ndp = idp->di_next;
1316 		dt_ident_destroy(idp);
1317 	}
1318 
1319 	if (dtp->dt_macros != NULL)
1320 		dt_idhash_destroy(dtp->dt_macros);
1321 	if (dtp->dt_aggs != NULL)
1322 		dt_idhash_destroy(dtp->dt_aggs);
1323 	if (dtp->dt_globals != NULL)
1324 		dt_idhash_destroy(dtp->dt_globals);
1325 	if (dtp->dt_tls != NULL)
1326 		dt_idhash_destroy(dtp->dt_tls);
1327 
1328 	while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1329 		dt_module_destroy(dtp, dmp);
1330 
1331 	while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1332 		dt_provider_destroy(dtp, pvp);
1333 
1334 	if (dtp->dt_fd != -1)
1335 		(void) close(dtp->dt_fd);
1336 	if (dtp->dt_ftfd != -1)
1337 		(void) close(dtp->dt_ftfd);
1338 	if (dtp->dt_cdefs_fd != -1)
1339 		(void) close(dtp->dt_cdefs_fd);
1340 	if (dtp->dt_ddefs_fd != -1)
1341 		(void) close(dtp->dt_ddefs_fd);
1342 	if (dtp->dt_stdout_fd != -1)
1343 		(void) close(dtp->dt_stdout_fd);
1344 
1345 	dt_epid_destroy(dtp);
1346 	dt_aggid_destroy(dtp);
1347 	dt_format_destroy(dtp);
1348 	dt_buffered_destroy(dtp);
1349 	dt_aggregate_destroy(dtp);
1350 	free(dtp->dt_buf.dtbd_data);
1351 	dt_pfdict_destroy(dtp);
1352 	dt_provmod_destroy(&dtp->dt_provmod);
1353 	dt_dof_fini(dtp);
1354 
1355 	for (i = 1; i < dtp->dt_cpp_argc; i++)
1356 		free(dtp->dt_cpp_argv[i]);
1357 
1358 	while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1359 		dt_list_delete(&dtp->dt_lib_path, dirp);
1360 		free(dirp->dir_path);
1361 		free(dirp);
1362 	}
1363 
1364 	free(dtp->dt_cpp_argv);
1365 	free(dtp->dt_cpp_path);
1366 	free(dtp->dt_ld_path);
1367 
1368 	free(dtp->dt_mods);
1369 	free(dtp->dt_provs);
1370 	free(dtp);
1371 }
1372 
1373 int
1374 dtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1375 {
1376 	dt_provmod_t *prov;
1377 	int i = 0;
1378 
1379 	for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1380 		if (i < nmods)
1381 			mods[i] = prov->dp_name;
1382 	}
1383 
1384 	return (i);
1385 }
1386 
1387 int
1388 dtrace_ctlfd(dtrace_hdl_t *dtp)
1389 {
1390 	return (dtp->dt_fd);
1391 }
1392