15ffd83dbSDimitry Andric //===-- UnixSignals.cpp ---------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Target/UnixSignals.h"
100b57cec5SDimitry Andric #include "Plugins/Process/Utility/FreeBSDSignals.h"
110b57cec5SDimitry Andric #include "Plugins/Process/Utility/LinuxSignals.h"
120b57cec5SDimitry Andric #include "Plugins/Process/Utility/NetBSDSignals.h"
130b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h"
140b57cec5SDimitry Andric #include "lldb/Utility/ArchSpec.h"
15bdd1243dSDimitry Andric #include <optional>
16fe013be4SDimitry Andric #include <sstream>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace lldb_private;
19349cc55cSDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
Signal(llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)21*c9157d92SDimitry Andric UnixSignals::Signal::Signal(llvm::StringRef name, bool default_suppress,
220b57cec5SDimitry Andric                             bool default_stop, bool default_notify,
23*c9157d92SDimitry Andric                             llvm::StringRef description, llvm::StringRef alias)
24*c9157d92SDimitry Andric     : m_name(name), m_alias(alias), m_description(description),
250b57cec5SDimitry Andric       m_suppress(default_suppress), m_stop(default_stop),
26*c9157d92SDimitry Andric       m_notify(default_notify), m_default_suppress(default_suppress),
27*c9157d92SDimitry Andric       m_default_stop(default_stop), m_default_notify(default_notify) {}
280b57cec5SDimitry Andric 
Create(const ArchSpec & arch)290b57cec5SDimitry Andric lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) {
300b57cec5SDimitry Andric   const auto &triple = arch.GetTriple();
310b57cec5SDimitry Andric   switch (triple.getOS()) {
32fe013be4SDimitry Andric   case llvm::Triple::Linux:
330b57cec5SDimitry Andric     return std::make_shared<LinuxSignals>();
340b57cec5SDimitry Andric   case llvm::Triple::FreeBSD:
350b57cec5SDimitry Andric   case llvm::Triple::OpenBSD:
360b57cec5SDimitry Andric     return std::make_shared<FreeBSDSignals>();
370b57cec5SDimitry Andric   case llvm::Triple::NetBSD:
380b57cec5SDimitry Andric     return std::make_shared<NetBSDSignals>();
390b57cec5SDimitry Andric   default:
400b57cec5SDimitry Andric     return std::make_shared<UnixSignals>();
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
CreateForHost()440b57cec5SDimitry Andric lldb::UnixSignalsSP UnixSignals::CreateForHost() {
450b57cec5SDimitry Andric   static lldb::UnixSignalsSP s_unix_signals_sp =
460b57cec5SDimitry Andric       Create(HostInfo::GetArchitecture());
470b57cec5SDimitry Andric   return s_unix_signals_sp;
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // UnixSignals constructor
UnixSignals()510b57cec5SDimitry Andric UnixSignals::UnixSignals() { Reset(); }
520b57cec5SDimitry Andric 
UnixSignals(const UnixSignals & rhs)530b57cec5SDimitry Andric UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric UnixSignals::~UnixSignals() = default;
560b57cec5SDimitry Andric 
Reset()570b57cec5SDimitry Andric void UnixSignals::Reset() {
580b57cec5SDimitry Andric   // This builds one standard set of Unix Signals. If yours aren't quite in
590b57cec5SDimitry Andric   // this order, you can either subclass this class, and use Add & Remove to
60e8d8bef9SDimitry Andric   // change them or you can subclass and build them afresh in your constructor.
610b57cec5SDimitry Andric   //
620b57cec5SDimitry Andric   // Note: the signals below are the Darwin signals. Do not change these!
63e8d8bef9SDimitry Andric 
640b57cec5SDimitry Andric   m_signals.clear();
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric   // clang-format off
670b57cec5SDimitry Andric   //        SIGNO   NAME            SUPPRESS  STOP    NOTIFY  DESCRIPTION
68e8d8bef9SDimitry Andric   //        ======  ==============  ========  ======  ======  ===================================================
690b57cec5SDimitry Andric   AddSignal(1,      "SIGHUP",       false,    true,   true,   "hangup");
700b57cec5SDimitry Andric   AddSignal(2,      "SIGINT",       true,     true,   true,   "interrupt");
710b57cec5SDimitry Andric   AddSignal(3,      "SIGQUIT",      false,    true,   true,   "quit");
720b57cec5SDimitry Andric   AddSignal(4,      "SIGILL",       false,    true,   true,   "illegal instruction");
73e8d8bef9SDimitry Andric   AddSignal(5,      "SIGTRAP",      true,     true,   true,   "trace trap (not reset when caught)");
740b57cec5SDimitry Andric   AddSignal(6,      "SIGABRT",      false,    true,   true,   "abort()");
750b57cec5SDimitry Andric   AddSignal(7,      "SIGEMT",       false,    true,   true,   "pollable event");
760b57cec5SDimitry Andric   AddSignal(8,      "SIGFPE",       false,    true,   true,   "floating point exception");
770b57cec5SDimitry Andric   AddSignal(9,      "SIGKILL",      false,    true,   true,   "kill");
780b57cec5SDimitry Andric   AddSignal(10,     "SIGBUS",       false,    true,   true,   "bus error");
790b57cec5SDimitry Andric   AddSignal(11,     "SIGSEGV",      false,    true,   true,   "segmentation violation");
800b57cec5SDimitry Andric   AddSignal(12,     "SIGSYS",       false,    true,   true,   "bad argument to system call");
81e8d8bef9SDimitry Andric   AddSignal(13,     "SIGPIPE",      false,    false,  false,  "write on a pipe with no one to read it");
820b57cec5SDimitry Andric   AddSignal(14,     "SIGALRM",      false,    false,  false,  "alarm clock");
83e8d8bef9SDimitry Andric   AddSignal(15,     "SIGTERM",      false,    true,   true,   "software termination signal from kill");
84e8d8bef9SDimitry Andric   AddSignal(16,     "SIGURG",       false,    false,  false,  "urgent condition on IO channel");
85e8d8bef9SDimitry Andric   AddSignal(17,     "SIGSTOP",      true,     true,   true,   "sendable stop signal not from tty");
860b57cec5SDimitry Andric   AddSignal(18,     "SIGTSTP",      false,    true,   true,   "stop signal from tty");
87e8d8bef9SDimitry Andric   AddSignal(19,     "SIGCONT",      false,    false,  true,   "continue a stopped process");
88e8d8bef9SDimitry Andric   AddSignal(20,     "SIGCHLD",      false,    false,  false,  "to parent on child stop or exit");
89e8d8bef9SDimitry Andric   AddSignal(21,     "SIGTTIN",      false,    true,   true,   "to readers process group upon background tty read");
90e8d8bef9SDimitry Andric   AddSignal(22,     "SIGTTOU",      false,    true,   true,   "to readers process group upon background tty write");
910b57cec5SDimitry Andric   AddSignal(23,     "SIGIO",        false,    false,  false,  "input/output possible signal");
920b57cec5SDimitry Andric   AddSignal(24,     "SIGXCPU",      false,    true,   true,   "exceeded CPU time limit");
930b57cec5SDimitry Andric   AddSignal(25,     "SIGXFSZ",      false,    true,   true,   "exceeded file size limit");
940b57cec5SDimitry Andric   AddSignal(26,     "SIGVTALRM",    false,    false,  false,  "virtual time alarm");
950b57cec5SDimitry Andric   AddSignal(27,     "SIGPROF",      false,    false,  false,  "profiling time alarm");
960b57cec5SDimitry Andric   AddSignal(28,     "SIGWINCH",     false,    false,  false,  "window size changes");
970b57cec5SDimitry Andric   AddSignal(29,     "SIGINFO",      false,    true,   true,   "information request");
980b57cec5SDimitry Andric   AddSignal(30,     "SIGUSR1",      false,    true,   true,   "user defined signal 1");
990b57cec5SDimitry Andric   AddSignal(31,     "SIGUSR2",      false,    true,   true,   "user defined signal 2");
100e8d8bef9SDimitry Andric   // clang-format on
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
AddSignal(int signo,llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)103*c9157d92SDimitry Andric void UnixSignals::AddSignal(int signo, llvm::StringRef name,
104*c9157d92SDimitry Andric                             bool default_suppress, bool default_stop,
105*c9157d92SDimitry Andric                             bool default_notify, llvm::StringRef description,
106*c9157d92SDimitry Andric                             llvm::StringRef alias) {
1070b57cec5SDimitry Andric   Signal new_signal(name, default_suppress, default_stop, default_notify,
1080b57cec5SDimitry Andric                     description, alias);
1090b57cec5SDimitry Andric   m_signals.insert(std::make_pair(signo, new_signal));
1100b57cec5SDimitry Andric   ++m_version;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
AddSignalCode(int signo,int code,const llvm::StringLiteral description,SignalCodePrintOption print_option)113fe013be4SDimitry Andric void UnixSignals::AddSignalCode(int signo, int code,
114fe013be4SDimitry Andric                                 const llvm::StringLiteral description,
115fe013be4SDimitry Andric                                 SignalCodePrintOption print_option) {
116fe013be4SDimitry Andric   collection::iterator signal = m_signals.find(signo);
117fe013be4SDimitry Andric   assert(signal != m_signals.end() &&
118fe013be4SDimitry Andric          "Tried to add code to signal that does not exist.");
119fe013be4SDimitry Andric   signal->second.m_codes.insert(
120fe013be4SDimitry Andric       std::pair{code, SignalCode{description, print_option}});
121fe013be4SDimitry Andric   ++m_version;
122fe013be4SDimitry Andric }
123fe013be4SDimitry Andric 
RemoveSignal(int signo)1240b57cec5SDimitry Andric void UnixSignals::RemoveSignal(int signo) {
1250b57cec5SDimitry Andric   collection::iterator pos = m_signals.find(signo);
1260b57cec5SDimitry Andric   if (pos != m_signals.end())
1270b57cec5SDimitry Andric     m_signals.erase(pos);
1280b57cec5SDimitry Andric   ++m_version;
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
GetSignalAsStringRef(int32_t signo) const131*c9157d92SDimitry Andric llvm::StringRef UnixSignals::GetSignalAsStringRef(int32_t signo) const {
132*c9157d92SDimitry Andric   const auto pos = m_signals.find(signo);
1330b57cec5SDimitry Andric   if (pos == m_signals.end())
134*c9157d92SDimitry Andric     return {};
135*c9157d92SDimitry Andric   return pos->second.m_name;
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric 
138fe013be4SDimitry Andric std::string
GetSignalDescription(int32_t signo,std::optional<int32_t> code,std::optional<lldb::addr_t> addr,std::optional<lldb::addr_t> lower,std::optional<lldb::addr_t> upper) const139fe013be4SDimitry Andric UnixSignals::GetSignalDescription(int32_t signo, std::optional<int32_t> code,
140fe013be4SDimitry Andric                                   std::optional<lldb::addr_t> addr,
141fe013be4SDimitry Andric                                   std::optional<lldb::addr_t> lower,
142fe013be4SDimitry Andric                                   std::optional<lldb::addr_t> upper) const {
143fe013be4SDimitry Andric   std::string str;
144fe013be4SDimitry Andric 
145fe013be4SDimitry Andric   collection::const_iterator pos = m_signals.find(signo);
146fe013be4SDimitry Andric   if (pos != m_signals.end()) {
147*c9157d92SDimitry Andric     str = pos->second.m_name.str();
148fe013be4SDimitry Andric 
149fe013be4SDimitry Andric     if (code) {
150fe013be4SDimitry Andric       std::map<int32_t, SignalCode>::const_iterator cpos =
151fe013be4SDimitry Andric           pos->second.m_codes.find(*code);
152fe013be4SDimitry Andric       if (cpos != pos->second.m_codes.end()) {
153fe013be4SDimitry Andric         const SignalCode &sc = cpos->second;
154fe013be4SDimitry Andric         str += ": ";
155fe013be4SDimitry Andric         if (sc.m_print_option != SignalCodePrintOption::Bounds)
156fe013be4SDimitry Andric           str += sc.m_description.str();
157fe013be4SDimitry Andric 
158fe013be4SDimitry Andric         std::stringstream strm;
159fe013be4SDimitry Andric         switch (sc.m_print_option) {
160fe013be4SDimitry Andric         case SignalCodePrintOption::None:
161fe013be4SDimitry Andric           break;
162fe013be4SDimitry Andric         case SignalCodePrintOption::Address:
163fe013be4SDimitry Andric           if (addr)
164fe013be4SDimitry Andric             strm << " (fault address: 0x" << std::hex << *addr << ")";
165fe013be4SDimitry Andric           break;
166fe013be4SDimitry Andric         case SignalCodePrintOption::Bounds:
167fe013be4SDimitry Andric           if (lower && upper && addr) {
168fe013be4SDimitry Andric             if ((unsigned long)(*addr) < *lower)
169fe013be4SDimitry Andric               strm << "lower bound violation ";
170fe013be4SDimitry Andric             else
171fe013be4SDimitry Andric               strm << "upper bound violation ";
172fe013be4SDimitry Andric 
173fe013be4SDimitry Andric             strm << "(fault address: 0x" << std::hex << *addr;
174fe013be4SDimitry Andric             strm << ", lower bound: 0x" << std::hex << *lower;
175fe013be4SDimitry Andric             strm << ", upper bound: 0x" << std::hex << *upper;
176fe013be4SDimitry Andric             strm << ")";
177fe013be4SDimitry Andric           } else
178fe013be4SDimitry Andric             strm << sc.m_description.str();
179fe013be4SDimitry Andric 
180fe013be4SDimitry Andric           break;
181fe013be4SDimitry Andric         }
182fe013be4SDimitry Andric         str += strm.str();
183fe013be4SDimitry Andric       }
184fe013be4SDimitry Andric     }
185fe013be4SDimitry Andric   }
186fe013be4SDimitry Andric 
187fe013be4SDimitry Andric   return str;
188fe013be4SDimitry Andric }
189fe013be4SDimitry Andric 
SignalIsValid(int32_t signo) const1900b57cec5SDimitry Andric bool UnixSignals::SignalIsValid(int32_t signo) const {
1910b57cec5SDimitry Andric   return m_signals.find(signo) != m_signals.end();
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
GetShortName(llvm::StringRef name) const194fe013be4SDimitry Andric llvm::StringRef UnixSignals::GetShortName(llvm::StringRef name) const {
195fe013be4SDimitry Andric   return name.substr(3); // Remove "SIG" from name
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
GetSignalNumberFromName(const char * name) const1980b57cec5SDimitry Andric int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
199*c9157d92SDimitry Andric   llvm::StringRef name_ref(name);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   collection::const_iterator pos, end = m_signals.end();
2020b57cec5SDimitry Andric   for (pos = m_signals.begin(); pos != end; pos++) {
203*c9157d92SDimitry Andric     if ((name_ref == pos->second.m_name) || (name_ref == pos->second.m_alias) ||
204*c9157d92SDimitry Andric         (name_ref == GetShortName(pos->second.m_name)) ||
205*c9157d92SDimitry Andric         (name_ref == GetShortName(pos->second.m_alias)))
2060b57cec5SDimitry Andric       return pos->first;
2070b57cec5SDimitry Andric   }
2080b57cec5SDimitry Andric 
209349cc55cSDimitry Andric   int32_t signo;
210349cc55cSDimitry Andric   if (llvm::to_integer(name, signo))
2110b57cec5SDimitry Andric     return signo;
2120b57cec5SDimitry Andric   return LLDB_INVALID_SIGNAL_NUMBER;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
GetFirstSignalNumber() const2150b57cec5SDimitry Andric int32_t UnixSignals::GetFirstSignalNumber() const {
2160b57cec5SDimitry Andric   if (m_signals.empty())
2170b57cec5SDimitry Andric     return LLDB_INVALID_SIGNAL_NUMBER;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   return (*m_signals.begin()).first;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
GetNextSignalNumber(int32_t current_signal) const2220b57cec5SDimitry Andric int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
2230b57cec5SDimitry Andric   collection::const_iterator pos = m_signals.find(current_signal);
2240b57cec5SDimitry Andric   collection::const_iterator end = m_signals.end();
2250b57cec5SDimitry Andric   if (pos == end)
2260b57cec5SDimitry Andric     return LLDB_INVALID_SIGNAL_NUMBER;
2270b57cec5SDimitry Andric   else {
2280b57cec5SDimitry Andric     pos++;
2290b57cec5SDimitry Andric     if (pos == end)
2300b57cec5SDimitry Andric       return LLDB_INVALID_SIGNAL_NUMBER;
2310b57cec5SDimitry Andric     else
2320b57cec5SDimitry Andric       return pos->first;
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
GetSignalInfo(int32_t signo,bool & should_suppress,bool & should_stop,bool & should_notify) const236*c9157d92SDimitry Andric bool UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
237*c9157d92SDimitry Andric                                 bool &should_stop, bool &should_notify) const {
238*c9157d92SDimitry Andric   const auto pos = m_signals.find(signo);
2390b57cec5SDimitry Andric   if (pos == m_signals.end())
240*c9157d92SDimitry Andric     return false;
241*c9157d92SDimitry Andric 
2420b57cec5SDimitry Andric   const Signal &signal = pos->second;
2430b57cec5SDimitry Andric   should_suppress = signal.m_suppress;
2440b57cec5SDimitry Andric   should_stop = signal.m_stop;
2450b57cec5SDimitry Andric   should_notify = signal.m_notify;
246*c9157d92SDimitry Andric   return true;
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
GetShouldSuppress(int signo) const2490b57cec5SDimitry Andric bool UnixSignals::GetShouldSuppress(int signo) const {
2500b57cec5SDimitry Andric   collection::const_iterator pos = m_signals.find(signo);
2510b57cec5SDimitry Andric   if (pos != m_signals.end())
2520b57cec5SDimitry Andric     return pos->second.m_suppress;
2530b57cec5SDimitry Andric   return false;
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
SetShouldSuppress(int signo,bool value)2560b57cec5SDimitry Andric bool UnixSignals::SetShouldSuppress(int signo, bool value) {
2570b57cec5SDimitry Andric   collection::iterator pos = m_signals.find(signo);
2580b57cec5SDimitry Andric   if (pos != m_signals.end()) {
2590b57cec5SDimitry Andric     pos->second.m_suppress = value;
2600b57cec5SDimitry Andric     ++m_version;
2610b57cec5SDimitry Andric     return true;
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric   return false;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
SetShouldSuppress(const char * signal_name,bool value)2660b57cec5SDimitry Andric bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
2670b57cec5SDimitry Andric   const int32_t signo = GetSignalNumberFromName(signal_name);
2680b57cec5SDimitry Andric   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
2690b57cec5SDimitry Andric     return SetShouldSuppress(signo, value);
2700b57cec5SDimitry Andric   return false;
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
GetShouldStop(int signo) const2730b57cec5SDimitry Andric bool UnixSignals::GetShouldStop(int signo) const {
2740b57cec5SDimitry Andric   collection::const_iterator pos = m_signals.find(signo);
2750b57cec5SDimitry Andric   if (pos != m_signals.end())
2760b57cec5SDimitry Andric     return pos->second.m_stop;
2770b57cec5SDimitry Andric   return false;
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric 
SetShouldStop(int signo,bool value)2800b57cec5SDimitry Andric bool UnixSignals::SetShouldStop(int signo, bool value) {
2810b57cec5SDimitry Andric   collection::iterator pos = m_signals.find(signo);
2820b57cec5SDimitry Andric   if (pos != m_signals.end()) {
2830b57cec5SDimitry Andric     pos->second.m_stop = value;
2840b57cec5SDimitry Andric     ++m_version;
2850b57cec5SDimitry Andric     return true;
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric   return false;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
SetShouldStop(const char * signal_name,bool value)2900b57cec5SDimitry Andric bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
2910b57cec5SDimitry Andric   const int32_t signo = GetSignalNumberFromName(signal_name);
2920b57cec5SDimitry Andric   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
2930b57cec5SDimitry Andric     return SetShouldStop(signo, value);
2940b57cec5SDimitry Andric   return false;
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
GetShouldNotify(int signo) const2970b57cec5SDimitry Andric bool UnixSignals::GetShouldNotify(int signo) const {
2980b57cec5SDimitry Andric   collection::const_iterator pos = m_signals.find(signo);
2990b57cec5SDimitry Andric   if (pos != m_signals.end())
3000b57cec5SDimitry Andric     return pos->second.m_notify;
3010b57cec5SDimitry Andric   return false;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
SetShouldNotify(int signo,bool value)3040b57cec5SDimitry Andric bool UnixSignals::SetShouldNotify(int signo, bool value) {
3050b57cec5SDimitry Andric   collection::iterator pos = m_signals.find(signo);
3060b57cec5SDimitry Andric   if (pos != m_signals.end()) {
3070b57cec5SDimitry Andric     pos->second.m_notify = value;
3080b57cec5SDimitry Andric     ++m_version;
3090b57cec5SDimitry Andric     return true;
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric   return false;
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
SetShouldNotify(const char * signal_name,bool value)3140b57cec5SDimitry Andric bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
3150b57cec5SDimitry Andric   const int32_t signo = GetSignalNumberFromName(signal_name);
3160b57cec5SDimitry Andric   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
3170b57cec5SDimitry Andric     return SetShouldNotify(signo, value);
3180b57cec5SDimitry Andric   return false;
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
GetNumSignals() const3210b57cec5SDimitry Andric int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
3220b57cec5SDimitry Andric 
GetSignalAtIndex(int32_t index) const3230b57cec5SDimitry Andric int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
3240b57cec5SDimitry Andric   if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
3250b57cec5SDimitry Andric     return LLDB_INVALID_SIGNAL_NUMBER;
3260b57cec5SDimitry Andric   auto it = m_signals.begin();
3270b57cec5SDimitry Andric   std::advance(it, index);
3280b57cec5SDimitry Andric   return it->first;
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
GetVersion() const3310b57cec5SDimitry Andric uint64_t UnixSignals::GetVersion() const { return m_version; }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric std::vector<int32_t>
GetFilteredSignals(std::optional<bool> should_suppress,std::optional<bool> should_stop,std::optional<bool> should_notify)334bdd1243dSDimitry Andric UnixSignals::GetFilteredSignals(std::optional<bool> should_suppress,
335bdd1243dSDimitry Andric                                 std::optional<bool> should_stop,
336bdd1243dSDimitry Andric                                 std::optional<bool> should_notify) {
3370b57cec5SDimitry Andric   std::vector<int32_t> result;
3380b57cec5SDimitry Andric   for (int32_t signo = GetFirstSignalNumber();
3390b57cec5SDimitry Andric        signo != LLDB_INVALID_SIGNAL_NUMBER;
3400b57cec5SDimitry Andric        signo = GetNextSignalNumber(signo)) {
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric     bool signal_suppress = false;
3430b57cec5SDimitry Andric     bool signal_stop = false;
3440b57cec5SDimitry Andric     bool signal_notify = false;
3450b57cec5SDimitry Andric     GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     // If any of filtering conditions are not met, we move on to the next
3480b57cec5SDimitry Andric     // signal.
349bdd1243dSDimitry Andric     if (should_suppress && signal_suppress != *should_suppress)
3500b57cec5SDimitry Andric       continue;
3510b57cec5SDimitry Andric 
352bdd1243dSDimitry Andric     if (should_stop && signal_stop != *should_stop)
3530b57cec5SDimitry Andric       continue;
3540b57cec5SDimitry Andric 
355bdd1243dSDimitry Andric     if (should_notify && signal_notify != *should_notify)
3560b57cec5SDimitry Andric       continue;
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric     result.push_back(signo);
3590b57cec5SDimitry Andric   }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   return result;
3620b57cec5SDimitry Andric }
363349cc55cSDimitry Andric 
IncrementSignalHitCount(int signo)364349cc55cSDimitry Andric void UnixSignals::IncrementSignalHitCount(int signo) {
365349cc55cSDimitry Andric   collection::iterator pos = m_signals.find(signo);
366349cc55cSDimitry Andric   if (pos != m_signals.end())
367349cc55cSDimitry Andric     pos->second.m_hit_count += 1;
368349cc55cSDimitry Andric }
369349cc55cSDimitry Andric 
GetHitCountStatistics() const370349cc55cSDimitry Andric json::Value UnixSignals::GetHitCountStatistics() const {
371349cc55cSDimitry Andric   json::Array json_signals;
372349cc55cSDimitry Andric   for (const auto &pair : m_signals) {
373349cc55cSDimitry Andric     if (pair.second.m_hit_count > 0)
374*c9157d92SDimitry Andric       json_signals.emplace_back(
375*c9157d92SDimitry Andric           json::Object{{pair.second.m_name, pair.second.m_hit_count}});
376349cc55cSDimitry Andric   }
377349cc55cSDimitry Andric   return std::move(json_signals);
378349cc55cSDimitry Andric }
37981ad6265SDimitry Andric 
Reset(bool reset_stop,bool reset_notify,bool reset_suppress)38081ad6265SDimitry Andric void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify,
38181ad6265SDimitry Andric                                 bool reset_suppress) {
38281ad6265SDimitry Andric   if (reset_stop)
38381ad6265SDimitry Andric     m_stop = m_default_stop;
38481ad6265SDimitry Andric   if (reset_notify)
38581ad6265SDimitry Andric     m_notify = m_default_notify;
38681ad6265SDimitry Andric   if (reset_suppress)
38781ad6265SDimitry Andric     m_suppress = m_default_suppress;
38881ad6265SDimitry Andric }
38981ad6265SDimitry Andric 
ResetSignal(int32_t signo,bool reset_stop,bool reset_notify,bool reset_suppress)39081ad6265SDimitry Andric bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop,
39181ad6265SDimitry Andric                                  bool reset_notify, bool reset_suppress) {
39281ad6265SDimitry Andric     auto elem = m_signals.find(signo);
39381ad6265SDimitry Andric     if (elem == m_signals.end())
39481ad6265SDimitry Andric       return false;
39581ad6265SDimitry Andric     (*elem).second.Reset(reset_stop, reset_notify, reset_suppress);
39681ad6265SDimitry Andric     return true;
39781ad6265SDimitry Andric }
39881ad6265SDimitry Andric 
399