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