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