1254721Semaste//===-- SBWatchpoint.cpp --------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/API/SBWatchpoint.h"
11254721Semaste#include "lldb/API/SBDefines.h"
12254721Semaste#include "lldb/API/SBAddress.h"
13254721Semaste#include "lldb/API/SBDebugger.h"
14254721Semaste#include "lldb/API/SBEvent.h"
15254721Semaste#include "lldb/API/SBStream.h"
16254721Semaste
17254721Semaste#include "lldb/lldb-types.h"
18254721Semaste#include "lldb/lldb-defines.h"
19254721Semaste#include "lldb/Breakpoint/Watchpoint.h"
20254721Semaste#include "lldb/Breakpoint/WatchpointList.h"
21254721Semaste#include "lldb/Core/Log.h"
22254721Semaste#include "lldb/Core/Stream.h"
23254721Semaste#include "lldb/Core/StreamFile.h"
24254721Semaste#include "lldb/Target/Target.h"
25254721Semaste
26254721Semasteusing namespace lldb;
27254721Semasteusing namespace lldb_private;
28254721Semaste
29254721Semaste
30254721SemasteSBWatchpoint::SBWatchpoint () :
31254721Semaste    m_opaque_sp ()
32254721Semaste{
33254721Semaste}
34254721Semaste
35254721SemasteSBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp) :
36254721Semaste    m_opaque_sp (wp_sp)
37254721Semaste{
38254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
39254721Semaste
40254721Semaste    if (log)
41254721Semaste    {
42254721Semaste        SBStream sstr;
43254721Semaste        GetDescription (sstr, lldb::eDescriptionLevelBrief);
44254721Semaste        log->Printf ("SBWatchpoint::SBWatchpoint (const lldb::WatchpointSP &wp_sp"
45254721Semaste                     "=%p)  => this.sp = %p (%s)", wp_sp.get(), m_opaque_sp.get(), sstr.GetData());
46254721Semaste    }
47254721Semaste}
48254721Semaste
49254721SemasteSBWatchpoint::SBWatchpoint(const SBWatchpoint &rhs) :
50254721Semaste    m_opaque_sp (rhs.m_opaque_sp)
51254721Semaste{
52254721Semaste}
53254721Semaste
54254721Semasteconst SBWatchpoint &
55254721SemasteSBWatchpoint::operator = (const SBWatchpoint &rhs)
56254721Semaste{
57254721Semaste    if (this != &rhs)
58254721Semaste        m_opaque_sp = rhs.m_opaque_sp;
59254721Semaste    return *this;
60254721Semaste}
61254721Semaste
62254721Semaste
63254721SemasteSBWatchpoint::~SBWatchpoint ()
64254721Semaste{
65254721Semaste}
66254721Semaste
67254721Semastewatch_id_t
68254721SemasteSBWatchpoint::GetID ()
69254721Semaste{
70254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
71254721Semaste
72254721Semaste    watch_id_t watch_id = LLDB_INVALID_WATCH_ID;
73254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
74254721Semaste    if (watchpoint_sp)
75254721Semaste        watch_id = watchpoint_sp->GetID();
76254721Semaste
77254721Semaste    if (log)
78254721Semaste    {
79254721Semaste        if (watch_id == LLDB_INVALID_WATCH_ID)
80254721Semaste            log->Printf ("SBWatchpoint(%p)::GetID () => LLDB_INVALID_WATCH_ID", watchpoint_sp.get());
81254721Semaste        else
82254721Semaste            log->Printf ("SBWatchpoint(%p)::GetID () => %u", watchpoint_sp.get(), watch_id);
83254721Semaste    }
84254721Semaste
85254721Semaste    return watch_id;
86254721Semaste}
87254721Semaste
88254721Semastebool
89254721SemasteSBWatchpoint::IsValid() const
90254721Semaste{
91254721Semaste    return (bool) m_opaque_sp;
92254721Semaste}
93254721Semaste
94254721SemasteSBError
95254721SemasteSBWatchpoint::GetError ()
96254721Semaste{
97254721Semaste    SBError sb_error;
98254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
99254721Semaste    if (watchpoint_sp)
100254721Semaste    {
101254721Semaste        sb_error.SetError(watchpoint_sp->GetError());
102254721Semaste    }
103254721Semaste    return sb_error;
104254721Semaste}
105254721Semaste
106254721Semasteint32_t
107254721SemasteSBWatchpoint::GetHardwareIndex ()
108254721Semaste{
109254721Semaste    int32_t hw_index = -1;
110254721Semaste
111254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
112254721Semaste    if (watchpoint_sp)
113254721Semaste    {
114254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
115254721Semaste        hw_index = watchpoint_sp->GetHardwareIndex();
116254721Semaste    }
117254721Semaste
118254721Semaste    return hw_index;
119254721Semaste}
120254721Semaste
121254721Semasteaddr_t
122254721SemasteSBWatchpoint::GetWatchAddress ()
123254721Semaste{
124254721Semaste    addr_t ret_addr = LLDB_INVALID_ADDRESS;
125254721Semaste
126254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
127254721Semaste    if (watchpoint_sp)
128254721Semaste    {
129254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
130254721Semaste        ret_addr = watchpoint_sp->GetLoadAddress();
131254721Semaste    }
132254721Semaste
133254721Semaste    return ret_addr;
134254721Semaste}
135254721Semaste
136254721Semastesize_t
137254721SemasteSBWatchpoint::GetWatchSize ()
138254721Semaste{
139254721Semaste    size_t watch_size = 0;
140254721Semaste
141254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
142254721Semaste    if (watchpoint_sp)
143254721Semaste    {
144254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
145254721Semaste        watch_size = watchpoint_sp->GetByteSize();
146254721Semaste    }
147254721Semaste
148254721Semaste    return watch_size;
149254721Semaste}
150254721Semaste
151254721Semastevoid
152254721SemasteSBWatchpoint::SetEnabled (bool enabled)
153254721Semaste{
154254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
155254721Semaste    if (watchpoint_sp)
156254721Semaste    {
157254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
158254721Semaste        watchpoint_sp->GetTarget().DisableWatchpointByID(watchpoint_sp->GetID());
159254721Semaste    }
160254721Semaste}
161254721Semaste
162254721Semastebool
163254721SemasteSBWatchpoint::IsEnabled ()
164254721Semaste{
165254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
166254721Semaste    if (watchpoint_sp)
167254721Semaste    {
168254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
169254721Semaste        return watchpoint_sp->IsEnabled();
170254721Semaste    }
171254721Semaste    else
172254721Semaste        return false;
173254721Semaste}
174254721Semaste
175254721Semasteuint32_t
176254721SemasteSBWatchpoint::GetHitCount ()
177254721Semaste{
178254721Semaste    uint32_t count = 0;
179254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
180254721Semaste    if (watchpoint_sp)
181254721Semaste    {
182254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
183254721Semaste        count = watchpoint_sp->GetHitCount();
184254721Semaste    }
185254721Semaste
186254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
187254721Semaste    if (log)
188254721Semaste        log->Printf ("SBWatchpoint(%p)::GetHitCount () => %u", watchpoint_sp.get(), count);
189254721Semaste
190254721Semaste    return count;
191254721Semaste}
192254721Semaste
193254721Semasteuint32_t
194254721SemasteSBWatchpoint::GetIgnoreCount ()
195254721Semaste{
196254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
197254721Semaste    if (watchpoint_sp)
198254721Semaste    {
199254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
200254721Semaste        return watchpoint_sp->GetIgnoreCount();
201254721Semaste    }
202254721Semaste    else
203254721Semaste        return 0;
204254721Semaste}
205254721Semaste
206254721Semastevoid
207254721SemasteSBWatchpoint::SetIgnoreCount (uint32_t n)
208254721Semaste{
209254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
210254721Semaste    if (watchpoint_sp)
211254721Semaste    {
212254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
213254721Semaste        watchpoint_sp->SetIgnoreCount (n);
214254721Semaste    }
215254721Semaste}
216254721Semaste
217254721Semasteconst char *
218254721SemasteSBWatchpoint::GetCondition ()
219254721Semaste{
220254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
221254721Semaste    if (watchpoint_sp)
222254721Semaste    {
223254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
224254721Semaste        return watchpoint_sp->GetConditionText ();
225254721Semaste    }
226254721Semaste    return NULL;
227254721Semaste}
228254721Semaste
229254721Semastevoid
230254721SemasteSBWatchpoint::SetCondition (const char *condition)
231254721Semaste{
232254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
233254721Semaste    if (watchpoint_sp)
234254721Semaste    {
235254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
236254721Semaste        watchpoint_sp->SetCondition (condition);
237254721Semaste    }
238254721Semaste}
239254721Semaste
240254721Semastebool
241254721SemasteSBWatchpoint::GetDescription (SBStream &description, DescriptionLevel level)
242254721Semaste{
243254721Semaste    Stream &strm = description.ref();
244254721Semaste
245254721Semaste    lldb::WatchpointSP watchpoint_sp(GetSP());
246254721Semaste    if (watchpoint_sp)
247254721Semaste    {
248254721Semaste        Mutex::Locker api_locker (watchpoint_sp->GetTarget().GetAPIMutex());
249254721Semaste        watchpoint_sp->GetDescription (&strm, level);
250254721Semaste        strm.EOL();
251254721Semaste    }
252254721Semaste    else
253254721Semaste        strm.PutCString ("No value");
254254721Semaste
255254721Semaste    return true;
256254721Semaste}
257254721Semaste
258254721Semastevoid
259254721SemasteSBWatchpoint::Clear ()
260254721Semaste{
261254721Semaste    m_opaque_sp.reset();
262254721Semaste}
263254721Semaste
264254721Semastelldb::WatchpointSP
265254721SemasteSBWatchpoint::GetSP () const
266254721Semaste{
267254721Semaste    return m_opaque_sp;
268254721Semaste}
269254721Semaste
270254721Semastevoid
271254721SemasteSBWatchpoint::SetSP (const lldb::WatchpointSP &sp)
272254721Semaste{
273254721Semaste    m_opaque_sp = sp;
274254721Semaste}
275254721Semaste
276254721Semastebool
277254721SemasteSBWatchpoint::EventIsWatchpointEvent (const lldb::SBEvent &event)
278254721Semaste{
279254721Semaste    return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) != NULL;
280254721Semaste
281254721Semaste}
282254721Semaste
283254721SemasteWatchpointEventType
284254721SemasteSBWatchpoint::GetWatchpointEventTypeFromEvent (const SBEvent& event)
285254721Semaste{
286254721Semaste    if (event.IsValid())
287254721Semaste        return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent (event.GetSP());
288254721Semaste    return eWatchpointEventTypeInvalidType;
289254721Semaste}
290254721Semaste
291254721SemasteSBWatchpoint
292254721SemasteSBWatchpoint::GetWatchpointFromEvent (const lldb::SBEvent& event)
293254721Semaste{
294254721Semaste    SBWatchpoint sb_watchpoint;
295254721Semaste    if (event.IsValid())
296254721Semaste        sb_watchpoint.m_opaque_sp = Watchpoint::WatchpointEventData::GetWatchpointFromEvent (event.GetSP());
297254721Semaste    return sb_watchpoint;
298254721Semaste}
299