1 //===-- BreakpointOptions.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/Breakpoint/BreakpointOptions.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/StringList.h"
18 #include "lldb/Breakpoint/StoppointCallbackContext.h"
19 #include "lldb/Target/ThreadSpec.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 bool
25 BreakpointOptions::NullCallback (void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
26 {
27     return true;
28 }
29 
30 //----------------------------------------------------------------------
31 // BreakpointOptions constructor
32 //----------------------------------------------------------------------
33 BreakpointOptions::BreakpointOptions() :
34     m_callback (BreakpointOptions::NullCallback),
35     m_callback_is_synchronous (false),
36     m_callback_baton_sp (),
37     m_enabled (true),
38     m_ignore_count (0),
39     m_thread_spec_ap (NULL)
40 {
41 }
42 
43 //----------------------------------------------------------------------
44 // BreakpointOptions copy constructor
45 //----------------------------------------------------------------------
46 BreakpointOptions::BreakpointOptions(const BreakpointOptions& rhs) :
47     m_callback (rhs.m_callback),
48     m_callback_baton_sp (rhs.m_callback_baton_sp),
49     m_callback_is_synchronous (rhs.m_callback_is_synchronous),
50     m_enabled (rhs.m_enabled),
51     m_ignore_count (rhs.m_ignore_count),
52     m_thread_spec_ap (NULL)
53 {
54     if (rhs.m_thread_spec_ap.get() != NULL)
55         m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
56 }
57 
58 //----------------------------------------------------------------------
59 // BreakpointOptions assignment operator
60 //----------------------------------------------------------------------
61 const BreakpointOptions&
62 BreakpointOptions::operator=(const BreakpointOptions& rhs)
63 {
64     m_callback = rhs.m_callback;
65     m_callback_baton_sp = rhs.m_callback_baton_sp;
66     m_callback_is_synchronous = rhs.m_callback_is_synchronous;
67     m_enabled = rhs.m_enabled;
68     m_ignore_count = rhs.m_ignore_count;
69     if (rhs.m_thread_spec_ap.get() != NULL)
70         m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
71     return *this;
72 }
73 
74 BreakpointOptions *
75 BreakpointOptions::CopyOptionsNoCallback (BreakpointOptions &orig)
76 {
77     BreakpointHitCallback orig_callback = orig.m_callback;
78     lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
79     bool orig_is_sync = orig.m_callback_is_synchronous;
80 
81     orig.ClearCallback();
82     BreakpointOptions *ret_val = new BreakpointOptions(orig);
83 
84     orig.SetCallback (orig_callback, orig_callback_baton_sp, orig_is_sync);
85 
86     return ret_val;
87 }
88 
89 //----------------------------------------------------------------------
90 // Destructor
91 //----------------------------------------------------------------------
92 BreakpointOptions::~BreakpointOptions()
93 {
94 }
95 
96 //------------------------------------------------------------------
97 // Callbacks
98 //------------------------------------------------------------------
99 void
100 BreakpointOptions::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool callback_is_synchronous)
101 {
102     m_callback_is_synchronous = callback_is_synchronous;
103     m_callback = callback;
104     m_callback_baton_sp = callback_baton_sp;
105 }
106 
107 void
108 BreakpointOptions::ClearCallback ()
109 {
110     m_callback = NULL;
111     m_callback_baton_sp.reset();
112 }
113 
114 Baton *
115 BreakpointOptions::GetBaton ()
116 {
117     return m_callback_baton_sp.get();
118 }
119 
120 const Baton *
121 BreakpointOptions::GetBaton () const
122 {
123     return m_callback_baton_sp.get();
124 }
125 
126 bool
127 BreakpointOptions::InvokeCallback (StoppointCallbackContext *context,
128                                    lldb::user_id_t break_id,
129                                    lldb::user_id_t break_loc_id)
130 {
131     if (m_callback && context->is_synchronous == IsCallbackSynchronous())
132     {
133         return m_callback (m_callback_baton_sp ? m_callback_baton_sp->m_data : NULL,
134                            context,
135                            break_id,
136                            break_loc_id);
137     }
138     else
139         return true;
140 }
141 
142 bool
143 BreakpointOptions::HasCallback ()
144 {
145     return m_callback != BreakpointOptions::NullCallback;
146 }
147 
148 //------------------------------------------------------------------
149 // Enabled/Ignore Count
150 //------------------------------------------------------------------
151 bool
152 BreakpointOptions::IsEnabled () const
153 {
154     return m_enabled;
155 }
156 
157 void
158 BreakpointOptions::SetEnabled (bool enabled)
159 {
160     m_enabled = enabled;
161 }
162 
163 int32_t
164 BreakpointOptions::GetIgnoreCount () const
165 {
166     return m_ignore_count;
167 }
168 
169 void
170 BreakpointOptions::SetIgnoreCount (int32_t n)
171 {
172     m_ignore_count = n;
173 }
174 
175 const ThreadSpec *
176 BreakpointOptions::GetThreadSpecNoCreate () const
177 {
178     return m_thread_spec_ap.get();
179 }
180 
181 ThreadSpec *
182 BreakpointOptions::GetThreadSpec ()
183 {
184     if (m_thread_spec_ap.get() == NULL)
185         m_thread_spec_ap.reset (new ThreadSpec());
186 
187     return m_thread_spec_ap.get();
188 }
189 
190 void
191 BreakpointOptions::SetThreadID (lldb::tid_t thread_id)
192 {
193     GetThreadSpec()->SetTID(thread_id);
194 }
195 
196 void
197 BreakpointOptions::GetDescription (Stream *s, lldb::DescriptionLevel level) const
198 {
199 
200     // Figure out if there are any options not at their default value, and only print
201     // anything if there are:
202 
203     if (m_ignore_count != 0 || !m_enabled || (GetThreadSpecNoCreate() != NULL && GetThreadSpecNoCreate()->HasSpecification ()))
204     {
205         if (level == lldb::eDescriptionLevelVerbose)
206         {
207             s->EOL ();
208             s->IndentMore();
209             s->Indent();
210             s->PutCString("Breakpoint Options:\n");
211             s->IndentMore();
212             s->Indent();
213         }
214         else
215             s->PutCString(" Options: ");
216 
217         if (m_ignore_count > 0)
218             s->Printf("ignore: %d ", m_ignore_count);
219         s->Printf("%sabled ", m_enabled ? "en" : "dis");
220 
221         if (m_thread_spec_ap.get())
222             m_thread_spec_ap->GetDescription (s, level);
223         else if (level == eDescriptionLevelBrief)
224             s->PutCString ("thread spec: no ");
225         if (level == lldb::eDescriptionLevelFull)
226         {
227             s->IndentLess();
228             s->IndentMore();
229         }
230     }
231 
232     if (m_callback_baton_sp.get())
233     {
234         if (level != eDescriptionLevelBrief)
235             s->EOL();
236         m_callback_baton_sp->GetDescription (s, level);
237     }
238 }
239 
240 void
241 BreakpointOptions::CommandBaton::GetDescription (Stream *s, lldb::DescriptionLevel level) const
242 {
243     CommandData *data = (CommandData *)m_data;
244 
245     if (level == eDescriptionLevelBrief)
246     {
247         s->Printf (", commands = %s", (data && data->user_source.GetSize() > 0) ? "yes" : "no");
248         return;
249     }
250 
251     s->IndentMore ();
252     s->Indent("Breakpoint commands:\n");
253 
254     s->IndentMore ();
255     if (data && data->user_source.GetSize() > 0)
256     {
257         const size_t num_strings = data->user_source.GetSize();
258         for (size_t i = 0; i < num_strings; ++i)
259         {
260             s->Indent(data->user_source.GetStringAtIndex(i));
261             s->EOL();
262         }
263     }
264     else
265     {
266         s->PutCString ("No commands.\n");
267     }
268     s->IndentLess ();
269     s->IndentLess ();
270 }
271 
272