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