1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb
22178479Sjb/*
23210767Srpaulo * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24268578Srpaulo * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25250574Smarkj * Copyright (c) 2012 by Delphix. All rights reserved.
26178479Sjb */
27178479Sjb
28178479Sjb#include <sys/types.h>
29178570Sjb#if defined(sun)
30178479Sjb#include <sys/modctl.h>
31178479Sjb#include <sys/systeminfo.h>
32178570Sjb#endif
33178479Sjb#include <sys/resource.h>
34178479Sjb
35178479Sjb#include <libelf.h>
36178479Sjb#include <strings.h>
37178570Sjb#if defined(sun)
38178479Sjb#include <alloca.h>
39178570Sjb#endif
40178479Sjb#include <limits.h>
41178479Sjb#include <unistd.h>
42178479Sjb#include <stdlib.h>
43178479Sjb#include <stdio.h>
44178479Sjb#include <fcntl.h>
45178479Sjb#include <errno.h>
46178479Sjb#include <assert.h>
47178479Sjb
48178479Sjb#define	_POSIX_PTHREAD_SEMANTICS
49178479Sjb#include <dirent.h>
50178479Sjb#undef	_POSIX_PTHREAD_SEMANTICS
51178479Sjb
52178479Sjb#include <dt_impl.h>
53178479Sjb#include <dt_program.h>
54178479Sjb#include <dt_module.h>
55178479Sjb#include <dt_printf.h>
56178479Sjb#include <dt_string.h>
57178479Sjb#include <dt_provider.h>
58178570Sjb#if !defined(sun)
59178570Sjb#include <sys/sysctl.h>
60178570Sjb#include <string.h>
61178570Sjb#endif
62178570Sjb#if defined(__i386__)
63178570Sjb#include <ieeefp.h>
64178570Sjb#endif
65178479Sjb
66178479Sjb/*
67178479Sjb * Stability and versioning definitions.  These #defines are used in the tables
68178479Sjb * of identifiers below to fill in the attribute and version fields associated
69178479Sjb * with each identifier.  The DT_ATTR_* macros are a convenience to permit more
70178479Sjb * concise declarations of common attributes such as Stable/Stable/Common.  The
71178479Sjb * DT_VERS_* macros declare the encoded integer values of all versions used so
72178479Sjb * far.  DT_VERS_LATEST must correspond to the latest version value among all
73178479Sjb * versions exported by the D compiler.  DT_VERS_STRING must be an ASCII string
74178479Sjb * that contains DT_VERS_LATEST within it along with any suffixes (e.g. Beta).
75178479Sjb * You must update DT_VERS_LATEST and DT_VERS_STRING when adding a new version,
76178479Sjb * and then add the new version to the _dtrace_versions[] array declared below.
77178479Sjb * Refer to the Solaris Dynamic Tracing Guide Stability and Versioning chapters
78178479Sjb * respectively for an explanation of these DTrace features and their values.
79178479Sjb *
80178479Sjb * NOTE: Although the DTrace versioning scheme supports the labeling and
81178479Sjb *       introduction of incompatible changes (e.g. dropping an interface in a
82178479Sjb *       major release), the libdtrace code does not currently support this.
83178479Sjb *       All versions are assumed to strictly inherit from one another.  If
84178479Sjb *       we ever need to provide divergent interfaces, this will need work.
85178479Sjb */
86178479Sjb#define	DT_ATTR_STABCMN	{ DTRACE_STABILITY_STABLE, \
87178479Sjb	DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }
88178479Sjb
89178479Sjb#define	DT_ATTR_EVOLCMN { DTRACE_STABILITY_EVOLVING, \
90178479Sjb	DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON \
91178479Sjb}
92178479Sjb
93178479Sjb/*
94178479Sjb * The version number should be increased for every customer visible release
95250574Smarkj * of DTrace. The major number should be incremented when a fundamental
96178479Sjb * change has been made that would affect all consumers, and would reflect
97178479Sjb * sweeping changes to DTrace or the D language. The minor number should be
98178479Sjb * incremented when a change is introduced that could break scripts that had
99178479Sjb * previously worked; for example, adding a new built-in variable could break
100178479Sjb * a script which was already using that identifier. The micro number should
101178479Sjb * be changed when introducing functionality changes or major bug fixes that
102178479Sjb * do not affect backward compatibility -- this is merely to make capabilities
103178479Sjb * easily determined from the version number. Minor bugs do not require any
104178479Sjb * modification to the version number.
105178479Sjb */
106178479Sjb#define	DT_VERS_1_0	DT_VERSION_NUMBER(1, 0, 0)
107178479Sjb#define	DT_VERS_1_1	DT_VERSION_NUMBER(1, 1, 0)
108178479Sjb#define	DT_VERS_1_2	DT_VERSION_NUMBER(1, 2, 0)
109178479Sjb#define	DT_VERS_1_2_1	DT_VERSION_NUMBER(1, 2, 1)
110178479Sjb#define	DT_VERS_1_2_2	DT_VERSION_NUMBER(1, 2, 2)
111178479Sjb#define	DT_VERS_1_3	DT_VERSION_NUMBER(1, 3, 0)
112178479Sjb#define	DT_VERS_1_4	DT_VERSION_NUMBER(1, 4, 0)
113178479Sjb#define	DT_VERS_1_4_1	DT_VERSION_NUMBER(1, 4, 1)
114178479Sjb#define	DT_VERS_1_5	DT_VERSION_NUMBER(1, 5, 0)
115178479Sjb#define	DT_VERS_1_6	DT_VERSION_NUMBER(1, 6, 0)
116178570Sjb#define	DT_VERS_1_6_1	DT_VERSION_NUMBER(1, 6, 1)
117184696Srodrigc#define	DT_VERS_1_6_2	DT_VERSION_NUMBER(1, 6, 2)
118210767Srpaulo#define	DT_VERS_1_6_3	DT_VERSION_NUMBER(1, 6, 3)
119237624Spfg#define	DT_VERS_1_7	DT_VERSION_NUMBER(1, 7, 0)
120248690Spfg#define	DT_VERS_1_7_1	DT_VERSION_NUMBER(1, 7, 1)
121248706Spfg#define	DT_VERS_1_8	DT_VERSION_NUMBER(1, 8, 0)
122248706Spfg#define	DT_VERS_1_8_1	DT_VERSION_NUMBER(1, 8, 1)
123248708Spfg#define	DT_VERS_1_9	DT_VERSION_NUMBER(1, 9, 0)
124250574Smarkj#define	DT_VERS_1_9_1	DT_VERSION_NUMBER(1, 9, 1)
125268578Srpaulo#define	DT_VERS_1_10	DT_VERSION_NUMBER(1, 10, 0)
126268578Srpaulo#define	DT_VERS_1_11	DT_VERSION_NUMBER(1, 11, 0)
127268578Srpaulo#define	DT_VERS_1_12	DT_VERSION_NUMBER(1, 12, 0)
128268578Srpaulo#define	DT_VERS_1_12_1	DT_VERSION_NUMBER(1, 12, 1)
129268578Srpaulo#define	DT_VERS_LATEST	DT_VERS_1_12_1
130268578Srpaulo#define	DT_VERS_STRING	"Sun D 1.12.1"
131178479Sjb
132178479Sjbconst dt_version_t _dtrace_versions[] = {
133178479Sjb	DT_VERS_1_0,	/* D API 1.0.0 (PSARC 2001/466) Solaris 10 FCS */
134178479Sjb	DT_VERS_1_1,	/* D API 1.1.0 Solaris Express 6/05 */
135178479Sjb	DT_VERS_1_2,	/* D API 1.2.0 Solaris 10 Update 1 */
136178479Sjb	DT_VERS_1_2_1,	/* D API 1.2.1 Solaris Express 4/06 */
137178479Sjb	DT_VERS_1_2_2,	/* D API 1.2.2 Solaris Express 6/06 */
138178479Sjb	DT_VERS_1_3,	/* D API 1.3 Solaris Express 10/06 */
139178479Sjb	DT_VERS_1_4,	/* D API 1.4 Solaris Express 2/07 */
140178479Sjb	DT_VERS_1_4_1,	/* D API 1.4.1 Solaris Express 4/07 */
141178479Sjb	DT_VERS_1_5,	/* D API 1.5 Solaris Express 7/07 */
142178479Sjb	DT_VERS_1_6,	/* D API 1.6 */
143178570Sjb	DT_VERS_1_6_1,	/* D API 1.6.1 */
144184696Srodrigc	DT_VERS_1_6_2,	/* D API 1.6.2 */
145210767Srpaulo	DT_VERS_1_6_3,	/* D API 1.6.3 */
146237624Spfg	DT_VERS_1_7,	/* D API 1.7 */
147248690Spfg	DT_VERS_1_7_1,	/* D API 1.7.1 */
148248706Spfg	DT_VERS_1_8,	/* D API 1.8 */
149248706Spfg	DT_VERS_1_8_1,	/* D API 1.8.1 */
150248708Spfg	DT_VERS_1_9,	/* D API 1.9 */
151250574Smarkj	DT_VERS_1_9_1,	/* D API 1.9.1 */
152268578Srpaulo	DT_VERS_1_10,	/* D API 1.10 */
153268578Srpaulo	DT_VERS_1_11,	/* D API 1.11 */
154268578Srpaulo	DT_VERS_1_12,	/* D API 1.12 */
155268578Srpaulo	DT_VERS_1_12_1,	/* D API 1.12.1 */
156178479Sjb	0
157178479Sjb};
158178479Sjb
159178479Sjb/*
160178570Sjb * Global variables that are formatted on FreeBSD based on the kernel file name.
161178570Sjb */
162178570Sjb#if !defined(sun)
163178570Sjbstatic char	curthread_str[MAXPATHLEN];
164178570Sjbstatic char	intmtx_str[MAXPATHLEN];
165178570Sjbstatic char	threadmtx_str[MAXPATHLEN];
166178570Sjbstatic char	rwlock_str[MAXPATHLEN];
167178570Sjbstatic char	sxlock_str[MAXPATHLEN];
168178570Sjb#endif
169178570Sjb
170178570Sjb/*
171178479Sjb * Table of global identifiers.  This is used to populate the global identifier
172178479Sjb * hash when a new dtrace client open occurs.  For more info see dt_ident.h.
173178479Sjb * The global identifiers that represent functions use the dt_idops_func ops
174178479Sjb * and specify the private data pointer as a prototype string which is parsed
175178479Sjb * when the identifier is first encountered.  These prototypes look like ANSI
176178479Sjb * C function prototypes except that the special symbol "@" can be used as a
177178479Sjb * wildcard to represent a single parameter of any type (i.e. any dt_node_t).
178178479Sjb * The standard "..." notation can also be used to represent varargs.  An empty
179178479Sjb * parameter list is taken to mean void (that is, no arguments are permitted).
180178479Sjb * A parameter enclosed in square brackets (e.g. "[int]") denotes an optional
181178479Sjb * argument.
182178479Sjb */
183178479Sjbstatic const dt_ident_t _dtrace_globals[] = {
184178479Sjb{ "alloca", DT_IDENT_FUNC, 0, DIF_SUBR_ALLOCA, DT_ATTR_STABCMN, DT_VERS_1_0,
185178479Sjb	&dt_idops_func, "void *(size_t)" },
186178479Sjb{ "arg0", DT_IDENT_SCALAR, 0, DIF_VAR_ARG0, DT_ATTR_STABCMN, DT_VERS_1_0,
187178479Sjb	&dt_idops_type, "int64_t" },
188178479Sjb{ "arg1", DT_IDENT_SCALAR, 0, DIF_VAR_ARG1, DT_ATTR_STABCMN, DT_VERS_1_0,
189178479Sjb	&dt_idops_type, "int64_t" },
190178479Sjb{ "arg2", DT_IDENT_SCALAR, 0, DIF_VAR_ARG2, DT_ATTR_STABCMN, DT_VERS_1_0,
191178479Sjb	&dt_idops_type, "int64_t" },
192178479Sjb{ "arg3", DT_IDENT_SCALAR, 0, DIF_VAR_ARG3, DT_ATTR_STABCMN, DT_VERS_1_0,
193178479Sjb	&dt_idops_type, "int64_t" },
194178479Sjb{ "arg4", DT_IDENT_SCALAR, 0, DIF_VAR_ARG4, DT_ATTR_STABCMN, DT_VERS_1_0,
195178479Sjb	&dt_idops_type, "int64_t" },
196178479Sjb{ "arg5", DT_IDENT_SCALAR, 0, DIF_VAR_ARG5, DT_ATTR_STABCMN, DT_VERS_1_0,
197178479Sjb	&dt_idops_type, "int64_t" },
198178479Sjb{ "arg6", DT_IDENT_SCALAR, 0, DIF_VAR_ARG6, DT_ATTR_STABCMN, DT_VERS_1_0,
199178479Sjb	&dt_idops_type, "int64_t" },
200178479Sjb{ "arg7", DT_IDENT_SCALAR, 0, DIF_VAR_ARG7, DT_ATTR_STABCMN, DT_VERS_1_0,
201178479Sjb	&dt_idops_type, "int64_t" },
202178479Sjb{ "arg8", DT_IDENT_SCALAR, 0, DIF_VAR_ARG8, DT_ATTR_STABCMN, DT_VERS_1_0,
203178479Sjb	&dt_idops_type, "int64_t" },
204178479Sjb{ "arg9", DT_IDENT_SCALAR, 0, DIF_VAR_ARG9, DT_ATTR_STABCMN, DT_VERS_1_0,
205178479Sjb	&dt_idops_type, "int64_t" },
206178479Sjb{ "args", DT_IDENT_ARRAY, 0, DIF_VAR_ARGS, DT_ATTR_STABCMN, DT_VERS_1_0,
207178479Sjb	&dt_idops_args, NULL },
208178479Sjb{ "avg", DT_IDENT_AGGFUNC, 0, DTRACEAGG_AVG, DT_ATTR_STABCMN, DT_VERS_1_0,
209178479Sjb	&dt_idops_func, "void(@)" },
210178479Sjb{ "basename", DT_IDENT_FUNC, 0, DIF_SUBR_BASENAME, DT_ATTR_STABCMN, DT_VERS_1_0,
211178479Sjb	&dt_idops_func, "string(const char *)" },
212178479Sjb{ "bcopy", DT_IDENT_FUNC, 0, DIF_SUBR_BCOPY, DT_ATTR_STABCMN, DT_VERS_1_0,
213178479Sjb	&dt_idops_func, "void(void *, void *, size_t)" },
214178479Sjb{ "breakpoint", DT_IDENT_ACTFUNC, 0, DT_ACT_BREAKPOINT,
215178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
216178479Sjb	&dt_idops_func, "void()" },
217178479Sjb{ "caller", DT_IDENT_SCALAR, 0, DIF_VAR_CALLER, DT_ATTR_STABCMN, DT_VERS_1_0,
218178479Sjb	&dt_idops_type, "uintptr_t" },
219178479Sjb{ "chill", DT_IDENT_ACTFUNC, 0, DT_ACT_CHILL, DT_ATTR_STABCMN, DT_VERS_1_0,
220178479Sjb	&dt_idops_func, "void(int)" },
221178479Sjb{ "cleanpath", DT_IDENT_FUNC, 0, DIF_SUBR_CLEANPATH, DT_ATTR_STABCMN,
222178479Sjb	DT_VERS_1_0, &dt_idops_func, "string(const char *)" },
223178479Sjb{ "clear", DT_IDENT_ACTFUNC, 0, DT_ACT_CLEAR, DT_ATTR_STABCMN, DT_VERS_1_0,
224178479Sjb	&dt_idops_func, "void(...)" },
225178479Sjb{ "commit", DT_IDENT_ACTFUNC, 0, DT_ACT_COMMIT, DT_ATTR_STABCMN, DT_VERS_1_0,
226178479Sjb	&dt_idops_func, "void(int)" },
227178479Sjb{ "copyin", DT_IDENT_FUNC, 0, DIF_SUBR_COPYIN, DT_ATTR_STABCMN, DT_VERS_1_0,
228178479Sjb	&dt_idops_func, "void *(uintptr_t, size_t)" },
229178479Sjb{ "copyinstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINSTR,
230178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
231178479Sjb	&dt_idops_func, "string(uintptr_t, [size_t])" },
232178479Sjb{ "copyinto", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINTO, DT_ATTR_STABCMN,
233178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(uintptr_t, size_t, void *)" },
234178479Sjb{ "copyout", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUT, DT_ATTR_STABCMN, DT_VERS_1_0,
235178479Sjb	&dt_idops_func, "void(void *, uintptr_t, size_t)" },
236178479Sjb{ "copyoutstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUTSTR,
237178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
238178479Sjb	&dt_idops_func, "void(char *, uintptr_t, size_t)" },
239178479Sjb{ "count", DT_IDENT_AGGFUNC, 0, DTRACEAGG_COUNT, DT_ATTR_STABCMN, DT_VERS_1_0,
240178479Sjb	&dt_idops_func, "void()" },
241178479Sjb{ "curthread", DT_IDENT_SCALAR, 0, DIF_VAR_CURTHREAD,
242178479Sjb	{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_PRIVATE,
243178479Sjb	DTRACE_CLASS_COMMON }, DT_VERS_1_0,
244178570Sjb#if defined(sun)
245178479Sjb	&dt_idops_type, "genunix`kthread_t *" },
246178570Sjb#else
247178570Sjb	&dt_idops_type, curthread_str },
248178570Sjb#endif
249178479Sjb{ "ddi_pathname", DT_IDENT_FUNC, 0, DIF_SUBR_DDI_PATHNAME,
250178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
251178479Sjb	&dt_idops_func, "string(void *, int64_t)" },
252178479Sjb{ "denormalize", DT_IDENT_ACTFUNC, 0, DT_ACT_DENORMALIZE, DT_ATTR_STABCMN,
253178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
254178479Sjb{ "dirname", DT_IDENT_FUNC, 0, DIF_SUBR_DIRNAME, DT_ATTR_STABCMN, DT_VERS_1_0,
255178479Sjb	&dt_idops_func, "string(const char *)" },
256178479Sjb{ "discard", DT_IDENT_ACTFUNC, 0, DT_ACT_DISCARD, DT_ATTR_STABCMN, DT_VERS_1_0,
257178479Sjb	&dt_idops_func, "void(int)" },
258178479Sjb{ "epid", DT_IDENT_SCALAR, 0, DIF_VAR_EPID, DT_ATTR_STABCMN, DT_VERS_1_0,
259178479Sjb	&dt_idops_type, "uint_t" },
260178479Sjb{ "errno", DT_IDENT_SCALAR, 0, DIF_VAR_ERRNO, DT_ATTR_STABCMN, DT_VERS_1_0,
261178479Sjb	&dt_idops_type, "int" },
262178570Sjb{ "execargs", DT_IDENT_SCALAR, 0, DIF_VAR_EXECARGS,
263178570Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
264178479Sjb{ "execname", DT_IDENT_SCALAR, 0, DIF_VAR_EXECNAME,
265178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
266178479Sjb{ "exit", DT_IDENT_ACTFUNC, 0, DT_ACT_EXIT, DT_ATTR_STABCMN, DT_VERS_1_0,
267178479Sjb	&dt_idops_func, "void(int)" },
268178479Sjb{ "freopen", DT_IDENT_ACTFUNC, 0, DT_ACT_FREOPEN, DT_ATTR_STABCMN,
269178479Sjb	DT_VERS_1_1, &dt_idops_func, "void(@, ...)" },
270178479Sjb{ "ftruncate", DT_IDENT_ACTFUNC, 0, DT_ACT_FTRUNCATE, DT_ATTR_STABCMN,
271178479Sjb	DT_VERS_1_0, &dt_idops_func, "void()" },
272178479Sjb{ "func", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
273178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
274178479Sjb{ "getmajor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMAJOR,
275178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
276178479Sjb	&dt_idops_func, "genunix`major_t(genunix`dev_t)" },
277178479Sjb{ "getminor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMINOR,
278178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
279178479Sjb	&dt_idops_func, "genunix`minor_t(genunix`dev_t)" },
280178479Sjb{ "htonl", DT_IDENT_FUNC, 0, DIF_SUBR_HTONL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
281178479Sjb	&dt_idops_func, "uint32_t(uint32_t)" },
282178479Sjb{ "htonll", DT_IDENT_FUNC, 0, DIF_SUBR_HTONLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
283178479Sjb	&dt_idops_func, "uint64_t(uint64_t)" },
284178479Sjb{ "htons", DT_IDENT_FUNC, 0, DIF_SUBR_HTONS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
285178479Sjb	&dt_idops_func, "uint16_t(uint16_t)" },
286268578Srpaulo{ "getf", DT_IDENT_FUNC, 0, DIF_SUBR_GETF, DT_ATTR_STABCMN, DT_VERS_1_10,
287268578Srpaulo	&dt_idops_func, "file_t *(int)" },
288178479Sjb{ "gid", DT_IDENT_SCALAR, 0, DIF_VAR_GID, DT_ATTR_STABCMN, DT_VERS_1_0,
289178479Sjb	&dt_idops_type, "gid_t" },
290178479Sjb{ "id", DT_IDENT_SCALAR, 0, DIF_VAR_ID, DT_ATTR_STABCMN, DT_VERS_1_0,
291178479Sjb	&dt_idops_type, "uint_t" },
292178479Sjb{ "index", DT_IDENT_FUNC, 0, DIF_SUBR_INDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
293178479Sjb	&dt_idops_func, "int(const char *, const char *, [int])" },
294178479Sjb{ "inet_ntoa", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA, DT_ATTR_STABCMN,
295178570Sjb#if defined(sun)
296178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(ipaddr_t *)" },
297178570Sjb#else
298178570Sjb	DT_VERS_1_5, &dt_idops_func, "string(in_addr_t *)" },
299178570Sjb#endif
300178479Sjb{ "inet_ntoa6", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA6, DT_ATTR_STABCMN,
301178570Sjb#if defined(sun)
302178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(in6_addr_t *)" },
303178570Sjb#else
304178570Sjb	DT_VERS_1_5, &dt_idops_func, "string(struct in6_addr *)" },
305178570Sjb#endif
306178479Sjb{ "inet_ntop", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOP, DT_ATTR_STABCMN,
307178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(int, void *)" },
308178479Sjb{ "ipl", DT_IDENT_SCALAR, 0, DIF_VAR_IPL, DT_ATTR_STABCMN, DT_VERS_1_0,
309178479Sjb	&dt_idops_type, "uint_t" },
310268578Srpaulo{ "json", DT_IDENT_FUNC, 0, DIF_SUBR_JSON, DT_ATTR_STABCMN, DT_VERS_1_11,
311268578Srpaulo	&dt_idops_func, "string(const char *, const char *)" },
312178479Sjb{ "jstack", DT_IDENT_ACTFUNC, 0, DT_ACT_JSTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
313178479Sjb	&dt_idops_func, "stack(...)" },
314178479Sjb{ "lltostr", DT_IDENT_FUNC, 0, DIF_SUBR_LLTOSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
315248706Spfg	&dt_idops_func, "string(int64_t, [int])" },
316237624Spfg{ "llquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LLQUANTIZE, DT_ATTR_STABCMN,
317237624Spfg	DT_VERS_1_7, &dt_idops_func,
318237624Spfg	"void(@, int32_t, int32_t, int32_t, int32_t, ...)" },
319178479Sjb{ "lquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LQUANTIZE,
320178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
321178479Sjb	&dt_idops_func, "void(@, int32_t, int32_t, ...)" },
322178479Sjb{ "max", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MAX, DT_ATTR_STABCMN, DT_VERS_1_0,
323178479Sjb	&dt_idops_func, "void(@)" },
324178570Sjb{ "memref", DT_IDENT_FUNC, 0, DIF_SUBR_MEMREF, DT_ATTR_STABCMN, DT_VERS_1_1,
325178570Sjb	&dt_idops_func, "uintptr_t *(void *, size_t)" },
326269520Smarkj#if !defined(sun)
327269520Smarkj{ "memstr", DT_IDENT_FUNC, 0, DIF_SUBR_MEMSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
328269520Smarkj	&dt_idops_func, "string(void *, char, size_t)" },
329269520Smarkj#endif
330178479Sjb{ "min", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MIN, DT_ATTR_STABCMN, DT_VERS_1_0,
331178479Sjb	&dt_idops_func, "void(@)" },
332178479Sjb{ "mod", DT_IDENT_ACTFUNC, 0, DT_ACT_MOD, DT_ATTR_STABCMN,
333178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
334178479Sjb{ "msgdsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGDSIZE,
335178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
336178479Sjb	&dt_idops_func, "size_t(mblk_t *)" },
337178479Sjb{ "msgsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGSIZE,
338178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
339178479Sjb	&dt_idops_func, "size_t(mblk_t *)" },
340178570Sjb#if defined(sun)
341178479Sjb{ "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
342178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
343178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
344178479Sjb{ "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
345178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
346178479Sjb	&dt_idops_func, "genunix`kthread_t *(genunix`kmutex_t *)" },
347178479Sjb{ "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
348178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
349178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
350178479Sjb{ "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
351178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
352178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
353178570Sjb#else
354178570Sjb{ "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
355178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
356178570Sjb	&dt_idops_func, intmtx_str },
357178570Sjb{ "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
358178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
359178570Sjb	&dt_idops_func, threadmtx_str },
360178570Sjb{ "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
361178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
362178570Sjb	&dt_idops_func, intmtx_str },
363178570Sjb{ "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
364178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
365178570Sjb	&dt_idops_func, intmtx_str },
366178570Sjb#endif
367178479Sjb{ "ntohl", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
368178479Sjb	&dt_idops_func, "uint32_t(uint32_t)" },
369178479Sjb{ "ntohll", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
370178479Sjb	&dt_idops_func, "uint64_t(uint64_t)" },
371178479Sjb{ "ntohs", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
372178479Sjb	&dt_idops_func, "uint16_t(uint16_t)" },
373178479Sjb{ "normalize", DT_IDENT_ACTFUNC, 0, DT_ACT_NORMALIZE, DT_ATTR_STABCMN,
374178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
375178479Sjb{ "panic", DT_IDENT_ACTFUNC, 0, DT_ACT_PANIC, DT_ATTR_STABCMN, DT_VERS_1_0,
376178479Sjb	&dt_idops_func, "void()" },
377178479Sjb{ "pid", DT_IDENT_SCALAR, 0, DIF_VAR_PID, DT_ATTR_STABCMN, DT_VERS_1_0,
378178479Sjb	&dt_idops_type, "pid_t" },
379178479Sjb{ "ppid", DT_IDENT_SCALAR, 0, DIF_VAR_PPID, DT_ATTR_STABCMN, DT_VERS_1_0,
380178479Sjb	&dt_idops_type, "pid_t" },
381248708Spfg{ "print", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINT, DT_ATTR_STABCMN, DT_VERS_1_9,
382248708Spfg	&dt_idops_func, "void(@)" },
383178479Sjb{ "printa", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTA, DT_ATTR_STABCMN, DT_VERS_1_0,
384178479Sjb	&dt_idops_func, "void(@, ...)" },
385178479Sjb{ "printf", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTF, DT_ATTR_STABCMN, DT_VERS_1_0,
386178479Sjb	&dt_idops_func, "void(@, ...)" },
387178570Sjb{ "printm", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTM, DT_ATTR_STABCMN, DT_VERS_1_0,
388178570Sjb	&dt_idops_func, "void(size_t, uintptr_t *)" },
389178570Sjb{ "printt", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTT, DT_ATTR_STABCMN, DT_VERS_1_0,
390178570Sjb	&dt_idops_func, "void(size_t, uintptr_t *)" },
391178479Sjb{ "probefunc", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEFUNC,
392178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
393178479Sjb{ "probemod", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEMOD,
394178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
395178479Sjb{ "probename", DT_IDENT_SCALAR, 0, DIF_VAR_PROBENAME,
396178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
397178479Sjb{ "probeprov", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEPROV,
398178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
399178479Sjb{ "progenyof", DT_IDENT_FUNC, 0, DIF_SUBR_PROGENYOF,
400178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
401178479Sjb	&dt_idops_func, "int(pid_t)" },
402178479Sjb{ "quantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_QUANTIZE,
403178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
404178479Sjb	&dt_idops_func, "void(@, ...)" },
405178479Sjb{ "raise", DT_IDENT_ACTFUNC, 0, DT_ACT_RAISE, DT_ATTR_STABCMN, DT_VERS_1_0,
406178479Sjb	&dt_idops_func, "void(int)" },
407178479Sjb{ "rand", DT_IDENT_FUNC, 0, DIF_SUBR_RAND, DT_ATTR_STABCMN, DT_VERS_1_0,
408178479Sjb	&dt_idops_func, "int()" },
409178479Sjb{ "rindex", DT_IDENT_FUNC, 0, DIF_SUBR_RINDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
410178479Sjb	&dt_idops_func, "int(const char *, const char *, [int])" },
411178570Sjb#if defined(sun)
412178479Sjb{ "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
413178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
414178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
415178479Sjb{ "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
416178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
417178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
418178479Sjb{ "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
419178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
420178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
421178570Sjb#else
422178570Sjb{ "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
423178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
424178570Sjb	&dt_idops_func, rwlock_str },
425178570Sjb{ "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
426178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
427178570Sjb	&dt_idops_func, rwlock_str },
428178570Sjb{ "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
429178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
430178570Sjb	&dt_idops_func, rwlock_str },
431178570Sjb#endif
432178479Sjb{ "self", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
433178479Sjb	&dt_idops_type, "void" },
434178479Sjb{ "setopt", DT_IDENT_ACTFUNC, 0, DT_ACT_SETOPT, DT_ATTR_STABCMN,
435178479Sjb	DT_VERS_1_2, &dt_idops_func, "void(const char *, [const char *])" },
436178479Sjb{ "speculate", DT_IDENT_ACTFUNC, 0, DT_ACT_SPECULATE,
437178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
438178479Sjb	&dt_idops_func, "void(int)" },
439178479Sjb{ "speculation", DT_IDENT_FUNC, 0, DIF_SUBR_SPECULATION,
440178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
441178479Sjb	&dt_idops_func, "int()" },
442178479Sjb{ "stack", DT_IDENT_ACTFUNC, 0, DT_ACT_STACK, DT_ATTR_STABCMN, DT_VERS_1_0,
443178479Sjb	&dt_idops_func, "stack(...)" },
444178479Sjb{ "stackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_STACKDEPTH,
445178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
446178479Sjb	&dt_idops_type, "uint32_t" },
447178479Sjb{ "stddev", DT_IDENT_AGGFUNC, 0, DTRACEAGG_STDDEV, DT_ATTR_STABCMN,
448178479Sjb	DT_VERS_1_6, &dt_idops_func, "void(@)" },
449178479Sjb{ "stop", DT_IDENT_ACTFUNC, 0, DT_ACT_STOP, DT_ATTR_STABCMN, DT_VERS_1_0,
450178479Sjb	&dt_idops_func, "void()" },
451178479Sjb{ "strchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
452178479Sjb	&dt_idops_func, "string(const char *, char)" },
453178479Sjb{ "strlen", DT_IDENT_FUNC, 0, DIF_SUBR_STRLEN, DT_ATTR_STABCMN, DT_VERS_1_0,
454178479Sjb	&dt_idops_func, "size_t(const char *)" },
455178479Sjb{ "strjoin", DT_IDENT_FUNC, 0, DIF_SUBR_STRJOIN, DT_ATTR_STABCMN, DT_VERS_1_0,
456178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
457178479Sjb{ "strrchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
458178479Sjb	&dt_idops_func, "string(const char *, char)" },
459178479Sjb{ "strstr", DT_IDENT_FUNC, 0, DIF_SUBR_STRSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
460178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
461178479Sjb{ "strtok", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOK, DT_ATTR_STABCMN, DT_VERS_1_1,
462178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
463268578Srpaulo{ "strtoll", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOLL, DT_ATTR_STABCMN, DT_VERS_1_11,
464268578Srpaulo	&dt_idops_func, "int64_t(const char *, [int])" },
465178479Sjb{ "substr", DT_IDENT_FUNC, 0, DIF_SUBR_SUBSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
466178479Sjb	&dt_idops_func, "string(const char *, int, [int])" },
467178479Sjb{ "sum", DT_IDENT_AGGFUNC, 0, DTRACEAGG_SUM, DT_ATTR_STABCMN, DT_VERS_1_0,
468178479Sjb	&dt_idops_func, "void(@)" },
469178570Sjb#if !defined(sun)
470178570Sjb{ "sx_isexclusive", DT_IDENT_FUNC, 0, DIF_SUBR_SX_ISEXCLUSIVE,
471178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
472178570Sjb	&dt_idops_func, sxlock_str },
473178570Sjb{ "sx_shared_held", DT_IDENT_FUNC, 0, DIF_SUBR_SX_SHARED_HELD,
474178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
475178570Sjb	&dt_idops_func, sxlock_str },
476178570Sjb{ "sx_exclusive_held", DT_IDENT_FUNC, 0, DIF_SUBR_SX_EXCLUSIVE_HELD,
477178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
478178570Sjb	&dt_idops_func, sxlock_str },
479178570Sjb#endif
480178479Sjb{ "sym", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
481178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
482178479Sjb{ "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
483178479Sjb	&dt_idops_func, "void(@, ...)" },
484178479Sjb{ "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
485178479Sjb	&dt_idops_type, "void" },
486178479Sjb{ "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
487178479Sjb	&dt_idops_type, "id_t" },
488178479Sjb{ "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
489178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
490178479Sjb	&dt_idops_type, "uint64_t" },
491248706Spfg{ "tolower", DT_IDENT_FUNC, 0, DIF_SUBR_TOLOWER, DT_ATTR_STABCMN, DT_VERS_1_8,
492248706Spfg	&dt_idops_func, "string(const char *)" },
493248706Spfg{ "toupper", DT_IDENT_FUNC, 0, DIF_SUBR_TOUPPER, DT_ATTR_STABCMN, DT_VERS_1_8,
494248706Spfg	&dt_idops_func, "string(const char *)" },
495178479Sjb{ "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
496178479Sjb	&dt_idops_func, "void(@)" },
497178479Sjb{ "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
498178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
499248690Spfg	&dt_idops_func, "void(@, size_t, ...)" },
500178479Sjb{ "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
501178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
502178570Sjb{ "typeref", DT_IDENT_FUNC, 0, DIF_SUBR_TYPEREF, DT_ATTR_STABCMN, DT_VERS_1_1,
503178570Sjb	&dt_idops_func, "uintptr_t *(void *, size_t, string, size_t)" },
504178479Sjb{ "uaddr", DT_IDENT_ACTFUNC, 0, DT_ACT_UADDR, DT_ATTR_STABCMN,
505178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
506178479Sjb{ "ucaller", DT_IDENT_SCALAR, 0, DIF_VAR_UCALLER, DT_ATTR_STABCMN,
507178479Sjb	DT_VERS_1_2, &dt_idops_type, "uint64_t" },
508178479Sjb{ "ufunc", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
509178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
510178479Sjb{ "uid", DT_IDENT_SCALAR, 0, DIF_VAR_UID, DT_ATTR_STABCMN, DT_VERS_1_0,
511178479Sjb	&dt_idops_type, "uid_t" },
512178479Sjb{ "umod", DT_IDENT_ACTFUNC, 0, DT_ACT_UMOD, DT_ATTR_STABCMN,
513178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
514178479Sjb{ "uregs", DT_IDENT_ARRAY, 0, DIF_VAR_UREGS, DT_ATTR_STABCMN, DT_VERS_1_0,
515178479Sjb	&dt_idops_regs, NULL },
516178479Sjb{ "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
517178479Sjb	&dt_idops_func, "stack(...)" },
518178479Sjb{ "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
519178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_2,
520178479Sjb	&dt_idops_type, "uint32_t" },
521178479Sjb{ "usym", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
522178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
523178479Sjb{ "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
524178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
525178479Sjb	&dt_idops_type, "uint64_t" },
526178479Sjb{ "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
527178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
528178479Sjb	&dt_idops_type, "int64_t" },
529178570Sjb#if defined(sun)
530178479Sjb{ "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
531178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
532178570Sjb#endif
533234691Srstone
534234691Srstone#if !defined(sun)
535234691Srstone{ "cpu", DT_IDENT_SCALAR, 0, DIF_VAR_CPU,
536234691Srstone	DT_ATTR_STABCMN, DT_VERS_1_6_3, &dt_idops_type, "int" },
537234691Srstone#endif
538234691Srstone
539178479Sjb{ NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
540178479Sjb};
541178479Sjb
542178479Sjb/*
543178479Sjb * Tables of ILP32 intrinsic integer and floating-point type templates to use
544178479Sjb * to populate the dynamic "C" CTF type container.
545178479Sjb */
546178479Sjbstatic const dt_intrinsic_t _dtrace_intrinsics_32[] = {
547178479Sjb{ "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
548178479Sjb{ "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
549178479Sjb{ "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
550178479Sjb{ "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
551178479Sjb{ "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
552178479Sjb{ "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
553178479Sjb{ "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
554178479Sjb{ "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
555178479Sjb{ "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
556178479Sjb{ "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
557178479Sjb{ "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
558178479Sjb{ "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
559178479Sjb{ "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
560178479Sjb{ "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
561178479Sjb{ "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
562178479Sjb{ "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
563178479Sjb{ "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
564178479Sjb{ "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
565178479Sjb{ "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
566178479Sjb{ "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
567178479Sjb{ "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
568178479Sjb{ "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
569178479Sjb{ "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
570178479Sjb{ "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
571178479Sjb{ "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
572178479Sjb{ "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
573178479Sjb{ "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
574178479Sjb{ "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
575178479Sjb{ NULL, { 0, 0, 0 }, 0 }
576178479Sjb};
577178479Sjb
578178479Sjb/*
579178479Sjb * Tables of LP64 intrinsic integer and floating-point type templates to use
580178479Sjb * to populate the dynamic "C" CTF type container.
581178479Sjb */
582178479Sjbstatic const dt_intrinsic_t _dtrace_intrinsics_64[] = {
583178479Sjb{ "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
584178479Sjb{ "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
585178479Sjb{ "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
586178479Sjb{ "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
587178479Sjb{ "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
588178479Sjb{ "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
589178479Sjb{ "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
590178479Sjb{ "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
591178479Sjb{ "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
592178479Sjb{ "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
593178479Sjb{ "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
594178479Sjb{ "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
595178479Sjb{ "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
596178479Sjb{ "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
597178479Sjb{ "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
598178479Sjb{ "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
599178479Sjb{ "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
600178479Sjb{ "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
601178479Sjb{ "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
602178479Sjb{ "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
603178479Sjb{ "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
604178479Sjb{ "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
605178479Sjb{ "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
606178479Sjb{ "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
607178479Sjb{ "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
608178479Sjb{ "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
609178479Sjb{ "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
610178479Sjb{ "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
611178479Sjb{ NULL, { 0, 0, 0 }, 0 }
612178479Sjb};
613178479Sjb
614178479Sjb/*
615178479Sjb * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
616178479Sjb * These aliases ensure that D definitions can use typical <sys/types.h> names.
617178479Sjb */
618178479Sjbstatic const dt_typedef_t _dtrace_typedefs_32[] = {
619178479Sjb{ "char", "int8_t" },
620178479Sjb{ "short", "int16_t" },
621178479Sjb{ "int", "int32_t" },
622178479Sjb{ "long long", "int64_t" },
623178479Sjb{ "int", "intptr_t" },
624178479Sjb{ "int", "ssize_t" },
625178479Sjb{ "unsigned char", "uint8_t" },
626178479Sjb{ "unsigned short", "uint16_t" },
627178479Sjb{ "unsigned", "uint32_t" },
628178479Sjb{ "unsigned long long", "uint64_t" },
629178479Sjb{ "unsigned char", "uchar_t" },
630178479Sjb{ "unsigned short", "ushort_t" },
631178479Sjb{ "unsigned", "uint_t" },
632178479Sjb{ "unsigned long", "ulong_t" },
633178479Sjb{ "unsigned long long", "u_longlong_t" },
634178479Sjb{ "int", "ptrdiff_t" },
635178479Sjb{ "unsigned", "uintptr_t" },
636178479Sjb{ "unsigned", "size_t" },
637178479Sjb{ "long", "id_t" },
638178479Sjb{ "long", "pid_t" },
639178479Sjb{ NULL, NULL }
640178479Sjb};
641178479Sjb
642178479Sjb/*
643178479Sjb * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
644178479Sjb * These aliases ensure that D definitions can use typical <sys/types.h> names.
645178479Sjb */
646178479Sjbstatic const dt_typedef_t _dtrace_typedefs_64[] = {
647178479Sjb{ "char", "int8_t" },
648178479Sjb{ "short", "int16_t" },
649178479Sjb{ "int", "int32_t" },
650178479Sjb{ "long", "int64_t" },
651178479Sjb{ "long", "intptr_t" },
652178479Sjb{ "long", "ssize_t" },
653178479Sjb{ "unsigned char", "uint8_t" },
654178479Sjb{ "unsigned short", "uint16_t" },
655178479Sjb{ "unsigned", "uint32_t" },
656178479Sjb{ "unsigned long", "uint64_t" },
657178479Sjb{ "unsigned char", "uchar_t" },
658178479Sjb{ "unsigned short", "ushort_t" },
659178479Sjb{ "unsigned", "uint_t" },
660178479Sjb{ "unsigned long", "ulong_t" },
661178479Sjb{ "unsigned long long", "u_longlong_t" },
662178479Sjb{ "long", "ptrdiff_t" },
663178479Sjb{ "unsigned long", "uintptr_t" },
664178479Sjb{ "unsigned long", "size_t" },
665178479Sjb{ "int", "id_t" },
666178479Sjb{ "int", "pid_t" },
667178479Sjb{ NULL, NULL }
668178479Sjb};
669178479Sjb
670178479Sjb/*
671178479Sjb * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
672178479Sjb * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
673178479Sjb */
674178479Sjbstatic const dt_intdesc_t _dtrace_ints_32[] = {
675178479Sjb{ "int", NULL, CTF_ERR, 0x7fffffffULL },
676178479Sjb{ "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
677178479Sjb{ "long", NULL, CTF_ERR, 0x7fffffffULL },
678178479Sjb{ "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
679178479Sjb{ "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
680178479Sjb{ "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
681178479Sjb};
682178479Sjb
683178479Sjb/*
684178479Sjb * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
685178479Sjb * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
686178479Sjb */
687178479Sjbstatic const dt_intdesc_t _dtrace_ints_64[] = {
688178479Sjb{ "int", NULL, CTF_ERR, 0x7fffffffULL },
689178479Sjb{ "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
690178479Sjb{ "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
691178479Sjb{ "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
692178479Sjb{ "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
693178479Sjb{ "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
694178479Sjb};
695178479Sjb
696178479Sjb/*
697178479Sjb * Table of macro variable templates used to populate the macro identifier hash
698178479Sjb * when a new dtrace client open occurs.  Values are set by dtrace_update().
699178479Sjb */
700178479Sjbstatic const dt_ident_t _dtrace_macros[] = {
701178479Sjb{ "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
702178479Sjb{ "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
703178479Sjb{ "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
704178479Sjb{ "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
705178479Sjb{ "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
706178479Sjb{ "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
707178479Sjb{ "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
708178479Sjb{ "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
709178479Sjb{ "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
710178479Sjb{ "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
711178479Sjb{ "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
712178479Sjb{ NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
713178479Sjb};
714178479Sjb
715178479Sjb/*
716178479Sjb * Hard-wired definition string to be compiled and cached every time a new
717178479Sjb * DTrace library handle is initialized.  This string should only be used to
718178479Sjb * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
719178479Sjb */
720178479Sjbstatic const char _dtrace_hardwire[] = "\
721178479Sjbinline long NULL = 0; \n\
722178479Sjb#pragma D binding \"1.0\" NULL\n\
723178479Sjb";
724178479Sjb
725178479Sjb/*
726178479Sjb * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
727178479Sjb * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
728178479Sjb * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
729178479Sjb * relying on the fact that when running dtrace(1M), isaexec will invoke the
730178479Sjb * binary with the same bitness as the kernel, which is what we want by default
731178479Sjb * when generating our DIF.  The user can override the choice using oflags.
732178479Sjb */
733178479Sjbstatic const dtrace_conf_t _dtrace_conf = {
734178479Sjb	DIF_VERSION,		/* dtc_difversion */
735178479Sjb	DIF_DIR_NREGS,		/* dtc_difintregs */
736178479Sjb	DIF_DTR_NREGS,		/* dtc_diftupregs */
737178479Sjb	CTF_MODEL_NATIVE	/* dtc_ctfmodel */
738178479Sjb};
739178479Sjb
740178479Sjbconst dtrace_attribute_t _dtrace_maxattr = {
741178479Sjb	DTRACE_STABILITY_MAX,
742178479Sjb	DTRACE_STABILITY_MAX,
743178479Sjb	DTRACE_CLASS_MAX
744178479Sjb};
745178479Sjb
746178479Sjbconst dtrace_attribute_t _dtrace_defattr = {
747178479Sjb	DTRACE_STABILITY_STABLE,
748178479Sjb	DTRACE_STABILITY_STABLE,
749178479Sjb	DTRACE_CLASS_COMMON
750178479Sjb};
751178479Sjb
752178479Sjbconst dtrace_attribute_t _dtrace_symattr = {
753178479Sjb	DTRACE_STABILITY_PRIVATE,
754178479Sjb	DTRACE_STABILITY_PRIVATE,
755178479Sjb	DTRACE_CLASS_UNKNOWN
756178479Sjb};
757178479Sjb
758178479Sjbconst dtrace_attribute_t _dtrace_typattr = {
759178479Sjb	DTRACE_STABILITY_PRIVATE,
760178479Sjb	DTRACE_STABILITY_PRIVATE,
761178479Sjb	DTRACE_CLASS_UNKNOWN
762178479Sjb};
763178479Sjb
764178479Sjbconst dtrace_attribute_t _dtrace_prvattr = {
765178479Sjb	DTRACE_STABILITY_PRIVATE,
766178479Sjb	DTRACE_STABILITY_PRIVATE,
767178479Sjb	DTRACE_CLASS_UNKNOWN
768178479Sjb};
769178479Sjb
770178479Sjbconst dtrace_pattr_t _dtrace_prvdesc = {
771178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
772178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
773178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
774178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
775178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
776178479Sjb};
777178479Sjb
778178570Sjb#if defined(sun)
779178479Sjbconst char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
780178479Sjbconst char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
781178570Sjb#else
782178570Sjbconst char *_dtrace_defcpp = "cpp"; /* default cpp(1) to invoke */
783178570Sjbconst char *_dtrace_defld = "ld";   /* default ld(1) to invoke */
784178570Sjb#endif
785178479Sjb
786178479Sjbconst char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
787178570Sjb#if defined(sun)
788178479Sjbconst char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
789178570Sjb#else
790178570Sjbconst char *_dtrace_provdir = "/dev/dtrace"; /* provider directory */
791178570Sjb#endif
792178479Sjb
793178479Sjbint _dtrace_strbuckets = 211;	/* default number of hash buckets (prime) */
794178479Sjbint _dtrace_intbuckets = 256;	/* default number of integer buckets (Pof2) */
795178479Sjbuint_t _dtrace_strsize = 256;	/* default size of string intrinsic type */
796178479Sjbuint_t _dtrace_stkindent = 14;	/* default whitespace indent for stack/ustack */
797178479Sjbuint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
798178479Sjbuint_t _dtrace_pidlrulim = 8;	/* default number of pid handles to cache */
799178479Sjbsize_t _dtrace_bufsize = 512;	/* default dt_buf_create() size */
800178479Sjbint _dtrace_argmax = 32;	/* default maximum number of probe arguments */
801178479Sjb
802178479Sjbint _dtrace_debug = 0;		/* debug messages enabled (off) */
803178479Sjbconst char *const _dtrace_version = DT_VERS_STRING; /* API version string */
804178479Sjbint _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
805178479Sjb
806178479Sjbtypedef struct dt_fdlist {
807178479Sjb	int *df_fds;		/* array of provider driver file descriptors */
808178479Sjb	uint_t df_ents;		/* number of valid elements in df_fds[] */
809178479Sjb	uint_t df_size;		/* size of df_fds[] */
810178479Sjb} dt_fdlist_t;
811178479Sjb
812178570Sjb#if defined(sun)
813178479Sjb#pragma init(_dtrace_init)
814178570Sjb#else
815178570Sjbvoid _dtrace_init(void) __attribute__ ((constructor));
816178570Sjb#endif
817178479Sjbvoid
818178479Sjb_dtrace_init(void)
819178479Sjb{
820178479Sjb	_dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
821178479Sjb
822178479Sjb	for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
823178479Sjb		if (rd_init(_dtrace_rdvers) == RD_OK)
824178479Sjb			break;
825178479Sjb	}
826178570Sjb#if defined(__i386__)
827178570Sjb	/* make long doubles 64 bits -sson */
828178570Sjb	(void) fpsetprec(FP_PE);
829178570Sjb#endif
830178479Sjb}
831178479Sjb
832178479Sjbstatic dtrace_hdl_t *
833178479Sjbset_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
834178479Sjb{
835178479Sjb	if (dtp != NULL)
836178479Sjb		dtrace_close(dtp);
837178479Sjb	if (errp != NULL)
838178479Sjb		*errp = err;
839178479Sjb	return (NULL);
840178479Sjb}
841178479Sjb
842178479Sjbstatic void
843178479Sjbdt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
844178479Sjb{
845178479Sjb	dt_provmod_t *prov;
846178479Sjb	char path[PATH_MAX];
847178570Sjb	int fd;
848178570Sjb#if defined(sun)
849178479Sjb	struct dirent *dp, *ep;
850178479Sjb	DIR *dirp;
851178479Sjb
852178479Sjb	if ((dirp = opendir(_dtrace_provdir)) == NULL)
853178479Sjb		return; /* failed to open directory; just skip it */
854178479Sjb
855178479Sjb	ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
856178479Sjb	bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
857178479Sjb
858178479Sjb	while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
859178479Sjb		if (dp->d_name[0] == '.')
860178479Sjb			continue; /* skip "." and ".." */
861178479Sjb
862178479Sjb		if (dfp->df_ents == dfp->df_size) {
863178479Sjb			uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
864178479Sjb			int *fds = realloc(dfp->df_fds, size * sizeof (int));
865178479Sjb
866178479Sjb			if (fds == NULL)
867178479Sjb				break; /* skip the rest of this directory */
868178479Sjb
869178479Sjb			dfp->df_fds = fds;
870178479Sjb			dfp->df_size = size;
871178479Sjb		}
872178479Sjb
873178479Sjb		(void) snprintf(path, sizeof (path), "%s/%s",
874178479Sjb		    _dtrace_provdir, dp->d_name);
875178479Sjb
876178479Sjb		if ((fd = open(path, O_RDONLY)) == -1)
877178479Sjb			continue; /* failed to open driver; just skip it */
878178479Sjb
879178479Sjb		if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
880178479Sjb		    (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
881178479Sjb			free(prov);
882178479Sjb			(void) close(fd);
883178479Sjb			break;
884178479Sjb		}
885178479Sjb
886178479Sjb		(void) strcpy(prov->dp_name, dp->d_name);
887178479Sjb		prov->dp_next = *provmod;
888178479Sjb		*provmod = prov;
889178479Sjb
890178479Sjb		dt_dprintf("opened provider %s\n", dp->d_name);
891178479Sjb		dfp->df_fds[dfp->df_ents++] = fd;
892178479Sjb	}
893178479Sjb
894178479Sjb	(void) closedir(dirp);
895178570Sjb#else
896178570Sjb	char	*p;
897178570Sjb	char	*p1;
898178570Sjb	char	*p_providers = NULL;
899178570Sjb	int	error;
900178570Sjb	size_t	len = 0;
901178570Sjb
902178570Sjb	/*
903178570Sjb	 * Loop to allocate/reallocate memory for the string of provider
904178570Sjb	 * names and retry:
905178570Sjb	 */
906178570Sjb	while(1) {
907178570Sjb		/*
908178570Sjb		 * The first time around, get the string length. The next time,
909178570Sjb		 * hopefully we've allocated enough memory.
910178570Sjb		 */
911178570Sjb		error = sysctlbyname("debug.dtrace.providers",p_providers,&len,NULL,0);
912178570Sjb		if (len == 0)
913178570Sjb			/* No providers? That's strange. Where's dtrace? */
914178570Sjb			break;
915178570Sjb		else if (error == 0 && p_providers == NULL) {
916178570Sjb			/*
917178570Sjb			 * Allocate the initial memory which should be enough
918178570Sjb			 * unless another provider loads before we have
919178570Sjb			 * time to go back and get the string.
920178570Sjb			 */
921178570Sjb			if ((p_providers = malloc(len)) == NULL)
922178570Sjb				/* How do we report errors here? */
923178570Sjb				return;
924178570Sjb		} else if (error == -1 && errno == ENOMEM) {
925178570Sjb			/*
926178570Sjb			 * The current buffer isn't large enough, so
927178570Sjb			 * reallocate it. We normally won't need to do this
928178570Sjb			 * because providers aren't being loaded all the time.
929178570Sjb			 */
930178570Sjb			if ((p = realloc(p_providers,len)) == NULL)
931178570Sjb				/* How do we report errors here? */
932178570Sjb				return;
933178570Sjb			p_providers = p;
934178570Sjb		} else
935178570Sjb			break;
936178570Sjb	}
937178570Sjb
938178570Sjb	/* Check if we got a string of provider names: */
939178570Sjb	if (error == 0 && len > 0 && p_providers != NULL) {
940178570Sjb		p = p_providers;
941178570Sjb
942178570Sjb		/*
943178570Sjb		 * Parse the string containing the space separated
944178570Sjb		 * provider names.
945178570Sjb		 */
946178570Sjb		while ((p1 = strsep(&p," ")) != NULL) {
947178570Sjb			if (dfp->df_ents == dfp->df_size) {
948178570Sjb				uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
949178570Sjb				int *fds = realloc(dfp->df_fds, size * sizeof (int));
950178570Sjb
951178570Sjb				if (fds == NULL)
952178570Sjb					break;
953178570Sjb
954178570Sjb				dfp->df_fds = fds;
955178570Sjb				dfp->df_size = size;
956178570Sjb			}
957178570Sjb
958178570Sjb			(void) snprintf(path, sizeof (path), "/dev/dtrace/%s", p1);
959178570Sjb
960178570Sjb			if ((fd = open(path, O_RDONLY)) == -1)
961178570Sjb				continue; /* failed to open driver; just skip it */
962178570Sjb
963178570Sjb			if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
964178570Sjb			    (prov->dp_name = malloc(strlen(p1) + 1)) == NULL) {
965178570Sjb				free(prov);
966178570Sjb				(void) close(fd);
967178570Sjb				break;
968178570Sjb			}
969178570Sjb
970178570Sjb			(void) strcpy(prov->dp_name, p1);
971178570Sjb			prov->dp_next = *provmod;
972178570Sjb			*provmod = prov;
973178570Sjb
974178570Sjb			dt_dprintf("opened provider %s\n", p1);
975178570Sjb			dfp->df_fds[dfp->df_ents++] = fd;
976178570Sjb		}
977178570Sjb	}
978178570Sjb	if (p_providers != NULL)
979178570Sjb		free(p_providers);
980178570Sjb#endif
981178479Sjb}
982178479Sjb
983178479Sjbstatic void
984178479Sjbdt_provmod_destroy(dt_provmod_t **provmod)
985178479Sjb{
986178479Sjb	dt_provmod_t *next, *current;
987178479Sjb
988178479Sjb	for (current = *provmod; current != NULL; current = next) {
989178479Sjb		next = current->dp_next;
990178479Sjb		free(current->dp_name);
991178479Sjb		free(current);
992178479Sjb	}
993178479Sjb
994178479Sjb	*provmod = NULL;
995178479Sjb}
996178479Sjb
997178570Sjb#if defined(sun)
998178479Sjbstatic const char *
999178479Sjbdt_get_sysinfo(int cmd, char *buf, size_t len)
1000178479Sjb{
1001178479Sjb	ssize_t rv = sysinfo(cmd, buf, len);
1002178479Sjb	char *p = buf;
1003178479Sjb
1004178479Sjb	if (rv < 0 || rv > len)
1005178479Sjb		(void) snprintf(buf, len, "%s", "Unknown");
1006178479Sjb
1007178479Sjb	while ((p = strchr(p, '.')) != NULL)
1008178479Sjb		*p++ = '_';
1009178479Sjb
1010178479Sjb	return (buf);
1011178479Sjb}
1012178570Sjb#endif
1013178479Sjb
1014178479Sjbstatic dtrace_hdl_t *
1015178479Sjbdt_vopen(int version, int flags, int *errp,
1016178479Sjb    const dtrace_vector_t *vector, void *arg)
1017178479Sjb{
1018178479Sjb	dtrace_hdl_t *dtp = NULL;
1019178479Sjb	int dtfd = -1, ftfd = -1, fterr = 0;
1020178479Sjb	dtrace_prog_t *pgp;
1021178479Sjb	dt_module_t *dmp;
1022178479Sjb	dt_provmod_t *provmod = NULL;
1023178479Sjb	int i, err;
1024178479Sjb	struct rlimit rl;
1025178479Sjb
1026178479Sjb	const dt_intrinsic_t *dinp;
1027178479Sjb	const dt_typedef_t *dtyp;
1028178479Sjb	const dt_ident_t *idp;
1029178479Sjb
1030178479Sjb	dtrace_typeinfo_t dtt;
1031178479Sjb	ctf_funcinfo_t ctc;
1032178479Sjb	ctf_arinfo_t ctr;
1033178479Sjb
1034178479Sjb	dt_fdlist_t df = { NULL, 0, 0 };
1035178479Sjb
1036178479Sjb	char isadef[32], utsdef[32];
1037178479Sjb	char s1[64], s2[64];
1038178479Sjb
1039178479Sjb	if (version <= 0)
1040178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1041178479Sjb
1042178479Sjb	if (version > DTRACE_VERSION)
1043178479Sjb		return (set_open_errno(dtp, errp, EDT_VERSION));
1044178479Sjb
1045178479Sjb	if (version < DTRACE_VERSION) {
1046178479Sjb		/*
1047178479Sjb		 * Currently, increasing the library version number is used to
1048178479Sjb		 * denote a binary incompatible change.  That is, a consumer
1049178479Sjb		 * of the library cannot run on a version of the library with
1050178479Sjb		 * a higher DTRACE_VERSION number than the consumer compiled
1051178479Sjb		 * against.  Once the library API has been committed to,
1052178479Sjb		 * backwards binary compatibility will be required; at that
1053178479Sjb		 * time, this check should change to return EDT_OVERSION only
1054178479Sjb		 * if the specified version number is less than the version
1055178479Sjb		 * number at the time of interface commitment.
1056178479Sjb		 */
1057178479Sjb		return (set_open_errno(dtp, errp, EDT_OVERSION));
1058178479Sjb	}
1059178479Sjb
1060178479Sjb	if (flags & ~DTRACE_O_MASK)
1061178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1062178479Sjb
1063178479Sjb	if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
1064178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1065178479Sjb
1066178479Sjb	if (vector == NULL && arg != NULL)
1067178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1068178479Sjb
1069178479Sjb	if (elf_version(EV_CURRENT) == EV_NONE)
1070178479Sjb		return (set_open_errno(dtp, errp, EDT_ELFVERSION));
1071178479Sjb
1072178479Sjb	if (vector != NULL || (flags & DTRACE_O_NODEV))
1073178479Sjb		goto alloc; /* do not attempt to open dtrace device */
1074178479Sjb
1075178479Sjb	/*
1076178479Sjb	 * Before we get going, crank our limit on file descriptors up to the
1077178479Sjb	 * hard limit.  This is to allow for the fact that libproc keeps file
1078178479Sjb	 * descriptors to objects open for the lifetime of the proc handle;
1079178479Sjb	 * without raising our hard limit, we would have an acceptably small
1080178479Sjb	 * bound on the number of processes that we could concurrently
1081178479Sjb	 * instrument with the pid provider.
1082178479Sjb	 */
1083178479Sjb	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1084178479Sjb		rl.rlim_cur = rl.rlim_max;
1085178479Sjb		(void) setrlimit(RLIMIT_NOFILE, &rl);
1086178479Sjb	}
1087178479Sjb
1088178479Sjb	/*
1089178479Sjb	 * Get the device path of each of the providers.  We hold them open
1090178479Sjb	 * in the df.df_fds list until we open the DTrace driver itself,
1091178479Sjb	 * allowing us to see all of the probes provided on this system.  Once
1092178479Sjb	 * we have the DTrace driver open, we can safely close all the providers
1093178479Sjb	 * now that they have registered with the framework.
1094178479Sjb	 */
1095178479Sjb	dt_provmod_open(&provmod, &df);
1096178479Sjb
1097178479Sjb	dtfd = open("/dev/dtrace/dtrace", O_RDWR);
1098178479Sjb	err = errno; /* save errno from opening dtfd */
1099254197Srpaulo#if defined(__FreeBSD__)
1100254197Srpaulo	/*
1101254197Srpaulo	 * Automatically load the 'dtraceall' module if we couldn't open the
1102254197Srpaulo	 * char device.
1103254197Srpaulo	 */
1104254197Srpaulo	if (err == ENOENT && modfind("dtraceall") < 0) {
1105254197Srpaulo		kldload("dtraceall"); /* ignore the error */
1106254197Srpaulo		dtfd = open("/dev/dtrace/dtrace", O_RDWR);
1107254197Srpaulo		err = errno;
1108254197Srpaulo	}
1109254197Srpaulo#endif
1110178570Sjb#if defined(sun)
1111178479Sjb	ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
1112178570Sjb#else
1113178570Sjb	ftfd = open("/dev/dtrace/fasttrap", O_RDWR);
1114178570Sjb#endif
1115178479Sjb	fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
1116178479Sjb
1117178479Sjb	while (df.df_ents-- != 0)
1118178479Sjb		(void) close(df.df_fds[df.df_ents]);
1119178479Sjb
1120178479Sjb	free(df.df_fds);
1121178479Sjb
1122178479Sjb	/*
1123178479Sjb	 * If we failed to open the dtrace device, fail dtrace_open().
1124178479Sjb	 * We convert some kernel errnos to custom libdtrace errnos to
1125178479Sjb	 * improve the resulting message from the usual strerror().
1126178479Sjb	 */
1127178479Sjb	if (dtfd == -1) {
1128178479Sjb		dt_provmod_destroy(&provmod);
1129178479Sjb		switch (err) {
1130178479Sjb		case ENOENT:
1131178479Sjb			err = EDT_NOENT;
1132178479Sjb			break;
1133178479Sjb		case EBUSY:
1134178479Sjb			err = EDT_BUSY;
1135178479Sjb			break;
1136178479Sjb		case EACCES:
1137178479Sjb			err = EDT_ACCESS;
1138178479Sjb			break;
1139178479Sjb		}
1140178479Sjb		return (set_open_errno(dtp, errp, err));
1141178479Sjb	}
1142178479Sjb
1143178479Sjb	(void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
1144178479Sjb	(void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
1145178479Sjb
1146178479Sjballoc:
1147178479Sjb	if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
1148178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1149178479Sjb
1150178479Sjb	bzero(dtp, sizeof (dtrace_hdl_t));
1151178479Sjb	dtp->dt_oflags = flags;
1152211554Srpaulo#if defined(sun)
1153178479Sjb	dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
1154211554Srpaulo#else
1155269724Smarkj	dtp->dt_prcmode = DT_PROC_STOP_POSTINIT;
1156211554Srpaulo#endif
1157178479Sjb	dtp->dt_linkmode = DT_LINK_KERNEL;
1158178479Sjb	dtp->dt_linktype = DT_LTYP_ELF;
1159178479Sjb	dtp->dt_xlatemode = DT_XL_STATIC;
1160178479Sjb	dtp->dt_stdcmode = DT_STDC_XA;
1161268578Srpaulo	dtp->dt_encoding = DT_ENCODING_UNSET;
1162178479Sjb	dtp->dt_version = version;
1163178479Sjb	dtp->dt_fd = dtfd;
1164178479Sjb	dtp->dt_ftfd = ftfd;
1165178479Sjb	dtp->dt_fterr = fterr;
1166178479Sjb	dtp->dt_cdefs_fd = -1;
1167178479Sjb	dtp->dt_ddefs_fd = -1;
1168178570Sjb#if defined(sun)
1169178479Sjb	dtp->dt_stdout_fd = -1;
1170178570Sjb#else
1171178570Sjb	dtp->dt_freopen_fp = NULL;
1172178570Sjb#endif
1173178479Sjb	dtp->dt_modbuckets = _dtrace_strbuckets;
1174178479Sjb	dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
1175178479Sjb	dtp->dt_provbuckets = _dtrace_strbuckets;
1176178479Sjb	dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
1177249573Spfg	dt_proc_hash_create(dtp);
1178178479Sjb	dtp->dt_vmax = DT_VERS_LATEST;
1179178479Sjb	dtp->dt_cpp_path = strdup(_dtrace_defcpp);
1180178479Sjb	dtp->dt_cpp_argv = malloc(sizeof (char *));
1181178479Sjb	dtp->dt_cpp_argc = 1;
1182178479Sjb	dtp->dt_cpp_args = 1;
1183178479Sjb	dtp->dt_ld_path = strdup(_dtrace_defld);
1184178479Sjb	dtp->dt_provmod = provmod;
1185178479Sjb	dtp->dt_vector = vector;
1186178479Sjb	dtp->dt_varg = arg;
1187178479Sjb	dt_dof_init(dtp);
1188178479Sjb	(void) uname(&dtp->dt_uts);
1189178479Sjb
1190178479Sjb	if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
1191249573Spfg	    dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
1192249573Spfg	    dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
1193178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1194178479Sjb
1195178479Sjb	for (i = 0; i < DTRACEOPT_MAX; i++)
1196178479Sjb		dtp->dt_options[i] = DTRACEOPT_UNSET;
1197178479Sjb
1198178479Sjb	dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
1199178479Sjb
1200178570Sjb#if defined(sun)
1201178479Sjb	(void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
1202178479Sjb	    (uint_t)(sizeof (void *) * NBBY));
1203178479Sjb
1204178479Sjb	(void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
1205178479Sjb	    dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
1206178479Sjb	    dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
1207178479Sjb
1208178479Sjb	if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
1209178479Sjb	    dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
1210178479Sjb	    dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
1211178479Sjb	    dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
1212178479Sjb	    dt_cpp_add_arg(dtp, isadef) == NULL ||
1213178479Sjb	    dt_cpp_add_arg(dtp, utsdef) == NULL)
1214178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1215178570Sjb#endif
1216178479Sjb
1217178479Sjb	if (flags & DTRACE_O_NODEV)
1218178479Sjb		bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
1219178479Sjb	else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
1220178479Sjb		return (set_open_errno(dtp, errp, errno));
1221178479Sjb
1222178479Sjb	if (flags & DTRACE_O_LP64)
1223178479Sjb		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
1224178479Sjb	else if (flags & DTRACE_O_ILP32)
1225178479Sjb		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
1226178479Sjb
1227178479Sjb#ifdef __sparc
1228178479Sjb	/*
1229178479Sjb	 * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
1230178479Sjb	 * and __sparcv9 is defined if we are doing a 64-bit compile.
1231178479Sjb	 */
1232178479Sjb	if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
1233178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1234178479Sjb
1235178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
1236178479Sjb	    dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
1237178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1238178479Sjb#endif
1239178479Sjb
1240178570Sjb#if defined(sun)
1241178479Sjb#ifdef __x86
1242178479Sjb	/*
1243178479Sjb	 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
1244178479Sjb	 * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
1245178479Sjb	 * they are defined exclusive of one another (see PSARC 2004/619).
1246178479Sjb	 */
1247178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1248178479Sjb		if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
1249178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1250178479Sjb	} else {
1251178479Sjb		if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
1252178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1253178479Sjb	}
1254178479Sjb#endif
1255178570Sjb#else
1256178570Sjb#if defined(__amd64__) || defined(__i386__)
1257178570Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1258178570Sjb		if (dt_cpp_add_arg(dtp, "-m64") == NULL)
1259178570Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1260178570Sjb	} else {
1261178570Sjb		if (dt_cpp_add_arg(dtp, "-m32") == NULL)
1262178570Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1263178570Sjb	}
1264178570Sjb#endif
1265178570Sjb#endif
1266178479Sjb
1267178479Sjb	if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1268178479Sjb		return (set_open_errno(dtp, errp, EDT_DIFVERS));
1269178479Sjb
1270178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1271178479Sjb		bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1272178479Sjb	else
1273178479Sjb		bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1274178479Sjb
1275178570Sjb	/*
1276178570Sjb	 * On FreeBSD the kernel module name can't be hard-coded. The
1277178570Sjb	 * 'kern.bootfile' sysctl value tells us exactly which file is being
1278178570Sjb	 * used as the kernel.
1279178570Sjb	 */
1280178570Sjb#if !defined(sun)
1281178570Sjb	{
1282178570Sjb	char bootfile[MAXPATHLEN];
1283178570Sjb	char *p;
1284178570Sjb	int i;
1285178570Sjb	size_t len = sizeof(bootfile);
1286178570Sjb
1287178570Sjb	/* This call shouldn't fail, but use a default just in case. */
1288178570Sjb	if (sysctlbyname("kern.bootfile", bootfile, &len, NULL, 0) != 0)
1289178570Sjb		strlcpy(bootfile, "kernel", sizeof(bootfile));
1290178570Sjb
1291178570Sjb	if ((p = strrchr(bootfile, '/')) != NULL)
1292178570Sjb		p++;
1293178570Sjb	else
1294178570Sjb		p = bootfile;
1295178570Sjb
1296178570Sjb	/*
1297178570Sjb	 * Format the global variables based on the kernel module name.
1298178570Sjb	 */
1299178570Sjb	snprintf(curthread_str, sizeof(curthread_str), "%s`struct thread *",p);
1300178570Sjb	snprintf(intmtx_str, sizeof(intmtx_str), "int(%s`struct mtx *)",p);
1301178570Sjb	snprintf(threadmtx_str, sizeof(threadmtx_str), "struct thread *(%s`struct mtx *)",p);
1302178570Sjb	snprintf(rwlock_str, sizeof(rwlock_str), "int(%s`struct rwlock *)",p);
1303178570Sjb	snprintf(sxlock_str, sizeof(sxlock_str), "int(%s`struct sxlock *)",p);
1304178570Sjb	}
1305178570Sjb#endif
1306178570Sjb
1307178479Sjb	dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1308178479Sjb	dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1309178479Sjb	    DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1310178479Sjb
1311178479Sjb	dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1312178479Sjb	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1313178479Sjb
1314178479Sjb	dtp->dt_tls = dt_idhash_create("thread local", NULL,
1315178479Sjb	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1316178479Sjb
1317178479Sjb	if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1318178479Sjb	    dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1319178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1320178479Sjb
1321178479Sjb	/*
1322178479Sjb	 * Populate the dt_macros identifier hash table by hand: we can't use
1323178479Sjb	 * the dt_idhash_populate() mechanism because we're not yet compiling
1324178479Sjb	 * and dtrace_update() needs to immediately reference these idents.
1325178479Sjb	 */
1326178479Sjb	for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1327178479Sjb		if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1328178479Sjb		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1329178479Sjb		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1330178479Sjb		    idp->di_iarg, 0) == NULL)
1331178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1332178479Sjb	}
1333178479Sjb
1334178479Sjb	/*
1335178479Sjb	 * Update the module list using /system/object and load the values for
1336178479Sjb	 * the macro variable definitions according to the current process.
1337178479Sjb	 */
1338178479Sjb	dtrace_update(dtp);
1339178479Sjb
1340178479Sjb	/*
1341178479Sjb	 * Select the intrinsics and typedefs we want based on the data model.
1342178479Sjb	 * The intrinsics are under "C".  The typedefs are added under "D".
1343178479Sjb	 */
1344178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1345178479Sjb		dinp = _dtrace_intrinsics_32;
1346178479Sjb		dtyp = _dtrace_typedefs_32;
1347178479Sjb	} else {
1348178479Sjb		dinp = _dtrace_intrinsics_64;
1349178479Sjb		dtyp = _dtrace_typedefs_64;
1350178479Sjb	}
1351178479Sjb
1352178479Sjb	/*
1353178479Sjb	 * Create a dynamic CTF container under the "C" scope for intrinsic
1354178479Sjb	 * types and types defined in ANSI-C header files that are included.
1355178479Sjb	 */
1356178479Sjb	if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1357178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1358178479Sjb
1359178479Sjb	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1360178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1361178479Sjb
1362178479Sjb	dt_dprintf("created CTF container for %s (%p)\n",
1363178479Sjb	    dmp->dm_name, (void *)dmp->dm_ctfp);
1364178479Sjb
1365178479Sjb	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1366178479Sjb	ctf_setspecific(dmp->dm_ctfp, dmp);
1367178479Sjb
1368178479Sjb	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1369178479Sjb	dmp->dm_modid = -1; /* no module ID */
1370178479Sjb
1371178479Sjb	/*
1372178479Sjb	 * Fill the dynamic "C" CTF container with all of the intrinsic
1373178479Sjb	 * integer and floating-point types appropriate for this data model.
1374178479Sjb	 */
1375178479Sjb	for (; dinp->din_name != NULL; dinp++) {
1376178479Sjb		if (dinp->din_kind == CTF_K_INTEGER) {
1377178479Sjb			err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1378178479Sjb			    dinp->din_name, &dinp->din_data);
1379178479Sjb		} else {
1380178479Sjb			err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1381178479Sjb			    dinp->din_name, &dinp->din_data);
1382178479Sjb		}
1383178479Sjb
1384178479Sjb		if (err == CTF_ERR) {
1385178479Sjb			dt_dprintf("failed to add %s to C container: %s\n",
1386178479Sjb			    dinp->din_name, ctf_errmsg(
1387178479Sjb			    ctf_errno(dmp->dm_ctfp)));
1388178479Sjb			return (set_open_errno(dtp, errp, EDT_CTF));
1389178479Sjb		}
1390178479Sjb	}
1391178479Sjb
1392178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1393178479Sjb		dt_dprintf("failed to update C container: %s\n",
1394178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1395178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1396178479Sjb	}
1397178479Sjb
1398178479Sjb	/*
1399178479Sjb	 * Add intrinsic pointer types that are needed to initialize printf
1400178479Sjb	 * format dictionary types (see table in dt_printf.c).
1401178479Sjb	 */
1402178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1403178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1404178479Sjb
1405178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1406178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1407178479Sjb
1408178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1409178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1410178479Sjb
1411178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1412178479Sjb		dt_dprintf("failed to update C container: %s\n",
1413178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1414178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1415178479Sjb	}
1416178479Sjb
1417178479Sjb	/*
1418178479Sjb	 * Create a dynamic CTF container under the "D" scope for types that
1419178479Sjb	 * are defined by the D program itself or on-the-fly by the D compiler.
1420178479Sjb	 * The "D" CTF container is a child of the "C" CTF container.
1421178479Sjb	 */
1422178479Sjb	if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1423178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1424178479Sjb
1425178479Sjb	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1426178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1427178479Sjb
1428178479Sjb	dt_dprintf("created CTF container for %s (%p)\n",
1429178479Sjb	    dmp->dm_name, (void *)dmp->dm_ctfp);
1430178479Sjb
1431178479Sjb	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1432178479Sjb	ctf_setspecific(dmp->dm_ctfp, dmp);
1433178479Sjb
1434178479Sjb	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1435178479Sjb	dmp->dm_modid = -1; /* no module ID */
1436178479Sjb
1437178479Sjb	if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1438178479Sjb		dt_dprintf("failed to import D parent container: %s\n",
1439178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1440178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1441178479Sjb	}
1442178479Sjb
1443178479Sjb	/*
1444178479Sjb	 * Fill the dynamic "D" CTF container with all of the built-in typedefs
1445178479Sjb	 * that we need to use for our D variable and function definitions.
1446178479Sjb	 * This ensures that basic inttypes.h names are always available to us.
1447178479Sjb	 */
1448178479Sjb	for (; dtyp->dty_src != NULL; dtyp++) {
1449178479Sjb		if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1450178479Sjb		    dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1451178479Sjb		    dtyp->dty_src)) == CTF_ERR) {
1452178479Sjb			dt_dprintf("failed to add typedef %s %s to D "
1453178479Sjb			    "container: %s", dtyp->dty_src, dtyp->dty_dst,
1454178479Sjb			    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1455178479Sjb			return (set_open_errno(dtp, errp, EDT_CTF));
1456178479Sjb		}
1457178479Sjb	}
1458178479Sjb
1459178479Sjb	/*
1460178479Sjb	 * Insert a CTF ID corresponding to a pointer to a type of kind
1461178479Sjb	 * CTF_K_FUNCTION we can use in the compiler for function pointers.
1462178479Sjb	 * CTF treats all function pointers as "int (*)()" so we only need one.
1463178479Sjb	 */
1464178479Sjb	ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1465178479Sjb	ctc.ctc_argc = 0;
1466178479Sjb	ctc.ctc_flags = 0;
1467178479Sjb
1468178479Sjb	dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
1469178479Sjb	    CTF_ADD_ROOT, &ctc, NULL);
1470178479Sjb
1471178479Sjb	dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
1472178479Sjb	    CTF_ADD_ROOT, dtp->dt_type_func);
1473178479Sjb
1474178479Sjb	/*
1475178479Sjb	 * We also insert CTF definitions for the special D intrinsic types
1476178479Sjb	 * string and <DYN> into the D container.  The string type is added
1477178479Sjb	 * as a typedef of char[n].  The <DYN> type is an alias for void.
1478178479Sjb	 * We compare types to these special CTF ids throughout the compiler.
1479178479Sjb	 */
1480178479Sjb	ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1481178479Sjb	ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1482178479Sjb	ctr.ctr_nelems = _dtrace_strsize;
1483178479Sjb
1484178479Sjb	dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1485178479Sjb	    "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1486178479Sjb
1487178479Sjb	dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1488178479Sjb	    "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1489178479Sjb
1490178479Sjb	dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1491178479Sjb	    "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1492178479Sjb
1493178479Sjb	dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1494178479Sjb	    "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1495178479Sjb
1496178479Sjb	dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1497178479Sjb	    "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1498178479Sjb
1499178479Sjb	if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1500178479Sjb	    dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1501178479Sjb	    dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1502178479Sjb	    dtp->dt_type_usymaddr == CTF_ERR) {
1503178479Sjb		dt_dprintf("failed to add intrinsic to D container: %s\n",
1504178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1505178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1506178479Sjb	}
1507178479Sjb
1508178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1509178479Sjb		dt_dprintf("failed update D container: %s\n",
1510178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1511178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1512178479Sjb	}
1513178479Sjb
1514178479Sjb	/*
1515178479Sjb	 * Initialize the integer description table used to convert integer
1516178479Sjb	 * constants to the appropriate types.  Refer to the comments above
1517178479Sjb	 * dt_node_int() for a complete description of how this table is used.
1518178479Sjb	 */
1519178479Sjb	for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1520178479Sjb		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1521178479Sjb		    dtp->dt_ints[i].did_name, &dtt) != 0) {
1522178479Sjb			dt_dprintf("failed to lookup integer type %s: %s\n",
1523178479Sjb			    dtp->dt_ints[i].did_name,
1524178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1525178479Sjb			return (set_open_errno(dtp, errp, dtp->dt_errno));
1526178479Sjb		}
1527178479Sjb		dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1528178479Sjb		dtp->dt_ints[i].did_type = dtt.dtt_type;
1529178479Sjb	}
1530178479Sjb
1531178479Sjb	/*
1532178479Sjb	 * Now that we've created the "C" and "D" containers, move them to the
1533178479Sjb	 * start of the module list so that these types and symbols are found
1534178479Sjb	 * first (for stability) when iterating through the module list.
1535178479Sjb	 */
1536178479Sjb	dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1537178479Sjb	dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1538178479Sjb
1539178479Sjb	dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1540178479Sjb	dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1541178479Sjb
1542178479Sjb	if (dt_pfdict_create(dtp) == -1)
1543178479Sjb		return (set_open_errno(dtp, errp, dtp->dt_errno));
1544178479Sjb
1545178479Sjb	/*
1546178479Sjb	 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1547178479Sjb	 * because without /dev/dtrace open, we will not be able to load the
1548178479Sjb	 * names and attributes of any providers or probes from the kernel.
1549178479Sjb	 */
1550178479Sjb	if (flags & DTRACE_O_NODEV)
1551178479Sjb		dtp->dt_cflags |= DTRACE_C_ZDEFS;
1552178479Sjb
1553178479Sjb	/*
1554178479Sjb	 * Load hard-wired inlines into the definition cache by calling the
1555178479Sjb	 * compiler on the raw definition string defined above.
1556178479Sjb	 */
1557178479Sjb	if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1558178479Sjb	    DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1559178479Sjb		dt_dprintf("failed to load hard-wired definitions: %s\n",
1560178479Sjb		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1561178479Sjb		return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1562178479Sjb	}
1563178479Sjb
1564178479Sjb	dt_program_destroy(dtp, pgp);
1565178479Sjb
1566178479Sjb	/*
1567178479Sjb	 * Set up the default DTrace library path.  Once set, the next call to
1568178479Sjb	 * dt_compile() will compile all the libraries.  We intentionally defer
1569178479Sjb	 * library processing to improve overhead for clients that don't ever
1570178479Sjb	 * compile, and to provide better error reporting (because the full
1571178479Sjb	 * reporting of compiler errors requires dtrace_open() to succeed).
1572178479Sjb	 */
1573178479Sjb	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1574178479Sjb		return (set_open_errno(dtp, errp, dtp->dt_errno));
1575178479Sjb
1576178479Sjb	return (dtp);
1577178479Sjb}
1578178479Sjb
1579178479Sjbdtrace_hdl_t *
1580178479Sjbdtrace_open(int version, int flags, int *errp)
1581178479Sjb{
1582178479Sjb	return (dt_vopen(version, flags, errp, NULL, NULL));
1583178479Sjb}
1584178479Sjb
1585178479Sjbdtrace_hdl_t *
1586178479Sjbdtrace_vopen(int version, int flags, int *errp,
1587178479Sjb    const dtrace_vector_t *vector, void *arg)
1588178479Sjb{
1589178479Sjb	return (dt_vopen(version, flags, errp, vector, arg));
1590178479Sjb}
1591178479Sjb
1592178479Sjbvoid
1593178479Sjbdtrace_close(dtrace_hdl_t *dtp)
1594178479Sjb{
1595178479Sjb	dt_ident_t *idp, *ndp;
1596178479Sjb	dt_module_t *dmp;
1597178479Sjb	dt_provider_t *pvp;
1598178479Sjb	dtrace_prog_t *pgp;
1599178479Sjb	dt_xlator_t *dxp;
1600178479Sjb	dt_dirpath_t *dirp;
1601178479Sjb	int i;
1602178479Sjb
1603178570Sjb	if (dtp->dt_procs != NULL)
1604249573Spfg		dt_proc_hash_destroy(dtp);
1605178570Sjb
1606178479Sjb	while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1607178479Sjb		dt_program_destroy(dtp, pgp);
1608178479Sjb
1609178479Sjb	while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1610178479Sjb		dt_xlator_destroy(dtp, dxp);
1611178479Sjb
1612178479Sjb	dt_free(dtp, dtp->dt_xlatormap);
1613178479Sjb
1614178479Sjb	for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1615178479Sjb		ndp = idp->di_next;
1616178479Sjb		dt_ident_destroy(idp);
1617178479Sjb	}
1618178479Sjb
1619178479Sjb	if (dtp->dt_macros != NULL)
1620178479Sjb		dt_idhash_destroy(dtp->dt_macros);
1621178479Sjb	if (dtp->dt_aggs != NULL)
1622178479Sjb		dt_idhash_destroy(dtp->dt_aggs);
1623178479Sjb	if (dtp->dt_globals != NULL)
1624178479Sjb		dt_idhash_destroy(dtp->dt_globals);
1625178479Sjb	if (dtp->dt_tls != NULL)
1626178479Sjb		dt_idhash_destroy(dtp->dt_tls);
1627178479Sjb
1628178479Sjb	while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1629178479Sjb		dt_module_destroy(dtp, dmp);
1630178479Sjb
1631178479Sjb	while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1632178479Sjb		dt_provider_destroy(dtp, pvp);
1633178479Sjb
1634178479Sjb	if (dtp->dt_fd != -1)
1635178479Sjb		(void) close(dtp->dt_fd);
1636178479Sjb	if (dtp->dt_ftfd != -1)
1637178479Sjb		(void) close(dtp->dt_ftfd);
1638178479Sjb	if (dtp->dt_cdefs_fd != -1)
1639178479Sjb		(void) close(dtp->dt_cdefs_fd);
1640178479Sjb	if (dtp->dt_ddefs_fd != -1)
1641178479Sjb		(void) close(dtp->dt_ddefs_fd);
1642178570Sjb#if defined(sun)
1643178479Sjb	if (dtp->dt_stdout_fd != -1)
1644178479Sjb		(void) close(dtp->dt_stdout_fd);
1645178570Sjb#else
1646178570Sjb	if (dtp->dt_freopen_fp != NULL)
1647178570Sjb		(void) fclose(dtp->dt_freopen_fp);
1648178570Sjb#endif
1649178479Sjb
1650178479Sjb	dt_epid_destroy(dtp);
1651178479Sjb	dt_aggid_destroy(dtp);
1652178479Sjb	dt_format_destroy(dtp);
1653248708Spfg	dt_strdata_destroy(dtp);
1654178479Sjb	dt_buffered_destroy(dtp);
1655178479Sjb	dt_aggregate_destroy(dtp);
1656178479Sjb	dt_pfdict_destroy(dtp);
1657178479Sjb	dt_provmod_destroy(&dtp->dt_provmod);
1658178479Sjb	dt_dof_fini(dtp);
1659178479Sjb
1660178479Sjb	for (i = 1; i < dtp->dt_cpp_argc; i++)
1661178479Sjb		free(dtp->dt_cpp_argv[i]);
1662178479Sjb
1663178479Sjb	while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1664178479Sjb		dt_list_delete(&dtp->dt_lib_path, dirp);
1665178479Sjb		free(dirp->dir_path);
1666178479Sjb		free(dirp);
1667178479Sjb	}
1668178479Sjb
1669178479Sjb	free(dtp->dt_cpp_argv);
1670178479Sjb	free(dtp->dt_cpp_path);
1671178479Sjb	free(dtp->dt_ld_path);
1672178479Sjb
1673178479Sjb	free(dtp->dt_mods);
1674178479Sjb	free(dtp->dt_provs);
1675178479Sjb	free(dtp);
1676178479Sjb}
1677178479Sjb
1678178479Sjbint
1679178479Sjbdtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1680178479Sjb{
1681178479Sjb	dt_provmod_t *prov;
1682178479Sjb	int i = 0;
1683178479Sjb
1684178479Sjb	for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1685178479Sjb		if (i < nmods)
1686178479Sjb			mods[i] = prov->dp_name;
1687178479Sjb	}
1688178479Sjb
1689178479Sjb	return (i);
1690178479Sjb}
1691178479Sjb
1692178479Sjbint
1693178479Sjbdtrace_ctlfd(dtrace_hdl_t *dtp)
1694178479Sjb{
1695178479Sjb	return (dtp->dt_fd);
1696178479Sjb}
1697