2023-08-15 01:14:02 -03:00
|
|
|
#include "AP_InternalError_config.h"
|
|
|
|
|
|
|
|
#if AP_INTERNALERROR_ENABLED
|
|
|
|
|
2019-03-18 08:32:42 -03:00
|
|
|
#include "AP_InternalError.h"
|
|
|
|
|
2023-08-15 01:14:02 -03:00
|
|
|
#include <AP_HAL/HAL.h>
|
|
|
|
#include <AP_HAL/Util.h>
|
|
|
|
|
2020-11-13 01:10:16 -04:00
|
|
|
#include <stdio.h>
|
2019-07-03 21:06:02 -03:00
|
|
|
|
2019-05-09 08:15:00 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2019-03-18 08:32:42 -03:00
|
|
|
// actually create the instance:
|
|
|
|
static AP_InternalError instance;
|
|
|
|
|
2020-04-29 21:40:46 -03:00
|
|
|
void AP_InternalError::error(const AP_InternalError::error_t e, uint16_t line) {
|
2022-10-20 21:28:40 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && defined(HAL_DEBUG_BUILD)
|
2019-05-09 08:15:00 -03:00
|
|
|
switch (e) {
|
|
|
|
case AP_InternalError::error_t::watchdog_reset:
|
|
|
|
case AP_InternalError::error_t::main_loop_stuck:
|
2021-01-12 19:10:42 -04:00
|
|
|
case AP_InternalError::error_t::params_restored:
|
2019-05-09 08:15:00 -03:00
|
|
|
// don't panic on these to facilitate watchdog testing
|
|
|
|
break;
|
|
|
|
default:
|
2021-08-20 03:03:25 -03:00
|
|
|
char buffer[50];
|
|
|
|
AP::internalerror().error_to_string(buffer, ARRAY_SIZE(buffer), e);
|
|
|
|
AP_HAL::panic("AP_InternalError::error_t::%s", buffer);
|
2019-05-09 05:48:50 -03:00
|
|
|
}
|
2023-05-01 20:29:17 -03:00
|
|
|
#endif
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_ESP32
|
|
|
|
if (e == AP_InternalError::error_t::imu_reset) return;// don't worry about this for esp32
|
2019-03-18 08:32:42 -03:00
|
|
|
#endif
|
|
|
|
internal_errors |= uint32_t(e);
|
2019-06-03 00:20:53 -03:00
|
|
|
total_error_count++;
|
2020-04-29 21:40:46 -03:00
|
|
|
last_line = line;
|
2019-06-03 00:20:53 -03:00
|
|
|
|
2019-05-09 08:15:00 -03:00
|
|
|
hal.util->persistent_data.internal_errors = internal_errors;
|
2019-06-03 00:20:53 -03:00
|
|
|
hal.util->persistent_data.internal_error_count = total_error_count;
|
2020-04-29 21:40:46 -03:00
|
|
|
hal.util->persistent_data.internal_error_last_line = line;
|
2019-03-18 08:32:42 -03:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:03:25 -03:00
|
|
|
static const char * const error_bit_descriptions[] {
|
|
|
|
"mapfailure", // logger_mapfailure
|
|
|
|
"miss_struct", // logger_missing_logstructure
|
|
|
|
"write_mssfmt", // logger_logwrite_missingfmt
|
|
|
|
"many_deletes", // logger_too_many_deletions
|
|
|
|
"bad_getfile", // logger_bad_getfilename
|
|
|
|
"panic",
|
|
|
|
"flush_no_sem", // logger_flushing_without_sem
|
|
|
|
"bad_curr_blk", // logger_bad_current_block
|
|
|
|
"blkcnt_bad", // logger_blockcount_mismatch
|
|
|
|
"dq_failure", // logger_dequeue_failure
|
|
|
|
"cnstring_nan", // constraining_nan
|
|
|
|
"watchdog_rst", // watchdog_reset
|
|
|
|
"iomcu_reset",
|
|
|
|
"iomcu_fail",
|
|
|
|
"spi_fail",
|
|
|
|
"main_loop_stk", // main_loop_stuck
|
|
|
|
"gcs_bad_link", // gcs_bad_missionprotocol_link
|
|
|
|
"bitmask_range",
|
|
|
|
"gcs_offset",
|
|
|
|
"i2c_isr",
|
|
|
|
"flow_of_ctrl", // flow_of_control
|
|
|
|
"sfs_recursion", // switch_full_sector_recursion
|
|
|
|
"bad_rotation",
|
|
|
|
"stack_ovrflw", // stack_overflow
|
|
|
|
"imu_reset", // imu_reset
|
|
|
|
"gpio_isr",
|
|
|
|
"mem_guard",
|
|
|
|
"dma_fail",
|
|
|
|
"params_restored",
|
|
|
|
"invalid arguments",
|
|
|
|
};
|
2020-04-30 02:09:40 -03:00
|
|
|
|
2021-08-20 03:03:25 -03:00
|
|
|
static_assert((1U<<(ARRAY_SIZE(error_bit_descriptions))) == uint32_t(AP_InternalError::error_t::__LAST__), "too few descriptions for bits");
|
2020-04-30 02:09:40 -03:00
|
|
|
|
2021-08-20 03:03:25 -03:00
|
|
|
void AP_InternalError::error_to_string(char *buffer, const uint16_t len, error_t error_code) const
|
|
|
|
{
|
|
|
|
uint32_t temp = log2f(int(error_code));
|
|
|
|
strncpy(buffer, error_bit_descriptions[temp], len - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InternalError::errors_as_string(uint8_t *buffer, const uint16_t len) const
|
|
|
|
{
|
2020-04-30 02:09:40 -03:00
|
|
|
buffer[0] = 0;
|
|
|
|
uint32_t buffer_used = 0;
|
2020-09-01 01:27:03 -03:00
|
|
|
const char *format = "%s"; // no comma before the first item
|
2020-04-30 02:09:40 -03:00
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(error_bit_descriptions); i++) {
|
|
|
|
if (buffer_used >= len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (internal_errors & (1U<<i)) {
|
2020-09-01 01:27:03 -03:00
|
|
|
const int written = hal.util->snprintf((char*)&buffer[buffer_used],
|
2020-04-30 02:09:40 -03:00
|
|
|
len-buffer_used,
|
|
|
|
format,
|
|
|
|
error_bit_descriptions[i]);
|
2020-09-01 01:27:03 -03:00
|
|
|
format = ",%s"; // once we write something, need commas thereafter
|
|
|
|
|
|
|
|
if (written < 0) {
|
2020-04-30 02:09:40 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer_used += written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 08:32:42 -03:00
|
|
|
namespace AP {
|
|
|
|
|
|
|
|
AP_InternalError &internalerror()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2020-04-28 03:35:38 -03:00
|
|
|
|
|
|
|
// stack overflow hook for low level RTOS code, C binding
|
|
|
|
void AP_stack_overflow(const char *thread_name)
|
|
|
|
{
|
|
|
|
static bool done_stack_overflow;
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::stack_overflow);
|
2020-04-28 03:35:38 -03:00
|
|
|
if (!done_stack_overflow) {
|
|
|
|
// we don't want to record the thread name more than once, as
|
|
|
|
// first overflow can trigger a 2nd
|
2020-07-07 00:14:35 -03:00
|
|
|
strncpy_noterm(hal.util->persistent_data.thread_name4, thread_name, 4);
|
2020-04-28 03:35:38 -03:00
|
|
|
done_stack_overflow = true;
|
|
|
|
}
|
|
|
|
hal.util->persistent_data.fault_type = 42; // magic value
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("stack overflow %s", thread_name);
|
2020-04-28 03:35:38 -03:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 01:10:16 -04:00
|
|
|
|
|
|
|
// hook for memory guard errors with --enable-memory-guard
|
|
|
|
void AP_memory_guard_error(uint32_t size)
|
|
|
|
{
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::mem_guard);
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
::printf("memory guard error size=%u\n", unsigned(size));
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("memory guard size=%u", unsigned(size));
|
2020-11-13 01:10:16 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-15 01:14:02 -03:00
|
|
|
|
|
|
|
#endif // AP_INTERNALERROR_ENABLED
|