brintos

brintos / llvm-project-archived public Read only

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