blob: 50153c8c23463e533164aadfdb4815ae565ecd03 [file] [log] [blame]
Greg Clayton1b282f92011-10-13 18:08:26 +00001//===-- SBWatchpoint.cpp --------------------------------*- C++ -*-===//
Johnny Chen5d043462011-09-26 22:40:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Greg Clayton1b282f92011-10-13 18:08:26 +000010#include "lldb/API/SBWatchpoint.h"
Johnny Chen5d043462011-09-26 22:40:50 +000011#include "lldb/API/SBAddress.h"
12#include "lldb/API/SBDebugger.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/API/SBDefines.h"
Jim Ingham1b5792e2012-12-18 02:03:49 +000014#include "lldb/API/SBEvent.h"
Johnny Chen5d043462011-09-26 22:40:50 +000015#include "lldb/API/SBStream.h"
16
Johnny Chen01a67862011-10-14 00:42:25 +000017#include "lldb/Breakpoint/Watchpoint.h"
18#include "lldb/Breakpoint/WatchpointList.h"
Johnny Chen5d043462011-09-26 22:40:50 +000019#include "lldb/Core/Log.h"
20#include "lldb/Core/Stream.h"
21#include "lldb/Core/StreamFile.h"
Jim Ingham306b62b2016-11-01 20:37:02 +000022#include "lldb/Target/Process.h"
Johnny Chen5d043462011-09-26 22:40:50 +000023#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/lldb-defines.h"
25#include "lldb/lldb-types.h"
Johnny Chen5d043462011-09-26 22:40:50 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030SBWatchpoint::SBWatchpoint() : m_opaque_sp() {}
Johnny Chen5d043462011-09-26 22:40:50 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032SBWatchpoint::SBWatchpoint(const lldb::WatchpointSP &wp_sp)
33 : m_opaque_sp(wp_sp) {
34 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
35
36 if (log) {
37 SBStream sstr;
38 GetDescription(sstr, lldb::eDescriptionLevelBrief);
39 log->Printf("SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp"
40 "=%p) => this.sp = %p (%s)",
41 static_cast<void *>(wp_sp.get()),
42 static_cast<void *>(m_opaque_sp.get()), sstr.GetData());
43 }
Johnny Chen5d043462011-09-26 22:40:50 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046SBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs)
47 : m_opaque_sp(rhs.m_opaque_sp) {}
Johnny Chen5d043462011-09-26 22:40:50 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049const SBWatchpoint &SBWatchpoint::operator=(const SBWatchpoint &rhs) {
50 if (this != &rhs)
51 m_opaque_sp = rhs.m_opaque_sp;
52 return *this;
Johnny Chen5d043462011-09-26 22:40:50 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055SBWatchpoint::~SBWatchpoint() {}
Johnny Chen5d043462011-09-26 22:40:50 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057watch_id_t SBWatchpoint::GetID() {
58 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
61 lldb::WatchpointSP watchpoint_sp(GetSP());
62 if (watchpoint_sp)
63 watch_id = watchpoint_sp->GetID();
Johnny Chen5d043462011-09-26 22:40:50 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 if (log) {
66 if (watch_id == LLDB_INVALID_WATCH_ID)
67 log->Printf("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID",
68 static_cast<void *>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +000069 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 log->Printf("SBWatchpoint(%p)::GetID () => %u",
71 static_cast<void *>(watchpoint_sp.get()), watch_id);
72 }
73
74 return watch_id;
Johnny Chen5d043462011-09-26 22:40:50 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077bool SBWatchpoint::IsValid() const { return (bool)m_opaque_sp; }
Johnny Chend4dd7992011-09-27 01:19:20 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079SBError SBWatchpoint::GetError() {
80 SBError sb_error;
81 lldb::WatchpointSP watchpoint_sp(GetSP());
82 if (watchpoint_sp) {
83 sb_error.SetError(watchpoint_sp->GetError());
84 }
85 return sb_error;
Johnny Chend4dd7992011-09-27 01:19:20 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088int32_t SBWatchpoint::GetHardwareIndex() {
89 int32_t hw_index = -1;
90
91 lldb::WatchpointSP watchpoint_sp(GetSP());
92 if (watchpoint_sp) {
93 std::lock_guard<std::recursive_mutex> guard(
94 watchpoint_sp->GetTarget().GetAPIMutex());
95 hw_index = watchpoint_sp->GetHardwareIndex();
96 }
97
98 return hw_index;
Johnny Chen5d043462011-09-26 22:40:50 +000099}
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101addr_t SBWatchpoint::GetWatchAddress() {
102 addr_t ret_addr = LLDB_INVALID_ADDRESS;
103
104 lldb::WatchpointSP watchpoint_sp(GetSP());
105 if (watchpoint_sp) {
106 std::lock_guard<std::recursive_mutex> guard(
107 watchpoint_sp->GetTarget().GetAPIMutex());
108 ret_addr = watchpoint_sp->GetLoadAddress();
109 }
110
111 return ret_addr;
Johnny Chen5d043462011-09-26 22:40:50 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114size_t SBWatchpoint::GetWatchSize() {
115 size_t watch_size = 0;
116
117 lldb::WatchpointSP watchpoint_sp(GetSP());
118 if (watchpoint_sp) {
119 std::lock_guard<std::recursive_mutex> guard(
120 watchpoint_sp->GetTarget().GetAPIMutex());
121 watch_size = watchpoint_sp->GetByteSize();
122 }
123
124 return watch_size;
Johnny Chen16dcf712011-10-17 18:58:00 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void SBWatchpoint::SetEnabled(bool enabled) {
128 lldb::WatchpointSP watchpoint_sp(GetSP());
129 if (watchpoint_sp) {
Jim Ingham306b62b2016-11-01 20:37:02 +0000130 Target &target = watchpoint_sp->GetTarget();
131 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
132 ProcessSP process_sp = target.GetProcessSP();
Jim Inghamc76c3f22016-11-02 01:06:42 +0000133 const bool notify = true;
Jim Ingham306b62b2016-11-01 20:37:02 +0000134 if (process_sp) {
135 if (enabled)
Jim Inghamc76c3f22016-11-02 01:06:42 +0000136 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000137 else
Jim Inghamc76c3f22016-11-02 01:06:42 +0000138 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000139 } else {
Jim Inghamc76c3f22016-11-02 01:06:42 +0000140 watchpoint_sp->SetEnabled(enabled, notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145bool SBWatchpoint::IsEnabled() {
146 lldb::WatchpointSP watchpoint_sp(GetSP());
147 if (watchpoint_sp) {
148 std::lock_guard<std::recursive_mutex> guard(
149 watchpoint_sp->GetTarget().GetAPIMutex());
150 return watchpoint_sp->IsEnabled();
151 } else
152 return false;
Johnny Chen5d043462011-09-26 22:40:50 +0000153}
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155uint32_t SBWatchpoint::GetHitCount() {
156 uint32_t count = 0;
157 lldb::WatchpointSP watchpoint_sp(GetSP());
158 if (watchpoint_sp) {
159 std::lock_guard<std::recursive_mutex> guard(
160 watchpoint_sp->GetTarget().GetAPIMutex());
161 count = watchpoint_sp->GetHitCount();
162 }
163
164 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
165 if (log)
166 log->Printf("SBWatchpoint(%p)::GetHitCount () => %u",
167 static_cast<void *>(watchpoint_sp.get()), count);
168
169 return count;
Johnny Chen5d043462011-09-26 22:40:50 +0000170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172uint32_t SBWatchpoint::GetIgnoreCount() {
173 lldb::WatchpointSP watchpoint_sp(GetSP());
174 if (watchpoint_sp) {
175 std::lock_guard<std::recursive_mutex> guard(
176 watchpoint_sp->GetTarget().GetAPIMutex());
177 return watchpoint_sp->GetIgnoreCount();
178 } else
179 return 0;
Johnny Chen5d043462011-09-26 22:40:50 +0000180}
Greg Clayton81e871e2012-02-04 02:27:34 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182void SBWatchpoint::SetIgnoreCount(uint32_t n) {
183 lldb::WatchpointSP watchpoint_sp(GetSP());
184 if (watchpoint_sp) {
185 std::lock_guard<std::recursive_mutex> guard(
186 watchpoint_sp->GetTarget().GetAPIMutex());
187 watchpoint_sp->SetIgnoreCount(n);
188 }
Greg Clayton81e871e2012-02-04 02:27:34 +0000189}
Jim Ingham1b5792e2012-12-18 02:03:49 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191const char *SBWatchpoint::GetCondition() {
192 lldb::WatchpointSP watchpoint_sp(GetSP());
193 if (watchpoint_sp) {
194 std::lock_guard<std::recursive_mutex> guard(
195 watchpoint_sp->GetTarget().GetAPIMutex());
196 return watchpoint_sp->GetConditionText();
197 }
198 return NULL;
199}
Jim Ingham1b5792e2012-12-18 02:03:49 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201void SBWatchpoint::SetCondition(const char *condition) {
202 lldb::WatchpointSP watchpoint_sp(GetSP());
203 if (watchpoint_sp) {
204 std::lock_guard<std::recursive_mutex> guard(
205 watchpoint_sp->GetTarget().GetAPIMutex());
206 watchpoint_sp->SetCondition(condition);
207 }
208}
209
210bool SBWatchpoint::GetDescription(SBStream &description,
211 DescriptionLevel level) {
212 Stream &strm = description.ref();
213
214 lldb::WatchpointSP watchpoint_sp(GetSP());
215 if (watchpoint_sp) {
216 std::lock_guard<std::recursive_mutex> guard(
217 watchpoint_sp->GetTarget().GetAPIMutex());
218 watchpoint_sp->GetDescription(&strm, level);
219 strm.EOL();
220 } else
221 strm.PutCString("No value");
222
223 return true;
224}
225
226void SBWatchpoint::Clear() { m_opaque_sp.reset(); }
227
228lldb::WatchpointSP SBWatchpoint::GetSP() const { return m_opaque_sp; }
229
230void SBWatchpoint::SetSP(const lldb::WatchpointSP &sp) { m_opaque_sp = sp; }
231
232bool SBWatchpoint::EventIsWatchpointEvent(const lldb::SBEvent &event) {
233 return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) !=
234 NULL;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000235}
236
237WatchpointEventType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238SBWatchpoint::GetWatchpointEventTypeFromEvent(const SBEvent &event) {
239 if (event.IsValid())
240 return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
241 event.GetSP());
242 return eWatchpointEventTypeInvalidType;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000243}
244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245SBWatchpoint SBWatchpoint::GetWatchpointFromEvent(const lldb::SBEvent &event) {
246 SBWatchpoint sb_watchpoint;
247 if (event.IsValid())
248 sb_watchpoint.m_opaque_sp =
249 Watchpoint::WatchpointEventData::GetWatchpointFromEvent(event.GetSP());
250 return sb_watchpoint;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000251}