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