diff --git a/arch/posix/Kconfig b/arch/posix/Kconfig index c3841e9795a..4ebc8f87c18 100644 --- a/arch/posix/Kconfig +++ b/arch/posix/Kconfig @@ -57,4 +57,10 @@ config ARCH_POSIX_FUZZ_TICKS endif # CONFIG_ARCH_POSIX_LIBFUZZER +config ARCH_POSIX_TRAP_ON_FATAL + bool "Raise a SIGTRAP on fatal error" + help + Raise a SIGTRAP signal on fatal error before exiting. + This automatically suspends the target if a debugger is attached. + endmenu diff --git a/arch/posix/core/CMakeLists.txt b/arch/posix/core/CMakeLists.txt index 4fbfb617949..3cf83c9fdda 100644 --- a/arch/posix/core/CMakeLists.txt +++ b/arch/posix/core/CMakeLists.txt @@ -10,6 +10,14 @@ zephyr_library_sources( thread.c ) +if(CONFIG_ARCH_POSIX_TRAP_ON_FATAL) + if(CONFIG_NATIVE_LIBRARY) + target_sources(native_simulator INTERFACE fatal_trap.c) + else() + zephyr_library_sources(fatal_trap.c) + endif() +endif() + if(CONFIG_NATIVE_APPLICATION) zephyr_include_directories( nsi_compat/ diff --git a/arch/posix/core/fatal.c b/arch/posix/core/fatal.c index 66d91676ea4..328ff7af445 100644 --- a/arch/posix/core/fatal.c +++ b/arch/posix/core/fatal.c @@ -13,10 +13,16 @@ #include #include +extern void nsi_raise_sigtrap(void); + FUNC_NORETURN void arch_system_halt(unsigned int reason) { ARG_UNUSED(reason); + if (IS_ENABLED(CONFIG_ARCH_POSIX_TRAP_ON_FATAL)) { + nsi_raise_sigtrap(); + } + posix_print_error_and_exit("Exiting due to fatal error\n"); CODE_UNREACHABLE; /* LCOV_EXCL_LINE */ } diff --git a/arch/posix/core/fatal_trap.c b/arch/posix/core/fatal_trap.c new file mode 100644 index 00000000000..08d91d7dfa0 --- /dev/null +++ b/arch/posix/core/fatal_trap.c @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +void nsi_raise_sigtrap(void) +{ + raise(SIGTRAP); +}