1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 
9 #include "sdkconfig.h"
10 #include "esp_types.h"
11 #include "esp_attr.h"
12 #include "esp_err.h"
13 #include "esp_debug_helpers.h"
14 #include "soc/soc_memory_layout.h"
15 #include "esp_cpu_utils.h"
16 #include "esp_private/panic_internal.h"
17 
18 #include "xtensa/xtensa_context.h"
19 
20 #include "sdkconfig.h"
21 
22 #include "esp_rom_sys.h"
23 
esp_backtrace_get_next_frame(esp_backtrace_frame_t * frame)24 bool IRAM_ATTR esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame)
25 {
26     //Use frame(i-1)'s BS area located below frame(i)'s sp to get frame(i-1)'s sp and frame(i-2)'s pc
27     void *base_save = (void *)frame->sp;     //Base save area consists of 4 words under SP
28     frame->pc = frame->next_pc;
29     frame->next_pc = *((uint32_t *)(base_save - 16));     //If next_pc = 0, indicates frame(i-1) is the last frame on the stack
30     frame->sp =  *((uint32_t *)(base_save - 12));
31 
32     //Return true if both sp and pc of frame(i-1) are sane, false otherwise
33     return (esp_stack_ptr_is_sane(frame->sp) && esp_ptr_executable((void*)esp_cpu_process_stack_pc(frame->pc)));
34 }
35 
print_entry(uint32_t pc,uint32_t sp,bool panic)36 static void IRAM_ATTR print_entry(uint32_t pc, uint32_t sp, bool panic)
37 {
38     if (panic) {
39         panic_print_str(" 0x");
40         panic_print_hex(pc);
41         panic_print_str(":0x");
42         panic_print_hex(sp);
43     } else {
44         esp_rom_printf(" 0x%08X:0x%08X", pc, sp);
45     }
46 }
47 
print_str(const char * str,bool panic)48 static void IRAM_ATTR print_str(const char* str, bool panic)
49 {
50     if (panic) {
51         panic_print_str(str);
52     } else {
53         esp_rom_printf(str);
54     }
55 }
56 
esp_backtrace_print_from_frame(int depth,const esp_backtrace_frame_t * frame,bool panic)57 esp_err_t IRAM_ATTR esp_backtrace_print_from_frame(int depth, const esp_backtrace_frame_t* frame, bool panic)
58 {
59     //Check arguments
60     if (depth <= 0) {
61         return ESP_ERR_INVALID_ARG;
62     }
63 
64     //Initialize stk_frame with first frame of stack
65     esp_backtrace_frame_t stk_frame = { 0 };
66     memcpy(&stk_frame, frame, sizeof(esp_backtrace_frame_t));
67 
68     print_str("\r\n\r\nBacktrace:", panic);
69     print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
70 
71     //Check if first frame is valid
72     bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
73                        (esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
74                         /* Ignore the first corrupted PC in case of InstrFetchProhibited */
75                         (stk_frame.exc_frame && ((XtExcFrame *)stk_frame.exc_frame)->exccause == EXCCAUSE_INSTR_PROHIBITED)));
76 
77     uint32_t i = (depth <= 0) ? INT32_MAX : depth;
78     while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
79         if (!esp_backtrace_get_next_frame(&stk_frame)) {    //Get previous stack frame
80             corrupted = true;
81         }
82         print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
83     }
84 
85     //Print backtrace termination marker
86     esp_err_t ret = ESP_OK;
87     if (corrupted) {
88         print_str(" |<-CORRUPTED", panic);
89         ret =  ESP_FAIL;
90     } else if (stk_frame.next_pc != 0) {    //Backtrace continues
91         print_str(" |<-CONTINUES", panic);
92     }
93     print_str("\r\n\r\n", panic);
94     return ret;
95 }
96 
esp_backtrace_print(int depth)97 esp_err_t IRAM_ATTR esp_backtrace_print(int depth)
98 {
99     //Initialize stk_frame with first frame of stack
100     esp_backtrace_frame_t start = { 0 };
101     esp_backtrace_get_start(&(start.pc), &(start.sp), &(start.next_pc));
102     return esp_backtrace_print_from_frame(depth, &start, false);
103 }
104