17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ad4023c4Sdp  * Common Development and Distribution License (the "License").
6ad4023c4Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e5803b76SAdam H. Leventhal 
227c478bd9Sstevel@tonic-gate /*
23b9e93c10SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27e5803b76SAdam H. Leventhal /*
28e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
2954a20ab4SBryan Cantrill  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
30e5803b76SAdam H. Leventhal  */
31e5803b76SAdam H. Leventhal 
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <dt_impl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static const struct {
367c478bd9Sstevel@tonic-gate 	int err;
377c478bd9Sstevel@tonic-gate 	const char *msg;
387c478bd9Sstevel@tonic-gate } _dt_errlist[] = {
397c478bd9Sstevel@tonic-gate 	{ EDT_VERSION,	"Client requested version newer than library" },
407c478bd9Sstevel@tonic-gate 	{ EDT_VERSINVAL, "Version is not properly formatted or is too large" },
417c478bd9Sstevel@tonic-gate 	{ EDT_VERSUNDEF, "Requested version is not supported by compiler" },
427c478bd9Sstevel@tonic-gate 	{ EDT_VERSREDUCED, "Requested version conflicts with earlier setting" },
437c478bd9Sstevel@tonic-gate 	{ EDT_CTF,	"Unexpected libctf error" },
447c478bd9Sstevel@tonic-gate 	{ EDT_COMPILER, "Error in D program compilation" },
457c478bd9Sstevel@tonic-gate 	{ EDT_NOTUPREG,	"Insufficient tuple registers to generate code" },
467c478bd9Sstevel@tonic-gate 	{ EDT_NOMEM,	"Memory allocation failure" },
477c478bd9Sstevel@tonic-gate 	{ EDT_INT2BIG,	"Integer constant table limit exceeded" },
487c478bd9Sstevel@tonic-gate 	{ EDT_STR2BIG,	"String constant table limit exceeded" },
497c478bd9Sstevel@tonic-gate 	{ EDT_NOMOD,	"Unknown module name" },
507c478bd9Sstevel@tonic-gate 	{ EDT_NOPROV,	"Unknown provider name" },
517c478bd9Sstevel@tonic-gate 	{ EDT_NOPROBE,	"No probe matches description" },
527c478bd9Sstevel@tonic-gate 	{ EDT_NOSYM,	"Unknown symbol name" },
537c478bd9Sstevel@tonic-gate 	{ EDT_NOSYMADDR, "No symbol corresponds to address" },
547c478bd9Sstevel@tonic-gate 	{ EDT_NOTYPE,	"Unknown type name" },
557c478bd9Sstevel@tonic-gate 	{ EDT_NOVAR,	"Unknown variable name" },
567c478bd9Sstevel@tonic-gate 	{ EDT_NOAGG,	"Unknown aggregation name" },
577c478bd9Sstevel@tonic-gate 	{ EDT_BADSCOPE,	"Improper use of scoping operator in type name" },
587c478bd9Sstevel@tonic-gate 	{ EDT_BADSPEC,	"Overspecified probe description" },
597c478bd9Sstevel@tonic-gate 	{ EDT_BADSPCV,	"Undefined macro variable in probe description" },
607c478bd9Sstevel@tonic-gate 	{ EDT_BADID,	"Unknown probe identifier" },
617c478bd9Sstevel@tonic-gate 	{ EDT_NOTLOADED, "Module is no longer loaded" },
627c478bd9Sstevel@tonic-gate 	{ EDT_NOCTF,	"Module does not contain any CTF data" },
637c478bd9Sstevel@tonic-gate 	{ EDT_DATAMODEL, "Module and program data models do not match" },
647c478bd9Sstevel@tonic-gate 	{ EDT_DIFVERS,	"Library uses newer DIF version than kernel" },
657c478bd9Sstevel@tonic-gate 	{ EDT_BADAGG,	"Unknown aggregating action" },
667c478bd9Sstevel@tonic-gate 	{ EDT_FIO,	"Error occurred while reading from input stream" },
677c478bd9Sstevel@tonic-gate 	{ EDT_DIFINVAL,	"DIF program content is invalid" },
687c478bd9Sstevel@tonic-gate 	{ EDT_DIFSIZE,	"DIF program exceeds maximum program size" },
697c478bd9Sstevel@tonic-gate 	{ EDT_DIFFAULT,	"DIF program contains invalid pointer" },
707c478bd9Sstevel@tonic-gate 	{ EDT_BADPROBE,	"Invalid probe specification" },
717c478bd9Sstevel@tonic-gate 	{ EDT_BADPGLOB, "Probe description has too many globbing characters" },
727c478bd9Sstevel@tonic-gate 	{ EDT_NOSCOPE,	"Declaration scope stack underflow" },
737c478bd9Sstevel@tonic-gate 	{ EDT_NODECL,	"Declaration stack underflow" },
747c478bd9Sstevel@tonic-gate 	{ EDT_DMISMATCH, "Data record list does not match statement" },
757c478bd9Sstevel@tonic-gate 	{ EDT_DOFFSET,	"Data record offset exceeds buffer boundary" },
767c478bd9Sstevel@tonic-gate 	{ EDT_DALIGN,	"Data record has inappropriate alignment" },
777c478bd9Sstevel@tonic-gate 	{ EDT_BADOPTNAME, "Invalid option name" },
787c478bd9Sstevel@tonic-gate 	{ EDT_BADOPTVAL, "Invalid value for specified option" },
797c478bd9Sstevel@tonic-gate 	{ EDT_BADOPTCTX, "Option cannot be used from within a D program" },
807c478bd9Sstevel@tonic-gate 	{ EDT_CPPFORK,	"Failed to fork preprocessor" },
817c478bd9Sstevel@tonic-gate 	{ EDT_CPPEXEC,	"Failed to exec preprocessor" },
827c478bd9Sstevel@tonic-gate 	{ EDT_CPPENT,	"Preprocessor not found" },
837c478bd9Sstevel@tonic-gate 	{ EDT_CPPERR,	"Preprocessor failed to process input program" },
847c478bd9Sstevel@tonic-gate 	{ EDT_SYMOFLOW,	"Symbol table identifier space exhausted" },
857c478bd9Sstevel@tonic-gate 	{ EDT_ACTIVE,	"Operation illegal when tracing is active" },
867c478bd9Sstevel@tonic-gate 	{ EDT_DESTRUCTIVE, "Destructive actions not allowed" },
877c478bd9Sstevel@tonic-gate 	{ EDT_NOANON,	"No anonymous tracing state" },
887c478bd9Sstevel@tonic-gate 	{ EDT_ISANON,	"Can't claim anonymous state and enable probes" },
897c478bd9Sstevel@tonic-gate 	{ EDT_ENDTOOBIG, "END enablings exceed size of principal buffer" },
907c478bd9Sstevel@tonic-gate 	{ EDT_NOCONV,	"Failed to load type for printf conversion" },
917c478bd9Sstevel@tonic-gate 	{ EDT_BADCONV,	"Incomplete printf conversion" },
927c478bd9Sstevel@tonic-gate 	{ EDT_BADERROR,	"Invalid library ERROR action" },
937c478bd9Sstevel@tonic-gate 	{ EDT_ERRABORT,	"Abort due to error" },
947c478bd9Sstevel@tonic-gate 	{ EDT_DROPABORT, "Abort due to drop" },
957c478bd9Sstevel@tonic-gate 	{ EDT_DIRABORT,	"Abort explicitly directed" },
967c478bd9Sstevel@tonic-gate 	{ EDT_BADRVAL,	"Invalid return value from callback" },
977c478bd9Sstevel@tonic-gate 	{ EDT_BADNORMAL, "Invalid normalization" },
987c478bd9Sstevel@tonic-gate 	{ EDT_BUFTOOSMALL, "Enabling exceeds size of buffer" },
997c478bd9Sstevel@tonic-gate 	{ EDT_BADTRUNC, "Invalid truncation" },
1007c478bd9Sstevel@tonic-gate 	{ EDT_BUSY, "DTrace cannot be used when kernel debugger is active" },
1017c478bd9Sstevel@tonic-gate 	{ EDT_ACCESS, "DTrace requires additional privileges" },
102ad4023c4Sdp 	{ EDT_NOENT, "DTrace device not available on system" },
1037c478bd9Sstevel@tonic-gate 	{ EDT_BRICKED, "Abort due to systemic unresponsiveness" },
1047c478bd9Sstevel@tonic-gate 	{ EDT_HARDWIRE, "Failed to load language definitions" },
1057c478bd9Sstevel@tonic-gate 	{ EDT_ELFVERSION, "libelf is out-of-date with respect to libdtrace" },
1067c478bd9Sstevel@tonic-gate 	{ EDT_NOBUFFERED, "Attempt to buffer output without handler" },
1077c478bd9Sstevel@tonic-gate 	{ EDT_UNSTABLE, "Description matched an unstable set of probes" },
10830ef842dSbmc 	{ EDT_BADSETOPT, "Invalid setopt() library action" },
10930ef842dSbmc 	{ EDT_BADSTACKPC, "Invalid stack program counter size" },
11030ef842dSbmc 	{ EDT_BADAGGVAR, "Invalid aggregation variable identifier" },
111b9e93c10SJonathan Haslam 	{ EDT_OVERSION,	"Client requested deprecated version of library" },
11254a20ab4SBryan Cantrill 	{ EDT_ENABLING_ERR, "Failed to enable probe" },
113*a386cc11SRobert Mustacchi 	{ EDT_NOPROBES, "No probe sites found for declared provider" },
114*a386cc11SRobert Mustacchi 	{ EDT_CANTLOAD, "Failed to load module" },
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static const int _dt_nerr = sizeof (_dt_errlist) / sizeof (_dt_errlist[0]);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate const char *
dtrace_errmsg(dtrace_hdl_t * dtp,int error)1207c478bd9Sstevel@tonic-gate dtrace_errmsg(dtrace_hdl_t *dtp, int error)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	const char *str;
1237c478bd9Sstevel@tonic-gate 	int i;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (error == EDT_COMPILER && dtp != NULL && dtp->dt_errmsg[0] != '\0')
1267c478bd9Sstevel@tonic-gate 		str = dtp->dt_errmsg;
1277c478bd9Sstevel@tonic-gate 	else if (error == EDT_CTF && dtp != NULL && dtp->dt_ctferr != 0)
1287c478bd9Sstevel@tonic-gate 		str = ctf_errmsg(dtp->dt_ctferr);
1297c478bd9Sstevel@tonic-gate 	else if (error >= EDT_BASE && (error - EDT_BASE) < _dt_nerr) {
1307c478bd9Sstevel@tonic-gate 		for (i = 0; i < _dt_nerr; i++) {
1317c478bd9Sstevel@tonic-gate 			if (_dt_errlist[i].err == error)
1327c478bd9Sstevel@tonic-gate 				return (_dt_errlist[i].msg);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		str = NULL;
1357c478bd9Sstevel@tonic-gate 	} else
1367c478bd9Sstevel@tonic-gate 		str = strerror(error);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (str ? str : "Unknown error");
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
dtrace_errno(dtrace_hdl_t * dtp)1427c478bd9Sstevel@tonic-gate dtrace_errno(dtrace_hdl_t *dtp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	return (dtp->dt_errno);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int
dt_set_errno(dtrace_hdl_t * dtp,int err)1487c478bd9Sstevel@tonic-gate dt_set_errno(dtrace_hdl_t *dtp, int err)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	dtp->dt_errno = err;
1517c478bd9Sstevel@tonic-gate 	return (-1);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@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)1557c478bd9Sstevel@tonic-gate dt_set_errmsg(dtrace_hdl_t *dtp, const char *errtag, const char *region,
1567c478bd9Sstevel@tonic-gate     const char *filename, int lineno, const char *format, va_list ap)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	size_t len, n;
1597c478bd9Sstevel@tonic-gate 	char *p, *s;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	s = dtp->dt_errmsg;
1627c478bd9Sstevel@tonic-gate 	n = sizeof (dtp->dt_errmsg);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (errtag != NULL && (yypcb->pcb_cflags & DTRACE_C_ETAGS))
1657c478bd9Sstevel@tonic-gate 		(void) snprintf(s, n, "[%s] ", errtag);
1667c478bd9Sstevel@tonic-gate 	else
1677c478bd9Sstevel@tonic-gate 		s[0] = '\0';
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	len = strlen(dtp->dt_errmsg);
1707c478bd9Sstevel@tonic-gate 	s = dtp->dt_errmsg + len;
1717c478bd9Sstevel@tonic-gate 	n = sizeof (dtp->dt_errmsg) - len;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (filename == NULL)
1747c478bd9Sstevel@tonic-gate 		filename = dtp->dt_filetag;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (filename != NULL)
1777c478bd9Sstevel@tonic-gate 		(void) snprintf(s, n, "\"%s\", line %d: ", filename, lineno);
1787c478bd9Sstevel@tonic-gate 	else if (lineno != 0)
1797c478bd9Sstevel@tonic-gate 		(void) snprintf(s, n, "line %d: ", lineno);
1807c478bd9Sstevel@tonic-gate 	else if (region != NULL)
1817c478bd9Sstevel@tonic-gate 		(void) snprintf(s, n, "in %s: ", region);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	len = strlen(dtp->dt_errmsg);
1847c478bd9Sstevel@tonic-gate 	s = dtp->dt_errmsg + len;
1857c478bd9Sstevel@tonic-gate 	n = sizeof (dtp->dt_errmsg) - len;
1867c478bd9Sstevel@tonic-gate 	(void) vsnprintf(s, n, format, ap);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if ((p = strrchr(dtp->dt_errmsg, '\n')) != NULL)
1897c478bd9Sstevel@tonic-gate 		*p = '\0'; /* remove trailing \n from message buffer */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	dtp->dt_errtag = errtag;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1957c478bd9Sstevel@tonic-gate const char *
dtrace_faultstr(dtrace_hdl_t * dtp,int fault)1967c478bd9Sstevel@tonic-gate dtrace_faultstr(dtrace_hdl_t *dtp, int fault)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	int i;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	static const struct {
2017c478bd9Sstevel@tonic-gate 		int code;
2027c478bd9Sstevel@tonic-gate 		const char *str;
2037c478bd9Sstevel@tonic-gate 	} faults[] = {
2047c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_BADADDR,	"invalid address" },
2057c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_BADALIGN,	"invalid alignment" },
2067c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_ILLOP,	"illegal operation" },
2077c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_DIVZERO,	"divide-by-zero" },
2087c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_NOSCRATCH,	"out of scratch space" },
2097c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_KPRIV,	"invalid kernel access" },
2107c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_UPRIV,	"invalid user access" },
2117c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_TUPOFLOW,	"tuple stack overflow" },
212b8fac8e1Sjhaslam 		{ DTRACEFLT_BADSTACK,	"bad stack" },
2137c478bd9Sstevel@tonic-gate 		{ DTRACEFLT_LIBRARY,	"library-level fault" },
2147c478bd9Sstevel@tonic-gate 		{ 0,			NULL }
2157c478bd9Sstevel@tonic-gate 	};
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	for (i = 0; faults[i].str != NULL; i++) {
2187c478bd9Sstevel@tonic-gate 		if (faults[i].code == fault)
2197c478bd9Sstevel@tonic-gate 			return (faults[i].str);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	return ("unknown fault");
2237c478bd9Sstevel@tonic-gate }
224