Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / API / SBWatchpoint.cpp
blob8b4e0ad3178b1827327f1a165748ec7e94f34237
1 //===-- SBWatchpoint.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 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBWatchpoint.h"
10 #include "lldb/API/SBAddress.h"
11 #include "lldb/API/SBDebugger.h"
12 #include "lldb/API/SBDefines.h"
13 #include "lldb/API/SBEvent.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Utility/Instrumentation.h"
17 #include "lldb/Breakpoint/Watchpoint.h"
18 #include "lldb/Breakpoint/WatchpointList.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Stream.h"
23 #include "lldb/lldb-defines.h"
24 #include "lldb/lldb-types.h"
26 using namespace lldb;
27 using namespace lldb_private;
29 SBWatchpoint::SBWatchpoint() { LLDB_INSTRUMENT_VA(this); }
31 SBWatchpoint::SBWatchpoint(const lldb::WatchpointSP &wp_sp)
32 : m_opaque_wp(wp_sp) {
33 LLDB_INSTRUMENT_VA(this, wp_sp);
36 SBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs)
37 : m_opaque_wp(rhs.m_opaque_wp) {
38 LLDB_INSTRUMENT_VA(this, rhs);
41 const SBWatchpoint &SBWatchpoint::operator=(const SBWatchpoint &rhs) {
42 LLDB_INSTRUMENT_VA(this, rhs);
44 m_opaque_wp = rhs.m_opaque_wp;
45 return *this;
48 SBWatchpoint::~SBWatchpoint() = default;
50 watch_id_t SBWatchpoint::GetID() {
51 LLDB_INSTRUMENT_VA(this);
53 watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
54 lldb::WatchpointSP watchpoint_sp(GetSP());
55 if (watchpoint_sp)
56 watch_id = watchpoint_sp->GetID();
58 return watch_id;
61 bool SBWatchpoint::IsValid() const {
62 LLDB_INSTRUMENT_VA(this);
63 return this->operator bool();
65 SBWatchpoint::operator bool() const {
66 LLDB_INSTRUMENT_VA(this);
68 return bool(m_opaque_wp.lock());
71 bool SBWatchpoint::operator==(const SBWatchpoint &rhs) const {
72 LLDB_INSTRUMENT_VA(this, rhs);
74 return GetSP() == rhs.GetSP();
77 bool SBWatchpoint::operator!=(const SBWatchpoint &rhs) const {
78 LLDB_INSTRUMENT_VA(this, rhs);
80 return !(*this == rhs);
83 SBError SBWatchpoint::GetError() {
84 LLDB_INSTRUMENT_VA(this);
86 SBError sb_error;
87 lldb::WatchpointSP watchpoint_sp(GetSP());
88 if (watchpoint_sp) {
89 sb_error.SetError(watchpoint_sp->GetError());
91 return sb_error;
94 int32_t SBWatchpoint::GetHardwareIndex() {
95 LLDB_INSTRUMENT_VA(this);
97 int32_t hw_index = -1;
99 lldb::WatchpointSP watchpoint_sp(GetSP());
100 if (watchpoint_sp) {
101 std::lock_guard<std::recursive_mutex> guard(
102 watchpoint_sp->GetTarget().GetAPIMutex());
103 hw_index = watchpoint_sp->GetHardwareIndex();
106 return hw_index;
109 addr_t SBWatchpoint::GetWatchAddress() {
110 LLDB_INSTRUMENT_VA(this);
112 addr_t ret_addr = LLDB_INVALID_ADDRESS;
114 lldb::WatchpointSP watchpoint_sp(GetSP());
115 if (watchpoint_sp) {
116 std::lock_guard<std::recursive_mutex> guard(
117 watchpoint_sp->GetTarget().GetAPIMutex());
118 ret_addr = watchpoint_sp->GetLoadAddress();
121 return ret_addr;
124 size_t SBWatchpoint::GetWatchSize() {
125 LLDB_INSTRUMENT_VA(this);
127 size_t watch_size = 0;
129 lldb::WatchpointSP watchpoint_sp(GetSP());
130 if (watchpoint_sp) {
131 std::lock_guard<std::recursive_mutex> guard(
132 watchpoint_sp->GetTarget().GetAPIMutex());
133 watch_size = watchpoint_sp->GetByteSize();
136 return watch_size;
139 void SBWatchpoint::SetEnabled(bool enabled) {
140 LLDB_INSTRUMENT_VA(this, enabled);
142 lldb::WatchpointSP watchpoint_sp(GetSP());
143 if (watchpoint_sp) {
144 Target &target = watchpoint_sp->GetTarget();
145 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
146 ProcessSP process_sp = target.GetProcessSP();
147 const bool notify = true;
148 if (process_sp) {
149 if (enabled)
150 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
151 else
152 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
153 } else {
154 watchpoint_sp->SetEnabled(enabled, notify);
159 bool SBWatchpoint::IsEnabled() {
160 LLDB_INSTRUMENT_VA(this);
162 lldb::WatchpointSP watchpoint_sp(GetSP());
163 if (watchpoint_sp) {
164 std::lock_guard<std::recursive_mutex> guard(
165 watchpoint_sp->GetTarget().GetAPIMutex());
166 return watchpoint_sp->IsEnabled();
167 } else
168 return false;
171 uint32_t SBWatchpoint::GetHitCount() {
172 LLDB_INSTRUMENT_VA(this);
174 uint32_t count = 0;
175 lldb::WatchpointSP watchpoint_sp(GetSP());
176 if (watchpoint_sp) {
177 std::lock_guard<std::recursive_mutex> guard(
178 watchpoint_sp->GetTarget().GetAPIMutex());
179 count = watchpoint_sp->GetHitCount();
182 return count;
185 uint32_t SBWatchpoint::GetIgnoreCount() {
186 LLDB_INSTRUMENT_VA(this);
188 lldb::WatchpointSP watchpoint_sp(GetSP());
189 if (watchpoint_sp) {
190 std::lock_guard<std::recursive_mutex> guard(
191 watchpoint_sp->GetTarget().GetAPIMutex());
192 return watchpoint_sp->GetIgnoreCount();
193 } else
194 return 0;
197 void SBWatchpoint::SetIgnoreCount(uint32_t n) {
198 LLDB_INSTRUMENT_VA(this, n);
200 lldb::WatchpointSP watchpoint_sp(GetSP());
201 if (watchpoint_sp) {
202 std::lock_guard<std::recursive_mutex> guard(
203 watchpoint_sp->GetTarget().GetAPIMutex());
204 watchpoint_sp->SetIgnoreCount(n);
208 const char *SBWatchpoint::GetCondition() {
209 LLDB_INSTRUMENT_VA(this);
211 lldb::WatchpointSP watchpoint_sp(GetSP());
212 if (!watchpoint_sp)
213 return nullptr;
215 std::lock_guard<std::recursive_mutex> guard(
216 watchpoint_sp->GetTarget().GetAPIMutex());
217 return ConstString(watchpoint_sp->GetConditionText()).GetCString();
220 void SBWatchpoint::SetCondition(const char *condition) {
221 LLDB_INSTRUMENT_VA(this, condition);
223 lldb::WatchpointSP watchpoint_sp(GetSP());
224 if (watchpoint_sp) {
225 std::lock_guard<std::recursive_mutex> guard(
226 watchpoint_sp->GetTarget().GetAPIMutex());
227 watchpoint_sp->SetCondition(condition);
231 bool SBWatchpoint::GetDescription(SBStream &description,
232 DescriptionLevel level) {
233 LLDB_INSTRUMENT_VA(this, description, level);
235 Stream &strm = description.ref();
237 lldb::WatchpointSP watchpoint_sp(GetSP());
238 if (watchpoint_sp) {
239 std::lock_guard<std::recursive_mutex> guard(
240 watchpoint_sp->GetTarget().GetAPIMutex());
241 watchpoint_sp->GetDescription(&strm, level);
242 strm.EOL();
243 } else
244 strm.PutCString("No value");
246 return true;
249 void SBWatchpoint::Clear() {
250 LLDB_INSTRUMENT_VA(this);
252 m_opaque_wp.reset();
255 lldb::WatchpointSP SBWatchpoint::GetSP() const {
256 LLDB_INSTRUMENT_VA(this);
258 return m_opaque_wp.lock();
261 void SBWatchpoint::SetSP(const lldb::WatchpointSP &sp) {
262 LLDB_INSTRUMENT_VA(this, sp);
264 m_opaque_wp = sp;
267 bool SBWatchpoint::EventIsWatchpointEvent(const lldb::SBEvent &event) {
268 LLDB_INSTRUMENT_VA(event);
270 return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) !=
271 nullptr;
274 WatchpointEventType
275 SBWatchpoint::GetWatchpointEventTypeFromEvent(const SBEvent &event) {
276 LLDB_INSTRUMENT_VA(event);
278 if (event.IsValid())
279 return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
280 event.GetSP());
281 return eWatchpointEventTypeInvalidType;
284 SBWatchpoint SBWatchpoint::GetWatchpointFromEvent(const lldb::SBEvent &event) {
285 LLDB_INSTRUMENT_VA(event);
287 SBWatchpoint sb_watchpoint;
288 if (event.IsValid())
289 sb_watchpoint =
290 Watchpoint::WatchpointEventData::GetWatchpointFromEvent(event.GetSP());
291 return sb_watchpoint;
294 lldb::SBType SBWatchpoint::GetType() {
295 LLDB_INSTRUMENT_VA(this);
297 lldb::WatchpointSP watchpoint_sp(GetSP());
298 if (watchpoint_sp) {
299 std::lock_guard<std::recursive_mutex> guard(
300 watchpoint_sp->GetTarget().GetAPIMutex());
301 const CompilerType &type = watchpoint_sp->GetCompilerType();
302 return lldb::SBType(type);
304 return lldb::SBType();
307 WatchpointValueKind SBWatchpoint::GetWatchValueKind() {
308 LLDB_INSTRUMENT_VA(this);
310 lldb::WatchpointSP watchpoint_sp(GetSP());
311 if (watchpoint_sp) {
312 std::lock_guard<std::recursive_mutex> guard(
313 watchpoint_sp->GetTarget().GetAPIMutex());
314 if (watchpoint_sp->IsWatchVariable())
315 return WatchpointValueKind::eWatchPointValueKindVariable;
316 return WatchpointValueKind::eWatchPointValueKindExpression;
318 return WatchpointValueKind::eWatchPointValueKindInvalid;
321 const char *SBWatchpoint::GetWatchSpec() {
322 LLDB_INSTRUMENT_VA(this);
324 lldb::WatchpointSP watchpoint_sp(GetSP());
325 if (!watchpoint_sp)
326 return nullptr;
328 std::lock_guard<std::recursive_mutex> guard(
329 watchpoint_sp->GetTarget().GetAPIMutex());
330 // Store the result of `GetWatchSpec()` as a ConstString
331 // so that the C string we return has a sufficiently long
332 // lifetime. Note this a memory leak but should be fairly
333 // low impact.
334 return ConstString(watchpoint_sp->GetWatchSpec()).AsCString();
337 bool SBWatchpoint::IsWatchingReads() {
338 LLDB_INSTRUMENT_VA(this);
339 lldb::WatchpointSP watchpoint_sp(GetSP());
340 if (watchpoint_sp) {
341 std::lock_guard<std::recursive_mutex> guard(
342 watchpoint_sp->GetTarget().GetAPIMutex());
344 return watchpoint_sp->WatchpointRead();
347 return false;
350 bool SBWatchpoint::IsWatchingWrites() {
351 LLDB_INSTRUMENT_VA(this);
352 lldb::WatchpointSP watchpoint_sp(GetSP());
353 if (watchpoint_sp) {
354 std::lock_guard<std::recursive_mutex> guard(
355 watchpoint_sp->GetTarget().GetAPIMutex());
357 return watchpoint_sp->WatchpointWrite() ||
358 watchpoint_sp->WatchpointModify();
361 return false;