xref: /freebsd/contrib/llvm-project/lldb/source/Target/UnixSignals.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
Signal(llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)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 
Create(const ArchSpec & arch)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 
CreateForHost()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
UnixSignals()53 UnixSignals::UnixSignals() { Reset(); }
54 
UnixSignals(const UnixSignals & rhs)55 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
56 
57 UnixSignals::~UnixSignals() = default;
58 
Reset()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 
AddSignal(int signo,llvm::StringRef name,bool default_suppress,bool default_stop,bool default_notify,llvm::StringRef description,llvm::StringRef alias)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 
AddSignalCode(int signo,int code,const llvm::StringLiteral description,SignalCodePrintOption print_option)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 
RemoveSignal(int signo)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 
GetSignalAsStringRef(int32_t signo) const133 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 
GetSignalDescription(int32_t signo,std::optional<int32_t> code,std::optional<lldb::addr_t> addr,std::optional<lldb::addr_t> lower,std::optional<lldb::addr_t> upper,std::optional<uint32_t> pid,std::optional<uint32_t> uid) const140 std::string UnixSignals::GetSignalDescription(
141     int32_t signo, std::optional<int32_t> code,
142     std::optional<lldb::addr_t> addr, std::optional<lldb::addr_t> lower,
143     std::optional<lldb::addr_t> upper, std::optional<uint32_t> pid,
144     std::optional<uint32_t> uid) 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         case SignalCodePrintOption::Sender:
184           if (pid && uid)
185             strm << " (sender pid=" << *pid << ", uid=" << *uid << ")";
186           break;
187         }
188         str += strm.str();
189       }
190     }
191   }
192 
193   return str;
194 }
195 
SignalIsValid(int32_t signo) const196 bool UnixSignals::SignalIsValid(int32_t signo) const {
197   return m_signals.find(signo) != m_signals.end();
198 }
199 
GetShortName(llvm::StringRef name) const200 llvm::StringRef UnixSignals::GetShortName(llvm::StringRef name) const {
201   return name.substr(3); // Remove "SIG" from name
202 }
203 
GetSignalNumberFromName(const char * name) const204 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
205   llvm::StringRef name_ref(name);
206 
207   collection::const_iterator pos, end = m_signals.end();
208   for (pos = m_signals.begin(); pos != end; pos++) {
209     if ((name_ref == pos->second.m_name) || (name_ref == pos->second.m_alias) ||
210         (name_ref == GetShortName(pos->second.m_name)) ||
211         (name_ref == GetShortName(pos->second.m_alias)))
212       return pos->first;
213   }
214 
215   int32_t signo;
216   if (llvm::to_integer(name, signo))
217     return signo;
218   return LLDB_INVALID_SIGNAL_NUMBER;
219 }
220 
GetFirstSignalNumber() const221 int32_t UnixSignals::GetFirstSignalNumber() const {
222   if (m_signals.empty())
223     return LLDB_INVALID_SIGNAL_NUMBER;
224 
225   return (*m_signals.begin()).first;
226 }
227 
GetNextSignalNumber(int32_t current_signal) const228 int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
229   collection::const_iterator pos = m_signals.find(current_signal);
230   collection::const_iterator end = m_signals.end();
231   if (pos == end)
232     return LLDB_INVALID_SIGNAL_NUMBER;
233   else {
234     pos++;
235     if (pos == end)
236       return LLDB_INVALID_SIGNAL_NUMBER;
237     else
238       return pos->first;
239   }
240 }
241 
GetSignalInfo(int32_t signo,bool & should_suppress,bool & should_stop,bool & should_notify) const242 bool UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
243                                 bool &should_stop, bool &should_notify) const {
244   const auto pos = m_signals.find(signo);
245   if (pos == m_signals.end())
246     return false;
247 
248   const Signal &signal = pos->second;
249   should_suppress = signal.m_suppress;
250   should_stop = signal.m_stop;
251   should_notify = signal.m_notify;
252   return true;
253 }
254 
GetShouldSuppress(int signo) const255 bool UnixSignals::GetShouldSuppress(int signo) const {
256   collection::const_iterator pos = m_signals.find(signo);
257   if (pos != m_signals.end())
258     return pos->second.m_suppress;
259   return false;
260 }
261 
SetShouldSuppress(int signo,bool value)262 bool UnixSignals::SetShouldSuppress(int signo, bool value) {
263   collection::iterator pos = m_signals.find(signo);
264   if (pos != m_signals.end()) {
265     pos->second.m_suppress = value;
266     ++m_version;
267     return true;
268   }
269   return false;
270 }
271 
SetShouldSuppress(const char * signal_name,bool value)272 bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
273   const int32_t signo = GetSignalNumberFromName(signal_name);
274   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
275     return SetShouldSuppress(signo, value);
276   return false;
277 }
278 
GetShouldStop(int signo) const279 bool UnixSignals::GetShouldStop(int signo) const {
280   collection::const_iterator pos = m_signals.find(signo);
281   if (pos != m_signals.end())
282     return pos->second.m_stop;
283   return false;
284 }
285 
SetShouldStop(int signo,bool value)286 bool UnixSignals::SetShouldStop(int signo, bool value) {
287   collection::iterator pos = m_signals.find(signo);
288   if (pos != m_signals.end()) {
289     pos->second.m_stop = value;
290     ++m_version;
291     return true;
292   }
293   return false;
294 }
295 
SetShouldStop(const char * signal_name,bool value)296 bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
297   const int32_t signo = GetSignalNumberFromName(signal_name);
298   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
299     return SetShouldStop(signo, value);
300   return false;
301 }
302 
GetShouldNotify(int signo) const303 bool UnixSignals::GetShouldNotify(int signo) const {
304   collection::const_iterator pos = m_signals.find(signo);
305   if (pos != m_signals.end())
306     return pos->second.m_notify;
307   return false;
308 }
309 
SetShouldNotify(int signo,bool value)310 bool UnixSignals::SetShouldNotify(int signo, bool value) {
311   collection::iterator pos = m_signals.find(signo);
312   if (pos != m_signals.end()) {
313     pos->second.m_notify = value;
314     ++m_version;
315     return true;
316   }
317   return false;
318 }
319 
SetShouldNotify(const char * signal_name,bool value)320 bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
321   const int32_t signo = GetSignalNumberFromName(signal_name);
322   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
323     return SetShouldNotify(signo, value);
324   return false;
325 }
326 
GetNumSignals() const327 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
328 
GetSignalAtIndex(int32_t index) const329 int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
330   if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
331     return LLDB_INVALID_SIGNAL_NUMBER;
332   auto it = m_signals.begin();
333   std::advance(it, index);
334   return it->first;
335 }
336 
GetVersion() const337 uint64_t UnixSignals::GetVersion() const { return m_version; }
338 
339 std::vector<int32_t>
GetFilteredSignals(std::optional<bool> should_suppress,std::optional<bool> should_stop,std::optional<bool> should_notify)340 UnixSignals::GetFilteredSignals(std::optional<bool> should_suppress,
341                                 std::optional<bool> should_stop,
342                                 std::optional<bool> should_notify) {
343   std::vector<int32_t> result;
344   for (int32_t signo = GetFirstSignalNumber();
345        signo != LLDB_INVALID_SIGNAL_NUMBER;
346        signo = GetNextSignalNumber(signo)) {
347 
348     bool signal_suppress = false;
349     bool signal_stop = false;
350     bool signal_notify = false;
351     GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
352 
353     // If any of filtering conditions are not met, we move on to the next
354     // signal.
355     if (should_suppress && signal_suppress != *should_suppress)
356       continue;
357 
358     if (should_stop && signal_stop != *should_stop)
359       continue;
360 
361     if (should_notify && signal_notify != *should_notify)
362       continue;
363 
364     result.push_back(signo);
365   }
366 
367   return result;
368 }
369 
IncrementSignalHitCount(int signo)370 void UnixSignals::IncrementSignalHitCount(int signo) {
371   collection::iterator pos = m_signals.find(signo);
372   if (pos != m_signals.end())
373     pos->second.m_hit_count += 1;
374 }
375 
GetHitCountStatistics() const376 json::Value UnixSignals::GetHitCountStatistics() const {
377   json::Array json_signals;
378   for (const auto &pair : m_signals) {
379     if (pair.second.m_hit_count > 0)
380       json_signals.emplace_back(
381           json::Object{{pair.second.m_name, pair.second.m_hit_count}});
382   }
383   return std::move(json_signals);
384 }
385 
Reset(bool reset_stop,bool reset_notify,bool reset_suppress)386 void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify,
387                                 bool reset_suppress) {
388   if (reset_stop)
389     m_stop = m_default_stop;
390   if (reset_notify)
391     m_notify = m_default_notify;
392   if (reset_suppress)
393     m_suppress = m_default_suppress;
394 }
395 
ResetSignal(int32_t signo,bool reset_stop,bool reset_notify,bool reset_suppress)396 bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop,
397                                  bool reset_notify, bool reset_suppress) {
398     auto elem = m_signals.find(signo);
399     if (elem == m_signals.end())
400       return false;
401     (*elem).second.Reset(reset_stop, reset_notify, reset_suppress);
402     return true;
403 }
404