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