xref: /src/contrib/llvm-project/lldb/source/Target/UnixSignals.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1cfca06d7SDimitry Andric //===-- UnixSignals.cpp ---------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f3fbd1c0SDimitry Andric #include "lldb/Target/UnixSignals.h"
10027f1c96SDimitry Andric #include "Plugins/Process/Utility/FreeBSDSignals.h"
11027f1c96SDimitry Andric #include "Plugins/Process/Utility/LinuxSignals.h"
12e81d9d49SDimitry Andric #include "Plugins/Process/Utility/NetBSDSignals.h"
135f29bb8aSDimitry Andric #include "lldb/Host/HostInfo.h"
14ef5d0b5eSDimitry Andric #include "lldb/Utility/ArchSpec.h"
15e3b55780SDimitry Andric #include <optional>
167fa27ce4SDimitry Andric #include <sstream>
17027f1c96SDimitry Andric 
18f034231aSEd Maste using namespace lldb_private;
19c0981da4SDimitry Andric using namespace llvm;
20f034231aSEd Maste 
Signal(llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)21b1c73532SDimitry Andric UnixSignals::Signal::Signal(llvm::StringRef name, bool default_suppress,
2214f1b3e8SDimitry Andric                             bool default_stop, bool default_notify,
23b1c73532SDimitry Andric                             llvm::StringRef description, llvm::StringRef alias)
24b1c73532SDimitry Andric     : m_name(name), m_alias(alias), m_description(description),
2514f1b3e8SDimitry Andric       m_suppress(default_suppress), m_stop(default_stop),
26b1c73532SDimitry Andric       m_notify(default_notify), m_default_suppress(default_suppress),
27b1c73532SDimitry Andric       m_default_stop(default_stop), m_default_notify(default_notify) {}
28f034231aSEd Maste 
Create(const ArchSpec & arch)2914f1b3e8SDimitry Andric lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) {
30027f1c96SDimitry Andric   const auto &triple = arch.GetTriple();
3114f1b3e8SDimitry Andric   switch (triple.getOS()) {
327fa27ce4SDimitry Andric   case llvm::Triple::Linux:
33027f1c96SDimitry Andric     return std::make_shared<LinuxSignals>();
34027f1c96SDimitry Andric   case llvm::Triple::FreeBSD:
35027f1c96SDimitry Andric   case llvm::Triple::OpenBSD:
36027f1c96SDimitry Andric     return std::make_shared<FreeBSDSignals>();
37e81d9d49SDimitry Andric   case llvm::Triple::NetBSD:
38e81d9d49SDimitry Andric     return std::make_shared<NetBSDSignals>();
39027f1c96SDimitry Andric   default:
40027f1c96SDimitry Andric     return std::make_shared<UnixSignals>();
41027f1c96SDimitry Andric   }
42027f1c96SDimitry Andric }
43027f1c96SDimitry Andric 
CreateForHost()445f29bb8aSDimitry Andric lldb::UnixSignalsSP UnixSignals::CreateForHost() {
455f29bb8aSDimitry Andric   static lldb::UnixSignalsSP s_unix_signals_sp =
465f29bb8aSDimitry Andric       Create(HostInfo::GetArchitecture());
475f29bb8aSDimitry Andric   return s_unix_signals_sp;
485f29bb8aSDimitry Andric }
495f29bb8aSDimitry Andric 
50f034231aSEd Maste // UnixSignals constructor
UnixSignals()5114f1b3e8SDimitry Andric UnixSignals::UnixSignals() { Reset(); }
52f034231aSEd Maste 
UnixSignals(const UnixSignals & rhs)5314f1b3e8SDimitry Andric UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
54027f1c96SDimitry Andric 
55f3fbd1c0SDimitry Andric UnixSignals::~UnixSignals() = default;
56f034231aSEd Maste 
Reset()5714f1b3e8SDimitry Andric void UnixSignals::Reset() {
5814f1b3e8SDimitry Andric   // This builds one standard set of Unix Signals. If yours aren't quite in
59f73363f1SDimitry Andric   // this order, you can either subclass this class, and use Add & Remove to
60b60736ecSDimitry Andric   // change them or you can subclass and build them afresh in your constructor.
6114f1b3e8SDimitry Andric   //
6214f1b3e8SDimitry Andric   // Note: the signals below are the Darwin signals. Do not change these!
63b60736ecSDimitry Andric 
64f034231aSEd Maste   m_signals.clear();
65b60736ecSDimitry Andric 
66b60736ecSDimitry Andric   // clang-format off
67e81d9d49SDimitry Andric   //        SIGNO   NAME            SUPPRESS  STOP    NOTIFY  DESCRIPTION
68b60736ecSDimitry Andric   //        ======  ==============  ========  ======  ======  ===================================================
69e81d9d49SDimitry Andric   AddSignal(1,      "SIGHUP",       false,    true,   true,   "hangup");
70e81d9d49SDimitry Andric   AddSignal(2,      "SIGINT",       true,     true,   true,   "interrupt");
71e81d9d49SDimitry Andric   AddSignal(3,      "SIGQUIT",      false,    true,   true,   "quit");
72e81d9d49SDimitry Andric   AddSignal(4,      "SIGILL",       false,    true,   true,   "illegal instruction");
73b60736ecSDimitry Andric   AddSignal(5,      "SIGTRAP",      true,     true,   true,   "trace trap (not reset when caught)");
74e81d9d49SDimitry Andric   AddSignal(6,      "SIGABRT",      false,    true,   true,   "abort()");
75e81d9d49SDimitry Andric   AddSignal(7,      "SIGEMT",       false,    true,   true,   "pollable event");
76e81d9d49SDimitry Andric   AddSignal(8,      "SIGFPE",       false,    true,   true,   "floating point exception");
77e81d9d49SDimitry Andric   AddSignal(9,      "SIGKILL",      false,    true,   true,   "kill");
78e81d9d49SDimitry Andric   AddSignal(10,     "SIGBUS",       false,    true,   true,   "bus error");
79e81d9d49SDimitry Andric   AddSignal(11,     "SIGSEGV",      false,    true,   true,   "segmentation violation");
80e81d9d49SDimitry Andric   AddSignal(12,     "SIGSYS",       false,    true,   true,   "bad argument to system call");
81b60736ecSDimitry Andric   AddSignal(13,     "SIGPIPE",      false,    false,  false,  "write on a pipe with no one to read it");
82e81d9d49SDimitry Andric   AddSignal(14,     "SIGALRM",      false,    false,  false,  "alarm clock");
83b60736ecSDimitry Andric   AddSignal(15,     "SIGTERM",      false,    true,   true,   "software termination signal from kill");
84b60736ecSDimitry Andric   AddSignal(16,     "SIGURG",       false,    false,  false,  "urgent condition on IO channel");
85b60736ecSDimitry Andric   AddSignal(17,     "SIGSTOP",      true,     true,   true,   "sendable stop signal not from tty");
86e81d9d49SDimitry Andric   AddSignal(18,     "SIGTSTP",      false,    true,   true,   "stop signal from tty");
87b60736ecSDimitry Andric   AddSignal(19,     "SIGCONT",      false,    false,  true,   "continue a stopped process");
88b60736ecSDimitry Andric   AddSignal(20,     "SIGCHLD",      false,    false,  false,  "to parent on child stop or exit");
89b60736ecSDimitry Andric   AddSignal(21,     "SIGTTIN",      false,    true,   true,   "to readers process group upon background tty read");
90b60736ecSDimitry Andric   AddSignal(22,     "SIGTTOU",      false,    true,   true,   "to readers process group upon background tty write");
91e81d9d49SDimitry Andric   AddSignal(23,     "SIGIO",        false,    false,  false,  "input/output possible signal");
92e81d9d49SDimitry Andric   AddSignal(24,     "SIGXCPU",      false,    true,   true,   "exceeded CPU time limit");
93e81d9d49SDimitry Andric   AddSignal(25,     "SIGXFSZ",      false,    true,   true,   "exceeded file size limit");
94e81d9d49SDimitry Andric   AddSignal(26,     "SIGVTALRM",    false,    false,  false,  "virtual time alarm");
95e81d9d49SDimitry Andric   AddSignal(27,     "SIGPROF",      false,    false,  false,  "profiling time alarm");
96e81d9d49SDimitry Andric   AddSignal(28,     "SIGWINCH",     false,    false,  false,  "window size changes");
97e81d9d49SDimitry Andric   AddSignal(29,     "SIGINFO",      false,    true,   true,   "information request");
98e81d9d49SDimitry Andric   AddSignal(30,     "SIGUSR1",      false,    true,   true,   "user defined signal 1");
99e81d9d49SDimitry Andric   AddSignal(31,     "SIGUSR2",      false,    true,   true,   "user defined signal 2");
100b60736ecSDimitry Andric   // clang-format on
101f034231aSEd Maste }
102f034231aSEd Maste 
AddSignal(int signo,llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)103b1c73532SDimitry Andric void UnixSignals::AddSignal(int signo, llvm::StringRef name,
104b1c73532SDimitry Andric                             bool default_suppress, bool default_stop,
105b1c73532SDimitry Andric                             bool default_notify, llvm::StringRef description,
106b1c73532SDimitry Andric                             llvm::StringRef alias) {
10714f1b3e8SDimitry Andric   Signal new_signal(name, default_suppress, default_stop, default_notify,
10814f1b3e8SDimitry Andric                     description, alias);
109f034231aSEd Maste   m_signals.insert(std::make_pair(signo, new_signal));
11074a628f7SDimitry Andric   ++m_version;
111f034231aSEd Maste }
112f034231aSEd Maste 
AddSignalCode(int signo,int code,const llvm::StringLiteral description,SignalCodePrintOption print_option)1137fa27ce4SDimitry Andric void UnixSignals::AddSignalCode(int signo, int code,
1147fa27ce4SDimitry Andric                                 const llvm::StringLiteral description,
1157fa27ce4SDimitry Andric                                 SignalCodePrintOption print_option) {
1167fa27ce4SDimitry Andric   collection::iterator signal = m_signals.find(signo);
1177fa27ce4SDimitry Andric   assert(signal != m_signals.end() &&
1187fa27ce4SDimitry Andric          "Tried to add code to signal that does not exist.");
1197fa27ce4SDimitry Andric   signal->second.m_codes.insert(
1207fa27ce4SDimitry Andric       std::pair{code, SignalCode{description, print_option}});
1217fa27ce4SDimitry Andric   ++m_version;
1227fa27ce4SDimitry Andric }
1237fa27ce4SDimitry Andric 
RemoveSignal(int signo)12414f1b3e8SDimitry Andric void UnixSignals::RemoveSignal(int signo) {
125f034231aSEd Maste   collection::iterator pos = m_signals.find(signo);
126f034231aSEd Maste   if (pos != m_signals.end())
127f034231aSEd Maste     m_signals.erase(pos);
12874a628f7SDimitry Andric   ++m_version;
129f034231aSEd Maste }
130f034231aSEd Maste 
GetSignalAsStringRef(int32_t signo) const131b1c73532SDimitry Andric llvm::StringRef UnixSignals::GetSignalAsStringRef(int32_t signo) const {
132b1c73532SDimitry Andric   const auto pos = m_signals.find(signo);
133f034231aSEd Maste   if (pos == m_signals.end())
134b1c73532SDimitry Andric     return {};
135b1c73532SDimitry Andric   return pos->second.m_name;
136f034231aSEd Maste }
137f034231aSEd Maste 
1387fa27ce4SDimitry 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) const1397fa27ce4SDimitry Andric UnixSignals::GetSignalDescription(int32_t signo, std::optional<int32_t> code,
1407fa27ce4SDimitry Andric                                   std::optional<lldb::addr_t> addr,
1417fa27ce4SDimitry Andric                                   std::optional<lldb::addr_t> lower,
1427fa27ce4SDimitry Andric                                   std::optional<lldb::addr_t> upper) const {
1437fa27ce4SDimitry Andric   std::string str;
1447fa27ce4SDimitry Andric 
1457fa27ce4SDimitry Andric   collection::const_iterator pos = m_signals.find(signo);
1467fa27ce4SDimitry Andric   if (pos != m_signals.end()) {
147b1c73532SDimitry Andric     str = pos->second.m_name.str();
1487fa27ce4SDimitry Andric 
1497fa27ce4SDimitry Andric     if (code) {
1507fa27ce4SDimitry Andric       std::map<int32_t, SignalCode>::const_iterator cpos =
1517fa27ce4SDimitry Andric           pos->second.m_codes.find(*code);
1527fa27ce4SDimitry Andric       if (cpos != pos->second.m_codes.end()) {
1537fa27ce4SDimitry Andric         const SignalCode &sc = cpos->second;
1547fa27ce4SDimitry Andric         str += ": ";
1557fa27ce4SDimitry Andric         if (sc.m_print_option != SignalCodePrintOption::Bounds)
1567fa27ce4SDimitry Andric           str += sc.m_description.str();
1577fa27ce4SDimitry Andric 
1587fa27ce4SDimitry Andric         std::stringstream strm;
1597fa27ce4SDimitry Andric         switch (sc.m_print_option) {
1607fa27ce4SDimitry Andric         case SignalCodePrintOption::None:
1617fa27ce4SDimitry Andric           break;
1627fa27ce4SDimitry Andric         case SignalCodePrintOption::Address:
1637fa27ce4SDimitry Andric           if (addr)
1647fa27ce4SDimitry Andric             strm << " (fault address: 0x" << std::hex << *addr << ")";
1657fa27ce4SDimitry Andric           break;
1667fa27ce4SDimitry Andric         case SignalCodePrintOption::Bounds:
1677fa27ce4SDimitry Andric           if (lower && upper && addr) {
1687fa27ce4SDimitry Andric             if ((unsigned long)(*addr) < *lower)
1697fa27ce4SDimitry Andric               strm << "lower bound violation ";
1707fa27ce4SDimitry Andric             else
1717fa27ce4SDimitry Andric               strm << "upper bound violation ";
1727fa27ce4SDimitry Andric 
1737fa27ce4SDimitry Andric             strm << "(fault address: 0x" << std::hex << *addr;
1747fa27ce4SDimitry Andric             strm << ", lower bound: 0x" << std::hex << *lower;
1757fa27ce4SDimitry Andric             strm << ", upper bound: 0x" << std::hex << *upper;
1767fa27ce4SDimitry Andric             strm << ")";
1777fa27ce4SDimitry Andric           } else
1787fa27ce4SDimitry Andric             strm << sc.m_description.str();
1797fa27ce4SDimitry Andric 
1807fa27ce4SDimitry Andric           break;
1817fa27ce4SDimitry Andric         }
1827fa27ce4SDimitry Andric         str += strm.str();
1837fa27ce4SDimitry Andric       }
1847fa27ce4SDimitry Andric     }
1857fa27ce4SDimitry Andric   }
1867fa27ce4SDimitry Andric 
1877fa27ce4SDimitry Andric   return str;
1887fa27ce4SDimitry Andric }
1897fa27ce4SDimitry Andric 
SignalIsValid(int32_t signo) const19014f1b3e8SDimitry Andric bool UnixSignals::SignalIsValid(int32_t signo) const {
191f034231aSEd Maste   return m_signals.find(signo) != m_signals.end();
192f034231aSEd Maste }
193f034231aSEd Maste 
GetShortName(llvm::StringRef name) const1947fa27ce4SDimitry Andric llvm::StringRef UnixSignals::GetShortName(llvm::StringRef name) const {
1957fa27ce4SDimitry Andric   return name.substr(3); // Remove "SIG" from name
196e81d9d49SDimitry Andric }
197f034231aSEd Maste 
GetSignalNumberFromName(const char * name) const19814f1b3e8SDimitry Andric int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
199b1c73532SDimitry Andric   llvm::StringRef name_ref(name);
200f034231aSEd Maste 
201f034231aSEd Maste   collection::const_iterator pos, end = m_signals.end();
20214f1b3e8SDimitry Andric   for (pos = m_signals.begin(); pos != end; pos++) {
203b1c73532SDimitry Andric     if ((name_ref == pos->second.m_name) || (name_ref == pos->second.m_alias) ||
204b1c73532SDimitry Andric         (name_ref == GetShortName(pos->second.m_name)) ||
205b1c73532SDimitry Andric         (name_ref == GetShortName(pos->second.m_alias)))
206f034231aSEd Maste       return pos->first;
207f034231aSEd Maste   }
208f034231aSEd Maste 
209c0981da4SDimitry Andric   int32_t signo;
210c0981da4SDimitry Andric   if (llvm::to_integer(name, signo))
211f034231aSEd Maste     return signo;
212f034231aSEd Maste   return LLDB_INVALID_SIGNAL_NUMBER;
213f034231aSEd Maste }
214f034231aSEd Maste 
GetFirstSignalNumber() const21514f1b3e8SDimitry Andric int32_t UnixSignals::GetFirstSignalNumber() const {
216f034231aSEd Maste   if (m_signals.empty())
217f034231aSEd Maste     return LLDB_INVALID_SIGNAL_NUMBER;
218f034231aSEd Maste 
219f034231aSEd Maste   return (*m_signals.begin()).first;
220f034231aSEd Maste }
221f034231aSEd Maste 
GetNextSignalNumber(int32_t current_signal) const22214f1b3e8SDimitry Andric int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
223f034231aSEd Maste   collection::const_iterator pos = m_signals.find(current_signal);
224f034231aSEd Maste   collection::const_iterator end = m_signals.end();
225f034231aSEd Maste   if (pos == end)
226f034231aSEd Maste     return LLDB_INVALID_SIGNAL_NUMBER;
22714f1b3e8SDimitry Andric   else {
228f034231aSEd Maste     pos++;
229f034231aSEd Maste     if (pos == end)
230f034231aSEd Maste       return LLDB_INVALID_SIGNAL_NUMBER;
231f034231aSEd Maste     else
232f034231aSEd Maste       return pos->first;
233f034231aSEd Maste   }
234f034231aSEd Maste }
235f034231aSEd Maste 
GetSignalInfo(int32_t signo,bool & should_suppress,bool & should_stop,bool & should_notify) const236b1c73532SDimitry Andric bool UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
237b1c73532SDimitry Andric                                 bool &should_stop, bool &should_notify) const {
238b1c73532SDimitry Andric   const auto pos = m_signals.find(signo);
239f034231aSEd Maste   if (pos == m_signals.end())
240b1c73532SDimitry Andric     return false;
241b1c73532SDimitry Andric 
242f034231aSEd Maste   const Signal &signal = pos->second;
243f034231aSEd Maste   should_suppress = signal.m_suppress;
244f034231aSEd Maste   should_stop = signal.m_stop;
245f034231aSEd Maste   should_notify = signal.m_notify;
246b1c73532SDimitry Andric   return true;
247f034231aSEd Maste }
248f034231aSEd Maste 
GetShouldSuppress(int signo) const24914f1b3e8SDimitry Andric bool UnixSignals::GetShouldSuppress(int signo) const {
250f034231aSEd Maste   collection::const_iterator pos = m_signals.find(signo);
251f034231aSEd Maste   if (pos != m_signals.end())
252f034231aSEd Maste     return pos->second.m_suppress;
253f034231aSEd Maste   return false;
254f034231aSEd Maste }
255f034231aSEd Maste 
SetShouldSuppress(int signo,bool value)25614f1b3e8SDimitry Andric bool UnixSignals::SetShouldSuppress(int signo, bool value) {
257f034231aSEd Maste   collection::iterator pos = m_signals.find(signo);
25814f1b3e8SDimitry Andric   if (pos != m_signals.end()) {
259f034231aSEd Maste     pos->second.m_suppress = value;
26074a628f7SDimitry Andric     ++m_version;
261f034231aSEd Maste     return true;
262f034231aSEd Maste   }
263f034231aSEd Maste   return false;
264f034231aSEd Maste }
265f034231aSEd Maste 
SetShouldSuppress(const char * signal_name,bool value)26614f1b3e8SDimitry Andric bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
267f034231aSEd Maste   const int32_t signo = GetSignalNumberFromName(signal_name);
268f034231aSEd Maste   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
269f034231aSEd Maste     return SetShouldSuppress(signo, value);
270f034231aSEd Maste   return false;
271f034231aSEd Maste }
272f034231aSEd Maste 
GetShouldStop(int signo) const27314f1b3e8SDimitry Andric bool UnixSignals::GetShouldStop(int signo) const {
274f034231aSEd Maste   collection::const_iterator pos = m_signals.find(signo);
275f034231aSEd Maste   if (pos != m_signals.end())
276f034231aSEd Maste     return pos->second.m_stop;
277f034231aSEd Maste   return false;
278f034231aSEd Maste }
279f034231aSEd Maste 
SetShouldStop(int signo,bool value)28014f1b3e8SDimitry Andric bool UnixSignals::SetShouldStop(int signo, bool value) {
281f034231aSEd Maste   collection::iterator pos = m_signals.find(signo);
28214f1b3e8SDimitry Andric   if (pos != m_signals.end()) {
283f034231aSEd Maste     pos->second.m_stop = value;
28474a628f7SDimitry Andric     ++m_version;
285f034231aSEd Maste     return true;
286f034231aSEd Maste   }
287f034231aSEd Maste   return false;
288f034231aSEd Maste }
289f034231aSEd Maste 
SetShouldStop(const char * signal_name,bool value)29014f1b3e8SDimitry Andric bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
291f034231aSEd Maste   const int32_t signo = GetSignalNumberFromName(signal_name);
292f034231aSEd Maste   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
293f034231aSEd Maste     return SetShouldStop(signo, value);
294f034231aSEd Maste   return false;
295f034231aSEd Maste }
296f034231aSEd Maste 
GetShouldNotify(int signo) const29714f1b3e8SDimitry Andric bool UnixSignals::GetShouldNotify(int signo) const {
298f034231aSEd Maste   collection::const_iterator pos = m_signals.find(signo);
299f034231aSEd Maste   if (pos != m_signals.end())
300f034231aSEd Maste     return pos->second.m_notify;
301f034231aSEd Maste   return false;
302f034231aSEd Maste }
303f034231aSEd Maste 
SetShouldNotify(int signo,bool value)30414f1b3e8SDimitry Andric bool UnixSignals::SetShouldNotify(int signo, bool value) {
305f034231aSEd Maste   collection::iterator pos = m_signals.find(signo);
30614f1b3e8SDimitry Andric   if (pos != m_signals.end()) {
307f034231aSEd Maste     pos->second.m_notify = value;
30874a628f7SDimitry Andric     ++m_version;
309f034231aSEd Maste     return true;
310f034231aSEd Maste   }
311f034231aSEd Maste   return false;
312f034231aSEd Maste }
313f034231aSEd Maste 
SetShouldNotify(const char * signal_name,bool value)31414f1b3e8SDimitry Andric bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
315f034231aSEd Maste   const int32_t signo = GetSignalNumberFromName(signal_name);
316f034231aSEd Maste   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
317f034231aSEd Maste     return SetShouldNotify(signo, value);
318f034231aSEd Maste   return false;
319f034231aSEd Maste }
320027f1c96SDimitry Andric 
GetNumSignals() const32114f1b3e8SDimitry Andric int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
322027f1c96SDimitry Andric 
GetSignalAtIndex(int32_t index) const32314f1b3e8SDimitry Andric int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
324027f1c96SDimitry Andric   if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
325027f1c96SDimitry Andric     return LLDB_INVALID_SIGNAL_NUMBER;
326027f1c96SDimitry Andric   auto it = m_signals.begin();
327027f1c96SDimitry Andric   std::advance(it, index);
328027f1c96SDimitry Andric   return it->first;
329027f1c96SDimitry Andric }
33074a628f7SDimitry Andric 
GetVersion() const33174a628f7SDimitry Andric uint64_t UnixSignals::GetVersion() const { return m_version; }
33274a628f7SDimitry Andric 
33374a628f7SDimitry Andric std::vector<int32_t>
GetFilteredSignals(std::optional<bool> should_suppress,std::optional<bool> should_stop,std::optional<bool> should_notify)334e3b55780SDimitry Andric UnixSignals::GetFilteredSignals(std::optional<bool> should_suppress,
335e3b55780SDimitry Andric                                 std::optional<bool> should_stop,
336e3b55780SDimitry Andric                                 std::optional<bool> should_notify) {
33774a628f7SDimitry Andric   std::vector<int32_t> result;
33874a628f7SDimitry Andric   for (int32_t signo = GetFirstSignalNumber();
33974a628f7SDimitry Andric        signo != LLDB_INVALID_SIGNAL_NUMBER;
34074a628f7SDimitry Andric        signo = GetNextSignalNumber(signo)) {
34174a628f7SDimitry Andric 
34274a628f7SDimitry Andric     bool signal_suppress = false;
34374a628f7SDimitry Andric     bool signal_stop = false;
34474a628f7SDimitry Andric     bool signal_notify = false;
34574a628f7SDimitry Andric     GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
34674a628f7SDimitry Andric 
347f73363f1SDimitry Andric     // If any of filtering conditions are not met, we move on to the next
348f73363f1SDimitry Andric     // signal.
349e3b55780SDimitry Andric     if (should_suppress && signal_suppress != *should_suppress)
35074a628f7SDimitry Andric       continue;
35174a628f7SDimitry Andric 
352e3b55780SDimitry Andric     if (should_stop && signal_stop != *should_stop)
35374a628f7SDimitry Andric       continue;
35474a628f7SDimitry Andric 
355e3b55780SDimitry Andric     if (should_notify && signal_notify != *should_notify)
35674a628f7SDimitry Andric       continue;
35774a628f7SDimitry Andric 
35874a628f7SDimitry Andric     result.push_back(signo);
35974a628f7SDimitry Andric   }
36074a628f7SDimitry Andric 
36174a628f7SDimitry Andric   return result;
36274a628f7SDimitry Andric }
363c0981da4SDimitry Andric 
IncrementSignalHitCount(int signo)364c0981da4SDimitry Andric void UnixSignals::IncrementSignalHitCount(int signo) {
365c0981da4SDimitry Andric   collection::iterator pos = m_signals.find(signo);
366c0981da4SDimitry Andric   if (pos != m_signals.end())
367c0981da4SDimitry Andric     pos->second.m_hit_count += 1;
368c0981da4SDimitry Andric }
369c0981da4SDimitry Andric 
GetHitCountStatistics() const370c0981da4SDimitry Andric json::Value UnixSignals::GetHitCountStatistics() const {
371c0981da4SDimitry Andric   json::Array json_signals;
372c0981da4SDimitry Andric   for (const auto &pair : m_signals) {
373c0981da4SDimitry Andric     if (pair.second.m_hit_count > 0)
374b1c73532SDimitry Andric       json_signals.emplace_back(
375b1c73532SDimitry Andric           json::Object{{pair.second.m_name, pair.second.m_hit_count}});
376c0981da4SDimitry Andric   }
377c0981da4SDimitry Andric   return std::move(json_signals);
378c0981da4SDimitry Andric }
379145449b1SDimitry Andric 
Reset(bool reset_stop,bool reset_notify,bool reset_suppress)380145449b1SDimitry Andric void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify,
381145449b1SDimitry Andric                                 bool reset_suppress) {
382145449b1SDimitry Andric   if (reset_stop)
383145449b1SDimitry Andric     m_stop = m_default_stop;
384145449b1SDimitry Andric   if (reset_notify)
385145449b1SDimitry Andric     m_notify = m_default_notify;
386145449b1SDimitry Andric   if (reset_suppress)
387145449b1SDimitry Andric     m_suppress = m_default_suppress;
388145449b1SDimitry Andric }
389145449b1SDimitry Andric 
ResetSignal(int32_t signo,bool reset_stop,bool reset_notify,bool reset_suppress)390145449b1SDimitry Andric bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop,
391145449b1SDimitry Andric                                  bool reset_notify, bool reset_suppress) {
392145449b1SDimitry Andric     auto elem = m_signals.find(signo);
393145449b1SDimitry Andric     if (elem == m_signals.end())
394145449b1SDimitry Andric       return false;
395145449b1SDimitry Andric     (*elem).second.Reset(reset_stop, reset_notify, reset_suppress);
396145449b1SDimitry Andric     return true;
397145449b1SDimitry Andric }
398145449b1SDimitry Andric 
399