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