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