From dba703e50368a3cb6de26b42dc2b7682978dfdd5 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 4 Jun 2021 23:20:50 +0100 Subject: [PATCH] better logging and error reporting --- Makefile | 7 +++++++ include/cow.hpp | 2 +- src/cow.c | 29 ++++++++++++++++++++++++----- src/test/main.cpp | 4 ++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 17bbfb6..ecd33e2 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,13 @@ debug: | dirs $(PROJECT)-debug.a -$(MAKE) dirs $(MAKE) $(PROJECT)-debug.so +# Rebuild both release and debug targets from scratch +.PHONY: all +all: | clean + -$(MAKE) release + -$(MAKE) clean-rebuild + -$(MAKE) debug + # Targets dirs: diff --git a/include/cow.hpp b/include/cow.hpp index 467fbf4..39588f2 100644 --- a/include/cow.hpp +++ b/include/cow.hpp @@ -39,6 +39,7 @@ struct Cow { struct Cow::Fake : Cow { Fake() = delete; + Fake(const Cow& real); Fake(const Fake& copy); Fake(Fake&& move); ~Fake(); @@ -50,7 +51,6 @@ struct Cow::Fake : Cow { protected: cow_t* get_raw() const override; private: - Fake(const Cow& real); cow_t* const fake; }; diff --git a/src/cow.c b/src/cow.c index 1127bcb..de79f11 100644 --- a/src/cow.c +++ b/src/cow.c @@ -2,23 +2,35 @@ #include #include -#include #include #include #include #include #include +#include #include +#define LIKELY(ex) __builtin_expect(!!(ex), 1) +#define UNLIKELY(ex) __builtin_expect(!!(ex), 0) + #define box(t) aligned_alloc(_Alignof(t), sizeof(t)) #if defined(DEBUG) || defined(COW_TRACE) -#define TRACE(msg, ...) (fprintf(stderr, "[TRACE] %s->%s %d: " msg "\n", __FILE__, __func__, __LINE__, __VA_ARGS__), (void)0) +#define TRACE(msg, ...) (fprintf(stderr, " [TRACE] %s->%s():%d: " msg "\n", __FILE__, __func__, __LINE__, __VA_ARGS__), (void)0) #else #define TRACE(msg, ...) ((void)0) #endif +#if !defined(COW_NO_ASSERT) +#define ASSERT(expr, msg) do { if(!(expr)) die("assertion failed: `" #expr "`: " msg); } while(0) +#else +#define ASSERT(op, msg) ((void)0) +#endif + +#define LASSERT(expr, msg) ASSERT(LIKELY(expr), "(unexpected) " msg) +#define UASSERT(expr, msg) ASSERT(UNLIKELY(expr), "(expected) " msg) + struct cow { void* origin; // ptr to mapped memory. This *MUST* be the first field and have an offset of 0. @@ -30,8 +42,13 @@ _Static_assert(offsetof(cow_t, origin) == 0, "`cow_t.origin` must have an offset static __attribute__((noreturn)) __attribute__((noinline)) __attribute__((cold)) void die(const char* error) { - perror(error); - exit(1); + fprintf(stderr, " [FATAL]: "); + if(!errno) + fprintf(stderr, "%s\n (no matching errno, try compiling with `-DCOW_TRACE` and/or `-DDEBUG`)\n", error); + else + perror(error); + + abort(); } static inline cow_t* box_value(cow_t v) @@ -39,6 +56,8 @@ static inline cow_t* box_value(cow_t v) cow_t* boxed = box(cow_t); TRACE("boxing cow_t { origin = %p, fd = 0x%x, size = %lu } -> %p (%lu bytes)", v.origin, v.fd, v.size, (const void*)boxed, sizeof(cow_t)); *boxed = v; + + LASSERT(cow_ptr(boxed) == boxed->origin, "ptr extraction mismatch. this should never happen (check origin field offset)"); return boxed; } @@ -81,7 +100,7 @@ cow_t* cow_create(size_t size) ret.origin = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, ret.fd, 0); if(ret.origin == MAP_FAILED) die("cow_create:mmap"); - TRACE("mapped new cow page of %lu size at %p (memfd %d)", size, ret.origin, ret.fd); + TRACE("mapped new origin cow page of %lu size at %p (memfd %d)", size, ret.origin, ret.fd); return box_value(ret); } diff --git a/src/test/main.cpp b/src/test/main.cpp index 3e18238..cb2a7a2 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -4,9 +4,9 @@ int main() { Cow real(4096); - Cow::Fake clone = real.clone(); + Cow::Fake clone = real; { - Cow::Fake clone2 = clone.clone(); + Cow::Fake clone2 = clone; } return 0; }