10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*1399Sahl
230Sstevel@tonic-gate /*
24*1399Sahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <dt_impl.h>
310Sstevel@tonic-gate #include <stddef.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <time.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate static const struct {
370Sstevel@tonic-gate int dtslt_option;
380Sstevel@tonic-gate size_t dtslt_offs;
390Sstevel@tonic-gate } _dtrace_sleeptab[] = {
400Sstevel@tonic-gate { DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) },
410Sstevel@tonic-gate { DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) },
420Sstevel@tonic-gate { DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) },
430Sstevel@tonic-gate { DTRACEOPT_MAX, 0 }
440Sstevel@tonic-gate };
450Sstevel@tonic-gate
460Sstevel@tonic-gate void
dtrace_sleep(dtrace_hdl_t * dtp)470Sstevel@tonic-gate dtrace_sleep(dtrace_hdl_t *dtp)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs;
500Sstevel@tonic-gate dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
51*1399Sahl dt_proc_notify_t *dprn;
520Sstevel@tonic-gate
530Sstevel@tonic-gate hrtime_t earliest = INT64_MAX;
540Sstevel@tonic-gate struct timespec tv;
550Sstevel@tonic-gate hrtime_t now;
560Sstevel@tonic-gate int i;
570Sstevel@tonic-gate
580Sstevel@tonic-gate for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) {
590Sstevel@tonic-gate uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs;
600Sstevel@tonic-gate int opt = _dtrace_sleeptab[i].dtslt_option;
610Sstevel@tonic-gate dtrace_optval_t interval = dtp->dt_options[opt];
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * If the buffering policy is set to anything other than
650Sstevel@tonic-gate * "switch", we ignore the aggrate and switchrate -- they're
660Sstevel@tonic-gate * meaningless.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate if (policy != DTRACEOPT_BUFPOLICY_SWITCH &&
690Sstevel@tonic-gate _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE)
700Sstevel@tonic-gate continue;
710Sstevel@tonic-gate
720Sstevel@tonic-gate if (*((hrtime_t *)a) + interval < earliest)
730Sstevel@tonic-gate earliest = *((hrtime_t *)a) + interval;
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate (void) pthread_mutex_lock(&dph->dph_lock);
770Sstevel@tonic-gate
780Sstevel@tonic-gate now = gethrtime();
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (earliest < now) {
810Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock);
820Sstevel@tonic-gate return; /* sleep duration has already past */
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate tv.tv_sec = (earliest - now) / NANOSEC;
860Sstevel@tonic-gate tv.tv_nsec = (earliest - now) % NANOSEC;
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Wait for either 'tv' nanoseconds to pass or to receive notification
900Sstevel@tonic-gate * that a process is in an interesting state. Regardless of why we
910Sstevel@tonic-gate * awaken, iterate over any pending notifications and process them.
920Sstevel@tonic-gate */
930Sstevel@tonic-gate (void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv);
940Sstevel@tonic-gate
95*1399Sahl while ((dprn = dph->dph_notify) != NULL) {
96*1399Sahl if (dtp->dt_prochdlr != NULL) {
97*1399Sahl char *err = dprn->dprn_errmsg;
98*1399Sahl if (*err == '\0')
99*1399Sahl err = NULL;
1000Sstevel@tonic-gate
101*1399Sahl dtp->dt_prochdlr(dprn->dprn_dpr->dpr_proc, err,
102*1399Sahl dtp->dt_procarg);
103*1399Sahl }
104*1399Sahl
105*1399Sahl dph->dph_notify = dprn->dprn_next;
106*1399Sahl dt_free(dtp, dprn);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate int
dtrace_status(dtrace_hdl_t * dtp)1130Sstevel@tonic-gate dtrace_status(dtrace_hdl_t *dtp)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate int gen = dtp->dt_statusgen;
1160Sstevel@tonic-gate dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE];
1170Sstevel@tonic-gate hrtime_t now = gethrtime();
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (!dtp->dt_active)
1200Sstevel@tonic-gate return (DTRACE_STATUS_NONE);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if (dtp->dt_stopped)
1230Sstevel@tonic-gate return (DTRACE_STATUS_STOPPED);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (dtp->dt_laststatus != 0) {
1260Sstevel@tonic-gate if (now - dtp->dt_laststatus < interval)
1270Sstevel@tonic-gate return (DTRACE_STATUS_NONE);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate dtp->dt_laststatus += interval;
1300Sstevel@tonic-gate } else {
1310Sstevel@tonic-gate dtp->dt_laststatus = now;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
1350Sstevel@tonic-gate return (dt_set_errno(dtp, errno));
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate dtp->dt_statusgen ^= 1;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen],
1400Sstevel@tonic-gate &dtp->dt_status[gen]) == -1)
1410Sstevel@tonic-gate return (-1);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if (dtp->dt_status[gen].dtst_exiting) {
1440Sstevel@tonic-gate if (!dtp->dt_stopped)
1450Sstevel@tonic-gate (void) dtrace_stop(dtp);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate return (DTRACE_STATUS_EXITED);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (dtp->dt_status[gen].dtst_filled == 0)
1510Sstevel@tonic-gate return (DTRACE_STATUS_OKAY);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
1540Sstevel@tonic-gate return (DTRACE_STATUS_OKAY);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if (!dtp->dt_stopped) {
1570Sstevel@tonic-gate if (dtrace_stop(dtp) == -1)
1580Sstevel@tonic-gate return (-1);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate return (DTRACE_STATUS_FILLED);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
164457Sbmc int
dtrace_go(dtrace_hdl_t * dtp)165457Sbmc dtrace_go(dtrace_hdl_t *dtp)
166457Sbmc {
167457Sbmc void *dof;
168457Sbmc int err;
169457Sbmc
170457Sbmc if (dtp->dt_active)
171457Sbmc return (dt_set_errno(dtp, EINVAL));
172457Sbmc
173457Sbmc /*
174457Sbmc * If a dtrace:::ERROR program and callback are registered, enable the
175457Sbmc * program before we start tracing. If this fails for a vector open
176457Sbmc * with ENOTTY, we permit dtrace_go() to succeed so that vector clients
177457Sbmc * such as mdb's dtrace module can execute the rest of dtrace_go() even
178457Sbmc * though they do not provide support for the DTRACEIOC_ENABLE ioctl.
179457Sbmc */
180457Sbmc if (dtp->dt_errprog != NULL &&
181457Sbmc dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && (
182457Sbmc dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL))
183457Sbmc return (-1); /* dt_errno has been set for us */
184457Sbmc
185457Sbmc if ((dof = dtrace_getopt_dof(dtp)) == NULL)
186457Sbmc return (-1); /* dt_errno has been set for us */
187457Sbmc
188457Sbmc err = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
189457Sbmc dtrace_dof_destroy(dtp, dof);
190457Sbmc
191457Sbmc if (err == -1 && (errno != ENOTTY || dtp->dt_vector == NULL))
192457Sbmc return (dt_set_errno(dtp, errno));
193457Sbmc
194457Sbmc if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) {
195457Sbmc if (errno == EACCES)
196457Sbmc return (dt_set_errno(dtp, EDT_DESTRUCTIVE));
197457Sbmc
198457Sbmc if (errno == EALREADY)
199457Sbmc return (dt_set_errno(dtp, EDT_ISANON));
200457Sbmc
201457Sbmc if (errno == ENOENT)
202457Sbmc return (dt_set_errno(dtp, EDT_NOANON));
203457Sbmc
204457Sbmc if (errno == E2BIG)
205457Sbmc return (dt_set_errno(dtp, EDT_ENDTOOBIG));
206457Sbmc
207457Sbmc if (errno == ENOSPC)
208457Sbmc return (dt_set_errno(dtp, EDT_BUFTOOSMALL));
209457Sbmc
210457Sbmc return (dt_set_errno(dtp, errno));
211457Sbmc }
212457Sbmc
213457Sbmc dtp->dt_active = 1;
214457Sbmc
215457Sbmc if (dt_options_load(dtp) == -1)
216457Sbmc return (dt_set_errno(dtp, errno));
217457Sbmc
218457Sbmc return (dt_aggregate_go(dtp));
219457Sbmc }
220457Sbmc
221457Sbmc int
dtrace_stop(dtrace_hdl_t * dtp)222457Sbmc dtrace_stop(dtrace_hdl_t *dtp)
223457Sbmc {
224457Sbmc int gen = dtp->dt_statusgen;
225457Sbmc
226457Sbmc if (dtp->dt_stopped)
227457Sbmc return (0);
228457Sbmc
229457Sbmc if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1)
230457Sbmc return (dt_set_errno(dtp, errno));
231457Sbmc
232457Sbmc dtp->dt_stopped = 1;
233457Sbmc
234457Sbmc /*
235457Sbmc * Now that we're stopped, we're going to get status one final time.
236457Sbmc */
237457Sbmc if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
238457Sbmc return (dt_set_errno(dtp, errno));
239457Sbmc
240457Sbmc if (dt_handle_status(dtp, &dtp->dt_status[gen ^ 1],
241457Sbmc &dtp->dt_status[gen]) == -1)
242457Sbmc return (-1);
243457Sbmc
244457Sbmc return (0);
245457Sbmc }
246457Sbmc
247457Sbmc
2480Sstevel@tonic-gate dtrace_workstatus_t
dtrace_work(dtrace_hdl_t * dtp,FILE * fp,dtrace_consume_probe_f * pfunc,dtrace_consume_rec_f * rfunc,void * arg)2490Sstevel@tonic-gate dtrace_work(dtrace_hdl_t *dtp, FILE *fp,
2500Sstevel@tonic-gate dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate int status = dtrace_status(dtp);
2530Sstevel@tonic-gate dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
2540Sstevel@tonic-gate dtrace_workstatus_t rval;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate switch (status) {
2570Sstevel@tonic-gate case DTRACE_STATUS_EXITED:
2580Sstevel@tonic-gate case DTRACE_STATUS_FILLED:
2590Sstevel@tonic-gate case DTRACE_STATUS_STOPPED:
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate * Tracing is stopped. We now want to force dtrace_consume()
2620Sstevel@tonic-gate * and dtrace_aggregate_snap() to proceed, regardless of
2630Sstevel@tonic-gate * switchrate and aggrate. We do this by clearing the times.
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate dtp->dt_lastswitch = 0;
2660Sstevel@tonic-gate dtp->dt_lastagg = 0;
2670Sstevel@tonic-gate rval = DTRACE_WORKSTATUS_DONE;
2680Sstevel@tonic-gate break;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate case DTRACE_STATUS_NONE:
2710Sstevel@tonic-gate case DTRACE_STATUS_OKAY:
2720Sstevel@tonic-gate rval = DTRACE_WORKSTATUS_OKAY;
2730Sstevel@tonic-gate break;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate case -1:
2760Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) &&
2800Sstevel@tonic-gate policy != DTRACEOPT_BUFPOLICY_SWITCH) {
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * There either isn't any status or things are fine -- and
2830Sstevel@tonic-gate * this is a "ring" or "fill" buffer. We don't want to consume
2840Sstevel@tonic-gate * any of the trace data or snapshot the aggregations; we just
2850Sstevel@tonic-gate * return.
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate assert(rval == DTRACE_WORKSTATUS_OKAY);
2880Sstevel@tonic-gate return (rval);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (dtrace_aggregate_snap(dtp) == -1)
2920Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR);
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1)
2950Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR);
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate return (rval);
2980Sstevel@tonic-gate }
299