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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "signal_to_string.h"
10#include "platform_signals.h"
11
12#include "src/__support/CPP/span.h"
13#include "src/__support/CPP/string_view.h"
14#include "src/__support/CPP/stringstream.h"
15#include "src/__support/StringUtil/message_mapper.h"
16#include "src/__support/integer_to_string.h"
17#include "src/__support/macros/attributes.h"
18
19#include <signal.h>
20#include <stddef.h>
21
22namespace LIBC_NAMESPACE {
23namespace internal {
24
25constexpr size_t max_buff_size() {
26 constexpr size_t base_str_len = sizeof("Real-time signal");
27 // the buffer should be able to hold "Real-time signal" + ' ' + num_str
28 return (base_str_len + 1 + IntegerToString<int>::buffer_size()) *
29 sizeof(char);
30}
31
32// This is to hold signal strings that have to be custom built. It may be
33// rewritten on every call to strsignal (or other signal to string function).
34constexpr size_t SIG_BUFFER_SIZE = max_buff_size();
35LIBC_THREAD_LOCAL char signal_buffer[SIG_BUFFER_SIZE];
36
37constexpr size_t TOTAL_STR_LEN = total_str_len(table: PLATFORM_SIGNALS);
38
39constexpr size_t SIG_ARRAY_SIZE = max_key_val(table: PLATFORM_SIGNALS) + 1;
40
41constexpr MessageMapper<SIG_ARRAY_SIZE, TOTAL_STR_LEN>
42 signal_mapper(PLATFORM_SIGNALS);
43
44cpp::string_view build_signal_string(int sig_num, cpp::span<char> buffer) {
45 cpp::string_view base_str;
46 if (sig_num >= SIGRTMIN && sig_num <= SIGRTMAX) {
47 base_str = cpp::string_view("Real-time signal");
48 sig_num -= SIGRTMIN;
49 } else {
50 base_str = cpp::string_view("Unknown signal");
51 }
52
53 // if the buffer can't hold "Unknown signal" + ' ' + num_str, then just
54 // return "Unknown signal".
55 if (buffer.size() <
56 (base_str.size() + 1 + IntegerToString<int>::buffer_size()))
57 return base_str;
58
59 cpp::StringStream buffer_stream(
60 {const_cast<char *>(buffer.data()), buffer.size()});
61 buffer_stream << base_str << ' ' << sig_num << '\0';
62 return buffer_stream.str();
63}
64
65} // namespace internal
66
67cpp::string_view get_signal_string(int sig_num) {
68 return get_signal_string(
69 err_num: sig_num, buffer: {internal::signal_buffer, internal::SIG_BUFFER_SIZE});
70}
71
72cpp::string_view get_signal_string(int sig_num, cpp::span<char> buffer) {
73 auto opt_str = internal::signal_mapper.get_str(num: sig_num);
74 if (opt_str)
75 return *opt_str;
76 else
77 return internal::build_signal_string(sig_num, buffer);
78}
79
80} // namespace LIBC_NAMESPACE
81

source code of libc/src/__support/StringUtil/signal_to_string.cpp