blob: c7bb40e8fba2306d6af155350ff632905e13a5c1 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- UnixSignals.cpp -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/UnixSignals.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
17using namespace lldb_private;
18
Greg Claytondeb391c2010-10-15 02:39:01 +000019UnixSignals::Signal::Signal
20(
21 const char *name,
22 const char *short_name,
23 bool default_suppress,
24 bool default_stop,
25 bool default_notify,
26 const char *description
27) :
Chris Lattner24943d22010-06-08 16:52:24 +000028 m_name (name),
Greg Claytondeb391c2010-10-15 02:39:01 +000029 m_short_name (short_name),
30 m_description (),
31 m_suppress (default_suppress),
32 m_stop (default_stop),
33 m_notify (default_notify)
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Greg Claytondeb391c2010-10-15 02:39:01 +000035 if (description)
36 m_description.assign (description);
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
39//----------------------------------------------------------------------
40// UnixSignals constructor
41//----------------------------------------------------------------------
42UnixSignals::UnixSignals ()
43{
44 Reset ();
45}
46
47//----------------------------------------------------------------------
48// Destructor
49//----------------------------------------------------------------------
50UnixSignals::~UnixSignals ()
51{
52}
53
54void
55UnixSignals::Reset ()
56{
57 // This builds one standard set of Unix Signals. If yours aren't quite in this
58 // order, you can either subclass this class, and use Add & Remove to change them
59 // or you can subclass and build them afresh in your constructor;
60 m_signals.clear();
Greg Claytondeb391c2010-10-15 02:39:01 +000061 // SIGNO NAME SHORT NAME SUPPRESS STOP NOTIFY DESCRIPTION
62 // ====== ============ ========== ========= ====== ====== ===================================================
63 AddSignal (1, "SIGHUP", "HUP", false, true, true, "hangup");
64 AddSignal (2, "SIGINT", "INT", true, true, true, "interrupt");
65 AddSignal (3, "SIGQUIT", "QUIT", false, true, true, "quit");
66 AddSignal (4, "SIGILL", "ILL", false, true, true, "illegal instruction");
67 AddSignal (5, "SIGTRAP", "TRAP", true, true, true, "trace trap (not reset when caught)");
68 AddSignal (6, "SIGABRT", "ABRT", false, true, true, "abort()");
69 AddSignal (7, "SIGEMT", "EMT", false, true, true, "pollable event");
70 AddSignal (8, "SIGFPE", "FPE", false, true, true, "floating point exception");
71 AddSignal (9, "SIGKILL", "KILL", false, true, true, "kill");
72 AddSignal (10, "SIGBUS", "BUS", false, true, true, "bus error");
73 AddSignal (11, "SIGSEGV", "SEGV", false, true, true, "segmentation violation");
74 AddSignal (12, "SIGSYS", "SYS", false, true, true, "bad argument to system call");
75 AddSignal (13, "SIGPIPE", "PIPE", false, true, true, "write on a pipe with no one to read it");
76 AddSignal (14, "SIGALRM", "ALRM", false, false, true, "alarm clock");
77 AddSignal (15, "SIGTERM", "TERM", false, true, true, "software termination signal from kill");
78 AddSignal (16, "SIGURG", "URG", false, false, false, "urgent condition on IO channel");
79 AddSignal (17, "SIGSTOP", "STOP", false, true, true, "sendable stop signal not from tty");
80 AddSignal (18, "SIGTSTP", "TSTP", false, true, true, "stop signal from tty");
81 AddSignal (19, "SIGCONT", "CONT", false, true, true, "continue a stopped process");
82 AddSignal (20, "SIGCHLD", "CHLD", false, false, true, "to parent on child stop or exit");
83 AddSignal (21, "SIGTTIN", "TTIN", false, true, true, "to readers process group upon background tty read");
84 AddSignal (22, "SIGTTOU", "TTOU", false, true, true, "to readers process group upon background tty write");
85 AddSignal (23, "SIGIO", "IO", false, false, false, "input/output possible signal");
86 AddSignal (24, "SIGXCPU", "XCPU", false, true, true, "exceeded CPU time limit");
87 AddSignal (25, "SIGXFSZ", "XFSZ", false, true, true, "exceeded file size limit");
88 AddSignal (26, "SIGVTALRM", "VTALRM", false, false, false, "virtual time alarm");
89 AddSignal (27, "SIGPROF", "PROF", false, false, false, "profiling time alarm");
90 AddSignal (28, "SIGWINCH", "WINCH", false, false, false, "window size changes");
91 AddSignal (29, "SIGINFO", "INFO", false, true, true, "information request");
92 AddSignal (30, "SIGUSR1", "USR1", false, true, true, "user defined signal 1");
93 AddSignal (31, "SIGUSR2", "USR2", false, true, true, "user defined signal 2");
Chris Lattner24943d22010-06-08 16:52:24 +000094}
Greg Clayton8f6be2a2010-10-09 01:40:57 +000095
Chris Lattner24943d22010-06-08 16:52:24 +000096void
Greg Claytondeb391c2010-10-15 02:39:01 +000097UnixSignals::AddSignal
98(
99 int signo,
100 const char *name,
101 const char *short_name,
102 bool default_suppress,
103 bool default_stop,
104 bool default_notify,
105 const char *description
106)
Chris Lattner24943d22010-06-08 16:52:24 +0000107{
Greg Claytondeb391c2010-10-15 02:39:01 +0000108 Signal new_signal (name, short_name, default_suppress, default_stop, default_notify, description);
109 m_signals.insert (std::make_pair(signo, new_signal));
Chris Lattner24943d22010-06-08 16:52:24 +0000110}
111
112void
113UnixSignals::RemoveSignal (int signo)
114{
115 collection::iterator pos = m_signals.find (signo);
116 if (pos != m_signals.end())
117 m_signals.erase (pos);
118}
119
120UnixSignals::Signal *
121UnixSignals::GetSignalByName (const char *name, int32_t &signo)
122{
123 ConstString const_name (name);
124
125 collection::iterator pos, end = m_signals.end ();
126 for (pos = m_signals.begin (); pos != end; pos++)
127 {
Greg Claytondeb391c2010-10-15 02:39:01 +0000128 if ((const_name == pos->second.m_name) || (const_name == pos->second.m_short_name))
Chris Lattner24943d22010-06-08 16:52:24 +0000129 {
Greg Claytondeb391c2010-10-15 02:39:01 +0000130 signo = pos->first;
131 return &pos->second;
Chris Lattner24943d22010-06-08 16:52:24 +0000132 }
133 }
134 return NULL;
135}
136
137
138const UnixSignals::Signal *
139UnixSignals::GetSignalByName (const char *name, int32_t &signo) const
140{
141 ConstString const_name (name);
142
143 collection::const_iterator pos, end = m_signals.end ();
144 for (pos = m_signals.begin (); pos != end; pos++)
145 {
Greg Claytondeb391c2010-10-15 02:39:01 +0000146 if (const_name == pos->second.m_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000147 {
Greg Claytondeb391c2010-10-15 02:39:01 +0000148 signo = pos->first;
149 return &(pos->second);
Chris Lattner24943d22010-06-08 16:52:24 +0000150 }
151 }
152 return NULL;
153}
154
155const char *
156UnixSignals::GetSignalAsCString (int signo) const
157{
158 collection::const_iterator pos = m_signals.find (signo);
159 if (pos == m_signals.end())
160 return NULL;
161 else
Greg Claytondeb391c2010-10-15 02:39:01 +0000162 return pos->second.m_name.GetCString ();
Chris Lattner24943d22010-06-08 16:52:24 +0000163}
164
165
166bool
167UnixSignals::SignalIsValid (int32_t signo) const
168{
169 return m_signals.find (signo) != m_signals.end();
170}
171
172
173int32_t
174UnixSignals::GetSignalNumberFromName (const char *name) const
175{
176 int32_t signo;
177 const Signal *signal = GetSignalByName (name, signo);
178 if (signal == NULL)
179 return LLDB_INVALID_SIGNAL_NUMBER;
180 else
181 return signo;
182}
183
184int32_t
185UnixSignals::GetFirstSignalNumber () const
186{
187 if (m_signals.empty())
188 return LLDB_INVALID_SIGNAL_NUMBER;
189
190 return (*m_signals.begin ()).first;
191}
192
193int32_t
194UnixSignals::GetNextSignalNumber (int32_t current_signal) const
195{
196 collection::const_iterator pos = m_signals.find (current_signal);
197 collection::const_iterator end = m_signals.end();
198 if (pos == end)
199 return LLDB_INVALID_SIGNAL_NUMBER;
200 else
201 {
202 pos++;
203 if (pos == end)
204 return LLDB_INVALID_SIGNAL_NUMBER;
205 else
Greg Claytondeb391c2010-10-15 02:39:01 +0000206 return pos->first;
Chris Lattner24943d22010-06-08 16:52:24 +0000207 }
208}
209
210const char *
211UnixSignals::GetSignalInfo
212(
213 int32_t signo,
214 bool &should_suppress,
215 bool &should_stop,
216 bool &should_notify
217) const
218{
219 collection::const_iterator pos = m_signals.find (signo);
220 if (pos == m_signals.end())
221 return NULL;
222 else
223 {
Greg Claytondeb391c2010-10-15 02:39:01 +0000224 const Signal &signal = pos->second;
225 should_suppress = signal.m_suppress;
226 should_stop = signal.m_stop;
227 should_notify = signal.m_notify;
Chris Lattner24943d22010-06-08 16:52:24 +0000228 return signal.m_name.AsCString("");
229 }
230}
231
232bool
Chris Lattner24943d22010-06-08 16:52:24 +0000233UnixSignals::GetShouldSuppress (int signo) const
234{
Greg Claytondeb391c2010-10-15 02:39:01 +0000235 collection::const_iterator pos = m_signals.find (signo);
236 if (pos != m_signals.end())
237 return pos->second.m_suppress;
238 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000239}
240
241bool
242UnixSignals::SetShouldSuppress (int signo, bool value)
243{
Greg Claytondeb391c2010-10-15 02:39:01 +0000244 collection::iterator pos = m_signals.find (signo);
245 if (pos != m_signals.end())
246 {
247 pos->second.m_suppress = value;
248 return true;
249 }
250 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000251}
252
253bool
254UnixSignals::SetShouldSuppress (const char *signal_name, bool value)
255{
Greg Claytondeb391c2010-10-15 02:39:01 +0000256 return SetShouldSuppress (GetSignalNumberFromName (signal_name), value);
Chris Lattner24943d22010-06-08 16:52:24 +0000257}
258
259bool
260UnixSignals::GetShouldStop (int signo) const
261{
Greg Claytondeb391c2010-10-15 02:39:01 +0000262 collection::const_iterator pos = m_signals.find (signo);
263 if (pos != m_signals.end())
264 return pos->second.m_stop;
265 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000266}
267
268bool
269UnixSignals::SetShouldStop (int signo, bool value)
270{
Greg Claytondeb391c2010-10-15 02:39:01 +0000271 collection::iterator pos = m_signals.find (signo);
272 if (pos != m_signals.end())
273 {
274 pos->second.m_stop = value;
275 return true;
276 }
277 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000278}
279
280bool
281UnixSignals::SetShouldStop (const char *signal_name, bool value)
282{
Greg Claytondeb391c2010-10-15 02:39:01 +0000283 return SetShouldStop (GetSignalNumberFromName (signal_name), value);
Chris Lattner24943d22010-06-08 16:52:24 +0000284}
285
286bool
287UnixSignals::GetShouldNotify (int signo) const
288{
Greg Claytondeb391c2010-10-15 02:39:01 +0000289 collection::const_iterator pos = m_signals.find (signo);
290 if (pos != m_signals.end())
291 return pos->second.m_notify;
292 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000293}
294
295bool
296UnixSignals::SetShouldNotify (int signo, bool value)
297{
Greg Claytondeb391c2010-10-15 02:39:01 +0000298 collection::iterator pos = m_signals.find (signo);
299 if (pos != m_signals.end())
300 {
301 pos->second.m_notify = value;
302 return true;
303 }
304 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000305}
306
307bool
308UnixSignals::SetShouldNotify (const char *signal_name, bool value)
309{
Greg Claytondeb391c2010-10-15 02:39:01 +0000310 return SetShouldNotify (GetSignalNumberFromName (signal_name), value);
Chris Lattner24943d22010-06-08 16:52:24 +0000311}
Greg Claytondeb391c2010-10-15 02:39:01 +0000312