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/Core/Value.h"
19 #include "lldb/Breakpoint/StoppointCallbackContext.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/ThreadSpec.h"
23 #include "lldb/Target/ThreadPlanTestCondition.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 bool
29 BreakpointOptions::NullCallback (void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
30 {
31     return true;
32 }
33 
34 //----------------------------------------------------------------------
35 // BreakpointOptions constructor
36 //----------------------------------------------------------------------
37 BreakpointOptions::BreakpointOptions() :
38     m_callback (BreakpointOptions::NullCallback),
39     m_callback_baton_sp (),
40     m_callback_is_synchronous (false),
41     m_enabled (true),
42     m_ignore_count (0),
43     m_thread_spec_ap (NULL),
44     m_condition_ap()
45 {
46 }
47 
48 //----------------------------------------------------------------------
49 // BreakpointOptions copy constructor
50 //----------------------------------------------------------------------
51 BreakpointOptions::BreakpointOptions(const BreakpointOptions& rhs) :
52     m_callback (rhs.m_callback),
53     m_callback_baton_sp (rhs.m_callback_baton_sp),
54     m_callback_is_synchronous (rhs.m_callback_is_synchronous),
55     m_enabled (rhs.m_enabled),
56     m_ignore_count (rhs.m_ignore_count),
57     m_thread_spec_ap (NULL),
58     m_condition_ap (NULL)
59 {
60     if (rhs.m_thread_spec_ap.get() != NULL)
61         m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
62     if (rhs.m_condition_ap.get())
63         m_condition_ap.reset (new ClangUserExpression (rhs.m_condition_ap->GetUserText(), NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
64 }
65 
66 //----------------------------------------------------------------------
67 // BreakpointOptions assignment operator
68 //----------------------------------------------------------------------
69 const BreakpointOptions&
70 BreakpointOptions::operator=(const BreakpointOptions& rhs)
71 {
72     m_callback = rhs.m_callback;
73     m_callback_baton_sp = rhs.m_callback_baton_sp;
74     m_callback_is_synchronous = rhs.m_callback_is_synchronous;
75     m_enabled = rhs.m_enabled;
76     m_ignore_count = rhs.m_ignore_count;
77     if (rhs.m_thread_spec_ap.get() != NULL)
78         m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
79     if (rhs.m_condition_ap.get())
80         m_condition_ap.reset (new ClangUserExpression (rhs.m_condition_ap->GetUserText(), NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
81     return *this;
82 }
83 
84 BreakpointOptions *
85 BreakpointOptions::CopyOptionsNoCallback (BreakpointOptions &orig)
86 {
87     BreakpointHitCallback orig_callback = orig.m_callback;
88     lldb::BatonSP orig_callback_baton_sp = orig.m_callback_baton_sp;
89     bool orig_is_sync = orig.m_callback_is_synchronous;
90 
91     orig.ClearCallback();
92     BreakpointOptions *ret_val = new BreakpointOptions(orig);
93 
94     orig.SetCallback (orig_callback, orig_callback_baton_sp, orig_is_sync);
95 
96     return ret_val;
97 }
98 
99 //----------------------------------------------------------------------
100 // Destructor
101 //----------------------------------------------------------------------
102 BreakpointOptions::~BreakpointOptions()
103 {
104 }
105 
106 //------------------------------------------------------------------
107 // Callbacks
108 //------------------------------------------------------------------
109 void
110 BreakpointOptions::SetCallback (BreakpointHitCallback callback, const BatonSP &callback_baton_sp, bool callback_is_synchronous)
111 {
112     m_callback_is_synchronous = callback_is_synchronous;
113     m_callback = callback;
114     m_callback_baton_sp = callback_baton_sp;
115 }
116 
117 void
118 BreakpointOptions::ClearCallback ()
119 {
120     m_callback = BreakpointOptions::NullCallback;
121     m_callback_is_synchronous = false;
122     m_callback_baton_sp.reset();
123 }
124 
125 Baton *
126 BreakpointOptions::GetBaton ()
127 {
128     return m_callback_baton_sp.get();
129 }
130 
131 const Baton *
132 BreakpointOptions::GetBaton () const
133 {
134     return m_callback_baton_sp.get();
135 }
136 
137 bool
138 BreakpointOptions::InvokeCallback (StoppointCallbackContext *context,
139                                    lldb::user_id_t break_id,
140                                    lldb::user_id_t break_loc_id)
141 {
142     if (m_callback && context->is_synchronous == IsCallbackSynchronous())
143     {
144         return m_callback (m_callback_baton_sp ? m_callback_baton_sp->m_data : NULL,
145                            context,
146                            break_id,
147                            break_loc_id);
148     }
149     else
150         return true;
151 }
152 
153 bool
154 BreakpointOptions::HasCallback ()
155 {
156     return m_callback != BreakpointOptions::NullCallback;
157 }
158 
159 void
160 BreakpointOptions::SetCondition (const char *condition)
161 {
162     if (condition == NULL || condition[0] == '\0')
163     {
164         if (m_condition_ap.get())
165             m_condition_ap.reset();
166     }
167     else
168     {
169         m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
170     }
171 }
172 
173 ThreadPlan *
174 BreakpointOptions::GetThreadPlanToTestCondition (ExecutionContext &exe_ctx,
175                                                  const BreakpointLocationSP &break_loc_sp,
176                                                  Stream &error_stream)
177 {
178     // No condition means we should stop, so return NULL.
179     if (!m_condition_ap.get())
180         return NULL;
181 
182     // FIXME: I shouldn't have to do this, the process should handle it for me:
183     Process *process = exe_ctx.GetProcessPtr();
184     if (!process->GetDynamicCheckers())
185     {
186         DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
187 
188         StreamString install_errors;
189 
190         if (!dynamic_checkers->Install(install_errors, exe_ctx))
191         {
192             error_stream.Printf("Couldn't install dynamic checkers into the execution context: %s\n", install_errors.GetData());
193             return NULL;
194         }
195 
196         process->SetDynamicCheckers(dynamic_checkers);
197     }
198 
199     const bool keep_in_memory = false;
200 
201     if (!m_condition_ap->Parse (error_stream, exe_ctx, eExecutionPolicyAlways, keep_in_memory))
202     {
203         // Errors mean we should stop.
204         return NULL;
205     }
206     // FIXME: When we can execute static expressions without running the target, we should check that here,
207     // and return something to indicate we should stop or just continue.
208 
209     ThreadPlan *new_plan = new ThreadPlanTestCondition (exe_ctx.GetThreadRef(),
210                                                         exe_ctx,
211                                                         m_condition_ap.get(),
212                                                         break_loc_sp,
213                                                         true);
214 
215     return new_plan;
216 }
217 
218 const char *
219 BreakpointOptions::GetConditionText () const
220 {
221     if (m_condition_ap.get())
222         return m_condition_ap->GetUserText();
223     else
224         return NULL;
225 }
226 
227 //------------------------------------------------------------------
228 // Enabled/Ignore Count
229 //------------------------------------------------------------------
230 bool
231 BreakpointOptions::IsEnabled () const
232 {
233     return m_enabled;
234 }
235 
236 void
237 BreakpointOptions::SetEnabled (bool enabled)
238 {
239     m_enabled = enabled;
240 }
241 
242 uint32_t
243 BreakpointOptions::GetIgnoreCount () const
244 {
245     return m_ignore_count;
246 }
247 
248 void
249 BreakpointOptions::SetIgnoreCount (uint32_t n)
250 {
251     m_ignore_count = n;
252 }
253 
254 const ThreadSpec *
255 BreakpointOptions::GetThreadSpecNoCreate () const
256 {
257     return m_thread_spec_ap.get();
258 }
259 
260 ThreadSpec *
261 BreakpointOptions::GetThreadSpec ()
262 {
263     if (m_thread_spec_ap.get() == NULL)
264         m_thread_spec_ap.reset (new ThreadSpec());
265 
266     return m_thread_spec_ap.get();
267 }
268 
269 void
270 BreakpointOptions::SetThreadID (lldb::tid_t thread_id)
271 {
272     GetThreadSpec()->SetTID(thread_id);
273 }
274 
275 void
276 BreakpointOptions::GetDescription (Stream *s, lldb::DescriptionLevel level) const
277 {
278 
279     // Figure out if there are any options not at their default value, and only print
280     // anything if there are:
281 
282     if (m_ignore_count != 0 || !m_enabled || (GetThreadSpecNoCreate() != NULL && GetThreadSpecNoCreate()->HasSpecification ()))
283     {
284         if (level == lldb::eDescriptionLevelVerbose)
285         {
286             s->EOL ();
287             s->IndentMore();
288             s->Indent();
289             s->PutCString("Breakpoint Options:\n");
290             s->IndentMore();
291             s->Indent();
292         }
293         else
294             s->PutCString(" Options: ");
295 
296         if (m_ignore_count > 0)
297             s->Printf("ignore: %d ", m_ignore_count);
298         s->Printf("%sabled ", m_enabled ? "en" : "dis");
299 
300         if (m_thread_spec_ap.get())
301             m_thread_spec_ap->GetDescription (s, level);
302         else if (level == eDescriptionLevelBrief)
303             s->PutCString ("thread spec: no ");
304         if (level == lldb::eDescriptionLevelFull)
305         {
306             s->IndentLess();
307             s->IndentMore();
308         }
309     }
310 
311     if (m_callback_baton_sp.get())
312     {
313         if (level != eDescriptionLevelBrief)
314         {
315             s->EOL();
316             m_callback_baton_sp->GetDescription (s, level);
317         }
318     }
319     if (m_condition_ap.get())
320     {
321        if (level != eDescriptionLevelBrief)
322        {
323             s->EOL();
324             s->Printf("Condition: %s\n", m_condition_ap->GetUserText());
325         }
326     }
327 }
328 
329 void
330 BreakpointOptions::CommandBaton::GetDescription (Stream *s, lldb::DescriptionLevel level) const
331 {
332     CommandData *data = (CommandData *)m_data;
333 
334     if (level == eDescriptionLevelBrief)
335     {
336         s->Printf (", commands = %s", (data && data->user_source.GetSize() > 0) ? "yes" : "no");
337         return;
338     }
339 
340     s->IndentMore ();
341     s->Indent("Breakpoint commands:\n");
342 
343     s->IndentMore ();
344     if (data && data->user_source.GetSize() > 0)
345     {
346         const size_t num_strings = data->user_source.GetSize();
347         for (size_t i = 0; i < num_strings; ++i)
348         {
349             s->Indent(data->user_source.GetStringAtIndex(i));
350             s->EOL();
351         }
352     }
353     else
354     {
355         s->PutCString ("No commands.\n");
356     }
357     s->IndentLess ();
358     s->IndentLess ();
359 }
360 
361