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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <stddef.h> 29 #include <stdlib.h> 30 #include <strings.h> 31 #include <errno.h> 32 #include <unistd.h> 33 #include <assert.h> 34 #if defined(sun) 35 #include <alloca.h> 36 #endif 37 38 #include <dt_impl.h> 39 #include <dt_program.h> 40 41 static const char _dt_errprog[] = 42 "dtrace:::ERROR" 43 "{" 44 " trace(arg1);" 45 " trace(arg2);" 46 " trace(arg3);" 47 " trace(arg4);" 48 " trace(arg5);" 49 "}"; 50 51 int 52 dtrace_handle_err(dtrace_hdl_t *dtp, dtrace_handle_err_f *hdlr, void *arg) 53 { 54 dtrace_prog_t *pgp = NULL; 55 dt_stmt_t *stp; 56 dtrace_ecbdesc_t *edp; 57 58 /* 59 * We don't currently support multiple error handlers. 60 */ 61 if (dtp->dt_errhdlr != NULL) 62 return (dt_set_errno(dtp, EALREADY)); 63 64 /* 65 * If the DTRACEOPT_GRABANON is enabled, the anonymous enabling will 66 * already have a dtrace:::ERROR probe enabled; save 'hdlr' and 'arg' 67 * but do not bother compiling and enabling _dt_errprog. 68 */ 69 if (dtp->dt_options[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) 70 goto out; 71 72 if ((pgp = dtrace_program_strcompile(dtp, _dt_errprog, 73 DTRACE_PROBESPEC_NAME, DTRACE_C_ZDEFS, 0, NULL)) == NULL) 74 return (dt_set_errno(dtp, dtrace_errno(dtp))); 75 76 stp = dt_list_next(&pgp->dp_stmts); 77 assert(stp != NULL); 78 79 edp = stp->ds_desc->dtsd_ecbdesc; 80 assert(edp != NULL); 81 edp->dted_uarg = DT_ECB_ERROR; 82 83 out: 84 dtp->dt_errhdlr = hdlr; 85 dtp->dt_errarg = arg; 86 dtp->dt_errprog = pgp; 87 88 return (0); 89 } 90 91 int 92 dtrace_handle_drop(dtrace_hdl_t *dtp, dtrace_handle_drop_f *hdlr, void *arg) 93 { 94 if (dtp->dt_drophdlr != NULL) 95 return (dt_set_errno(dtp, EALREADY)); 96 97 dtp->dt_drophdlr = hdlr; 98 dtp->dt_droparg = arg; 99 100 return (0); 101 } 102 103 int 104 dtrace_handle_proc(dtrace_hdl_t *dtp, dtrace_handle_proc_f *hdlr, void *arg) 105 { 106 if (dtp->dt_prochdlr != NULL) 107 return (dt_set_errno(dtp, EALREADY)); 108 109 dtp->dt_prochdlr = hdlr; 110 dtp->dt_procarg = arg; 111 112 return (0); 113 } 114 115 int 116 dtrace_handle_buffered(dtrace_hdl_t *dtp, dtrace_handle_buffered_f *hdlr, 117 void *arg) 118 { 119 if (dtp->dt_bufhdlr != NULL) 120 return (dt_set_errno(dtp, EALREADY)); 121 122 if (hdlr == NULL) 123 return (dt_set_errno(dtp, EINVAL)); 124 125 dtp->dt_bufhdlr = hdlr; 126 dtp->dt_bufarg = arg; 127 128 return (0); 129 } 130 131 int 132 dtrace_handle_setopt(dtrace_hdl_t *dtp, dtrace_handle_setopt_f *hdlr, 133 void *arg) 134 { 135 if (hdlr == NULL) 136 return (dt_set_errno(dtp, EINVAL)); 137 138 dtp->dt_setopthdlr = hdlr; 139 dtp->dt_setoptarg = arg; 140 141 return (0); 142 } 143 144 #define DT_REC(type, ndx) *((type *)((uintptr_t)data->dtpda_data + \ 145 epd->dtepd_rec[(ndx)].dtrd_offset)) 146 147 static int 148 dt_handle_err(dtrace_hdl_t *dtp, dtrace_probedata_t *data) 149 { 150 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *errepd; 151 dtrace_probedesc_t *pd = data->dtpda_pdesc, *errpd; 152 dtrace_errdata_t err; 153 dtrace_epid_t epid; 154 155 char where[30]; 156 char details[30]; 157 char offinfo[30]; 158 const int slop = 80; 159 const char *faultstr; 160 char *str; 161 int len; 162 163 assert(epd->dtepd_uarg == DT_ECB_ERROR); 164 165 if (epd->dtepd_nrecs != 5 || strcmp(pd->dtpd_provider, "dtrace") != 0 || 166 strcmp(pd->dtpd_name, "ERROR") != 0) 167 return (dt_set_errno(dtp, EDT_BADERROR)); 168 169 /* 170 * This is an error. We have the following items here: EPID, 171 * faulting action, DIF offset, fault code and faulting address. 172 */ 173 epid = (uint32_t)DT_REC(uint64_t, 0); 174 175 if (dt_epid_lookup(dtp, epid, &errepd, &errpd) != 0) 176 return (dt_set_errno(dtp, EDT_BADERROR)); 177 178 err.dteda_edesc = errepd; 179 err.dteda_pdesc = errpd; 180 err.dteda_cpu = data->dtpda_cpu; 181 err.dteda_action = (int)DT_REC(uint64_t, 1); 182 err.dteda_offset = (int)DT_REC(uint64_t, 2); 183 err.dteda_fault = (int)DT_REC(uint64_t, 3); 184 err.dteda_addr = DT_REC(uint64_t, 4); 185 186 faultstr = dtrace_faultstr(dtp, err.dteda_fault); 187 len = sizeof (where) + sizeof (offinfo) + strlen(faultstr) + 188 strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) + 189 strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) + 190 slop; 191 192 str = (char *)alloca(len); 193 194 if (err.dteda_action == 0) { 195 (void) sprintf(where, "predicate"); 196 } else { 197 (void) sprintf(where, "action #%d", err.dteda_action); 198 } 199 200 if (err.dteda_offset != -1) { 201 (void) sprintf(offinfo, " at DIF offset %d", err.dteda_offset); 202 } else { 203 offinfo[0] = 0; 204 } 205 206 switch (err.dteda_fault) { 207 case DTRACEFLT_BADADDR: 208 case DTRACEFLT_BADALIGN: 209 case DTRACEFLT_BADSTACK: 210 (void) sprintf(details, " (0x%llx)", 211 (u_longlong_t)err.dteda_addr); 212 break; 213 214 default: 215 details[0] = 0; 216 } 217 218 (void) snprintf(str, len, "error on enabled probe ID %u " 219 "(ID %u: %s:%s:%s:%s): %s%s in %s%s\n", 220 epid, errpd->dtpd_id, errpd->dtpd_provider, 221 errpd->dtpd_mod, errpd->dtpd_func, 222 errpd->dtpd_name, dtrace_faultstr(dtp, err.dteda_fault), 223 details, where, offinfo); 224 225 err.dteda_msg = str; 226 227 if (dtp->dt_errhdlr == NULL) 228 return (dt_set_errno(dtp, EDT_ERRABORT)); 229 230 if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT) 231 return (dt_set_errno(dtp, EDT_ERRABORT)); 232 233 return (0); 234 } 235 236 int 237 dt_handle_liberr(dtrace_hdl_t *dtp, const dtrace_probedata_t *data, 238 const char *faultstr) 239 { 240 dtrace_probedesc_t *errpd = data->dtpda_pdesc; 241 dtrace_errdata_t err; 242 const int slop = 80; 243 char *str; 244 int len; 245 246 err.dteda_edesc = data->dtpda_edesc; 247 err.dteda_pdesc = errpd; 248 err.dteda_cpu = data->dtpda_cpu; 249 err.dteda_action = -1; 250 err.dteda_offset = -1; 251 err.dteda_fault = DTRACEFLT_LIBRARY; 252 err.dteda_addr = 0; 253 254 len = strlen(faultstr) + 255 strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) + 256 strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) + 257 slop; 258 259 str = alloca(len); 260 261 (void) snprintf(str, len, "error on enabled probe ID %u " 262 "(ID %u: %s:%s:%s:%s): %s\n", 263 data->dtpda_edesc->dtepd_epid, 264 errpd->dtpd_id, errpd->dtpd_provider, 265 errpd->dtpd_mod, errpd->dtpd_func, 266 errpd->dtpd_name, faultstr); 267 268 err.dteda_msg = str; 269 270 if (dtp->dt_errhdlr == NULL) 271 return (dt_set_errno(dtp, EDT_ERRABORT)); 272 273 if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT) 274 return (dt_set_errno(dtp, EDT_ERRABORT)); 275 276 return (0); 277 } 278 279 #define DROPTAG(x) x, #x 280 281 static const struct { 282 dtrace_dropkind_t dtdrg_kind; 283 char *dtdrg_tag; 284 } _dt_droptags[] = { 285 { DROPTAG(DTRACEDROP_PRINCIPAL) }, 286 { DROPTAG(DTRACEDROP_AGGREGATION) }, 287 { DROPTAG(DTRACEDROP_DYNAMIC) }, 288 { DROPTAG(DTRACEDROP_DYNRINSE) }, 289 { DROPTAG(DTRACEDROP_DYNDIRTY) }, 290 { DROPTAG(DTRACEDROP_SPEC) }, 291 { DROPTAG(DTRACEDROP_SPECBUSY) }, 292 { DROPTAG(DTRACEDROP_SPECUNAVAIL) }, 293 { DROPTAG(DTRACEDROP_DBLERROR) }, 294 { DROPTAG(DTRACEDROP_STKSTROVERFLOW) }, 295 { 0, NULL } 296 }; 297 298 static const char * 299 dt_droptag(dtrace_dropkind_t kind) 300 { 301 int i; 302 303 for (i = 0; _dt_droptags[i].dtdrg_tag != NULL; i++) { 304 if (_dt_droptags[i].dtdrg_kind == kind) 305 return (_dt_droptags[i].dtdrg_tag); 306 } 307 308 return ("DTRACEDROP_UNKNOWN"); 309 } 310 311 int 312 dt_handle_cpudrop(dtrace_hdl_t *dtp, processorid_t cpu, 313 dtrace_dropkind_t what, uint64_t howmany) 314 { 315 dtrace_dropdata_t drop; 316 char str[80], *s; 317 int size; 318 319 assert(what == DTRACEDROP_PRINCIPAL || what == DTRACEDROP_AGGREGATION); 320 321 bzero(&drop, sizeof (drop)); 322 drop.dtdda_handle = dtp; 323 drop.dtdda_cpu = cpu; 324 drop.dtdda_kind = what; 325 drop.dtdda_drops = howmany; 326 drop.dtdda_msg = str; 327 328 if (dtp->dt_droptags) { 329 (void) snprintf(str, sizeof (str), "[%s] ", dt_droptag(what)); 330 s = &str[strlen(str)]; 331 size = sizeof (str) - (s - str); 332 } else { 333 s = str; 334 size = sizeof (str); 335 } 336 337 (void) snprintf(s, size, "%llu %sdrop%s on CPU %d\n", 338 howmany, what == DTRACEDROP_PRINCIPAL ? "" : "aggregation ", 339 howmany > 1 ? "s" : "", cpu); 340 341 if (dtp->dt_drophdlr == NULL) 342 return (dt_set_errno(dtp, EDT_DROPABORT)); 343 344 if ((*dtp->dt_drophdlr)(&drop, dtp->dt_droparg) == DTRACE_HANDLE_ABORT) 345 return (dt_set_errno(dtp, EDT_DROPABORT)); 346 347 return (0); 348 } 349 350 static const struct { 351 dtrace_dropkind_t dtdrt_kind; 352 uintptr_t dtdrt_offset; 353 const char *dtdrt_str; 354 const char *dtdrt_msg; 355 } _dt_droptab[] = { 356 { DTRACEDROP_DYNAMIC, 357 offsetof(dtrace_status_t, dtst_dyndrops), 358 "dynamic variable drop" }, 359 360 { DTRACEDROP_DYNRINSE, 361 offsetof(dtrace_status_t, dtst_dyndrops_rinsing), 362 "dynamic variable drop", " with non-empty rinsing list" }, 363 364 { DTRACEDROP_DYNDIRTY, 365 offsetof(dtrace_status_t, dtst_dyndrops_dirty), 366 "dynamic variable drop", " with non-empty dirty list" }, 367 368 { DTRACEDROP_SPEC, 369 offsetof(dtrace_status_t, dtst_specdrops), 370 "speculative drop" }, 371 372 { DTRACEDROP_SPECBUSY, 373 offsetof(dtrace_status_t, dtst_specdrops_busy), 374 "failed speculation", " (available buffer(s) still busy)" }, 375 376 { DTRACEDROP_SPECUNAVAIL, 377 offsetof(dtrace_status_t, dtst_specdrops_unavail), 378 "failed speculation", " (no speculative buffer available)" }, 379 380 { DTRACEDROP_STKSTROVERFLOW, 381 offsetof(dtrace_status_t, dtst_stkstroverflows), 382 "jstack()/ustack() string table overflow" }, 383 384 { DTRACEDROP_DBLERROR, 385 offsetof(dtrace_status_t, dtst_dblerrors), 386 "error", " in ERROR probe enabling" }, 387 388 { 0, 0, NULL } 389 }; 390 391 int 392 dt_handle_status(dtrace_hdl_t *dtp, dtrace_status_t *old, dtrace_status_t *new) 393 { 394 dtrace_dropdata_t drop; 395 char str[80], *s; 396 uintptr_t base = (uintptr_t)new, obase = (uintptr_t)old; 397 int i, size; 398 399 bzero(&drop, sizeof (drop)); 400 drop.dtdda_handle = dtp; 401 drop.dtdda_cpu = DTRACE_CPUALL; 402 drop.dtdda_msg = str; 403 404 /* 405 * First, check to see if we've been killed -- in which case we abort. 406 */ 407 if (new->dtst_killed && !old->dtst_killed) 408 return (dt_set_errno(dtp, EDT_BRICKED)); 409 410 for (i = 0; _dt_droptab[i].dtdrt_str != NULL; i++) { 411 uintptr_t naddr = base + _dt_droptab[i].dtdrt_offset; 412 uintptr_t oaddr = obase + _dt_droptab[i].dtdrt_offset; 413 414 uint64_t nval = *((uint64_t *)naddr); 415 uint64_t oval = *((uint64_t *)oaddr); 416 417 if (nval == oval) 418 continue; 419 420 if (dtp->dt_droptags) { 421 (void) snprintf(str, sizeof (str), "[%s] ", 422 dt_droptag(_dt_droptab[i].dtdrt_kind)); 423 s = &str[strlen(str)]; 424 size = sizeof (str) - (s - str); 425 } else { 426 s = str; 427 size = sizeof (str); 428 } 429 430 (void) snprintf(s, size, "%llu %s%s%s\n", nval - oval, 431 _dt_droptab[i].dtdrt_str, (nval - oval > 1) ? "s" : "", 432 _dt_droptab[i].dtdrt_msg != NULL ? 433 _dt_droptab[i].dtdrt_msg : ""); 434 435 drop.dtdda_kind = _dt_droptab[i].dtdrt_kind; 436 drop.dtdda_total = nval; 437 drop.dtdda_drops = nval - oval; 438 439 if (dtp->dt_drophdlr == NULL) 440 return (dt_set_errno(dtp, EDT_DROPABORT)); 441 442 if ((*dtp->dt_drophdlr)(&drop, 443 dtp->dt_droparg) == DTRACE_HANDLE_ABORT) 444 return (dt_set_errno(dtp, EDT_DROPABORT)); 445 } 446 447 return (0); 448 } 449 450 int 451 dt_handle_setopt(dtrace_hdl_t *dtp, dtrace_setoptdata_t *data) 452 { 453 void *arg = dtp->dt_setoptarg; 454 455 if (dtp->dt_setopthdlr == NULL) 456 return (0); 457 458 if ((*dtp->dt_setopthdlr)(data, arg) == DTRACE_HANDLE_ABORT) 459 return (dt_set_errno(dtp, EDT_DIRABORT)); 460 461 return (0); 462 } 463 464 int 465 dt_handle(dtrace_hdl_t *dtp, dtrace_probedata_t *data) 466 { 467 dtrace_eprobedesc_t *epd = data->dtpda_edesc; 468 int rval; 469 470 switch (epd->dtepd_uarg) { 471 case DT_ECB_ERROR: 472 rval = dt_handle_err(dtp, data); 473 break; 474 475 default: 476 return (DTRACE_CONSUME_THIS); 477 } 478 479 if (rval == 0) 480 return (DTRACE_CONSUME_NEXT); 481 482 return (DTRACE_CONSUME_ERROR); 483 } 484