dt_error.c revision 264040
1178479Sjb/* 2178479Sjb * CDDL HEADER START 3178479Sjb * 4178479Sjb * The contents of this file are subject to the terms of the 5178479Sjb * Common Development and Distribution License (the "License"). 6178479Sjb * You may not use this file except in compliance with the License. 7178479Sjb * 8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178479Sjb * or http://www.opensolaris.org/os/licensing. 10178479Sjb * See the License for the specific language governing permissions 11178479Sjb * and limitations under the License. 12178479Sjb * 13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178479Sjb * If applicable, add the following below this CDDL HEADER, with the 16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178479Sjb * 19178479Sjb * CDDL HEADER END 20178479Sjb */ 21253725Spfg 22178479Sjb/* 23210767Srpaulo * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24178479Sjb * Use is subject to license terms. 25178479Sjb */ 26178479Sjb 27250812Smarkj/* 28250812Smarkj * Copyright (c) 2012 by Delphix. All rights reserved. 29264040Spfg * Copyright (c) 2013, Joyent, Inc. All rights reserved. 30250812Smarkj */ 31250812Smarkj 32178561Sjb#include <string.h> 33178479Sjb#include <strings.h> 34178479Sjb#include <dt_impl.h> 35178479Sjb 36178479Sjbstatic const struct { 37178479Sjb int err; 38178479Sjb const char *msg; 39178479Sjb} _dt_errlist[] = { 40178479Sjb { EDT_VERSION, "Client requested version newer than library" }, 41178479Sjb { EDT_VERSINVAL, "Version is not properly formatted or is too large" }, 42178479Sjb { EDT_VERSUNDEF, "Requested version is not supported by compiler" }, 43178479Sjb { EDT_VERSREDUCED, "Requested version conflicts with earlier setting" }, 44178479Sjb { EDT_CTF, "Unexpected libctf error" }, 45178479Sjb { EDT_COMPILER, "Error in D program compilation" }, 46178479Sjb { EDT_NOTUPREG, "Insufficient tuple registers to generate code" }, 47178479Sjb { EDT_NOMEM, "Memory allocation failure" }, 48178479Sjb { EDT_INT2BIG, "Integer constant table limit exceeded" }, 49178479Sjb { EDT_STR2BIG, "String constant table limit exceeded" }, 50178479Sjb { EDT_NOMOD, "Unknown module name" }, 51178479Sjb { EDT_NOPROV, "Unknown provider name" }, 52178479Sjb { EDT_NOPROBE, "No probe matches description" }, 53178479Sjb { EDT_NOSYM, "Unknown symbol name" }, 54178479Sjb { EDT_NOSYMADDR, "No symbol corresponds to address" }, 55178479Sjb { EDT_NOTYPE, "Unknown type name" }, 56178479Sjb { EDT_NOVAR, "Unknown variable name" }, 57178479Sjb { EDT_NOAGG, "Unknown aggregation name" }, 58178479Sjb { EDT_BADSCOPE, "Improper use of scoping operator in type name" }, 59178479Sjb { EDT_BADSPEC, "Overspecified probe description" }, 60178479Sjb { EDT_BADSPCV, "Undefined macro variable in probe description" }, 61178479Sjb { EDT_BADID, "Unknown probe identifier" }, 62178479Sjb { EDT_NOTLOADED, "Module is no longer loaded" }, 63178479Sjb { EDT_NOCTF, "Module does not contain any CTF data" }, 64178479Sjb { EDT_DATAMODEL, "Module and program data models do not match" }, 65178479Sjb { EDT_DIFVERS, "Library uses newer DIF version than kernel" }, 66178479Sjb { EDT_BADAGG, "Unknown aggregating action" }, 67178479Sjb { EDT_FIO, "Error occurred while reading from input stream" }, 68178479Sjb { EDT_DIFINVAL, "DIF program content is invalid" }, 69178479Sjb { EDT_DIFSIZE, "DIF program exceeds maximum program size" }, 70178479Sjb { EDT_DIFFAULT, "DIF program contains invalid pointer" }, 71178479Sjb { EDT_BADPROBE, "Invalid probe specification" }, 72178479Sjb { EDT_BADPGLOB, "Probe description has too many globbing characters" }, 73178479Sjb { EDT_NOSCOPE, "Declaration scope stack underflow" }, 74178479Sjb { EDT_NODECL, "Declaration stack underflow" }, 75178479Sjb { EDT_DMISMATCH, "Data record list does not match statement" }, 76178479Sjb { EDT_DOFFSET, "Data record offset exceeds buffer boundary" }, 77178479Sjb { EDT_DALIGN, "Data record has inappropriate alignment" }, 78178479Sjb { EDT_BADOPTNAME, "Invalid option name" }, 79178479Sjb { EDT_BADOPTVAL, "Invalid value for specified option" }, 80178479Sjb { EDT_BADOPTCTX, "Option cannot be used from within a D program" }, 81178479Sjb { EDT_CPPFORK, "Failed to fork preprocessor" }, 82178479Sjb { EDT_CPPEXEC, "Failed to exec preprocessor" }, 83178479Sjb { EDT_CPPENT, "Preprocessor not found" }, 84178479Sjb { EDT_CPPERR, "Preprocessor failed to process input program" }, 85178479Sjb { EDT_SYMOFLOW, "Symbol table identifier space exhausted" }, 86178479Sjb { EDT_ACTIVE, "Operation illegal when tracing is active" }, 87178479Sjb { EDT_DESTRUCTIVE, "Destructive actions not allowed" }, 88178479Sjb { EDT_NOANON, "No anonymous tracing state" }, 89178479Sjb { EDT_ISANON, "Can't claim anonymous state and enable probes" }, 90178479Sjb { EDT_ENDTOOBIG, "END enablings exceed size of principal buffer" }, 91178479Sjb { EDT_NOCONV, "Failed to load type for printf conversion" }, 92178479Sjb { EDT_BADCONV, "Incomplete printf conversion" }, 93178479Sjb { EDT_BADERROR, "Invalid library ERROR action" }, 94178479Sjb { EDT_ERRABORT, "Abort due to error" }, 95178479Sjb { EDT_DROPABORT, "Abort due to drop" }, 96178479Sjb { EDT_DIRABORT, "Abort explicitly directed" }, 97178479Sjb { EDT_BADRVAL, "Invalid return value from callback" }, 98178479Sjb { EDT_BADNORMAL, "Invalid normalization" }, 99178479Sjb { EDT_BUFTOOSMALL, "Enabling exceeds size of buffer" }, 100178479Sjb { EDT_BADTRUNC, "Invalid truncation" }, 101178479Sjb { EDT_BUSY, "DTrace cannot be used when kernel debugger is active" }, 102178479Sjb { EDT_ACCESS, "DTrace requires additional privileges" }, 103178479Sjb { EDT_NOENT, "DTrace device not available on system" }, 104178479Sjb { EDT_BRICKED, "Abort due to systemic unresponsiveness" }, 105178479Sjb { EDT_HARDWIRE, "Failed to load language definitions" }, 106178479Sjb { EDT_ELFVERSION, "libelf is out-of-date with respect to libdtrace" }, 107178479Sjb { EDT_NOBUFFERED, "Attempt to buffer output without handler" }, 108178479Sjb { EDT_UNSTABLE, "Description matched an unstable set of probes" }, 109178479Sjb { EDT_BADSETOPT, "Invalid setopt() library action" }, 110178479Sjb { EDT_BADSTACKPC, "Invalid stack program counter size" }, 111178479Sjb { EDT_BADAGGVAR, "Invalid aggregation variable identifier" }, 112210767Srpaulo { EDT_OVERSION, "Client requested deprecated version of library" }, 113264040Spfg { EDT_ENABLING_ERR, "Failed to enable probe" }, 114264040Spfg { EDT_NOPROBES, "No probe sites found for declared provider" } 115178479Sjb}; 116178479Sjb 117178479Sjbstatic const int _dt_nerr = sizeof (_dt_errlist) / sizeof (_dt_errlist[0]); 118178479Sjb 119178479Sjbconst char * 120178479Sjbdtrace_errmsg(dtrace_hdl_t *dtp, int error) 121178479Sjb{ 122178479Sjb const char *str; 123178479Sjb int i; 124178479Sjb 125178479Sjb if (error == EDT_COMPILER && dtp != NULL && dtp->dt_errmsg[0] != '\0') 126178479Sjb str = dtp->dt_errmsg; 127178479Sjb else if (error == EDT_CTF && dtp != NULL && dtp->dt_ctferr != 0) 128178479Sjb str = ctf_errmsg(dtp->dt_ctferr); 129178479Sjb else if (error >= EDT_BASE && (error - EDT_BASE) < _dt_nerr) { 130178479Sjb for (i = 0; i < _dt_nerr; i++) { 131178479Sjb if (_dt_errlist[i].err == error) 132178479Sjb return (_dt_errlist[i].msg); 133178479Sjb } 134178479Sjb str = NULL; 135178479Sjb } else 136178479Sjb str = strerror(error); 137178479Sjb 138178479Sjb return (str ? str : "Unknown error"); 139178479Sjb} 140178479Sjb 141178479Sjbint 142178479Sjbdtrace_errno(dtrace_hdl_t *dtp) 143178479Sjb{ 144178479Sjb return (dtp->dt_errno); 145178479Sjb} 146178479Sjb 147178561Sjb#if defined(sun) 148178479Sjbint 149178479Sjbdt_set_errno(dtrace_hdl_t *dtp, int err) 150178479Sjb{ 151178479Sjb dtp->dt_errno = err; 152178479Sjb return (-1); 153178479Sjb} 154178561Sjb#else 155178561Sjbint 156178561Sjb_dt_set_errno(dtrace_hdl_t *dtp, int err, const char *errfile, int errline) 157178561Sjb{ 158178561Sjb dtp->dt_errno = err; 159178561Sjb dtp->dt_errfile = errfile; 160178561Sjb dtp->dt_errline = errline; 161178561Sjb return (-1); 162178561Sjb} 163178479Sjb 164178561Sjbvoid dt_get_errloc(dtrace_hdl_t *dtp, const char **p_errfile, int *p_errline) 165178561Sjb{ 166178561Sjb *p_errfile = dtp->dt_errfile; 167178561Sjb *p_errline = dtp->dt_errline; 168178561Sjb} 169178561Sjb#endif 170178561Sjb 171178479Sjbvoid 172178479Sjbdt_set_errmsg(dtrace_hdl_t *dtp, const char *errtag, const char *region, 173178479Sjb const char *filename, int lineno, const char *format, va_list ap) 174178479Sjb{ 175178479Sjb size_t len, n; 176178479Sjb char *p, *s; 177178479Sjb 178178479Sjb s = dtp->dt_errmsg; 179178479Sjb n = sizeof (dtp->dt_errmsg); 180178479Sjb 181178479Sjb if (errtag != NULL && (yypcb->pcb_cflags & DTRACE_C_ETAGS)) 182178479Sjb (void) snprintf(s, n, "[%s] ", errtag); 183178479Sjb else 184178479Sjb s[0] = '\0'; 185178479Sjb 186178479Sjb len = strlen(dtp->dt_errmsg); 187178479Sjb s = dtp->dt_errmsg + len; 188178479Sjb n = sizeof (dtp->dt_errmsg) - len; 189178479Sjb 190178479Sjb if (filename == NULL) 191178479Sjb filename = dtp->dt_filetag; 192178479Sjb 193178479Sjb if (filename != NULL) 194178479Sjb (void) snprintf(s, n, "\"%s\", line %d: ", filename, lineno); 195178479Sjb else if (lineno != 0) 196178479Sjb (void) snprintf(s, n, "line %d: ", lineno); 197178479Sjb else if (region != NULL) 198178479Sjb (void) snprintf(s, n, "in %s: ", region); 199178479Sjb 200178479Sjb len = strlen(dtp->dt_errmsg); 201178479Sjb s = dtp->dt_errmsg + len; 202178479Sjb n = sizeof (dtp->dt_errmsg) - len; 203178479Sjb (void) vsnprintf(s, n, format, ap); 204178479Sjb 205178479Sjb if ((p = strrchr(dtp->dt_errmsg, '\n')) != NULL) 206178479Sjb *p = '\0'; /* remove trailing \n from message buffer */ 207178479Sjb 208178479Sjb dtp->dt_errtag = errtag; 209178479Sjb} 210178479Sjb 211178479Sjb/*ARGSUSED*/ 212178479Sjbconst char * 213178479Sjbdtrace_faultstr(dtrace_hdl_t *dtp, int fault) 214178479Sjb{ 215178479Sjb int i; 216178479Sjb 217178479Sjb static const struct { 218178479Sjb int code; 219178479Sjb const char *str; 220178479Sjb } faults[] = { 221178479Sjb { DTRACEFLT_BADADDR, "invalid address" }, 222178479Sjb { DTRACEFLT_BADALIGN, "invalid alignment" }, 223178479Sjb { DTRACEFLT_ILLOP, "illegal operation" }, 224178479Sjb { DTRACEFLT_DIVZERO, "divide-by-zero" }, 225178479Sjb { DTRACEFLT_NOSCRATCH, "out of scratch space" }, 226178479Sjb { DTRACEFLT_KPRIV, "invalid kernel access" }, 227178479Sjb { DTRACEFLT_UPRIV, "invalid user access" }, 228178479Sjb { DTRACEFLT_TUPOFLOW, "tuple stack overflow" }, 229178479Sjb { DTRACEFLT_BADSTACK, "bad stack" }, 230178479Sjb { DTRACEFLT_LIBRARY, "library-level fault" }, 231178479Sjb { 0, NULL } 232178479Sjb }; 233178479Sjb 234178479Sjb for (i = 0; faults[i].str != NULL; i++) { 235178479Sjb if (faults[i].code == fault) 236178479Sjb return (faults[i].str); 237178479Sjb } 238178479Sjb 239178479Sjb return ("unknown fault"); 240178479Sjb} 241