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