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