1 //===-- SBWatchpoint.cpp --------------------------------*- C++ -*-===//
2 //
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 
10 #include "lldb/API/SBWatchpoint.h"
11 #include "lldb/API/SBDefines.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBDebugger.h"
14 #include "lldb/API/SBStream.h"
15 
16 #include "lldb/lldb-types.h"
17 #include "lldb/lldb-defines.h"
18 #include "lldb/Breakpoint/Watchpoint.h"
19 #include "lldb/Breakpoint/WatchpointList.h"
20 #include "lldb/Core/Log.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Target/Target.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 
29 SBWatchpoint::SBWatchpoint () :
30     m_opaque_sp ()
31 {
32 }
33 
34 SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp) :
35     m_opaque_sp (wp_sp)
36 {
37     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
38 
39     if (log)
40     {
41         SBStream sstr;
42         GetDescription (sstr, lldb::eDescriptionLevelBrief);
43         log->Printf ("SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp"
44                      "=%p)  => this.sp = %p (%s)", wp_sp.get(), m_opaque_sp.get(), sstr.GetData());
45     }
46 }
47 
48 SBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs) :
49     m_opaque_sp (rhs.m_opaque_sp)
50 {
51 }
52 
53 const SBWatchpoint &
54 SBWatchpoint::operator = (const SBWatchpoint &rhs)
55 {
56     if (this != &rhs)
57         m_opaque_sp = rhs.m_opaque_sp;
58     return *this;
59 }
60 
61 
62 SBWatchpoint::~SBWatchpoint ()
63 {
64 }
65 
66 watch_id_t
67 SBWatchpoint::GetID ()
68 {
69     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
70 
71     watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
72     lldb::WatchpointSP watchpoint_sp(GetSP());
73     if (watchpoint_sp)
74         watch_id = watchpoint_sp->GetID();
75 
76     if (log)
77     {
78         if (watch_id == LLDB_INVALID_WATCH_ID)
79             log->Printf ("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID", watchpoint_sp.get());
80         else
81             log->Printf ("SBWatchpoint(%p)::GetID () => %u", watchpoint_sp.get(), watch_id);
82     }
83 
84     return watch_id;
85 }
86 
87 bool
88 SBWatchpoint::IsValid() const
89 {
90     lldb::WatchpointSP watchpoint_sp(GetSP());
91     if (watchpoint_sp && watchpoint_sp->GetError().Success())
92         return true;
93     return false;
94 }
95 
96 SBError
97 SBWatchpoint::GetError ()
98 {
99     SBError sb_error;
100     lldb::WatchpointSP watchpoint_sp(GetSP());
101     if (watchpoint_sp)
102     {
103         sb_error.SetError(watchpoint_sp->GetError());
104     }
105     return sb_error;
106 }
107 
108 int32_t
109 SBWatchpoint::GetHardwareIndex ()
110 {
111     int32_t hw_index = -1;
112 
113     lldb::WatchpointSP watchpoint_sp(GetSP());
114     if (watchpoint_sp)
115     {
116         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
117         hw_index = watchpoint_sp->GetHardwareIndex();
118     }
119 
120     return hw_index;
121 }
122 
123 addr_t
124 SBWatchpoint::GetWatchAddress ()
125 {
126     addr_t ret_addr = LLDB_INVALID_ADDRESS;
127 
128     lldb::WatchpointSP watchpoint_sp(GetSP());
129     if (watchpoint_sp)
130     {
131         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
132         ret_addr = watchpoint_sp->GetLoadAddress();
133     }
134 
135     return ret_addr;
136 }
137 
138 size_t
139 SBWatchpoint::GetWatchSize ()
140 {
141     size_t watch_size = 0;
142 
143     lldb::WatchpointSP watchpoint_sp(GetSP());
144     if (watchpoint_sp)
145     {
146         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
147         watch_size = watchpoint_sp->GetByteSize();
148     }
149 
150     return watch_size;
151 }
152 
153 void
154 SBWatchpoint::SetEnabled (bool enabled)
155 {
156     lldb::WatchpointSP watchpoint_sp(GetSP());
157     if (watchpoint_sp)
158     {
159         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
160         watchpoint_sp->GetTarget().DisableWatchpointByID(watchpoint_sp->GetID());
161     }
162 }
163 
164 bool
165 SBWatchpoint::IsEnabled ()
166 {
167     lldb::WatchpointSP watchpoint_sp(GetSP());
168     if (watchpoint_sp)
169     {
170         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
171         return watchpoint_sp->IsEnabled();
172     }
173     else
174         return false;
175 }
176 
177 uint32_t
178 SBWatchpoint::GetHitCount ()
179 {
180     uint32_t count = 0;
181     lldb::WatchpointSP watchpoint_sp(GetSP());
182     if (watchpoint_sp)
183     {
184         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
185         count = watchpoint_sp->GetHitCount();
186     }
187 
188     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
189     if (log)
190         log->Printf ("SBWatchpoint(%p)::GetHitCount () => %u", watchpoint_sp.get(), count);
191 
192     return count;
193 }
194 
195 uint32_t
196 SBWatchpoint::GetIgnoreCount ()
197 {
198     lldb::WatchpointSP watchpoint_sp(GetSP());
199     if (watchpoint_sp)
200     {
201         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
202         return watchpoint_sp->GetIgnoreCount();
203     }
204     else
205         return 0;
206 }
207 
208 void
209 SBWatchpoint::SetIgnoreCount (uint32_t n)
210 {
211     lldb::WatchpointSP watchpoint_sp(GetSP());
212     if (watchpoint_sp)
213     {
214         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
215         watchpoint_sp->SetIgnoreCount (n);
216     }
217 }
218 
219 const char *
220 SBWatchpoint::GetCondition ()
221 {
222     lldb::WatchpointSP watchpoint_sp(GetSP());
223     if (watchpoint_sp)
224     {
225         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
226         return watchpoint_sp->GetConditionText ();
227     }
228     return NULL;
229 }
230 
231 void
232 SBWatchpoint::SetCondition (const char *condition)
233 {
234     lldb::WatchpointSP watchpoint_sp(GetSP());
235     if (watchpoint_sp)
236     {
237         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
238         watchpoint_sp->SetCondition (condition);
239     }
240 }
241 
242 bool
243 SBWatchpoint::GetDescription (SBStream &description, DescriptionLevel level)
244 {
245     Stream &strm = description.ref();
246 
247     lldb::WatchpointSP watchpoint_sp(GetSP());
248     if (watchpoint_sp)
249     {
250         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
251         watchpoint_sp->GetDescription (&strm, level);
252         strm.EOL();
253     }
254     else
255         strm.PutCString ("No value");
256 
257     return true;
258 }
259 
260 void
261 SBWatchpoint::Clear ()
262 {
263     m_opaque_sp.reset();
264 }
265 
266 lldb::WatchpointSP
267 SBWatchpoint::GetSP () const
268 {
269     return m_opaque_sp;
270 }
271 
272 void
273 SBWatchpoint::SetSP (const lldb::WatchpointSP &sp)
274 {
275     m_opaque_sp = sp;
276 }
277