[FL-3286] Don't reboot on crash in debug builds (#2613)
* furi: never reboot on furi_crash in debug builds * furi: crash info: added registers * furi: check and assert optimization, split registers and stack info dump * furi: macro uppercase Co-authored-by: SG <who.just.the.doctor@gmail.com>
This commit is contained in:
		@@ -14,7 +14,7 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
PLACE_IN_SECTION("MB_MEM2") const char* __furi_check_message = NULL;
 | 
					PLACE_IN_SECTION("MB_MEM2") const char* __furi_check_message = NULL;
 | 
				
			||||||
PLACE_IN_SECTION("MB_MEM2") uint32_t __furi_check_registers[12] = {0};
 | 
					PLACE_IN_SECTION("MB_MEM2") uint32_t __furi_check_registers[13] = {0};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Load r12 value to __furi_check_message and store registers to __furi_check_registers */
 | 
					/** Load r12 value to __furi_check_message and store registers to __furi_check_registers */
 | 
				
			||||||
#define GET_MESSAGE_AND_STORE_REGISTERS()               \
 | 
					#define GET_MESSAGE_AND_STORE_REGISTERS()               \
 | 
				
			||||||
@@ -22,6 +22,7 @@ PLACE_IN_SECTION("MB_MEM2") uint32_t __furi_check_registers[12] = {0};
 | 
				
			|||||||
                 "str r12, [r11]                    \n" \
 | 
					                 "str r12, [r11]                    \n" \
 | 
				
			||||||
                 "ldr r12, =__furi_check_registers  \n" \
 | 
					                 "ldr r12, =__furi_check_registers  \n" \
 | 
				
			||||||
                 "stm r12, {r0-r11}                 \n" \
 | 
					                 "stm r12, {r0-r11}                 \n" \
 | 
				
			||||||
 | 
					                 "str lr, [r12, #48]                \n" \
 | 
				
			||||||
                 :                                      \
 | 
					                 :                                      \
 | 
				
			||||||
                 :                                      \
 | 
					                 :                                      \
 | 
				
			||||||
                 : "memory");
 | 
					                 : "memory");
 | 
				
			||||||
