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