diff --git a/include/zephyr/arch/posix/arch.h b/include/zephyr/arch/posix/arch.h index 83aceb1c14ba1f4..7ed1963f144819e 100644 --- a/include/zephyr/arch/posix/arch.h +++ b/include/zephyr/arch/posix/arch.h @@ -17,6 +17,8 @@ #ifndef ZEPHYR_INCLUDE_ARCH_POSIX_ARCH_H_ #define ZEPHYR_INCLUDE_ARCH_POSIX_ARCH_H_ +#include + /* Add include for DTS generated information */ #include @@ -38,6 +40,41 @@ extern "C" { #define ARCH_STACK_PTR_ALIGN 4 #endif +/* "native_posix" should really use CONFIG_LOG_MODE_IMMEDIATE=y but better safe + * than sorry: debugging crashes is painful enough already, so try to be nice + * and flush all messages. The deferred logging code may also want to enjoy + * native_posix too. + * + * Other archs use Zephyr's elaborate "Fatal Errors" framework which takes care + * of flushing logs but native_posix is simpler so we have to do it ourselves. + */ +static inline void posix_arch_log_immediate(void) +{ +#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_MODE_MINIMAL) + /* We can't #include the higher-level "log_ctrl.h" in this low-level + * file here because this descends into .h dependency hell. So let's + * use a one-line forward declaration instead. This void->void + * prototype does not look like it's going to change much in the + * future. + * + * We can't declare "void log_panic(void);" because it is not what it + * seems but a complicated static inline + macro override in + * generated/syscalls/log_ctrl.h. In native_posix mode log_panic() + * boils down to just z_impl_log_panic() so exceptionally invoke that + * directly. + */ + void z_impl_log_panic(void); + z_impl_log_panic(); +#endif +} +/* Invoked by k_panic() and k_oops() */ +#define ARCH_EXCEPT(reason_p) do { \ + posix_arch_log_immediate(); \ + printk("ARCH_EXCEPT reason: %u\n", reason_p); \ + abort(); CODE_UNREACHABLE; \ +} while (false) + +/* Exception context */ struct __esf { uint32_t dummy; /*maybe we will want to add something someday*/ };