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