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