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 | |
9 | #include "lldb/Target/UnixSignals.h" |
10 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
11 | #include "Plugins/Process/Utility/LinuxSignals.h" |
12 | #include "Plugins/Process/Utility/NetBSDSignals.h" |
13 | #include "lldb/Host/HostInfo.h" |
14 | #include "lldb/Utility/ArchSpec.h" |
15 | #include <optional> |
16 | #include <sstream> |
17 | |
18 | using namespace lldb_private; |
19 | using namespace llvm; |
20 | |
21 | UnixSignals::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 | |
29 | lldb::UnixSignalsSP UnixSignals::Create(const ArchSpec &arch) { |
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 | |
44 | lldb::UnixSignalsSP UnixSignals::CreateForHost() { |
45 | static lldb::UnixSignalsSP s_unix_signals_sp = |
46 | Create(arch: HostInfo::GetArchitecture()); |
47 | return s_unix_signals_sp; |
48 | } |
49 | |
50 | // UnixSignals constructor |
51 | UnixSignals::UnixSignals() { Reset(); } |
52 | |
53 | UnixSignals::UnixSignals(const UnixSignals &rhs) : m_signals(rhs.m_signals) {} |
54 | |
55 | UnixSignals::~UnixSignals() = default; |
56 | |
57 | void UnixSignals::Reset() { |
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(signo: 1, name: "SIGHUP" , default_suppress: false, default_stop: true, default_notify: true, description: "hangup" ); |
70 | AddSignal(signo: 2, name: "SIGINT" , default_suppress: true, default_stop: true, default_notify: true, description: "interrupt" ); |
71 | AddSignal(signo: 3, name: "SIGQUIT" , default_suppress: false, default_stop: true, default_notify: true, description: "quit" ); |
72 | AddSignal(signo: 4, name: "SIGILL" , default_suppress: false, default_stop: true, default_notify: true, description: "illegal instruction" ); |
73 | AddSignal(signo: 5, name: "SIGTRAP" , default_suppress: true, default_stop: true, default_notify: true, description: "trace trap (not reset when caught)" ); |
74 | AddSignal(signo: 6, name: "SIGABRT" , default_suppress: false, default_stop: true, default_notify: true, description: "abort()" ); |
75 | AddSignal(signo: 7, name: "SIGEMT" , default_suppress: false, default_stop: true, default_notify: true, description: "pollable event" ); |
76 | AddSignal(signo: 8, name: "SIGFPE" , default_suppress: false, default_stop: true, default_notify: true, description: "floating point exception" ); |
77 | AddSignal(signo: 9, name: "SIGKILL" , default_suppress: false, default_stop: true, default_notify: true, description: "kill" ); |
78 | AddSignal(signo: 10, name: "SIGBUS" , default_suppress: false, default_stop: true, default_notify: true, description: "bus error" ); |
79 | AddSignal(signo: 11, name: "SIGSEGV" , default_suppress: false, default_stop: true, default_notify: true, description: "segmentation violation" ); |
80 | AddSignal(signo: 12, name: "SIGSYS" , default_suppress: false, default_stop: true, default_notify: true, description: "bad argument to system call" ); |
81 | AddSignal(signo: 13, name: "SIGPIPE" , default_suppress: false, default_stop: false, default_notify: false, description: "write on a pipe with no one to read it" ); |
82 | AddSignal(signo: 14, name: "SIGALRM" , default_suppress: false, default_stop: false, default_notify: false, description: "alarm clock" ); |
83 | AddSignal(signo: 15, name: "SIGTERM" , default_suppress: false, default_stop: true, default_notify: true, description: "software termination signal from kill" ); |
84 | AddSignal(signo: 16, name: "SIGURG" , default_suppress: false, default_stop: false, default_notify: false, description: "urgent condition on IO channel" ); |
85 | AddSignal(signo: 17, name: "SIGSTOP" , default_suppress: true, default_stop: true, default_notify: true, description: "sendable stop signal not from tty" ); |
86 | AddSignal(signo: 18, name: "SIGTSTP" , default_suppress: false, default_stop: true, default_notify: true, description: "stop signal from tty" ); |
87 | AddSignal(signo: 19, name: "SIGCONT" , default_suppress: false, default_stop: false, default_notify: true, description: "continue a stopped process" ); |
88 | AddSignal(signo: 20, name: "SIGCHLD" , default_suppress: false, default_stop: false, default_notify: false, description: "to parent on child stop or exit" ); |
89 | AddSignal(signo: 21, name: "SIGTTIN" , default_suppress: false, default_stop: true, default_notify: true, description: "to readers process group upon background tty read" ); |
90 | AddSignal(signo: 22, name: "SIGTTOU" , default_suppress: false, default_stop: true, default_notify: true, description: "to readers process group upon background tty write" ); |
91 | AddSignal(signo: 23, name: "SIGIO" , default_suppress: false, default_stop: false, default_notify: false, description: "input/output possible signal" ); |
92 | AddSignal(signo: 24, name: "SIGXCPU" , default_suppress: false, default_stop: true, default_notify: true, description: "exceeded CPU time limit" ); |
93 | AddSignal(signo: 25, name: "SIGXFSZ" , default_suppress: false, default_stop: true, default_notify: true, description: "exceeded file size limit" ); |
94 | AddSignal(signo: 26, name: "SIGVTALRM" , default_suppress: false, default_stop: false, default_notify: false, description: "virtual time alarm" ); |
95 | AddSignal(signo: 27, name: "SIGPROF" , default_suppress: false, default_stop: false, default_notify: false, description: "profiling time alarm" ); |
96 | AddSignal(signo: 28, name: "SIGWINCH" , default_suppress: false, default_stop: false, default_notify: false, description: "window size changes" ); |
97 | AddSignal(signo: 29, name: "SIGINFO" , default_suppress: false, default_stop: true, default_notify: true, description: "information request" ); |
98 | AddSignal(signo: 30, name: "SIGUSR1" , default_suppress: false, default_stop: true, default_notify: true, description: "user defined signal 1" ); |
99 | AddSignal(signo: 31, name: "SIGUSR2" , default_suppress: false, default_stop: true, default_notify: true, description: "user defined signal 2" ); |
100 | // clang-format on |
101 | } |
102 | |
103 | void 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(x: std::make_pair(x&: signo, y&: new_signal)); |
110 | ++m_version; |
111 | } |
112 | |
113 | void UnixSignals::AddSignalCode(int signo, int code, |
114 | const llvm::StringLiteral description, |
115 | SignalCodePrintOption print_option) { |
116 | collection::iterator signal = m_signals.find(x: 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 | x: std::pair{code, SignalCode{.m_description: description, .m_print_option: print_option}}); |
121 | ++m_version; |
122 | } |
123 | |
124 | void UnixSignals::RemoveSignal(int signo) { |
125 | collection::iterator pos = m_signals.find(x: signo); |
126 | if (pos != m_signals.end()) |
127 | m_signals.erase(position: pos); |
128 | ++m_version; |
129 | } |
130 | |
131 | llvm::StringRef UnixSignals::GetSignalAsStringRef(int32_t signo) const { |
132 | const auto pos = m_signals.find(x: signo); |
133 | if (pos == m_signals.end()) |
134 | return {}; |
135 | return pos->second.m_name; |
136 | } |
137 | |
138 | std::string |
139 | UnixSignals::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(x: 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(x: *code); |
152 | if (cpos != pos->second.m_codes.end()) { |
153 | const SignalCode &sc = cpos->second; |
154 | str += ": " ; |
155 | if (sc.m_print_option != SignalCodePrintOption::Bounds) |
156 | str += sc.m_description.str(); |
157 | |
158 | std::stringstream strm; |
159 | switch (sc.m_print_option) { |
160 | case SignalCodePrintOption::None: |
161 | break; |
162 | case SignalCodePrintOption::Address: |
163 | if (addr) |
164 | strm << " (fault address: 0x" << std::hex << *addr << ")" ; |
165 | break; |
166 | case SignalCodePrintOption::Bounds: |
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 | |
190 | bool UnixSignals::SignalIsValid(int32_t signo) const { |
191 | return m_signals.find(x: signo) != m_signals.end(); |
192 | } |
193 | |
194 | llvm::StringRef UnixSignals::GetShortName(llvm::StringRef name) const { |
195 | return name.substr(Start: 3); // Remove "SIG" from name |
196 | } |
197 | |
198 | int32_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(name: pos->second.m_name)) || |
205 | (name_ref == GetShortName(name: pos->second.m_alias))) |
206 | return pos->first; |
207 | } |
208 | |
209 | int32_t signo; |
210 | if (llvm::to_integer(S: name, Num&: signo)) |
211 | return signo; |
212 | return LLDB_INVALID_SIGNAL_NUMBER; |
213 | } |
214 | |
215 | int32_t UnixSignals::GetFirstSignalNumber() const { |
216 | if (m_signals.empty()) |
217 | return LLDB_INVALID_SIGNAL_NUMBER; |
218 | |
219 | return (*m_signals.begin()).first; |
220 | } |
221 | |
222 | int32_t UnixSignals::GetNextSignalNumber(int32_t current_signal) const { |
223 | collection::const_iterator pos = m_signals.find(x: current_signal); |
224 | collection::const_iterator end = m_signals.end(); |
225 | if (pos == end) |
226 | return LLDB_INVALID_SIGNAL_NUMBER; |
227 | else { |
228 | pos++; |
229 | if (pos == end) |
230 | return LLDB_INVALID_SIGNAL_NUMBER; |
231 | else |
232 | return pos->first; |
233 | } |
234 | } |
235 | |
236 | bool UnixSignals::GetSignalInfo(int32_t signo, bool &should_suppress, |
237 | bool &should_stop, bool &should_notify) const { |
238 | const auto pos = m_signals.find(x: 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 | |
249 | bool UnixSignals::GetShouldSuppress(int signo) const { |
250 | collection::const_iterator pos = m_signals.find(x: signo); |
251 | if (pos != m_signals.end()) |
252 | return pos->second.m_suppress; |
253 | return false; |
254 | } |
255 | |
256 | bool UnixSignals::SetShouldSuppress(int signo, bool value) { |
257 | collection::iterator pos = m_signals.find(x: 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 | |
266 | bool UnixSignals::SetShouldSuppress(const char *signal_name, bool value) { |
267 | const int32_t signo = GetSignalNumberFromName(name: signal_name); |
268 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
269 | return SetShouldSuppress(signo, value); |
270 | return false; |
271 | } |
272 | |
273 | bool UnixSignals::GetShouldStop(int signo) const { |
274 | collection::const_iterator pos = m_signals.find(x: signo); |
275 | if (pos != m_signals.end()) |
276 | return pos->second.m_stop; |
277 | return false; |
278 | } |
279 | |
280 | bool UnixSignals::SetShouldStop(int signo, bool value) { |
281 | collection::iterator pos = m_signals.find(x: 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 | |
290 | bool UnixSignals::SetShouldStop(const char *signal_name, bool value) { |
291 | const int32_t signo = GetSignalNumberFromName(name: signal_name); |
292 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
293 | return SetShouldStop(signo, value); |
294 | return false; |
295 | } |
296 | |
297 | bool UnixSignals::GetShouldNotify(int signo) const { |
298 | collection::const_iterator pos = m_signals.find(x: signo); |
299 | if (pos != m_signals.end()) |
300 | return pos->second.m_notify; |
301 | return false; |
302 | } |
303 | |
304 | bool UnixSignals::SetShouldNotify(int signo, bool value) { |
305 | collection::iterator pos = m_signals.find(x: 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 | |
314 | bool UnixSignals::SetShouldNotify(const char *signal_name, bool value) { |
315 | const int32_t signo = GetSignalNumberFromName(name: signal_name); |
316 | if (signo != LLDB_INVALID_SIGNAL_NUMBER) |
317 | return SetShouldNotify(signo, value); |
318 | return false; |
319 | } |
320 | |
321 | int32_t UnixSignals::GetNumSignals() const { return m_signals.size(); } |
322 | |
323 | int32_t UnixSignals::GetSignalAtIndex(int32_t index) const { |
324 | if (index < 0 || m_signals.size() <= static_cast<size_t>(index)) |
325 | return LLDB_INVALID_SIGNAL_NUMBER; |
326 | auto it = m_signals.begin(); |
327 | std::advance(i&: it, n: index); |
328 | return it->first; |
329 | } |
330 | |
331 | uint64_t UnixSignals::GetVersion() const { return m_version; } |
332 | |
333 | std::vector<int32_t> |
334 | UnixSignals::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(); |
339 | signo != LLDB_INVALID_SIGNAL_NUMBER; |
340 | signo = GetNextSignalNumber(current_signal: signo)) { |
341 | |
342 | bool signal_suppress = false; |
343 | bool signal_stop = false; |
344 | bool signal_notify = false; |
345 | GetSignalInfo(signo, should_suppress&: signal_suppress, should_stop&: signal_stop, should_notify&: 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(x: signo); |
359 | } |
360 | |
361 | return result; |
362 | } |
363 | |
364 | void UnixSignals::IncrementSignalHitCount(int signo) { |
365 | collection::iterator pos = m_signals.find(x: signo); |
366 | if (pos != m_signals.end()) |
367 | pos->second.m_hit_count += 1; |
368 | } |
369 | |
370 | json::Value UnixSignals::GetHitCountStatistics() const { |
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 | A: json::Object{{.K: pair.second.m_name, .V: pair.second.m_hit_count}}); |
376 | } |
377 | return std::move(json_signals); |
378 | } |
379 | |
380 | void UnixSignals::Signal::Reset(bool reset_stop, bool reset_notify, |
381 | bool reset_suppress) { |
382 | if (reset_stop) |
383 | m_stop = m_default_stop; |
384 | if (reset_notify) |
385 | m_notify = m_default_notify; |
386 | if (reset_suppress) |
387 | m_suppress = m_default_suppress; |
388 | } |
389 | |
390 | bool UnixSignals::ResetSignal(int32_t signo, bool reset_stop, |
391 | bool reset_notify, bool reset_suppress) { |
392 | auto elem = m_signals.find(x: 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 | |
399 | |