| 1 |
'\" |
| 2 |
'\" Copyright (c) 1989-1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH Tcl_AddErrorInfo 3 8.5 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_GetReturnOptions, Tcl_SetReturnOptions, Tcl_AddErrorInfo, Tcl_AppendObjToErrorInfo, Tcl_AddObjErrorInfo, Tcl_SetObjErrorCode, Tcl_SetErrorCode, Tcl_SetErrorCodeVA, Tcl_SetErrorLine, Tcl_GetErrorLine, Tcl_PosixError, Tcl_LogCommandInfo \- retrieve or record information about errors and other return options |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
Tcl_Obj * |
| 18 |
\fBTcl_GetReturnOptions\fR(\fIinterp, code\fR) |
| 19 |
.sp |
| 20 |
int |
| 21 |
\fBTcl_SetReturnOptions\fR(\fIinterp, options\fR) |
| 22 |
.sp |
| 23 |
\fBTcl_AddErrorInfo\fR(\fIinterp, message\fR) |
| 24 |
.sp |
| 25 |
\fBTcl_AppendObjToErrorInfo\fR(\fIinterp, objPtr\fR) |
| 26 |
.sp |
| 27 |
\fBTcl_AddObjErrorInfo\fR(\fIinterp, message, length\fR) |
| 28 |
.sp |
| 29 |
\fBTcl_SetObjErrorCode\fR(\fIinterp, errorObjPtr\fR) |
| 30 |
.sp |
| 31 |
\fBTcl_SetErrorCode\fR(\fIinterp, element, element, ... \fB(char *)NULL\fR) |
| 32 |
.sp |
| 33 |
\fBTcl_SetErrorCodeVA\fR(\fIinterp, argList\fR) |
| 34 |
.sp |
| 35 |
\fBTcl_GetErrorLine\fR(\fIinterp\fR) |
| 36 |
.sp |
| 37 |
\fBTcl_SetErrorLine\fR(\fIinterp, lineNum\fR) |
| 38 |
.sp |
| 39 |
const char * |
| 40 |
\fBTcl_PosixError\fR(\fIinterp\fR) |
| 41 |
.sp |
| 42 |
void |
| 43 |
\fBTcl_LogCommandInfo\fR(\fIinterp, script, command, commandLength\fR) |
| 44 |
.SH ARGUMENTS |
| 45 |
.AS Tcl_Interp commandLength |
| 46 |
.AP Tcl_Interp *interp in |
| 47 |
Interpreter in which to record information. |
| 48 |
.AP int code |
| 49 |
The code returned from script evaluation. |
| 50 |
.AP Tcl_Obj *options |
| 51 |
A dictionary of return options. |
| 52 |
.AP "const char" *message in |
| 53 |
For \fBTcl_AddErrorInfo\fR, |
| 54 |
this is a conventional C string to append to the \fB\-errorinfo\fR return option. |
| 55 |
For \fBTcl_AddObjErrorInfo\fR, |
| 56 |
this points to the first byte of an array of \fIlength\fR bytes |
| 57 |
containing a string to append to the \fB\-errorinfo\fR return option. |
| 58 |
This byte array may contain embedded null bytes |
| 59 |
unless \fIlength\fR is negative. |
| 60 |
.AP Tcl_Obj *objPtr in |
| 61 |
A message to be appended to the \fB\-errorinfo\fR return option |
| 62 |
in the form of a Tcl_Obj value. |
| 63 |
.AP int length in |
| 64 |
The number of bytes to copy from \fImessage\fR when |
| 65 |
appending to the \fB\-errorinfo\fR return option. |
| 66 |
If negative, all bytes up to the first null byte are used. |
| 67 |
.AP Tcl_Obj *errorObjPtr in |
| 68 |
The \fB\-errorcode\fR return option will be set to this value. |
| 69 |
.AP "const char" *element in |
| 70 |
String to record as one element of the \fB\-errorcode\fR return option. |
| 71 |
Last \fIelement\fR argument must be (char *)NULL. |
| 72 |
.AP va_list argList in |
| 73 |
An argument list which must have been initialized using |
| 74 |
\fBva_start\fR, and cleared using \fBva_end\fR. |
| 75 |
.AP int lineNum |
| 76 |
The line number of a script where an error occurred. |
| 77 |
.AP "const char" *script in |
| 78 |
Pointer to first character in script containing command (must be <= command) |
| 79 |
.AP "const char" *command in |
| 80 |
Pointer to first character in command that generated the error |
| 81 |
.AP int commandLength in |
| 82 |
Number of bytes in command; -1 means use all bytes up to first null byte |
| 83 |
.BE |
| 84 |
.SH DESCRIPTION |
| 85 |
.PP |
| 86 |
The \fBTcl_SetReturnOptions\fR and \fBTcl_GetReturnOptions\fR |
| 87 |
routines expose the same capabilities as the \fBreturn\fR and |
| 88 |
\fBcatch\fR commands, respectively, in the form of a C interface. |
| 89 |
.PP |
| 90 |
\fBTcl_GetReturnOptions\fR retrieves the dictionary of return options |
| 91 |
from an interpreter following a script evaluation. |
| 92 |
Routines such as \fBTcl_Eval\fR are called to evaluate a |
| 93 |
script in an interpreter. These routines return an integer |
| 94 |
completion code. These routines also leave in the interpreter |
| 95 |
both a result and a dictionary of return options generated |
| 96 |
by script evaluation. Just as \fBTcl_GetObjResult\fR retrieves |
| 97 |
the result, \fBTcl_GetReturnOptions\fR retrieves the dictionary |
| 98 |
of return options. The integer completion code should be |
| 99 |
passed as the \fIcode\fR argument to \fBTcl_GetReturnOptions\fR |
| 100 |
so that all required options will be present in the dictionary. |
| 101 |
Specifically, a \fIcode\fR value of \fBTCL_ERROR\fR will |
| 102 |
ensure that entries for the keys \fB\-errorinfo\fR, |
| 103 |
\fB\-errorcode\fR, and \fB\-errorline\fR will appear in the |
| 104 |
dictionary. Also, the entries for the keys \fB\-code\fR |
| 105 |
and \fB\-level\fR will be adjusted if necessary to agree |
| 106 |
with the value of \fIcode\fR. The \fB(Tcl_Obj *)\fR returned |
| 107 |
by \fBTcl_GetReturnOptions\fR points to an unshared |
| 108 |
\fBTcl_Obj\fR with reference count of zero. The dictionary |
| 109 |
may be written to, either adding, removing, or overwriting |
| 110 |
any entries in it, without the need to check for a shared value. |
| 111 |
As with any \fBTcl_Obj\fR with reference count of zero, it is up to |
| 112 |
the caller to arrange for its disposal with \fBTcl_DecrRefCount\fR or |
| 113 |
to a reference to it via \fBTcl_IncrRefCount\fR (or one of the many |
| 114 |
functions that call that, notably including \fBTcl_SetObjResult\fR and |
| 115 |
\fBTcl_SetVar2Ex\fR). |
| 116 |
.PP |
| 117 |
A typical usage for \fBTcl_GetReturnOptions\fR is to |
| 118 |
retrieve the stack trace when script evaluation returns |
| 119 |
\fBTCL_ERROR\fR, like so: |
| 120 |
.PP |
| 121 |
.CS |
| 122 |
int code = Tcl_Eval(interp, script); |
| 123 |
if (code == TCL_ERROR) { |
| 124 |
Tcl_Obj *options = \fBTcl_GetReturnOptions\fR(interp, code); |
| 125 |
Tcl_Obj *key = Tcl_NewStringObj("-errorinfo", -1); |
| 126 |
Tcl_Obj *stackTrace; |
| 127 |
Tcl_IncrRefCount(key); |
| 128 |
Tcl_DictObjGet(NULL, options, key, &stackTrace); |
| 129 |
Tcl_DecrRefCount(key); |
| 130 |
/* Do something with stackTrace */ |
| 131 |
Tcl_DecrRefCount(options); |
| 132 |
} |
| 133 |
.CE |
| 134 |
.PP |
| 135 |
\fBTcl_SetReturnOptions\fR sets the return options |
| 136 |
of \fIinterp\fR to be \fIoptions\fR. If \fIoptions\fR |
| 137 |
contains any invalid value for any key, TCL_ERROR will |
| 138 |
be returned, and the interp result will be set to an |
| 139 |
appropriate error message. Otherwise, a completion code |
| 140 |
in agreement with the \fB\-code\fR and \fB\-level\fR |
| 141 |
keys in \fIoptions\fR will be returned. |
| 142 |
.PP |
| 143 |
As an example, Tcl's \fBreturn\fR command itself could |
| 144 |
be implemented in terms of \fBTcl_SetReturnOptions\fR |
| 145 |
like so: |
| 146 |
.PP |
| 147 |
.CS |
| 148 |
if ((objc % 2) == 0) { /* explicit result argument */ |
| 149 |
objc--; |
| 150 |
Tcl_SetObjResult(interp, objv[objc]); |
| 151 |
} |
| 152 |
return \fBTcl_SetReturnOptions\fR(interp, Tcl_NewListObj(objc-1, objv+1)); |
| 153 |
.CE |
| 154 |
.PP |
| 155 |
(It is not really implemented that way. Internal access |
| 156 |
privileges allow for a more efficient alternative that meshes |
| 157 |
better with the bytecode compiler.) |
| 158 |
.PP |
| 159 |
Note that a newly created \fBTcl_Obj\fR may be passed |
| 160 |
in as the \fIoptions\fR argument without the need to tend |
| 161 |
to any reference counting. This is analogous to |
| 162 |
\fBTcl_SetObjResult\fR. |
| 163 |
.PP |
| 164 |
While \fBTcl_SetReturnOptions\fR provides a general interface |
| 165 |
to set any collection of return options, there are a handful |
| 166 |
of return options that are very frequently used. Most |
| 167 |
notably the \fB\-errorinfo\fR and \fB\-errorcode\fR return |
| 168 |
options should be set properly when the command procedure |
| 169 |
of a command returns \fBTCL_ERROR\fR. The \fB\-errorline\fR |
| 170 |
return option is also read by commands that evaluate scripts |
| 171 |
and wish to supply detailed error location information in |
| 172 |
the stack trace text they append to the \fB\-errorinfo\fR option. |
| 173 |
Tcl provides several simpler interfaces to more directly set |
| 174 |
these return options. |
| 175 |
.PP |
| 176 |
The \fB\-errorinfo\fR option holds a stack trace of the |
| 177 |
operations that were in progress when an error occurred, |
| 178 |
and is intended to be human-readable. |
| 179 |
The \fB\-errorcode\fR option holds a Tcl list of items that |
| 180 |
are intended to be machine-readable. |
| 181 |
The first item in the \fB\-errorcode\fR value identifies the class of |
| 182 |
error that occurred |
| 183 |
(e.g., POSIX means an error occurred in a POSIX system call) |
| 184 |
and additional elements hold additional pieces |
| 185 |
of information that depend on the class. |
| 186 |
See the manual entry on the \fBerrorCode\fR variable for details on the |
| 187 |
various formats for the \fB\-errorcode\fR option used by Tcl's built-in |
| 188 |
commands. |
| 189 |
.PP |
| 190 |
The \fB\-errorinfo\fR option value is gradually built up as an |
| 191 |
error unwinds through the nested operations. |
| 192 |
Each time an error code is returned to \fBTcl_Eval\fR, or |
| 193 |
any of the routines that performs script evaluation, |
| 194 |
the procedure \fBTcl_AddErrorInfo\fR is called to add |
| 195 |
additional text to the \fB\-errorinfo\fR value describing the |
| 196 |
command that was being executed when the error occurred. |
| 197 |
By the time the error has been passed all the way back |
| 198 |
to the application, it will contain a complete trace |
| 199 |
of the activity in progress when the error occurred. |
| 200 |
.PP |
| 201 |
It is sometimes useful to add additional information to |
| 202 |
the \fB\-errorinfo\fR value beyond what can be supplied automatically |
| 203 |
by the script evaluation routines. |
| 204 |
\fBTcl_AddErrorInfo\fR may be used for this purpose: |
| 205 |
its \fImessage\fR argument is an additional |
| 206 |
string to be appended to the \fB\-errorinfo\fR option. |
| 207 |
For example, when an error arises during the \fBsource\fR command, |
| 208 |
the procedure \fBTcl_AddErrorInfo\fR is called to |
| 209 |
record the name of the file being processed and the |
| 210 |
line number on which the error occurred. |
| 211 |
Likewise, when an error arises during evaluation of a |
| 212 |
Tcl procedures, the procedure name and line number |
| 213 |
within the procedure are recorded, and so on. |
| 214 |
The best time to call \fBTcl_AddErrorInfo\fR is just after |
| 215 |
a script evaluation routine has returned \fBTCL_ERROR\fR. |
| 216 |
The value of the \fB\-errorline\fR return option (retrieved |
| 217 |
via a call to \fBTcl_GetReturnOptions\fR) often makes up |
| 218 |
a useful part of the \fImessage\fR passed to \fBTcl_AddErrorInfo\fR. |
| 219 |
.PP |
| 220 |
\fBTcl_AppendObjToErrorInfo\fR is an alternative interface to the |
| 221 |
same functionality as \fBTcl_AddErrorInfo\fR. \fBTcl_AppendObjToErrorInfo\fR |
| 222 |
is called when the string value to be appended to the \fB\-errorinfo\fR option |
| 223 |
is available as a \fBTcl_Obj\fR instead of as a \fBchar\fR array. |
| 224 |
.PP |
| 225 |
\fBTcl_AddObjErrorInfo\fR is nearly identical |
| 226 |
to \fBTcl_AddErrorInfo\fR, except that it has an additional \fIlength\fR |
| 227 |
argument. This allows the \fImessage\fR string to contain |
| 228 |
embedded null bytes. This is essentially never a good idea. |
| 229 |
If the \fImessage\fR needs to contain the null character \fBU+0000\fR, |
| 230 |
Tcl's usual internal encoding rules should be used to avoid |
| 231 |
the need for a null byte. If the \fBTcl_AddObjErrorInfo\fR |
| 232 |
interface is used at all, it should be with a negative \fIlength\fR value. |
| 233 |
.PP |
| 234 |
The procedure \fBTcl_SetObjErrorCode\fR is used to set the |
| 235 |
\fB\-errorcode\fR return option to the list value \fIerrorObjPtr\fR |
| 236 |
built up by the caller. |
| 237 |
\fBTcl_SetObjErrorCode\fR is typically invoked just |
| 238 |
before returning an error. If an error is |
| 239 |
returned without calling \fBTcl_SetObjErrorCode\fR or |
| 240 |
\fBTcl_SetErrorCode\fR the Tcl interpreter automatically sets |
| 241 |
the \fB\-errorcode\fR return option to \fBNONE\fR. |
| 242 |
.PP |
| 243 |
The procedure \fBTcl_SetErrorCode\fR is also used to set the |
| 244 |
\fB\-errorcode\fR return option. However, it takes one or more strings to |
| 245 |
record instead of a value. Otherwise, it is similar to |
| 246 |
\fBTcl_SetObjErrorCode\fR in behavior. |
| 247 |
.PP |
| 248 |
\fBTcl_SetErrorCodeVA\fR is the same as \fBTcl_SetErrorCode\fR except that |
| 249 |
instead of taking a variable number of arguments it takes an argument list. |
| 250 |
.PP |
| 251 |
The procedure \fBTcl_GetErrorLine\fR is used to read the integer value |
| 252 |
of the \fB\-errorline\fR return option without the overhead of a full |
| 253 |
call to \fBTcl_GetReturnOptions\fR. Likewise, \fBTcl_SetErrorLine\fR |
| 254 |
sets the \fB\-errorline\fR return option value. |
| 255 |
.PP |
| 256 |
\fBTcl_PosixError\fR |
| 257 |
sets the \fB\-errorcode\fR variable after an error in a POSIX kernel call. |
| 258 |
It reads the value of the \fBerrno\fR C variable and calls |
| 259 |
\fBTcl_SetErrorCode\fR to set the \fB\-errorcode\fR return |
| 260 |
option in the \fBPOSIX\fR format. |
| 261 |
The caller must previously have called \fBTcl_SetErrno\fR to set |
| 262 |
\fBerrno\fR; this is necessary on some platforms (e.g. Windows) where Tcl |
| 263 |
is linked into an application as a shared library, or when the error |
| 264 |
occurs in a dynamically loaded extension. See the manual entry for |
| 265 |
\fBTcl_SetErrno\fR for more information. |
| 266 |
.PP |
| 267 |
\fBTcl_PosixError\fR returns a human-readable diagnostic message |
| 268 |
for the error |
| 269 |
(this is the same value that will appear as the third element |
| 270 |
in the \fB\-errorcode\fR value). |
| 271 |
It may be convenient to include this string as part of the |
| 272 |
error message returned to the application in |
| 273 |
the interpreter's result. |
| 274 |
.PP |
| 275 |
\fBTcl_LogCommandInfo\fR is invoked after an error occurs in an |
| 276 |
interpreter. It adds information about the command that was being |
| 277 |
executed when the error occurred to the \fB\-errorinfo\fR value, and |
| 278 |
the line number stored internally in the interpreter is set. |
| 279 |
.PP |
| 280 |
In older releases of Tcl, there was no \fBTcl_GetReturnOptions\fR |
| 281 |
routine. In its place, the global Tcl variables \fBerrorInfo\fR |
| 282 |
and \fBerrorCode\fR were the only place to retrieve the error |
| 283 |
information. Much existing code written for older Tcl releases |
| 284 |
still access this information via those global variables. |
| 285 |
.PP |
| 286 |
It is important to realize that while reading from those |
| 287 |
global variables remains a supported way to access these |
| 288 |
return option values, it is important not to assume that |
| 289 |
writing to those global variables will properly set the |
| 290 |
corresponding return options. It has long been emphasized |
| 291 |
in this manual page that it is important to |
| 292 |
call the procedures described here rather than |
| 293 |
setting \fBerrorInfo\fR or \fBerrorCode\fR directly with |
| 294 |
\fBTcl_ObjSetVar2\fR. |
| 295 |
.PP |
| 296 |
If the procedure \fBTcl_ResetResult\fR is called, |
| 297 |
it clears all of the state of the interpreter associated with |
| 298 |
script evaluation, including the entire return options dictionary. |
| 299 |
In particular, the \fB\-errorinfo\fR and \fB\-errorcode\fR options |
| 300 |
are reset. |
| 301 |
If an error had occurred, the \fBTcl_ResetResult\fR call will |
| 302 |
clear the error state to make it appear as if no error had |
| 303 |
occurred after all. |
| 304 |
The global variables \fBerrorInfo\fR and |
| 305 |
\fBerrorCode\fR are not modified by \fBTcl_ResetResult\fR |
| 306 |
so they continue to hold a record of information about the |
| 307 |
most recent error seen in an interpreter. |
| 308 |
.SH "SEE ALSO" |
| 309 |
Tcl_DecrRefCount(3), Tcl_IncrRefCount(3), Tcl_Interp(3), Tcl_ResetResult(3), |
| 310 |
Tcl_SetErrno(3), errorCode(n), errorInfo(n) |
| 311 |
.SH KEYWORDS |
| 312 |
error, value, value result, stack, trace, variable |