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 
19 #include "lldb/Core/StreamFile.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"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
30 
31 SBWatchpoint::SBWatchpoint(const lldb::WatchpointSP &wp_sp)
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 
48 SBWatchpoint::~SBWatchpoint() = default;
49 
51  LLDB_INSTRUMENT_VA(this);
52 
54  lldb::WatchpointSP watchpoint_sp(GetSP());
55  if (watchpoint_sp)
56  watch_id = watchpoint_sp->GetID();
57 
58  return watch_id;
59 }
60 
61 bool SBWatchpoint::IsValid() const {
62  LLDB_INSTRUMENT_VA(this);
63  return this->operator bool();
64 }
65 SBWatchpoint::operator bool() const {
66  LLDB_INSTRUMENT_VA(this);
67 
68  return bool(m_opaque_wp.lock());
69 }
70 
71 bool SBWatchpoint::operator==(const SBWatchpoint &rhs) const {
72  LLDB_INSTRUMENT_VA(this, rhs);
73 
74  return GetSP() == rhs.GetSP();
75 }
76 
77 bool SBWatchpoint::operator!=(const SBWatchpoint &rhs) const {
78  LLDB_INSTRUMENT_VA(this, rhs);
79 
80  return !(*this == rhs);
81 }
82 
84  LLDB_INSTRUMENT_VA(this);
85 
86  SBError sb_error;
87  lldb::WatchpointSP watchpoint_sp(GetSP());
88  if (watchpoint_sp) {
89  sb_error.SetError(watchpoint_sp->GetError());
90  }
91  return sb_error;
92 }
93 
95  LLDB_INSTRUMENT_VA(this);
96 
97  int32_t hw_index = -1;
98 
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();
104  }
105 
106  return hw_index;
107 }
108 
110  LLDB_INSTRUMENT_VA(this);
111 
112  addr_t ret_addr = LLDB_INVALID_ADDRESS;
113 
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();
119  }
120 
121  return ret_addr;
122 }
123 
125  LLDB_INSTRUMENT_VA(this);
126 
127  size_t watch_size = 0;
128 
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();
134  }
135 
136  return watch_size;
137 }
138 
139 void SBWatchpoint::SetEnabled(bool enabled) {
140  LLDB_INSTRUMENT_VA(this, enabled);
141 
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);
155  }
156  }
157 }
158 
160  LLDB_INSTRUMENT_VA(this);
161 
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;
169 }
170 
172  LLDB_INSTRUMENT_VA(this);
173 
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();
180  }
181 
182  return count;
183 }
184 
186  LLDB_INSTRUMENT_VA(this);
187 
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;
195 }
196 
198  LLDB_INSTRUMENT_VA(this, n);
199 
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);
205  }
206 }
207 
209  LLDB_INSTRUMENT_VA(this);
210 
211  lldb::WatchpointSP watchpoint_sp(GetSP());
212  if (watchpoint_sp) {
213  std::lock_guard<std::recursive_mutex> guard(
214  watchpoint_sp->GetTarget().GetAPIMutex());
215  return watchpoint_sp->GetConditionText();
216  }
217  return nullptr;
218 }
219 
220 void SBWatchpoint::SetCondition(const char *condition) {
221  LLDB_INSTRUMENT_VA(this, condition);
222 
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);
228  }
229 }
230 
232  DescriptionLevel level) {
233  LLDB_INSTRUMENT_VA(this, description, level);
234 
235  Stream &strm = description.ref();
236 
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");
245 
246  return true;
247 }
248 
250  LLDB_INSTRUMENT_VA(this);
251 
252  m_opaque_wp.reset();
253 }
254 
255 lldb::WatchpointSP SBWatchpoint::GetSP() const {
256  LLDB_INSTRUMENT_VA(this);
257 
258  return m_opaque_wp.lock();
259 }
260 
261 void SBWatchpoint::SetSP(const lldb::WatchpointSP &sp) {
262  LLDB_INSTRUMENT_VA(this, sp);
263 
264  m_opaque_wp = sp;
265 }
266 
268  LLDB_INSTRUMENT_VA(event);
269 
270  return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) !=
271  nullptr;
272 }
273 
274 WatchpointEventType
276  LLDB_INSTRUMENT_VA(event);
277 
278  if (event.IsValid())
279  return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
280  event.GetSP());
281  return eWatchpointEventTypeInvalidType;
282 }
283 
285  LLDB_INSTRUMENT_VA(event);
286 
287  SBWatchpoint sb_watchpoint;
288  if (event.IsValid())
289  sb_watchpoint =
290  Watchpoint::WatchpointEventData::GetWatchpointFromEvent(event.GetSP());
291  return sb_watchpoint;
292 }
LLDB_INVALID_WATCH_ID
#define LLDB_INVALID_WATCH_ID
Definition: lldb-defines.h:43
Instrumentation.h
lldb::SBEvent::get
lldb_private::Event * get() const
Definition: SBEvent.cpp:133
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb::SBWatchpoint::GetID
watch_id_t GetID()
Definition: SBWatchpoint.cpp:50
SBDefines.h
lldb::SBWatchpoint::Clear
void Clear()
Definition: SBWatchpoint.cpp:249
lldb::SBWatchpoint::SetSP
void SetSP(const lldb::WatchpointSP &sp)
Definition: SBWatchpoint.cpp:261
lldb::SBError
Definition: SBError.h:20
lldb::SBWatchpoint::GetWatchpointFromEvent
static lldb::SBWatchpoint GetWatchpointFromEvent(const lldb::SBEvent &event)
Definition: SBWatchpoint.cpp:284
lldb-defines.h
lldb::SBWatchpoint::operator==
bool operator==(const SBWatchpoint &rhs) const
Definition: SBWatchpoint.cpp:71
lldb::SBEvent
Definition: SBEvent.h:21
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb::SBWatchpoint::GetWatchSize
size_t GetWatchSize()
Definition: SBWatchpoint.cpp:124
SBWatchpoint.h
lldb_private::Target
Definition: Target.h:469
lldb::SBWatchpoint::SBWatchpoint
SBWatchpoint()
Definition: SBWatchpoint.cpp:29
Process.h
lldb::SBWatchpoint::SetIgnoreCount
void SetIgnoreCount(uint32_t n)
Definition: SBWatchpoint.cpp:197
Target.h
lldb::SBWatchpoint::operator=
const lldb::SBWatchpoint & operator=(const lldb::SBWatchpoint &rhs)
Definition: SBWatchpoint.cpp:41
SBStream.h
Watchpoint.h
lldb::SBWatchpoint::GetHardwareIndex
int32_t GetHardwareIndex()
With -1 representing an invalid hardware index.
Definition: SBWatchpoint.cpp:94
lldb_private::Target::GetAPIMutex
std::recursive_mutex & GetAPIMutex()
Definition: Target.cpp:4804
lldb::SBEvent::GetSP
lldb::EventSP & GetSP() const
Definition: SBEvent.cpp:131
lldb::SBWatchpoint::operator!=
bool operator!=(const SBWatchpoint &rhs) const
Definition: SBWatchpoint.cpp:77
lldb::SBWatchpoint::GetDescription
bool GetDescription(lldb::SBStream &description, DescriptionLevel level)
Definition: SBWatchpoint.cpp:231
lldb::SBWatchpoint::EventIsWatchpointEvent
static bool EventIsWatchpointEvent(const lldb::SBEvent &event)
Definition: SBWatchpoint.cpp:267
lldb::SBWatchpoint::m_opaque_wp
std::weak_ptr< lldb_private::Watchpoint > m_opaque_wp
Definition: SBWatchpoint.h:80
SBDebugger.h
sp
@ sp
Definition: CompactUnwindInfo.cpp:1250
lldb::watch_id_t
int32_t watch_id_t
Definition: lldb-types.h:89
lldb::SBError::SetError
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:100
SBAddress.h
lldb::SBWatchpoint::SetEnabled
void SetEnabled(bool enabled)
Definition: SBWatchpoint.cpp:139
lldb-types.h
lldb::SBWatchpoint
Definition: SBWatchpoint.h:16
StreamFile.h
lldb_private::Target::GetProcessSP
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:219
uint32_t
lldb::SBWatchpoint::SetCondition
void SetCondition(const char *condition)
Definition: SBWatchpoint.cpp:220
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb::SBWatchpoint::GetHitCount
uint32_t GetHitCount()
Definition: SBWatchpoint.cpp:171
lldb::SBWatchpoint::GetWatchpointEventTypeFromEvent
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::SBEvent &event)
Definition: SBWatchpoint.cpp:275
lldb::SBWatchpoint::GetSP
lldb::WatchpointSP GetSP() const
Definition: SBWatchpoint.cpp:255
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
SBEvent.h
lldb::SBWatchpoint::~SBWatchpoint
~SBWatchpoint()
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBEvent::IsValid
bool IsValid() const
Definition: SBEvent.cpp:154
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBWatchpoint::IsValid
bool IsValid() const
Definition: SBWatchpoint.cpp:61
lldb::SBStream
Definition: SBStream.h:18
lldb::SBWatchpoint::IsEnabled
bool IsEnabled()
Definition: SBWatchpoint.cpp:159
lldb::SBWatchpoint::GetWatchAddress
lldb::addr_t GetWatchAddress()
Definition: SBWatchpoint.cpp:109
WatchpointList.h
lldb
Definition: SBAddress.h:15
lldb::SBWatchpoint::GetCondition
const char * GetCondition()
Definition: SBWatchpoint.cpp:208
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:208
lldb::SBWatchpoint::GetIgnoreCount
uint32_t GetIgnoreCount()
Definition: SBWatchpoint.cpp:185
lldb::SBWatchpoint::GetError
SBError GetError()
Definition: SBWatchpoint.cpp:83