From 4832c5ab33cbff8148933be71d1ea24572ac639b Mon Sep 17 00:00:00 2001 From: buxiasen Date: Thu, 19 Sep 2024 00:09:53 +0800 Subject: [PATCH 1/4] system/coredump: compatible restore name with NAME_MAX 32 Bytes Coredump should compatible with default 32B NAME_MAX Signed-off-by: buxiasen --- system/coredump/coredump.c | 52 +++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/system/coredump/coredump.c b/system/coredump/coredump.c index 0d4b0fd895..4579bca7f5 100644 --- a/system/coredump/coredump.c +++ b/system/coredump/coredump.c @@ -43,6 +43,14 @@ * Private Types ****************************************************************************/ +#ifdef CONFIG_BOARD_COREDUMP_COMPRESSION +# define COREDUMP_FILE_SUFFIX ".lzf" +#else +# define COREDUMP_FILE_SUFFIX ".core" +#endif + +#define COREDUMP_FILE_SUFFIX_LEN (sizeof(COREDUMP_FILE_SUFFIX) - 1) + typedef CODE void (*dumpfile_cb_t)(FAR char *path, FAR const char *filename, FAR void *arg); @@ -66,6 +74,22 @@ static struct memory_region_s g_memory_region[] = ****************************************************************************/ #ifdef CONFIG_BOARD_COREDUMP_BLKDEV + +static bool dumpfile_is_valid(FAR const char *name) +{ + FAR const char *suffix; + size_t name_len; + + name_len = strlen(name); + if (name_len < COREDUMP_FILE_SUFFIX_LEN) + { + return false; + } + + suffix = name + name_len - COREDUMP_FILE_SUFFIX_LEN; + return !!memcmp(suffix, COREDUMP_FILE_SUFFIX, COREDUMP_FILE_SUFFIX_LEN); +} + static int dumpfile_iterate(FAR char *path, dumpfile_cb_t cb, FAR void *arg) { FAR struct dirent *entry; @@ -84,7 +108,7 @@ static int dumpfile_iterate(FAR char *path, dumpfile_cb_t cb, FAR void *arg) while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == DT_REG && !strncmp(entry->d_name, "core-", 5)) + if (entry->d_type == DT_REG && dumpfile_is_valid(entry->d_name)) { cb(path, entry->d_name, arg); } @@ -132,16 +156,11 @@ static void dumpfile_delete(FAR char *path, FAR const char *filename, static void coredump_restore(FAR char *savepath, size_t maxfile) { FAR struct coredump_info_s *info; - char dumppath[PATH_MAX] = - { - 0 - }; - + char dumppath[PATH_MAX]; unsigned char *swap; struct geometry geo; ssize_t writesize; ssize_t readsize; - struct tm *dtime; size_t offset = 0; size_t max = 0; int dumpfd; @@ -203,22 +222,9 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) } ret = snprintf(dumppath, sizeof(dumppath), - "%s/core-%s", savepath, - info->name.version); - dtime = localtime(&info->time.tv_sec); - if (dtime) - { - ret += snprintf(dumppath + ret, sizeof(dumppath) - ret, - "-%d-%d-%d-%d-%d", dtime->tm_mon + 1, - dtime->tm_mday, dtime->tm_hour, - dtime->tm_min, dtime->tm_sec); - } - -#ifdef CONFIG_BOARD_COREDUMP_COMPRESSION - ret += snprintf(dumppath + ret, sizeof(dumppath) - ret, ".lzf"); -#else - ret += snprintf(dumppath + ret, sizeof(dumppath) - ret, ".core"); -#endif + "%s/%.16s-%x"COREDUMP_FILE_SUFFIX, + savepath, info->name.version, + (unsigned int)info->time.tv_sec); while (ret--) { From ee473ee7ce4e8aec1502458dc0c8033cf2621005 Mon Sep 17 00:00:00 2001 From: buxiasen Date: Wed, 30 Oct 2024 21:12:02 +0800 Subject: [PATCH 2/4] system/coredump: coredump restore independ and support mtd Signed-off-by: buxiasen --- system/coredump/Kconfig | 21 +++++++-- system/coredump/coredump.c | 91 ++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 57 deletions(-) diff --git a/system/coredump/Kconfig b/system/coredump/Kconfig index 1ddeaf0cdb..c6e74964be 100644 --- a/system/coredump/Kconfig +++ b/system/coredump/Kconfig @@ -22,10 +22,23 @@ config SYSTEM_COREDUMP_PRIORITY ---help--- This is the task priority that will be used when starting the coredump. -config SYSTEM_COREDUMP_SWAPBUFFER_NUMS - int "coredump read/write swap buffer nums" - default 64 +config SYSTEM_COREDUMP_SWAPBUFFER_SIZE + int "coredump read/write swap buffer size" + default 16384 ---help--- - The coredump read/write swap buffer nums. swap buffer size = sectorsize * nums + The coredump read/write swap buffer size in bytes. + +config SYSTEM_COREDUMP_RESTORE + bool "coredump restore from block/mtd device to filesystem" + default BOARD_COREDUMP_BLKDEV || BOARD_COREDUMP_MTDDEV + ---help--- + Userspace restore from block/mtd device to specific filesystem path. + +config SYSTEM_COREDUMP_DEVPATH + string "coredump block device path to restore" + depends on SYSTEM_COREDUMP_RESTORE + default BOARD_COREDUMP_DEVPATH if BOARD_COREDUMP_BLKDEV || BOARD_COREDUMP_MTDDEV + ---help--- + This is the block device path to restore. endif # SYSTEM_COREDUMP diff --git a/system/coredump/coredump.c b/system/coredump/coredump.c index 4579bca7f5..0b7c9b7ba0 100644 --- a/system/coredump/coredump.c +++ b/system/coredump/coredump.c @@ -73,7 +73,7 @@ static struct memory_region_s g_memory_region[] = * dumpfile_iterate ****************************************************************************/ -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE static bool dumpfile_is_valid(FAR const char *name) { @@ -155,10 +155,9 @@ static void dumpfile_delete(FAR char *path, FAR const char *filename, static void coredump_restore(FAR char *savepath, size_t maxfile) { - FAR struct coredump_info_s *info; + struct coredump_info_s info; char dumppath[PATH_MAX]; unsigned char *swap; - struct geometry geo; ssize_t writesize; ssize_t readsize; size_t offset = 0; @@ -168,48 +167,36 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) off_t off; int ret; - blkfd = open(CONFIG_BOARD_COREDUMP_BLKDEV_PATH, O_RDWR); + blkfd = open(CONFIG_SYSTEM_COREDUMP_DEVPATH, O_RDWR); if (blkfd < 0) { return; } - ret = ioctl(blkfd, BIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo)); - if (ret < 0) + off = lseek(blkfd, -(off_t)sizeof(info), SEEK_END); + if (off < 0) { + printf("Seek %s fail\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); goto blkfd_err; } - info = malloc(geo.geo_sectorsize); - if (info == NULL) + readsize = read(blkfd, &info, sizeof(info)); + if (readsize != sizeof(info)) { + printf("Read %s fail\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); goto blkfd_err; } - off = lseek(blkfd, (geo.geo_nsectors - 1) * geo.geo_sectorsize, SEEK_SET); - if (off < 0) + if (info.magic != COREDUMP_MAGIC) { - printf("Seek %s fail\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); - goto info_err; - } - - readsize = read(blkfd, info, geo.geo_sectorsize); - if (readsize != geo.geo_sectorsize) - { - printf("Read %s fail\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); - goto info_err; - } - - if (info->magic != COREDUMP_MAGIC) - { - printf("%s coredump not found!\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); - goto info_err; + printf("%s coredump not found!\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); + goto blkfd_err; } ret = dumpfile_iterate(savepath, dumpfile_count, &max); if (ret < 0) { - goto info_err; + goto blkfd_err; } if (max >= maxfile) @@ -217,14 +204,16 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) ret = dumpfile_iterate(savepath, dumpfile_delete, dumppath); if (ret < 0) { - goto info_err; + goto blkfd_err; } } + /* 'date -d @$(printf "%d" 0x6720C67E)' restore utc to date */ + ret = snprintf(dumppath, sizeof(dumppath), "%s/%.16s-%x"COREDUMP_FILE_SUFFIX, - savepath, info->name.version, - (unsigned int)info->time.tv_sec); + savepath, info.name.version, + (unsigned int)info.time.tv_sec); while (ret--) { @@ -238,11 +227,10 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) if (dumpfd < 0) { printf("Open %s fail\n", dumppath); - goto info_err; + goto blkfd_err; } - swap = malloc(geo.geo_sectorsize * - CONFIG_SYSTEM_COREDUMP_SWAPBUFFER_NUMS); + swap = malloc(CONFIG_SYSTEM_COREDUMP_SWAPBUFFER_SIZE); if (swap == NULL) { printf("Malloc fail\n"); @@ -250,13 +238,12 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) } lseek(blkfd, 0, SEEK_SET); - while (offset < info->size) + while (offset < info.size) { - readsize = read(blkfd, swap, geo.geo_sectorsize * - CONFIG_SYSTEM_COREDUMP_SWAPBUFFER_NUMS); + readsize = read(blkfd, swap, CONFIG_SYSTEM_COREDUMP_SWAPBUFFER_SIZE); if (readsize < 0) { - printf("Read %s fail\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); + printf("Read %s fail\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); break; } else if (readsize == 0) @@ -274,36 +261,34 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) offset += writesize; } - if (offset < info->size) + if (offset < info.size) { printf("Coredump error [%s] need [%zu], but just get %zu\n", - dumppath, info->size, offset); + dumppath, info.size, offset); } else { - printf("Coredump finish [%s][%zu]\n", dumppath, info->size); + printf("Coredump finish [%s][%zu]\n", dumppath, info.size); } - info->magic = 0; - off = lseek(blkfd, (geo.geo_nsectors - 1) * geo.geo_sectorsize, SEEK_SET); + info.magic = 0; + off = lseek(blkfd, -(off_t)sizeof(info), SEEK_END); if (off < 0) { - printf("Seek %s fail\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); + printf("Seek %s fail\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); goto swap_err; } - writesize = write(blkfd, info, geo.geo_sectorsize); - if (writesize != geo.geo_sectorsize) + writesize = write(blkfd, &info, sizeof(info)); + if (writesize != sizeof(info)) { - printf("Write %s fail\n", CONFIG_BOARD_COREDUMP_BLKDEV_PATH); + printf("Write %s fail\n", CONFIG_SYSTEM_COREDUMP_DEVPATH); } swap_err: free(swap); fd_err: close(dumpfd); -info_err: - free(info); blkfd_err: close(blkfd); } @@ -422,10 +407,10 @@ static void usage(FAR const char *progname, int exitcode) fprintf(stderr, "\t -p, --pid , Default, all thread\n"); fprintf(stderr, "\t -f, --filename , Default stdout\n"); -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE fprintf(stderr, "Second usage, will restore coredump" "from %s to savepath\n", - CONFIG_BOARD_COREDUMP_BLKDEV_PATH); + CONFIG_SYSTEM_COREDUMP_DEVPATH); fprintf(stderr, "\t -s, --savepath \n"); fprintf(stderr, "\t -m, --maxfile ," "Maximum number of coredump files, Default 1\n"); @@ -443,7 +428,7 @@ static void usage(FAR const char *progname, int exitcode) int main(int argc, FAR char *argv[]) { -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE FAR char *savepath = NULL; size_t maxfile = 1; #endif @@ -455,7 +440,7 @@ int main(int argc, FAR char *argv[]) { {"pid", 1, NULL, 'p'}, {"filename", 1, NULL, 'f'}, -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE {"savepath", 1, NULL, 's'}, {"maxfile", 1, NULL, 'm'}, #endif @@ -473,7 +458,7 @@ int main(int argc, FAR char *argv[]) case 'f': name = optarg; break; -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE case 's': savepath = optarg; break; @@ -488,7 +473,7 @@ int main(int argc, FAR char *argv[]) } } -#ifdef CONFIG_BOARD_COREDUMP_BLKDEV +#ifdef CONFIG_SYSTEM_COREDUMP_RESTORE if (savepath != NULL) { coredump_restore(savepath, maxfile); From f25ab524a3fcb2ab8425e027b2c41012fdbe6162 Mon Sep 17 00:00:00 2001 From: buxiasen Date: Tue, 19 Nov 2024 12:39:21 +0800 Subject: [PATCH 3/4] coredump: use llx to fix coverity report Signed-off-by: buxiasen --- system/coredump/coredump.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/coredump/coredump.c b/system/coredump/coredump.c index 0b7c9b7ba0..762fb20e28 100644 --- a/system/coredump/coredump.c +++ b/system/coredump/coredump.c @@ -211,9 +211,9 @@ static void coredump_restore(FAR char *savepath, size_t maxfile) /* 'date -d @$(printf "%d" 0x6720C67E)' restore utc to date */ ret = snprintf(dumppath, sizeof(dumppath), - "%s/%.16s-%x"COREDUMP_FILE_SUFFIX, + "%s/%.16s-%llx"COREDUMP_FILE_SUFFIX, savepath, info.name.version, - (unsigned int)info.time.tv_sec); + (unsigned long long)info.time.tv_sec); while (ret--) { From 1c4ce3c6bc0b5cbf53e8974c9f02d8e2871e4e3c Mon Sep 17 00:00:00 2001 From: buxiasen Date: Fri, 29 Nov 2024 21:26:14 +0800 Subject: [PATCH 4/4] coredump: move definition out of types Signed-off-by: buxiasen --- system/coredump/coredump.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/system/coredump/coredump.c b/system/coredump/coredump.c index 762fb20e28..1846dc9ee8 100644 --- a/system/coredump/coredump.c +++ b/system/coredump/coredump.c @@ -40,7 +40,7 @@ #include /**************************************************************************** - * Private Types + * Pre-processor Definitions ****************************************************************************/ #ifdef CONFIG_BOARD_COREDUMP_COMPRESSION @@ -51,6 +51,10 @@ #define COREDUMP_FILE_SUFFIX_LEN (sizeof(COREDUMP_FILE_SUFFIX) - 1) +/**************************************************************************** + * Private Types + ****************************************************************************/ + typedef CODE void (*dumpfile_cb_t)(FAR char *path, FAR const char *filename, FAR void *arg);