test_backtrace.c 3.1 KB

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