17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*900524f3Sahl 237c478bd9Sstevel@tonic-gate /* 24*900524f3Sahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <dt_impl.h> 317c478bd9Sstevel@tonic-gate #include <stddef.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <assert.h> 347c478bd9Sstevel@tonic-gate #include <time.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate static const struct { 377c478bd9Sstevel@tonic-gate int dtslt_option; 387c478bd9Sstevel@tonic-gate size_t dtslt_offs; 397c478bd9Sstevel@tonic-gate } _dtrace_sleeptab[] = { 407c478bd9Sstevel@tonic-gate { DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) }, 417c478bd9Sstevel@tonic-gate { DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) }, 427c478bd9Sstevel@tonic-gate { DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) }, 437c478bd9Sstevel@tonic-gate { DTRACEOPT_MAX, 0 } 447c478bd9Sstevel@tonic-gate }; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate void 477c478bd9Sstevel@tonic-gate dtrace_sleep(dtrace_hdl_t *dtp) 487c478bd9Sstevel@tonic-gate { 497c478bd9Sstevel@tonic-gate dt_proc_hash_t *dph = dtp->dt_procs; 507c478bd9Sstevel@tonic-gate dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY]; 51*900524f3Sahl dt_proc_notify_t *dprn; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate hrtime_t earliest = INT64_MAX; 547c478bd9Sstevel@tonic-gate struct timespec tv; 557c478bd9Sstevel@tonic-gate hrtime_t now; 567c478bd9Sstevel@tonic-gate int i; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) { 597c478bd9Sstevel@tonic-gate uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs; 607c478bd9Sstevel@tonic-gate int opt = _dtrace_sleeptab[i].dtslt_option; 617c478bd9Sstevel@tonic-gate dtrace_optval_t interval = dtp->dt_options[opt]; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * If the buffering policy is set to anything other than 657c478bd9Sstevel@tonic-gate * "switch", we ignore the aggrate and switchrate -- they're 667c478bd9Sstevel@tonic-gate * meaningless. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate if (policy != DTRACEOPT_BUFPOLICY_SWITCH && 697c478bd9Sstevel@tonic-gate _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE) 707c478bd9Sstevel@tonic-gate continue; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (*((hrtime_t *)a) + interval < earliest) 737c478bd9Sstevel@tonic-gate earliest = *((hrtime_t *)a) + interval; 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&dph->dph_lock); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate now = gethrtime(); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate if (earliest < now) { 817c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock); 827c478bd9Sstevel@tonic-gate return; /* sleep duration has already past */ 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate tv.tv_sec = (earliest - now) / NANOSEC; 867c478bd9Sstevel@tonic-gate tv.tv_nsec = (earliest - now) % NANOSEC; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * Wait for either 'tv' nanoseconds to pass or to receive notification 907c478bd9Sstevel@tonic-gate * that a process is in an interesting state. Regardless of why we 917c478bd9Sstevel@tonic-gate * awaken, iterate over any pending notifications and process them. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate (void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv); 947c478bd9Sstevel@tonic-gate 95*900524f3Sahl while ((dprn = dph->dph_notify) != NULL) { 96*900524f3Sahl if (dtp->dt_prochdlr != NULL) { 97*900524f3Sahl char *err = dprn->dprn_errmsg; 98*900524f3Sahl if (*err == '\0') 99*900524f3Sahl err = NULL; 1007c478bd9Sstevel@tonic-gate 101*900524f3Sahl dtp->dt_prochdlr(dprn->dprn_dpr->dpr_proc, err, 102*900524f3Sahl dtp->dt_procarg); 103*900524f3Sahl } 104*900524f3Sahl 105*900524f3Sahl dph->dph_notify = dprn->dprn_next; 106*900524f3Sahl dt_free(dtp, dprn); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&dph->dph_lock); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate int 1137c478bd9Sstevel@tonic-gate dtrace_status(dtrace_hdl_t *dtp) 1147c478bd9Sstevel@tonic-gate { 1157c478bd9Sstevel@tonic-gate int gen = dtp->dt_statusgen; 1167c478bd9Sstevel@tonic-gate dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE]; 1177c478bd9Sstevel@tonic-gate hrtime_t now = gethrtime(); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate if (!dtp->dt_active) 1207c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_NONE); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (dtp->dt_stopped) 1237c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_STOPPED); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (dtp->dt_laststatus != 0) { 1267c478bd9Sstevel@tonic-gate if (now - dtp->dt_laststatus < interval) 1277c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_NONE); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate dtp->dt_laststatus += interval; 1307c478bd9Sstevel@tonic-gate } else { 1317c478bd9Sstevel@tonic-gate dtp->dt_laststatus = now; 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1) 1357c478bd9Sstevel@tonic-gate return (dt_set_errno(dtp, errno)); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate dtp->dt_statusgen ^= 1; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen], 1407c478bd9Sstevel@tonic-gate &dtp->dt_status[gen]) == -1) 1417c478bd9Sstevel@tonic-gate return (-1); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (dtp->dt_status[gen].dtst_exiting) { 1447c478bd9Sstevel@tonic-gate if (!dtp->dt_stopped) 1457c478bd9Sstevel@tonic-gate (void) dtrace_stop(dtp); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_EXITED); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate if (dtp->dt_status[gen].dtst_filled == 0) 1517c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_OKAY); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 1547c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_OKAY); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate if (!dtp->dt_stopped) { 1577c478bd9Sstevel@tonic-gate if (dtrace_stop(dtp) == -1) 1587c478bd9Sstevel@tonic-gate return (-1); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate return (DTRACE_STATUS_FILLED); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 164a1b5e537Sbmc int 165a1b5e537Sbmc dtrace_go(dtrace_hdl_t *dtp) 166a1b5e537Sbmc { 167a1b5e537Sbmc void *dof; 168a1b5e537Sbmc int err; 169a1b5e537Sbmc 170a1b5e537Sbmc if (dtp->dt_active) 171a1b5e537Sbmc return (dt_set_errno(dtp, EINVAL)); 172a1b5e537Sbmc 173a1b5e537Sbmc /* 174a1b5e537Sbmc * If a dtrace:::ERROR program and callback are registered, enable the 175a1b5e537Sbmc * program before we start tracing. If this fails for a vector open 176a1b5e537Sbmc * with ENOTTY, we permit dtrace_go() to succeed so that vector clients 177a1b5e537Sbmc * such as mdb's dtrace module can execute the rest of dtrace_go() even 178a1b5e537Sbmc * though they do not provide support for the DTRACEIOC_ENABLE ioctl. 179a1b5e537Sbmc */ 180a1b5e537Sbmc if (dtp->dt_errprog != NULL && 181a1b5e537Sbmc dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && ( 182a1b5e537Sbmc dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL)) 183a1b5e537Sbmc return (-1); /* dt_errno has been set for us */ 184a1b5e537Sbmc 185a1b5e537Sbmc if ((dof = dtrace_getopt_dof(dtp)) == NULL) 186a1b5e537Sbmc return (-1); /* dt_errno has been set for us */ 187a1b5e537Sbmc 188a1b5e537Sbmc err = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof); 189a1b5e537Sbmc dtrace_dof_destroy(dtp, dof); 190a1b5e537Sbmc 191a1b5e537Sbmc if (err == -1 && (errno != ENOTTY || dtp->dt_vector == NULL)) 192a1b5e537Sbmc return (dt_set_errno(dtp, errno)); 193a1b5e537Sbmc 194a1b5e537Sbmc if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) { 195a1b5e537Sbmc if (errno == EACCES) 196a1b5e537Sbmc return (dt_set_errno(dtp, EDT_DESTRUCTIVE)); 197a1b5e537Sbmc 198a1b5e537Sbmc if (errno == EALREADY) 199a1b5e537Sbmc return (dt_set_errno(dtp, EDT_ISANON)); 200a1b5e537Sbmc 201a1b5e537Sbmc if (errno == ENOENT) 202a1b5e537Sbmc return (dt_set_errno(dtp, EDT_NOANON)); 203a1b5e537Sbmc 204a1b5e537Sbmc if (errno == E2BIG) 205a1b5e537Sbmc return (dt_set_errno(dtp, EDT_ENDTOOBIG)); 206a1b5e537Sbmc 207a1b5e537Sbmc if (errno == ENOSPC) 208a1b5e537Sbmc return (dt_set_errno(dtp, EDT_BUFTOOSMALL)); 209a1b5e537Sbmc 210a1b5e537Sbmc return (dt_set_errno(dtp, errno)); 211a1b5e537Sbmc } 212a1b5e537Sbmc 213a1b5e537Sbmc dtp->dt_active = 1; 214a1b5e537Sbmc 215a1b5e537Sbmc if (dt_options_load(dtp) == -1) 216a1b5e537Sbmc return (dt_set_errno(dtp, errno)); 217a1b5e537Sbmc 218a1b5e537Sbmc return (dt_aggregate_go(dtp)); 219a1b5e537Sbmc } 220a1b5e537Sbmc 221a1b5e537Sbmc int 222a1b5e537Sbmc dtrace_stop(dtrace_hdl_t *dtp) 223a1b5e537Sbmc { 224a1b5e537Sbmc int gen = dtp->dt_statusgen; 225a1b5e537Sbmc 226a1b5e537Sbmc if (dtp->dt_stopped) 227a1b5e537Sbmc return (0); 228a1b5e537Sbmc 229a1b5e537Sbmc if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1) 230a1b5e537Sbmc return (dt_set_errno(dtp, errno)); 231a1b5e537Sbmc 232a1b5e537Sbmc dtp->dt_stopped = 1; 233a1b5e537Sbmc 234a1b5e537Sbmc /* 235a1b5e537Sbmc * Now that we're stopped, we're going to get status one final time. 236a1b5e537Sbmc */ 237a1b5e537Sbmc if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1) 238a1b5e537Sbmc return (dt_set_errno(dtp, errno)); 239a1b5e537Sbmc 240a1b5e537Sbmc if (dt_handle_status(dtp, &dtp->dt_status[gen ^ 1], 241a1b5e537Sbmc &dtp->dt_status[gen]) == -1) 242a1b5e537Sbmc return (-1); 243a1b5e537Sbmc 244a1b5e537Sbmc return (0); 245a1b5e537Sbmc } 246a1b5e537Sbmc 247a1b5e537Sbmc 2487c478bd9Sstevel@tonic-gate dtrace_workstatus_t 2497c478bd9Sstevel@tonic-gate dtrace_work(dtrace_hdl_t *dtp, FILE *fp, 2507c478bd9Sstevel@tonic-gate dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate int status = dtrace_status(dtp); 2537c478bd9Sstevel@tonic-gate dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY]; 2547c478bd9Sstevel@tonic-gate dtrace_workstatus_t rval; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate switch (status) { 2577c478bd9Sstevel@tonic-gate case DTRACE_STATUS_EXITED: 2587c478bd9Sstevel@tonic-gate case DTRACE_STATUS_FILLED: 2597c478bd9Sstevel@tonic-gate case DTRACE_STATUS_STOPPED: 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * Tracing is stopped. We now want to force dtrace_consume() 2627c478bd9Sstevel@tonic-gate * and dtrace_aggregate_snap() to proceed, regardless of 2637c478bd9Sstevel@tonic-gate * switchrate and aggrate. We do this by clearing the times. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate dtp->dt_lastswitch = 0; 2667c478bd9Sstevel@tonic-gate dtp->dt_lastagg = 0; 2677c478bd9Sstevel@tonic-gate rval = DTRACE_WORKSTATUS_DONE; 2687c478bd9Sstevel@tonic-gate break; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate case DTRACE_STATUS_NONE: 2717c478bd9Sstevel@tonic-gate case DTRACE_STATUS_OKAY: 2727c478bd9Sstevel@tonic-gate rval = DTRACE_WORKSTATUS_OKAY; 2737c478bd9Sstevel@tonic-gate break; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate case -1: 2767c478bd9Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) && 2807c478bd9Sstevel@tonic-gate policy != DTRACEOPT_BUFPOLICY_SWITCH) { 2817c478bd9Sstevel@tonic-gate /* 2827c478bd9Sstevel@tonic-gate * There either isn't any status or things are fine -- and 2837c478bd9Sstevel@tonic-gate * this is a "ring" or "fill" buffer. We don't want to consume 2847c478bd9Sstevel@tonic-gate * any of the trace data or snapshot the aggregations; we just 2857c478bd9Sstevel@tonic-gate * return. 2867c478bd9Sstevel@tonic-gate */ 2877c478bd9Sstevel@tonic-gate assert(rval == DTRACE_WORKSTATUS_OKAY); 2887c478bd9Sstevel@tonic-gate return (rval); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (dtrace_aggregate_snap(dtp) == -1) 2927c478bd9Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1) 2957c478bd9Sstevel@tonic-gate return (DTRACE_WORKSTATUS_ERROR); 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate return (rval); 2987c478bd9Sstevel@tonic-gate } 299