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