LLDB mainline
SBWatchpoint.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
8
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"
16
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"
25
26using namespace lldb;
27using namespace lldb_private;
28
30
32 : m_opaque_wp(wp_sp) {
33 LLDB_INSTRUMENT_VA(this, wp_sp);
34}
35
40
42 LLDB_INSTRUMENT_VA(this, rhs);
43
45 return *this;
46}
47
49
52
54 lldb::WatchpointSP watchpoint_sp(GetSP());
55 if (watchpoint_sp)
56 watch_id = watchpoint_sp->GetID();
57
58 return watch_id;
59}
60
63 return this->operator bool();
64}
65SBWatchpoint::operator bool() const {
67
68 return bool(m_opaque_wp.lock());
69}
70
71bool SBWatchpoint::operator==(const SBWatchpoint &rhs) const {
72 LLDB_INSTRUMENT_VA(this, rhs);
73
74 return GetSP() == rhs.GetSP();
75}
76
77bool SBWatchpoint::operator!=(const SBWatchpoint &rhs) const {
78 LLDB_INSTRUMENT_VA(this, rhs);
79
80 return !(*this == rhs);
81}
82
85
86 SBError sb_error;
87 lldb::WatchpointSP watchpoint_sp(GetSP());
88 if (watchpoint_sp) {
89 sb_error.SetError(watchpoint_sp->GetError().Clone());
90 }
91 return sb_error;
92}
93
96
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.
101 return -1;
102}
103
105 LLDB_INSTRUMENT_VA(this);
106
107 addr_t ret_addr = LLDB_INVALID_ADDRESS;
108
109 lldb::WatchpointSP watchpoint_sp(GetSP());
110 if (watchpoint_sp) {
111 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
112 std::lock_guard<TargetAPIMutex> guard(api_lock);
113 ret_addr = watchpoint_sp->GetLoadAddress();
114 }
115
116 return ret_addr;
117}
118
120 LLDB_INSTRUMENT_VA(this);
121
122 size_t watch_size = 0;
123
124 lldb::WatchpointSP watchpoint_sp(GetSP());
125 if (watchpoint_sp) {
126 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
127 std::lock_guard<TargetAPIMutex> guard(api_lock);
128 watch_size = watchpoint_sp->GetByteSize();
129 }
130
131 return watch_size;
132}
133
134void SBWatchpoint::SetEnabled(bool enabled) {
135 LLDB_INSTRUMENT_VA(this, enabled);
136
137 lldb::WatchpointSP watchpoint_sp(GetSP());
138 if (watchpoint_sp) {
139 Target &target = watchpoint_sp->GetTarget();
140 TargetAPIMutex api_lock = target.GetAPIMutex();
141 std::lock_guard<TargetAPIMutex> guard(api_lock);
142 ProcessSP process_sp = target.GetProcessSP();
143 const bool notify = true;
144 if (process_sp) {
145 if (enabled)
146 process_sp->EnableWatchpoint(watchpoint_sp, notify);
147 else
148 process_sp->DisableWatchpoint(watchpoint_sp, notify);
149 } else {
150 watchpoint_sp->SetEnabled(enabled, notify);
151 }
152 }
153}
154
156 LLDB_INSTRUMENT_VA(this);
157
158 lldb::WatchpointSP watchpoint_sp(GetSP());
159 if (watchpoint_sp) {
160 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
161 std::lock_guard<TargetAPIMutex> guard(api_lock);
162 return watchpoint_sp->IsEnabled();
163 } else
164 return false;
165}
166
168 LLDB_INSTRUMENT_VA(this);
169
170 uint32_t count = 0;
171 lldb::WatchpointSP watchpoint_sp(GetSP());
172 if (watchpoint_sp) {
173 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
174 std::lock_guard<TargetAPIMutex> guard(api_lock);
175 count = watchpoint_sp->GetHitCount();
176 }
177
178 return count;
179}
180
182 LLDB_INSTRUMENT_VA(this);
183
184 lldb::WatchpointSP watchpoint_sp(GetSP());
185 if (watchpoint_sp) {
186 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
187 std::lock_guard<TargetAPIMutex> guard(api_lock);
188 return watchpoint_sp->GetIgnoreCount();
189 } else
190 return 0;
191}
192
194 LLDB_INSTRUMENT_VA(this, n);
195
196 lldb::WatchpointSP watchpoint_sp(GetSP());
197 if (watchpoint_sp) {
198 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
199 std::lock_guard<TargetAPIMutex> guard(api_lock);
200 watchpoint_sp->SetIgnoreCount(n);
201 }
202}
203
205 LLDB_INSTRUMENT_VA(this);
206
207 lldb::WatchpointSP watchpoint_sp(GetSP());
208 if (!watchpoint_sp)
209 return nullptr;
210
211 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
212 std::lock_guard<TargetAPIMutex> guard(api_lock);
213 return ConstString(watchpoint_sp->GetConditionText()).GetCString();
214}
215
216void SBWatchpoint::SetCondition(const char *condition) {
217 LLDB_INSTRUMENT_VA(this, condition);
218
219 lldb::WatchpointSP watchpoint_sp(GetSP());
220 if (watchpoint_sp) {
221 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
222 std::lock_guard<TargetAPIMutex> guard(api_lock);
223 watchpoint_sp->SetCondition(condition);
224 }
225}
226
228 DescriptionLevel level) {
229 LLDB_INSTRUMENT_VA(this, description, level);
230
231 Stream &strm = description.ref();
232
233 lldb::WatchpointSP watchpoint_sp(GetSP());
234 if (watchpoint_sp) {
235 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
236 std::lock_guard<TargetAPIMutex> guard(api_lock);
237 watchpoint_sp->GetDescription(&strm, level);
238 strm.EOL();
239 } else
240 strm.PutCString("No value");
241
242 return true;
243}
244
246 LLDB_INSTRUMENT_VA(this);
247
248 m_opaque_wp.reset();
249}
250
252 LLDB_INSTRUMENT_VA(this);
253
254 return m_opaque_wp.lock();
255}
256
262
269
270WatchpointEventType
272 LLDB_INSTRUMENT_VA(event);
273
274 if (event.IsValid())
276 event.GetSP());
277 return eWatchpointEventTypeInvalidType;
278}
279
281 LLDB_INSTRUMENT_VA(event);
282
283 SBWatchpoint sb_watchpoint;
284 if (event.IsValid())
285 sb_watchpoint =
287 return sb_watchpoint;
288}
289
291 LLDB_INSTRUMENT_VA(this);
292
293 lldb::WatchpointSP watchpoint_sp(GetSP());
294 if (watchpoint_sp) {
295 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
296 std::lock_guard<TargetAPIMutex> guard(api_lock);
297 const CompilerType &type = watchpoint_sp->GetCompilerType();
298 return lldb::SBType(type);
299 }
300 return lldb::SBType();
301}
302
304 LLDB_INSTRUMENT_VA(this);
305
306 lldb::WatchpointSP watchpoint_sp(GetSP());
307 if (watchpoint_sp) {
308 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
309 std::lock_guard<TargetAPIMutex> guard(api_lock);
310 if (watchpoint_sp->IsWatchVariable())
313 }
315}
316
318 LLDB_INSTRUMENT_VA(this);
319
320 lldb::WatchpointSP watchpoint_sp(GetSP());
321 if (!watchpoint_sp)
322 return nullptr;
323
324 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
325 std::lock_guard<TargetAPIMutex> guard(api_lock);
326 // Store the result of `GetWatchSpec()` as a ConstString
327 // so that the C string we return has a sufficiently long
328 // lifetime. Note this a memory leak but should be fairly
329 // low impact.
330 return ConstString(watchpoint_sp->GetWatchSpec()).AsCString(nullptr);
331}
332
334 LLDB_INSTRUMENT_VA(this);
335 lldb::WatchpointSP watchpoint_sp(GetSP());
336 if (watchpoint_sp) {
337 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
338 std::lock_guard<TargetAPIMutex> guard(api_lock);
339
340 return watchpoint_sp->WatchpointRead();
341 }
342
343 return false;
344}
345
347 LLDB_INSTRUMENT_VA(this);
348 lldb::WatchpointSP watchpoint_sp(GetSP());
349 if (watchpoint_sp) {
350 TargetAPIMutex api_lock = watchpoint_sp->GetTarget().GetAPIMutex();
351 std::lock_guard<TargetAPIMutex> guard(api_lock);
352
353 return watchpoint_sp->WatchpointWrite() ||
354 watchpoint_sp->WatchpointModify();
355 }
356
357 return false;
358}
#define LLDB_INSTRUMENT_VA(...)
void SetError(uint32_t err, lldb::ErrorType type)
Definition SBError.cpp:124
lldb_private::Event * get() const
Definition SBEvent.cpp:134
bool IsValid() const
Definition SBEvent.cpp:155
lldb::EventSP & GetSP() const
Definition SBEvent.cpp:132
lldb_private::Stream & ref()
Definition SBStream.cpp:179
void SetCondition(const char *condition)
const char * GetCondition()
void SetSP(const lldb::WatchpointSP &sp)
const char * GetWatchSpec()
bool GetDescription(lldb::SBStream &description, DescriptionLevel level)
lldb::addr_t GetWatchAddress()
lldb::SBType GetType()
std::weak_ptr< lldb_private::Watchpoint > m_opaque_wp
bool operator==(const SBWatchpoint &rhs) const
uint32_t GetIgnoreCount()
bool IsValid() const
void SetIgnoreCount(uint32_t n)
WatchpointValueKind GetWatchValueKind()
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::SBEvent &event)
int32_t GetHardwareIndex()
void SetEnabled(bool enabled)
static bool EventIsWatchpointEvent(const lldb::SBEvent &event)
static lldb::SBWatchpoint GetWatchpointFromEvent(const lldb::SBEvent &event)
bool operator!=(const SBWatchpoint &rhs) const
lldb::WatchpointSP GetSP() const
const lldb::SBWatchpoint & operator=(const lldb::SBWatchpoint &rhs)
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
TargetAPIMutex GetAPIMutex()
Returns a handle resolved to the mutex to serialize on before touching the target through the SB API.
Definition Target.cpp:6027
const lldb::ProcessSP & GetProcessSP() const
Definition Target.cpp:329
static lldb::WatchpointSP GetWatchpointFromEvent(const lldb::EventSP &event_sp)
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
#define LLDB_INVALID_WATCH_ID
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eWatchPointValueKindInvalid
@ eWatchPointValueKindExpression
Watchpoint was created watching the result of an expression that was evaluated at creation time.
@ eWatchPointValueKindVariable
Watchpoint was created watching a variable.
class LLDB_API SBType
Definition SBDefines.h:122
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
int32_t watch_id_t
Definition lldb-types.h:89
uint64_t addr_t
Definition lldb-types.h:80