1 /*
2  * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 
12 #include "esp_macros.h"
13 #include "soc/soc_caps.h"
14 
15 #include "sdkconfig.h"
16 
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 extern bool g_panic_abort;
23 extern char *g_panic_abort_details;
24 extern void *g_exc_frames[SOC_CPU_CORES_NUM];
25 
26 // Function to print longer amounts of information such as the details
27 // and backtrace field of panic_info_t. These functions should limit themselves
28 // to printing to the console and should do other more involved processing,
29 // and must be aware that the main logic in panic.c has a watchdog timer active.
30 typedef void (*panic_info_dump_fn_t)(const void* frame);
31 
32 // Non architecture specific exceptions (generally valid for all targets).
33 // Can be used to convey to the main logic what exception is being
34 // dealt with to perform some actions, without knowing the underlying
35 // architecture/chip-specific exception.
36 typedef enum {
37     PANIC_EXCEPTION_DEBUG,
38     PANIC_EXCEPTION_IWDT,
39     PANIC_EXCEPTION_TWDT,
40     PANIC_EXCEPTION_ABORT,
41     PANIC_EXCEPTION_FAULT,                  // catch-all for all types of faults
42 } panic_exception_t;
43 
44 typedef struct {
45     int core;                               // core which triggered panic
46     panic_exception_t exception;            // non-architecture-specific exception code
47     const char* reason;                     // exception string
48     const char* description;                // short description of the exception
49     panic_info_dump_fn_t details;           // more details on the exception
50     panic_info_dump_fn_t state;             // processor state, usually the contents of the registers
51     const void* addr;                       // instruction address that triggered the exception
52     const void* frame;                      // reference to the frame
53     bool pseudo_excause;                    // flag indicating that exception cause has special meaning
54 } panic_info_t;
55 
56 #define PANIC_INFO_DUMP(info, dump_fn)      {if ((info)->dump_fn) (*(info)->dump_fn)((info->frame));}
57 
58 // Create own print functions, since printf might be broken, and can be silenced
59 // when CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
60 #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
61 void panic_print_char(char c);
62 void panic_print_str(const char *str);
63 void panic_print_dec(int d);
64 void panic_print_hex(int h);
65 #else
66 #define panic_print_char(c)     ESP_UNUSED(c)
67 #define panic_print_str(str)    ESP_UNUSED(str)
68 #define panic_print_dec(d)      ESP_UNUSED(d)
69 #define panic_print_hex(h)      ESP_UNUSED(h)
70 #endif
71 
72 void __attribute__((__noreturn__)) panic_abort(const char *details);
73 
74 void panic_arch_fill_info(void *frame, panic_info_t *info);
75 
76 void panic_soc_fill_info(void *frame, panic_info_t *info);
77 
78 void panic_print_registers(const void *frame, int core);
79 
80 void panic_print_backtrace(const void *frame, int core);
81 
82 uint32_t panic_get_address(const void* frame);
83 
84 void panic_set_address(void *frame, uint32_t addr);
85 
86 uint32_t panic_get_cause(const void* frame);
87 
88 #ifdef __cplusplus
89 }
90 #endif
91