xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_impl.h (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
30  * Copyright 2022 Oxide Computer Company
31  */
32 
33 #ifndef	_DT_IMPL_H
34 #define	_DT_IMPL_H
35 
36 #include <sys/param.h>
37 #include <sys/objfs.h>
38 #include <setjmp.h>
39 #include <libctf.h>
40 #include <dtrace.h>
41 #include <gelf.h>
42 #include <synch.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 #include <dt_parser.h>
49 #include <dt_regset.h>
50 #include <dt_inttab.h>
51 #include <dt_strtab.h>
52 #include <dt_ident.h>
53 #include <dt_list.h>
54 #include <dt_decl.h>
55 #include <dt_as.h>
56 #include <dt_proc.h>
57 #include <dt_dof.h>
58 #include <dt_pcb.h>
59 #include <dt_pq.h>
60 
61 struct dt_module;		/* see below */
62 struct dt_pfdict;		/* see <dt_printf.h> */
63 struct dt_arg;			/* see below */
64 struct dt_provider;		/* see <dt_provider.h> */
65 struct dt_xlator;		/* see <dt_xlator.h> */
66 
67 typedef struct dt_intrinsic {
68 	const char *din_name;	/* string name of the intrinsic type */
69 	ctf_encoding_t din_data; /* integer or floating-point CTF encoding */
70 	uint_t din_kind;	/* CTF type kind to instantiate */
71 } dt_intrinsic_t;
72 
73 typedef struct dt_typedef {
74 	const char *dty_src;	/* string name of typedef source type */
75 	const char *dty_dst;	/* string name of typedef destination type */
76 } dt_typedef_t;
77 
78 typedef struct dt_intdesc {
79 	const char *did_name;	/* string name of the integer type */
80 	ctf_file_t *did_ctfp;	/* CTF container for this type reference */
81 	ctf_id_t did_type;	/* CTF type reference for this type */
82 	uintmax_t did_limit;	/* maximum positive value held by type */
83 } dt_intdesc_t;
84 
85 typedef struct dt_modops {
86 	uint_t (*do_syminit)(struct dt_module *);
87 	void (*do_symsort)(struct dt_module *);
88 	GElf_Sym *(*do_symname)(struct dt_module *,
89 	    const char *, GElf_Sym *, uint_t *);
90 	GElf_Sym *(*do_symaddr)(struct dt_module *,
91 	    GElf_Addr, GElf_Sym *, uint_t *);
92 } dt_modops_t;
93 
94 typedef struct dt_arg {
95 	int da_ndx;		/* index of this argument */
96 	int da_mapping;		/* mapping of argument indices to arguments */
97 	ctf_id_t da_type;	/* type of argument */
98 	ctf_file_t *da_ctfp;	/* CTF container for type */
99 	dt_ident_t *da_xlator;	/* translator, if any */
100 	struct dt_arg *da_next;	/* next argument */
101 } dt_arg_t;
102 
103 typedef struct dt_sym {
104 	uint_t ds_symid;	/* id of corresponding symbol */
105 	uint_t ds_next;		/* index of next element in hash chain */
106 } dt_sym_t;
107 
108 typedef struct dt_module {
109 	dt_list_t dm_list;	/* list forward/back pointers */
110 	char dm_name[DTRACE_MODNAMELEN]; /* string name of module */
111 	char dm_file[MAXPATHLEN]; /* file path of module (if any) */
112 	struct dt_module *dm_next; /* pointer to next module in hash chain */
113 	const dt_modops_t *dm_ops; /* pointer to data model's ops vector */
114 	Elf *dm_elf;		/* libelf handle for module object */
115 	objfs_info_t dm_info;	/* object filesystem private info */
116 	ctf_sect_t dm_symtab;	/* symbol table for module */
117 	ctf_sect_t dm_strtab;	/* string table for module */
118 	ctf_sect_t dm_ctdata;	/* CTF data for module */
119 	ctf_file_t *dm_ctfp;	/* CTF container handle */
120 	uint_t *dm_symbuckets;	/* symbol table hash buckets (chain indices) */
121 	dt_sym_t *dm_symchains;	/* symbol table hash chains buffer */
122 	void *dm_asmap;		/* symbol pointers sorted by value */
123 	uint_t dm_symfree;	/* index of next free hash element */
124 	uint_t dm_nsymbuckets;	/* number of elements in bucket array */
125 	uint_t dm_nsymelems;	/* number of elements in hash table */
126 	uint_t dm_asrsv;	/* actual reserved size of dm_asmap */
127 	uint_t dm_aslen;	/* number of entries in dm_asmap */
128 	uint_t dm_flags;	/* module flags (see below) */
129 	int dm_modid;		/* modinfo(8) module identifier */
130 	GElf_Addr dm_text_va;	/* virtual address of text section */
131 	GElf_Xword dm_text_size; /* size in bytes of text section */
132 	GElf_Addr dm_data_va;	/* virtual address of data section */
133 	GElf_Xword dm_data_size; /* size in bytes of data section */
134 	GElf_Addr dm_bss_va;	/* virtual address of BSS */
135 	GElf_Xword dm_bss_size;	/* size in bytes of BSS */
136 	dt_idhash_t *dm_extern;	/* external symbol definitions */
137 	pid_t dm_pid;		/* pid for this module */
138 	uint_t dm_nctflibs;	/* number of ctf children libraries */
139 	ctf_file_t **dm_libctfp; /* process library ctf pointers */
140 	char **dm_libctfn;	/* names of process ctf containers */
141 } dt_module_t;
142 
143 #define	DT_DM_LOADED	0x1	/* module symbol and type data is loaded */
144 #define	DT_DM_KERNEL	0x2	/* module is associated with a kernel object */
145 #define	DT_DM_PRIMARY	0x4	/* module is a krtld primary kernel object */
146 
147 typedef struct dt_provmod {
148 	char *dp_name;				/* name of provider module */
149 	struct dt_provmod *dp_next;		/* next module */
150 } dt_provmod_t;
151 
152 typedef struct dt_ahashent {
153 	struct dt_ahashent *dtahe_prev;		/* prev on hash chain */
154 	struct dt_ahashent *dtahe_next;		/* next on hash chain */
155 	struct dt_ahashent *dtahe_prevall;	/* prev on list of all */
156 	struct dt_ahashent *dtahe_nextall;	/* next on list of all */
157 	uint64_t dtahe_hashval;			/* hash value */
158 	size_t dtahe_size;			/* size of data */
159 	dtrace_aggdata_t dtahe_data;		/* data */
160 	void (*dtahe_aggregate)(int64_t *, int64_t *, size_t); /* function */
161 } dt_ahashent_t;
162 
163 typedef struct dt_ahash {
164 	dt_ahashent_t	**dtah_hash;		/* hash table */
165 	dt_ahashent_t	*dtah_all;		/* list of all elements */
166 	size_t		dtah_size;		/* size of hash table */
167 } dt_ahash_t;
168 
169 typedef struct dt_aggregate {
170 	dtrace_bufdesc_t dtat_buf;	/* buf aggregation snapshot */
171 	int dtat_flags;			/* aggregate flags */
172 	processorid_t dtat_ncpus;	/* number of CPUs in aggregate */
173 	processorid_t *dtat_cpus;	/* CPUs in aggregate */
174 	processorid_t dtat_ncpu;	/* size of dtat_cpus array */
175 	processorid_t dtat_maxcpu;	/* maximum number of CPUs */
176 	dt_ahash_t dtat_hash;		/* aggregate hash table */
177 } dt_aggregate_t;
178 
179 typedef struct dt_print_aggdata {
180 	dtrace_hdl_t *dtpa_dtp;		/* pointer to libdtrace handle */
181 	dtrace_aggvarid_t dtpa_id;	/* aggregation variable of interest */
182 	FILE *dtpa_fp;			/* file pointer */
183 	int dtpa_allunprint;		/* print only unprinted aggregations */
184 	int dtpa_agghist;		/* print aggregation as histogram */
185 	int dtpa_agghisthdr;		/* aggregation histogram hdr printed */
186 	int dtpa_aggpack;		/* pack quantized aggregations */
187 } dt_print_aggdata_t;
188 
189 typedef struct dt_dirpath {
190 	dt_list_t dir_list;		/* linked-list forward/back pointers */
191 	char *dir_path;			/* directory pathname */
192 } dt_dirpath_t;
193 
194 typedef struct dt_lib_depend {
195 	dt_list_t dtld_deplist;		/* linked-list forward/back pointers */
196 	char *dtld_library;		/* library name */
197 	char *dtld_libpath;		/* library pathname */
198 	uint_t dtld_finish;		/* completion time in tsort for lib */
199 	uint_t dtld_start;		/* starting time in tsort for lib */
200 	uint_t dtld_loaded;		/* boolean: is this library loaded */
201 	dt_list_t dtld_dependencies;	/* linked-list of lib dependencies */
202 	dt_list_t dtld_dependents;	/* linked-list of lib dependents */
203 } dt_lib_depend_t;
204 
205 typedef uint32_t dt_version_t;		/* encoded version (see below) */
206 
207 struct dtrace_hdl {
208 	const dtrace_vector_t *dt_vector; /* library vector, if vectored open */
209 	void *dt_varg;	/* vector argument, if vectored open */
210 	dtrace_conf_t dt_conf;	/* DTrace driver configuration profile */
211 	char dt_errmsg[BUFSIZ];	/* buffer for formatted syntax error msgs */
212 	const char *dt_errtag;	/* tag used with last call to dt_set_errmsg() */
213 	dt_pcb_t *dt_pcb;	/* pointer to current parsing control block */
214 	ulong_t dt_gen;		/* compiler generation number */
215 	dt_list_t dt_programs;	/* linked list of dtrace_prog_t's */
216 	dt_list_t dt_xlators;	/* linked list of dt_xlator_t's */
217 	struct dt_xlator **dt_xlatormap; /* dt_xlator_t's indexed by dx_id */
218 	id_t dt_xlatorid;	/* next dt_xlator_t id to assign */
219 	dt_ident_t *dt_externs;	/* linked list of external symbol identifiers */
220 	dt_idhash_t *dt_macros;	/* hash table of macro variable identifiers */
221 	dt_idhash_t *dt_aggs;	/* hash table of aggregation identifiers */
222 	dt_idhash_t *dt_globals; /* hash table of global identifiers */
223 	dt_idhash_t *dt_tls;	/* hash table of thread-local identifiers */
224 	dt_list_t dt_modlist;	/* linked list of dt_module_t's */
225 	dt_module_t **dt_mods;	/* hash table of dt_module_t's */
226 	uint_t dt_modbuckets;	/* number of module hash buckets */
227 	uint_t dt_nmods;	/* number of modules in hash and list */
228 	dt_provmod_t *dt_provmod; /* linked list of provider modules */
229 	dt_module_t *dt_exec;	/* pointer to executable module */
230 	dt_module_t *dt_rtld;	/* pointer to run-time linker module */
231 	dt_module_t *dt_cdefs;	/* pointer to C dynamic type module */
232 	dt_module_t *dt_ddefs;	/* pointer to D dynamic type module */
233 	dt_list_t dt_provlist;	/* linked list of dt_provider_t's */
234 	struct dt_provider **dt_provs; /* hash table of dt_provider_t's */
235 	uint_t dt_provbuckets;	/* number of provider hash buckets */
236 	uint_t dt_nprovs;	/* number of providers in hash and list */
237 	dt_proc_hash_t *dt_procs; /* hash table of grabbed process handles */
238 	char **dt_proc_env;	/* additional environment variables */
239 	dt_intdesc_t dt_ints[6]; /* cached integer type descriptions */
240 	ctf_id_t dt_type_func;	/* cached CTF identifier for function type */
241 	ctf_id_t dt_type_fptr;	/* cached CTF identifier for function pointer */
242 	ctf_id_t dt_type_str;	/* cached CTF identifier for string type */
243 	ctf_id_t dt_type_dyn;	/* cached CTF identifier for <DYN> type */
244 	ctf_id_t dt_type_stack;	/* cached CTF identifier for stack type */
245 	ctf_id_t dt_type_symaddr; /* cached CTF identifier for _symaddr type */
246 	ctf_id_t dt_type_usymaddr; /* cached CTF ident. for _usymaddr type */
247 	size_t dt_maxprobe;	/* max enabled probe ID */
248 	dtrace_eprobedesc_t **dt_edesc; /* enabled probe descriptions */
249 	dtrace_probedesc_t **dt_pdesc; /* probe descriptions for enabled prbs */
250 	size_t dt_maxagg;	/* max aggregation ID */
251 	dtrace_aggdesc_t **dt_aggdesc; /* aggregation descriptions */
252 	int dt_maxformat;	/* max format ID */
253 	void **dt_formats;	/* pointer to format array */
254 	int dt_maxstrdata;	/* max strdata ID */
255 	char **dt_strdata;	/* pointer to strdata array */
256 	dt_aggregate_t dt_aggregate; /* aggregate */
257 	dt_pq_t *dt_bufq;	/* CPU-specific data queue */
258 	struct dt_pfdict *dt_pfdict; /* dictionary of printf conversions */
259 	dt_version_t dt_vmax;	/* optional ceiling on program API binding */
260 	dtrace_attribute_t dt_amin; /* optional floor on program attributes */
261 	char *dt_cpp_path;	/* pathname of cpp(1) to invoke if needed */
262 	char **dt_cpp_argv;	/* argument vector for exec'ing cpp(1) */
263 	int dt_cpp_argc;	/* count of initialized cpp(1) arguments */
264 	int dt_cpp_args;	/* size of dt_cpp_argv[] array */
265 	char *dt_ld_path;	/* pathname of ld(1) to invoke if needed */
266 	dt_list_t dt_lib_path;	/* linked-list forming library search path */
267 	uint_t dt_lazyload;	/* boolean:  set via -xlazyload */
268 	uint_t dt_droptags;	/* boolean:  set via -xdroptags */
269 	uint_t dt_active;	/* boolean:  set once tracing is active */
270 	uint_t dt_stopped;	/* boolean:  set once tracing is stopped */
271 	processorid_t dt_beganon; /* CPU that executed BEGIN probe (if any) */
272 	processorid_t dt_endedon; /* CPU that executed END probe (if any) */
273 	uint_t dt_oflags;	/* dtrace open-time options (see dtrace.h) */
274 	uint_t dt_cflags;	/* dtrace compile-time options (see dtrace.h) */
275 	uint_t dt_dflags;	/* dtrace link-time options (see dtrace.h) */
276 	uint_t dt_prcmode;	/* dtrace process create mode (see dt_proc.h) */
277 	uint_t dt_linkmode;	/* dtrace symbol linking mode (see below) */
278 	uint_t dt_linktype;	/* dtrace link output file type (see below) */
279 	uint_t dt_xlatemode;	/* dtrace translator linking mode (see below) */
280 	uint_t dt_stdcmode;	/* dtrace stdc compatibility mode (see below) */
281 	uint_t dt_encoding;	/* dtrace output encoding (see below) */
282 	uint_t dt_treedump;	/* dtrace tree debug bitmap (see below) */
283 	uint64_t dt_options[DTRACEOPT_MAX]; /* dtrace run-time options */
284 	int dt_version;		/* library version requested by client */
285 	int dt_ctferr;		/* error resulting from last CTF failure */
286 	int dt_errno;		/* error resulting from last failed operation */
287 	int dt_fd;		/* file descriptor for dtrace pseudo-device */
288 	int dt_ftfd;		/* file descriptor for fasttrap pseudo-device */
289 	int dt_fterr;		/* saved errno from failed open of dt_ftfd */
290 	int dt_cdefs_fd;	/* file descriptor for C CTF debugging cache */
291 	int dt_ddefs_fd;	/* file descriptor for D CTF debugging cache */
292 	int dt_stdout_fd;	/* file descriptor for saved stdout */
293 	dtrace_handle_err_f *dt_errhdlr; /* error handler, if any */
294 	void *dt_errarg;	/* error handler argument */
295 	dtrace_prog_t *dt_errprog; /* error handler program, if any */
296 	dtrace_handle_drop_f *dt_drophdlr; /* drop handler, if any */
297 	void *dt_droparg;	/* drop handler argument */
298 	dtrace_handle_proc_f *dt_prochdlr; /* proc handler, if any */
299 	void *dt_procarg;	/* proc handler argument */
300 	dtrace_handle_setopt_f *dt_setopthdlr; /* setopt handler, if any */
301 	void *dt_setoptarg;	/* setopt handler argument */
302 	dtrace_status_t dt_status[2]; /* status cache */
303 	int dt_statusgen;	/* current status generation */
304 	hrtime_t dt_laststatus;	/* last status */
305 	hrtime_t dt_lastswitch;	/* last switch of buffer data */
306 	hrtime_t dt_lastagg;	/* last snapshot of aggregation data */
307 	char *dt_sprintf_buf;	/* buffer for dtrace_sprintf() */
308 	int dt_sprintf_buflen;	/* length of dtrace_sprintf() buffer */
309 	const char *dt_filetag;	/* default filetag for dt_set_errmsg() */
310 	char *dt_buffered_buf;	/* buffer for buffered output */
311 	size_t dt_buffered_offs; /* current offset into buffered buffer */
312 	size_t dt_buffered_size; /* size of buffered buffer */
313 	dtrace_handle_buffered_f *dt_bufhdlr; /* buffered handler, if any */
314 	void *dt_bufarg;	/* buffered handler argument */
315 	dt_dof_t dt_dof;	/* DOF generation buffers (see dt_dof.c) */
316 	struct utsname dt_uts;	/* uname(2) information for system */
317 	dt_list_t dt_lib_dep;	/* scratch linked-list of lib dependencies */
318 	dt_list_t dt_lib_dep_sorted;	/* dependency sorted library list */
319 	dtrace_flowkind_t dt_flow;	/* flow kind */
320 	const char *dt_prefix;	/* recommended flow prefix */
321 	int dt_indent;		/* recommended flow indent */
322 	dtrace_epid_t dt_last_epid;	/* most recently consumed EPID */
323 	uint64_t dt_last_timestamp;	/* most recently consumed timestamp */
324 	boolean_t dt_has_sugar;	/* syntactic sugar used? */
325 };
326 
327 /*
328  * Values for the user arg of the ECB.
329  */
330 #define	DT_ECB_DEFAULT		0
331 #define	DT_ECB_ERROR		1
332 
333 /*
334  * Values for the dt_linkmode property, which is used by the assembler when
335  * processing external symbol references.  User can set using -xlink=<mode>.
336  */
337 #define	DT_LINK_KERNEL	0	/* kernel syms static, user syms dynamic */
338 #define	DT_LINK_PRIMARY	1	/* primary kernel syms static, others dynamic */
339 #define	DT_LINK_DYNAMIC	2	/* all symbols dynamic */
340 #define	DT_LINK_STATIC	3	/* all symbols static */
341 
342 /*
343  * Values for the dt_linktype property, which is used by dtrace_program_link()
344  * to determine the type of output file that is desired by the client.
345  */
346 #define	DT_LTYP_ELF	0	/* produce ELF containing DOF */
347 #define	DT_LTYP_DOF	1	/* produce stand-alone DOF */
348 
349 /*
350  * Values for the dt_xlatemode property, which is used to determine whether
351  * references to dynamic translators are permitted.  Set using -xlate=<mode>.
352  */
353 #define	DT_XL_STATIC	0	/* require xlators to be statically defined */
354 #define	DT_XL_DYNAMIC	1	/* produce references to dynamic translators */
355 
356 /*
357  * Values for the dt_stdcmode property, which is used by the compiler when
358  * running cpp to determine the presence and setting of the __STDC__ macro.
359  */
360 #define	DT_STDC_XA	0	/* ISO C + K&R C compat w/o ISO: __STDC__=0 */
361 #define	DT_STDC_XC	1	/* Strict ISO C: __STDC__=1 */
362 #define	DT_STDC_XS	2	/* K&R C: __STDC__ not defined */
363 #define	DT_STDC_XT	3	/* ISO C + K&R C compat with ISO: __STDC__=0 */
364 
365 /*
366  * Values for the dt_encoding property, which is used to force a particular
367  * character encoding (overriding default behavior and/or automatic detection).
368  */
369 #define	DT_ENCODING_UNSET	0
370 #define	DT_ENCODING_ASCII	1
371 #define	DT_ENCODING_UTF8	2
372 
373 /*
374  * Macro to test whether a given pass bit is set in the dt_treedump bit-vector.
375  * If the bit for pass 'p' is set, the D compiler displays the parse tree for
376  * the program by printing it to stderr at the end of compiler pass 'p'.
377  */
378 #define	DT_TREEDUMP_PASS(dtp, p)	((dtp)->dt_treedump & (1 << ((p) - 1)))
379 
380 /*
381  * Macros for accessing the cached CTF container and type ID for the common
382  * types "int", "string", and <DYN>, which we need to use frequently in the D
383  * compiler.  The DT_INT_* macro relies upon "int" being at index 0 in the
384  * _dtrace_ints_* tables in dt_open.c; the others are also set up there.
385  */
386 #define	DT_INT_CTFP(dtp)	((dtp)->dt_ints[0].did_ctfp)
387 #define	DT_INT_TYPE(dtp)	((dtp)->dt_ints[0].did_type)
388 
389 #define	DT_FUNC_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
390 #define	DT_FUNC_TYPE(dtp)	((dtp)->dt_type_func)
391 
392 #define	DT_FPTR_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
393 #define	DT_FPTR_TYPE(dtp)	((dtp)->dt_type_fptr)
394 
395 #define	DT_STR_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
396 #define	DT_STR_TYPE(dtp)	((dtp)->dt_type_str)
397 
398 #define	DT_DYN_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
399 #define	DT_DYN_TYPE(dtp)	((dtp)->dt_type_dyn)
400 
401 #define	DT_STACK_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
402 #define	DT_STACK_TYPE(dtp)	((dtp)->dt_type_stack)
403 
404 #define	DT_SYMADDR_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
405 #define	DT_SYMADDR_TYPE(dtp)	((dtp)->dt_type_symaddr)
406 
407 #define	DT_USYMADDR_CTFP(dtp)	((dtp)->dt_ddefs->dm_ctfp)
408 #define	DT_USYMADDR_TYPE(dtp)	((dtp)->dt_type_usymaddr)
409 
410 /*
411  * Actions and subroutines are both DT_NODE_FUNC nodes; to avoid confusing
412  * an action for a subroutine (or vice versa), we assure that the DT_ACT_*
413  * constants and the DIF_SUBR_* constants occupy non-overlapping ranges by
414  * starting the DT_ACT_* constants at DIF_SUBR_MAX + 1.
415  */
416 #define	DT_ACT_BASE		DIF_SUBR_MAX + 1
417 #define	DT_ACT(n)		(DT_ACT_BASE + (n))
418 
419 #define	DT_ACT_PRINTF		DT_ACT(0)	/* printf() action */
420 #define	DT_ACT_TRACE		DT_ACT(1)	/* trace() action */
421 #define	DT_ACT_TRACEMEM		DT_ACT(2)	/* tracemem() action */
422 #define	DT_ACT_STACK		DT_ACT(3)	/* stack() action */
423 #define	DT_ACT_STOP		DT_ACT(4)	/* stop() action */
424 #define	DT_ACT_BREAKPOINT	DT_ACT(5)	/* breakpoint() action */
425 #define	DT_ACT_PANIC		DT_ACT(6)	/* panic() action */
426 #define	DT_ACT_SPECULATE	DT_ACT(7)	/* speculate() action */
427 #define	DT_ACT_COMMIT		DT_ACT(8)	/* commit() action */
428 #define	DT_ACT_DISCARD		DT_ACT(9)	/* discard() action */
429 #define	DT_ACT_CHILL		DT_ACT(10)	/* chill() action */
430 #define	DT_ACT_EXIT		DT_ACT(11)	/* exit() action */
431 #define	DT_ACT_USTACK		DT_ACT(12)	/* ustack() action */
432 #define	DT_ACT_PRINTA		DT_ACT(13)	/* printa() action */
433 #define	DT_ACT_RAISE		DT_ACT(14)	/* raise() action */
434 #define	DT_ACT_CLEAR		DT_ACT(15)	/* clear() action */
435 #define	DT_ACT_NORMALIZE	DT_ACT(16)	/* normalize() action */
436 #define	DT_ACT_DENORMALIZE	DT_ACT(17)	/* denormalize() action */
437 #define	DT_ACT_TRUNC		DT_ACT(18)	/* trunc() action */
438 #define	DT_ACT_SYSTEM		DT_ACT(19)	/* system() action */
439 #define	DT_ACT_JSTACK		DT_ACT(20)	/* jstack() action */
440 #define	DT_ACT_FTRUNCATE	DT_ACT(21)	/* ftruncate() action */
441 #define	DT_ACT_FREOPEN		DT_ACT(22)	/* freopen() action */
442 #define	DT_ACT_SYM		DT_ACT(23)	/* sym()/func() actions */
443 #define	DT_ACT_MOD		DT_ACT(24)	/* mod() action */
444 #define	DT_ACT_USYM		DT_ACT(25)	/* usym()/ufunc() actions */
445 #define	DT_ACT_UMOD		DT_ACT(26)	/* umod() action */
446 #define	DT_ACT_UADDR		DT_ACT(27)	/* uaddr() action */
447 #define	DT_ACT_SETOPT		DT_ACT(28)	/* setopt() action */
448 #define	DT_ACT_PRINT		DT_ACT(29)	/* print() action */
449 
450 /*
451  * Sentinel to tell freopen() to restore the saved stdout.  This must not
452  * be ever valid for opening for write access via freopen(3C), which of
453  * course, "." never is.
454  */
455 #define	DT_FREOPEN_RESTORE	"."
456 
457 #define	EDT_BASE	1000	/* base value for libdtrace errnos */
458 
459 enum {
460 	EDT_VERSION = EDT_BASE,	/* client is requesting unsupported version */
461 	EDT_VERSINVAL,		/* version string is invalid or overflows */
462 	EDT_VERSUNDEF,		/* requested API version is not defined */
463 	EDT_VERSREDUCED,	/* requested API version has been reduced */
464 	EDT_CTF,		/* libctf called failed (dt_ctferr has more) */
465 	EDT_COMPILER,		/* error in D program compilation */
466 	EDT_NOTUPREG,		/* tuple register allocation failure */
467 	EDT_NOMEM,		/* memory allocation failure */
468 	EDT_INT2BIG,		/* integer limit exceeded */
469 	EDT_STR2BIG,		/* string limit exceeded */
470 	EDT_NOMOD,		/* unknown module name */
471 	EDT_NOPROV,		/* unknown provider name */
472 	EDT_NOPROBE,		/* unknown probe name */
473 	EDT_NOSYM,		/* unknown symbol name */
474 	EDT_NOSYMADDR,		/* no symbol corresponds to address */
475 	EDT_NOTYPE,		/* unknown type name */
476 	EDT_NOVAR,		/* unknown variable name */
477 	EDT_NOAGG,		/* unknown aggregation name */
478 	EDT_BADSCOPE,		/* improper use of type name scoping operator */
479 	EDT_BADSPEC,		/* overspecified probe description */
480 	EDT_BADSPCV,		/* bad macro variable in probe description */
481 	EDT_BADID,		/* invalid probe identifier */
482 	EDT_NOTLOADED,		/* module is not currently loaded */
483 	EDT_NOCTF,		/* module does not contain any CTF data */
484 	EDT_DATAMODEL,		/* module and program data models don't match */
485 	EDT_DIFVERS,		/* library has newer DIF version than driver */
486 	EDT_BADAGG,		/* unrecognized aggregating action */
487 	EDT_FIO,		/* file i/o error */
488 	EDT_DIFINVAL,		/* invalid DIF program */
489 	EDT_DIFSIZE,		/* invalid DIF size */
490 	EDT_DIFFAULT,		/* failed to copyin DIF program */
491 	EDT_BADPROBE,		/* bad probe description */
492 	EDT_BADPGLOB,		/* bad probe description globbing pattern */
493 	EDT_NOSCOPE,		/* declaration scope stack underflow */
494 	EDT_NODECL,		/* declaration stack underflow */
495 	EDT_DMISMATCH,		/* record list does not match statement */
496 	EDT_DOFFSET,		/* record data offset error */
497 	EDT_DALIGN,		/* record data alignment error */
498 	EDT_BADOPTNAME,		/* invalid dtrace_setopt option name */
499 	EDT_BADOPTVAL,		/* invalid dtrace_setopt option value */
500 	EDT_BADOPTCTX,		/* invalid dtrace_setopt option context */
501 	EDT_CPPFORK,		/* failed to fork preprocessor */
502 	EDT_CPPEXEC,		/* failed to exec preprocessor */
503 	EDT_CPPENT,		/* preprocessor not found */
504 	EDT_CPPERR,		/* unknown preprocessor error */
505 	EDT_SYMOFLOW,		/* external symbol table overflow */
506 	EDT_ACTIVE,		/* operation illegal when tracing is active */
507 	EDT_DESTRUCTIVE,	/* destructive actions not allowed */
508 	EDT_NOANON,		/* no anonymous tracing state */
509 	EDT_ISANON,		/* can't claim anon state and enable probes */
510 	EDT_ENDTOOBIG,		/* END enablings exceed size of prncpl buffer */
511 	EDT_NOCONV,		/* failed to load type for printf conversion */
512 	EDT_BADCONV,		/* incomplete printf conversion */
513 	EDT_BADERROR,		/* invalid library ERROR action */
514 	EDT_ERRABORT,		/* abort due to error */
515 	EDT_DROPABORT,		/* abort due to drop */
516 	EDT_DIRABORT,		/* abort explicitly directed */
517 	EDT_BADRVAL,		/* invalid return value from callback */
518 	EDT_BADNORMAL,		/* invalid normalization */
519 	EDT_BUFTOOSMALL,	/* enabling exceeds size of buffer */
520 	EDT_BADTRUNC,		/* invalid truncation */
521 	EDT_BUSY,		/* device busy (active kernel debugger) */
522 	EDT_ACCESS,		/* insufficient privileges to use DTrace */
523 	EDT_NOENT,		/* dtrace device not available */
524 	EDT_BRICKED,		/* abort due to systemic unresponsiveness */
525 	EDT_HARDWIRE,		/* failed to load hard-wired definitions */
526 	EDT_ELFVERSION,		/* libelf is out-of-date w.r.t libdtrace */
527 	EDT_NOBUFFERED,		/* attempt to buffer output without handler */
528 	EDT_UNSTABLE,		/* description matched unstable set of probes */
529 	EDT_BADSETOPT,		/* invalid setopt library action */
530 	EDT_BADSTACKPC,		/* invalid stack program counter size */
531 	EDT_BADAGGVAR,		/* invalid aggregation variable identifier */
532 	EDT_OVERSION,		/* client is requesting deprecated version */
533 	EDT_ENABLING_ERR,	/* failed to enable probe */
534 	EDT_NOPROBES,		/* no probes sites for declared provider */
535 	EDT_CANTLOAD		/* failed to load a module */
536 };
537 
538 /*
539  * Interfaces for parsing and comparing DTrace attribute tuples, which describe
540  * stability and architectural binding information.  The dtrace_attribute_t
541  * structure and associated constant definitions are found in <sys/dtrace.h>.
542  */
543 extern dtrace_attribute_t dt_attr_min(dtrace_attribute_t, dtrace_attribute_t);
544 extern dtrace_attribute_t dt_attr_max(dtrace_attribute_t, dtrace_attribute_t);
545 extern char *dt_attr_str(dtrace_attribute_t, char *, size_t);
546 extern int dt_attr_cmp(dtrace_attribute_t, dtrace_attribute_t);
547 
548 /*
549  * Interfaces for parsing and handling DTrace version strings.  Version binding
550  * is a feature of the D compiler that is handled completely independently of
551  * the DTrace kernel infrastructure, so the definitions are here in libdtrace.
552  * Version strings are compiled into an encoded uint32_t which can be compared
553  * using C comparison operators.  Version definitions are found in dt_open.c.
554  */
555 #define	DT_VERSION_STRMAX	16	/* enough for "255.4095.4095\0" */
556 #define	DT_VERSION_MAJMAX	0xFF	/* maximum major version number */
557 #define	DT_VERSION_MINMAX	0xFFF	/* maximum minor version number */
558 #define	DT_VERSION_MICMAX	0xFFF	/* maximum micro version number */
559 
560 #define	DT_VERSION_NUMBER(M, m, u) \
561 	((((M) & 0xFF) << 24) | (((m) & 0xFFF) << 12) | ((u) & 0xFFF))
562 
563 #define	DT_VERSION_MAJOR(v)	(((v) & 0xFF000000) >> 24)
564 #define	DT_VERSION_MINOR(v)	(((v) & 0x00FFF000) >> 12)
565 #define	DT_VERSION_MICRO(v)	((v) & 0x00000FFF)
566 
567 extern char *dt_version_num2str(dt_version_t, char *, size_t);
568 extern int dt_version_str2num(const char *, dt_version_t *);
569 extern int dt_version_defined(dt_version_t);
570 
571 /*
572  * Miscellaneous internal libdtrace interfaces.  The definitions below are for
573  * libdtrace routines that do not yet merit their own separate header file.
574  */
575 extern char *dt_cpp_add_arg(dtrace_hdl_t *, const char *);
576 extern char *dt_cpp_pop_arg(dtrace_hdl_t *);
577 
578 extern int dt_set_errno(dtrace_hdl_t *, int);
579 extern void dt_set_errmsg(dtrace_hdl_t *, const char *, const char *,
580     const char *, int, const char *, va_list);
581 
582 extern int dt_ioctl(dtrace_hdl_t *, int, void *);
583 extern int dt_status(dtrace_hdl_t *, processorid_t);
584 extern long dt_sysconf(dtrace_hdl_t *, int);
585 extern ssize_t dt_write(dtrace_hdl_t *, int, const void *, size_t);
586 extern int dt_printf(dtrace_hdl_t *, FILE *, const char *, ...);
587 
588 extern void *dt_zalloc(dtrace_hdl_t *, size_t);
589 extern void *dt_alloc(dtrace_hdl_t *, size_t);
590 extern void dt_free(dtrace_hdl_t *, void *);
591 extern void dt_difo_free(dtrace_hdl_t *, dtrace_difo_t *);
592 
593 extern int dt_gmatch(const char *, const char *);
594 extern char *dt_basename(char *);
595 
596 extern ulong_t dt_popc(ulong_t);
597 extern ulong_t dt_popcb(const ulong_t *, ulong_t);
598 
599 extern int dt_buffered_enable(dtrace_hdl_t *);
600 extern int dt_buffered_flush(dtrace_hdl_t *, dtrace_probedata_t *,
601     const dtrace_recdesc_t *, const dtrace_aggdata_t *, uint32_t flags);
602 extern void dt_buffered_disable(dtrace_hdl_t *);
603 extern void dt_buffered_destroy(dtrace_hdl_t *);
604 
605 extern uint64_t dt_stddev(uint64_t *, uint64_t);
606 
607 extern int dt_options_load(dtrace_hdl_t *);
608 
609 extern void dt_dprintf(const char *, ...);
610 
611 extern void dt_setcontext(dtrace_hdl_t *, dtrace_probedesc_t *);
612 extern void dt_endcontext(dtrace_hdl_t *);
613 
614 extern void dt_pragma(dt_node_t *);
615 extern int dt_reduce(dtrace_hdl_t *, dt_version_t);
616 extern void dt_cg(dt_pcb_t *, dt_node_t *);
617 extern dtrace_difo_t *dt_as(dt_pcb_t *);
618 extern void dt_dis(const dtrace_difo_t *, FILE *);
619 
620 extern int dt_aggregate_go(dtrace_hdl_t *);
621 extern int dt_aggregate_init(dtrace_hdl_t *);
622 extern void dt_aggregate_destroy(dtrace_hdl_t *);
623 
624 extern int dt_epid_lookup(dtrace_hdl_t *, dtrace_epid_t,
625     dtrace_eprobedesc_t **, dtrace_probedesc_t **);
626 extern void dt_epid_destroy(dtrace_hdl_t *);
627 extern int dt_aggid_lookup(dtrace_hdl_t *, dtrace_aggid_t, dtrace_aggdesc_t **);
628 extern void dt_aggid_destroy(dtrace_hdl_t *);
629 
630 extern void *dt_format_lookup(dtrace_hdl_t *, int);
631 extern void dt_format_destroy(dtrace_hdl_t *);
632 
633 extern const char *dt_strdata_lookup(dtrace_hdl_t *, int);
634 extern void dt_strdata_destroy(dtrace_hdl_t *);
635 
636 extern int dt_print_quantize(dtrace_hdl_t *, FILE *,
637     const void *, size_t, uint64_t);
638 extern int dt_print_lquantize(dtrace_hdl_t *, FILE *,
639     const void *, size_t, uint64_t);
640 extern int dt_print_llquantize(dtrace_hdl_t *, FILE *,
641     const void *, size_t, uint64_t);
642 extern int dt_print_agg(const dtrace_aggdata_t *, void *);
643 
644 extern int dt_handle(dtrace_hdl_t *, dtrace_probedata_t *);
645 extern int dt_handle_liberr(dtrace_hdl_t *,
646     const dtrace_probedata_t *, const char *);
647 extern int dt_handle_cpudrop(dtrace_hdl_t *, processorid_t,
648     dtrace_dropkind_t, uint64_t);
649 extern int dt_handle_status(dtrace_hdl_t *,
650     dtrace_status_t *, dtrace_status_t *);
651 extern int dt_handle_setopt(dtrace_hdl_t *, dtrace_setoptdata_t *);
652 
653 extern int dt_lib_depend_add(dtrace_hdl_t *, dt_list_t *, const char *);
654 extern dt_lib_depend_t *dt_lib_depend_lookup(dt_list_t *, const char *);
655 
656 extern boolean_t dt_is_bitfield(const ctf_encoding_t *, ulong_t);
657 
658 extern dt_pcb_t *yypcb;		/* pointer to current parser control block */
659 extern char yyintprefix;	/* int token prefix for macros (+/-) */
660 extern char yyintsuffix[4];	/* int token suffix ([uUlL]*) */
661 extern int yyintdecimal;	/* int token is decimal (1) or octal/hex (0) */
662 extern char yytext[];		/* lex input buffer */
663 extern int yylineno;		/* lex line number */
664 extern int yydebug;		/* lex debugging */
665 extern dt_node_t *yypragma;	/* lex token list for control lines */
666 
667 extern const dtrace_attribute_t _dtrace_maxattr; /* maximum attributes */
668 extern const dtrace_attribute_t _dtrace_defattr; /* default attributes */
669 extern const dtrace_attribute_t _dtrace_symattr; /* symbol ref attributes */
670 extern const dtrace_attribute_t _dtrace_typattr; /* type ref attributes */
671 extern const dtrace_attribute_t _dtrace_prvattr; /* provider attributes */
672 extern const dtrace_pattr_t _dtrace_prvdesc;	 /* provider attribute bundle */
673 
674 extern const dt_version_t _dtrace_versions[];	 /* array of valid versions */
675 extern const char *const _dtrace_version;	 /* current version string */
676 
677 extern int _dtrace_strbuckets;		/* number of hash buckets for strings */
678 extern int _dtrace_intbuckets;		/* number of hash buckets for ints */
679 extern uint_t _dtrace_stkindent;	/* default indent for stack/ustack */
680 extern uint_t _dtrace_pidbuckets;	/* number of hash buckets for pids */
681 extern uint_t _dtrace_pidlrulim;	/* number of proc handles to cache */
682 extern int _dtrace_debug;		/* debugging messages enabled */
683 extern size_t _dtrace_bufsize;		/* default dt_buf_create() size */
684 extern int _dtrace_argmax;		/* default maximum probe arguments */
685 
686 extern const char *_dtrace_libdir;	/* default library directory */
687 extern const char *_dtrace_moddir;	/* default kernel module directory */
688 
689 #ifdef	__cplusplus
690 }
691 #endif
692 
693 #endif	/* _DT_IMPL_H */
694