Commits

Ned Batchelder  committed 431a718

Add a bunch of logging to tracer.c to help find tracing problems.

  • Participants
  • Parent commits 008bc62

Comments (0)

Files changed (1)

File coverage/tracer.c

 #include "structmember.h"
 #include "frameobject.h"
 
-#define DEBUG 1
-
-#if DEBUG
-#define IFDEBUG(x)      x
-#else
-#define IFDEBUG(x)
-#endif
+#undef WHAT_LOG     /* Define to log the WHAT params in the trace function. */
+#undef TRACE_LOG    /* Define to log our bookkeeping. */
 
 /* The Tracer type. */
 
     self->ob_type->tp_free((PyObject*)self);
 }
 
+#if TRACE_LOG
+static const char *
+indent(int n)
+{
+    static const char * spaces = 
+        "                                                                    "
+        "                                                                    "
+        "                                                                    "
+        "                                                                    "
+        ;
+    return spaces + strlen(spaces) - n*2;
+}
+
+static int logging = 0;
+/* Set these constants to be a file substring and line number to start logging. */
+static const char * start_file = "tests/views";
+static int start_line = 27;
+
+static void
+showlog(int depth, int lineno, PyObject * filename, const char * msg)
+{
+    if (logging) {
+        printf("%s%3d ", indent(depth), depth);
+        if (lineno) {
+            printf("%4d", lineno);
+        }
+        else {
+            printf("    ");
+        }
+        if (filename) {
+            printf(" %s", PyString_AS_STRING(filename));
+        }
+        if (msg) {
+            printf(" %s", msg);
+        }
+        printf("\n");
+    }
+}
+
+#define SHOWLOG(a,b,c,d)    showlog(a,b,c,d)
+#else
+#define SHOWLOG(a,b,c,d)
+#endif /* TRACE_LOG */
+
+#if WHAT_LOG
+static const char * what_sym[] = {"CALL", "EXC ", "LINE", "RET "};
+#endif
+
 static int
 Tracer_trace(Tracer *self, PyFrameObject *frame, int what, PyObject *arg)
 {
     PyObject * filename = NULL;
     PyObject * tracename = NULL;
 
-    /* printf("trace: %d @ %d\n", what, frame->f_lineno); */
-    
+    #if WHAT_LOG 
+    if (what <= sizeof(what_sym)/sizeof(const char *)) {
+        printf("trace: %s @ %s %d\n", what_sym[what], PyString_AS_STRING(frame->f_code->co_filename), frame->f_lineno);
+    }
+    #endif 
+
+    #if TRACE_LOG
+    if (strstr(PyString_AS_STRING(frame->f_code->co_filename), start_file) && frame->f_lineno == start_line) {
+        logging = 1;
+    }
+    #endif
+
     switch (what) {
     case PyTrace_CALL:      /* 0 */
         self->depth++;
         /* If tracename is a string, then we're supposed to trace. */
         if (PyString_Check(tracename)) {
             self->tracenames[self->depth] = tracename;
+            SHOWLOG(self->depth, frame->f_lineno, filename, "traced");
         }
         else {
             self->tracenames[self->depth] = NULL;
             Py_DECREF(tracename);
+            SHOWLOG(self->depth, frame->f_lineno, filename, "skipped");
         }
         break;
     
     case PyTrace_RETURN:    /* 3 */
         if (self->depth >= 0) {
+            SHOWLOG(self->depth, frame->f_lineno, frame->f_code->co_filename, "return");
             Py_XDECREF(self->tracenames[self->depth]);
             self->depth--;
         }
     
     case PyTrace_LINE:      /* 2 */
         if (self->depth >= 0) {
+            SHOWLOG(self->depth, frame->f_lineno, frame->f_code->co_filename, "line");
             if (self->tracenames[self->depth]) {
                 PyObject * t = PyTuple_New(2);
                 tracename = self->tracenames[self->depth];