From 18986250a24c36666881d5e1374409bc8b16d247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20Mi=C5=9B?= Date: Sat, 6 Jun 2020 21:06:34 +0200 Subject: [PATCH] libc: newlib: assert: Add handler for C std assert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Libraries using standard C assert from did not give useful information on assertion fault, as default newlib implementation tries to print assert info to stderr. It causes another assertion in Zephyr. With this patch, the default handler is overridden to use native Zephyr assert macros and give useful info when assertion fails. Signed-off-by: Hubert Miś --- lib/libc/newlib/CMakeLists.txt | 3 ++- lib/libc/newlib/assert.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 lib/libc/newlib/assert.c diff --git a/lib/libc/newlib/CMakeLists.txt b/lib/libc/newlib/CMakeLists.txt index e18a856eb941b..736e89ab4b99a 100644 --- a/lib/libc/newlib/CMakeLists.txt +++ b/lib/libc/newlib/CMakeLists.txt @@ -3,6 +3,8 @@ zephyr_library() zephyr_library_sources(libc-hooks.c) +zephyr_library_sources_ifdef(CONFIG_ASSERT assert.c) + # Zephyr normally uses -ffreestanding, which with current GNU toolchains # means that the flag macros used by newlib 3.x to signal # support for PRI.64 macros are not present. To make them available we @@ -47,4 +49,3 @@ if(CONFIG_NEWLIB_LIBC_NANO) -specs=nano.specs ) endif() - diff --git a/lib/libc/newlib/assert.c b/lib/libc/newlib/assert.c new file mode 100644 index 0000000000000..7b79549dc6fe1 --- /dev/null +++ b/lib/libc/newlib/assert.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +/** + * + * @brief Handler of C standard library assert. + */ +void __assert_func(const char *file, + int line, + const char *func, + const char *failedexpr) +{ + __ASSERT_LOC(...); + __ASSERT_MSG_INFO("Assertion fail: (%s) at %s:%d", + failedexpr, file, line); + __ASSERT_POST_ACTION(); +}