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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/*
* nsfDebug.c --
*
* Debugging facilities for the Next Scripting Framework.
*
* Copyright (C) 1999-2017 Gustaf Neumann (a, b)
* Copyright (C) 1999-2007 Uwe Zdun (a, b)
* Copyright (C) 2016-2017 Stefan Sobernig (b)
*
*
* (a) University of Essen
* Specification of Software Systems
* Altendorferstrasse 97-101
* D-45143 Essen, Germany
*
* (b) Vienna University of Economics and Business
* Institute of Information Systems and New Media
* A-1020, Welthandelsplatz 1
* Vienna, Austria
*
* This work is licensed under the MIT License http://www.opensource.org/licenses/MIT
*
* Copyright:
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "nsfInt.h"
#include "nsfAccessInt.h"
#define NsfConfigStr(x) (NsfConfigEnabled(x) ? "1" : "0")
static Tcl_Config const cfg[] = {
{"version", NSF_VERSION},
{"commit", NSF_COMMIT},
{"patchLevel", NSF_PATCHLEVEL},
{"development", NsfConfigStr(DEVELOPMENT)},
{"memcount", NsfConfigStr(MEM_COUNT)},
{"memtrace", NsfConfigStr(MEM_TRACE)},
{"profile", NsfConfigStr(PROFILE)},
{"dtrace", NsfConfigStr(DTRACE)},
{"assertions", NsfConfigStr(WITH_ASSERTIONS)},
{NULL, NULL}
};
/*
*----------------------------------------------------------------------
* NsfInitPkgConfig --
*
* Registers NSF's build configuration according to TIP 59:
* https://core.tcl.tk/tips/doc/trunk/tip/59.md. This way, the build
* configuration is preserved in a non-destructible manner (from the script
* level, at least) and can be queried via a common interface:
*
* ::nsf::pkgconfig list
* ::nsf::pkgconfig get /key/
*
* Results:
* None.
*
* Side effects:
* Creates the command ::nsf::pkgconfig, configuration data is
* stored within a dict associated with a given interp.
*
*----------------------------------------------------------------------
*/
void
NsfInitPkgConfig(Tcl_Interp *interp) {
Tcl_RegisterConfig(interp, "nsf", cfg, "iso8859-1");
}
/*
*----------------------------------------------------------------------
* NsfStackDump --
*
* Write the current callstack with various debugging infos to stderr. This
* function is primarily for debugging proposes of the C implementation of
* nsf.
*
* Results:
* None.
*
* Side effects:
* Debugging output
*
*----------------------------------------------------------------------
*/
void NsfStackDump(Tcl_Interp *interp) nonnull(1);
void
NsfStackDump(Tcl_Interp *interp) {
Interp *iPtr;
CallFrame *f, *v;
Tcl_Obj *varCmdObj;
nonnull_assert(interp != NULL);
iPtr = (Interp *)interp;
f = iPtr->framePtr;
v = iPtr->varFramePtr;
varCmdObj = Tcl_NewObj();
fprintf (stderr, " TCL STACK:\n");
if (f == NULL) {
fprintf(stderr, "- ");
}
while (f != NULL) {
Tcl_Obj *cmdObj = Tcl_NewObj();
fprintf(stderr, "\tFrame=%p ", (void *)f);
if ((f != NULL)
&& (f->isProcCallFrame != 0)
&& (f->procPtr != NULL)
&& (f->procPtr->cmdPtr != NULL)
) {
fprintf(stderr,"caller %p ", (void *)Tcl_CallFrame_callerPtr(f));
fprintf(stderr,"callerV %p ", (void *)Tcl_CallFrame_callerVarPtr(f));
Tcl_GetCommandFullName(interp, (Tcl_Command)f->procPtr->cmdPtr, cmdObj);
fprintf(stderr, "%s (%p) lvl=%d\n", ObjStr(cmdObj), (void *)f->procPtr->cmdPtr, f->level);
} else {
if (f != NULL && f->varTablePtr != NULL) {
fprintf(stderr, "var_table = %p ", (void *)f->varTablePtr);
}
fprintf(stderr, "- \n");
}
DECR_REF_COUNT(cmdObj);
f = f->callerPtr;
}
fprintf (stderr, " VARFRAME:\n");
fprintf(stderr, "\tFrame=%p ", (void *)v);
if (v != NULL) {
fprintf(stderr, "caller %p var_table %p ", (void *)v->callerPtr, (void *)v->varTablePtr);
/* if (v->varTablePtr != NULL)
panic(0, "testing");*/
}
if (v != NULL && v->isProcCallFrame && v->procPtr && v->procPtr->cmdPtr) {
Tcl_GetCommandFullName(interp, (Tcl_Command) v->procPtr->cmdPtr, varCmdObj);
fprintf(stderr, " %s (%d)\n", ObjStr(varCmdObj), v->level);
} else {
fprintf(stderr, "- \n");
}
DECR_REF_COUNT(varCmdObj);
}
#ifdef NSF_PRINT_OBJV
/*
*----------------------------------------------------------------------
* NsfPrintObjv --
*
* Print the provided argument vector to stderr. This function is primarily
* for debugging proposes of the C implementation of nsf.
*
* Results:
* None.
*
* Side effects:
* Debugging output
*
*----------------------------------------------------------------------
*/
void NsfPrintObjv(char *string, int objc, Tcl_Obj *CONST objv[]) nonnull(1) nonnull(3);
void
NsfPrintObjv(char *string, int objc, Tcl_Obj *CONST objv[]) {
int j;
nonnull_assert(string != NULL);
nonnull_assert(objv != NULL);
fprintf(stderr, "%s", string);
for (j = 0; j < objc; j++) {
/*fprintf(stderr, " objv[%d]=%s, ", j, objv[j] ? ObjStr(objv[j]) : "NULL");*/
fprintf(stderr, " objv[%d]=%s %p, ", j, objv[j] ? ObjStr(objv[j]) : "NULL", (void *)objv[j]);
}
fprintf(stderr, "\n");
}
#endif
#ifdef NSF_MEM_COUNT
/*
*----------------------------------------------------------------------
* NsfMemCountGetTable --
*
* Obtain the hash table structure
*
* Results:
* None.
*
* Side effects:
* Updating Hash table
*
*----------------------------------------------------------------------
*/
static Tcl_HashTable * NsfMemCountGetTable(int **initialized) nonnull(1);
static Tcl_HashTable *
NsfMemCountGetTable(int **initialized) {
static Tcl_ThreadDataKey memCountTableKey;
static Tcl_ThreadDataKey memCountFlagKey;
Tcl_HashTable *tablePtr;
nonnull_assert(initialized != NULL);
tablePtr = (Tcl_HashTable *)Tcl_GetThreadData(&memCountTableKey, sizeof(Tcl_HashTable));
*initialized = (int *)Tcl_GetThreadData(&memCountFlagKey, sizeof(int));
return tablePtr;
}
/*
*----------------------------------------------------------------------
* NsfMemCountAlloc --
*
* Bookkeeping function for memory und refcount debugging. This function
* records the allocation of memory resources. The accompanying function is
* NsfMemCountFree().
*
* Results:
* None.
*
* Side effects:
* Updating Hash table
*
*----------------------------------------------------------------------
*/
void
NsfMemCountAlloc(const char *id, const void *p) {
int new, *tableInitialized;
NsfMemCounter *entry;
Tcl_HashTable *tablePtr = NsfMemCountGetTable(&tableInitialized);
Tcl_HashEntry *hPtr;
nonnull_assert(id != NULL);
if (!*tableInitialized) {
fprintf(stderr, "+++ alloc %s %p\n", id, p);
return;
}
hPtr = Tcl_CreateHashEntry(tablePtr, id, &new);
#ifdef NSF_MEM_TRACE
fprintf(stderr, "+++ alloc %s %p\n", id, p);
#endif
if (new != 0) {
entry = (NsfMemCounter*)ckalloc(sizeof(NsfMemCounter));
entry->count = 1;
entry->peak = 1;
Tcl_SetHashValue(hPtr, entry);
} else {
entry = (NsfMemCounter*) Tcl_GetHashValue(hPtr);
entry->count++;
if (entry->count > entry->peak) {
entry->peak = entry->count;
}
}
}
/*
*----------------------------------------------------------------------
* NsfMemCountFree --
*
* Bookkeeping function for memory und refcount debugging. This function
* records the deallocation of memory resources. The accompanying function
* is NsfMemCountAlloc().
*
* Results:
* None.
*
* Side effects:
* Updating Hash table
*
*----------------------------------------------------------------------
*/
void
NsfMemCountFree(const char *id, const void *p) {
NsfMemCounter *entry;
int *tableInitialized;
Tcl_HashTable *tablePtr = NsfMemCountGetTable(&tableInitialized);
Tcl_HashEntry *hPtr;
nonnull_assert(id != NULL);
if (!*tableInitialized) {
fprintf(stderr, "+++ free %s %p !tableInitialized !\n", id, p);
return;
}
#ifdef NSF_MEM_TRACE
fprintf(stderr, "+++ free %s %p\n", id, p);
#endif
hPtr = Tcl_FindHashEntry(tablePtr, id);
if (hPtr == NULL) {
fprintf(stderr, "******** MEM COUNT ALERT: Trying to free %p <%s>, "
"but was not allocated\n", p, id);
return;
}
entry = (NsfMemCounter *)Tcl_GetHashValue(hPtr);
entry->count--;
}
/*
*----------------------------------------------------------------------
* NsfMemCountInit --
*
* Initialize book-keeping for memory und refcount debugging. The
* bookkeeping is realized via a per-interp hash table.
*
* Results:
* None.
*
* Side effects:
* Initializes a hash table
*
*----------------------------------------------------------------------
*/
void
NsfMemCountInit(void) {
int *tableInitialized;
Tcl_HashTable *tablePtr = NsfMemCountGetTable(&tableInitialized);
if (!*tableInitialized) {
Tcl_InitHashTable(tablePtr, TCL_STRING_KEYS);
}
(*tableInitialized) ++;
}
/*
*----------------------------------------------------------------------
* NsfMemCountRelease --
*
* Terminate book-keeping for memory und refcount debugging. This function
* prints the resulting book-information to stderr, in case of paired
* allocs/frees and incr-ref-counts and dec-ref-counts, the Overall count
* should be 0.
*
* Results:
* None.
*
* Side effects:
* Deletes the book-keeping hash table, outputs to stderr
*
*----------------------------------------------------------------------
*/
void
NsfMemCountRelease(void) {
int *tableInitialized;
Tcl_HashTable *tablePtr = NsfMemCountGetTable(&tableInitialized);
#ifdef NSF_MEM_TRACE
fprintf(stderr, "+++ release count %d\n", *tableInitialized);
#endif
if (!*tableInitialized) {
fprintf(stderr, "+++ release called on uninitialized/free hash table\n");
return;
}
if (*tableInitialized == 1) {
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
int count = 0;
fprintf(stderr, "******** NSF MEM Count *********\n* count peak\n");
for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
hPtr = Tcl_NextHashEntry(&search)) {
char *id = Tcl_GetHashKey(tablePtr, hPtr);
NsfMemCounter *entry = (NsfMemCounter*) Tcl_GetHashValue(hPtr);
count += entry->count;
fprintf(stderr, "* %4d %6d %s\n", entry->count, entry->peak, id);
ckfree ((char*) entry);
}
Tcl_DeleteHashTable(tablePtr);
fprintf(stderr, "******** Count Overall = %d\n", count);
}
(*tableInitialized) --;
}
#endif
/*
* Local Variables:
* mode: c
* c-basic-offset: 2
* indent-tabs-mode: nil
* fill-column: 78
* End:
*/
|