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