1 //===-- SBWatchpoint.cpp --------------------------------------------------===//
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
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"
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
;
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());
56 watch_id
= watchpoint_sp
->GetID();
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);
87 lldb::WatchpointSP
watchpoint_sp(GetSP());
89 sb_error
.SetError(watchpoint_sp
->GetError());
94 int32_t SBWatchpoint::GetHardwareIndex() {
95 LLDB_INSTRUMENT_VA(this);
97 // For processes using gdb remote protocol,
98 // we cannot determine the hardware breakpoint
99 // index reliably; providing possibly correct
100 // guesses is not useful to anyone.
104 addr_t
SBWatchpoint::GetWatchAddress() {
105 LLDB_INSTRUMENT_VA(this);
107 addr_t ret_addr
= LLDB_INVALID_ADDRESS
;
109 lldb::WatchpointSP
watchpoint_sp(GetSP());
111 std::lock_guard
<std::recursive_mutex
> guard(
112 watchpoint_sp
->GetTarget().GetAPIMutex());
113 ret_addr
= watchpoint_sp
->GetLoadAddress();
119 size_t SBWatchpoint::GetWatchSize() {
120 LLDB_INSTRUMENT_VA(this);
122 size_t watch_size
= 0;
124 lldb::WatchpointSP
watchpoint_sp(GetSP());
126 std::lock_guard
<std::recursive_mutex
> guard(
127 watchpoint_sp
->GetTarget().GetAPIMutex());
128 watch_size
= watchpoint_sp
->GetByteSize();
134 void SBWatchpoint::SetEnabled(bool enabled
) {
135 LLDB_INSTRUMENT_VA(this, enabled
);
137 lldb::WatchpointSP
watchpoint_sp(GetSP());
139 Target
&target
= watchpoint_sp
->GetTarget();
140 std::lock_guard
<std::recursive_mutex
> guard(target
.GetAPIMutex());
141 ProcessSP process_sp
= target
.GetProcessSP();
142 const bool notify
= true;
145 process_sp
->EnableWatchpoint(watchpoint_sp
, notify
);
147 process_sp
->DisableWatchpoint(watchpoint_sp
, notify
);
149 watchpoint_sp
->SetEnabled(enabled
, notify
);
154 bool SBWatchpoint::IsEnabled() {
155 LLDB_INSTRUMENT_VA(this);
157 lldb::WatchpointSP
watchpoint_sp(GetSP());
159 std::lock_guard
<std::recursive_mutex
> guard(
160 watchpoint_sp
->GetTarget().GetAPIMutex());
161 return watchpoint_sp
->IsEnabled();
166 uint32_t SBWatchpoint::GetHitCount() {
167 LLDB_INSTRUMENT_VA(this);
170 lldb::WatchpointSP
watchpoint_sp(GetSP());
172 std::lock_guard
<std::recursive_mutex
> guard(
173 watchpoint_sp
->GetTarget().GetAPIMutex());
174 count
= watchpoint_sp
->GetHitCount();
180 uint32_t SBWatchpoint::GetIgnoreCount() {
181 LLDB_INSTRUMENT_VA(this);
183 lldb::WatchpointSP
watchpoint_sp(GetSP());
185 std::lock_guard
<std::recursive_mutex
> guard(
186 watchpoint_sp
->GetTarget().GetAPIMutex());
187 return watchpoint_sp
->GetIgnoreCount();
192 void SBWatchpoint::SetIgnoreCount(uint32_t n
) {
193 LLDB_INSTRUMENT_VA(this, n
);
195 lldb::WatchpointSP
watchpoint_sp(GetSP());
197 std::lock_guard
<std::recursive_mutex
> guard(
198 watchpoint_sp
->GetTarget().GetAPIMutex());
199 watchpoint_sp
->SetIgnoreCount(n
);
203 const char *SBWatchpoint::GetCondition() {
204 LLDB_INSTRUMENT_VA(this);
206 lldb::WatchpointSP
watchpoint_sp(GetSP());
210 std::lock_guard
<std::recursive_mutex
> guard(
211 watchpoint_sp
->GetTarget().GetAPIMutex());
212 return ConstString(watchpoint_sp
->GetConditionText()).GetCString();
215 void SBWatchpoint::SetCondition(const char *condition
) {
216 LLDB_INSTRUMENT_VA(this, condition
);
218 lldb::WatchpointSP
watchpoint_sp(GetSP());
220 std::lock_guard
<std::recursive_mutex
> guard(
221 watchpoint_sp
->GetTarget().GetAPIMutex());
222 watchpoint_sp
->SetCondition(condition
);
226 bool SBWatchpoint::GetDescription(SBStream
&description
,
227 DescriptionLevel level
) {
228 LLDB_INSTRUMENT_VA(this, description
, level
);
230 Stream
&strm
= description
.ref();
232 lldb::WatchpointSP
watchpoint_sp(GetSP());
234 std::lock_guard
<std::recursive_mutex
> guard(
235 watchpoint_sp
->GetTarget().GetAPIMutex());
236 watchpoint_sp
->GetDescription(&strm
, level
);
239 strm
.PutCString("No value");
244 void SBWatchpoint::Clear() {
245 LLDB_INSTRUMENT_VA(this);
250 lldb::WatchpointSP
SBWatchpoint::GetSP() const {
251 LLDB_INSTRUMENT_VA(this);
253 return m_opaque_wp
.lock();
256 void SBWatchpoint::SetSP(const lldb::WatchpointSP
&sp
) {
257 LLDB_INSTRUMENT_VA(this, sp
);
262 bool SBWatchpoint::EventIsWatchpointEvent(const lldb::SBEvent
&event
) {
263 LLDB_INSTRUMENT_VA(event
);
265 return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event
.get()) !=
270 SBWatchpoint::GetWatchpointEventTypeFromEvent(const SBEvent
&event
) {
271 LLDB_INSTRUMENT_VA(event
);
274 return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
276 return eWatchpointEventTypeInvalidType
;
279 SBWatchpoint
SBWatchpoint::GetWatchpointFromEvent(const lldb::SBEvent
&event
) {
280 LLDB_INSTRUMENT_VA(event
);
282 SBWatchpoint sb_watchpoint
;
285 Watchpoint::WatchpointEventData::GetWatchpointFromEvent(event
.GetSP());
286 return sb_watchpoint
;
289 lldb::SBType
SBWatchpoint::GetType() {
290 LLDB_INSTRUMENT_VA(this);
292 lldb::WatchpointSP
watchpoint_sp(GetSP());
294 std::lock_guard
<std::recursive_mutex
> guard(
295 watchpoint_sp
->GetTarget().GetAPIMutex());
296 const CompilerType
&type
= watchpoint_sp
->GetCompilerType();
297 return lldb::SBType(type
);
299 return lldb::SBType();
302 WatchpointValueKind
SBWatchpoint::GetWatchValueKind() {
303 LLDB_INSTRUMENT_VA(this);
305 lldb::WatchpointSP
watchpoint_sp(GetSP());
307 std::lock_guard
<std::recursive_mutex
> guard(
308 watchpoint_sp
->GetTarget().GetAPIMutex());
309 if (watchpoint_sp
->IsWatchVariable())
310 return WatchpointValueKind::eWatchPointValueKindVariable
;
311 return WatchpointValueKind::eWatchPointValueKindExpression
;
313 return WatchpointValueKind::eWatchPointValueKindInvalid
;
316 const char *SBWatchpoint::GetWatchSpec() {
317 LLDB_INSTRUMENT_VA(this);
319 lldb::WatchpointSP
watchpoint_sp(GetSP());
323 std::lock_guard
<std::recursive_mutex
> guard(
324 watchpoint_sp
->GetTarget().GetAPIMutex());
325 // Store the result of `GetWatchSpec()` as a ConstString
326 // so that the C string we return has a sufficiently long
327 // lifetime. Note this a memory leak but should be fairly
329 return ConstString(watchpoint_sp
->GetWatchSpec()).AsCString();
332 bool SBWatchpoint::IsWatchingReads() {
333 LLDB_INSTRUMENT_VA(this);
334 lldb::WatchpointSP
watchpoint_sp(GetSP());
336 std::lock_guard
<std::recursive_mutex
> guard(
337 watchpoint_sp
->GetTarget().GetAPIMutex());
339 return watchpoint_sp
->WatchpointRead();
345 bool SBWatchpoint::IsWatchingWrites() {
346 LLDB_INSTRUMENT_VA(this);
347 lldb::WatchpointSP
watchpoint_sp(GetSP());
349 std::lock_guard
<std::recursive_mutex
> guard(
350 watchpoint_sp
->GetTarget().GetAPIMutex());
352 return watchpoint_sp
->WatchpointWrite() ||
353 watchpoint_sp
->WatchpointModify();