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/WatchpointLocation.h"
19 #include "lldb/Breakpoint/WatchpointLocationList.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::WatchpointLocationSP &watch_loc_sp) :
35     m_opaque_sp (watch_loc_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::WatchpointLocationsSP &watch_loc_sp"
44                      "=%p)  => this.sp = %p (%s)", watch_loc_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     if (m_opaque_sp)
73         watch_id = m_opaque_sp->GetID();
74 
75     if (log)
76     {
77         if (watch_id == LLDB_INVALID_WATCH_ID)
78             log->Printf ("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID", m_opaque_sp.get());
79         else
80             log->Printf ("SBWatchpoint(%p)::GetID () => %u", m_opaque_sp.get(), watch_id);
81     }
82 
83     return watch_id;
84 }
85 
86 bool
87 SBWatchpoint::IsValid() const
88 {
89     return m_opaque_sp.get() != NULL;
90 #if 0
91     if (m_opaque_sp)
92         return m_opaque_sp->GetError().Success();
93     return false;
94 #endif
95 }
96 
97 SBError
98 SBWatchpoint::GetError ()
99 {
100     SBError sb_error;
101 #if 0
102     if (m_opaque_sp)
103     {
104         // TODO: Johnny fill this in
105         sb_error.ref() = m_opaque_sp->GetError();
106     }
107 #endif
108     return sb_error;
109 }
110 
111 int32_t
112 SBWatchpoint::GetHardwareIndex ()
113 {
114     int32_t hw_index = -1;
115 
116     if (m_opaque_sp)
117     {
118         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
119         hw_index = m_opaque_sp->GetHardwareIndex();
120     }
121 
122     return hw_index;
123 }
124 
125 addr_t
126 SBWatchpoint::GetWatchAddress ()
127 {
128     addr_t ret_addr = LLDB_INVALID_ADDRESS;
129 
130     if (m_opaque_sp)
131     {
132         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
133         ret_addr = m_opaque_sp->GetLoadAddress();
134     }
135 
136     return ret_addr;
137 }
138 
139 size_t
140 SBWatchpoint::GetWatchSize ()
141 {
142     size_t watch_size = 0;
143 
144     if (m_opaque_sp)
145     {
146         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
147         watch_size = m_opaque_sp->GetByteSize();
148     }
149 
150     return watch_size;
151 }
152 
153 void
154 SBWatchpoint::SetEnabled (bool enabled)
155 {
156     if (m_opaque_sp)
157     {
158         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
159         m_opaque_sp->GetTarget().DisableWatchpointLocationByID(m_opaque_sp->GetID());
160     }
161 }
162 
163 bool
164 SBWatchpoint::IsEnabled ()
165 {
166     if (m_opaque_sp)
167     {
168         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
169         return m_opaque_sp->IsEnabled();
170     }
171     else
172         return false;
173 }
174 
175 uint32_t
176 SBWatchpoint::GetHitCount ()
177 {
178     uint32_t count = 0;
179     if (m_opaque_sp)
180     {
181         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
182         count = m_opaque_sp->GetHitCount();
183     }
184 
185     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
186     if (log)
187         log->Printf ("SBWatchpoint(%p)::GetHitCount () => %u", m_opaque_sp.get(), count);
188 
189     return count;
190 }
191 
192 uint32_t
193 SBWatchpoint::GetIgnoreCount ()
194 {
195     if (m_opaque_sp)
196     {
197         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
198         return m_opaque_sp->GetIgnoreCount();
199     }
200     else
201         return 0;
202 }
203 
204 void
205 SBWatchpoint::SetIgnoreCount (uint32_t n)
206 {
207     if (m_opaque_sp)
208     {
209         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
210         m_opaque_sp->SetIgnoreCount (n);
211     }
212 }
213 
214 bool
215 SBWatchpoint::GetDescription (SBStream &description, DescriptionLevel level)
216 {
217     if (m_opaque_sp)
218     {
219         Mutex::Locker api_locker (m_opaque_sp->GetTarget().GetAPIMutex());
220         description.ref();
221         m_opaque_sp->GetDescription (description.get(), level);
222         description.get()->EOL();
223     }
224     else
225         description.Printf ("No value");
226 
227     return true;
228 }
229 
230 lldb_private::WatchpointLocation *
231 SBWatchpoint::operator->()
232 {
233     return m_opaque_sp.get();
234 }
235 
236 lldb_private::WatchpointLocation *
237 SBWatchpoint::get()
238 {
239     return m_opaque_sp.get();
240 }
241 
242 lldb::WatchpointLocationSP &
243 SBWatchpoint::operator *()
244 {
245     return m_opaque_sp;
246 }
247