@@ -62,6 +63,25 @@ static void __furi_put_uint32_as_text(uint32_t data) {
 | 
				
			|||||||
    furi_hal_console_puts(tmp_str);
 | 
					    furi_hal_console_puts(tmp_str);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void __furi_put_uint32_as_hex(uint32_t data) {
 | 
				
			||||||
 | 
					    char tmp_str[] = "0xFFFFFFFF";
 | 
				
			||||||
 | 
					    itoa(data, tmp_str, 16);
 | 
				
			||||||
 | 
					    furi_hal_console_puts(tmp_str);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void __furi_print_register_info() {
 | 
				
			||||||
 | 
					    // Print registers
 | 
				
			||||||
 | 
					    for(uint8_t i = 0; i < 12; i++) {
 | 
				
			||||||
 | 
					        furi_hal_console_puts("\r\n\tr");
 | 
				
			||||||
 | 
					        __furi_put_uint32_as_text(i);
 | 
				
			||||||
 | 
					        furi_hal_console_puts(" : ");
 | 
				
			||||||
 | 
					        __furi_put_uint32_as_hex(__furi_check_registers[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    furi_hal_console_puts("\r\n\tlr : ");
 | 
				
			||||||
 | 
					    __furi_put_uint32_as_hex(__furi_check_registers[12]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void __furi_print_stack_info() {
 | 
					static void __furi_print_stack_info() {
 | 
				
			||||||
    furi_hal_console_puts("\r\n\tstack watermark: ");
 | 
					    furi_hal_console_puts("\r\n\tstack watermark: ");
 | 
				
			||||||
    __furi_put_uint32_as_text(uxTaskGetStackHighWaterMark(NULL) * 4);
 | 
					    __furi_put_uint32_as_text(uxTaskGetStackHighWaterMark(NULL) * 4);
 | 
				
			||||||
@@ -101,32 +121,41 @@ FURI_NORETURN void __furi_crash() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if(__furi_check_message == NULL) {
 | 
					    if(__furi_check_message == NULL) {
 | 
				
			||||||
        __furi_check_message = "Fatal Error";
 | 
					        __furi_check_message = "Fatal Error";
 | 
				
			||||||
 | 
					    } else if(__furi_check_message == (void*)__FURI_ASSERT_MESSAGE_FLAG) {
 | 
				
			||||||
 | 
					        __furi_check_message = "furi_assert failed";
 | 
				
			||||||
 | 
					    } else if(__furi_check_message == (void*)__FURI_CHECK_MESSAGE_FLAG) {
 | 
				
			||||||
 | 
					        __furi_check_message = "furi_check failed";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    furi_hal_console_puts("\r\n\033[0;31m[CRASH]");
 | 
					    furi_hal_console_puts("\r\n\033[0;31m[CRASH]");
 | 
				
			||||||
    __furi_print_name(isr);
 | 
					    __furi_print_name(isr);
 | 
				
			||||||
    furi_hal_console_puts(__furi_check_message);
 | 
					    furi_hal_console_puts(__furi_check_message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    __furi_print_register_info();
 | 
				
			||||||
    if(!isr) {
 | 
					    if(!isr) {
 | 
				
			||||||
        __furi_print_stack_info();
 | 
					        __furi_print_stack_info();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    __furi_print_heap_info();
 | 
					    __furi_print_heap_info();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef FURI_DEBUG
 | 
				
			||||||
    // Check if debug enabled by DAP
 | 
					    // Check if debug enabled by DAP
 | 
				
			||||||
    // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
 | 
					    // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
 | 
				
			||||||
    bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
 | 
					    bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
 | 
				
			||||||
    if(debug) {
 | 
					    if(debug) {
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
        furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
 | 
					        furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
 | 
				
			||||||
        furi_hal_console_puts("\033[0m\r\n");
 | 
					        furi_hal_console_puts("\033[0m\r\n");
 | 
				
			||||||
        furi_hal_debug_enable();
 | 
					        furi_hal_debug_enable();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        RESTORE_REGISTERS_AND_HALT_MCU(true);
 | 
					        RESTORE_REGISTERS_AND_HALT_MCU(true);
 | 
				
			||||||
 | 
					#ifndef FURI_DEBUG
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
 | 
					        furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
 | 
				
			||||||
        furi_hal_console_puts("\r\nRebooting system.\r\n");
 | 
					        furi_hal_console_puts("\r\nRebooting system.\r\n");
 | 
				
			||||||
        furi_hal_console_puts("\033[0m\r\n");
 | 
					        furi_hal_console_puts("\033[0m\r\n");
 | 
				
			||||||
        furi_hal_power_reset();
 | 
					        furi_hal_power_reset();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
    __builtin_unreachable();
 | 
					    __builtin_unreachable();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,10 @@ extern "C" {
 | 
				
			|||||||
#define FURI_NORETURN noreturn
 | 
					#define FURI_NORETURN noreturn
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Flags instead of pointers will save ~4 bytes on furi_assert and furi_check calls.
 | 
				
			||||||
 | 
					#define __FURI_ASSERT_MESSAGE_FLAG (0x01)
 | 
				
			||||||
 | 
					#define __FURI_CHECK_MESSAGE_FLAG (0x02)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Crash system */
 | 
					/** Crash system */
 | 
				
			||||||
FURI_NORETURN void __furi_crash();
 | 
					FURI_NORETURN void __furi_crash();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -44,20 +48,20 @@ FURI_NORETURN void __furi_halt();
 | 
				
			|||||||
    } while(0)
 | 
					    } while(0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Check condition and crash if check failed */
 | 
					/** Check condition and crash if check failed */
 | 
				
			||||||
#define furi_check(__e)                          \
 | 
					#define furi_check(__e)                             \
 | 
				
			||||||
    do {                                         \
 | 
					    do {                                            \
 | 
				
			||||||
        if(!(__e)) {                             \
 | 
					        if(!(__e)) {                                \
 | 
				
			||||||
            furi_crash("furi_check failed\r\n"); \
 | 
					            furi_crash(__FURI_ASSERT_MESSAGE_FLAG); \
 | 
				
			||||||
        }                                        \
 | 
					        }                                           \
 | 
				
			||||||
    } while(0)
 | 
					    } while(0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Only in debug build: Assert condition and crash if assert failed  */
 | 
					/** Only in debug build: Assert condition and crash if assert failed  */
 | 
				
			||||||
#ifdef FURI_DEBUG
 | 
					#ifdef FURI_DEBUG
 | 
				
			||||||
#define furi_assert(__e)                          \
 | 
					#define furi_assert(__e)                           \
 | 
				
			||||||
    do {                                          \
 | 
					    do {                                           \
 | 
				
			||||||
        if(!(__e)) {                              \
 | 
					        if(!(__e)) {                               \
 | 
				
			||||||
            furi_crash("furi_assert failed\r\n"); \
 | 
					            furi_crash(__FURI_CHECK_MESSAGE_FLAG); \
 | 
				
			||||||
        }                                         \
 | 
					        }                                          \
 | 
				
			||||||
    } while(0)
 | 
					    } while(0)
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
#define furi_assert(__e) \
 | 
					#define furi_assert(__e) \
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user