]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/UnixSignals.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / UnixSignals.cpp
1 //===-- UnixSignals.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Target/UnixSignals.h"
11 #include "Plugins/Process/Utility/FreeBSDSignals.h"
12 #include "Plugins/Process/Utility/LinuxSignals.h"
13 #include "Plugins/Process/Utility/MipsLinuxSignals.h"
14 #include "Plugins/Process/Utility/NetBSDSignals.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 //----------------------------------------------------------------------
55 // UnixSignals constructor
56 //----------------------------------------------------------------------
57 UnixSignals::UnixSignals() { Reset(); }
58
59 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
60
61 UnixSignals::~UnixSignals() = default;
62
63 void UnixSignals::Reset() {
64   // This builds one standard set of Unix Signals.  If yours aren't quite in
65   // this order, you can either subclass this class, and use Add & Remove to
66   // change them
67   // or you can subclass and build them afresh in your constructor;
68   //
69   // Note: the signals below are the Darwin signals.  Do not change these!
70   m_signals.clear();
71   //        SIGNO  NAME          SUPPRESS STOP   NOTIFY DESCRIPTION
72   //        ====== ============  ======== ====== ======
73   //        ===================================================
74   AddSignal(1, "SIGHUP", false, true, true, "hangup");
75   AddSignal(2, "SIGINT", true, true, true, "interrupt");
76   AddSignal(3, "SIGQUIT", false, true, true, "quit");
77   AddSignal(4, "SIGILL", false, true, true, "illegal instruction");
78   AddSignal(5, "SIGTRAP", true, true, true,
79             "trace trap (not reset when caught)");
80   AddSignal(6, "SIGABRT", false, true, true, "abort()");
81   AddSignal(7, "SIGEMT", false, true, true, "pollable event");
82   AddSignal(8, "SIGFPE", false, true, true, "floating point exception");
83   AddSignal(9, "SIGKILL", false, true, true, "kill");
84   AddSignal(10, "SIGBUS", false, true, true, "bus error");
85   AddSignal(11, "SIGSEGV", false, true, true, "segmentation violation");
86   AddSignal(12, "SIGSYS", false, true, true, "bad argument to system call");
87   AddSignal(13, "SIGPIPE", false, false, false,
88             "write on a pipe with no one to read it");
89   AddSignal(14, "SIGALRM", false, false, false, "alarm clock");
90   AddSignal(15, "SIGTERM", false, true, true,
91             "software termination signal from kill");
92   AddSignal(16, "SIGURG", false, false, false,
93             "urgent condition on IO channel");
94   AddSignal(17, "SIGSTOP", true, true, true,
95             "sendable stop signal not from tty");
96   AddSignal(18, "SIGTSTP", false, true, true, "stop signal from tty");
97   AddSignal(19, "SIGCONT", false, true, true, "continue a stopped process");
98   AddSignal(20, "SIGCHLD", false, false, false,
99             "to parent on child stop or exit");
100   AddSignal(21, "SIGTTIN", false, true, true,
101             "to readers process group upon background tty read");
102   AddSignal(22, "SIGTTOU", false, true, true,
103             "to readers process group upon background tty write");
104   AddSignal(23, "SIGIO", false, false, false, "input/output possible signal");
105   AddSignal(24, "SIGXCPU", false, true, true, "exceeded CPU time limit");
106   AddSignal(25, "SIGXFSZ", false, true, true, "exceeded file size limit");
107   AddSignal(26, "SIGVTALRM", false, false, false, "virtual time alarm");
108   AddSignal(27, "SIGPROF", false, false, false, "profiling time alarm");
109   AddSignal(28, "SIGWINCH", false, false, false, "window size changes");
110   AddSignal(29, "SIGINFO", false, true, true, "information request");
111   AddSignal(30, "SIGUSR1", false, true, true, "user defined signal 1");
112   AddSignal(31, "SIGUSR2", false, true, true, "user defined signal 2");
113 }
114
115 void UnixSignals::AddSignal(int signo, const char *name, bool default_suppress,
116                             bool default_stop, bool default_notify,
117                             const char *description, const char *alias) {
118   Signal new_signal(name, default_suppress, default_stop, default_notify,
119                     description, alias);
120   m_signals.insert(std::make_pair(signo, new_signal));
121   ++m_version;
122 }
123
124 void UnixSignals::RemoveSignal(int signo) {
125   collection::iterator pos = m_signals.find(signo);
126   if (pos != m_signals.end())
127     m_signals.erase(pos);
128   ++m_version;
129 }
130
131 const char *UnixSignals::GetSignalAsCString(int signo) const {
132   collection::const_iterator pos = m_signals.find(signo);
133   if (pos == m_signals.end())
134     return nullptr;
135   else
136     return pos->second.m_name.GetCString();
137 }
138
139 bool UnixSignals::SignalIsValid(int32_t signo) const {
140   return m_signals.find(signo) != m_signals.end();
141 }
142
143 ConstString UnixSignals::GetShortName(ConstString name) const {
144   if (name) {
145     const char *signame = name.AsCString();
146     return ConstString(signame + 3); // Remove "SIG" from name
147   }
148   return name;
149 }
150
151 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
152   ConstString const_name(name);
153
154   collection::const_iterator pos, end = m_signals.end();
155   for (pos = m_signals.begin(); pos != end; pos++) {
156     if ((const_name == pos->second.m_name) ||
157         (const_name == pos->second.m_alias) ||
158         (const_name == GetShortName(pos->second.m_name)) ||
159         (const_name == GetShortName(pos->second.m_alias)))
160       return pos->first;
161   }
162
163   const int32_t signo =
164       StringConvert::ToSInt32(name, LLDB_INVALID_SIGNAL_NUMBER, 0);
165   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
166     return signo;
167   return LLDB_INVALID_SIGNAL_NUMBER;
168 }
169
170 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
177 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
191 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
206 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
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
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
230 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
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
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
254 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
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
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
278 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
279
280 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
288 uint64_t UnixSignals::GetVersion() const { return m_version; }
289
290 std::vector<int32_t>
291 UnixSignals::GetFilteredSignals(llvm::Optional<bool> should_suppress,
292                                 llvm::Optional<bool> should_stop,
293                                 llvm::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.hasValue() &&
307         signal_suppress != should_suppress.getValue())
308       continue;
309
310     if (should_stop.hasValue() && signal_stop != should_stop.getValue())
311       continue;
312
313     if (should_notify.hasValue() && signal_notify != should_notify.getValue())
314       continue;
315
316     result.push_back(signo);
317   }
318
319   return result;
320 }