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