Skip to content

Commit 6904501

Browse files
nordic-krchandrewboie
authored andcommitted
misc: Add k_panic on assert
Replaced forever loop in assert with call to a function. In post_assert_action() function, k_panic is called. Forever loop was preventing logs to be printed and had behavior ependent on the context (low prioriy thread - system continue to ork, irq - system is blocked). Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent ff88b7f commit 6904501

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

include/misc/__assert.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,7 @@
8181

8282
#if __ASSERT_ON
8383
#include <misc/printk.h>
84-
85-
#if defined(CONFIG_ARCH_POSIX)
86-
extern void posix_exit(int exit_code);
87-
#define __ASSERT_POST posix_exit(1)
88-
#else
89-
#define __ASSERT_POST \
90-
for (;;) { \
91-
/* spin thread */ \
92-
}
93-
#endif
84+
void assert_post_action(void);
9485

9586
#define __ASSERT_LOC(test) \
9687
printk("ASSERTION FAIL [%s] @ %s:%d\n", \
@@ -102,16 +93,16 @@ extern void posix_exit(int exit_code);
10293
do { \
10394
if (!(test)) { \
10495
__ASSERT_LOC(test); \
105-
__ASSERT_POST; \
96+
assert_post_action(); \
10697
} \
10798
} while (false)
10899

109100
#define __ASSERT(test, fmt, ...) \
110101
do { \
111102
if (!(test)) { \
112103
__ASSERT_LOC(test); \
113-
printk("\t" fmt "\n", ##__VA_ARGS__); \
114-
__ASSERT_POST; \
104+
printk("\t" fmt "\n", ##__VA_ARGS__); \
105+
assert_post_action(); \
115106
} \
116107
} while (false)
117108

lib/os/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ zephyr_sources_ifdef(CONFIG_JSON_LIBRARY json.c)
1717
zephyr_sources_if_kconfig(printk.c)
1818

1919
zephyr_sources_if_kconfig(ring_buffer.c)
20+
21+
zephyr_sources_ifdef(CONFIG_ASSERT assert.c)

lib/os/assert.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <misc/__assert.h>
8+
#include <zephyr.h>
9+
10+
void assert_post_action(void)
11+
{
12+
if (IS_ENABLED(CONFIG_ARCH_POSIX)) {
13+
extern void posix_exit(int exit_code);
14+
posix_exit(1);
15+
} else {
16+
k_panic();
17+
}
18+
}

0 commit comments

Comments
 (0)