xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_error.c (revision 6dcefcac2b043be030851e03a721607b414b666b)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2012 by Delphix. All rights reserved.
28  */
29 
30 #include <string.h>
31 #include <strings.h>
32 #include <dt_impl.h>
33 
34 static const struct {
35 	int err;
36 	const char *msg;
37 } _dt_errlist[] = {
38 	{ EDT_VERSION,	"Client requested version newer than library" },
39 	{ EDT_VERSINVAL, "Version is not properly formatted or is too large" },
40 	{ EDT_VERSUNDEF, "Requested version is not supported by compiler" },
41 	{ EDT_VERSREDUCED, "Requested version conflicts with earlier setting" },
42 	{ EDT_CTF,	"Unexpected libctf error" },
43 	{ EDT_COMPILER, "Error in D program compilation" },
44 	{ EDT_NOTUPREG,	"Insufficient tuple registers to generate code" },
45 	{ EDT_NOMEM,	"Memory allocation failure" },
46 	{ EDT_INT2BIG,	"Integer constant table limit exceeded" },
47 	{ EDT_STR2BIG,	"String constant table limit exceeded" },
48 	{ EDT_NOMOD,	"Unknown module name" },
49 	{ EDT_NOPROV,	"Unknown provider name" },
50 	{ EDT_NOPROBE,	"No probe matches description" },
51 	{ EDT_NOSYM,	"Unknown symbol name" },
52 	{ EDT_NOSYMADDR, "No symbol corresponds to address" },
53 	{ EDT_NOTYPE,	"Unknown type name" },
54 	{ EDT_NOVAR,	"Unknown variable name" },
55 	{ EDT_NOAGG,	"Unknown aggregation name" },
56 	{ EDT_BADSCOPE,	"Improper use of scoping operator in type name" },
57 	{ EDT_BADSPEC,	"Overspecified probe description" },
58 	{ EDT_BADSPCV,	"Undefined macro variable in probe description" },
59 	{ EDT_BADID,	"Unknown probe identifier" },
60 	{ EDT_NOTLOADED, "Module is no longer loaded" },
61 	{ EDT_NOCTF,	"Module does not contain any CTF data" },
62 	{ EDT_DATAMODEL, "Module and program data models do not match" },
63 	{ EDT_DIFVERS,	"Library uses newer DIF version than kernel" },
64 	{ EDT_BADAGG,	"Unknown aggregating action" },
65 	{ EDT_FIO,	"Error occurred while reading from input stream" },
66 	{ EDT_DIFINVAL,	"DIF program content is invalid" },
67 	{ EDT_DIFSIZE,	"DIF program exceeds maximum program size" },
68 	{ EDT_DIFFAULT,	"DIF program contains invalid pointer" },
69 	{ EDT_BADPROBE,	"Invalid probe specification" },
70 	{ EDT_BADPGLOB, "Probe description has too many globbing characters" },
71 	{ EDT_NOSCOPE,	"Declaration scope stack underflow" },
72 	{ EDT_NODECL,	"Declaration stack underflow" },
73 	{ EDT_DMISMATCH, "Data record list does not match statement" },
74 	{ EDT_DOFFSET,	"Data record offset exceeds buffer boundary" },
75 	{ EDT_DALIGN,	"Data record has inappropriate alignment" },
76 	{ EDT_BADOPTNAME, "Invalid option name" },
77 	{ EDT_BADOPTVAL, "Invalid value for specified option" },
78 	{ EDT_BADOPTCTX, "Option cannot be used from within a D program" },
79 	{ EDT_CPPFORK,	"Failed to fork preprocessor" },
80 	{ EDT_CPPEXEC,	"Failed to exec preprocessor" },
81 	{ EDT_CPPENT,	"Preprocessor not found" },
82 	{ EDT_CPPERR,	"Preprocessor failed to process input program" },
83 	{ EDT_SYMOFLOW,	"Symbol table identifier space exhausted" },
84 	{ EDT_ACTIVE,	"Operation illegal when tracing is active" },
85 	{ EDT_DESTRUCTIVE, "Destructive actions not allowed" },
86 	{ EDT_NOANON,	"No anonymous tracing state" },
87 	{ EDT_ISANON,	"Can't claim anonymous state and enable probes" },
88 	{ EDT_ENDTOOBIG, "END enablings exceed size of principal buffer" },
89 	{ EDT_NOCONV,	"Failed to load type for printf conversion" },
90 	{ EDT_BADCONV,	"Incomplete printf conversion" },
91 	{ EDT_BADERROR,	"Invalid library ERROR action" },
92 	{ EDT_ERRABORT,	"Abort due to error" },
93 	{ EDT_DROPABORT, "Abort due to drop" },
94 	{ EDT_DIRABORT,	"Abort explicitly directed" },
95 	{ EDT_BADRVAL,	"Invalid return value from callback" },
96 	{ EDT_BADNORMAL, "Invalid normalization" },
97 	{ EDT_BUFTOOSMALL, "Enabling exceeds size of buffer" },
98 	{ EDT_BADTRUNC, "Invalid truncation" },
99 	{ EDT_BUSY, "DTrace cannot be used when kernel debugger is active" },
100 	{ EDT_ACCESS, "DTrace requires additional privileges" },
101 	{ EDT_NOENT, "DTrace device not available on system" },
102 	{ EDT_BRICKED, "Abort due to systemic unresponsiveness" },
103 	{ EDT_HARDWIRE, "Failed to load language definitions" },
104 	{ EDT_ELFVERSION, "libelf is out-of-date with respect to libdtrace" },
105 	{ EDT_NOBUFFERED, "Attempt to buffer output without handler" },
106 	{ EDT_UNSTABLE, "Description matched an unstable set of probes" },
107 	{ EDT_BADSETOPT, "Invalid setopt() library action" },
108 	{ EDT_BADSTACKPC, "Invalid stack program counter size" },
109 	{ EDT_BADAGGVAR, "Invalid aggregation variable identifier" },
110 	{ EDT_OVERSION,	"Client requested deprecated version of library" },
111 	{ EDT_ENABLING_ERR, "Failed to enable probe" }
112 };
113 
114 static const int _dt_nerr = sizeof (_dt_errlist) / sizeof (_dt_errlist[0]);
115 
116 const char *
117 dtrace_errmsg(dtrace_hdl_t *dtp, int error)
118 {
119 	const char *str;
120 	int i;
121 
122 	if (error == EDT_COMPILER && dtp != NULL && dtp->dt_errmsg[0] != '\0')
123 		str = dtp->dt_errmsg;
124 	else if (error == EDT_CTF && dtp != NULL && dtp->dt_ctferr != 0)
125 		str = ctf_errmsg(dtp->dt_ctferr);
126 	else if (error >= EDT_BASE && (error - EDT_BASE) < _dt_nerr) {
127 		for (i = 0; i < _dt_nerr; i++) {
128 			if (_dt_errlist[i].err == error)
129 				return (_dt_errlist[i].msg);
130 		}
131 		str = NULL;
132 	} else
133 		str = strerror(error);
134 
135 	return (str ? str : "Unknown error");
136 }
137 
138 int
139 dtrace_errno(dtrace_hdl_t *dtp)
140 {
141 	return (dtp->dt_errno);
142 }
143 
144 #if defined(sun)
145 int
146 dt_set_errno(dtrace_hdl_t *dtp, int err)
147 {
148 	dtp->dt_errno = err;
149 	return (-1);
150 }
151 #else
152 int
153 _dt_set_errno(dtrace_hdl_t *dtp, int err, const char *errfile, int errline)
154 {
155 	dtp->dt_errno = err;
156 	dtp->dt_errfile = errfile;
157 	dtp->dt_errline = errline;
158 	return (-1);
159 }
160 
161 void dt_get_errloc(dtrace_hdl_t *dtp, const char **p_errfile, int *p_errline)
162 {
163 	*p_errfile = dtp->dt_errfile;
164 	*p_errline = dtp->dt_errline;
165 }
166 #endif
167 
168 void
169 dt_set_errmsg(dtrace_hdl_t *dtp, const char *errtag, const char *region,
170     const char *filename, int lineno, const char *format, va_list ap)
171 {
172 	size_t len, n;
173 	char *p, *s;
174 
175 	s = dtp->dt_errmsg;
176 	n = sizeof (dtp->dt_errmsg);
177 
178 	if (errtag != NULL && (yypcb->pcb_cflags & DTRACE_C_ETAGS))
179 		(void) snprintf(s, n, "[%s] ", errtag);
180 	else
181 		s[0] = '\0';
182 
183 	len = strlen(dtp->dt_errmsg);
184 	s = dtp->dt_errmsg + len;
185 	n = sizeof (dtp->dt_errmsg) - len;
186 
187 	if (filename == NULL)
188 		filename = dtp->dt_filetag;
189 
190 	if (filename != NULL)
191 		(void) snprintf(s, n, "\"%s\", line %d: ", filename, lineno);
192 	else if (lineno != 0)
193 		(void) snprintf(s, n, "line %d: ", lineno);
194 	else if (region != NULL)
195 		(void) snprintf(s, n, "in %s: ", region);
196 
197 	len = strlen(dtp->dt_errmsg);
198 	s = dtp->dt_errmsg + len;
199 	n = sizeof (dtp->dt_errmsg) - len;
200 	(void) vsnprintf(s, n, format, ap);
201 
202 	if ((p = strrchr(dtp->dt_errmsg, '\n')) != NULL)
203 		*p = '\0'; /* remove trailing \n from message buffer */
204 
205 	dtp->dt_errtag = errtag;
206 }
207 
208 /*ARGSUSED*/
209 const char *
210 dtrace_faultstr(dtrace_hdl_t *dtp, int fault)
211 {
212 	int i;
213 
214 	static const struct {
215 		int code;
216 		const char *str;
217 	} faults[] = {
218 		{ DTRACEFLT_BADADDR,	"invalid address" },
219 		{ DTRACEFLT_BADALIGN,	"invalid alignment" },
220 		{ DTRACEFLT_ILLOP,	"illegal operation" },
221 		{ DTRACEFLT_DIVZERO,	"divide-by-zero" },
222 		{ DTRACEFLT_NOSCRATCH,	"out of scratch space" },
223 		{ DTRACEFLT_KPRIV,	"invalid kernel access" },
224 		{ DTRACEFLT_UPRIV,	"invalid user access" },
225 		{ DTRACEFLT_TUPOFLOW,	"tuple stack overflow" },
226 		{ DTRACEFLT_BADSTACK,	"bad stack" },
227 		{ DTRACEFLT_LIBRARY,	"library-level fault" },
228 		{ 0,			NULL }
229 	};
230 
231 	for (i = 0; faults[i].str != NULL; i++) {
232 		if (faults[i].code == fault)
233 			return (faults[i].str);
234 	}
235 
236 	return ("unknown fault");
237 }
238