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