10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/resource.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <signal.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <alloca.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <dt_impl.h>
420Sstevel@tonic-gate #include <dt_string.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static int
450Sstevel@tonic-gate dt_opt_agg(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if (arg != NULL)
500Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	agp->dtat_flags |= option;
530Sstevel@tonic-gate 	return (0);
540Sstevel@tonic-gate }
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*ARGSUSED*/
570Sstevel@tonic-gate static int
580Sstevel@tonic-gate dt_opt_amin(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	char str[DTRACE_ATTR2STR_MAX];
610Sstevel@tonic-gate 	dtrace_attribute_t attr;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (arg == NULL || dtrace_str2attr(arg, &attr) == -1)
640Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	dt_dprintf("set compiler attribute minimum to %s\n",
670Sstevel@tonic-gate 	    dtrace_attr2str(attr, str, sizeof (str)));
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL) {
700Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags |= DTRACE_C_EATTR;
710Sstevel@tonic-gate 		dtp->dt_pcb->pcb_amin = attr;
720Sstevel@tonic-gate 	} else {
730Sstevel@tonic-gate 		dtp->dt_cflags |= DTRACE_C_EATTR;
740Sstevel@tonic-gate 		dtp->dt_amin = attr;
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	return (0);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static void
810Sstevel@tonic-gate dt_coredump(void)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	const char msg[] = "libdtrace DEBUG: [ forcing coredump ]\n";
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	struct sigaction act;
860Sstevel@tonic-gate 	struct rlimit lim;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	(void) write(STDERR_FILENO, msg, sizeof (msg) - 1);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
910Sstevel@tonic-gate 	act.sa_flags = 0;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
940Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	lim.rlim_cur = RLIM_INFINITY;
970Sstevel@tonic-gate 	lim.rlim_max = RLIM_INFINITY;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_CORE, &lim);
1000Sstevel@tonic-gate 	abort();
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*ARGSUSED*/
1040Sstevel@tonic-gate static int
1050Sstevel@tonic-gate dt_opt_core(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	static int enabled = 0;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (arg != NULL)
1100Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (enabled++ || atexit(dt_coredump) == 0)
1130Sstevel@tonic-gate 		return (0);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (dt_set_errno(dtp, errno));
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*ARGSUSED*/
1190Sstevel@tonic-gate static int
1200Sstevel@tonic-gate dt_opt_cpp_hdrs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	if (arg != NULL)
1230Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
1260Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, "-H") == NULL)
1290Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	return (0);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*ARGSUSED*/
1350Sstevel@tonic-gate static int
1360Sstevel@tonic-gate dt_opt_cpp_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	char *cpp;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (arg == NULL)
1410Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
1440Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if ((cpp = strdup(arg)) == NULL)
1470Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	dtp->dt_cpp_argv[0] = (char *)strbasename(cpp);
1500Sstevel@tonic-gate 	free(dtp->dt_cpp_path);
1510Sstevel@tonic-gate 	dtp->dt_cpp_path = cpp;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	return (0);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static int
1570Sstevel@tonic-gate dt_opt_cpp_opts(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	char *buf;
1600Sstevel@tonic-gate 	size_t len;
1610Sstevel@tonic-gate 	const char *opt = (const char *)option;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (opt == NULL || arg == NULL)
1640Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
1670Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	len = strlen(opt) + strlen(arg) + 1;
1700Sstevel@tonic-gate 	buf = alloca(len);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	(void) strcpy(buf, opt);
1730Sstevel@tonic-gate 	(void) strcat(buf, arg);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, buf) == NULL)
1760Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	return (0);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*ARGSUSED*/
1820Sstevel@tonic-gate static int
1830Sstevel@tonic-gate dt_opt_ctypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	int fd;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (arg == NULL)
1880Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
1910Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	(void) close(dtp->dt_cdefs_fd);
1940Sstevel@tonic-gate 	dtp->dt_cdefs_fd = fd;
1950Sstevel@tonic-gate 	return (0);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*ARGSUSED*/
1990Sstevel@tonic-gate static int
2000Sstevel@tonic-gate dt_opt_dtypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	int fd;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (arg == NULL)
2050Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
2080Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	(void) close(dtp->dt_ddefs_fd);
2110Sstevel@tonic-gate 	dtp->dt_ddefs_fd = fd;
2120Sstevel@tonic-gate 	return (0);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*ARGSUSED*/
2160Sstevel@tonic-gate static int
2170Sstevel@tonic-gate dt_opt_debug(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	if (arg != NULL)
2200Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	_dtrace_debug = 1;
2230Sstevel@tonic-gate 	return (0);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*ARGSUSED*/
2270Sstevel@tonic-gate static int
2280Sstevel@tonic-gate dt_opt_iregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	int n;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) <= 0)
2330Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	dtp->dt_conf.dtc_difintregs = n;
2360Sstevel@tonic-gate 	return (0);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*ARGSUSED*/
2400Sstevel@tonic-gate static int
2410Sstevel@tonic-gate dt_opt_lazyload(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	dtp->dt_lazyload = 1;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*ARGSUSED*/
2490Sstevel@tonic-gate static int
2500Sstevel@tonic-gate dt_opt_ld_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	char *ld;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (arg == NULL)
2550Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
2580Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if ((ld = strdup(arg)) == NULL)
2610Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	free(dtp->dt_ld_path);
2640Sstevel@tonic-gate 	dtp->dt_ld_path = ld;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	return (0);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*ARGSUSED*/
2700Sstevel@tonic-gate static int
2710Sstevel@tonic-gate dt_opt_libdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	dt_dirpath_t *dp;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (arg == NULL)
2760Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if ((dp = malloc(sizeof (dt_dirpath_t))) == NULL ||
2790Sstevel@tonic-gate 	    (dp->dir_path = strdup(arg)) == NULL) {
2800Sstevel@tonic-gate 		free(dp);
2810Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	dt_list_append(&dtp->dt_lib_path, dp);
2850Sstevel@tonic-gate 	return (0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*ARGSUSED*/
2890Sstevel@tonic-gate static int
2900Sstevel@tonic-gate dt_opt_linkmode(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	if (arg == NULL)
2930Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (strcmp(arg, "kernel") == 0)
2960Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_KERNEL;
2970Sstevel@tonic-gate 	else if (strcmp(arg, "primary") == 0)
2980Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_PRIMARY;
2990Sstevel@tonic-gate 	else if (strcmp(arg, "dynamic") == 0)
3000Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_DYNAMIC;
3010Sstevel@tonic-gate 	else if (strcmp(arg, "static") == 0)
3020Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_STATIC;
3030Sstevel@tonic-gate 	else
3040Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	return (0);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*ARGSUSED*/
3100Sstevel@tonic-gate static int
3110Sstevel@tonic-gate dt_opt_linktype(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	if (arg == NULL)
3140Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (strcasecmp(arg, "elf") == 0)
3170Sstevel@tonic-gate 		dtp->dt_linktype = DT_LTYP_ELF;
3180Sstevel@tonic-gate 	else if (strcasecmp(arg, "dof") == 0)
3190Sstevel@tonic-gate 		dtp->dt_linktype = DT_LTYP_DOF;
3200Sstevel@tonic-gate 	else
3210Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return (0);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*ARGSUSED*/
3270Sstevel@tonic-gate static int
3280Sstevel@tonic-gate dt_opt_evaltime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	if (arg == NULL)
3310Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (strcmp(arg, "exec") == 0)
3340Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_CREATE;
3350Sstevel@tonic-gate 	else if (strcmp(arg, "preinit") == 0)
3360Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
3370Sstevel@tonic-gate 	else if (strcmp(arg, "postinit") == 0)
3380Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_POSTINIT;
3390Sstevel@tonic-gate 	else if (strcmp(arg, "main") == 0)
3400Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_MAIN;
3410Sstevel@tonic-gate 	else
3420Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (0);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*ARGSUSED*/
3480Sstevel@tonic-gate static int
3490Sstevel@tonic-gate dt_opt_pgmax(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	int n;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) < 0)
3540Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	dtp->dt_procs->dph_lrulim = n;
3570Sstevel@tonic-gate 	return (0);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*ARGSUSED*/
3610Sstevel@tonic-gate static int
3620Sstevel@tonic-gate dt_opt_stdc(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	if (arg == NULL)
3650Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
3680Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (strcmp(arg, "a") == 0)
3710Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XA;
3720Sstevel@tonic-gate 	else if (strcmp(arg, "c") == 0)
3730Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XC;
3740Sstevel@tonic-gate 	else if (strcmp(arg, "s") == 0)
3750Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XS;
3760Sstevel@tonic-gate 	else if (strcmp(arg, "t") == 0)
3770Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XT;
3780Sstevel@tonic-gate 	else
3790Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	return (0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*ARGSUSED*/
3850Sstevel@tonic-gate static int
3860Sstevel@tonic-gate dt_opt_syslibdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	dt_dirpath_t *dp = dt_list_next(&dtp->dt_lib_path);
3890Sstevel@tonic-gate 	char *path;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (arg == NULL)
3920Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if ((path = strdup(arg)) == NULL)
3950Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	free(dp->dir_path);
3980Sstevel@tonic-gate 	dp->dir_path = path;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	return (0);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*ARGSUSED*/
4050Sstevel@tonic-gate static int
4060Sstevel@tonic-gate dt_opt_tree(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	int m;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (arg == NULL || (m = atoi(arg)) <= 0)
4110Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	dtp->dt_treedump = m;
4140Sstevel@tonic-gate 	return (0);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*ARGSUSED*/
4180Sstevel@tonic-gate static int
4190Sstevel@tonic-gate dt_opt_tregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	int n;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) <= 0)
4240Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	dtp->dt_conf.dtc_diftupregs = n;
4270Sstevel@tonic-gate 	return (0);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static int
4310Sstevel@tonic-gate dt_opt_cflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	if (arg != NULL)
4340Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
4370Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags |= option;
4380Sstevel@tonic-gate 	else
4390Sstevel@tonic-gate 		dtp->dt_cflags |= option;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	return (0);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate static int
4450Sstevel@tonic-gate dt_opt_dflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	if (arg != NULL)
4480Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	dtp->dt_dflags |= option;
4510Sstevel@tonic-gate 	return (0);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate static int
4550Sstevel@tonic-gate dt_opt_invcflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	if (arg != NULL)
4580Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
4610Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags &= ~option;
4620Sstevel@tonic-gate 	else
4630Sstevel@tonic-gate 		dtp->dt_cflags &= ~option;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	return (0);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*ARGSUSED*/
4690Sstevel@tonic-gate static int
4700Sstevel@tonic-gate dt_opt_version(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	dt_version_t v;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (arg == NULL)
4750Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (dt_version_str2num(arg, &v) == -1)
4780Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSINVAL));
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (!dt_version_defined(v))
4810Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSUNDEF));
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	return (dt_reduce(dtp, v));
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate static int
4870Sstevel@tonic-gate dt_opt_runtime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	char *end;
4900Sstevel@tonic-gate 	dtrace_optval_t val = 0;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (arg != NULL) {
4930Sstevel@tonic-gate 		errno = 0;
4940Sstevel@tonic-gate 		val = strtoull(arg, &end, 0);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		if (*end != '\0' || errno != 0 || val < 0)
4970Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	dtp->dt_options[option] = val;
5010Sstevel@tonic-gate 	return (0);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate static int
5050Sstevel@tonic-gate dt_opt_size(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate 	char *end;
5080Sstevel@tonic-gate 	int len;
5090Sstevel@tonic-gate 	dtrace_optval_t mul = 1, val = 0;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (arg != NULL) {
5120Sstevel@tonic-gate 		len = strlen(arg);
5130Sstevel@tonic-gate 		errno = 0;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		switch (arg[len - 1]) {
5160Sstevel@tonic-gate 		case 't':
5170Sstevel@tonic-gate 		case 'T':
5180Sstevel@tonic-gate 			mul *= 1024;
5190Sstevel@tonic-gate 			/*FALLTHRU*/
5200Sstevel@tonic-gate 		case 'g':
5210Sstevel@tonic-gate 		case 'G':
5220Sstevel@tonic-gate 			mul *= 1024;
5230Sstevel@tonic-gate 			/*FALLTHRU*/
5240Sstevel@tonic-gate 		case 'm':
5250Sstevel@tonic-gate 		case 'M':
5260Sstevel@tonic-gate 			mul *= 1024;
5270Sstevel@tonic-gate 			/*FALLTHRU*/
5280Sstevel@tonic-gate 		case 'k':
5290Sstevel@tonic-gate 		case 'K':
5300Sstevel@tonic-gate 			mul *= 1024;
5310Sstevel@tonic-gate 			/*FALLTHRU*/
5320Sstevel@tonic-gate 		default:
5330Sstevel@tonic-gate 			break;
5340Sstevel@tonic-gate 		}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		val = strtoull(arg, &end, 0) * mul;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		if ((mul > 1 && end != &arg[len - 1]) ||
5390Sstevel@tonic-gate 		    (mul == 1 && *end != '\0') || val < 0 ||
5400Sstevel@tonic-gate 		    errno != 0 || val == DTRACEOPT_UNSET)
5410Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	dtp->dt_options[option] = val;
5450Sstevel@tonic-gate 	return (0);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate static int
5490Sstevel@tonic-gate dt_opt_rate(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	char *end;
5520Sstevel@tonic-gate 	int i;
5530Sstevel@tonic-gate 	dtrace_optval_t mul = 1, val = 0;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	const struct {
5560Sstevel@tonic-gate 		char *name;
5570Sstevel@tonic-gate 		hrtime_t mul;
5580Sstevel@tonic-gate 	} suffix[] = {
5590Sstevel@tonic-gate 		{ "ns", 	NANOSEC / NANOSEC },
5600Sstevel@tonic-gate 		{ "nsec",	NANOSEC / NANOSEC },
5610Sstevel@tonic-gate 		{ "us",		NANOSEC / MICROSEC },
5620Sstevel@tonic-gate 		{ "usec",	NANOSEC / MICROSEC },
5630Sstevel@tonic-gate 		{ "ms",		NANOSEC / MILLISEC },
5640Sstevel@tonic-gate 		{ "msec",	NANOSEC / MILLISEC },
5650Sstevel@tonic-gate 		{ "s",		NANOSEC / SEC },
5660Sstevel@tonic-gate 		{ "sec",	NANOSEC / SEC },
5670Sstevel@tonic-gate 		{ "m",		NANOSEC * (hrtime_t)60 },
5680Sstevel@tonic-gate 		{ "min",	NANOSEC * (hrtime_t)60 },
5690Sstevel@tonic-gate 		{ "h",		NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
5700Sstevel@tonic-gate 		{ "hour",	NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
5710Sstevel@tonic-gate 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
5720Sstevel@tonic-gate 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
5730Sstevel@tonic-gate 		{ "hz",		0 },
5740Sstevel@tonic-gate 		{ NULL }
5750Sstevel@tonic-gate 	};
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (arg != NULL) {
5780Sstevel@tonic-gate 		errno = 0;
5790Sstevel@tonic-gate 		val = strtoull(arg, &end, 0);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		for (i = 0; suffix[i].name != NULL; i++) {
5820Sstevel@tonic-gate 			if (strcasecmp(suffix[i].name, end) == 0) {
5830Sstevel@tonic-gate 				mul = suffix[i].mul;
5840Sstevel@tonic-gate 				break;
5850Sstevel@tonic-gate 			}
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		if (suffix[i].name == NULL && *end != '\0' || val < 0)
5890Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		if (mul == 0) {
5920Sstevel@tonic-gate 			/*
5930Sstevel@tonic-gate 			 * The rate has been specified in frequency-per-second.
5940Sstevel@tonic-gate 			 */
5950Sstevel@tonic-gate 			if (val != 0)
5960Sstevel@tonic-gate 				val = NANOSEC / val;
5970Sstevel@tonic-gate 		} else {
5980Sstevel@tonic-gate 			val *= mul;
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	dtp->dt_options[option] = val;
6030Sstevel@tonic-gate 	return (0);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * When setting the strsize option, set the option in the dt_options array
6080Sstevel@tonic-gate  * using dt_opt_size() as usual, and then update the definition of the CTF
6090Sstevel@tonic-gate  * type for the D intrinsic "string" to be an array of the corresponding size.
6100Sstevel@tonic-gate  * If any errors occur, reset dt_options[option] to its previous value.
6110Sstevel@tonic-gate  */
6120Sstevel@tonic-gate static int
6130Sstevel@tonic-gate dt_opt_strsize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	dtrace_optval_t val = dtp->dt_options[option];
6160Sstevel@tonic-gate 	ctf_file_t *fp = DT_STR_CTFP(dtp);
6170Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(fp, DT_STR_TYPE(dtp));
6180Sstevel@tonic-gate 	ctf_arinfo_t r;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if (dt_opt_size(dtp, arg, option) != 0)
6210Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (dtp->dt_options[option] > UINT_MAX) {
6240Sstevel@tonic-gate 		dtp->dt_options[option] = val;
6250Sstevel@tonic-gate 		return (dt_set_errno(dtp, EOVERFLOW));
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (ctf_array_info(fp, type, &r) == CTF_ERR) {
6290Sstevel@tonic-gate 		dtp->dt_options[option] = val;
6300Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(fp);
6310Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	r.ctr_nelems = (uint_t)dtp->dt_options[option];
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (ctf_set_array(fp, type, &r) == CTF_ERR ||
6370Sstevel@tonic-gate 	    ctf_update(fp) == CTF_ERR) {
6380Sstevel@tonic-gate 		dtp->dt_options[option] = val;
6390Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(fp);
6400Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (0);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static const struct {
6470Sstevel@tonic-gate 	const char *dtbp_name;
6480Sstevel@tonic-gate 	int dtbp_policy;
6490Sstevel@tonic-gate } _dtrace_bufpolicies[] = {
6500Sstevel@tonic-gate 	{ "ring", DTRACEOPT_BUFPOLICY_RING },
6510Sstevel@tonic-gate 	{ "fill", DTRACEOPT_BUFPOLICY_FILL },
6520Sstevel@tonic-gate 	{ "switch", DTRACEOPT_BUFPOLICY_SWITCH },
6530Sstevel@tonic-gate 	{ NULL, 0 }
6540Sstevel@tonic-gate };
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*ARGSUSED*/
6570Sstevel@tonic-gate static int
6580Sstevel@tonic-gate dt_opt_bufpolicy(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate 	dtrace_optval_t policy = DTRACEOPT_UNSET;
6610Sstevel@tonic-gate 	int i;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (arg == NULL)
6640Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	for (i = 0; _dtrace_bufpolicies[i].dtbp_name != NULL; i++) {
6670Sstevel@tonic-gate 		if (strcmp(_dtrace_bufpolicies[i].dtbp_name, arg) == 0) {
6680Sstevel@tonic-gate 			policy = _dtrace_bufpolicies[i].dtbp_policy;
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (policy == DTRACEOPT_UNSET)
6740Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_BUFPOLICY] = policy;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	return (0);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate static const struct {
6820Sstevel@tonic-gate 	const char *dtbr_name;
6830Sstevel@tonic-gate 	int dtbr_policy;
6840Sstevel@tonic-gate } _dtrace_bufresize[] = {
6850Sstevel@tonic-gate 	{ "auto", DTRACEOPT_BUFRESIZE_AUTO },
6860Sstevel@tonic-gate 	{ "manual", DTRACEOPT_BUFRESIZE_MANUAL },
6870Sstevel@tonic-gate 	{ NULL, 0 }
6880Sstevel@tonic-gate };
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate /*ARGSUSED*/
6910Sstevel@tonic-gate static int
6920Sstevel@tonic-gate dt_opt_bufresize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	dtrace_optval_t policy = DTRACEOPT_UNSET;
6950Sstevel@tonic-gate 	int i;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (arg == NULL)
6980Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	for (i = 0; _dtrace_bufresize[i].dtbr_name != NULL; i++) {
7010Sstevel@tonic-gate 		if (strcmp(_dtrace_bufresize[i].dtbr_name, arg) == 0) {
7020Sstevel@tonic-gate 			policy = _dtrace_bufresize[i].dtbr_policy;
7030Sstevel@tonic-gate 			break;
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (policy == DTRACEOPT_UNSET)
7080Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_BUFRESIZE] = policy;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	return (0);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate int
7160Sstevel@tonic-gate dt_options_load(dtrace_hdl_t *dtp)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	dof_hdr_t hdr, *dof;
7190Sstevel@tonic-gate 	dof_sec_t *sec;
7200Sstevel@tonic-gate 	size_t offs;
7210Sstevel@tonic-gate 	int i;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/*
7240Sstevel@tonic-gate 	 * To load the option values, we need to ask the kernel to provide its
7250Sstevel@tonic-gate 	 * DOF, which we'll sift through to look for OPTDESC sections.
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	bzero(&hdr, sizeof (dof_hdr_t));
7280Sstevel@tonic-gate 	hdr.dofh_loadsz = sizeof (dof_hdr_t);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, &hdr) == -1)
7310Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (hdr.dofh_loadsz < sizeof (dof_hdr_t))
7340Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	dof = alloca(hdr.dofh_loadsz);
7370Sstevel@tonic-gate 	bzero(dof, sizeof (dof_hdr_t));
7380Sstevel@tonic-gate 	dof->dofh_loadsz = hdr.dofh_loadsz;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++)
7410Sstevel@tonic-gate 		dtp->dt_options[i] = DTRACEOPT_UNSET;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, dof) == -1)
7440Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	for (i = 0; i < dof->dofh_secnum; i++) {
747*191Sahl 		sec = (dof_sec_t *)(uintptr_t)((uintptr_t)dof +
7480Sstevel@tonic-gate 		    dof->dofh_secoff + i * dof->dofh_secsize);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		if (sec->dofs_type != DOF_SECT_OPTDESC)
7510Sstevel@tonic-gate 			continue;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		break;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
757*191Sahl 		dof_optdesc_t *opt = (dof_optdesc_t *)(uintptr_t)
758*191Sahl 		    ((uintptr_t)dof + sec->dofs_offset + offs);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (opt->dofo_strtab != DOF_SECIDX_NONE)
7610Sstevel@tonic-gate 			continue;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		if (opt->dofo_option >= DTRACEOPT_MAX)
7640Sstevel@tonic-gate 			continue;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		dtp->dt_options[opt->dofo_option] = opt->dofo_value;
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	return (0);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate typedef struct dt_option {
7730Sstevel@tonic-gate 	const char *o_name;
7740Sstevel@tonic-gate 	int (*o_func)(dtrace_hdl_t *, const char *, uintptr_t);
7750Sstevel@tonic-gate 	uintptr_t o_option;
7760Sstevel@tonic-gate } dt_option_t;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate /*
7790Sstevel@tonic-gate  * Compile-time options.
7800Sstevel@tonic-gate  */
7810Sstevel@tonic-gate static const dt_option_t _dtrace_ctoptions[] = {
7820Sstevel@tonic-gate 	{ "aggpercpu", dt_opt_agg, DTRACE_A_PERCPU },
7830Sstevel@tonic-gate 	{ "amin", dt_opt_amin },
7840Sstevel@tonic-gate 	{ "argref", dt_opt_cflags, DTRACE_C_ARGREF },
7850Sstevel@tonic-gate 	{ "core", dt_opt_core },
7860Sstevel@tonic-gate 	{ "cpp", dt_opt_cflags, DTRACE_C_CPP },
7870Sstevel@tonic-gate 	{ "cpphdrs", dt_opt_cpp_hdrs },
7880Sstevel@tonic-gate 	{ "cpppath", dt_opt_cpp_path },
7890Sstevel@tonic-gate 	{ "ctypes", dt_opt_ctypes },
7900Sstevel@tonic-gate 	{ "defaultargs", dt_opt_cflags, DTRACE_C_DEFARG },
7910Sstevel@tonic-gate 	{ "dtypes", dt_opt_dtypes },
7920Sstevel@tonic-gate 	{ "debug", dt_opt_debug },
7930Sstevel@tonic-gate 	{ "define", dt_opt_cpp_opts, (uintptr_t)"-D" },
7940Sstevel@tonic-gate 	{ "empty", dt_opt_cflags, DTRACE_C_EMPTY },
7950Sstevel@tonic-gate 	{ "errtags", dt_opt_cflags, DTRACE_C_ETAGS },
7960Sstevel@tonic-gate 	{ "evaltime", dt_opt_evaltime },
7970Sstevel@tonic-gate 	{ "incdir", dt_opt_cpp_opts, (uintptr_t)"-I" },
7980Sstevel@tonic-gate 	{ "iregs", dt_opt_iregs },
7990Sstevel@tonic-gate 	{ "kdefs", dt_opt_invcflags, DTRACE_C_KNODEF },
8000Sstevel@tonic-gate 	{ "knodefs", dt_opt_cflags, DTRACE_C_KNODEF },
8010Sstevel@tonic-gate 	{ "lazyload", dt_opt_lazyload },
8020Sstevel@tonic-gate 	{ "ldpath", dt_opt_ld_path },
8030Sstevel@tonic-gate 	{ "libdir", dt_opt_libdir },
8040Sstevel@tonic-gate 	{ "linkmode", dt_opt_linkmode },
8050Sstevel@tonic-gate 	{ "linktype", dt_opt_linktype },
8060Sstevel@tonic-gate 	{ "nolibs", dt_opt_cflags, DTRACE_C_NOLIBS },
8070Sstevel@tonic-gate 	{ "pgmax", dt_opt_pgmax },
8080Sstevel@tonic-gate 	{ "pspec", dt_opt_cflags, DTRACE_C_PSPEC },
8090Sstevel@tonic-gate 	{ "stdc", dt_opt_stdc },
8100Sstevel@tonic-gate 	{ "strip", dt_opt_dflags, DTRACE_D_STRIP },
8110Sstevel@tonic-gate 	{ "syslibdir", dt_opt_syslibdir },
8120Sstevel@tonic-gate 	{ "tree", dt_opt_tree },
8130Sstevel@tonic-gate 	{ "tregs", dt_opt_tregs },
8140Sstevel@tonic-gate 	{ "udefs", dt_opt_invcflags, DTRACE_C_UNODEF },
8150Sstevel@tonic-gate 	{ "undef", dt_opt_cpp_opts, (uintptr_t)"-U" },
8160Sstevel@tonic-gate 	{ "unodefs", dt_opt_cflags, DTRACE_C_UNODEF },
8170Sstevel@tonic-gate 	{ "verbose", dt_opt_cflags, DTRACE_C_DIFV },
8180Sstevel@tonic-gate 	{ "version", dt_opt_version },
8190Sstevel@tonic-gate 	{ "zdefs", dt_opt_cflags, DTRACE_C_ZDEFS },
8200Sstevel@tonic-gate 	{ NULL }
8210Sstevel@tonic-gate };
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * Run-time options.
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate static const dt_option_t _dtrace_rtoptions[] = {
8270Sstevel@tonic-gate 	{ "aggrate", dt_opt_rate, DTRACEOPT_AGGRATE },
8280Sstevel@tonic-gate 	{ "aggsize", dt_opt_size, DTRACEOPT_AGGSIZE },
8290Sstevel@tonic-gate 	{ "bufsize", dt_opt_size, DTRACEOPT_BUFSIZE },
8300Sstevel@tonic-gate 	{ "bufpolicy", dt_opt_bufpolicy, DTRACEOPT_BUFPOLICY },
8310Sstevel@tonic-gate 	{ "bufresize", dt_opt_bufresize, DTRACEOPT_BUFRESIZE },
8320Sstevel@tonic-gate 	{ "cleanrate", dt_opt_rate, DTRACEOPT_CLEANRATE },
8330Sstevel@tonic-gate 	{ "cpu", dt_opt_runtime, DTRACEOPT_CPU },
8340Sstevel@tonic-gate 	{ "destructive", dt_opt_runtime, DTRACEOPT_DESTRUCTIVE },
8350Sstevel@tonic-gate 	{ "dynvarsize", dt_opt_size, DTRACEOPT_DYNVARSIZE },
8360Sstevel@tonic-gate 	{ "flowindent", dt_opt_runtime, DTRACEOPT_FLOWINDENT },
8370Sstevel@tonic-gate 	{ "grabanon", dt_opt_runtime, DTRACEOPT_GRABANON },
8380Sstevel@tonic-gate 	{ "jstackframes", dt_opt_runtime, DTRACEOPT_JSTACKFRAMES },
8390Sstevel@tonic-gate 	{ "jstackstrsize", dt_opt_runtime, DTRACEOPT_JSTACKSTRSIZE },
8400Sstevel@tonic-gate 	{ "nspec", dt_opt_runtime, DTRACEOPT_NSPEC },
8410Sstevel@tonic-gate 	{ "quiet", dt_opt_runtime, DTRACEOPT_QUIET },
8420Sstevel@tonic-gate 	{ "rawbytes", dt_opt_runtime, DTRACEOPT_RAWBYTES },
8430Sstevel@tonic-gate 	{ "specsize", dt_opt_size, DTRACEOPT_SPECSIZE },
8440Sstevel@tonic-gate 	{ "stackframes", dt_opt_runtime, DTRACEOPT_STACKFRAMES },
8450Sstevel@tonic-gate 	{ "stackindent", dt_opt_runtime, DTRACEOPT_STACKINDENT },
8460Sstevel@tonic-gate 	{ "statusrate", dt_opt_rate, DTRACEOPT_STATUSRATE },
8470Sstevel@tonic-gate 	{ "strsize", dt_opt_strsize, DTRACEOPT_STRSIZE },
8480Sstevel@tonic-gate 	{ "switchrate", dt_opt_rate, DTRACEOPT_SWITCHRATE },
8490Sstevel@tonic-gate 	{ "ustackframes", dt_opt_runtime, DTRACEOPT_USTACKFRAMES },
8500Sstevel@tonic-gate 	{ NULL }
8510Sstevel@tonic-gate };
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate int
8540Sstevel@tonic-gate dtrace_getopt(dtrace_hdl_t *dtp, const char *opt, dtrace_optval_t *val)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	const dt_option_t *op;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (opt == NULL)
8590Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/*
8620Sstevel@tonic-gate 	 * We only need to search the run-time options -- it's not legal
8630Sstevel@tonic-gate 	 * to get the values of compile-time options.
8640Sstevel@tonic-gate 	 */
8650Sstevel@tonic-gate 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
8660Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0) {
8670Sstevel@tonic-gate 			*val = dtp->dt_options[op->o_option];
8680Sstevel@tonic-gate 			return (0);
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate int
8760Sstevel@tonic-gate dtrace_setopt(dtrace_hdl_t *dtp, const char *opt, const char *val)
8770Sstevel@tonic-gate {
8780Sstevel@tonic-gate 	const dt_option_t *op;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (opt == NULL)
8810Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	for (op = _dtrace_ctoptions; op->o_name != NULL; op++) {
8840Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0)
8850Sstevel@tonic-gate 			return (op->o_func(dtp, val, op->o_option));
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
8890Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0) {
8900Sstevel@tonic-gate 			/*
8910Sstevel@tonic-gate 			 * Currently, no run-time option may be set while
8920Sstevel@tonic-gate 			 * tracing is active.
8930Sstevel@tonic-gate 			 */
8940Sstevel@tonic-gate 			if (dtp->dt_active)
8950Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_ACTIVE));
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			return (op->o_func(dtp, val, op->o_option));
8980Sstevel@tonic-gate 		}
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
9020Sstevel@tonic-gate }
903