From f0018687203674e7a3fce5d51388f024bc0cf1da Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 20 Jan 2022 21:51:54 +0500 Subject: [PATCH] Add macro KERNAUX_PANIC --- .gitignore | 1 + ChangeLog | 3 +++ Makefile.am | 7 +++++- README.md | 5 +++-- examples/panic_simple.c | 47 ++++++++++++++++++++++++++++++++++++++++ include/kernaux/assert.h | 2 ++ 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 examples/panic_simple.c diff --git a/.gitignore b/.gitignore index d66a100..638b298 100644 --- a/.gitignore +++ b/.gitignore @@ -89,6 +89,7 @@ /examples/assert_guards /examples/assert_simple /examples/cmdline +/examples/panic_simple /examples/pfa /examples/printf /examples/printf_va diff --git a/ChangeLog b/ChangeLog index e69de29..23f5d9d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -0,0 +1,3 @@ +2022-01-20 Alex Kotov + + * include/kernaux/assert.h: Added unconditional assertion (panic) diff --git a/Makefile.am b/Makefile.am index 88d1bbd..afec273 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,7 +19,8 @@ libkernaux_a_SOURCES = \ if ENABLE_TESTS TESTS = \ examples/assert_guards \ - examples/assert_simple + examples/assert_simple \ + examples/panic_simple endif if ASM_I386 @@ -140,6 +141,10 @@ examples_cmdline_SOURCES = \ $(libkernaux_a_SOURCES) \ examples/cmdline.c +examples_panic_simple_SOURCES = \ + $(libkernaux_a_SOURCES) \ + examples/panic_simple.c + examples_pfa_SOURCES = \ $(libkernaux_a_SOURCES) \ examples/pfa.c diff --git a/README.md b/README.md index 1bbafef..6bafde0 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ zero). Work-in-progress APIs can change at any time. * [Declarations](/include/kernaux/arch/) * [Functions](/include/kernaux/asm/) * [Assertions](/include/kernaux/assert.h) (*stable since* **0.1.0**) - * [Simple](/examples/assert_simple.c) - * [Guards](/examples/assert_guards.c) + * [Assert: simple](/examples/assert_simple.c) + * [Assert: guards](/examples/assert_guards.c) + * [Panic: simple](/examples/panic_simple.c) * Stack trace *(planned)* * Device drivers (for debugging only) * [Serial console](/include/kernaux/console.h) (*work in progress*) diff --git a/examples/panic_simple.c b/examples/panic_simple.c new file mode 100644 index 0000000..e0ce9e1 --- /dev/null +++ b/examples/panic_simple.c @@ -0,0 +1,47 @@ +#include + +#include +#include +#include + +static unsigned int count = 0; +static const char *last_file = NULL; +static int last_line = 0; +static const char *last_str = NULL; + +static void assert_cb( + const char *const file, + const int line, + const char *const str +) { + ++count; + last_file = file; + last_line = line; + last_str = str; +} + +int main() +{ + KERNAUX_PANIC("foo"); + + assert(count == 0); + assert(last_file == NULL); + assert(last_line == 0); + assert(last_str == NULL); + + kernaux_assert_cb = assert_cb; + + KERNAUX_PANIC("bar"); + + assert(count == 1); + assert(strcmp(last_file, __FILE__) == 0); + assert(last_line == __LINE__ - 4); + assert(strcmp(last_str, "\"bar\"") == 0); + + KERNAUX_PANIC("car"); + + assert(count == 2); + assert(strcmp(last_file, __FILE__) == 0); + assert(last_line == __LINE__ - 4); + assert(strcmp(last_str, "\"car\"") == 0); +} diff --git a/include/kernaux/assert.h b/include/kernaux/assert.h index 4817119..105c938 100644 --- a/include/kernaux/assert.h +++ b/include/kernaux/assert.h @@ -5,6 +5,8 @@ extern "C" { #endif +#define KERNAUX_PANIC(msg) (kernaux_assert_do(__FILE__, __LINE__, #msg)) + #ifdef KERNAUX_ENABLE_ASSERT #define KERNAUX_ASSERT(cond) \ ((cond) ? (void)0 : kernaux_assert_do(__FILE__, __LINE__, #cond))