Merge commit '7928f4baf4ab3230557eb6289be68aa7a3003f38'
[unleashed.git] / usr / src / lib / libdtrace / common / dt_open.c
blob78935c3cc39f7705e39bd9f3f363939bc4b48c20
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2017, Joyent, Inc.
25 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28 #include <sys/types.h>
29 #include <sys/modctl.h>
30 #include <sys/systeminfo.h>
31 #include <sys/resource.h>
33 #include <libelf.h>
34 #include <strings.h>
35 #include <alloca.h>
36 #include <limits.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <assert.h>
44 #define _POSIX_PTHREAD_SEMANTICS
45 #include <dirent.h>
46 #undef _POSIX_PTHREAD_SEMANTICS
48 #include <dt_impl.h>
49 #include <dt_program.h>
50 #include <dt_module.h>
51 #include <dt_printf.h>
52 #include <dt_string.h>
53 #include <dt_provider.h>
56 * Stability and versioning definitions. These #defines are used in the tables
57 * of identifiers below to fill in the attribute and version fields associated
58 * with each identifier. The DT_ATTR_* macros are a convenience to permit more
59 * concise declarations of common attributes such as Stable/Stable/Common. The
60 * DT_VERS_* macros declare the encoded integer values of all versions used so
61 * far. DT_VERS_LATEST must correspond to the latest version value among all
62 * versions exported by the D compiler. DT_VERS_STRING must be an ASCII string
63 * that contains DT_VERS_LATEST within it along with any suffixes (e.g. Beta).
64 * You must update DT_VERS_LATEST and DT_VERS_STRING when adding a new version,
65 * and then add the new version to the _dtrace_versions[] array declared below.
66 * Refer to the Solaris Dynamic Tracing Guide Stability and Versioning chapters
67 * respectively for an explanation of these DTrace features and their values.
69 * NOTE: Although the DTrace versioning scheme supports the labeling and
70 * introduction of incompatible changes (e.g. dropping an interface in a
71 * major release), the libdtrace code does not currently support this.
72 * All versions are assumed to strictly inherit from one another. If
73 * we ever need to provide divergent interfaces, this will need work.
75 #define DT_ATTR_STABCMN { DTRACE_STABILITY_STABLE, \
76 DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }
78 #define DT_ATTR_EVOLCMN { DTRACE_STABILITY_EVOLVING, \
79 DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON \
83 * The version number should be increased for every customer visible release
84 * of DTrace. The major number should be incremented when a fundamental
85 * change has been made that would affect all consumers, and would reflect
86 * sweeping changes to DTrace or the D language. The minor number should be
87 * incremented when a change is introduced that could break scripts that had
88 * previously worked; for example, adding a new built-in variable could break
89 * a script which was already using that identifier. The micro number should
90 * be changed when introducing functionality changes or major bug fixes that
91 * do not affect backward compatibility -- this is merely to make capabilities
92 * easily determined from the version number. Minor bugs do not require any
93 * modification to the version number.
95 #define DT_VERS_1_0 DT_VERSION_NUMBER(1, 0, 0)
96 #define DT_VERS_1_1 DT_VERSION_NUMBER(1, 1, 0)
97 #define DT_VERS_1_2 DT_VERSION_NUMBER(1, 2, 0)
98 #define DT_VERS_1_2_1 DT_VERSION_NUMBER(1, 2, 1)
99 #define DT_VERS_1_2_2 DT_VERSION_NUMBER(1, 2, 2)
100 #define DT_VERS_1_3 DT_VERSION_NUMBER(1, 3, 0)
101 #define DT_VERS_1_4 DT_VERSION_NUMBER(1, 4, 0)
102 #define DT_VERS_1_4_1 DT_VERSION_NUMBER(1, 4, 1)
103 #define DT_VERS_1_5 DT_VERSION_NUMBER(1, 5, 0)
104 #define DT_VERS_1_6 DT_VERSION_NUMBER(1, 6, 0)
105 #define DT_VERS_1_6_1 DT_VERSION_NUMBER(1, 6, 1)
106 #define DT_VERS_1_6_2 DT_VERSION_NUMBER(1, 6, 2)
107 #define DT_VERS_1_6_3 DT_VERSION_NUMBER(1, 6, 3)
108 #define DT_VERS_1_7 DT_VERSION_NUMBER(1, 7, 0)
109 #define DT_VERS_1_7_1 DT_VERSION_NUMBER(1, 7, 1)
110 #define DT_VERS_1_8 DT_VERSION_NUMBER(1, 8, 0)
111 #define DT_VERS_1_8_1 DT_VERSION_NUMBER(1, 8, 1)
112 #define DT_VERS_1_9 DT_VERSION_NUMBER(1, 9, 0)
113 #define DT_VERS_1_9_1 DT_VERSION_NUMBER(1, 9, 1)
114 #define DT_VERS_1_10 DT_VERSION_NUMBER(1, 10, 0)
115 #define DT_VERS_1_11 DT_VERSION_NUMBER(1, 11, 0)
116 #define DT_VERS_1_12 DT_VERSION_NUMBER(1, 12, 0)
117 #define DT_VERS_1_12_1 DT_VERSION_NUMBER(1, 12, 1)
118 #define DT_VERS_1_13 DT_VERSION_NUMBER(1, 13, 0)
119 #define DT_VERS_LATEST DT_VERS_1_13
120 #define DT_VERS_STRING "Sun D 1.13"
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 DT_VERS_1_7_1, /* D API 1.7.1 */
138 DT_VERS_1_8, /* D API 1.8 */
139 DT_VERS_1_8_1, /* D API 1.8.1 */
140 DT_VERS_1_9, /* D API 1.9 */
141 DT_VERS_1_9_1, /* D API 1.9.1 */
142 DT_VERS_1_10, /* D API 1.10 */
143 DT_VERS_1_11, /* D API 1.11 */
144 DT_VERS_1_12, /* D API 1.12 */
145 DT_VERS_1_12_1, /* D API 1.12.1 */
146 DT_VERS_1_13, /* D API 1.13 */
151 * Table of global identifiers. This is used to populate the global identifier
152 * hash when a new dtrace client open occurs. For more info see dt_ident.h.
153 * The global identifiers that represent functions use the dt_idops_func ops
154 * and specify the private data pointer as a prototype string which is parsed
155 * when the identifier is first encountered. These prototypes look like ANSI
156 * C function prototypes except that the special symbol "@" can be used as a
157 * wildcard to represent a single parameter of any type (i.e. any dt_node_t).
158 * The standard "..." notation can also be used to represent varargs. An empty
159 * parameter list is taken to mean void (that is, no arguments are permitted).
160 * A parameter enclosed in square brackets (e.g. "[int]") denotes an optional
161 * argument.
163 static const dt_ident_t _dtrace_globals[] = {
164 { "alloca", DT_IDENT_FUNC, 0, DIF_SUBR_ALLOCA, DT_ATTR_STABCMN, DT_VERS_1_0,
165 &dt_idops_func, "void *(size_t)" },
166 { "arg0", DT_IDENT_SCALAR, 0, DIF_VAR_ARG0, DT_ATTR_STABCMN, DT_VERS_1_0,
167 &dt_idops_type, "int64_t" },
168 { "arg1", DT_IDENT_SCALAR, 0, DIF_VAR_ARG1, DT_ATTR_STABCMN, DT_VERS_1_0,
169 &dt_idops_type, "int64_t" },
170 { "arg2", DT_IDENT_SCALAR, 0, DIF_VAR_ARG2, DT_ATTR_STABCMN, DT_VERS_1_0,
171 &dt_idops_type, "int64_t" },
172 { "arg3", DT_IDENT_SCALAR, 0, DIF_VAR_ARG3, DT_ATTR_STABCMN, DT_VERS_1_0,
173 &dt_idops_type, "int64_t" },
174 { "arg4", DT_IDENT_SCALAR, 0, DIF_VAR_ARG4, DT_ATTR_STABCMN, DT_VERS_1_0,
175 &dt_idops_type, "int64_t" },
176 { "arg5", DT_IDENT_SCALAR, 0, DIF_VAR_ARG5, DT_ATTR_STABCMN, DT_VERS_1_0,
177 &dt_idops_type, "int64_t" },
178 { "arg6", DT_IDENT_SCALAR, 0, DIF_VAR_ARG6, DT_ATTR_STABCMN, DT_VERS_1_0,
179 &dt_idops_type, "int64_t" },
180 { "arg7", DT_IDENT_SCALAR, 0, DIF_VAR_ARG7, DT_ATTR_STABCMN, DT_VERS_1_0,
181 &dt_idops_type, "int64_t" },
182 { "arg8", DT_IDENT_SCALAR, 0, DIF_VAR_ARG8, DT_ATTR_STABCMN, DT_VERS_1_0,
183 &dt_idops_type, "int64_t" },
184 { "arg9", DT_IDENT_SCALAR, 0, DIF_VAR_ARG9, DT_ATTR_STABCMN, DT_VERS_1_0,
185 &dt_idops_type, "int64_t" },
186 { "args", DT_IDENT_ARRAY, 0, DIF_VAR_ARGS, DT_ATTR_STABCMN, DT_VERS_1_0,
187 &dt_idops_args, NULL },
188 { "avg", DT_IDENT_AGGFUNC, 0, DTRACEAGG_AVG, DT_ATTR_STABCMN, DT_VERS_1_0,
189 &dt_idops_func, "void(@)" },
190 { "basename", DT_IDENT_FUNC, 0, DIF_SUBR_BASENAME, DT_ATTR_STABCMN, DT_VERS_1_0,
191 &dt_idops_func, "string(const char *)" },
192 { "bcopy", DT_IDENT_FUNC, 0, DIF_SUBR_BCOPY, DT_ATTR_STABCMN, DT_VERS_1_0,
193 &dt_idops_func, "void(void *, void *, size_t)" },
194 { "breakpoint", DT_IDENT_ACTFUNC, 0, DT_ACT_BREAKPOINT,
195 DT_ATTR_STABCMN, DT_VERS_1_0,
196 &dt_idops_func, "void()" },
197 { "caller", DT_IDENT_SCALAR, 0, DIF_VAR_CALLER, DT_ATTR_STABCMN, DT_VERS_1_0,
198 &dt_idops_type, "uintptr_t" },
199 { "chill", DT_IDENT_ACTFUNC, 0, DT_ACT_CHILL, DT_ATTR_STABCMN, DT_VERS_1_0,
200 &dt_idops_func, "void(int)" },
201 { "cleanpath", DT_IDENT_FUNC, 0, DIF_SUBR_CLEANPATH, DT_ATTR_STABCMN,
202 DT_VERS_1_0, &dt_idops_func, "string(const char *)" },
203 { "clear", DT_IDENT_ACTFUNC, 0, DT_ACT_CLEAR, DT_ATTR_STABCMN, DT_VERS_1_0,
204 &dt_idops_func, "void(...)" },
205 { "commit", DT_IDENT_ACTFUNC, 0, DT_ACT_COMMIT, DT_ATTR_STABCMN, DT_VERS_1_0,
206 &dt_idops_func, "void(int)" },
207 { "copyin", DT_IDENT_FUNC, 0, DIF_SUBR_COPYIN, DT_ATTR_STABCMN, DT_VERS_1_0,
208 &dt_idops_func, "void *(uintptr_t, size_t)" },
209 { "copyinstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINSTR,
210 DT_ATTR_STABCMN, DT_VERS_1_0,
211 &dt_idops_func, "string(uintptr_t, [size_t])" },
212 { "copyinto", DT_IDENT_FUNC, 0, DIF_SUBR_COPYINTO, DT_ATTR_STABCMN,
213 DT_VERS_1_0, &dt_idops_func, "void(uintptr_t, size_t, void *)" },
214 { "copyout", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUT, DT_ATTR_STABCMN, DT_VERS_1_0,
215 &dt_idops_func, "void(void *, uintptr_t, size_t)" },
216 { "copyoutstr", DT_IDENT_FUNC, 0, DIF_SUBR_COPYOUTSTR,
217 DT_ATTR_STABCMN, DT_VERS_1_0,
218 &dt_idops_func, "void(char *, uintptr_t, size_t)" },
219 { "count", DT_IDENT_AGGFUNC, 0, DTRACEAGG_COUNT, DT_ATTR_STABCMN, DT_VERS_1_0,
220 &dt_idops_func, "void()" },
221 { "curthread", DT_IDENT_SCALAR, 0, DIF_VAR_CURTHREAD,
222 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_PRIVATE,
223 DTRACE_CLASS_COMMON }, DT_VERS_1_0,
224 &dt_idops_type, "genunix`kthread_t *" },
225 { "ddi_pathname", DT_IDENT_FUNC, 0, DIF_SUBR_DDI_PATHNAME,
226 DT_ATTR_EVOLCMN, DT_VERS_1_0,
227 &dt_idops_func, "string(void *, int64_t)" },
228 { "denormalize", DT_IDENT_ACTFUNC, 0, DT_ACT_DENORMALIZE, DT_ATTR_STABCMN,
229 DT_VERS_1_0, &dt_idops_func, "void(...)" },
230 { "dirname", DT_IDENT_FUNC, 0, DIF_SUBR_DIRNAME, DT_ATTR_STABCMN, DT_VERS_1_0,
231 &dt_idops_func, "string(const char *)" },
232 { "discard", DT_IDENT_ACTFUNC, 0, DT_ACT_DISCARD, DT_ATTR_STABCMN, DT_VERS_1_0,
233 &dt_idops_func, "void(int)" },
234 { "epid", DT_IDENT_SCALAR, 0, DIF_VAR_EPID, DT_ATTR_STABCMN, DT_VERS_1_0,
235 &dt_idops_type, "uint_t" },
236 { "errno", DT_IDENT_SCALAR, 0, DIF_VAR_ERRNO, DT_ATTR_STABCMN, DT_VERS_1_0,
237 &dt_idops_type, "int" },
238 { "execname", DT_IDENT_SCALAR, 0, DIF_VAR_EXECNAME,
239 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
240 { "exit", DT_IDENT_ACTFUNC, 0, DT_ACT_EXIT, DT_ATTR_STABCMN, DT_VERS_1_0,
241 &dt_idops_func, "void(int)" },
242 { "freopen", DT_IDENT_ACTFUNC, 0, DT_ACT_FREOPEN, DT_ATTR_STABCMN,
243 DT_VERS_1_1, &dt_idops_func, "void(@, ...)" },
244 { "ftruncate", DT_IDENT_ACTFUNC, 0, DT_ACT_FTRUNCATE, DT_ATTR_STABCMN,
245 DT_VERS_1_0, &dt_idops_func, "void()" },
246 { "func", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
247 DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
248 { "getmajor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMAJOR,
249 DT_ATTR_EVOLCMN, DT_VERS_1_0,
250 &dt_idops_func, "genunix`major_t(genunix`dev_t)" },
251 { "getminor", DT_IDENT_FUNC, 0, DIF_SUBR_GETMINOR,
252 DT_ATTR_EVOLCMN, DT_VERS_1_0,
253 &dt_idops_func, "genunix`minor_t(genunix`dev_t)" },
254 { "htonl", DT_IDENT_FUNC, 0, DIF_SUBR_HTONL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
255 &dt_idops_func, "uint32_t(uint32_t)" },
256 { "htonll", DT_IDENT_FUNC, 0, DIF_SUBR_HTONLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
257 &dt_idops_func, "uint64_t(uint64_t)" },
258 { "htons", DT_IDENT_FUNC, 0, DIF_SUBR_HTONS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
259 &dt_idops_func, "uint16_t(uint16_t)" },
260 { "getf", DT_IDENT_FUNC, 0, DIF_SUBR_GETF, DT_ATTR_STABCMN, DT_VERS_1_10,
261 &dt_idops_func, "file_t *(int)" },
262 { "gid", DT_IDENT_SCALAR, 0, DIF_VAR_GID, DT_ATTR_STABCMN, DT_VERS_1_0,
263 &dt_idops_type, "gid_t" },
264 { "id", DT_IDENT_SCALAR, 0, DIF_VAR_ID, DT_ATTR_STABCMN, DT_VERS_1_0,
265 &dt_idops_type, "uint_t" },
266 { "index", DT_IDENT_FUNC, 0, DIF_SUBR_INDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
267 &dt_idops_func, "int(const char *, const char *, [int])" },
268 { "inet_ntoa", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA, DT_ATTR_STABCMN,
269 DT_VERS_1_5, &dt_idops_func, "string(ipaddr_t *)" },
270 { "inet_ntoa6", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOA6, DT_ATTR_STABCMN,
271 DT_VERS_1_5, &dt_idops_func, "string(in6_addr_t *)" },
272 { "inet_ntop", DT_IDENT_FUNC, 0, DIF_SUBR_INET_NTOP, DT_ATTR_STABCMN,
273 DT_VERS_1_5, &dt_idops_func, "string(int, void *)" },
274 { "ipl", DT_IDENT_SCALAR, 0, DIF_VAR_IPL, DT_ATTR_STABCMN, DT_VERS_1_0,
275 &dt_idops_type, "uint_t" },
276 { "json", DT_IDENT_FUNC, 0, DIF_SUBR_JSON, DT_ATTR_STABCMN, DT_VERS_1_11,
277 &dt_idops_func, "string(const char *, const char *)" },
278 { "jstack", DT_IDENT_ACTFUNC, 0, DT_ACT_JSTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
279 &dt_idops_func, "stack(...)" },
280 { "lltostr", DT_IDENT_FUNC, 0, DIF_SUBR_LLTOSTR, DT_ATTR_STABCMN, DT_VERS_1_0,
281 &dt_idops_func, "string(int64_t, [int])" },
282 { "llquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LLQUANTIZE, DT_ATTR_STABCMN,
283 DT_VERS_1_7, &dt_idops_func,
284 "void(@, int32_t, int32_t, int32_t, int32_t, ...)" },
285 { "lquantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_LQUANTIZE,
286 DT_ATTR_STABCMN, DT_VERS_1_0,
287 &dt_idops_func, "void(@, int32_t, int32_t, ...)" },
288 { "max", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MAX, DT_ATTR_STABCMN, DT_VERS_1_0,
289 &dt_idops_func, "void(@)" },
290 { "min", DT_IDENT_AGGFUNC, 0, DTRACEAGG_MIN, DT_ATTR_STABCMN, DT_VERS_1_0,
291 &dt_idops_func, "void(@)" },
292 { "mod", DT_IDENT_ACTFUNC, 0, DT_ACT_MOD, DT_ATTR_STABCMN,
293 DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
294 { "msgdsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGDSIZE,
295 DT_ATTR_STABCMN, DT_VERS_1_0,
296 &dt_idops_func, "size_t(mblk_t *)" },
297 { "msgsize", DT_IDENT_FUNC, 0, DIF_SUBR_MSGSIZE,
298 DT_ATTR_STABCMN, DT_VERS_1_0,
299 &dt_idops_func, "size_t(mblk_t *)" },
300 { "mutex_owned", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNED,
301 DT_ATTR_EVOLCMN, DT_VERS_1_0,
302 &dt_idops_func, "int(genunix`kmutex_t *)" },
303 { "mutex_owner", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_OWNER,
304 DT_ATTR_EVOLCMN, DT_VERS_1_0,
305 &dt_idops_func, "genunix`kthread_t *(genunix`kmutex_t *)" },
306 { "mutex_type_adaptive", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_ADAPTIVE,
307 DT_ATTR_EVOLCMN, DT_VERS_1_0,
308 &dt_idops_func, "int(genunix`kmutex_t *)" },
309 { "mutex_type_spin", DT_IDENT_FUNC, 0, DIF_SUBR_MUTEX_TYPE_SPIN,
310 DT_ATTR_EVOLCMN, DT_VERS_1_0,
311 &dt_idops_func, "int(genunix`kmutex_t *)" },
312 { "ntohl", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
313 &dt_idops_func, "uint32_t(uint32_t)" },
314 { "ntohll", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHLL, DT_ATTR_EVOLCMN, DT_VERS_1_3,
315 &dt_idops_func, "uint64_t(uint64_t)" },
316 { "ntohs", DT_IDENT_FUNC, 0, DIF_SUBR_NTOHS, DT_ATTR_EVOLCMN, DT_VERS_1_3,
317 &dt_idops_func, "uint16_t(uint16_t)" },
318 { "normalize", DT_IDENT_ACTFUNC, 0, DT_ACT_NORMALIZE, DT_ATTR_STABCMN,
319 DT_VERS_1_0, &dt_idops_func, "void(...)" },
320 { "panic", DT_IDENT_ACTFUNC, 0, DT_ACT_PANIC, DT_ATTR_STABCMN, DT_VERS_1_0,
321 &dt_idops_func, "void()" },
322 { "pid", DT_IDENT_SCALAR, 0, DIF_VAR_PID, DT_ATTR_STABCMN, DT_VERS_1_0,
323 &dt_idops_type, "pid_t" },
324 { "ppid", DT_IDENT_SCALAR, 0, DIF_VAR_PPID, DT_ATTR_STABCMN, DT_VERS_1_0,
325 &dt_idops_type, "pid_t" },
326 { "print", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINT, DT_ATTR_STABCMN, DT_VERS_1_9,
327 &dt_idops_func, "void(@)" },
328 { "printa", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTA, DT_ATTR_STABCMN, DT_VERS_1_0,
329 &dt_idops_func, "void(@, ...)" },
330 { "printf", DT_IDENT_ACTFUNC, 0, DT_ACT_PRINTF, DT_ATTR_STABCMN, DT_VERS_1_0,
331 &dt_idops_func, "void(@, ...)" },
332 { "probefunc", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEFUNC,
333 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
334 { "probemod", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEMOD,
335 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
336 { "probename", DT_IDENT_SCALAR, 0, DIF_VAR_PROBENAME,
337 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
338 { "probeprov", DT_IDENT_SCALAR, 0, DIF_VAR_PROBEPROV,
339 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
340 { "progenyof", DT_IDENT_FUNC, 0, DIF_SUBR_PROGENYOF,
341 DT_ATTR_STABCMN, DT_VERS_1_0,
342 &dt_idops_func, "int(pid_t)" },
343 { "quantize", DT_IDENT_AGGFUNC, 0, DTRACEAGG_QUANTIZE,
344 DT_ATTR_STABCMN, DT_VERS_1_0,
345 &dt_idops_func, "void(@, ...)" },
346 { "raise", DT_IDENT_ACTFUNC, 0, DT_ACT_RAISE, DT_ATTR_STABCMN, DT_VERS_1_0,
347 &dt_idops_func, "void(int)" },
348 { "rand", DT_IDENT_FUNC, 0, DIF_SUBR_RAND, DT_ATTR_STABCMN, DT_VERS_1_0,
349 &dt_idops_func, "int()" },
350 { "rindex", DT_IDENT_FUNC, 0, DIF_SUBR_RINDEX, DT_ATTR_STABCMN, DT_VERS_1_1,
351 &dt_idops_func, "int(const char *, const char *, [int])" },
352 { "rw_iswriter", DT_IDENT_FUNC, 0, DIF_SUBR_RW_ISWRITER,
353 DT_ATTR_EVOLCMN, DT_VERS_1_0,
354 &dt_idops_func, "int(genunix`krwlock_t *)" },
355 { "rw_read_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_READ_HELD,
356 DT_ATTR_EVOLCMN, DT_VERS_1_0,
357 &dt_idops_func, "int(genunix`krwlock_t *)" },
358 { "rw_write_held", DT_IDENT_FUNC, 0, DIF_SUBR_RW_WRITE_HELD,
359 DT_ATTR_EVOLCMN, DT_VERS_1_0,
360 &dt_idops_func, "int(genunix`krwlock_t *)" },
361 { "self", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
362 &dt_idops_type, "void" },
363 { "setopt", DT_IDENT_ACTFUNC, 0, DT_ACT_SETOPT, DT_ATTR_STABCMN,
364 DT_VERS_1_2, &dt_idops_func, "void(const char *, [const char *])" },
365 { "speculate", DT_IDENT_ACTFUNC, 0, DT_ACT_SPECULATE,
366 DT_ATTR_STABCMN, DT_VERS_1_0,
367 &dt_idops_func, "void(int)" },
368 { "speculation", DT_IDENT_FUNC, 0, DIF_SUBR_SPECULATION,
369 DT_ATTR_STABCMN, DT_VERS_1_0,
370 &dt_idops_func, "int()" },
371 { "stack", DT_IDENT_ACTFUNC, 0, DT_ACT_STACK, DT_ATTR_STABCMN, DT_VERS_1_0,
372 &dt_idops_func, "stack(...)" },
373 { "stackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_STACKDEPTH,
374 DT_ATTR_STABCMN, DT_VERS_1_0,
375 &dt_idops_type, "uint32_t" },
376 { "stddev", DT_IDENT_AGGFUNC, 0, DTRACEAGG_STDDEV, DT_ATTR_STABCMN,
377 DT_VERS_1_6, &dt_idops_func, "void(@)" },
378 { "stop", DT_IDENT_ACTFUNC, 0, DT_ACT_STOP, DT_ATTR_STABCMN, DT_VERS_1_0,
379 &dt_idops_func, "void()" },
380 { "strchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
381 &dt_idops_func, "string(const char *, char)" },
382 { "strlen", DT_IDENT_FUNC, 0, DIF_SUBR_STRLEN, DT_ATTR_STABCMN, DT_VERS_1_0,
383 &dt_idops_func, "size_t(const char *)" },
384 { "strjoin", DT_IDENT_FUNC, 0, DIF_SUBR_STRJOIN, DT_ATTR_STABCMN, DT_VERS_1_0,
385 &dt_idops_func, "string(const char *, const char *)" },
386 { "strrchr", DT_IDENT_FUNC, 0, DIF_SUBR_STRRCHR, DT_ATTR_STABCMN, DT_VERS_1_1,
387 &dt_idops_func, "string(const char *, char)" },
388 { "strstr", DT_IDENT_FUNC, 0, DIF_SUBR_STRSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
389 &dt_idops_func, "string(const char *, const char *)" },
390 { "strtok", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOK, DT_ATTR_STABCMN, DT_VERS_1_1,
391 &dt_idops_func, "string(const char *, const char *)" },
392 { "strtoll", DT_IDENT_FUNC, 0, DIF_SUBR_STRTOLL, DT_ATTR_STABCMN, DT_VERS_1_11,
393 &dt_idops_func, "int64_t(const char *, [int])" },
394 { "substr", DT_IDENT_FUNC, 0, DIF_SUBR_SUBSTR, DT_ATTR_STABCMN, DT_VERS_1_1,
395 &dt_idops_func, "string(const char *, int, [int])" },
396 { "sum", DT_IDENT_AGGFUNC, 0, DTRACEAGG_SUM, DT_ATTR_STABCMN, DT_VERS_1_0,
397 &dt_idops_func, "void(@)" },
398 { "sym", DT_IDENT_ACTFUNC, 0, DT_ACT_SYM, DT_ATTR_STABCMN,
399 DT_VERS_1_2, &dt_idops_func, "_symaddr(uintptr_t)" },
400 { "system", DT_IDENT_ACTFUNC, 0, DT_ACT_SYSTEM, DT_ATTR_STABCMN, DT_VERS_1_0,
401 &dt_idops_func, "void(@, ...)" },
402 { "this", DT_IDENT_PTR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0,
403 &dt_idops_type, "void" },
404 { "tid", DT_IDENT_SCALAR, 0, DIF_VAR_TID, DT_ATTR_STABCMN, DT_VERS_1_0,
405 &dt_idops_type, "id_t" },
406 { "timestamp", DT_IDENT_SCALAR, 0, DIF_VAR_TIMESTAMP,
407 DT_ATTR_STABCMN, DT_VERS_1_0,
408 &dt_idops_type, "uint64_t" },
409 { "tolower", DT_IDENT_FUNC, 0, DIF_SUBR_TOLOWER, DT_ATTR_STABCMN, DT_VERS_1_8,
410 &dt_idops_func, "string(const char *)" },
411 { "toupper", DT_IDENT_FUNC, 0, DIF_SUBR_TOUPPER, DT_ATTR_STABCMN, DT_VERS_1_8,
412 &dt_idops_func, "string(const char *)" },
413 { "trace", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACE, DT_ATTR_STABCMN, DT_VERS_1_0,
414 &dt_idops_func, "void(@)" },
415 { "tracemem", DT_IDENT_ACTFUNC, 0, DT_ACT_TRACEMEM,
416 DT_ATTR_STABCMN, DT_VERS_1_0,
417 &dt_idops_func, "void(@, size_t, ...)" },
418 { "trunc", DT_IDENT_ACTFUNC, 0, DT_ACT_TRUNC, DT_ATTR_STABCMN,
419 DT_VERS_1_0, &dt_idops_func, "void(...)" },
420 { "uaddr", DT_IDENT_ACTFUNC, 0, DT_ACT_UADDR, DT_ATTR_STABCMN,
421 DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
422 { "ucaller", DT_IDENT_SCALAR, 0, DIF_VAR_UCALLER, DT_ATTR_STABCMN,
423 DT_VERS_1_2, &dt_idops_type, "uint64_t" },
424 { "ufunc", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
425 DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
426 { "uid", DT_IDENT_SCALAR, 0, DIF_VAR_UID, DT_ATTR_STABCMN, DT_VERS_1_0,
427 &dt_idops_type, "uid_t" },
428 { "umod", DT_IDENT_ACTFUNC, 0, DT_ACT_UMOD, DT_ATTR_STABCMN,
429 DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
430 { "uregs", DT_IDENT_ARRAY, DT_IDFLG_WRITE, DIF_VAR_UREGS, DT_ATTR_STABCMN,
431 DT_VERS_1_0, &dt_idops_regs, NULL },
432 { "ustack", DT_IDENT_ACTFUNC, 0, DT_ACT_USTACK, DT_ATTR_STABCMN, DT_VERS_1_0,
433 &dt_idops_func, "stack(...)" },
434 { "ustackdepth", DT_IDENT_SCALAR, 0, DIF_VAR_USTACKDEPTH,
435 DT_ATTR_STABCMN, DT_VERS_1_2,
436 &dt_idops_type, "uint32_t" },
437 { "usym", DT_IDENT_ACTFUNC, 0, DT_ACT_USYM, DT_ATTR_STABCMN,
438 DT_VERS_1_2, &dt_idops_func, "_usymaddr(uintptr_t)" },
439 { "vmregs", DT_IDENT_ARRAY, 0, DIF_VAR_VMREGS, DT_ATTR_STABCMN, DT_VERS_1_7,
440 &dt_idops_regs, NULL },
441 { "vtimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_VTIMESTAMP,
442 DT_ATTR_STABCMN, DT_VERS_1_0,
443 &dt_idops_type, "uint64_t" },
444 { "walltimestamp", DT_IDENT_SCALAR, 0, DIF_VAR_WALLTIMESTAMP,
445 DT_ATTR_STABCMN, DT_VERS_1_0,
446 &dt_idops_type, "int64_t" },
447 { "zonename", DT_IDENT_SCALAR, 0, DIF_VAR_ZONENAME,
448 DT_ATTR_STABCMN, DT_VERS_1_0, &dt_idops_type, "string" },
449 { NULL, 0, 0, 0, { 0, 0, 0 }, 0, NULL, NULL }
453 * Tables of ILP32 intrinsic integer and floating-point type templates to use
454 * to populate the dynamic "C" CTF type container.
456 static const dt_intrinsic_t _dtrace_intrinsics_32[] = {
457 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
458 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
459 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
460 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
461 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
462 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
463 { "long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
464 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
465 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
466 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
467 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
468 { "signed long", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
469 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
470 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
471 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
472 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
473 { "unsigned long", { 0, 0, 32 }, CTF_K_INTEGER },
474 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
475 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
476 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
477 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
478 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
479 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
480 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
481 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
482 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
483 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
484 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
485 { NULL, { 0, 0, 0 }, 0 }
489 * Tables of LP64 intrinsic integer and floating-point type templates to use
490 * to populate the dynamic "C" CTF type container.
492 static const dt_intrinsic_t _dtrace_intrinsics_64[] = {
493 { "void", { CTF_INT_SIGNED, 0, 0 }, CTF_K_INTEGER },
494 { "signed", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
495 { "unsigned", { 0, 0, 32 }, CTF_K_INTEGER },
496 { "char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
497 { "short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
498 { "int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
499 { "long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
500 { "long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
501 { "signed char", { CTF_INT_SIGNED | CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
502 { "signed short", { CTF_INT_SIGNED, 0, 16 }, CTF_K_INTEGER },
503 { "signed int", { CTF_INT_SIGNED, 0, 32 }, CTF_K_INTEGER },
504 { "signed long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
505 { "signed long long", { CTF_INT_SIGNED, 0, 64 }, CTF_K_INTEGER },
506 { "unsigned char", { CTF_INT_CHAR, 0, 8 }, CTF_K_INTEGER },
507 { "unsigned short", { 0, 0, 16 }, CTF_K_INTEGER },
508 { "unsigned int", { 0, 0, 32 }, CTF_K_INTEGER },
509 { "unsigned long", { 0, 0, 64 }, CTF_K_INTEGER },
510 { "unsigned long long", { 0, 0, 64 }, CTF_K_INTEGER },
511 { "_Bool", { CTF_INT_BOOL, 0, 8 }, CTF_K_INTEGER },
512 { "float", { CTF_FP_SINGLE, 0, 32 }, CTF_K_FLOAT },
513 { "double", { CTF_FP_DOUBLE, 0, 64 }, CTF_K_FLOAT },
514 { "long double", { CTF_FP_LDOUBLE, 0, 128 }, CTF_K_FLOAT },
515 { "float imaginary", { CTF_FP_IMAGRY, 0, 32 }, CTF_K_FLOAT },
516 { "double imaginary", { CTF_FP_DIMAGRY, 0, 64 }, CTF_K_FLOAT },
517 { "long double imaginary", { CTF_FP_LDIMAGRY, 0, 128 }, CTF_K_FLOAT },
518 { "float complex", { CTF_FP_CPLX, 0, 64 }, CTF_K_FLOAT },
519 { "double complex", { CTF_FP_DCPLX, 0, 128 }, CTF_K_FLOAT },
520 { "long double complex", { CTF_FP_LDCPLX, 0, 256 }, CTF_K_FLOAT },
521 { NULL, { 0, 0, 0 }, 0 }
525 * Tables of ILP32 typedefs to use to populate the dynamic "D" CTF container.
526 * These aliases ensure that D definitions can use typical <sys/types.h> names.
528 static const dt_typedef_t _dtrace_typedefs_32[] = {
529 { "char", "int8_t" },
530 { "short", "int16_t" },
531 { "int", "int32_t" },
532 { "long long", "int64_t" },
533 { "int", "intptr_t" },
534 { "int", "ssize_t" },
535 { "unsigned char", "uint8_t" },
536 { "unsigned short", "uint16_t" },
537 { "unsigned", "uint32_t" },
538 { "unsigned long long", "uint64_t" },
539 { "unsigned char", "uchar_t" },
540 { "unsigned short", "ushort_t" },
541 { "unsigned", "uint_t" },
542 { "unsigned long", "ulong_t" },
543 { "unsigned long long", "u_longlong_t" },
544 { "int", "ptrdiff_t" },
545 { "unsigned", "uintptr_t" },
546 { "unsigned", "size_t" },
547 { "long", "id_t" },
548 { "long", "pid_t" },
549 { NULL, NULL }
553 * Tables of LP64 typedefs to use to populate the dynamic "D" CTF container.
554 * These aliases ensure that D definitions can use typical <sys/types.h> names.
556 static const dt_typedef_t _dtrace_typedefs_64[] = {
557 { "char", "int8_t" },
558 { "short", "int16_t" },
559 { "int", "int32_t" },
560 { "long", "int64_t" },
561 { "long", "intptr_t" },
562 { "long", "ssize_t" },
563 { "unsigned char", "uint8_t" },
564 { "unsigned short", "uint16_t" },
565 { "unsigned", "uint32_t" },
566 { "unsigned long", "uint64_t" },
567 { "unsigned char", "uchar_t" },
568 { "unsigned short", "ushort_t" },
569 { "unsigned", "uint_t" },
570 { "unsigned long", "ulong_t" },
571 { "unsigned long long", "u_longlong_t" },
572 { "long", "ptrdiff_t" },
573 { "unsigned long", "uintptr_t" },
574 { "unsigned long", "size_t" },
575 { "int", "id_t" },
576 { "int", "pid_t" },
577 { NULL, NULL }
581 * Tables of ILP32 integer type templates used to populate the dtp->dt_ints[]
582 * cache when a new dtrace client open occurs. Values are set by dtrace_open().
584 static const dt_intdesc_t _dtrace_ints_32[] = {
585 { "int", NULL, CTF_ERR, 0x7fffffffULL },
586 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
587 { "long", NULL, CTF_ERR, 0x7fffffffULL },
588 { "unsigned long", NULL, CTF_ERR, 0xffffffffULL },
589 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
590 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
594 * Tables of LP64 integer type templates used to populate the dtp->dt_ints[]
595 * cache when a new dtrace client open occurs. Values are set by dtrace_open().
597 static const dt_intdesc_t _dtrace_ints_64[] = {
598 { "int", NULL, CTF_ERR, 0x7fffffffULL },
599 { "unsigned int", NULL, CTF_ERR, 0xffffffffULL },
600 { "long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
601 { "unsigned long", NULL, CTF_ERR, 0xffffffffffffffffULL },
602 { "long long", NULL, CTF_ERR, 0x7fffffffffffffffULL },
603 { "unsigned long long", NULL, CTF_ERR, 0xffffffffffffffffULL }
607 * Table of macro variable templates used to populate the macro identifier hash
608 * when a new dtrace client open occurs. Values are set by dtrace_update().
610 static const dt_ident_t _dtrace_macros[] = {
611 { "egid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
612 { "euid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
613 { "gid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
614 { "pid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
615 { "pgid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
616 { "ppid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
617 { "projid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
618 { "sid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
619 { "taskid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
620 { "target", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
621 { "uid", DT_IDENT_SCALAR, 0, 0, DT_ATTR_STABCMN, DT_VERS_1_0 },
622 { NULL, 0, 0, 0, { 0, 0, 0 }, 0 }
626 * Hard-wired definition string to be compiled and cached every time a new
627 * DTrace library handle is initialized. This string should only be used to
628 * contain definitions that should be present regardless of DTRACE_O_NOLIBS.
630 static const char _dtrace_hardwire[] = "\
631 inline long NULL = 0; \n\
632 #pragma D binding \"1.0\" NULL\n\
636 * Default DTrace configuration to use when opening libdtrace DTRACE_O_NODEV.
637 * If DTRACE_O_NODEV is not set, we load the configuration from the kernel.
638 * The use of CTF_MODEL_NATIVE is more subtle than it might appear: we are
639 * relying on the fact that when running dtrace(8), isaexec will invoke the
640 * binary with the same bitness as the kernel, which is what we want by default
641 * when generating our DIF. The user can override the choice using oflags.
643 static const dtrace_conf_t _dtrace_conf = {
644 DIF_VERSION, /* dtc_difversion */
645 DIF_DIR_NREGS, /* dtc_difintregs */
646 DIF_DTR_NREGS, /* dtc_diftupregs */
647 CTF_MODEL_NATIVE /* dtc_ctfmodel */
650 const dtrace_attribute_t _dtrace_maxattr = {
651 DTRACE_STABILITY_MAX,
652 DTRACE_STABILITY_MAX,
653 DTRACE_CLASS_MAX
656 const dtrace_attribute_t _dtrace_defattr = {
657 DTRACE_STABILITY_STABLE,
658 DTRACE_STABILITY_STABLE,
659 DTRACE_CLASS_COMMON
662 const dtrace_attribute_t _dtrace_symattr = {
663 DTRACE_STABILITY_PRIVATE,
664 DTRACE_STABILITY_PRIVATE,
665 DTRACE_CLASS_UNKNOWN
668 const dtrace_attribute_t _dtrace_typattr = {
669 DTRACE_STABILITY_PRIVATE,
670 DTRACE_STABILITY_PRIVATE,
671 DTRACE_CLASS_UNKNOWN
674 const dtrace_attribute_t _dtrace_prvattr = {
675 DTRACE_STABILITY_PRIVATE,
676 DTRACE_STABILITY_PRIVATE,
677 DTRACE_CLASS_UNKNOWN
680 const dtrace_pattr_t _dtrace_prvdesc = {
681 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
682 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
683 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
684 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
685 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_COMMON },
688 const char *_dtrace_defcpp = "/opt/gcc/4.4.4/bin/cpp"; /* default cpp(1) to invoke */
689 const char *_dtrace_defld = "/usr/bin/ld"; /* default ld(1) to invoke */
691 const char *_dtrace_libdir = "/usr/lib/dtrace"; /* default library directory */
692 const char *_dtrace_provdir = "/dev/dtrace/provider"; /* provider directory */
694 int _dtrace_strbuckets = 211; /* default number of hash buckets (prime) */
695 int _dtrace_intbuckets = 256; /* default number of integer buckets (Pof2) */
696 uint_t _dtrace_strsize = 256; /* default size of string intrinsic type */
697 uint_t _dtrace_stkindent = 14; /* default whitespace indent for stack/ustack */
698 uint_t _dtrace_pidbuckets = 64; /* default number of pid hash buckets */
699 uint_t _dtrace_pidlrulim = 8; /* default number of pid handles to cache */
700 size_t _dtrace_bufsize = 512; /* default dt_buf_create() size */
701 int _dtrace_argmax = 32; /* default maximum number of probe arguments */
703 int _dtrace_debug = 0; /* debug messages enabled (off) */
704 const char *const _dtrace_version = DT_VERS_STRING; /* API version string */
705 int _dtrace_rdvers = RD_VERSION; /* rtld_db feature version */
707 typedef struct dt_fdlist {
708 int *df_fds; /* array of provider driver file descriptors */
709 uint_t df_ents; /* number of valid elements in df_fds[] */
710 uint_t df_size; /* size of df_fds[] */
711 } dt_fdlist_t;
713 #pragma init(_dtrace_init)
714 void
715 _dtrace_init(void)
717 _dtrace_debug = getenv("DTRACE_DEBUG") != NULL;
719 for (; _dtrace_rdvers > 0; _dtrace_rdvers--) {
720 if (rd_init(_dtrace_rdvers) == RD_OK)
721 break;
725 static dtrace_hdl_t *
726 set_open_errno(dtrace_hdl_t *dtp, int *errp, int err)
728 if (dtp != NULL)
729 dtrace_close(dtp);
730 if (errp != NULL)
731 *errp = err;
732 return (NULL);
735 static void
736 dt_provmod_open(dt_provmod_t **provmod, dt_fdlist_t *dfp)
738 dt_provmod_t *prov;
739 char path[PATH_MAX];
740 struct dirent *dp, *ep;
741 DIR *dirp;
742 int fd;
744 if ((dirp = opendir(_dtrace_provdir)) == NULL)
745 return; /* failed to open directory; just skip it */
747 ep = alloca(sizeof (struct dirent) + PATH_MAX + 1);
748 bzero(ep, sizeof (struct dirent) + PATH_MAX + 1);
750 while (readdir_r(dirp, ep, &dp) == 0 && dp != NULL) {
751 if (dp->d_name[0] == '.')
752 continue; /* skip "." and ".." */
754 if (dfp->df_ents == dfp->df_size) {
755 uint_t size = dfp->df_size ? dfp->df_size * 2 : 16;
756 int *fds = reallocarray(dfp->df_fds, size,
757 sizeof (int));
759 if (fds == NULL)
760 break; /* skip the rest of this directory */
762 dfp->df_fds = fds;
763 dfp->df_size = size;
766 (void) snprintf(path, sizeof (path), "%s/%s",
767 _dtrace_provdir, dp->d_name);
769 if ((fd = open(path, O_RDONLY)) == -1)
770 continue; /* failed to open driver; just skip it */
772 if (((prov = malloc(sizeof (dt_provmod_t))) == NULL) ||
773 (prov->dp_name = malloc(strlen(dp->d_name) + 1)) == NULL) {
774 free(prov);
775 (void) close(fd);
776 break;
779 (void) strcpy(prov->dp_name, dp->d_name);
780 prov->dp_next = *provmod;
781 *provmod = prov;
783 dt_dprintf("opened provider %s\n", dp->d_name);
784 dfp->df_fds[dfp->df_ents++] = fd;
787 (void) closedir(dirp);
790 static void
791 dt_provmod_destroy(dt_provmod_t **provmod)
793 dt_provmod_t *next, *current;
795 for (current = *provmod; current != NULL; current = next) {
796 next = current->dp_next;
797 free(current->dp_name);
798 free(current);
801 *provmod = NULL;
804 static const char *
805 dt_get_sysinfo(int cmd, char *buf, size_t len)
807 ssize_t rv = sysinfo(cmd, buf, len);
808 char *p = buf;
810 if (rv < 0 || rv > len)
811 (void) snprintf(buf, len, "%s", "Unknown");
813 while ((p = strchr(p, '.')) != NULL)
814 *p++ = '_';
816 return (buf);
819 static dtrace_hdl_t *
820 dt_vopen(int version, int flags, int *errp,
821 const dtrace_vector_t *vector, void *arg)
823 dtrace_hdl_t *dtp = NULL;
824 int dtfd = -1, ftfd = -1, fterr = 0;
825 dtrace_prog_t *pgp;
826 dt_module_t *dmp;
827 dt_provmod_t *provmod = NULL;
828 int i, err;
829 struct rlimit rl;
831 const dt_intrinsic_t *dinp;
832 const dt_typedef_t *dtyp;
833 const dt_ident_t *idp;
835 dtrace_typeinfo_t dtt;
836 ctf_funcinfo_t ctc;
837 ctf_arinfo_t ctr;
839 dt_fdlist_t df = { NULL, 0, 0 };
841 char isadef[32], utsdef[32];
842 char s1[64], s2[64];
844 if (version <= 0)
845 return (set_open_errno(dtp, errp, EINVAL));
847 if (version > DTRACE_VERSION)
848 return (set_open_errno(dtp, errp, EDT_VERSION));
850 if (version < DTRACE_VERSION) {
852 * Currently, increasing the library version number is used to
853 * denote a binary incompatible change. That is, a consumer
854 * of the library cannot run on a version of the library with
855 * a higher DTRACE_VERSION number than the consumer compiled
856 * against. Once the library API has been committed to,
857 * backwards binary compatibility will be required; at that
858 * time, this check should change to return EDT_OVERSION only
859 * if the specified version number is less than the version
860 * number at the time of interface commitment.
862 return (set_open_errno(dtp, errp, EDT_OVERSION));
865 if (flags & ~DTRACE_O_MASK)
866 return (set_open_errno(dtp, errp, EINVAL));
868 if ((flags & DTRACE_O_LP64) && (flags & DTRACE_O_ILP32))
869 return (set_open_errno(dtp, errp, EINVAL));
871 if (vector == NULL && arg != NULL)
872 return (set_open_errno(dtp, errp, EINVAL));
874 if (elf_version(EV_CURRENT) == EV_NONE)
875 return (set_open_errno(dtp, errp, EDT_ELFVERSION));
877 if (vector != NULL || (flags & DTRACE_O_NODEV))
878 goto alloc; /* do not attempt to open dtrace device */
881 * Before we get going, crank our limit on file descriptors up to the
882 * hard limit. This is to allow for the fact that libproc keeps file
883 * descriptors to objects open for the lifetime of the proc handle;
884 * without raising our hard limit, we would have an acceptably small
885 * bound on the number of processes that we could concurrently
886 * instrument with the pid provider.
888 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
889 rl.rlim_cur = rl.rlim_max;
890 (void) setrlimit(RLIMIT_NOFILE, &rl);
894 * Get the device path of each of the providers. We hold them open
895 * in the df.df_fds list until we open the DTrace driver itself,
896 * allowing us to see all of the probes provided on this system. Once
897 * we have the DTrace driver open, we can safely close all the providers
898 * now that they have registered with the framework.
900 dt_provmod_open(&provmod, &df);
902 dtfd = open("/dev/dtrace/dtrace", O_RDWR);
903 err = errno; /* save errno from opening dtfd */
905 ftfd = open("/dev/dtrace/provider/fasttrap", O_RDWR);
906 fterr = ftfd == -1 ? errno : 0; /* save errno from open ftfd */
908 while (df.df_ents-- != 0)
909 (void) close(df.df_fds[df.df_ents]);
911 free(df.df_fds);
914 * If we failed to open the dtrace device, fail dtrace_open().
915 * We convert some kernel errnos to custom libdtrace errnos to
916 * improve the resulting message from the usual strerror().
918 if (dtfd == -1) {
919 dt_provmod_destroy(&provmod);
920 switch (err) {
921 case ENOENT:
922 err = EDT_NOENT;
923 break;
924 case EBUSY:
925 err = EDT_BUSY;
926 break;
927 case EACCES:
928 err = EDT_ACCESS;
929 break;
931 return (set_open_errno(dtp, errp, err));
934 (void) fcntl(dtfd, F_SETFD, FD_CLOEXEC);
935 (void) fcntl(ftfd, F_SETFD, FD_CLOEXEC);
937 alloc:
938 if ((dtp = malloc(sizeof (dtrace_hdl_t))) == NULL)
939 return (set_open_errno(dtp, errp, EDT_NOMEM));
941 bzero(dtp, sizeof (dtrace_hdl_t));
942 dtp->dt_oflags = flags;
943 dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
944 dtp->dt_linkmode = DT_LINK_KERNEL;
945 dtp->dt_linktype = DT_LTYP_ELF;
946 dtp->dt_xlatemode = DT_XL_STATIC;
947 dtp->dt_stdcmode = DT_STDC_XA;
948 dtp->dt_encoding = DT_ENCODING_UNSET;
949 dtp->dt_version = version;
950 dtp->dt_fd = dtfd;
951 dtp->dt_ftfd = ftfd;
952 dtp->dt_fterr = fterr;
953 dtp->dt_cdefs_fd = -1;
954 dtp->dt_ddefs_fd = -1;
955 dtp->dt_stdout_fd = -1;
956 dtp->dt_modbuckets = _dtrace_strbuckets;
957 dtp->dt_mods = calloc(dtp->dt_modbuckets, sizeof (dt_module_t *));
958 dtp->dt_provbuckets = _dtrace_strbuckets;
959 dtp->dt_provs = calloc(dtp->dt_provbuckets, sizeof (dt_provider_t *));
960 dt_proc_init(dtp);
961 dtp->dt_vmax = DT_VERS_LATEST;
962 dtp->dt_cpp_path = strdup(_dtrace_defcpp);
963 dtp->dt_cpp_argv = malloc(sizeof (char *));
964 dtp->dt_cpp_argc = 1;
965 dtp->dt_cpp_args = 1;
966 dtp->dt_ld_path = strdup(_dtrace_defld);
967 dtp->dt_provmod = provmod;
968 dtp->dt_vector = vector;
969 dtp->dt_varg = arg;
970 dt_dof_init(dtp);
971 (void) uname(&dtp->dt_uts);
973 if (dtp->dt_mods == NULL || dtp->dt_provs == NULL ||
974 dtp->dt_procs == NULL || dtp->dt_proc_env == NULL ||
975 dtp->dt_ld_path == NULL || dtp->dt_cpp_path == NULL ||
976 dtp->dt_cpp_argv == NULL)
977 return (set_open_errno(dtp, errp, EDT_NOMEM));
979 for (i = 0; i < DTRACEOPT_MAX; i++)
980 dtp->dt_options[i] = DTRACEOPT_UNSET;
982 dtp->dt_cpp_argv[0] = dtp->dt_cpp_path;
984 (void) snprintf(isadef, sizeof (isadef), "-D__SUNW_D_%u",
985 (uint_t)(sizeof (void *) * NBBY));
987 (void) snprintf(utsdef, sizeof (utsdef), "-D__%s_%s",
988 dt_get_sysinfo(SI_SYSNAME, s1, sizeof (s1)),
989 dt_get_sysinfo(SI_RELEASE, s2, sizeof (s2)));
991 if (dt_cpp_add_arg(dtp, "-undef") == NULL ||
992 dt_cpp_add_arg(dtp, "-D__sun") == NULL ||
993 dt_cpp_add_arg(dtp, "-D__unix") == NULL ||
994 dt_cpp_add_arg(dtp, "-D__SVR4") == NULL ||
995 dt_cpp_add_arg(dtp, "-D__SUNW_D=1") == NULL ||
996 dt_cpp_add_arg(dtp, isadef) == NULL ||
997 dt_cpp_add_arg(dtp, utsdef) == NULL)
998 return (set_open_errno(dtp, errp, EDT_NOMEM));
1000 if (flags & DTRACE_O_NODEV)
1001 bcopy(&_dtrace_conf, &dtp->dt_conf, sizeof (_dtrace_conf));
1002 else if (dt_ioctl(dtp, DTRACEIOC_CONF, &dtp->dt_conf) != 0)
1003 return (set_open_errno(dtp, errp, errno));
1005 if (flags & DTRACE_O_LP64)
1006 dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_LP64;
1007 else if (flags & DTRACE_O_ILP32)
1008 dtp->dt_conf.dtc_ctfmodel = CTF_MODEL_ILP32;
1011 #ifdef __x86
1013 * On x86 systems, __i386 is defined for <sys/isa_defs.h> for 32-bit
1014 * compiles and __amd64 is defined for 64-bit compiles. Unlike SPARC,
1015 * they are defined exclusive of one another (see PSARC 2004/619).
1017 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) {
1018 if (dt_cpp_add_arg(dtp, "-D__amd64") == NULL)
1019 return (set_open_errno(dtp, errp, EDT_NOMEM));
1020 } else {
1021 if (dt_cpp_add_arg(dtp, "-D__i386") == NULL)
1022 return (set_open_errno(dtp, errp, EDT_NOMEM));
1024 #endif
1026 if (dtp->dt_conf.dtc_difversion < DIF_VERSION)
1027 return (set_open_errno(dtp, errp, EDT_DIFVERS));
1029 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32)
1030 bcopy(_dtrace_ints_32, dtp->dt_ints, sizeof (_dtrace_ints_32));
1031 else
1032 bcopy(_dtrace_ints_64, dtp->dt_ints, sizeof (_dtrace_ints_64));
1034 dtp->dt_macros = dt_idhash_create("macro", NULL, 0, UINT_MAX);
1035 dtp->dt_aggs = dt_idhash_create("aggregation", NULL,
1036 DTRACE_AGGVARIDNONE + 1, UINT_MAX);
1038 dtp->dt_globals = dt_idhash_create("global", _dtrace_globals,
1039 DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1041 dtp->dt_tls = dt_idhash_create("thread local", NULL,
1042 DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
1044 if (dtp->dt_macros == NULL || dtp->dt_aggs == NULL ||
1045 dtp->dt_globals == NULL || dtp->dt_tls == NULL)
1046 return (set_open_errno(dtp, errp, EDT_NOMEM));
1049 * Populate the dt_macros identifier hash table by hand: we can't use
1050 * the dt_idhash_populate() mechanism because we're not yet compiling
1051 * and dtrace_update() needs to immediately reference these idents.
1053 for (idp = _dtrace_macros; idp->di_name != NULL; idp++) {
1054 if (dt_idhash_insert(dtp->dt_macros, idp->di_name,
1055 idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
1056 idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
1057 idp->di_iarg, 0) == NULL)
1058 return (set_open_errno(dtp, errp, EDT_NOMEM));
1062 * Update the module list using /system/object and load the values for
1063 * the macro variable definitions according to the current process.
1065 dtrace_update(dtp);
1068 * Select the intrinsics and typedefs we want based on the data model.
1069 * The intrinsics are under "C". The typedefs are added under "D".
1071 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_ILP32) {
1072 dinp = _dtrace_intrinsics_32;
1073 dtyp = _dtrace_typedefs_32;
1074 } else {
1075 dinp = _dtrace_intrinsics_64;
1076 dtyp = _dtrace_typedefs_64;
1080 * Create a dynamic CTF container under the "C" scope for intrinsic
1081 * types and types defined in ANSI-C header files that are included.
1083 if ((dmp = dtp->dt_cdefs = dt_module_create(dtp, "C")) == NULL)
1084 return (set_open_errno(dtp, errp, EDT_NOMEM));
1086 if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1087 return (set_open_errno(dtp, errp, EDT_CTF));
1089 dt_dprintf("created CTF container for %s (%p)\n",
1090 dmp->dm_name, (void *)dmp->dm_ctfp);
1092 (void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1093 ctf_setspecific(dmp->dm_ctfp, dmp);
1095 dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1096 dmp->dm_modid = -1; /* no module ID */
1099 * Fill the dynamic "C" CTF container with all of the intrinsic
1100 * integer and floating-point types appropriate for this data model.
1102 for (; dinp->din_name != NULL; dinp++) {
1103 if (dinp->din_kind == CTF_K_INTEGER) {
1104 err = ctf_add_integer(dmp->dm_ctfp, CTF_ADD_ROOT,
1105 dinp->din_name, &dinp->din_data);
1106 } else {
1107 err = ctf_add_float(dmp->dm_ctfp, CTF_ADD_ROOT,
1108 dinp->din_name, &dinp->din_data);
1111 if (err == CTF_ERR) {
1112 dt_dprintf("failed to add %s to C container: %s\n",
1113 dinp->din_name, ctf_errmsg(
1114 ctf_errno(dmp->dm_ctfp)));
1115 return (set_open_errno(dtp, errp, EDT_CTF));
1119 if (ctf_update(dmp->dm_ctfp) != 0) {
1120 dt_dprintf("failed to update C container: %s\n",
1121 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1122 return (set_open_errno(dtp, errp, EDT_CTF));
1126 * Add intrinsic pointer types that are needed to initialize printf
1127 * format dictionary types (see table in dt_printf.c).
1129 (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL,
1130 ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1132 (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL,
1133 ctf_lookup_by_name(dmp->dm_ctfp, "char"));
1135 (void) ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL,
1136 ctf_lookup_by_name(dmp->dm_ctfp, "int"));
1138 if (ctf_update(dmp->dm_ctfp) != 0) {
1139 dt_dprintf("failed to update C container: %s\n",
1140 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1141 return (set_open_errno(dtp, errp, EDT_CTF));
1145 * Create a dynamic CTF container under the "D" scope for types that
1146 * are defined by the D program itself or on-the-fly by the D compiler.
1147 * The "D" CTF container is a child of the "C" CTF container.
1149 if ((dmp = dtp->dt_ddefs = dt_module_create(dtp, "D")) == NULL)
1150 return (set_open_errno(dtp, errp, EDT_NOMEM));
1152 if ((dmp->dm_ctfp = ctf_create(&dtp->dt_ctferr)) == NULL)
1153 return (set_open_errno(dtp, errp, EDT_CTF));
1155 dt_dprintf("created CTF container for %s (%p)\n",
1156 dmp->dm_name, (void *)dmp->dm_ctfp);
1158 (void) ctf_setmodel(dmp->dm_ctfp, dtp->dt_conf.dtc_ctfmodel);
1159 ctf_setspecific(dmp->dm_ctfp, dmp);
1161 dmp->dm_flags = DT_DM_LOADED; /* fake up loaded bit */
1162 dmp->dm_modid = -1; /* no module ID */
1164 if (ctf_import(dmp->dm_ctfp, dtp->dt_cdefs->dm_ctfp) == CTF_ERR) {
1165 dt_dprintf("failed to import D parent container: %s\n",
1166 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1167 return (set_open_errno(dtp, errp, EDT_CTF));
1171 * Fill the dynamic "D" CTF container with all of the built-in typedefs
1172 * that we need to use for our D variable and function definitions.
1173 * This ensures that basic inttypes.h names are always available to us.
1175 for (; dtyp->dty_src != NULL; dtyp++) {
1176 if (ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1177 dtyp->dty_dst, ctf_lookup_by_name(dmp->dm_ctfp,
1178 dtyp->dty_src)) == CTF_ERR) {
1179 dt_dprintf("failed to add typedef %s %s to D "
1180 "container: %s", dtyp->dty_src, dtyp->dty_dst,
1181 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1182 return (set_open_errno(dtp, errp, EDT_CTF));
1187 * Insert a CTF ID corresponding to a pointer to a type of kind
1188 * CTF_K_FUNCTION we can use in the compiler for function pointers.
1189 * CTF treats all function pointers as "int (*)()" so we only need one.
1191 ctc.ctc_return = ctf_lookup_by_name(dmp->dm_ctfp, "int");
1192 ctc.ctc_argc = 0;
1193 ctc.ctc_flags = 0;
1195 dtp->dt_type_func = ctf_add_funcptr(dmp->dm_ctfp,
1196 CTF_ADD_ROOT, &ctc, NULL);
1198 dtp->dt_type_fptr = ctf_add_pointer(dmp->dm_ctfp, CTF_ADD_ROOT, NULL,
1199 dtp->dt_type_func);
1202 * We also insert CTF definitions for the special D intrinsic types
1203 * string and <DYN> into the D container. The string type is added
1204 * as a typedef of char[n]. The <DYN> type is an alias for void.
1205 * We compare types to these special CTF ids throughout the compiler.
1207 ctr.ctr_contents = ctf_lookup_by_name(dmp->dm_ctfp, "char");
1208 ctr.ctr_index = ctf_lookup_by_name(dmp->dm_ctfp, "long");
1209 ctr.ctr_nelems = _dtrace_strsize;
1211 dtp->dt_type_str = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1212 "string", ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &ctr));
1214 dtp->dt_type_dyn = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1215 "<DYN>", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1217 dtp->dt_type_stack = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1218 "stack", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1220 dtp->dt_type_symaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1221 "_symaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1223 dtp->dt_type_usymaddr = ctf_add_typedef(dmp->dm_ctfp, CTF_ADD_ROOT,
1224 "_usymaddr", ctf_lookup_by_name(dmp->dm_ctfp, "void"));
1226 if (dtp->dt_type_func == CTF_ERR || dtp->dt_type_fptr == CTF_ERR ||
1227 dtp->dt_type_str == CTF_ERR || dtp->dt_type_dyn == CTF_ERR ||
1228 dtp->dt_type_stack == CTF_ERR || dtp->dt_type_symaddr == CTF_ERR ||
1229 dtp->dt_type_usymaddr == CTF_ERR) {
1230 dt_dprintf("failed to add intrinsic to D container: %s\n",
1231 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1232 return (set_open_errno(dtp, errp, EDT_CTF));
1235 if (ctf_update(dmp->dm_ctfp) != 0) {
1236 dt_dprintf("failed update D container: %s\n",
1237 ctf_errmsg(ctf_errno(dmp->dm_ctfp)));
1238 return (set_open_errno(dtp, errp, EDT_CTF));
1242 * Initialize the integer description table used to convert integer
1243 * constants to the appropriate types. Refer to the comments above
1244 * dt_node_int() for a complete description of how this table is used.
1246 for (i = 0; i < sizeof (dtp->dt_ints) / sizeof (dtp->dt_ints[0]); i++) {
1247 if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY,
1248 dtp->dt_ints[i].did_name, &dtt) != 0) {
1249 dt_dprintf("failed to lookup integer type %s: %s\n",
1250 dtp->dt_ints[i].did_name,
1251 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1252 return (set_open_errno(dtp, errp, dtp->dt_errno));
1254 dtp->dt_ints[i].did_ctfp = dtt.dtt_ctfp;
1255 dtp->dt_ints[i].did_type = dtt.dtt_type;
1259 * Now that we've created the "C" and "D" containers, move them to the
1260 * start of the module list so that these types and symbols are found
1261 * first (for stability) when iterating through the module list.
1263 dt_list_delete(&dtp->dt_modlist, dtp->dt_ddefs);
1264 dt_list_prepend(&dtp->dt_modlist, dtp->dt_ddefs);
1266 dt_list_delete(&dtp->dt_modlist, dtp->dt_cdefs);
1267 dt_list_prepend(&dtp->dt_modlist, dtp->dt_cdefs);
1269 if (dt_pfdict_create(dtp) == -1)
1270 return (set_open_errno(dtp, errp, dtp->dt_errno));
1273 * If we are opening libdtrace DTRACE_O_NODEV enable C_ZDEFS by default
1274 * because without /dev/dtrace open, we will not be able to load the
1275 * names and attributes of any providers or probes from the kernel.
1277 if (flags & DTRACE_O_NODEV)
1278 dtp->dt_cflags |= DTRACE_C_ZDEFS;
1281 * Load hard-wired inlines into the definition cache by calling the
1282 * compiler on the raw definition string defined above.
1284 if ((pgp = dtrace_program_strcompile(dtp, _dtrace_hardwire,
1285 DTRACE_PROBESPEC_NONE, DTRACE_C_EMPTY, 0, NULL)) == NULL) {
1286 dt_dprintf("failed to load hard-wired definitions: %s\n",
1287 dtrace_errmsg(dtp, dtrace_errno(dtp)));
1288 return (set_open_errno(dtp, errp, EDT_HARDWIRE));
1291 dt_program_destroy(dtp, pgp);
1294 * Set up the default DTrace library path. Once set, the next call to
1295 * dt_compile() will compile all the libraries. We intentionally defer
1296 * library processing to improve overhead for clients that don't ever
1297 * compile, and to provide better error reporting (because the full
1298 * reporting of compiler errors requires dtrace_open() to succeed).
1300 if (dtrace_setopt(dtp, "libdir", _dtrace_libdir) != 0)
1301 return (set_open_errno(dtp, errp, dtp->dt_errno));
1303 return (dtp);
1306 dtrace_hdl_t *
1307 dtrace_open(int version, int flags, int *errp)
1309 return (dt_vopen(version, flags, errp, NULL, NULL));
1312 dtrace_hdl_t *
1313 dtrace_vopen(int version, int flags, int *errp,
1314 const dtrace_vector_t *vector, void *arg)
1316 return (dt_vopen(version, flags, errp, vector, arg));
1319 void
1320 dtrace_close(dtrace_hdl_t *dtp)
1322 dt_ident_t *idp, *ndp;
1323 dt_module_t *dmp;
1324 dt_provider_t *pvp;
1325 dtrace_prog_t *pgp;
1326 dt_xlator_t *dxp;
1327 dt_dirpath_t *dirp;
1328 int i;
1330 if (dtp->dt_procs != NULL)
1331 dt_proc_fini(dtp);
1333 while ((pgp = dt_list_next(&dtp->dt_programs)) != NULL)
1334 dt_program_destroy(dtp, pgp);
1336 while ((dxp = dt_list_next(&dtp->dt_xlators)) != NULL)
1337 dt_xlator_destroy(dtp, dxp);
1339 dt_free(dtp, dtp->dt_xlatormap);
1341 for (idp = dtp->dt_externs; idp != NULL; idp = ndp) {
1342 ndp = idp->di_next;
1343 dt_ident_destroy(idp);
1346 if (dtp->dt_macros != NULL)
1347 dt_idhash_destroy(dtp->dt_macros);
1348 if (dtp->dt_aggs != NULL)
1349 dt_idhash_destroy(dtp->dt_aggs);
1350 if (dtp->dt_globals != NULL)
1351 dt_idhash_destroy(dtp->dt_globals);
1352 if (dtp->dt_tls != NULL)
1353 dt_idhash_destroy(dtp->dt_tls);
1355 while ((dmp = dt_list_next(&dtp->dt_modlist)) != NULL)
1356 dt_module_destroy(dtp, dmp);
1358 while ((pvp = dt_list_next(&dtp->dt_provlist)) != NULL)
1359 dt_provider_destroy(dtp, pvp);
1361 if (dtp->dt_fd != -1)
1362 (void) close(dtp->dt_fd);
1363 if (dtp->dt_ftfd != -1)
1364 (void) close(dtp->dt_ftfd);
1365 if (dtp->dt_cdefs_fd != -1)
1366 (void) close(dtp->dt_cdefs_fd);
1367 if (dtp->dt_ddefs_fd != -1)
1368 (void) close(dtp->dt_ddefs_fd);
1369 if (dtp->dt_stdout_fd != -1)
1370 (void) close(dtp->dt_stdout_fd);
1372 dt_epid_destroy(dtp);
1373 dt_aggid_destroy(dtp);
1374 dt_format_destroy(dtp);
1375 dt_strdata_destroy(dtp);
1376 dt_buffered_destroy(dtp);
1377 dt_aggregate_destroy(dtp);
1378 dt_pfdict_destroy(dtp);
1379 dt_provmod_destroy(&dtp->dt_provmod);
1380 dt_dof_fini(dtp);
1382 for (i = 1; i < dtp->dt_cpp_argc; i++)
1383 free(dtp->dt_cpp_argv[i]);
1385 while ((dirp = dt_list_next(&dtp->dt_lib_path)) != NULL) {
1386 dt_list_delete(&dtp->dt_lib_path, dirp);
1387 free(dirp->dir_path);
1388 free(dirp);
1391 free(dtp->dt_cpp_argv);
1392 free(dtp->dt_cpp_path);
1393 free(dtp->dt_ld_path);
1395 free(dtp->dt_mods);
1396 free(dtp->dt_provs);
1397 free(dtp);
1401 dtrace_provider_modules(dtrace_hdl_t *dtp, const char **mods, int nmods)
1403 dt_provmod_t *prov;
1404 int i = 0;
1406 for (prov = dtp->dt_provmod; prov != NULL; prov = prov->dp_next, i++) {
1407 if (i < nmods)
1408 mods[i] = prov->dp_name;
1411 return (i);
1415 dtrace_ctlfd(dtrace_hdl_t *dtp)
1417 return (dtp->dt_fd);