Commit 3b630300 authored by Alessandro Rubini's avatar Alessandro Rubini Committed by Adam Wujek

general: added panic/assert mechanism

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent dec144d5
......@@ -21,6 +21,15 @@ config PRINT_BUFSIZE
int
default 128
config ASSERT
bool "Build assertion checks in the code"
default y
help
Build assertions in the code, to catch unexpected situations.
When an assertion fails the code loops over repeating the
error message every second. OTOH, panic() is always built,
with no Kconfig -- and it does the same, unconditionally.
config RAMSIZE
int
default 65536 if WR_SWITCH
......
#ifndef __ASSERT_H__
#define __ASSERT_H__
/*
* Both panic and assert loop over the console, re-printing the
* message, so you can connnect to a paniced node and see the message
* that caused the panic. assert_warn(condition) is once only, like
* warn_on(!condition) in the kernel.
*/
extern void panic(const char *fmt, ...)
__attribute__((format(printf,1,2)));
#define assert(cond, fmt, ...) \
if (CONFIG_HAS_ASSERT && !(cond)) \
__assert(__func__, __LINE__, 1 /* forever */, fmt __VA_ARGS__)
#define assert_warn(cond, fmt, ...) \
if (CONFIG_HAS_ASSERT && !(cond)) \
__assert(__func__, __LINE__, 0 /* once */, fmt __VA_ARGS__)
extern void __assert(const char *func, int line, int forever,
const char *fmt, ...)
__attribute__((format(printf, 4, 5)));
#ifdef CONFIG_ASSERT
# define CONFIG_HAS_ASSERT 1
#else
# define CONFIG_HAS_ASSERT 0
#endif
#endif /* __ASSERT_H__ */
#include <stdarg.h>
#include <assert.h>
#include <wrc.h>
void panic(const char *fmt, ...)
{
va_list args;
while (1) {
pp_printf("Panic: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
usleep(1000 * 1000);
}
}
void __assert(const char *func, int line, int forever,
const char *fmt, ...)
{
va_list args;
while (1) {
pp_printf("Assertion failed (%s:%i)", func, line);
if (fmt && fmt[0]) {
pp_printf(": ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
} else {
pp_printf(".\n");
}
if (!forever)
break;
usleep(1000 * 1000);
}
}
obj-y += lib/util.o
obj-y += lib/assert.o
obj-$(CONFIG_LM32) += \
lib/atoi.o \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment