1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 
17 #include "sdkconfig.h"
18 #include "esp_types.h"
19 #include "esp_attr.h"
20 #include "esp_err.h"
21 #include "esp_debug_helpers.h"
22 #include "soc/soc_memory_layout.h"
23 #include "soc/cpu.h"
24 #include "esp_private/panic_internal.h"
25 
26 #include "xtensa/xtensa_context.h"
27 
28 #include "sdkconfig.h"
29 
30 #include "esp_rom_sys.h"
31 
esp_backtrace_get_next_frame(esp_backtrace_frame_t * frame)32 bool IRAM_ATTR esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame)
33 {
34     //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
35     void *base_save = (void *)frame->sp;     //Base save area consists of 4 words under SP
36     frame->pc = frame->next_pc;
37     frame->next_pc = *((uint32_t *)(base_save - 16));     //If next_pc = 0, indicates frame(i-1) is the last frame on the stack
38     frame->sp =  *((uint32_t *)(base_save - 12));
39 
40     //Return true if both sp and pc of frame(i-1) are sane, false otherwise
41     return (esp_stack_ptr_is_sane(frame->sp) && esp_ptr_executable((void*)esp_cpu_process_stack_pc(frame->pc)));
42 }
43 
print_entry(uint32_t pc,uint32_t sp,bool panic)44 static void IRAM_ATTR print_entry(uint32_t pc, uint32_t sp, bool panic)
45 {
46     if (panic) {
47         panic_print_str("0x");
48         panic_print_hex(pc);
49         panic_print_str(":0x");
50         panic_print_hex(sp);
51     } else {
52         esp_rom_printf("0x%08X:0x%08X", pc, sp);
53     }
54 }
55 
print_str(const char * str,bool panic)56 static void IRAM_ATTR print_str(const char* str, bool panic)
57 {
58     if (panic) {
59         panic_print_str(str);
60     } else {
61         esp_rom_printf(str);
62     }
63 }
64 
esp_backtrace_print_from_frame(int depth,const esp_backtrace_frame_t * frame,bool panic)65 esp_err_t IRAM_ATTR esp_backtrace_print_from_frame(int depth, const esp_backtrace_frame_t* frame, bool panic)
66 {
67     //Check arguments
68     if (depth <= 0) {
69         return ESP_ERR_INVALID_ARG;
70     }
71 
72     //Initialize stk_frame with first frame of stack
73     esp_backtrace_frame_t stk_frame = { 0 };
74     memcpy(&stk_frame, frame, sizeof(esp_backtrace_frame_t));
75 
76     print_str("\r\n\r\nBacktrace:", panic);
77     print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
78 
79     //Check if first frame is valid
80     bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
81                        (esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
82                         /* Ignore the first corrupted PC in case of InstrFetchProhibited */
83                         (stk_frame.exc_frame && ((XtExcFrame *)stk_frame.exc_frame)->exccause == EXCCAUSE_INSTR_PROHIBITED)));
84 
85     uint32_t i = (depth <= 0) ? INT32_MAX : depth;
86     while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
87         if (!esp_backtrace_get_next_frame(&stk_frame)) {    //Get previous stack frame
88             corrupted = true;
89         }
90         print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
91         print_str(" ", panic);
92     }
93 
94     //Print backtrace termination marker
95     esp_err_t ret = ESP_OK;
96     if (corrupted) {
97         print_str(" |<-CORRUPTED", panic);
98         ret =  ESP_FAIL;
99     } else if (stk_frame.next_pc != 0) {    //Backtrace continues
100         print_str(" |<-CONTINUES", panic);
101     }
102     print_str("\r\n\r\n", panic);
103     return ret;
104 }
105 
esp_backtrace_print(int depth)106 esp_err_t IRAM_ATTR esp_backtrace_print(int depth)
107 {
108     //Initialize stk_frame with first frame of stack
109     esp_backtrace_frame_t start = { 0 };
110     esp_backtrace_get_start(&(start.pc), &(start.sp), &(start.next_pc));
111     return esp_backtrace_print_from_frame(depth, &start, false);
112 }
113