10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/systeminfo.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <libelf.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <alloca.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <zone.h>
430Sstevel@tonic-gate #include <assert.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	_POSIX_PTHREAD_SEMANTICS
460Sstevel@tonic-gate #include <dirent.h>
470Sstevel@tonic-gate #undef	_POSIX_PTHREAD_SEMANTICS
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <dt_impl.h>
500Sstevel@tonic-gate #include <dt_module.h>
510Sstevel@tonic-gate #include <dt_printf.h>
520Sstevel@tonic-gate #include <dt_string.h>
530Sstevel@tonic-gate #include <dt_provider.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Stability and versioning definitions.  These #defines are used in the tables
570Sstevel@tonic-gate  * of identifiers below to fill in the attribute and version fields associated
580Sstevel@tonic-gate  * with each identifier.  The DT_ATTR_* macros are a convenience to permit more
590Sstevel@tonic-gate  * concise declarations of common attributes such as Stable/Stable/Common.  The
600Sstevel@tonic-gate  * DT_VERS_* macros declare the encoded integer values of all versions used so
610Sstevel@tonic-gate  * far.  DT_VERS_LATEST must correspond to the latest version value among all
620Sstevel@tonic-gate  * versions exported by the D compiler.  DT_VERS_STRING must be an ASCII string
630Sstevel@tonic-gate  * that contains DT_VERS_LATEST within it along with any suffixes (e.g. Beta).
640Sstevel@tonic-gate  * You must update DT_VERS_LATEST and DT_VERS_STRING when adding a new version,
650Sstevel@tonic-gate  * and then add the new version to the _dtrace_versions[] array declared below.
660Sstevel@tonic-gate  * Refer to the Solaris Dynamic Tracing Guide Stability and Versioning chapters
670Sstevel@tonic-gate  * respectively for an explanation of these DTrace features and their values.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * NOTE: Although the DTrace versioning scheme supports the labeling and
700Sstevel@tonic-gate  *       introduction of incompatible changes (e.g. dropping an interface in a
710Sstevel@tonic-gate  *       major release), the libdtrace code does not currently support this.
720Sstevel@tonic-gate  *       All versions are assumed to strictly inherit from one another.  If
730Sstevel@tonic-gate  *       we ever need to provide divergent interfaces, this will need work.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate #define	DT_ATTR_STABCMN	{ DTRACE_STABILITY_STABLE, \
760Sstevel@tonic-gate 	DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	DT_ATTR_EVOLCMN { DTRACE_STABILITY_EVOLVING, \
790Sstevel@tonic-gate 	DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON \
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	DT_VERS_1_0	DT_VERSION_NUMBER(1, 0, 0)
830Sstevel@tonic-gate #define	DT_VERS_1_1	DT_VERSION_NUMBER(1, 1, 0)
84*191Sahl #define	DT_VERS_1_2	DT_VERSION_NUMBER(1, 2, 0)
85*191Sahl #define	DT_VERS_LATEST	DT_VERS_1_2
86*191Sahl #define	DT_VERS_STRING	"Sun D 1.2"
870Sstevel@tonic-gate 
880Sstevel@tonic-gate const dt_version_t _dtrace_versions[] = {
890Sstevel@tonic-gate 	DT_VERS_1_0,	/* D API 1.0.0 (PSARC 2001/466) Solaris 10 FCS */
90*191Sahl 	DT_VERS_1_1,	/* D API 1.1.0 Solaris Express 6/05 */
91*191Sahl 	DT_VERS_1_2,	/* D API 1.2.0 Solaris 10 Update 1 */
920Sstevel@tonic-gate 	0
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Table of global identifiers.  This is used to populate the global identifier
970Sstevel@tonic-gate  * hash when a new dtrace client open occurs.  For more info see dt_ident.h.
980Sstevel@tonic-gate  * The global identifiers that represent functions use the dt_idops_func ops
990Sstevel@tonic-gate  * and specify the private data pointer as a prototype string which is parsed
1000Sstevel@tonic-gate  * when the identifier is first encountered.  These prototypes look like ANSI
1010Sstevel@tonic-gate  * C function prototypes except that the special symbol "@" can be used as a
1020Sstevel@tonic-gate  * wildcard to represent a single parameter of any type (i.e. any dt_node_t).
1030Sstevel@tonic-gate  * The standard "..." notation can also be used to represent varargs.  An empty
1040Sstevel@tonic-gate  * parameter list is taken to mean void (that is, no arguments are permitted).
1050Sstevel@tonic-gate  * A parameter enclosed in square brackets (e.g. "[int]") denotes an optional
1060Sstevel@tonic-gate  * argument.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate static const dt_ident_t _dtrace_globals[] = {
1090Sstevel@tonic-gate { "alloca", DT_IDENT_FUNC, 0, DIF_SUBR_ALLOCA, DT_ATTR_STABCMN, DT_VERS_1_0,
1100Sstevel@tonic-gate 	&dt_idops_func, "void *(size_t)" },
1110Sstevel@tonic-gate { "arg0", DT_IDENT_SCALAR, 0, DIF_VAR_ARG0, DT_ATTR_STABCMN, DT_VERS_1_0,
1120Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1130Sstevel@tonic-gate { "arg1", DT_IDENT_SCALAR, 0, DIF_VAR_ARG1, DT_ATTR_STABCMN, DT_VERS_1_0,
1140Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1150Sstevel@tonic-gate { "arg2", DT_IDENT_SCALAR, 0, DIF_VAR_ARG2, DT_ATTR_STABCMN, DT_VERS_1_0,
1160Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1170Sstevel@tonic-gate { "arg3", DT_IDENT_SCALAR, 0, DIF_VAR_ARG3, DT_ATTR_STABCMN, DT_VERS_1_0,
1180Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1190Sstevel@tonic-gate { "arg4", DT_IDENT_SCALAR, 0, DIF_VAR_ARG4, DT_ATTR_STABCMN, DT_VERS_1_0,
1200Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1210Sstevel@tonic-gate { "arg5", DT_IDENT_SCALAR, 0, DIF_VAR_ARG5, DT_ATTR_STABCMN, DT_VERS_1_0,
1220Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1230Sstevel@tonic-gate { "arg6", DT_IDENT_SCALAR, 0, DIF_VAR_ARG6, DT_ATTR_STABCMN, DT_VERS_1_0,
1240Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1250Sstevel@tonic-gate { "arg7", DT_IDENT_SCALAR, 0, DIF_VAR_ARG7, DT_ATTR_STABCMN, DT_VERS_1_0,
1260Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1270Sstevel@tonic-gate { "arg8", DT_IDENT_SCALAR, 0, DIF_VAR_ARG8, DT_ATTR_STABCMN, DT_VERS_1_0,
1280Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1290Sstevel@tonic-gate { "arg9", DT_IDENT_SCALAR, 0, DIF_VAR_ARG9, DT_ATTR_STABCMN, DT_VERS_1_0,
1300Sstevel@tonic-gate 	&dt_idops_type, "int64_t" },
1310Sstevel@tonic-gate { "args", DT_IDENT_ARRAY, 0, DIF_VAR_ARGS, DT_ATTR_STABCMN, DT_VERS_1_0,
1320Sstevel@tonic-gate 	&dt_idops_args, NULL },
1330Sstevel@tonic-gate { "avg", DT_IDENT_AGGFUNC, 0, DTRACEAGG_AVG, DT_ATTR_STABCMN, DT_VERS_1_0,
1340Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
1350Sstevel@tonic-gate { "basename", DT_IDENT_FUNC, 0, DIF_SUBR_BASENAME, DT_ATTR_STABCMN, DT_VERS_1_0,
1360Sstevel@tonic-gate 	&dt_idops_func, "string(const char *)" },
1370Sstevel@tonic-gate { "bcopy", DT_IDENT_FUNC, 0, DIF_SUBR_BCOPY, DT_ATTR_STABCMN, DT_VERS_1_0,
1380Sstevel@tonic-gate 	&dt_idops_func, "void(void *, void *, size_t)" },
1390Sstevel@tonic-gate { "breakpoint", DT_IDENT_ACTFUNC, 0, DT_ACT_BREAKPOINT,
1400Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
1410Sstevel@tonic-gate 	&dt_idops_func, "void()" },
1420Sstevel@tonic-gate { "caller", DT_IDENT_SCALAR, 0, DIF_VAR_CALLER, DT_ATTR_STABCMN, DT_VERS_1_0,
1430Sstevel@tonic-gate 	&dt_idops_type, "uintptr_t" },
1440Sstevel@tonic-gate { "chill", DT_IDENT_ACTFUNC, 0, DT_ACT_CHILL, DT_ATTR_STABCMN, DT_VERS_1_0,
1450Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
1460Sstevel@tonic-gate { "cleanpath", DT_IDENT_FUNC, 0, DIF_SUBR_CLEANPATH, DT_ATTR_STABCMN,
1470Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "string(const char *)" },
1480Sstevel@tonic-gate { "clear", DT_IDENT_ACTFUNC, 0, DT_ACT_CLEAR, DT_ATTR_STABCMN, DT_VERS_1_0,
1490Sstevel@tonic-gate 	&dt_idops_func, "void(...)" },
1500Sstevel@tonic-gate { "commit", DT_IDENT_ACTFUNC, 0, DT_ACT_COMMIT, DT_ATTR_STABCMN, DT_VERS_1_0,
1510Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
1520Sstevel@tonic-gate { "copyin", DT_IDENT_FUNC, 0, DIF_SUBR_COPYIN, DT_ATTR_STABCMN, DT_VERS_1_0,
1530Sstevel@tonic-gate 	&dt_idops_func, "void *(uintptr_t, size_t)" },
1540Sstevel@tonic-gate { "copyinstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINSTR,
1550Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
1560Sstevel@tonic-gate 	&dt_idops_func, "string(uintptr_t, [size_t])" },
1570Sstevel@tonic-gate { "copyinto", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINTO, DT_ATTR_STABCMN,
1580Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "void(uintptr_t, size_t, void *)" },
1590Sstevel@tonic-gate { "copyout", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUT, DT_ATTR_STABCMN, DT_VERS_1_0,
1600Sstevel@tonic-gate 	&dt_idops_func, "void(void *, uintptr_t, size_t)" },
1610Sstevel@tonic-gate { "copyoutstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUTSTR,
1620Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
1630Sstevel@tonic-gate 	&dt_idops_func, "void(char *, uintptr_t, size_t)" },
1640Sstevel@tonic-gate { "count", DT_IDENT_AGGFUNC, 0, DTRACEAGG_COUNT, DT_ATTR_STABCMN, DT_VERS_1_0,
1650Sstevel@tonic-gate 	&dt_idops_func, "void()" },
1660Sstevel@tonic-gate { "curthread", DT_IDENT_SCALAR, 0, DIF_VAR_CURTHREAD,
1670Sstevel@tonic-gate 	{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_PRIVATE,
1680Sstevel@tonic-gate 	DTRACE_CLASS_COMMON }, DT_VERS_1_0,
1690Sstevel@tonic-gate 	&dt_idops_type, "genunix`kthread_t *" },
1700Sstevel@tonic-gate { "ddi_pathname", DT_IDENT_FUNC, 0, DIF_SUBR_DDI_PATHNAME,
1710Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
1720Sstevel@tonic-gate 	&dt_idops_func, "string(void *, int64_t)" },
1730Sstevel@tonic-gate { "denormalize", DT_IDENT_ACTFUNC, 0, DT_ACT_DENORMALIZE, DT_ATTR_STABCMN,
1740Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "void(...)" },
1750Sstevel@tonic-gate { "dirname", DT_IDENT_FUNC, 0, DIF_SUBR_DIRNAME, DT_ATTR_STABCMN, DT_VERS_1_0,
1760Sstevel@tonic-gate 	&dt_idops_func, "string(const char *)" },
1770Sstevel@tonic-gate { "discard", DT_IDENT_ACTFUNC, 0, DT_ACT_DISCARD, DT_ATTR_STABCMN, DT_VERS_1_0,
1780Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
1790Sstevel@tonic-gate { "epid", DT_IDENT_SCALAR, 0, DIF_VAR_EPID, DT_ATTR_STABCMN, DT_VERS_1_0,
1800Sstevel@tonic-gate 	&dt_idops_type, "uint_t" },
1810Sstevel@tonic-gate { "execname", DT_IDENT_SCALAR, 0, DIF_VAR_EXECNAME,
1820Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
1830Sstevel@tonic-gate { "exit", DT_IDENT_ACTFUNC, 0, DT_ACT_EXIT, DT_ATTR_STABCMN, DT_VERS_1_0,
1840Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
1850Sstevel@tonic-gate { "freopen", DT_IDENT_ACTFUNC, 0, DT_ACT_FREOPEN, DT_ATTR_STABCMN,
1860Sstevel@tonic-gate 	DT_VERS_1_1, &dt_idops_func, "void(@, ...)" },
1870Sstevel@tonic-gate { "ftruncate", DT_IDENT_ACTFUNC, 0, DT_ACT_FTRUNCATE, DT_ATTR_STABCMN,
1880Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "void()" },
1890Sstevel@tonic-gate { "getmajor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMAJOR,
1900Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
1910Sstevel@tonic-gate 	&dt_idops_func, "genunix`major_t(genunix`dev_t)" },
1920Sstevel@tonic-gate { "getminor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMINOR,
1930Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
1940Sstevel@tonic-gate 	&dt_idops_func, "genunix`minor_t(genunix`dev_t)" },
1950Sstevel@tonic-gate { "id", DT_IDENT_SCALAR, 0, DIF_VAR_ID, DT_ATTR_STABCMN, DT_VERS_1_0,
1960Sstevel@tonic-gate 	&dt_idops_type, "uint_t" },
1970Sstevel@tonic-gate { "index", DT_IDENT_FUNC, 0, DIF_SUBR_INDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
1980Sstevel@tonic-gate 	&dt_idops_func, "int(const char *, const char *, [int])" },
1990Sstevel@tonic-gate { "ipl", DT_IDENT_SCALAR, 0, DIF_VAR_IPL, DT_ATTR_STABCMN, DT_VERS_1_0,
2000Sstevel@tonic-gate 	&dt_idops_type, "uint_t" },
2010Sstevel@tonic-gate { "jstack", DT_IDENT_ACTFUNC, 0, DT_ACT_JSTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
2020Sstevel@tonic-gate 	&dt_idops_func, "stack(...)" },
2030Sstevel@tonic-gate { "lltostr", DT_IDENT_FUNC, 0, DIF_SUBR_LLTOSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
2040Sstevel@tonic-gate 	&dt_idops_func, "string(int64_t)" },
2050Sstevel@tonic-gate { "lquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LQUANTIZE,
2060Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2070Sstevel@tonic-gate 	&dt_idops_func, "void(@, int32_t, int32_t, ...)" },
2080Sstevel@tonic-gate { "max", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MAX, DT_ATTR_STABCMN, DT_VERS_1_0,
2090Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
2100Sstevel@tonic-gate { "min", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MIN, DT_ATTR_STABCMN, DT_VERS_1_0,
2110Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
2120Sstevel@tonic-gate { "msgdsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGDSIZE,
2130Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2140Sstevel@tonic-gate 	&dt_idops_func, "size_t(mblk_t *)" },
2150Sstevel@tonic-gate { "msgsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGSIZE,
2160Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2170Sstevel@tonic-gate 	&dt_idops_func, "size_t(mblk_t *)" },
2180Sstevel@tonic-gate { "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
2190Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2200Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`kmutex_t *)" },
2210Sstevel@tonic-gate { "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
2220Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2230Sstevel@tonic-gate 	&dt_idops_func, "genunix`kthread_t *(genunix`kmutex_t *)" },
2240Sstevel@tonic-gate { "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
2250Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2260Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`kmutex_t *)" },
2270Sstevel@tonic-gate { "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
2280Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2290Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`kmutex_t *)" },
2300Sstevel@tonic-gate { "normalize", DT_IDENT_ACTFUNC, 0, DT_ACT_NORMALIZE, DT_ATTR_STABCMN,
2310Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "void(...)" },
2320Sstevel@tonic-gate { "panic", DT_IDENT_ACTFUNC, 0, DT_ACT_PANIC, DT_ATTR_STABCMN, DT_VERS_1_0,
2330Sstevel@tonic-gate 	&dt_idops_func, "void()" },
2340Sstevel@tonic-gate { "pid", DT_IDENT_SCALAR, 0, DIF_VAR_PID, DT_ATTR_STABCMN, DT_VERS_1_0,
2350Sstevel@tonic-gate 	&dt_idops_type, "pid_t" },
2360Sstevel@tonic-gate { "printa", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTA, DT_ATTR_STABCMN, DT_VERS_1_0,
2370Sstevel@tonic-gate 	&dt_idops_func, "void(@, ...)" },
2380Sstevel@tonic-gate { "printf", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTF, DT_ATTR_STABCMN, DT_VERS_1_0,
2390Sstevel@tonic-gate 	&dt_idops_func, "void(@, ...)" },
2400Sstevel@tonic-gate { "probefunc", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEFUNC,
2410Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
2420Sstevel@tonic-gate { "probemod", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEMOD,
2430Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
2440Sstevel@tonic-gate { "probename", DT_IDENT_SCALAR, 0, DIF_VAR_PROBENAME,
2450Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
2460Sstevel@tonic-gate { "probeprov", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEPROV,
2470Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
2480Sstevel@tonic-gate { "progenyof", DT_IDENT_FUNC, 0, DIF_SUBR_PROGENYOF,
2490Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2500Sstevel@tonic-gate 	&dt_idops_func, "int(pid_t)" },
2510Sstevel@tonic-gate { "quantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_QUANTIZE,
2520Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2530Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
2540Sstevel@tonic-gate { "raise", DT_IDENT_ACTFUNC, 0, DT_ACT_RAISE, DT_ATTR_STABCMN, DT_VERS_1_0,
2550Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
2560Sstevel@tonic-gate { "rand", DT_IDENT_FUNC, 0, DIF_SUBR_RAND, DT_ATTR_STABCMN, DT_VERS_1_0,
2570Sstevel@tonic-gate 	&dt_idops_func, "int()" },
2580Sstevel@tonic-gate { "rindex", DT_IDENT_FUNC, 0, DIF_SUBR_RINDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
2590Sstevel@tonic-gate 	&dt_idops_func, "int(const char *, const char *, [int])" },
2600Sstevel@tonic-gate { "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
2610Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2620Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`krwlock_t *)" },
2630Sstevel@tonic-gate { "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
2640Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2650Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`krwlock_t *)" },
2660Sstevel@tonic-gate { "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
2670Sstevel@tonic-gate 	DT_ATTR_EVOLCMN, DT_VERS_1_0,
2680Sstevel@tonic-gate 	&dt_idops_func, "int(genunix`krwlock_t *)" },
2690Sstevel@tonic-gate { "self", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
2700Sstevel@tonic-gate 	&dt_idops_type, "void" },
2710Sstevel@tonic-gate { "speculate", DT_IDENT_ACTFUNC, 0, DT_ACT_SPECULATE,
2720Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2730Sstevel@tonic-gate 	&dt_idops_func, "void(int)" },
2740Sstevel@tonic-gate { "speculation", DT_IDENT_FUNC, 0, DIF_SUBR_SPECULATION,
2750Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2760Sstevel@tonic-gate 	&dt_idops_func, "int()" },
2770Sstevel@tonic-gate { "stack", DT_IDENT_ACTFUNC, 0, DT_ACT_STACK, DT_ATTR_STABCMN, DT_VERS_1_0,
2780Sstevel@tonic-gate 	&dt_idops_func, "stack(...)" },
2790Sstevel@tonic-gate { "stackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_STACKDEPTH,
2800Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
2810Sstevel@tonic-gate 	&dt_idops_type, "uint32_t" },
2820Sstevel@tonic-gate { "stop", DT_IDENT_ACTFUNC, 0, DT_ACT_STOP, DT_ATTR_STABCMN, DT_VERS_1_0,
2830Sstevel@tonic-gate 	&dt_idops_func, "void()" },
2840Sstevel@tonic-gate { "strchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
2850Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, char)" },
2860Sstevel@tonic-gate { "strlen", DT_IDENT_FUNC, 0, DIF_SUBR_STRLEN, DT_ATTR_STABCMN, DT_VERS_1_0,
2870Sstevel@tonic-gate 	&dt_idops_func, "size_t(const char *)" },
2880Sstevel@tonic-gate { "strjoin", DT_IDENT_FUNC, 0, DIF_SUBR_STRJOIN, DT_ATTR_STABCMN, DT_VERS_1_0,
2890Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, const char *)" },
2900Sstevel@tonic-gate { "strrchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
2910Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, char)" },
2920Sstevel@tonic-gate { "strstr", DT_IDENT_FUNC, 0, DIF_SUBR_STRSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
2930Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, const char *)" },
2940Sstevel@tonic-gate { "strtok", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOK, DT_ATTR_STABCMN, DT_VERS_1_1,
2950Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, const char *)" },
2960Sstevel@tonic-gate { "substr", DT_IDENT_FUNC, 0, DIF_SUBR_SUBSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
2970Sstevel@tonic-gate 	&dt_idops_func, "string(const char *, int, [int])" },
2980Sstevel@tonic-gate { "sum", DT_IDENT_AGGFUNC, 0, DTRACEAGG_SUM, DT_ATTR_STABCMN, DT_VERS_1_0,
2990Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
3000Sstevel@tonic-gate { "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
3010Sstevel@tonic-gate 	&dt_idops_func, "void(@, ...)" },
3020Sstevel@tonic-gate { "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
3030Sstevel@tonic-gate 	&dt_idops_type, "void" },
3040Sstevel@tonic-gate { "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
3050Sstevel@tonic-gate 	&dt_idops_type, "id_t" },
3060Sstevel@tonic-gate { "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
3070Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
3080Sstevel@tonic-gate 	&dt_idops_type, "uint64_t" },
3090Sstevel@tonic-gate { "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
3100Sstevel@tonic-gate 	&dt_idops_func, "void(@)" },
3110Sstevel@tonic-gate { "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
3120Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
3130Sstevel@tonic-gate 	&dt_idops_func, "void(@, size_t)" },
3140Sstevel@tonic-gate { "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
3150Sstevel@tonic-gate 	DT_VERS_1_0, &dt_idops_func, "void(...)" },
3160Sstevel@tonic-gate { "uregs", DT_IDENT_ARRAY, 0, DIF_VAR_UREGS, DT_ATTR_STABCMN, DT_VERS_1_0,
3170Sstevel@tonic-gate 	&dt_idops_regs, NULL },
3180Sstevel@tonic-gate { "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
3190Sstevel@tonic-gate 	&dt_idops_func, "stack(...)" },
320*191Sahl { "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
321*191Sahl 	DT_ATTR_STABCMN, DT_VERS_1_2,
322*191Sahl 	&dt_idops_type, "uint32_t" },
3230Sstevel@tonic-gate { "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
3240Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
3250Sstevel@tonic-gate 	&dt_idops_type, "uint64_t" },
3260Sstevel@tonic-gate { "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
3270Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0,
3280Sstevel@tonic-gate 	&dt_idops_type, "uint64_t" },
3290Sstevel@tonic-gate { "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
3300Sstevel@tonic-gate 	DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
3310Sstevel@tonic-gate { NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
3320Sstevel@tonic-gate };
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * Tables of ILP32 intrinsic integer and floating-point type templates to use
3360Sstevel@tonic-gate  * to populate the dynamic "C" CTF type container.
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate static const dt_intrinsic_t _dtrace_intrinsics_32[] = {
3390Sstevel@tonic-gate { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
3400Sstevel@tonic-gate { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3410Sstevel@tonic-gate { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
3420Sstevel@tonic-gate { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3430Sstevel@tonic-gate { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
3440Sstevel@tonic-gate { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3450Sstevel@tonic-gate { "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3460Sstevel@tonic-gate { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3470Sstevel@tonic-gate { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3480Sstevel@tonic-gate { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
3490Sstevel@tonic-gate { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3500Sstevel@tonic-gate { "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3510Sstevel@tonic-gate { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3520Sstevel@tonic-gate { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3530Sstevel@tonic-gate { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
3540Sstevel@tonic-gate { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
3550Sstevel@tonic-gate { "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
3560Sstevel@tonic-gate { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
3570Sstevel@tonic-gate { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
3580Sstevel@tonic-gate { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
3590Sstevel@tonic-gate { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
3600Sstevel@tonic-gate { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
3610Sstevel@tonic-gate { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
3620Sstevel@tonic-gate { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
3630Sstevel@tonic-gate { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
3640Sstevel@tonic-gate { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
3650Sstevel@tonic-gate { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
3660Sstevel@tonic-gate { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
3670Sstevel@tonic-gate { NULL, { 0, 0, 0 }, 0 }
3680Sstevel@tonic-gate };
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Tables of LP64 intrinsic integer and floating-point type templates to use
3720Sstevel@tonic-gate  * to populate the dynamic "C" CTF type container.
3730Sstevel@tonic-gate  */
3740Sstevel@tonic-gate static const dt_intrinsic_t _dtrace_intrinsics_64[] = {
3750Sstevel@tonic-gate { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
3760Sstevel@tonic-gate { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3770Sstevel@tonic-gate { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
3780Sstevel@tonic-gate { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3790Sstevel@tonic-gate { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
3800Sstevel@tonic-gate { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3810Sstevel@tonic-gate { "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3820Sstevel@tonic-gate { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3830Sstevel@tonic-gate { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3840Sstevel@tonic-gate { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
3850Sstevel@tonic-gate { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
3860Sstevel@tonic-gate { "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3870Sstevel@tonic-gate { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
3880Sstevel@tonic-gate { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
3890Sstevel@tonic-gate { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
3900Sstevel@tonic-gate { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
3910Sstevel@tonic-gate { "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
3920Sstevel@tonic-gate { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
3930Sstevel@tonic-gate { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
3940Sstevel@tonic-gate { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
3950Sstevel@tonic-gate { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
3960Sstevel@tonic-gate { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
3970Sstevel@tonic-gate { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
3980Sstevel@tonic-gate { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
3990Sstevel@tonic-gate { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
4000Sstevel@tonic-gate { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
4010Sstevel@tonic-gate { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
4020Sstevel@tonic-gate { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
4030Sstevel@tonic-gate { NULL, { 0, 0, 0 }, 0 }
4040Sstevel@tonic-gate };
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
4080Sstevel@tonic-gate  * These aliases ensure that D definitions can use typical <sys/types.h> names.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate static const dt_typedef_t _dtrace_typedefs_32[] = {
4110Sstevel@tonic-gate { "char", "int8_t" },
4120Sstevel@tonic-gate { "short", "int16_t" },
4130Sstevel@tonic-gate { "int", "int32_t" },
4140Sstevel@tonic-gate { "long long", "int64_t" },
4150Sstevel@tonic-gate { "int", "intptr_t" },
4160Sstevel@tonic-gate { "int", "ssize_t" },
4170Sstevel@tonic-gate { "unsigned char", "uint8_t" },
4180Sstevel@tonic-gate { "unsigned short", "uint16_t" },
4190Sstevel@tonic-gate { "unsigned", "uint32_t" },
4200Sstevel@tonic-gate { "unsigned long long", "uint64_t" },
4210Sstevel@tonic-gate { "unsigned char", "uchar_t" },
4220Sstevel@tonic-gate { "unsigned short", "ushort_t" },
4230Sstevel@tonic-gate { "unsigned", "uint_t" },
4240Sstevel@tonic-gate { "unsigned long", "ulong_t" },
4250Sstevel@tonic-gate { "unsigned long long", "u_longlong_t" },
4260Sstevel@tonic-gate { "int", "ptrdiff_t" },
4270Sstevel@tonic-gate { "unsigned", "uintptr_t" },
4280Sstevel@tonic-gate { "unsigned", "size_t" },
4290Sstevel@tonic-gate { "long", "id_t" },
4300Sstevel@tonic-gate { "long", "pid_t" },
4310Sstevel@tonic-gate { NULL, NULL }
4320Sstevel@tonic-gate };
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate  * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
4360Sstevel@tonic-gate  * These aliases ensure that D definitions can use typical <sys/types.h> names.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate static const dt_typedef_t _dtrace_typedefs_64[] = {
4390Sstevel@tonic-gate { "char", "int8_t" },
4400Sstevel@tonic-gate { "short", "int16_t" },
4410Sstevel@tonic-gate { "int", "int32_t" },
4420Sstevel@tonic-gate { "long", "int64_t" },
4430Sstevel@tonic-gate { "long", "intptr_t" },
4440Sstevel@tonic-gate { "long", "ssize_t" },
4450Sstevel@tonic-gate { "unsigned char", "uint8_t" },
4460Sstevel@tonic-gate { "unsigned short", "uint16_t" },
4470Sstevel@tonic-gate { "unsigned", "uint32_t" },
4480Sstevel@tonic-gate { "unsigned long", "uint64_t" },
4490Sstevel@tonic-gate { "unsigned char", "uchar_t" },
4500Sstevel@tonic-gate { "unsigned short", "ushort_t" },
4510Sstevel@tonic-gate { "unsigned", "uint_t" },
4520Sstevel@tonic-gate { "unsigned long", "ulong_t" },
4530Sstevel@tonic-gate { "unsigned long long", "u_longlong_t" },
4540Sstevel@tonic-gate { "long", "ptrdiff_t" },
4550Sstevel@tonic-gate { "unsigned long", "uintptr_t" },
4560Sstevel@tonic-gate { "unsigned long", "size_t" },
4570Sstevel@tonic-gate { "int", "id_t" },
4580Sstevel@tonic-gate { "int", "pid_t" },
4590Sstevel@tonic-gate { NULL, NULL }
4600Sstevel@tonic-gate };
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
4640Sstevel@tonic-gate  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate static const dt_intdesc_t _dtrace_ints_32[] = {
4670Sstevel@tonic-gate { "int", NULL, CTF_ERR, 0x7fffffffULL },
4680Sstevel@tonic-gate { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
4690Sstevel@tonic-gate { "long", NULL, CTF_ERR, 0x7fffffffULL },
4700Sstevel@tonic-gate { "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
4710Sstevel@tonic-gate { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
4720Sstevel@tonic-gate { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
4730Sstevel@tonic-gate };
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
4770Sstevel@tonic-gate  * cache when a new dtrace client open occurs.  Values are set by dtrace_open().
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate static const dt_intdesc_t _dtrace_ints_64[] = {
4800Sstevel@tonic-gate { "int", NULL, CTF_ERR, 0x7fffffffULL },
4810Sstevel@tonic-gate { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
4820Sstevel@tonic-gate { "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
4830Sstevel@tonic-gate { "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
4840Sstevel@tonic-gate { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
4850Sstevel@tonic-gate { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
4860Sstevel@tonic-gate };
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Table of macro variable templates used to populate the macro identifier hash
4900Sstevel@tonic-gate  * when a new dtrace client open occurs.  Values are set by dtrace_update().
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static const dt_ident_t _dtrace_macros[] = {
4930Sstevel@tonic-gate { "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4940Sstevel@tonic-gate { "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4950Sstevel@tonic-gate { "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4960Sstevel@tonic-gate { "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4970Sstevel@tonic-gate { "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4980Sstevel@tonic-gate { "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
4990Sstevel@tonic-gate { "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
5000Sstevel@tonic-gate { "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
5010Sstevel@tonic-gate { "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
5020Sstevel@tonic-gate { "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
5030Sstevel@tonic-gate { "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
5040Sstevel@tonic-gate { NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
5050Sstevel@tonic-gate };
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * Hard-wired definition string to be compiled and cached every time a new
5090Sstevel@tonic-gate  * DTrace library handle is initialized.  This string should only be used to
5100Sstevel@tonic-gate  * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate static const char _dtrace_hardwire[] = "\
5130Sstevel@tonic-gate inline long NULL = 0; \n\
5140Sstevel@tonic-gate #pragma D binding \"1.0\" NULL\n\
5150Sstevel@tonic-gate ";
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
5190Sstevel@tonic-gate  * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
5200Sstevel@tonic-gate  * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
5210Sstevel@tonic-gate  * relying on the fact that when running dtrace(1M), isaexec will invoke the
5220Sstevel@tonic-gate  * binary with the same bitness as the kernel, which is what we want by default
5230Sstevel@tonic-gate  * when generating our DIF.  The user can override the choice using oflags.
5240Sstevel@tonic-gate  */
5250Sstevel@tonic-gate static const dtrace_conf_t _dtrace_conf = {
5260Sstevel@tonic-gate 	DIF_VERSION,		/* dtc_difversion */
5270Sstevel@tonic-gate 	DIF_DIR_NREGS,		/* dtc_difintregs */
5280Sstevel@tonic-gate 	DIF_DTR_NREGS,		/* dtc_diftupregs */
5290Sstevel@tonic-gate 	CTF_MODEL_NATIVE	/* dtc_ctfmodel */
5300Sstevel@tonic-gate };
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate const dtrace_attribute_t _dtrace_maxattr = {
5330Sstevel@tonic-gate 	DTRACE_STABILITY_MAX,
5340Sstevel@tonic-gate 	DTRACE_STABILITY_MAX,
5350Sstevel@tonic-gate 	DTRACE_CLASS_MAX
5360Sstevel@tonic-gate };
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate const dtrace_attribute_t _dtrace_defattr = {
5390Sstevel@tonic-gate 	DTRACE_STABILITY_STABLE,
5400Sstevel@tonic-gate 	DTRACE_STABILITY_STABLE,
5410Sstevel@tonic-gate 	DTRACE_CLASS_COMMON
5420Sstevel@tonic-gate };
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate const dtrace_attribute_t _dtrace_symattr = {
5450Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5460Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5470Sstevel@tonic-gate 	DTRACE_CLASS_UNKNOWN
5480Sstevel@tonic-gate };
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate const dtrace_attribute_t _dtrace_typattr = {
5510Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5520Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5530Sstevel@tonic-gate 	DTRACE_CLASS_UNKNOWN
5540Sstevel@tonic-gate };
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate const dtrace_attribute_t _dtrace_prvattr = {
5570Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5580Sstevel@tonic-gate 	DTRACE_STABILITY_PRIVATE,
5590Sstevel@tonic-gate 	DTRACE_CLASS_UNKNOWN
5600Sstevel@tonic-gate };
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate const dtrace_pattr_t _dtrace_prvdesc = {
5630Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
5640Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
5650Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
5660Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
5670Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
5680Sstevel@tonic-gate };
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate const char *_dtrace_defcpp = "/usr/ccs/lib/cpp"; /* default cpp(1) to invoke */
5710Sstevel@tonic-gate const char *_dtrace_defld = "/usr/ccs/bin/ld";   /* default ld(1) to invoke */
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
5740Sstevel@tonic-gate const char *_dtrace_moddir = "dtrace";		/* kernel module directory */
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate int _dtrace_strbuckets = 211;	/* default number of hash buckets (prime) */
5770Sstevel@tonic-gate int _dtrace_intbuckets = 256;	/* default number of integer buckets (Pof2) */
5780Sstevel@tonic-gate uint_t _dtrace_strsize = 256;	/* default size of string intrinsic type */
5790Sstevel@tonic-gate uint_t _dtrace_stkindent = 14;	/* default whitespace indent for stack/ustack */
5800Sstevel@tonic-gate uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
5810Sstevel@tonic-gate uint_t _dtrace_pidlrulim = 8;	/* default number of pid handles to cache */
5820Sstevel@tonic-gate size_t _dtrace_bufsize = 512;	/* default dt_buf_create() size */
5830Sstevel@tonic-gate int _dtrace_argmax = 32;	/* default maximum number of probe arguments */
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate int _dtrace_debug = 0;		/* debug messages enabled (off) */
5860Sstevel@tonic-gate const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
5870Sstevel@tonic-gate int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate typedef struct dt_fdlist {
5900Sstevel@tonic-gate 	int *df_fds;		/* array of provider driver file descriptors */
5910Sstevel@tonic-gate 	uint_t df_ents;		/* number of valid elements in df_fds[] */
5920Sstevel@tonic-gate 	uint_t df_size;		/* size of df_fds[] */
5930Sstevel@tonic-gate } dt_fdlist_t;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate #pragma init(_dtrace_init)
5960Sstevel@tonic-gate void
5970Sstevel@tonic-gate _dtrace_init(void)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	_dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
6020Sstevel@tonic-gate 		if (rd_init(_dtrace_rdvers) == RD_OK)
6030Sstevel@tonic-gate 			break;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate static dtrace_hdl_t *
6080Sstevel@tonic-gate set_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	if (dtp != NULL)
6110Sstevel@tonic-gate 		dtrace_close(dtp);
6120Sstevel@tonic-gate 	if (errp != NULL)
6130Sstevel@tonic-gate 		*errp = err;
6140Sstevel@tonic-gate 	return (NULL);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static void
6180Sstevel@tonic-gate dt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp, const char *dirname,
6190Sstevel@tonic-gate     const char *subdir, const char *isadir)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	dt_provmod_t *prov;
6220Sstevel@tonic-gate 	char path[PATH_MAX];
6230Sstevel@tonic-gate 	struct dirent *dp, *ep;
6240Sstevel@tonic-gate 	DIR *dirp;
6250Sstevel@tonic-gate 	int fd;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (isadir) {
6280Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s/%s/%s",
6290Sstevel@tonic-gate 		    dirname, subdir, isadir);
6300Sstevel@tonic-gate 	} else {
6310Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s/%s",
6320Sstevel@tonic-gate 		    dirname, subdir);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if ((dirp = opendir(path)) == NULL)
6360Sstevel@tonic-gate 		return; /* failed to open directory; just skip it */
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
6390Sstevel@tonic-gate 	bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
6420Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
6430Sstevel@tonic-gate 			continue; /* skip "." and ".." */
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		if (dfp->df_ents == dfp->df_size) {
6460Sstevel@tonic-gate 			uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
6470Sstevel@tonic-gate 			int *fds = realloc(dfp->df_fds, size * sizeof (int));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 			if (fds == NULL)
6500Sstevel@tonic-gate 				break; /* skip the rest of this directory */
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 			dfp->df_fds = fds;
6530Sstevel@tonic-gate 			dfp->df_size = size;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path),
6570Sstevel@tonic-gate 		    "/devices/pseudo/%s@0:%s", dp->d_name, dp->d_name);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		if ((fd = open(path, O_RDONLY)) == -1)
6600Sstevel@tonic-gate 			continue; /* failed to open driver; just skip it */
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
6630Sstevel@tonic-gate 		    (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
6640Sstevel@tonic-gate 			free(prov);
6650Sstevel@tonic-gate 			(void) close(fd);
6660Sstevel@tonic-gate 			break;
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		(void) strcpy(prov->dp_name, dp->d_name);
6700Sstevel@tonic-gate 		prov->dp_next = *provmod;
6710Sstevel@tonic-gate 		*provmod = prov;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		dt_dprintf("opened provider %s\n", dp->d_name);
6740Sstevel@tonic-gate 		dfp->df_fds[dfp->df_ents++] = fd;
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	(void) closedir(dirp);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate static void
6810Sstevel@tonic-gate dt_provmod_destroy(dt_provmod_t **provmod)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	dt_provmod_t *next, *current;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	for (current = *provmod; current != NULL; current = next) {
6860Sstevel@tonic-gate 		next = current->dp_next;
6870Sstevel@tonic-gate 		free(current->dp_name);
6880Sstevel@tonic-gate 		free(current);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	*provmod = NULL;
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate static const char *
6950Sstevel@tonic-gate dt_get_sysinfo(int cmd, char *buf, size_t len)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	ssize_t rv = sysinfo(cmd, buf, len);
6980Sstevel@tonic-gate 	char *p = buf;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (rv < 0 || rv > len)
7010Sstevel@tonic-gate 		(void) snprintf(buf, len, "%s", "Unknown");
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	while ((p = strchr(p, '.')) != NULL)
7040Sstevel@tonic-gate 		*p++ = '_';
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (buf);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static dtrace_hdl_t *
7100Sstevel@tonic-gate dt_vopen(int version, int flags, int *errp,
7110Sstevel@tonic-gate     const dtrace_vector_t *vector, void *arg)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	dtrace_hdl_t *dtp = NULL;
7140Sstevel@tonic-gate 	int dtfd = -1, ftfd = -1, fterr = 0;
7150Sstevel@tonic-gate 	dtrace_prog_t *pgp;
7160Sstevel@tonic-gate 	dt_module_t *dmp;
7170Sstevel@tonic-gate 	dt_provmod_t *provmod = NULL;
7180Sstevel@tonic-gate 	int i, err;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	const dt_intrinsic_t *dinp;
7210Sstevel@tonic-gate 	const dt_typedef_t *dtyp;
7220Sstevel@tonic-gate 	const dt_ident_t *idp;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	dtrace_typeinfo_t dtt;
7250Sstevel@tonic-gate 	ctf_funcinfo_t ctc;
7260Sstevel@tonic-gate 	ctf_arinfo_t ctr;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	dt_fdlist_t df = { NULL, 0, 0 };
7290Sstevel@tonic-gate 	char *p, *q, *path = NULL;
7300Sstevel@tonic-gate 	const char *isadir = NULL;
7310Sstevel@tonic-gate 	int pathlen;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	char isadef[32], utsdef[32];
7340Sstevel@tonic-gate 	char s1[64], s2[64];
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (version <= 0)
7370Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EINVAL));
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (version > DTRACE_VERSION)
7400Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_VERSION));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (flags & ~DTRACE_O_MASK)
7430Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EINVAL));
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
7460Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EINVAL));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (vector == NULL && arg != NULL)
7490Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EINVAL));
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE)
7520Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_ELFVERSION));
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (vector != NULL || (flags & DTRACE_O_NODEV))
7550Sstevel@tonic-gate 		goto alloc; /* do not attempt to open dtrace device */
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	/*
7580Sstevel@tonic-gate 	 * For each directory in the kernel's module path, build the name of
7590Sstevel@tonic-gate 	 * the corresponding dtrace provider subdirectory and attempt to open a
7600Sstevel@tonic-gate 	 * pseudo-driver whose name matches the name of each provider therein.
7610Sstevel@tonic-gate 	 * We hold them open in the df.df_fds list until we open the DTrace
7620Sstevel@tonic-gate 	 * driver itself, allowing us to see all of the probes provided on this
7630Sstevel@tonic-gate 	 * system.  Once we have the DTrace driver open, we can safely close
7640Sstevel@tonic-gate 	 * all the providers now that they have registered with the framework.
7650Sstevel@tonic-gate 	 */
7660Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isadef, sizeof (isadef)) > 0) {
7670Sstevel@tonic-gate 		if (strstr(isadef, "sparcv9") != NULL)
7680Sstevel@tonic-gate 			isadir = "sparcv9";
7690Sstevel@tonic-gate 		else if (strstr(isadef, "amd64") != NULL)
7700Sstevel@tonic-gate 			isadir = "amd64";
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (modctl(MODGETPATHLEN, NULL, &pathlen) == 0 &&
7740Sstevel@tonic-gate 	    (path = malloc(pathlen + 1)) != NULL &&
7750Sstevel@tonic-gate 	    modctl(MODGETPATH, NULL, path) == 0) {
7760Sstevel@tonic-gate 		for (p = path; *p != '\0'; p = q) {
7770Sstevel@tonic-gate 			if ((q = strchr(p, ' ')) != NULL)
7780Sstevel@tonic-gate 				*q++ = '\0';
7790Sstevel@tonic-gate 			else
7800Sstevel@tonic-gate 				q = p + strlen(p);
7810Sstevel@tonic-gate 			dt_provmod_open(&provmod, &df, p,
7820Sstevel@tonic-gate 			    _dtrace_moddir, isadir);
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	dtfd = open("/devices/pseudo/dtrace@0:dtrace", O_RDWR);
7870Sstevel@tonic-gate 	err = errno; /* save errno from opening dtfd */
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	ftfd = open("/devices/pseudo/fasttrap@0:fasttrap", O_RDWR);
7900Sstevel@tonic-gate 	fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	while (df.df_ents-- != 0)
7930Sstevel@tonic-gate 		(void) close(df.df_fds[df.df_ents]);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	free(df.df_fds);
7960Sstevel@tonic-gate 	free(path);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * If we failed to open the dtrace device, fail dtrace_open().
8000Sstevel@tonic-gate 	 * We convert some kernel errnos to custom libdtrace errnos to
8010Sstevel@tonic-gate 	 * improve the resulting message from the usual strerror().
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate 	if (dtfd == -1) {
8040Sstevel@tonic-gate 		dt_provmod_destroy(&provmod);
8050Sstevel@tonic-gate 		switch (err) {
8060Sstevel@tonic-gate 		case ENOENT:
8070Sstevel@tonic-gate 			if (getzoneid() != GLOBAL_ZONEID)
8080Sstevel@tonic-gate 				err = EDT_ZNOENT;
8090Sstevel@tonic-gate 			else
8100Sstevel@tonic-gate 				err = EDT_GNOENT;
8110Sstevel@tonic-gate 			break;
8120Sstevel@tonic-gate 		case EBUSY:
8130Sstevel@tonic-gate 			err = EDT_BUSY;
8140Sstevel@tonic-gate 			break;
8150Sstevel@tonic-gate 		case EACCES:
8160Sstevel@tonic-gate 			err = EDT_ACCESS;
8170Sstevel@tonic-gate 			break;
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, err));
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	(void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
8230Sstevel@tonic-gate 	(void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate alloc:
8260Sstevel@tonic-gate 	if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
8270Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	bzero(dtp, sizeof (dtrace_hdl_t));
8300Sstevel@tonic-gate 	dtp->dt_oflags = flags;
8310Sstevel@tonic-gate 	dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
8320Sstevel@tonic-gate 	dtp->dt_linkmode = DT_LINK_KERNEL;
8330Sstevel@tonic-gate 	dtp->dt_linktype = DT_LTYP_ELF;
8340Sstevel@tonic-gate 	dtp->dt_stdcmode = DT_STDC_XA;
8350Sstevel@tonic-gate 	dtp->dt_version = version;
8360Sstevel@tonic-gate 	dtp->dt_fd = dtfd;
8370Sstevel@tonic-gate 	dtp->dt_ftfd = ftfd;
8380Sstevel@tonic-gate 	dtp->dt_fterr = fterr;
8390Sstevel@tonic-gate 	dtp->dt_cdefs_fd = -1;
8400Sstevel@tonic-gate 	dtp->dt_ddefs_fd = -1;
8410Sstevel@tonic-gate 	dtp->dt_stdout_fd = -1;
8420Sstevel@tonic-gate 	dtp->dt_modbuckets = _dtrace_strbuckets;
8430Sstevel@tonic-gate 	dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
8440Sstevel@tonic-gate 	dtp->dt_provbuckets = _dtrace_strbuckets;
8450Sstevel@tonic-gate 	dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
8460Sstevel@tonic-gate 	dt_proc_hash_create(dtp);
8470Sstevel@tonic-gate 	dtp->dt_vmax = DT_VERS_LATEST;
8480Sstevel@tonic-gate 	dtp->dt_cpp_path = strdup(_dtrace_defcpp);
8490Sstevel@tonic-gate 	dtp->dt_cpp_argv = malloc(sizeof (char *));
8500Sstevel@tonic-gate 	dtp->dt_cpp_argc = 1;
8510Sstevel@tonic-gate 	dtp->dt_cpp_args = 1;
8520Sstevel@tonic-gate 	dtp->dt_ld_path = strdup(_dtrace_defld);
8530Sstevel@tonic-gate 	dtp->dt_provmod = provmod;
8540Sstevel@tonic-gate 	dtp->dt_vector = vector;
8550Sstevel@tonic-gate 	dtp->dt_varg = arg;
8560Sstevel@tonic-gate 	dt_dof_init(dtp);
8570Sstevel@tonic-gate 	(void) uname(&dtp->dt_uts);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
8600Sstevel@tonic-gate 	    dtp->dt_procs == NULL || dtp->dt_ld_path == NULL ||
8610Sstevel@tonic-gate 	    dtp->dt_cpp_path == NULL || dtp->dt_cpp_argv == NULL)
8620Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++)
8650Sstevel@tonic-gate 		dtp->dt_options[i] = DTRACEOPT_UNSET;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	dtp->dt_cpp_argv[0] = (char *)strbasename(dtp->dt_cpp_path);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	(void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
8700Sstevel@tonic-gate 	    (uint_t)(sizeof (void *) * NBBY));
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	(void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
8730Sstevel@tonic-gate 	    dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
8740Sstevel@tonic-gate 	    dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
8770Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
8780Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
8790Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
8800Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, isadef) == NULL ||
8810Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, utsdef) == NULL)
8820Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (flags & DTRACE_O_NODEV)
8850Sstevel@tonic-gate 		bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
8860Sstevel@tonic-gate 	else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
8870Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, errno));
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (flags & DTRACE_O_LP64)
8900Sstevel@tonic-gate 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
8910Sstevel@tonic-gate 	else if (flags & DTRACE_O_ILP32)
8920Sstevel@tonic-gate 		dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate #ifdef __sparc
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * On SPARC systems, __sparc is always defined for <sys/isa_defs.h>
8970Sstevel@tonic-gate 	 * and __sparcv9 is defined if we are doing a 64-bit compile.
8980Sstevel@tonic-gate 	 */
8990Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, "-D__sparc") == NULL)
9000Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64 &&
9030Sstevel@tonic-gate 	    dt_cpp_add_arg(dtp, "-D__sparcv9") == NULL)
9040Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
9050Sstevel@tonic-gate #endif
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate #ifdef __x86
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
9100Sstevel@tonic-gate 	 * compiles and __amd64 is defined for 64-bit compiles.  Unlike SPARC,
9110Sstevel@tonic-gate 	 * they are defined exclusive of one another (see PSARC 2004/619).
9120Sstevel@tonic-gate 	 */
9130Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
9140Sstevel@tonic-gate 		p = dt_cpp_add_arg(dtp, "-D__amd64");
9150Sstevel@tonic-gate 	else
9160Sstevel@tonic-gate 		p = dt_cpp_add_arg(dtp, "-D__i386");
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (p == NULL)
9190Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
9200Sstevel@tonic-gate #endif
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
9230Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_DIFVERS));
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
9260Sstevel@tonic-gate 		bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
9270Sstevel@tonic-gate 	else
9280Sstevel@tonic-gate 		bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
9310Sstevel@tonic-gate 	dtp->dt_aggs = dt_idhash_create("aggregation", NULL, 0, UINT_MAX);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
9340Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	dtp->dt_tls = dt_idhash_create("thread local", NULL,
9370Sstevel@tonic-gate 	    DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
9400Sstevel@tonic-gate 	    dtp->dt_globals == NULL || dtp->dt_tls == NULL)
9410Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * Populate the dt_macros identifier hash table by hand: we can't use
9450Sstevel@tonic-gate 	 * the dt_idhash_populate() mechanism because we're not yet compiling
9460Sstevel@tonic-gate 	 * and dtrace_update() needs to immediately reference these idents.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
9490Sstevel@tonic-gate 		if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
9500Sstevel@tonic-gate 		    idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
9510Sstevel@tonic-gate 		    idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
9520Sstevel@tonic-gate 		    idp->di_iarg, 0) == NULL)
9530Sstevel@tonic-gate 			return (set_open_errno(dtp, errp, EDT_NOMEM));
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * Update the module list using /system/object and load the values for
9580Sstevel@tonic-gate 	 * the macro variable definitions according to the current process.
9590Sstevel@tonic-gate 	 */
9600Sstevel@tonic-gate 	dtrace_update(dtp);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Select the intrinsics and typedefs we want based on the data model.
9640Sstevel@tonic-gate 	 * The intrinsics are under "C".  The typedefs are added under "D".
9650Sstevel@tonic-gate 	 */
9660Sstevel@tonic-gate 	if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
9670Sstevel@tonic-gate 		dinp = _dtrace_intrinsics_32;
9680Sstevel@tonic-gate 		dtyp = _dtrace_typedefs_32;
9690Sstevel@tonic-gate 	} else {
9700Sstevel@tonic-gate 		dinp = _dtrace_intrinsics_64;
9710Sstevel@tonic-gate 		dtyp = _dtrace_typedefs_64;
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * Create a dynamic CTF container under the "C" scope for intrinsic
9760Sstevel@tonic-gate 	 * types and types defined in ANSI-C header files that are included.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
9790Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
9820Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	dt_dprintf("created CTF container for %s (%p)\n",
9850Sstevel@tonic-gate 	    dmp->dm_name, (void *)dmp->dm_ctfp);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
9880Sstevel@tonic-gate 	ctf_setspecific(dmp->dm_ctfp, dmp);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
9910Sstevel@tonic-gate 	dmp->dm_modid = -1; /* no module ID */
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Fill the dynamic "C" CTF container with all of the intrinsic
9950Sstevel@tonic-gate 	 * integer and floating-point types appropriate for this data model.
9960Sstevel@tonic-gate 	 */
9970Sstevel@tonic-gate 	for (; dinp->din_name != NULL; dinp++) {
9980Sstevel@tonic-gate 		if (dinp->din_kind == CTF_K_INTEGER) {
9990Sstevel@tonic-gate 			err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
10000Sstevel@tonic-gate 			    dinp->din_name, &dinp->din_data);
10010Sstevel@tonic-gate 		} else {
10020Sstevel@tonic-gate 			err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
10030Sstevel@tonic-gate 			    dinp->din_name, &dinp->din_data);
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 		if (err == CTF_ERR) {
10070Sstevel@tonic-gate 			dt_dprintf("failed to add %s to C container: %s\n",
10080Sstevel@tonic-gate 			    dinp->din_name, ctf_errmsg(
10090Sstevel@tonic-gate 			    ctf_errno(dmp->dm_ctfp)));
10100Sstevel@tonic-gate 			return (set_open_errno(dtp, errp, EDT_CTF));
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (ctf_update(dmp->dm_ctfp) != 0) {
10150Sstevel@tonic-gate 		dt_dprintf("failed to update C container: %s\n",
10160Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10170Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * Add intrinsic pointer types that are needed to initialize printf
10220Sstevel@tonic-gate 	 * format dictionary types (see table in dt_printf.c).
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
10250Sstevel@tonic-gate 	    ctf_lookup_by_name(dmp->dm_ctfp, "void"));
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
10280Sstevel@tonic-gate 	    ctf_lookup_by_name(dmp->dm_ctfp, "char"));
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	(void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT,
10310Sstevel@tonic-gate 	    ctf_lookup_by_name(dmp->dm_ctfp, "int"));
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (ctf_update(dmp->dm_ctfp) != 0) {
10340Sstevel@tonic-gate 		dt_dprintf("failed to update C container: %s\n",
10350Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10360Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/*
10400Sstevel@tonic-gate 	 * Create a dynamic CTF container under the "D" scope for types that
10410Sstevel@tonic-gate 	 * are defined by the D program itself or on-the-fly by the D compiler.
10420Sstevel@tonic-gate 	 * The "D" CTF container is a child of the "C" CTF container.
10430Sstevel@tonic-gate 	 */
10440Sstevel@tonic-gate 	if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
10450Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_NOMEM));
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
10480Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	dt_dprintf("created CTF container for %s (%p)\n",
10510Sstevel@tonic-gate 	    dmp->dm_name, (void *)dmp->dm_ctfp);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	(void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
10540Sstevel@tonic-gate 	ctf_setspecific(dmp->dm_ctfp, dmp);
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
10570Sstevel@tonic-gate 	dmp->dm_modid = -1; /* no module ID */
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
10600Sstevel@tonic-gate 		dt_dprintf("failed to import D parent container: %s\n",
10610Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10620Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/*
10660Sstevel@tonic-gate 	 * Fill the dynamic "D" CTF container with all of the built-in typedefs
10670Sstevel@tonic-gate 	 * that we need to use for our D variable and function definitions.
10680Sstevel@tonic-gate 	 * This ensures that basic inttypes.h names are always available to us.
10690Sstevel@tonic-gate 	 */
10700Sstevel@tonic-gate 	for (; dtyp->dty_src != NULL; dtyp++) {
10710Sstevel@tonic-gate 		if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
10720Sstevel@tonic-gate 		    dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
10730Sstevel@tonic-gate 		    dtyp->dty_src)) == CTF_ERR) {
10740Sstevel@tonic-gate 			dt_dprintf("failed to add typedef %s %s to D "
10750Sstevel@tonic-gate 			    "container: %s", dtyp->dty_src, dtyp->dty_dst,
10760Sstevel@tonic-gate 			    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
10770Sstevel@tonic-gate 			return (set_open_errno(dtp, errp, EDT_CTF));
10780Sstevel@tonic-gate 		}
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * Insert a CTF ID corresponding to a pointer to a type of kind
10830Sstevel@tonic-gate 	 * CTF_K_FUNCTION we can use in the compiler for function pointers.
10840Sstevel@tonic-gate 	 * CTF treats all function pointers as "int (*)()" so we only need one.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
10870Sstevel@tonic-gate 	ctc.ctc_argc = 0;
10880Sstevel@tonic-gate 	ctc.ctc_flags = 0;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	dtp->dt_type_func = ctf_add_function(dmp->dm_ctfp,
10910Sstevel@tonic-gate 	    CTF_ADD_ROOT, &ctc, NULL);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp,
10940Sstevel@tonic-gate 	    CTF_ADD_ROOT, dtp->dt_type_func);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/*
10970Sstevel@tonic-gate 	 * We also insert CTF definitions for the special D intrinsic types
10980Sstevel@tonic-gate 	 * string and <DYN> into the D container.  The string type is added
10990Sstevel@tonic-gate 	 * as a typedef of char[n].  The <DYN> type is an alias for void.
11000Sstevel@tonic-gate 	 * We compare types to these special CTF ids throughout the compiler.
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
11030Sstevel@tonic-gate 	ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
11040Sstevel@tonic-gate 	ctr.ctr_nelems = _dtrace_strsize;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
11070Sstevel@tonic-gate 	    "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
11100Sstevel@tonic-gate 	    "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
11130Sstevel@tonic-gate 	    "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
11160Sstevel@tonic-gate 	    dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
11170Sstevel@tonic-gate 	    dtp->dt_type_stack == CTF_ERR) {
11180Sstevel@tonic-gate 		dt_dprintf("failed to add intrinsic to D container: %s\n",
11190Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
11200Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (ctf_update(dmp->dm_ctfp) != 0) {
11240Sstevel@tonic-gate 		dt_dprintf("failed update D container: %s\n",
11250Sstevel@tonic-gate 		    ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
11260Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_CTF));
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/*
11300Sstevel@tonic-gate 	 * Initialize the integer description table used to convert integer
11310Sstevel@tonic-gate 	 * constants to the appropriate types.  Refer to the comments above
11320Sstevel@tonic-gate 	 * dt_node_int() for a complete description of how this table is used.
11330Sstevel@tonic-gate 	 */
11340Sstevel@tonic-gate 	for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
11350Sstevel@tonic-gate 		if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
11360Sstevel@tonic-gate 		    dtp->dt_ints[i].did_name, &dtt) != 0) {
11370Sstevel@tonic-gate 			dt_dprintf("failed to lookup integer type %s: %s\n",
11380Sstevel@tonic-gate 			    dtp->dt_ints[i].did_name,
11390Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11400Sstevel@tonic-gate 			return (set_open_errno(dtp, errp, dtp->dt_errno));
11410Sstevel@tonic-gate 		}
11420Sstevel@tonic-gate 		dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
11430Sstevel@tonic-gate 		dtp->dt_ints[i].did_type = dtt.dtt_type;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * Now that we've created the "C" and "D" containers, move them to the
11480Sstevel@tonic-gate 	 * start of the module list so that these types and symbols are found
11490Sstevel@tonic-gate 	 * first (for stability) when iterating through the module list.
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
11520Sstevel@tonic-gate 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
11550Sstevel@tonic-gate 	dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if (dt_pfdict_create(dtp) == -1)
11580Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, dtp->dt_errno));
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/*
11610Sstevel@tonic-gate 	 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
11620Sstevel@tonic-gate 	 * because without /dev/dtrace open, we will not be able to load the
11630Sstevel@tonic-gate 	 * names and attributes of any providers or probes from the kernel.
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	if (flags & DTRACE_O_NODEV)
11660Sstevel@tonic-gate 		dtp->dt_cflags |= DTRACE_C_ZDEFS;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	/*
11690Sstevel@tonic-gate 	 * Load hard-wired inlines into the definition cache by calling the
11700Sstevel@tonic-gate 	 * compiler on the raw definition string defined above.
11710Sstevel@tonic-gate 	 */
11720Sstevel@tonic-gate 	if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
11730Sstevel@tonic-gate 	    DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
11740Sstevel@tonic-gate 		dt_dprintf("failed to load hard-wired definitions: %s\n",
11750Sstevel@tonic-gate 		    dtrace_errmsg(dtp, dtrace_errno(dtp)));
11760Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, EDT_HARDWIRE));
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	dtrace_program_destroy(dtp, pgp);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	/*
11820Sstevel@tonic-gate 	 * Set up the default DTrace library path.  Once set, the next call to
11830Sstevel@tonic-gate 	 * dt_compile() will compile all the libraries.  We intentionally defer
11840Sstevel@tonic-gate 	 * library processing to improve overhead for clients that don't ever
11850Sstevel@tonic-gate 	 * compile, and to provide better error reporting (because the full
11860Sstevel@tonic-gate 	 * reporting of compiler errors requires dtrace_open() to succeed).
11870Sstevel@tonic-gate 	 */
11880Sstevel@tonic-gate 	if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
11890Sstevel@tonic-gate 		return (set_open_errno(dtp, errp, dtp->dt_errno));
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	return (dtp);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate dtrace_hdl_t *
11950Sstevel@tonic-gate dtrace_open(int version, int flags, int *errp)
11960Sstevel@tonic-gate {
11970Sstevel@tonic-gate 	return (dt_vopen(version, flags, errp, NULL, NULL));
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate dtrace_hdl_t *
12010Sstevel@tonic-gate dtrace_vopen(int version, int flags, int *errp,
12020Sstevel@tonic-gate     const dtrace_vector_t *vector, void *arg)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate 	return (dt_vopen(version, flags, errp, vector, arg));
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate void
12080Sstevel@tonic-gate dtrace_close(dtrace_hdl_t *dtp)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	dt_ident_t *idp, *ndp;
12110Sstevel@tonic-gate 	dt_module_t *dmp;
12120Sstevel@tonic-gate 	dt_provider_t *pvp;
12130Sstevel@tonic-gate 	dtrace_prog_t *pgp;
12140Sstevel@tonic-gate 	dt_xlator_t *dxp;
12150Sstevel@tonic-gate 	dt_dirpath_t *dirp;
12160Sstevel@tonic-gate 	int i;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
12190Sstevel@tonic-gate 		dtrace_program_destroy(dtp, pgp);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
12220Sstevel@tonic-gate 		dt_xlator_destroy(dtp, dxp);
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
12250Sstevel@tonic-gate 		ndp = idp->di_next;
12260Sstevel@tonic-gate 		dt_ident_destroy(idp);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	if (dtp->dt_macros != NULL)
12300Sstevel@tonic-gate 		dt_idhash_destroy(dtp->dt_macros);
12310Sstevel@tonic-gate 	if (dtp->dt_aggs != NULL)
12320Sstevel@tonic-gate 		dt_idhash_destroy(dtp->dt_aggs);
12330Sstevel@tonic-gate 	if (dtp->dt_globals != NULL)
12340Sstevel@tonic-gate 		dt_idhash_destroy(dtp->dt_globals);
12350Sstevel@tonic-gate 	if (dtp->dt_tls != NULL)
12360Sstevel@tonic-gate 		dt_idhash_destroy(dtp->dt_tls);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
12390Sstevel@tonic-gate 		dt_module_destroy(dtp, dmp);
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
12420Sstevel@tonic-gate 		dt_provider_destroy(dtp, pvp);
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (dtp->dt_procs != NULL)
12450Sstevel@tonic-gate 		dt_proc_hash_destroy(dtp);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if (dtp->dt_fd != -1)
12480Sstevel@tonic-gate 		(void) close(dtp->dt_fd);
12490Sstevel@tonic-gate 	if (dtp->dt_ftfd != -1)
12500Sstevel@tonic-gate 		(void) close(dtp->dt_ftfd);
12510Sstevel@tonic-gate 	if (dtp->dt_cdefs_fd != -1)
12520Sstevel@tonic-gate 		(void) close(dtp->dt_cdefs_fd);
12530Sstevel@tonic-gate 	if (dtp->dt_ddefs_fd != -1)
12540Sstevel@tonic-gate 		(void) close(dtp->dt_ddefs_fd);
12550Sstevel@tonic-gate 	if (dtp->dt_stdout_fd != -1)
12560Sstevel@tonic-gate 		(void) close(dtp->dt_stdout_fd);
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	dt_epid_destroy(dtp);
12590Sstevel@tonic-gate 	dt_aggid_destroy(dtp);
12600Sstevel@tonic-gate 	dt_format_destroy(dtp);
12610Sstevel@tonic-gate 	dt_buffered_destroy(dtp);
12620Sstevel@tonic-gate 	dt_aggregate_destroy(dtp);
12630Sstevel@tonic-gate 	free(dtp->dt_buf.dtbd_data);
12640Sstevel@tonic-gate 	dt_pfdict_destroy(dtp);
12650Sstevel@tonic-gate 	dt_provmod_destroy(&dtp->dt_provmod);
12660Sstevel@tonic-gate 	dt_dof_fini(dtp);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	for (i = 1; i < dtp->dt_cpp_argc; i++)
12690Sstevel@tonic-gate 		free(dtp->dt_cpp_argv[i]);
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
12720Sstevel@tonic-gate 		dt_list_delete(&dtp->dt_lib_path, dirp);
12730Sstevel@tonic-gate 		free(dirp->dir_path);
12740Sstevel@tonic-gate 		free(dirp);
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	free(dtp->dt_cpp_argv);
12780Sstevel@tonic-gate 	free(dtp->dt_cpp_path);
12790Sstevel@tonic-gate 	free(dtp->dt_ld_path);
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	free(dtp->dt_mods);
12820Sstevel@tonic-gate 	free(dtp->dt_provs);
12830Sstevel@tonic-gate 	free(dtp);
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate int
12870Sstevel@tonic-gate dtrace_go(dtrace_hdl_t *dtp)
12880Sstevel@tonic-gate {
12890Sstevel@tonic-gate 	void *dof;
12900Sstevel@tonic-gate 	int err;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	if (dtp->dt_active)
12930Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * If a dtrace:::ERROR program and callback are registered, enable the
12970Sstevel@tonic-gate 	 * program before we start tracing.  If this fails for a vector open
12980Sstevel@tonic-gate 	 * with ENOTTY, we permit dtrace_go() to succeed so that vector clients
12990Sstevel@tonic-gate 	 * such as mdb's dtrace module can execute the rest of dtrace_go() even
13000Sstevel@tonic-gate 	 * though they do not provide support for the DTRACEIOC_ENABLE ioctl.
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 	if (dtp->dt_errprog != NULL &&
13030Sstevel@tonic-gate 	    dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && (
13040Sstevel@tonic-gate 	    dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL))
13050Sstevel@tonic-gate 		return (-1); /* dt_errno has been set for us */
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if ((dof = dtrace_getopt_dof(dtp)) == NULL)
13080Sstevel@tonic-gate 		return (-1); /* dt_errno has been set for us */
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	err = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
13110Sstevel@tonic-gate 	dtrace_dof_destroy(dtp, dof);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	if (err == -1 && (errno != ENOTTY || dtp->dt_vector == NULL))
13140Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) {
13170Sstevel@tonic-gate 		if (errno == EACCES)
13180Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_DESTRUCTIVE));
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 		if (errno == EALREADY)
13210Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_ISANON));
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 		if (errno == ENOENT)
13240Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOANON));
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 		if (errno == E2BIG)
13270Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_ENDTOOBIG));
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		if (errno == ENOSPC)
13300Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BUFTOOSMALL));
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	dtp->dt_active = 1;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	if (dt_options_load(dtp) == -1)
13380Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	return (dt_aggregate_go(dtp));
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate int
13440Sstevel@tonic-gate dtrace_stop(dtrace_hdl_t *dtp)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	if (dtp->dt_stopped)
13470Sstevel@tonic-gate 		return (0);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1)
13500Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	dtp->dt_stopped = 1;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	return (0);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate int
13580Sstevel@tonic-gate dtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	dt_provmod_t *prov;
13610Sstevel@tonic-gate 	int i = 0;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
13640Sstevel@tonic-gate 		if (i < nmods)
13650Sstevel@tonic-gate 			mods[i] = prov->dp_name;
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	return (i);
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate int
13720Sstevel@tonic-gate dtrace_ctlfd(dtrace_hdl_t *dtp)
13730Sstevel@tonic-gate {
13740Sstevel@tonic-gate 	return (dtp->dt_fd);
13750Sstevel@tonic-gate }
1376