test_backtrace.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Note: Currently, the backtraces must still be checked manually. Therefore,
  3. * these test cases should always pass.
  4. * Todo: Automate the checking of backtrace addresses.
  5. */
  6. #include <stdlib.h>
  7. #include "unity.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/xtensa_api.h"
  11. #include "esp_intr_alloc.h"
  12. #define SW_ISR_LEVEL_1 7
  13. #define SW_ISR_LEVEL_3 29
  14. #define RECUR_DEPTH 3
  15. #define ACTION_ABORT -1
  16. #define ACTION_INT_WDT -2
  17. // Set to (-1) for abort(), (-2) for interrupt watchdog
  18. static int backtrace_trigger_source;
  19. /*
  20. * Recursive functions to generate a longer call stack. When the max specified
  21. * recursion depth is reached, the following actions can be taken.
  22. */
  23. static void __attribute__((__noinline__)) recursive_func(int recur_depth, int action)
  24. {
  25. if (recur_depth > 1) {
  26. recursive_func(recur_depth - 1, action);
  27. } else if (action >= 0) {
  28. xt_set_intset(1 << action);
  29. } else if (action == ACTION_ABORT) {
  30. abort();
  31. // Todo: abort() causes problems in GDB Stub backtrace due to being 'non returning'.
  32. } else if (action == ACTION_INT_WDT) {
  33. portDISABLE_INTERRUPTS();
  34. while (1) {
  35. ;
  36. }
  37. }
  38. }
  39. static void level_three_isr (void *arg)
  40. {
  41. xt_set_intclear(1 << SW_ISR_LEVEL_3); //Clear interrupt
  42. recursive_func(RECUR_DEPTH, backtrace_trigger_source); //Abort at the max recursive depth
  43. }
  44. static void level_one_isr(void *arg)
  45. {
  46. xt_set_intclear(1 << SW_ISR_LEVEL_1); //Clear interrupt
  47. recursive_func(RECUR_DEPTH, SW_ISR_LEVEL_3); //Trigger nested interrupt max recursive depth
  48. }
  49. TEST_CASE("Test backtrace from abort", "[reset_reason][reset=abort,SW_CPU_RESET]")
  50. {
  51. //Allocate level one and three SW interrupts
  52. esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, 0, level_one_isr, NULL, NULL); //Level 1 SW intr
  53. esp_intr_alloc(ETS_INTERNAL_SW1_INTR_SOURCE, 0, level_three_isr, NULL, NULL); //Level 3 SW intr
  54. backtrace_trigger_source = ACTION_ABORT;
  55. recursive_func(RECUR_DEPTH, SW_ISR_LEVEL_1); //Trigger lvl 1 SW interrupt at max recursive depth
  56. }
  57. TEST_CASE("Test backtrace from interrupt watchdog timeout", "[reset_reason][reset=Interrupt wdt timeout on CPU0,SW_CPU_RESET]")
  58. {
  59. //Allocate level one and three SW interrupts
  60. esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, 0, level_one_isr, NULL, NULL); //Level 1 SW intr
  61. esp_intr_alloc(ETS_INTERNAL_SW1_INTR_SOURCE, 0, level_three_isr, NULL, NULL); //Level 3 SW intr
  62. backtrace_trigger_source = ACTION_INT_WDT;
  63. recursive_func(RECUR_DEPTH, SW_ISR_LEVEL_1); //Trigger lvl 1 SW interrupt at max recursive depth
  64. }
  65. static void write_char_crash(char c)
  66. {
  67. ets_write_char_uart(c);
  68. *(char*) 0x00000001 = 0;
  69. }
  70. TEST_CASE("Test backtrace with a ROM function", "[reset_reason][reset=StoreProhibited,SW_CPU_RESET]")
  71. {
  72. ets_install_putc1(&write_char_crash);
  73. ets_printf("foo");
  74. }