1 //===-- SBUnixSignals.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/lldb-defines.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/UnixSignals.h"
13 #include "lldb/Core/Log.h"
14 
15 #include "lldb/API/SBUnixSignals.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 SBUnixSignals::SBUnixSignals ()
21 {}
22 
23 SBUnixSignals::SBUnixSignals (const SBUnixSignals &rhs) :
24     m_opaque_wp(rhs.m_opaque_wp)
25 {
26 }
27 
28 SBUnixSignals::SBUnixSignals (ProcessSP &process_sp) :
29     m_opaque_wp(process_sp)
30 {
31 }
32 
33 const SBUnixSignals&
34 SBUnixSignals::operator = (const SBUnixSignals& rhs)
35 {
36     if (this != &rhs)
37         m_opaque_wp = rhs.m_opaque_wp;
38     return *this;
39 }
40 
41 SBUnixSignals::~SBUnixSignals()
42 {
43 }
44 
45 ProcessSP
46 SBUnixSignals::GetSP() const
47 {
48     return m_opaque_wp.lock();
49 }
50 
51 void
52 SBUnixSignals::SetSP (const ProcessSP &process_sp)
53 {
54     m_opaque_wp = process_sp;
55 }
56 
57 void
58 SBUnixSignals::Clear ()
59 {
60     m_opaque_wp.reset();
61 }
62 
63 bool
64 SBUnixSignals::IsValid() const
65 {
66     return (bool) GetSP();
67 }
68 
69 const char *
70 SBUnixSignals::GetSignalAsCString (int32_t signo) const
71 {
72     ProcessSP process_sp(GetSP());
73     if (process_sp) return process_sp->GetUnixSignals().GetSignalAsCString(signo);
74     return NULL;
75 }
76 
77 int32_t
78 SBUnixSignals::GetSignalNumberFromName (const char *name) const
79 {
80     ProcessSP process_sp(GetSP());
81     if (process_sp) return process_sp->GetUnixSignals().GetSignalNumberFromName(name);
82     return -1;
83 }
84 
85 bool
86 SBUnixSignals::GetShouldSuppress (int32_t signo) const
87 {
88     ProcessSP process_sp(GetSP());
89     if (process_sp) return process_sp->GetUnixSignals().GetShouldSuppress(signo);
90     return false;
91 }
92 
93 bool
94 SBUnixSignals::SetShouldSuppress (int32_t signo, bool value)
95 {
96     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
97     ProcessSP process_sp(GetSP());
98 
99     if (log)
100     {
101         log->Printf ("SBUnixSignals(%p)::SetShouldSuppress (signo=%d, value=%d)",
102                      static_cast<void*>(process_sp.get()),
103                      signo,
104                      value);
105     }
106 
107     if (process_sp) return process_sp->GetUnixSignals().SetShouldSuppress(signo, value);
108     return false;
109 }
110 
111 bool
112 SBUnixSignals::GetShouldStop (int32_t signo) const
113 {
114     ProcessSP process_sp(GetSP());
115     if (process_sp) return process_sp->GetUnixSignals().GetShouldStop(signo);
116     return false;
117 }
118 
119 bool
120 SBUnixSignals::SetShouldStop (int32_t signo, bool value)
121 {
122     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
123     ProcessSP process_sp(GetSP());
124 
125     if (log)
126     {
127         log->Printf ("SBUnixSignals(%p)::SetShouldStop (signo=%d, value=%d)",
128                      static_cast<void*>(process_sp.get()),
129                      signo,
130                      value);
131     }
132 
133     if (process_sp) return process_sp->GetUnixSignals().SetShouldStop(signo, value);
134     return false;
135 }
136 
137 bool
138 SBUnixSignals::GetShouldNotify (int32_t signo) const
139 {
140     ProcessSP process_sp(GetSP());
141     if (process_sp) return process_sp->GetUnixSignals().GetShouldNotify(signo);
142     return false;
143 }
144 
145 bool
146 SBUnixSignals::SetShouldNotify (int32_t signo, bool value)
147 {
148     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
149     ProcessSP process_sp(GetSP());
150 
151     if (log)
152     {
153         log->Printf ("SBUnixSignals(%p)::SetShouldNotify (signo=%d, value=%d)",
154                      static_cast<void*>(process_sp.get()),
155                      signo,
156                      value);
157     }
158 
159     if (process_sp) return process_sp->GetUnixSignals().SetShouldNotify(signo, value);
160     return false;
161 }
162 
163 int32_t
164 SBUnixSignals::GetNumSignals () const
165 {
166     if (auto process_sp = GetSP())
167     {
168         // only valid while we hold process_sp
169         UnixSignals *unix_signals_ptr = &process_sp->GetUnixSignals();
170         int32_t num_signals = 0;
171         for (int32_t signo = unix_signals_ptr->GetFirstSignalNumber();
172              signo != LLDB_INVALID_SIGNAL_NUMBER;
173              signo = unix_signals_ptr->GetNextSignalNumber(signo))
174         {
175             num_signals++;
176         }
177         return num_signals;
178     }
179     return LLDB_INVALID_SIGNAL_NUMBER;
180 }
181 
182 int32_t
183 SBUnixSignals::GetSignalAtIndex (int32_t index) const
184 {
185     if (auto process_sp = GetSP())
186     {
187         // only valid while we hold process_sp
188         UnixSignals *unix_signals_ptr = &process_sp->GetUnixSignals();
189         int32_t idx = 0;
190         for (int32_t signo = unix_signals_ptr->GetFirstSignalNumber();
191              signo != LLDB_INVALID_SIGNAL_NUMBER;
192              signo = unix_signals_ptr->GetNextSignalNumber(signo))
193         {
194             if (index == idx) return signo;
195             idx++;
196         }
197     }
198     return LLDB_INVALID_SIGNAL_NUMBER;
199 }
200