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