xref: /freebsd-src/contrib/llvm-project/lldb/source/Target/UnixSignals.cpp (revision 8cc087a1eee9ec1ca9f7ac1e63ad51bdb5a682eb)
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/Utility/ArchSpec.h"
16 
17 using namespace lldb_private;
18 using namespace llvm;
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 or you can subclass and build them afresh in your constructor.
71   //
72   // Note: the signals below are the Darwin signals. Do not change these!
73 
74   m_signals.clear();
75 
76   // clang-format off
77   //        SIGNO   NAME            SUPPRESS  STOP    NOTIFY  DESCRIPTION
78   //        ======  ==============  ========  ======  ======  ===================================================
79   AddSignal(1,      "SIGHUP",       false,    true,   true,   "hangup");
80   AddSignal(2,      "SIGINT",       true,     true,   true,   "interrupt");
81   AddSignal(3,      "SIGQUIT",      false,    true,   true,   "quit");
82   AddSignal(4,      "SIGILL",       false,    true,   true,   "illegal instruction");
83   AddSignal(5,      "SIGTRAP",      true,     true,   true,   "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,  "write on a pipe with no one to read it");
92   AddSignal(14,     "SIGALRM",      false,    false,  false,  "alarm clock");
93   AddSignal(15,     "SIGTERM",      false,    true,   true,   "software termination signal from kill");
94   AddSignal(16,     "SIGURG",       false,    false,  false,  "urgent condition on IO channel");
95   AddSignal(17,     "SIGSTOP",      true,     true,   true,   "sendable stop signal not from tty");
96   AddSignal(18,     "SIGTSTP",      false,    true,   true,   "stop signal from tty");
97   AddSignal(19,     "SIGCONT",      false,    false,  true,   "continue a stopped process");
98   AddSignal(20,     "SIGCHLD",      false,    false,  false,  "to parent on child stop or exit");
99   AddSignal(21,     "SIGTTIN",      false,    true,   true,   "to readers process group upon background tty read");
100   AddSignal(22,     "SIGTTOU",      false,    true,   true,   "to readers process group upon background tty write");
101   AddSignal(23,     "SIGIO",        false,    false,  false,  "input/output possible signal");
102   AddSignal(24,     "SIGXCPU",      false,    true,   true,   "exceeded CPU time limit");
103   AddSignal(25,     "SIGXFSZ",      false,    true,   true,   "exceeded file size limit");
104   AddSignal(26,     "SIGVTALRM",    false,    false,  false,  "virtual time alarm");
105   AddSignal(27,     "SIGPROF",      false,    false,  false,  "profiling time alarm");
106   AddSignal(28,     "SIGWINCH",     false,    false,  false,  "window size changes");
107   AddSignal(29,     "SIGINFO",      false,    true,   true,   "information request");
108   AddSignal(30,     "SIGUSR1",      false,    true,   true,   "user defined signal 1");
109   AddSignal(31,     "SIGUSR2",      false,    true,   true,   "user defined signal 2");
110   // clang-format on
111 }
112 
113 void UnixSignals::AddSignal(int signo, const char *name, bool default_suppress,
114                             bool default_stop, bool default_notify,
115                             const char *description, const char *alias) {
116   Signal new_signal(name, default_suppress, default_stop, default_notify,
117                     description, alias);
118   m_signals.insert(std::make_pair(signo, new_signal));
119   ++m_version;
120 }
121 
122 void UnixSignals::RemoveSignal(int signo) {
123   collection::iterator pos = m_signals.find(signo);
124   if (pos != m_signals.end())
125     m_signals.erase(pos);
126   ++m_version;
127 }
128 
129 const char *UnixSignals::GetSignalAsCString(int signo) const {
130   collection::const_iterator pos = m_signals.find(signo);
131   if (pos == m_signals.end())
132     return nullptr;
133   else
134     return pos->second.m_name.GetCString();
135 }
136 
137 bool UnixSignals::SignalIsValid(int32_t signo) const {
138   return m_signals.find(signo) != m_signals.end();
139 }
140 
141 ConstString UnixSignals::GetShortName(ConstString name) const {
142   if (name)
143     return ConstString(name.GetStringRef().substr(3)); // Remove "SIG" from name
144   return name;
145 }
146 
147 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
148   ConstString const_name(name);
149 
150   collection::const_iterator pos, end = m_signals.end();
151   for (pos = m_signals.begin(); pos != end; pos++) {
152     if ((const_name == pos->second.m_name) ||
153         (const_name == pos->second.m_alias) ||
154         (const_name == GetShortName(pos->second.m_name)) ||
155         (const_name == GetShortName(pos->second.m_alias)))
156       return pos->first;
157   }
158 
159   int32_t signo;
160   if (llvm::to_integer(name, signo))
161     return signo;
162   return LLDB_INVALID_SIGNAL_NUMBER;
163 }
164 
165 int32_t UnixSignals::GetFirstSignalNumber() const {
166   if (m_signals.empty())
167     return LLDB_INVALID_SIGNAL_NUMBER;
168 
169   return (*m_signals.begin()).first;
170 }
171 
172 int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
173   collection::const_iterator pos = m_signals.find(current_signal);
174   collection::const_iterator end = m_signals.end();
175   if (pos == end)
176     return LLDB_INVALID_SIGNAL_NUMBER;
177   else {
178     pos++;
179     if (pos == end)
180       return LLDB_INVALID_SIGNAL_NUMBER;
181     else
182       return pos->first;
183   }
184 }
185 
186 const char *UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
187                                        bool &should_stop,
188                                        bool &should_notify) const {
189   collection::const_iterator pos = m_signals.find(signo);
190   if (pos == m_signals.end())
191     return nullptr;
192   else {
193     const Signal &signal = pos->second;
194     should_suppress = signal.m_suppress;
195     should_stop = signal.m_stop;
196     should_notify = signal.m_notify;
197     return signal.m_name.AsCString("");
198   }
199 }
200 
201 bool UnixSignals::GetShouldSuppress(int signo) const {
202   collection::const_iterator pos = m_signals.find(signo);
203   if (pos != m_signals.end())
204     return pos->second.m_suppress;
205   return false;
206 }
207 
208 bool UnixSignals::SetShouldSuppress(int signo, bool value) {
209   collection::iterator pos = m_signals.find(signo);
210   if (pos != m_signals.end()) {
211     pos->second.m_suppress = value;
212     ++m_version;
213     return true;
214   }
215   return false;
216 }
217 
218 bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
219   const int32_t signo = GetSignalNumberFromName(signal_name);
220   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
221     return SetShouldSuppress(signo, value);
222   return false;
223 }
224 
225 bool UnixSignals::GetShouldStop(int signo) const {
226   collection::const_iterator pos = m_signals.find(signo);
227   if (pos != m_signals.end())
228     return pos->second.m_stop;
229   return false;
230 }
231 
232 bool UnixSignals::SetShouldStop(int signo, bool value) {
233   collection::iterator pos = m_signals.find(signo);
234   if (pos != m_signals.end()) {
235     pos->second.m_stop = value;
236     ++m_version;
237     return true;
238   }
239   return false;
240 }
241 
242 bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
243   const int32_t signo = GetSignalNumberFromName(signal_name);
244   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
245     return SetShouldStop(signo, value);
246   return false;
247 }
248 
249 bool UnixSignals::GetShouldNotify(int signo) const {
250   collection::const_iterator pos = m_signals.find(signo);
251   if (pos != m_signals.end())
252     return pos->second.m_notify;
253   return false;
254 }
255 
256 bool UnixSignals::SetShouldNotify(int signo, bool value) {
257   collection::iterator pos = m_signals.find(signo);
258   if (pos != m_signals.end()) {
259     pos->second.m_notify = value;
260     ++m_version;
261     return true;
262   }
263   return false;
264 }
265 
266 bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
267   const int32_t signo = GetSignalNumberFromName(signal_name);
268   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
269     return SetShouldNotify(signo, value);
270   return false;
271 }
272 
273 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
274 
275 int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
276   if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
277     return LLDB_INVALID_SIGNAL_NUMBER;
278   auto it = m_signals.begin();
279   std::advance(it, index);
280   return it->first;
281 }
282 
283 uint64_t UnixSignals::GetVersion() const { return m_version; }
284 
285 std::vector<int32_t>
286 UnixSignals::GetFilteredSignals(llvm::Optional<bool> should_suppress,
287                                 llvm::Optional<bool> should_stop,
288                                 llvm::Optional<bool> should_notify) {
289   std::vector<int32_t> result;
290   for (int32_t signo = GetFirstSignalNumber();
291        signo != LLDB_INVALID_SIGNAL_NUMBER;
292        signo = GetNextSignalNumber(signo)) {
293 
294     bool signal_suppress = false;
295     bool signal_stop = false;
296     bool signal_notify = false;
297     GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
298 
299     // If any of filtering conditions are not met, we move on to the next
300     // signal.
301     if (should_suppress.hasValue() &&
302         signal_suppress != should_suppress.getValue())
303       continue;
304 
305     if (should_stop.hasValue() && signal_stop != should_stop.getValue())
306       continue;
307 
308     if (should_notify.hasValue() && signal_notify != should_notify.getValue())
309       continue;
310 
311     result.push_back(signo);
312   }
313 
314   return result;
315 }
316 
317 void UnixSignals::IncrementSignalHitCount(int signo) {
318   collection::iterator pos = m_signals.find(signo);
319   if (pos != m_signals.end())
320     pos->second.m_hit_count += 1;
321 }
322 
323 json::Value UnixSignals::GetHitCountStatistics() const {
324   json::Array json_signals;
325   for (const auto &pair: m_signals) {
326     if (pair.second.m_hit_count > 0)
327       json_signals.emplace_back(json::Object{
328         { pair.second.m_name.GetCString(), pair.second.m_hit_count }
329       });
330   }
331   return std::move(json_signals);
332 }
333