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