Skip to content

Commit

Permalink
better tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Nov 13, 2023
1 parent 23989d9 commit 9455a51
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion common/monitor_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class MonitorHelper : Looper, CommandParser {
#ifdef ENABLE_TRACING
if (!strcmp(cmd, "dumptrace")) {
for (size_t i = 0; i < NELEM(trace); i++) {
STDOUT << (const char *)(trace[(trace_pos + i) & (NELEM(trace) - 1)]) << "\n";
STDOUT << trace[(trace_pos + i) & (NELEM(trace) - 1)].location
<< "(" << trace[(trace_pos + i) & (NELEM(trace) - 1)].arg
<< ")\n";
}
return true;
}
Expand Down
29 changes: 26 additions & 3 deletions common/monitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ extern Monitoring monitor;
#define TRACE_CATEGORY_IR 0x4
#define TRACE_CATEGORY_PROP 0x8
#define TRACE_CATEGORY_I2C 0x10
#define TRACE_CATEGORY_RGB565 0x20

#define TRACE_EXPAND_AGAIN(CAT) (CAT)
#define TRACE_CATEGORY(CAT) TRACE_EXPAND_AGAIN(TRACE_CATEGORY_##CAT)
Expand All @@ -88,26 +89,48 @@ extern Monitoring monitor;
#define TRACING_CATEGORIES ENABLE_TRACING
#endif

struct TraceEntry {
const char* location;
int arg;
};

// Must be power of 2
#ifndef PO_TRACE_LENGTH
#define PO_TRACE_LENGTH 128
#endif

// TODO: Move this somewhere more global
volatile const char* trace[128];
volatile TraceEntry trace[PO_TRACE_LENGTH];
volatile int trace_pos;

void DoTrace(const char* str) {
void DoTrace(const char* str, int arg = 0) {
noInterrupts();
trace[trace_pos++ & 127] = str;
trace[trace_pos & (PO_TRACE_LENGTH - 1)].arg = arg;
trace[trace_pos & (PO_TRACE_LENGTH - 1)].location = str;
trace_pos++;
trace[trace_pos & (PO_TRACE_LENGTH - 1)].location = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
trace[trace_pos & (PO_TRACE_LENGTH - 1)].arg = 0;
interrupts();
}

#define TRACE(CAT, X) do { \
if (TRACE_CATEGORY(CAT) & (TRACING_CATEGORIES)) \
DoTrace(__FILE__ ":" TOSTRING(__LINE__) ": " X); \
} while(0)
#define TRACE2(CAT, X, ARG) do { \
if (TRACE_CATEGORY(CAT) & (TRACING_CATEGORIES)) \
DoTrace(__FILE__ ":" TOSTRING(__LINE__) ": " X, ARG); \
} while(0)
#else
#define TRACING_CATEGORIES 0
#define TRACE(CAT, X) do { \
if (TRACE_CATEGORY(CAT) & (TRACING_CATEGORIES)) \
do { } while(0); \
} while(0)
#define TRACE2(CAT, X, ARG) do { \
if (TRACE_CATEGORY(CAT) & (TRACING_CATEGORIES)) \
do { } while(0); \
} while(0)
#endif // ENABLE_TRACING


Expand Down

0 comments on commit 9455a51

Please sign in to comment.