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