1254721Semaste//===-- WatchpointOptions.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/Breakpoint/WatchpointOptions.h"
11254721Semaste
12254721Semaste// C Includes
13254721Semaste// C++ Includes
14254721Semaste// Other libraries and framework includes
15254721Semaste// Project includes
16254721Semaste#include "lldb/Core/Stream.h"
17254721Semaste#include "lldb/Core/StringList.h"
18254721Semaste#include "lldb/Core/Value.h"
19254721Semaste#include "lldb/Breakpoint/StoppointCallbackContext.h"
20254721Semaste#include "lldb/Target/Process.h"
21254721Semaste#include "lldb/Target/Target.h"
22254721Semaste#include "lldb/Target/ThreadSpec.h"
23254721Semaste#include "lldb/Expression/ClangUserExpression.h"
24254721Semaste
25254721Semasteusing namespace lldb;
26254721Semasteusing namespace lldb_private;
27254721Semaste
28254721Semastebool
29254721SemasteWatchpointOptions::NullCallback (void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
30254721Semaste{
31254721Semaste    return true;
32254721Semaste}
33254721Semaste
34254721Semaste//----------------------------------------------------------------------
35254721Semaste// WatchpointOptions constructor
36254721Semaste//----------------------------------------------------------------------
37254721SemasteWatchpointOptions::WatchpointOptions() :
38254721Semaste    m_callback (WatchpointOptions::NullCallback),
39254721Semaste    m_callback_baton_sp (),
40254721Semaste    m_callback_is_synchronous (false),
41254721Semaste    m_thread_spec_ap ()
42254721Semaste{
43254721Semaste}
44254721Semaste
45254721Semaste//----------------------------------------------------------------------
46254721Semaste// WatchpointOptions copy constructor
47254721Semaste//----------------------------------------------------------------------
48254721SemasteWatchpointOptions::WatchpointOptions(const WatchpointOptions& rhs) :
49254721Semaste    m_callback (rhs.m_callback),
50254721Semaste    m_callback_baton_sp (rhs.m_callback_baton_sp),
51254721Semaste    m_callback_is_synchronous (rhs.m_callback_is_synchronous),
52254721Semaste    m_thread_spec_ap ()
53254721Semaste{
54254721Semaste    if (rhs.m_thread_spec_ap.get() != NULL)
55254721Semaste        m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
56254721Semaste}
57254721Semaste
58254721Semaste//----------------------------------------------------------------------
59254721Semaste// WatchpointOptions assignment operator
60254721Semaste//----------------------------------------------------------------------
61254721Semasteconst WatchpointOptions&
62254721SemasteWatchpointOptions::operator=(const WatchpointOptions& rhs)
63254721Semaste{
64254721Semaste    m_callback = rhs.m_callback;
65254721Semaste    m_callback_baton_sp = rhs.m_callback_baton_sp;
66254721Semaste    m_callback_is_synchronous = rhs.m_callback_is_synchronous;
67254721Semaste    if (rhs.m_thread_spec_ap.get() != NULL)
68254721Semaste        m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
69254721Semaste    return *this;
70254721Semaste}
71254721Semaste
72254721SemasteWatchpointOptions *
73254721SemasteWatchpointOptions::CopyOptionsNoCallback (WatchpointOptions &orig)
74254721Semaste{
75254721Semaste    WatchpointHitCallback orig_callback = orig.m_callback;
76254721Semaste    lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
77254721Semaste    bool orig_is_sync = orig.m_callback_is_synchronous;
78254721Semaste
79254721Semaste    orig.ClearCallback();
80254721Semaste    WatchpointOptions *ret_val = new WatchpointOptions(orig);
81254721Semaste
82254721Semaste    orig.SetCallback (orig_callback, orig_callback_baton_sp, orig_is_sync);
83254721Semaste
84254721Semaste    return ret_val;
85254721Semaste}
86254721Semaste
87254721Semaste//----------------------------------------------------------------------
88254721Semaste// Destructor
89254721Semaste//----------------------------------------------------------------------
90254721SemasteWatchpointOptions::~WatchpointOptions()
91254721Semaste{
92254721Semaste}
93254721Semaste
94254721Semaste//------------------------------------------------------------------
95254721Semaste// Callbacks
96254721Semaste//------------------------------------------------------------------
97254721Semastevoid
98254721SemasteWatchpointOptions::SetCallback (WatchpointHitCallback callback, const BatonSP &callback_baton_sp, bool callback_is_synchronous)
99254721Semaste{
100254721Semaste    m_callback_is_synchronous = callback_is_synchronous;
101254721Semaste    m_callback = callback;
102254721Semaste    m_callback_baton_sp = callback_baton_sp;
103254721Semaste}
104254721Semaste
105254721Semastevoid
106254721SemasteWatchpointOptions::ClearCallback ()
107254721Semaste{
108254721Semaste    m_callback = WatchpointOptions::NullCallback;
109254721Semaste    m_callback_is_synchronous = false;
110254721Semaste    m_callback_baton_sp.reset();
111254721Semaste}
112254721Semaste
113254721SemasteBaton *
114254721SemasteWatchpointOptions::GetBaton ()
115254721Semaste{
116254721Semaste    return m_callback_baton_sp.get();
117254721Semaste}
118254721Semaste
119254721Semasteconst Baton *
120254721SemasteWatchpointOptions::GetBaton () const
121254721Semaste{
122254721Semaste    return m_callback_baton_sp.get();
123254721Semaste}
124254721Semaste
125254721Semastebool
126254721SemasteWatchpointOptions::InvokeCallback (StoppointCallbackContext *context,
127254721Semaste                                   lldb::user_id_t watch_id)
128254721Semaste{
129254721Semaste    if (m_callback && context->is_synchronous == IsCallbackSynchronous())
130254721Semaste    {
131254721Semaste        return m_callback (m_callback_baton_sp ? m_callback_baton_sp->m_data : NULL,
132254721Semaste                           context,
133254721Semaste                           watch_id);
134254721Semaste    }
135254721Semaste    else
136254721Semaste        return true;
137254721Semaste}
138254721Semaste
139254721Semastebool
140254721SemasteWatchpointOptions::HasCallback ()
141254721Semaste{
142254721Semaste    return m_callback != WatchpointOptions::NullCallback;
143254721Semaste}
144254721Semaste
145254721Semasteconst ThreadSpec *
146254721SemasteWatchpointOptions::GetThreadSpecNoCreate () const
147254721Semaste{
148254721Semaste    return m_thread_spec_ap.get();
149254721Semaste}
150254721Semaste
151254721SemasteThreadSpec *
152254721SemasteWatchpointOptions::GetThreadSpec ()
153254721Semaste{
154254721Semaste    if (m_thread_spec_ap.get() == NULL)
155254721Semaste        m_thread_spec_ap.reset (new ThreadSpec());
156254721Semaste
157254721Semaste    return m_thread_spec_ap.get();
158254721Semaste}
159254721Semaste
160254721Semastevoid
161254721SemasteWatchpointOptions::SetThreadID (lldb::tid_t thread_id)
162254721Semaste{
163254721Semaste    GetThreadSpec()->SetTID(thread_id);
164254721Semaste}
165254721Semaste
166254721Semastevoid
167254721SemasteWatchpointOptions::GetCallbackDescription (Stream *s, lldb::DescriptionLevel level) const
168254721Semaste{
169254721Semaste    if (m_callback_baton_sp.get())
170254721Semaste    {
171254721Semaste        s->EOL();
172254721Semaste        m_callback_baton_sp->GetDescription (s, level);
173254721Semaste    }
174254721Semaste}
175254721Semastevoid
176254721SemasteWatchpointOptions::GetDescription (Stream *s, lldb::DescriptionLevel level) const
177254721Semaste{
178254721Semaste
179254721Semaste    // Figure out if there are any options not at their default value, and only print
180254721Semaste    // anything if there are:
181254721Semaste
182254721Semaste    if ((GetThreadSpecNoCreate() != NULL && GetThreadSpecNoCreate()->HasSpecification ()))
183254721Semaste    {
184254721Semaste        if (level == lldb::eDescriptionLevelVerbose)
185254721Semaste        {
186254721Semaste            s->EOL ();
187254721Semaste            s->IndentMore();
188254721Semaste            s->Indent();
189254721Semaste            s->PutCString("Watchpoint Options:\n");
190254721Semaste            s->IndentMore();
191254721Semaste            s->Indent();
192254721Semaste        }
193254721Semaste        else
194254721Semaste            s->PutCString(" Options: ");
195254721Semaste
196254721Semaste        if (m_thread_spec_ap.get())
197254721Semaste            m_thread_spec_ap->GetDescription (s, level);
198254721Semaste        else if (level == eDescriptionLevelBrief)
199254721Semaste            s->PutCString ("thread spec: no ");
200254721Semaste        if (level == lldb::eDescriptionLevelFull)
201254721Semaste        {
202254721Semaste            s->IndentLess();
203254721Semaste            s->IndentMore();
204254721Semaste        }
205254721Semaste    }
206254721Semaste
207254721Semaste    GetCallbackDescription(s, level);
208254721Semaste}
209254721Semaste
210254721Semastevoid
211254721SemasteWatchpointOptions::CommandBaton::GetDescription (Stream *s, lldb::DescriptionLevel level) const
212254721Semaste{
213254721Semaste    CommandData *data = (CommandData *)m_data;
214254721Semaste
215254721Semaste    if (level == eDescriptionLevelBrief)
216254721Semaste    {
217254721Semaste        s->Printf (", commands = %s", (data && data->user_source.GetSize() > 0) ? "yes" : "no");
218254721Semaste        return;
219254721Semaste    }
220254721Semaste
221254721Semaste    s->IndentMore ();
222254721Semaste    s->Indent("watchpoint commands:\n");
223254721Semaste
224254721Semaste    s->IndentMore ();
225254721Semaste    if (data && data->user_source.GetSize() > 0)
226254721Semaste    {
227254721Semaste        const size_t num_strings = data->user_source.GetSize();
228254721Semaste        for (size_t i = 0; i < num_strings; ++i)
229254721Semaste        {
230254721Semaste            s->Indent(data->user_source.GetStringAtIndex(i));
231254721Semaste            s->EOL();
232254721Semaste        }
233254721Semaste    }
234254721Semaste    else
235254721Semaste    {
236254721Semaste        s->PutCString ("No commands.\n");
237254721Semaste    }
238254721Semaste    s->IndentLess ();
239254721Semaste    s->IndentLess ();
240254721Semaste}
241254721Semaste
242