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