From 1be089cf0c8be7666c9bfe8bc048972ccae39a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 3 Jul 2017 18:02:27 +0200 Subject: [PATCH] BBSRAM: add another partition and store the last ulog path --- nuttx-configs/aerocore/nsh/defconfig | 2 +- nuttx-configs/aerofc-v1/nsh/defconfig | 2 +- nuttx-configs/auav-x21/nsh/defconfig | 2 +- nuttx-configs/crazyflie/nsh/defconfig | 2 +- nuttx-configs/mindpx-v2/nsh/defconfig | 2 +- .../px4-stm32f4discovery/nsh/defconfig | 2 +- nuttx-configs/px4fmu-v1/nsh/defconfig | 2 +- nuttx-configs/px4fmu-v2/nsh/defconfig | 2 +- nuttx-configs/px4fmu-v3/nsh/defconfig | 2 +- nuttx-configs/px4fmu-v4/nsh/defconfig | 2 +- nuttx-configs/px4fmu-v4pro/nsh/defconfig | 2 +- nuttx-configs/tap-v1/nsh/defconfig | 2 +- src/modules/logger/log_writer_file.cpp | 42 ++++++++++++------- src/modules/systemlib/hardfault_log.h | 40 ++++++++++++++---- src/systemcmds/hardfault_log/hardfault_log.c | 29 +++++++++++++ 15 files changed, 102 insertions(+), 33 deletions(-) diff --git a/nuttx-configs/aerocore/nsh/defconfig b/nuttx-configs/aerocore/nsh/defconfig index 229bff14b7..190970cd7f 100644 --- a/nuttx-configs/aerocore/nsh/defconfig +++ b/nuttx-configs/aerocore/nsh/defconfig @@ -428,7 +428,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set # CONFIG_STM32_BBSRAM is not set -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 # CONFIG_STM32_SAVE_CRASHDUMP is not set # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/aerofc-v1/nsh/defconfig b/nuttx-configs/aerofc-v1/nsh/defconfig index eb91405028..f65d1f738c 100644 --- a/nuttx-configs/aerofc-v1/nsh/defconfig +++ b/nuttx-configs/aerofc-v1/nsh/defconfig @@ -529,7 +529,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set # CONFIG_STM32_BBSRAM is not set -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 # CONFIG_STM32_SAVE_CRASHDUMP is not set # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/auav-x21/nsh/defconfig b/nuttx-configs/auav-x21/nsh/defconfig index 61bea69b49..ddc93a754e 100644 --- a/nuttx-configs/auav-x21/nsh/defconfig +++ b/nuttx-configs/auav-x21/nsh/defconfig @@ -542,7 +542,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/crazyflie/nsh/defconfig b/nuttx-configs/crazyflie/nsh/defconfig index b8a5251845..0abc83e9b9 100644 --- a/nuttx-configs/crazyflie/nsh/defconfig +++ b/nuttx-configs/crazyflie/nsh/defconfig @@ -507,7 +507,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/mindpx-v2/nsh/defconfig b/nuttx-configs/mindpx-v2/nsh/defconfig index 5c432680a3..c9861e29bc 100644 --- a/nuttx-configs/mindpx-v2/nsh/defconfig +++ b/nuttx-configs/mindpx-v2/nsh/defconfig @@ -551,7 +551,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4-stm32f4discovery/nsh/defconfig b/nuttx-configs/px4-stm32f4discovery/nsh/defconfig index f18fea1fdf..7b538b4891 100644 --- a/nuttx-configs/px4-stm32f4discovery/nsh/defconfig +++ b/nuttx-configs/px4-stm32f4discovery/nsh/defconfig @@ -518,7 +518,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4fmu-v1/nsh/defconfig b/nuttx-configs/px4fmu-v1/nsh/defconfig index 7010c79fbd..c5baecda53 100644 --- a/nuttx-configs/px4fmu-v1/nsh/defconfig +++ b/nuttx-configs/px4fmu-v1/nsh/defconfig @@ -517,7 +517,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set # CONFIG_STM32_BBSRAM is not set -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 # CONFIG_STM32_SAVE_CRASHDUMP is not set # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4fmu-v2/nsh/defconfig b/nuttx-configs/px4fmu-v2/nsh/defconfig index c154ab0e65..2a57809a96 100644 --- a/nuttx-configs/px4fmu-v2/nsh/defconfig +++ b/nuttx-configs/px4fmu-v2/nsh/defconfig @@ -542,7 +542,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4fmu-v3/nsh/defconfig b/nuttx-configs/px4fmu-v3/nsh/defconfig index c154ab0e65..2a57809a96 100644 --- a/nuttx-configs/px4fmu-v3/nsh/defconfig +++ b/nuttx-configs/px4fmu-v3/nsh/defconfig @@ -542,7 +542,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4fmu-v4/nsh/defconfig b/nuttx-configs/px4fmu-v4/nsh/defconfig index 34e3277bfa..f3f4d51c7f 100644 --- a/nuttx-configs/px4fmu-v4/nsh/defconfig +++ b/nuttx-configs/px4fmu-v4/nsh/defconfig @@ -535,7 +535,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/px4fmu-v4pro/nsh/defconfig b/nuttx-configs/px4fmu-v4pro/nsh/defconfig index bbad739cae..2594d00b3b 100644 --- a/nuttx-configs/px4fmu-v4pro/nsh/defconfig +++ b/nuttx-configs/px4fmu-v4pro/nsh/defconfig @@ -546,7 +546,7 @@ CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_SDIO_DMAPRIO=0x00010000 CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/nuttx-configs/tap-v1/nsh/defconfig b/nuttx-configs/tap-v1/nsh/defconfig index 083cc6e225..d815cb4af1 100644 --- a/nuttx-configs/tap-v1/nsh/defconfig +++ b/nuttx-configs/tap-v1/nsh/defconfig @@ -524,7 +524,7 @@ CONFIG_STM32_I2CTIMEOMS=10 CONFIG_STM32_I2CTIMEOTICKS=10 # CONFIG_STM32_I2C_DUTY16_9 is not set CONFIG_STM32_BBSRAM=y -CONFIG_STM32_BBSRAM_FILES=4 +CONFIG_STM32_BBSRAM_FILES=5 CONFIG_STM32_SAVE_CRASHDUMP=y # CONFIG_STM32_HAVE_RTC_COUNTER is not set # CONFIG_STM32_HAVE_RTC_SUBSECONDS is not set diff --git a/src/modules/logger/log_writer_file.cpp b/src/modules/logger/log_writer_file.cpp index 5f977ee38a..e41d1a5933 100644 --- a/src/modules/logger/log_writer_file.cpp +++ b/src/modules/logger/log_writer_file.cpp @@ -38,6 +38,9 @@ #include #include +#ifdef __PX4_NUTTX +#include +#endif /* __PX4_NUTTX */ namespace px4 { @@ -87,26 +90,37 @@ void LogWriterFile::start_log(const char *filename) PX4_ERR("Can't open log file %s, errno: %d", filename, errno); _should_run = false; return; + } - } else { + if (_buffer == nullptr) { + _buffer = new uint8_t[_buffer_size]; if (_buffer == nullptr) { - _buffer = new uint8_t[_buffer_size]; - - if (_buffer == nullptr) { - PX4_ERR("Can't create log buffer"); - ::close(_fd); - _fd = -1; - _should_run = false; - return; - } + PX4_ERR("Can't create log buffer"); + ::close(_fd); + _fd = -1; + _should_run = false; + return; } - - PX4_INFO("Opened log file: %s", filename); - _should_run = true; - _running = true; } +#ifdef __PX4_NUTTX + // register the current file with the hardfault handler: if the system crashes, + // the hardfault handler will append the crash log to that file on the next reboot. + // Note that we don't deregister it when closing the log, so that crashes after disarming + // are appended as well (the same holds for crashes before arming, which can be a bit misleading) + int ret = hardfault_store_ulog_filename(filename); + + if (ret) { + PX4_ERR("Failed to register ULog file to the hardfault handler (%i)", ret); + } + +#endif /* __PX4_NUTTX */ + + PX4_INFO("Opened log file: %s", filename); + _should_run = true; + _running = true; + // Clear buffer and counters _head = 0; _count = 0; diff --git a/src/modules/systemlib/hardfault_log.h b/src/modules/systemlib/hardfault_log.h index a80245a9a9..d7e3ee03fa 100644 --- a/src/modules/systemlib/hardfault_log.h +++ b/src/modules/systemlib/hardfault_log.h @@ -37,20 +37,25 @@ ****************************************************************************/ #include #include +#include /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define HARDFAULT_FILENO 3 -#define HARDFAULT_PATH BBSRAM_PATH""STRINGIFY(HARDFAULT_FILENO) #define HARDFAULT_REBOOT_FILENO 0 -#define HARDFAULT_REBOOT_PATH BBSRAM_PATH""STRINGIFY(HARDFAULT_REBOOT_FILENO) +#define HARDFAULT_REBOOT_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_REBOOT_FILENO) +#define HARDFAULT_ULOG_FILENO 3 +#define HARDFAULT_ULOG_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_ULOG_FILENO) +#define HARDFAULT_FILENO 4 +#define HARDFAULT_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_FILENO) +#define HARDFAULT_MAX_ULOG_FILE_LEN 64 /* must be large enough to store the full path to the log file */ #define BBSRAM_SIZE_FN0 (sizeof(int)) #define BBSRAM_SIZE_FN1 384 /* greater then 2.5 times the size of vehicle_status_s */ #define BBSRAM_SIZE_FN2 384 /* greater then 2.5 times the size of vehicle_status_s */ -#define BBSRAM_SIZE_FN3 -1 +#define BBSRAM_SIZE_FN3 HARDFAULT_MAX_ULOG_FILE_LEN +#define BBSRAM_SIZE_FN4 -1 /* The following guides in the amount of the user and interrupt stack * data we can save. The amount of storage left will dictate the actual @@ -58,7 +63,7 @@ * It will be truncated by the call to stm32_bbsram_savepanic */ #define BBSRAM_HEADER_SIZE 20 /* This is an assumption */ -#define BBSRAM_USED ((4*BBSRAM_HEADER_SIZE)+(BBSRAM_SIZE_FN0+BBSRAM_SIZE_FN1+BBSRAM_SIZE_FN2)) +#define BBSRAM_USED ((4*BBSRAM_HEADER_SIZE)+(BBSRAM_SIZE_FN0+BBSRAM_SIZE_FN1+BBSRAM_SIZE_FN2+BBSRAM_SIZE_FN3)) #define BBSRAM_REAMINING (PX4_BBSRAM_SIZE-BBSRAM_USED) #if CONFIG_ARCH_INTERRUPTSTACK <= 3 # define BBSRAM_NUMBER_STACKS 1 @@ -77,8 +82,9 @@ #define BSRAM_FILE_SIZES { \ BBSRAM_SIZE_FN0, /* For Time stamp only */ \ BBSRAM_SIZE_FN1, /* For Current Flight Parameters Copy A */ \ - BBSRAM_SIZE_FN2, /* For Current Flight Parameters Copy B*/ \ - BBSRAM_SIZE_FN3, /* For the Panic Log use rest of space */ \ + BBSRAM_SIZE_FN2, /* For Current Flight Parameters Copy B */ \ + BBSRAM_SIZE_FN3, /* For the latest ULog file path */ \ + BBSRAM_SIZE_FN4, /* For the Panic Log use rest of space */ \ 0 /* End of table marker */ \ } @@ -350,4 +356,24 @@ int hardfault_rearm(char *caller) weak_function; ****************************************************************************/ int hardfault_increment_reboot(char *caller, bool reset) weak_function; + +/**************************************************************************** + * Name: hardfault_store_ulog_filename + * + * Description: + * Permanently store the current ulog file name. A crash dump will + * be appended to this file (must be ULog). + * + * + * Inputs: + * - log_file: full path to the log file + * + * Returned Value: + * + * OK or errno + * + * + ****************************************************************************/ +int hardfault_store_ulog_filename(const char *log_file); + __END_DECLS diff --git a/src/systemcmds/hardfault_log/hardfault_log.c b/src/systemcmds/hardfault_log/hardfault_log.c index 47a3ab6921..761152fa0f 100644 --- a/src/systemcmds/hardfault_log/hardfault_log.c +++ b/src/systemcmds/hardfault_log/hardfault_log.c @@ -910,6 +910,35 @@ __EXPORT int hardfault_write(char *caller, int fd, int format, bool rearm) return ret; } +int hardfault_store_ulog_filename(const char *log_file) +{ + int fd = open(HARDFAULT_ULOG_PATH, O_TRUNC | O_WRONLY | O_CREAT); + + if (fd < 0) { + return -errno; + } + + int n = strlen(log_file); + + if (n >= HARDFAULT_MAX_ULOG_FILE_LEN) { + syslog(LOG_INFO, "ULog file name too long (%s, %i>=%i)\n", log_file, n, HARDFAULT_MAX_ULOG_FILE_LEN); + return -EINVAL; + } + + if (n + 1 != write(fd, log_file, n + 1)) { + close(fd); + return -errno; + } + + int ret = close(fd); + + if (ret != 0) { + return -errno; + } + + return 0; +} + /**************************************************************************** * Name: hardfault_log_main ****************************************************************************/