furi_check - a new way to asserting (#204)

* hal-related task_is_isr_context function
* furi_check implementation
* change application to use furi_check
* add second level of assertion
* add TODO about ISR context
* Applications: refactor furi_check and furi_assert.
* Apploader: propwer widget usage. Menu: check on furi resource request.
* refactor furi_check

Co-authored-by: Aleksandr Kutuzov <aku@plooks.com>
Co-authored-by: coreglitch <mail@s3f.ru>
This commit is contained in:
DrZlo13
2020-10-29 09:27:17 +03:00
committed by GitHub
parent c9b921f6ce
commit 8aeafd8179
24 changed files with 292 additions and 136 deletions

41
core/api-basic/check.c Normal file
View File

@@ -0,0 +1,41 @@
#include "check.h"
#include "api-hal-task.h"
void __furi_abort(void);
// TODO printf doesnt work in ISR context
void __furi_check(void) {
printf("assertion failed in release mode, switch to debug mode to see full assert info");
__furi_abort();
}
// TODO printf doesnt work in ISR context
void __furi_check_debug(const char* file, int line, const char* function, const char* condition) {
printf(
"assertion \"%s\" failed: file \"%s\", line %d%s%s",
condition,
file,
line,
function ? ", function: " : "",
function ? function : "");
if(task_is_isr_context()) {
printf(" in [ISR] context");
} else {
FuriApp* app = find_task(xTaskGetCurrentTaskHandle());
if(app == NULL) {
printf(", in [main] context");
} else {
printf(", in [%s] app context", app->name);
}
}
__furi_abort();
}
void __furi_abort(void) {
taskDISABLE_INTERRUPTS();
while(1) {
}
}

41
core/api-basic/check.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include "flipper.h"
// Find how to how get function's pretty name
#ifndef __FURI_CHECK_FUNC
// Use g++'s demangled names in C++
#if defined __cplusplus && defined __GNUC__
#define __FURI_CHECK_FUNC __PRETTY_FUNCTION__
// C99 requires the use of __func__
#elif __STDC_VERSION__ >= 199901L
#define __FURI_CHECK_FUNC __func__
// Older versions of gcc don't have __func__ but can use __FUNCTION__
#elif __GNUC__ >= 2
#define __FURI_CHECK_FUNC __FUNCTION__
// failed to detect __func__ support
#else
#define __FURI_CHECK_FUNC ((char*)0)
#endif
#endif
// !__FURI_CHECK_FUNC
// We have two levels of assertion
// One - furi_check, which always runs, the only difference is in the level of debug information
// The second is furi_assert, which doesn't compile in release mode
#ifdef NDEBUG
#define furi_check(__e) ((__e) ? (void)0 : __furi_check())
#define furi_assert(__e) ((void)0)
#else
#define furi_check(__e) \
((__e) ? (void)0 : __furi_check_debug(__FILE__, __LINE__, __FURI_CHECK_FUNC, #__e))
#define furi_assert(__e) \
((__e) ? (void)0 : __furi_check_debug(__FILE__, __LINE__, __FURI_CHECK_FUNC, #__e))
#endif
// !NDEBUG
void __furi_check(void);
void __furi_check_debug(const char* file, int line, const char* function, const char* condition);

View File

@@ -49,7 +49,7 @@ static inline void* acquire_mutex_block(ValueMutex* valuemutex) {
#define with_value_mutex(value_mutex, function_body) \
{ \
void* p = acquire_mutex_block(value_mutex); \
assert(p); \
furi_check(p); \
({ void __fn__ function_body __fn__; })(p); \
release_mutex(value_mutex, p); \
}