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