From 8fc42d16988ebeb9f7e29cd0bb55f9943eb9c67b Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Thu, 3 Mar 2022 09:08:44 +0000 Subject: [PATCH] Add Pal::message. (#467) This provides a single place for reporting messages to the user. While here, be consistent about using stderr for things that should go to stderr. We were previously using a mix of stderr and stdout. --- src/pal/pal_freebsd_kernel.h | 9 +++++++++ src/pal/pal_noalloc.h | 8 ++++++++ src/pal/pal_posix.h | 27 ++++++++++++++++++++------- src/pal/pal_windows.h | 10 ++++++++-- src/test/helpers.h | 4 ++-- src/test/setup.h | 6 +++--- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/pal/pal_freebsd_kernel.h b/src/pal/pal_freebsd_kernel.h index 72f8487..2e25d91 100644 --- a/src/pal/pal_freebsd_kernel.h +++ b/src/pal/pal_freebsd_kernel.h @@ -28,6 +28,15 @@ namespace snmalloc * PAL supports. */ static constexpr uint64_t pal_features = AlignedAllocation; + + /** + * Report a message to console, followed by a newline. + */ + static void message(const char* const str) noexcept + { + printf("%s\n", str); + } + [[noreturn]] void error(const char* const str) { panic("snmalloc error: %s", str); diff --git a/src/pal/pal_noalloc.h b/src/pal/pal_noalloc.h index 9092b65..d44d149 100644 --- a/src/pal/pal_noalloc.h +++ b/src/pal/pal_noalloc.h @@ -48,6 +48,14 @@ namespace snmalloc BasePAL::print_stack_trace(); } + /** + * Report a message to the user. + */ + static void message(const char* const str) noexcept + { + BasePAL::message(str); + } + /** * Report a fatal error an exit. */ diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 4689f69..d3ec0b7 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #if __has_include() @@ -152,12 +153,23 @@ namespace snmalloc constexpr int SIZE = 1024; void* buffer[SIZE]; auto nptrs = backtrace(buffer, SIZE); - backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO); - UNUSED(write(STDOUT_FILENO, "\n", 1)); - UNUSED(fsync(STDOUT_FILENO)); + backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO); + UNUSED(write(STDERR_FILENO, "\n", 1)); + UNUSED(fsync(STDERR_FILENO)); #endif } + /** + * Report a message to standard error, followed by a newline. + */ + static void message(const char* const str) noexcept + { + void* nl = const_cast("\n"); + struct iovec iov[] = {{const_cast(str), strlen(str)}, {nl, 1}}; + UNUSED(writev(STDERR_FILENO, iov, sizeof(iov) / sizeof(struct iovec))); + UNUSED(fsync(STDERR_FILENO)); + } + /** * Report a fatal error an exit. */ @@ -167,10 +179,11 @@ namespace snmalloc /// subsequent allocation will work. /// @attention: since the program is failing, we do not guarantee that /// previous bytes in stdout will be flushed - UNUSED(write(STDOUT_FILENO, "\n", 1)); - UNUSED(write(STDOUT_FILENO, str, strlen(str))); - UNUSED(write(STDOUT_FILENO, "\n", 1)); - UNUSED(fsync(STDOUT_FILENO)); + void* nl = const_cast("\n"); + struct iovec iov[] = { + {nl, 1}, {const_cast(str), strlen(str)}, {nl, 1}}; + UNUSED(writev(STDERR_FILENO, iov, sizeof(iov) / sizeof(struct iovec))); + UNUSED(fsync(STDERR_FILENO)); print_stack_trace(); abort(); } diff --git a/src/pal/pal_windows.h b/src/pal/pal_windows.h index 6f88420..dc1a1f6 100644 --- a/src/pal/pal_windows.h +++ b/src/pal/pal_windows.h @@ -116,10 +116,16 @@ namespace snmalloc low_memory_callbacks.register_notification(callback); } + static void message(const char* const str) + { + fputs(str, stderr); + fputc('\n', stderr); + fflush(stderr); + } + [[noreturn]] static void error(const char* const str) { - puts(str); - fflush(stdout); + message(str); abort(); } diff --git a/src/test/helpers.h b/src/test/helpers.h index a4ea84d..7e11ba2 100644 --- a/src/test/helpers.h +++ b/src/test/helpers.h @@ -19,7 +19,7 @@ namespace snmalloc { \ current_test = __PRETTY_FUNCTION__; \ MessageBuilder<1024> mb{"Starting test: " msg "\n", ##__VA_ARGS__}; \ - fputs(mb.get_message(), stderr); \ + Pal::message(mb.get_message()); \ } while (0) /** @@ -33,7 +33,7 @@ namespace snmalloc do \ { \ MessageBuilder<1024> mb{msg "\n", ##__VA_ARGS__}; \ - fputs(mb.get_message(), stderr); \ + Pal::message(mb.get_message()); \ } while (0) } diff --git a/src/test/setup.h b/src/test/setup.h index 319902e..2b440a6 100644 --- a/src/test/setup.h +++ b/src/test/setup.h @@ -1,4 +1,5 @@ #if defined(SNMALLOC_CI_BUILD) +# include # if defined(WIN32) # include # include @@ -64,7 +65,7 @@ void print_stack_trace() void _cdecl error(int signal) { snmalloc::UNUSED(signal); - puts("*****ABORT******"); + snmalloc::Pal::message("*****ABORT******"); print_stack_trace(); @@ -75,7 +76,7 @@ LONG WINAPI VectoredHandler(struct _EXCEPTION_POINTERS* ExceptionInfo) { snmalloc::UNUSED(ExceptionInfo); - puts("*****UNHANDLED EXCEPTION******"); + snmalloc::Pal::message("*****UNHANDLED EXCEPTION******"); print_stack_trace(); @@ -96,7 +97,6 @@ void setup() SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); } # else -# include # include void error_handle(int signal) {