Skip to content

Commit

Permalink
debug: add ledger_assert
Browse files Browse the repository at this point in the history
  • Loading branch information
sgliner-ledger committed Nov 28, 2023
1 parent 7c30dd9 commit ae6e659
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
#####################################################################
Expand Down
91 changes: 82 additions & 9 deletions lib_standard_app/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -60,13 +97,49 @@ 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
while (1) {
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
}
165 changes: 164 additions & 1 deletion lib_standard_app/debug.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib_standard_app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit ae6e659

Please sign in to comment.