Skip to content

Commit

Permalink
arch/posix.h: implement ARCH_EXCEPT() with abort() for debug convenience
Browse files Browse the repository at this point in the history
Flush all messages and invoke `abort()` when a k_panic() or k_oops() is
hit in native_posix mode.

One of the main purposes of `native_posix` is to provide debug
convenience. When running in a debugger, `abort()` stops execution which
provides a backtrace and the ability to inspect all variables.

A practical use case is fuzzing failures in SOF, see an example in:
thesofproject/sof#8632

Signed-off-by: Marc Herbert <[email protected]>
  • Loading branch information
marc-hb committed Feb 2, 2024
1 parent 9852e8e commit de98ca8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions include/zephyr/arch/posix/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef ZEPHYR_INCLUDE_ARCH_POSIX_ARCH_H_
#define ZEPHYR_INCLUDE_ARCH_POSIX_ARCH_H_

#include <stdlib.h>

/* Add include for DTS generated information */
#include <zephyr/devicetree.h>

Expand All @@ -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)
{
#ifdef CONFIG_LOG
/* 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*/
};
Expand Down

0 comments on commit de98ca8

Please sign in to comment.