| 1 |
/* |
| 2 |
* tclTrace.c -- |
| 3 |
* |
| 4 |
* This file contains code to handle most trace management. |
| 5 |
* |
| 6 |
* Copyright (c) 1987-1993 The Regents of the University of California. |
| 7 |
* Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1998-2000 Scriptics Corporation. |
| 9 |
* Copyright (c) 2002 ActiveState Corporation. |
| 10 |
* |
| 11 |
* See the file "license.terms" for information on usage and redistribution of |
| 12 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "tclInt.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* Structures used to hold information about variable traces: |
| 19 |
*/ |
| 20 |
|
| 21 |
typedef struct { |
| 22 |
int flags; /* Operations for which Tcl command is to be |
| 23 |
* invoked. */ |
| 24 |
size_t length; /* Number of non-NUL chars. in command. */ |
| 25 |
char command[1]; /* Space for Tcl command to invoke. Actual |
| 26 |
* size will be as large as necessary to hold |
| 27 |
* command. This field must be the last in the |
| 28 |
* structure, so that it can be larger than 1 |
| 29 |
* byte. */ |
| 30 |
} TraceVarInfo; |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
VarTrace traceInfo; |
| 34 |
TraceVarInfo traceCmdInfo; |
| 35 |
} CombinedTraceVarInfo; |
| 36 |
|
| 37 |
/* |
| 38 |
* Structure used to hold information about command traces: |
| 39 |
*/ |
| 40 |
|
| 41 |
typedef struct { |
| 42 |
int flags; /* Operations for which Tcl command is to be |
| 43 |
* invoked. */ |
| 44 |
size_t length; /* Number of non-NUL chars. in command. */ |
| 45 |
Tcl_Trace stepTrace; /* Used for execution traces, when tracing |
| 46 |
* inside the given command */ |
| 47 |
int startLevel; /* Used for bookkeeping with step execution |
| 48 |
* traces, store the level at which the step |
| 49 |
* trace was invoked */ |
| 50 |
char *startCmd; /* Used for bookkeeping with step execution |
| 51 |
* traces, store the command name which |
| 52 |
* invoked step trace */ |
| 53 |
int curFlags; /* Trace flags for the current command */ |
| 54 |
int curCode; /* Return code for the current command */ |
| 55 |
int refCount; /* Used to ensure this structure is not |
| 56 |
* deleted too early. Keeps track of how many |
| 57 |
* pieces of code have a pointer to this |
| 58 |
* structure. */ |
| 59 |
char command[1]; /* Space for Tcl command to invoke. Actual |
| 60 |
* size will be as large as necessary to hold |
| 61 |
* command. This field must be the last in the |
| 62 |
* structure, so that it can be larger than 1 |
| 63 |
* byte. */ |
| 64 |
} TraceCommandInfo; |
| 65 |
|
| 66 |
/* |
| 67 |
* Used by command execution traces. Note that we assume in the code that |
| 68 |
* TCL_TRACE_ENTER_DURING_EXEC == 4 * TCL_TRACE_ENTER_EXEC and that |
| 69 |
* TCL_TRACE_LEAVE_DURING_EXEC == 4 * TCL_TRACE_LEAVE_EXEC. |
| 70 |
* |
| 71 |
* TCL_TRACE_ENTER_DURING_EXEC - Trace each command inside the command |
| 72 |
* currently being traced, before execution. |
| 73 |
* TCL_TRACE_LEAVE_DURING_EXEC - Trace each command inside the command |
| 74 |
* currently being traced, after execution. |
| 75 |
* TCL_TRACE_ANY_EXEC - OR'd combination of all EXEC flags. |
| 76 |
* TCL_TRACE_EXEC_IN_PROGRESS - The callback function on this trace is |
| 77 |
* currently executing. Therefore we don't let |
| 78 |
* further traces execute. |
| 79 |
* TCL_TRACE_EXEC_DIRECT - This execution trace is triggered directly |
| 80 |
* by the command being traced, not because of |
| 81 |
* an internal trace. |
| 82 |
* The flags 'TCL_TRACE_DESTROYED' and 'TCL_INTERP_DESTROYED' may also be used |
| 83 |
* in command execution traces. |
| 84 |
*/ |
| 85 |
|
| 86 |
#define TCL_TRACE_ENTER_DURING_EXEC 4 |
| 87 |
#define TCL_TRACE_LEAVE_DURING_EXEC 8 |
| 88 |
#define TCL_TRACE_ANY_EXEC 15 |
| 89 |
#define TCL_TRACE_EXEC_IN_PROGRESS 0x10 |
| 90 |
#define TCL_TRACE_EXEC_DIRECT 0x20 |
| 91 |
|
| 92 |
/* |
| 93 |
* Forward declarations for functions defined in this file: |
| 94 |
*/ |
| 95 |
|
| 96 |
typedef int (Tcl_TraceTypeObjCmd)(Tcl_Interp *interp, int optionIndex, |
| 97 |
int objc, Tcl_Obj *const objv[]); |
| 98 |
|
| 99 |
static Tcl_TraceTypeObjCmd TraceVariableObjCmd; |
| 100 |
static Tcl_TraceTypeObjCmd TraceCommandObjCmd; |
| 101 |
static Tcl_TraceTypeObjCmd TraceExecutionObjCmd; |
| 102 |
|
| 103 |
/* |
| 104 |
* Each subcommand has a number of 'types' to which it can apply. Currently |
| 105 |
* 'execution', 'command' and 'variable' are the only types supported. These |
| 106 |
* three arrays MUST be kept in sync! In the future we may provide an API to |
| 107 |
* add to the list of supported trace types. |
| 108 |
*/ |
| 109 |
|
| 110 |
static const char *const traceTypeOptions[] = { |
| 111 |
"execution", "command", "variable", NULL |
| 112 |
}; |
| 113 |
static Tcl_TraceTypeObjCmd *const traceSubCmds[] = { |
| 114 |
TraceExecutionObjCmd, |
| 115 |
TraceCommandObjCmd, |
| 116 |
TraceVariableObjCmd |
| 117 |
}; |
| 118 |
|
| 119 |
/* |
| 120 |
* Declarations for local functions to this file: |
| 121 |
*/ |
| 122 |
|
| 123 |
static int CallTraceFunction(Tcl_Interp *interp, Trace *tracePtr, |
| 124 |
Command *cmdPtr, const char *command, int numChars, |
| 125 |
int objc, Tcl_Obj *const objv[]); |
| 126 |
static char * TraceVarProc(ClientData clientData, Tcl_Interp *interp, |
| 127 |
const char *name1, const char *name2, int flags); |
| 128 |
static void TraceCommandProc(ClientData clientData, |
| 129 |
Tcl_Interp *interp, const char *oldName, |
| 130 |
const char *newName, int flags); |
| 131 |
static Tcl_CmdObjTraceProc TraceExecutionProc; |
| 132 |
static int StringTraceProc(ClientData clientData, |
| 133 |
Tcl_Interp *interp, int level, |
| 134 |
const char *command, Tcl_Command commandInfo, |
| 135 |
int objc, Tcl_Obj *const objv[]); |
| 136 |
static void StringTraceDeleteProc(ClientData clientData); |
| 137 |
static void DisposeTraceResult(int flags, char *result); |
| 138 |
static int TraceVarEx(Tcl_Interp *interp, const char *part1, |
| 139 |
const char *part2, VarTrace *tracePtr); |
| 140 |
|
| 141 |
/* |
| 142 |
* The following structure holds the client data for string-based |
| 143 |
* trace procs |
| 144 |
*/ |
| 145 |
|
| 146 |
typedef struct StringTraceData { |
| 147 |
ClientData clientData; /* Client data from Tcl_CreateTrace */ |
| 148 |
Tcl_CmdTraceProc *proc; /* Trace function from Tcl_CreateTrace */ |
| 149 |
} StringTraceData; |
| 150 |
|
| 151 |
/* |
| 152 |
* Convenience macros for iterating over the list of traces. Note that each of |
| 153 |
* these *must* be treated as a command, and *must* have a block following it. |
| 154 |
*/ |
| 155 |
|
| 156 |
#define FOREACH_VAR_TRACE(interp, name, clientData) \ |
| 157 |
(clientData) = NULL; \ |
| 158 |
while (((clientData) = Tcl_VarTraceInfo2((interp), (name), NULL, \ |
| 159 |
0, TraceVarProc, (clientData))) != NULL) |
| 160 |
|
| 161 |
#define FOREACH_COMMAND_TRACE(interp, name, clientData) \ |
| 162 |
(clientData) = NULL; \ |
| 163 |
while (((clientData) = Tcl_CommandTraceInfo((interp), (name), 0, \ |
| 164 |
TraceCommandProc, (clientData))) != NULL) |
| 165 |
|
| 166 |
/* |
| 167 |
*---------------------------------------------------------------------- |
| 168 |
* |
| 169 |
* Tcl_TraceObjCmd -- |
| 170 |
* |
| 171 |
* This function is invoked to process the "trace" Tcl command. See the |
| 172 |
* user documentation for details on what it does. |
| 173 |
* |
| 174 |
* Standard syntax as of Tcl 8.4 is: |
| 175 |
* trace {add|info|remove} {command|variable} name ops cmd |
| 176 |
* |
| 177 |
* Results: |
| 178 |
* A standard Tcl result. |
| 179 |
* |
| 180 |
* Side effects: |
| 181 |
* See the user documentation. |
| 182 |
*---------------------------------------------------------------------- |
| 183 |
*/ |
| 184 |
|
| 185 |
int |
| 186 |
Tcl_TraceObjCmd( |
| 187 |
ClientData dummy, /* Not used. */ |
| 188 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 189 |
int objc, /* Number of arguments. */ |
| 190 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 191 |
{ |
| 192 |
int optionIndex; |
| 193 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 194 |
const char *name; |
| 195 |
const char *flagOps, *p; |
| 196 |
#endif |
| 197 |
/* Main sub commands to 'trace' */ |
| 198 |
static const char *const traceOptions[] = { |
| 199 |
"add", "info", "remove", |
| 200 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 201 |
"variable", "vdelete", "vinfo", |
| 202 |
#endif |
| 203 |
NULL |
| 204 |
}; |
| 205 |
/* 'OLD' options are pre-Tcl-8.4 style */ |
| 206 |
enum traceOptions { |
| 207 |
TRACE_ADD, TRACE_INFO, TRACE_REMOVE, |
| 208 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 209 |
TRACE_OLD_VARIABLE, TRACE_OLD_VDELETE, TRACE_OLD_VINFO |
| 210 |
#endif |
| 211 |
}; |
| 212 |
|
| 213 |
if (objc < 2) { |
| 214 |
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?"); |
| 215 |
return TCL_ERROR; |
| 216 |
} |
| 217 |
|
| 218 |
if (Tcl_GetIndexFromObj(interp, objv[1], traceOptions, "option", 0, |
| 219 |
&optionIndex) != TCL_OK) { |
| 220 |
return TCL_ERROR; |
| 221 |
} |
| 222 |
switch ((enum traceOptions) optionIndex) { |
| 223 |
case TRACE_ADD: |
| 224 |
case TRACE_REMOVE: { |
| 225 |
/* |
| 226 |
* All sub commands of trace add/remove must take at least one more |
| 227 |
* argument. Beyond that we let the subcommand itself control the |
| 228 |
* argument structure. |
| 229 |
*/ |
| 230 |
|
| 231 |
int typeIndex; |
| 232 |
|
| 233 |
if (objc < 3) { |
| 234 |
Tcl_WrongNumArgs(interp, 2, objv, "type ?arg ...?"); |
| 235 |
return TCL_ERROR; |
| 236 |
} |
| 237 |
if (Tcl_GetIndexFromObj(interp, objv[2], traceTypeOptions, "option", |
| 238 |
0, &typeIndex) != TCL_OK) { |
| 239 |
return TCL_ERROR; |
| 240 |
} |
| 241 |
return traceSubCmds[typeIndex](interp, optionIndex, objc, objv); |
| 242 |
} |
| 243 |
case TRACE_INFO: { |
| 244 |
/* |
| 245 |
* All sub commands of trace info must take exactly two more arguments |
| 246 |
* which name the type of thing being traced and the name of the thing |
| 247 |
* being traced. |
| 248 |
*/ |
| 249 |
|
| 250 |
int typeIndex; |
| 251 |
if (objc < 3) { |
| 252 |
/* |
| 253 |
* Delegate other complaints to the type-specific code which can |
| 254 |
* give a better error message. |
| 255 |
*/ |
| 256 |
|
| 257 |
Tcl_WrongNumArgs(interp, 2, objv, "type name"); |
| 258 |
return TCL_ERROR; |
| 259 |
} |
| 260 |
if (Tcl_GetIndexFromObj(interp, objv[2], traceTypeOptions, "option", |
| 261 |
0, &typeIndex) != TCL_OK) { |
| 262 |
return TCL_ERROR; |
| 263 |
} |
| 264 |
return traceSubCmds[typeIndex](interp, optionIndex, objc, objv); |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 269 |
case TRACE_OLD_VARIABLE: |
| 270 |
case TRACE_OLD_VDELETE: { |
| 271 |
Tcl_Obj *copyObjv[6]; |
| 272 |
Tcl_Obj *opsList; |
| 273 |
int code; |
| 274 |
int numFlags; |
| 275 |
|
| 276 |
if (objc != 5) { |
| 277 |
Tcl_WrongNumArgs(interp, 2, objv, "name ops command"); |
| 278 |
return TCL_ERROR; |
| 279 |
} |
| 280 |
|
| 281 |
TclNewObj(opsList); |
| 282 |
Tcl_IncrRefCount(opsList); |
| 283 |
flagOps = TclGetStringFromObj(objv[3], &numFlags); |
| 284 |
if (numFlags == 0) { |
| 285 |
Tcl_DecrRefCount(opsList); |
| 286 |
goto badVarOps; |
| 287 |
} |
| 288 |
for (p = flagOps; *p != 0; p++) { |
| 289 |
Tcl_Obj *opObj; |
| 290 |
|
| 291 |
if (*p == 'r') { |
| 292 |
TclNewLiteralStringObj(opObj, "read"); |
| 293 |
} else if (*p == 'w') { |
| 294 |
TclNewLiteralStringObj(opObj, "write"); |
| 295 |
} else if (*p == 'u') { |
| 296 |
TclNewLiteralStringObj(opObj, "unset"); |
| 297 |
} else if (*p == 'a') { |
| 298 |
TclNewLiteralStringObj(opObj, "array"); |
| 299 |
} else { |
| 300 |
Tcl_DecrRefCount(opsList); |
| 301 |
goto badVarOps; |
| 302 |
} |
| 303 |
Tcl_ListObjAppendElement(NULL, opsList, opObj); |
| 304 |
} |
| 305 |
copyObjv[0] = NULL; |
| 306 |
memcpy(copyObjv+1, objv, objc*sizeof(Tcl_Obj *)); |
| 307 |
copyObjv[4] = opsList; |
| 308 |
if (optionIndex == TRACE_OLD_VARIABLE) { |
| 309 |
code = traceSubCmds[2](interp, TRACE_ADD, objc+1, copyObjv); |
| 310 |
} else { |
| 311 |
code = traceSubCmds[2](interp, TRACE_REMOVE, objc+1, copyObjv); |
| 312 |
} |
| 313 |
Tcl_DecrRefCount(opsList); |
| 314 |
return code; |
| 315 |
} |
| 316 |
case TRACE_OLD_VINFO: { |
| 317 |
ClientData clientData; |
| 318 |
char ops[5]; |
| 319 |
Tcl_Obj *resultListPtr, *pairObjPtr, *elemObjPtr; |
| 320 |
|
| 321 |
if (objc != 3) { |
| 322 |
Tcl_WrongNumArgs(interp, 2, objv, "name"); |
| 323 |
return TCL_ERROR; |
| 324 |
} |
| 325 |
TclNewObj(resultListPtr); |
| 326 |
name = TclGetString(objv[2]); |
| 327 |
FOREACH_VAR_TRACE(interp, name, clientData) { |
| 328 |
TraceVarInfo *tvarPtr = (TraceVarInfo *)clientData; |
| 329 |
char *q = ops; |
| 330 |
|
| 331 |
pairObjPtr = Tcl_NewListObj(0, NULL); |
| 332 |
if (tvarPtr->flags & TCL_TRACE_READS) { |
| 333 |
*q = 'r'; |
| 334 |
q++; |
| 335 |
} |
| 336 |
if (tvarPtr->flags & TCL_TRACE_WRITES) { |
| 337 |
*q = 'w'; |
| 338 |
q++; |
| 339 |
} |
| 340 |
if (tvarPtr->flags & TCL_TRACE_UNSETS) { |
| 341 |
*q = 'u'; |
| 342 |
q++; |
| 343 |
} |
| 344 |
if (tvarPtr->flags & TCL_TRACE_ARRAY) { |
| 345 |
*q = 'a'; |
| 346 |
q++; |
| 347 |
} |
| 348 |
*q = '\0'; |
| 349 |
|
| 350 |
/* |
| 351 |
* Build a pair (2-item list) with the ops string as the first obj |
| 352 |
* element and the tvarPtr->command string as the second obj |
| 353 |
* element. Append the pair (as an element) to the end of the |
| 354 |
* result object list. |
| 355 |
*/ |
| 356 |
|
| 357 |
elemObjPtr = Tcl_NewStringObj(ops, -1); |
| 358 |
Tcl_ListObjAppendElement(NULL, pairObjPtr, elemObjPtr); |
| 359 |
elemObjPtr = Tcl_NewStringObj(tvarPtr->command, -1); |
| 360 |
Tcl_ListObjAppendElement(NULL, pairObjPtr, elemObjPtr); |
| 361 |
Tcl_ListObjAppendElement(interp, resultListPtr, pairObjPtr); |
| 362 |
} |
| 363 |
Tcl_SetObjResult(interp, resultListPtr); |
| 364 |
break; |
| 365 |
} |
| 366 |
#endif /* TCL_REMOVE_OBSOLETE_TRACES */ |
| 367 |
} |
| 368 |
return TCL_OK; |
| 369 |
|
| 370 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 371 |
badVarOps: |
| 372 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 373 |
"bad operations \"%s\": should be one or more of rwua", |
| 374 |
flagOps)); |
| 375 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "TRACE", "BADOPS", (char *)NULL); |
| 376 |
return TCL_ERROR; |
| 377 |
#endif |
| 378 |
} |
| 379 |
|
| 380 |
/* |
| 381 |
*---------------------------------------------------------------------- |
| 382 |
* |
| 383 |
* TraceExecutionObjCmd -- |
| 384 |
* |
| 385 |
* Helper function for Tcl_TraceObjCmd; implements the [trace |
| 386 |
* {add|remove|info} execution ...] subcommands. See the user |
| 387 |
* documentation for details on what these do. |
| 388 |
* |
| 389 |
* Results: |
| 390 |
* Standard Tcl result. |
| 391 |
* |
| 392 |
* Side effects: |
| 393 |
* Depends on the operation (add, remove, or info) being performed; may |
| 394 |
* add or remove command traces on a command. |
| 395 |
* |
| 396 |
*---------------------------------------------------------------------- |
| 397 |
*/ |
| 398 |
|
| 399 |
static int |
| 400 |
TraceExecutionObjCmd( |
| 401 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 402 |
int optionIndex, /* Add, info or remove */ |
| 403 |
int objc, /* Number of arguments. */ |
| 404 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 405 |
{ |
| 406 |
int commandLength, index; |
| 407 |
const char *name, *command; |
| 408 |
size_t length; |
| 409 |
enum traceOptions { |
| 410 |
TRACE_ADD, TRACE_INFO, TRACE_REMOVE |
| 411 |
}; |
| 412 |
static const char *const opStrings[] = { |
| 413 |
"enter", "leave", "enterstep", "leavestep", NULL |
| 414 |
}; |
| 415 |
enum operations { |
| 416 |
TRACE_EXEC_ENTER, TRACE_EXEC_LEAVE, |
| 417 |
TRACE_EXEC_ENTER_STEP, TRACE_EXEC_LEAVE_STEP |
| 418 |
}; |
| 419 |
|
| 420 |
switch ((enum traceOptions) optionIndex) { |
| 421 |
case TRACE_ADD: |
| 422 |
case TRACE_REMOVE: { |
| 423 |
int flags = 0; |
| 424 |
int i, listLen, result; |
| 425 |
Tcl_Obj **elemPtrs; |
| 426 |
|
| 427 |
if (objc != 6) { |
| 428 |
Tcl_WrongNumArgs(interp, 3, objv, "name opList command"); |
| 429 |
return TCL_ERROR; |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Make sure the ops argument is a list object; get its length and a |
| 434 |
* pointer to its array of element pointers. |
| 435 |
*/ |
| 436 |
|
| 437 |
result = TclListObjGetElements(interp, objv[4], &listLen, &elemPtrs); |
| 438 |
if (result != TCL_OK) { |
| 439 |
return result; |
| 440 |
} |
| 441 |
if (listLen == 0) { |
| 442 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 443 |
"bad operation list \"\": must be one or more of" |
| 444 |
" enter, leave, enterstep, or leavestep", -1)); |
| 445 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "TRACE", "NOOPS", |
| 446 |
(char *)NULL); |
| 447 |
return TCL_ERROR; |
| 448 |
} |
| 449 |
for (i = 0; i < listLen; i++) { |
| 450 |
if (Tcl_GetIndexFromObj(interp, elemPtrs[i], opStrings, |
| 451 |
"operation", TCL_EXACT, &index) != TCL_OK) { |
| 452 |
return TCL_ERROR; |
| 453 |
} |
| 454 |
switch ((enum operations) index) { |
| 455 |
case TRACE_EXEC_ENTER: |
| 456 |
flags |= TCL_TRACE_ENTER_EXEC; |
| 457 |
break; |
| 458 |
case TRACE_EXEC_LEAVE: |
| 459 |
flags |= TCL_TRACE_LEAVE_EXEC; |
| 460 |
break; |
| 461 |
case TRACE_EXEC_ENTER_STEP: |
| 462 |
flags |= TCL_TRACE_ENTER_DURING_EXEC; |
| 463 |
break; |
| 464 |
case TRACE_EXEC_LEAVE_STEP: |
| 465 |
flags |= TCL_TRACE_LEAVE_DURING_EXEC; |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
command = TclGetStringFromObj(objv[5], &commandLength); |
| 470 |
length = commandLength; |
| 471 |
if ((enum traceOptions) optionIndex == TRACE_ADD) { |
| 472 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)ckalloc( |
| 473 |
TclOffset(TraceCommandInfo, command) + 1 + length); |
| 474 |
|
| 475 |
tcmdPtr->flags = flags; |
| 476 |
tcmdPtr->stepTrace = NULL; |
| 477 |
tcmdPtr->startLevel = 0; |
| 478 |
tcmdPtr->startCmd = NULL; |
| 479 |
tcmdPtr->length = length; |
| 480 |
tcmdPtr->refCount = 1; |
| 481 |
flags |= TCL_TRACE_DELETE; |
| 482 |
if (flags & (TCL_TRACE_ENTER_DURING_EXEC | |
| 483 |
TCL_TRACE_LEAVE_DURING_EXEC)) { |
| 484 |
flags |= (TCL_TRACE_ENTER_EXEC | TCL_TRACE_LEAVE_EXEC); |
| 485 |
} |
| 486 |
memcpy(tcmdPtr->command, command, length+1); |
| 487 |
name = TclGetString(objv[3]); |
| 488 |
if (Tcl_TraceCommand(interp, name, flags, TraceCommandProc, |
| 489 |
tcmdPtr) != TCL_OK) { |
| 490 |
ckfree(tcmdPtr); |
| 491 |
return TCL_ERROR; |
| 492 |
} |
| 493 |
} else { |
| 494 |
/* |
| 495 |
* Search through all of our traces on this command to see if |
| 496 |
* there's one with the given command. If so, then delete the |
| 497 |
* first one that matches. |
| 498 |
*/ |
| 499 |
|
| 500 |
ClientData clientData; |
| 501 |
|
| 502 |
/* |
| 503 |
* First ensure the name given is valid. |
| 504 |
*/ |
| 505 |
|
| 506 |
name = TclGetString(objv[3]); |
| 507 |
if (Tcl_FindCommand(interp,name,NULL,TCL_LEAVE_ERR_MSG) == NULL) { |
| 508 |
return TCL_ERROR; |
| 509 |
} |
| 510 |
|
| 511 |
FOREACH_COMMAND_TRACE(interp, name, clientData) { |
| 512 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 513 |
|
| 514 |
/* |
| 515 |
* In checking the 'flags' field we must remove any extraneous |
| 516 |
* flags which may have been temporarily added by various |
| 517 |
* pieces of the trace mechanism. |
| 518 |
*/ |
| 519 |
|
| 520 |
if ((tcmdPtr->length == length) |
| 521 |
&& ((tcmdPtr->flags & (TCL_TRACE_ANY_EXEC | |
| 522 |
TCL_TRACE_RENAME | TCL_TRACE_DELETE)) == flags) |
| 523 |
&& (strncmp(command, tcmdPtr->command, |
| 524 |
length) == 0)) { |
| 525 |
flags |= TCL_TRACE_DELETE; |
| 526 |
if (flags & (TCL_TRACE_ENTER_DURING_EXEC | |
| 527 |
TCL_TRACE_LEAVE_DURING_EXEC)) { |
| 528 |
flags |= (TCL_TRACE_ENTER_EXEC | TCL_TRACE_LEAVE_EXEC); |
| 529 |
} |
| 530 |
Tcl_UntraceCommand(interp, name, flags, |
| 531 |
TraceCommandProc, clientData); |
| 532 |
if (tcmdPtr->stepTrace != NULL) { |
| 533 |
/* |
| 534 |
* We need to remove the interpreter-wide trace which |
| 535 |
* we created to allow 'step' traces. |
| 536 |
*/ |
| 537 |
|
| 538 |
Tcl_DeleteTrace(interp, tcmdPtr->stepTrace); |
| 539 |
tcmdPtr->stepTrace = NULL; |
| 540 |
ckfree(tcmdPtr->startCmd); |
| 541 |
} |
| 542 |
if (tcmdPtr->flags & TCL_TRACE_EXEC_IN_PROGRESS) { |
| 543 |
/* |
| 544 |
* Postpone deletion. |
| 545 |
*/ |
| 546 |
|
| 547 |
tcmdPtr->flags = 0; |
| 548 |
} |
| 549 |
if (tcmdPtr->refCount-- <= 1) { |
| 550 |
ckfree(tcmdPtr); |
| 551 |
} |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
break; |
| 557 |
} |
| 558 |
case TRACE_INFO: { |
| 559 |
ClientData clientData; |
| 560 |
Tcl_Obj *resultListPtr; |
| 561 |
|
| 562 |
if (objc != 4) { |
| 563 |
Tcl_WrongNumArgs(interp, 3, objv, "name"); |
| 564 |
return TCL_ERROR; |
| 565 |
} |
| 566 |
|
| 567 |
name = TclGetString(objv[3]); |
| 568 |
|
| 569 |
/* |
| 570 |
* First ensure the name given is valid. |
| 571 |
*/ |
| 572 |
|
| 573 |
if (Tcl_FindCommand(interp, name, NULL, TCL_LEAVE_ERR_MSG) == NULL) { |
| 574 |
return TCL_ERROR; |
| 575 |
} |
| 576 |
|
| 577 |
resultListPtr = Tcl_NewListObj(0, NULL); |
| 578 |
FOREACH_COMMAND_TRACE(interp, name, clientData) { |
| 579 |
int numOps = 0; |
| 580 |
Tcl_Obj *opObj, *eachTraceObjPtr, *elemObjPtr; |
| 581 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 582 |
|
| 583 |
/* |
| 584 |
* Build a list with the ops list as the first obj element and the |
| 585 |
* tcmdPtr->command string as the second obj element. Append this |
| 586 |
* list (as an element) to the end of the result object list. |
| 587 |
*/ |
| 588 |
|
| 589 |
elemObjPtr = Tcl_NewListObj(0, NULL); |
| 590 |
Tcl_IncrRefCount(elemObjPtr); |
| 591 |
if (tcmdPtr->flags & TCL_TRACE_ENTER_EXEC) { |
| 592 |
TclNewLiteralStringObj(opObj, "enter"); |
| 593 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 594 |
} |
| 595 |
if (tcmdPtr->flags & TCL_TRACE_LEAVE_EXEC) { |
| 596 |
TclNewLiteralStringObj(opObj, "leave"); |
| 597 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 598 |
} |
| 599 |
if (tcmdPtr->flags & TCL_TRACE_ENTER_DURING_EXEC) { |
| 600 |
TclNewLiteralStringObj(opObj, "enterstep"); |
| 601 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 602 |
} |
| 603 |
if (tcmdPtr->flags & TCL_TRACE_LEAVE_DURING_EXEC) { |
| 604 |
TclNewLiteralStringObj(opObj, "leavestep"); |
| 605 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 606 |
} |
| 607 |
TclListObjLength(NULL, elemObjPtr, &numOps); |
| 608 |
if (0 == numOps) { |
| 609 |
Tcl_DecrRefCount(elemObjPtr); |
| 610 |
continue; |
| 611 |
} |
| 612 |
eachTraceObjPtr = Tcl_NewListObj(0, NULL); |
| 613 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, elemObjPtr); |
| 614 |
Tcl_DecrRefCount(elemObjPtr); |
| 615 |
elemObjPtr = NULL; |
| 616 |
|
| 617 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, |
| 618 |
Tcl_NewStringObj(tcmdPtr->command, -1)); |
| 619 |
Tcl_ListObjAppendElement(interp, resultListPtr, eachTraceObjPtr); |
| 620 |
} |
| 621 |
Tcl_SetObjResult(interp, resultListPtr); |
| 622 |
break; |
| 623 |
} |
| 624 |
} |
| 625 |
return TCL_OK; |
| 626 |
} |
| 627 |
|
| 628 |
/* |
| 629 |
*---------------------------------------------------------------------- |
| 630 |
* |
| 631 |
* TraceCommandObjCmd -- |
| 632 |
* |
| 633 |
* Helper function for Tcl_TraceObjCmd; implements the [trace |
| 634 |
* {add|info|remove} command ...] subcommands. See the user documentation |
| 635 |
* for details on what these do. |
| 636 |
* |
| 637 |
* Results: |
| 638 |
* Standard Tcl result. |
| 639 |
* |
| 640 |
* Side effects: |
| 641 |
* Depends on the operation (add, remove, or info) being performed; may |
| 642 |
* add or remove command traces on a command. |
| 643 |
* |
| 644 |
*---------------------------------------------------------------------- |
| 645 |
*/ |
| 646 |
|
| 647 |
static int |
| 648 |
TraceCommandObjCmd( |
| 649 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 650 |
int optionIndex, /* Add, info or remove */ |
| 651 |
int objc, /* Number of arguments. */ |
| 652 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 653 |
{ |
| 654 |
int commandLength, index; |
| 655 |
const char *name, *command; |
| 656 |
size_t length; |
| 657 |
enum traceOptions { TRACE_ADD, TRACE_INFO, TRACE_REMOVE }; |
| 658 |
static const char *const opStrings[] = { "delete", "rename", NULL }; |
| 659 |
enum operations { TRACE_CMD_DELETE, TRACE_CMD_RENAME }; |
| 660 |
|
| 661 |
switch ((enum traceOptions) optionIndex) { |
| 662 |
case TRACE_ADD: |
| 663 |
case TRACE_REMOVE: { |
| 664 |
int flags = 0, result; |
| 665 |
int i, listLen; |
| 666 |
Tcl_Obj **elemPtrs; |
| 667 |
|
| 668 |
if (objc != 6) { |
| 669 |
Tcl_WrongNumArgs(interp, 3, objv, "name opList command"); |
| 670 |
return TCL_ERROR; |
| 671 |
} |
| 672 |
|
| 673 |
/* |
| 674 |
* Make sure the ops argument is a list object; get its length and a |
| 675 |
* pointer to its array of element pointers. |
| 676 |
*/ |
| 677 |
|
| 678 |
result = TclListObjGetElements(interp, objv[4], &listLen, &elemPtrs); |
| 679 |
if (result != TCL_OK) { |
| 680 |
return result; |
| 681 |
} |
| 682 |
if (listLen == 0) { |
| 683 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 684 |
"bad operation list \"\": must be one or more of" |
| 685 |
" delete or rename", -1)); |
| 686 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "TRACE", "NOOPS", |
| 687 |
(char *)NULL); |
| 688 |
return TCL_ERROR; |
| 689 |
} |
| 690 |
|
| 691 |
for (i = 0; i < listLen; i++) { |
| 692 |
if (Tcl_GetIndexFromObj(interp, elemPtrs[i], opStrings, |
| 693 |
"operation", TCL_EXACT, &index) != TCL_OK) { |
| 694 |
return TCL_ERROR; |
| 695 |
} |
| 696 |
switch ((enum operations) index) { |
| 697 |
case TRACE_CMD_RENAME: |
| 698 |
flags |= TCL_TRACE_RENAME; |
| 699 |
break; |
| 700 |
case TRACE_CMD_DELETE: |
| 701 |
flags |= TCL_TRACE_DELETE; |
| 702 |
break; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
command = TclGetStringFromObj(objv[5], &commandLength); |
| 707 |
length = commandLength; |
| 708 |
if ((enum traceOptions) optionIndex == TRACE_ADD) { |
| 709 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)ckalloc( |
| 710 |
TclOffset(TraceCommandInfo, command) + 1 + length); |
| 711 |
|
| 712 |
tcmdPtr->flags = flags; |
| 713 |
tcmdPtr->stepTrace = NULL; |
| 714 |
tcmdPtr->startLevel = 0; |
| 715 |
tcmdPtr->startCmd = NULL; |
| 716 |
tcmdPtr->length = length; |
| 717 |
tcmdPtr->refCount = 1; |
| 718 |
flags |= TCL_TRACE_DELETE; |
| 719 |
memcpy(tcmdPtr->command, command, length+1); |
| 720 |
name = TclGetString(objv[3]); |
| 721 |
if (Tcl_TraceCommand(interp, name, flags, TraceCommandProc, |
| 722 |
tcmdPtr) != TCL_OK) { |
| 723 |
ckfree(tcmdPtr); |
| 724 |
return TCL_ERROR; |
| 725 |
} |
| 726 |
} else { |
| 727 |
/* |
| 728 |
* Search through all of our traces on this command to see if |
| 729 |
* there's one with the given command. If so, then delete the |
| 730 |
* first one that matches. |
| 731 |
*/ |
| 732 |
|
| 733 |
ClientData clientData; |
| 734 |
|
| 735 |
/* |
| 736 |
* First ensure the name given is valid. |
| 737 |
*/ |
| 738 |
|
| 739 |
name = TclGetString(objv[3]); |
| 740 |
if (Tcl_FindCommand(interp,name,NULL,TCL_LEAVE_ERR_MSG) == NULL) { |
| 741 |
return TCL_ERROR; |
| 742 |
} |
| 743 |
|
| 744 |
FOREACH_COMMAND_TRACE(interp, name, clientData) { |
| 745 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 746 |
|
| 747 |
if ((tcmdPtr->length == length) && (tcmdPtr->flags == flags) |
| 748 |
&& (strncmp(command, tcmdPtr->command, |
| 749 |
length) == 0)) { |
| 750 |
Tcl_UntraceCommand(interp, name, flags | TCL_TRACE_DELETE, |
| 751 |
TraceCommandProc, clientData); |
| 752 |
tcmdPtr->flags |= TCL_TRACE_DESTROYED; |
| 753 |
if (tcmdPtr->refCount-- <= 1) { |
| 754 |
ckfree(tcmdPtr); |
| 755 |
} |
| 756 |
break; |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
break; |
| 761 |
} |
| 762 |
case TRACE_INFO: { |
| 763 |
ClientData clientData; |
| 764 |
Tcl_Obj *resultListPtr; |
| 765 |
|
| 766 |
if (objc != 4) { |
| 767 |
Tcl_WrongNumArgs(interp, 3, objv, "name"); |
| 768 |
return TCL_ERROR; |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
* First ensure the name given is valid. |
| 773 |
*/ |
| 774 |
|
| 775 |
name = TclGetString(objv[3]); |
| 776 |
if (Tcl_FindCommand(interp, name, NULL, TCL_LEAVE_ERR_MSG) == NULL) { |
| 777 |
return TCL_ERROR; |
| 778 |
} |
| 779 |
|
| 780 |
resultListPtr = Tcl_NewListObj(0, NULL); |
| 781 |
FOREACH_COMMAND_TRACE(interp, name, clientData) { |
| 782 |
int numOps = 0; |
| 783 |
Tcl_Obj *opObj, *eachTraceObjPtr, *elemObjPtr; |
| 784 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 785 |
|
| 786 |
/* |
| 787 |
* Build a list with the ops list as the first obj element and the |
| 788 |
* tcmdPtr->command string as the second obj element. Append this |
| 789 |
* list (as an element) to the end of the result object list. |
| 790 |
*/ |
| 791 |
|
| 792 |
elemObjPtr = Tcl_NewListObj(0, NULL); |
| 793 |
Tcl_IncrRefCount(elemObjPtr); |
| 794 |
if (tcmdPtr->flags & TCL_TRACE_RENAME) { |
| 795 |
TclNewLiteralStringObj(opObj, "rename"); |
| 796 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 797 |
} |
| 798 |
if (tcmdPtr->flags & TCL_TRACE_DELETE) { |
| 799 |
TclNewLiteralStringObj(opObj, "delete"); |
| 800 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObj); |
| 801 |
} |
| 802 |
TclListObjLength(NULL, elemObjPtr, &numOps); |
| 803 |
if (0 == numOps) { |
| 804 |
Tcl_DecrRefCount(elemObjPtr); |
| 805 |
continue; |
| 806 |
} |
| 807 |
eachTraceObjPtr = Tcl_NewListObj(0, NULL); |
| 808 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, elemObjPtr); |
| 809 |
Tcl_DecrRefCount(elemObjPtr); |
| 810 |
|
| 811 |
elemObjPtr = Tcl_NewStringObj(tcmdPtr->command, -1); |
| 812 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, elemObjPtr); |
| 813 |
Tcl_ListObjAppendElement(interp, resultListPtr, eachTraceObjPtr); |
| 814 |
} |
| 815 |
Tcl_SetObjResult(interp, resultListPtr); |
| 816 |
break; |
| 817 |
} |
| 818 |
} |
| 819 |
return TCL_OK; |
| 820 |
} |
| 821 |
|
| 822 |
/* |
| 823 |
*---------------------------------------------------------------------- |
| 824 |
* |
| 825 |
* TraceVariableObjCmd -- |
| 826 |
* |
| 827 |
* Helper function for Tcl_TraceObjCmd; implements the [trace |
| 828 |
* {add|info|remove} variable ...] subcommands. See the user |
| 829 |
* documentation for details on what these do. |
| 830 |
* |
| 831 |
* Results: |
| 832 |
* Standard Tcl result. |
| 833 |
* |
| 834 |
* Side effects: |
| 835 |
* Depends on the operation (add, remove, or info) being performed; may |
| 836 |
* add or remove variable traces on a variable. |
| 837 |
* |
| 838 |
*---------------------------------------------------------------------- |
| 839 |
*/ |
| 840 |
|
| 841 |
static int |
| 842 |
TraceVariableObjCmd( |
| 843 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 844 |
int optionIndex, /* Add, info or remove */ |
| 845 |
int objc, /* Number of arguments. */ |
| 846 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 847 |
{ |
| 848 |
int commandLength, index; |
| 849 |
const char *name, *command; |
| 850 |
size_t length; |
| 851 |
ClientData clientData; |
| 852 |
enum traceOptions { TRACE_ADD, TRACE_INFO, TRACE_REMOVE }; |
| 853 |
static const char *const opStrings[] = { |
| 854 |
"array", "read", "unset", "write", NULL |
| 855 |
}; |
| 856 |
enum operations { |
| 857 |
TRACE_VAR_ARRAY, TRACE_VAR_READ, TRACE_VAR_UNSET, TRACE_VAR_WRITE |
| 858 |
}; |
| 859 |
|
| 860 |
switch ((enum traceOptions) optionIndex) { |
| 861 |
case TRACE_ADD: |
| 862 |
case TRACE_REMOVE: { |
| 863 |
int flags = 0, result; |
| 864 |
int i, listLen; |
| 865 |
Tcl_Obj **elemPtrs; |
| 866 |
|
| 867 |
if (objc != 6) { |
| 868 |
Tcl_WrongNumArgs(interp, 3, objv, "name opList command"); |
| 869 |
return TCL_ERROR; |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* Make sure the ops argument is a list object; get its length and a |
| 874 |
* pointer to its array of element pointers. |
| 875 |
*/ |
| 876 |
|
| 877 |
result = TclListObjGetElements(interp, objv[4], &listLen, &elemPtrs); |
| 878 |
if (result != TCL_OK) { |
| 879 |
return result; |
| 880 |
} |
| 881 |
if (listLen == 0) { |
| 882 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 883 |
"bad operation list \"\": must be one or more of" |
| 884 |
" array, read, unset, or write", -1)); |
| 885 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "TRACE", "NOOPS", |
| 886 |
(char *)NULL); |
| 887 |
return TCL_ERROR; |
| 888 |
} |
| 889 |
for (i = 0; i < listLen ; i++) { |
| 890 |
if (Tcl_GetIndexFromObj(interp, elemPtrs[i], opStrings, |
| 891 |
"operation", TCL_EXACT, &index) != TCL_OK) { |
| 892 |
return TCL_ERROR; |
| 893 |
} |
| 894 |
switch ((enum operations) index) { |
| 895 |
case TRACE_VAR_ARRAY: |
| 896 |
flags |= TCL_TRACE_ARRAY; |
| 897 |
break; |
| 898 |
case TRACE_VAR_READ: |
| 899 |
flags |= TCL_TRACE_READS; |
| 900 |
break; |
| 901 |
case TRACE_VAR_UNSET: |
| 902 |
flags |= TCL_TRACE_UNSETS; |
| 903 |
break; |
| 904 |
case TRACE_VAR_WRITE: |
| 905 |
flags |= TCL_TRACE_WRITES; |
| 906 |
break; |
| 907 |
} |
| 908 |
} |
| 909 |
command = TclGetStringFromObj(objv[5], &commandLength); |
| 910 |
length = commandLength; |
| 911 |
if ((enum traceOptions) optionIndex == TRACE_ADD) { |
| 912 |
CombinedTraceVarInfo *ctvarPtr = (CombinedTraceVarInfo *)ckalloc( |
| 913 |
TclOffset(CombinedTraceVarInfo, traceCmdInfo.command) |
| 914 |
+ 1 + length); |
| 915 |
|
| 916 |
ctvarPtr->traceCmdInfo.flags = flags; |
| 917 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 918 |
if (objv[0] == NULL) { |
| 919 |
ctvarPtr->traceCmdInfo.flags |= TCL_TRACE_OLD_STYLE; |
| 920 |
} |
| 921 |
#endif |
| 922 |
ctvarPtr->traceCmdInfo.length = length; |
| 923 |
flags |= TCL_TRACE_UNSETS | TCL_TRACE_RESULT_OBJECT; |
| 924 |
memcpy(ctvarPtr->traceCmdInfo.command, command, length+1); |
| 925 |
ctvarPtr->traceInfo.traceProc = TraceVarProc; |
| 926 |
ctvarPtr->traceInfo.clientData = &ctvarPtr->traceCmdInfo; |
| 927 |
ctvarPtr->traceInfo.flags = flags; |
| 928 |
name = TclGetString(objv[3]); |
| 929 |
if (TraceVarEx(interp, name, NULL, (VarTrace *) ctvarPtr) |
| 930 |
!= TCL_OK) { |
| 931 |
ckfree(ctvarPtr); |
| 932 |
return TCL_ERROR; |
| 933 |
} |
| 934 |
} else { |
| 935 |
/* |
| 936 |
* Search through all of our traces on this variable to see if |
| 937 |
* there's one with the given command. If so, then delete the |
| 938 |
* first one that matches. |
| 939 |
*/ |
| 940 |
|
| 941 |
name = TclGetString(objv[3]); |
| 942 |
FOREACH_VAR_TRACE(interp, name, clientData) { |
| 943 |
TraceVarInfo *tvarPtr = (TraceVarInfo *)clientData; |
| 944 |
|
| 945 |
if ((tvarPtr->length == length) |
| 946 |
&& ((tvarPtr->flags |
| 947 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 948 |
& ~TCL_TRACE_OLD_STYLE |
| 949 |
#endif |
| 950 |
)==flags) |
| 951 |
&& (strncmp(command, tvarPtr->command, |
| 952 |
length) == 0)) { |
| 953 |
Tcl_UntraceVar2(interp, name, NULL, |
| 954 |
flags | TCL_TRACE_UNSETS | TCL_TRACE_RESULT_OBJECT, |
| 955 |
TraceVarProc, clientData); |
| 956 |
break; |
| 957 |
} |
| 958 |
} |
| 959 |
} |
| 960 |
break; |
| 961 |
} |
| 962 |
case TRACE_INFO: { |
| 963 |
Tcl_Obj *resultListPtr; |
| 964 |
|
| 965 |
if (objc != 4) { |
| 966 |
Tcl_WrongNumArgs(interp, 3, objv, "name"); |
| 967 |
return TCL_ERROR; |
| 968 |
} |
| 969 |
|
| 970 |
TclNewObj(resultListPtr); |
| 971 |
name = TclGetString(objv[3]); |
| 972 |
FOREACH_VAR_TRACE(interp, name, clientData) { |
| 973 |
Tcl_Obj *opObjPtr, *eachTraceObjPtr, *elemObjPtr; |
| 974 |
TraceVarInfo *tvarPtr = (TraceVarInfo *)clientData; |
| 975 |
|
| 976 |
/* |
| 977 |
* Build a list with the ops list as the first obj element and the |
| 978 |
* tcmdPtr->command string as the second obj element. Append this |
| 979 |
* list (as an element) to the end of the result object list. |
| 980 |
*/ |
| 981 |
|
| 982 |
elemObjPtr = Tcl_NewListObj(0, NULL); |
| 983 |
if (tvarPtr->flags & TCL_TRACE_ARRAY) { |
| 984 |
TclNewLiteralStringObj(opObjPtr, "array"); |
| 985 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObjPtr); |
| 986 |
} |
| 987 |
if (tvarPtr->flags & TCL_TRACE_READS) { |
| 988 |
TclNewLiteralStringObj(opObjPtr, "read"); |
| 989 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObjPtr); |
| 990 |
} |
| 991 |
if (tvarPtr->flags & TCL_TRACE_WRITES) { |
| 992 |
TclNewLiteralStringObj(opObjPtr, "write"); |
| 993 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObjPtr); |
| 994 |
} |
| 995 |
if (tvarPtr->flags & TCL_TRACE_UNSETS) { |
| 996 |
TclNewLiteralStringObj(opObjPtr, "unset"); |
| 997 |
Tcl_ListObjAppendElement(NULL, elemObjPtr, opObjPtr); |
| 998 |
} |
| 999 |
eachTraceObjPtr = Tcl_NewListObj(0, NULL); |
| 1000 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, elemObjPtr); |
| 1001 |
|
| 1002 |
elemObjPtr = Tcl_NewStringObj(tvarPtr->command, -1); |
| 1003 |
Tcl_ListObjAppendElement(NULL, eachTraceObjPtr, elemObjPtr); |
| 1004 |
Tcl_ListObjAppendElement(interp, resultListPtr, |
| 1005 |
eachTraceObjPtr); |
| 1006 |
} |
| 1007 |
Tcl_SetObjResult(interp, resultListPtr); |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
return TCL_OK; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* |
| 1015 |
*---------------------------------------------------------------------- |
| 1016 |
* |
| 1017 |
* Tcl_CommandTraceInfo -- |
| 1018 |
* |
| 1019 |
* Return the clientData value associated with a trace on a command. |
| 1020 |
* This function can also be used to step through all of the traces on a |
| 1021 |
* particular command that have the same trace function. |
| 1022 |
* |
| 1023 |
* Results: |
| 1024 |
* The return value is the clientData value associated with a trace on |
| 1025 |
* the given command. Information will only be returned for a trace with |
| 1026 |
* proc as trace function. If the clientData argument is NULL then the |
| 1027 |
* first such trace is returned; otherwise, the next relevant one after |
| 1028 |
* the one given by clientData will be returned. If the command doesn't |
| 1029 |
* exist then an error message is left in the interpreter and NULL is |
| 1030 |
* returned. Also, if there are no (more) traces for the given command, |
| 1031 |
* NULL is returned. |
| 1032 |
* |
| 1033 |
* Side effects: |
| 1034 |
* None. |
| 1035 |
* |
| 1036 |
*---------------------------------------------------------------------- |
| 1037 |
*/ |
| 1038 |
|
| 1039 |
ClientData |
| 1040 |
Tcl_CommandTraceInfo( |
| 1041 |
Tcl_Interp *interp, /* Interpreter containing command. */ |
| 1042 |
const char *cmdName, /* Name of command. */ |
| 1043 |
int flags, /* OR-ed combo or TCL_GLOBAL_ONLY, |
| 1044 |
* TCL_NAMESPACE_ONLY (can be 0). */ |
| 1045 |
Tcl_CommandTraceProc *proc, /* Function assocated with trace. */ |
| 1046 |
ClientData prevClientData) /* If non-NULL, gives last value returned by |
| 1047 |
* this function, so this call will return the |
| 1048 |
* next trace after that one. If NULL, this |
| 1049 |
* call will return the first trace. */ |
| 1050 |
{ |
| 1051 |
Command *cmdPtr; |
| 1052 |
CommandTrace *tracePtr; |
| 1053 |
|
| 1054 |
cmdPtr = (Command *) Tcl_FindCommand(interp, cmdName, NULL, |
| 1055 |
TCL_LEAVE_ERR_MSG); |
| 1056 |
if (cmdPtr == NULL) { |
| 1057 |
return NULL; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* |
| 1061 |
* Find the relevant trace, if any, and return its clientData. |
| 1062 |
*/ |
| 1063 |
|
| 1064 |
tracePtr = cmdPtr->tracePtr; |
| 1065 |
if (prevClientData != NULL) { |
| 1066 |
for (; tracePtr!=NULL ; tracePtr=tracePtr->nextPtr) { |
| 1067 |
if ((tracePtr->clientData == prevClientData) |
| 1068 |
&& (tracePtr->traceProc == proc)) { |
| 1069 |
tracePtr = tracePtr->nextPtr; |
| 1070 |
break; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
for (; tracePtr!=NULL ; tracePtr=tracePtr->nextPtr) { |
| 1075 |
if (tracePtr->traceProc == proc) { |
| 1076 |
return tracePtr->clientData; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
return NULL; |
| 1080 |
} |
| 1081 |
|
| 1082 |
/* |
| 1083 |
*---------------------------------------------------------------------- |
| 1084 |
* |
| 1085 |
* Tcl_TraceCommand -- |
| 1086 |
* |
| 1087 |
* Arrange for rename/deletes to a command to cause a function to be |
| 1088 |
* invoked, which can monitor the operations. |
| 1089 |
* |
| 1090 |
* Also optionally arrange for execution of that command to cause a |
| 1091 |
* function to be invoked. |
| 1092 |
* |
| 1093 |
* Results: |
| 1094 |
* A standard Tcl return value. |
| 1095 |
* |
| 1096 |
* Side effects: |
| 1097 |
* A trace is set up on the command given by cmdName, such that future |
| 1098 |
* changes to the command will be mediated by proc. See the manual |
| 1099 |
* entry for complete details on the calling sequence for proc. |
| 1100 |
* |
| 1101 |
*---------------------------------------------------------------------- |
| 1102 |
*/ |
| 1103 |
|
| 1104 |
int |
| 1105 |
Tcl_TraceCommand( |
| 1106 |
Tcl_Interp *interp, /* Interpreter in which command is to be |
| 1107 |
* traced. */ |
| 1108 |
const char *cmdName, /* Name of command. */ |
| 1109 |
int flags, /* OR-ed collection of bits, including any of |
| 1110 |
* TCL_TRACE_RENAME, TCL_TRACE_DELETE, and any |
| 1111 |
* of the TRACE_*_EXEC flags */ |
| 1112 |
Tcl_CommandTraceProc *proc, /* Function to call when specified ops are |
| 1113 |
* invoked upon cmdName. */ |
| 1114 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 1115 |
{ |
| 1116 |
Command *cmdPtr; |
| 1117 |
CommandTrace *tracePtr; |
| 1118 |
|
| 1119 |
cmdPtr = (Command *) Tcl_FindCommand(interp, cmdName, NULL, |
| 1120 |
TCL_LEAVE_ERR_MSG); |
| 1121 |
if (cmdPtr == NULL) { |
| 1122 |
return TCL_ERROR; |
| 1123 |
} |
| 1124 |
|
| 1125 |
/* |
| 1126 |
* Set up trace information. |
| 1127 |
*/ |
| 1128 |
|
| 1129 |
tracePtr = (CommandTrace *)ckalloc(sizeof(CommandTrace)); |
| 1130 |
tracePtr->traceProc = proc; |
| 1131 |
tracePtr->clientData = clientData; |
| 1132 |
tracePtr->flags = flags & |
| 1133 |
(TCL_TRACE_RENAME | TCL_TRACE_DELETE | TCL_TRACE_ANY_EXEC); |
| 1134 |
tracePtr->nextPtr = cmdPtr->tracePtr; |
| 1135 |
tracePtr->refCount = 1; |
| 1136 |
cmdPtr->tracePtr = tracePtr; |
| 1137 |
if (tracePtr->flags & TCL_TRACE_ANY_EXEC) { |
| 1138 |
/* |
| 1139 |
* Bug 3484621: up the interp's epoch if this is a BC'ed command |
| 1140 |
*/ |
| 1141 |
|
| 1142 |
if ((cmdPtr->compileProc != NULL) && !(cmdPtr->flags & CMD_HAS_EXEC_TRACES)){ |
| 1143 |
Interp *iPtr = (Interp *) interp; |
| 1144 |
iPtr->compileEpoch++; |
| 1145 |
} |
| 1146 |
cmdPtr->flags |= CMD_HAS_EXEC_TRACES; |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
return TCL_OK; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* |
| 1154 |
*---------------------------------------------------------------------- |
| 1155 |
* |
| 1156 |
* Tcl_UntraceCommand -- |
| 1157 |
* |
| 1158 |
* Remove a previously-created trace for a command. |
| 1159 |
* |
| 1160 |
* Results: |
| 1161 |
* None. |
| 1162 |
* |
| 1163 |
* Side effects: |
| 1164 |
* If there exists a trace for the command given by cmdName with the |
| 1165 |
* given flags, proc, and clientData, then that trace is removed. |
| 1166 |
* |
| 1167 |
*---------------------------------------------------------------------- |
| 1168 |
*/ |
| 1169 |
|
| 1170 |
void |
| 1171 |
Tcl_UntraceCommand( |
| 1172 |
Tcl_Interp *interp, /* Interpreter containing command. */ |
| 1173 |
const char *cmdName, /* Name of command. */ |
| 1174 |
int flags, /* OR-ed collection of bits, including any of |
| 1175 |
* TCL_TRACE_RENAME, TCL_TRACE_DELETE, and any |
| 1176 |
* of the TRACE_*_EXEC flags */ |
| 1177 |
Tcl_CommandTraceProc *proc, /* Function assocated with trace. */ |
| 1178 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 1179 |
{ |
| 1180 |
CommandTrace *tracePtr; |
| 1181 |
CommandTrace *prevPtr; |
| 1182 |
Command *cmdPtr; |
| 1183 |
Interp *iPtr = (Interp *)interp; |
| 1184 |
ActiveCommandTrace *activePtr; |
| 1185 |
int hasExecTraces = 0; |
| 1186 |
|
| 1187 |
cmdPtr = (Command *) Tcl_FindCommand(interp, cmdName, NULL, |
| 1188 |
TCL_LEAVE_ERR_MSG); |
| 1189 |
if (cmdPtr == NULL) { |
| 1190 |
return; |
| 1191 |
} |
| 1192 |
|
| 1193 |
flags &= (TCL_TRACE_RENAME | TCL_TRACE_DELETE | TCL_TRACE_ANY_EXEC); |
| 1194 |
|
| 1195 |
for (tracePtr = cmdPtr->tracePtr, prevPtr = NULL; ; |
| 1196 |
prevPtr = tracePtr, tracePtr = tracePtr->nextPtr) { |
| 1197 |
if (tracePtr == NULL) { |
| 1198 |
return; |
| 1199 |
} |
| 1200 |
if ((tracePtr->traceProc == proc) |
| 1201 |
&& ((tracePtr->flags & (TCL_TRACE_RENAME | TCL_TRACE_DELETE | |
| 1202 |
TCL_TRACE_ANY_EXEC)) == flags) |
| 1203 |
&& (tracePtr->clientData == clientData)) { |
| 1204 |
if (tracePtr->flags & TCL_TRACE_ANY_EXEC) { |
| 1205 |
hasExecTraces = 1; |
| 1206 |
} |
| 1207 |
break; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* |
| 1212 |
* The code below makes it possible to delete traces while traces are |
| 1213 |
* active: it makes sure that the deleted trace won't be processed by |
| 1214 |
* CallCommandTraces. |
| 1215 |
*/ |
| 1216 |
|
| 1217 |
for (activePtr = iPtr->activeCmdTracePtr; activePtr != NULL; |
| 1218 |
activePtr = activePtr->nextPtr) { |
| 1219 |
if (activePtr->nextTracePtr == tracePtr) { |
| 1220 |
if (activePtr->reverseScan) { |
| 1221 |
activePtr->nextTracePtr = prevPtr; |
| 1222 |
} else { |
| 1223 |
activePtr->nextTracePtr = tracePtr->nextPtr; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
} |
| 1227 |
if (prevPtr == NULL) { |
| 1228 |
cmdPtr->tracePtr = tracePtr->nextPtr; |
| 1229 |
} else { |
| 1230 |
prevPtr->nextPtr = tracePtr->nextPtr; |
| 1231 |
} |
| 1232 |
tracePtr->flags = 0; |
| 1233 |
|
| 1234 |
if (tracePtr->refCount-- <= 1) { |
| 1235 |
ckfree(tracePtr); |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (hasExecTraces) { |
| 1239 |
for (tracePtr = cmdPtr->tracePtr, prevPtr = NULL; tracePtr != NULL ; |
| 1240 |
prevPtr = tracePtr, tracePtr = tracePtr->nextPtr) { |
| 1241 |
if (tracePtr->flags & TCL_TRACE_ANY_EXEC) { |
| 1242 |
return; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
/* |
| 1247 |
* None of the remaining traces on this command are execution traces. |
| 1248 |
* We therefore remove this flag: |
| 1249 |
*/ |
| 1250 |
|
| 1251 |
cmdPtr->flags &= ~CMD_HAS_EXEC_TRACES; |
| 1252 |
|
| 1253 |
/* |
| 1254 |
* Bug 3484621: up the interp's epoch if this is a BC'ed command |
| 1255 |
*/ |
| 1256 |
|
| 1257 |
if (cmdPtr->compileProc != NULL) { |
| 1258 |
iPtr->compileEpoch++; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* |
| 1264 |
*---------------------------------------------------------------------- |
| 1265 |
* |
| 1266 |
* TraceCommandProc -- |
| 1267 |
* |
| 1268 |
* This function is called to handle command changes that have been |
| 1269 |
* traced using the "trace" command, when using the 'rename' or 'delete' |
| 1270 |
* options. |
| 1271 |
* |
| 1272 |
* Results: |
| 1273 |
* None. |
| 1274 |
* |
| 1275 |
* Side effects: |
| 1276 |
* Depends on the command associated with the trace. |
| 1277 |
* |
| 1278 |
*---------------------------------------------------------------------- |
| 1279 |
*/ |
| 1280 |
|
| 1281 |
static void |
| 1282 |
TraceCommandProc( |
| 1283 |
ClientData clientData, /* Information about the command trace. */ |
| 1284 |
Tcl_Interp *interp, /* Interpreter containing command. */ |
| 1285 |
const char *oldName, /* Name of command being changed. */ |
| 1286 |
const char *newName, /* New name of command. Empty string or NULL |
| 1287 |
* means command is being deleted (renamed to |
| 1288 |
* ""). */ |
| 1289 |
int flags) /* OR-ed bits giving operation and other |
| 1290 |
* information. */ |
| 1291 |
{ |
| 1292 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 1293 |
int code; |
| 1294 |
Tcl_DString cmd; |
| 1295 |
|
| 1296 |
tcmdPtr->refCount++; |
| 1297 |
|
| 1298 |
if ((tcmdPtr->flags & flags) && !Tcl_InterpDeleted(interp) |
| 1299 |
&& !Tcl_LimitExceeded(interp)) { |
| 1300 |
/* |
| 1301 |
* Generate a command to execute by appending list elements for the |
| 1302 |
* old and new command name and the operation. |
| 1303 |
*/ |
| 1304 |
|
| 1305 |
Tcl_DStringInit(&cmd); |
| 1306 |
Tcl_DStringAppend(&cmd, tcmdPtr->command, tcmdPtr->length); |
| 1307 |
Tcl_DStringAppendElement(&cmd, oldName); |
| 1308 |
Tcl_DStringAppendElement(&cmd, (newName ? newName : "")); |
| 1309 |
if (flags & TCL_TRACE_RENAME) { |
| 1310 |
TclDStringAppendLiteral(&cmd, " rename"); |
| 1311 |
} else if (flags & TCL_TRACE_DELETE) { |
| 1312 |
TclDStringAppendLiteral(&cmd, " delete"); |
| 1313 |
} |
| 1314 |
|
| 1315 |
/* |
| 1316 |
* Execute the command. We discard any object result the command |
| 1317 |
* returns. |
| 1318 |
* |
| 1319 |
* Add the TCL_TRACE_DESTROYED flag to tcmdPtr to indicate to other |
| 1320 |
* areas that this will be destroyed by us, otherwise a double-free |
| 1321 |
* might occur depending on what the eval does. |
| 1322 |
*/ |
| 1323 |
|
| 1324 |
if (flags & TCL_TRACE_DESTROYED) { |
| 1325 |
tcmdPtr->flags |= TCL_TRACE_DESTROYED; |
| 1326 |
} |
| 1327 |
code = Tcl_EvalEx(interp, Tcl_DStringValue(&cmd), |
| 1328 |
Tcl_DStringLength(&cmd), 0); |
| 1329 |
if (code != TCL_OK) { |
| 1330 |
/* We ignore errors in these traced commands */ |
| 1331 |
/*** QUESTION: Use Tcl_BackgroundException(interp, code); instead? ***/ |
| 1332 |
} |
| 1333 |
Tcl_DStringFree(&cmd); |
| 1334 |
} |
| 1335 |
|
| 1336 |
/* |
| 1337 |
* We delete when the trace was destroyed or if this is a delete trace, |
| 1338 |
* because command deletes are unconditional, so the trace must go away. |
| 1339 |
*/ |
| 1340 |
|
| 1341 |
if (flags & (TCL_TRACE_DESTROYED | TCL_TRACE_DELETE)) { |
| 1342 |
int untraceFlags = tcmdPtr->flags; |
| 1343 |
Tcl_InterpState state; |
| 1344 |
|
| 1345 |
if (tcmdPtr->stepTrace != NULL) { |
| 1346 |
Tcl_DeleteTrace(interp, tcmdPtr->stepTrace); |
| 1347 |
tcmdPtr->stepTrace = NULL; |
| 1348 |
ckfree(tcmdPtr->startCmd); |
| 1349 |
} |
| 1350 |
if (tcmdPtr->flags & TCL_TRACE_EXEC_IN_PROGRESS) { |
| 1351 |
/* |
| 1352 |
* Postpone deletion, until exec trace returns. |
| 1353 |
*/ |
| 1354 |
|
| 1355 |
tcmdPtr->flags = 0; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* |
| 1359 |
* We need to construct the same flags for Tcl_UntraceCommand as were |
| 1360 |
* passed to Tcl_TraceCommand. Reproduce the processing of [trace add |
| 1361 |
* execution/command]. Be careful to keep this code in sync with that. |
| 1362 |
*/ |
| 1363 |
|
| 1364 |
if (untraceFlags & TCL_TRACE_ANY_EXEC) { |
| 1365 |
untraceFlags |= TCL_TRACE_DELETE; |
| 1366 |
if (untraceFlags & (TCL_TRACE_ENTER_DURING_EXEC |
| 1367 |
| TCL_TRACE_LEAVE_DURING_EXEC)) { |
| 1368 |
untraceFlags |= (TCL_TRACE_ENTER_EXEC | TCL_TRACE_LEAVE_EXEC); |
| 1369 |
} |
| 1370 |
} else if (untraceFlags & TCL_TRACE_RENAME) { |
| 1371 |
untraceFlags |= TCL_TRACE_DELETE; |
| 1372 |
} |
| 1373 |
|
| 1374 |
/* |
| 1375 |
* Remove the trace since TCL_TRACE_DESTROYED tells us to, or the |
| 1376 |
* command we're tracing has just gone away. Then decrement the |
| 1377 |
* clientData refCount that was set up by trace creation. |
| 1378 |
* |
| 1379 |
* Note that we save the (return) state of the interpreter to prevent |
| 1380 |
* bizarre error messages. |
| 1381 |
*/ |
| 1382 |
|
| 1383 |
state = Tcl_SaveInterpState(interp, TCL_OK); |
| 1384 |
Tcl_UntraceCommand(interp, oldName, untraceFlags, |
| 1385 |
TraceCommandProc, clientData); |
| 1386 |
Tcl_RestoreInterpState(interp, state); |
| 1387 |
tcmdPtr->refCount--; |
| 1388 |
} |
| 1389 |
if (tcmdPtr->refCount-- <= 1) { |
| 1390 |
ckfree(tcmdPtr); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* |
| 1395 |
*---------------------------------------------------------------------- |
| 1396 |
* |
| 1397 |
* TclCheckExecutionTraces -- |
| 1398 |
* |
| 1399 |
* Checks on all current command execution traces, and invokes functions |
| 1400 |
* which have been registered. This function can be used by other code |
| 1401 |
* which performs execution to unify the tracing system, so that |
| 1402 |
* execution traces will function for that other code. |
| 1403 |
* |
| 1404 |
* For instance extensions like [incr Tcl] which use their own execution |
| 1405 |
* technique can make use of Tcl's tracing. |
| 1406 |
* |
| 1407 |
* This function is called by 'TclEvalObjvInternal' |
| 1408 |
* |
| 1409 |
* Results: |
| 1410 |
* The return value is a standard Tcl completion code such as TCL_OK or |
| 1411 |
* TCL_ERROR, etc. |
| 1412 |
* |
| 1413 |
* Side effects: |
| 1414 |
* Those side effects made by any trace functions called. |
| 1415 |
* |
| 1416 |
*---------------------------------------------------------------------- |
| 1417 |
*/ |
| 1418 |
|
| 1419 |
int |
| 1420 |
TclCheckExecutionTraces( |
| 1421 |
Tcl_Interp *interp, /* The current interpreter. */ |
| 1422 |
const char *command, /* Pointer to beginning of the current command |
| 1423 |
* string. */ |
| 1424 |
int numChars, /* The number of characters in 'command' which |
| 1425 |
* are part of the command string. */ |
| 1426 |
Command *cmdPtr, /* Points to command's Command struct. */ |
| 1427 |
int code, /* The current result code. */ |
| 1428 |
int traceFlags, /* Current tracing situation. */ |
| 1429 |
int objc, /* Number of arguments for the command. */ |
| 1430 |
Tcl_Obj *const objv[]) /* Pointers to Tcl_Obj of each argument. */ |
| 1431 |
{ |
| 1432 |
Interp *iPtr = (Interp *) interp; |
| 1433 |
CommandTrace *tracePtr, *lastTracePtr; |
| 1434 |
ActiveCommandTrace active; |
| 1435 |
int curLevel; |
| 1436 |
int traceCode = TCL_OK; |
| 1437 |
Tcl_InterpState state = NULL; |
| 1438 |
|
| 1439 |
if (cmdPtr->tracePtr == NULL) { |
| 1440 |
return traceCode; |
| 1441 |
} |
| 1442 |
|
| 1443 |
curLevel = iPtr->varFramePtr->level; |
| 1444 |
|
| 1445 |
active.nextPtr = iPtr->activeCmdTracePtr; |
| 1446 |
iPtr->activeCmdTracePtr = &active; |
| 1447 |
|
| 1448 |
active.cmdPtr = cmdPtr; |
| 1449 |
lastTracePtr = NULL; |
| 1450 |
for (tracePtr = cmdPtr->tracePtr; |
| 1451 |
(traceCode == TCL_OK) && (tracePtr != NULL); |
| 1452 |
tracePtr = active.nextTracePtr) { |
| 1453 |
if (traceFlags & TCL_TRACE_LEAVE_EXEC) { |
| 1454 |
/* |
| 1455 |
* Execute the trace command in order of creation for "leave". |
| 1456 |
*/ |
| 1457 |
|
| 1458 |
active.reverseScan = 1; |
| 1459 |
active.nextTracePtr = NULL; |
| 1460 |
tracePtr = cmdPtr->tracePtr; |
| 1461 |
while (tracePtr->nextPtr != lastTracePtr) { |
| 1462 |
active.nextTracePtr = tracePtr; |
| 1463 |
tracePtr = tracePtr->nextPtr; |
| 1464 |
} |
| 1465 |
} else { |
| 1466 |
active.reverseScan = 0; |
| 1467 |
active.nextTracePtr = tracePtr->nextPtr; |
| 1468 |
} |
| 1469 |
if (tracePtr->traceProc == TraceCommandProc) { |
| 1470 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)tracePtr->clientData; |
| 1471 |
|
| 1472 |
if (tcmdPtr->flags != 0) { |
| 1473 |
tcmdPtr->curFlags = traceFlags | TCL_TRACE_EXEC_DIRECT; |
| 1474 |
tcmdPtr->curCode = code; |
| 1475 |
tcmdPtr->refCount++; |
| 1476 |
if (state == NULL) { |
| 1477 |
state = Tcl_SaveInterpState(interp, code); |
| 1478 |
} |
| 1479 |
traceCode = TraceExecutionProc(tcmdPtr, interp, curLevel, |
| 1480 |
command, (Tcl_Command) cmdPtr, objc, objv); |
| 1481 |
if (tcmdPtr->refCount-- <= 1) { |
| 1482 |
ckfree(tcmdPtr); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
} |
| 1486 |
if (active.nextTracePtr) { |
| 1487 |
lastTracePtr = active.nextTracePtr->nextPtr; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
iPtr->activeCmdTracePtr = active.nextPtr; |
| 1491 |
if (state) { |
| 1492 |
if (traceCode == TCL_OK) { |
| 1493 |
(void) Tcl_RestoreInterpState(interp, state); |
| 1494 |
} else { |
| 1495 |
Tcl_DiscardInterpState(state); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
return traceCode; |
| 1500 |
} |
| 1501 |
|
| 1502 |
/* |
| 1503 |
*---------------------------------------------------------------------- |
| 1504 |
* |
| 1505 |
* TclCheckInterpTraces -- |
| 1506 |
* |
| 1507 |
* Checks on all current traces, and invokes functions which have been |
| 1508 |
* registered. This function can be used by other code which performs |
| 1509 |
* execution to unify the tracing system. For instance extensions like |
| 1510 |
* [incr Tcl] which use their own execution technique can make use of |
| 1511 |
* Tcl's tracing. |
| 1512 |
* |
| 1513 |
* This function is called by 'TclEvalObjvInternal' |
| 1514 |
* |
| 1515 |
* Results: |
| 1516 |
* The return value is a standard Tcl completion code such as TCL_OK or |
| 1517 |
* TCL_ERROR, etc. |
| 1518 |
* |
| 1519 |
* Side effects: |
| 1520 |
* Those side effects made by any trace functions called. |
| 1521 |
* |
| 1522 |
*---------------------------------------------------------------------- |
| 1523 |
*/ |
| 1524 |
|
| 1525 |
int |
| 1526 |
TclCheckInterpTraces( |
| 1527 |
Tcl_Interp *interp, /* The current interpreter. */ |
| 1528 |
const char *command, /* Pointer to beginning of the current command |
| 1529 |
* string. */ |
| 1530 |
int numChars, /* The number of characters in 'command' which |
| 1531 |
* are part of the command string. */ |
| 1532 |
Command *cmdPtr, /* Points to command's Command struct. */ |
| 1533 |
int code, /* The current result code. */ |
| 1534 |
int traceFlags, /* Current tracing situation. */ |
| 1535 |
int objc, /* Number of arguments for the command. */ |
| 1536 |
Tcl_Obj *const objv[]) /* Pointers to Tcl_Obj of each argument. */ |
| 1537 |
{ |
| 1538 |
Interp *iPtr = (Interp *) interp; |
| 1539 |
Trace *tracePtr, *lastTracePtr; |
| 1540 |
ActiveInterpTrace active; |
| 1541 |
int curLevel; |
| 1542 |
int traceCode = TCL_OK; |
| 1543 |
Tcl_InterpState state = NULL; |
| 1544 |
|
| 1545 |
if ((iPtr->tracePtr == NULL) |
| 1546 |
|| (iPtr->flags & INTERP_TRACE_IN_PROGRESS)) { |
| 1547 |
return(traceCode); |
| 1548 |
} |
| 1549 |
|
| 1550 |
curLevel = iPtr->numLevels; |
| 1551 |
|
| 1552 |
active.nextPtr = iPtr->activeInterpTracePtr; |
| 1553 |
iPtr->activeInterpTracePtr = &active; |
| 1554 |
|
| 1555 |
lastTracePtr = NULL; |
| 1556 |
for (tracePtr = iPtr->tracePtr; |
| 1557 |
(traceCode == TCL_OK) && (tracePtr != NULL); |
| 1558 |
tracePtr = active.nextTracePtr) { |
| 1559 |
if (traceFlags & TCL_TRACE_ENTER_EXEC) { |
| 1560 |
/* |
| 1561 |
* Execute the trace command in reverse order of creation for |
| 1562 |
* "enterstep" operation. The order is changed for "enterstep" |
| 1563 |
* instead of for "leavestep" as was done in |
| 1564 |
* TclCheckExecutionTraces because for step traces, |
| 1565 |
* Tcl_CreateObjTrace creates one more linked list of traces which |
| 1566 |
* results in one more reversal of trace invocation. |
| 1567 |
*/ |
| 1568 |
|
| 1569 |
active.reverseScan = 1; |
| 1570 |
active.nextTracePtr = NULL; |
| 1571 |
tracePtr = iPtr->tracePtr; |
| 1572 |
while (tracePtr->nextPtr != lastTracePtr) { |
| 1573 |
active.nextTracePtr = tracePtr; |
| 1574 |
tracePtr = tracePtr->nextPtr; |
| 1575 |
} |
| 1576 |
if (active.nextTracePtr) { |
| 1577 |
lastTracePtr = active.nextTracePtr->nextPtr; |
| 1578 |
} |
| 1579 |
} else { |
| 1580 |
active.reverseScan = 0; |
| 1581 |
active.nextTracePtr = tracePtr->nextPtr; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (tracePtr->level > 0 && curLevel > tracePtr->level) { |
| 1585 |
continue; |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (!(tracePtr->flags & TCL_TRACE_EXEC_IN_PROGRESS)) { |
| 1589 |
/* |
| 1590 |
* The proc invoked might delete the traced command which which |
| 1591 |
* might try to free tracePtr. We want to use tracePtr until the |
| 1592 |
* end of this if section, so we use Tcl_Preserve() and |
| 1593 |
* Tcl_Release() to be sure it is not freed while we still need |
| 1594 |
* it. |
| 1595 |
*/ |
| 1596 |
|
| 1597 |
Tcl_Preserve(tracePtr); |
| 1598 |
tracePtr->flags |= TCL_TRACE_EXEC_IN_PROGRESS; |
| 1599 |
if (state == NULL) { |
| 1600 |
state = Tcl_SaveInterpState(interp, code); |
| 1601 |
} |
| 1602 |
|
| 1603 |
if (tracePtr->flags & |
| 1604 |
(TCL_TRACE_ENTER_EXEC | TCL_TRACE_LEAVE_EXEC)) { |
| 1605 |
/* |
| 1606 |
* New style trace. |
| 1607 |
*/ |
| 1608 |
|
| 1609 |
if (tracePtr->flags & traceFlags) { |
| 1610 |
if (tracePtr->proc == TraceExecutionProc) { |
| 1611 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)tracePtr->clientData; |
| 1612 |
|
| 1613 |
tcmdPtr->curFlags = traceFlags; |
| 1614 |
tcmdPtr->curCode = code; |
| 1615 |
} |
| 1616 |
traceCode = tracePtr->proc(tracePtr->clientData, interp, |
| 1617 |
curLevel, command, (Tcl_Command) cmdPtr, objc, |
| 1618 |
objv); |
| 1619 |
} |
| 1620 |
} else { |
| 1621 |
/* |
| 1622 |
* Old-style trace. |
| 1623 |
*/ |
| 1624 |
|
| 1625 |
if (traceFlags & TCL_TRACE_ENTER_EXEC) { |
| 1626 |
/* |
| 1627 |
* Old-style interpreter-wide traces only trigger before |
| 1628 |
* the command is executed. |
| 1629 |
*/ |
| 1630 |
|
| 1631 |
traceCode = CallTraceFunction(interp, tracePtr, cmdPtr, |
| 1632 |
command, numChars, objc, objv); |
| 1633 |
} |
| 1634 |
} |
| 1635 |
tracePtr->flags &= ~TCL_TRACE_EXEC_IN_PROGRESS; |
| 1636 |
Tcl_Release(tracePtr); |
| 1637 |
} |
| 1638 |
} |
| 1639 |
iPtr->activeInterpTracePtr = active.nextPtr; |
| 1640 |
if (state) { |
| 1641 |
if (traceCode == TCL_OK) { |
| 1642 |
Tcl_RestoreInterpState(interp, state); |
| 1643 |
} else { |
| 1644 |
Tcl_DiscardInterpState(state); |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
return traceCode; |
| 1649 |
} |
| 1650 |
|
| 1651 |
/* |
| 1652 |
*---------------------------------------------------------------------- |
| 1653 |
* |
| 1654 |
* CallTraceFunction -- |
| 1655 |
* |
| 1656 |
* Invokes a trace function registered with an interpreter. These |
| 1657 |
* functions trace command execution. Currently this trace function is |
| 1658 |
* called with the address of the string-based Tcl_CmdProc for the |
| 1659 |
* command, not the Tcl_ObjCmdProc. |
| 1660 |
* |
| 1661 |
* Results: |
| 1662 |
* None. |
| 1663 |
* |
| 1664 |
* Side effects: |
| 1665 |
* Those side effects made by the trace function. |
| 1666 |
* |
| 1667 |
*---------------------------------------------------------------------- |
| 1668 |
*/ |
| 1669 |
|
| 1670 |
static int |
| 1671 |
CallTraceFunction( |
| 1672 |
Tcl_Interp *interp, /* The current interpreter. */ |
| 1673 |
Trace *tracePtr, /* Describes the trace function to call. */ |
| 1674 |
Command *cmdPtr, /* Points to command's Command struct. */ |
| 1675 |
const char *command, /* Points to the first character of the |
| 1676 |
* command's source before substitutions. */ |
| 1677 |
int numChars, /* The number of characters in the command's |
| 1678 |
* source. */ |
| 1679 |
int objc, /* Number of arguments for the command. */ |
| 1680 |
Tcl_Obj *const objv[]) /* Pointers to Tcl_Obj of each argument. */ |
| 1681 |
{ |
| 1682 |
Interp *iPtr = (Interp *) interp; |
| 1683 |
char *commandCopy; |
| 1684 |
int traceCode; |
| 1685 |
|
| 1686 |
/* |
| 1687 |
* Copy the command characters into a new string. |
| 1688 |
*/ |
| 1689 |
|
| 1690 |
commandCopy = (char *)TclStackAlloc(interp, numChars + 1); |
| 1691 |
memcpy(commandCopy, command, numChars); |
| 1692 |
commandCopy[numChars] = '\0'; |
| 1693 |
|
| 1694 |
/* |
| 1695 |
* Call the trace function then free allocated storage. |
| 1696 |
*/ |
| 1697 |
|
| 1698 |
traceCode = tracePtr->proc(tracePtr->clientData, (Tcl_Interp *) iPtr, |
| 1699 |
iPtr->numLevels, commandCopy, (Tcl_Command) cmdPtr, objc, objv); |
| 1700 |
|
| 1701 |
TclStackFree(interp, commandCopy); |
| 1702 |
return traceCode; |
| 1703 |
} |
| 1704 |
|
| 1705 |
/* |
| 1706 |
*---------------------------------------------------------------------- |
| 1707 |
* |
| 1708 |
* CommandObjTraceDeleted -- |
| 1709 |
* |
| 1710 |
* Ensure the trace is correctly deleted by decrementing its refCount and |
| 1711 |
* only deleting if no other references exist. |
| 1712 |
* |
| 1713 |
* Results: |
| 1714 |
* None. |
| 1715 |
* |
| 1716 |
* Side effects: |
| 1717 |
* May release memory. |
| 1718 |
* |
| 1719 |
*---------------------------------------------------------------------- |
| 1720 |
*/ |
| 1721 |
|
| 1722 |
static void |
| 1723 |
CommandObjTraceDeleted( |
| 1724 |
ClientData clientData) |
| 1725 |
{ |
| 1726 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 1727 |
|
| 1728 |
if (tcmdPtr->refCount-- <= 1) { |
| 1729 |
ckfree(tcmdPtr); |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
/* |
| 1734 |
*---------------------------------------------------------------------- |
| 1735 |
* |
| 1736 |
* TraceExecutionProc -- |
| 1737 |
* |
| 1738 |
* This function is invoked whenever code relevant to a 'trace execution' |
| 1739 |
* command is executed. It is called in one of two ways in Tcl's core: |
| 1740 |
* |
| 1741 |
* (i) by the TclCheckExecutionTraces, when an execution trace has been |
| 1742 |
* triggered. |
| 1743 |
* (ii) by TclCheckInterpTraces, when a prior execution trace has created |
| 1744 |
* a trace of the internals of a procedure, passing in this function as |
| 1745 |
* the one to be called. |
| 1746 |
* |
| 1747 |
* Results: |
| 1748 |
* The return value is a standard Tcl completion code such as TCL_OK or |
| 1749 |
* TCL_ERROR, etc. |
| 1750 |
* |
| 1751 |
* Side effects: |
| 1752 |
* May invoke an arbitrary Tcl procedure, and may create or delete an |
| 1753 |
* interpreter-wide trace. |
| 1754 |
* |
| 1755 |
*---------------------------------------------------------------------- |
| 1756 |
*/ |
| 1757 |
|
| 1758 |
static int |
| 1759 |
TraceExecutionProc( |
| 1760 |
ClientData clientData, |
| 1761 |
Tcl_Interp *interp, |
| 1762 |
int level, |
| 1763 |
const char *command, |
| 1764 |
Tcl_Command cmdInfo, |
| 1765 |
int objc, |
| 1766 |
Tcl_Obj *const objv[]) |
| 1767 |
{ |
| 1768 |
int call = 0; |
| 1769 |
Interp *iPtr = (Interp *) interp; |
| 1770 |
TraceCommandInfo *tcmdPtr = (TraceCommandInfo *)clientData; |
| 1771 |
int flags = tcmdPtr->curFlags; |
| 1772 |
int code = tcmdPtr->curCode; |
| 1773 |
int traceCode = TCL_OK; |
| 1774 |
|
| 1775 |
if (tcmdPtr->flags & TCL_TRACE_EXEC_IN_PROGRESS) { |
| 1776 |
/* |
| 1777 |
* Inside any kind of execution trace callback, we do not allow any |
| 1778 |
* further execution trace callbacks to be called for the same trace. |
| 1779 |
*/ |
| 1780 |
|
| 1781 |
return traceCode; |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (!Tcl_InterpDeleted(interp) && !Tcl_LimitExceeded(interp)) { |
| 1785 |
/* |
| 1786 |
* Check whether the current call is going to eval arbitrary Tcl code |
| 1787 |
* with a generated trace, or whether we are only going to setup |
| 1788 |
* interpreter-wide traces to implement the 'step' traces. This latter |
| 1789 |
* situation can happen if we create a command trace without either |
| 1790 |
* before or after operations, but with either of the step operations. |
| 1791 |
*/ |
| 1792 |
|
| 1793 |
if (flags & TCL_TRACE_EXEC_DIRECT) { |
| 1794 |
call = flags & tcmdPtr->flags & |
| 1795 |
(TCL_TRACE_ENTER_EXEC | TCL_TRACE_LEAVE_EXEC); |
| 1796 |
} else { |
| 1797 |
call = 1; |
| 1798 |
} |
| 1799 |
|
| 1800 |
/* |
| 1801 |
* First, if we have returned back to the level at which we created an |
| 1802 |
* interpreter trace for enterstep and/or leavestep execution traces, |
| 1803 |
* we remove it here. |
| 1804 |
*/ |
| 1805 |
|
| 1806 |
if ((flags & TCL_TRACE_LEAVE_EXEC) && (tcmdPtr->stepTrace != NULL) |
| 1807 |
&& (level == tcmdPtr->startLevel) |
| 1808 |
&& (strcmp(command, tcmdPtr->startCmd) == 0)) { |
| 1809 |
Tcl_DeleteTrace(interp, tcmdPtr->stepTrace); |
| 1810 |
tcmdPtr->stepTrace = NULL; |
| 1811 |
ckfree(tcmdPtr->startCmd); |
| 1812 |
} |
| 1813 |
|
| 1814 |
/* |
| 1815 |
* Second, create the tcl callback, if required. |
| 1816 |
*/ |
| 1817 |
|
| 1818 |
if (call) { |
| 1819 |
Tcl_DString cmd, sub; |
| 1820 |
int i, saveInterpFlags; |
| 1821 |
|
| 1822 |
Tcl_DStringInit(&cmd); |
| 1823 |
Tcl_DStringAppend(&cmd, tcmdPtr->command, tcmdPtr->length); |
| 1824 |
|
| 1825 |
/* |
| 1826 |
* Append command with arguments. |
| 1827 |
*/ |
| 1828 |
|
| 1829 |
Tcl_DStringInit(&sub); |
| 1830 |
for (i = 0; i < objc; i++) { |
| 1831 |
Tcl_DStringAppendElement(&sub, TclGetString(objv[i])); |
| 1832 |
} |
| 1833 |
Tcl_DStringAppendElement(&cmd, Tcl_DStringValue(&sub)); |
| 1834 |
Tcl_DStringFree(&sub); |
| 1835 |
|
| 1836 |
if (flags & TCL_TRACE_ENTER_EXEC) { |
| 1837 |
/* |
| 1838 |
* Append trace operation. |
| 1839 |
*/ |
| 1840 |
|
| 1841 |
if (flags & TCL_TRACE_EXEC_DIRECT) { |
| 1842 |
Tcl_DStringAppendElement(&cmd, "enter"); |
| 1843 |
} else { |
| 1844 |
Tcl_DStringAppendElement(&cmd, "enterstep"); |
| 1845 |
} |
| 1846 |
} else if (flags & TCL_TRACE_LEAVE_EXEC) { |
| 1847 |
Tcl_Obj *resultCode; |
| 1848 |
const char *resultCodeStr; |
| 1849 |
|
| 1850 |
/* |
| 1851 |
* Append result code. |
| 1852 |
*/ |
| 1853 |
|
| 1854 |
TclNewIntObj(resultCode, code); |
| 1855 |
resultCodeStr = TclGetString(resultCode); |
| 1856 |
Tcl_DStringAppendElement(&cmd, resultCodeStr); |
| 1857 |
Tcl_DecrRefCount(resultCode); |
| 1858 |
|
| 1859 |
/* |
| 1860 |
* Append result string. |
| 1861 |
*/ |
| 1862 |
|
| 1863 |
Tcl_DStringAppendElement(&cmd, Tcl_GetStringResult(interp)); |
| 1864 |
|
| 1865 |
/* |
| 1866 |
* Append trace operation. |
| 1867 |
*/ |
| 1868 |
|
| 1869 |
if (flags & TCL_TRACE_EXEC_DIRECT) { |
| 1870 |
Tcl_DStringAppendElement(&cmd, "leave"); |
| 1871 |
} else { |
| 1872 |
Tcl_DStringAppendElement(&cmd, "leavestep"); |
| 1873 |
} |
| 1874 |
} else { |
| 1875 |
Tcl_Panic("TraceExecutionProc: bad flag combination"); |
| 1876 |
} |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* Execute the command. We discard any object result the command |
| 1880 |
* returns. |
| 1881 |
*/ |
| 1882 |
|
| 1883 |
saveInterpFlags = iPtr->flags; |
| 1884 |
iPtr->flags |= INTERP_TRACE_IN_PROGRESS; |
| 1885 |
tcmdPtr->flags |= TCL_TRACE_EXEC_IN_PROGRESS; |
| 1886 |
tcmdPtr->refCount++; |
| 1887 |
|
| 1888 |
/* |
| 1889 |
* This line can have quite arbitrary side-effects, including |
| 1890 |
* deleting the trace, the command being traced, or even the |
| 1891 |
* interpreter. |
| 1892 |
*/ |
| 1893 |
|
| 1894 |
traceCode = Tcl_EvalEx(interp, Tcl_DStringValue(&cmd), |
| 1895 |
Tcl_DStringLength(&cmd), 0); |
| 1896 |
tcmdPtr->flags &= ~TCL_TRACE_EXEC_IN_PROGRESS; |
| 1897 |
|
| 1898 |
/* |
| 1899 |
* Restore the interp tracing flag to prevent cmd traces from |
| 1900 |
* affecting interp traces. |
| 1901 |
*/ |
| 1902 |
|
| 1903 |
iPtr->flags = saveInterpFlags; |
| 1904 |
if (tcmdPtr->flags == 0) { |
| 1905 |
flags |= TCL_TRACE_DESTROYED; |
| 1906 |
} |
| 1907 |
Tcl_DStringFree(&cmd); |
| 1908 |
} |
| 1909 |
|
| 1910 |
/* |
| 1911 |
* Third, if there are any step execution traces for this proc, we |
| 1912 |
* register an interpreter trace to invoke enterstep and/or leavestep |
| 1913 |
* traces. We also need to save the current stack level and the proc |
| 1914 |
* string in startLevel and startCmd so that we can delete this |
| 1915 |
* interpreter trace when it reaches the end of this proc. |
| 1916 |
*/ |
| 1917 |
|
| 1918 |
if ((flags & TCL_TRACE_ENTER_EXEC) && (tcmdPtr->stepTrace == NULL) |
| 1919 |
&& (tcmdPtr->flags & (TCL_TRACE_ENTER_DURING_EXEC | |
| 1920 |
TCL_TRACE_LEAVE_DURING_EXEC))) { |
| 1921 |
unsigned len = strlen(command) + 1; |
| 1922 |
|
| 1923 |
tcmdPtr->startLevel = level; |
| 1924 |
tcmdPtr->startCmd = (char *)ckalloc(len); |
| 1925 |
memcpy(tcmdPtr->startCmd, command, len); |
| 1926 |
tcmdPtr->refCount++; |
| 1927 |
tcmdPtr->stepTrace = Tcl_CreateObjTrace(interp, 0, |
| 1928 |
(tcmdPtr->flags & TCL_TRACE_ANY_EXEC) >> 2, |
| 1929 |
TraceExecutionProc, tcmdPtr, CommandObjTraceDeleted); |
| 1930 |
} |
| 1931 |
} |
| 1932 |
if (flags & TCL_TRACE_DESTROYED) { |
| 1933 |
if (tcmdPtr->stepTrace != NULL) { |
| 1934 |
Tcl_DeleteTrace(interp, tcmdPtr->stepTrace); |
| 1935 |
tcmdPtr->stepTrace = NULL; |
| 1936 |
ckfree(tcmdPtr->startCmd); |
| 1937 |
} |
| 1938 |
} |
| 1939 |
if (call) { |
| 1940 |
if (tcmdPtr->refCount-- <= 1) { |
| 1941 |
ckfree(tcmdPtr); |
| 1942 |
} |
| 1943 |
} |
| 1944 |
return traceCode; |
| 1945 |
} |
| 1946 |
|
| 1947 |
/* |
| 1948 |
*---------------------------------------------------------------------- |
| 1949 |
* |
| 1950 |
* TraceVarProc -- |
| 1951 |
* |
| 1952 |
* This function is called to handle variable accesses that have been |
| 1953 |
* traced using the "trace" command. |
| 1954 |
* |
| 1955 |
* Results: |
| 1956 |
* Normally returns NULL. If the trace command returns an error, then |
| 1957 |
* this function returns an error string. |
| 1958 |
* |
| 1959 |
* Side effects: |
| 1960 |
* Depends on the command associated with the trace. |
| 1961 |
* |
| 1962 |
*---------------------------------------------------------------------- |
| 1963 |
*/ |
| 1964 |
|
| 1965 |
static char * |
| 1966 |
TraceVarProc( |
| 1967 |
ClientData clientData, /* Information about the variable trace. */ |
| 1968 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 1969 |
const char *name1, /* Name of variable or array. */ |
| 1970 |
const char *name2, /* Name of element within array; NULL means |
| 1971 |
* scalar variable is being referenced. */ |
| 1972 |
int flags) /* OR-ed bits giving operation and other |
| 1973 |
* information. */ |
| 1974 |
{ |
| 1975 |
TraceVarInfo *tvarPtr = (TraceVarInfo *)clientData; |
| 1976 |
char *result; |
| 1977 |
int code, destroy = 0; |
| 1978 |
Tcl_DString cmd; |
| 1979 |
int rewind = ((Interp *)interp)->execEnvPtr->rewind; |
| 1980 |
|
| 1981 |
/* |
| 1982 |
* We might call Tcl_EvalEx() below, and that might evaluate |
| 1983 |
* [trace remove variable] which might try to free tvarPtr. We want to |
| 1984 |
* use tvarPtr until the end of this function, so we use Tcl_Preserve() |
| 1985 |
* and Tcl_Release() to be sure it is not freed while we still need it. |
| 1986 |
*/ |
| 1987 |
|
| 1988 |
result = NULL; |
| 1989 |
if ((tvarPtr->flags & flags) && !Tcl_InterpDeleted(interp) |
| 1990 |
&& !Tcl_LimitExceeded(interp)) { |
| 1991 |
if (tvarPtr->length) { |
| 1992 |
/* |
| 1993 |
* Generate a command to execute by appending list elements for |
| 1994 |
* the two variable names and the operation. |
| 1995 |
*/ |
| 1996 |
|
| 1997 |
Tcl_DStringInit(&cmd); |
| 1998 |
Tcl_DStringAppend(&cmd, tvarPtr->command, tvarPtr->length); |
| 1999 |
Tcl_DStringAppendElement(&cmd, name1); |
| 2000 |
Tcl_DStringAppendElement(&cmd, (name2 ? name2 : "")); |
| 2001 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 2002 |
if (tvarPtr->flags & TCL_TRACE_OLD_STYLE) { |
| 2003 |
if (flags & TCL_TRACE_ARRAY) { |
| 2004 |
TclDStringAppendLiteral(&cmd, " a"); |
| 2005 |
} else if (flags & TCL_TRACE_READS) { |
| 2006 |
TclDStringAppendLiteral(&cmd, " r"); |
| 2007 |
} else if (flags & TCL_TRACE_WRITES) { |
| 2008 |
TclDStringAppendLiteral(&cmd, " w"); |
| 2009 |
} else if (flags & TCL_TRACE_UNSETS) { |
| 2010 |
TclDStringAppendLiteral(&cmd, " u"); |
| 2011 |
} |
| 2012 |
} else { |
| 2013 |
#endif |
| 2014 |
if (flags & TCL_TRACE_ARRAY) { |
| 2015 |
TclDStringAppendLiteral(&cmd, " array"); |
| 2016 |
} else if (flags & TCL_TRACE_READS) { |
| 2017 |
TclDStringAppendLiteral(&cmd, " read"); |
| 2018 |
} else if (flags & TCL_TRACE_WRITES) { |
| 2019 |
TclDStringAppendLiteral(&cmd, " write"); |
| 2020 |
} else if (flags & TCL_TRACE_UNSETS) { |
| 2021 |
TclDStringAppendLiteral(&cmd, " unset"); |
| 2022 |
} |
| 2023 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 2024 |
} |
| 2025 |
#endif |
| 2026 |
|
| 2027 |
/* |
| 2028 |
* Execute the command. We discard any object result the command |
| 2029 |
* returns. |
| 2030 |
* |
| 2031 |
* Add the TCL_TRACE_DESTROYED flag to tvarPtr to indicate to |
| 2032 |
* other areas that this will be destroyed by us, otherwise a |
| 2033 |
* double-free might occur depending on what the eval does. |
| 2034 |
*/ |
| 2035 |
|
| 2036 |
if ((flags & TCL_TRACE_DESTROYED) |
| 2037 |
&& !(tvarPtr->flags & TCL_TRACE_DESTROYED)) { |
| 2038 |
destroy = 1; |
| 2039 |
tvarPtr->flags |= TCL_TRACE_DESTROYED; |
| 2040 |
} |
| 2041 |
|
| 2042 |
/* |
| 2043 |
* Make sure that unset traces are rune even if the execEnv is |
| 2044 |
* rewinding (coroutine deletion, [Bug 2093947] |
| 2045 |
*/ |
| 2046 |
|
| 2047 |
if (rewind && (flags & TCL_TRACE_UNSETS)) { |
| 2048 |
((Interp *)interp)->execEnvPtr->rewind = 0; |
| 2049 |
} |
| 2050 |
code = Tcl_EvalEx(interp, Tcl_DStringValue(&cmd), |
| 2051 |
Tcl_DStringLength(&cmd), 0); |
| 2052 |
if (rewind) { |
| 2053 |
((Interp *)interp)->execEnvPtr->rewind = rewind; |
| 2054 |
} |
| 2055 |
if (code != TCL_OK) { /* copy error msg to result */ |
| 2056 |
Tcl_Obj *errMsgObj = Tcl_GetObjResult(interp); |
| 2057 |
|
| 2058 |
Tcl_IncrRefCount(errMsgObj); |
| 2059 |
result = (char *) errMsgObj; |
| 2060 |
} |
| 2061 |
Tcl_DStringFree(&cmd); |
| 2062 |
} |
| 2063 |
} |
| 2064 |
if (destroy && result != NULL) { |
| 2065 |
Tcl_Obj *errMsgObj = (Tcl_Obj *) result; |
| 2066 |
|
| 2067 |
Tcl_DecrRefCount(errMsgObj); |
| 2068 |
result = NULL; |
| 2069 |
} |
| 2070 |
return result; |
| 2071 |
} |
| 2072 |
|
| 2073 |
/* |
| 2074 |
*---------------------------------------------------------------------- |
| 2075 |
* |
| 2076 |
* Tcl_CreateObjTrace -- |
| 2077 |
* |
| 2078 |
* Arrange for a function to be called to trace command execution. |
| 2079 |
* |
| 2080 |
* Results: |
| 2081 |
* The return value is a token for the trace, which may be passed to |
| 2082 |
* Tcl_DeleteTrace to eliminate the trace. |
| 2083 |
* |
| 2084 |
* Side effects: |
| 2085 |
* From now on, proc will be called just before a command function is |
| 2086 |
* called to execute a Tcl command. Calls to proc will have the following |
| 2087 |
* form: |
| 2088 |
* |
| 2089 |
* void proc(ClientData clientData, |
| 2090 |
* Tcl_Interp * interp, |
| 2091 |
* int level, |
| 2092 |
* const char * command, |
| 2093 |
* Tcl_Command commandInfo, |
| 2094 |
* int objc, |
| 2095 |
* Tcl_Obj *const objv[]); |
| 2096 |
* |
| 2097 |
* The 'clientData' and 'interp' arguments to 'proc' will be the same as |
| 2098 |
* the arguments to Tcl_CreateObjTrace. The 'level' argument gives the |
| 2099 |
* nesting depth of command interpretation within the interpreter. The |
| 2100 |
* 'command' argument is the ASCII text of the command being evaluated - |
| 2101 |
* before any substitutions are performed. The 'commandInfo' argument |
| 2102 |
* gives a handle to the command procedure that will be evaluated. The |
| 2103 |
* 'objc' and 'objv' parameters give the parameter vector that will be |
| 2104 |
* passed to the command procedure. Proc does not return a value. |
| 2105 |
* |
| 2106 |
* The 'level' argument specifies the maximum nesting level of calls to |
| 2107 |
* be traced. If the execution depth of the interpreter exceeds 'level', |
| 2108 |
* the trace callback is not executed. |
| 2109 |
* |
| 2110 |
* The 'flags' argument is either zero or the value, |
| 2111 |
* TCL_ALLOW_INLINE_COMPILATION. If the TCL_ALLOW_INLINE_COMPILATION flag |
| 2112 |
* is not present, the bytecode compiler will not generate inline code |
| 2113 |
* for Tcl's built-in commands. This behavior will have a significant |
| 2114 |
* impact on performance, but will ensure that all command evaluations |
| 2115 |
* are traced. If the TCL_ALLOW_INLINE_COMPILATION flag is present, the |
| 2116 |
* bytecode compiler will have its normal behavior of compiling in-line |
| 2117 |
* code for some of Tcl's built-in commands. In this case, the tracing |
| 2118 |
* will be imprecise - in-line code will not be traced - but run-time |
| 2119 |
* performance will be improved. The latter behavior is desired for many |
| 2120 |
* applications such as profiling of run time. |
| 2121 |
* |
| 2122 |
* When the trace is deleted, the 'delProc' function will be invoked, |
| 2123 |
* passing it the original client data. |
| 2124 |
* |
| 2125 |
*---------------------------------------------------------------------- |
| 2126 |
*/ |
| 2127 |
|
| 2128 |
Tcl_Trace |
| 2129 |
Tcl_CreateObjTrace( |
| 2130 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 2131 |
int level, /* Maximum nesting level */ |
| 2132 |
int flags, /* Flags, see above */ |
| 2133 |
Tcl_CmdObjTraceProc *proc, /* Trace callback */ |
| 2134 |
ClientData clientData, /* Client data for the callback */ |
| 2135 |
Tcl_CmdObjTraceDeleteProc *delProc) |
| 2136 |
/* Function to call when trace is deleted */ |
| 2137 |
{ |
| 2138 |
Trace *tracePtr; |
| 2139 |
Interp *iPtr = (Interp *) interp; |
| 2140 |
|
| 2141 |
/* |
| 2142 |
* Test if this trace allows inline compilation of commands. |
| 2143 |
*/ |
| 2144 |
|
| 2145 |
if (!(flags & TCL_ALLOW_INLINE_COMPILATION)) { |
| 2146 |
if (iPtr->tracesForbiddingInline == 0) { |
| 2147 |
/* |
| 2148 |
* When the first trace forbidding inline compilation is created, |
| 2149 |
* invalidate existing compiled code for this interpreter and |
| 2150 |
* arrange (by setting the DONT_COMPILE_CMDS_INLINE flag) that |
| 2151 |
* when compiling new code, no commands will be compiled inline |
| 2152 |
* (i.e., into an inline sequence of instructions). We do this |
| 2153 |
* because commands that were compiled inline will never result in |
| 2154 |
* a command trace being called. |
| 2155 |
*/ |
| 2156 |
|
| 2157 |
iPtr->compileEpoch++; |
| 2158 |
iPtr->flags |= DONT_COMPILE_CMDS_INLINE; |
| 2159 |
} |
| 2160 |
iPtr->tracesForbiddingInline++; |
| 2161 |
} |
| 2162 |
|
| 2163 |
tracePtr = (Trace *)ckalloc(sizeof(Trace)); |
| 2164 |
tracePtr->level = level; |
| 2165 |
tracePtr->proc = proc; |
| 2166 |
tracePtr->clientData = clientData; |
| 2167 |
tracePtr->delProc = delProc; |
| 2168 |
tracePtr->nextPtr = iPtr->tracePtr; |
| 2169 |
tracePtr->flags = flags; |
| 2170 |
iPtr->tracePtr = tracePtr; |
| 2171 |
|
| 2172 |
return (Tcl_Trace) tracePtr; |
| 2173 |
} |
| 2174 |
|
| 2175 |
/* |
| 2176 |
*---------------------------------------------------------------------- |
| 2177 |
* |
| 2178 |
* Tcl_CreateTrace -- |
| 2179 |
* |
| 2180 |
* Arrange for a function to be called to trace command execution. |
| 2181 |
* |
| 2182 |
* Results: |
| 2183 |
* The return value is a token for the trace, which may be passed to |
| 2184 |
* Tcl_DeleteTrace to eliminate the trace. |
| 2185 |
* |
| 2186 |
* Side effects: |
| 2187 |
* From now on, proc will be called just before a command procedure is |
| 2188 |
* called to execute a Tcl command. Calls to proc will have the following |
| 2189 |
* form: |
| 2190 |
* |
| 2191 |
* void |
| 2192 |
* proc(clientData, interp, level, command, cmdProc, cmdClientData, |
| 2193 |
* argc, argv) |
| 2194 |
* ClientData clientData; |
| 2195 |
* Tcl_Interp *interp; |
| 2196 |
* int level; |
| 2197 |
* char *command; |
| 2198 |
* int (*cmdProc)(); |
| 2199 |
* ClientData cmdClientData; |
| 2200 |
* int argc; |
| 2201 |
* char **argv; |
| 2202 |
* { |
| 2203 |
* } |
| 2204 |
* |
| 2205 |
* The clientData and interp arguments to proc will be the same as the |
| 2206 |
* corresponding arguments to this function. Level gives the nesting |
| 2207 |
* level of command interpretation for this interpreter (0 corresponds to |
| 2208 |
* top level). Command gives the ASCII text of the raw command, cmdProc |
| 2209 |
* and cmdClientData give the function that will be called to process the |
| 2210 |
* command and the ClientData value it will receive, and argc and argv |
| 2211 |
* give the arguments to the command, after any argument parsing and |
| 2212 |
* substitution. Proc does not return a value. |
| 2213 |
* |
| 2214 |
*---------------------------------------------------------------------- |
| 2215 |
*/ |
| 2216 |
|
| 2217 |
Tcl_Trace |
| 2218 |
Tcl_CreateTrace( |
| 2219 |
Tcl_Interp *interp, /* Interpreter in which to create trace. */ |
| 2220 |
int level, /* Only call proc for commands at nesting |
| 2221 |
* level<=argument level (1=>top level). */ |
| 2222 |
Tcl_CmdTraceProc *proc, /* Function to call before executing each |
| 2223 |
* command. */ |
| 2224 |
ClientData clientData) /* Arbitrary value word to pass to proc. */ |
| 2225 |
{ |
| 2226 |
StringTraceData *data = (StringTraceData *)ckalloc(sizeof(StringTraceData)); |
| 2227 |
|
| 2228 |
data->clientData = clientData; |
| 2229 |
data->proc = proc; |
| 2230 |
return Tcl_CreateObjTrace(interp, level, 0, StringTraceProc, |
| 2231 |
data, StringTraceDeleteProc); |
| 2232 |
} |
| 2233 |
|
| 2234 |
/* |
| 2235 |
*---------------------------------------------------------------------- |
| 2236 |
* |
| 2237 |
* StringTraceProc -- |
| 2238 |
* |
| 2239 |
* Invoke a string-based trace function from an object-based callback. |
| 2240 |
* |
| 2241 |
* Results: |
| 2242 |
* None. |
| 2243 |
* |
| 2244 |
* Side effects: |
| 2245 |
* Whatever the string-based trace function does. |
| 2246 |
* |
| 2247 |
*---------------------------------------------------------------------- |
| 2248 |
*/ |
| 2249 |
|
| 2250 |
static int |
| 2251 |
StringTraceProc( |
| 2252 |
ClientData clientData, |
| 2253 |
Tcl_Interp *interp, |
| 2254 |
int level, |
| 2255 |
const char *command, |
| 2256 |
Tcl_Command commandInfo, |
| 2257 |
int objc, |
| 2258 |
Tcl_Obj *const *objv) |
| 2259 |
{ |
| 2260 |
StringTraceData *data = (StringTraceData *)clientData; |
| 2261 |
Command *cmdPtr = (Command *) commandInfo; |
| 2262 |
const char **argv; /* Args to pass to string trace proc */ |
| 2263 |
int i; |
| 2264 |
|
| 2265 |
/* |
| 2266 |
* This is a bit messy because we have to emulate the old trace interface, |
| 2267 |
* which uses strings for everything. |
| 2268 |
*/ |
| 2269 |
|
| 2270 |
argv = (const char **) TclStackAlloc(interp, |
| 2271 |
(objc + 1) * sizeof(const char *)); |
| 2272 |
for (i = 0; i < objc; i++) { |
| 2273 |
argv[i] = TclGetString(objv[i]); |
| 2274 |
} |
| 2275 |
argv[objc] = 0; |
| 2276 |
|
| 2277 |
/* |
| 2278 |
* Invoke the command function. Note that we cast away const-ness on two |
| 2279 |
* parameters for compatibility with legacy code; the code MUST NOT modify |
| 2280 |
* either command or argv. |
| 2281 |
*/ |
| 2282 |
|
| 2283 |
data->proc(data->clientData, interp, level, (char *) command, |
| 2284 |
cmdPtr->proc, cmdPtr->clientData, objc, argv); |
| 2285 |
TclStackFree(interp, (void *) argv); |
| 2286 |
|
| 2287 |
return TCL_OK; |
| 2288 |
} |
| 2289 |
|
| 2290 |
/* |
| 2291 |
*---------------------------------------------------------------------- |
| 2292 |
* |
| 2293 |
* StringTraceDeleteProc -- |
| 2294 |
* |
| 2295 |
* Clean up memory when a string-based trace is deleted. |
| 2296 |
* |
| 2297 |
* Results: |
| 2298 |
* None. |
| 2299 |
* |
| 2300 |
* Side effects: |
| 2301 |
* Allocated memory is returned to the system. |
| 2302 |
* |
| 2303 |
*---------------------------------------------------------------------- |
| 2304 |
*/ |
| 2305 |
|
| 2306 |
static void |
| 2307 |
StringTraceDeleteProc( |
| 2308 |
ClientData clientData) |
| 2309 |
{ |
| 2310 |
ckfree(clientData); |
| 2311 |
} |
| 2312 |
|
| 2313 |
/* |
| 2314 |
*---------------------------------------------------------------------- |
| 2315 |
* |
| 2316 |
* Tcl_DeleteTrace -- |
| 2317 |
* |
| 2318 |
* Remove a trace. |
| 2319 |
* |
| 2320 |
* Results: |
| 2321 |
* None. |
| 2322 |
* |
| 2323 |
* Side effects: |
| 2324 |
* From now on there will be no more calls to the function given in |
| 2325 |
* trace. |
| 2326 |
* |
| 2327 |
*---------------------------------------------------------------------- |
| 2328 |
*/ |
| 2329 |
|
| 2330 |
void |
| 2331 |
Tcl_DeleteTrace( |
| 2332 |
Tcl_Interp *interp, /* Interpreter that contains trace. */ |
| 2333 |
Tcl_Trace trace) /* Token for trace (returned previously by |
| 2334 |
* Tcl_CreateTrace). */ |
| 2335 |
{ |
| 2336 |
Interp *iPtr = (Interp *) interp; |
| 2337 |
Trace *prevPtr, *tracePtr = (Trace *) trace; |
| 2338 |
Trace **tracePtr2 = &iPtr->tracePtr; |
| 2339 |
ActiveInterpTrace *activePtr; |
| 2340 |
|
| 2341 |
/* |
| 2342 |
* Locate the trace entry in the interpreter's trace list, and remove it |
| 2343 |
* from the list. |
| 2344 |
*/ |
| 2345 |
|
| 2346 |
prevPtr = NULL; |
| 2347 |
while (*tracePtr2 != NULL && *tracePtr2 != tracePtr) { |
| 2348 |
prevPtr = *tracePtr2; |
| 2349 |
tracePtr2 = &prevPtr->nextPtr; |
| 2350 |
} |
| 2351 |
if (*tracePtr2 == NULL) { |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
*tracePtr2 = (*tracePtr2)->nextPtr; |
| 2355 |
|
| 2356 |
/* |
| 2357 |
* The code below makes it possible to delete traces while traces are |
| 2358 |
* active: it makes sure that the deleted trace won't be processed by |
| 2359 |
* TclCheckInterpTraces. |
| 2360 |
*/ |
| 2361 |
|
| 2362 |
for (activePtr = iPtr->activeInterpTracePtr; activePtr != NULL; |
| 2363 |
activePtr = activePtr->nextPtr) { |
| 2364 |
if (activePtr->nextTracePtr == tracePtr) { |
| 2365 |
if (activePtr->reverseScan) { |
| 2366 |
activePtr->nextTracePtr = prevPtr; |
| 2367 |
} else { |
| 2368 |
activePtr->nextTracePtr = tracePtr->nextPtr; |
| 2369 |
} |
| 2370 |
} |
| 2371 |
} |
| 2372 |
|
| 2373 |
/* |
| 2374 |
* If the trace forbids bytecode compilation, change the interpreter's |
| 2375 |
* state. If bytecode compilation is now permitted, flag the fact and |
| 2376 |
* advance the compilation epoch so that procs will be recompiled to take |
| 2377 |
* advantage of it. |
| 2378 |
*/ |
| 2379 |
|
| 2380 |
if (!(tracePtr->flags & TCL_ALLOW_INLINE_COMPILATION)) { |
| 2381 |
iPtr->tracesForbiddingInline--; |
| 2382 |
if (iPtr->tracesForbiddingInline == 0) { |
| 2383 |
iPtr->flags &= ~DONT_COMPILE_CMDS_INLINE; |
| 2384 |
iPtr->compileEpoch++; |
| 2385 |
} |
| 2386 |
} |
| 2387 |
|
| 2388 |
/* |
| 2389 |
* Execute any delete callback. |
| 2390 |
*/ |
| 2391 |
|
| 2392 |
if (tracePtr->delProc != NULL) { |
| 2393 |
tracePtr->delProc(tracePtr->clientData); |
| 2394 |
} |
| 2395 |
|
| 2396 |
/* |
| 2397 |
* Delete the trace object. |
| 2398 |
*/ |
| 2399 |
|
| 2400 |
Tcl_EventuallyFree((char *) tracePtr, TCL_DYNAMIC); |
| 2401 |
} |
| 2402 |
|
| 2403 |
/* |
| 2404 |
*---------------------------------------------------------------------- |
| 2405 |
* |
| 2406 |
* TclTraceVarExists -- |
| 2407 |
* |
| 2408 |
* This is called from info exists. We need to trigger read and/or array |
| 2409 |
* traces because they may end up creating a variable that doesn't |
| 2410 |
* currently exist. |
| 2411 |
* |
| 2412 |
* Results: |
| 2413 |
* A pointer to the Var structure, or NULL. |
| 2414 |
* |
| 2415 |
* Side effects: |
| 2416 |
* May fill in error messages in the interp. |
| 2417 |
* |
| 2418 |
*---------------------------------------------------------------------- |
| 2419 |
*/ |
| 2420 |
|
| 2421 |
Var * |
| 2422 |
TclVarTraceExists( |
| 2423 |
Tcl_Interp *interp, /* The interpreter */ |
| 2424 |
const char *varName) /* The variable name */ |
| 2425 |
{ |
| 2426 |
Var *varPtr, *arrayPtr; |
| 2427 |
|
| 2428 |
/* |
| 2429 |
* The choice of "create" flag values is delicate here, and matches the |
| 2430 |
* semantics of GetVar. Things are still not perfect, however, because if |
| 2431 |
* you do "info exists x" you get a varPtr and therefore trigger traces. |
| 2432 |
* However, if you do "info exists x(i)", then you only get a varPtr if x |
| 2433 |
* is already known to be an array. Otherwise you get NULL, and no trace |
| 2434 |
* is triggered. This matches Tcl 7.6 semantics. |
| 2435 |
*/ |
| 2436 |
|
| 2437 |
varPtr = TclLookupVar(interp, varName, NULL, 0, "access", |
| 2438 |
/*createPart1*/ 0, /*createPart2*/ 1, &arrayPtr); |
| 2439 |
|
| 2440 |
if (varPtr == NULL) { |
| 2441 |
return NULL; |
| 2442 |
} |
| 2443 |
|
| 2444 |
if ((varPtr->flags & VAR_TRACED_READ) |
| 2445 |
|| (arrayPtr && (arrayPtr->flags & VAR_TRACED_READ))) { |
| 2446 |
TclCallVarTraces((Interp *) interp, arrayPtr, varPtr, varName, NULL, |
| 2447 |
TCL_TRACE_READS, /* leaveErrMsg */ 0); |
| 2448 |
} |
| 2449 |
|
| 2450 |
/* |
| 2451 |
* If the variable doesn't exist anymore and no-one's using it, then free |
| 2452 |
* up the relevant structures and hash table entries. |
| 2453 |
*/ |
| 2454 |
|
| 2455 |
if (TclIsVarUndefined(varPtr)) { |
| 2456 |
TclCleanupVar(varPtr, arrayPtr); |
| 2457 |
return NULL; |
| 2458 |
} |
| 2459 |
|
| 2460 |
return varPtr; |
| 2461 |
} |
| 2462 |
|
| 2463 |
/* |
| 2464 |
*---------------------------------------------------------------------- |
| 2465 |
* |
| 2466 |
* TclCheckArrayTraces -- |
| 2467 |
* |
| 2468 |
* This function is invoked to when we operate on an array variable, |
| 2469 |
* to allow any array traces to fire. |
| 2470 |
* |
| 2471 |
* Results: |
| 2472 |
* Returns TCL_OK to indicate normal operation. Returns TCL_ERROR if |
| 2473 |
* invocation of a trace function indicated an error. When TCL_ERROR is |
| 2474 |
* returned, then error information is left in interp. |
| 2475 |
* |
| 2476 |
* Side effects: |
| 2477 |
* Almost anything can happen, depending on trace; this function itself |
| 2478 |
* doesn't have any side effects. |
| 2479 |
* |
| 2480 |
*---------------------------------------------------------------------- |
| 2481 |
*/ |
| 2482 |
|
| 2483 |
int |
| 2484 |
TclCheckArrayTraces( |
| 2485 |
Tcl_Interp *interp, |
| 2486 |
Var *varPtr, |
| 2487 |
Var *arrayPtr, |
| 2488 |
Tcl_Obj *name, |
| 2489 |
int index) |
| 2490 |
{ |
| 2491 |
int code = TCL_OK; |
| 2492 |
|
| 2493 |
if (varPtr && (varPtr->flags & VAR_TRACED_ARRAY) |
| 2494 |
&& (TclIsVarArray(varPtr) || TclIsVarUndefined(varPtr))) { |
| 2495 |
Interp *iPtr = (Interp *)interp; |
| 2496 |
|
| 2497 |
code = TclObjCallVarTraces(iPtr, arrayPtr, varPtr, name, NULL, |
| 2498 |
(TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY| TCL_TRACE_ARRAY), |
| 2499 |
/* leaveErrMsg */ 1, index); |
| 2500 |
} |
| 2501 |
return code; |
| 2502 |
} |
| 2503 |
|
| 2504 |
/* |
| 2505 |
*---------------------------------------------------------------------- |
| 2506 |
* |
| 2507 |
* TclCallVarTraces -- |
| 2508 |
* |
| 2509 |
* This function is invoked to find and invoke relevant trace functions |
| 2510 |
* associated with a particular operation on a variable. This function |
| 2511 |
* invokes traces both on the variable and on its containing array (where |
| 2512 |
* relevant). |
| 2513 |
* |
| 2514 |
* Results: |
| 2515 |
* Returns TCL_OK to indicate normal operation. Returns TCL_ERROR if |
| 2516 |
* invocation of a trace function indicated an error. When TCL_ERROR is |
| 2517 |
* returned and leaveErrMsg is true, then the errorInfo field of iPtr has |
| 2518 |
* information about the error placed in it. |
| 2519 |
* |
| 2520 |
* Side effects: |
| 2521 |
* Almost anything can happen, depending on trace; this function itself |
| 2522 |
* doesn't have any side effects. |
| 2523 |
* |
| 2524 |
*---------------------------------------------------------------------- |
| 2525 |
*/ |
| 2526 |
|
| 2527 |
int |
| 2528 |
TclObjCallVarTraces( |
| 2529 |
Interp *iPtr, /* Interpreter containing variable. */ |
| 2530 |
Var *arrayPtr, /* Pointer to array variable that contains the |
| 2531 |
* variable, or NULL if the variable isn't an |
| 2532 |
* element of an array. */ |
| 2533 |
Var *varPtr, /* Variable whose traces are to be invoked. */ |
| 2534 |
Tcl_Obj *part1Ptr, |
| 2535 |
Tcl_Obj *part2Ptr, /* Variable's two-part name. */ |
| 2536 |
int flags, /* Flags passed to trace functions: indicates |
| 2537 |
* what's happening to variable, plus maybe |
| 2538 |
* TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY */ |
| 2539 |
int leaveErrMsg, /* If true, and one of the traces indicates an |
| 2540 |
* error, then leave an error message and |
| 2541 |
* stack trace information in *iPTr. */ |
| 2542 |
int index) /* Index into the local variable table of the |
| 2543 |
* variable, or -1. Only used when part1Ptr is |
| 2544 |
* NULL. */ |
| 2545 |
{ |
| 2546 |
const char *part1, *part2; |
| 2547 |
|
| 2548 |
if (!part1Ptr) { |
| 2549 |
part1Ptr = localName(iPtr->varFramePtr, index); |
| 2550 |
} |
| 2551 |
if (!part1Ptr) { |
| 2552 |
Tcl_Panic("Cannot trace a variable with no name"); |
| 2553 |
} |
| 2554 |
part1 = TclGetString(part1Ptr); |
| 2555 |
part2 = part2Ptr? TclGetString(part2Ptr) : NULL; |
| 2556 |
|
| 2557 |
return TclCallVarTraces(iPtr, arrayPtr, varPtr, part1, part2, flags, |
| 2558 |
leaveErrMsg); |
| 2559 |
} |
| 2560 |
|
| 2561 |
int |
| 2562 |
TclCallVarTraces( |
| 2563 |
Interp *iPtr, /* Interpreter containing variable. */ |
| 2564 |
Var *arrayPtr, /* Pointer to array variable that contains the |
| 2565 |
* variable, or NULL if the variable isn't an |
| 2566 |
* element of an array. */ |
| 2567 |
Var *varPtr, /* Variable whose traces are to be invoked. */ |
| 2568 |
const char *part1, |
| 2569 |
const char *part2, /* Variable's two-part name. */ |
| 2570 |
int flags, /* Flags passed to trace functions: indicates |
| 2571 |
* what's happening to variable, plus maybe |
| 2572 |
* TCL_GLOBAL_ONLY or TCL_NAMESPACE_ONLY */ |
| 2573 |
int leaveErrMsg) /* If true, and one of the traces indicates an |
| 2574 |
* error, then leave an error message and |
| 2575 |
* stack trace information in *iPTr. */ |
| 2576 |
{ |
| 2577 |
VarTrace *tracePtr; |
| 2578 |
ActiveVarTrace active; |
| 2579 |
char *result; |
| 2580 |
const char *openParen, *p; |
| 2581 |
Tcl_DString nameCopy; |
| 2582 |
int copiedName; |
| 2583 |
int code = TCL_OK; |
| 2584 |
int disposeFlags = 0; |
| 2585 |
Tcl_InterpState state = NULL; |
| 2586 |
Tcl_HashEntry *hPtr; |
| 2587 |
int traceflags = flags & VAR_ALL_TRACES; |
| 2588 |
|
| 2589 |
/* |
| 2590 |
* If there are already similar trace functions active for the variable, |
| 2591 |
* don't call them again. |
| 2592 |
*/ |
| 2593 |
|
| 2594 |
if (TclIsVarTraceActive(varPtr)) { |
| 2595 |
return code; |
| 2596 |
} |
| 2597 |
TclSetVarTraceActive(varPtr); |
| 2598 |
if (TclIsVarInHash(varPtr)) { |
| 2599 |
VarHashRefCount(varPtr)++; |
| 2600 |
} |
| 2601 |
if (arrayPtr && TclIsVarInHash(arrayPtr)) { |
| 2602 |
VarHashRefCount(arrayPtr)++; |
| 2603 |
} |
| 2604 |
|
| 2605 |
/* |
| 2606 |
* If the variable name hasn't been parsed into array name and element, do |
| 2607 |
* it here. If there really is an array element, make a copy of the |
| 2608 |
* original name so that NULLs can be inserted into it to separate the |
| 2609 |
* names (can't modify the name string in place, because the string might |
| 2610 |
* get used by the callbacks we invoke). |
| 2611 |
*/ |
| 2612 |
|
| 2613 |
copiedName = 0; |
| 2614 |
if (part2 == NULL) { |
| 2615 |
for (p = part1; *p ; p++) { |
| 2616 |
if (*p == '(') { |
| 2617 |
openParen = p; |
| 2618 |
do { |
| 2619 |
p++; |
| 2620 |
} while (*p != '\0'); |
| 2621 |
p--; |
| 2622 |
if (*p == ')') { |
| 2623 |
int offset = (openParen - part1); |
| 2624 |
char *newPart1; |
| 2625 |
|
| 2626 |
Tcl_DStringInit(&nameCopy); |
| 2627 |
Tcl_DStringAppend(&nameCopy, part1, p-part1); |
| 2628 |
newPart1 = Tcl_DStringValue(&nameCopy); |
| 2629 |
newPart1[offset] = 0; |
| 2630 |
part1 = newPart1; |
| 2631 |
part2 = newPart1 + offset + 1; |
| 2632 |
copiedName = 1; |
| 2633 |
} |
| 2634 |
break; |
| 2635 |
} |
| 2636 |
} |
| 2637 |
} |
| 2638 |
|
| 2639 |
/* |
| 2640 |
* Ignore any caller-provided TCL_INTERP_DESTROYED flag. Only we can |
| 2641 |
* set it correctly. |
| 2642 |
*/ |
| 2643 |
|
| 2644 |
flags &= ~TCL_INTERP_DESTROYED; |
| 2645 |
|
| 2646 |
/* |
| 2647 |
* Invoke traces on the array containing the variable, if relevant. |
| 2648 |
*/ |
| 2649 |
|
| 2650 |
result = NULL; |
| 2651 |
active.nextPtr = iPtr->activeVarTracePtr; |
| 2652 |
iPtr->activeVarTracePtr = &active; |
| 2653 |
Tcl_Preserve(iPtr); |
| 2654 |
if (arrayPtr && !TclIsVarTraceActive(arrayPtr) |
| 2655 |
&& (arrayPtr->flags & traceflags)) { |
| 2656 |
hPtr = Tcl_FindHashEntry(&iPtr->varTraces, (char *) arrayPtr); |
| 2657 |
active.varPtr = arrayPtr; |
| 2658 |
for (tracePtr = (VarTrace *)Tcl_GetHashValue(hPtr); |
| 2659 |
tracePtr != NULL; tracePtr = active.nextTracePtr) { |
| 2660 |
active.nextTracePtr = tracePtr->nextPtr; |
| 2661 |
if (!(tracePtr->flags & flags)) { |
| 2662 |
continue; |
| 2663 |
} |
| 2664 |
Tcl_Preserve(tracePtr); |
| 2665 |
if (state == NULL) { |
| 2666 |
state = Tcl_SaveInterpState((Tcl_Interp *) iPtr, code); |
| 2667 |
} |
| 2668 |
if (Tcl_InterpDeleted((Tcl_Interp *) iPtr)) { |
| 2669 |
flags |= TCL_INTERP_DESTROYED; |
| 2670 |
} |
| 2671 |
result = tracePtr->traceProc(tracePtr->clientData, |
| 2672 |
(Tcl_Interp *) iPtr, part1, part2, flags); |
| 2673 |
if (result != NULL) { |
| 2674 |
if (flags & TCL_TRACE_UNSETS) { |
| 2675 |
/* |
| 2676 |
* Ignore errors in unset traces. |
| 2677 |
*/ |
| 2678 |
|
| 2679 |
DisposeTraceResult(tracePtr->flags, result); |
| 2680 |
} else { |
| 2681 |
disposeFlags = tracePtr->flags; |
| 2682 |
code = TCL_ERROR; |
| 2683 |
} |
| 2684 |
} |
| 2685 |
Tcl_Release(tracePtr); |
| 2686 |
if (code == TCL_ERROR) { |
| 2687 |
goto done; |
| 2688 |
} |
| 2689 |
} |
| 2690 |
} |
| 2691 |
|
| 2692 |
/* |
| 2693 |
* Invoke traces on the variable itself. |
| 2694 |
*/ |
| 2695 |
|
| 2696 |
if (flags & TCL_TRACE_UNSETS) { |
| 2697 |
flags |= TCL_TRACE_DESTROYED; |
| 2698 |
} |
| 2699 |
active.varPtr = varPtr; |
| 2700 |
if (varPtr->flags & traceflags) { |
| 2701 |
hPtr = Tcl_FindHashEntry(&iPtr->varTraces, (char *) varPtr); |
| 2702 |
for (tracePtr = (VarTrace *)Tcl_GetHashValue(hPtr); |
| 2703 |
tracePtr != NULL; tracePtr = active.nextTracePtr) { |
| 2704 |
active.nextTracePtr = tracePtr->nextPtr; |
| 2705 |
if (!(tracePtr->flags & flags)) { |
| 2706 |
continue; |
| 2707 |
} |
| 2708 |
Tcl_Preserve(tracePtr); |
| 2709 |
if (state == NULL) { |
| 2710 |
state = Tcl_SaveInterpState((Tcl_Interp *) iPtr, code); |
| 2711 |
} |
| 2712 |
if (Tcl_InterpDeleted((Tcl_Interp *) iPtr)) { |
| 2713 |
flags |= TCL_INTERP_DESTROYED; |
| 2714 |
} |
| 2715 |
result = tracePtr->traceProc(tracePtr->clientData, |
| 2716 |
(Tcl_Interp *) iPtr, part1, part2, flags); |
| 2717 |
if (result != NULL) { |
| 2718 |
if (flags & TCL_TRACE_UNSETS) { |
| 2719 |
/* |
| 2720 |
* Ignore errors in unset traces. |
| 2721 |
*/ |
| 2722 |
|
| 2723 |
DisposeTraceResult(tracePtr->flags, result); |
| 2724 |
} else { |
| 2725 |
disposeFlags = tracePtr->flags; |
| 2726 |
code = TCL_ERROR; |
| 2727 |
} |
| 2728 |
} |
| 2729 |
Tcl_Release(tracePtr); |
| 2730 |
if (code == TCL_ERROR) { |
| 2731 |
goto done; |
| 2732 |
} |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
/* |
| 2737 |
* Restore the variable's flags, remove the record of our active traces, |
| 2738 |
* and then return. |
| 2739 |
*/ |
| 2740 |
|
| 2741 |
done: |
| 2742 |
if (code == TCL_ERROR) { |
| 2743 |
if (leaveErrMsg) { |
| 2744 |
const char *verb = ""; |
| 2745 |
const char *type = ""; |
| 2746 |
|
| 2747 |
switch (flags&(TCL_TRACE_READS|TCL_TRACE_WRITES|TCL_TRACE_ARRAY)) { |
| 2748 |
case TCL_TRACE_READS: |
| 2749 |
verb = "read"; |
| 2750 |
type = verb; |
| 2751 |
break; |
| 2752 |
case TCL_TRACE_WRITES: |
| 2753 |
verb = "set"; |
| 2754 |
type = "write"; |
| 2755 |
break; |
| 2756 |
case TCL_TRACE_ARRAY: |
| 2757 |
verb = "trace array"; |
| 2758 |
type = "array"; |
| 2759 |
break; |
| 2760 |
} |
| 2761 |
|
| 2762 |
if (disposeFlags & TCL_TRACE_RESULT_OBJECT) { |
| 2763 |
Tcl_SetObjResult((Tcl_Interp *)iPtr, (Tcl_Obj *) result); |
| 2764 |
} else { |
| 2765 |
Tcl_SetObjResult((Tcl_Interp *)iPtr, |
| 2766 |
Tcl_NewStringObj(result, -1)); |
| 2767 |
} |
| 2768 |
Tcl_AddErrorInfo((Tcl_Interp *)iPtr, ""); |
| 2769 |
|
| 2770 |
Tcl_AppendObjToErrorInfo((Tcl_Interp *)iPtr, Tcl_ObjPrintf( |
| 2771 |
"\n (%s trace on \"%s%s%s%s\")", type, part1, |
| 2772 |
(part2 ? "(" : ""), (part2 ? part2 : ""), |
| 2773 |
(part2 ? ")" : "") )); |
| 2774 |
if (disposeFlags & TCL_TRACE_RESULT_OBJECT) { |
| 2775 |
TclVarErrMsg((Tcl_Interp *) iPtr, part1, part2, verb, |
| 2776 |
TclGetString((Tcl_Obj *) result)); |
| 2777 |
} else { |
| 2778 |
TclVarErrMsg((Tcl_Interp *) iPtr, part1, part2, verb, result); |
| 2779 |
} |
| 2780 |
iPtr->flags &= ~(ERR_ALREADY_LOGGED); |
| 2781 |
Tcl_DiscardInterpState(state); |
| 2782 |
} else { |
| 2783 |
Tcl_RestoreInterpState((Tcl_Interp *) iPtr, state); |
| 2784 |
} |
| 2785 |
DisposeTraceResult(disposeFlags,result); |
| 2786 |
} else if (state) { |
| 2787 |
if (code == TCL_OK) { |
| 2788 |
code = Tcl_RestoreInterpState((Tcl_Interp *) iPtr, state); |
| 2789 |
} else { |
| 2790 |
Tcl_DiscardInterpState(state); |
| 2791 |
} |
| 2792 |
} |
| 2793 |
|
| 2794 |
if (arrayPtr && TclIsVarInHash(arrayPtr)) { |
| 2795 |
VarHashRefCount(arrayPtr)--; |
| 2796 |
} |
| 2797 |
if (copiedName) { |
| 2798 |
Tcl_DStringFree(&nameCopy); |
| 2799 |
} |
| 2800 |
TclClearVarTraceActive(varPtr); |
| 2801 |
if (TclIsVarInHash(varPtr)) { |
| 2802 |
VarHashRefCount(varPtr)--; |
| 2803 |
} |
| 2804 |
iPtr->activeVarTracePtr = active.nextPtr; |
| 2805 |
Tcl_Release(iPtr); |
| 2806 |
return code; |
| 2807 |
} |
| 2808 |
|
| 2809 |
/* |
| 2810 |
*---------------------------------------------------------------------- |
| 2811 |
* |
| 2812 |
* DisposeTraceResult-- |
| 2813 |
* |
| 2814 |
* This function is called to dispose of the result returned from a trace |
| 2815 |
* function. The disposal method appropriate to the type of result is |
| 2816 |
* determined by flags. |
| 2817 |
* |
| 2818 |
* Results: |
| 2819 |
* None. |
| 2820 |
* |
| 2821 |
* Side effects: |
| 2822 |
* The memory allocated for the trace result may be freed. |
| 2823 |
* |
| 2824 |
*---------------------------------------------------------------------- |
| 2825 |
*/ |
| 2826 |
|
| 2827 |
static void |
| 2828 |
DisposeTraceResult( |
| 2829 |
int flags, /* Indicates type of result to determine |
| 2830 |
* proper disposal method. */ |
| 2831 |
char *result) /* The result returned from a trace function |
| 2832 |
* to be disposed. */ |
| 2833 |
{ |
| 2834 |
if (flags & TCL_TRACE_RESULT_DYNAMIC) { |
| 2835 |
ckfree(result); |
| 2836 |
} else if (flags & TCL_TRACE_RESULT_OBJECT) { |
| 2837 |
Tcl_DecrRefCount((Tcl_Obj *) result); |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
/* |
| 2842 |
*---------------------------------------------------------------------- |
| 2843 |
* |
| 2844 |
* Tcl_UntraceVar -- |
| 2845 |
* |
| 2846 |
* Remove a previously-created trace for a variable. |
| 2847 |
* |
| 2848 |
* Results: |
| 2849 |
* None. |
| 2850 |
* |
| 2851 |
* Side effects: |
| 2852 |
* If there exists a trace for the variable given by varName with the |
| 2853 |
* given flags, proc, and clientData, then that trace is removed. |
| 2854 |
* |
| 2855 |
*---------------------------------------------------------------------- |
| 2856 |
*/ |
| 2857 |
|
| 2858 |
#undef Tcl_UntraceVar |
| 2859 |
void |
| 2860 |
Tcl_UntraceVar( |
| 2861 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 2862 |
const char *varName, /* Name of variable; may end with "(index)" to |
| 2863 |
* signify an array reference. */ |
| 2864 |
int flags, /* OR-ed collection of bits describing current |
| 2865 |
* trace, including any of TCL_TRACE_READS, |
| 2866 |
* TCL_TRACE_WRITES, TCL_TRACE_UNSETS, |
| 2867 |
* TCL_GLOBAL_ONLY and TCL_NAMESPACE_ONLY. */ |
| 2868 |
Tcl_VarTraceProc *proc, /* Function assocated with trace. */ |
| 2869 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 2870 |
{ |
| 2871 |
Tcl_UntraceVar2(interp, varName, NULL, flags, proc, clientData); |
| 2872 |
} |
| 2873 |
|
| 2874 |
/* |
| 2875 |
*---------------------------------------------------------------------- |
| 2876 |
* |
| 2877 |
* Tcl_UntraceVar2 -- |
| 2878 |
* |
| 2879 |
* Remove a previously-created trace for a variable. |
| 2880 |
* |
| 2881 |
* Results: |
| 2882 |
* None. |
| 2883 |
* |
| 2884 |
* Side effects: |
| 2885 |
* If there exists a trace for the variable given by part1 and part2 with |
| 2886 |
* the given flags, proc, and clientData, then that trace is removed. |
| 2887 |
* |
| 2888 |
*---------------------------------------------------------------------- |
| 2889 |
*/ |
| 2890 |
|
| 2891 |
void |
| 2892 |
Tcl_UntraceVar2( |
| 2893 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 2894 |
const char *part1, /* Name of variable or array. */ |
| 2895 |
const char *part2, /* Name of element within array; NULL means |
| 2896 |
* trace applies to scalar variable or array |
| 2897 |
* as-a-whole. */ |
| 2898 |
int flags, /* OR-ed collection of bits describing current |
| 2899 |
* trace, including any of TCL_TRACE_READS, |
| 2900 |
* TCL_TRACE_WRITES, TCL_TRACE_UNSETS, |
| 2901 |
* TCL_GLOBAL_ONLY, and TCL_NAMESPACE_ONLY. */ |
| 2902 |
Tcl_VarTraceProc *proc, /* Function associated with trace. */ |
| 2903 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 2904 |
{ |
| 2905 |
VarTrace *tracePtr; |
| 2906 |
VarTrace *prevPtr, *nextPtr; |
| 2907 |
Var *varPtr, *arrayPtr; |
| 2908 |
Interp *iPtr = (Interp *) interp; |
| 2909 |
ActiveVarTrace *activePtr; |
| 2910 |
int flagMask, allFlags = 0; |
| 2911 |
Tcl_HashEntry *hPtr; |
| 2912 |
|
| 2913 |
/* |
| 2914 |
* Set up a mask to mask out the parts of the flags that we are not |
| 2915 |
* interested in now. |
| 2916 |
*/ |
| 2917 |
|
| 2918 |
flagMask = TCL_GLOBAL_ONLY | TCL_NAMESPACE_ONLY; |
| 2919 |
varPtr = TclLookupVar(interp, part1, part2, flags & flagMask, /*msg*/ NULL, |
| 2920 |
/*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr); |
| 2921 |
if (varPtr == NULL || !(varPtr->flags & VAR_ALL_TRACES & flags)) { |
| 2922 |
return; |
| 2923 |
} |
| 2924 |
|
| 2925 |
/* |
| 2926 |
* Set up a mask to mask out the parts of the flags that we are not |
| 2927 |
* interested in now. |
| 2928 |
*/ |
| 2929 |
|
| 2930 |
flagMask = TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS | |
| 2931 |
TCL_TRACE_ARRAY | TCL_TRACE_RESULT_DYNAMIC | TCL_TRACE_RESULT_OBJECT; |
| 2932 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 2933 |
flagMask |= TCL_TRACE_OLD_STYLE; |
| 2934 |
#endif |
| 2935 |
flags &= flagMask; |
| 2936 |
|
| 2937 |
hPtr = Tcl_FindHashEntry(&iPtr->varTraces, (char *) varPtr); |
| 2938 |
for (tracePtr = (VarTrace *)Tcl_GetHashValue(hPtr), prevPtr = NULL; ; |
| 2939 |
prevPtr = tracePtr, tracePtr = tracePtr->nextPtr) { |
| 2940 |
if (tracePtr == NULL) { |
| 2941 |
goto updateFlags; |
| 2942 |
} |
| 2943 |
if ((tracePtr->traceProc == proc) && (tracePtr->flags == flags) |
| 2944 |
&& (tracePtr->clientData == clientData)) { |
| 2945 |
break; |
| 2946 |
} |
| 2947 |
allFlags |= tracePtr->flags; |
| 2948 |
} |
| 2949 |
|
| 2950 |
/* |
| 2951 |
* The code below makes it possible to delete traces while traces are |
| 2952 |
* active: it makes sure that the deleted trace won't be processed by |
| 2953 |
* TclCallVarTraces. |
| 2954 |
* |
| 2955 |
* Caveat (Bug 3062331): When an unset trace handler on a variable |
| 2956 |
* tries to delete a different unset trace handler on the same variable, |
| 2957 |
* the results may be surprising. When variable unset traces fire, the |
| 2958 |
* traced variable is already gone. So the TclLookupVar() call above |
| 2959 |
* will not find that variable, and not finding it will never reach here |
| 2960 |
* to perform the deletion. This means callers of Tcl_UntraceVar*() |
| 2961 |
* attempting to delete unset traces from within the handler of another |
| 2962 |
* unset trace have to account for the possibility that their call to |
| 2963 |
* Tcl_UntraceVar*() is a no-op. |
| 2964 |
*/ |
| 2965 |
|
| 2966 |
for (activePtr = iPtr->activeVarTracePtr; activePtr != NULL; |
| 2967 |
activePtr = activePtr->nextPtr) { |
| 2968 |
if (activePtr->nextTracePtr == tracePtr) { |
| 2969 |
activePtr->nextTracePtr = tracePtr->nextPtr; |
| 2970 |
} |
| 2971 |
} |
| 2972 |
nextPtr = tracePtr->nextPtr; |
| 2973 |
if (prevPtr == NULL) { |
| 2974 |
if (nextPtr) { |
| 2975 |
Tcl_SetHashValue(hPtr, nextPtr); |
| 2976 |
} else { |
| 2977 |
Tcl_DeleteHashEntry(hPtr); |
| 2978 |
} |
| 2979 |
} else { |
| 2980 |
prevPtr->nextPtr = nextPtr; |
| 2981 |
} |
| 2982 |
tracePtr->nextPtr = NULL; |
| 2983 |
Tcl_EventuallyFree(tracePtr, TCL_DYNAMIC); |
| 2984 |
|
| 2985 |
for (tracePtr = nextPtr; tracePtr != NULL; |
| 2986 |
tracePtr = tracePtr->nextPtr) { |
| 2987 |
allFlags |= tracePtr->flags; |
| 2988 |
} |
| 2989 |
|
| 2990 |
updateFlags: |
| 2991 |
varPtr->flags &= ~VAR_ALL_TRACES; |
| 2992 |
if (allFlags & VAR_ALL_TRACES) { |
| 2993 |
varPtr->flags |= (allFlags & VAR_ALL_TRACES); |
| 2994 |
} else if (TclIsVarUndefined(varPtr)) { |
| 2995 |
/* |
| 2996 |
* If this is the last trace on the variable, and the variable is |
| 2997 |
* unset and unused, then free up the variable. |
| 2998 |
*/ |
| 2999 |
|
| 3000 |
TclCleanupVar(varPtr, NULL); |
| 3001 |
} |
| 3002 |
} |
| 3003 |
|
| 3004 |
/* |
| 3005 |
*---------------------------------------------------------------------- |
| 3006 |
* |
| 3007 |
* Tcl_VarTraceInfo -- |
| 3008 |
* |
| 3009 |
* Return the clientData value associated with a trace on a variable. |
| 3010 |
* This function can also be used to step through all of the traces on a |
| 3011 |
* particular variable that have the same trace function. |
| 3012 |
* |
| 3013 |
* Results: |
| 3014 |
* The return value is the clientData value associated with a trace on |
| 3015 |
* the given variable. Information will only be returned for a trace with |
| 3016 |
* proc as trace function. If the clientData argument is NULL then the |
| 3017 |
* first such trace is returned; otherwise, the next relevant one after |
| 3018 |
* the one given by clientData will be returned. If the variable doesn't |
| 3019 |
* exist, or if there are no (more) traces for it, then NULL is returned. |
| 3020 |
* |
| 3021 |
* Side effects: |
| 3022 |
* None. |
| 3023 |
* |
| 3024 |
*---------------------------------------------------------------------- |
| 3025 |
*/ |
| 3026 |
|
| 3027 |
#undef Tcl_VarTraceInfo |
| 3028 |
ClientData |
| 3029 |
Tcl_VarTraceInfo( |
| 3030 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 3031 |
const char *varName, /* Name of variable; may end with "(index)" to |
| 3032 |
* signify an array reference. */ |
| 3033 |
int flags, /* OR-ed combo or TCL_GLOBAL_ONLY, |
| 3034 |
* TCL_NAMESPACE_ONLY (can be 0). */ |
| 3035 |
Tcl_VarTraceProc *proc, /* Function associated with trace. */ |
| 3036 |
ClientData prevClientData) /* If non-NULL, gives last value returned by |
| 3037 |
* this function, so this call will return the |
| 3038 |
* next trace after that one. If NULL, this |
| 3039 |
* call will return the first trace. */ |
| 3040 |
{ |
| 3041 |
return Tcl_VarTraceInfo2(interp, varName, NULL, flags, proc, |
| 3042 |
prevClientData); |
| 3043 |
} |
| 3044 |
|
| 3045 |
/* |
| 3046 |
*---------------------------------------------------------------------- |
| 3047 |
* |
| 3048 |
* Tcl_VarTraceInfo2 -- |
| 3049 |
* |
| 3050 |
* Same as Tcl_VarTraceInfo, except takes name in two pieces instead of |
| 3051 |
* one. |
| 3052 |
* |
| 3053 |
* Results: |
| 3054 |
* Same as Tcl_VarTraceInfo. |
| 3055 |
* |
| 3056 |
* Side effects: |
| 3057 |
* None. |
| 3058 |
* |
| 3059 |
*---------------------------------------------------------------------- |
| 3060 |
*/ |
| 3061 |
|
| 3062 |
ClientData |
| 3063 |
Tcl_VarTraceInfo2( |
| 3064 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 3065 |
const char *part1, /* Name of variable or array. */ |
| 3066 |
const char *part2, /* Name of element within array; NULL means |
| 3067 |
* trace applies to scalar variable or array |
| 3068 |
* as-a-whole. */ |
| 3069 |
int flags, /* OR-ed combination of TCL_GLOBAL_ONLY, |
| 3070 |
* TCL_NAMESPACE_ONLY. */ |
| 3071 |
Tcl_VarTraceProc *proc, /* Function associated with trace. */ |
| 3072 |
ClientData prevClientData) /* If non-NULL, gives last value returned by |
| 3073 |
* this function, so this call will return the |
| 3074 |
* next trace after that one. If NULL, this |
| 3075 |
* call will return the first trace. */ |
| 3076 |
{ |
| 3077 |
Interp *iPtr = (Interp *) interp; |
| 3078 |
Var *varPtr, *arrayPtr; |
| 3079 |
Tcl_HashEntry *hPtr; |
| 3080 |
|
| 3081 |
varPtr = TclLookupVar(interp, part1, part2, |
| 3082 |
flags & (TCL_GLOBAL_ONLY|TCL_NAMESPACE_ONLY), /*msg*/ NULL, |
| 3083 |
/*createPart1*/ 0, /*createPart2*/ 0, &arrayPtr); |
| 3084 |
if (varPtr == NULL) { |
| 3085 |
return NULL; |
| 3086 |
} |
| 3087 |
|
| 3088 |
/* |
| 3089 |
* Find the relevant trace, if any, and return its clientData. |
| 3090 |
*/ |
| 3091 |
|
| 3092 |
hPtr = Tcl_FindHashEntry(&iPtr->varTraces, (char *) varPtr); |
| 3093 |
|
| 3094 |
if (hPtr) { |
| 3095 |
VarTrace *tracePtr = (VarTrace *)Tcl_GetHashValue(hPtr); |
| 3096 |
|
| 3097 |
if (prevClientData != NULL) { |
| 3098 |
for (; tracePtr != NULL; tracePtr = tracePtr->nextPtr) { |
| 3099 |
if ((tracePtr->clientData == prevClientData) |
| 3100 |
&& (tracePtr->traceProc == proc)) { |
| 3101 |
tracePtr = tracePtr->nextPtr; |
| 3102 |
break; |
| 3103 |
} |
| 3104 |
} |
| 3105 |
} |
| 3106 |
for (; tracePtr != NULL ; tracePtr = tracePtr->nextPtr) { |
| 3107 |
if (tracePtr->traceProc == proc) { |
| 3108 |
return tracePtr->clientData; |
| 3109 |
} |
| 3110 |
} |
| 3111 |
} |
| 3112 |
return NULL; |
| 3113 |
} |
| 3114 |
|
| 3115 |
/* |
| 3116 |
*---------------------------------------------------------------------- |
| 3117 |
* |
| 3118 |
* Tcl_TraceVar -- |
| 3119 |
* |
| 3120 |
* Arrange for reads and/or writes to a variable to cause a function to |
| 3121 |
* be invoked, which can monitor the operations and/or change their |
| 3122 |
* actions. |
| 3123 |
* |
| 3124 |
* Results: |
| 3125 |
* A standard Tcl return value. |
| 3126 |
* |
| 3127 |
* Side effects: |
| 3128 |
* A trace is set up on the variable given by varName, such that future |
| 3129 |
* references to the variable will be mediated by proc. See the |
| 3130 |
* manual entry for complete details on the calling sequence for proc. |
| 3131 |
* The variable's flags are updated. |
| 3132 |
* |
| 3133 |
*---------------------------------------------------------------------- |
| 3134 |
*/ |
| 3135 |
|
| 3136 |
#undef Tcl_TraceVar |
| 3137 |
int |
| 3138 |
Tcl_TraceVar( |
| 3139 |
Tcl_Interp *interp, /* Interpreter in which variable is to be |
| 3140 |
* traced. */ |
| 3141 |
const char *varName, /* Name of variable; may end with "(index)" to |
| 3142 |
* signify an array reference. */ |
| 3143 |
int flags, /* OR-ed collection of bits, including any of |
| 3144 |
* TCL_TRACE_READS, TCL_TRACE_WRITES, |
| 3145 |
* TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY, and |
| 3146 |
* TCL_NAMESPACE_ONLY. */ |
| 3147 |
Tcl_VarTraceProc *proc, /* Function to call when specified ops are |
| 3148 |
* invoked upon varName. */ |
| 3149 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 3150 |
{ |
| 3151 |
return Tcl_TraceVar2(interp, varName, NULL, flags, proc, clientData); |
| 3152 |
} |
| 3153 |
|
| 3154 |
/* |
| 3155 |
*---------------------------------------------------------------------- |
| 3156 |
* |
| 3157 |
* Tcl_TraceVar2 -- |
| 3158 |
* |
| 3159 |
* Arrange for reads and/or writes to a variable to cause a function to |
| 3160 |
* be invoked, which can monitor the operations and/or change their |
| 3161 |
* actions. |
| 3162 |
* |
| 3163 |
* Results: |
| 3164 |
* A standard Tcl return value. |
| 3165 |
* |
| 3166 |
* Side effects: |
| 3167 |
* A trace is set up on the variable given by part1 and part2, such that |
| 3168 |
* future references to the variable will be mediated by proc. See |
| 3169 |
* the manual entry for complete details on the calling sequence for |
| 3170 |
* proc. The variable's flags are updated. |
| 3171 |
* |
| 3172 |
*---------------------------------------------------------------------- |
| 3173 |
*/ |
| 3174 |
|
| 3175 |
int |
| 3176 |
Tcl_TraceVar2( |
| 3177 |
Tcl_Interp *interp, /* Interpreter in which variable is to be |
| 3178 |
* traced. */ |
| 3179 |
const char *part1, /* Name of scalar variable or array. */ |
| 3180 |
const char *part2, /* Name of element within array; NULL means |
| 3181 |
* trace applies to scalar variable or array |
| 3182 |
* as-a-whole. */ |
| 3183 |
int flags, /* OR-ed collection of bits, including any of |
| 3184 |
* TCL_TRACE_READS, TCL_TRACE_WRITES, |
| 3185 |
* TCL_TRACE_UNSETS, TCL_GLOBAL_ONLY, and |
| 3186 |
* TCL_NAMESPACE_ONLY. */ |
| 3187 |
Tcl_VarTraceProc *proc, /* Function to call when specified ops are |
| 3188 |
* invoked upon varName. */ |
| 3189 |
ClientData clientData) /* Arbitrary argument to pass to proc. */ |
| 3190 |
{ |
| 3191 |
VarTrace *tracePtr; |
| 3192 |
int result; |
| 3193 |
|
| 3194 |
tracePtr = (VarTrace *)ckalloc(sizeof(VarTrace)); |
| 3195 |
tracePtr->traceProc = proc; |
| 3196 |
tracePtr->clientData = clientData; |
| 3197 |
tracePtr->flags = flags; |
| 3198 |
|
| 3199 |
result = TraceVarEx(interp, part1, part2, tracePtr); |
| 3200 |
|
| 3201 |
if (result != TCL_OK) { |
| 3202 |
ckfree(tracePtr); |
| 3203 |
} |
| 3204 |
return result; |
| 3205 |
} |
| 3206 |
|
| 3207 |
/* |
| 3208 |
*---------------------------------------------------------------------- |
| 3209 |
* |
| 3210 |
* TraceVarEx -- |
| 3211 |
* |
| 3212 |
* Arrange for reads and/or writes to a variable to cause a function to |
| 3213 |
* be invoked, which can monitor the operations and/or change their |
| 3214 |
* actions. |
| 3215 |
* |
| 3216 |
* Results: |
| 3217 |
* A standard Tcl return value. |
| 3218 |
* |
| 3219 |
* Side effects: |
| 3220 |
* A trace is set up on the variable given by part1 and part2, such that |
| 3221 |
* future references to the variable will be mediated by the |
| 3222 |
* traceProc listed in tracePtr. See the manual entry for complete |
| 3223 |
* details on the calling sequence for proc. |
| 3224 |
* |
| 3225 |
*---------------------------------------------------------------------- |
| 3226 |
*/ |
| 3227 |
|
| 3228 |
static int |
| 3229 |
TraceVarEx( |
| 3230 |
Tcl_Interp *interp, /* Interpreter in which variable is to be |
| 3231 |
* traced. */ |
| 3232 |
const char *part1, /* Name of scalar variable or array. */ |
| 3233 |
const char *part2, /* Name of element within array; NULL means |
| 3234 |
* trace applies to scalar variable or array |
| 3235 |
* as-a-whole. */ |
| 3236 |
VarTrace *tracePtr)/* Structure containing flags, traceProc and |
| 3237 |
* clientData fields. Others should be left |
| 3238 |
* blank. Will be ckfree()d (eventually) if |
| 3239 |
* this function returns TCL_OK, and up to |
| 3240 |
* caller to free if this function returns |
| 3241 |
* TCL_ERROR. */ |
| 3242 |
{ |
| 3243 |
Interp *iPtr = (Interp *) interp; |
| 3244 |
Var *varPtr, *arrayPtr; |
| 3245 |
int flagMask, isNew; |
| 3246 |
Tcl_HashEntry *hPtr; |
| 3247 |
|
| 3248 |
/* |
| 3249 |
* We strip 'flags' down to just the parts which are relevant to |
| 3250 |
* TclLookupVar, to avoid conflicts between trace flags and internal |
| 3251 |
* namespace flags such as 'TCL_FIND_ONLY_NS'. This can now occur since we |
| 3252 |
* have trace flags with values 0x1000 and higher. |
| 3253 |
*/ |
| 3254 |
|
| 3255 |
flagMask = TCL_GLOBAL_ONLY | TCL_NAMESPACE_ONLY; |
| 3256 |
varPtr = TclLookupVar(interp, part1, part2, |
| 3257 |
(tracePtr->flags & flagMask) | TCL_LEAVE_ERR_MSG, |
| 3258 |
"trace", /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr); |
| 3259 |
if (varPtr == NULL) { |
| 3260 |
return TCL_ERROR; |
| 3261 |
} |
| 3262 |
|
| 3263 |
/* |
| 3264 |
* Check for a nonsense flag combination. Note that this is a Tcl_Panic() |
| 3265 |
* because there should be no code path that ever sets both flags. |
| 3266 |
*/ |
| 3267 |
|
| 3268 |
if ((tracePtr->flags & TCL_TRACE_RESULT_DYNAMIC) |
| 3269 |
&& (tracePtr->flags & TCL_TRACE_RESULT_OBJECT)) { |
| 3270 |
Tcl_Panic("bad result flag combination"); |
| 3271 |
} |
| 3272 |
|
| 3273 |
/* |
| 3274 |
* Set up trace information. |
| 3275 |
*/ |
| 3276 |
|
| 3277 |
flagMask = TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS | |
| 3278 |
TCL_TRACE_ARRAY | TCL_TRACE_RESULT_DYNAMIC | TCL_TRACE_RESULT_OBJECT; |
| 3279 |
#ifndef TCL_REMOVE_OBSOLETE_TRACES |
| 3280 |
flagMask |= TCL_TRACE_OLD_STYLE; |
| 3281 |
#endif |
| 3282 |
tracePtr->flags = tracePtr->flags & flagMask; |
| 3283 |
|
| 3284 |
hPtr = Tcl_CreateHashEntry(&iPtr->varTraces, varPtr, &isNew); |
| 3285 |
if (isNew) { |
| 3286 |
tracePtr->nextPtr = NULL; |
| 3287 |
} else { |
| 3288 |
tracePtr->nextPtr = (VarTrace *)Tcl_GetHashValue(hPtr); |
| 3289 |
} |
| 3290 |
Tcl_SetHashValue(hPtr, tracePtr); |
| 3291 |
|
| 3292 |
/* |
| 3293 |
* Mark the variable as traced so we know to call them. |
| 3294 |
*/ |
| 3295 |
|
| 3296 |
varPtr->flags |= (tracePtr->flags & VAR_ALL_TRACES); |
| 3297 |
|
| 3298 |
return TCL_OK; |
| 3299 |
} |
| 3300 |
|
| 3301 |
/* |
| 3302 |
* Local Variables: |
| 3303 |
* mode: c |
| 3304 |
* c-basic-offset: 4 |
| 3305 |
* fill-column: 78 |
| 3306 |
* End: |
| 3307 |
*/ |