Skip to content

Commit

Permalink
ledger_assert: add LEDGER_ASSERT macro with debug capacities
Browse files Browse the repository at this point in the history
  • Loading branch information
sgliner-ledger committed Dec 4, 2023
1 parent 47af250 commit 9adfe8e
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 94 deletions.
7 changes: 7 additions & 0 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ ifneq ($(DEBUG), 0)
else
DEFINES += PRINTF=mcu_usb_printf
endif
ifneq ($(DISABLE_DEBUG_LEDGER_ASSERT), 1)
DEFINES += HAVE_LEDGER_ASSERT_DISPLAY
DEFINES += LEDGER_ASSERT_CONFIG_FILE_INFO
endif
ifneq ($(DISABLE_DEBUG_THROW), 1)
DEFINES += HAVE_DEBUG_THROWS
endif
else
DEFINES += PRINTF\(...\)=
endif
Expand Down
156 changes: 156 additions & 0 deletions include/ledger_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#pragma once

#include "os.h"

#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

#if defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY)
#define LR_AND_PC_SIZE 30
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
#define ASSERT_DISPLAY_LR_AND_PC(lr, pc) \
do { \
} while (0)
#endif

#if defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY)
#define MESSAGE_SIZE 20
void assert_display_message(const char *message);
#define ASSERT_DISPLAY_MESSAGE(message) assert_display_message(message)
#else
#define MESSAGE_SIZE 0
#define ASSERT_DISPLAY_MESSAGE(message) \
do { \
} while (0)
#endif

#if defined(LEDGER_ASSERT_CONFIG_FILE_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY)
#define FILE_SIZE 50
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
#define ASSERT_DISPLAY_FILE_INFO(file, line) \
do { \
} while (0)
#endif

#ifdef HAVE_LEDGER_ASSERT_DISPLAY
#define ASSERT_BUFFER_LEN LR_AND_PC_SIZE + MESSAGE_SIZE + FILE_SIZE
void __attribute__((noreturn)) assert_display_exit(void);

#define LEDGER_ASSERT_EXIT() assert_display_exit()
#else
void assert_exit(bool confirm);
#define LEDGER_ASSERT_EXIT() assert_exit(true)
#endif

#if defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) && defined(HAVE_PRINTF)
void assert_print_lr_and_pc(int lr, int pc);
#define ASSERT_PRINT_LR_AND_PC(lr, pc) assert_print_lr_and_pc(lr, pc)
#else
#define ASSERT_PRINT_LR_AND_PC(lr, pc) \
do { \
} while (0)
#endif

#if defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) && defined(HAVE_PRINTF)
void assert_print_message(const char *message);
#define ASSERT_PRINT_MESSAGE(message) assert_print_message(message)
#else
#define ASSERT_PRINT_MESSAGE(message) \
do { \
} while (0)
#endif

#if defined(LEDGER_ASSERT_CONFIG_FILE_INFO) && defined(HAVE_PRINTF)
void assert_print_file_info(const char *file, int line);
#define ASSERT_PRINT_FILE_INFO(file, line) assert_print_file_info(file, line)
#else
#define ASSERT_PRINT_FILE_INFO(file, line) \
do { \
} while (0)
#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)); \
ASSERT_PRINT_LR_AND_PC(_lr_address, _pc_address); \
ASSERT_DISPLAY_LR_AND_PC(_lr_address, _pc_address); \
} while (0)
#elif defined(HAVE_PRINTF)
#define LEDGER_ASSERT_LR_AND_PC() PRINTF("LEDGER_ASSERT FAILED\n")
#else
#define LEDGER_ASSERT_LR_AND_PC() \
do { \
} 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)
#else
#define LEDGER_ASSERT_MESSAGE(message) \
do { \
} 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)
#else
#define LEDGER_ASSERT_FILE_INFO() \
do { \
} while (0)
#endif

#define LEDGER_ASSERT(test, message) \
do { \
if (!(test)) { \
LEDGER_ASSERT_LR_AND_PC(); \
LEDGER_ASSERT_MESSAGE(message); \
LEDGER_ASSERT_FILE_INFO(); \
LEDGER_ASSERT_EXIT(); \
} \
} while (0)

#if defined(HAVE_DEBUG_THROWS) && defined(HAVE_PRINTF)
void throw_print_lr(int e, int lr);
#define THROW_PRINT_LR(e, lr_val) throw_print_lr(e, lr_val)
#else
#define THROW_PRINT_LR(e, lr_val) \
do { \
} while (0)
#endif

#if defined(HAVE_DEBUG_THROWS)
void __attribute__((noreturn)) assert_display_exit(void);
void throw_display_lr(int e, int lr);
#define DEBUG_THROW(e) \
do { \
unsigned int lr_val; \
__asm volatile("mov %0, lr" : "=r"(lr_val)); \
throw_display_lr(e, lr_val); \
THROW_PRINT_LR(e, lr_val); \
} while (0)
#endif
4 changes: 4 additions & 0 deletions include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ int snprintf(char *str, size_t str_size, const char *format, ...);
#endif // APPLE
#endif // HAVE_SPRINTF

#ifndef HAVE_BOLOS
int compute_address_location(int address);
#endif

// syscall test
// SYSCALL void dummy_1(unsigned int* p PLENGTH(2+len+15+ len + 16 + sizeof(io_send_t) + 1 ),
// unsigned int len);
Expand Down
71 changes: 0 additions & 71 deletions lib_standard_app/debug.c

This file was deleted.

3 changes: 0 additions & 3 deletions lib_standard_app/debug.h

This file was deleted.

10 changes: 5 additions & 5 deletions lib_standard_app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "os.h"
#include "io.h"
#include "debug.h"
#include "ledger_assert.h"

#ifdef HAVE_SWAP
#include "swap.h"
Expand Down Expand Up @@ -72,8 +72,6 @@ static void standalone_app_main(void)
}
CATCH_OTHER(e)
{
PRINTF("Exiting following exception: %d\n", e);

#ifdef HAVE_DEBUG_THROWS
// Disable USB and BLE, the app have crashed and is going to be exited
// This is necessary to avoid device freeze while displaying throw error
Expand All @@ -90,7 +88,9 @@ static void standalone_app_main(void)
BLE_power(0, NULL);
#endif
// Display crash info on screen for debug purpose
debug_display_throw_error(e);
assert_display_exit();
#else
PRINTF("Exiting following exception: 0x%04X\n", e);
#endif
}
FINALLY {}
Expand Down Expand Up @@ -141,7 +141,7 @@ static void library_app_main(libargs_t *args)
}
CATCH_OTHER(e)
{
PRINTF("Exiting following exception: %d\n", e);
PRINTF("Exiting following exception: 0x%04X\n", e);
}
FINALLY
{
Expand Down
Loading

0 comments on commit 9adfe8e

Please sign in to comment.