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 #include <optional> 17 18 using namespace lldb_private; 19 using namespace llvm; 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 m_default_suppress(default_suppress), m_default_stop(default_stop), 28 m_default_notify(default_notify) { 29 if (description) 30 m_description.assign(description); 31 } 32 33 lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) { 34 const auto &triple = arch.GetTriple(); 35 switch (triple.getOS()) { 36 case llvm::Triple::Linux: { 37 switch (triple.getArch()) { 38 case llvm::Triple::mips: 39 case llvm::Triple::mipsel: 40 case llvm::Triple::mips64: 41 case llvm::Triple::mips64el: 42 return std::make_shared<MipsLinuxSignals>(); 43 default: 44 return std::make_shared<LinuxSignals>(); 45 } 46 } 47 case llvm::Triple::FreeBSD: 48 case llvm::Triple::OpenBSD: 49 return std::make_shared<FreeBSDSignals>(); 50 case llvm::Triple::NetBSD: 51 return std::make_shared<NetBSDSignals>(); 52 default: 53 return std::make_shared<UnixSignals>(); 54 } 55 } 56 57 lldb::UnixSignalsSP UnixSignals::CreateForHost() { 58 static lldb::UnixSignalsSP s_unix_signals_sp = 59 Create(HostInfo::GetArchitecture()); 60 return s_unix_signals_sp; 61 } 62 63 // UnixSignals constructor 64 UnixSignals::UnixSignals() { Reset(); } 65 66 UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {} 67 68 UnixSignals::~UnixSignals() = default; 69 70 void UnixSignals::Reset() { 71 // This builds one standard set of Unix Signals. If yours aren't quite in 72 // this order, you can either subclass this class, and use Add & Remove to 73 // change them or you can subclass and build them afresh in your constructor. 74 // 75 // Note: the signals below are the Darwin signals. Do not change these! 76 77 m_signals.clear(); 78 79 // clang-format off 80 // SIGNO NAME SUPPRESS STOP NOTIFY DESCRIPTION 81 // ====== ============== ======== ====== ====== =================================================== 82 AddSignal(1, "SIGHUP", false, true, true, "hangup"); 83 AddSignal(2, "SIGINT", true, true, true, "interrupt"); 84 AddSignal(3, "SIGQUIT", false, true, true, "quit"); 85 AddSignal(4, "SIGILL", false, true, true, "illegal instruction"); 86 AddSignal(5, "SIGTRAP", true, true, true, "trace trap (not reset when caught)"); 87 AddSignal(6, "SIGABRT", false, true, true, "abort()"); 88 AddSignal(7, "SIGEMT", false, true, true, "pollable event"); 89 AddSignal(8, "SIGFPE", false, true, true, "floating point exception"); 90 AddSignal(9, "SIGKILL", false, true, true, "kill"); 91 AddSignal(10, "SIGBUS", false, true, true, "bus error"); 92 AddSignal(11, "SIGSEGV", false, true, true, "segmentation violation"); 93 AddSignal(12, "SIGSYS", false, true, true, "bad argument to system call"); 94 AddSignal(13, "SIGPIPE", false, false, false, "write on a pipe with no one to read it"); 95 AddSignal(14, "SIGALRM", false, false, false, "alarm clock"); 96 AddSignal(15, "SIGTERM", false, true, true, "software termination signal from kill"); 97 AddSignal(16, "SIGURG", false, false, false, "urgent condition on IO channel"); 98 AddSignal(17, "SIGSTOP", true, true, true, "sendable stop signal not from tty"); 99 AddSignal(18, "SIGTSTP", false, true, true, "stop signal from tty"); 100 AddSignal(19, "SIGCONT", false, false, true, "continue a stopped process"); 101 AddSignal(20, "SIGCHLD", false, false, false, "to parent on child stop or exit"); 102 AddSignal(21, "SIGTTIN", false, true, true, "to readers process group upon background tty read"); 103 AddSignal(22, "SIGTTOU", false, true, true, "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 // clang-format on 114 } 115 116 void UnixSignals::AddSignal(int signo, const char *name, bool default_suppress, 117 bool default_stop, bool default_notify, 118 const char *description, const char *alias) { 119 Signal new_signal(name, default_suppress, default_stop, default_notify, 120 description, alias); 121 m_signals.insert(std::make_pair(signo, new_signal)); 122 ++m_version; 123 } 124 125 void UnixSignals::RemoveSignal(int signo) { 126 collection::iterator pos = m_signals.find(signo); 127 if (pos != m_signals.end()) 128 m_signals.erase(pos); 129 ++m_version; 130 } 131 132 const char *UnixSignals::GetSignalAsCString(int signo) const { 133 collection::const_iterator pos = m_signals.find(signo); 134 if (pos == m_signals.end()) 135 return nullptr; 136 else 137 return pos->second.m_name.GetCString(); 138 } 139 140 bool UnixSignals::SignalIsValid(int32_t signo) const { 141 return m_signals.find(signo) != m_signals.end(); 142 } 143 144 ConstString UnixSignals::GetShortName(ConstString name) const { 145 if (name) 146 return ConstString(name.GetStringRef().substr(3)); // Remove "SIG" from name 147 return name; 148 } 149 150 int32_t UnixSignals::GetSignalNumberFromName(const char *name) const { 151 ConstString const_name(name); 152 153 collection::const_iterator pos, end = m_signals.end(); 154 for (pos = m_signals.begin(); pos != end; pos++) { 155 if ((const_name == pos->second.m_name) || 156 (const_name == pos->second.m_alias) || 157 (const_name == GetShortName(pos->second.m_name)) || 158 (const_name == GetShortName(pos->second.m_alias))) 159 return pos->first; 160 } 161 162 int32_t signo; 163 if (llvm::to_integer(name, signo)) 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(std::optional<bool> should_suppress, 290 std::optional<bool> should_stop, 291 std::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 && signal_suppress != *should_suppress) 305 continue; 306 307 if (should_stop && signal_stop != *should_stop) 308 continue; 309 310 if (should_notify && signal_notify != *should_notify) 311 continue; 312 313 result.push_back(signo); 314 } 315 316 return result; 317 } 318 319 void UnixSignals::IncrementSignalHitCount(int signo) { 320 collection::iterator pos = m_signals.find(signo); 321 if (pos != m_signals.end()) 322 pos->second.m_hit_count += 1; 323 } 324 325 json::Value UnixSignals::GetHitCountStatistics() const { 326 json::Array json_signals; 327 for (const auto &pair: m_signals) { 328 if (pair.second.m_hit_count > 0) 329 json_signals.emplace_back(json::Object{ 330 { pair.second.m_name.GetCString(), pair.second.m_hit_count } 331 }); 332 } 333 return std::move(json_signals); 334 } 335 336 void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify, 337 bool reset_suppress) { 338 if (reset_stop) 339 m_stop = m_default_stop; 340 if (reset_notify) 341 m_notify = m_default_notify; 342 if (reset_suppress) 343 m_suppress = m_default_suppress; 344 } 345 346 bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop, 347 bool reset_notify, bool reset_suppress) { 348 auto elem = m_signals.find(signo); 349 if (elem == m_signals.end()) 350 return false; 351 (*elem).second.Reset(reset_stop, reset_notify, reset_suppress); 352 return true; 353 } 354 355