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.
24237624Spfg * Copyright (c) 2011, 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)
125250574Smarkj#define	DT_VERS_LATEST	DT_VERS_1_9_1
126250574Smarkj#define	DT_VERS_STRING	"Sun D 1.9.1"
127178479Sjb
128178479Sjbconst dt_version_t _dtrace_versions[] = {
129178479Sjb	DT_VERS_1_0,	/* D API 1.0.0 (PSARC 2001/466) Solaris 10 FCS */
130178479Sjb	DT_VERS_1_1,	/* D API 1.1.0 Solaris Express 6/05 */
131178479Sjb	DT_VERS_1_2,	/* D API 1.2.0 Solaris 10 Update 1 */
132178479Sjb	DT_VERS_1_2_1,	/* D API 1.2.1 Solaris Express 4/06 */
133178479Sjb	DT_VERS_1_2_2,	/* D API 1.2.2 Solaris Express 6/06 */
134178479Sjb	DT_VERS_1_3,	/* D API 1.3 Solaris Express 10/06 */
135178479Sjb	DT_VERS_1_4,	/* D API 1.4 Solaris Express 2/07 */
136178479Sjb	DT_VERS_1_4_1,	/* D API 1.4.1 Solaris Express 4/07 */
137178479Sjb	DT_VERS_1_5,	/* D API 1.5 Solaris Express 7/07 */
138178479Sjb	DT_VERS_1_6,	/* D API 1.6 */
139178570Sjb	DT_VERS_1_6_1,	/* D API 1.6.1 */
140184696Srodrigc	DT_VERS_1_6_2,	/* D API 1.6.2 */
141210767Srpaulo	DT_VERS_1_6_3,	/* D API 1.6.3 */
142237624Spfg	DT_VERS_1_7,	/* D API 1.7 */
143248690Spfg	DT_VERS_1_7_1,	/* D API 1.7.1 */
144248706Spfg	DT_VERS_1_8,	/* D API 1.8 */
145248706Spfg	DT_VERS_1_8_1,	/* D API 1.8.1 */
146248708Spfg	DT_VERS_1_9,	/* D API 1.9 */
147250574Smarkj	DT_VERS_1_9_1,	/* D API 1.9.1 */
148178479Sjb	0
149178479Sjb};
150178479Sjb
151178479Sjb/*
152178570Sjb * Global variables that are formatted on FreeBSD based on the kernel file name.
153178570Sjb */
154178570Sjb#if !defined(sun)
155178570Sjbstatic char	curthread_str[MAXPATHLEN];
156178570Sjbstatic char	intmtx_str[MAXPATHLEN];
157178570Sjbstatic char	threadmtx_str[MAXPATHLEN];
158178570Sjbstatic char	rwlock_str[MAXPATHLEN];
159178570Sjbstatic char	sxlock_str[MAXPATHLEN];
160178570Sjb#endif
161178570Sjb
162178570Sjb/*
163178479Sjb * Table of global identifiers.  This is used to populate the global identifier
164178479Sjb * hash when a new dtrace client open occurs.  For more info see dt_ident.h.
165178479Sjb * The global identifiers that represent functions use the dt_idops_func ops
166178479Sjb * and specify the private data pointer as a prototype string which is parsed
167178479Sjb * when the identifier is first encountered.  These prototypes look like ANSI
168178479Sjb * C function prototypes except that the special symbol "@" can be used as a
169178479Sjb * wildcard to represent a single parameter of any type (i.e. any dt_node_t).
170178479Sjb * The standard "..." notation can also be used to represent varargs.  An empty
171178479Sjb * parameter list is taken to mean void (that is, no arguments are permitted).
172178479Sjb * A parameter enclosed in square brackets (e.g. "[int]") denotes an optional
173178479Sjb * argument.
174178479Sjb */
175178479Sjbstatic const dt_ident_t _dtrace_globals[] = {
176178479Sjb{ "alloca", DT_IDENT_FUNC, 0, DIF_SUBR_ALLOCA, DT_ATTR_STABCMN, DT_VERS_1_0,
177178479Sjb	&dt_idops_func, "void *(size_t)" },
178178479Sjb{ "arg0", DT_IDENT_SCALAR, 0, DIF_VAR_ARG0, DT_ATTR_STABCMN, DT_VERS_1_0,
179178479Sjb	&dt_idops_type, "int64_t" },
180178479Sjb{ "arg1", DT_IDENT_SCALAR, 0, DIF_VAR_ARG1, DT_ATTR_STABCMN, DT_VERS_1_0,
181178479Sjb	&dt_idops_type, "int64_t" },
182178479Sjb{ "arg2", DT_IDENT_SCALAR, 0, DIF_VAR_ARG2, DT_ATTR_STABCMN, DT_VERS_1_0,
183178479Sjb	&dt_idops_type, "int64_t" },
184178479Sjb{ "arg3", DT_IDENT_SCALAR, 0, DIF_VAR_ARG3, DT_ATTR_STABCMN, DT_VERS_1_0,
185178479Sjb	&dt_idops_type, "int64_t" },
186178479Sjb{ "arg4", DT_IDENT_SCALAR, 0, DIF_VAR_ARG4, DT_ATTR_STABCMN, DT_VERS_1_0,
187178479Sjb	&dt_idops_type, "int64_t" },
188178479Sjb{ "arg5", DT_IDENT_SCALAR, 0, DIF_VAR_ARG5, DT_ATTR_STABCMN, DT_VERS_1_0,
189178479Sjb	&dt_idops_type, "int64_t" },
190178479Sjb{ "arg6", DT_IDENT_SCALAR, 0, DIF_VAR_ARG6, DT_ATTR_STABCMN, DT_VERS_1_0,
191178479Sjb	&dt_idops_type, "int64_t" },
192178479Sjb{ "arg7", DT_IDENT_SCALAR, 0, DIF_VAR_ARG7, DT_ATTR_STABCMN, DT_VERS_1_0,
193178479Sjb	&dt_idops_type, "int64_t" },
194178479Sjb{ "arg8", DT_IDENT_SCALAR, 0, DIF_VAR_ARG8, DT_ATTR_STABCMN, DT_VERS_1_0,
195178479Sjb	&dt_idops_type, "int64_t" },
196178479Sjb{ "arg9", DT_IDENT_SCALAR, 0, DIF_VAR_ARG9, DT_ATTR_STABCMN, DT_VERS_1_0,
197178479Sjb	&dt_idops_type, "int64_t" },
198178479Sjb{ "args", DT_IDENT_ARRAY, 0, DIF_VAR_ARGS, DT_ATTR_STABCMN, DT_VERS_1_0,
199178479Sjb	&dt_idops_args, NULL },
200178479Sjb{ "avg", DT_IDENT_AGGFUNC, 0, DTRACEAGG_AVG, DT_ATTR_STABCMN, DT_VERS_1_0,
201178479Sjb	&dt_idops_func, "void(@)" },
202178479Sjb{ "basename", DT_IDENT_FUNC, 0, DIF_SUBR_BASENAME, DT_ATTR_STABCMN, DT_VERS_1_0,
203178479Sjb	&dt_idops_func, "string(const char *)" },
204178479Sjb{ "bcopy", DT_IDENT_FUNC, 0, DIF_SUBR_BCOPY, DT_ATTR_STABCMN, DT_VERS_1_0,
205178479Sjb	&dt_idops_func, "void(void *, void *, size_t)" },
206178479Sjb{ "breakpoint", DT_IDENT_ACTFUNC, 0, DT_ACT_BREAKPOINT,
207178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
208178479Sjb	&dt_idops_func, "void()" },
209178479Sjb{ "caller", DT_IDENT_SCALAR, 0, DIF_VAR_CALLER, DT_ATTR_STABCMN, DT_VERS_1_0,
210178479Sjb	&dt_idops_type, "uintptr_t" },
211178479Sjb{ "chill", DT_IDENT_ACTFUNC, 0, DT_ACT_CHILL, DT_ATTR_STABCMN, DT_VERS_1_0,
212178479Sjb	&dt_idops_func, "void(int)" },
213178479Sjb{ "cleanpath", DT_IDENT_FUNC, 0, DIF_SUBR_CLEANPATH, DT_ATTR_STABCMN,
214178479Sjb	DT_VERS_1_0, &dt_idops_func, "string(const char *)" },
215178479Sjb{ "clear", DT_IDENT_ACTFUNC, 0, DT_ACT_CLEAR, DT_ATTR_STABCMN, DT_VERS_1_0,
216178479Sjb	&dt_idops_func, "void(...)" },
217178479Sjb{ "commit", DT_IDENT_ACTFUNC, 0, DT_ACT_COMMIT, DT_ATTR_STABCMN, DT_VERS_1_0,
218178479Sjb	&dt_idops_func, "void(int)" },
219178479Sjb{ "copyin", DT_IDENT_FUNC, 0, DIF_SUBR_COPYIN, DT_ATTR_STABCMN, DT_VERS_1_0,
220178479Sjb	&dt_idops_func, "void *(uintptr_t, size_t)" },
221178479Sjb{ "copyinstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINSTR,
222178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
223178479Sjb	&dt_idops_func, "string(uintptr_t, [size_t])" },
224178479Sjb{ "copyinto", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINTO, DT_ATTR_STABCMN,
225178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(uintptr_t, size_t, void *)" },
226178479Sjb{ "copyout", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUT, DT_ATTR_STABCMN, DT_VERS_1_0,
227178479Sjb	&dt_idops_func, "void(void *, uintptr_t, size_t)" },
228178479Sjb{ "copyoutstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUTSTR,
229178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
230178479Sjb	&dt_idops_func, "void(char *, uintptr_t, size_t)" },
231178479Sjb{ "count", DT_IDENT_AGGFUNC, 0, DTRACEAGG_COUNT, DT_ATTR_STABCMN, DT_VERS_1_0,
232178479Sjb	&dt_idops_func, "void()" },
233178479Sjb{ "curthread", DT_IDENT_SCALAR, 0, DIF_VAR_CURTHREAD,
234178479Sjb	{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_PRIVATE,
235178479Sjb	DTRACE_CLASS_COMMON }, DT_VERS_1_0,
236178570Sjb#if defined(sun)
237178479Sjb	&dt_idops_type, "genunix`kthread_t *" },
238178570Sjb#else
239178570Sjb	&dt_idops_type, curthread_str },
240178570Sjb#endif
241178479Sjb{ "ddi_pathname", DT_IDENT_FUNC, 0, DIF_SUBR_DDI_PATHNAME,
242178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
243178479Sjb	&dt_idops_func, "string(void *, int64_t)" },
244178479Sjb{ "denormalize", DT_IDENT_ACTFUNC, 0, DT_ACT_DENORMALIZE, DT_ATTR_STABCMN,
245178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
246178479Sjb{ "dirname", DT_IDENT_FUNC, 0, DIF_SUBR_DIRNAME, DT_ATTR_STABCMN, DT_VERS_1_0,
247178479Sjb	&dt_idops_func, "string(const char *)" },
248178479Sjb{ "discard", DT_IDENT_ACTFUNC, 0, DT_ACT_DISCARD, DT_ATTR_STABCMN, DT_VERS_1_0,
249178479Sjb	&dt_idops_func, "void(int)" },
250178479Sjb{ "epid", DT_IDENT_SCALAR, 0, DIF_VAR_EPID, DT_ATTR_STABCMN, DT_VERS_1_0,
251178479Sjb	&dt_idops_type, "uint_t" },
252178479Sjb{ "errno", DT_IDENT_SCALAR, 0, DIF_VAR_ERRNO, DT_ATTR_STABCMN, DT_VERS_1_0,
253178479Sjb	&dt_idops_type, "int" },
254178570Sjb{ "execargs", DT_IDENT_SCALAR, 0, DIF_VAR_EXECARGS,
255178570Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
256178479Sjb{ "execname", DT_IDENT_SCALAR, 0, DIF_VAR_EXECNAME,
257178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
258178479Sjb{ "exit", DT_IDENT_ACTFUNC, 0, DT_ACT_EXIT, DT_ATTR_STABCMN, DT_VERS_1_0,
259178479Sjb	&dt_idops_func, "void(int)" },
260178479Sjb{ "freopen", DT_IDENT_ACTFUNC, 0, DT_ACT_FREOPEN, DT_ATTR_STABCMN,
261178479Sjb	DT_VERS_1_1, &dt_idops_func, "void(@, ...)" },
262178479Sjb{ "ftruncate", DT_IDENT_ACTFUNC, 0, DT_ACT_FTRUNCATE, DT_ATTR_STABCMN,
263178479Sjb	DT_VERS_1_0, &dt_idops_func, "void()" },
264178479Sjb{ "func", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
265178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
266178479Sjb{ "getmajor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMAJOR,
267178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
268178479Sjb	&dt_idops_func, "genunix`major_t(genunix`dev_t)" },
269178479Sjb{ "getminor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMINOR,
270178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
271178479Sjb	&dt_idops_func, "genunix`minor_t(genunix`dev_t)" },
272178479Sjb{ "htonl", DT_IDENT_FUNC, 0, DIF_SUBR_HTONL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
273178479Sjb	&dt_idops_func, "uint32_t(uint32_t)" },
274178479Sjb{ "htonll", DT_IDENT_FUNC, 0, DIF_SUBR_HTONLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
275178479Sjb	&dt_idops_func, "uint64_t(uint64_t)" },
276178479Sjb{ "htons", DT_IDENT_FUNC, 0, DIF_SUBR_HTONS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
277178479Sjb	&dt_idops_func, "uint16_t(uint16_t)" },
278178479Sjb{ "gid", DT_IDENT_SCALAR, 0, DIF_VAR_GID, DT_ATTR_STABCMN, DT_VERS_1_0,
279178479Sjb	&dt_idops_type, "gid_t" },
280178479Sjb{ "id", DT_IDENT_SCALAR, 0, DIF_VAR_ID, DT_ATTR_STABCMN, DT_VERS_1_0,
281178479Sjb	&dt_idops_type, "uint_t" },
282178479Sjb{ "index", DT_IDENT_FUNC, 0, DIF_SUBR_INDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
283178479Sjb	&dt_idops_func, "int(const char *, const char *, [int])" },
284178479Sjb{ "inet_ntoa", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA, DT_ATTR_STABCMN,
285178570Sjb#if defined(sun)
286178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(ipaddr_t *)" },
287178570Sjb#else
288178570Sjb	DT_VERS_1_5, &dt_idops_func, "string(in_addr_t *)" },
289178570Sjb#endif
290178479Sjb{ "inet_ntoa6", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA6, DT_ATTR_STABCMN,
291178570Sjb#if defined(sun)
292178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(in6_addr_t *)" },
293178570Sjb#else
294178570Sjb	DT_VERS_1_5, &dt_idops_func, "string(struct in6_addr *)" },
295178570Sjb#endif
296178479Sjb{ "inet_ntop", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOP, DT_ATTR_STABCMN,
297178479Sjb	DT_VERS_1_5, &dt_idops_func, "string(int, void *)" },
298178479Sjb{ "ipl", DT_IDENT_SCALAR, 0, DIF_VAR_IPL, DT_ATTR_STABCMN, DT_VERS_1_0,
299178479Sjb	&dt_idops_type, "uint_t" },
300178479Sjb{ "jstack", DT_IDENT_ACTFUNC, 0, DT_ACT_JSTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
301178479Sjb	&dt_idops_func, "stack(...)" },
302178479Sjb{ "lltostr", DT_IDENT_FUNC, 0, DIF_SUBR_LLTOSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
303248706Spfg	&dt_idops_func, "string(int64_t, [int])" },
304237624Spfg{ "llquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LLQUANTIZE, DT_ATTR_STABCMN,
305237624Spfg	DT_VERS_1_7, &dt_idops_func,
306237624Spfg	"void(@, int32_t, int32_t, int32_t, int32_t, ...)" },
307178479Sjb{ "lquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LQUANTIZE,
308178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
309178479Sjb	&dt_idops_func, "void(@, int32_t, int32_t, ...)" },
310178479Sjb{ "max", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MAX, DT_ATTR_STABCMN, DT_VERS_1_0,
311178479Sjb	&dt_idops_func, "void(@)" },
312178570Sjb{ "memref", DT_IDENT_FUNC, 0, DIF_SUBR_MEMREF, DT_ATTR_STABCMN, DT_VERS_1_1,
313178570Sjb	&dt_idops_func, "uintptr_t *(void *, size_t)" },
314178479Sjb{ "min", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MIN, DT_ATTR_STABCMN, DT_VERS_1_0,
315178479Sjb	&dt_idops_func, "void(@)" },
316178479Sjb{ "mod", DT_IDENT_ACTFUNC, 0, DT_ACT_MOD, DT_ATTR_STABCMN,
317178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
318178479Sjb{ "msgdsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGDSIZE,
319178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
320178479Sjb	&dt_idops_func, "size_t(mblk_t *)" },
321178479Sjb{ "msgsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGSIZE,
322178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
323178479Sjb	&dt_idops_func, "size_t(mblk_t *)" },
324178570Sjb#if defined(sun)
325178479Sjb{ "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
326178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
327178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
328178479Sjb{ "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
329178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
330178479Sjb	&dt_idops_func, "genunix`kthread_t *(genunix`kmutex_t *)" },
331178479Sjb{ "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
332178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
333178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
334178479Sjb{ "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
335178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
336178479Sjb	&dt_idops_func, "int(genunix`kmutex_t *)" },
337178570Sjb#else
338178570Sjb{ "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
339178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
340178570Sjb	&dt_idops_func, intmtx_str },
341178570Sjb{ "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
342178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
343178570Sjb	&dt_idops_func, threadmtx_str },
344178570Sjb{ "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
345178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
346178570Sjb	&dt_idops_func, intmtx_str },
347178570Sjb{ "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
348178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
349178570Sjb	&dt_idops_func, intmtx_str },
350178570Sjb#endif
351178479Sjb{ "ntohl", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
352178479Sjb	&dt_idops_func, "uint32_t(uint32_t)" },
353178479Sjb{ "ntohll", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
354178479Sjb	&dt_idops_func, "uint64_t(uint64_t)" },
355178479Sjb{ "ntohs", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
356178479Sjb	&dt_idops_func, "uint16_t(uint16_t)" },
357178479Sjb{ "normalize", DT_IDENT_ACTFUNC, 0, DT_ACT_NORMALIZE, DT_ATTR_STABCMN,
358178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
359178479Sjb{ "panic", DT_IDENT_ACTFUNC, 0, DT_ACT_PANIC, DT_ATTR_STABCMN, DT_VERS_1_0,
360178479Sjb	&dt_idops_func, "void()" },
361178479Sjb{ "pid", DT_IDENT_SCALAR, 0, DIF_VAR_PID, DT_ATTR_STABCMN, DT_VERS_1_0,
362178479Sjb	&dt_idops_type, "pid_t" },
363178479Sjb{ "ppid", DT_IDENT_SCALAR, 0, DIF_VAR_PPID, DT_ATTR_STABCMN, DT_VERS_1_0,
364178479Sjb	&dt_idops_type, "pid_t" },
365248708Spfg{ "print", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINT, DT_ATTR_STABCMN, DT_VERS_1_9,
366248708Spfg	&dt_idops_func, "void(@)" },
367178479Sjb{ "printa", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTA, DT_ATTR_STABCMN, DT_VERS_1_0,
368178479Sjb	&dt_idops_func, "void(@, ...)" },
369178479Sjb{ "printf", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTF, DT_ATTR_STABCMN, DT_VERS_1_0,
370178479Sjb	&dt_idops_func, "void(@, ...)" },
371178570Sjb{ "printm", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTM, DT_ATTR_STABCMN, DT_VERS_1_0,
372178570Sjb	&dt_idops_func, "void(size_t, uintptr_t *)" },
373178570Sjb{ "printt", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTT, DT_ATTR_STABCMN, DT_VERS_1_0,
374178570Sjb	&dt_idops_func, "void(size_t, uintptr_t *)" },
375178479Sjb{ "probefunc", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEFUNC,
376178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
377178479Sjb{ "probemod", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEMOD,
378178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
379178479Sjb{ "probename", DT_IDENT_SCALAR, 0, DIF_VAR_PROBENAME,
380178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
381178479Sjb{ "probeprov", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEPROV,
382178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
383178479Sjb{ "progenyof", DT_IDENT_FUNC, 0, DIF_SUBR_PROGENYOF,
384178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
385178479Sjb	&dt_idops_func, "int(pid_t)" },
386178479Sjb{ "quantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_QUANTIZE,
387178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
388178479Sjb	&dt_idops_func, "void(@, ...)" },
389178479Sjb{ "raise", DT_IDENT_ACTFUNC, 0, DT_ACT_RAISE, DT_ATTR_STABCMN, DT_VERS_1_0,
390178479Sjb	&dt_idops_func, "void(int)" },
391178479Sjb{ "rand", DT_IDENT_FUNC, 0, DIF_SUBR_RAND, DT_ATTR_STABCMN, DT_VERS_1_0,
392178479Sjb	&dt_idops_func, "int()" },
393178479Sjb{ "rindex", DT_IDENT_FUNC, 0, DIF_SUBR_RINDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
394178479Sjb	&dt_idops_func, "int(const char *, const char *, [int])" },
395178570Sjb#if defined(sun)
396178479Sjb{ "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
397178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
398178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
399178479Sjb{ "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
400178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
401178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
402178479Sjb{ "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
403178479Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
404178479Sjb	&dt_idops_func, "int(genunix`krwlock_t *)" },
405178570Sjb#else
406178570Sjb{ "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
407178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
408178570Sjb	&dt_idops_func, rwlock_str },
409178570Sjb{ "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
410178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
411178570Sjb	&dt_idops_func, rwlock_str },
412178570Sjb{ "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
413178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
414178570Sjb	&dt_idops_func, rwlock_str },
415178570Sjb#endif
416178479Sjb{ "self", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
417178479Sjb	&dt_idops_type, "void" },
418178479Sjb{ "setopt", DT_IDENT_ACTFUNC, 0, DT_ACT_SETOPT, DT_ATTR_STABCMN,
419178479Sjb	DT_VERS_1_2, &dt_idops_func, "void(const char *, [const char *])" },
420178479Sjb{ "speculate", DT_IDENT_ACTFUNC, 0, DT_ACT_SPECULATE,
421178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
422178479Sjb	&dt_idops_func, "void(int)" },
423178479Sjb{ "speculation", DT_IDENT_FUNC, 0, DIF_SUBR_SPECULATION,
424178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
425178479Sjb	&dt_idops_func, "int()" },
426178479Sjb{ "stack", DT_IDENT_ACTFUNC, 0, DT_ACT_STACK, DT_ATTR_STABCMN, DT_VERS_1_0,
427178479Sjb	&dt_idops_func, "stack(...)" },
428178479Sjb{ "stackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_STACKDEPTH,
429178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
430178479Sjb	&dt_idops_type, "uint32_t" },
431178479Sjb{ "stddev", DT_IDENT_AGGFUNC, 0, DTRACEAGG_STDDEV, DT_ATTR_STABCMN,
432178479Sjb	DT_VERS_1_6, &dt_idops_func, "void(@)" },
433178479Sjb{ "stop", DT_IDENT_ACTFUNC, 0, DT_ACT_STOP, DT_ATTR_STABCMN, DT_VERS_1_0,
434178479Sjb	&dt_idops_func, "void()" },
435178479Sjb{ "strchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
436178479Sjb	&dt_idops_func, "string(const char *, char)" },
437178479Sjb{ "strlen", DT_IDENT_FUNC, 0, DIF_SUBR_STRLEN, DT_ATTR_STABCMN, DT_VERS_1_0,
438178479Sjb	&dt_idops_func, "size_t(const char *)" },
439178479Sjb{ "strjoin", DT_IDENT_FUNC, 0, DIF_SUBR_STRJOIN, DT_ATTR_STABCMN, DT_VERS_1_0,
440178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
441178479Sjb{ "strrchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
442178479Sjb	&dt_idops_func, "string(const char *, char)" },
443178479Sjb{ "strstr", DT_IDENT_FUNC, 0, DIF_SUBR_STRSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
444178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
445178479Sjb{ "strtok", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOK, DT_ATTR_STABCMN, DT_VERS_1_1,
446178479Sjb	&dt_idops_func, "string(const char *, const char *)" },
447178479Sjb{ "substr", DT_IDENT_FUNC, 0, DIF_SUBR_SUBSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
448178479Sjb	&dt_idops_func, "string(const char *, int, [int])" },
449178479Sjb{ "sum", DT_IDENT_AGGFUNC, 0, DTRACEAGG_SUM, DT_ATTR_STABCMN, DT_VERS_1_0,
450178479Sjb	&dt_idops_func, "void(@)" },
451178570Sjb#if !defined(sun)
452178570Sjb{ "sx_isexclusive", DT_IDENT_FUNC, 0, DIF_SUBR_SX_ISEXCLUSIVE,
453178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
454178570Sjb	&dt_idops_func, sxlock_str },
455178570Sjb{ "sx_shared_held", DT_IDENT_FUNC, 0, DIF_SUBR_SX_SHARED_HELD,
456178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
457178570Sjb	&dt_idops_func, sxlock_str },
458178570Sjb{ "sx_exclusive_held", DT_IDENT_FUNC, 0, DIF_SUBR_SX_EXCLUSIVE_HELD,
459178570Sjb	DT_ATTR_EVOLCMN, DT_VERS_1_0,
460178570Sjb	&dt_idops_func, sxlock_str },
461178570Sjb#endif
462178479Sjb{ "sym", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
463178479Sjb	DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
464178479Sjb{ "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
465178479Sjb	&dt_idops_func, "void(@, ...)" },
466178479Sjb{ "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
467178479Sjb	&dt_idops_type, "void" },
468178479Sjb{ "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
469178479Sjb	&dt_idops_type, "id_t" },
470178479Sjb{ "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
471178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
472178479Sjb	&dt_idops_type, "uint64_t" },
473248706Spfg{ "tolower", DT_IDENT_FUNC, 0, DIF_SUBR_TOLOWER, DT_ATTR_STABCMN, DT_VERS_1_8,
474248706Spfg	&dt_idops_func, "string(const char *)" },
475248706Spfg{ "toupper", DT_IDENT_FUNC, 0, DIF_SUBR_TOUPPER, DT_ATTR_STABCMN, DT_VERS_1_8,
476248706Spfg	&dt_idops_func, "string(const char *)" },
477178479Sjb{ "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
478178479Sjb	&dt_idops_func, "void(@)" },
479178479Sjb{ "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
480178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
481248690Spfg	&dt_idops_func, "void(@, size_t, ...)" },
482178479Sjb{ "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
483178479Sjb	DT_VERS_1_0, &dt_idops_func, "void(...)" },
484178570Sjb{ "typeref", DT_IDENT_FUNC, 0, DIF_SUBR_TYPEREF, DT_ATTR_STABCMN, DT_VERS_1_1,
485178570Sjb	&dt_idops_func, "uintptr_t *(void *, size_t, string, size_t)" },
486178570Sjb#if defined(sun)
487178479Sjb{ "uaddr", DT_IDENT_ACTFUNC, 0, DT_ACT_UADDR, DT_ATTR_STABCMN,
488178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
489211554Srpaulo#endif
490178479Sjb{ "ucaller", DT_IDENT_SCALAR, 0, DIF_VAR_UCALLER, DT_ATTR_STABCMN,
491178479Sjb	DT_VERS_1_2, &dt_idops_type, "uint64_t" },
492211554Srpaulo#if defined(sun)
493178479Sjb{ "ufunc", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
494178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
495178570Sjb#endif
496178479Sjb{ "uid", DT_IDENT_SCALAR, 0, DIF_VAR_UID, DT_ATTR_STABCMN, DT_VERS_1_0,
497178479Sjb	&dt_idops_type, "uid_t" },
498178570Sjb#if defined(sun)
499178479Sjb{ "umod", DT_IDENT_ACTFUNC, 0, DT_ACT_UMOD, DT_ATTR_STABCMN,
500178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
501211554Srpaulo#endif
502178479Sjb{ "uregs", DT_IDENT_ARRAY, 0, DIF_VAR_UREGS, DT_ATTR_STABCMN, DT_VERS_1_0,
503178479Sjb	&dt_idops_regs, NULL },
504178479Sjb{ "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
505178479Sjb	&dt_idops_func, "stack(...)" },
506178479Sjb{ "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
507178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_2,
508178479Sjb	&dt_idops_type, "uint32_t" },
509211554Srpaulo#if defined(sun)
510178479Sjb{ "usym", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
511178479Sjb	DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
512178570Sjb#endif
513178479Sjb{ "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
514178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
515178479Sjb	&dt_idops_type, "uint64_t" },
516178479Sjb{ "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
517178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0,
518178479Sjb	&dt_idops_type, "int64_t" },
519178570Sjb#if defined(sun)
520178479Sjb{ "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
521178479Sjb	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
522178570Sjb#endif
523234691Srstone
524234691Srstone#if !defined(sun)
525234691Srstone{ "cpu", DT_IDENT_SCALAR, 0, DIF_VAR_CPU,
526234691Srstone	DT_ATTR_STABCMN, DT_VERS_1_6_3, &dt_idops_type, "int" },
527234691Srstone#endif
528234691Srstone
529178479Sjb{ NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
530178479Sjb};
531178479Sjb
532178479Sjb/*
533178479Sjb * Tables of ILP32 intrinsic integer and floating-point type templates to use
534178479Sjb * to populate the dynamic "C" CTF type container.
535178479Sjb */
536178479Sjbstatic const dt_intrinsic_t _dtrace_intrinsics_32[] = {
537178479Sjb{ "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
538178479Sjb{ "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
539178479Sjb{ "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
540178479Sjb{ "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
541178479Sjb{ "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
542178479Sjb{ "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
543178479Sjb{ "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
544178479Sjb{ "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
545178479Sjb{ "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
546178479Sjb{ "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
547178479Sjb{ "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
548178479Sjb{ "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
549178479Sjb{ "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
550178479Sjb{ "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
551178479Sjb{ "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
552178479Sjb{ "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
553178479Sjb{ "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
554178479Sjb{ "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
555178479Sjb{ "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
556178479Sjb{ "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
557178479Sjb{ "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
558178479Sjb{ "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
559178479Sjb{ "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
560178479Sjb{ "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
561178479Sjb{ "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
562178479Sjb{ "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
563178479Sjb{ "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
564178479Sjb{ "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
565178479Sjb{ NULL, { 0, 0, 0 }, 0 }
566178479Sjb};
567178479Sjb
568178479Sjb/*
569178479Sjb * Tables of LP64 intrinsic integer and floating-point type templates to use
570178479Sjb * to populate the dynamic "C" CTF type container.
571178479Sjb */
572178479Sjbstatic const dt_intrinsic_t _dtrace_intrinsics_64[] = {
573178479Sjb{ "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
574178479Sjb{ "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
575178479Sjb{ "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
576178479Sjb{ "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
577178479Sjb{ "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
578178479Sjb{ "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
579178479Sjb{ "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
580178479Sjb{ "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
581178479Sjb{ "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
582178479Sjb{ "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
583178479Sjb{ "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
584178479Sjb{ "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
585178479Sjb{ "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
586178479Sjb{ "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
587178479Sjb{ "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
588178479Sjb{ "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
589178479Sjb{ "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
590178479Sjb{ "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
591178479Sjb{ "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
592178479Sjb{ "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
593178479Sjb{ "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
594178479Sjb{ "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
595178479Sjb{ "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
596178479Sjb{ "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
597178479Sjb{ "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
598178479Sjb{ "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
599178479Sjb{ "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
600178479Sjb{ "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
601178479Sjb{ NULL, { 0, 0, 0 }, 0 }
602178479Sjb};
603178479Sjb
604178479Sjb/*
605178479Sjb * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
606178479Sjb * These aliases ensure that D definitions can use typical <sys/types.h> names.
607178479Sjb */
608178479Sjbstatic const dt_typedef_t _dtrace_typedefs_32[] = {
609178479Sjb{ "char", "int8_t" },
610178479Sjb{ "short", "int16_t" },
611178479Sjb{ "int", "int32_t" },
612178479Sjb{ "long long", "int64_t" },
613178479Sjb{ "int", "intptr_t" },
614178479Sjb{ "int", "ssize_t" },
615178479Sjb{ "unsigned char", "uint8_t" },
616178479Sjb{ "unsigned short", "uint16_t" },
617178479Sjb{ "unsigned", "uint32_t" },
618178479Sjb{ "unsigned long long", "uint64_t" },
619178479Sjb{ "unsigned char", "uchar_t" },
620178479Sjb{ "unsigned short", "ushort_t" },
621178479Sjb{ "unsigned", "uint_t" },
622178479Sjb{ "unsigned long", "ulong_t" },
623178479Sjb{ "unsigned long long", "u_longlong_t" },
624178479Sjb{ "int", "ptrdiff_t" },
625178479Sjb{ "unsigned", "uintptr_t" },
626178479Sjb{ "unsigned", "size_t" },
627178479Sjb{ "long", "id_t" },
628178479Sjb{ "long", "pid_t" },
629178479Sjb{ NULL, NULL }
630178479Sjb};
631178479Sjb
632178479Sjb/*
633178479Sjb * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
634178479Sjb * These aliases ensure that D definitions can use typical <sys/types.h> names.
635178479Sjb */
636178479Sjbstatic const dt_typedef_t _dtrace_typedefs_64[] = {
637178479Sjb{ "char", "int8_t" },
638178479Sjb{ "short", "int16_t" },
639178479Sjb{ "int", "int32_t" },
640178479Sjb{ "long", "int64_t" },
641178479Sjb{ "long", "intptr_t" },
642178479Sjb{ "long", "ssize_t" },
643178479Sjb{ "unsigned char", "uint8_t" },
644178479Sjb{ "unsigned short", "uint16_t" },
645178479Sjb{ "unsigned", "uint32_t" },
646178479Sjb{ "unsigned long", "uint64_t" },
647178479Sjb{ "unsigned char", "uchar_t" },
648178479Sjb{ "unsigned short", "ushort_t" },
649178479Sjb{ "unsigned", "uint_t" },
650178479Sjb{ "unsigned long", "ulong_t" },
651178479Sjb{ "unsigned long long", "u_longlong_t" },
652178479Sjb{ "long", "ptrdiff_t" },
653178479Sjb{ "unsigned long", "uintptr_t" },
654178479Sjb{ "unsigned long", "size_t" },
655178479Sjb{ "int", "id_t" },
656178479Sjb{ "int", "pid_t" },
657178479Sjb{ NULL, NULL }
658178479Sjb};
659178479Sjb
660178479Sjb/*
661178479Sjb * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
662178479Sjb * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
663178479Sjb */
664178479Sjbstatic const dt_intdesc_t _dtrace_ints_32[] = {
665178479Sjb{ "int", NULL, CTF_ERR, 0x7fffffffULL },
666178479Sjb{ "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
667178479Sjb{ "long", NULL, CTF_ERR, 0x7fffffffULL },
668178479Sjb{ "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
669178479Sjb{ "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
670178479Sjb{ "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
671178479Sjb};
672178479Sjb
673178479Sjb/*
674178479Sjb * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
675178479Sjb * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
676178479Sjb */
677178479Sjbstatic const dt_intdesc_t _dtrace_ints_64[] = {
678178479Sjb{ "int", NULL, CTF_ERR, 0x7fffffffULL },
679178479Sjb{ "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
680178479Sjb{ "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
681178479Sjb{ "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
682178479Sjb{ "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
683178479Sjb{ "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
684178479Sjb};
685178479Sjb
686178479Sjb/*
687178479Sjb * Table of macro variable templates used to populate the macro identifier hash
688178479Sjb * when a new dtrace client open occurs.  Values are set by dtrace_update().
689178479Sjb */
690178479Sjbstatic const dt_ident_t _dtrace_macros[] = {
691178479Sjb{ "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
692178479Sjb{ "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
693178479Sjb{ "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
694178479Sjb{ "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
695178479Sjb{ "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
696178479Sjb{ "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
697178479Sjb{ "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
698178479Sjb{ "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
699178479Sjb{ "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
700178479Sjb{ "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
701178479Sjb{ "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
702178479Sjb{ NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
703178479Sjb};
704178479Sjb
705178479Sjb/*
706178479Sjb * Hard-wired definition string to be compiled and cached every time a new
707178479Sjb * DTrace library handle is initialized.  This string should only be used to
708178479Sjb * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
709178479Sjb */
710178479Sjbstatic const char _dtrace_hardwire[] = "\
711178479Sjbinline long NULL = 0; \n\
712178479Sjb#pragma D binding \"1.0\" NULL\n\
713178479Sjb";
714178479Sjb
715178479Sjb/*
716178479Sjb * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
717178479Sjb * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
718178479Sjb * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
719178479Sjb * relying on the fact that when running dtrace(1M), isaexec will invoke the
720178479Sjb * binary with the same bitness as the kernel, which is what we want by default
721178479Sjb * when generating our DIF.  The user can override the choice using oflags.
722178479Sjb */
723178479Sjbstatic const dtrace_conf_t _dtrace_conf = {
724178479Sjb	DIF_VERSION,		/* dtc_difversion */
725178479Sjb	DIF_DIR_NREGS,		/* dtc_difintregs */
726178479Sjb	DIF_DTR_NREGS,		/* dtc_diftupregs */
727178479Sjb	CTF_MODEL_NATIVE	/* dtc_ctfmodel */
728178479Sjb};
729178479Sjb
730178479Sjbconst dtrace_attribute_t _dtrace_maxattr = {
731178479Sjb	DTRACE_STABILITY_MAX,
732178479Sjb	DTRACE_STABILITY_MAX,
733178479Sjb	DTRACE_CLASS_MAX
734178479Sjb};
735178479Sjb
736178479Sjbconst dtrace_attribute_t _dtrace_defattr = {
737178479Sjb	DTRACE_STABILITY_STABLE,
738178479Sjb	DTRACE_STABILITY_STABLE,
739178479Sjb	DTRACE_CLASS_COMMON
740178479Sjb};
741178479Sjb
742178479Sjbconst dtrace_attribute_t _dtrace_symattr = {
743178479Sjb	DTRACE_STABILITY_PRIVATE,
744178479Sjb	DTRACE_STABILITY_PRIVATE,
745178479Sjb	DTRACE_CLASS_UNKNOWN
746178479Sjb};
747178479Sjb
748178479Sjbconst dtrace_attribute_t _dtrace_typattr = {
749178479Sjb	DTRACE_STABILITY_PRIVATE,
750178479Sjb	DTRACE_STABILITY_PRIVATE,
751178479Sjb	DTRACE_CLASS_UNKNOWN
752178479Sjb};
753178479Sjb
754178479Sjbconst dtrace_attribute_t _dtrace_prvattr = {
755178479Sjb	DTRACE_STABILITY_PRIVATE,
756178479Sjb	DTRACE_STABILITY_PRIVATE,
757178479Sjb	DTRACE_CLASS_UNKNOWN
758178479Sjb};
759178479Sjb
760178479Sjbconst dtrace_pattr_t _dtrace_prvdesc = {
761178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
762178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
763178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
764178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
765178479Sjb{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
766178479Sjb};
767178479Sjb
768178570Sjb#if defined(sun)
769178479Sjbconst char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
770178479Sjbconst char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
771178570Sjb#else
772178570Sjbconst char *_dtrace_defcpp = "cpp"; /* default cpp(1) to invoke */
773178570Sjbconst char *_dtrace_defld = "ld";   /* default ld(1) to invoke */
774178570Sjb#endif
775178479Sjb
776178479Sjbconst char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
777178570Sjb#if defined(sun)
778178479Sjbconst char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
779178570Sjb#else
780178570Sjbconst char *_dtrace_provdir = "/dev/dtrace"; /* provider directory */
781178570Sjb#endif
782178479Sjb
783178479Sjbint _dtrace_strbuckets = 211;	/* default number of hash buckets (prime) */
784178479Sjbint _dtrace_intbuckets = 256;	/* default number of integer buckets (Pof2) */
785178479Sjbuint_t _dtrace_strsize = 256;	/* default size of string intrinsic type */
786178479Sjbuint_t _dtrace_stkindent = 14;	/* default whitespace indent for stack/ustack */
787178479Sjbuint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
788178479Sjbuint_t _dtrace_pidlrulim = 8;	/* default number of pid handles to cache */
789178479Sjbsize_t _dtrace_bufsize = 512;	/* default dt_buf_create() size */
790178479Sjbint _dtrace_argmax = 32;	/* default maximum number of probe arguments */
791178479Sjb
792178479Sjbint _dtrace_debug = 0;		/* debug messages enabled (off) */
793178479Sjbconst char *const _dtrace_version = DT_VERS_STRING; /* API version string */
794178479Sjbint _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
795178479Sjb
796178479Sjbtypedef struct dt_fdlist {
797178479Sjb	int *df_fds;		/* array of provider driver file descriptors */
798178479Sjb	uint_t df_ents;		/* number of valid elements in df_fds[] */
799178479Sjb	uint_t df_size;		/* size of df_fds[] */
800178479Sjb} dt_fdlist_t;
801178479Sjb
802178570Sjb#if defined(sun)
803178479Sjb#pragma init(_dtrace_init)
804178570Sjb#else
805178570Sjbvoid _dtrace_init(void) __attribute__ ((constructor));
806178570Sjb#endif
807178479Sjbvoid
808178479Sjb_dtrace_init(void)
809178479Sjb{
810178479Sjb	_dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
811178479Sjb
812178479Sjb	for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
813178479Sjb		if (rd_init(_dtrace_rdvers) == RD_OK)
814178479Sjb			break;
815178479Sjb	}
816178570Sjb#if defined(__i386__)
817178570Sjb	/* make long doubles 64 bits -sson */
818178570Sjb	(void) fpsetprec(FP_PE);
819178570Sjb#endif
820178479Sjb}
821178479Sjb
822178479Sjbstatic dtrace_hdl_t *
823178479Sjbset_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
824178479Sjb{
825178479Sjb	if (dtp != NULL)
826178479Sjb		dtrace_close(dtp);
827178479Sjb	if (errp != NULL)
828178479Sjb		*errp = err;
829178479Sjb	return (NULL);
830178479Sjb}
831178479Sjb
832178479Sjbstatic void
833178479Sjbdt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
834178479Sjb{
835178479Sjb	dt_provmod_t *prov;
836178479Sjb	char path[PATH_MAX];
837178570Sjb	int fd;
838178570Sjb#if defined(sun)
839178479Sjb	struct dirent *dp, *ep;
840178479Sjb	DIR *dirp;
841178479Sjb
842178479Sjb	if ((dirp = opendir(_dtrace_provdir)) == NULL)
843178479Sjb		return; /* failed to open directory; just skip it */
844178479Sjb
845178479Sjb	ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
846178479Sjb	bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
847178479Sjb
848178479Sjb	while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
849178479Sjb		if (dp->d_name[0] == '.')
850178479Sjb			continue; /* skip "." and ".." */
851178479Sjb
852178479Sjb		if (dfp->df_ents == dfp->df_size) {
853178479Sjb			uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
854178479Sjb			int *fds = realloc(dfp->df_fds, size * sizeof (int));
855178479Sjb
856178479Sjb			if (fds == NULL)
857178479Sjb				break; /* skip the rest of this directory */
858178479Sjb
859178479Sjb			dfp->df_fds = fds;
860178479Sjb			dfp->df_size = size;
861178479Sjb		}
862178479Sjb
863178479Sjb		(void) snprintf(path, sizeof (path), "%s/%s",
864178479Sjb		    _dtrace_provdir, dp->d_name);
865178479Sjb
866178479Sjb		if ((fd = open(path, O_RDONLY)) == -1)
867178479Sjb			continue; /* failed to open driver; just skip it */
868178479Sjb
869178479Sjb		if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
870178479Sjb		    (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
871178479Sjb			free(prov);
872178479Sjb			(void) close(fd);
873178479Sjb			break;
874178479Sjb		}
875178479Sjb
876178479Sjb		(void) strcpy(prov->dp_name, dp->d_name);
877178479Sjb		prov->dp_next = *provmod;
878178479Sjb		*provmod = prov;
879178479Sjb
880178479Sjb		dt_dprintf("opened provider %s\n", dp->d_name);
881178479Sjb		dfp->df_fds[dfp->df_ents++] = fd;
882178479Sjb	}
883178479Sjb
884178479Sjb	(void) closedir(dirp);
885178570Sjb#else
886178570Sjb	char	*p;
887178570Sjb	char	*p1;
888178570Sjb	char	*p_providers = NULL;
889178570Sjb	int	error;
890178570Sjb	size_t	len = 0;
891178570Sjb
892178570Sjb	/*
893178570Sjb	 * Loop to allocate/reallocate memory for the string of provider
894178570Sjb	 * names and retry:
895178570Sjb	 */
896178570Sjb	while(1) {
897178570Sjb		/*
898178570Sjb		 * The first time around, get the string length. The next time,
899178570Sjb		 * hopefully we've allocated enough memory.
900178570Sjb		 */
901178570Sjb		error = sysctlbyname("debug.dtrace.providers",p_providers,&len,NULL,0);
902178570Sjb		if (len == 0)
903178570Sjb			/* No providers? That's strange. Where's dtrace? */
904178570Sjb			break;
905178570Sjb		else if (error == 0 && p_providers == NULL) {
906178570Sjb			/*
907178570Sjb			 * Allocate the initial memory which should be enough
908178570Sjb			 * unless another provider loads before we have
909178570Sjb			 * time to go back and get the string.
910178570Sjb			 */
911178570Sjb			if ((p_providers = malloc(len)) == NULL)
912178570Sjb				/* How do we report errors here? */
913178570Sjb				return;
914178570Sjb		} else if (error == -1 && errno == ENOMEM) {
915178570Sjb			/*
916178570Sjb			 * The current buffer isn't large enough, so
917178570Sjb			 * reallocate it. We normally won't need to do this
918178570Sjb			 * because providers aren't being loaded all the time.
919178570Sjb			 */
920178570Sjb			if ((p = realloc(p_providers,len)) == NULL)
921178570Sjb				/* How do we report errors here? */
922178570Sjb				return;
923178570Sjb			p_providers = p;
924178570Sjb		} else
925178570Sjb			break;
926178570Sjb	}
927178570Sjb
928178570Sjb	/* Check if we got a string of provider names: */
929178570Sjb	if (error == 0 && len > 0 && p_providers != NULL) {
930178570Sjb		p = p_providers;
931178570Sjb
932178570Sjb		/*
933178570Sjb		 * Parse the string containing the space separated
934178570Sjb		 * provider names.
935178570Sjb		 */
936178570Sjb		while ((p1 = strsep(&p," ")) != NULL) {
937178570Sjb			if (dfp->df_ents == dfp->df_size) {
938178570Sjb				uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
939178570Sjb				int *fds = realloc(dfp->df_fds, size * sizeof (int));
940178570Sjb
941178570Sjb				if (fds == NULL)
942178570Sjb					break;
943178570Sjb
944178570Sjb				dfp->df_fds = fds;
945178570Sjb				dfp->df_size = size;
946178570Sjb			}
947178570Sjb
948178570Sjb			(void) snprintf(path, sizeof (path), "/dev/dtrace/%s", p1);
949178570Sjb
950178570Sjb			if ((fd = open(path, O_RDONLY)) == -1)
951178570Sjb				continue; /* failed to open driver; just skip it */
952178570Sjb
953178570Sjb			if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
954178570Sjb			    (prov->dp_name = malloc(strlen(p1) + 1)) == NULL) {
955178570Sjb				free(prov);
956178570Sjb				(void) close(fd);
957178570Sjb				break;
958178570Sjb			}
959178570Sjb
960178570Sjb			(void) strcpy(prov->dp_name, p1);
961178570Sjb			prov->dp_next = *provmod;
962178570Sjb			*provmod = prov;
963178570Sjb
964178570Sjb			dt_dprintf("opened provider %s\n", p1);
965178570Sjb			dfp->df_fds[dfp->df_ents++] = fd;
966178570Sjb		}
967178570Sjb	}
968178570Sjb	if (p_providers != NULL)
969178570Sjb		free(p_providers);
970178570Sjb#endif
971178479Sjb}
972178479Sjb
973178479Sjbstatic void
974178479Sjbdt_provmod_destroy(dt_provmod_t **provmod)
975178479Sjb{
976178479Sjb	dt_provmod_t *next, *current;
977178479Sjb
978178479Sjb	for (current = *provmod; current != NULL; current = next) {
979178479Sjb		next = current->dp_next;
980178479Sjb		free(current->dp_name);
981178479Sjb		free(current);
982178479Sjb	}
983178479Sjb
984178479Sjb	*provmod = NULL;
985178479Sjb}
986178479Sjb
987178570Sjb#if defined(sun)
988178479Sjbstatic const char *
989178479Sjbdt_get_sysinfo(int cmd, char *buf, size_t len)
990178479Sjb{
991178479Sjb	ssize_t rv = sysinfo(cmd, buf, len);
992178479Sjb	char *p = buf;
993178479Sjb
994178479Sjb	if (rv < 0 || rv > len)
995178479Sjb		(void) snprintf(buf, len, "%s", "Unknown");
996178479Sjb
997178479Sjb	while ((p = strchr(p, '.')) != NULL)
998178479Sjb		*p++ = '_';
999178479Sjb
1000178479Sjb	return (buf);
1001178479Sjb}
1002178570Sjb#endif
1003178479Sjb
1004178479Sjbstatic dtrace_hdl_t *
1005178479Sjbdt_vopen(int version, int flags, int *errp,
1006178479Sjb    const dtrace_vector_t *vector, void *arg)
1007178479Sjb{
1008178479Sjb	dtrace_hdl_t *dtp = NULL;
1009178479Sjb	int dtfd = -1, ftfd = -1, fterr = 0;
1010178479Sjb	dtrace_prog_t *pgp;
1011178479Sjb	dt_module_t *dmp;
1012178479Sjb	dt_provmod_t *provmod = NULL;
1013178479Sjb	int i, err;
1014178479Sjb	struct rlimit rl;
1015178479Sjb
1016178479Sjb	const dt_intrinsic_t *dinp;
1017178479Sjb	const dt_typedef_t *dtyp;
1018178479Sjb	const dt_ident_t *idp;
1019178479Sjb
1020178479Sjb	dtrace_typeinfo_t dtt;
1021178479Sjb	ctf_funcinfo_t ctc;
1022178479Sjb	ctf_arinfo_t ctr;
1023178479Sjb
1024178479Sjb	dt_fdlist_t df = { NULL, 0, 0 };
1025178479Sjb
1026178479Sjb	char isadef[32], utsdef[32];
1027178479Sjb	char s1[64], s2[64];
1028178479Sjb
1029178479Sjb	if (version <= 0)
1030178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1031178479Sjb
1032178479Sjb	if (version > DTRACE_VERSION)
1033178479Sjb		return (set_open_errno(dtp, errp, EDT_VERSION));
1034178479Sjb
1035178479Sjb	if (version < DTRACE_VERSION) {
1036178479Sjb		/*
1037178479Sjb		 * Currently, increasing the library version number is used to
1038178479Sjb		 * denote a binary incompatible change.  That is, a consumer
1039178479Sjb		 * of the library cannot run on a version of the library with
1040178479Sjb		 * a higher DTRACE_VERSION number than the consumer compiled
1041178479Sjb		 * against.  Once the library API has been committed to,
1042178479Sjb		 * backwards binary compatibility will be required; at that
1043178479Sjb		 * time, this check should change to return EDT_OVERSION only
1044178479Sjb		 * if the specified version number is less than the version
1045178479Sjb		 * number at the time of interface commitment.
1046178479Sjb		 */
1047178479Sjb		return (set_open_errno(dtp, errp, EDT_OVERSION));
1048178479Sjb	}
1049178479Sjb
1050178479Sjb	if (flags & ~DTRACE_O_MASK)
1051178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1052178479Sjb
1053178479Sjb	if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
1054178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1055178479Sjb
1056178479Sjb	if (vector == NULL && arg != NULL)
1057178479Sjb		return (set_open_errno(dtp, errp, EINVAL));
1058178479Sjb
1059178479Sjb	if (elf_version(EV_CURRENT) == EV_NONE)
1060178479Sjb		return (set_open_errno(dtp, errp, EDT_ELFVERSION));
1061178479Sjb
1062178479Sjb	if (vector != NULL || (flags & DTRACE_O_NODEV))
1063178479Sjb		goto alloc; /* do not attempt to open dtrace device */
1064178479Sjb
1065178479Sjb	/*
1066178479Sjb	 * Before we get going, crank our limit on file descriptors up to the
1067178479Sjb	 * hard limit.  This is to allow for the fact that libproc keeps file
1068178479Sjb	 * descriptors to objects open for the lifetime of the proc handle;
1069178479Sjb	 * without raising our hard limit, we would have an acceptably small
1070178479Sjb	 * bound on the number of processes that we could concurrently
1071178479Sjb	 * instrument with the pid provider.
1072178479Sjb	 */
1073178479Sjb	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1074178479Sjb		rl.rlim_cur = rl.rlim_max;
1075178479Sjb		(void) setrlimit(RLIMIT_NOFILE, &rl);
1076178479Sjb	}
1077178479Sjb
1078178479Sjb	/*
1079178479Sjb	 * Get the device path of each of the providers.  We hold them open
1080178479Sjb	 * in the df.df_fds list until we open the DTrace driver itself,
1081178479Sjb	 * allowing us to see all of the probes provided on this system.  Once
1082178479Sjb	 * we have the DTrace driver open, we can safely close all the providers
1083178479Sjb	 * now that they have registered with the framework.
1084178479Sjb	 */
1085178479Sjb	dt_provmod_open(&provmod, &df);
1086178479Sjb
1087178479Sjb	dtfd = open("/dev/dtrace/dtrace", O_RDWR);
1088178479Sjb	err = errno; /* save errno from opening dtfd */
1089254197Srpaulo#if defined(__FreeBSD__)
1090254197Srpaulo	/*
1091254197Srpaulo	 * Automatically load the 'dtraceall' module if we couldn't open the
1092254197Srpaulo	 * char device.
1093254197Srpaulo	 */
1094254197Srpaulo	if (err == ENOENT && modfind("dtraceall") < 0) {
1095254197Srpaulo		kldload("dtraceall"); /* ignore the error */
1096254197Srpaulo		dtfd = open("/dev/dtrace/dtrace", O_RDWR);
1097254197Srpaulo		err = errno;
1098254197Srpaulo	}
1099254197Srpaulo#endif
1100178570Sjb#if defined(sun)
1101178479Sjb	ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
1102178570Sjb#else
1103178570Sjb	ftfd = open("/dev/dtrace/fasttrap", O_RDWR);
1104178570Sjb#endif
1105178479Sjb	fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
1106178479Sjb
1107178479Sjb	while (df.df_ents-- != 0)
1108178479Sjb		(void) close(df.df_fds[df.df_ents]);
1109178479Sjb
1110178479Sjb	free(df.df_fds);
1111178479Sjb
1112178479Sjb	/*
1113178479Sjb	 * If we failed to open the dtrace device, fail dtrace_open().
1114178479Sjb	 * We convert some kernel errnos to custom libdtrace errnos to
1115178479Sjb	 * improve the resulting message from the usual strerror().
1116178479Sjb	 */
1117178479Sjb	if (dtfd == -1) {
1118178479Sjb		dt_provmod_destroy(&provmod);
1119178479Sjb		switch (err) {
1120178479Sjb		case ENOENT:
1121178479Sjb			err = EDT_NOENT;
1122178479Sjb			break;
1123178479Sjb		case EBUSY:
1124178479Sjb			err = EDT_BUSY;
1125178479Sjb			break;
1126178479Sjb		case EACCES:
1127178479Sjb			err = EDT_ACCESS;
1128178479Sjb			break;
1129178479Sjb		}
1130178479Sjb		return (set_open_errno(dtp, errp, err));
1131178479Sjb	}
1132178479Sjb
1133178479Sjb	(void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
1134178479Sjb	(void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
1135178479Sjb
1136178479Sjballoc:
1137178479Sjb	if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
1138178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1139178479Sjb
1140178479Sjb	bzero(dtp, sizeof (dtrace_hdl_t));
1141178479Sjb	dtp->dt_oflags = flags;
1142211554Srpaulo#if defined(sun)
1143178479Sjb	dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
1144211554Srpaulo#else
1145249884Savg	dtp->dt_prcmode = DT_PROC_STOP_MAIN;
1146211554Srpaulo#endif
1147178479Sjb	dtp->dt_linkmode = DT_LINK_KERNEL;
1148178479Sjb	dtp->dt_linktype = DT_LTYP_ELF;
1149178479Sjb	dtp->dt_xlatemode = DT_XL_STATIC;
1150178479Sjb	dtp->dt_stdcmode = DT_STDC_XA;
1151178479Sjb	dtp->dt_version = version;
1152178479Sjb	dtp->dt_fd = dtfd;
1153178479Sjb	dtp->dt_ftfd = ftfd;
1154178479Sjb	dtp->dt_fterr = fterr;
1155178479Sjb	dtp->dt_cdefs_fd = -1;
1156178479Sjb	dtp->dt_ddefs_fd = -1;
1157178570Sjb#if defined(sun)
1158178479Sjb	dtp->dt_stdout_fd = -1;
1159178570Sjb#else
1160178570Sjb	dtp->dt_freopen_fp = NULL;
1161178570Sjb#endif
1162178479Sjb	dtp->dt_modbuckets = _dtrace_strbuckets;
1163178479Sjb	dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
1164178479Sjb	dtp->dt_provbuckets = _dtrace_strbuckets;
1165178479Sjb	dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
1166249573Spfg	dt_proc_hash_create(dtp);
1167178479Sjb	dtp->dt_vmax = DT_VERS_LATEST;
1168178479Sjb	dtp->dt_cpp_path = strdup(_dtrace_defcpp);
1169178479Sjb	dtp->dt_cpp_argv = malloc(sizeof (char *));
1170178479Sjb	dtp->dt_cpp_argc = 1;
1171178479Sjb	dtp->dt_cpp_args = 1;
1172178479Sjb	dtp->dt_ld_path = strdup(_dtrace_defld);
1173178479Sjb	dtp->dt_provmod = provmod;
1174178479Sjb	dtp->dt_vector = vector;
1175178479Sjb	dtp->dt_varg = arg;
1176178479Sjb	dt_dof_init(dtp);
1177178479Sjb	(void) uname(&dtp->dt_uts);
1178178479Sjb
1179178479Sjb	if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
1180249573Spfg	    dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
1181249573Spfg	    dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
1182178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1183178479Sjb
1184178479Sjb	for (i = 0; i < DTRACEOPT_MAX; i++)
1185178479Sjb		dtp->dt_options[i] = DTRACEOPT_UNSET;
1186178479Sjb
1187178479Sjb	dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
1188178479Sjb
1189178570Sjb#if defined(sun)
1190178479Sjb	(void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
1191178479Sjb	    (uint_t)(sizeof (void *) * NBBY));
1192178479Sjb
1193178479Sjb	(void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
1194178479Sjb	    dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
1195178479Sjb	    dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
1196178479Sjb
1197178479Sjb	if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
1198178479Sjb	    dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
1199178479Sjb	    dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
1200178479Sjb	    dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
1201178479Sjb	    dt_cpp_add_arg(dtp, isadef) == NULL ||
1202178479Sjb	    dt_cpp_add_arg(dtp, utsdef) == NULL)
1203178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1204178570Sjb#endif
1205178479Sjb
1206178479Sjb	if (flags & DTRACE_O_NODEV)
1207178479Sjb		bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
1208178479Sjb	else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
1209178479Sjb		return (set_open_errno(dtp, errp, errno));
1210178479Sjb
1211178479Sjb	if (flags & DTRACE_O_LP64)
1212178479Sjb		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
1213178479Sjb	else if (flags & DTRACE_O_ILP32)
1214178479Sjb		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
1215178479Sjb
1216178479Sjb#ifdef __sparc
1217178479Sjb	/*
1218178479Sjb	 * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
1219178479Sjb	 * and __sparcv9 is defined if we are doing a 64-bit compile.
1220178479Sjb	 */
1221178479Sjb	if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
1222178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1223178479Sjb
1224178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
1225178479Sjb	    dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
1226178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1227178479Sjb#endif
1228178479Sjb
1229178570Sjb#if defined(sun)
1230178479Sjb#ifdef __x86
1231178479Sjb	/*
1232178479Sjb	 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
1233178479Sjb	 * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
1234178479Sjb	 * they are defined exclusive of one another (see PSARC 2004/619).
1235178479Sjb	 */
1236178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1237178479Sjb		if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
1238178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1239178479Sjb	} else {
1240178479Sjb		if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
1241178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1242178479Sjb	}
1243178479Sjb#endif
1244178570Sjb#else
1245178570Sjb#if defined(__amd64__) || defined(__i386__)
1246178570Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1247178570Sjb		if (dt_cpp_add_arg(dtp, "-m64") == NULL)
1248178570Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1249178570Sjb	} else {
1250178570Sjb		if (dt_cpp_add_arg(dtp, "-m32") == NULL)
1251178570Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1252178570Sjb	}
1253178570Sjb#endif
1254178570Sjb#endif
1255178479Sjb
1256178479Sjb	if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1257178479Sjb		return (set_open_errno(dtp, errp, EDT_DIFVERS));
1258178479Sjb
1259178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1260178479Sjb		bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1261178479Sjb	else
1262178479Sjb		bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1263178479Sjb
1264178570Sjb	/*
1265178570Sjb	 * On FreeBSD the kernel module name can't be hard-coded. The
1266178570Sjb	 * 'kern.bootfile' sysctl value tells us exactly which file is being
1267178570Sjb	 * used as the kernel.
1268178570Sjb	 */
1269178570Sjb#if !defined(sun)
1270178570Sjb	{
1271178570Sjb	char bootfile[MAXPATHLEN];
1272178570Sjb	char *p;
1273178570Sjb	int i;
1274178570Sjb	size_t len = sizeof(bootfile);
1275178570Sjb
1276178570Sjb	/* This call shouldn't fail, but use a default just in case. */
1277178570Sjb	if (sysctlbyname("kern.bootfile", bootfile, &len, NULL, 0) != 0)
1278178570Sjb		strlcpy(bootfile, "kernel", sizeof(bootfile));
1279178570Sjb
1280178570Sjb	if ((p = strrchr(bootfile, '/')) != NULL)
1281178570Sjb		p++;
1282178570Sjb	else
1283178570Sjb		p = bootfile;
1284178570Sjb
1285178570Sjb	/*
1286178570Sjb	 * Format the global variables based on the kernel module name.
1287178570Sjb	 */
1288178570Sjb	snprintf(curthread_str, sizeof(curthread_str), "%s`struct thread *",p);
1289178570Sjb	snprintf(intmtx_str, sizeof(intmtx_str), "int(%s`struct mtx *)",p);
1290178570Sjb	snprintf(threadmtx_str, sizeof(threadmtx_str), "struct thread *(%s`struct mtx *)",p);
1291178570Sjb	snprintf(rwlock_str, sizeof(rwlock_str), "int(%s`struct rwlock *)",p);
1292178570Sjb	snprintf(sxlock_str, sizeof(sxlock_str), "int(%s`struct sxlock *)",p);
1293178570Sjb	}
1294178570Sjb#endif
1295178570Sjb
1296178479Sjb	dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1297178479Sjb	dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1298178479Sjb	    DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1299178479Sjb
1300178479Sjb	dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1301178479Sjb	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1302178479Sjb
1303178479Sjb	dtp->dt_tls = dt_idhash_create("thread local", NULL,
1304178479Sjb	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1305178479Sjb
1306178479Sjb	if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1307178479Sjb	    dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1308178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1309178479Sjb
1310178479Sjb	/*
1311178479Sjb	 * Populate the dt_macros identifier hash table by hand: we can't use
1312178479Sjb	 * the dt_idhash_populate() mechanism because we're not yet compiling
1313178479Sjb	 * and dtrace_update() needs to immediately reference these idents.
1314178479Sjb	 */
1315178479Sjb	for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1316178479Sjb		if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1317178479Sjb		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1318178479Sjb		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1319178479Sjb		    idp->di_iarg, 0) == NULL)
1320178479Sjb			return (set_open_errno(dtp, errp, EDT_NOMEM));
1321178479Sjb	}
1322178479Sjb
1323178479Sjb	/*
1324178479Sjb	 * Update the module list using /system/object and load the values for
1325178479Sjb	 * the macro variable definitions according to the current process.
1326178479Sjb	 */
1327178479Sjb	dtrace_update(dtp);
1328178479Sjb
1329178479Sjb	/*
1330178479Sjb	 * Select the intrinsics and typedefs we want based on the data model.
1331178479Sjb	 * The intrinsics are under "C".  The typedefs are added under "D".
1332178479Sjb	 */
1333178479Sjb	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1334178479Sjb		dinp = _dtrace_intrinsics_32;
1335178479Sjb		dtyp = _dtrace_typedefs_32;
1336178479Sjb	} else {
1337178479Sjb		dinp = _dtrace_intrinsics_64;
1338178479Sjb		dtyp = _dtrace_typedefs_64;
1339178479Sjb	}
1340178479Sjb
1341178479Sjb	/*
1342178479Sjb	 * Create a dynamic CTF container under the "C" scope for intrinsic
1343178479Sjb	 * types and types defined in ANSI-C header files that are included.
1344178479Sjb	 */
1345178479Sjb	if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1346178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1347178479Sjb
1348178479Sjb	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1349178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1350178479Sjb
1351178479Sjb	dt_dprintf("created CTF container for %s (%p)\n",
1352178479Sjb	    dmp->dm_name, (void *)dmp->dm_ctfp);
1353178479Sjb
1354178479Sjb	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1355178479Sjb	ctf_setspecific(dmp->dm_ctfp, dmp);
1356178479Sjb
1357178479Sjb	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1358178479Sjb	dmp->dm_modid = -1; /* no module ID */
1359178479Sjb
1360178479Sjb	/*
1361178479Sjb	 * Fill the dynamic "C" CTF container with all of the intrinsic
1362178479Sjb	 * integer and floating-point types appropriate for this data model.
1363178479Sjb	 */
1364178479Sjb	for (; dinp->din_name != NULL; dinp++) {
1365178479Sjb		if (dinp->din_kind == CTF_K_INTEGER) {
1366178479Sjb			err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1367178479Sjb			    dinp->din_name, &dinp->din_data);
1368178479Sjb		} else {
1369178479Sjb			err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1370178479Sjb			    dinp->din_name, &dinp->din_data);
1371178479Sjb		}
1372178479Sjb
1373178479Sjb		if (err == CTF_ERR) {
1374178479Sjb			dt_dprintf("failed to add %s to C container: %s\n",
1375178479Sjb			    dinp->din_name, ctf_errmsg(
1376178479Sjb			    ctf_errno(dmp->dm_ctfp)));
1377178479Sjb			return (set_open_errno(dtp, errp, EDT_CTF));
1378178479Sjb		}
1379178479Sjb	}
1380178479Sjb
1381178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1382178479Sjb		dt_dprintf("failed to update C container: %s\n",
1383178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1384178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1385178479Sjb	}
1386178479Sjb
1387178479Sjb	/*
1388178479Sjb	 * Add intrinsic pointer types that are needed to initialize printf
1389178479Sjb	 * format dictionary types (see table in dt_printf.c).
1390178479Sjb	 */
1391178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1392178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1393178479Sjb
1394178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1395178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1396178479Sjb
1397178479Sjb	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
1398178479Sjb	    ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1399178479Sjb
1400178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1401178479Sjb		dt_dprintf("failed to update C container: %s\n",
1402178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1403178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1404178479Sjb	}
1405178479Sjb
1406178479Sjb	/*
1407178479Sjb	 * Create a dynamic CTF container under the "D" scope for types that
1408178479Sjb	 * are defined by the D program itself or on-the-fly by the D compiler.
1409178479Sjb	 * The "D" CTF container is a child of the "C" CTF container.
1410178479Sjb	 */
1411178479Sjb	if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1412178479Sjb		return (set_open_errno(dtp, errp, EDT_NOMEM));
1413178479Sjb
1414178479Sjb	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1415178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1416178479Sjb
1417178479Sjb	dt_dprintf("created CTF container for %s (%p)\n",
1418178479Sjb	    dmp->dm_name, (void *)dmp->dm_ctfp);
1419178479Sjb
1420178479Sjb	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1421178479Sjb	ctf_setspecific(dmp->dm_ctfp, dmp);
1422178479Sjb
1423178479Sjb	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1424178479Sjb	dmp->dm_modid = -1; /* no module ID */
1425178479Sjb
1426178479Sjb	if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1427178479Sjb		dt_dprintf("failed to import D parent container: %s\n",
1428178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1429178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1430178479Sjb	}
1431178479Sjb
1432178479Sjb	/*
1433178479Sjb	 * Fill the dynamic "D" CTF container with all of the built-in typedefs
1434178479Sjb	 * that we need to use for our D variable and function definitions.
1435178479Sjb	 * This ensures that basic inttypes.h names are always available to us.
1436178479Sjb	 */
1437178479Sjb	for (; dtyp->dty_src != NULL; dtyp++) {
1438178479Sjb		if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1439178479Sjb		    dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1440178479Sjb		    dtyp->dty_src)) == CTF_ERR) {
1441178479Sjb			dt_dprintf("failed to add typedef %s %s to D "
1442178479Sjb			    "container: %s", dtyp->dty_src, dtyp->dty_dst,
1443178479Sjb			    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1444178479Sjb			return (set_open_errno(dtp, errp, EDT_CTF));
1445178479Sjb		}
1446178479Sjb	}
1447178479Sjb
1448178479Sjb	/*
1449178479Sjb	 * Insert a CTF ID corresponding to a pointer to a type of kind
1450178479Sjb	 * CTF_K_FUNCTION we can use in the compiler for function pointers.
1451178479Sjb	 * CTF treats all function pointers as "int (*)()" so we only need one.
1452178479Sjb	 */
1453178479Sjb	ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1454178479Sjb	ctc.ctc_argc = 0;
1455178479Sjb	ctc.ctc_flags = 0;
1456178479Sjb
1457178479Sjb	dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
1458178479Sjb	    CTF_ADD_ROOT, &ctc, NULL);
1459178479Sjb
1460178479Sjb	dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
1461178479Sjb	    CTF_ADD_ROOT, dtp->dt_type_func);
1462178479Sjb
1463178479Sjb	/*
1464178479Sjb	 * We also insert CTF definitions for the special D intrinsic types
1465178479Sjb	 * string and <DYN> into the D container.  The string type is added
1466178479Sjb	 * as a typedef of char[n].  The <DYN> type is an alias for void.
1467178479Sjb	 * We compare types to these special CTF ids throughout the compiler.
1468178479Sjb	 */
1469178479Sjb	ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1470178479Sjb	ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1471178479Sjb	ctr.ctr_nelems = _dtrace_strsize;
1472178479Sjb
1473178479Sjb	dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1474178479Sjb	    "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1475178479Sjb
1476178479Sjb	dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1477178479Sjb	    "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1478178479Sjb
1479178479Sjb	dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1480178479Sjb	    "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1481178479Sjb
1482178479Sjb	dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1483178479Sjb	    "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1484178479Sjb
1485178479Sjb	dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1486178479Sjb	    "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1487178479Sjb
1488178479Sjb	if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1489178479Sjb	    dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1490178479Sjb	    dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1491178479Sjb	    dtp->dt_type_usymaddr == CTF_ERR) {
1492178479Sjb		dt_dprintf("failed to add intrinsic to D container: %s\n",
1493178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1494178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1495178479Sjb	}
1496178479Sjb
1497178479Sjb	if (ctf_update(dmp->dm_ctfp) != 0) {
1498178479Sjb		dt_dprintf("failed update D container: %s\n",
1499178479Sjb		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1500178479Sjb		return (set_open_errno(dtp, errp, EDT_CTF));
1501178479Sjb	}
1502178479Sjb
1503178479Sjb	/*
1504178479Sjb	 * Initialize the integer description table used to convert integer
1505178479Sjb	 * constants to the appropriate types.  Refer to the comments above
1506178479Sjb	 * dt_node_int() for a complete description of how this table is used.
1507178479Sjb	 */
1508178479Sjb	for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1509178479Sjb		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1510178479Sjb		    dtp->dt_ints[i].did_name, &dtt) != 0) {
1511178479Sjb			dt_dprintf("failed to lookup integer type %s: %s\n",
1512178479Sjb			    dtp->dt_ints[i].did_name,
1513178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1514178479Sjb			return (set_open_errno(dtp, errp, dtp->dt_errno));
1515178479Sjb		}
1516178479Sjb		dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1517178479Sjb		dtp->dt_ints[i].did_type = dtt.dtt_type;
1518178479Sjb	}
1519178479Sjb
1520178479Sjb	/*
1521178479Sjb	 * Now that we've created the "C" and "D" containers, move them to the
1522178479Sjb	 * start of the module list so that these types and symbols are found
1523178479Sjb	 * first (for stability) when iterating through the module list.
1524178479Sjb	 */
1525178479Sjb	dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1526178479Sjb	dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1527178479Sjb
1528178479Sjb	dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1529178479Sjb	dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1530178479Sjb
1531178479Sjb	if (dt_pfdict_create(dtp) == -1)
1532178479Sjb		return (set_open_errno(dtp, errp, dtp->dt_errno));
1533178479Sjb
1534178479Sjb	/*
1535178479Sjb	 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1536178479Sjb	 * because without /dev/dtrace open, we will not be able to load the
1537178479Sjb	 * names and attributes of any providers or probes from the kernel.
1538178479Sjb	 */
1539178479Sjb	if (flags & DTRACE_O_NODEV)
1540178479Sjb		dtp->dt_cflags |= DTRACE_C_ZDEFS;
1541178479Sjb
1542178479Sjb	/*
1543178479Sjb	 * Load hard-wired inlines into the definition cache by calling the
1544178479Sjb	 * compiler on the raw definition string defined above.
1545178479Sjb	 */
1546178479Sjb	if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1547178479Sjb	    DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1548178479Sjb		dt_dprintf("failed to load hard-wired definitions: %s\n",
1549178479Sjb		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
1550178479Sjb		return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1551178479Sjb	}
1552178479Sjb
1553178479Sjb	dt_program_destroy(dtp, pgp);
1554178479Sjb
1555178479Sjb	/*
1556178479Sjb	 * Set up the default DTrace library path.  Once set, the next call to
1557178479Sjb	 * dt_compile() will compile all the libraries.  We intentionally defer
1558178479Sjb	 * library processing to improve overhead for clients that don't ever
1559178479Sjb	 * compile, and to provide better error reporting (because the full
1560178479Sjb	 * reporting of compiler errors requires dtrace_open() to succeed).
1561178479Sjb	 */
1562178479Sjb	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1563178479Sjb		return (set_open_errno(dtp, errp, dtp->dt_errno));
1564178479Sjb
1565178479Sjb	return (dtp);
1566178479Sjb}
1567178479Sjb
1568178479Sjbdtrace_hdl_t *
1569178479Sjbdtrace_open(int version, int flags, int *errp)
1570178479Sjb{
1571178479Sjb	return (dt_vopen(version, flags, errp, NULL, NULL));
1572178479Sjb}
1573178479Sjb
1574178479Sjbdtrace_hdl_t *
1575178479Sjbdtrace_vopen(int version, int flags, int *errp,
1576178479Sjb    const dtrace_vector_t *vector, void *arg)
1577178479Sjb{
1578178479Sjb	return (dt_vopen(version, flags, errp, vector, arg));
1579178479Sjb}
1580178479Sjb
1581178479Sjbvoid
1582178479Sjbdtrace_close(dtrace_hdl_t *dtp)
1583178479Sjb{
1584178479Sjb	dt_ident_t *idp, *ndp;
1585178479Sjb	dt_module_t *dmp;
1586178479Sjb	dt_provider_t *pvp;
1587178479Sjb	dtrace_prog_t *pgp;
1588178479Sjb	dt_xlator_t *dxp;
1589178479Sjb	dt_dirpath_t *dirp;
1590178479Sjb	int i;
1591178479Sjb
1592178570Sjb	if (dtp->dt_procs != NULL)
1593249573Spfg		dt_proc_hash_destroy(dtp);
1594178570Sjb
1595178479Sjb	while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1596178479Sjb		dt_program_destroy(dtp, pgp);
1597178479Sjb
1598178479Sjb	while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1599178479Sjb		dt_xlator_destroy(dtp, dxp);
1600178479Sjb
1601178479Sjb	dt_free(dtp, dtp->dt_xlatormap);
1602178479Sjb
1603178479Sjb	for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1604178479Sjb		ndp = idp->di_next;
1605178479Sjb		dt_ident_destroy(idp);
1606178479Sjb	}
1607178479Sjb
1608178479Sjb	if (dtp->dt_macros != NULL)
1609178479Sjb		dt_idhash_destroy(dtp->dt_macros);
1610178479Sjb	if (dtp->dt_aggs != NULL)
1611178479Sjb		dt_idhash_destroy(dtp->dt_aggs);
1612178479Sjb	if (dtp->dt_globals != NULL)
1613178479Sjb		dt_idhash_destroy(dtp->dt_globals);
1614178479Sjb	if (dtp->dt_tls != NULL)
1615178479Sjb		dt_idhash_destroy(dtp->dt_tls);
1616178479Sjb
1617178479Sjb	while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1618178479Sjb		dt_module_destroy(dtp, dmp);
1619178479Sjb
1620178479Sjb	while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1621178479Sjb		dt_provider_destroy(dtp, pvp);
1622178479Sjb
1623178479Sjb	if (dtp->dt_fd != -1)
1624178479Sjb		(void) close(dtp->dt_fd);
1625178479Sjb	if (dtp->dt_ftfd != -1)
1626178479Sjb		(void) close(dtp->dt_ftfd);
1627178479Sjb	if (dtp->dt_cdefs_fd != -1)
1628178479Sjb		(void) close(dtp->dt_cdefs_fd);
1629178479Sjb	if (dtp->dt_ddefs_fd != -1)
1630178479Sjb		(void) close(dtp->dt_ddefs_fd);
1631178570Sjb#if defined(sun)
1632178479Sjb	if (dtp->dt_stdout_fd != -1)
1633178479Sjb		(void) close(dtp->dt_stdout_fd);
1634178570Sjb#else
1635178570Sjb	if (dtp->dt_freopen_fp != NULL)
1636178570Sjb		(void) fclose(dtp->dt_freopen_fp);
1637178570Sjb#endif
1638178479Sjb
1639178479Sjb	dt_epid_destroy(dtp);
1640178479Sjb	dt_aggid_destroy(dtp);
1641178479Sjb	dt_format_destroy(dtp);
1642248708Spfg	dt_strdata_destroy(dtp);
1643178479Sjb	dt_buffered_destroy(dtp);
1644178479Sjb	dt_aggregate_destroy(dtp);
1645178479Sjb	dt_pfdict_destroy(dtp);
1646178479Sjb	dt_provmod_destroy(&dtp->dt_provmod);
1647178479Sjb	dt_dof_fini(dtp);
1648178479Sjb
1649178479Sjb	for (i = 1; i < dtp->dt_cpp_argc; i++)
1650178479Sjb		free(dtp->dt_cpp_argv[i]);
1651178479Sjb
1652178479Sjb	while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1653178479Sjb		dt_list_delete(&dtp->dt_lib_path, dirp);
1654178479Sjb		free(dirp->dir_path);
1655178479Sjb		free(dirp);
1656178479Sjb	}
1657178479Sjb
1658178479Sjb	free(dtp->dt_cpp_argv);
1659178479Sjb	free(dtp->dt_cpp_path);
1660178479Sjb	free(dtp->dt_ld_path);
1661178479Sjb
1662178479Sjb	free(dtp->dt_mods);
1663178479Sjb	free(dtp->dt_provs);
1664178479Sjb	free(dtp);
1665178479Sjb}
1666178479Sjb
1667178479Sjbint
1668178479Sjbdtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1669178479Sjb{
1670178479Sjb	dt_provmod_t *prov;
1671178479Sjb	int i = 0;
1672178479Sjb
1673178479Sjb	for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1674178479Sjb		if (i < nmods)
1675178479Sjb			mods[i] = prov->dp_name;
1676178479Sjb	}
1677178479Sjb
1678178479Sjb	return (i);
1679178479Sjb}
1680178479Sjb
1681178479Sjbint
1682178479Sjbdtrace_ctlfd(dtrace_hdl_t *dtp)
1683178479Sjb{
1684178479Sjb	return (dtp->dt_fd);
1685178479Sjb}
1686