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