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