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