2017-03-28 22:20:36 +00:00
|
|
|
/* Copyright 2016 Fred Sundvik
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-11-06 19:57:26 +00:00
|
|
|
#include "variable_trace.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef NO_PRINT
|
2019-08-30 18:19:03 +00:00
|
|
|
# error "You need undef NO_PRINT to use the variable trace feature"
|
2016-11-06 19:57:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONSOLE_ENABLE
|
2019-08-30 18:19:03 +00:00
|
|
|
# error "The console needs to be enabled in the makefile to use the variable trace feature"
|
2016-11-06 19:57:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define NUM_TRACED_VARIABLES 1
|
2016-11-06 20:11:24 +00:00
|
|
|
#ifndef MAX_VARIABLE_TRACE_SIZE
|
2019-08-30 18:19:03 +00:00
|
|
|
# define MAX_VARIABLE_TRACE_SIZE 4
|
2016-11-06 20:11:24 +00:00
|
|
|
#endif
|
2016-11-06 19:57:26 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char* name;
|
2019-08-30 18:19:03 +00:00
|
|
|
void* addr;
|
|
|
|
unsigned size;
|
2016-11-06 19:57:26 +00:00
|
|
|
const char* func;
|
2019-08-30 18:19:03 +00:00
|
|
|
int line;
|
|
|
|
uint8_t last_value[MAX_VARIABLE_TRACE_SIZE];
|
2016-11-06 19:57:26 +00:00
|
|
|
|
|
|
|
} traced_variable_t;
|
|
|
|
|
|
|
|
static traced_variable_t traced_variables[NUM_TRACED_VARIABLES];
|
|
|
|
|
|
|
|
void add_traced_variable(const char* name, void* addr, unsigned size, const char* func, int line) {
|
|
|
|
verify_traced_variables(func, line);
|
2016-11-06 20:11:24 +00:00
|
|
|
if (size > MAX_VARIABLE_TRACE_SIZE) {
|
2016-11-06 19:57:26 +00:00
|
|
|
#if defined(__AVR__)
|
2019-08-30 18:19:03 +00:00
|
|
|
xprintf("Traced variable \"%S\" exceeds the maximum size %d\n", name, size);
|
2016-11-06 19:57:26 +00:00
|
|
|
#else
|
2019-08-30 18:19:03 +00:00
|
|
|
xprintf("Traced variable \"%s\" exceeds the maximum size %d\n", name, size);
|
2016-11-06 19:57:26 +00:00
|
|
|
#endif
|
2019-08-30 18:19:03 +00:00
|
|
|
size = MAX_VARIABLE_TRACE_SIZE;
|
2016-11-06 19:57:26 +00:00
|
|
|
}
|
|
|
|
int index = -1;
|
|
|
|
for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
|
2019-08-30 18:19:03 +00:00
|
|
|
if (index == -1 && traced_variables[i].addr == NULL) {
|
2016-11-06 19:57:26 +00:00
|
|
|
index = i;
|
2019-08-30 18:19:03 +00:00
|
|
|
} else if (strcmp_P(name, traced_variables[i].name) == 0) {
|
2016-11-06 19:57:26 +00:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == -1) {
|
|
|
|
xprintf("You can only trace %d variables at the same time\n", NUM_TRACED_VARIABLES);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
traced_variable_t* t = &traced_variables[index];
|
2019-08-30 18:19:03 +00:00
|
|
|
t->name = name;
|
|
|
|
t->addr = addr;
|
|
|
|
t->size = size;
|
|
|
|
t->func = func;
|
|
|
|
t->line = line;
|
2016-11-06 19:57:26 +00:00
|
|
|
memcpy(&t->last_value[0], addr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_traced_variable(const char* name, const char* func, int line) {
|
|
|
|
verify_traced_variables(func, line);
|
|
|
|
for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
|
2019-08-30 18:19:03 +00:00
|
|
|
if (strcmp_P(name, traced_variables[i].name) == 0) {
|
2016-11-06 19:57:26 +00:00
|
|
|
traced_variables[i].name = 0;
|
|
|
|
traced_variables[i].addr = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void verify_traced_variables(const char* func, int line) {
|
|
|
|
for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
|
|
|
|
traced_variable_t* t = &traced_variables[i];
|
|
|
|
if (t->addr != NULL && t->name != NULL) {
|
2019-08-30 18:19:03 +00:00
|
|
|
if (memcmp(t->last_value, t->addr, t->size) != 0) {
|
2016-11-06 19:57:26 +00:00
|
|
|
#if defined(__AVR__)
|
2019-08-30 18:19:03 +00:00
|
|
|
xprintf("Traced variable \"%S\" has been modified\n", t->name);
|
|
|
|
xprintf("Between %S:%d\n", t->func, t->line);
|
|
|
|
xprintf("And %S:%d\n", func, line);
|
2016-11-06 19:57:26 +00:00
|
|
|
|
|
|
|
#else
|
2019-08-30 18:19:03 +00:00
|
|
|
xprintf("Traced variable \"%s\" has been modified\n", t->name);
|
|
|
|
xprintf("Between %s:%d\n", t->func, t->line);
|
|
|
|
xprintf("And %s:%d\n", func, line);
|
2016-11-06 19:57:26 +00:00
|
|
|
#endif
|
2019-08-30 18:19:03 +00:00
|
|
|
xprintf("Previous value ");
|
|
|
|
for (int j = 0; j < t->size; j++) {
|
|
|
|
print_hex8(t->last_value[j]);
|
|
|
|
}
|
|
|
|
xprintf("\nNew value ");
|
|
|
|
uint8_t* addr = (uint8_t*)(t->addr);
|
|
|
|
for (int j = 0; j < t->size; j++) {
|
|
|
|
print_hex8(addr[j]);
|
|
|
|
}
|
|
|
|
xprintf("\n");
|
|
|
|
memcpy(t->last_value, addr, t->size);
|
|
|
|
}
|
2016-11-06 19:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t->func = func;
|
|
|
|
t->line = line;
|
|
|
|
}
|
|
|
|
}
|