1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* bltDebug.c --
*
* Copyright 1993-1998 Lucent Technologies, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and warranty
* disclaimer appear in supporting documentation, and that the names
* of Lucent Technologies any of their entities not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* Lucent Technologies disclaims all warranties with regard to this
* software, including all implied warranties of merchantability and
* fitness. In no event shall Lucent Technologies be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in
* an action of contract, negligence or other tortuous action, arising
* out of or in connection with the use or performance of this
* software.
*/
#include "bltInt.h"
#ifndef NO_BLTDEBUG
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif /* HAVE_SYS_TIME_H */
#endif /* TIME_WITH_SYS_TIME */
#include "bltChain.h"
static Blt_Chain watchChain;
static Tcl_CmdTraceProc DebugProc;
static Tcl_CmdProc DebugCmd;
typedef struct {
char *pattern;
char *name;
} WatchInfo;
static WatchInfo *
GetWatch(name)
char *name;
{
Blt_ChainLink *linkPtr;
char c;
WatchInfo *infoPtr;
c = name[0];
for (linkPtr = Blt_ChainFirstLink(&watchChain); linkPtr != NULL;
linkPtr = Blt_ChainNextLink(linkPtr)) {
infoPtr = Blt_ChainGetValue(linkPtr);
if ((infoPtr->name[0] == c) && (strcmp(name, infoPtr->name) == 0)) {
return infoPtr;
}
}
linkPtr = Blt_ChainAllocLink(sizeof(WatchInfo));
infoPtr = Blt_ChainGetValue(linkPtr);
infoPtr->name = Blt_Strdup(name);
Blt_ChainLinkAfter(&watchChain, linkPtr, (Blt_ChainLink *)NULL);
return infoPtr;
}
static void
DeleteWatch(watchName)
char *watchName;
{
Blt_ChainLink *linkPtr;
char c;
WatchInfo *infoPtr;
c = watchName[0];
for (linkPtr = Blt_ChainFirstLink(&watchChain); linkPtr != NULL;
linkPtr = Blt_ChainNextLink(linkPtr)) {
infoPtr = Blt_ChainGetValue(linkPtr);
if ((infoPtr->name[0] == c) &&
(strcmp(infoPtr->name, watchName) == 0)) {
Blt_Free(infoPtr->name);
Blt_ChainDeleteLink(&watchChain, linkPtr);
return;
}
}
}
/*ARGSUSED*/
static void
DebugProc(clientData, interp, level, command, proc, cmdClientData,
argc, argv)
ClientData clientData; /* Not used. */
Tcl_Interp *interp; /* Not used. */
int level; /* Current level */
char *command; /* Command before substitution */
Tcl_CmdProc *proc; /* Not used. */
ClientData cmdClientData; /* Not used. */
int argc;
char **argv; /* Command after parsing, but before
* evaluation */
{
static unsigned char traceStack[200];
register int i;
char *string;
Tcl_Channel errChannel;
Tcl_DString dString;
char prompt[200];
register char *p;
char *lineStart;
int count;
/* This is pretty crappy, but there's no way to trigger stack pops */
for (i = level + 1; i < 200; i++) {
traceStack[i] = 0;
}
if (Blt_ChainGetLength(&watchChain) > 0) {
WatchInfo *infoPtr;
int found;
Blt_ChainLink *linkPtr;
found = FALSE;
for (linkPtr = Blt_ChainFirstLink(&watchChain); linkPtr != NULL;
linkPtr = Blt_ChainNextLink(linkPtr)) {
infoPtr = Blt_ChainGetValue(linkPtr);
if (Tcl_StringMatch(argv[0], infoPtr->name)) {
found = TRUE;
break;
}
}
if ((found) && (level < 200)) {
traceStack[level] = 1;
traceStack[level + 1] = 1;
}
if ((level >= 200) || (!traceStack[level])) {
return;
}
}
/*
* Use stderr channel, for compatibility with systems that don't have a
* tty (like WIN32). In reality, it doesn't make a difference since
* Tk's Win32 console can't handle large streams of data anyways.
*/
errChannel = Tcl_GetStdChannel(TCL_STDERR);
if (errChannel == NULL) {
Tcl_AppendResult(interp, "can't get stderr channel", (char *)NULL);
Tcl_BackgroundError(interp);
return;
}
Tcl_DStringInit(&dString);
sprintf(prompt, "%-2d-> ", level);
p = command;
/* Skip leading spaces in command line. */
while(isspace(UCHAR(*p))) {
p++;
}
lineStart = p;
count = 0;
for (/* empty */; *p != '\0'; /* empty */) {
if (*p == '\n') {
if (count > 0) {
Tcl_DStringAppend(&dString, " ", -1);
} else {
Tcl_DStringAppend(&dString, prompt, -1);
}
Tcl_DStringAppend(&dString, lineStart, p - lineStart);
Tcl_DStringAppend(&dString, "\n", -1);
p++;
lineStart = p;
count++;
if (count > 6) {
break;
}
} else {
p++;
}
}
while (isspace(UCHAR(*lineStart))) {
lineStart++;
}
if (lineStart < p) {
if (count > 0) {
Tcl_DStringAppend(&dString, " ", -1);
} else {
Tcl_DStringAppend(&dString, prompt, -1);
}
Tcl_DStringAppend(&dString, lineStart, p - lineStart);
if (count <= 6) {
Tcl_DStringAppend(&dString, "\n", -1);
}
}
if (count > 6) {
Tcl_DStringAppend(&dString, " ...\n", -1);
}
string = Tcl_Merge(argc, argv);
lineStart = string;
sprintf(prompt, " <- ");
count = 0;
for (p = string; *p != '\0'; /* empty */) {
if (*p == '\n') {
if (count > 0) {
Tcl_DStringAppend(&dString, " ", -1);
} else {
Tcl_DStringAppend(&dString, prompt, -1);
}
count++;
Tcl_DStringAppend(&dString, lineStart, p - lineStart);
Tcl_DStringAppend(&dString, "\n", -1);
p++;
lineStart = p;
if (count > 6) {
break;
}
} else {
p++;
}
}
if (lineStart < p) {
if (count > 0) {
Tcl_DStringAppend(&dString, " ", -1);
} else {
Tcl_DStringAppend(&dString, prompt, -1);
}
Tcl_DStringAppend(&dString, lineStart, p - lineStart);
if (count <= 6) {
Tcl_DStringAppend(&dString, "\n", -1);
}
}
if (count > 6) {
Tcl_DStringAppend(&dString, " ...\n", -1);
}
Tcl_DStringAppend(&dString, "\n", -1);
Blt_Free(string);
Tcl_Write(errChannel, (char *)Tcl_DStringValue(&dString), -1);
Tcl_Flush(errChannel);
Tcl_DStringFree(&dString);
}
/*ARGSUSED*/
static int
DebugCmd(clientData, interp, argc, argv)
ClientData clientData; /* Not used. */
Tcl_Interp *interp;
int argc;
char **argv;
{
static Tcl_Trace token;
static int level;
int newLevel;
char c;
int length;
WatchInfo *infoPtr;
Blt_ChainLink *linkPtr;
register int i;
if (argc == 1) {
Tcl_SetResult(interp, Blt_Itoa(level), TCL_VOLATILE);
return TCL_OK;
}
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'w') && (strncmp(argv[1], "watch", length) == 0)) {
/* Add patterns of command names to watch to the chain */
for (i = 2; i < argc; i++) {
GetWatch(argv[i]);
}
} else if ((c == 'i') && (strncmp(argv[1], "ignore", length) == 0)) {
for (i = 2; i < argc; i++) {
DeleteWatch(argv[i]);
}
} else {
goto levelTest;
}
/* Return the current watch patterns */
for (linkPtr = Blt_ChainFirstLink(&watchChain); linkPtr != NULL;
linkPtr = Blt_ChainNextLink(linkPtr)) {
infoPtr = Blt_ChainGetValue(linkPtr);
Tcl_AppendElement(interp, infoPtr->name);
}
return TCL_OK;
levelTest:
if (Tcl_GetBoolean(interp, argv[1], &newLevel) == TCL_OK) {
if (newLevel > 0) {
newLevel = 10000; /* Max out the level */
}
} else if (Tcl_GetInt(interp, argv[1], &newLevel) == TCL_OK) {
if (newLevel < 0) {
newLevel = 0;
}
} else {
return TCL_ERROR;
}
if (token != 0) {
Tcl_DeleteTrace(interp, token);
}
if (newLevel > 0) {
token = Tcl_CreateTrace(interp, newLevel, DebugProc, (ClientData)0);
}
level = newLevel;
Tcl_SetResult(interp, Blt_Itoa(level), TCL_VOLATILE);
return TCL_OK;
}
int
Blt_DebugInit(interp)
Tcl_Interp *interp;
{
static Blt_CmdSpec cmdSpec =
{"bltdebug", DebugCmd,};
Blt_ChainInit(&watchChain);
if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
return TCL_ERROR;
}
return TCL_OK;
}
#endif /* NO_BLTDEBUG */
|