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