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     return (bool) m_opaque_sp;
91 }
92 
93 SBError
94 SBWatchpoint::GetError ()
95 {
96     SBError sb_error;
97     lldb::WatchpointSP watchpoint_sp(GetSP());
98     if (watchpoint_sp)
99     {
100         sb_error.SetError(watchpoint_sp->GetError());
101     }
102     return sb_error;
103 }
104 
105 int32_t
106 SBWatchpoint::GetHardwareIndex ()
107 {
108     int32_t hw_index = -1;
109 
110     lldb::WatchpointSP watchpoint_sp(GetSP());
111     if (watchpoint_sp)
112     {
113         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
114         hw_index = watchpoint_sp->GetHardwareIndex();
115     }
116 
117     return hw_index;
118 }
119 
120 addr_t
121 SBWatchpoint::GetWatchAddress ()
122 {
123     addr_t ret_addr = LLDB_INVALID_ADDRESS;
124 
125     lldb::WatchpointSP watchpoint_sp(GetSP());
126     if (watchpoint_sp)
127     {
128         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
129         ret_addr = watchpoint_sp->GetLoadAddress();
130     }
131 
132     return ret_addr;
133 }
134 
135 size_t
136 SBWatchpoint::GetWatchSize ()
137 {
138     size_t watch_size = 0;
139 
140     lldb::WatchpointSP watchpoint_sp(GetSP());
141     if (watchpoint_sp)
142     {
143         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
144         watch_size = watchpoint_sp->GetByteSize();
145     }
146 
147     return watch_size;
148 }
149 
150 void
151 SBWatchpoint::SetEnabled (bool enabled)
152 {
153     lldb::WatchpointSP watchpoint_sp(GetSP());
154     if (watchpoint_sp)
155     {
156         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
157         watchpoint_sp->GetTarget().DisableWatchpointByID(watchpoint_sp->GetID());
158     }
159 }
160 
161 bool
162 SBWatchpoint::IsEnabled ()
163 {
164     lldb::WatchpointSP watchpoint_sp(GetSP());
165     if (watchpoint_sp)
166     {
167         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
168         return watchpoint_sp->IsEnabled();
169     }
170     else
171         return false;
172 }
173 
174 uint32_t
175 SBWatchpoint::GetHitCount ()
176 {
177     uint32_t count = 0;
178     lldb::WatchpointSP watchpoint_sp(GetSP());
179     if (watchpoint_sp)
180     {
181         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
182         count = watchpoint_sp->GetHitCount();
183     }
184 
185     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
186     if (log)
187         log->Printf ("SBWatchpoint(%p)::GetHitCount () => %u", watchpoint_sp.get(), count);
188 
189     return count;
190 }
191 
192 uint32_t
193 SBWatchpoint::GetIgnoreCount ()
194 {
195     lldb::WatchpointSP watchpoint_sp(GetSP());
196     if (watchpoint_sp)
197     {
198         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
199         return watchpoint_sp->GetIgnoreCount();
200     }
201     else
202         return 0;
203 }
204 
205 void
206 SBWatchpoint::SetIgnoreCount (uint32_t n)
207 {
208     lldb::WatchpointSP watchpoint_sp(GetSP());
209     if (watchpoint_sp)
210     {
211         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
212         watchpoint_sp->SetIgnoreCount (n);
213     }
214 }
215 
216 const char *
217 SBWatchpoint::GetCondition ()
218 {
219     lldb::WatchpointSP watchpoint_sp(GetSP());
220     if (watchpoint_sp)
221     {
222         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
223         return watchpoint_sp->GetConditionText ();
224     }
225     return NULL;
226 }
227 
228 void
229 SBWatchpoint::SetCondition (const char *condition)
230 {
231     lldb::WatchpointSP watchpoint_sp(GetSP());
232     if (watchpoint_sp)
233     {
234         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
235         watchpoint_sp->SetCondition (condition);
236     }
237 }
238 
239 bool
240 SBWatchpoint::GetDescription (SBStream &description, DescriptionLevel level)
241 {
242     Stream &strm = description.ref();
243 
244     lldb::WatchpointSP watchpoint_sp(GetSP());
245     if (watchpoint_sp)
246     {
247         Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
248         watchpoint_sp->GetDescription (&strm, level);
249         strm.EOL();
250     }
251     else
252         strm.PutCString ("No value");
253 
254     return true;
255 }
256 
257 void
258 SBWatchpoint::Clear ()
259 {
260     m_opaque_sp.reset();
261 }
262 
263 lldb::WatchpointSP
264 SBWatchpoint::GetSP () const
265 {
266     return m_opaque_sp;
267 }
268 
269 void
270 SBWatchpoint::SetSP (const lldb::WatchpointSP &sp)
271 {
272     m_opaque_sp = sp;
273 }
274