1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <dt_impl.h>
29 #include <stddef.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <time.h>
33 
34 static const struct {
35 	int dtslt_option;
36 	size_t dtslt_offs;
37 } _dtrace_sleeptab[] = {
38 	{ DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) },
39 	{ DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) },
40 	{ DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) },
41 	{ DTRACEOPT_MAX, 0 }
42 };
43 
44 void
dtrace_sleep(dtrace_hdl_t * dtp)45 dtrace_sleep(dtrace_hdl_t *dtp)
46 {
47 	dt_proc_hash_t *dph = dtp->dt_procs;
48 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
49 	dt_proc_notify_t *dprn;
50 
51 	hrtime_t earliest = INT64_MAX;
52 	struct timespec tv;
53 	hrtime_t now;
54 	int i;
55 
56 	for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) {
57 		uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs;
58 		int opt = _dtrace_sleeptab[i].dtslt_option;
59 		dtrace_optval_t interval = dtp->dt_options[opt];
60 
61 		/*
62 		 * If the buffering policy is set to anything other than
63 		 * "switch", we ignore the aggrate and switchrate -- they're
64 		 * meaningless.
65 		 */
66 		if (policy != DTRACEOPT_BUFPOLICY_SWITCH &&
67 		    _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE)
68 			continue;
69 
70 		if (*((hrtime_t *)a) + interval < earliest)
71 			earliest = *((hrtime_t *)a) + interval;
72 	}
73 
74 	(void) pthread_mutex_lock(&dph->dph_lock);
75 
76 	now = gethrtime();
77 
78 	if (earliest < now) {
79 		(void) pthread_mutex_unlock(&dph->dph_lock);
80 		return; /* sleep duration has already past */
81 	}
82 
83 	tv.tv_sec = (earliest - now) / NANOSEC;
84 	tv.tv_nsec = (earliest - now) % NANOSEC;
85 
86 	/*
87 	 * Wait for either 'tv' nanoseconds to pass or to receive notification
88 	 * that a process is in an interesting state.  Regardless of why we
89 	 * awaken, iterate over any pending notifications and process them.
90 	 */
91 	(void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv);
92 
93 	while ((dprn = dph->dph_notify) != NULL) {
94 		if (dtp->dt_prochdlr != NULL) {
95 			char *err = dprn->dprn_errmsg;
96 			if (*err == '\0')
97 				err = NULL;
98 
99 			dtp->dt_prochdlr(dprn->dprn_dpr->dpr_proc, err,
100 			    dtp->dt_procarg);
101 		}
102 
103 		dph->dph_notify = dprn->dprn_next;
104 		dt_free(dtp, dprn);
105 	}
106 
107 	(void) pthread_mutex_unlock(&dph->dph_lock);
108 }
109 
110 int
dtrace_status(dtrace_hdl_t * dtp)111 dtrace_status(dtrace_hdl_t *dtp)
112 {
113 	int gen = dtp->dt_statusgen;
114 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE];
115 	hrtime_t now = gethrtime();
116 
117 	if (!dtp->dt_active)
118 		return (DTRACE_STATUS_NONE);
119 
120 	if (dtp->dt_stopped)
121 		return (DTRACE_STATUS_STOPPED);
122 
123 	if (dtp->dt_laststatus != 0) {
124 		if (now - dtp->dt_laststatus < interval)
125 			return (DTRACE_STATUS_NONE);
126 
127 		dtp->dt_laststatus += interval;
128 	} else {
129 		dtp->dt_laststatus = now;
130 	}
131 
132 	if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
133 		return (dt_set_errno(dtp, errno));
134 
135 	dtp->dt_statusgen ^= 1;
136 
137 	if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen],
138 	    &dtp->dt_status[gen]) == -1)
139 		return (-1);
140 
141 	if (dtp->dt_status[gen].dtst_exiting) {
142 		if (!dtp->dt_stopped)
143 			(void) dtrace_stop(dtp);
144 
145 		return (DTRACE_STATUS_EXITED);
146 	}
147 
148 	if (dtp->dt_status[gen].dtst_filled == 0)
149 		return (DTRACE_STATUS_OKAY);
150 
151 	if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
152 		return (DTRACE_STATUS_OKAY);
153 
154 	if (!dtp->dt_stopped) {
155 		if (dtrace_stop(dtp) == -1)
156 			return (-1);
157 	}
158 
159 	return (DTRACE_STATUS_FILLED);
160 }
161 
162 int
dtrace_go(dtrace_hdl_t * dtp)163 dtrace_go(dtrace_hdl_t *dtp)
164 {
165 	void *dof;
166 	int err;
167 
168 	if (dtp->dt_active)
169 		return (dt_set_errno(dtp, EINVAL));
170 
171 	/*
172 	 * If a dtrace:::ERROR program and callback are registered, enable the
173 	 * program before we start tracing.  If this fails for a vector open
174 	 * with ENOTTY, we permit dtrace_go() to succeed so that vector clients
175 	 * such as mdb's dtrace module can execute the rest of dtrace_go() even
176 	 * though they do not provide support for the DTRACEIOC_ENABLE ioctl.
177 	 */
178 	if (dtp->dt_errprog != NULL &&
179 	    dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && (
180 	    dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL))
181 		return (-1); /* dt_errno has been set for us */
182 
183 	if ((dof = dtrace_getopt_dof(dtp)) == NULL)
184 		return (-1); /* dt_errno has been set for us */
185 
186 	err = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
187 	dtrace_dof_destroy(dtp, dof);
188 
189 	if (err == -1 && (errno != ENOTTY || dtp->dt_vector == NULL))
190 		return (dt_set_errno(dtp, errno));
191 
192 	if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) {
193 		if (errno == EACCES)
194 			return (dt_set_errno(dtp, EDT_DESTRUCTIVE));
195 
196 		if (errno == EALREADY)
197 			return (dt_set_errno(dtp, EDT_ISANON));
198 
199 		if (errno == ENOENT)
200 			return (dt_set_errno(dtp, EDT_NOANON));
201 
202 		if (errno == E2BIG)
203 			return (dt_set_errno(dtp, EDT_ENDTOOBIG));
204 
205 		if (errno == ENOSPC)
206 			return (dt_set_errno(dtp, EDT_BUFTOOSMALL));
207 
208 		return (dt_set_errno(dtp, errno));
209 	}
210 
211 	dtp->dt_active = 1;
212 
213 	if (dt_options_load(dtp) == -1)
214 		return (dt_set_errno(dtp, errno));
215 
216 	return (dt_aggregate_go(dtp));
217 }
218 
219 int
dtrace_stop(dtrace_hdl_t * dtp)220 dtrace_stop(dtrace_hdl_t *dtp)
221 {
222 	int gen = dtp->dt_statusgen;
223 
224 	if (dtp->dt_stopped)
225 		return (0);
226 
227 	if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1)
228 		return (dt_set_errno(dtp, errno));
229 
230 	dtp->dt_stopped = 1;
231 
232 	/*
233 	 * Now that we're stopped, we're going to get status one final time.
234 	 */
235 	if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
236 		return (dt_set_errno(dtp, errno));
237 
238 	if (dt_handle_status(dtp, &dtp->dt_status[gen ^ 1],
239 	    &dtp->dt_status[gen]) == -1)
240 		return (-1);
241 
242 	return (0);
243 }
244 
245 
246 dtrace_workstatus_t
dtrace_work(dtrace_hdl_t * dtp,FILE * fp,dtrace_consume_probe_f * pfunc,dtrace_consume_rec_f * rfunc,void * arg)247 dtrace_work(dtrace_hdl_t *dtp, FILE *fp,
248     dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg)
249 {
250 	int status = dtrace_status(dtp);
251 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
252 	dtrace_workstatus_t rval;
253 
254 	switch (status) {
255 	case DTRACE_STATUS_EXITED:
256 	case DTRACE_STATUS_FILLED:
257 	case DTRACE_STATUS_STOPPED:
258 		/*
259 		 * Tracing is stopped.  We now want to force dtrace_consume()
260 		 * and dtrace_aggregate_snap() to proceed, regardless of
261 		 * switchrate and aggrate.  We do this by clearing the times.
262 		 */
263 		dtp->dt_lastswitch = 0;
264 		dtp->dt_lastagg = 0;
265 		rval = DTRACE_WORKSTATUS_DONE;
266 		break;
267 
268 	case DTRACE_STATUS_NONE:
269 	case DTRACE_STATUS_OKAY:
270 		rval = DTRACE_WORKSTATUS_OKAY;
271 		break;
272 
273 	case -1:
274 		return (DTRACE_WORKSTATUS_ERROR);
275 	}
276 
277 	if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) &&
278 	    policy != DTRACEOPT_BUFPOLICY_SWITCH) {
279 		/*
280 		 * There either isn't any status or things are fine -- and
281 		 * this is a "ring" or "fill" buffer.  We don't want to consume
282 		 * any of the trace data or snapshot the aggregations; we just
283 		 * return.
284 		 */
285 		assert(rval == DTRACE_WORKSTATUS_OKAY);
286 		return (rval);
287 	}
288 
289 	if (dtrace_aggregate_snap(dtp) == -1)
290 		return (DTRACE_WORKSTATUS_ERROR);
291 
292 	if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1)
293 		return (DTRACE_WORKSTATUS_ERROR);
294 
295 	return (rval);
296 }
297