You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/09/21 23:10:54 UTC

[GitHub] ccollins476ad closed pull request #1417: sys/reboot: Add info to log entries and limit writes to one per boot

ccollins476ad closed pull request #1417: sys/reboot: Add info to log entries and limit writes to one per boot
URL: https://github.com/apache/mynewt-core/pull/1417
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/kernel/os/pkg.yml b/kernel/os/pkg.yml
index 10dc8d095a..b1dffa5f15 100644
--- a/kernel/os/pkg.yml
+++ b/kernel/os/pkg.yml
@@ -43,5 +43,8 @@ pkg.deps.BSP_SIMULATED:
 pkg.deps.OS_SYSVIEW:
     - "@apache-mynewt-core/sys/sysview"
 
+pkg.deps.OS_CRASH_LOG:
+    - "@apache-mynewt-core/sys/reboot"
+
 pkg.init:
     os_pkg_init: 0
diff --git a/kernel/os/src/arch/cortex_m4/os_fault.c b/kernel/os/src/arch/cortex_m4/os_fault.c
index 32e7c1512b..ca678b5ada 100644
--- a/kernel/os/src/arch/cortex_m4/os_fault.c
+++ b/kernel/os/src/arch/cortex_m4/os_fault.c
@@ -29,6 +29,10 @@
 #include "coredump/coredump.h"
 #endif
 
