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