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