From ae6e65982097979c89f25bc1c7d4e4e496b061fa Mon Sep 17 00:00:00 2001 From: Sarah GLINER Date: Tue, 28 Nov 2023 15:10:57 +0100 Subject: [PATCH] debug: add ledger_assert --- Makefile.standard_app | 8 ++ lib_standard_app/debug.c | 91 ++++++++++++++++++--- lib_standard_app/debug.h | 165 ++++++++++++++++++++++++++++++++++++++- lib_standard_app/main.c | 2 +- 4 files changed, 255 insertions(+), 11 deletions(-) diff --git a/Makefile.standard_app b/Makefile.standard_app index 4199a179a..9ca4daf55 100644 --- a/Makefile.standard_app +++ b/Makefile.standard_app @@ -60,6 +60,14 @@ else DEFINES += PRINTF\(...\)= endif +##################################################################### +# LEDGER_ASSERT # +##################################################################### +ifneq ($(DISABLE_LEDGER_ASSERT), 1) + DEFINES += HAVE_LEDGER_ASSERT_DISPLAY + DEFINES += LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +endif + ##################################################################### # IO SEPROXY BUFFER SIZE # ##################################################################### diff --git a/lib_standard_app/debug.c b/lib_standard_app/debug.c index 3d0e99c8b..2a20d190c 100644 --- a/lib_standard_app/debug.c +++ b/lib_standard_app/debug.c @@ -19,21 +19,58 @@ #include "os.h" #include "io.h" +#include "debug.h" #ifdef HAVE_NBGL #include "nbgl_use_case.h" #endif -#ifdef HAVE_DEBUG_THROWS +#if defined(HAVE_LEDGER_ASSERT_DISPLAY) || defined(HAVE_DEBUG_THROWS) + +#ifndef ASSERT_BUFFER_LEN +#define ASSERT_BUFFER_LEN 24 +#endif +char assert_buffer[ASSERT_BUFFER_LEN]; -static char errordata[30]; -// This function can be used to declare a callback to THROW in the application +#ifdef HAVE_DEBUG_THROWS WEAK void app_throw_info(unsigned int exception, unsigned int lr_val) { - snprintf(errordata, sizeof(errordata), "0x%04X LR=0x%08X", exception, lr_val); + snprintf(assert_buffer, sizeof(assert_buffer), "e=0x%04X\n LR=0x%08X", exception, lr_val); +} +#endif +#endif + +#ifdef HAVE_LEDGER_ASSERT_DISPLAY +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +WEAK void assert_display_lr_and_pc(int lr, int pc) +{ + char buff[LR_AND_PC_SIZE]; + snprintf(buff, LR_AND_PC_SIZE, "LR=0x%08X\n PC=0x%08X\n", lr, pc); + strncat(assert_buffer, buff, LR_AND_PC_SIZE); } +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +WEAK void assert_display_message(const char *message) +{ + char buff[MESSAGE_SIZE]; + snprintf(buff, MESSAGE_SIZE, "%s\n", message); + strncat(assert_buffer, buff, MESSAGE_SIZE); +} +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +WEAK void assert_display_file_info(const char *file, unsigned int line) +{ + char buff[FILE_SIZE]; + snprintf(buff, FILE_SIZE, "%s::%d\n", file, line); + strncat(assert_buffer, buff, FILE_SIZE); +} +#endif +#endif -static void review_choice(bool confirm) +#if defined(HAVE_LEDGER_ASSERT_DISPLAY) || defined(HAVE_DEBUG_THROWS) +static void exit_app(bool confirm) { UNUSED(confirm); os_sched_exit(-1); @@ -42,15 +79,15 @@ static void review_choice(bool confirm) #ifdef HAVE_BAGL UX_STEP_CB(ux_error, bnnn_paging, - review_choice(true), + exit_app(true), { .title = "App error", - .text = errordata, + .text = assert_buffer, }); UX_FLOW(ux_error_flow, &ux_error); #endif -WEAK void __attribute__((noreturn)) debug_display_throw_error(int exception) +WEAK void __attribute__((noreturn)) debug_display_exit(int exception) { UNUSED(exception); @@ -60,7 +97,7 @@ WEAK void __attribute__((noreturn)) debug_display_throw_error(int exception) #ifdef HAVE_NBGL nbgl_useCaseChoice( - &C_round_warning_64px, "App error", errordata, "Exit app", "Exit app", review_choice); + &C_round_warning_64px, "App error", assert_buffer, "Exit app", "Exit app", exit_app); #endif // Block until the user approve and the app is quit @@ -68,5 +105,41 @@ WEAK void __attribute__((noreturn)) debug_display_throw_error(int exception) io_seproxyhal_io_heartbeat(); } } +#endif + +#ifdef HAVE_PRINTF + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +void print_file_info(const char *file, int line) +{ + PRINTF("%s::%d \n", file, line); +} +#endif +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +void print_message(const char *message) +{ + if (message) { + PRINTF("%s\n", message); + } +} #endif + +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +void print_lr_and_pc(int lr, int pc) +{ + PRINTF("=> LR: 0x%04X \n", lr); + PRINTF("=> PC: 0x%04X \n", pc); +} +#endif + +#endif + +void ledger_assert(void) +{ +#ifdef HAVE_LEDGER_ASSERT_DISPLAY + debug_display_exit(ASSERT_ERROR_CODE); +#else + exit_app(true); +#endif +} diff --git a/lib_standard_app/debug.h b/lib_standard_app/debug.h index 657f78808..83cdf17c9 100644 --- a/lib_standard_app/debug.h +++ b/lib_standard_app/debug.h @@ -1,5 +1,168 @@ #pragma once #include "macros.h" +#include "os.h" -WEAK void __attribute__((noreturn)) debug_display_throw_error(int exception); +#ifdef HAVE_DEBUG_THROWS WEAK void app_throw_info(unsigned int exception, unsigned int lr_val); +WEAK void __attribute__((noreturn)) debug_display_exit(int exception); +#endif + +void ledger_assert(void); + +#define ASSERT_ERROR_CODE 0xBAAD + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define LEDGER_ASSERT_CONFIG_MESSAGE_INFO 1 +#define LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO 1 +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#define LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO 1 +#endif + +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#endif + +#ifdef HAVE_LEDGER_ASSERT_DISPLAY +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#define LR_AND_PC_OFFSET 0 +#define LR_AND_PC_SIZE 30 +WEAK void assert_display_lr_and_pc(int lr, int pc); +#define ASSERT_DISPLAY_LR_AND_PC(lr, pc) assert_display_lr_and_pc(lr, pc) +#else +#define LR_AND_PC_SIZE 0 +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#define MESSAGE_OFFSET LR_AND_PC_SIZE +#define MESSAGE_SIZE 20 +WEAK void assert_display_message(const char *message); +#define ASSERT_DISPLAY_MESSAGE(message) assert_display_message(message) +#else +#define MESSAGE_SIZE 0 +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define FILE_OFFSET MESSAGE_OFFSET + MESSAGE_SIZE +#define FILE_SIZE 50 +WEAK void assert_display_file_info(const char *file, unsigned int line); +#define ASSERT_DISPLAY_FILE_INFO(file, line) assert_display_file_info(file, line) +#else +#define FILE_SIZE 0 +#endif + +#define ASSERT_BUFFER_LEN LR_AND_PC_SIZE + MESSAGE_SIZE + FILE_SIZE +#else +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#define ASSERT_DISPLAY_LR_AND_PC(lr, pc) \ + do { \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#define ASSERT_DISPLAY_MESSAGE(message) \ + do { \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define ASSERT_DISPLAY_FILE_INFO(file, line) \ + do { \ + } while (0) +#endif +#endif + +#ifdef HAVE_PRINTF +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +void print_lr_and_pc(int lr, int pc); +#define ASSERT_PRINT_LR_AND_PC(lr, pc) print_lr_and_pc(lr, pc) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +void print_message(const char *message); +#define ASSERT_PRINT_MESSAGE(message) print_message(message) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +void print_file_info(const char *file, int line); +#define ASSERT_PRINT_FILE_INFO(file, line) print_file_info(file, line) +#endif +#else +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#define ASSERT_PRINT_LR_AND_PC(lr, pc) \ + do { \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#define ASSERT_PRINT_MESSAGE(message) \ + do { \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define ASSERT_PRINT_FILE_INFO(file, line) \ + do { \ + } while (0) +#endif +#endif + +#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#define LEDGER_ASSERT_LR_AND_PC() \ + do { \ + int _lr_address = 0; \ + int _pc_address = 0; \ + \ + __asm volatile("mov %0, lr" : "=r"(_lr_address)); \ + __asm volatile("mov %0, pc" : "=r"(_pc_address)); \ + _lr_address = compute_address_location(_lr_address); \ + _pc_address = compute_address_location(_pc_address); \ + ASSERT_PRINT_LR_AND_PC(_lr_address, _pc_address); \ + ASSERT_DISPLAY_LR_AND_PC(_lr_address, _pc_address); \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#define LEDGER_ASSERT_MESSAGE(message) \ + do { \ + ASSERT_PRINT_MESSAGE(message); \ + ASSERT_DISPLAY_MESSAGE(message); \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define LEDGER_ASSERT_FILE_INFO() \ + do { \ + ASSERT_PRINT_FILE_INFO(__FILE__, __LINE__); \ + ASSERT_DISPLAY_FILE_INFO(__FILE__, __LINE__); \ + } while (0) +#endif + +#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#define LEDGER_ASSERT(test, message) \ + do { \ + if (!(test)) { \ + LEDGER_ASSERT_LR_AND_PC(); \ + LEDGER_ASSERT_MESSAGE(message); \ + LEDGER_ASSERT_FILE_INFO(); \ + ledger_assert(); \ + } \ + } while (0) +#elif defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) +#define LEDGER_ASSERT(test, message) \ + do { \ + if (!(test)) { \ + LEDGER_ASSERT_LR_AND_PC(); \ + LEDGER_ASSERT_MESSAGE(message); \ + ledger_assert(); \ + } \ + } while (0) +#elif defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) +#define LEDGER_ASSERT(test, message) \ + do { \ + if (!(test)) { \ + LEDGER_ASSERT_LR_AND_PC(); \ + ledger_assert(); \ + } \ + } while (0) +#endif diff --git a/lib_standard_app/main.c b/lib_standard_app/main.c index b1d2b9935..6a18693cb 100644 --- a/lib_standard_app/main.c +++ b/lib_standard_app/main.c @@ -90,7 +90,7 @@ static void standalone_app_main(void) BLE_power(0, NULL); #endif // Display crash info on screen for debug purpose - debug_display_throw_error(e); + debug_display_exit(e); #endif } FINALLY {}