1 // Copyright 2024 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_cpu_exception/state.h" 17 // The cpu exception cortex m crash backend must provide a definition for the 18 // PW_CPU_EXCEPTION_CORTEX_M_HANDLE_CRASH function macro through this header. 19 #include "pw_cpu_exception_cortex_m_backend/crash.h" 20 21 // Handles crashes given a CPU state and an analysis message with optional 22 // variable arguments. 23 // 24 // Args: 25 // state: The CPU state at the time of crash. 26 // reason_and_optional_args: A text or format string and arguments with the 27 // crash reason. 28 // 29 // For example, the implementation below saves the message in a safe location 30 // and reboots. 31 // 32 // #define PW_CPU_EXCEPTION_CORTEX_M_HANDLE_CRASH(state, 33 // reason_and_optional_args...) 34 // do { 35 // PW_TOKENIZE_TO_BUFFER(persistent_buffer, &size, 36 // reason_and_optional_args); 37 // reboot(); 38 // } while (0) 39 // 40 #define PW_CPU_EXCEPTION_CORTEX_M_CRASH(state, reason_and_optional_args...) \ 41 PW_CPU_EXCEPTION_CORTEX_M_HANDLE_CRASH(state, reason_and_optional_args) 42 43 namespace pw::cpu_exception::cortex_m { 44 45 // Analyses the CPU state and crashes calling PW_CPU_EXCEPTION_CORTEX_M_CRASH(), 46 // Passing along the thread name that led to the crash. 47 // This can be helpful inside an exception handler to analyze the state for 48 // later reporting. For example, 49 // 50 // PW_NO_RETURN void pw_cpu_exception_HandleException(void* cpu_state) { 51 // AnalyzeCpuStateAndCrash(state); 52 // PW_UNREACHABLE; 53 // } 54 // 55 // This example assumes that the PW_CPU_EXCEPTION_CORTEX_M_HANDLE_CRASH 56 // implementation does not return. 57 void AnalyzeCpuStateAndCrash(const pw_cpu_exception_State& state, 58 const char* optional_thread_name = nullptr); 59 } // namespace pw::cpu_exception::cortex_m 60