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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <dt_program.h>
41 static const char _dt_errprog[] =
52 dtrace_handle_err(dtrace_hdl_t *dtp, dtrace_handle_err_f *hdlr, void *arg)
54 dtrace_prog_t *pgp = NULL;
56 dtrace_ecbdesc_t *edp;
59 * We don't currently support multiple error handlers.
61 if (dtp->dt_errhdlr != NULL)
62 return (dt_set_errno(dtp, EALREADY));
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.
69 if (dtp->dt_options[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET)
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)));
76 stp = dt_list_next(&pgp->dp_stmts);
79 edp = stp->ds_desc->dtsd_ecbdesc;
81 edp->dted_uarg = DT_ECB_ERROR;
84 dtp->dt_errhdlr = hdlr;
86 dtp->dt_errprog = pgp;
92 dtrace_handle_drop(dtrace_hdl_t *dtp, dtrace_handle_drop_f *hdlr, void *arg)
94 if (dtp->dt_drophdlr != NULL)
95 return (dt_set_errno(dtp, EALREADY));
97 dtp->dt_drophdlr = hdlr;
98 dtp->dt_droparg = arg;
104 dtrace_handle_proc(dtrace_hdl_t *dtp, dtrace_handle_proc_f *hdlr, void *arg)
106 if (dtp->dt_prochdlr != NULL)
107 return (dt_set_errno(dtp, EALREADY));
109 dtp->dt_prochdlr = hdlr;
110 dtp->dt_procarg = arg;
116 dtrace_handle_buffered(dtrace_hdl_t *dtp, dtrace_handle_buffered_f *hdlr,
119 if (dtp->dt_bufhdlr != NULL)
120 return (dt_set_errno(dtp, EALREADY));
123 return (dt_set_errno(dtp, EINVAL));
125 dtp->dt_bufhdlr = hdlr;
126 dtp->dt_bufarg = arg;
132 dtrace_handle_setopt(dtrace_hdl_t *dtp, dtrace_handle_setopt_f *hdlr,
136 return (dt_set_errno(dtp, EINVAL));
138 dtp->dt_setopthdlr = hdlr;
139 dtp->dt_setoptarg = arg;
144 #define DT_REC(type, ndx) *((type *)((uintptr_t)data->dtpda_data + \
145 epd->dtepd_rec[(ndx)].dtrd_offset))
148 dt_handle_err(dtrace_hdl_t *dtp, dtrace_probedata_t *data)
150 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *errepd;
151 dtrace_probedesc_t *pd = data->dtpda_pdesc, *errpd;
152 dtrace_errdata_t err;
159 const char *faultstr;
163 assert(epd->dtepd_uarg == DT_ECB_ERROR);
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));
170 * This is an error. We have the following items here: EPID,
171 * faulting action, DIF offset, fault code and faulting address.
173 epid = (uint32_t)DT_REC(uint64_t, 0);
175 if (dt_epid_lookup(dtp, epid, &errepd, &errpd) != 0)
176 return (dt_set_errno(dtp, EDT_BADERROR));
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);
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) +
192 str = (char *)alloca(len);
194 if (err.dteda_action == 0) {
195 (void) sprintf(where, "predicate");
197 (void) sprintf(where, "action #%d", err.dteda_action);
200 if (err.dteda_offset != -1) {
201 (void) sprintf(offinfo, " at DIF offset %d", err.dteda_offset);
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);
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);
227 if (dtp->dt_errhdlr == NULL)
228 return (dt_set_errno(dtp, EDT_ERRABORT));
230 if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)
231 return (dt_set_errno(dtp, EDT_ERRABORT));
237 dt_handle_liberr(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
238 const char *faultstr)
240 dtrace_probedesc_t *errpd = data->dtpda_pdesc;
241 dtrace_errdata_t err;
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;
254 len = strlen(faultstr) +
255 strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) +
256 strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) +
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);
270 if (dtp->dt_errhdlr == NULL)
271 return (dt_set_errno(dtp, EDT_ERRABORT));
273 if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)
274 return (dt_set_errno(dtp, EDT_ERRABORT));
279 #define DROPTAG(x) x, #x
281 static const struct {
282 dtrace_dropkind_t dtdrg_kind;
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) },
299 dt_droptag(dtrace_dropkind_t kind)
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);
308 return ("DTRACEDROP_UNKNOWN");
312 dt_handle_cpudrop(dtrace_hdl_t *dtp, processorid_t cpu,
313 dtrace_dropkind_t what, uint64_t howmany)
315 dtrace_dropdata_t drop;
319 assert(what == DTRACEDROP_PRINCIPAL || what == DTRACEDROP_AGGREGATION);
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;
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);
337 (void) snprintf(s, size, "%llu %sdrop%s on CPU %d\n",
338 howmany, what == DTRACEDROP_PRINCIPAL ? "" : "aggregation ",
339 howmany > 1 ? "s" : "", cpu);
341 if (dtp->dt_drophdlr == NULL)
342 return (dt_set_errno(dtp, EDT_DROPABORT));
344 if ((*dtp->dt_drophdlr)(&drop, dtp->dt_droparg) == DTRACE_HANDLE_ABORT)
345 return (dt_set_errno(dtp, EDT_DROPABORT));
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;
356 { DTRACEDROP_DYNAMIC,
357 offsetof(dtrace_status_t, dtst_dyndrops),
358 "dynamic variable drop" },
360 { DTRACEDROP_DYNRINSE,
361 offsetof(dtrace_status_t, dtst_dyndrops_rinsing),
362 "dynamic variable drop", " with non-empty rinsing list" },
364 { DTRACEDROP_DYNDIRTY,
365 offsetof(dtrace_status_t, dtst_dyndrops_dirty),
366 "dynamic variable drop", " with non-empty dirty list" },
369 offsetof(dtrace_status_t, dtst_specdrops),
370 "speculative drop" },
372 { DTRACEDROP_SPECBUSY,
373 offsetof(dtrace_status_t, dtst_specdrops_busy),
374 "failed speculation", " (available buffer(s) still busy)" },
376 { DTRACEDROP_SPECUNAVAIL,
377 offsetof(dtrace_status_t, dtst_specdrops_unavail),
378 "failed speculation", " (no speculative buffer available)" },
380 { DTRACEDROP_STKSTROVERFLOW,
381 offsetof(dtrace_status_t, dtst_stkstroverflows),
382 "jstack()/ustack() string table overflow" },
384 { DTRACEDROP_DBLERROR,
385 offsetof(dtrace_status_t, dtst_dblerrors),
386 "error", " in ERROR probe enabling" },
392 dt_handle_status(dtrace_hdl_t *dtp, dtrace_status_t *old, dtrace_status_t *new)
394 dtrace_dropdata_t drop;
396 uintptr_t base = (uintptr_t)new, obase = (uintptr_t)old;
399 bzero(&drop, sizeof (drop));
400 drop.dtdda_handle = dtp;
401 drop.dtdda_cpu = DTRACE_CPUALL;
402 drop.dtdda_msg = str;
405 * First, check to see if we've been killed -- in which case we abort.
407 if (new->dtst_killed && !old->dtst_killed)
408 return (dt_set_errno(dtp, EDT_BRICKED));
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;
414 uint64_t nval = *((uint64_t *)naddr);
415 uint64_t oval = *((uint64_t *)oaddr);
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);
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 : "");
435 drop.dtdda_kind = _dt_droptab[i].dtdrt_kind;
436 drop.dtdda_total = nval;
437 drop.dtdda_drops = nval - oval;
439 if (dtp->dt_drophdlr == NULL)
440 return (dt_set_errno(dtp, EDT_DROPABORT));
442 if ((*dtp->dt_drophdlr)(&drop,
443 dtp->dt_droparg) == DTRACE_HANDLE_ABORT)
444 return (dt_set_errno(dtp, EDT_DROPABORT));
451 dt_handle_setopt(dtrace_hdl_t *dtp, dtrace_setoptdata_t *data)
453 void *arg = dtp->dt_setoptarg;
455 if (dtp->dt_setopthdlr == NULL)
458 if ((*dtp->dt_setopthdlr)(data, arg) == DTRACE_HANDLE_ABORT)
459 return (dt_set_errno(dtp, EDT_DIRABORT));
465 dt_handle(dtrace_hdl_t *dtp, dtrace_probedata_t *data)
467 dtrace_eprobedesc_t *epd = data->dtpda_edesc;
470 switch (epd->dtepd_uarg) {
472 rval = dt_handle_err(dtp, data);
476 return (DTRACE_CONSUME_THIS);
480 return (DTRACE_CONSUME_NEXT);
482 return (DTRACE_CONSUME_ERROR);