xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_options.c (revision 93f27766a7e1af009c5b1e4ca538632857c91aa1)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
266ff6d951SJohn Birrell 
2709e6105fSMark Johnston /*
28a43f0be9SRui Paulo  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
2909e6105fSMark Johnston  * Copyright (c) 2012 by Delphix. All rights reserved.
3009e6105fSMark Johnston  */
3109e6105fSMark Johnston 
326ff6d951SJohn Birrell #include <sys/resource.h>
336ff6d951SJohn Birrell #include <sys/mman.h>
346ff6d951SJohn Birrell #include <sys/types.h>
356ff6d951SJohn Birrell 
366ff6d951SJohn Birrell #include <strings.h>
376ff6d951SJohn Birrell #include <signal.h>
386ff6d951SJohn Birrell #include <stdlib.h>
396ff6d951SJohn Birrell #include <unistd.h>
406ff6d951SJohn Birrell #include <limits.h>
416ff6d951SJohn Birrell #include <errno.h>
426ff6d951SJohn Birrell #include <fcntl.h>
436ff6d951SJohn Birrell 
446ff6d951SJohn Birrell #include <dt_impl.h>
456ff6d951SJohn Birrell #include <dt_string.h>
46*93f27766SDomagoj Stolfa #include <dt_oformat.h>
476ff6d951SJohn Birrell 
486ff6d951SJohn Birrell static int
dt_opt_agg(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)496ff6d951SJohn Birrell dt_opt_agg(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
506ff6d951SJohn Birrell {
516ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
526ff6d951SJohn Birrell 
536ff6d951SJohn Birrell 	if (arg != NULL)
546ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
556ff6d951SJohn Birrell 
566ff6d951SJohn Birrell 	agp->dtat_flags |= option;
576ff6d951SJohn Birrell 	return (0);
586ff6d951SJohn Birrell }
596ff6d951SJohn Birrell 
606ff6d951SJohn Birrell /*ARGSUSED*/
616ff6d951SJohn Birrell static int
dt_opt_amin(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)626ff6d951SJohn Birrell dt_opt_amin(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
636ff6d951SJohn Birrell {
646ff6d951SJohn Birrell 	char str[DTRACE_ATTR2STR_MAX];
656ff6d951SJohn Birrell 	dtrace_attribute_t attr;
666ff6d951SJohn Birrell 
676ff6d951SJohn Birrell 	if (arg == NULL || dtrace_str2attr(arg, &attr) == -1)
686ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
696ff6d951SJohn Birrell 
706ff6d951SJohn Birrell 	dt_dprintf("set compiler attribute minimum to %s\n",
716ff6d951SJohn Birrell 	    dtrace_attr2str(attr, str, sizeof (str)));
726ff6d951SJohn Birrell 
736ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL) {
746ff6d951SJohn Birrell 		dtp->dt_pcb->pcb_cflags |= DTRACE_C_EATTR;
756ff6d951SJohn Birrell 		dtp->dt_pcb->pcb_amin = attr;
766ff6d951SJohn Birrell 	} else {
776ff6d951SJohn Birrell 		dtp->dt_cflags |= DTRACE_C_EATTR;
786ff6d951SJohn Birrell 		dtp->dt_amin = attr;
796ff6d951SJohn Birrell 	}
806ff6d951SJohn Birrell 
816ff6d951SJohn Birrell 	return (0);
826ff6d951SJohn Birrell }
836ff6d951SJohn Birrell 
846ff6d951SJohn Birrell static void
dt_coredump(void)856ff6d951SJohn Birrell dt_coredump(void)
866ff6d951SJohn Birrell {
876ff6d951SJohn Birrell 	const char msg[] = "libdtrace DEBUG: [ forcing coredump ]\n";
886ff6d951SJohn Birrell 
896ff6d951SJohn Birrell 	struct sigaction act;
906ff6d951SJohn Birrell 	struct rlimit lim;
916ff6d951SJohn Birrell 
926ff6d951SJohn Birrell 	(void) write(STDERR_FILENO, msg, sizeof (msg) - 1);
936ff6d951SJohn Birrell 
946ff6d951SJohn Birrell 	act.sa_handler = SIG_DFL;
956ff6d951SJohn Birrell 	act.sa_flags = 0;
966ff6d951SJohn Birrell 
976ff6d951SJohn Birrell 	(void) sigemptyset(&act.sa_mask);
986ff6d951SJohn Birrell 	(void) sigaction(SIGABRT, &act, NULL);
996ff6d951SJohn Birrell 
1006ff6d951SJohn Birrell 	lim.rlim_cur = RLIM_INFINITY;
1016ff6d951SJohn Birrell 	lim.rlim_max = RLIM_INFINITY;
1026ff6d951SJohn Birrell 
1036ff6d951SJohn Birrell 	(void) setrlimit(RLIMIT_CORE, &lim);
1046ff6d951SJohn Birrell 	abort();
1056ff6d951SJohn Birrell }
1066ff6d951SJohn Birrell 
1076ff6d951SJohn Birrell /*ARGSUSED*/
1086ff6d951SJohn Birrell static int
dt_opt_core(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)1096ff6d951SJohn Birrell dt_opt_core(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1106ff6d951SJohn Birrell {
1116ff6d951SJohn Birrell 	static int enabled = 0;
1126ff6d951SJohn Birrell 
1136ff6d951SJohn Birrell 	if (arg != NULL)
1146ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1156ff6d951SJohn Birrell 
1166ff6d951SJohn Birrell 	if (enabled++ || atexit(dt_coredump) == 0)
1176ff6d951SJohn Birrell 		return (0);
1186ff6d951SJohn Birrell 
1196ff6d951SJohn Birrell 	return (dt_set_errno(dtp, errno));
1206ff6d951SJohn Birrell }
1216ff6d951SJohn Birrell 
1226ff6d951SJohn Birrell /*ARGSUSED*/
1236ff6d951SJohn Birrell static int
dt_opt_cpp_hdrs(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)1246ff6d951SJohn Birrell dt_opt_cpp_hdrs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1256ff6d951SJohn Birrell {
1266ff6d951SJohn Birrell 	if (arg != NULL)
1276ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1286ff6d951SJohn Birrell 
1296ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
1306ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
1316ff6d951SJohn Birrell 
1326ff6d951SJohn Birrell 	if (dt_cpp_add_arg(dtp, "-H") == NULL)
1336ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
1346ff6d951SJohn Birrell 
1356ff6d951SJohn Birrell 	return (0);
1366ff6d951SJohn Birrell }
1376ff6d951SJohn Birrell 
1386ff6d951SJohn Birrell /*ARGSUSED*/
1396ff6d951SJohn Birrell static int
dt_opt_cpp_path(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)1406ff6d951SJohn Birrell dt_opt_cpp_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1416ff6d951SJohn Birrell {
1426ff6d951SJohn Birrell 	char *cpp;
1436ff6d951SJohn Birrell 
1446ff6d951SJohn Birrell 	if (arg == NULL)
1456ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
1466ff6d951SJohn Birrell 
1476ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
1486ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
1496ff6d951SJohn Birrell 
1506ff6d951SJohn Birrell 	if ((cpp = strdup(arg)) == NULL)
1516ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
1526ff6d951SJohn Birrell 
1536ff6d951SJohn Birrell 	dtp->dt_cpp_argv[0] = (char *)strbasename(cpp);
1546ff6d951SJohn Birrell 	free(dtp->dt_cpp_path);
1556ff6d951SJohn Birrell 	dtp->dt_cpp_path = cpp;
1566ff6d951SJohn Birrell 
1576ff6d951SJohn Birrell 	return (0);
1586ff6d951SJohn Birrell }
1596ff6d951SJohn Birrell 
1606ff6d951SJohn Birrell static int
dt_opt_cpp_opts(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)1616ff6d951SJohn Birrell dt_opt_cpp_opts(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
1626ff6d951SJohn Birrell {
163fa19b250SRyan Libby 	char *buf = NULL;
1646ff6d951SJohn Birrell 	size_t len;
1656ff6d951SJohn Birrell 	const char *opt = (const char *)option;
166fa19b250SRyan Libby 	int ret;
1676ff6d951SJohn Birrell 
168fa19b250SRyan Libby 	if (opt == NULL || arg == NULL) {
169fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EDT_BADOPTVAL);
170fa19b250SRyan Libby 		goto out;
171fa19b250SRyan Libby 	}
1726ff6d951SJohn Birrell 
173fa19b250SRyan Libby 	if (dtp->dt_pcb != NULL) {
174fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EDT_BADOPTCTX);
175fa19b250SRyan Libby 		goto out;
176fa19b250SRyan Libby 	}
1776ff6d951SJohn Birrell 
1786ff6d951SJohn Birrell 	len = strlen(opt) + strlen(arg) + 1;
179fa19b250SRyan Libby 	if ((buf = dt_alloc(dtp, len)) == NULL) {
180fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EDT_NOMEM);
181fa19b250SRyan Libby 		goto out;
182fa19b250SRyan Libby 	}
1836ff6d951SJohn Birrell 
1846ff6d951SJohn Birrell 	(void) strcpy(buf, opt);
1856ff6d951SJohn Birrell 	(void) strcat(buf, arg);
1866ff6d951SJohn Birrell 
187fa19b250SRyan Libby 	if (dt_cpp_add_arg(dtp, buf) == NULL) {
188fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EDT_NOMEM);
189fa19b250SRyan Libby 		goto out;
190fa19b250SRyan Libby 	}
1916ff6d951SJohn Birrell 
192fa19b250SRyan Libby 	ret = 0;
193fa19b250SRyan Libby out:
194fa19b250SRyan Libby 	if (buf != NULL)
195fa19b250SRyan Libby 		dt_free(dtp, buf);
196fa19b250SRyan Libby 	return (ret);
1976ff6d951SJohn Birrell }
1986ff6d951SJohn Birrell 
1996ff6d951SJohn Birrell /*ARGSUSED*/
2006ff6d951SJohn Birrell static int
dt_opt_ctypes(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2016ff6d951SJohn Birrell dt_opt_ctypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2026ff6d951SJohn Birrell {
2036ff6d951SJohn Birrell 	int fd;
2046ff6d951SJohn Birrell 
2056ff6d951SJohn Birrell 	if (arg == NULL)
2066ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2076ff6d951SJohn Birrell 
2086ff6d951SJohn Birrell 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
2096ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2106ff6d951SJohn Birrell 
2116ff6d951SJohn Birrell 	(void) close(dtp->dt_cdefs_fd);
2126ff6d951SJohn Birrell 	dtp->dt_cdefs_fd = fd;
2136ff6d951SJohn Birrell 	return (0);
2146ff6d951SJohn Birrell }
2156ff6d951SJohn Birrell 
2166ff6d951SJohn Birrell /*ARGSUSED*/
2176ff6d951SJohn Birrell static int
dt_opt_droptags(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2186ff6d951SJohn Birrell dt_opt_droptags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2196ff6d951SJohn Birrell {
2206ff6d951SJohn Birrell 	dtp->dt_droptags = 1;
2216ff6d951SJohn Birrell 	return (0);
2226ff6d951SJohn Birrell }
2236ff6d951SJohn Birrell 
2246ff6d951SJohn Birrell /*ARGSUSED*/
2256ff6d951SJohn Birrell static int
dt_opt_dtypes(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2266ff6d951SJohn Birrell dt_opt_dtypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2276ff6d951SJohn Birrell {
2286ff6d951SJohn Birrell 	int fd;
2296ff6d951SJohn Birrell 
2306ff6d951SJohn Birrell 	if (arg == NULL)
2316ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2326ff6d951SJohn Birrell 
2336ff6d951SJohn Birrell 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
2346ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2356ff6d951SJohn Birrell 
2366ff6d951SJohn Birrell 	(void) close(dtp->dt_ddefs_fd);
2376ff6d951SJohn Birrell 	dtp->dt_ddefs_fd = fd;
2386ff6d951SJohn Birrell 	return (0);
2396ff6d951SJohn Birrell }
2406ff6d951SJohn Birrell 
2416ff6d951SJohn Birrell /*ARGSUSED*/
2426ff6d951SJohn Birrell static int
dt_opt_debug(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2436ff6d951SJohn Birrell dt_opt_debug(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2446ff6d951SJohn Birrell {
2456ff6d951SJohn Birrell 	if (arg != NULL)
2466ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2476ff6d951SJohn Birrell 
2486ff6d951SJohn Birrell 	_dtrace_debug = 1;
2496ff6d951SJohn Birrell 	return (0);
2506ff6d951SJohn Birrell }
2516ff6d951SJohn Birrell 
2526ff6d951SJohn Birrell /*ARGSUSED*/
2536ff6d951SJohn Birrell static int
dt_opt_iregs(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2546ff6d951SJohn Birrell dt_opt_iregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2556ff6d951SJohn Birrell {
2566ff6d951SJohn Birrell 	int n;
2576ff6d951SJohn Birrell 
2586ff6d951SJohn Birrell 	if (arg == NULL || (n = atoi(arg)) <= 0)
2596ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2606ff6d951SJohn Birrell 
2616ff6d951SJohn Birrell 	dtp->dt_conf.dtc_difintregs = n;
2626ff6d951SJohn Birrell 	return (0);
2636ff6d951SJohn Birrell }
2646ff6d951SJohn Birrell 
2656ff6d951SJohn Birrell /*ARGSUSED*/
2666ff6d951SJohn Birrell static int
dt_opt_lazyload(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2676ff6d951SJohn Birrell dt_opt_lazyload(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2686ff6d951SJohn Birrell {
2696ff6d951SJohn Birrell 	dtp->dt_lazyload = 1;
2706ff6d951SJohn Birrell 
2716ff6d951SJohn Birrell 	return (0);
2726ff6d951SJohn Birrell }
2736ff6d951SJohn Birrell 
2746ff6d951SJohn Birrell /*ARGSUSED*/
2756ff6d951SJohn Birrell static int
dt_opt_ld_path(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)2766ff6d951SJohn Birrell dt_opt_ld_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
2776ff6d951SJohn Birrell {
2786ff6d951SJohn Birrell 	char *ld;
2796ff6d951SJohn Birrell 
2806ff6d951SJohn Birrell 	if (arg == NULL)
2816ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
2826ff6d951SJohn Birrell 
2836ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
2846ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
2856ff6d951SJohn Birrell 
2866ff6d951SJohn Birrell 	if ((ld = strdup(arg)) == NULL)
2876ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
2886ff6d951SJohn Birrell 
2896ff6d951SJohn Birrell 	free(dtp->dt_ld_path);
2906ff6d951SJohn Birrell 	dtp->dt_ld_path = ld;
2916ff6d951SJohn Birrell 
2926ff6d951SJohn Birrell 	return (0);
2936ff6d951SJohn Birrell }
2946ff6d951SJohn Birrell 
29503a5f9f0SMark Johnston #ifdef __FreeBSD__
29603a5f9f0SMark Johnston static int
dt_opt_objcopy_path(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)29703a5f9f0SMark Johnston dt_opt_objcopy_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
29803a5f9f0SMark Johnston {
29903a5f9f0SMark Johnston 	char *objcopy;
30003a5f9f0SMark Johnston 
30103a5f9f0SMark Johnston 	if (arg == NULL)
30203a5f9f0SMark Johnston 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
30303a5f9f0SMark Johnston 
30403a5f9f0SMark Johnston 	if (dtp->dt_pcb != NULL)
30503a5f9f0SMark Johnston 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
30603a5f9f0SMark Johnston 
30703a5f9f0SMark Johnston 	if ((objcopy = strdup(arg)) == NULL)
30803a5f9f0SMark Johnston 		return (dt_set_errno(dtp, EDT_NOMEM));
30903a5f9f0SMark Johnston 
31003a5f9f0SMark Johnston 	free(dtp->dt_objcopy_path);
31103a5f9f0SMark Johnston 	dtp->dt_objcopy_path = objcopy;
31203a5f9f0SMark Johnston 
31303a5f9f0SMark Johnston 	return (0);
31403a5f9f0SMark Johnston }
31503a5f9f0SMark Johnston #endif
31603a5f9f0SMark Johnston 
3176ff6d951SJohn Birrell /*ARGSUSED*/
3186ff6d951SJohn Birrell static int
dt_opt_libdir(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)3196ff6d951SJohn Birrell dt_opt_libdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3206ff6d951SJohn Birrell {
3216ff6d951SJohn Birrell 	dt_dirpath_t *dp;
3226ff6d951SJohn Birrell 
3236ff6d951SJohn Birrell 	if (arg == NULL)
3246ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3256ff6d951SJohn Birrell 
3266ff6d951SJohn Birrell 	if ((dp = malloc(sizeof (dt_dirpath_t))) == NULL ||
3276ff6d951SJohn Birrell 	    (dp->dir_path = strdup(arg)) == NULL) {
3286ff6d951SJohn Birrell 		free(dp);
3296ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
3306ff6d951SJohn Birrell 	}
3316ff6d951SJohn Birrell 
3326ff6d951SJohn Birrell 	dt_list_append(&dtp->dt_lib_path, dp);
3336ff6d951SJohn Birrell 	return (0);
3346ff6d951SJohn Birrell }
3356ff6d951SJohn Birrell 
3366ff6d951SJohn Birrell /*ARGSUSED*/
3376ff6d951SJohn Birrell static int
dt_opt_linkmode(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)3386ff6d951SJohn Birrell dt_opt_linkmode(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3396ff6d951SJohn Birrell {
3406ff6d951SJohn Birrell 	if (arg == NULL)
3416ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3426ff6d951SJohn Birrell 
3436ff6d951SJohn Birrell 	if (strcmp(arg, "kernel") == 0)
3446ff6d951SJohn Birrell 		dtp->dt_linkmode = DT_LINK_KERNEL;
3456ff6d951SJohn Birrell 	else if (strcmp(arg, "primary") == 0)
3466ff6d951SJohn Birrell 		dtp->dt_linkmode = DT_LINK_PRIMARY;
3476ff6d951SJohn Birrell 	else if (strcmp(arg, "dynamic") == 0)
3486ff6d951SJohn Birrell 		dtp->dt_linkmode = DT_LINK_DYNAMIC;
3496ff6d951SJohn Birrell 	else if (strcmp(arg, "static") == 0)
3506ff6d951SJohn Birrell 		dtp->dt_linkmode = DT_LINK_STATIC;
3516ff6d951SJohn Birrell 	else
3526ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3536ff6d951SJohn Birrell 
3546ff6d951SJohn Birrell 	return (0);
3556ff6d951SJohn Birrell }
3566ff6d951SJohn Birrell 
3576ff6d951SJohn Birrell /*ARGSUSED*/
3586ff6d951SJohn Birrell static int
dt_opt_linktype(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)3596ff6d951SJohn Birrell dt_opt_linktype(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3606ff6d951SJohn Birrell {
3616ff6d951SJohn Birrell 	if (arg == NULL)
3626ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3636ff6d951SJohn Birrell 
3646ff6d951SJohn Birrell 	if (strcasecmp(arg, "elf") == 0)
3656ff6d951SJohn Birrell 		dtp->dt_linktype = DT_LTYP_ELF;
3666ff6d951SJohn Birrell 	else if (strcasecmp(arg, "dof") == 0)
3676ff6d951SJohn Birrell 		dtp->dt_linktype = DT_LTYP_DOF;
3686ff6d951SJohn Birrell 	else
3696ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3706ff6d951SJohn Birrell 
3716ff6d951SJohn Birrell 	return (0);
3726ff6d951SJohn Birrell }
3736ff6d951SJohn Birrell 
3746ff6d951SJohn Birrell /*ARGSUSED*/
3756ff6d951SJohn Birrell static int
dt_opt_encoding(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)376a43f0be9SRui Paulo dt_opt_encoding(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
377a43f0be9SRui Paulo {
378a43f0be9SRui Paulo 	if (arg == NULL)
379a43f0be9SRui Paulo 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
380a43f0be9SRui Paulo 
381a43f0be9SRui Paulo 	if (strcmp(arg, "ascii") == 0)
382a43f0be9SRui Paulo 		dtp->dt_encoding = DT_ENCODING_ASCII;
383a43f0be9SRui Paulo 	else if (strcmp(arg, "utf8") == 0)
384a43f0be9SRui Paulo 		dtp->dt_encoding = DT_ENCODING_UTF8;
385a43f0be9SRui Paulo 	else
386a43f0be9SRui Paulo 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
387a43f0be9SRui Paulo 
388a43f0be9SRui Paulo 	return (0);
389a43f0be9SRui Paulo }
390a43f0be9SRui Paulo 
391a43f0be9SRui Paulo /*ARGSUSED*/
392a43f0be9SRui Paulo static int
dt_opt_evaltime(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)3936ff6d951SJohn Birrell dt_opt_evaltime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
3946ff6d951SJohn Birrell {
3956ff6d951SJohn Birrell 	if (arg == NULL)
3966ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
3976ff6d951SJohn Birrell 
3986ff6d951SJohn Birrell 	if (strcmp(arg, "exec") == 0)
3996ff6d951SJohn Birrell 		dtp->dt_prcmode = DT_PROC_STOP_CREATE;
4006ff6d951SJohn Birrell 	else if (strcmp(arg, "preinit") == 0)
4016ff6d951SJohn Birrell 		dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
4026ff6d951SJohn Birrell 	else if (strcmp(arg, "postinit") == 0)
4036ff6d951SJohn Birrell 		dtp->dt_prcmode = DT_PROC_STOP_POSTINIT;
4046ff6d951SJohn Birrell 	else if (strcmp(arg, "main") == 0)
4056ff6d951SJohn Birrell 		dtp->dt_prcmode = DT_PROC_STOP_MAIN;
4066ff6d951SJohn Birrell 	else
4076ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4086ff6d951SJohn Birrell 
4096ff6d951SJohn Birrell 	return (0);
4106ff6d951SJohn Birrell }
4116ff6d951SJohn Birrell 
4126ff6d951SJohn Birrell /*ARGSUSED*/
4136ff6d951SJohn Birrell static int
dt_opt_pgmax(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)4146ff6d951SJohn Birrell dt_opt_pgmax(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4156ff6d951SJohn Birrell {
4166ff6d951SJohn Birrell 	int n;
4176ff6d951SJohn Birrell 
4186ff6d951SJohn Birrell 	if (arg == NULL || (n = atoi(arg)) < 0)
4196ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4206ff6d951SJohn Birrell 
4216ff6d951SJohn Birrell 	dtp->dt_procs->dph_lrulim = n;
4226ff6d951SJohn Birrell 	return (0);
4236ff6d951SJohn Birrell }
4246ff6d951SJohn Birrell 
42509e6105fSMark Johnston static int
dt_opt_setenv(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)42609e6105fSMark Johnston dt_opt_setenv(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
42709e6105fSMark Johnston {
42809e6105fSMark Johnston 	char **p;
42909e6105fSMark Johnston 	char *var;
430ff9ebfc4SMark Johnston 	int nvars;
43109e6105fSMark Johnston 
43209e6105fSMark Johnston 	/*
43309e6105fSMark Johnston 	 * We can't effectively set environment variables from #pragma lines
43409e6105fSMark Johnston 	 * since the processes have already been spawned.
43509e6105fSMark Johnston 	 */
43609e6105fSMark Johnston 	if (dtp->dt_pcb != NULL)
43709e6105fSMark Johnston 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
43809e6105fSMark Johnston 
43909e6105fSMark Johnston 	if (arg == NULL)
44009e6105fSMark Johnston 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
44109e6105fSMark Johnston 
44209e6105fSMark Johnston 	if (!option && strchr(arg, '=') != NULL)
44309e6105fSMark Johnston 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
44409e6105fSMark Johnston 
445ff9ebfc4SMark Johnston 	for (nvars = 0, p = dtp->dt_proc_env; *p != NULL; nvars++, p++)
44609e6105fSMark Johnston 		continue;
44709e6105fSMark Johnston 
44809e6105fSMark Johnston 	for (p = dtp->dt_proc_env; *p != NULL; p++) {
44909e6105fSMark Johnston 		var = strchr(*p, '=');
45009e6105fSMark Johnston 		if (var == NULL)
45109e6105fSMark Johnston 			var = *p + strlen(*p);
45209e6105fSMark Johnston 		if (strncmp(*p, arg, var - *p) == 0) {
45309e6105fSMark Johnston 			dt_free(dtp, *p);
454ff9ebfc4SMark Johnston 			*p = dtp->dt_proc_env[nvars - 1];
455ff9ebfc4SMark Johnston 			dtp->dt_proc_env[nvars - 1] = NULL;
456ff9ebfc4SMark Johnston 			nvars--;
45709e6105fSMark Johnston 		}
45809e6105fSMark Johnston 	}
45909e6105fSMark Johnston 
46009e6105fSMark Johnston 	if (option) {
46109e6105fSMark Johnston 		if ((var = strdup(arg)) == NULL)
46209e6105fSMark Johnston 			return (dt_set_errno(dtp, EDT_NOMEM));
46309e6105fSMark Johnston 
464ff9ebfc4SMark Johnston 		nvars++;
465ff9ebfc4SMark Johnston 		if ((p = dt_alloc(dtp, sizeof(char *) * (nvars + 1))) == NULL) {
46609e6105fSMark Johnston 			dt_free(dtp, var);
46709e6105fSMark Johnston 			return (dt_set_errno(dtp, EDT_NOMEM));
46809e6105fSMark Johnston 		}
46909e6105fSMark Johnston 
470ff9ebfc4SMark Johnston 		bcopy(dtp->dt_proc_env, p, sizeof(char *) * nvars);
47109e6105fSMark Johnston 		dt_free(dtp, dtp->dt_proc_env);
47209e6105fSMark Johnston 		dtp->dt_proc_env = p;
47309e6105fSMark Johnston 
474ff9ebfc4SMark Johnston 		dtp->dt_proc_env[nvars - 1] = var;
475ff9ebfc4SMark Johnston 		dtp->dt_proc_env[nvars] = NULL;
47609e6105fSMark Johnston 	}
47709e6105fSMark Johnston 
47809e6105fSMark Johnston 	return (0);
47909e6105fSMark Johnston }
48009e6105fSMark Johnston 
4816ff6d951SJohn Birrell /*ARGSUSED*/
4826ff6d951SJohn Birrell static int
dt_opt_stdc(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)4836ff6d951SJohn Birrell dt_opt_stdc(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
4846ff6d951SJohn Birrell {
4856ff6d951SJohn Birrell 	if (arg == NULL)
4866ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
4876ff6d951SJohn Birrell 
4886ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
4896ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
4906ff6d951SJohn Birrell 
4916ff6d951SJohn Birrell 	if (strcmp(arg, "a") == 0)
4926ff6d951SJohn Birrell 		dtp->dt_stdcmode = DT_STDC_XA;
4936ff6d951SJohn Birrell 	else if (strcmp(arg, "c") == 0)
4946ff6d951SJohn Birrell 		dtp->dt_stdcmode = DT_STDC_XC;
4956ff6d951SJohn Birrell 	else if (strcmp(arg, "s") == 0)
4966ff6d951SJohn Birrell 		dtp->dt_stdcmode = DT_STDC_XS;
4976ff6d951SJohn Birrell 	else if (strcmp(arg, "t") == 0)
4986ff6d951SJohn Birrell 		dtp->dt_stdcmode = DT_STDC_XT;
4996ff6d951SJohn Birrell 	else
5006ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5016ff6d951SJohn Birrell 
5026ff6d951SJohn Birrell 	return (0);
5036ff6d951SJohn Birrell }
5046ff6d951SJohn Birrell 
5056ff6d951SJohn Birrell /*ARGSUSED*/
5066ff6d951SJohn Birrell static int
dt_opt_syslibdir(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5076ff6d951SJohn Birrell dt_opt_syslibdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5086ff6d951SJohn Birrell {
5096ff6d951SJohn Birrell 	dt_dirpath_t *dp = dt_list_next(&dtp->dt_lib_path);
5106ff6d951SJohn Birrell 	char *path;
5116ff6d951SJohn Birrell 
5126ff6d951SJohn Birrell 	if (arg == NULL)
5136ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5146ff6d951SJohn Birrell 
5156ff6d951SJohn Birrell 	if ((path = strdup(arg)) == NULL)
5166ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
5176ff6d951SJohn Birrell 
5186ff6d951SJohn Birrell 	free(dp->dir_path);
5196ff6d951SJohn Birrell 	dp->dir_path = path;
5206ff6d951SJohn Birrell 
5216ff6d951SJohn Birrell 	return (0);
5226ff6d951SJohn Birrell }
5236ff6d951SJohn Birrell 
5246ff6d951SJohn Birrell /*ARGSUSED*/
5256ff6d951SJohn Birrell static int
dt_opt_tree(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5266ff6d951SJohn Birrell dt_opt_tree(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5276ff6d951SJohn Birrell {
5286ff6d951SJohn Birrell 	int m;
5296ff6d951SJohn Birrell 
5306ff6d951SJohn Birrell 	if (arg == NULL || (m = atoi(arg)) <= 0)
5316ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5326ff6d951SJohn Birrell 
5336ff6d951SJohn Birrell 	dtp->dt_treedump = m;
5346ff6d951SJohn Birrell 	return (0);
5356ff6d951SJohn Birrell }
5366ff6d951SJohn Birrell 
5376ff6d951SJohn Birrell /*ARGSUSED*/
5386ff6d951SJohn Birrell static int
dt_opt_tregs(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5396ff6d951SJohn Birrell dt_opt_tregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5406ff6d951SJohn Birrell {
5416ff6d951SJohn Birrell 	int n;
5426ff6d951SJohn Birrell 
5436ff6d951SJohn Birrell 	if (arg == NULL || (n = atoi(arg)) <= 0)
5446ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5456ff6d951SJohn Birrell 
5466ff6d951SJohn Birrell 	dtp->dt_conf.dtc_diftupregs = n;
5476ff6d951SJohn Birrell 	return (0);
5486ff6d951SJohn Birrell }
5496ff6d951SJohn Birrell 
5506ff6d951SJohn Birrell /*ARGSUSED*/
5516ff6d951SJohn Birrell static int
dt_opt_xlate(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5526ff6d951SJohn Birrell dt_opt_xlate(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5536ff6d951SJohn Birrell {
5546ff6d951SJohn Birrell 	if (arg == NULL)
5556ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5566ff6d951SJohn Birrell 
5576ff6d951SJohn Birrell 	if (strcmp(arg, "dynamic") == 0)
5586ff6d951SJohn Birrell 		dtp->dt_xlatemode = DT_XL_DYNAMIC;
5596ff6d951SJohn Birrell 	else if (strcmp(arg, "static") == 0)
5606ff6d951SJohn Birrell 		dtp->dt_xlatemode = DT_XL_STATIC;
5616ff6d951SJohn Birrell 	else
5626ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5636ff6d951SJohn Birrell 
5646ff6d951SJohn Birrell 	return (0);
5656ff6d951SJohn Birrell }
5666ff6d951SJohn Birrell 
5676ff6d951SJohn Birrell /*ARGSUSED*/
5686ff6d951SJohn Birrell static int
dt_opt_cflags(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5696ff6d951SJohn Birrell dt_opt_cflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5706ff6d951SJohn Birrell {
5716ff6d951SJohn Birrell 	if (arg != NULL)
5726ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5736ff6d951SJohn Birrell 
5746ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
5756ff6d951SJohn Birrell 		dtp->dt_pcb->pcb_cflags |= option;
5766ff6d951SJohn Birrell 	else
5776ff6d951SJohn Birrell 		dtp->dt_cflags |= option;
5786ff6d951SJohn Birrell 
5796ff6d951SJohn Birrell 	return (0);
5806ff6d951SJohn Birrell }
5816ff6d951SJohn Birrell 
5826ff6d951SJohn Birrell static int
dt_opt_dflags(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5836ff6d951SJohn Birrell dt_opt_dflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5846ff6d951SJohn Birrell {
5856ff6d951SJohn Birrell 	if (arg != NULL)
5866ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5876ff6d951SJohn Birrell 
5886ff6d951SJohn Birrell 	dtp->dt_dflags |= option;
5896ff6d951SJohn Birrell 	return (0);
5906ff6d951SJohn Birrell }
5916ff6d951SJohn Birrell 
5926ff6d951SJohn Birrell static int
dt_opt_invcflags(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)5936ff6d951SJohn Birrell dt_opt_invcflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
5946ff6d951SJohn Birrell {
5956ff6d951SJohn Birrell 	if (arg != NULL)
5966ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
5976ff6d951SJohn Birrell 
5986ff6d951SJohn Birrell 	if (dtp->dt_pcb != NULL)
5996ff6d951SJohn Birrell 		dtp->dt_pcb->pcb_cflags &= ~option;
6006ff6d951SJohn Birrell 	else
6016ff6d951SJohn Birrell 		dtp->dt_cflags &= ~option;
6026ff6d951SJohn Birrell 
6036ff6d951SJohn Birrell 	return (0);
6046ff6d951SJohn Birrell }
6056ff6d951SJohn Birrell 
6066ff6d951SJohn Birrell /*ARGSUSED*/
6076ff6d951SJohn Birrell static int
dt_opt_version(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)6086ff6d951SJohn Birrell dt_opt_version(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
6096ff6d951SJohn Birrell {
6106ff6d951SJohn Birrell 	dt_version_t v;
6116ff6d951SJohn Birrell 
6126ff6d951SJohn Birrell 	if (arg == NULL)
6136ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
6146ff6d951SJohn Birrell 
6156ff6d951SJohn Birrell 	if (dt_version_str2num(arg, &v) == -1)
6166ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_VERSINVAL));
6176ff6d951SJohn Birrell 
6186ff6d951SJohn Birrell 	if (!dt_version_defined(v))
6196ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_VERSUNDEF));
6206ff6d951SJohn Birrell 
6216ff6d951SJohn Birrell 	return (dt_reduce(dtp, v));
6226ff6d951SJohn Birrell }
6236ff6d951SJohn Birrell 
6246ff6d951SJohn Birrell static int
dt_opt_runtime(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)6256ff6d951SJohn Birrell dt_opt_runtime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
6266ff6d951SJohn Birrell {
6276ff6d951SJohn Birrell 	char *end;
6286ff6d951SJohn Birrell 	dtrace_optval_t val = 0;
6296ff6d951SJohn Birrell 	int i;
6306ff6d951SJohn Birrell 
6316ff6d951SJohn Birrell 	const struct {
6326ff6d951SJohn Birrell 		char *positive;
6336ff6d951SJohn Birrell 		char *negative;
6346ff6d951SJohn Birrell 	} couples[] = {
6356ff6d951SJohn Birrell 		{ "yes",	"no" },
6366ff6d951SJohn Birrell 		{ "enable",	"disable" },
6376ff6d951SJohn Birrell 		{ "enabled",	"disabled" },
6386ff6d951SJohn Birrell 		{ "true",	"false" },
6396ff6d951SJohn Birrell 		{ "on",		"off" },
6406ff6d951SJohn Birrell 		{ "set",	"unset" },
6416ff6d951SJohn Birrell 		{ NULL }
6426ff6d951SJohn Birrell 	};
6436ff6d951SJohn Birrell 
6446ff6d951SJohn Birrell 	if (arg != NULL) {
6456ff6d951SJohn Birrell 		if (arg[0] == '\0') {
6466ff6d951SJohn Birrell 			val = DTRACEOPT_UNSET;
6476ff6d951SJohn Birrell 			goto out;
6486ff6d951SJohn Birrell 		}
6496ff6d951SJohn Birrell 
6506ff6d951SJohn Birrell 		for (i = 0; couples[i].positive != NULL; i++) {
6516ff6d951SJohn Birrell 			if (strcasecmp(couples[i].positive, arg) == 0) {
6526ff6d951SJohn Birrell 				val = 1;
6536ff6d951SJohn Birrell 				goto out;
6546ff6d951SJohn Birrell 			}
6556ff6d951SJohn Birrell 
6566ff6d951SJohn Birrell 			if (strcasecmp(couples[i].negative, arg) == 0) {
6576ff6d951SJohn Birrell 				val = DTRACEOPT_UNSET;
6586ff6d951SJohn Birrell 				goto out;
6596ff6d951SJohn Birrell 			}
6606ff6d951SJohn Birrell 		}
6616ff6d951SJohn Birrell 
6626ff6d951SJohn Birrell 		errno = 0;
6636ff6d951SJohn Birrell 		val = strtoull(arg, &end, 0);
6646ff6d951SJohn Birrell 
6656ff6d951SJohn Birrell 		if (*end != '\0' || errno != 0 || val < 0)
6666ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
6676ff6d951SJohn Birrell 	}
6686ff6d951SJohn Birrell 
6696ff6d951SJohn Birrell out:
6706ff6d951SJohn Birrell 	dtp->dt_options[option] = val;
6716ff6d951SJohn Birrell 	return (0);
6726ff6d951SJohn Birrell }
6736ff6d951SJohn Birrell 
6746ff6d951SJohn Birrell static int
dt_optval_parse(const char * arg,dtrace_optval_t * rval)6756ff6d951SJohn Birrell dt_optval_parse(const char *arg, dtrace_optval_t *rval)
6766ff6d951SJohn Birrell {
6776ff6d951SJohn Birrell 	dtrace_optval_t mul = 1;
6786ff6d951SJohn Birrell 	size_t len;
6796ff6d951SJohn Birrell 	char *end;
6806ff6d951SJohn Birrell 
6816ff6d951SJohn Birrell 	len = strlen(arg);
6826ff6d951SJohn Birrell 	errno = 0;
6836ff6d951SJohn Birrell 
6846ff6d951SJohn Birrell 	switch (arg[len - 1]) {
6856ff6d951SJohn Birrell 	case 't':
6866ff6d951SJohn Birrell 	case 'T':
6876ff6d951SJohn Birrell 		mul *= 1024;
6886ff6d951SJohn Birrell 		/*FALLTHRU*/
6896ff6d951SJohn Birrell 	case 'g':
6906ff6d951SJohn Birrell 	case 'G':
6916ff6d951SJohn Birrell 		mul *= 1024;
6926ff6d951SJohn Birrell 		/*FALLTHRU*/
6936ff6d951SJohn Birrell 	case 'm':
6946ff6d951SJohn Birrell 	case 'M':
6956ff6d951SJohn Birrell 		mul *= 1024;
6966ff6d951SJohn Birrell 		/*FALLTHRU*/
6976ff6d951SJohn Birrell 	case 'k':
6986ff6d951SJohn Birrell 	case 'K':
6996ff6d951SJohn Birrell 		mul *= 1024;
7006ff6d951SJohn Birrell 		/*FALLTHRU*/
7016ff6d951SJohn Birrell 	default:
7026ff6d951SJohn Birrell 		break;
7036ff6d951SJohn Birrell 	}
7046ff6d951SJohn Birrell 
7056ff6d951SJohn Birrell 	errno = 0;
7066ff6d951SJohn Birrell 	*rval = strtoull(arg, &end, 0) * mul;
7076ff6d951SJohn Birrell 
7086ff6d951SJohn Birrell 	if ((mul > 1 && end != &arg[len - 1]) || (mul == 1 && *end != '\0') ||
7096ff6d951SJohn Birrell 	    *rval < 0 || errno != 0)
7106ff6d951SJohn Birrell 		return (-1);
7116ff6d951SJohn Birrell 
7126ff6d951SJohn Birrell 	return (0);
7136ff6d951SJohn Birrell }
7146ff6d951SJohn Birrell 
7156ff6d951SJohn Birrell static int
dt_opt_size(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)7166ff6d951SJohn Birrell dt_opt_size(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
7176ff6d951SJohn Birrell {
7186ff6d951SJohn Birrell 	dtrace_optval_t val = 0;
7196ff6d951SJohn Birrell 
7206ff6d951SJohn Birrell 	if (arg != NULL && dt_optval_parse(arg, &val) != 0)
7216ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
7226ff6d951SJohn Birrell 
7236ff6d951SJohn Birrell 	dtp->dt_options[option] = val;
7246ff6d951SJohn Birrell 	return (0);
7256ff6d951SJohn Birrell }
7266ff6d951SJohn Birrell 
7276ff6d951SJohn Birrell static int
dt_opt_oformat(dtrace_hdl_t * dtp,const char * arg,uintptr_t option __unused)728*93f27766SDomagoj Stolfa dt_opt_oformat(dtrace_hdl_t *dtp, const char *arg, uintptr_t option __unused)
729*93f27766SDomagoj Stolfa {
730*93f27766SDomagoj Stolfa 	if (arg == NULL)
731*93f27766SDomagoj Stolfa 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
732*93f27766SDomagoj Stolfa 
733*93f27766SDomagoj Stolfa 	if (xo_set_options(NULL, arg) < 0)
734*93f27766SDomagoj Stolfa 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
735*93f27766SDomagoj Stolfa 
736*93f27766SDomagoj Stolfa 	return (dtrace_oformat_configure(dtp));
737*93f27766SDomagoj Stolfa }
738*93f27766SDomagoj Stolfa 
739*93f27766SDomagoj Stolfa static int
dt_opt_rate(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)7406ff6d951SJohn Birrell dt_opt_rate(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
7416ff6d951SJohn Birrell {
7426ff6d951SJohn Birrell 	char *end;
7436ff6d951SJohn Birrell 	int i;
7446ff6d951SJohn Birrell 	dtrace_optval_t mul = 1, val = 0;
7456ff6d951SJohn Birrell 
7466ff6d951SJohn Birrell 	const struct {
7476ff6d951SJohn Birrell 		char *name;
7486ff6d951SJohn Birrell 		hrtime_t mul;
7496ff6d951SJohn Birrell 	} suffix[] = {
7506ff6d951SJohn Birrell 		{ "ns", 	NANOSEC / NANOSEC },
7516ff6d951SJohn Birrell 		{ "nsec",	NANOSEC / NANOSEC },
7526ff6d951SJohn Birrell 		{ "us",		NANOSEC / MICROSEC },
7536ff6d951SJohn Birrell 		{ "usec",	NANOSEC / MICROSEC },
7546ff6d951SJohn Birrell 		{ "ms",		NANOSEC / MILLISEC },
7556ff6d951SJohn Birrell 		{ "msec",	NANOSEC / MILLISEC },
7566ff6d951SJohn Birrell 		{ "s",		NANOSEC / SEC },
7576ff6d951SJohn Birrell 		{ "sec",	NANOSEC / SEC },
7586ff6d951SJohn Birrell 		{ "m",		NANOSEC * (hrtime_t)60 },
7596ff6d951SJohn Birrell 		{ "min",	NANOSEC * (hrtime_t)60 },
7606ff6d951SJohn Birrell 		{ "h",		NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
7616ff6d951SJohn Birrell 		{ "hour",	NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
7626ff6d951SJohn Birrell 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
7636ff6d951SJohn Birrell 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
7646ff6d951SJohn Birrell 		{ "hz",		0 },
7656ff6d951SJohn Birrell 		{ NULL }
7666ff6d951SJohn Birrell 	};
7676ff6d951SJohn Birrell 
7686ff6d951SJohn Birrell 	if (arg != NULL) {
7696ff6d951SJohn Birrell 		errno = 0;
7706ff6d951SJohn Birrell 		val = strtoull(arg, &end, 0);
7716ff6d951SJohn Birrell 
7726ff6d951SJohn Birrell 		for (i = 0; suffix[i].name != NULL; i++) {
7736ff6d951SJohn Birrell 			if (strcasecmp(suffix[i].name, end) == 0) {
7746ff6d951SJohn Birrell 				mul = suffix[i].mul;
7756ff6d951SJohn Birrell 				break;
7766ff6d951SJohn Birrell 			}
7776ff6d951SJohn Birrell 		}
7786ff6d951SJohn Birrell 
7796ff6d951SJohn Birrell 		if (suffix[i].name == NULL && *end != '\0' || val < 0)
7806ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
7816ff6d951SJohn Birrell 
7826ff6d951SJohn Birrell 		if (mul == 0) {
7836ff6d951SJohn Birrell 			/*
7846ff6d951SJohn Birrell 			 * The rate has been specified in frequency-per-second.
7856ff6d951SJohn Birrell 			 */
7866ff6d951SJohn Birrell 			if (val != 0)
7876ff6d951SJohn Birrell 				val = NANOSEC / val;
7886ff6d951SJohn Birrell 		} else {
7896ff6d951SJohn Birrell 			val *= mul;
7906ff6d951SJohn Birrell 		}
7916ff6d951SJohn Birrell 	}
7926ff6d951SJohn Birrell 
7936ff6d951SJohn Birrell 	dtp->dt_options[option] = val;
7946ff6d951SJohn Birrell 	return (0);
7956ff6d951SJohn Birrell }
7966ff6d951SJohn Birrell 
7976ff6d951SJohn Birrell /*
7986ff6d951SJohn Birrell  * When setting the strsize option, set the option in the dt_options array
7996ff6d951SJohn Birrell  * using dt_opt_size() as usual, and then update the definition of the CTF
8006ff6d951SJohn Birrell  * type for the D intrinsic "string" to be an array of the corresponding size.
8016ff6d951SJohn Birrell  * If any errors occur, reset dt_options[option] to its previous value.
8026ff6d951SJohn Birrell  */
8036ff6d951SJohn Birrell static int
dt_opt_strsize(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)8046ff6d951SJohn Birrell dt_opt_strsize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
8056ff6d951SJohn Birrell {
8066ff6d951SJohn Birrell 	dtrace_optval_t val = dtp->dt_options[option];
8076ff6d951SJohn Birrell 	ctf_file_t *fp = DT_STR_CTFP(dtp);
8086ff6d951SJohn Birrell 	ctf_id_t type = ctf_type_resolve(fp, DT_STR_TYPE(dtp));
8096ff6d951SJohn Birrell 	ctf_arinfo_t r;
8106ff6d951SJohn Birrell 
8116ff6d951SJohn Birrell 	if (dt_opt_size(dtp, arg, option) != 0)
8126ff6d951SJohn Birrell 		return (-1); /* dt_errno is set for us */
8136ff6d951SJohn Birrell 
8146ff6d951SJohn Birrell 	if (dtp->dt_options[option] > UINT_MAX) {
8156ff6d951SJohn Birrell 		dtp->dt_options[option] = val;
8166ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EOVERFLOW));
8176ff6d951SJohn Birrell 	}
8186ff6d951SJohn Birrell 
8196ff6d951SJohn Birrell 	if (ctf_array_info(fp, type, &r) == CTF_ERR) {
8206ff6d951SJohn Birrell 		dtp->dt_options[option] = val;
8216ff6d951SJohn Birrell 		dtp->dt_ctferr = ctf_errno(fp);
8226ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_CTF));
8236ff6d951SJohn Birrell 	}
8246ff6d951SJohn Birrell 
8256ff6d951SJohn Birrell 	r.ctr_nelems = (uint_t)dtp->dt_options[option];
8266ff6d951SJohn Birrell 
8276ff6d951SJohn Birrell 	if (ctf_set_array(fp, type, &r) == CTF_ERR ||
8286ff6d951SJohn Birrell 	    ctf_update(fp) == CTF_ERR) {
8296ff6d951SJohn Birrell 		dtp->dt_options[option] = val;
8306ff6d951SJohn Birrell 		dtp->dt_ctferr = ctf_errno(fp);
8316ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_CTF));
8326ff6d951SJohn Birrell 	}
8336ff6d951SJohn Birrell 
8346ff6d951SJohn Birrell 	return (0);
8356ff6d951SJohn Birrell }
8366ff6d951SJohn Birrell 
8376ff6d951SJohn Birrell static const struct {
8386ff6d951SJohn Birrell 	const char *dtbp_name;
8396ff6d951SJohn Birrell 	int dtbp_policy;
8406ff6d951SJohn Birrell } _dtrace_bufpolicies[] = {
8416ff6d951SJohn Birrell 	{ "ring", DTRACEOPT_BUFPOLICY_RING },
8426ff6d951SJohn Birrell 	{ "fill", DTRACEOPT_BUFPOLICY_FILL },
8436ff6d951SJohn Birrell 	{ "switch", DTRACEOPT_BUFPOLICY_SWITCH },
8446ff6d951SJohn Birrell 	{ NULL, 0 }
8456ff6d951SJohn Birrell };
8466ff6d951SJohn Birrell 
8476ff6d951SJohn Birrell /*ARGSUSED*/
8486ff6d951SJohn Birrell static int
dt_opt_bufpolicy(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)8496ff6d951SJohn Birrell dt_opt_bufpolicy(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
8506ff6d951SJohn Birrell {
8516ff6d951SJohn Birrell 	dtrace_optval_t policy = DTRACEOPT_UNSET;
8526ff6d951SJohn Birrell 	int i;
8536ff6d951SJohn Birrell 
8546ff6d951SJohn Birrell 	if (arg == NULL)
8556ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
8566ff6d951SJohn Birrell 
8576ff6d951SJohn Birrell 	for (i = 0; _dtrace_bufpolicies[i].dtbp_name != NULL; i++) {
8586ff6d951SJohn Birrell 		if (strcmp(_dtrace_bufpolicies[i].dtbp_name, arg) == 0) {
8596ff6d951SJohn Birrell 			policy = _dtrace_bufpolicies[i].dtbp_policy;
8606ff6d951SJohn Birrell 			break;
8616ff6d951SJohn Birrell 		}
8626ff6d951SJohn Birrell 	}
8636ff6d951SJohn Birrell 
8646ff6d951SJohn Birrell 	if (policy == DTRACEOPT_UNSET)
8656ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
8666ff6d951SJohn Birrell 
8676ff6d951SJohn Birrell 	dtp->dt_options[DTRACEOPT_BUFPOLICY] = policy;
8686ff6d951SJohn Birrell 
8696ff6d951SJohn Birrell 	return (0);
8706ff6d951SJohn Birrell }
8716ff6d951SJohn Birrell 
8726ff6d951SJohn Birrell static const struct {
8736ff6d951SJohn Birrell 	const char *dtbr_name;
8746ff6d951SJohn Birrell 	int dtbr_policy;
8756ff6d951SJohn Birrell } _dtrace_bufresize[] = {
8766ff6d951SJohn Birrell 	{ "auto", DTRACEOPT_BUFRESIZE_AUTO },
8776ff6d951SJohn Birrell 	{ "manual", DTRACEOPT_BUFRESIZE_MANUAL },
8786ff6d951SJohn Birrell 	{ NULL, 0 }
8796ff6d951SJohn Birrell };
8806ff6d951SJohn Birrell 
8816ff6d951SJohn Birrell /*ARGSUSED*/
8826ff6d951SJohn Birrell static int
dt_opt_bufresize(dtrace_hdl_t * dtp,const char * arg,uintptr_t option)8836ff6d951SJohn Birrell dt_opt_bufresize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
8846ff6d951SJohn Birrell {
8856ff6d951SJohn Birrell 	dtrace_optval_t policy = DTRACEOPT_UNSET;
8866ff6d951SJohn Birrell 	int i;
8876ff6d951SJohn Birrell 
8886ff6d951SJohn Birrell 	if (arg == NULL)
8896ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
8906ff6d951SJohn Birrell 
8916ff6d951SJohn Birrell 	for (i = 0; _dtrace_bufresize[i].dtbr_name != NULL; i++) {
8926ff6d951SJohn Birrell 		if (strcmp(_dtrace_bufresize[i].dtbr_name, arg) == 0) {
8936ff6d951SJohn Birrell 			policy = _dtrace_bufresize[i].dtbr_policy;
8946ff6d951SJohn Birrell 			break;
8956ff6d951SJohn Birrell 		}
8966ff6d951SJohn Birrell 	}
8976ff6d951SJohn Birrell 
8986ff6d951SJohn Birrell 	if (policy == DTRACEOPT_UNSET)
8996ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
9006ff6d951SJohn Birrell 
9016ff6d951SJohn Birrell 	dtp->dt_options[DTRACEOPT_BUFRESIZE] = policy;
9026ff6d951SJohn Birrell 
9036ff6d951SJohn Birrell 	return (0);
9046ff6d951SJohn Birrell }
9056ff6d951SJohn Birrell 
9066ff6d951SJohn Birrell int
dt_options_load(dtrace_hdl_t * dtp)9076ff6d951SJohn Birrell dt_options_load(dtrace_hdl_t *dtp)
9086ff6d951SJohn Birrell {
9096ff6d951SJohn Birrell 	dof_hdr_t hdr, *dof;
9106ff6d951SJohn Birrell 	dof_sec_t *sec;
9116ff6d951SJohn Birrell 	size_t offs;
912fa19b250SRyan Libby 	int i, ret;
9136ff6d951SJohn Birrell 
9146ff6d951SJohn Birrell 	/*
9156ff6d951SJohn Birrell 	 * To load the option values, we need to ask the kernel to provide its
9166ff6d951SJohn Birrell 	 * DOF, which we'll sift through to look for OPTDESC sections.
9176ff6d951SJohn Birrell 	 */
918fa19b250SRyan Libby 	dof = &hdr;
9196ff6d951SJohn Birrell 	bzero(&hdr, sizeof (dof_hdr_t));
9206ff6d951SJohn Birrell 	hdr.dofh_loadsz = sizeof (dof_hdr_t);
9216ff6d951SJohn Birrell 
922bc96366cSSteven Hartland #ifdef illumos
923fa19b250SRyan Libby 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, dof) == -1)
924132df6e9SJohn Birrell #else
925132df6e9SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, &dof) == -1)
926132df6e9SJohn Birrell #endif
927fa19b250SRyan Libby 	{
928fa19b250SRyan Libby 		ret = dt_set_errno(dtp, errno);
929fa19b250SRyan Libby 		goto out;
930fa19b250SRyan Libby 	}
9316ff6d951SJohn Birrell 
932fa19b250SRyan Libby 	if (hdr.dofh_loadsz < sizeof (dof_hdr_t)) {
933fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EINVAL);
934fa19b250SRyan Libby 		goto out;
935fa19b250SRyan Libby 	}
9366ff6d951SJohn Birrell 
937fa19b250SRyan Libby 	if ((dof = dt_alloc(dtp, hdr.dofh_loadsz)) == NULL) {
938fa19b250SRyan Libby 		ret = dt_set_errno(dtp, EDT_NOMEM);
939fa19b250SRyan Libby 		goto out;
940fa19b250SRyan Libby 	}
9416ff6d951SJohn Birrell 	bzero(dof, sizeof (dof_hdr_t));
9426ff6d951SJohn Birrell 	dof->dofh_loadsz = hdr.dofh_loadsz;
9436ff6d951SJohn Birrell 
9446ff6d951SJohn Birrell 	for (i = 0; i < DTRACEOPT_MAX; i++)
9456ff6d951SJohn Birrell 		dtp->dt_options[i] = DTRACEOPT_UNSET;
9466ff6d951SJohn Birrell 
947bc96366cSSteven Hartland #ifdef illumos
9486ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, dof) == -1)
949132df6e9SJohn Birrell #else
950132df6e9SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, &dof) == -1)
951132df6e9SJohn Birrell #endif
952fa19b250SRyan Libby 	{
953fa19b250SRyan Libby 		ret = dt_set_errno(dtp, errno);
954fa19b250SRyan Libby 		goto out;
955fa19b250SRyan Libby 	}
9566ff6d951SJohn Birrell 
9576ff6d951SJohn Birrell 	for (i = 0; i < dof->dofh_secnum; i++) {
9586ff6d951SJohn Birrell 		sec = (dof_sec_t *)(uintptr_t)((uintptr_t)dof +
9596ff6d951SJohn Birrell 		    dof->dofh_secoff + i * dof->dofh_secsize);
9606ff6d951SJohn Birrell 
9616ff6d951SJohn Birrell 		if (sec->dofs_type != DOF_SECT_OPTDESC)
9626ff6d951SJohn Birrell 			continue;
9636ff6d951SJohn Birrell 
9646ff6d951SJohn Birrell 		break;
9656ff6d951SJohn Birrell 	}
9666ff6d951SJohn Birrell 
9676ff6d951SJohn Birrell 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
9686ff6d951SJohn Birrell 		dof_optdesc_t *opt = (dof_optdesc_t *)(uintptr_t)
9696ff6d951SJohn Birrell 		    ((uintptr_t)dof + sec->dofs_offset + offs);
9706ff6d951SJohn Birrell 
9716ff6d951SJohn Birrell 		if (opt->dofo_strtab != DOF_SECIDX_NONE)
9726ff6d951SJohn Birrell 			continue;
9736ff6d951SJohn Birrell 
9746ff6d951SJohn Birrell 		if (opt->dofo_option >= DTRACEOPT_MAX)
9756ff6d951SJohn Birrell 			continue;
9766ff6d951SJohn Birrell 
9776ff6d951SJohn Birrell 		dtp->dt_options[opt->dofo_option] = opt->dofo_value;
9786ff6d951SJohn Birrell 	}
9796ff6d951SJohn Birrell 
980fa19b250SRyan Libby 	ret = 0;
981fa19b250SRyan Libby out:
982fa19b250SRyan Libby 	if (dof != NULL && dof != &hdr)
983fa19b250SRyan Libby 		dt_free(dtp, dof);
984fa19b250SRyan Libby 	return (ret);
9856ff6d951SJohn Birrell }
9866ff6d951SJohn Birrell 
9876ff6d951SJohn Birrell typedef struct dt_option {
9886ff6d951SJohn Birrell 	const char *o_name;
9896ff6d951SJohn Birrell 	int (*o_func)(dtrace_hdl_t *, const char *, uintptr_t);
9906ff6d951SJohn Birrell 	uintptr_t o_option;
9916ff6d951SJohn Birrell } dt_option_t;
9926ff6d951SJohn Birrell 
9936ff6d951SJohn Birrell /*
9946ff6d951SJohn Birrell  * Compile-time options.
9956ff6d951SJohn Birrell  */
9966ff6d951SJohn Birrell static const dt_option_t _dtrace_ctoptions[] = {
9976ff6d951SJohn Birrell 	{ "aggpercpu", dt_opt_agg, DTRACE_A_PERCPU },
9986ff6d951SJohn Birrell 	{ "amin", dt_opt_amin },
9996ff6d951SJohn Birrell 	{ "argref", dt_opt_cflags, DTRACE_C_ARGREF },
10006ff6d951SJohn Birrell 	{ "core", dt_opt_core },
10016ff6d951SJohn Birrell 	{ "cpp", dt_opt_cflags, DTRACE_C_CPP },
10026ff6d951SJohn Birrell 	{ "cpphdrs", dt_opt_cpp_hdrs },
10036ff6d951SJohn Birrell 	{ "cpppath", dt_opt_cpp_path },
10046ff6d951SJohn Birrell 	{ "ctypes", dt_opt_ctypes },
10056ff6d951SJohn Birrell 	{ "defaultargs", dt_opt_cflags, DTRACE_C_DEFARG },
10066ff6d951SJohn Birrell 	{ "dtypes", dt_opt_dtypes },
10076ff6d951SJohn Birrell 	{ "debug", dt_opt_debug },
10086ff6d951SJohn Birrell 	{ "define", dt_opt_cpp_opts, (uintptr_t)"-D" },
10096ff6d951SJohn Birrell 	{ "droptags", dt_opt_droptags },
10106ff6d951SJohn Birrell 	{ "empty", dt_opt_cflags, DTRACE_C_EMPTY },
1011a43f0be9SRui Paulo 	{ "encoding", dt_opt_encoding },
10126ff6d951SJohn Birrell 	{ "errtags", dt_opt_cflags, DTRACE_C_ETAGS },
10136ff6d951SJohn Birrell 	{ "evaltime", dt_opt_evaltime },
10146ff6d951SJohn Birrell 	{ "incdir", dt_opt_cpp_opts, (uintptr_t)"-I" },
10156ff6d951SJohn Birrell 	{ "iregs", dt_opt_iregs },
10166ff6d951SJohn Birrell 	{ "kdefs", dt_opt_invcflags, DTRACE_C_KNODEF },
10176ff6d951SJohn Birrell 	{ "knodefs", dt_opt_cflags, DTRACE_C_KNODEF },
10186ff6d951SJohn Birrell 	{ "late", dt_opt_xlate },
10196ff6d951SJohn Birrell 	{ "lazyload", dt_opt_lazyload },
10206ff6d951SJohn Birrell 	{ "ldpath", dt_opt_ld_path },
10216ff6d951SJohn Birrell 	{ "libdir", dt_opt_libdir },
10226ff6d951SJohn Birrell 	{ "linkmode", dt_opt_linkmode },
10236ff6d951SJohn Birrell 	{ "linktype", dt_opt_linktype },
10246ff6d951SJohn Birrell 	{ "nolibs", dt_opt_cflags, DTRACE_C_NOLIBS },
102503a5f9f0SMark Johnston #ifdef __FreeBSD__
102603a5f9f0SMark Johnston 	{ "objcopypath", dt_opt_objcopy_path },
102703a5f9f0SMark Johnston #endif
10286ff6d951SJohn Birrell 	{ "pgmax", dt_opt_pgmax },
10296ff6d951SJohn Birrell 	{ "pspec", dt_opt_cflags, DTRACE_C_PSPEC },
103009e6105fSMark Johnston 	{ "setenv", dt_opt_setenv, 1 },
10316ff6d951SJohn Birrell 	{ "stdc", dt_opt_stdc },
10326ff6d951SJohn Birrell 	{ "strip", dt_opt_dflags, DTRACE_D_STRIP },
10336ff6d951SJohn Birrell 	{ "syslibdir", dt_opt_syslibdir },
10346ff6d951SJohn Birrell 	{ "tree", dt_opt_tree },
10356ff6d951SJohn Birrell 	{ "tregs", dt_opt_tregs },
10366ff6d951SJohn Birrell 	{ "udefs", dt_opt_invcflags, DTRACE_C_UNODEF },
10376ff6d951SJohn Birrell 	{ "undef", dt_opt_cpp_opts, (uintptr_t)"-U" },
10386ff6d951SJohn Birrell 	{ "unodefs", dt_opt_cflags, DTRACE_C_UNODEF },
103909e6105fSMark Johnston 	{ "unsetenv", dt_opt_setenv, 0 },
10406ff6d951SJohn Birrell 	{ "verbose", dt_opt_cflags, DTRACE_C_DIFV },
10416ff6d951SJohn Birrell 	{ "version", dt_opt_version },
10426ff6d951SJohn Birrell 	{ "zdefs", dt_opt_cflags, DTRACE_C_ZDEFS },
1043132df6e9SJohn Birrell 	{ NULL, NULL, 0 }
10446ff6d951SJohn Birrell };
10456ff6d951SJohn Birrell 
10466ff6d951SJohn Birrell /*
10476ff6d951SJohn Birrell  * Run-time options.
10486ff6d951SJohn Birrell  */
10496ff6d951SJohn Birrell static const dt_option_t _dtrace_rtoptions[] = {
10506ff6d951SJohn Birrell 	{ "aggsize", dt_opt_size, DTRACEOPT_AGGSIZE },
10516ff6d951SJohn Birrell 	{ "bufsize", dt_opt_size, DTRACEOPT_BUFSIZE },
10526ff6d951SJohn Birrell 	{ "bufpolicy", dt_opt_bufpolicy, DTRACEOPT_BUFPOLICY },
10536ff6d951SJohn Birrell 	{ "bufresize", dt_opt_bufresize, DTRACEOPT_BUFRESIZE },
10546ff6d951SJohn Birrell 	{ "cleanrate", dt_opt_rate, DTRACEOPT_CLEANRATE },
10556ff6d951SJohn Birrell 	{ "cpu", dt_opt_runtime, DTRACEOPT_CPU },
10566ff6d951SJohn Birrell 	{ "destructive", dt_opt_runtime, DTRACEOPT_DESTRUCTIVE },
10576ff6d951SJohn Birrell 	{ "dynvarsize", dt_opt_size, DTRACEOPT_DYNVARSIZE },
10586ff6d951SJohn Birrell 	{ "grabanon", dt_opt_runtime, DTRACEOPT_GRABANON },
10596ff6d951SJohn Birrell 	{ "jstackframes", dt_opt_runtime, DTRACEOPT_JSTACKFRAMES },
10606ff6d951SJohn Birrell 	{ "jstackstrsize", dt_opt_size, DTRACEOPT_JSTACKSTRSIZE },
10616ff6d951SJohn Birrell 	{ "nspec", dt_opt_runtime, DTRACEOPT_NSPEC },
1062*93f27766SDomagoj Stolfa 	{ "oformat", dt_opt_oformat, 0 },
10636ff6d951SJohn Birrell 	{ "specsize", dt_opt_size, DTRACEOPT_SPECSIZE },
10646ff6d951SJohn Birrell 	{ "stackframes", dt_opt_runtime, DTRACEOPT_STACKFRAMES },
10656ff6d951SJohn Birrell 	{ "statusrate", dt_opt_rate, DTRACEOPT_STATUSRATE },
10666ff6d951SJohn Birrell 	{ "strsize", dt_opt_strsize, DTRACEOPT_STRSIZE },
10676ff6d951SJohn Birrell 	{ "ustackframes", dt_opt_runtime, DTRACEOPT_USTACKFRAMES },
106809e6105fSMark Johnston 	{ "temporal", dt_opt_runtime, DTRACEOPT_TEMPORAL },
1069132df6e9SJohn Birrell 	{ NULL, NULL, 0 }
10706ff6d951SJohn Birrell };
10716ff6d951SJohn Birrell 
10726ff6d951SJohn Birrell /*
10736ff6d951SJohn Birrell  * Dynamic run-time options.
10746ff6d951SJohn Birrell  */
10756ff6d951SJohn Birrell static const dt_option_t _dtrace_drtoptions[] = {
1076a43f0be9SRui Paulo 	{ "agghist", dt_opt_runtime, DTRACEOPT_AGGHIST },
1077a43f0be9SRui Paulo 	{ "aggpack", dt_opt_runtime, DTRACEOPT_AGGPACK },
10786ff6d951SJohn Birrell 	{ "aggrate", dt_opt_rate, DTRACEOPT_AGGRATE },
10796ff6d951SJohn Birrell 	{ "aggsortkey", dt_opt_runtime, DTRACEOPT_AGGSORTKEY },
10806ff6d951SJohn Birrell 	{ "aggsortkeypos", dt_opt_runtime, DTRACEOPT_AGGSORTKEYPOS },
10816ff6d951SJohn Birrell 	{ "aggsortpos", dt_opt_runtime, DTRACEOPT_AGGSORTPOS },
10826ff6d951SJohn Birrell 	{ "aggsortrev", dt_opt_runtime, DTRACEOPT_AGGSORTREV },
1083a43f0be9SRui Paulo 	{ "aggzoom", dt_opt_runtime, DTRACEOPT_AGGZOOM },
10846ff6d951SJohn Birrell 	{ "flowindent", dt_opt_runtime, DTRACEOPT_FLOWINDENT },
10856ff6d951SJohn Birrell 	{ "quiet", dt_opt_runtime, DTRACEOPT_QUIET },
10866ff6d951SJohn Birrell 	{ "rawbytes", dt_opt_runtime, DTRACEOPT_RAWBYTES },
10876ff6d951SJohn Birrell 	{ "stackindent", dt_opt_runtime, DTRACEOPT_STACKINDENT },
10886ff6d951SJohn Birrell 	{ "switchrate", dt_opt_rate, DTRACEOPT_SWITCHRATE },
1089132df6e9SJohn Birrell 	{ NULL, NULL, 0 }
10906ff6d951SJohn Birrell };
10916ff6d951SJohn Birrell 
10926ff6d951SJohn Birrell int
dtrace_getopt(dtrace_hdl_t * dtp,const char * opt,dtrace_optval_t * val)10936ff6d951SJohn Birrell dtrace_getopt(dtrace_hdl_t *dtp, const char *opt, dtrace_optval_t *val)
10946ff6d951SJohn Birrell {
10956ff6d951SJohn Birrell 	const dt_option_t *op;
10966ff6d951SJohn Birrell 
10976ff6d951SJohn Birrell 	if (opt == NULL)
10986ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EINVAL));
10996ff6d951SJohn Birrell 
11006ff6d951SJohn Birrell 	/*
11016ff6d951SJohn Birrell 	 * We only need to search the run-time options -- it's not legal
11026ff6d951SJohn Birrell 	 * to get the values of compile-time options.
11036ff6d951SJohn Birrell 	 */
11046ff6d951SJohn Birrell 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
11056ff6d951SJohn Birrell 		if (strcmp(op->o_name, opt) == 0) {
11066ff6d951SJohn Birrell 			*val = dtp->dt_options[op->o_option];
11076ff6d951SJohn Birrell 			return (0);
11086ff6d951SJohn Birrell 		}
11096ff6d951SJohn Birrell 	}
11106ff6d951SJohn Birrell 
11116ff6d951SJohn Birrell 	for (op = _dtrace_drtoptions; op->o_name != NULL; op++) {
11126ff6d951SJohn Birrell 		if (strcmp(op->o_name, opt) == 0) {
11136ff6d951SJohn Birrell 			*val = dtp->dt_options[op->o_option];
11146ff6d951SJohn Birrell 			return (0);
11156ff6d951SJohn Birrell 		}
11166ff6d951SJohn Birrell 	}
11176ff6d951SJohn Birrell 
11186ff6d951SJohn Birrell 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
11196ff6d951SJohn Birrell }
11206ff6d951SJohn Birrell 
11216ff6d951SJohn Birrell int
dtrace_setopt(dtrace_hdl_t * dtp,const char * opt,const char * val)11226ff6d951SJohn Birrell dtrace_setopt(dtrace_hdl_t *dtp, const char *opt, const char *val)
11236ff6d951SJohn Birrell {
11246ff6d951SJohn Birrell 	const dt_option_t *op;
11256ff6d951SJohn Birrell 
11266ff6d951SJohn Birrell 	if (opt == NULL)
11276ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EINVAL));
11286ff6d951SJohn Birrell 
11296ff6d951SJohn Birrell 	for (op = _dtrace_ctoptions; op->o_name != NULL; op++) {
11306ff6d951SJohn Birrell 		if (strcmp(op->o_name, opt) == 0)
11316ff6d951SJohn Birrell 			return (op->o_func(dtp, val, op->o_option));
11326ff6d951SJohn Birrell 	}
11336ff6d951SJohn Birrell 
11346ff6d951SJohn Birrell 	for (op = _dtrace_drtoptions; op->o_name != NULL; op++) {
11356ff6d951SJohn Birrell 		if (strcmp(op->o_name, opt) == 0)
11366ff6d951SJohn Birrell 			return (op->o_func(dtp, val, op->o_option));
11376ff6d951SJohn Birrell 	}
11386ff6d951SJohn Birrell 
11396ff6d951SJohn Birrell 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
11406ff6d951SJohn Birrell 		if (strcmp(op->o_name, opt) == 0) {
11416ff6d951SJohn Birrell 			/*
11426ff6d951SJohn Birrell 			 * Only dynamic run-time options may be set while
11436ff6d951SJohn Birrell 			 * tracing is active.
11446ff6d951SJohn Birrell 			 */
11456ff6d951SJohn Birrell 			if (dtp->dt_active)
11466ff6d951SJohn Birrell 				return (dt_set_errno(dtp, EDT_ACTIVE));
11476ff6d951SJohn Birrell 
11486ff6d951SJohn Birrell 			return (op->o_func(dtp, val, op->o_option));
11496ff6d951SJohn Birrell 		}
11506ff6d951SJohn Birrell 	}
11516ff6d951SJohn Birrell 
11526ff6d951SJohn Birrell 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
11536ff6d951SJohn Birrell }
1154