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