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