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
37 : m_opaque_wp(rhs.m_opaque_wp) {
38 LLDB_INSTRUMENT_VA(this, rhs);
39}
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 std::lock_guard<std::recursive_mutex> guard(
112 watchpoint_sp->GetTarget().GetAPIMutex());
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 std::lock_guard<std::recursive_mutex> guard(
127 watchpoint_sp->GetTarget().GetAPIMutex());
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 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
141 ProcessSP process_sp = target.GetProcessSP();
142 const bool notify = true;
143 if (process_sp) {
144 if (enabled)
145 process_sp->EnableWatchpoint(watchpoint_sp, notify);
146 else
147 process_sp->DisableWatchpoint(watchpoint_sp, notify);
148 } else {
149 watchpoint_sp->SetEnabled(enabled, notify);
150 }
151 }
152}
153
155 LLDB_INSTRUMENT_VA(this);
156
157 lldb::WatchpointSP watchpoint_sp(GetSP());
158 if (watchpoint_sp) {
159 std::lock_guard<std::recursive_mutex> guard(
160 watchpoint_sp->GetTarget().GetAPIMutex());
161 return watchpoint_sp->IsEnabled();
162 } else
163 return false;
164}
165
167 LLDB_INSTRUMENT_VA(this);
168
169 uint32_t count = 0;
170 lldb::WatchpointSP watchpoint_sp(GetSP());
171 if (watchpoint_sp) {
172 std::lock_guard<std::recursive_mutex> guard(
173 watchpoint_sp->GetTarget().GetAPIMutex());
174 count = watchpoint_sp->GetHitCount();
175 }
176
177 return count;
178}
179
181 LLDB_INSTRUMENT_VA(this);
182
183 lldb::WatchpointSP watchpoint_sp(GetSP());
184 if (watchpoint_sp) {
185 std::lock_guard<std::recursive_mutex> guard(
186 watchpoint_sp->GetTarget().GetAPIMutex());
187 return watchpoint_sp->GetIgnoreCount();
188 } else
189 return 0;
190}
191
193 LLDB_INSTRUMENT_VA(this, n);
194
195 lldb::WatchpointSP watchpoint_sp(GetSP());
196 if (watchpoint_sp) {
197 std::lock_guard<std::recursive_mutex> guard(
198 watchpoint_sp->GetTarget().GetAPIMutex());
199 watchpoint_sp->SetIgnoreCount(n);
200 }
201}
202
204 LLDB_INSTRUMENT_VA(this);
205
206 lldb::WatchpointSP watchpoint_sp(GetSP());
207 if (!watchpoint_sp)
208 return nullptr;
209
210 std::lock_guard<std::recursive_mutex> guard(
211 watchpoint_sp->GetTarget().GetAPIMutex());
212 return ConstString(watchpoint_sp->GetConditionText()).GetCString();
213}
214
215void SBWatchpoint::SetCondition(const char *condition) {
216 LLDB_INSTRUMENT_VA(this, condition);
217
218 lldb::WatchpointSP watchpoint_sp(GetSP());
219 if (watchpoint_sp) {
220 std::lock_guard<std::recursive_mutex> guard(
221 watchpoint_sp->GetTarget().GetAPIMutex());
222 watchpoint_sp->SetCondition(condition);
223 }
224}
225
227 DescriptionLevel level) {
228 LLDB_INSTRUMENT_VA(this, description, level);
229
230 Stream &strm = description.ref();
231
232 lldb::WatchpointSP watchpoint_sp(GetSP());
233 if (watchpoint_sp) {
234 std::lock_guard<std::recursive_mutex> guard(
235 watchpoint_sp->GetTarget().GetAPIMutex());
236 watchpoint_sp->GetDescription(&strm, level);
237 strm.EOL();
238 } else
239 strm.PutCString("No value");
240
241 return true;
242}
243
245 LLDB_INSTRUMENT_VA(this);
246
247 m_opaque_wp.reset();
248}
249
251 LLDB_INSTRUMENT_VA(this);
252
253 return m_opaque_wp.lock();
254}
255
257 LLDB_INSTRUMENT_VA(this, sp);
258
259 m_opaque_wp = sp;
260}
261
263 LLDB_INSTRUMENT_VA(event);
264
266 nullptr;
267}
268
269WatchpointEventType
271 LLDB_INSTRUMENT_VA(event);
272
273 if (event.IsValid())
275 event.GetSP());
276 return eWatchpointEventTypeInvalidType;
277}
278
280 LLDB_INSTRUMENT_VA(event);
281
282 SBWatchpoint sb_watchpoint;
283 if (event.IsValid())
284 sb_watchpoint =
286 return sb_watchpoint;
287}
288
290 LLDB_INSTRUMENT_VA(this);
291
292 lldb::WatchpointSP watchpoint_sp(GetSP());
293 if (watchpoint_sp) {
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);
298 }
299 return lldb::SBType();
300}
301
303 LLDB_INSTRUMENT_VA(this);
304
305 lldb::WatchpointSP watchpoint_sp(GetSP());
306 if (watchpoint_sp) {
307 std::lock_guard<std::recursive_mutex> guard(
308 watchpoint_sp->GetTarget().GetAPIMutex());
309 if (watchpoint_sp->IsWatchVariable())
312 }
314}
315
317 LLDB_INSTRUMENT_VA(this);
318
319 lldb::WatchpointSP watchpoint_sp(GetSP());
320 if (!watchpoint_sp)
321 return nullptr;
322
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
328 // low impact.
329 return ConstString(watchpoint_sp->GetWatchSpec()).AsCString();
330}
331
333 LLDB_INSTRUMENT_VA(this);
334 lldb::WatchpointSP watchpoint_sp(GetSP());
335 if (watchpoint_sp) {
336 std::lock_guard<std::recursive_mutex> guard(
337 watchpoint_sp->GetTarget().GetAPIMutex());
338
339 return watchpoint_sp->WatchpointRead();
340 }
341
342 return false;
343}
344
346 LLDB_INSTRUMENT_VA(this);
347 lldb::WatchpointSP watchpoint_sp(GetSP());
348 if (watchpoint_sp) {
349 std::lock_guard<std::recursive_mutex> guard(
350 watchpoint_sp->GetTarget().GetAPIMutex());
351
352 return watchpoint_sp->WatchpointWrite() ||
353 watchpoint_sp->WatchpointModify();
354 }
355
356 return false;
357}
#define LLDB_INSTRUMENT_VA(...)
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:110
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:177
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
Definition: SBWatchpoint.h:104
bool operator==(const SBWatchpoint &rhs) const
uint32_t GetHitCount()
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)
watch_id_t GetID()
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.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
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:65
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:297
std::recursive_mutex & GetAPIMutex()
Definition: Target.cpp:5136
static lldb::WatchpointSP GetWatchpointFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:541
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:531
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
Definition: Watchpoint.cpp:520
#define LLDB_INVALID_WATCH_ID
Definition: lldb-defines.h:43
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eWatchPointValueKindInvalid
Watchpoint was created watching a variable.
@ eWatchPointValueKindExpression
@ eWatchPointValueKindVariable
Watchpoint was created watching the result of an expression that was evaluated at creation time.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
Definition: lldb-forward.h:489
int32_t watch_id_t
Definition: lldb-types.h:87
uint64_t addr_t
Definition: lldb-types.h:80