xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/common/dt_open.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
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_1_6_2	DT_VERSION_NUMBER(1, 6, 2)
119 #define	DT_VERS_LATEST	DT_VERS_1_6_2
120 #define	DT_VERS_STRING	"Sun D 1.6.2"
121 
122 const dt_version_t _dtrace_versions[] = {
123 	DT_VERS_1_0,	/* D API 1.0.0 (PSARC 2001/466) Solaris 10 FCS */
124 	DT_VERS_1_1,	/* D API 1.1.0 Solaris Express 6/05 */
125 	DT_VERS_1_2,	/* D API 1.2.0 Solaris 10 Update 1 */
126 	DT_VERS_1_2_1,	/* D API 1.2.1 Solaris Express 4/06 */
127 	DT_VERS_1_2_2,	/* D API 1.2.2 Solaris Express 6/06 */
128 	DT_VERS_1_3,	/* D API 1.3 Solaris Express 10/06 */
129 	DT_VERS_1_4,	/* D API 1.4 Solaris Express 2/07 */
130 	DT_VERS_1_4_1,	/* D API 1.4.1 Solaris Express 4/07 */
131 	DT_VERS_1_5,	/* D API 1.5 Solaris Express 7/07 */
132 	DT_VERS_1_6,	/* D API 1.6 */
133 	DT_VERS_1_6_1,	/* D API 1.6.1 */
134 	DT_VERS_1_6_2,	/* D API 1.6.2 */
135 	0
136 };
137 
138 /*
139  * Global variables that are formatted on FreeBSD based on the kernel file name.
140  */
141 #if !defined(sun)
142 static char	curthread_str[MAXPATHLEN];
143 static char	intmtx_str[MAXPATHLEN];
144 static char	threadmtx_str[MAXPATHLEN];
145 static char	rwlock_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 { "sym", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
435 	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
436 { "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
437 	&dt_idops_func, "void(@, ...)" },
438 { "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
439 	&dt_idops_type, "void" },
440 { "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
441 	&dt_idops_type, "id_t" },
442 { "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
443 	DT_ATTR_STABCMN, DT_VERS_1_0,
444 	&dt_idops_type, "uint64_t" },
445 { "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
446 	&dt_idops_func, "void(@)" },
447 { "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
448 	DT_ATTR_STABCMN, DT_VERS_1_0,
449 	&dt_idops_func, "void(@, size_t)" },
450 { "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
451 	DT_VERS_1_0, &dt_idops_func, "void(...)" },
452 { "typeref", DT_IDENT_FUNC, 0, DIF_SUBR_TYPEREF, DT_ATTR_STABCMN, DT_VERS_1_1,
453 	&dt_idops_func, "uintptr_t *(void *, size_t, string, size_t)" },
454 #if defined(sun)
455 { "uaddr", DT_IDENT_ACTFUNC, 0, DT_ACT_UADDR, DT_ATTR_STABCMN,
456 	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
457 { "ucaller", DT_IDENT_SCALAR, 0, DIF_VAR_UCALLER, DT_ATTR_STABCMN,
458 	DT_VERS_1_2, &dt_idops_type, "uint64_t" },
459 { "ufunc", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
460 	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
461 #endif
462 { "uid", DT_IDENT_SCALAR, 0, DIF_VAR_UID, DT_ATTR_STABCMN, DT_VERS_1_0,
463 	&dt_idops_type, "uid_t" },
464 #if defined(sun)
465 { "umod", DT_IDENT_ACTFUNC, 0, DT_ACT_UMOD, DT_ATTR_STABCMN,
466 	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
467 { "uregs", DT_IDENT_ARRAY, 0, DIF_VAR_UREGS, DT_ATTR_STABCMN, DT_VERS_1_0,
468 	&dt_idops_regs, NULL },
469 { "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
470 	&dt_idops_func, "stack(...)" },
471 { "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
472 	DT_ATTR_STABCMN, DT_VERS_1_2,
473 	&dt_idops_type, "uint32_t" },
474 { "usym", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
475 	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
476 #endif
477 { "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
478 	DT_ATTR_STABCMN, DT_VERS_1_0,
479 	&dt_idops_type, "uint64_t" },
480 { "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
481 	DT_ATTR_STABCMN, DT_VERS_1_0,
482 	&dt_idops_type, "int64_t" },
483 #if defined(sun)
484 { "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
485 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
486 #endif
487 { NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
488 };
489 
490 /*
491  * Tables of ILP32 intrinsic integer and floating-point type templates to use
492  * to populate the dynamic "C" CTF type container.
493  */
494 static const dt_intrinsic_t _dtrace_intrinsics_32[] = {
495 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
496 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
497 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
498 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
499 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
500 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
501 { "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
502 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
503 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
504 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
505 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
506 { "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
507 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
508 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
509 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
510 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
511 { "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
512 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
513 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
514 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
515 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
516 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
517 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
518 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
519 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
520 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
521 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
522 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
523 { NULL, { 0, 0, 0 }, 0 }
524 };
525 
526 /*
527  * Tables of LP64 intrinsic integer and floating-point type templates to use
528  * to populate the dynamic "C" CTF type container.
529  */
530 static const dt_intrinsic_t _dtrace_intrinsics_64[] = {
531 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
532 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
533 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
534 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
535 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
536 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
537 { "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
538 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
539 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
540 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
541 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
542 { "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
543 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
544 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
545 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
546 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
547 { "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
548 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
549 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
550 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
551 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
552 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
553 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
554 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
555 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
556 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
557 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
558 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
559 { NULL, { 0, 0, 0 }, 0 }
560 };
561 
562 /*
563  * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
564  * These aliases ensure that D definitions can use typical <sys/types.h> names.
565  */
566 static const dt_typedef_t _dtrace_typedefs_32[] = {
567 { "char", "int8_t" },
568 { "short", "int16_t" },
569 { "int", "int32_t" },
570 { "long long", "int64_t" },
571 { "int", "intptr_t" },
572 { "int", "ssize_t" },
573 { "unsigned char", "uint8_t" },
574 { "unsigned short", "uint16_t" },
575 { "unsigned", "uint32_t" },
576 { "unsigned long long", "uint64_t" },
577 { "unsigned char", "uchar_t" },
578 { "unsigned short", "ushort_t" },
579 { "unsigned", "uint_t" },
580 { "unsigned long", "ulong_t" },
581 { "unsigned long long", "u_longlong_t" },
582 { "int", "ptrdiff_t" },
583 { "unsigned", "uintptr_t" },
584 { "unsigned", "size_t" },
585 { "long", "id_t" },
586 { "long", "pid_t" },
587 { NULL, NULL }
588 };
589 
590 /*
591  * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
592  * These aliases ensure that D definitions can use typical <sys/types.h> names.
593  */
594 static const dt_typedef_t _dtrace_typedefs_64[] = {
595 { "char", "int8_t" },
596 { "short", "int16_t" },
597 { "int", "int32_t" },
598 { "long", "int64_t" },
599 { "long", "intptr_t" },
600 { "long", "ssize_t" },
601 { "unsigned char", "uint8_t" },
602 { "unsigned short", "uint16_t" },
603 { "unsigned", "uint32_t" },
604 { "unsigned long", "uint64_t" },
605 { "unsigned char", "uchar_t" },
606 { "unsigned short", "ushort_t" },
607 { "unsigned", "uint_t" },
608 { "unsigned long", "ulong_t" },
609 { "unsigned long long", "u_longlong_t" },
610 { "long", "ptrdiff_t" },
611 { "unsigned long", "uintptr_t" },
612 { "unsigned long", "size_t" },
613 { "int", "id_t" },
614 { "int", "pid_t" },
615 { NULL, NULL }
616 };
617 
618 /*
619  * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
620  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
621  */
622 static const dt_intdesc_t _dtrace_ints_32[] = {
623 { "int", NULL, CTF_ERR, 0x7fffffffULL },
624 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
625 { "long", NULL, CTF_ERR, 0x7fffffffULL },
626 { "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
627 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
628 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
629 };
630 
631 /*
632  * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
633  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
634  */
635 static const dt_intdesc_t _dtrace_ints_64[] = {
636 { "int", NULL, CTF_ERR, 0x7fffffffULL },
637 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
638 { "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
639 { "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
640 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
641 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
642 };
643 
644 /*
645  * Table of macro variable templates used to populate the macro identifier hash
646  * when a new dtrace client open occurs.  Values are set by dtrace_update().
647  */
648 static const dt_ident_t _dtrace_macros[] = {
649 { "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
650 { "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
651 { "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
652 { "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
653 { "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
654 { "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
655 { "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
656 { "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
657 { "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
658 { "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
659 { "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
660 { NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
661 };
662 
663 /*
664  * Hard-wired definition string to be compiled and cached every time a new
665  * DTrace library handle is initialized.  This string should only be used to
666  * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
667  */
668 static const char _dtrace_hardwire[] = "\
669 inline long NULL = 0; \n\
670 #pragma D binding \"1.0\" NULL\n\
671 ";
672 
673 /*
674  * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
675  * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
676  * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
677  * relying on the fact that when running dtrace(1M), isaexec will invoke the
678  * binary with the same bitness as the kernel, which is what we want by default
679  * when generating our DIF.  The user can override the choice using oflags.
680  */
681 static const dtrace_conf_t _dtrace_conf = {
682 	DIF_VERSION,		/* dtc_difversion */
683 	DIF_DIR_NREGS,		/* dtc_difintregs */
684 	DIF_DTR_NREGS,		/* dtc_diftupregs */
685 	CTF_MODEL_NATIVE	/* dtc_ctfmodel */
686 };
687 
688 const dtrace_attribute_t _dtrace_maxattr = {
689 	DTRACE_STABILITY_MAX,
690 	DTRACE_STABILITY_MAX,
691 	DTRACE_CLASS_MAX
692 };
693 
694 const dtrace_attribute_t _dtrace_defattr = {
695 	DTRACE_STABILITY_STABLE,
696 	DTRACE_STABILITY_STABLE,
697 	DTRACE_CLASS_COMMON
698 };
699 
700 const dtrace_attribute_t _dtrace_symattr = {
701 	DTRACE_STABILITY_PRIVATE,
702 	DTRACE_STABILITY_PRIVATE,
703 	DTRACE_CLASS_UNKNOWN
704 };
705 
706 const dtrace_attribute_t _dtrace_typattr = {
707 	DTRACE_STABILITY_PRIVATE,
708 	DTRACE_STABILITY_PRIVATE,
709 	DTRACE_CLASS_UNKNOWN
710 };
711 
712 const dtrace_attribute_t _dtrace_prvattr = {
713 	DTRACE_STABILITY_PRIVATE,
714 	DTRACE_STABILITY_PRIVATE,
715 	DTRACE_CLASS_UNKNOWN
716 };
717 
718 const dtrace_pattr_t _dtrace_prvdesc = {
719 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
720 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
721 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
722 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
723 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
724 };
725 
726 #if defined(sun)
727 const char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
728 const char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
729 #else
730 const char *_dtrace_defcpp = "cpp"; /* default cpp(1) to invoke */
731 const char *_dtrace_defld = "ld";   /* default ld(1) to invoke */
732 #endif
733 
734 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
735 #if defined(sun)
736 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
737 #else
738 const char *_dtrace_provdir = "/dev/dtrace"; /* provider directory */
739 #endif
740 
741 int _dtrace_strbuckets = 211;	/* default number of hash buckets (prime) */
742 int _dtrace_intbuckets = 256;	/* default number of integer buckets (Pof2) */
743 uint_t _dtrace_strsize = 256;	/* default size of string intrinsic type */
744 uint_t _dtrace_stkindent = 14;	/* default whitespace indent for stack/ustack */
745 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
746 uint_t _dtrace_pidlrulim = 8;	/* default number of pid handles to cache */
747 size_t _dtrace_bufsize = 512;	/* default dt_buf_create() size */
748 int _dtrace_argmax = 32;	/* default maximum number of probe arguments */
749 
750 int _dtrace_debug = 0;		/* debug messages enabled (off) */
751 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
752 #if defined(sun)
753 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
754 #endif
755 
756 typedef struct dt_fdlist {
757 	int *df_fds;		/* array of provider driver file descriptors */
758 	uint_t df_ents;		/* number of valid elements in df_fds[] */
759 	uint_t df_size;		/* size of df_fds[] */
760 } dt_fdlist_t;
761 
762 #if defined(sun)
763 #pragma init(_dtrace_init)
764 #else
765 void _dtrace_init(void) __attribute__ ((constructor));
766 #endif
767 void
768 _dtrace_init(void)
769 {
770 	_dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
771 
772 #if defined(sun)
773 	for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
774 		if (rd_init(_dtrace_rdvers) == RD_OK)
775 			break;
776 	}
777 #endif
778 #if defined(__i386__)
779 #if 0	/* XXX TBD - no NetBSD equivalent? */
780 	/* make long doubles 64 bits -sson */
781 	(void) fpsetprec(FP_PE);
782 #endif
783 #endif
784 }
785 
786 static dtrace_hdl_t *
787 set_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
788 {
789 	if (dtp != NULL)
790 		dtrace_close(dtp);
791 	if (errp != NULL)
792 		*errp = err;
793 	return (NULL);
794 }
795 
796 static void
797 dt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
798 {
799 	dt_provmod_t *prov;
800 	char path[PATH_MAX];
801 	int fd;
802 #if defined(sun)
803 	struct dirent *dp, *ep;
804 	DIR *dirp;
805 
806 	if ((dirp = opendir(_dtrace_provdir)) == NULL)
807 		return; /* failed to open directory; just skip it */
808 
809 	ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
810 	bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
811 
812 	while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
813 		if (dp->d_name[0] == '.')
814 			continue; /* skip "." and ".." */
815 
816 		if (dfp->df_ents == dfp->df_size) {
817 			uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
818 			int *fds = realloc(dfp->df_fds, size * sizeof (int));
819 
820 			if (fds == NULL)
821 				break; /* skip the rest of this directory */
822 
823 			dfp->df_fds = fds;
824 			dfp->df_size = size;
825 		}
826 
827 		(void) snprintf(path, sizeof (path), "%s/%s",
828 		    _dtrace_provdir, dp->d_name);
829 
830 		if ((fd = open(path, O_RDONLY)) == -1)
831 			continue; /* failed to open driver; just skip it */
832 
833 		if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
834 		    (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
835 			free(prov);
836 			(void) close(fd);
837 			break;
838 		}
839 
840 		(void) strcpy(prov->dp_name, dp->d_name);
841 		prov->dp_next = *provmod;
842 		*provmod = prov;
843 
844 		dt_dprintf("opened provider %s\n", dp->d_name);
845 		dfp->df_fds[dfp->df_ents++] = fd;
846 	}
847 
848 	(void) closedir(dirp);
849 #else
850 	char	*p;
851 	char	*p1;
852 	char	*p_providers = NULL;
853 	int	error;
854 	size_t	len = 0;
855 
856 	/*
857 	 * Loop to allocate/reallocate memory for the string of provider
858 	 * names and retry:
859 	 */
860 	while(1) {
861 		/*
862 		 * The first time around, get the string length. The next time,
863 		 * hopefully we've allocated enough memory.
864 		 */
865 		error = sysctlbyname("debug.dtrace.providers",p_providers,&len,NULL,0);
866 		if (len == 0)
867 			/* No providers? That's strange. Where's dtrace? */
868 			break;
869 		else if (error == 0 && p_providers == NULL) {
870 			/*
871 			 * Allocate the initial memory which should be enough
872 			 * unless another provider loads before we have
873 			 * time to go back and get the string.
874 			 */
875 			if ((p_providers = malloc(len)) == NULL)
876 				/* How do we report errors here? */
877 				return;
878 		} else if (error == -1 && errno == ENOMEM) {
879 			/*
880 			 * The current buffer isn't large enough, so
881 			 * reallocate it. We normally won't need to do this
882 			 * because providers aren't being loaded all the time.
883 			 */
884 			if ((p = realloc(p_providers,len)) == NULL)
885 				/* How do we report errors here? */
886 				return;
887 			p_providers = p;
888 		} else
889 			break;
890 	}
891 
892 	/* Check if we got a string of provider names: */
893 	if (error == 0 && len > 0 && p_providers != NULL) {
894 		p = p_providers;
895 
896 		/*
897 		 * Parse the string containing the space separated
898 		 * provider names.
899 		 */
900 		while ((p1 = strsep(&p," ")) != NULL) {
901 			if (dfp->df_ents == dfp->df_size) {
902 				uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
903 				int *fds = realloc(dfp->df_fds, size * sizeof (int));
904 
905 				if (fds == NULL)
906 					break;
907 
908 				dfp->df_fds = fds;
909 				dfp->df_size = size;
910 			}
911 
912 			(void) snprintf(path, sizeof (path), "/dev/dtrace/%s", p1);
913 
914 			if ((fd = open(path, O_RDONLY)) == -1)
915 				continue; /* failed to open driver; just skip it */
916 
917 			if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
918 			    (prov->dp_name = malloc(strlen(p1) + 1)) == NULL) {
919 				free(prov);
920 				(void) close(fd);
921 				break;
922 			}
923 
924 			(void) strcpy(prov->dp_name, p1);
925 			prov->dp_next = *provmod;
926 			*provmod = prov;
927 
928 			dt_dprintf("opened provider %s\n", p1);
929 			dfp->df_fds[dfp->df_ents++] = fd;
930 		}
931 	}
932 	if (p_providers != NULL)
933 		free(p_providers);
934 #endif
935 }
936 
937 static void
938 dt_provmod_destroy(dt_provmod_t **provmod)
939 {
940 	dt_provmod_t *next, *current;
941 
942 	for (current = *provmod; current != NULL; current = next) {
943 		next = current->dp_next;
944 		free(current->dp_name);
945 		free(current);
946 	}
947 
948 	*provmod = NULL;
949 }
950 
951 #if defined(sun)
952 static const char *
953 dt_get_sysinfo(int cmd, char *buf, size_t len)
954 {
955 	ssize_t rv = sysinfo(cmd, buf, len);
956 	char *p = buf;
957 
958 	if (rv < 0 || rv > len)
959 		(void) snprintf(buf, len, "%s", "Unknown");
960 
961 	while ((p = strchr(p, '.')) != NULL)
962 		*p++ = '_';
963 
964 	return (buf);
965 }
966 #endif
967 
968 static dtrace_hdl_t *
969 dt_vopen(int version, int flags, int *errp,
970     const dtrace_vector_t *vector, void *arg)
971 {
972 	dtrace_hdl_t *dtp = NULL;
973 	int dtfd = -1, ftfd = -1, fterr = 0;
974 	dtrace_prog_t *pgp;
975 	dt_module_t *dmp;
976 	dt_provmod_t *provmod = NULL;
977 	int i, err;
978 	struct rlimit rl;
979 
980 	const dt_intrinsic_t *dinp;
981 	const dt_typedef_t *dtyp;
982 	const dt_ident_t *idp;
983 
984 	dtrace_typeinfo_t dtt;
985 	ctf_funcinfo_t ctc;
986 	ctf_arinfo_t ctr;
987 
988 	dt_fdlist_t df = { NULL, 0, 0 };
989 
990 	char isadef[32], utsdef[32];
991 	char s1[64], s2[64];
992 
993 	if (version <= 0)
994 		return (set_open_errno(dtp, errp, EINVAL));
995 
996 	if (version > DTRACE_VERSION)
997 		return (set_open_errno(dtp, errp, EDT_VERSION));
998 
999 	if (version < DTRACE_VERSION) {
1000 		/*
1001 		 * Currently, increasing the library version number is used to
1002 		 * denote a binary incompatible change.  That is, a consumer
1003 		 * of the library cannot run on a version of the library with
1004 		 * a higher DTRACE_VERSION number than the consumer compiled
1005 		 * against.  Once the library API has been committed to,
1006 		 * backwards binary compatibility will be required; at that
1007 		 * time, this check should change to return EDT_OVERSION only
1008 		 * if the specified version number is less than the version
1009 		 * number at the time of interface commitment.
1010 		 */
1011 		return (set_open_errno(dtp, errp, EDT_OVERSION));
1012 	}
1013 
1014 	if (flags & ~DTRACE_O_MASK)
1015 		return (set_open_errno(dtp, errp, EINVAL));
1016 
1017 	if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
1018 		return (set_open_errno(dtp, errp, EINVAL));
1019 
1020 	if (vector == NULL && arg != NULL)
1021 		return (set_open_errno(dtp, errp, EINVAL));
1022 
1023 	if (elf_version(EV_CURRENT) == EV_NONE)
1024 		return (set_open_errno(dtp, errp, EDT_ELFVERSION));
1025 
1026 	if (vector != NULL || (flags & DTRACE_O_NODEV))
1027 		goto alloc; /* do not attempt to open dtrace device */
1028 
1029 	/*
1030 	 * Before we get going, crank our limit on file descriptors up to the
1031 	 * hard limit.  This is to allow for the fact that libproc keeps file
1032 	 * descriptors to objects open for the lifetime of the proc handle;
1033 	 * without raising our hard limit, we would have an acceptably small
1034 	 * bound on the number of processes that we could concurrently
1035 	 * instrument with the pid provider.
1036 	 */
1037 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1038 		rl.rlim_cur = rl.rlim_max;
1039 		(void) setrlimit(RLIMIT_NOFILE, &rl);
1040 	}
1041 
1042 	/*
1043 	 * Get the device path of each of the providers.  We hold them open
1044 	 * in the df.df_fds list until we open the DTrace driver itself,
1045 	 * allowing us to see all of the probes provided on this system.  Once
1046 	 * we have the DTrace driver open, we can safely close all the providers
1047 	 * now that they have registered with the framework.
1048 	 */
1049 	dt_provmod_open(&provmod, &df);
1050 
1051 	dtfd = open("/dev/dtrace/dtrace", O_RDWR);
1052 	err = errno; /* save errno from opening dtfd */
1053 
1054 #if defined(sun)
1055 	ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
1056 #else
1057 	ftfd = open("/dev/dtrace/fasttrap", O_RDWR);
1058 #endif
1059 	fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
1060 
1061 	while (df.df_ents-- != 0)
1062 		(void) close(df.df_fds[df.df_ents]);
1063 
1064 	free(df.df_fds);
1065 
1066 	/*
1067 	 * If we failed to open the dtrace device, fail dtrace_open().
1068 	 * We convert some kernel errnos to custom libdtrace errnos to
1069 	 * improve the resulting message from the usual strerror().
1070 	 */
1071 	if (dtfd == -1) {
1072 		dt_provmod_destroy(&provmod);
1073 		switch (err) {
1074 		case ENOENT:
1075 			err = EDT_NOENT;
1076 			break;
1077 		case EBUSY:
1078 			err = EDT_BUSY;
1079 			break;
1080 		case EACCES:
1081 			err = EDT_ACCESS;
1082 			break;
1083 		}
1084 		return (set_open_errno(dtp, errp, err));
1085 	}
1086 
1087 	(void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
1088 	(void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
1089 
1090 alloc:
1091 	if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
1092 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1093 
1094 	bzero(dtp, sizeof (dtrace_hdl_t));
1095 	dtp->dt_oflags = flags;
1096 	dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
1097 	dtp->dt_linkmode = DT_LINK_KERNEL;
1098 	dtp->dt_linktype = DT_LTYP_ELF;
1099 	dtp->dt_xlatemode = DT_XL_STATIC;
1100 	dtp->dt_stdcmode = DT_STDC_XA;
1101 	dtp->dt_version = version;
1102 	dtp->dt_fd = dtfd;
1103 	dtp->dt_ftfd = ftfd;
1104 	dtp->dt_fterr = fterr;
1105 	dtp->dt_cdefs_fd = -1;
1106 	dtp->dt_ddefs_fd = -1;
1107 #if defined(sun)
1108 	dtp->dt_stdout_fd = -1;
1109 #else
1110 	dtp->dt_freopen_fp = NULL;
1111 #endif
1112 	dtp->dt_modbuckets = _dtrace_strbuckets;
1113 	dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
1114 	dtp->dt_provbuckets = _dtrace_strbuckets;
1115 	dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
1116 	dt_proc_hash_create(dtp);
1117 	dtp->dt_vmax = DT_VERS_LATEST;
1118 	dtp->dt_cpp_path = strdup(_dtrace_defcpp);
1119 	dtp->dt_cpp_argv = malloc(sizeof (char *));
1120 	dtp->dt_cpp_argc = 1;
1121 	dtp->dt_cpp_args = 1;
1122 	dtp->dt_ld_path = strdup(_dtrace_defld);
1123 	dtp->dt_provmod = provmod;
1124 	dtp->dt_vector = vector;
1125 	dtp->dt_varg = arg;
1126 	dt_dof_init(dtp);
1127 	(void) uname(&dtp->dt_uts);
1128 
1129 	if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
1130 	    dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
1131 	    dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
1132 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1133 
1134 	for (i = 0; i < DTRACEOPT_MAX; i++)
1135 		dtp->dt_options[i] = DTRACEOPT_UNSET;
1136 
1137 	dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
1138 
1139 #if defined(sun)
1140 	(void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
1141 	    (uint_t)(sizeof (void *) * NBBY));
1142 
1143 	(void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
1144 	    dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
1145 	    dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
1146 
1147 	if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
1148 	    dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
1149 	    dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
1150 	    dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
1151 	    dt_cpp_add_arg(dtp, isadef) == NULL ||
1152 	    dt_cpp_add_arg(dtp, utsdef) == NULL)
1153 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1154 #endif
1155 
1156 	if (flags & DTRACE_O_NODEV)
1157 		bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
1158 	else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
1159 		return (set_open_errno(dtp, errp, errno));
1160 
1161 	if (flags & DTRACE_O_LP64)
1162 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
1163 	else if (flags & DTRACE_O_ILP32)
1164 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
1165 
1166 #ifdef __sparc
1167 	/*
1168 	 * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
1169 	 * and __sparcv9 is defined if we are doing a 64-bit compile.
1170 	 */
1171 	if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
1172 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1173 
1174 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
1175 	    dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
1176 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1177 #endif
1178 
1179 #if defined(sun)
1180 #ifdef __x86
1181 	/*
1182 	 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
1183 	 * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
1184 	 * they are defined exclusive of one another (see PSARC 2004/619).
1185 	 */
1186 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1187 		if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
1188 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1189 	} else {
1190 		if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
1191 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1192 	}
1193 #endif
1194 #else
1195 #if defined(__amd64__) || defined(__i386__)
1196 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1197 		if (dt_cpp_add_arg(dtp, "-m64") == NULL)
1198 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1199 	} else {
1200 		if (dt_cpp_add_arg(dtp, "-m32") == NULL)
1201 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1202 	}
1203 #endif
1204 #endif
1205 
1206 	if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1207 		return (set_open_errno(dtp, errp, EDT_DIFVERS));
1208 
1209 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1210 		bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1211 	else
1212 		bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1213 
1214 	/*
1215 	 * On FreeBSD the kernel module name can't be hard-coded. The
1216 	 * 'kern.bootfile' sysctl value tells us exactly which file is being
1217 	 * used as the kernel.
1218 	 */
1219 #if !defined(sun)
1220 	{
1221 	char bootfile[MAXPATHLEN];
1222 	char *p;
1223 	int i;
1224 	size_t len = sizeof(bootfile);
1225 
1226 #if 0	/* XXX debug */
1227 	/* This call shouldn't fail, but use a default just in case. */
1228 	if (sysctlbyname("kern.bootfile", bootfile, &len, NULL, 0) != 0)
1229 		strlcpy(bootfile, "kernel", sizeof(bootfile));
1230 
1231 	if ((p = strrchr(bootfile, '/')) != NULL)
1232 		p++;
1233 	else
1234 		p = bootfile;
1235 #else
1236 	p = "netbsd";
1237 #endif
1238 
1239 	/*
1240 	 * Format the global variables based on the kernel module name.
1241 	 */
1242 	snprintf(curthread_str, sizeof(curthread_str), "%s`struct lwp *",p);
1243 	snprintf(intmtx_str, sizeof(intmtx_str), "int(%s`struct kmutex *)",p);
1244 	snprintf(threadmtx_str, sizeof(threadmtx_str), "struct lwp *(%s`struct kmutex *)",p);
1245 	snprintf(rwlock_str, sizeof(rwlock_str), "int(%s`struct rwlock *)",p);
1246 	}
1247 #endif
1248 
1249 	dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1250 	dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1251 	    DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1252 
1253 	dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1254 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1255 
1256 	dtp->dt_tls = dt_idhash_create("thread local", NULL,
1257 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1258 
1259 	if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1260 	    dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1261 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1262 
1263 	/*
1264 	 * Populate the dt_macros identifier hash table by hand: we can't use
1265 	 * the dt_idhash_populate() mechanism because we're not yet compiling
1266 	 * and dtrace_update() needs to immediately reference these idents.
1267 	 */
1268 	for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1269 		if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1270 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1271 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1272 		    idp->di_iarg, 0) == NULL)
1273 			return (set_open_errno(dtp, errp, EDT_NOMEM));
1274 	}
1275 
1276 	/*
1277 	 * Update the module list using /system/object and load the values for
1278 	 * the macro variable definitions according to the current process.
1279 	 */
1280 	dtrace_update(dtp);
1281 
1282 	/*
1283 	 * Select the intrinsics and typedefs we want based on the data model.
1284 	 * The intrinsics are under "C".  The typedefs are added under "D".
1285 	 */
1286 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1287 		dinp = _dtrace_intrinsics_32;
1288 		dtyp = _dtrace_typedefs_32;
1289 	} else {
1290 		dinp = _dtrace_intrinsics_64;
1291 		dtyp = _dtrace_typedefs_64;
1292 	}
1293 
1294 	/*
1295 	 * Create a dynamic CTF container under the "C" scope for intrinsic
1296 	 * types and types defined in ANSI-C header files that are included.
1297 	 */
1298 	if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1299 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1300 
1301 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1302 		return (set_open_errno(dtp, errp, EDT_CTF));
1303 
1304 	dt_dprintf("created CTF container for %s (%p)\n",
1305 	    dmp->dm_name, (void *)dmp->dm_ctfp);
1306 
1307 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1308 	ctf_setspecific(dmp->dm_ctfp, dmp);
1309 
1310 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1311 	dmp->dm_modid = -1; /* no module ID */
1312 
1313 	/*
1314 	 * Fill the dynamic "C" CTF container with all of the intrinsic
1315 	 * integer and floating-point types appropriate for this data model.
1316 	 */
1317 	for (; dinp->din_name != NULL; dinp++) {
1318 		if (dinp->din_kind == CTF_K_INTEGER) {
1319 			err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1320 			    dinp->din_name, &dinp->din_data);
1321 		} else {
1322 			err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1323 			    dinp->din_name, &dinp->din_data);
1324 		}
1325 
1326 		if (err == CTF_ERR) {
1327 			dt_dprintf("failed to add %s to C container: %s\n",
1328 			    dinp->din_name, ctf_errmsg(
1329 			    ctf_errno(dmp->dm_ctfp)));
1330 			return (set_open_errno(dtp, errp, EDT_CTF));
1331 		}
1332 	}
1333 
1334 	if (ctf_update(dmp->dm_ctfp) != 0) {
1335 		dt_dprintf("failed to update C container: %s\n",
1336 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1337 		return (set_open_errno(dtp, errp, EDT_CTF));
1338 	}
1339 
1340 	/*
1341 	 * Add intrinsic pointer types that are needed to initialize printf
1342 	 * format dictionary types (see table in dt_printf.c).
1343 	 */
1344 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1345 	    ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1346 
1347 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1348 	    ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1349 
1350 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1351 	    ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1352 
1353 	if (ctf_update(dmp->dm_ctfp) != 0) {
1354 		dt_dprintf("failed to update C container: %s\n",
1355 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1356 		return (set_open_errno(dtp, errp, EDT_CTF));
1357 	}
1358 
1359 	/*
1360 	 * Create a dynamic CTF container under the "D" scope for types that
1361 	 * are defined by the D program itself or on-the-fly by the D compiler.
1362 	 * The "D" CTF container is a child of the "C" CTF container.
1363 	 */
1364 	if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1365 		return (set_open_errno(dtp, errp, EDT_NOMEM));
1366 
1367 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1368 		return (set_open_errno(dtp, errp, EDT_CTF));
1369 
1370 	dt_dprintf("created CTF container for %s (%p)\n",
1371 	    dmp->dm_name, (void *)dmp->dm_ctfp);
1372 
1373 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1374 	ctf_setspecific(dmp->dm_ctfp, dmp);
1375 
1376 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1377 	dmp->dm_modid = -1; /* no module ID */
1378 
1379 	if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1380 		dt_dprintf("failed to import D parent container: %s\n",
1381 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1382 		return (set_open_errno(dtp, errp, EDT_CTF));
1383 	}
1384 
1385 	/*
1386 	 * Fill the dynamic "D" CTF container with all of the built-in typedefs
1387 	 * that we need to use for our D variable and function definitions.
1388 	 * This ensures that basic inttypes.h names are always available to us.
1389 	 */
1390 	for (; dtyp->dty_src != NULL; dtyp++) {
1391 		if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1392 		    dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1393 		    dtyp->dty_src)) == CTF_ERR) {
1394 			dt_dprintf("failed to add typedef %s %s to D "
1395 			    "container: %s", dtyp->dty_src, dtyp->dty_dst,
1396 			    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1397 			return (set_open_errno(dtp, errp, EDT_CTF));
1398 		}
1399 	}
1400 
1401 	/*
1402 	 * Insert a CTF ID corresponding to a pointer to a type of kind
1403 	 * CTF_K_FUNCTION we can use in the compiler for function pointers.
1404 	 * CTF treats all function pointers as "int (*)()" so we only need one.
1405 	 */
1406 	ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1407 	ctc.ctc_argc = 0;
1408 	ctc.ctc_flags = 0;
1409 
1410 	dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
1411 	    CTF_ADD_ROOT, &ctc, NULL);
1412 
1413 	dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
1414 	    CTF_ADD_ROOT, dtp->dt_type_func);
1415 
1416 	/*
1417 	 * We also insert CTF definitions for the special D intrinsic types
1418 	 * string and <DYN> into the D container.  The string type is added
1419 	 * as a typedef of char[n].  The <DYN> type is an alias for void.
1420 	 * We compare types to these special CTF ids throughout the compiler.
1421 	 */
1422 	ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1423 	ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1424 	ctr.ctr_nelems = _dtrace_strsize;
1425 
1426 	dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1427 	    "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1428 
1429 	dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1430 	    "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1431 
1432 	dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1433 	    "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1434 
1435 	dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1436 	    "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1437 
1438 	dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1439 	    "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1440 
1441 	if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1442 	    dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1443 	    dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1444 	    dtp->dt_type_usymaddr == CTF_ERR) {
1445 		dt_dprintf("failed to add intrinsic to D container: %s\n",
1446 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1447 		return (set_open_errno(dtp, errp, EDT_CTF));
1448 	}
1449 
1450 	if (ctf_update(dmp->dm_ctfp) != 0) {
1451 		dt_dprintf("failed update D container: %s\n",
1452 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1453 		return (set_open_errno(dtp, errp, EDT_CTF));
1454 	}
1455 
1456 	/*
1457 	 * Initialize the integer description table used to convert integer
1458 	 * constants to the appropriate types.  Refer to the comments above
1459 	 * dt_node_int() for a complete description of how this table is used.
1460 	 */
1461 	for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1462 		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1463 		    dtp->dt_ints[i].did_name, &dtt) != 0) {
1464 			dt_dprintf("failed to lookup integer type %s: %s\n",
1465 			    dtp->dt_ints[i].did_name,
1466 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1467 			return (set_open_errno(dtp, errp, dtp->dt_errno));
1468 		}
1469 		dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1470 		dtp->dt_ints[i].did_type = dtt.dtt_type;
1471 	}
1472 
1473 	/*
1474 	 * Now that we've created the "C" and "D" containers, move them to the
1475 	 * start of the module list so that these types and symbols are found
1476 	 * first (for stability) when iterating through the module list.
1477 	 */
1478 	dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1479 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1480 
1481 	dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1482 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1483 
1484 	if (dt_pfdict_create(dtp) == -1)
1485 		return (set_open_errno(dtp, errp, dtp->dt_errno));
1486 
1487 	/*
1488 	 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1489 	 * because without /dev/dtrace open, we will not be able to load the
1490 	 * names and attributes of any providers or probes from the kernel.
1491 	 */
1492 	if (flags & DTRACE_O_NODEV)
1493 		dtp->dt_cflags |= DTRACE_C_ZDEFS;
1494 
1495 	/*
1496 	 * Load hard-wired inlines into the definition cache by calling the
1497 	 * compiler on the raw definition string defined above.
1498 	 */
1499 	if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1500 	    DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1501 		dt_dprintf("failed to load hard-wired definitions: %s\n",
1502 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1503 		return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1504 	}
1505 
1506 	dt_program_destroy(dtp, pgp);
1507 
1508 	/*
1509 	 * Set up the default DTrace library path.  Once set, the next call to
1510 	 * dt_compile() will compile all the libraries.  We intentionally defer
1511 	 * library processing to improve overhead for clients that don't ever
1512 	 * compile, and to provide better error reporting (because the full
1513 	 * reporting of compiler errors requires dtrace_open() to succeed).
1514 	 */
1515 	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1516 		return (set_open_errno(dtp, errp, dtp->dt_errno));
1517 
1518 	return (dtp);
1519 }
1520 
1521 dtrace_hdl_t *
1522 dtrace_open(int version, int flags, int *errp)
1523 {
1524 	return (dt_vopen(version, flags, errp, NULL, NULL));
1525 }
1526 
1527 dtrace_hdl_t *
1528 dtrace_vopen(int version, int flags, int *errp,
1529     const dtrace_vector_t *vector, void *arg)
1530 {
1531 	return (dt_vopen(version, flags, errp, vector, arg));
1532 }
1533 
1534 void
1535 dtrace_close(dtrace_hdl_t *dtp)
1536 {
1537 	dt_ident_t *idp, *ndp;
1538 	dt_module_t *dmp;
1539 	dt_provider_t *pvp;
1540 	dtrace_prog_t *pgp;
1541 	dt_xlator_t *dxp;
1542 	dt_dirpath_t *dirp;
1543 	int i;
1544 
1545 	if (dtp->dt_procs != NULL)
1546 		dt_proc_hash_destroy(dtp);
1547 
1548 	while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1549 		dt_program_destroy(dtp, pgp);
1550 
1551 	while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1552 		dt_xlator_destroy(dtp, dxp);
1553 
1554 	dt_free(dtp, dtp->dt_xlatormap);
1555 
1556 	for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1557 		ndp = idp->di_next;
1558 		dt_ident_destroy(idp);
1559 	}
1560 
1561 	if (dtp->dt_macros != NULL)
1562 		dt_idhash_destroy(dtp->dt_macros);
1563 	if (dtp->dt_aggs != NULL)
1564 		dt_idhash_destroy(dtp->dt_aggs);
1565 	if (dtp->dt_globals != NULL)
1566 		dt_idhash_destroy(dtp->dt_globals);
1567 	if (dtp->dt_tls != NULL)
1568 		dt_idhash_destroy(dtp->dt_tls);
1569 
1570 	while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1571 		dt_module_destroy(dtp, dmp);
1572 
1573 	while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1574 		dt_provider_destroy(dtp, pvp);
1575 
1576 	if (dtp->dt_fd != -1)
1577 		(void) close(dtp->dt_fd);
1578 	if (dtp->dt_ftfd != -1)
1579 		(void) close(dtp->dt_ftfd);
1580 	if (dtp->dt_cdefs_fd != -1)
1581 		(void) close(dtp->dt_cdefs_fd);
1582 	if (dtp->dt_ddefs_fd != -1)
1583 		(void) close(dtp->dt_ddefs_fd);
1584 #if defined(sun)
1585 	if (dtp->dt_stdout_fd != -1)
1586 		(void) close(dtp->dt_stdout_fd);
1587 #else
1588 	if (dtp->dt_freopen_fp != NULL)
1589 		(void) fclose(dtp->dt_freopen_fp);
1590 #endif
1591 
1592 	dt_epid_destroy(dtp);
1593 	dt_aggid_destroy(dtp);
1594 	dt_format_destroy(dtp);
1595 	dt_buffered_destroy(dtp);
1596 	dt_aggregate_destroy(dtp);
1597 	free(dtp->dt_buf.dtbd_data);
1598 	dt_pfdict_destroy(dtp);
1599 	dt_provmod_destroy(&dtp->dt_provmod);
1600 	dt_dof_fini(dtp);
1601 
1602 	for (i = 1; i < dtp->dt_cpp_argc; i++)
1603 		free(dtp->dt_cpp_argv[i]);
1604 
1605 	while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1606 		dt_list_delete(&dtp->dt_lib_path, dirp);
1607 		free(dirp->dir_path);
1608 		free(dirp);
1609 	}
1610 
1611 	free(dtp->dt_cpp_argv);
1612 	free(dtp->dt_cpp_path);
1613 	free(dtp->dt_ld_path);
1614 
1615 	free(dtp->dt_mods);
1616 	free(dtp->dt_provs);
1617 	free(dtp);
1618 }
1619 
1620 int
1621 dtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1622 {
1623 	dt_provmod_t *prov;
1624 	int i = 0;
1625 
1626 	for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1627 		if (i < nmods)
1628 			mods[i] = prov->dp_name;
1629 	}
1630 
1631 	return (i);
1632 }
1633 
1634 int
1635 dtrace_ctlfd(dtrace_hdl_t *dtp)
1636 {
1637 	return (dtp->dt_fd);
1638 }
1639