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#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
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
47 static lldb::UnixSignalsSP s_unix_signals_sp =
48 Create(HostInfo::GetArchitecture());
49 return s_unix_signals_sp;
50}
51
52// UnixSignals constructor
54
56
58
60 // This builds one standard set of Unix Signals. If yours aren't quite in
61 // this order, you can either subclass this class, and use Add & Remove to
62 // 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 off
69 // SIGNO NAME SUPPRESS STOP NOTIFY DESCRIPTION
70 // ====== ============== ======== ====== ====== ===================================================
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 on
103}
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
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
140std::string
141UnixSignals::GetSignalDescription(int32_t signo, std::optional<int32_t> code,
142 std::optional<lldb::addr_t> addr,
143 std::optional<lldb::addr_t> lower,
144 std::optional<lldb::addr_t> upper) const {
145 std::string str;
146
147 collection::const_iterator pos = m_signals.find(signo);
148 if (pos != m_signals.end()) {
149 str = pos->second.m_name.str();
150
151 if (code) {
152 std::map<int32_t, SignalCode>::const_iterator cpos =
153 pos->second.m_codes.find(*code);
154 if (cpos != pos->second.m_codes.end()) {
155 const SignalCode &sc = cpos->second;
156 str += ": ";
158 str += sc.m_description.str();
159
160 std::stringstream strm;
161 switch (sc.m_print_option) {
163 break;
165 if (addr)
166 strm << " (fault address=0x" << std::hex << *addr << ")";
167 break;
169 if (lower && upper && addr) {
170 if ((unsigned long)(*addr) < *lower)
171 strm << "lower bound violation ";
172 else
173 strm << "upper bound violation ";
174
175 strm << "(fault address=0x" << std::hex << *addr;
176 strm << ", lower bound=0x" << std::hex << *lower;
177 strm << ", upper bound=0x" << std::hex << *upper;
178 strm << ")";
179 } else
180 strm << sc.m_description.str();
181
182 break;
183 }
184 str += strm.str();
185 }
186 }
187 }
188
189 return str;
190}
191
192bool UnixSignals::SignalIsValid(int32_t signo) const {
193 return m_signals.find(signo) != m_signals.end();
194}
195
196llvm::StringRef UnixSignals::GetShortName(llvm::StringRef name) const {
197 return name.substr(3); // Remove "SIG" from name
198}
199
200int32_t UnixSignals::GetSignalNumberFromName(const char *name) const {
201 llvm::StringRef name_ref(name);
202
203 collection::const_iterator pos, end = m_signals.end();
204 for (pos = m_signals.begin(); pos != end; pos++) {
205 if ((name_ref == pos->second.m_name) || (name_ref == pos->second.m_alias) ||
206 (name_ref == GetShortName(pos->second.m_name)) ||
207 (name_ref == GetShortName(pos->second.m_alias)))
208 return pos->first;
209 }
210
211 int32_t signo;
212 if (llvm::to_integer(name, signo))
213 return signo;
215}
216
218 if (m_signals.empty())
220
221 return (*m_signals.begin()).first;
222}
223
224int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const {
225 collection::const_iterator pos = m_signals.find(current_signal);
226 collection::const_iterator end = m_signals.end();
227 if (pos == end)
229 else {
230 pos++;
231 if (pos == end)
233 else
234 return pos->first;
235 }
236}
237
238bool UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress,
239 bool &should_stop, bool &should_notify) const {
240 const auto pos = m_signals.find(signo);
241 if (pos == m_signals.end())
242 return false;
243
244 const Signal &signal = pos->second;
245 should_suppress = signal.m_suppress;
246 should_stop = signal.m_stop;
247 should_notify = signal.m_notify;
248 return true;
249}
250
251bool UnixSignals::GetShouldSuppress(int signo) const {
252 collection::const_iterator pos = m_signals.find(signo);
253 if (pos != m_signals.end())
254 return pos->second.m_suppress;
255 return false;
256}
257
258bool UnixSignals::SetShouldSuppress(int signo, bool value) {
259 collection::iterator pos = m_signals.find(signo);
260 if (pos != m_signals.end()) {
261 pos->second.m_suppress = value;
262 ++m_version;
263 return true;
264 }
265 return false;
266}
267
268bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) {
269 const int32_t signo = GetSignalNumberFromName(signal_name);
270 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
271 return SetShouldSuppress(signo, value);
272 return false;
273}
274
275bool UnixSignals::GetShouldStop(int signo) const {
276 collection::const_iterator pos = m_signals.find(signo);
277 if (pos != m_signals.end())
278 return pos->second.m_stop;
279 return false;
280}
281
282bool UnixSignals::SetShouldStop(int signo, bool value) {
283 collection::iterator pos = m_signals.find(signo);
284 if (pos != m_signals.end()) {
285 pos->second.m_stop = value;
286 ++m_version;
287 return true;
288 }
289 return false;
290}
291
292bool UnixSignals::SetShouldStop(const char *signal_name, bool value) {
293 const int32_t signo = GetSignalNumberFromName(signal_name);
294 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
295 return SetShouldStop(signo, value);
296 return false;
297}
298
299bool UnixSignals::GetShouldNotify(int signo) const {
300 collection::const_iterator pos = m_signals.find(signo);
301 if (pos != m_signals.end())
302 return pos->second.m_notify;
303 return false;
304}
305
306bool UnixSignals::SetShouldNotify(int signo, bool value) {
307 collection::iterator pos = m_signals.find(signo);
308 if (pos != m_signals.end()) {
309 pos->second.m_notify = value;
310 ++m_version;
311 return true;
312 }
313 return false;
314}
315
316bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) {
317 const int32_t signo = GetSignalNumberFromName(signal_name);
318 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
319 return SetShouldNotify(signo, value);
320 return false;
321}
322
323int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); }
324
325int32_t UnixSignals::GetSignalAtIndex(int32_t index) const {
326 if (index < 0 || m_signals.size() <= static_cast<size_t>(index))
328 auto it = m_signals.begin();
329 std::advance(it, index);
330 return it->first;
331}
332
333uint64_t UnixSignals::GetVersion() const { return m_version; }
334
335std::vector<int32_t>
336UnixSignals::GetFilteredSignals(std::optional<bool> should_suppress,
337 std::optional<bool> should_stop,
338 std::optional<bool> should_notify) {
339 std::vector<int32_t> result;
340 for (int32_t signo = GetFirstSignalNumber();
342 signo = GetNextSignalNumber(signo)) {
343
344 bool signal_suppress = false;
345 bool signal_stop = false;
346 bool signal_notify = false;
347 GetSignalInfo(signo, signal_suppress, signal_stop, signal_notify);
348
349 // If any of filtering conditions are not met, we move on to the next
350 // signal.
351 if (should_suppress && signal_suppress != *should_suppress)
352 continue;
353
354 if (should_stop && signal_stop != *should_stop)
355 continue;
356
357 if (should_notify && signal_notify != *should_notify)
358 continue;
359
360 result.push_back(signo);
361 }
362
363 return result;
364}
365
367 collection::iterator pos = m_signals.find(signo);
368 if (pos != m_signals.end())
369 pos->second.m_hit_count += 1;
370}
371
373 json::Array json_signals;
374 for (const auto &pair : m_signals) {
375 if (pair.second.m_hit_count > 0)
376 json_signals.emplace_back(
377 json::Object{{pair.second.m_name, pair.second.m_hit_count}});
378 }
379 return std::move(json_signals);
380}
381
382void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify,
383 bool reset_suppress) {
384 if (reset_stop)
386 if (reset_notify)
388 if (reset_suppress)
390}
391
392bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop,
393 bool reset_notify, bool reset_suppress) {
394 auto elem = m_signals.find(signo);
395 if (elem == m_signals.end())
396 return false;
397 (*elem).second.Reset(reset_stop, reset_notify, reset_suppress);
398 return true;
399}
400
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:463
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:46
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:30
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.
std::shared_ptr< lldb_private::UnixSignals > UnixSignalsSP
Definition: lldb-forward.h:481
Definition: Debugger.h:54
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:22
void Reset(bool reset_stop, bool reset_notify, bool reset_suppress)