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