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