1 #include "variable_trace.h"
6 #error "You need undef NO_PRINT to use the variable trace feature"
10 #error "The console needs to be enabled in the makefile to use the variable trace feature"
14 #define NUM_TRACED_VARIABLES 1
15 #define MAX_TRACE_SIZE 4
23 uint8_t last_value[MAX_TRACE_SIZE];
27 static traced_variable_t traced_variables[NUM_TRACED_VARIABLES];
29 void add_traced_variable(const char* name, void* addr, unsigned size, const char* func, int line) {
30 verify_traced_variables(func, line);
31 if (size > MAX_TRACE_SIZE) {
33 xprintf("Traced variable \"%S\" exceeds the maximum size %d\n", name, size);
35 xprintf("Traced variable \"%s\" exceeds the maximum size %d\n", name, size);
37 size = MAX_TRACE_SIZE;
40 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
41 if (index == -1 && traced_variables[i].addr == NULL){
44 else if (strcmp_P(name, traced_variables[i].name)==0) {
51 xprintf("You can only trace %d variables at the same time\n", NUM_TRACED_VARIABLES);
55 traced_variable_t* t = &traced_variables[index];
61 memcpy(&t->last_value[0], addr, size);
65 void remove_traced_variable(const char* name, const char* func, int line) {
66 verify_traced_variables(func, line);
67 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
68 if (strcmp_P(name, traced_variables[i].name)==0) {
69 traced_variables[i].name = 0;
70 traced_variables[i].addr = NULL;
76 void verify_traced_variables(const char* func, int line) {
77 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
78 traced_variable_t* t = &traced_variables[i];
79 if (t->addr != NULL && t->name != NULL) {
80 if (memcmp(t->last_value, t->addr, t->size)!=0){
82 xprintf("Traced variable \"%S\" has been modified\n", t->name);
83 xprintf("Between %S:%d\n", t->func, t->line);
84 xprintf("And %S:%d\n", func, line);
87 xprintf("Traced variable \"%s\" has been modified\n", t->name);
88 xprintf("Between %s:%d\n", t->func, t->line);
89 xprintf("And %s:%d\n", func, line);
91 xprintf("Previous value ");
92 for (int j=0; j<t->size;j++) {
93 print_hex8(t->last_value[j]);
95 xprintf("\nNew value ");
96 uint8_t* addr = (uint8_t*)(t->addr);
97 for (int j=0; j<t->size;j++) {
101 memcpy(t->last_value, addr, t->size);