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