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