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