1 //===-- UnixSignals.h -------------------------------------------*- C++ -*-===// 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 #ifndef LLDB_TARGET_UNIXSIGNALS_H 10 #define LLDB_TARGET_UNIXSIGNALS_H 11 12 #include <map> 13 #include <optional> 14 #include <string> 15 #include <vector> 16 17 #include "lldb/Utility/ConstString.h" 18 #include "lldb/lldb-private.h" 19 #include "llvm/Support/JSON.h" 20 21 namespace lldb_private { 22 23 class UnixSignals { 24 public: 25 static lldb::UnixSignalsSP Create(const ArchSpec &arch); 26 static lldb::UnixSignalsSP CreateForHost(); 27 28 // Constructors and Destructors 29 UnixSignals(); 30 31 virtual ~UnixSignals(); 32 33 const char *GetSignalAsCString(int32_t signo) const; 34 35 std::string 36 GetSignalDescription(int32_t signo, 37 std::optional<int32_t> code = std::nullopt, 38 std::optional<lldb::addr_t> addr = std::nullopt, 39 std::optional<lldb::addr_t> lower = std::nullopt, 40 std::optional<lldb::addr_t> upper = std::nullopt) const; 41 42 bool SignalIsValid(int32_t signo) const; 43 44 int32_t GetSignalNumberFromName(const char *name) const; 45 46 const char *GetSignalInfo(int32_t signo, bool &should_suppress, 47 bool &should_stop, bool &should_notify) const; 48 49 bool GetShouldSuppress(int32_t signo) const; 50 51 bool SetShouldSuppress(int32_t signo, bool value); 52 53 bool SetShouldSuppress(const char *signal_name, bool value); 54 55 bool GetShouldStop(int32_t signo) const; 56 57 bool SetShouldStop(int32_t signo, bool value); 58 bool SetShouldStop(const char *signal_name, bool value); 59 60 bool GetShouldNotify(int32_t signo) const; 61 62 bool SetShouldNotify(int32_t signo, bool value); 63 64 bool SetShouldNotify(const char *signal_name, bool value); 65 66 bool ResetSignal(int32_t signo, bool reset_stop = true, 67 bool reset_notify = true, bool reset_suppress = true); 68 69 // These provide an iterator through the signals available on this system. 70 // Call GetFirstSignalNumber to get the first entry, then iterate on 71 // GetNextSignalNumber till you get back LLDB_INVALID_SIGNAL_NUMBER. 72 int32_t GetFirstSignalNumber() const; 73 74 int32_t GetNextSignalNumber(int32_t current_signal) const; 75 76 int32_t GetNumSignals() const; 77 78 int32_t GetSignalAtIndex(int32_t index) const; 79 80 // We assume that the elements of this object are constant once it is 81 // constructed, since a process should never need to add or remove symbols as 82 // it runs. So don't call these functions anywhere but the constructor of 83 // your subclass of UnixSignals or in your Process Plugin's GetUnixSignals 84 // method before you return the UnixSignal object. 85 86 void AddSignal(int signo, const char *name, bool default_suppress, 87 bool default_stop, bool default_notify, 88 const char *description, const char *alias = nullptr); 89 90 enum SignalCodePrintOption { None, Address, Bounds }; 91 92 // Instead of calling this directly, use a ADD_SIGCODE macro to get compile 93 // time checks when on the native platform. 94 void AddSignalCode( 95 int signo, int code, const llvm::StringLiteral description, 96 SignalCodePrintOption print_option = SignalCodePrintOption::None); 97 98 void RemoveSignal(int signo); 99 100 /// Track how many times signals are hit as stop reasons. 101 void IncrementSignalHitCount(int signo); 102 103 /// Get the hit count statistics for signals. 104 /// 105 /// Gettings statistics on the hit counts of signals can help explain why some 106 /// debug sessions are slow since each stop takes a few hundred ms and some 107 /// software use signals a lot and can cause slow debugging performance if 108 /// they are used too often. Even if a signal is not stopped at, it will auto 109 /// continue the process and a delay will happen. 110 llvm::json::Value GetHitCountStatistics() const; 111 112 // Returns a current version of the data stored in this class. Version gets 113 // incremented each time Set... method is called. 114 uint64_t GetVersion() const; 115 116 // Returns a vector of signals that meet criteria provided in arguments. Each 117 // should_[suppress|stop|notify] flag can be std::nullopt - no filtering by 118 // this flag true - only signals that have it set to true are returned false - 119 // only signals that have it set to true are returned 120 std::vector<int32_t> GetFilteredSignals(std::optional<bool> should_suppress, 121 std::optional<bool> should_stop, 122 std::optional<bool> should_notify); 123 124 protected: 125 // Classes that inherit from UnixSignals can see and modify these 126 127 struct SignalCode { 128 const llvm::StringLiteral m_description; 129 const SignalCodePrintOption m_print_option; 130 }; 131 132 struct Signal { 133 ConstString m_name; 134 ConstString m_alias; 135 std::string m_description; 136 std::map<int32_t, SignalCode> m_codes; 137 uint32_t m_hit_count = 0; 138 bool m_suppress : 1, m_stop : 1, m_notify : 1; 139 bool m_default_suppress : 1, m_default_stop : 1, m_default_notify : 1; 140 141 Signal(const char *name, bool default_suppress, bool default_stop, 142 bool default_notify, const char *description, const char *alias); 143 144 ~Signal() = default; 145 void Reset(bool reset_stop, bool reset_notify, bool reset_suppress); 146 }; 147 148 llvm::StringRef GetShortName(llvm::StringRef name) const; 149 150 virtual void Reset(); 151 152 typedef std::map<int32_t, Signal> collection; 153 154 collection m_signals; 155 156 // This version gets incremented every time something is changing in this 157 // class, including when we call AddSignal from the constructor. So after the 158 // object is constructed m_version is going to be > 0 if it has at least one 159 // signal registered in it. 160 uint64_t m_version = 0; 161 162 // GDBRemote signals need to be copyable. 163 UnixSignals(const UnixSignals &rhs); 164 165 const UnixSignals &operator=(const UnixSignals &rhs) = delete; 166 }; 167 168 } // Namespace lldb 169 #endif // LLDB_TARGET_UNIXSIGNALS_H 170