81 lines · cpp
1//===-- Implementation of a class for mapping signals to strings ----------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "signal_to_string.h"10 11#include <signal.h>12#include <stddef.h>13 14#include "platform_signals.h"15#include "src/__support/CPP/span.h"16#include "src/__support/CPP/string_view.h"17#include "src/__support/CPP/stringstream.h"18#include "src/__support/StringUtil/message_mapper.h"19#include "src/__support/integer_to_string.h"20#include "src/__support/macros/attributes.h"21#include "src/__support/macros/config.h"22 23namespace LIBC_NAMESPACE_DECL {24namespace {25 26constexpr size_t max_buff_size() {27 constexpr size_t base_str_len = sizeof("Real-time signal");28 // the buffer should be able to hold "Real-time signal" + ' ' + num_str29 return (base_str_len + 1 + IntegerToString<int>::buffer_size()) *30 sizeof(char);31}32 33// This is to hold signal strings that have to be custom built. It may be34// rewritten on every call to strsignal (or other signal to string function).35constexpr size_t SIG_BUFFER_SIZE = max_buff_size();36LIBC_THREAD_LOCAL char signal_buffer[SIG_BUFFER_SIZE];37 38constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS);39 40constexpr size_t SIG_ARRAY_SIZE = max_key_val(PLATFORM_SIGNALS) + 1;41 42constexpr MessageMapper<SIG_ARRAY_SIZE, TOTAL_STR_LEN>43 signal_mapper(PLATFORM_SIGNALS);44 45cpp::string_view build_signal_string(int sig_num, cpp::span<char> buffer) {46 cpp::string_view base_str;47 if (sig_num >= SIGRTMIN && sig_num <= SIGRTMAX) {48 base_str = cpp::string_view("Real-time signal");49 sig_num -= SIGRTMIN;50 } else {51 base_str = cpp::string_view("Unknown signal");52 }53 54 // if the buffer can't hold "Unknown signal" + ' ' + num_str, then just55 // return "Unknown signal".56 if (buffer.size() <57 (base_str.size() + 1 + IntegerToString<int>::buffer_size()))58 return base_str;59 60 cpp::StringStream buffer_stream(61 {const_cast<char *>(buffer.data()), buffer.size()});62 buffer_stream << base_str << ' ' << sig_num << '\0';63 return buffer_stream.str();64}65 66} // namespace67 68cpp::string_view get_signal_string(int sig_num) {69 return get_signal_string(sig_num, {signal_buffer, SIG_BUFFER_SIZE});70}71 72cpp::string_view get_signal_string(int sig_num, cpp::span<char> buffer) {73 auto opt_str = signal_mapper.get_str(sig_num);74 if (opt_str)75 return *opt_str;76 else77 return build_signal_string(sig_num, buffer);78}79 80} // namespace LIBC_NAMESPACE_DECL81