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