diff --git a/libc/src/__support/StringUtil/CMakeLists.txt b/libc/src/__support/StringUtil/CMakeLists.txt --- a/libc/src/__support/StringUtil/CMakeLists.txt +++ b/libc/src/__support/StringUtil/CMakeLists.txt @@ -3,10 +3,21 @@ HDRS message_mapper.h DEPENDS - libc.src.__support.CPP.string_view - libc.src.__support.CPP.optional + libc.src.__support.CPP.string_view + libc.src.__support.CPP.optional ) +# The platform specific maps depend on message_mapper. +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) + add_subdirectory(${LIBC_TARGET_OS}) +endif() + +set(error_to_string_platform_dep "") +if(TARGET libc.src.__support.StringUtil.${LIBC_TARGET_OS}.strerror_map) + set(error_to_string_platform_dep + libc.src.__support.StringUtil.${LIBC_TARGET_OS}.strerror_map) +endif() + add_object_library( error_to_string HDRS @@ -14,15 +25,22 @@ SRCS error_to_string.cpp DEPENDS - .message_mapper - libc.src.errno.errno - libc.src.__support.CPP.span - libc.src.__support.CPP.string_view - libc.src.__support.CPP.stringstream - libc.src.__support.integer_to_string + .message_mapper + libc.src.errno.errno + libc.src.__support.CPP.span + libc.src.__support.CPP.string_view + libc.src.__support.CPP.stringstream + libc.src.__support.integer_to_string + ${error_to_string_platform_dep} ) +set(signal_to_string_platform_dep "") +if(TARGET libc.src.__support.StringUtil.${LIBC_TARGET_OS}.strsignal_map) + set(signal_to_string_platform_dep + libc.src.__support.StringUtil.${LIBC_TARGET_OS}.strsignal_map) +endif() + add_object_library( signal_to_string HDRS @@ -30,10 +48,11 @@ SRCS signal_to_string.cpp DEPENDS - .message_mapper - libc.include.signal - libc.src.__support.CPP.span - libc.src.__support.CPP.string_view - libc.src.__support.CPP.stringstream - libc.src.__support.integer_to_string + .message_mapper + libc.include.signal + libc.src.__support.CPP.span + libc.src.__support.CPP.string_view + libc.src.__support.CPP.stringstream + libc.src.__support.integer_to_string + ${signal_to_string_platform_dep} ) diff --git a/libc/src/__support/StringUtil/error_to_string.cpp b/libc/src/__support/StringUtil/error_to_string.cpp --- a/libc/src/__support/StringUtil/error_to_string.cpp +++ b/libc/src/__support/StringUtil/error_to_string.cpp @@ -8,42 +8,32 @@ #include "src/__support/StringUtil/error_to_string.h" +#include "src/errno/libc_errno.h" // For error macros + +#include "src/__support/CPP/array.h" #include "src/__support/CPP/span.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/stringstream.h" #include "src/__support/StringUtil/message_mapper.h" #include "src/__support/integer_to_string.h" -#include "src/errno/libc_errno.h" // For error macros + +#ifdef __linux__ +#include "src/__support/StringUtil/linux/strerror_map.h" +#endif #include namespace __llvm_libc { namespace internal { -constexpr size_t max_buff_size() { - constexpr size_t unknown_str_len = sizeof("Unknown error"); - constexpr size_t max_num_len = - __llvm_libc::IntegerToString::dec_bufsize(); - // the buffer should be able to hold "Unknown error" + ' ' + num_str - return (unknown_str_len + 1 + max_num_len) * sizeof(char); -} - -// This is to hold error strings that have to be custom built. It may be -// rewritten on every call to strerror (or other error to string function). -constexpr size_t ERR_BUFFER_SIZE = max_buff_size(); -thread_local char error_buffer[ERR_BUFFER_SIZE]; - -// Since the StringMappings array is a map from error numbers to their -// corresponding strings, we have to have an array large enough we can use the -// error numbers as indexes. Thankfully there are 132 errors in the above list -// (41 and 58 are skipped) and the highest number is 133. If other platforms use -// different error numbers, then this number may need to be adjusted. -// Also if negative numbers or particularly large numbers are used, then the -// array should be turned into a proper hashmap. -constexpr size_t ERR_ARRAY_SIZE = 134; - -constexpr MsgMapping raw_err_array[] = { +constexpr const cpp::array cstd_err_arr = { MsgMapping(0, "Success"), + MsgMapping(EDOM, "Numerical argument out of domain"), + MsgMapping(ERANGE, "Numerical result out of range"), + MsgMapping(EILSEQ, "Invalid or incomplete multibyte or wide character"), +}; + +constexpr cpp::array posix_err_arr = { MsgMapping(EPERM, "Operation not permitted"), MsgMapping(ENOENT, "No such file or directory"), MsgMapping(ESRCH, "No such process"), @@ -58,7 +48,6 @@ MsgMapping(ENOMEM, "Cannot allocate memory"), MsgMapping(EACCES, "Permission denied"), MsgMapping(EFAULT, "Bad address"), - MsgMapping(ENOTBLK, "Block device required"), MsgMapping(EBUSY, "Device or resource busy"), MsgMapping(EEXIST, "File exists"), MsgMapping(EXDEV, "Invalid cross-device link"), @@ -76,70 +65,30 @@ MsgMapping(EROFS, "Read-only file system"), MsgMapping(EMLINK, "Too many links"), MsgMapping(EPIPE, "Broken pipe"), - MsgMapping(EDOM, "Numerical argument out of domain"), - MsgMapping(ERANGE, "Numerical result out of range"), MsgMapping(EDEADLK, "Resource deadlock avoided"), MsgMapping(ENAMETOOLONG, "File name too long"), MsgMapping(ENOLCK, "No locks available"), MsgMapping(ENOSYS, "Function not implemented"), MsgMapping(ENOTEMPTY, "Directory not empty"), MsgMapping(ELOOP, "Too many levels of symbolic links"), - // No error for 41. Would be EWOULDBLOCK MsgMapping(ENOMSG, "No message of desired type"), MsgMapping(EIDRM, "Identifier removed"), - MsgMapping(ECHRNG, "Channel number out of range"), - MsgMapping(EL2NSYNC, "Level 2 not synchronized"), - MsgMapping(EL3HLT, "Level 3 halted"), - MsgMapping(EL3RST, "Level 3 reset"), - MsgMapping(ELNRNG, "Link number out of range"), - MsgMapping(EUNATCH, "Protocol driver not attached"), - MsgMapping(ENOCSI, "No CSI structure available"), - MsgMapping(EL2HLT, "Level 2 halted"), - MsgMapping(EBADE, "Invalid exchange"), - MsgMapping(EBADR, "Invalid request descriptor"), - MsgMapping(EXFULL, "Exchange full"), - MsgMapping(ENOANO, "No anode"), - MsgMapping(EBADRQC, "Invalid request code"), - MsgMapping(EBADSLT, "Invalid slot"), - // No error for 58. Would be EDEADLOCK. - MsgMapping(EBFONT, "Bad font file format"), MsgMapping(ENOSTR, "Device not a stream"), MsgMapping(ENODATA, "No data available"), MsgMapping(ETIME, "Timer expired"), MsgMapping(ENOSR, "Out of streams resources"), - MsgMapping(ENONET, "Machine is not on the network"), - MsgMapping(ENOPKG, "Package not installed"), - MsgMapping(EREMOTE, "Object is remote"), MsgMapping(ENOLINK, "Link has been severed"), - MsgMapping(EADV, "Advertise error"), - MsgMapping(ESRMNT, "Srmount error"), - MsgMapping(ECOMM, "Communication error on send"), MsgMapping(EPROTO, "Protocol error"), MsgMapping(EMULTIHOP, "Multihop attempted"), - MsgMapping(EDOTDOT, "RFS specific error"), MsgMapping(EBADMSG, "Bad message"), MsgMapping(EOVERFLOW, "Value too large for defined data type"), - MsgMapping(ENOTUNIQ, "Name not unique on network"), - MsgMapping(EBADFD, "File descriptor in bad state"), - MsgMapping(EREMCHG, "Remote address changed"), - MsgMapping(ELIBACC, "Can not access a needed shared library"), - MsgMapping(ELIBBAD, "Accessing a corrupted shared library"), - MsgMapping(ELIBSCN, ".lib section in a.out corrupted"), - MsgMapping(ELIBMAX, "Attempting to link in too many shared libraries"), - MsgMapping(ELIBEXEC, "Cannot exec a shared library directly"), - MsgMapping(EILSEQ, "Invalid or incomplete multibyte or wide character"), - MsgMapping(ERESTART, "Interrupted system call should be restarted"), - MsgMapping(ESTRPIPE, "Streams pipe error"), - MsgMapping(EUSERS, "Too many users"), MsgMapping(ENOTSOCK, "Socket operation on non-socket"), MsgMapping(EDESTADDRREQ, "Destination address required"), MsgMapping(EMSGSIZE, "Message too long"), MsgMapping(EPROTOTYPE, "Protocol wrong type for socket"), MsgMapping(ENOPROTOOPT, "Protocol not available"), MsgMapping(EPROTONOSUPPORT, "Protocol not supported"), - MsgMapping(ESOCKTNOSUPPORT, "Socket type not supported"), MsgMapping(ENOTSUP, "Operation not supported"), - MsgMapping(EPFNOSUPPORT, "Protocol family not supported"), MsgMapping(EAFNOSUPPORT, "Address family not supported by protocol"), MsgMapping(EADDRINUSE, "Address already in use"), MsgMapping(EADDRNOTAVAIL, "Cannot assign requested address"), @@ -151,39 +100,53 @@ MsgMapping(ENOBUFS, "No buffer space available"), MsgMapping(EISCONN, "Transport endpoint is already connected"), MsgMapping(ENOTCONN, "Transport endpoint is not connected"), - MsgMapping(ESHUTDOWN, "Cannot send after transport endpoint shutdown"), - MsgMapping(ETOOMANYREFS, "Too many references: cannot splice"), MsgMapping(ETIMEDOUT, "Connection timed out"), MsgMapping(ECONNREFUSED, "Connection refused"), - MsgMapping(EHOSTDOWN, "Host is down"), MsgMapping(EHOSTUNREACH, "No route to host"), MsgMapping(EALREADY, "Operation already in progress"), MsgMapping(EINPROGRESS, "Operation now in progress"), MsgMapping(ESTALE, "Stale file handle"), - MsgMapping(EUCLEAN, "Structure needs cleaning"), - MsgMapping(ENOTNAM, "Not a XENIX named type file"), - MsgMapping(ENAVAIL, "No XENIX semaphores available"), - MsgMapping(EISNAM, "Is a named type file"), - MsgMapping(EREMOTEIO, "Remote I/O error"), MsgMapping(EDQUOT, "Disk quota exceeded"), - MsgMapping(ENOMEDIUM, "No medium found"), - MsgMapping(EMEDIUMTYPE, "Wrong medium type"), MsgMapping(ECANCELED, "Operation canceled"), - MsgMapping(ENOKEY, "Required key not available"), - MsgMapping(EKEYEXPIRED, "Key has expired"), - MsgMapping(EKEYREVOKED, "Key has been revoked"), - MsgMapping(EKEYREJECTED, "Key was rejected by service"), MsgMapping(EOWNERDEAD, "Owner died"), MsgMapping(ENOTRECOVERABLE, "State not recoverable"), - MsgMapping(ERFKILL, "Operation not possible due to RF-kill"), - MsgMapping(EHWPOISON, "Memory page has hardware error"), }; -constexpr size_t RAW_ARRAY_LEN = sizeof(raw_err_array) / sizeof(MsgMapping); -constexpr size_t TOTAL_STR_LEN = total_str_len(raw_err_array, RAW_ARRAY_LEN); +constexpr ArrayConcat cstd_err_concat(cstd_err_arr); + +#ifdef __linux__ +constexpr auto total_arr = cstd_err_concat + posix_err_arr + linux_err_arr; +#else +constexpr auto total_arr = cstd_err_concat + posix_err_arr; +#endif + +constexpr size_t max_buff_size() { + constexpr size_t unknown_str_len = sizeof("Unknown error"); + constexpr size_t max_num_len = + __llvm_libc::IntegerToString::dec_bufsize(); + // the buffer should be able to hold "Unknown error" + ' ' + num_str + return (unknown_str_len + 1 + max_num_len) * sizeof(char); +} + +// This is to hold error strings that have to be custom built. It may be +// rewritten on every call to strerror (or other error to string function). +constexpr size_t ERR_BUFFER_SIZE = max_buff_size(); +thread_local char error_buffer[ERR_BUFFER_SIZE]; + +constexpr size_t RAW_ARRAY_LEN = total_arr.size(); +constexpr size_t TOTAL_STR_LEN = total_str_len(total_arr.data(), RAW_ARRAY_LEN); + +// Since the StringMappings array is a map from error numbers to their +// corresponding strings, we have to have an array large enough we can use the +// error numbers as indexes. The current linux configuration has 132 values with +// the maximum value being 133 (41 and 58 are skipped). If other platforms use +// negative numbers or discontiguous ranges, then the array should be turned +// into a proper hashmap. +constexpr size_t ERR_ARRAY_SIZE = + max_key_val(total_arr.data(), RAW_ARRAY_LEN) + 1; static constexpr MessageMapper - error_mapper(raw_err_array, RAW_ARRAY_LEN); + error_mapper(total_arr.data(), RAW_ARRAY_LEN); cpp::string_view build_error_string(int err_num, cpp::span buffer) { // if the buffer can't hold "Unknown error" + ' ' + num_str, then just diff --git a/libc/src/__support/StringUtil/linux/CMakeLists.txt b/libc/src/__support/StringUtil/linux/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/libc/src/__support/StringUtil/linux/CMakeLists.txt @@ -0,0 +1,17 @@ +add_header_library( + strerror_map + HDRS + strerror_map.h + DEPENDS + libc.src.__support.StringUtil.message_mapper + libc.src.errno.errno +) + +add_header_library( + strsignal_map + HDRS + strsignal_map.h + DEPENDS + libc.src.__support.StringUtil.message_mapper + libc.include.signal +) diff --git a/libc/src/__support/StringUtil/linux/strerror_map.h b/libc/src/__support/StringUtil/linux/strerror_map.h new file mode 100644 --- /dev/null +++ b/libc/src/__support/StringUtil/linux/strerror_map.h @@ -0,0 +1,71 @@ +//===-- Map from signal numbers to strings on linux -------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/errno/libc_errno.h" // For error macros + +#include "src/__support/CPP/array.h" +#include "src/__support/StringUtil/message_mapper.h" + +namespace __llvm_libc::internal { + +constexpr cpp::array linux_err_arr = { + MsgMapping(ENOTBLK, "Block device required"), + MsgMapping(ECHRNG, "Channel number out of range"), + MsgMapping(EL2NSYNC, "Level 2 not synchronized"), + MsgMapping(EL3HLT, "Level 3 halted"), + MsgMapping(EL3RST, "Level 3 reset"), + MsgMapping(ELNRNG, "Link number out of range"), + MsgMapping(EUNATCH, "Protocol driver not attached"), + MsgMapping(ENOCSI, "No CSI structure available"), + MsgMapping(EL2HLT, "Level 2 halted"), + MsgMapping(EBADE, "Invalid exchange"), + MsgMapping(EBADR, "Invalid request descriptor"), + MsgMapping(EXFULL, "Exchange full"), + MsgMapping(ENOANO, "No anode"), + MsgMapping(EBADRQC, "Invalid request code"), + MsgMapping(EBADSLT, "Invalid slot"), + MsgMapping(EBFONT, "Bad font file format"), + MsgMapping(ENONET, "Machine is not on the network"), + MsgMapping(ENOPKG, "Package not installed"), + MsgMapping(EREMOTE, "Object is remote"), + MsgMapping(EADV, "Advertise error"), + MsgMapping(ESRMNT, "Srmount error"), + MsgMapping(ECOMM, "Communication error on send"), + MsgMapping(EDOTDOT, "RFS specific error"), + MsgMapping(ENOTUNIQ, "Name not unique on network"), + MsgMapping(EBADFD, "File descriptor in bad state"), + MsgMapping(EREMCHG, "Remote address changed"), + MsgMapping(ELIBACC, "Can not access a needed shared library"), + MsgMapping(ELIBBAD, "Accessing a corrupted shared library"), + MsgMapping(ELIBSCN, ".lib section in a.out corrupted"), + MsgMapping(ELIBMAX, "Attempting to link in too many shared libraries"), + MsgMapping(ELIBEXEC, "Cannot exec a shared library directly"), + MsgMapping(ERESTART, "Interrupted system call should be restarted"), + MsgMapping(ESTRPIPE, "Streams pipe error"), + MsgMapping(EUSERS, "Too many users"), + MsgMapping(ESOCKTNOSUPPORT, "Socket type not supported"), + MsgMapping(EPFNOSUPPORT, "Protocol family not supported"), + MsgMapping(ESHUTDOWN, "Cannot send after transport endpoint shutdown"), + MsgMapping(ETOOMANYREFS, "Too many references: cannot splice"), + MsgMapping(EHOSTDOWN, "Host is down"), + MsgMapping(EUCLEAN, "Structure needs cleaning"), + MsgMapping(ENOTNAM, "Not a XENIX named type file"), + MsgMapping(ENAVAIL, "No XENIX semaphores available"), + MsgMapping(EISNAM, "Is a named type file"), + MsgMapping(EREMOTEIO, "Remote I/O error"), + MsgMapping(ENOMEDIUM, "No medium found"), + MsgMapping(EMEDIUMTYPE, "Wrong medium type"), + MsgMapping(ENOKEY, "Required key not available"), + MsgMapping(EKEYEXPIRED, "Key has expired"), + MsgMapping(EKEYREVOKED, "Key has been revoked"), + MsgMapping(EKEYREJECTED, "Key was rejected by service"), + MsgMapping(ERFKILL, "Operation not possible due to RF-kill"), + MsgMapping(EHWPOISON, "Memory page has hardware error"), +}; + +} // namespace __llvm_libc::internal diff --git a/libc/src/__support/StringUtil/linux/strsignal_map.h b/libc/src/__support/StringUtil/linux/strsignal_map.h new file mode 100644 --- /dev/null +++ b/libc/src/__support/StringUtil/linux/strsignal_map.h @@ -0,0 +1,28 @@ +//===-- Map from error numbers to strings on linux --------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include + +#include "src/__support/StringUtil/message_mapper.h" + +namespace __llvm_libc::internal { + +// The array being larger than necessary isn't a problem. The MsgMappings will +// be set to their default state which maps 0 to an empty string. This will get +// filtered out in the MessageMapper building stage. +constexpr const cpp::array linux_sig_arr = { +#ifdef SIGSTKFLT + MsgMapping(SIGSTKFLT, "Stack fault"), // unused +#endif + MsgMapping(SIGWINCH, "Window changed"), +#ifdef SIGPWR + MsgMapping(SIGPWR, "Power failure"), // ignored +#endif +}; + +} // namespace __llvm_libc::internal diff --git a/libc/src/__support/StringUtil/message_mapper.h b/libc/src/__support/StringUtil/message_mapper.h --- a/libc/src/__support/StringUtil/message_mapper.h +++ b/libc/src/__support/StringUtil/message_mapper.h @@ -20,12 +20,50 @@ int num; cpp::string_view msg; + constexpr MsgMapping() : num(0), msg() { ; } + constexpr MsgMapping(int init_num, const char *init_msg) : num(init_num), msg(init_msg) { ; } }; +template class ArrayConcat { + T Data[N]; + +public: + constexpr size_t size() const { return N; } + constexpr const T *data() const { return Data; } + + constexpr ArrayConcat(cpp::array arr) { + for (size_t i = 0; i < N; ++i) { + Data[i] = arr[i]; + } + } + constexpr ArrayConcat(T raw_data[N]) { + for (size_t i = 0; i < N; ++i) { + Data[i] = raw_data[i]; + } + } + + template + constexpr ArrayConcat + operator+(const cpp::array &other) const { + T raw_new_data[N + OtherN]; + size_t new_index = 0; + for (size_t i = 0; i < N; ++i, ++new_index) { + raw_new_data[new_index] = Data[i]; + } + for (size_t i = 0; i < OtherN; ++i, ++new_index) { + raw_new_data[new_index] = other[i]; + } + return ArrayConcat(raw_new_data); + } +}; + +template +ArrayConcat(cpp::array arr) -> ArrayConcat; + constexpr size_t total_str_len(const MsgMapping *array, size_t len) { size_t total = 0; for (size_t i = 0; i < len; ++i) { @@ -35,6 +73,18 @@ return total; } +constexpr size_t max_key_val(const MsgMapping *array, size_t len) { + int max = 0; + for (size_t i = 0; i < len; ++i) { + if (array[i].num > max) { + max = array[i].num; + } + } + // max will never be negative since the starting value is 0. This is good, + // since it's used as a length. + return static_cast(max); +} + template class MessageMapper { int msg_offsets[ARR_SIZE] = {-1}; char string_array[TOTAL_STR_LEN] = {'\0'}; diff --git a/libc/src/__support/StringUtil/signal_to_string.cpp b/libc/src/__support/StringUtil/signal_to_string.cpp --- a/libc/src/__support/StringUtil/signal_to_string.cpp +++ b/libc/src/__support/StringUtil/signal_to_string.cpp @@ -14,6 +14,10 @@ #include "src/__support/StringUtil/message_mapper.h" #include "src/__support/integer_to_string.h" +#ifdef __linux__ +#include "src/__support/StringUtil/linux/strsignal_map.h" +#endif + #include #include @@ -28,25 +32,29 @@ return (base_str_len + 1 + max_num_len) * sizeof(char); } -// This is to hold signal strings that have to be custom built. It may be -// rewritten on every call to strsignal (or other signal to string function). -constexpr size_t SIG_BUFFER_SIZE = max_buff_size(); -thread_local char signal_buffer[SIG_BUFFER_SIZE]; - -constexpr MsgMapping raw_sig_array[] = { - MsgMapping(SIGHUP, "Hangup"), MsgMapping(SIGINT, "Interrupt"), - MsgMapping(SIGQUIT, "Quit"), MsgMapping(SIGILL, "Illegal instruction"), - MsgMapping(SIGTRAP, "Trace/breakpoint trap"), - MsgMapping(SIGABRT, "Aborted"), MsgMapping(SIGBUS, "Bus error"), +constexpr const cpp::array cstd_sig_arr = { + MsgMapping(SIGINT, "Interrupt"), + MsgMapping(SIGILL, "Illegal instruction"), + MsgMapping(SIGABRT, "Aborted"), MsgMapping(SIGFPE, "Floating point exception"), - MsgMapping(SIGKILL, "Killed"), MsgMapping(SIGUSR1, "User defined signal 1"), MsgMapping(SIGSEGV, "Segmentation fault"), - MsgMapping(SIGUSR2, "User defined signal 2"), - MsgMapping(SIGPIPE, "Broken pipe"), MsgMapping(SIGALRM, "Alarm clock"), MsgMapping(SIGTERM, "Terminated"), - // SIGSTKFLT (may not exist) - MsgMapping(SIGCHLD, "Child exited"), MsgMapping(SIGCONT, "Continued"), - MsgMapping(SIGSTOP, "Stopped (signal)"), MsgMapping(SIGTSTP, "Stopped"), +}; + +constexpr const cpp::array posix_sig_arr = { + MsgMapping(SIGHUP, "Hangup"), + MsgMapping(SIGQUIT, "Quit"), + MsgMapping(SIGTRAP, "Trace/breakpoint trap"), + MsgMapping(SIGBUS, "Bus error"), + MsgMapping(SIGKILL, "Killed"), + MsgMapping(SIGUSR1, "User defined signal 1"), + MsgMapping(SIGUSR2, "User defined signal 2"), + MsgMapping(SIGPIPE, "Broken pipe"), + MsgMapping(SIGALRM, "Alarm clock"), + MsgMapping(SIGCHLD, "Child exited"), + MsgMapping(SIGCONT, "Continued"), + MsgMapping(SIGSTOP, "Stopped (signal)"), + MsgMapping(SIGTSTP, "Stopped"), MsgMapping(SIGTTIN, "Stopped (tty input)"), MsgMapping(SIGTTOU, "Stopped (tty output)"), MsgMapping(SIGURG, "Urgent I/O condition"), @@ -54,29 +62,31 @@ MsgMapping(SIGXFSZ, "File size limit exceeded"), MsgMapping(SIGVTALRM, "Virtual timer expired"), MsgMapping(SIGPROF, "Profiling timer expired"), - MsgMapping(SIGWINCH, "Window changed"), MsgMapping(SIGPOLL, "I/O possible"), - // SIGPWR (may not exist) + MsgMapping(SIGPOLL, "I/O possible"), MsgMapping(SIGSYS, "Bad system call"), +}; -#ifdef SIGSTKFLT - MsgMapping(SIGSTKFLT, "Stack fault"), // unused -#endif -#ifdef SIGPWR - MsgMapping(SIGPWR, "Power failure"), // ignored +constexpr ArrayConcat cstd_sig_concat(cstd_sig_arr); + +#ifdef __linux__ +constexpr auto total_arr = cstd_sig_concat + posix_sig_arr + linux_sig_arr; +#else +constexpr auto total_arr = cstd_sig_concat + posix_sig_arr; #endif -}; -// Since the string_mappings array is a map from signal numbers to their -// corresponding strings, we have to have an array large enough we can use the -// signal numbers as indexes. The highest signal is SIGSYS at 31, so an array of -// 32 elements will be large enough to hold all of them. -constexpr size_t SIG_ARRAY_SIZE = 32; +// This is to hold signal strings that have to be custom built. It may be +// rewritten on every call to strsignal (or other signal to string function). +constexpr size_t SIG_BUFFER_SIZE = max_buff_size(); +thread_local char signal_buffer[SIG_BUFFER_SIZE]; + +constexpr size_t RAW_ARRAY_LEN = total_arr.size(); +constexpr size_t TOTAL_STR_LEN = total_str_len(total_arr.data(), RAW_ARRAY_LEN); -constexpr size_t RAW_ARRAY_LEN = sizeof(raw_sig_array) / sizeof(MsgMapping); -constexpr size_t TOTAL_STR_LEN = total_str_len(raw_sig_array, RAW_ARRAY_LEN); +constexpr size_t SIG_ARRAY_SIZE = + max_key_val(total_arr.data(), RAW_ARRAY_LEN) + 1; static constexpr MessageMapper - signal_mapper(raw_sig_array, RAW_ARRAY_LEN); + signal_mapper(total_arr.data(), RAW_ARRAY_LEN); cpp::string_view build_signal_string(int sig_num, cpp::span buffer) { cpp::string_view base_str;