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 #ifndef MAX_VARIABLE_TRACE_SIZE
16 #define MAX_VARIABLE_TRACE_SIZE 4
25 uint8_t last_value
[MAX_VARIABLE_TRACE_SIZE
];
29 static traced_variable_t traced_variables
[NUM_TRACED_VARIABLES
];
31 void add_traced_variable(const char* name
, void* addr
, unsigned size
, const char* func
, int line
) {
32 verify_traced_variables(func
, line
);
33 if (size
> MAX_VARIABLE_TRACE_SIZE
) {
35 xprintf("Traced variable \"%S\" exceeds the maximum size %d\n", name
, size
);
37 xprintf("Traced variable \"%s\" exceeds the maximum size %d\n", name
, size
);
39 size
= MAX_VARIABLE_TRACE_SIZE
;
42 for (int i
= 0; i
< NUM_TRACED_VARIABLES
; i
++) {
43 if (index
== -1 && traced_variables
[i
].addr
== NULL
){
46 else if (strcmp_P(name
, traced_variables
[i
].name
)==0) {
53 xprintf("You can only trace %d variables at the same time\n", NUM_TRACED_VARIABLES
);
57 traced_variable_t
* t
= &traced_variables
[index
];
63 memcpy(&t
->last_value
[0], addr
, size
);
67 void remove_traced_variable(const char* name
, const char* func
, int line
) {
68 verify_traced_variables(func
, line
);
69 for (int i
= 0; i
< NUM_TRACED_VARIABLES
; i
++) {
70 if (strcmp_P(name
, traced_variables
[i
].name
)==0) {
71 traced_variables
[i
].name
= 0;
72 traced_variables
[i
].addr
= NULL
;
78 void verify_traced_variables(const char* func
, int line
) {
79 for (int i
= 0; i
< NUM_TRACED_VARIABLES
; i
++) {
80 traced_variable_t
* t
= &traced_variables
[i
];
81 if (t
->addr
!= NULL
&& t
->name
!= NULL
) {
82 if (memcmp(t
->last_value
, t
->addr
, t
->size
)!=0){
84 xprintf("Traced variable \"%S\" has been modified\n", t
->name
);
85 xprintf("Between %S:%d\n", t
->func
, t
->line
);
86 xprintf("And %S:%d\n", func
, line
);
89 xprintf("Traced variable \"%s\" has been modified\n", t
->name
);
90 xprintf("Between %s:%d\n", t
->func
, t
->line
);
91 xprintf("And %s:%d\n", func
, line
);
93 xprintf("Previous value ");
94 for (int j
=0; j
<t
->size
;j
++) {
95 print_hex8(t
->last_value
[j
]);
97 xprintf("\nNew value ");
98 uint8_t* addr
= (uint8_t*)(t
->addr
);
99 for (int j
=0; j
<t
->size
;j
++) {
103 memcpy(t
->last_value
, addr
, t
->size
);