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