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