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 Nov 30, 2023
1 parent fd03e77 commit b9ea9f5
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 95 deletions.
4 changes: 4 additions & 0 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ ifneq ($(DEBUG), 0)
else
DEFINES += PRINTF=mcu_usb_printf
endif
ifneq ($(DISABLE_LEDGER_ASSERT), 1)
DEFINES += HAVE_LEDGER_ASSERT_DISPLAY
DEFINES += LEDGER_ASSERT_CONFIG_FILE_INFO
endif
else
DEFINES += PRINTF\(...\)=
endif
Expand Down
153 changes: 153 additions & 0 deletions include/ledger_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#pragma once

#include "appflags.h"
#include "bolos_target.h"
#include "decorators.h"
#include "errors.h"
#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_OFFSET 0
#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_OFFSET LR_AND_PC_SIZE
#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_OFFSET MESSAGE_OFFSET + MESSAGE_SIZE
#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)
#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)
void __attribute__((noreturn)) assert_display_exit(void);
void throw_display_lr(int e, int lr);
void throw_print_lr(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(lr_val); \
} while (0)
#endif
2 changes: 0 additions & 2 deletions include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ int snprintf(char *str, size_t str_size, const char *format, ...);
#endif // HAVE_SPRINTF

#ifndef HAVE_BOLOS
#if defined(HAVE_PRINTF) || defined(HAVE_DEBUG_THROWS)
int compute_address_location(int address);
#endif
#endif

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

This file was deleted.

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

This file was deleted.

4 changes: 2 additions & 2 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 @@ -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);
assert_display_exit();
#endif
}
FINALLY {}
Expand Down
Loading

0 comments on commit b9ea9f5

Please sign in to comment.