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