1 //===-- SBUnixSignals.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 "SBReproducerPrivate.h"
10 #include "lldb/Target/Platform.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/UnixSignals.h"
13 #include "lldb/lldb-defines.h"
14 
15 #include "lldb/API/SBUnixSignals.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
SBTypeSynthetic()19 
20 SBUnixSignals::SBUnixSignals() {
21   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBUnixSignals);
22 }
CreateWithClassName(const char * data,uint32_t options)23 
24 SBUnixSignals::SBUnixSignals(const SBUnixSignals &rhs)
25     : m_opaque_wp(rhs.m_opaque_wp) {
26   LLDB_RECORD_CONSTRUCTOR(SBUnixSignals, (const lldb::SBUnixSignals &), rhs);
27 }
28 
29 SBUnixSignals::SBUnixSignals(ProcessSP &process_sp)
30     : m_opaque_wp(process_sp ? process_sp->GetUnixSignals() : nullptr) {}
31 
32 SBUnixSignals::SBUnixSignals(PlatformSP &platform_sp)
33     : m_opaque_wp(platform_sp ? platform_sp->GetUnixSignals() : nullptr) {}
34 
CreateWithScriptCode(const char * data,uint32_t options)35 const SBUnixSignals &SBUnixSignals::operator=(const SBUnixSignals &rhs) {
36   LLDB_RECORD_METHOD(const lldb::SBUnixSignals &,
37                      SBUnixSignals, operator=,(const lldb::SBUnixSignals &),
38                      rhs);
39 
40   if (this != &rhs)
41     m_opaque_wp = rhs.m_opaque_wp;
42   return LLDB_RECORD_RESULT(*this);
43 }
44 
45 SBUnixSignals::~SBUnixSignals() = default;
46 
SBTypeSynthetic(const lldb::SBTypeSynthetic & rhs)47 UnixSignalsSP SBUnixSignals::GetSP() const { return m_opaque_wp.lock(); }
48 
49 void SBUnixSignals::SetSP(const UnixSignalsSP &signals_sp) {
50   m_opaque_wp = signals_sp;
51 }
52 
53 void SBUnixSignals::Clear() {
54   LLDB_RECORD_METHOD_NO_ARGS(void, SBUnixSignals, Clear);
IsValid() const55 
56   m_opaque_wp.reset();
57 }
58 
operator bool() const59 bool SBUnixSignals::IsValid() const {
60   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBUnixSignals, IsValid);
61   return this->operator bool();
62 }
63 SBUnixSignals::operator bool() const {
64   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBUnixSignals, operator bool);
IsClassCode()65 
66   return static_cast<bool>(GetSP());
67 }
68 
69 const char *SBUnixSignals::GetSignalAsCString(int32_t signo) const {
70   LLDB_RECORD_METHOD_CONST(const char *, SBUnixSignals, GetSignalAsCString,
71                            (int32_t), signo);
72 
73   if (auto signals_sp = GetSP())
IsClassName()74     return signals_sp->GetSignalAsCString(signo);
75 
76   return nullptr;
77 }
78 
79 int32_t SBUnixSignals::GetSignalNumberFromName(const char *name) const {
80   LLDB_RECORD_METHOD_CONST(int32_t, SBUnixSignals, GetSignalNumberFromName,
81                            (const char *), name);
GetData()82 
83   if (auto signals_sp = GetSP())
84     return signals_sp->GetSignalNumberFromName(name);
85 
86   return LLDB_INVALID_SIGNAL_NUMBER;
87 }
88 
89 bool SBUnixSignals::GetShouldSuppress(int32_t signo) const {
90   LLDB_RECORD_METHOD_CONST(bool, SBUnixSignals, GetShouldSuppress, (int32_t),
91                            signo);
92 
SetClassName(const char * data)93   if (auto signals_sp = GetSP())
94     return signals_sp->GetShouldSuppress(signo);
95 
96   return false;
97 }
98 
99 bool SBUnixSignals::SetShouldSuppress(int32_t signo, bool value) {
SetClassCode(const char * data)100   LLDB_RECORD_METHOD(bool, SBUnixSignals, SetShouldSuppress, (int32_t, bool),
101                      signo, value);
102 
103   auto signals_sp = GetSP();
104 
105   if (signals_sp)
106     return signals_sp->SetShouldSuppress(signo, value);
GetOptions()107 
108   return false;
109 }
110 
111 bool SBUnixSignals::GetShouldStop(int32_t signo) const {
112   LLDB_RECORD_METHOD_CONST(bool, SBUnixSignals, GetShouldStop, (int32_t),
113                            signo);
114 
SetOptions(uint32_t value)115   if (auto signals_sp = GetSP())
116     return signals_sp->GetShouldStop(signo);
117 
118   return false;
119 }
120 
121 bool SBUnixSignals::SetShouldStop(int32_t signo, bool value) {
122   LLDB_RECORD_METHOD(bool, SBUnixSignals, SetShouldStop, (int32_t, bool), signo,
GetDescription(lldb::SBStream & description,lldb::DescriptionLevel description_level)123                      value);
124 
125   auto signals_sp = GetSP();
126 
127   if (signals_sp)
128     return signals_sp->SetShouldStop(signo, value);
129 
130   return false;
131 }
132 
133 bool SBUnixSignals::GetShouldNotify(int32_t signo) const {
134   LLDB_RECORD_METHOD_CONST(bool, SBUnixSignals, GetShouldNotify, (int32_t),
135                            signo);
136 
operator =(const lldb::SBTypeSynthetic & rhs)137   if (auto signals_sp = GetSP())
138     return signals_sp->GetShouldNotify(signo);
139 
140   return false;
141 }
142 
143 bool SBUnixSignals::SetShouldNotify(int32_t signo, bool value) {
144   LLDB_RECORD_METHOD(bool, SBUnixSignals, SetShouldNotify, (int32_t, bool),
145                      signo, value);
146 
147   auto signals_sp = GetSP();
operator ==(lldb::SBTypeSynthetic & rhs)148 
149   if (signals_sp)
150     return signals_sp->SetShouldNotify(signo, value);
151 
152   return false;
153 }
154 
155 int32_t SBUnixSignals::GetNumSignals() const {
156   LLDB_RECORD_METHOD_CONST_NO_ARGS(int32_t, SBUnixSignals, GetNumSignals);
IsEqualTo(lldb::SBTypeSynthetic & rhs)157 
158   if (auto signals_sp = GetSP())
159     return signals_sp->GetNumSignals();
160 
161   return -1;
162 }
163 
164 int32_t SBUnixSignals::GetSignalAtIndex(int32_t index) const {
165   LLDB_RECORD_METHOD_CONST(int32_t, SBUnixSignals, GetSignalAtIndex, (int32_t),
166                            index);
167 
168   if (auto signals_sp = GetSP())
169     return signals_sp->GetSignalAtIndex(index);
170 
171   return LLDB_INVALID_SIGNAL_NUMBER;
172 }
173 
174 namespace lldb_private {
175 namespace repro {
operator !=(lldb::SBTypeSynthetic & rhs)176 
177 template <>
178 void RegisterMethods<SBUnixSignals>(Registry &R) {
179   LLDB_REGISTER_CONSTRUCTOR(SBUnixSignals, ());
180   LLDB_REGISTER_CONSTRUCTOR(SBUnixSignals, (const lldb::SBUnixSignals &));
181   LLDB_REGISTER_METHOD(
182       const lldb::SBUnixSignals &,
183       SBUnixSignals, operator=,(const lldb::SBUnixSignals &));
184   LLDB_REGISTER_METHOD(void, SBUnixSignals, Clear, ());
185   LLDB_REGISTER_METHOD_CONST(bool, SBUnixSignals, IsValid, ());
186   LLDB_REGISTER_METHOD_CONST(bool, SBUnixSignals, operator bool, ());
187   LLDB_REGISTER_METHOD_CONST(const char *, SBUnixSignals, GetSignalAsCString,
188                              (int32_t));
189   LLDB_REGISTER_METHOD_CONST(int32_t, SBUnixSignals, GetSignalNumberFromName,
190                              (const char *));
191   LLDB_REGISTER_METHOD_CONST(bool, SBUnixSignals, GetShouldSuppress,
192                              (int32_t));
193   LLDB_REGISTER_METHOD(bool, SBUnixSignals, SetShouldSuppress,
194                        (int32_t, bool));
195   LLDB_REGISTER_METHOD_CONST(bool, SBUnixSignals, GetShouldStop, (int32_t));
196   LLDB_REGISTER_METHOD(bool, SBUnixSignals, SetShouldStop, (int32_t, bool));
197   LLDB_REGISTER_METHOD_CONST(bool, SBUnixSignals, GetShouldNotify, (int32_t));
198   LLDB_REGISTER_METHOD(bool, SBUnixSignals, SetShouldNotify, (int32_t, bool));
199   LLDB_REGISTER_METHOD_CONST(int32_t, SBUnixSignals, GetNumSignals, ());
200   LLDB_REGISTER_METHOD_CONST(int32_t, SBUnixSignals, GetSignalAtIndex,
201                              (int32_t));
202 }
203 
204 }
205 }
206