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