Re-implement BT_ASSERT without using the assert macro
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 1 Apr 2019 15:55:33 +0000 (11:55 -0400)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 2 May 2019 20:50:15 +0000 (20:50 +0000)
BT_ASSERT is currently defined using the assert macro.  If NDEBUG is
inadvertently defined, BT_ASSERT has no effect.  This is quite
unfortunate, since a user who has defined BABELTRACE_DEBUG_MODE to 1
probably wants BT_ASSERT to be effective.

This problem was encountered while building the Python bindings, where
the distutils native code builder is passing -DNDEBUG to the compiler.
The BT_ASSERT macro usages in these files were found to be ineffective.

This patch avoids this situation by making BT_ASSERT call our own
handler if the assertion fails.  This also has the advantage of letting
us personalize the behavior on assertion failures.  The presence of the
so-called Lenny Face having absolutely no regards for the furniture is a
good example of this (and was a requirement coming from Philippe
Proulx).

Removing the inclusion of assert.h in assert-internal.h revealed a few
spots that use the assert macro without including assert.h, so they were
adjusted accordingly.

Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
cli/Makefile.am
common/Makefile.am
common/assert.c [new file with mode: 0644]
include/babeltrace/assert-internal.h
logging/log.c
plugins/ctf/common/metadata/ctf-meta-update-in-ir.c
tests/utils/tap/tap.c

index 8993c2f880a24a6ba095fc90cd0146d126e5e363..ed309d026b2edbae589c175fcbfbc902c8b3278e 100644 (file)
@@ -89,6 +89,8 @@ babeltrace_CFLAGS =   $(AM_CFLAGS) -DBT_SET_DEFAULT_IN_TREE_CONFIGURATION
 babeltrace_log_bin_SOURCES = babeltrace-log.c
 babeltrace_log_bin_LDADD = \
        $(top_builddir)/compat/libcompat.la \
+       $(top_builddir)/common/libbabeltrace-common.la \
+       $(top_builddir)/logging/libbabeltrace-logging.la \
        $(POPT_LIBS)
 babeltrace_log_bin_CFLAGS = $(AM_CFLAGS) '-DBT_CLI_PATH="$(abs_top_builddir)/cli/babeltrace$(EXEEXT)"'
 
index 0584b1c8076a1cfd4cf7ea2164ab449644a13b1d..ce0b661a81bece453b0a906628f1a6fede027e8f 100644 (file)
@@ -3,4 +3,4 @@ AM_CPPFLAGS += -DINSTALL_LIBDIR=\"$(libdir)\"
 
 noinst_LTLIBRARIES = libbabeltrace-common.la
 
-libbabeltrace_common_la_SOURCES = common.c logging.c logging.h
+libbabeltrace_common_la_SOURCES = assert.c common.c logging.c logging.h
diff --git a/common/assert.c b/common/assert.c
new file mode 100644 (file)
index 0000000..d02e911
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2019 EfficiOS Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/common-internal.h>
+
+void bt_common_assert_failed(
+       const char *file, int line, const char *func, const char *assertion) {
+       bt_common_color_bold();
+       bt_common_color_fg_red();
+       fprintf(stderr,
+               "%s%s:%d: %s: Assertion %s`%s`%s failed. (╯ ͡°  □ ͡°)╯︵ ┻━┻%s\n",
+               bt_common_color_bold(), file, line, func,
+               bt_common_color_fg_red(), assertion,
+               bt_common_color_fg_default(), bt_common_color_reset());
+       abort();
+}
\ No newline at end of file
index 431230c91d9f145e1e9f0f86ef99cdaef963eab9..f8fd0b922cf21a57d1f32be9c202deadd54e1dae 100644 (file)
 #include <babeltrace/babeltrace-internal.h>
 
 #ifdef BT_DEBUG_MODE
+
+extern void bt_common_assert_failed(const char *file, int line,
+       const char *func, const char *assertion) __attribute__((noreturn));
+
 /*
  * Internal assertion (to detect logic errors on which the library user
  * has no influence). Use BT_ASSERT_PRE() to check a precondition which
  * must be directly or indirectly satisfied by the library user.
  */
-# define BT_ASSERT(_cond)      do { assert(_cond); } while (0)
+#define BT_ASSERT(_cond)                                                       \
+       do {                                                                   \
+               if (!(_cond)) {                                                \
+                       bt_common_assert_failed(__FILE__, __LINE__, __func__,  \
+                               TOSTRING(_cond));                              \
+               }                                                              \
+       } while (0)
 
 /*
  * Marks a function as being only used within a BT_ASSERT() context.
index f2fa0cb66373eabaa492ff49e753d50ab86bd893..0d317f031c27774fc96ac4514e7dc7342d961f6a 100644 (file)
@@ -7,6 +7,7 @@
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/common-internal.h>
 #include <pthread.h>
+#include <assert.h>
 
 #ifdef __CYGWIN__
 extern unsigned long pthread_getsequence_np(pthread_t *);
index 5ad71c85ba9b1fae84248c39df1518a6322e6cdd..5048c53a4e5355cd97ec125f2528c25accf32df2 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <inttypes.h>
+#include <assert.h>
 
 #include "ctf-meta-visitors.h"
 
index d3d3f3df8a4db955eea39d225f4c9c7ed13a800a..5699f387015b9610abda9ec75bd170929492bf97 100644 (file)
@@ -29,9 +29,9 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <babeltrace/assert-internal.h>
 #include <string.h>
 #include <limits.h>
+#include <assert.h>
 
 #include "tap.h"
 
@@ -304,7 +304,7 @@ diag_multiline(const char *val)
 {
        size_t len, i, line_start_idx = 0;
 
-       BT_ASSERT(val);
+       assert(val);
        len = strlen(val);
 
        for (i = 0; i < len; i++) {
@@ -314,7 +314,7 @@ diag_multiline(const char *val)
                        continue;
                }
 
-               BT_ASSERT((i - line_start_idx + 1) <= INT_MAX);
+               assert((i - line_start_idx + 1) <= INT_MAX);
                line_length = i - line_start_idx + 1;
                fprintf(stderr, "# %.*s", line_length, &val[line_start_idx]);
                line_start_idx = i + 1;
This page took 0.028047 seconds and 4 git commands to generate.