]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
add metrics counter for BUG() reached
authortrinity-1686a <trinity@deuxfleurs.fr>
Wed, 30 Aug 2023 16:50:58 +0000 (18:50 +0200)
committertrinity-1686a <trinity@deuxfleurs.fr>
Tue, 12 Sep 2023 21:10:13 +0000 (23:10 +0200)
src/lib/log/.may_include
src/lib/log/log.c
src/lib/log/log_sys.c
src/lib/log/util_bug.c
src/lib/log/util_bug.h

index 54d96324db2791e9a2188e8307bb97bfdd8ce0ab..09e2b90282f503ac45a3a8f7be77a58392dd3282 100644 (file)
@@ -9,8 +9,10 @@ lib/intmath/*.h
 lib/lock/*.h
 lib/log/*.h
 lib/malloc/*.h
+lib/metrics/*.h
 lib/string/*.h
 lib/subsys/*.h
 lib/testsupport/*.h
+lib/thread/threads.h
 lib/version/*.h
 lib/wallclock/*.h
index db57ee61a26f358a7d12ebf93507e7b61f431ff3..7ee4ce86f9ae2a6c832d5680e983aeb55e018e02 100644 (file)
@@ -33,6 +33,7 @@
 #define LOG_PRIVATE
 #include "lib/log/log.h"
 #include "lib/log/log_sys.h"
+#include "lib/log/util_bug.h"
 #include "lib/version/git_revision.h"
 #include "lib/log/ratelim.h"
 #include "lib/lock/compat_mutex.h"
@@ -912,6 +913,7 @@ init_logging(int disable_startup_queue)
 {
   if (!log_mutex_initialized) {
     tor_mutex_init(&log_mutex);
+    tor_bug_init_counter();
     log_mutex_initialized = 1;
   }
 #ifdef __GNUC__
index 33609f52889854dd90d1418d4925898d37cfc2e6..9bae36e1d1ff0019521b48ec71c1df21bd94df33 100644 (file)
 #include "lib/log/escape.h"
 #include "lib/log/log.h"
 #include "lib/log/log_sys.h"
+#include "lib/log/util_bug.h"
+#include "lib/metrics/metrics_store.h"
+
+static metrics_store_t *the_store;
 
 static int
 subsys_logging_initialize(void)
 {
   init_logging(0);
+  the_store = metrics_store_new();
   return 0;
 }
 
@@ -26,6 +31,29 @@ subsys_logging_shutdown(void)
   escaped(NULL);
 }
 
+static const smartlist_t *
+logging_metrics_get_stores(void)
+{
+  static smartlist_t *stores_list = NULL;
+
+  metrics_store_reset(the_store);
+
+  metrics_store_entry_t *sentry = metrics_store_add(
+      the_store,
+      METRICS_TYPE_COUNTER,
+      METRICS_NAME(bug_reached),
+      "Total number of BUG() and similar assertion reached",
+      0, NULL);
+  metrics_store_entry_update(sentry, tor_bug_get_count());
+
+  if (!stores_list) {
+    stores_list = smartlist_new();
+    smartlist_add(stores_list, the_store);
+  }
+
+  return stores_list;
+}
+
 const subsys_fns_t sys_logging = {
   .name = "log",
   SUBSYS_DECLARE_LOCATION(),
@@ -35,4 +63,5 @@ const subsys_fns_t sys_logging = {
   .level = -90,
   .initialize = subsys_logging_initialize,
   .shutdown = subsys_logging_shutdown,
+  .get_metrics = logging_metrics_get_stores,
 };
index 34b41324afac7dbc7f4ce5358264bb4dc7c301ff..d2dbb0a7a5b26d34aa28b0c7eed5434932881b04 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 #include "lib/malloc/malloc.h"
 #include "lib/string/printf.h"
+#include "lib/thread/threads.h"
 
 #include <string.h>
 #include <stdlib.h>
@@ -101,6 +102,27 @@ tor_assertion_failed_(const char *fname, unsigned int line,
   tor_free(buf);
 }
 
+static atomic_counter_t total_bug_reached;
+
+void
+tor_bug_init_counter(void)
+{
+  atomic_counter_init(&total_bug_reached);
+}
+
+/** Helper to update BUG count in metrics. */
+void
+tor_bug_increment_count_(void)
+{
+  atomic_counter_add(&total_bug_reached, 1);
+}
+
+size_t
+tor_bug_get_count(void)
+{
+  return atomic_counter_get(&total_bug_reached);
+}
+
 /** Helper for tor_assert_nonfatal: report the assertion failure. */
 void
 tor_bug_occurred_(const char *fname, unsigned int line,
@@ -110,6 +132,11 @@ tor_bug_occurred_(const char *fname, unsigned int line,
   char *buf = NULL;
   const char *once_str = once ?
     " (Future instances of this warning will be silenced.)": "";
+  if (! once) {
+    // _once assertions count from the macro directly so we count them as many
+    // time as they are reached, and not just once.
+    tor_bug_increment_count_();
+  }
   if (! expr) {
     if (capturing_bugs()) {
       add_captured_bug("This line should not have been reached.");
index dd82981e089d619b2ba789bbed1b5f12c2af47cb..5ea198f7ffc1c2fe83b0990fe1d71e8db6f04d17 100644 (file)
@@ -39,6 +39,7 @@
 #include "orconfig.h"
 #include "lib/cc/compat_compiler.h"
 #include "lib/log/log.h"
+#include "lib/smartlist_core/smartlist_core.h"
 #include "lib/testsupport/testsupport.h"
 
 /* Replace assert() with a variant that sends failures to the log before
   STMT_END
 #define tor_assert_nonfatal_unreached_once() STMT_BEGIN                 \
   static int warning_logged__ = 0;                                      \
+  tor_bug_increment_count_();                                           \
   if (!warning_logged__) {                                              \
     warning_logged__ = 1;                                               \
     tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, NULL, 1, NULL); \
   STMT_END
 #define tor_assert_nonfatal_once(cond) STMT_BEGIN                       \
   static int warning_logged__ = 0;                                      \
-  if (ASSERT_PREDICT_LIKELY_(cond)) {                                   \
-  } else if (!warning_logged__) {                                       \
-    warning_logged__ = 1;                                               \
-    tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1, NULL);\
+  if (!ASSERT_PREDICT_LIKELY_(cond)) {                                   \
+    tor_bug_increment_count_();                                         \
+    if (!warning_logged__) {                                            \
+      warning_logged__ = 1;                                             \
+      tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1, NULL);\
+    }                                                                   \
   }                                                                     \
   STMT_END
 #define BUG(cond)                                                       \
   if (( {                                                               \
       static int var = 0;                                               \
       int bool_result = !!(cond);                                       \
-      if (bool_result && !var) {                                        \
-        var = 1;                                                        \
-        tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__,             \
-                          ("!("#cond")"), 1, NULL);                     \
+      if (bool_result) {                                                \
+        tor_bug_increment_count_();                                     \
+        if (!var) {                                                     \
+          var = 1;                                                      \
+          tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__,           \
+                            ("!("#cond")"), 1, NULL);                   \
+        }                                                               \
       }                                                                 \
       bool_result; } ))
 #else /* !defined(__GNUC__) */
 #define IF_BUG_ONCE__(cond,var)                                         \
   static int var = 0;                                                   \
   if ((cond) ?                                                          \
-      (var ? 1 :                                                        \
+      (var ? (tor_bug_increment_count_(), 1) :                          \
        (var=1,                                                          \
+        tor_bug_increment_count_(),                                     \
         tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__,             \
                           ("!("#cond")"), 1, NULL),                     \
         1))                                                             \
@@ -273,12 +281,15 @@ void tor_assertion_failed_(const char *fname, unsigned int line,
                            const char *func, const char *expr,
                            const char *fmt, ...)
     CHECK_PRINTF(5,6);
+void tor_bug_increment_count_(void);
+size_t tor_bug_get_count(void);
 void tor_bug_occurred_(const char *fname, unsigned int line,
                        const char *func, const char *expr,
                        int once, const char *fmt, ...)
   CHECK_PRINTF(6,7);
 
 void tor_abort_(void) ATTR_NORETURN;
+void tor_bug_init_counter(void);
 
 #ifdef _WIN32
 #define SHORT_FILE__ (tor_fix_source_file(__FILE__))