2021-12-13 23:38:15 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-11-29 04:06:21 -05:00
|
|
|
|
2021-03-03 00:29:01 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-08 01:27:22 -04:00
|
|
|
#include "sdkconfig.h"
|
2018-11-29 04:06:21 -05:00
|
|
|
#include "esp_types.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_debug_helpers.h"
|
|
|
|
#include "soc/soc_memory_layout.h"
|
2021-12-13 23:38:15 -05:00
|
|
|
#include "esp_cpu_utils.h"
|
2021-03-03 00:29:01 -05:00
|
|
|
#include "esp_private/panic_internal.h"
|
2018-11-29 04:06:21 -05:00
|
|
|
|
2023-10-27 06:10:47 -04:00
|
|
|
#include "xtensa_context.h"
|
2021-04-05 10:00:14 -04:00
|
|
|
|
2020-02-14 05:03:22 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2019-08-08 01:27:22 -04:00
|
|
|
|
2018-11-29 04:06:21 -05:00
|
|
|
bool IRAM_ATTR esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame)
|
|
|
|
{
|
|
|
|
//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
|
|
|
|
void *base_save = (void *)frame->sp; //Base save area consists of 4 words under SP
|
|
|
|
frame->pc = frame->next_pc;
|
|
|
|
frame->next_pc = *((uint32_t *)(base_save - 16)); //If next_pc = 0, indicates frame(i-1) is the last frame on the stack
|
|
|
|
frame->sp = *((uint32_t *)(base_save - 12));
|
|
|
|
|
|
|
|
//Return true if both sp and pc of frame(i-1) are sane, false otherwise
|
|
|
|
return (esp_stack_ptr_is_sane(frame->sp) && esp_ptr_executable((void*)esp_cpu_process_stack_pc(frame->pc)));
|
|
|
|
}
|
|
|
|
|
2021-03-03 00:29:01 -05:00
|
|
|
static void IRAM_ATTR print_entry(uint32_t pc, uint32_t sp, bool panic)
|
|
|
|
{
|
|
|
|
if (panic) {
|
2022-06-11 00:33:46 -04:00
|
|
|
panic_print_str(" 0x");
|
2021-03-03 00:29:01 -05:00
|
|
|
panic_print_hex(pc);
|
|
|
|
panic_print_str(":0x");
|
|
|
|
panic_print_hex(sp);
|
|
|
|
} else {
|
2022-06-11 00:33:46 -04:00
|
|
|
esp_rom_printf(" 0x%08X:0x%08X", pc, sp);
|
2021-03-03 00:29:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void IRAM_ATTR print_str(const char* str, bool panic)
|
|
|
|
{
|
|
|
|
if (panic) {
|
|
|
|
panic_print_str(str);
|
|
|
|
} else {
|
|
|
|
esp_rom_printf(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR esp_backtrace_print_from_frame(int depth, const esp_backtrace_frame_t* frame, bool panic)
|
2018-11-29 04:06:21 -05:00
|
|
|
{
|
|
|
|
//Check arguments
|
|
|
|
if (depth <= 0) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Initialize stk_frame with first frame of stack
|
2021-03-03 00:29:01 -05:00
|
|
|
esp_backtrace_frame_t stk_frame = { 0 };
|
|
|
|
memcpy(&stk_frame, frame, sizeof(esp_backtrace_frame_t));
|
|
|
|
|
|
|
|
print_str("\r\n\r\nBacktrace:", panic);
|
|
|
|
print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
|
2018-11-29 04:06:21 -05:00
|
|
|
|
|
|
|
//Check if first frame is valid
|
2021-04-05 10:00:14 -04:00
|
|
|
bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
|
|
|
|
(esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
|
|
|
|
/* Ignore the first corrupted PC in case of InstrFetchProhibited */
|
|
|
|
(stk_frame.exc_frame && ((XtExcFrame *)stk_frame.exc_frame)->exccause == EXCCAUSE_INSTR_PROHIBITED)));
|
2018-11-29 04:06:21 -05:00
|
|
|
|
|
|
|
uint32_t i = (depth <= 0) ? INT32_MAX : depth;
|
|
|
|
while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
|
|
|
|
if (!esp_backtrace_get_next_frame(&stk_frame)) { //Get previous stack frame
|
|
|
|
corrupted = true;
|
|
|
|
}
|
2021-03-03 00:29:01 -05:00
|
|
|
print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
|
2018-11-29 04:06:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Print backtrace termination marker
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
if (corrupted) {
|
2021-03-03 00:29:01 -05:00
|
|
|
print_str(" |<-CORRUPTED", panic);
|
2018-11-29 04:06:21 -05:00
|
|
|
ret = ESP_FAIL;
|
|
|
|
} else if (stk_frame.next_pc != 0) { //Backtrace continues
|
2021-03-03 00:29:01 -05:00
|
|
|
print_str(" |<-CONTINUES", panic);
|
2018-11-29 04:06:21 -05:00
|
|
|
}
|
2021-03-03 00:29:01 -05:00
|
|
|
print_str("\r\n\r\n", panic);
|
2018-11-29 04:06:21 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2021-03-03 00:29:01 -05:00
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR esp_backtrace_print(int depth)
|
|
|
|
{
|
|
|
|
//Initialize stk_frame with first frame of stack
|
2021-04-05 10:00:14 -04:00
|
|
|
esp_backtrace_frame_t start = { 0 };
|
2021-03-03 00:29:01 -05:00
|
|
|
esp_backtrace_get_start(&(start.pc), &(start.sp), &(start.next_pc));
|
|
|
|
return esp_backtrace_print_from_frame(depth, &start, false);
|
|
|
|
}
|