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.
This commit is contained in:
David Chisnall
2022-03-03 09:08:44 +00:00
committed by GitHub
parent c73846e418
commit 8fc42d1698
6 changed files with 50 additions and 14 deletions

View File

@@ -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);

View File

@@ -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.
*/

View File

@@ -15,6 +15,7 @@
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#include <utility>
#if __has_include(<sys/random.h>)
@@ -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<char*>("\n");
struct iovec iov[] = {{const_cast<char*>(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<char*>("\n");
struct iovec iov[] = {
{nl, 1}, {const_cast<char*>(str), strlen(str)}, {nl, 1}};
UNUSED(writev(STDERR_FILENO, iov, sizeof(iov) / sizeof(struct iovec)));
UNUSED(fsync(STDERR_FILENO));
print_stack_trace();
abort();
}

View File

@@ -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();
}

View File

@@ -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)
}

View File

@@ -1,4 +1,5 @@
#if defined(SNMALLOC_CI_BUILD)
# include <pal/pal.h>
# if defined(WIN32)
# include <ds/bits.h>
# include <iostream>
@@ -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 <pal/pal.h>
# include <signal.h>
void error_handle(int signal)
{