1 //===-- UnixSignals.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
18 using namespace lldb_private;
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) {
27 m_description.assign(description);
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>();
41 return std::make_shared<LinuxSignals>();
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>();
50 return std::make_shared<UnixSignals>();
54 //----------------------------------------------------------------------
55 // UnixSignals constructor
56 //----------------------------------------------------------------------
57 UnixSignals::UnixSignals() { Reset(); }
59 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {}
61 UnixSignals::~UnixSignals() = default;
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
67 // or you can subclass and build them afresh in your constructor;
69 // Note: the signals below are the Darwin signals. Do not change these!
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");
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,
120 m_signals.insert(std::make_pair(signo, new_signal));
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);
131 const char *UnixSignals::GetSignalAsCString(int signo) const {
132 collection::const_iterator pos = m_signals.find(signo);
133 if (pos == m_signals.end())
136 return pos->second.m_name.GetCString();
139 bool UnixSignals::SignalIsValid(int32_t signo) const {
140 return m_signals.find(signo) != m_signals.end();
143 ConstString UnixSignals::GetShortName(ConstString name) const {
145 const char *signame = name.AsCString();
146 return ConstString(signame + 3); // Remove "SIG" from name
151 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
152 ConstString const_name(name);
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)))
163 const int32_t signo =
164 StringConvert::ToSInt32(name, LLDB_INVALID_SIGNAL_NUMBER, 0);
165 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
167 return LLDB_INVALID_SIGNAL_NUMBER;
170 int32_t UnixSignals::GetFirstSignalNumber() const {
171 if (m_signals.empty())
172 return LLDB_INVALID_SIGNAL_NUMBER;
174 return (*m_signals.begin()).first;
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();
181 return LLDB_INVALID_SIGNAL_NUMBER;
185 return LLDB_INVALID_SIGNAL_NUMBER;
191 const char *UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
193 bool &should_notify) const {
194 collection::const_iterator pos = m_signals.find(signo);
195 if (pos == m_signals.end())
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("");
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;
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;
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);
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;
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;
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);
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;
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;
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);
278 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
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);
288 uint64_t UnixSignals::GetVersion() const { return m_version; }
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)) {
299 bool signal_suppress = false;
300 bool signal_stop = false;
301 bool signal_notify = false;
302 GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
304 // If any of filtering conditions are not met, we move on to the next
306 if (should_suppress.hasValue() &&
307 signal_suppress != should_suppress.getValue())
310 if (should_stop.hasValue() && signal_stop != should_stop.getValue())
313 if (should_notify.hasValue() && signal_notify != should_notify.getValue())
316 result.push_back(signo);