Skip to content

Commit

Permalink
Merge pull request #5985 from grom72/fatal-w-errno
Browse files Browse the repository at this point in the history
common: introduce a separate FATAL macro with errno
  • Loading branch information
janekmi authored Jan 30, 2024
2 parents ca901b4 + b8f11e9 commit 4f341b7
Show file tree
Hide file tree
Showing 36 changed files with 186 additions and 162 deletions.
7 changes: 5 additions & 2 deletions src/common/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ util_unmap_hdr(struct pool_set_part *part)
VALGRIND_REMOVE_PMEM_MAPPING(part->hdr, part->hdrsize);
if (munmap(part->hdr, part->hdrsize) != 0)
/* this means there's a bug on the caller side */
FATAL("!munmap: %s", part->path);
CORE_LOG_FATAL_W_ERRNO("munmap: %s", part->path);
part->hdr = NULL;
part->hdrsize = 0;
}
Expand Down Expand Up @@ -2209,7 +2209,8 @@ util_poolset_append_new_part(struct pool_set *set, size_t size)
d->path, PMEM_FILE_PADDING, set->next_id, PMEM_EXT);

if (util_replica_add_part(&set->replica[r], path, size) != 0)
FATAL("cannot add a new part to the replica info");
CORE_LOG_FATAL(
"cannot add a new part to the replica info");
}

set->next_directory_id += 1;
Expand Down Expand Up @@ -3262,8 +3263,10 @@ util_replica_deep_common(const void *addr, size_t len, struct pool_set *set,
addr, len, set, replica_id, flush);

struct pool_replica *rep = set->replica[replica_id];
#ifdef DEBUG /* variables required for ASSERTs below */
uintptr_t rep_start = (uintptr_t)rep->part[0].addr;
uintptr_t rep_end = rep_start + rep->repsize;
#endif
uintptr_t start = (uintptr_t)addr;
uintptr_t end = start + len;

Expand Down
4 changes: 2 additions & 2 deletions src/common/util_pmem.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright 2017-2020, Intel Corporation */
/* Copyright 2017-2024, Intel Corporation */

/*
* util_pmem.h -- internal definitions for pmem utils
Expand All @@ -26,7 +26,7 @@ util_persist(int is_pmem, const void *addr, size_t len)
if (is_pmem)
pmem_persist(addr, len);
else if (pmem_msync(addr, len))
FATAL("!pmem_msync");
CORE_LOG_FATAL_W_ERRNO("pmem_msync");
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/core/alloc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2019-2020, Intel Corporation */
/* Copyright 2019-2024, Intel Corporation */

#include <errno.h>

Expand Down Expand Up @@ -58,7 +58,7 @@ core_inject_fault_at(enum pmem_allocation_type type, int nth, const char *at)
fail_realloc_from = at;
break;
default:
FATAL("unknown allocation type");
CORE_LOG_FATAL("unknown allocation type");
}
}

