10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51677Sdp * Common Development and Distribution License (the "License").
61677Sdp * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*8803SJonathan.Haslam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <strings.h>
270Sstevel@tonic-gate #include <dt_impl.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate static const struct {
300Sstevel@tonic-gate int err;
310Sstevel@tonic-gate const char *msg;
320Sstevel@tonic-gate } _dt_errlist[] = {
330Sstevel@tonic-gate { EDT_VERSION, "Client requested version newer than library" },
340Sstevel@tonic-gate { EDT_VERSINVAL, "Version is not properly formatted or is too large" },
350Sstevel@tonic-gate { EDT_VERSUNDEF, "Requested version is not supported by compiler" },
360Sstevel@tonic-gate { EDT_VERSREDUCED, "Requested version conflicts with earlier setting" },
370Sstevel@tonic-gate { EDT_CTF, "Unexpected libctf error" },
380Sstevel@tonic-gate { EDT_COMPILER, "Error in D program compilation" },
390Sstevel@tonic-gate { EDT_NOREG, "Insufficient registers to generate code" },
400Sstevel@tonic-gate { EDT_NOTUPREG, "Insufficient tuple registers to generate code" },
410Sstevel@tonic-gate { EDT_NOMEM, "Memory allocation failure" },
420Sstevel@tonic-gate { EDT_INT2BIG, "Integer constant table limit exceeded" },
430Sstevel@tonic-gate { EDT_STR2BIG, "String constant table limit exceeded" },
440Sstevel@tonic-gate { EDT_NOMOD, "Unknown module name" },
450Sstevel@tonic-gate { EDT_NOPROV, "Unknown provider name" },
460Sstevel@tonic-gate { EDT_NOPROBE, "No probe matches description" },
470Sstevel@tonic-gate { EDT_NOSYM, "Unknown symbol name" },
480Sstevel@tonic-gate { EDT_NOSYMADDR, "No symbol corresponds to address" },
490Sstevel@tonic-gate { EDT_NOTYPE, "Unknown type name" },
500Sstevel@tonic-gate { EDT_NOVAR, "Unknown variable name" },
510Sstevel@tonic-gate { EDT_NOAGG, "Unknown aggregation name" },
520Sstevel@tonic-gate { EDT_BADSCOPE, "Improper use of scoping operator in type name" },
530Sstevel@tonic-gate { EDT_BADSPEC, "Overspecified probe description" },
540Sstevel@tonic-gate { EDT_BADSPCV, "Undefined macro variable in probe description" },
550Sstevel@tonic-gate { EDT_BADID, "Unknown probe identifier" },
560Sstevel@tonic-gate { EDT_NOTLOADED, "Module is no longer loaded" },
570Sstevel@tonic-gate { EDT_NOCTF, "Module does not contain any CTF data" },
580Sstevel@tonic-gate { EDT_DATAMODEL, "Module and program data models do not match" },
590Sstevel@tonic-gate { EDT_DIFVERS, "Library uses newer DIF version than kernel" },
600Sstevel@tonic-gate { EDT_BADAGG, "Unknown aggregating action" },
610Sstevel@tonic-gate { EDT_FIO, "Error occurred while reading from input stream" },
620Sstevel@tonic-gate { EDT_DIFINVAL, "DIF program content is invalid" },
630Sstevel@tonic-gate { EDT_DIFSIZE, "DIF program exceeds maximum program size" },
640Sstevel@tonic-gate { EDT_DIFFAULT, "DIF program contains invalid pointer" },
650Sstevel@tonic-gate { EDT_BADPROBE, "Invalid probe specification" },
660Sstevel@tonic-gate { EDT_BADPGLOB, "Probe description has too many globbing characters" },
670Sstevel@tonic-gate { EDT_NOSCOPE, "Declaration scope stack underflow" },
680Sstevel@tonic-gate { EDT_NODECL, "Declaration stack underflow" },
690Sstevel@tonic-gate { EDT_DMISMATCH, "Data record list does not match statement" },
700Sstevel@tonic-gate { EDT_DOFFSET, "Data record offset exceeds buffer boundary" },
710Sstevel@tonic-gate { EDT_DALIGN, "Data record has inappropriate alignment" },
720Sstevel@tonic-gate { EDT_BADOPTNAME, "Invalid option name" },
730Sstevel@tonic-gate { EDT_BADOPTVAL, "Invalid value for specified option" },
740Sstevel@tonic-gate { EDT_BADOPTCTX, "Option cannot be used from within a D program" },
750Sstevel@tonic-gate { EDT_CPPFORK, "Failed to fork preprocessor" },
760Sstevel@tonic-gate { EDT_CPPEXEC, "Failed to exec preprocessor" },
770Sstevel@tonic-gate { EDT_CPPENT, "Preprocessor not found" },
780Sstevel@tonic-gate { EDT_CPPERR, "Preprocessor failed to process input program" },
790Sstevel@tonic-gate { EDT_SYMOFLOW, "Symbol table identifier space exhausted" },
800Sstevel@tonic-gate { EDT_ACTIVE, "Operation illegal when tracing is active" },
810Sstevel@tonic-gate { EDT_DESTRUCTIVE, "Destructive actions not allowed" },
820Sstevel@tonic-gate { EDT_NOANON, "No anonymous tracing state" },
830Sstevel@tonic-gate { EDT_ISANON, "Can't claim anonymous state and enable probes" },
840Sstevel@tonic-gate { EDT_ENDTOOBIG, "END enablings exceed size of principal buffer" },
850Sstevel@tonic-gate { EDT_NOCONV, "Failed to load type for printf conversion" },
860Sstevel@tonic-gate { EDT_BADCONV, "Incomplete printf conversion" },
870Sstevel@tonic-gate { EDT_BADERROR, "Invalid library ERROR action" },
880Sstevel@tonic-gate { EDT_ERRABORT, "Abort due to error" },
890Sstevel@tonic-gate { EDT_DROPABORT, "Abort due to drop" },
900Sstevel@tonic-gate { EDT_DIRABORT, "Abort explicitly directed" },
910Sstevel@tonic-gate { EDT_BADRVAL, "Invalid return value from callback" },
920Sstevel@tonic-gate { EDT_BADNORMAL, "Invalid normalization" },
930Sstevel@tonic-gate { EDT_BUFTOOSMALL, "Enabling exceeds size of buffer" },
940Sstevel@tonic-gate { EDT_BADTRUNC, "Invalid truncation" },
950Sstevel@tonic-gate { EDT_BUSY, "DTrace cannot be used when kernel debugger is active" },
960Sstevel@tonic-gate { EDT_ACCESS, "DTrace requires additional privileges" },
971677Sdp { EDT_NOENT, "DTrace device not available on system" },
980Sstevel@tonic-gate { EDT_BRICKED, "Abort due to systemic unresponsiveness" },
990Sstevel@tonic-gate { EDT_HARDWIRE, "Failed to load language definitions" },
1000Sstevel@tonic-gate { EDT_ELFVERSION, "libelf is out-of-date with respect to libdtrace" },
1010Sstevel@tonic-gate { EDT_NOBUFFERED, "Attempt to buffer output without handler" },
1020Sstevel@tonic-gate { EDT_UNSTABLE, "Description matched an unstable set of probes" },
1031017Sbmc { EDT_BADSETOPT, "Invalid setopt() library action" },
1041017Sbmc { EDT_BADSTACKPC, "Invalid stack program counter size" },
1051017Sbmc { EDT_BADAGGVAR, "Invalid aggregation variable identifier" },
106*8803SJonathan.Haslam@Sun.COM { EDT_OVERSION, "Client requested deprecated version of library" },
107*8803SJonathan.Haslam@Sun.COM { EDT_ENABLING_ERR, "Failed to enable probe" }
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate static const int _dt_nerr = sizeof (_dt_errlist) / sizeof (_dt_errlist[0]);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate const char *
dtrace_errmsg(dtrace_hdl_t * dtp,int error)1130Sstevel@tonic-gate dtrace_errmsg(dtrace_hdl_t *dtp, int error)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate const char *str;
1160Sstevel@tonic-gate int i;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if (error == EDT_COMPILER && dtp != NULL && dtp->dt_errmsg[0] != '\0')
1190Sstevel@tonic-gate str = dtp->dt_errmsg;
1200Sstevel@tonic-gate else if (error == EDT_CTF && dtp != NULL && dtp->dt_ctferr != 0)
1210Sstevel@tonic-gate str = ctf_errmsg(dtp->dt_ctferr);
1220Sstevel@tonic-gate else if (error >= EDT_BASE && (error - EDT_BASE) < _dt_nerr) {
1230Sstevel@tonic-gate for (i = 0; i < _dt_nerr; i++) {
1240Sstevel@tonic-gate if (_dt_errlist[i].err == error)
1250Sstevel@tonic-gate return (_dt_errlist[i].msg);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate str = NULL;
1280Sstevel@tonic-gate } else
1290Sstevel@tonic-gate str = strerror(error);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate return (str ? str : "Unknown error");
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate int
dtrace_errno(dtrace_hdl_t * dtp)1350Sstevel@tonic-gate dtrace_errno(dtrace_hdl_t *dtp)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate return (dtp->dt_errno);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate int
dt_set_errno(dtrace_hdl_t * dtp,int err)1410Sstevel@tonic-gate dt_set_errno(dtrace_hdl_t *dtp, int err)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate dtp->dt_errno = err;
1440Sstevel@tonic-gate return (-1);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate void
dt_set_errmsg(dtrace_hdl_t * dtp,const char * errtag,const char * region,const char * filename,int lineno,const char * format,va_list ap)1480Sstevel@tonic-gate dt_set_errmsg(dtrace_hdl_t *dtp, const char *errtag, const char *region,
1490Sstevel@tonic-gate const char *filename, int lineno, const char *format, va_list ap)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate size_t len, n;
1520Sstevel@tonic-gate char *p, *s;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate s = dtp->dt_errmsg;
1550Sstevel@tonic-gate n = sizeof (dtp->dt_errmsg);
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (errtag != NULL && (yypcb->pcb_cflags & DTRACE_C_ETAGS))
1580Sstevel@tonic-gate (void) snprintf(s, n, "[%s] ", errtag);
1590Sstevel@tonic-gate else
1600Sstevel@tonic-gate s[0] = '\0';
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate len = strlen(dtp->dt_errmsg);
1630Sstevel@tonic-gate s = dtp->dt_errmsg + len;
1640Sstevel@tonic-gate n = sizeof (dtp->dt_errmsg) - len;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (filename == NULL)
1670Sstevel@tonic-gate filename = dtp->dt_filetag;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (filename != NULL)
1700Sstevel@tonic-gate (void) snprintf(s, n, "\"%s\", line %d: ", filename, lineno);
1710Sstevel@tonic-gate else if (lineno != 0)
1720Sstevel@tonic-gate (void) snprintf(s, n, "line %d: ", lineno);
1730Sstevel@tonic-gate else if (region != NULL)
1740Sstevel@tonic-gate (void) snprintf(s, n, "in %s: ", region);
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate len = strlen(dtp->dt_errmsg);
1770Sstevel@tonic-gate s = dtp->dt_errmsg + len;
1780Sstevel@tonic-gate n = sizeof (dtp->dt_errmsg) - len;
1790Sstevel@tonic-gate (void) vsnprintf(s, n, format, ap);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if ((p = strrchr(dtp->dt_errmsg, '\n')) != NULL)
1820Sstevel@tonic-gate *p = '\0'; /* remove trailing \n from message buffer */
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate dtp->dt_errtag = errtag;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /*ARGSUSED*/
1880Sstevel@tonic-gate const char *
dtrace_faultstr(dtrace_hdl_t * dtp,int fault)1890Sstevel@tonic-gate dtrace_faultstr(dtrace_hdl_t *dtp, int fault)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate int i;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate static const struct {
1940Sstevel@tonic-gate int code;
1950Sstevel@tonic-gate const char *str;
1960Sstevel@tonic-gate } faults[] = {
1970Sstevel@tonic-gate { DTRACEFLT_BADADDR, "invalid address" },
1980Sstevel@tonic-gate { DTRACEFLT_BADALIGN, "invalid alignment" },
1990Sstevel@tonic-gate { DTRACEFLT_ILLOP, "illegal operation" },
2000Sstevel@tonic-gate { DTRACEFLT_DIVZERO, "divide-by-zero" },
2010Sstevel@tonic-gate { DTRACEFLT_NOSCRATCH, "out of scratch space" },
2020Sstevel@tonic-gate { DTRACEFLT_KPRIV, "invalid kernel access" },
2030Sstevel@tonic-gate { DTRACEFLT_UPRIV, "invalid user access" },
2040Sstevel@tonic-gate { DTRACEFLT_TUPOFLOW, "tuple stack overflow" },
2053682Sjhaslam { DTRACEFLT_BADSTACK, "bad stack" },
2060Sstevel@tonic-gate { DTRACEFLT_LIBRARY, "library-level fault" },
2070Sstevel@tonic-gate { 0, NULL }
2080Sstevel@tonic-gate };
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate for (i = 0; faults[i].str != NULL; i++) {
2110Sstevel@tonic-gate if (faults[i].code == fault)
2120Sstevel@tonic-gate return (faults[i].str);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate return ("unknown fault");
2160Sstevel@tonic-gate }
217