aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Goulet <dgoulet@torproject.org>2023-10-11 18:59:34 +0000
committerDavid Goulet <dgoulet@torproject.org>2023-10-11 18:59:34 +0000
commitdad173d3bebec03c7ab83efea5de184f92b2f591 (patch)
treecf73fb605af8bd3da27b9c95bc3da903a5bdf4cf /src
parente32a6049875a4e8746885129fc4ea1b771bb96b3 (diff)
parenteffa9418efbfe38ccf82b80f8eb1b1ce06e4a1af (diff)
downloadtor-dad173d3bebec03c7ab83efea5de184f92b2f591.tar.gz
tor-dad173d3bebec03c7ab83efea5de184f92b2f591.zip
Merge branch 'metrics-count-bug' into 'main'
add metrics counter for BUG() reached Closes #40839 See merge request tpo/core/tor!760
Diffstat (limited to 'src')
-rw-r--r--src/lib/log/.may_include2
-rw-r--r--src/lib/log/log.c2
-rw-r--r--src/lib/log/log_sys.c29
-rw-r--r--src/lib/log/util_bug.c27
-rw-r--r--src/lib/log/util_bug.h29
5 files changed, 80 insertions, 9 deletions
diff --git a/src/lib/log/.may_include b/src/lib/log/.may_include
index 54d96324db..09e2b90282 100644
--- a/src/lib/log/.may_include
+++ b/src/lib/log/.may_include
@@ -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
diff --git a/src/lib/log/log.c b/src/lib/log/log.c
index db57ee61a2..7ee4ce86f9 100644
--- a/src/lib/log/log.c
+++ b/src/lib/log/log.c
@@ -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__
diff --git a/src/lib/log/log_sys.c b/src/lib/log/log_sys.c
index 33609f5288..9bae36e1d1 100644
--- a/src/lib/log/log_sys.c
+++ b/src/lib/log/log_sys.c
@@ -11,11 +11,16 @@
#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,
};
diff --git a/src/lib/log/util_bug.c b/src/lib/log/util_bug.c
index 34b41324af..d2dbb0a7a5 100644
--- a/src/lib/log/util_bug.c
+++ b/src/lib/log/util_bug.c
@@ -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.");
diff --git a/src/lib/log/util_bug.h b/src/lib/log/util_bug.h
index dd82981e08..5ea198f7ff 100644
--- a/src/lib/log/util_bug.h
+++ b/src/lib/log/util_bug.h
@@ -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
@@ -191,6 +192,7 @@
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); \
@@ -198,10 +200,12 @@
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) \
@@ -215,18 +219,22 @@
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__))