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