+#if MYNEWT_VAL(OS_CRASH_LOG)
+#include "reboot/log_reboot.h"
+#endif
+
 struct exception_frame {
     uint32_t r0;
     uint32_t r1;
@@ -123,6 +127,9 @@ trap_to_coredump(struct trap_frame *tf, struct coredump_regs *regs)
 void
 __assert_func(const char *file, int line, const char *func, const char *e)
 {
+#if MYNEWT_VAL(OS_CRASH_LOG)
+    struct log_reboot_info lri;
+#endif
     int sr;
 
     OS_ENTER_CRITICAL(sr);
@@ -130,6 +137,16 @@ __assert_func(const char *file, int line, const char *func, const char *e)
     console_blocking_mode();
     OS_PRINT_ASSERT(file, line, func, e);
 
+#if MYNEWT_VAL(OS_CRASH_LOG)
+    lri = (struct log_reboot_info) {
+        .reason = HAL_RESET_SOFT,
+        .file = file,
+        .line = line,
+        .pc = (uint32_t)__builtin_return_address(0),
+    };
+    log_reboot(&lri);
+#endif
+
     if (hal_debugger_connected()) {
        /*
         * If debugger is attached, breakpoint before the trap.
@@ -144,6 +161,9 @@ __assert_func(const char *file, int line, const char *func, const char *e)
 void
 os_default_irq(struct trap_frame *tf)
 {
+#if MYNEWT_VAL(OS_CRASH_LOG)
+    struct log_reboot_info lri;
+#endif
 #if MYNEWT_VAL(OS_COREDUMP)
     struct coredump_regs regs;
 #endif
@@ -164,6 +184,17 @@ os_default_irq(struct trap_frame *tf)
     console_printf("BFAR:0x%08lx MMFAR:0x%08lx\n", SCB->BFAR, SCB->MMFAR);
 
     os_stacktrace((uintptr_t)(tf->ef + 1));
+
+#if MYNEWT_VAL(OS_CRASH_LOG)
+    lri = (struct log_reboot_info) {
+        .reason = HAL_RESET_SOFT,
+        .file = NULL,
+        .line = 0,
+        .pc = tf->ef->pc,
+    };
+    log_reboot(&lri);
+#endif
+
 #if MYNEWT_VAL(OS_COREDUMP)
     trap_to_coredump(tf, &regs);
     coredump_dump(&regs, sizeof(regs));
diff --git a/kernel/os/syscfg.yml b/kernel/os/syscfg.yml
index 6e23f922f2..12e9214f9b 100644
--- a/kernel/os/syscfg.yml
+++ b/kernel/os/syscfg.yml
@@ -37,6 +37,10 @@ syscfg.defs:
     OS_CRASH_STACKTRACE:
         description: 'Attempt to print stack trace when system crashes.'
         value: 0
+    OS_CRASH_LOG:
+        description: >
+            Write an entry to the reboot log when the system crashes.
+        value: 1
     OS_WATCHDOG_MONITOR:
         description: >
             'Attempt to capture state of stuck system before HW watchdog fires.'
@@ -135,3 +139,9 @@ syscfg.vals.OS_DEBUG_MODE:
     OS_CTX_SW_STACK_CHECK: 1
     OS_MEMPOOL_CHECK: 1
     OS_MEMPOOL_POISON: 1
+
+syscfg.vals.SELFTEST:
+    # OS_CRASH_LOG depends on some APIs that are almost never present in self
+    # tests (bootloader and newtmgr).  Explicitly disable this setting in self
+    # tests.
+    OS_CRASH_LOG: 0
diff --git a/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c b/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c
index 12075d4fb0..c83ee24e21 100644
--- a/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c
+++ b/mgmt/newtmgr/nmgr_os/src/newtmgr_os.c
@@ -319,12 +319,21 @@ nmgr_reset_tmo(struct os_event *ev)
 static int
 nmgr_reset(struct mgmt_cbuf *cb)
 {
+#if MYNEWT_VAL(LOG_SOFT_RESET)
+    struct log_reboot_info info;
+#endif
     int rc;
 
     os_callout_init(&nmgr_reset_callout, mgmt_evq_get(), nmgr_reset_tmo, NULL);
 
 #if MYNEWT_VAL(LOG_SOFT_RESET)
-    log_reboot(HAL_RESET_REQUESTED);
+    info = (struct log_reboot_info) {
+        .reason = HAL_RESET_REQUESTED,
+        .file = NULL,
+        .line = 0,
+        .pc = 0,
+    };
+    log_reboot(&info);
 #endif
     os_callout_reset(&nmgr_reset_callout, OS_TICKS_PER_SEC / 4);
 
diff --git a/sys/reboot/include/reboot/log_reboot.h b/sys/reboot/include/reboot/log_reboot.h
index 556672115c..ccebdbf42c 100644
--- a/sys/reboot/include/reboot/log_reboot.h
+++ b/sys/reboot/include/reboot/log_reboot.h
@@ -19,11 +19,12 @@
 #ifndef __LOG_REBOOT_H__
 #define __LOG_REBOOT_H__
 
+#include <hal/hal_system.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#include <hal/hal_system.h>
 #define REBOOT_REASON_STR(reason)                                       \
     (reason == HAL_RESET_POR ? "HARD" :                                 \
       (reason == HAL_RESET_PIN ? "RESET_PIN" :                          \
@@ -33,7 +34,14 @@ extern "C" {
               (reason == HAL_RESET_REQUESTED ? "REQUESTED" :            \
                 "UNKNOWN"))))))
 
-int log_reboot(enum hal_reset_reason);
+struct log_reboot_info {
+    enum hal_reset_reason reason;
+    const char *file;
+    int line;
+    uint32_t pc;
+};
+
+int log_reboot(const struct log_reboot_info *info);
 void reboot_start(enum hal_reset_reason reason);
 
 extern uint16_t reboot_cnt;
diff --git a/sys/reboot/src/log_reboot.c b/sys/reboot/src/log_reboot.c
index 670eb25e8a..f4c320c1b9 100644
--- a/sys/reboot/src/log_reboot.c
+++ b/sys/reboot/src/log_reboot.c
@@ -35,13 +35,14 @@
 #endif
 
 uint16_t reboot_cnt;
-static uint16_t soft_reboot;
 static char reboot_cnt_str[12];
-static char soft_reboot_str[12];
+static char log_reboot_written_str[12];
+static int8_t log_reboot_written;
+
 static char *reboot_conf_get(int argc, char **argv, char *buf, int max_len);
 static int reboot_conf_set(int argc, char **argv, char *val);
 static int reboot_conf_export(void (*export_func)(char *name, char *val),
-                             enum conf_export_tgt tgt);
+                              enum conf_export_tgt tgt);
 
 struct conf_handler reboot_conf_handler = {
     .ch_name = "reboot",
@@ -138,10 +139,15 @@ reboot_cnt_inc(void)
  * @param reason for reboot
  * @return 0 on success; non-zero on failure
  */
-int
-log_reboot(enum hal_reset_reason reason)
+static int
+log_reboot_write(const struct log_reboot_info *info)
 {
     struct image_version ver;
+    uint8_t hash[IMGMGR_HASH_LEN];
+    char buf[MYNEWT_VAL(REBOOT_LOG_BUF_SIZE)];
+    int off;
+    int rc;
+    int i;
 
 #if MYNEWT_VAL(REBOOT_LOG_FCB)
     {
@@ -152,22 +158,66 @@ log_reboot(enum hal_reset_reason reason)
     }
 #endif
 
-    if (reason == HAL_RESET_REQUESTED) {
-        conf_save_one("reboot/soft_reboot", "1");
-    } else {
-        conf_save_one("reboot/soft_reboot", "0");
+    rc = imgr_read_info(boot_current_slot, &ver, hash, NULL);
+    if (rc != 0) {
+        return rc;
+    }
+
+    off = 0;
+    off += snprintf(buf + off, sizeof buf - off,
+                    "rsn:%s, cnt:%u, img:%u.%u.%u.%u, hash:",
+                    REBOOT_REASON_STR(info->reason), reboot_cnt, ver.iv_major,
+                    ver.iv_minor, ver.iv_revision,
+                    (unsigned int)ver.iv_build_num);
+
+    for (i = 0; i < sizeof hash; i++) {
+        off += snprintf(buf + off, sizeof buf - off, "%02x",
+                        (unsigned int)hash[i]);
+    }
+
+    if (info->file != NULL) {
+        off += snprintf(buf + off, sizeof buf - off, ", die:%s:%d",
+                info->file, info->line);
     }
 
-    if (!soft_reboot || reason != HAL_RESET_SOFT) {
-        imgr_my_version(&ver);
+    if (info->pc != 0) {
+        off += snprintf(buf + off, sizeof buf - off, ", pc:0x%lx",
+                (unsigned long)info->pc);
+    }
 
-        /* Log a reboot */
-        MODLOG_CRITICAL(LOG_MODULE_REBOOT, "rsn:%s, cnt:%u,"
-                        " img:%u.%u.%u.%u", REBOOT_REASON_STR(reason),
-                        reboot_cnt, ver.iv_major, ver.iv_minor,
-                        ver.iv_revision, (unsigned int)ver.iv_build_num);
+    /* Make sure we don't log beyond the end of the source buffer. */
+    if (off > sizeof buf) {
+        off = sizeof buf;
     }
 
+    /* Log a reboot */
+    modlog_append(LOG_MODULE_REBOOT, LOG_LEVEL_CRITICAL, LOG_ETYPE_STRING,
+                  buf, off);
+
+    return 0;
+}
+
+int
+log_reboot(const struct log_reboot_info *info)
+{
+    int rc;
+
+    /* Don't log a second reboot entry. */
+    if (log_reboot_written) {
+        return 0;
+    }
+
+    rc = log_reboot_write(info);
+    if (rc != 0) {
+        return rc;
+    }
+
+    /* Record that we have written a reboot entry for the current boot.  Upon
+     * rebooting, we won't write a second entry.
+     */
+    log_reboot_written = 1;
+    conf_save_one("reboot/written", "1");
+
     return 0;
 }
 
@@ -181,8 +231,24 @@ log_reboot(enum hal_reset_reason reason)
 void
 reboot_start(enum hal_reset_reason reason)
 {
+    struct log_reboot_info info;
+
     reboot_cnt_inc();
-    log_reboot(reason);
+
+    /* If an entry wasn't written before the previous reboot, write one now. */
+    if (!log_reboot_written) {
+        info = (struct log_reboot_info) {
+            .reason = reason,
+            .file = NULL,
+            .line = 0,
+            .pc = 0,
+        };
+        log_reboot_write(&info);
+    }
+
+    /* Record that we haven't written a reboot entry for the current boot. */
+    log_reboot_written = 0;
+    conf_save_one("reboot/written", "0");
 }
 
 static char *
@@ -192,10 +258,10 @@ reboot_conf_get(int argc, char **argv, char *buf, int max_len)
         if (!strcmp(argv[0], "reboot_cnt")) {
             return conf_str_from_value(CONF_INT16, &reboot_cnt,
                                        reboot_cnt_str, sizeof reboot_cnt_str);
-        } else if (!strcmp(argv[0], "soft_reboot")) {
-            return conf_str_from_value(CONF_INT16, &soft_reboot,
-                                       soft_reboot_str,
-                                       sizeof soft_reboot_str);
+        } else if (!strcmp(argv[0], "written")) {
+            return conf_str_from_value(CONF_BOOL, &log_reboot_written,
+                                       log_reboot_written_str,
+                                       sizeof log_reboot_written_str);
         }
     }
     return NULL;
@@ -207,8 +273,8 @@ reboot_conf_set(int argc, char **argv, char *val)
     if (argc == 1) {
         if (!strcmp(argv[0], "reboot_cnt")) {
             return CONF_VALUE_SET(val, CONF_INT16, reboot_cnt);
-        } else if (!strcmp(argv[0], "soft_reboot")) {
-            return CONF_VALUE_SET(val, CONF_INT16, soft_reboot);
+        } else if (!strcmp(argv[0], "written")) {
+            return CONF_VALUE_SET(val, CONF_INT16, log_reboot_written);
         }
     }
 
@@ -221,7 +287,7 @@ reboot_conf_export(void (*func)(char *name, char *val),
 {
     if (tgt == CONF_EXPORT_SHOW) {
         func("reboot/reboot_cnt", reboot_cnt_str);
-        func("reboot/soft_reboot", soft_reboot_str);
+        func("reboot/written", log_reboot_written_str);
     }
     return 0;
 }
diff --git a/sys/reboot/syscfg.yml b/sys/reboot/syscfg.yml
index 012e1a39de..2cc28e6e27 100644
--- a/sys/reboot/syscfg.yml
+++ b/sys/reboot/syscfg.yml
@@ -40,3 +40,9 @@ syscfg.defs:
     REBOOT_LOG_ENTRY_COUNT:
         description: 'Minimum number of reboot log entries retained'
         value: 10
+
+    REBOOT_LOG_BUF_SIZE:
+        description: >
+            The maximum size, in bytes, of a reboot log entry.  A buffer of
+            this size is allocated on the stack each time an entry is written.
+        value: 256


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services