variable_trace.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "variable_trace.h"
  2. #include <stddef.h>
  3. #include <string.h>
  4. #ifdef NO_PRINT
  5. #error "You need undef NO_PRINT to use the variable trace feature"
  6. #endif
  7. #ifndef CONSOLE_ENABLE
  8. #error "The console needs to be enabled in the makefile to use the variable trace feature"
  9. #endif
  10. #define NUM_TRACED_VARIABLES 1
  11. #ifndef MAX_VARIABLE_TRACE_SIZE
  12. #define MAX_VARIABLE_TRACE_SIZE 4
  13. #endif
  14. typedef struct {
  15. const char* name;
  16. void* addr;
  17. unsigned size;
  18. const char* func;
  19. int line;
  20. uint8_t last_value[MAX_VARIABLE_TRACE_SIZE];
  21. } traced_variable_t;
  22. static traced_variable_t traced_variables[NUM_TRACED_VARIABLES];
  23. void add_traced_variable(const char* name, void* addr, unsigned size, const char* func, int line) {
  24. verify_traced_variables(func, line);
  25. if (size > MAX_VARIABLE_TRACE_SIZE) {
  26. #if defined(__AVR__)
  27. xprintf("Traced variable \"%S\" exceeds the maximum size %d\n", name, size);
  28. #else
  29. xprintf("Traced variable \"%s\" exceeds the maximum size %d\n", name, size);
  30. #endif
  31. size = MAX_VARIABLE_TRACE_SIZE;
  32. }
  33. int index = -1;
  34. for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
  35. if (index == -1 && traced_variables[i].addr == NULL){
  36. index = i;
  37. }
  38. else if (strcmp_P(name, traced_variables[i].name)==0) {
  39. index = i;
  40. break;
  41. }
  42. }
  43. if (index == -1) {
  44. xprintf("You can only trace %d variables at the same time\n", NUM_TRACED_VARIABLES);
  45. return;
  46. }
  47. traced_variable_t* t = &traced_variables[index];
  48. t->name = name;
  49. t->addr = addr;
  50. t->size = size;
  51. t->func = func;
  52. t->line = line;
  53. memcpy(&t->last_value[0], addr, size);
  54. }
  55. void remove_traced_variable(const char* name, const char* func, int line) {
  56. verify_traced_variables(func, line);
  57. for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
  58. if (strcmp_P(name, traced_variables[i].name)==0) {
  59. traced_variables[i].name = 0;
  60. traced_variables[i].addr = NULL;
  61. break;
  62. }
  63. }
  64. }
  65. void verify_traced_variables(const char* func, int line) {
  66. for (int i = 0; i < NUM_TRACED_VARIABLES; i++) {
  67. traced_variable_t* t = &traced_variables[i];
  68. if (t->addr != NULL && t->name != NULL) {
  69. if (memcmp(t->last_value, t->addr, t->size)!=0){
  70. #if defined(__AVR__)
  71. xprintf("Traced variable \"%S\" has been modified\n", t->name);
  72. xprintf("Between %S:%d\n", t->func, t->line);
  73. xprintf("And %S:%d\n", func, line);
  74. #else
  75. xprintf("Traced variable \"%s\" has been modified\n", t->name);
  76. xprintf("Between %s:%d\n", t->func, t->line);
  77. xprintf("And %s:%d\n", func, line);
  78. #endif
  79. xprintf("Previous value ");
  80. for (int j=0; j<t->size;j++) {
  81. print_hex8(t->last_value[j]);
  82. }
  83. xprintf("\nNew value ");
  84. uint8_t* addr = (uint8_t*)(t->addr);
  85. for (int j=0; j<t->size;j++) {
  86. print_hex8(addr[j]);
  87. }
  88. xprintf("\n");
  89. memcpy(t->last_value, addr, t->size);
  90. }
  91. }
  92. t->func = func;
  93. t->line = line;
  94. }
  95. }