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"
16#include <optional>
17
18using namespace lldb_private;
19using namespace llvm;
20
21UnixSignals::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
33lldb::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
57lldb::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
65
67
69
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
116void 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
126 collection::iterator pos = m_signals.find(signo);
127 if (pos != m_signals.end())
128 m_signals.erase(pos);
129 ++m_version;
130}
131
132const 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
140bool UnixSignals::SignalIsValid(int32_t signo) const {
141 return m_signals.find(signo) != m_signals.end();
142}
143
145 if (name)
146 return ConstString(name.GetStringRef().substr(3)); // Remove "SIG" from name
147 return name;
148}
149
150int32_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;
166}
167
169 if (m_signals.empty())
171
172 return (*m_signals.begin()).first;
173}
174
175int32_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)
180 else {
181 pos++;
182 if (pos == end)
184 else
185 return pos->first;
186 }
187}
188
189const 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
204bool 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
211bool 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
221bool 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
228bool 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
235bool 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
245bool 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
252bool 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
259bool 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
269bool 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
276int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
277
278int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
279 if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
281 auto it = m_signals.begin();
282 std::advance(it, index);
283 return it->first;
284}
285
286uint64_t UnixSignals::GetVersion() const { return m_version; }
287
288std::vector<int32_t>
289UnixSignals::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();
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
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
336void 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
346bool 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
An architecture specification class.
Definition: ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:463
A uniqued constant string class.
Definition: ConstString.h:39
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:192
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
int32_t GetSignalNumberFromName(const char *name) const
static lldb::UnixSignalsSP CreateForHost()
Definition: UnixSignals.cpp:57
const char * GetSignalInfo(int32_t signo, bool &should_suppress, bool &should_stop, bool &should_notify) const
ConstString GetShortName(ConstString name) const
int32_t GetSignalAtIndex(int32_t index) const
bool GetShouldNotify(int32_t signo) const
std::vector< int32_t > GetFilteredSignals(std::optional< bool > should_suppress, std::optional< bool > should_stop, std::optional< bool > should_notify)
void IncrementSignalHitCount(int signo)
Track how many times signals are hit as stop reasons.
bool SetShouldStop(int32_t signo, bool value)
void RemoveSignal(int signo)
bool SetShouldSuppress(int32_t signo, bool value)
int32_t GetFirstSignalNumber() const
static lldb::UnixSignalsSP Create(const ArchSpec &arch)
Definition: UnixSignals.cpp:33
int32_t GetNumSignals() const
bool ResetSignal(int32_t signo, bool reset_stop=true, bool reset_notify=true, bool reset_suppress=true)
bool SignalIsValid(int32_t signo) const
llvm::json::Value GetHitCountStatistics() const
Get the hit count statistics for signals.
void AddSignal(int signo, const char *name, bool default_suppress, bool default_stop, bool default_notify, const char *description, const char *alias=nullptr)
int32_t GetNextSignalNumber(int32_t current_signal) const
bool SetShouldNotify(int32_t signo, bool value)
const char * GetSignalAsCString(int32_t signo) const
bool GetShouldSuppress(int32_t signo) const
bool GetShouldStop(int32_t signo) const
uint64_t GetVersion() const
#define LLDB_INVALID_SIGNAL_NUMBER
Definition: lldb-defines.h:84
A class that represents a running process on the host machine.
Definition: Debugger.h:51
Signal(const char *name, bool default_suppress, bool default_stop, bool default_notify, const char *description, const char *alias)
Definition: UnixSignals.cpp:21
void Reset(bool reset_stop, bool reset_notify, bool reset_suppress)