Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_handle.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2007 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/2425#pragma ident "%Z%%M% %I% %E% SMI"2627#include <stddef.h>28#include <stdlib.h>29#include <strings.h>30#include <errno.h>31#include <unistd.h>32#include <assert.h>33#ifdef illumos34#include <alloca.h>35#endif3637#include <dt_impl.h>38#include <dt_oformat.h>39#include <dt_program.h>4041static const char _dt_errprog[] =42"dtrace:::ERROR"43"{"44" trace(arg1);"45" trace(arg2);"46" trace(arg3);"47" trace(arg4);"48" trace(arg5);"49"}";5051int52dtrace_handle_err(dtrace_hdl_t *dtp, dtrace_handle_err_f *hdlr, void *arg)53{54dtrace_prog_t *pgp = NULL;55dt_stmt_t *stp;56dtrace_ecbdesc_t *edp;5758/*59* We don't currently support multiple error handlers.60*/61if (dtp->dt_errhdlr != NULL)62return (dt_set_errno(dtp, EALREADY));6364/*65* If the DTRACEOPT_GRABANON is enabled, the anonymous enabling will66* already have a dtrace:::ERROR probe enabled; save 'hdlr' and 'arg'67* but do not bother compiling and enabling _dt_errprog.68*/69if (dtp->dt_options[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET)70goto out;7172if ((pgp = dtrace_program_strcompile(dtp, _dt_errprog,73DTRACE_PROBESPEC_NAME, DTRACE_C_ZDEFS, 0, NULL)) == NULL)74return (dt_set_errno(dtp, dtrace_errno(dtp)));7576stp = dt_list_next(&pgp->dp_stmts);77assert(stp != NULL);7879edp = stp->ds_desc->dtsd_ecbdesc;80assert(edp != NULL);81edp->dted_uarg = DT_ECB_ERROR;8283out:84dtp->dt_errhdlr = hdlr;85dtp->dt_errarg = arg;86dtp->dt_errprog = pgp;8788return (0);89}9091int92dtrace_handle_drop(dtrace_hdl_t *dtp, dtrace_handle_drop_f *hdlr, void *arg)93{94if (dtp->dt_drophdlr != NULL)95return (dt_set_errno(dtp, EALREADY));9697dtp->dt_drophdlr = hdlr;98dtp->dt_droparg = arg;99100return (0);101}102103int104dtrace_handle_proc(dtrace_hdl_t *dtp, dtrace_handle_proc_f *hdlr, void *arg)105{106if (dtp->dt_prochdlr != NULL)107return (dt_set_errno(dtp, EALREADY));108109dtp->dt_prochdlr = hdlr;110dtp->dt_procarg = arg;111112return (0);113}114115int116dtrace_handle_buffered(dtrace_hdl_t *dtp, dtrace_handle_buffered_f *hdlr,117void *arg)118{119if (dtp->dt_bufhdlr != NULL)120return (dt_set_errno(dtp, EALREADY));121122if (hdlr == NULL)123return (dt_set_errno(dtp, EINVAL));124125dtp->dt_bufhdlr = hdlr;126dtp->dt_bufarg = arg;127128return (0);129}130131int132dtrace_handle_setopt(dtrace_hdl_t *dtp, dtrace_handle_setopt_f *hdlr,133void *arg)134{135if (hdlr == NULL)136return (dt_set_errno(dtp, EINVAL));137138dtp->dt_setopthdlr = hdlr;139dtp->dt_setoptarg = arg;140141return (0);142}143144#define DT_REC(type, ndx) *((type *)((uintptr_t)data->dtpda_data + \145epd->dtepd_rec[(ndx)].dtrd_offset))146147static int148dt_handle_err(dtrace_hdl_t *dtp, dtrace_probedata_t *data)149{150dtrace_eprobedesc_t *epd = data->dtpda_edesc, *errepd;151dtrace_probedesc_t *pd = data->dtpda_pdesc, *errpd;152dtrace_errdata_t err;153dtrace_epid_t epid;154155char where[30];156char details[30];157char offinfo[30];158const int slop = 80;159const char *faultstr;160char *str;161int len;162163assert(epd->dtepd_uarg == DT_ECB_ERROR);164165if (epd->dtepd_nrecs != 5 || strcmp(pd->dtpd_provider, "dtrace") != 0 ||166strcmp(pd->dtpd_name, "ERROR") != 0)167return (dt_set_errno(dtp, EDT_BADERROR));168169/*170* This is an error. We have the following items here: EPID,171* faulting action, DIF offset, fault code and faulting address.172*/173epid = (uint32_t)DT_REC(uint64_t, 0);174175if (dt_epid_lookup(dtp, epid, &errepd, &errpd) != 0)176return (dt_set_errno(dtp, EDT_BADERROR));177178err.dteda_edesc = errepd;179err.dteda_pdesc = errpd;180err.dteda_cpu = data->dtpda_cpu;181err.dteda_action = (int)DT_REC(uint64_t, 1);182err.dteda_offset = (int)DT_REC(uint64_t, 2);183err.dteda_fault = (int)DT_REC(uint64_t, 3);184err.dteda_addr = DT_REC(uint64_t, 4);185186faultstr = dtrace_faultstr(dtp, err.dteda_fault);187len = sizeof (where) + sizeof (offinfo) + strlen(faultstr) +188strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) +189strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) +190slop;191192str = (char *)alloca(len);193194if (err.dteda_action == 0) {195(void) sprintf(where, "predicate");196} else {197(void) sprintf(where, "action #%d", err.dteda_action);198}199200if (err.dteda_offset != -1) {201(void) sprintf(offinfo, " at DIF offset %d", err.dteda_offset);202} else {203offinfo[0] = 0;204}205206switch (err.dteda_fault) {207case DTRACEFLT_BADADDR:208case DTRACEFLT_BADALIGN:209case DTRACEFLT_BADSTACK:210(void) sprintf(details, " (0x%llx)",211(u_longlong_t)err.dteda_addr);212break;213214default:215details[0] = 0;216}217218(void) snprintf(str, len, "error on enabled probe ID %u "219"(ID %u: %s:%s:%s:%s): %s%s in %s%s\n",220epid, errpd->dtpd_id, errpd->dtpd_provider,221errpd->dtpd_mod, errpd->dtpd_func,222errpd->dtpd_name, dtrace_faultstr(dtp, err.dteda_fault),223details, where, offinfo);224225err.dteda_msg = str;226227if (dtp->dt_errhdlr == NULL)228return (dt_set_errno(dtp, EDT_ERRABORT));229230if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)231return (dt_set_errno(dtp, EDT_ERRABORT));232233return (0);234}235236int237dt_handle_liberr(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,238const char *faultstr)239{240dtrace_probedesc_t *errpd = data->dtpda_pdesc;241dtrace_errdata_t err;242const int slop = 80;243char *str;244int len;245246err.dteda_edesc = data->dtpda_edesc;247err.dteda_pdesc = errpd;248err.dteda_cpu = data->dtpda_cpu;249err.dteda_action = -1;250err.dteda_offset = -1;251err.dteda_fault = DTRACEFLT_LIBRARY;252err.dteda_addr = 0;253254len = strlen(faultstr) +255strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) +256strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) +257slop;258259str = alloca(len);260261(void) snprintf(str, len, "error on enabled probe ID %u "262"(ID %u: %s:%s:%s:%s): %s\n",263data->dtpda_edesc->dtepd_epid,264errpd->dtpd_id, errpd->dtpd_provider,265errpd->dtpd_mod, errpd->dtpd_func,266errpd->dtpd_name, faultstr);267268err.dteda_msg = str;269270if (dtp->dt_errhdlr == NULL)271return (dt_set_errno(dtp, EDT_ERRABORT));272273if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)274return (dt_set_errno(dtp, EDT_ERRABORT));275276return (0);277}278279#define DROPTAG(x) x, #x280281static const struct {282dtrace_dropkind_t dtdrg_kind;283char *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};297298static const char *299dt_droptag(dtrace_dropkind_t kind)300{301int i;302303for (i = 0; _dt_droptags[i].dtdrg_tag != NULL; i++) {304if (_dt_droptags[i].dtdrg_kind == kind)305return (_dt_droptags[i].dtdrg_tag);306}307308return ("DTRACEDROP_UNKNOWN");309}310311int312dt_handle_cpudrop(dtrace_hdl_t *dtp, processorid_t cpu,313dtrace_dropkind_t what, uint64_t howmany)314{315dtrace_dropdata_t drop;316char str[80], *s;317int size;318struct timeval tv;319320assert(what == DTRACEDROP_PRINCIPAL || what == DTRACEDROP_AGGREGATION);321322bzero(&drop, sizeof (drop));323drop.dtdda_handle = dtp;324drop.dtdda_cpu = cpu;325drop.dtdda_kind = what;326drop.dtdda_drops = howmany;327drop.dtdda_msg = str;328329if (dtp->dt_droptags) {330(void) snprintf(str, sizeof (str), "[%s] ", dt_droptag(what));331s = &str[strlen(str)];332size = sizeof (str) - (s - str);333} else {334s = str;335size = sizeof (str);336}337338(void) snprintf(s, size, "%llu %sdrop%s on CPU %d\n",339(u_longlong_t)howmany,340what == DTRACEDROP_PRINCIPAL ? "" : "aggregation ",341howmany > 1 ? "s" : "", cpu);342343if (dtp->dt_oformat) {344(void) gettimeofday(&tv, NULL);345xo_emit("{:timestamp/%ld.%06ld} {:count/%ju} "346"{:total/%ju} {:kind/%d} {:msg/%s}",347tv.tv_sec, tv.tv_usec, (uintmax_t)drop.dtdda_drops,348(uintmax_t)drop.dtdda_total, drop.dtdda_kind,349drop.dtdda_msg);350}351352if (dtp->dt_drophdlr == NULL)353return (dt_set_errno(dtp, EDT_DROPABORT));354355if ((*dtp->dt_drophdlr)(&drop, dtp->dt_droparg) == DTRACE_HANDLE_ABORT)356return (dt_set_errno(dtp, EDT_DROPABORT));357358return (0);359}360361static const struct {362dtrace_dropkind_t dtdrt_kind;363uintptr_t dtdrt_offset;364const char *dtdrt_str;365const char *dtdrt_msg;366} _dt_droptab[] = {367{ DTRACEDROP_DYNAMIC,368offsetof(dtrace_status_t, dtst_dyndrops),369"dynamic variable drop" },370371{ DTRACEDROP_DYNRINSE,372offsetof(dtrace_status_t, dtst_dyndrops_rinsing),373"dynamic variable drop", " with non-empty rinsing list" },374375{ DTRACEDROP_DYNDIRTY,376offsetof(dtrace_status_t, dtst_dyndrops_dirty),377"dynamic variable drop", " with non-empty dirty list" },378379{ DTRACEDROP_SPEC,380offsetof(dtrace_status_t, dtst_specdrops),381"speculative drop" },382383{ DTRACEDROP_SPECBUSY,384offsetof(dtrace_status_t, dtst_specdrops_busy),385"failed speculation", " (available buffer(s) still busy)" },386387{ DTRACEDROP_SPECUNAVAIL,388offsetof(dtrace_status_t, dtst_specdrops_unavail),389"failed speculation", " (no speculative buffer available)" },390391{ DTRACEDROP_STKSTROVERFLOW,392offsetof(dtrace_status_t, dtst_stkstroverflows),393"jstack()/ustack() string table overflow" },394395{ DTRACEDROP_DBLERROR,396offsetof(dtrace_status_t, dtst_dblerrors),397"error", " in ERROR probe enabling" },398399{ 0, 0, NULL }400};401402int403dt_handle_status(dtrace_hdl_t *dtp, dtrace_status_t *old, dtrace_status_t *new)404{405dtrace_dropdata_t drop;406char str[80], *s;407uintptr_t base = (uintptr_t)new, obase = (uintptr_t)old;408int i, size;409struct timeval tv;410411bzero(&drop, sizeof (drop));412drop.dtdda_handle = dtp;413drop.dtdda_cpu = DTRACE_CPUALL;414drop.dtdda_msg = str;415416/*417* First, check to see if we've been killed -- in which case we abort.418*/419if (new->dtst_killed && !old->dtst_killed)420return (dt_set_errno(dtp, EDT_BRICKED));421422(void) gettimeofday(&tv, NULL);423424for (i = 0; _dt_droptab[i].dtdrt_str != NULL; i++) {425uintptr_t naddr = base + _dt_droptab[i].dtdrt_offset;426uintptr_t oaddr = obase + _dt_droptab[i].dtdrt_offset;427428uint64_t nval = *((uint64_t *)naddr);429uint64_t oval = *((uint64_t *)oaddr);430431if (nval == oval)432continue;433434if (dtp->dt_droptags) {435(void) snprintf(str, sizeof (str), "[%s] ",436dt_droptag(_dt_droptab[i].dtdrt_kind));437s = &str[strlen(str)];438size = sizeof (str) - (s - str);439} else {440s = str;441size = sizeof (str);442}443444(void) snprintf(s, size, "%llu %s%s%s\n",445(u_longlong_t)(nval - oval),446_dt_droptab[i].dtdrt_str, (nval - oval > 1) ? "s" : "",447_dt_droptab[i].dtdrt_msg != NULL ?448_dt_droptab[i].dtdrt_msg : "");449450drop.dtdda_kind = _dt_droptab[i].dtdrt_kind;451drop.dtdda_total = nval;452drop.dtdda_drops = nval - oval;453454if (dtp->dt_oformat) {455xo_open_instance("probes");456dt_oformat_drop(dtp, DTRACE_CPUALL);457xo_emit("{:timestamp/%ld.%06ld} {:count/%ju} "458"{:total/%ju} {:kind/%d} {:msg/%s}",459tv.tv_sec, tv.tv_usec, (uintmax_t)drop.dtdda_drops,460(uintmax_t)drop.dtdda_total, drop.dtdda_kind,461drop.dtdda_msg);462}463464if (dtp->dt_drophdlr == NULL) {465if (dtp->dt_oformat)466xo_close_instance("probes");467return (dt_set_errno(dtp, EDT_DROPABORT));468}469470if ((*dtp->dt_drophdlr)(&drop,471dtp->dt_droparg) == DTRACE_HANDLE_ABORT) {472if (dtp->dt_oformat)473xo_close_instance("probes");474return (dt_set_errno(dtp, EDT_DROPABORT));475}476477if (dtp->dt_oformat)478xo_close_instance("probes");479}480481return (0);482}483484int485dt_handle_setopt(dtrace_hdl_t *dtp, dtrace_setoptdata_t *data)486{487void *arg = dtp->dt_setoptarg;488489if (dtp->dt_setopthdlr == NULL)490return (0);491492if ((*dtp->dt_setopthdlr)(data, arg) == DTRACE_HANDLE_ABORT)493return (dt_set_errno(dtp, EDT_DIRABORT));494495return (0);496}497498int499dt_handle(dtrace_hdl_t *dtp, dtrace_probedata_t *data)500{501dtrace_eprobedesc_t *epd = data->dtpda_edesc;502int rval;503504switch (epd->dtepd_uarg) {505case DT_ECB_ERROR:506rval = dt_handle_err(dtp, data);507break;508509default:510return (DTRACE_CONSUME_THIS);511}512513if (rval == 0)514return (DTRACE_CONSUME_NEXT);515516return (DTRACE_CONSUME_ERROR);517}518519520