dt_error.c revision 178561
1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb/*
22178479Sjb * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23178479Sjb * Use is subject to license terms.
24178479Sjb */
25178479Sjb
26178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
27178479Sjb
28178561Sjb#include <string.h>
29178479Sjb#include <strings.h>
30178479Sjb#include <dt_impl.h>
31178479Sjb
32178479Sjbstatic const struct {
33178479Sjb	int err;
34178479Sjb	const char *msg;
35178479Sjb} _dt_errlist[] = {
36178479Sjb	{ EDT_VERSION,	"Client requested version newer than library" },
37178479Sjb	{ EDT_VERSINVAL, "Version is not properly formatted or is too large" },
38178479Sjb	{ EDT_VERSUNDEF, "Requested version is not supported by compiler" },
39178479Sjb	{ EDT_VERSREDUCED, "Requested version conflicts with earlier setting" },
40178479Sjb	{ EDT_CTF,	"Unexpected libctf error" },
41178479Sjb	{ EDT_COMPILER, "Error in D program compilation" },
42178479Sjb	{ EDT_NOREG,	"Insufficient registers to generate code" },
43178479Sjb	{ EDT_NOTUPREG,	"Insufficient tuple registers to generate code" },
44178479Sjb	{ EDT_NOMEM,	"Memory allocation failure" },
45178479Sjb	{ EDT_INT2BIG,	"Integer constant table limit exceeded" },
46178479Sjb	{ EDT_STR2BIG,	"String constant table limit exceeded" },
47178479Sjb	{ EDT_NOMOD,	"Unknown module name" },
48178479Sjb	{ EDT_NOPROV,	"Unknown provider name" },
49178479Sjb	{ EDT_NOPROBE,	"No probe matches description" },
50178479Sjb	{ EDT_NOSYM,	"Unknown symbol name" },
51178479Sjb	{ EDT_NOSYMADDR, "No symbol corresponds to address" },
52178479Sjb	{ EDT_NOTYPE,	"Unknown type name" },
53178479Sjb	{ EDT_NOVAR,	"Unknown variable name" },
54178479Sjb	{ EDT_NOAGG,	"Unknown aggregation name" },
55178479Sjb	{ EDT_BADSCOPE,	"Improper use of scoping operator in type name" },
56178479Sjb	{ EDT_BADSPEC,	"Overspecified probe description" },
57178479Sjb	{ EDT_BADSPCV,	"Undefined macro variable in probe description" },
58178479Sjb	{ EDT_BADID,	"Unknown probe identifier" },
59178479Sjb	{ EDT_NOTLOADED, "Module is no longer loaded" },
60178479Sjb	{ EDT_NOCTF,	"Module does not contain any CTF data" },
61178479Sjb	{ EDT_DATAMODEL, "Module and program data models do not match" },
62178479Sjb	{ EDT_DIFVERS,	"Library uses newer DIF version than kernel" },
63178479Sjb	{ EDT_BADAGG,	"Unknown aggregating action" },
64178479Sjb	{ EDT_FIO,	"Error occurred while reading from input stream" },
65178479Sjb	{ EDT_DIFINVAL,	"DIF program content is invalid" },
66178479Sjb	{ EDT_DIFSIZE,	"DIF program exceeds maximum program size" },
67178479Sjb	{ EDT_DIFFAULT,	"DIF program contains invalid pointer" },
68178479Sjb	{ EDT_BADPROBE,	"Invalid probe specification" },
69178479Sjb	{ EDT_BADPGLOB, "Probe description has too many globbing characters" },
70178479Sjb	{ EDT_NOSCOPE,	"Declaration scope stack underflow" },
71178479Sjb	{ EDT_NODECL,	"Declaration stack underflow" },
72178479Sjb	{ EDT_DMISMATCH, "Data record list does not match statement" },
73178479Sjb	{ EDT_DOFFSET,	"Data record offset exceeds buffer boundary" },
74178479Sjb	{ EDT_DALIGN,	"Data record has inappropriate alignment" },
75178479Sjb	{ EDT_BADOPTNAME, "Invalid option name" },
76178479Sjb	{ EDT_BADOPTVAL, "Invalid value for specified option" },
77178479Sjb	{ EDT_BADOPTCTX, "Option cannot be used from within a D program" },
78178479Sjb	{ EDT_CPPFORK,	"Failed to fork preprocessor" },
79178479Sjb	{ EDT_CPPEXEC,	"Failed to exec preprocessor" },
80178479Sjb	{ EDT_CPPENT,	"Preprocessor not found" },
81178479Sjb	{ EDT_CPPERR,	"Preprocessor failed to process input program" },
82178479Sjb	{ EDT_SYMOFLOW,	"Symbol table identifier space exhausted" },
83178479Sjb	{ EDT_ACTIVE,	"Operation illegal when tracing is active" },
84178479Sjb	{ EDT_DESTRUCTIVE, "Destructive actions not allowed" },
85178479Sjb	{ EDT_NOANON,	"No anonymous tracing state" },
86178479Sjb	{ EDT_ISANON,	"Can't claim anonymous state and enable probes" },
87178479Sjb	{ EDT_ENDTOOBIG, "END enablings exceed size of principal buffer" },
88178479Sjb	{ EDT_NOCONV,	"Failed to load type for printf conversion" },
89178479Sjb	{ EDT_BADCONV,	"Incomplete printf conversion" },
90178479Sjb	{ EDT_BADERROR,	"Invalid library ERROR action" },
91178479Sjb	{ EDT_ERRABORT,	"Abort due to error" },
92178479Sjb	{ EDT_DROPABORT, "Abort due to drop" },
93178479Sjb	{ EDT_DIRABORT,	"Abort explicitly directed" },
94178479Sjb	{ EDT_BADRVAL,	"Invalid return value from callback" },
95178479Sjb	{ EDT_BADNORMAL, "Invalid normalization" },
96178479Sjb	{ EDT_BUFTOOSMALL, "Enabling exceeds size of buffer" },
97178479Sjb	{ EDT_BADTRUNC, "Invalid truncation" },
98178479Sjb	{ EDT_BUSY, "DTrace cannot be used when kernel debugger is active" },
99178479Sjb	{ EDT_ACCESS, "DTrace requires additional privileges" },
100178479Sjb	{ EDT_NOENT, "DTrace device not available on system" },
101178479Sjb	{ EDT_BRICKED, "Abort due to systemic unresponsiveness" },
102178479Sjb	{ EDT_HARDWIRE, "Failed to load language definitions" },
103178479Sjb	{ EDT_ELFVERSION, "libelf is out-of-date with respect to libdtrace" },
104178479Sjb	{ EDT_NOBUFFERED, "Attempt to buffer output without handler" },
105178479Sjb	{ EDT_UNSTABLE, "Description matched an unstable set of probes" },
106178479Sjb	{ EDT_BADSETOPT, "Invalid setopt() library action" },
107178479Sjb	{ EDT_BADSTACKPC, "Invalid stack program counter size" },
108178479Sjb	{ EDT_BADAGGVAR, "Invalid aggregation variable identifier" },
109178479Sjb	{ EDT_OVERSION,	"Client requested deprecated version of library" }
110178479Sjb};
111178479Sjb
112178479Sjbstatic const int _dt_nerr = sizeof (_dt_errlist) / sizeof (_dt_errlist[0]);
113178479Sjb
114178479Sjbconst char *
115178479Sjbdtrace_errmsg(dtrace_hdl_t *dtp, int error)
116178479Sjb{
117178479Sjb	const char *str;
118178479Sjb	int i;
119178479Sjb
120178479Sjb	if (error == EDT_COMPILER && dtp != NULL && dtp->dt_errmsg[0] != '\0')
121178479Sjb		str = dtp->dt_errmsg;
122178479Sjb	else if (error == EDT_CTF && dtp != NULL && dtp->dt_ctferr != 0)
123178479Sjb		str = ctf_errmsg(dtp->dt_ctferr);
124178479Sjb	else if (error >= EDT_BASE && (error - EDT_BASE) < _dt_nerr) {
125178479Sjb		for (i = 0; i < _dt_nerr; i++) {
126178479Sjb			if (_dt_errlist[i].err == error)
127178479Sjb				return (_dt_errlist[i].msg);
128178479Sjb		}
129178479Sjb		str = NULL;
130178479Sjb	} else
131178479Sjb		str = strerror(error);
132178479Sjb
133178479Sjb	return (str ? str : "Unknown error");
134178479Sjb}
135178479Sjb
136178479Sjbint
137178479Sjbdtrace_errno(dtrace_hdl_t *dtp)
138178479Sjb{
139178479Sjb	return (dtp->dt_errno);
140178479Sjb}
141178479Sjb
142178561Sjb#if defined(sun)
143178479Sjbint
144178479Sjbdt_set_errno(dtrace_hdl_t *dtp, int err)
145178479Sjb{
146178479Sjb	dtp->dt_errno = err;
147178479Sjb	return (-1);
148178479Sjb}
149178561Sjb#else
150178561Sjbint
151178561Sjb_dt_set_errno(dtrace_hdl_t *dtp, int err, const char *errfile, int errline)
152178561Sjb{
153178561Sjb	dtp->dt_errno = err;
154178561Sjb	dtp->dt_errfile = errfile;
155178561Sjb	dtp->dt_errline = errline;
156178561Sjb	return (-1);
157178561Sjb}
158178479Sjb
159178561Sjbvoid dt_get_errloc(dtrace_hdl_t *dtp, const char **p_errfile, int *p_errline)
160178561Sjb{
161178561Sjb	*p_errfile = dtp->dt_errfile;
162178561Sjb	*p_errline = dtp->dt_errline;
163178561Sjb}
164178561Sjb#endif
165178561Sjb
166178479Sjbvoid
167178479Sjbdt_set_errmsg(dtrace_hdl_t *dtp, const char *errtag, const char *region,
168178479Sjb    const char *filename, int lineno, const char *format, va_list ap)
169178479Sjb{
170178479Sjb	size_t len, n;
171178479Sjb	char *p, *s;
172178479Sjb
173178479Sjb	s = dtp->dt_errmsg;
174178479Sjb	n = sizeof (dtp->dt_errmsg);
175178479Sjb
176178479Sjb	if (errtag != NULL && (yypcb->pcb_cflags & DTRACE_C_ETAGS))
177178479Sjb		(void) snprintf(s, n, "[%s] ", errtag);
178178479Sjb	else
179178479Sjb		s[0] = '\0';
180178479Sjb
181178479Sjb	len = strlen(dtp->dt_errmsg);
182178479Sjb	s = dtp->dt_errmsg + len;
183178479Sjb	n = sizeof (dtp->dt_errmsg) - len;
184178479Sjb
185178479Sjb	if (filename == NULL)
186178479Sjb		filename = dtp->dt_filetag;
187178479Sjb
188178479Sjb	if (filename != NULL)
189178479Sjb		(void) snprintf(s, n, "\"%s\", line %d: ", filename, lineno);
190178479Sjb	else if (lineno != 0)
191178479Sjb		(void) snprintf(s, n, "line %d: ", lineno);
192178479Sjb	else if (region != NULL)
193178479Sjb		(void) snprintf(s, n, "in %s: ", region);
194178479Sjb
195178479Sjb	len = strlen(dtp->dt_errmsg);
196178479Sjb	s = dtp->dt_errmsg + len;
197178479Sjb	n = sizeof (dtp->dt_errmsg) - len;
198178479Sjb	(void) vsnprintf(s, n, format, ap);
199178479Sjb
200178479Sjb	if ((p = strrchr(dtp->dt_errmsg, '\n')) != NULL)
201178479Sjb		*p = '\0'; /* remove trailing \n from message buffer */
202178479Sjb
203178479Sjb	dtp->dt_errtag = errtag;
204178479Sjb}
205178479Sjb
206178479Sjb/*ARGSUSED*/
207178479Sjbconst char *
208178479Sjbdtrace_faultstr(dtrace_hdl_t *dtp, int fault)
209178479Sjb{
210178479Sjb	int i;
211178479Sjb
212178479Sjb	static const struct {
213178479Sjb		int code;
214178479Sjb		const char *str;
215178479Sjb	} faults[] = {
216178479Sjb		{ DTRACEFLT_BADADDR,	"invalid address" },
217178479Sjb		{ DTRACEFLT_BADALIGN,	"invalid alignment" },
218178479Sjb		{ DTRACEFLT_ILLOP,	"illegal operation" },
219178479Sjb		{ DTRACEFLT_DIVZERO,	"divide-by-zero" },
220178479Sjb		{ DTRACEFLT_NOSCRATCH,	"out of scratch space" },
221178479Sjb		{ DTRACEFLT_KPRIV,	"invalid kernel access" },
222178479Sjb		{ DTRACEFLT_UPRIV,	"invalid user access" },
223178479Sjb		{ DTRACEFLT_TUPOFLOW,	"tuple stack overflow" },
224178479Sjb		{ DTRACEFLT_BADSTACK,	"bad stack" },
225178479Sjb		{ DTRACEFLT_LIBRARY,	"library-level fault" },
226178479Sjb		{ 0,			NULL }
227178479Sjb	};
228178479Sjb
229178479Sjb	for (i = 0; faults[i].str != NULL; i++) {
230178479Sjb		if (faults[i].code == fault)
231178479Sjb			return (faults[i].str);
232178479Sjb	}
233178479Sjb
234178479Sjb	return ("unknown fault");
235178479Sjb}
236