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 "Plugins/Process/Utility/OpenBSDSignals.h" 15 #include "lldb/Host/HostInfo.h" 16 #include "lldb/Host/StringConvert.h" 17 #include "lldb/Utility/ArchSpec.h" 18 19 using namespace lldb_private; 20 21 UnixSignals::Signal::Signal(const char *name, bool default_suppress, 22 bool default_stop, bool default_notify, 23 const char *description, const char *alias) 24 : m_name(name), m_alias(alias), m_description(), 25 m_suppress(default_suppress), m_stop(default_stop), 26 m_notify(default_notify) { 27 if (description) 28 m_description.assign(description); 29 } 30 31 lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) { 32 const auto &triple = arch.GetTriple(); 33 switch (triple.getOS()) { 34 case llvm::Triple::Linux: { 35 switch (triple.getArch()) { 36 case llvm::Triple::mips: 37 case llvm::Triple::mipsel: 38 case llvm::Triple::mips64: 39 case llvm::Triple::mips64el: 40 return std::make_shared<MipsLinuxSignals>(); 41 default: 42 return std::make_shared<LinuxSignals>(); 43 } 44 } 45 case llvm::Triple::FreeBSD: 46 return std::make_shared<FreeBSDSignals>(); 47 case llvm::Triple::OpenBSD: 48 return std::make_shared<OpenBSDSignals>(); 49 case llvm::Triple::NetBSD: 50 return std::make_shared<NetBSDSignals>(); 51 default: 52 return std::make_shared<UnixSignals>(); 53 } 54 } 55 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 63 UnixSignals::UnixSignals() { Reset(); } 64 65 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {} 66 67 UnixSignals::~UnixSignals() = default; 68 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 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 return ConstString(name.GetStringRef().substr(3)); // Remove "SIG" from name 146 return name; 147 } 148 149 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 const int32_t signo = 162 StringConvert::ToSInt32(name, LLDB_INVALID_SIGNAL_NUMBER, 0); 163 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 164 return signo; 165 return LLDB_INVALID_SIGNAL_NUMBER; 166 } 167 168 int32_t UnixSignals::GetFirstSignalNumber() const { 169 if (m_signals.empty()) 170 return LLDB_INVALID_SIGNAL_NUMBER; 171 172 return (*m_signals.begin()).first; 173 } 174 175 int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const { 176 collection::const_iterator pos = m_signals.find(current_signal); 177 collection::const_iterator end = m_signals.end(); 178 if (pos == end) 179 return LLDB_INVALID_SIGNAL_NUMBER; 180 else { 181 pos++; 182 if (pos == end) 183 return LLDB_INVALID_SIGNAL_NUMBER; 184 else 185 return pos->first; 186 } 187 } 188 189 const char *UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress, 190 bool &should_stop, 191 bool &should_notify) const { 192 collection::const_iterator pos = m_signals.find(signo); 193 if (pos == m_signals.end()) 194 return nullptr; 195 else { 196 const Signal &signal = pos->second; 197 should_suppress = signal.m_suppress; 198 should_stop = signal.m_stop; 199 should_notify = signal.m_notify; 200 return signal.m_name.AsCString(""); 201 } 202 } 203 204 bool UnixSignals::GetShouldSuppress(int signo) const { 205 collection::const_iterator pos = m_signals.find(signo); 206 if (pos != m_signals.end()) 207 return pos->second.m_suppress; 208 return false; 209 } 210 211 bool UnixSignals::SetShouldSuppress(int signo, bool value) { 212 collection::iterator pos = m_signals.find(signo); 213 if (pos != m_signals.end()) { 214 pos->second.m_suppress = value; 215 ++m_version; 216 return true; 217 } 218 return false; 219 } 220 221 bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) { 222 const int32_t signo = GetSignalNumberFromName(signal_name); 223 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 224 return SetShouldSuppress(signo, value); 225 return false; 226 } 227 228 bool UnixSignals::GetShouldStop(int signo) const { 229 collection::const_iterator pos = m_signals.find(signo); 230 if (pos != m_signals.end()) 231 return pos->second.m_stop; 232 return false; 233 } 234 235 bool UnixSignals::SetShouldStop(int signo, bool value) { 236 collection::iterator pos = m_signals.find(signo); 237 if (pos != m_signals.end()) { 238 pos->second.m_stop = value; 239 ++m_version; 240 return true; 241 } 242 return false; 243 } 244 245 bool UnixSignals::SetShouldStop(const char *signal_name, bool value) { 246 const int32_t signo = GetSignalNumberFromName(signal_name); 247 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 248 return SetShouldStop(signo, value); 249 return false; 250 } 251 252 bool UnixSignals::GetShouldNotify(int signo) const { 253 collection::const_iterator pos = m_signals.find(signo); 254 if (pos != m_signals.end()) 255 return pos->second.m_notify; 256 return false; 257 } 258 259 bool UnixSignals::SetShouldNotify(int signo, bool value) { 260 collection::iterator pos = m_signals.find(signo); 261 if (pos != m_signals.end()) { 262 pos->second.m_notify = value; 263 ++m_version; 264 return true; 265 } 266 return false; 267 } 268 269 bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) { 270 const int32_t signo = GetSignalNumberFromName(signal_name); 271 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 272 return SetShouldNotify(signo, value); 273 return false; 274 } 275 276 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); } 277 278 int32_t UnixSignals::GetSignalAtIndex(int32_t index) const { 279 if (index < 0 || m_signals.size() <= static_cast<size_t>(index)) 280 return LLDB_INVALID_SIGNAL_NUMBER; 281 auto it = m_signals.begin(); 282 std::advance(it, index); 283 return it->first; 284 } 285 286 uint64_t UnixSignals::GetVersion() const { return m_version; } 287 288 std::vector<int32_t> 289 UnixSignals::GetFilteredSignals(llvm::Optional<bool> should_suppress, 290 llvm::Optional<bool> should_stop, 291 llvm::Optional<bool> should_notify) { 292 std::vector<int32_t> result; 293 for (int32_t signo = GetFirstSignalNumber(); 294 signo != LLDB_INVALID_SIGNAL_NUMBER; 295 signo = GetNextSignalNumber(signo)) { 296 297 bool signal_suppress = false; 298 bool signal_stop = false; 299 bool signal_notify = false; 300 GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify); 301 302 // If any of filtering conditions are not met, we move on to the next 303 // signal. 304 if (should_suppress.hasValue() && 305 signal_suppress != should_suppress.getValue()) 306 continue; 307 308 if (should_stop.hasValue() && signal_stop != should_stop.getValue()) 309 continue; 310 311 if (should_notify.hasValue() && signal_notify != should_notify.getValue()) 312 continue; 313 314 result.push_back(signo); 315 } 316 317 return result; 318 } 319