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