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