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