LLDB  mainline
UnixSignals.cpp
Go to the documentation of this file.
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 
14 #include "lldb/Host/HostInfo.h"
15 #include "lldb/Utility/ArchSpec.h"
16 
17 using namespace lldb_private;
18 using namespace llvm;
19 
20 UnixSignals::Signal::Signal(const char *name, bool default_suppress,
21  bool default_stop, bool default_notify,
22  const char *description, const char *alias)
23  : m_name(name), m_alias(alias), m_description(),
24  m_suppress(default_suppress), m_stop(default_stop),
25  m_notify(default_notify),
26  m_default_suppress(default_suppress), m_default_stop(default_stop),
27  m_default_notify(default_notify) {
28  if (description)
29  m_description.assign(description);
30 }
31 
32 lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) {
33  const auto &triple = arch.GetTriple();
34  switch (triple.getOS()) {
35  case llvm::Triple::Linux: {
36  switch (triple.getArch()) {
37  case llvm::Triple::mips:
38  case llvm::Triple::mipsel:
39  case llvm::Triple::mips64:
40  case llvm::Triple::mips64el:
41  return std::make_shared<MipsLinuxSignals>();
42  default:
43  return std::make_shared<LinuxSignals>();
44  }
45  }
46  case llvm::Triple::FreeBSD:
47  case llvm::Triple::OpenBSD:
48  return std::make_shared<FreeBSDSignals>();
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
64 
66 
67 UnixSignals::~UnixSignals() = default;
68 
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 
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  int32_t signo;
162  if (llvm::to_integer(name, signo))
163  return signo;
165 }
166 
168  if (m_signals.empty())
170 
171  return (*m_signals.begin()).first;
172 }
173 
174 int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
175  collection::const_iterator pos = m_signals.find(current_signal);
176  collection::const_iterator end = m_signals.end();
177  if (pos == end)
179  else {
180  pos++;
181  if (pos == end)
183  else
184  return pos->first;
185  }
186 }
187 
188 const char *UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
189  bool &should_stop,
190  bool &should_notify) const {
191  collection::const_iterator pos = m_signals.find(signo);
192  if (pos == m_signals.end())
193  return nullptr;
194  else {
195  const Signal &signal = pos->second;
196  should_suppress = signal.m_suppress;
197  should_stop = signal.m_stop;
198  should_notify = signal.m_notify;
199  return signal.m_name.AsCString("");
200  }
201 }
202 
203 bool UnixSignals::GetShouldSuppress(int signo) const {
204  collection::const_iterator pos = m_signals.find(signo);
205  if (pos != m_signals.end())
206  return pos->second.m_suppress;
207  return false;
208 }
209 
210 bool UnixSignals::SetShouldSuppress(int signo, bool value) {
211  collection::iterator pos = m_signals.find(signo);
212  if (pos != m_signals.end()) {
213  pos->second.m_suppress = value;
214  ++m_version;
215  return true;
216  }
217  return false;
218 }
219 
220 bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
221  const int32_t signo = GetSignalNumberFromName(signal_name);
222  if (signo != LLDB_INVALID_SIGNAL_NUMBER)
223  return SetShouldSuppress(signo, value);
224  return false;
225 }
226 
227 bool UnixSignals::GetShouldStop(int signo) const {
228  collection::const_iterator pos = m_signals.find(signo);
229  if (pos != m_signals.end())
230  return pos->second.m_stop;
231  return false;
232 }
233 
234 bool UnixSignals::SetShouldStop(int signo, bool value) {
235  collection::iterator pos = m_signals.find(signo);
236  if (pos != m_signals.end()) {
237  pos->second.m_stop = value;
238  ++m_version;
239  return true;
240  }
241  return false;
242 }
243 
244 bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
245  const int32_t signo = GetSignalNumberFromName(signal_name);
246  if (signo != LLDB_INVALID_SIGNAL_NUMBER)
247  return SetShouldStop(signo, value);
248  return false;
249 }
250 
251 bool UnixSignals::GetShouldNotify(int signo) const {
252  collection::const_iterator pos = m_signals.find(signo);
253  if (pos != m_signals.end())
254  return pos->second.m_notify;
255  return false;
256 }
257 
258 bool UnixSignals::SetShouldNotify(int signo, bool value) {
259  collection::iterator pos = m_signals.find(signo);
260  if (pos != m_signals.end()) {
261  pos->second.m_notify = value;
262  ++m_version;
263  return true;
264  }
265  return false;
266 }
267 
268 bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
269  const int32_t signo = GetSignalNumberFromName(signal_name);
270  if (signo != LLDB_INVALID_SIGNAL_NUMBER)
271  return SetShouldNotify(signo, value);
272  return false;
273 }
274 
275 int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
276 
277 int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
278  if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
280  auto it = m_signals.begin();
281  std::advance(it, index);
282  return it->first;
283 }
284 
285 uint64_t UnixSignals::GetVersion() const { return m_version; }
286 
287 std::vector<int32_t>
288 UnixSignals::GetFilteredSignals(llvm::Optional<bool> should_suppress,
289  llvm::Optional<bool> should_stop,
290  llvm::Optional<bool> should_notify) {
291  std::vector<int32_t> result;
292  for (int32_t signo = GetFirstSignalNumber();
294  signo = GetNextSignalNumber(signo)) {
295 
296  bool signal_suppress = false;
297  bool signal_stop = false;
298  bool signal_notify = false;
299  GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
300 
301  // If any of filtering conditions are not met, we move on to the next
302  // signal.
303  if (should_suppress &&
304  signal_suppress != should_suppress.getValue())
305  continue;
306 
307  if (should_stop && signal_stop != should_stop.getValue())
308  continue;
309 
310  if (should_notify && signal_notify != should_notify.getValue())
311  continue;
312 
313  result.push_back(signo);
314  }
315 
316  return result;
317 }
318 
320  collection::iterator pos = m_signals.find(signo);
321  if (pos != m_signals.end())
322  pos->second.m_hit_count += 1;
323 }
324 
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)
340  if (reset_notify)
342  if (reset_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 
lldb_private::UnixSignals::UnixSignals
UnixSignals()
Definition: UnixSignals.cpp:63
llvm
Definition: Debugger.h:50
FreeBSDSignals.h
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::UnixSignals::Signal::m_default_suppress
bool m_default_suppress
Definition: UnixSignals.h:120
lldb_private::UnixSignals::GetShouldStop
bool GetShouldStop(int32_t signo) const
Definition: UnixSignals.cpp:227
LinuxSignals.h
lldb_private::UnixSignals::AddSignal
void AddSignal(int signo, const char *name, bool default_suppress, bool default_stop, bool default_notify, const char *description, const char *alias=nullptr)
Definition: UnixSignals.cpp:115
lldb_private::UnixSignals::Reset
virtual void Reset()
Definition: UnixSignals.cpp:69
lldb_private::UnixSignals::GetSignalNumberFromName
int32_t GetSignalNumberFromName(const char *name) const
Definition: UnixSignals.cpp:149
lldb_private::UnixSignals::RemoveSignal
void RemoveSignal(int signo)
Definition: UnixSignals.cpp:124
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb_private::UnixSignals::IncrementSignalHitCount
void IncrementSignalHitCount(int signo)
Track how many times signals are hit as stop reasons.
Definition: UnixSignals.cpp:319
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:455
lldb_private::UnixSignals::SetShouldSuppress
bool SetShouldSuppress(int32_t signo, bool value)
lldb_private::UnixSignals::Signal::Reset
void Reset(bool reset_stop, bool reset_notify, bool reset_suppress)
Definition: UnixSignals.cpp:336
lldb_private::UnixSignals::GetShouldSuppress
bool GetShouldSuppress(int32_t signo) const
Definition: UnixSignals.cpp:203
lldb_private::UnixSignals::GetHitCountStatistics
llvm::json::Value GetHitCountStatistics() const
Get the hit count statistics for signals.
Definition: UnixSignals.cpp:325
lldb_private::UnixSignals::~UnixSignals
virtual ~UnixSignals()
lldb_private::UnixSignals::GetShouldNotify
bool GetShouldNotify(int32_t signo) const
Definition: UnixSignals.cpp:251
lldb_private::UnixSignals::Signal::Signal
Signal(const char *name, bool default_suppress, bool default_stop, bool default_notify, const char *description, const char *alias)
Definition: UnixSignals.cpp:20
lldb_private::UnixSignals::GetNumSignals
int32_t GetNumSignals() const
Definition: UnixSignals.cpp:275
lldb_private::UnixSignals::GetFirstSignalNumber
int32_t GetFirstSignalNumber() const
Definition: UnixSignals.cpp:167
LLDB_INVALID_SIGNAL_NUMBER
#define LLDB_INVALID_SIGNAL_NUMBER
Definition: lldb-defines.h:84
lldb_private::UnixSignals::m_version
uint64_t m_version
Definition: UnixSignals.h:139
lldb_private::UnixSignals::GetSignalAtIndex
int32_t GetSignalAtIndex(int32_t index) const
Definition: UnixSignals.cpp:277
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
UnixSignals.h
lldb_private::UnixSignals::Signal::m_name
ConstString m_name
Definition: UnixSignals.h:115
lldb_private::UnixSignals::SetShouldStop
bool SetShouldStop(int32_t signo, bool value)
lldb_private::UnixSignals::GetShortName
ConstString GetShortName(ConstString name) const
Definition: UnixSignals.cpp:143
lldb_private::UnixSignals::CreateForHost
static lldb::UnixSignalsSP CreateForHost()
Definition: UnixSignals.cpp:56
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::UnixSignals::GetVersion
uint64_t GetVersion() const
Definition: UnixSignals.cpp:285
lldb_private::UnixSignals::Signal::m_notify
bool m_notify
Definition: UnixSignals.h:119
lldb_private::UnixSignals
Definition: UnixSignals.h:23
lldb_private::UnixSignals::Signal
Definition: UnixSignals.h:114
HostInfo.h
lldb_private::UnixSignals::Signal::m_stop
bool m_stop
Definition: UnixSignals.h:119
lldb_private::UnixSignals::GetSignalInfo
const char * GetSignalInfo(int32_t signo, bool &should_suppress, bool &should_stop, bool &should_notify) const
Definition: UnixSignals.cpp:188
lldb_private::UnixSignals::GetNextSignalNumber
int32_t GetNextSignalNumber(int32_t current_signal) const
Definition: UnixSignals.cpp:174
lldb_private::UnixSignals::GetFilteredSignals
std::vector< int32_t > GetFilteredSignals(llvm::Optional< bool > should_suppress, llvm::Optional< bool > should_stop, llvm::Optional< bool > should_notify)
Definition: UnixSignals.cpp:288
lldb_private::UnixSignals::SignalIsValid
bool SignalIsValid(int32_t signo) const
Definition: UnixSignals.cpp:139
ArchSpec.h
lldb_private::UnixSignals::Signal::m_suppress
bool m_suppress
Definition: UnixSignals.h:119
MipsLinuxSignals.h
NetBSDSignals.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::UnixSignals::GetSignalAsCString
const char * GetSignalAsCString(int32_t signo) const
Definition: UnixSignals.cpp:131
lldb_private::UnixSignals::Signal::m_default_notify
bool m_default_notify
Definition: UnixSignals.h:120
lldb_private::UnixSignals::SetShouldNotify
bool SetShouldNotify(int32_t signo, bool value)
lldb_private::UnixSignals::ResetSignal
bool ResetSignal(int32_t signo, bool reset_stop=true, bool reset_notify=true, bool reset_suppress=true)
Definition: UnixSignals.cpp:346
lldb_private::UnixSignals::Signal::m_description
std::string m_description
Definition: UnixSignals.h:117
lldb_private::UnixSignals::m_signals
collection m_signals
Definition: UnixSignals.h:133
lldb_private::UnixSignals::Signal::m_default_stop
bool m_default_stop
Definition: UnixSignals.h:120
lldb_private::UnixSignals::Create
static lldb::UnixSignalsSP Create(const ArchSpec &arch)
Definition: UnixSignals.cpp:32