blob: 1437c708157e2fe95d449a312bc7f9e1678b97cf [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"
Johnny Chen5d043462011-09-26 22:40:50 +000020#include "lldb/Core/StreamFile.h"
Jim Ingham306b62b2016-11-01 20:37:02 +000021#include "lldb/Target/Process.h"
Johnny Chen5d043462011-09-26 22:40:50 +000022#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000023#include "lldb/Utility/Stream.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
Pavel Labath7a1d34b2017-02-28 12:32:45 +000030SBWatchpoint::SBWatchpoint() {}
Johnny Chen5d043462011-09-26 22:40:50 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032SBWatchpoint::SBWatchpoint(const lldb::WatchpointSP &wp_sp)
Pavel Labath7a1d34b2017-02-28 12:32:45 +000033 : m_opaque_wp(wp_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
35
36 if (log) {
37 SBStream sstr;
38 GetDescription(sstr, lldb::eDescriptionLevelBrief);
Pavel Labath7a1d34b2017-02-28 12:32:45 +000039 LLDB_LOG(log, "watchpoint = {0} ({1})", wp_sp.get(), sstr.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 }
Johnny Chen5d043462011-09-26 22:40:50 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043SBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs)
Pavel Labath7a1d34b2017-02-28 12:32:45 +000044 : m_opaque_wp(rhs.m_opaque_wp) {}
Johnny Chen5d043462011-09-26 22:40:50 +000045
Kate Stoneb9c1b512016-09-06 20:57:50 +000046const SBWatchpoint &SBWatchpoint::operator=(const SBWatchpoint &rhs) {
Pavel Labath7a1d34b2017-02-28 12:32:45 +000047 m_opaque_wp = rhs.m_opaque_wp;
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 return *this;
Johnny Chen5d043462011-09-26 22:40:50 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051SBWatchpoint::~SBWatchpoint() {}
Johnny Chen5d043462011-09-26 22:40:50 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053watch_id_t SBWatchpoint::GetID() {
54 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Johnny Chen5d043462011-09-26 22:40:50 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
57 lldb::WatchpointSP watchpoint_sp(GetSP());
58 if (watchpoint_sp)
59 watch_id = watchpoint_sp->GetID();
Johnny Chen5d043462011-09-26 22:40:50 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 if (log) {
62 if (watch_id == LLDB_INVALID_WATCH_ID)
63 log->Printf("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID",
64 static_cast<void *>(watchpoint_sp.get()));
Johnny Chen5d043462011-09-26 22:40:50 +000065 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 log->Printf("SBWatchpoint(%p)::GetID () => %u",
67 static_cast<void *>(watchpoint_sp.get()), watch_id);
68 }
69
70 return watch_id;
Johnny Chen5d043462011-09-26 22:40:50 +000071}
72
Pavel Labath7a1d34b2017-02-28 12:32:45 +000073bool SBWatchpoint::IsValid() const { return bool(m_opaque_wp.lock()); }
Johnny Chend4dd7992011-09-27 01:19:20 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075SBError SBWatchpoint::GetError() {
76 SBError sb_error;
77 lldb::WatchpointSP watchpoint_sp(GetSP());
78 if (watchpoint_sp) {
79 sb_error.SetError(watchpoint_sp->GetError());
80 }
81 return sb_error;
Johnny Chend4dd7992011-09-27 01:19:20 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084int32_t SBWatchpoint::GetHardwareIndex() {
85 int32_t hw_index = -1;
86
87 lldb::WatchpointSP watchpoint_sp(GetSP());
88 if (watchpoint_sp) {
89 std::lock_guard<std::recursive_mutex> guard(
90 watchpoint_sp->GetTarget().GetAPIMutex());
91 hw_index = watchpoint_sp->GetHardwareIndex();
92 }
93
94 return hw_index;
Johnny Chen5d043462011-09-26 22:40:50 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097addr_t SBWatchpoint::GetWatchAddress() {
98 addr_t ret_addr = LLDB_INVALID_ADDRESS;
99
100 lldb::WatchpointSP watchpoint_sp(GetSP());
101 if (watchpoint_sp) {
102 std::lock_guard<std::recursive_mutex> guard(
103 watchpoint_sp->GetTarget().GetAPIMutex());
104 ret_addr = watchpoint_sp->GetLoadAddress();
105 }
106
107 return ret_addr;
Johnny Chen5d043462011-09-26 22:40:50 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110size_t SBWatchpoint::GetWatchSize() {
111 size_t watch_size = 0;
112
113 lldb::WatchpointSP watchpoint_sp(GetSP());
114 if (watchpoint_sp) {
115 std::lock_guard<std::recursive_mutex> guard(
116 watchpoint_sp->GetTarget().GetAPIMutex());
117 watch_size = watchpoint_sp->GetByteSize();
118 }
119
120 return watch_size;
Johnny Chen16dcf712011-10-17 18:58:00 +0000121}
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123void SBWatchpoint::SetEnabled(bool enabled) {
124 lldb::WatchpointSP watchpoint_sp(GetSP());
125 if (watchpoint_sp) {
Jim Ingham306b62b2016-11-01 20:37:02 +0000126 Target &target = watchpoint_sp->GetTarget();
127 std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
128 ProcessSP process_sp = target.GetProcessSP();
Jim Inghamc76c3f22016-11-02 01:06:42 +0000129 const bool notify = true;
Jim Ingham306b62b2016-11-01 20:37:02 +0000130 if (process_sp) {
131 if (enabled)
Jim Inghamc76c3f22016-11-02 01:06:42 +0000132 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000133 else
Jim Inghamc76c3f22016-11-02 01:06:42 +0000134 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000135 } else {
Jim Inghamc76c3f22016-11-02 01:06:42 +0000136 watchpoint_sp->SetEnabled(enabled, notify);
Jim Ingham306b62b2016-11-01 20:37:02 +0000137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141bool SBWatchpoint::IsEnabled() {
142 lldb::WatchpointSP watchpoint_sp(GetSP());
143 if (watchpoint_sp) {
144 std::lock_guard<std::recursive_mutex> guard(
145 watchpoint_sp->GetTarget().GetAPIMutex());
146 return watchpoint_sp->IsEnabled();
147 } else
148 return false;
Johnny Chen5d043462011-09-26 22:40:50 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151uint32_t SBWatchpoint::GetHitCount() {
152 uint32_t count = 0;
153 lldb::WatchpointSP watchpoint_sp(GetSP());
154 if (watchpoint_sp) {
155 std::lock_guard<std::recursive_mutex> guard(
156 watchpoint_sp->GetTarget().GetAPIMutex());
157 count = watchpoint_sp->GetHitCount();
158 }
159
160 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
161 if (log)
162 log->Printf("SBWatchpoint(%p)::GetHitCount () => %u",
163 static_cast<void *>(watchpoint_sp.get()), count);
164
165 return count;
Johnny Chen5d043462011-09-26 22:40:50 +0000166}
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168uint32_t SBWatchpoint::GetIgnoreCount() {
169 lldb::WatchpointSP watchpoint_sp(GetSP());
170 if (watchpoint_sp) {
171 std::lock_guard<std::recursive_mutex> guard(
172 watchpoint_sp->GetTarget().GetAPIMutex());
173 return watchpoint_sp->GetIgnoreCount();
174 } else
175 return 0;
Johnny Chen5d043462011-09-26 22:40:50 +0000176}
Greg Clayton81e871e2012-02-04 02:27:34 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178void SBWatchpoint::SetIgnoreCount(uint32_t n) {
179 lldb::WatchpointSP watchpoint_sp(GetSP());
180 if (watchpoint_sp) {
181 std::lock_guard<std::recursive_mutex> guard(
182 watchpoint_sp->GetTarget().GetAPIMutex());
183 watchpoint_sp->SetIgnoreCount(n);
184 }
Greg Clayton81e871e2012-02-04 02:27:34 +0000185}
Jim Ingham1b5792e2012-12-18 02:03:49 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187const char *SBWatchpoint::GetCondition() {
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->GetConditionText();
193 }
194 return NULL;
195}
Jim Ingham1b5792e2012-12-18 02:03:49 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197void SBWatchpoint::SetCondition(const char *condition) {
198 lldb::WatchpointSP watchpoint_sp(GetSP());
199 if (watchpoint_sp) {
200 std::lock_guard<std::recursive_mutex> guard(
201 watchpoint_sp->GetTarget().GetAPIMutex());
202 watchpoint_sp->SetCondition(condition);
203 }
204}
205
206bool SBWatchpoint::GetDescription(SBStream &description,
207 DescriptionLevel level) {
208 Stream &strm = description.ref();
209
210 lldb::WatchpointSP watchpoint_sp(GetSP());
211 if (watchpoint_sp) {
212 std::lock_guard<std::recursive_mutex> guard(
213 watchpoint_sp->GetTarget().GetAPIMutex());
214 watchpoint_sp->GetDescription(&strm, level);
215 strm.EOL();
216 } else
217 strm.PutCString("No value");
218
219 return true;
220}
221
Pavel Labath7a1d34b2017-02-28 12:32:45 +0000222void SBWatchpoint::Clear() { m_opaque_wp.reset(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223
Pavel Labath7a1d34b2017-02-28 12:32:45 +0000224lldb::WatchpointSP SBWatchpoint::GetSP() const { return m_opaque_wp.lock(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225
Pavel Labath7a1d34b2017-02-28 12:32:45 +0000226void SBWatchpoint::SetSP(const lldb::WatchpointSP &sp) { m_opaque_wp = sp; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227
228bool SBWatchpoint::EventIsWatchpointEvent(const lldb::SBEvent &event) {
229 return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) !=
230 NULL;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000231}
232
233WatchpointEventType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234SBWatchpoint::GetWatchpointEventTypeFromEvent(const SBEvent &event) {
235 if (event.IsValid())
236 return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
237 event.GetSP());
238 return eWatchpointEventTypeInvalidType;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241SBWatchpoint SBWatchpoint::GetWatchpointFromEvent(const lldb::SBEvent &event) {
242 SBWatchpoint sb_watchpoint;
243 if (event.IsValid())
Pavel Labath7a1d34b2017-02-28 12:32:45 +0000244 sb_watchpoint =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 Watchpoint::WatchpointEventData::GetWatchpointFromEvent(event.GetSP());
246 return sb_watchpoint;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000247}