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