update template readme
[jackhill/qmk/firmware.git] / quantum / variable_trace.c
CommitLineData
23839b8c 1/* Copyright 2016 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
f519b94b
FS
17#include "variable_trace.h"
18#include <stddef.h>
19#include <string.h>
20
21#ifdef NO_PRINT
22#error "You need undef NO_PRINT to use the variable trace feature"
23#endif
24
25#ifndef CONSOLE_ENABLE
26#error "The console needs to be enabled in the makefile to use the variable trace feature"
27#endif
28
29
30#define NUM_TRACED_VARIABLES 1
a377017c
FS
31#ifndef MAX_VARIABLE_TRACE_SIZE
32 #define MAX_VARIABLE_TRACE_SIZE 4
33#endif
f519b94b
FS
34
35typedef struct {
36 const char* name;
37 void* addr;
38 unsigned size;
39 const char* func;
40 int line;
a377017c 41 uint8_t last_value[MAX_VARIABLE_TRACE_SIZE];
f519b94b
FS
42
43} traced_variable_t;
44
45static traced_variable_t traced_variables[NUM_TRACED_VARIABLES];
46
47void add_traced_variable(const char* name, void* addr, unsigned size, const char* func, int line) {
48 verify_traced_variables(func, line);
a377017c 49 if (size > MAX_VARIABLE_TRACE_SIZE) {
f519b94b
FS
50#if defined(__AVR__)
51 xprintf("Traced variable \"%S\" exceeds the maximum size %d\n", name, size);
52#else
53 xprintf("Traced variable \"%s\" exceeds the maximum size %d\n", name, size);
54#endif
a377017c 55 size = MAX_VARIABLE_TRACE_SIZE;
f519b94b
FS
56 }
57 int index = -1;
58 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
59 if (index == -1 && traced_variables[i].addr == NULL){
60 index = i;
61 }
62 else if (strcmp_P(name, traced_variables[i].name)==0) {
63 index = i;
64 break;
65 }
66 }
67
68 if (index == -1) {
69 xprintf("You can only trace %d variables at the same time\n", NUM_TRACED_VARIABLES);
70 return;
71 }
72
73 traced_variable_t* t = &traced_variables[index];
74 t->name = name;
75 t->addr = addr;
76 t->size = size;
77 t->func = func;
78 t->line = line;
79 memcpy(&t->last_value[0], addr, size);
80
81}
82
83void remove_traced_variable(const char* name, const char* func, int line) {
84 verify_traced_variables(func, line);
85 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
86 if (strcmp_P(name, traced_variables[i].name)==0) {
87 traced_variables[i].name = 0;
88 traced_variables[i].addr = NULL;
89 break;
90 }
91 }
92}
93
94void verify_traced_variables(const char* func, int line) {
95 for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
96 traced_variable_t* t = &traced_variables[i];
97 if (t->addr != NULL && t->name != NULL) {
98 if (memcmp(t->last_value, t->addr, t->size)!=0){
99#if defined(__AVR__)
100 xprintf("Traced variable \"%S\" has been modified\n", t->name);
101 xprintf("Between %S:%d\n", t->func, t->line);
102 xprintf("And %S:%d\n", func, line);
103
104#else
105 xprintf("Traced variable \"%s\" has been modified\n", t->name);
106 xprintf("Between %s:%d\n", t->func, t->line);
107 xprintf("And %s:%d\n", func, line);
108#endif
109 xprintf("Previous value ");
110 for (int j=0; j<t->size;j++) {
111 print_hex8(t->last_value[j]);
112 }
113 xprintf("\nNew value ");
114 uint8_t* addr = (uint8_t*)(t->addr);
115 for (int j=0; j<t->size;j++) {
116 print_hex8(addr[j]);
117 }
118 xprintf("\n");
119 memcpy(t->last_value, addr, t->size);
120 }
121 }
122
123 t->func = func;
124 t->line = line;
125 }
126}