1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/resource.h>
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <strings.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate #include <limits.h>
37*0Sstevel@tonic-gate #include <alloca.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <fcntl.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <dt_impl.h>
42*0Sstevel@tonic-gate #include <dt_string.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static int
45*0Sstevel@tonic-gate dt_opt_agg(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	if (arg != NULL)
50*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	agp->dtat_flags |= option;
53*0Sstevel@tonic-gate 	return (0);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*ARGSUSED*/
57*0Sstevel@tonic-gate static int
58*0Sstevel@tonic-gate dt_opt_amin(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	char str[DTRACE_ATTR2STR_MAX];
61*0Sstevel@tonic-gate 	dtrace_attribute_t attr;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if (arg == NULL || dtrace_str2attr(arg, &attr) == -1)
64*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	dt_dprintf("set compiler attribute minimum to %s\n",
67*0Sstevel@tonic-gate 	    dtrace_attr2str(attr, str, sizeof (str)));
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL) {
70*0Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags |= DTRACE_C_EATTR;
71*0Sstevel@tonic-gate 		dtp->dt_pcb->pcb_amin = attr;
72*0Sstevel@tonic-gate 	} else {
73*0Sstevel@tonic-gate 		dtp->dt_cflags |= DTRACE_C_EATTR;
74*0Sstevel@tonic-gate 		dtp->dt_amin = attr;
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	return (0);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static void
81*0Sstevel@tonic-gate dt_coredump(void)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	const char msg[] = "libdtrace DEBUG: [ forcing coredump ]\n";
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	struct sigaction act;
86*0Sstevel@tonic-gate 	struct rlimit lim;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	(void) write(STDERR_FILENO, msg, sizeof (msg) - 1);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
91*0Sstevel@tonic-gate 	act.sa_flags = 0;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
94*0Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	lim.rlim_cur = RLIM_INFINITY;
97*0Sstevel@tonic-gate 	lim.rlim_max = RLIM_INFINITY;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_CORE, &lim);
100*0Sstevel@tonic-gate 	abort();
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate /*ARGSUSED*/
104*0Sstevel@tonic-gate static int
105*0Sstevel@tonic-gate dt_opt_core(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
106*0Sstevel@tonic-gate {
107*0Sstevel@tonic-gate 	static int enabled = 0;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (arg != NULL)
110*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if (enabled++ || atexit(dt_coredump) == 0)
113*0Sstevel@tonic-gate 		return (0);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	return (dt_set_errno(dtp, errno));
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*ARGSUSED*/
119*0Sstevel@tonic-gate static int
120*0Sstevel@tonic-gate dt_opt_cpp_hdrs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	if (arg != NULL)
123*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
126*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, "-H") == NULL)
129*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	return (0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /*ARGSUSED*/
135*0Sstevel@tonic-gate static int
136*0Sstevel@tonic-gate dt_opt_cpp_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	char *cpp;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (arg == NULL)
141*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
144*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if ((cpp = strdup(arg)) == NULL)
147*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	dtp->dt_cpp_argv[0] = (char *)strbasename(cpp);
150*0Sstevel@tonic-gate 	free(dtp->dt_cpp_path);
151*0Sstevel@tonic-gate 	dtp->dt_cpp_path = cpp;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	return (0);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static int
157*0Sstevel@tonic-gate dt_opt_cpp_opts(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	char *buf;
160*0Sstevel@tonic-gate 	size_t len;
161*0Sstevel@tonic-gate 	const char *opt = (const char *)option;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if (opt == NULL || arg == NULL)
164*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
167*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	len = strlen(opt) + strlen(arg) + 1;
170*0Sstevel@tonic-gate 	buf = alloca(len);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	(void) strcpy(buf, opt);
173*0Sstevel@tonic-gate 	(void) strcat(buf, arg);
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (dt_cpp_add_arg(dtp, buf) == NULL)
176*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	return (0);
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*ARGSUSED*/
182*0Sstevel@tonic-gate static int
183*0Sstevel@tonic-gate dt_opt_ctypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	int fd;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (arg == NULL)
188*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
191*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	(void) close(dtp->dt_cdefs_fd);
194*0Sstevel@tonic-gate 	dtp->dt_cdefs_fd = fd;
195*0Sstevel@tonic-gate 	return (0);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /*ARGSUSED*/
199*0Sstevel@tonic-gate static int
200*0Sstevel@tonic-gate dt_opt_dtypes(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	int fd;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (arg == NULL)
205*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if ((fd = open64(arg, O_CREAT | O_WRONLY, 0666)) == -1)
208*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	(void) close(dtp->dt_ddefs_fd);
211*0Sstevel@tonic-gate 	dtp->dt_ddefs_fd = fd;
212*0Sstevel@tonic-gate 	return (0);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*ARGSUSED*/
216*0Sstevel@tonic-gate static int
217*0Sstevel@tonic-gate dt_opt_debug(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	if (arg != NULL)
220*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	_dtrace_debug = 1;
223*0Sstevel@tonic-gate 	return (0);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*ARGSUSED*/
227*0Sstevel@tonic-gate static int
228*0Sstevel@tonic-gate dt_opt_iregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate 	int n;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) <= 0)
233*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	dtp->dt_conf.dtc_difintregs = n;
236*0Sstevel@tonic-gate 	return (0);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate /*ARGSUSED*/
240*0Sstevel@tonic-gate static int
241*0Sstevel@tonic-gate dt_opt_lazyload(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	dtp->dt_lazyload = 1;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	return (0);
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*ARGSUSED*/
249*0Sstevel@tonic-gate static int
250*0Sstevel@tonic-gate dt_opt_ld_path(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	char *ld;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	if (arg == NULL)
255*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
258*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	if ((ld = strdup(arg)) == NULL)
261*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	free(dtp->dt_ld_path);
264*0Sstevel@tonic-gate 	dtp->dt_ld_path = ld;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	return (0);
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate /*ARGSUSED*/
270*0Sstevel@tonic-gate static int
271*0Sstevel@tonic-gate dt_opt_libdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
272*0Sstevel@tonic-gate {
273*0Sstevel@tonic-gate 	dt_dirpath_t *dp;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (arg == NULL)
276*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if ((dp = malloc(sizeof (dt_dirpath_t))) == NULL ||
279*0Sstevel@tonic-gate 	    (dp->dir_path = strdup(arg)) == NULL) {
280*0Sstevel@tonic-gate 		free(dp);
281*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	dt_list_append(&dtp->dt_lib_path, dp);
285*0Sstevel@tonic-gate 	return (0);
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /*ARGSUSED*/
289*0Sstevel@tonic-gate static int
290*0Sstevel@tonic-gate dt_opt_linkmode(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	if (arg == NULL)
293*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if (strcmp(arg, "kernel") == 0)
296*0Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_KERNEL;
297*0Sstevel@tonic-gate 	else if (strcmp(arg, "primary") == 0)
298*0Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_PRIMARY;
299*0Sstevel@tonic-gate 	else if (strcmp(arg, "dynamic") == 0)
300*0Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_DYNAMIC;
301*0Sstevel@tonic-gate 	else if (strcmp(arg, "static") == 0)
302*0Sstevel@tonic-gate 		dtp->dt_linkmode = DT_LINK_STATIC;
303*0Sstevel@tonic-gate 	else
304*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	return (0);
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*ARGSUSED*/
310*0Sstevel@tonic-gate static int
311*0Sstevel@tonic-gate dt_opt_linktype(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
312*0Sstevel@tonic-gate {
313*0Sstevel@tonic-gate 	if (arg == NULL)
314*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (strcasecmp(arg, "elf") == 0)
317*0Sstevel@tonic-gate 		dtp->dt_linktype = DT_LTYP_ELF;
318*0Sstevel@tonic-gate 	else if (strcasecmp(arg, "dof") == 0)
319*0Sstevel@tonic-gate 		dtp->dt_linktype = DT_LTYP_DOF;
320*0Sstevel@tonic-gate 	else
321*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	return (0);
324*0Sstevel@tonic-gate }
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate /*ARGSUSED*/
327*0Sstevel@tonic-gate static int
328*0Sstevel@tonic-gate dt_opt_evaltime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	if (arg == NULL)
331*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	if (strcmp(arg, "exec") == 0)
334*0Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_CREATE;
335*0Sstevel@tonic-gate 	else if (strcmp(arg, "preinit") == 0)
336*0Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_PREINIT;
337*0Sstevel@tonic-gate 	else if (strcmp(arg, "postinit") == 0)
338*0Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_POSTINIT;
339*0Sstevel@tonic-gate 	else if (strcmp(arg, "main") == 0)
340*0Sstevel@tonic-gate 		dtp->dt_prcmode = DT_PROC_STOP_MAIN;
341*0Sstevel@tonic-gate 	else
342*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	return (0);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate /*ARGSUSED*/
348*0Sstevel@tonic-gate static int
349*0Sstevel@tonic-gate dt_opt_pgmax(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
350*0Sstevel@tonic-gate {
351*0Sstevel@tonic-gate 	int n;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) < 0)
354*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	dtp->dt_procs->dph_lrulim = n;
357*0Sstevel@tonic-gate 	return (0);
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate /*ARGSUSED*/
361*0Sstevel@tonic-gate static int
362*0Sstevel@tonic-gate dt_opt_stdc(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	if (arg == NULL)
365*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
368*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTCTX));
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	if (strcmp(arg, "a") == 0)
371*0Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XA;
372*0Sstevel@tonic-gate 	else if (strcmp(arg, "c") == 0)
373*0Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XC;
374*0Sstevel@tonic-gate 	else if (strcmp(arg, "s") == 0)
375*0Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XS;
376*0Sstevel@tonic-gate 	else if (strcmp(arg, "t") == 0)
377*0Sstevel@tonic-gate 		dtp->dt_stdcmode = DT_STDC_XT;
378*0Sstevel@tonic-gate 	else
379*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	return (0);
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate /*ARGSUSED*/
385*0Sstevel@tonic-gate static int
386*0Sstevel@tonic-gate dt_opt_syslibdir(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
387*0Sstevel@tonic-gate {
388*0Sstevel@tonic-gate 	dt_dirpath_t *dp = dt_list_next(&dtp->dt_lib_path);
389*0Sstevel@tonic-gate 	char *path;
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (arg == NULL)
392*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	if ((path = strdup(arg)) == NULL)
395*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	free(dp->dir_path);
398*0Sstevel@tonic-gate 	dp->dir_path = path;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	return (0);
401*0Sstevel@tonic-gate }
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate /*ARGSUSED*/
405*0Sstevel@tonic-gate static int
406*0Sstevel@tonic-gate dt_opt_tree(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
407*0Sstevel@tonic-gate {
408*0Sstevel@tonic-gate 	int m;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	if (arg == NULL || (m = atoi(arg)) <= 0)
411*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	dtp->dt_treedump = m;
414*0Sstevel@tonic-gate 	return (0);
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*ARGSUSED*/
418*0Sstevel@tonic-gate static int
419*0Sstevel@tonic-gate dt_opt_tregs(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
420*0Sstevel@tonic-gate {
421*0Sstevel@tonic-gate 	int n;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	if (arg == NULL || (n = atoi(arg)) <= 0)
424*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	dtp->dt_conf.dtc_diftupregs = n;
427*0Sstevel@tonic-gate 	return (0);
428*0Sstevel@tonic-gate }
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate static int
431*0Sstevel@tonic-gate dt_opt_cflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	if (arg != NULL)
434*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
437*0Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags |= option;
438*0Sstevel@tonic-gate 	else
439*0Sstevel@tonic-gate 		dtp->dt_cflags |= option;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	return (0);
442*0Sstevel@tonic-gate }
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate static int
445*0Sstevel@tonic-gate dt_opt_dflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
446*0Sstevel@tonic-gate {
447*0Sstevel@tonic-gate 	if (arg != NULL)
448*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	dtp->dt_dflags |= option;
451*0Sstevel@tonic-gate 	return (0);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate static int
455*0Sstevel@tonic-gate dt_opt_invcflags(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
456*0Sstevel@tonic-gate {
457*0Sstevel@tonic-gate 	if (arg != NULL)
458*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	if (dtp->dt_pcb != NULL)
461*0Sstevel@tonic-gate 		dtp->dt_pcb->pcb_cflags &= ~option;
462*0Sstevel@tonic-gate 	else
463*0Sstevel@tonic-gate 		dtp->dt_cflags &= ~option;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	return (0);
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate /*ARGSUSED*/
469*0Sstevel@tonic-gate static int
470*0Sstevel@tonic-gate dt_opt_version(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
471*0Sstevel@tonic-gate {
472*0Sstevel@tonic-gate 	dt_version_t v;
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	if (arg == NULL)
475*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if (dt_version_str2num(arg, &v) == -1)
478*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSINVAL));
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	if (!dt_version_defined(v))
481*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_VERSUNDEF));
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	return (dt_reduce(dtp, v));
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate static int
487*0Sstevel@tonic-gate dt_opt_runtime(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
488*0Sstevel@tonic-gate {
489*0Sstevel@tonic-gate 	char *end;
490*0Sstevel@tonic-gate 	dtrace_optval_t val = 0;
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 	if (arg != NULL) {
493*0Sstevel@tonic-gate 		errno = 0;
494*0Sstevel@tonic-gate 		val = strtoull(arg, &end, 0);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		if (*end != '\0' || errno != 0 || val < 0)
497*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
498*0Sstevel@tonic-gate 	}
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	dtp->dt_options[option] = val;
501*0Sstevel@tonic-gate 	return (0);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static int
505*0Sstevel@tonic-gate dt_opt_size(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	char *end;
508*0Sstevel@tonic-gate 	int len;
509*0Sstevel@tonic-gate 	dtrace_optval_t mul = 1, val = 0;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	if (arg != NULL) {
512*0Sstevel@tonic-gate 		len = strlen(arg);
513*0Sstevel@tonic-gate 		errno = 0;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 		switch (arg[len - 1]) {
516*0Sstevel@tonic-gate 		case 't':
517*0Sstevel@tonic-gate 		case 'T':
518*0Sstevel@tonic-gate 			mul *= 1024;
519*0Sstevel@tonic-gate 			/*FALLTHRU*/
520*0Sstevel@tonic-gate 		case 'g':
521*0Sstevel@tonic-gate 		case 'G':
522*0Sstevel@tonic-gate 			mul *= 1024;
523*0Sstevel@tonic-gate 			/*FALLTHRU*/
524*0Sstevel@tonic-gate 		case 'm':
525*0Sstevel@tonic-gate 		case 'M':
526*0Sstevel@tonic-gate 			mul *= 1024;
527*0Sstevel@tonic-gate 			/*FALLTHRU*/
528*0Sstevel@tonic-gate 		case 'k':
529*0Sstevel@tonic-gate 		case 'K':
530*0Sstevel@tonic-gate 			mul *= 1024;
531*0Sstevel@tonic-gate 			/*FALLTHRU*/
532*0Sstevel@tonic-gate 		default:
533*0Sstevel@tonic-gate 			break;
534*0Sstevel@tonic-gate 		}
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 		val = strtoull(arg, &end, 0) * mul;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 		if ((mul > 1 && end != &arg[len - 1]) ||
539*0Sstevel@tonic-gate 		    (mul == 1 && *end != '\0') || val < 0 ||
540*0Sstevel@tonic-gate 		    errno != 0 || val == DTRACEOPT_UNSET)
541*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	dtp->dt_options[option] = val;
545*0Sstevel@tonic-gate 	return (0);
546*0Sstevel@tonic-gate }
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate static int
549*0Sstevel@tonic-gate dt_opt_rate(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
550*0Sstevel@tonic-gate {
551*0Sstevel@tonic-gate 	char *end;
552*0Sstevel@tonic-gate 	int i;
553*0Sstevel@tonic-gate 	dtrace_optval_t mul = 1, val = 0;
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	const struct {
556*0Sstevel@tonic-gate 		char *name;
557*0Sstevel@tonic-gate 		hrtime_t mul;
558*0Sstevel@tonic-gate 	} suffix[] = {
559*0Sstevel@tonic-gate 		{ "ns", 	NANOSEC / NANOSEC },
560*0Sstevel@tonic-gate 		{ "nsec",	NANOSEC / NANOSEC },
561*0Sstevel@tonic-gate 		{ "us",		NANOSEC / MICROSEC },
562*0Sstevel@tonic-gate 		{ "usec",	NANOSEC / MICROSEC },
563*0Sstevel@tonic-gate 		{ "ms",		NANOSEC / MILLISEC },
564*0Sstevel@tonic-gate 		{ "msec",	NANOSEC / MILLISEC },
565*0Sstevel@tonic-gate 		{ "s",		NANOSEC / SEC },
566*0Sstevel@tonic-gate 		{ "sec",	NANOSEC / SEC },
567*0Sstevel@tonic-gate 		{ "m",		NANOSEC * (hrtime_t)60 },
568*0Sstevel@tonic-gate 		{ "min",	NANOSEC * (hrtime_t)60 },
569*0Sstevel@tonic-gate 		{ "h",		NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
570*0Sstevel@tonic-gate 		{ "hour",	NANOSEC * (hrtime_t)60 * (hrtime_t)60 },
571*0Sstevel@tonic-gate 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
572*0Sstevel@tonic-gate 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
573*0Sstevel@tonic-gate 		{ "hz",		0 },
574*0Sstevel@tonic-gate 		{ NULL }
575*0Sstevel@tonic-gate 	};
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	if (arg != NULL) {
578*0Sstevel@tonic-gate 		errno = 0;
579*0Sstevel@tonic-gate 		val = strtoull(arg, &end, 0);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		for (i = 0; suffix[i].name != NULL; i++) {
582*0Sstevel@tonic-gate 			if (strcasecmp(suffix[i].name, end) == 0) {
583*0Sstevel@tonic-gate 				mul = suffix[i].mul;
584*0Sstevel@tonic-gate 				break;
585*0Sstevel@tonic-gate 			}
586*0Sstevel@tonic-gate 		}
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 		if (suffix[i].name == NULL && *end != '\0' || val < 0)
589*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADOPTVAL));
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 		if (mul == 0) {
592*0Sstevel@tonic-gate 			/*
593*0Sstevel@tonic-gate 			 * The rate has been specified in frequency-per-second.
594*0Sstevel@tonic-gate 			 */
595*0Sstevel@tonic-gate 			if (val != 0)
596*0Sstevel@tonic-gate 				val = NANOSEC / val;
597*0Sstevel@tonic-gate 		} else {
598*0Sstevel@tonic-gate 			val *= mul;
599*0Sstevel@tonic-gate 		}
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	dtp->dt_options[option] = val;
603*0Sstevel@tonic-gate 	return (0);
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate /*
607*0Sstevel@tonic-gate  * When setting the strsize option, set the option in the dt_options array
608*0Sstevel@tonic-gate  * using dt_opt_size() as usual, and then update the definition of the CTF
609*0Sstevel@tonic-gate  * type for the D intrinsic "string" to be an array of the corresponding size.
610*0Sstevel@tonic-gate  * If any errors occur, reset dt_options[option] to its previous value.
611*0Sstevel@tonic-gate  */
612*0Sstevel@tonic-gate static int
613*0Sstevel@tonic-gate dt_opt_strsize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
614*0Sstevel@tonic-gate {
615*0Sstevel@tonic-gate 	dtrace_optval_t val = dtp->dt_options[option];
616*0Sstevel@tonic-gate 	ctf_file_t *fp = DT_STR_CTFP(dtp);
617*0Sstevel@tonic-gate 	ctf_id_t type = ctf_type_resolve(fp, DT_STR_TYPE(dtp));
618*0Sstevel@tonic-gate 	ctf_arinfo_t r;
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	if (dt_opt_size(dtp, arg, option) != 0)
621*0Sstevel@tonic-gate 		return (-1); /* dt_errno is set for us */
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if (dtp->dt_options[option] > UINT_MAX) {
624*0Sstevel@tonic-gate 		dtp->dt_options[option] = val;
625*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EOVERFLOW));
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	if (ctf_array_info(fp, type, &r) == CTF_ERR) {
629*0Sstevel@tonic-gate 		dtp->dt_options[option] = val;
630*0Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(fp);
631*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
632*0Sstevel@tonic-gate 	}
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	r.ctr_nelems = (uint_t)dtp->dt_options[option];
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	if (ctf_set_array(fp, type, &r) == CTF_ERR ||
637*0Sstevel@tonic-gate 	    ctf_update(fp) == CTF_ERR) {
638*0Sstevel@tonic-gate 		dtp->dt_options[option] = val;
639*0Sstevel@tonic-gate 		dtp->dt_ctferr = ctf_errno(fp);
640*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_CTF));
641*0Sstevel@tonic-gate 	}
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	return (0);
644*0Sstevel@tonic-gate }
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate static const struct {
647*0Sstevel@tonic-gate 	const char *dtbp_name;
648*0Sstevel@tonic-gate 	int dtbp_policy;
649*0Sstevel@tonic-gate } _dtrace_bufpolicies[] = {
650*0Sstevel@tonic-gate 	{ "ring", DTRACEOPT_BUFPOLICY_RING },
651*0Sstevel@tonic-gate 	{ "fill", DTRACEOPT_BUFPOLICY_FILL },
652*0Sstevel@tonic-gate 	{ "switch", DTRACEOPT_BUFPOLICY_SWITCH },
653*0Sstevel@tonic-gate 	{ NULL, 0 }
654*0Sstevel@tonic-gate };
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate /*ARGSUSED*/
657*0Sstevel@tonic-gate static int
658*0Sstevel@tonic-gate dt_opt_bufpolicy(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
659*0Sstevel@tonic-gate {
660*0Sstevel@tonic-gate 	dtrace_optval_t policy = DTRACEOPT_UNSET;
661*0Sstevel@tonic-gate 	int i;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	if (arg == NULL)
664*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	for (i = 0; _dtrace_bufpolicies[i].dtbp_name != NULL; i++) {
667*0Sstevel@tonic-gate 		if (strcmp(_dtrace_bufpolicies[i].dtbp_name, arg) == 0) {
668*0Sstevel@tonic-gate 			policy = _dtrace_bufpolicies[i].dtbp_policy;
669*0Sstevel@tonic-gate 			break;
670*0Sstevel@tonic-gate 		}
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	if (policy == DTRACEOPT_UNSET)
674*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_BUFPOLICY] = policy;
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	return (0);
679*0Sstevel@tonic-gate }
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate static const struct {
682*0Sstevel@tonic-gate 	const char *dtbr_name;
683*0Sstevel@tonic-gate 	int dtbr_policy;
684*0Sstevel@tonic-gate } _dtrace_bufresize[] = {
685*0Sstevel@tonic-gate 	{ "auto", DTRACEOPT_BUFRESIZE_AUTO },
686*0Sstevel@tonic-gate 	{ "manual", DTRACEOPT_BUFRESIZE_MANUAL },
687*0Sstevel@tonic-gate 	{ NULL, 0 }
688*0Sstevel@tonic-gate };
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate /*ARGSUSED*/
691*0Sstevel@tonic-gate static int
692*0Sstevel@tonic-gate dt_opt_bufresize(dtrace_hdl_t *dtp, const char *arg, uintptr_t option)
693*0Sstevel@tonic-gate {
694*0Sstevel@tonic-gate 	dtrace_optval_t policy = DTRACEOPT_UNSET;
695*0Sstevel@tonic-gate 	int i;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	if (arg == NULL)
698*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	for (i = 0; _dtrace_bufresize[i].dtbr_name != NULL; i++) {
701*0Sstevel@tonic-gate 		if (strcmp(_dtrace_bufresize[i].dtbr_name, arg) == 0) {
702*0Sstevel@tonic-gate 			policy = _dtrace_bufresize[i].dtbr_policy;
703*0Sstevel@tonic-gate 			break;
704*0Sstevel@tonic-gate 		}
705*0Sstevel@tonic-gate 	}
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	if (policy == DTRACEOPT_UNSET)
708*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADOPTVAL));
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	dtp->dt_options[DTRACEOPT_BUFRESIZE] = policy;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	return (0);
713*0Sstevel@tonic-gate }
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate int
716*0Sstevel@tonic-gate dt_options_load(dtrace_hdl_t *dtp)
717*0Sstevel@tonic-gate {
718*0Sstevel@tonic-gate 	dof_hdr_t hdr, *dof;
719*0Sstevel@tonic-gate 	dof_sec_t *sec;
720*0Sstevel@tonic-gate 	size_t offs;
721*0Sstevel@tonic-gate 	int i;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	/*
724*0Sstevel@tonic-gate 	 * To load the option values, we need to ask the kernel to provide its
725*0Sstevel@tonic-gate 	 * DOF, which we'll sift through to look for OPTDESC sections.
726*0Sstevel@tonic-gate 	 */
727*0Sstevel@tonic-gate 	bzero(&hdr, sizeof (dof_hdr_t));
728*0Sstevel@tonic-gate 	hdr.dofh_loadsz = sizeof (dof_hdr_t);
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, &hdr) == -1)
731*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 	if (hdr.dofh_loadsz < sizeof (dof_hdr_t))
734*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 	dof = alloca(hdr.dofh_loadsz);
737*0Sstevel@tonic-gate 	bzero(dof, sizeof (dof_hdr_t));
738*0Sstevel@tonic-gate 	dof->dofh_loadsz = hdr.dofh_loadsz;
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	for (i = 0; i < DTRACEOPT_MAX; i++)
741*0Sstevel@tonic-gate 		dtp->dt_options[i] = DTRACEOPT_UNSET;
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_DOFGET, dof) == -1)
744*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	for (i = 0; i < dof->dofh_secnum; i++) {
747*0Sstevel@tonic-gate 		sec = (dof_sec_t *)((uintptr_t)dof +
748*0Sstevel@tonic-gate 		    dof->dofh_secoff + i * dof->dofh_secsize);
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 		if (sec->dofs_type != DOF_SECT_OPTDESC)
751*0Sstevel@tonic-gate 			continue;
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 		break;
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
757*0Sstevel@tonic-gate 		dof_optdesc_t *opt = (dof_optdesc_t *)((uintptr_t)dof +
758*0Sstevel@tonic-gate 		    sec->dofs_offset + offs);
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 		if (opt->dofo_strtab != DOF_SECIDX_NONE)
761*0Sstevel@tonic-gate 			continue;
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 		if (opt->dofo_option >= DTRACEOPT_MAX)
764*0Sstevel@tonic-gate 			continue;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		dtp->dt_options[opt->dofo_option] = opt->dofo_value;
767*0Sstevel@tonic-gate 	}
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	return (0);
770*0Sstevel@tonic-gate }
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate typedef struct dt_option {
773*0Sstevel@tonic-gate 	const char *o_name;
774*0Sstevel@tonic-gate 	int (*o_func)(dtrace_hdl_t *, const char *, uintptr_t);
775*0Sstevel@tonic-gate 	uintptr_t o_option;
776*0Sstevel@tonic-gate } dt_option_t;
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate /*
779*0Sstevel@tonic-gate  * Compile-time options.
780*0Sstevel@tonic-gate  */
781*0Sstevel@tonic-gate static const dt_option_t _dtrace_ctoptions[] = {
782*0Sstevel@tonic-gate 	{ "aggpercpu", dt_opt_agg, DTRACE_A_PERCPU },
783*0Sstevel@tonic-gate 	{ "amin", dt_opt_amin },
784*0Sstevel@tonic-gate 	{ "argref", dt_opt_cflags, DTRACE_C_ARGREF },
785*0Sstevel@tonic-gate 	{ "core", dt_opt_core },
786*0Sstevel@tonic-gate 	{ "cpp", dt_opt_cflags, DTRACE_C_CPP },
787*0Sstevel@tonic-gate 	{ "cpphdrs", dt_opt_cpp_hdrs },
788*0Sstevel@tonic-gate 	{ "cpppath", dt_opt_cpp_path },
789*0Sstevel@tonic-gate 	{ "ctypes", dt_opt_ctypes },
790*0Sstevel@tonic-gate 	{ "defaultargs", dt_opt_cflags, DTRACE_C_DEFARG },
791*0Sstevel@tonic-gate 	{ "dtypes", dt_opt_dtypes },
792*0Sstevel@tonic-gate 	{ "debug", dt_opt_debug },
793*0Sstevel@tonic-gate 	{ "define", dt_opt_cpp_opts, (uintptr_t)"-D" },
794*0Sstevel@tonic-gate 	{ "empty", dt_opt_cflags, DTRACE_C_EMPTY },
795*0Sstevel@tonic-gate 	{ "errtags", dt_opt_cflags, DTRACE_C_ETAGS },
796*0Sstevel@tonic-gate 	{ "evaltime", dt_opt_evaltime },
797*0Sstevel@tonic-gate 	{ "incdir", dt_opt_cpp_opts, (uintptr_t)"-I" },
798*0Sstevel@tonic-gate 	{ "iregs", dt_opt_iregs },
799*0Sstevel@tonic-gate 	{ "kdefs", dt_opt_invcflags, DTRACE_C_KNODEF },
800*0Sstevel@tonic-gate 	{ "knodefs", dt_opt_cflags, DTRACE_C_KNODEF },
801*0Sstevel@tonic-gate 	{ "lazyload", dt_opt_lazyload },
802*0Sstevel@tonic-gate 	{ "ldpath", dt_opt_ld_path },
803*0Sstevel@tonic-gate 	{ "libdir", dt_opt_libdir },
804*0Sstevel@tonic-gate 	{ "linkmode", dt_opt_linkmode },
805*0Sstevel@tonic-gate 	{ "linktype", dt_opt_linktype },
806*0Sstevel@tonic-gate 	{ "nolibs", dt_opt_cflags, DTRACE_C_NOLIBS },
807*0Sstevel@tonic-gate 	{ "pgmax", dt_opt_pgmax },
808*0Sstevel@tonic-gate 	{ "pspec", dt_opt_cflags, DTRACE_C_PSPEC },
809*0Sstevel@tonic-gate 	{ "stdc", dt_opt_stdc },
810*0Sstevel@tonic-gate 	{ "strip", dt_opt_dflags, DTRACE_D_STRIP },
811*0Sstevel@tonic-gate 	{ "syslibdir", dt_opt_syslibdir },
812*0Sstevel@tonic-gate 	{ "tree", dt_opt_tree },
813*0Sstevel@tonic-gate 	{ "tregs", dt_opt_tregs },
814*0Sstevel@tonic-gate 	{ "udefs", dt_opt_invcflags, DTRACE_C_UNODEF },
815*0Sstevel@tonic-gate 	{ "undef", dt_opt_cpp_opts, (uintptr_t)"-U" },
816*0Sstevel@tonic-gate 	{ "unodefs", dt_opt_cflags, DTRACE_C_UNODEF },
817*0Sstevel@tonic-gate 	{ "verbose", dt_opt_cflags, DTRACE_C_DIFV },
818*0Sstevel@tonic-gate 	{ "version", dt_opt_version },
819*0Sstevel@tonic-gate 	{ "zdefs", dt_opt_cflags, DTRACE_C_ZDEFS },
820*0Sstevel@tonic-gate 	{ NULL }
821*0Sstevel@tonic-gate };
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate /*
824*0Sstevel@tonic-gate  * Run-time options.
825*0Sstevel@tonic-gate  */
826*0Sstevel@tonic-gate static const dt_option_t _dtrace_rtoptions[] = {
827*0Sstevel@tonic-gate 	{ "aggrate", dt_opt_rate, DTRACEOPT_AGGRATE },
828*0Sstevel@tonic-gate 	{ "aggsize", dt_opt_size, DTRACEOPT_AGGSIZE },
829*0Sstevel@tonic-gate 	{ "bufsize", dt_opt_size, DTRACEOPT_BUFSIZE },
830*0Sstevel@tonic-gate 	{ "bufpolicy", dt_opt_bufpolicy, DTRACEOPT_BUFPOLICY },
831*0Sstevel@tonic-gate 	{ "bufresize", dt_opt_bufresize, DTRACEOPT_BUFRESIZE },
832*0Sstevel@tonic-gate 	{ "cleanrate", dt_opt_rate, DTRACEOPT_CLEANRATE },
833*0Sstevel@tonic-gate 	{ "cpu", dt_opt_runtime, DTRACEOPT_CPU },
834*0Sstevel@tonic-gate 	{ "destructive", dt_opt_runtime, DTRACEOPT_DESTRUCTIVE },
835*0Sstevel@tonic-gate 	{ "dynvarsize", dt_opt_size, DTRACEOPT_DYNVARSIZE },
836*0Sstevel@tonic-gate 	{ "flowindent", dt_opt_runtime, DTRACEOPT_FLOWINDENT },
837*0Sstevel@tonic-gate 	{ "grabanon", dt_opt_runtime, DTRACEOPT_GRABANON },
838*0Sstevel@tonic-gate 	{ "jstackframes", dt_opt_runtime, DTRACEOPT_JSTACKFRAMES },
839*0Sstevel@tonic-gate 	{ "jstackstrsize", dt_opt_runtime, DTRACEOPT_JSTACKSTRSIZE },
840*0Sstevel@tonic-gate 	{ "nspec", dt_opt_runtime, DTRACEOPT_NSPEC },
841*0Sstevel@tonic-gate 	{ "quiet", dt_opt_runtime, DTRACEOPT_QUIET },
842*0Sstevel@tonic-gate 	{ "rawbytes", dt_opt_runtime, DTRACEOPT_RAWBYTES },
843*0Sstevel@tonic-gate 	{ "specsize", dt_opt_size, DTRACEOPT_SPECSIZE },
844*0Sstevel@tonic-gate 	{ "stackframes", dt_opt_runtime, DTRACEOPT_STACKFRAMES },
845*0Sstevel@tonic-gate 	{ "stackindent", dt_opt_runtime, DTRACEOPT_STACKINDENT },
846*0Sstevel@tonic-gate 	{ "statusrate", dt_opt_rate, DTRACEOPT_STATUSRATE },
847*0Sstevel@tonic-gate 	{ "strsize", dt_opt_strsize, DTRACEOPT_STRSIZE },
848*0Sstevel@tonic-gate 	{ "switchrate", dt_opt_rate, DTRACEOPT_SWITCHRATE },
849*0Sstevel@tonic-gate 	{ "ustackframes", dt_opt_runtime, DTRACEOPT_USTACKFRAMES },
850*0Sstevel@tonic-gate 	{ NULL }
851*0Sstevel@tonic-gate };
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate int
854*0Sstevel@tonic-gate dtrace_getopt(dtrace_hdl_t *dtp, const char *opt, dtrace_optval_t *val)
855*0Sstevel@tonic-gate {
856*0Sstevel@tonic-gate 	const dt_option_t *op;
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	if (opt == NULL)
859*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	/*
862*0Sstevel@tonic-gate 	 * We only need to search the run-time options -- it's not legal
863*0Sstevel@tonic-gate 	 * to get the values of compile-time options.
864*0Sstevel@tonic-gate 	 */
865*0Sstevel@tonic-gate 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
866*0Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0) {
867*0Sstevel@tonic-gate 			*val = dtp->dt_options[op->o_option];
868*0Sstevel@tonic-gate 			return (0);
869*0Sstevel@tonic-gate 		}
870*0Sstevel@tonic-gate 	}
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
873*0Sstevel@tonic-gate }
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate int
876*0Sstevel@tonic-gate dtrace_setopt(dtrace_hdl_t *dtp, const char *opt, const char *val)
877*0Sstevel@tonic-gate {
878*0Sstevel@tonic-gate 	const dt_option_t *op;
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 	if (opt == NULL)
881*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	for (op = _dtrace_ctoptions; op->o_name != NULL; op++) {
884*0Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0)
885*0Sstevel@tonic-gate 			return (op->o_func(dtp, val, op->o_option));
886*0Sstevel@tonic-gate 	}
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
889*0Sstevel@tonic-gate 		if (strcmp(op->o_name, opt) == 0) {
890*0Sstevel@tonic-gate 			/*
891*0Sstevel@tonic-gate 			 * Currently, no run-time option may be set while
892*0Sstevel@tonic-gate 			 * tracing is active.
893*0Sstevel@tonic-gate 			 */
894*0Sstevel@tonic-gate 			if (dtp->dt_active)
895*0Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_ACTIVE));
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 			return (op->o_func(dtp, val, op->o_option));
898*0Sstevel@tonic-gate 		}
899*0Sstevel@tonic-gate 	}
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	return (dt_set_errno(dtp, EDT_BADOPTNAME));
902*0Sstevel@tonic-gate }
903