| 1 |
/* |
| 2 |
* Copyright (c) 2017-2024 Andreas Kupries <[email protected]> |
| 3 |
* = = == === ===== ======== ============= ===================== |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <critcl_alloc.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdarg.h> |
| 9 |
|
| 10 |
/* |
| 11 |
* = = == === ===== ======== ============= ===================== |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifdef CRITCL_TRACER |
| 15 |
|
| 16 |
/* Tracking the stack of scopes, |
| 17 |
* single-linked list, |
| 18 |
* top to bottom. |
| 19 |
*/ |
| 20 |
|
| 21 |
typedef struct scope_stack { |
| 22 |
const char* scope; |
| 23 |
struct scope_stack* down; |
| 24 |
} scope_stack; |
| 25 |
|
| 26 |
/* |
| 27 |
* = = == === ===== ======== ============= ===================== |
| 28 |
* Tracing state (stack of scopes, associated indentation level) |
| 29 |
* |
| 30 |
* API regexp for trace output: |
| 31 |
* (header printf* closer)* |
| 32 |
* |
| 33 |
* - closed == 1 :: post (closer) |
| 34 |
* - closed == 0 :: post (header) |
| 35 |
* |
| 36 |
* [1] in (header) && !closed |
| 37 |
* => starting a new line in the middle of an incomplete line |
| 38 |
* => force closer |
| 39 |
* [2] in (printf) && closed |
| 40 |
* => continuing a line which was interrupted by another (see [1]) |
| 41 |
* => force header |
| 42 |
*/ |
| 43 |
|
| 44 |
#define MSGMAX (1024*1024) |
| 45 |
|
| 46 |
#ifdef CRITCL_TRACE_NOTHREADS |
| 47 |
|
| 48 |
static scope_stack* top = 0; |
| 49 |
static int level = 0; |
| 50 |
static int closed = 1; |
| 51 |
static char msg [MSGMAX]; |
| 52 |
|
| 53 |
#define SETUP |
| 54 |
#define TOP top |
| 55 |
#define LEVEL level |
| 56 |
#define CLOSED closed |
| 57 |
#define MSG msg |
| 58 |
#define CHAN stdout |
| 59 |
|
| 60 |
/* Thread end means nothing |
| 61 |
*/ |
| 62 |
|
| 63 |
void |
| 64 |
critcl_trace_thread_end (void) {} |
| 65 |
|
| 66 |
#else |
| 67 |
|
| 68 |
typedef struct ThreadSpecificData { |
| 69 |
scope_stack* top; |
| 70 |
int level; |
| 71 |
int closed; |
| 72 |
char msg [MSGMAX]; |
| 73 |
FILE* chan; |
| 74 |
} ThreadSpecificData; |
| 75 |
|
| 76 |
/* copied from tclInt.h */ |
| 77 |
#define TCL_TSD_INIT(keyPtr) \ |
| 78 |
(ThreadSpecificData *)Tcl_GetThreadData((keyPtr), sizeof(ThreadSpecificData)) /* OK tcl9 */ |
| 79 |
|
| 80 |
static Tcl_ThreadDataKey ctraceDataKey; |
| 81 |
|
| 82 |
#define TOP tsdPtr->top |
| 83 |
#define LEVEL tsdPtr->level |
| 84 |
#define CLOSED tsdPtr->closed |
| 85 |
#define MSG tsdPtr->msg |
| 86 |
#define CHAN chan() |
| 87 |
#define SETUP ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&ctraceDataKey) |
| 88 |
|
| 89 |
// Very lazy channel initialization - First actual write |
| 90 |
static FILE* chan (void) { |
| 91 |
SETUP; |
| 92 |
if (!tsdPtr->chan) { |
| 93 |
sprintf (MSG, "%p.trace", Tcl_GetCurrentThread()); |
| 94 |
tsdPtr->chan = fopen (MSG, "a"); |
| 95 |
if (!tsdPtr->chan) { |
| 96 |
Tcl_Panic ("out of files to open: %s", MSG); |
| 97 |
} |
| 98 |
} |
| 99 |
return tsdPtr->chan; |
| 100 |
} |
| 101 |
|
| 102 |
/* Thread end marker, use it to close the trace file for the ended thread. |
| 103 |
* This is needed to keep the number of open files under control as traced |
| 104 |
* threads are spawned and end. Without it we will run out of file slots |
| 105 |
* over time and break. |
| 106 |
* |
| 107 |
* Example: Benchmarks running a traced command with threads a few thousand |
| 108 |
* times. |
| 109 |
*/ |
| 110 |
|
| 111 |
void |
| 112 |
critcl_trace_thread_end (void) |
| 113 |
{ |
| 114 |
SETUP; |
| 115 |
if (!tsdPtr->chan) return; |
| 116 |
fclose (tsdPtr->chan); |
| 117 |
tsdPtr->chan = NULL; |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
#endif |
| 122 |
|
| 123 |
/* |
| 124 |
* = = == === ===== ======== ============= ===================== |
| 125 |
* Internals |
| 126 |
*/ |
| 127 |
|
| 128 |
static void |
| 129 |
indent (void) |
| 130 |
{ |
| 131 |
int i; |
| 132 |
SETUP; |
| 133 |
for (i = 0; i < LEVEL; i++) { fwrite(" ", 1, 1, CHAN); } |
| 134 |
fflush (CHAN); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
scope (void) |
| 139 |
{ |
| 140 |
SETUP; |
| 141 |
if (!TOP) return; |
| 142 |
fwrite (TOP->scope, 1, strlen(TOP->scope), CHAN); |
| 143 |
fflush (CHAN); |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
separator (void) |
| 148 |
{ |
| 149 |
SETUP; |
| 150 |
fwrite(" | ", 1, 3, CHAN); |
| 151 |
fflush (CHAN); |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
* = = == === ===== ======== ============= ===================== |
| 156 |
* API |
| 157 |
*/ |
| 158 |
|
| 159 |
void |
| 160 |
critcl_trace_push (const char* scope) |
| 161 |
{ |
| 162 |
SETUP; |
| 163 |
scope_stack* new = ALLOC (scope_stack); |
| 164 |
new->scope = scope; |
| 165 |
new->down = TOP; |
| 166 |
TOP = new; |
| 167 |
LEVEL += 4; |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
critcl_trace_pop (void) |
| 172 |
{ |
| 173 |
SETUP; |
| 174 |
scope_stack* next = TOP->down; |
| 175 |
LEVEL -= 4; |
| 176 |
ckfree ((char*) TOP); |
| 177 |
TOP = next; |
| 178 |
} |
| 179 |
|
| 180 |
void |
| 181 |
critcl_trace_closer (int on) |
| 182 |
{ |
| 183 |
if (!on) return; |
| 184 |
SETUP; |
| 185 |
fwrite ("\n", 1, 1, CHAN); |
| 186 |
fflush (CHAN); |
| 187 |
CLOSED = 1; |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
critcl_trace_header (int on, int ind, const char* filename, int line) |
| 192 |
{ |
| 193 |
if (!on) return; |
| 194 |
SETUP; |
| 195 |
if (!CLOSED) critcl_trace_closer (1); |
| 196 |
// location prefix |
| 197 |
#if 0 /* varying path length breaks indenting by call level :( */ |
| 198 |
if (filename) { |
| 199 |
fprintf (CHAN, "%s:%6d", filename, line); |
| 200 |
fflush (CHAN); |
| 201 |
} |
| 202 |
#endif |
| 203 |
// indentation, scope, separator |
| 204 |
if (ind) { indent (); } |
| 205 |
scope (); |
| 206 |
separator(); |
| 207 |
CLOSED = 0; |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
critcl_trace_printf (int on, const char *format, ...) |
| 212 |
{ |
| 213 |
/* |
| 214 |
* 1MB output-buffer. We may trace large data structures. This is also a |
| 215 |
* reason why the implementation can be compiled out entirely. |
| 216 |
*/ |
| 217 |
int len; |
| 218 |
va_list args; |
| 219 |
if (!on) return; |
| 220 |
SETUP; |
| 221 |
if (CLOSED) critcl_trace_header (1, 1, 0, 0); |
| 222 |
|
| 223 |
va_start (args, format); |
| 224 |
len = vsnprintf (MSG, MSGMAX, format, args); |
| 225 |
va_end (args); |
| 226 |
fwrite (MSG, 1, len, CHAN); |
| 227 |
fflush (CHAN); |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
critcl_trace_cmd_args (const char* scopename, int argc, Tcl_Obj*const* argv) |
| 232 |
{ |
| 233 |
int i; |
| 234 |
critcl_trace_push (scopename); |
| 235 |
for (i=0; i < argc; i++) { |
| 236 |
// No location information |
| 237 |
indent(); |
| 238 |
scope(); |
| 239 |
separator(); |
| 240 |
critcl_trace_printf (1, "ARG [%3d] = %p (^%d:%s) '%s'\n", |
| 241 |
i, argv[i], argv[i]->refCount, |
| 242 |
argv[i]->typePtr ? argv[i]->typePtr->name : "<unknown>", |
| 243 |
Tcl_GetString((Tcl_Obj*) argv[i])); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
int |
| 248 |
critcl_trace_cmd_result (int status, Tcl_Interp* ip) |
| 249 |
{ |
| 250 |
Tcl_Obj* robj = Tcl_GetObjResult (ip); |
| 251 |
const char* rstr = Tcl_GetString (robj); |
| 252 |
const char* rstate; |
| 253 |
const char* rtype; |
| 254 |
static const char* state_str[] = { |
| 255 |
/* 0 */ "OK", |
| 256 |
/* 1 */ "ERROR", |
| 257 |
/* 2 */ "RETURN", |
| 258 |
/* 3 */ "BREAK", |
| 259 |
/* 4 */ "CONTINUE", |
| 260 |
}; |
| 261 |
char buf [TCL_INTEGER_SPACE]; |
| 262 |
if (status <= TCL_CONTINUE) { |
| 263 |
rstate = state_str [status]; |
| 264 |
} else { |
| 265 |
sprintf (buf, "%d", status); |
| 266 |
rstate = (const char*) buf; |
| 267 |
} |
| 268 |
if (robj->typePtr) { |
| 269 |
rtype = robj->typePtr->name; |
| 270 |
} else { |
| 271 |
rtype = "<unknown>"; |
| 272 |
} |
| 273 |
|
| 274 |
// No location information |
| 275 |
indent(); |
| 276 |
scope(); |
| 277 |
separator(); |
| 278 |
critcl_trace_printf (1, "RESULT = %s %p (^%d:%s) '%s'\n", |
| 279 |
rstate, robj, robj->refCount, rtype, rstr); |
| 280 |
critcl_trace_pop (); |
| 281 |
return status; |
| 282 |
} |
| 283 |
|
| 284 |
#endif /* CRITCL_TRACER */ |
| 285 |
/* |
| 286 |
* = = == === ===== ======== ============= ===================== |
| 287 |
*/ |
| 288 |
|
| 289 |
/* |
| 290 |
* local Variables: |
| 291 |
* mode: c |
| 292 |
* c-basic-offset: 4 |
| 293 |
* fill-column: 78 |
| 294 |
* End: |
| 295 |
*/ |