Expand Down
1 change: 0 additions & 1 deletion src/core/log_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* to syslog or to stderr
*/

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
Expand Down
18 changes: 17 additions & 1 deletion src/core/log_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#ifndef CORE_LOG_INTERNAL_H
#define CORE_LOG_INTERNAL_H

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
Expand Down Expand Up @@ -137,7 +141,19 @@ void core_log_default_function(void *context, enum core_log_level level,
CORE_LOG(CORE_LOG_LEVEL_ERROR, format, ##__VA_ARGS__)

#define CORE_LOG_FATAL(format, ...) \
CORE_LOG(CORE_LOG_LEVEL_FATAL, format, ##__VA_ARGS__)
do { \
CORE_LOG(CORE_LOG_LEVEL_FATAL, format, ##__VA_ARGS__); \
abort(); \
} while (0)

#define CORE_LOG_MAX_ERR_MSG 128
#define CORE_LOG_FATAL_W_ERRNO(format, ...) \
do { \
char buff[CORE_LOG_MAX_ERR_MSG]; \
CORE_LOG(CORE_LOG_LEVEL_FATAL, format ": %s", ##__VA_ARGS__, \
strerror_r(errno, buff, CORE_LOG_MAX_ERR_MSG)); \
abort(); \
} while (0)

#define CORE_LOG_ALWAYS(format, ...) \
CORE_LOG(CORE_LOG_LEVEL_ALWAYS, format, ##__VA_ARGS__)
Expand Down
21 changes: 2 additions & 19 deletions src/core/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ _Last_errormsg_key_alloc(void)
{
int pth_ret = os_tls_key_create(&Last_errormsg_key, free);
if (pth_ret)
FATAL("!os_thread_key_create");
CORE_LOG_FATAL_W_ERRNO("os_thread_key_create");

VALGRIND_ANNOTATE_HAPPENS_BEFORE(&Last_errormsg_key_once);
}
Expand Down Expand Up @@ -89,7 +89,7 @@ Last_errormsg_get(void)
errormsg->msg[0] = '\0';
int ret = os_tls_set(Last_errormsg_key, errormsg);
if (ret)
FATAL("!os_tls_set");
CORE_LOG_FATAL_W_ERRNO("os_tls_set");
}
return errormsg;
}
Expand Down Expand Up @@ -491,23 +491,6 @@ out_log(const char *file, int line, const char *func, int level,
va_end(ap);
}

/*
* out_fatal -- output a fatal error & die (i.e. assertion failure)
*/
void
out_fatal(const char *file, int line, const char *func,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);

out_common(file, line, func, 1, "\n", fmt, ap);

va_end(ap);

abort();
}

/*
* out_err -- output an error message
*/
Expand Down
70 changes: 24 additions & 46 deletions src/core/out.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ extern "C" {
#endif

/*
* Suppress errors which are after appropriate ASSERT* macro for nondebug
* Suppress errors messages (LOG()) in non-debug version
* builds.
*/
#if !defined(DEBUG) && (defined(__clang_analyzer__) || defined(__COVERITY__))
#define OUT_FATAL_DISCARD_NORETURN __attribute__((noreturn))
#else
#define OUT_FATAL_DISCARD_NORETURN
#endif

#ifndef EVALUATE_DBG_EXPRESSIONS
#if defined(DEBUG) || defined(__clang_analyzer__) || defined(__COVERITY__) ||\
defined(__KLOCWORK__)
Expand All @@ -43,8 +37,6 @@ extern "C" {

#define OUT_LOG out_log
#define OUT_NONL out_nonl
#define OUT_FATAL out_fatal
#define OUT_FATAL_ABORT out_fatal

#else

Expand All @@ -63,28 +55,8 @@ out_nonl_discard(int level, const char *fmt, ...)
SUPPRESS_UNUSED(level, fmt);
}

static __attribute__((always_inline)) OUT_FATAL_DISCARD_NORETURN inline void
out_fatal_discard(const char *file, int line, const char *func,
const char *fmt, ...)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(file, line, func, fmt);
}

static __attribute__((always_inline)) NORETURN inline void
out_fatal_abort(const char *file, int line, const char *func,
const char *fmt, ...)
{
/* suppress unused-parameter errors */
SUPPRESS_UNUSED(file, line, func, fmt);

abort();
}

#define OUT_LOG out_log_discard
#define OUT_NONL out_nonl_discard
#define OUT_FATAL out_fatal_discard
#define OUT_FATAL_ABORT out_fatal_abort

#endif

Expand Down Expand Up @@ -116,37 +88,33 @@ out_fatal_abort(const char *file, int line, const char *func,
OUT_NONL(level, __VA_ARGS__); \
} while (0)

/* produce output and exit */
#define FATAL(...)\
OUT_FATAL_ABORT(__FILE__, __LINE__, __func__, __VA_ARGS__)

/* assert a condition is true at runtime */
#if defined(DEBUG) || defined(__clang_analyzer__) || defined(__COVERITY__) ||\
defined(__KLOCWORK__)
#define ASSERT_rt(cnd) do { \
if (!EVALUATE_DBG_EXPRESSIONS || (cnd)) break; \
OUT_FATAL(__FILE__, __LINE__, __func__, "assertion failure: %s", #cnd);\
if ((cnd)) break; \
CORE_LOG_FATAL("assertion failure: %s", #cnd);\
} while (0)

/* assertion with extra info printed if assertion fails at runtime */
#define ASSERTinfo_rt(cnd, info) do { \
if (!EVALUATE_DBG_EXPRESSIONS || (cnd)) break; \
OUT_FATAL(__FILE__, __LINE__, __func__, \
"assertion failure: %s (%s = %s)", #cnd, #info, info);\
if ((cnd)) break; \
CORE_LOG_FATAL("assertion failure: %s (%s = %s)", #cnd, #info, info);\
} while (0)

/* assert two integer values are equal at runtime */
#define ASSERTeq_rt(lhs, rhs) do { \
if (!EVALUATE_DBG_EXPRESSIONS || ((lhs) == (rhs))) break; \
OUT_FATAL(__FILE__, __LINE__, __func__,\
"assertion failure: %s (0x%llx) == %s (0x%llx)", #lhs,\
(unsigned long long)(lhs), #rhs, (unsigned long long)(rhs)); \
if ((lhs) == (rhs)) break; \
CORE_LOG_FATAL( \
"assertion failure: %s (0x%llx) == %s (0x%llx)", #lhs, \
(unsigned long long)(lhs), #rhs, (unsigned long long)(rhs)); \
} while (0)

/* assert two integer values are not equal at runtime */
#define ASSERTne_rt(lhs, rhs) do { \
if (!EVALUATE_DBG_EXPRESSIONS || ((lhs) != (rhs))) break; \
OUT_FATAL(__FILE__, __LINE__, __func__,\
"assertion failure: %s (0x%llx) != %s (0x%llx)", #lhs,\
(unsigned long long)(lhs), #rhs, (unsigned long long)(rhs)); \
if ((lhs) != (rhs)) break; \
CORE_LOG_FATAL("assertion failure: %s (0x%llx) != %s (0x%llx)", #lhs,\
(unsigned long long)(lhs), #rhs, (unsigned long long)(rhs)); \
} while (0)

/* assert a condition is true */
Expand Down Expand Up @@ -183,6 +151,16 @@ out_fatal_abort(const char *file, int line, const char *func,
TEST_ALWAYS_NE_EXPR(lhs, rhs);\
ASSERTne_rt(lhs, rhs);\
} while (0)
#else
#define ASSERT_rt(cnd)
#define ASSERTinfo_rt(cnd, info)
#define ASSERTeq_rt(lhs, rhs)
#define ASSERTne_rt(lhs, rhs)
#define ASSERT(cnd)
#define ASSERTinfo(cnd, info)
#define ASSERTeq(lhs, rhs)
#define ASSERTne(lhs, rhs)
#endif /* DEBUG */

#define ERR(use_errno, ...)\
out_err(use_errno, __FILE__, __LINE__, __func__, __VA_ARGS__)
Expand Down
Loading

0 comments on commit 4f341b7

Please sign in to comment.