Add macro KERNAUX_PANIC

This commit is contained in:
Alex Kotov 2022-01-20 21:51:54 +05:00
parent 3afe745485
commit f001868720
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
6 changed files with 62 additions and 3 deletions

1
.gitignore vendored
View File

@ -89,6 +89,7 @@
/examples/assert_guards
/examples/assert_simple
/examples/cmdline
/examples/panic_simple
/examples/pfa
/examples/printf
/examples/printf_va

View File

@ -0,0 +1,3 @@
2022-01-20 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/assert.h: Added unconditional assertion (panic)

View File

@ -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

View File

@ -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*)

47
examples/panic_simple.c Normal file
View File

@ -0,0 +1,47 @@
#include <kernaux/assert.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
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);
}

View File

@ -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))