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