Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Support printing backtrace when an assertion fails #330

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ endif # LZ4_ENABLED

endif # UV_ENABLED

if BACKTRACE_ENABLED
libraft_la_CFLAGS += -DRAFT_ASSERT_WITH_BACKTRACE
libraft_la_LDFLAGS += -lbacktrace
Copy link
Contributor

@MathieuBordere MathieuBordere Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilation will fail if libbacktrace is not found, probably a bit cleaner to detect it during the configure step itself, you can practically do the same as for libuv in configure.ac.

Copy link
Contributor Author

@cole-miller cole-miller Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, libbacktrace doesn't install a pkg-config entry (see ianlancetaylor/libbacktrace#65), so I'm not sure how to search for it from configure.ac. If they offered an amalgamation build I'd consider just dropping those files into our build system, but no such luck.

Copy link
Contributor

@MathieuBordere MathieuBordere Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I can live with a failure further down the road, it's a debugging feature anyway. We could look for the header file, like in liblz4's case, but I don't think it's worth the pain here.

endif # BACKTRACE_ENABLED

if EXAMPLE_ENABLED

bin_PROGRAMS += \
Expand Down
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ AM_CONDITIONAL(LZ4_AVAILABLE, test "x$have_lz4" = "xyes")
# compression by default.
AM_CONDITIONAL(LZ4_ENABLED, test "x$enable_lz4" != "xno" -a "x$have_lz4" = "xyes")

AC_ARG_ENABLE(backtrace, AS_HELP_STRING([--enable-backtrace[=ARG]], [print backtrace on assertion failure [default=no]]))
AM_CONDITIONAL(BACKTRACE_ENABLED, test "x$enable_backtrace" = "xyes")

# The fake I/O implementation and associated fixture is built by default, unless
# explicitly disabled.
AC_ARG_ENABLE(fixture, AS_HELP_STRING([--disable-fixture], [do not build the raft_fixture test helper]))
Expand Down
14 changes: 14 additions & 0 deletions src/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ extern void munit_errorf_ex(const char *filename,
do { \
(void)sizeof(x); \
} while (0)
#elif defined(RAFT_ASSERT_WITH_BACKTRACE)
#include <assert.h> /* for __assert_fail */
#include <backtrace.h>
#include <stdio.h>
#undef assert
#define assert(x) \
do { \
struct backtrace_state *state_; \
if (!(x)) { \
state_ = backtrace_create_state(NULL, 0, NULL, NULL); \
backtrace_print(state_, 0, stderr); \
__assert_fail(#x, __FILE__, __LINE__, __func__); \
} \
} while (0)
#else
#include <assert.h>
#endif
Expand Down