1 //===-- UnixSignals.cpp -----------------------------------------*- C++ -*-===//
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 "lldb/Target/UnixSignals.h"
10 #include "Plugins/Process/Utility/FreeBSDSignals.h"
11 #include "Plugins/Process/Utility/LinuxSignals.h"
12 #include "Plugins/Process/Utility/MipsLinuxSignals.h"
13 #include "Plugins/Process/Utility/NetBSDSignals.h"
14 #include "Plugins/Process/Utility/OpenBSDSignals.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Host/StringConvert.h"
17 #include "lldb/Utility/ArchSpec.h"
18 
19 using namespace lldb_private;
20 
21 UnixSignals::Signal::Signal(const char *name, bool default_suppress,
22                             bool default_stop, bool default_notify,
23                             const char *description, const char *alias)
24     : m_name(name), m_alias(alias), m_description(),
25       m_suppress(default_suppress), m_stop(default_stop),
26       m_notify(default_notify) {
27   if (description)
28     m_description.assign(description);
29 }
30 
31 lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) {
32   const auto &triple = arch.GetTriple();
33   switch (triple.getOS()) {
34   case llvm::Triple::Linux: {
35     switch (triple.getArch()) {
36     case llvm::Triple::mips:
37     case llvm::Triple::mipsel:
38     case llvm::Triple::mips64:
39     case llvm::Triple::mips64el:
40       return std::make_shared<MipsLinuxSignals>();
41     default:
42       return std::make_shared<LinuxSignals>();
43     }
44   }
45   case llvm::Triple::FreeBSD:
46     return std::make_shared<FreeBSDSignals>();
47   case llvm::Triple::OpenBSD:
48     return std::make_shared<OpenBSDSignals>();
49   case llvm::Triple::NetBSD:
50     return std::make_shared<NetBSDSignals>();
51   default:
52     return std::make_shared<UnixSignals>();
53   }
54 }
55 
56 lldb::UnixSignalsSP UnixSignals::CreateForHost() {
57   static lldb::UnixSignalsSP s_unix_signals_sp =
58       Create(HostInfo::GetArchitecture());
59   return s_unix_signals_sp;
60 }
61 
62 // UnixSignals constructor
63 UnixSignals::UnixSignals() { Reset(); }
64 
65 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
66 
67 UnixSignals::~UnixSignals() = default;
68 
69 void UnixSignals::Reset() {
70   // This builds one standard set of Unix Signals.  If yours aren't quite in
71   // this order, you can either subclass this class, and use Add & Remove to
72   // change them
73   // or you can subclass and build them afresh in your constructor;
74   //
75   // Note: the signals below are the Darwin signals.  Do not change these!
76   m_signals.clear();
77   //        SIGNO  NAME          SUPPRESS STOP   NOTIFY DESCRIPTION
78   //        ====== ============  ======== ====== ======
79   //        ===================================================
80   AddSignal(1, "SIGHUP", false, true, true, "hangup");
81   AddSignal(2, "SIGINT", true, true, true, "interrupt");
82   AddSignal(3, "SIGQUIT", false, true, true, "quit");
83   AddSignal(4, "SIGILL", false, true, true, "illegal instruction");
84   AddSignal(5, "SIGTRAP", true, true, true,
85             "trace trap (not reset when caught)");
86   AddSignal(6, "SIGABRT", false, true, true, "abort()");
87   AddSignal(7, "SIGEMT", false, true, true, "pollable event");
88   AddSignal(8, "SIGFPE", false, true, true, "floating point exception");
89   AddSignal(9, "SIGKILL", false, true, true, "kill");
90   AddSignal(10, "SIGBUS", false, true, true, "bus error");
91   AddSignal(11, "SIGSEGV", false, true, true, "segmentation violation");
92   AddSignal(12, "SIGSYS", false, true, true, "bad argument to system call");
93   AddSignal(13, "SIGPIPE", false, false, false,
94             "write on a pipe with no one to read it");
95   AddSignal(14, "SIGALRM", false, false, false, "alarm clock");
96   AddSignal(15, "SIGTERM", false, true, true,
97             "software termination signal from kill");
98   AddSignal(16, "SIGURG", false, false, false,
99             "urgent condition on IO channel");
100   AddSignal(17, "SIGSTOP", true, true, true,
101             "sendable stop signal not from tty");
102   AddSignal(18, "SIGTSTP", false, true, true, "stop signal from tty");
103   AddSignal(19, "SIGCONT", false, true, true, "continue a stopped process");
104   AddSignal(20, "SIGCHLD", false, false, false,
105             "to parent on child stop or exit");
106   AddSignal(21, "SIGTTIN", false, true, true,
107             "to readers process group upon background tty read");
108   AddSignal(22, "SIGTTOU", false, true, true,
109             "to readers process group upon background tty write");
110   AddSignal(23, "SIGIO", false, false, false, "input/output possible signal");
111   AddSignal(24, "SIGXCPU", false, true, true, "exceeded CPU time limit");
112   AddSignal(25, "SIGXFSZ", false, true, true, "exceeded file size limit");
113   AddSignal(26, "SIGVTALRM", false, false, false, "virtual time alarm");
114   AddSignal(27, "SIGPROF", false, false, false, "profiling time alarm");
115   AddSignal(28, "SIGWINCH", false, false, false, "window size changes");
116   AddSignal(29, "SIGINFO", false, true, true, "information request");
117   AddSignal(30, "SIGUSR1", false, true, true, "user defined signal 1");
118   AddSignal(31, "SIGUSR2", false, true, true, "user defined signal 2");
119 }
120 
121 void UnixSignals::AddSignal(int signo, const char *name, bool default_suppress,
122                             bool default_stop, bool default_notify,
123                             const char *description, const char *alias) {
124   Signal new_signal(name, default_suppress, default_stop, default_notify,
125                     description, alias);
126   m_signals.insert(std::make_pair(signo, new_signal));
127   ++m_version;
128 }
129 
130 void UnixSignals::RemoveSignal(int signo) {
131   collection::iterator pos = m_signals.find(signo);
132   if (pos != m_signals.end())
133     m_signals.erase(pos);
134   ++m_version;
135 }
136 
137 const char *UnixSignals::GetSignalAsCString(int signo) const {
138   collection::const_iterator pos = m_signals.find(signo);
139   if (pos == m_signals.end())
140     return nullptr;
141   else
142     return pos->second.m_name.GetCString();
143 }
144 
145 bool UnixSignals::SignalIsValid(int32_t signo) const {
146   return m_signals.find(signo) != m_signals.end();
147 }
148 
149 ConstString UnixSignals::GetShortName(ConstString name) const {
150   if (name) {
151     const char *signame = name.AsCString();
152     return ConstString(signame + 3); // Remove "SIG" from name
153   }
154   return name;
155 }
156 
157 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
158   ConstString const_name(name);
159 
160   collection::const_iterator pos, end = m_signals.end();
161   for (pos = m_signals.begin(); pos != end; pos++) {
162     if ((const_name == pos->second.m_name) ||
163         (const_name == pos->second.m_alias) ||
164         (const_name == GetShortName(pos->second.m_name)) ||
165         (const_name == GetShortName(pos->second.m_alias)))
166       return pos->first;
167   }
168 
169   const int32_t signo =
170       StringConvert::ToSInt32(name, LLDB_INVALID_SIGNAL_NUMBER, 0);
171   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
172     return signo;
173   return LLDB_INVALID_SIGNAL_NUMBER;
174 }
175 
176 int32_t UnixSignals::GetFirstSignalNumber() const {
177   if (m_signals.empty())
178     return LLDB_INVALID_SIGNAL_NUMBER;
179 
180   return (*m_signals.begin()).first;
181 }
182 
183 int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
184   collection::const_iterator pos = m_signals.find(current_signal);
185   collection::const_iterator end = m_signals.end();
186   if (pos == end)
187     return LLDB_INVALID_SIGNAL_NUMBER;
188   else {
189     pos++;
190     if (pos == end)
191       return LLDB_INVALID_SIGNAL_NUMBER;
192     else
193       return pos->first;
194   }
195 }
196 
197 const char *UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
198                                        bool &should_stop,
199                                        bool &should_notify) const {
200   collection::const_iterator pos = m_signals.find(signo);
201   if (pos == m_signals.end())
202     return nullptr;
203   else {
204     const Signal &signal = pos->second;
205     should_suppress = signal.m_suppress;
206     should_stop = signal.m_stop;
207     should_notify = signal.m_notify;
208     return signal.m_name.AsCString("");
209   }
210 }
211 
212 bool UnixSignals::GetShouldSuppress(int signo) const {
213   collection::const_iterator pos = m_signals.find(signo);
214   if (pos != m_signals.end())
215     return pos->second.m_suppress;
216   return false;
217 }
218 
219 bool UnixSignals::SetShouldSuppress(int signo, bool value) {
220   collection::iterator pos = m_signals.find(signo);
221   if (pos != m_signals.end()) {
222     pos->second.m_suppress = value;
223     ++m_version;
224     return true;
225   }
226   return false;
227 }
228 
229 bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
230   const int32_t signo = GetSignalNumberFromName(signal_name);
231   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
232     return SetShouldSuppress(signo, value);
233   return false;
234 }
235 
236 bool UnixSignals::GetShouldStop(int signo) const {
237   collection::const_iterator pos = m_signals.find(signo);
238   if (pos != m_signals.end())
239     return pos->second.m_stop;
240   return false;
241 }
242 
243 bool UnixSignals::SetShouldStop(int signo, bool value) {
244   collection::iterator pos = m_signals.find(signo);
245   if (pos != m_signals.end()) {
246     pos->second.m_stop = value;
247     ++m_version;
248     return true;
249   }
250   return false;
251 }
252 
253 bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
254   const int32_t signo = GetSignalNumberFromName(signal_name);
255   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
256     return SetShouldStop(signo, value);
257   return false;
258 }
259 
260 bool UnixSignals::GetShouldNotify(int signo) const {
261   collection::const_iterator pos = m_signals.find(signo);
262   if (pos != m_signals.end())
263     return pos->second.m_notify;
264   return false;
265 }
266 
267 bool UnixSignals::SetShouldNotify(int signo, bool value) {
268   collection::iterator pos = m_signals.find(signo);
269   if (pos != m_signals.end()) {
270     pos->second.m_notify = value;
271     ++m_version;
272     return true;
273   }
274   return false;
275 }
276 
277 bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
278   const int32_t signo = GetSignalNumberFromName(signal_name);
279   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
280     return SetShouldNotify(signo, value);
281   return false;
282 }
283 
284 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
285 
286 int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
287   if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
288     return LLDB_INVALID_SIGNAL_NUMBER;
289   auto it = m_signals.begin();
290   std::advance(it, index);
291   return it->first;
292 }
293 
294 uint64_t UnixSignals::GetVersion() const { return m_version; }
295 
296 std::vector<int32_t>
297 UnixSignals::GetFilteredSignals(llvm::Optional<bool> should_suppress,
298                                 llvm::Optional<bool> should_stop,
299                                 llvm::Optional<bool> should_notify) {
300   std::vector<int32_t> result;
301   for (int32_t signo = GetFirstSignalNumber();
302        signo != LLDB_INVALID_SIGNAL_NUMBER;
303        signo = GetNextSignalNumber(signo)) {
304 
305     bool signal_suppress = false;
306     bool signal_stop = false;
307     bool signal_notify = false;
308     GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
309 
310     // If any of filtering conditions are not met, we move on to the next
311     // signal.
312     if (should_suppress.hasValue() &&
313         signal_suppress != should_suppress.getValue())
314       continue;
315 
316     if (should_stop.hasValue() && signal_stop != should_stop.getValue())
317       continue;
318 
319     if (should_notify.hasValue() && signal_notify != should_notify.getValue())
320       continue;
321 
322     result.push_back(signo);
323   }
324 
325   return result;
326 }
327