1 //===-- BreakpointOptions.cpp -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Breakpoint/BreakpointOptions.h"
10 
11 #include "lldb/Breakpoint/StoppointCallbackContext.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/ThreadSpec.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StringList.h"
20 
21 #include "llvm/ADT/STLExtras.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 const char
27     *BreakpointOptions::CommandData::g_option_names[static_cast<uint32_t>(
28         BreakpointOptions::CommandData::OptionNames::LastOptionName)]{
29         "UserSource", "ScriptSource", "StopOnError"};
30 
31 StructuredData::ObjectSP
32 BreakpointOptions::CommandData::SerializeToStructuredData() {
33   size_t num_strings = user_source.GetSize();
34   if (num_strings == 0 && script_source.empty()) {
35     // We shouldn't serialize commands if there aren't any, return an empty sp
36     // to indicate this.
37     return StructuredData::ObjectSP();
38   }
39 
40   StructuredData::DictionarySP options_dict_sp(
41       new StructuredData::Dictionary());
42   options_dict_sp->AddBooleanItem(GetKey(OptionNames::StopOnError),
43                                   stop_on_error);
44 
45   StructuredData::ArraySP user_source_sp(new StructuredData::Array());
46   for (size_t i = 0; i < num_strings; i++) {
47     StructuredData::StringSP item_sp(
48         new StructuredData::String(user_source[i]));
49     user_source_sp->AddItem(item_sp);
50     options_dict_sp->AddItem(GetKey(OptionNames::UserSource), user_source_sp);
51   }
52 
53   options_dict_sp->AddStringItem(
54       GetKey(OptionNames::Interpreter),
55       ScriptInterpreter::LanguageToString(interpreter));
56   return options_dict_sp;
57 }
58 
59 std::unique_ptr<BreakpointOptions::CommandData>
60 BreakpointOptions::CommandData::CreateFromStructuredData(
61     const StructuredData::Dictionary &options_dict, Status &error) {
62   std::unique_ptr<CommandData> data_up(new CommandData());
63   bool found_something = false;
64 
65   bool success = options_dict.GetValueForKeyAsBoolean(
66       GetKey(OptionNames::StopOnError), data_up->stop_on_error);
67 
68   if (success)
69     found_something = true;
70 
71   llvm::StringRef interpreter_str;
72   ScriptLanguage interp_language;
73   success = options_dict.GetValueForKeyAsString(
74       GetKey(OptionNames::Interpreter), interpreter_str);
75 
76   if (!success) {
77     error.SetErrorString("Missing command language value.");
78     return data_up;
79   }
80 
81   found_something = true;
82   interp_language = ScriptInterpreter::StringToLanguage(interpreter_str);
83   if (interp_language == eScriptLanguageUnknown) {
84     error.SetErrorStringWithFormatv("Unknown breakpoint command language: {0}.",
85                                     interpreter_str);
86     return data_up;
87   }
88   data_up->interpreter = interp_language;
89 
90   StructuredData::Array *user_source;
91   success = options_dict.GetValueForKeyAsArray(GetKey(OptionNames::UserSource),
92                                                user_source);
93   if (success) {
94     found_something = true;
95     size_t num_elems = user_source->GetSize();
96     for (size_t i = 0; i < num_elems; i++) {
97       llvm::StringRef elem_string;
98       success = user_source->GetItemAtIndexAsString(i, elem_string);
99       if (success)
100         data_up->user_source.AppendString(elem_string);
101     }
102   }
103 
104   if (found_something)
105     return data_up;
106   else
107     return std::unique_ptr<BreakpointOptions::CommandData>();
108 }
109 
110 const char *BreakpointOptions::g_option_names[(
111     size_t)BreakpointOptions::OptionNames::LastOptionName]{
112     "ConditionText", "IgnoreCount",
113     "EnabledState", "OneShotState", "AutoContinue"};
114 
115 bool BreakpointOptions::NullCallback(void *baton,
116                                      StoppointCallbackContext *context,
117                                      lldb::user_id_t break_id,
118                                      lldb::user_id_t break_loc_id) {
119   return true;
120 }
121 
122 //----------------------------------------------------------------------
123 // BreakpointOptions constructor
124 //----------------------------------------------------------------------
125 BreakpointOptions::BreakpointOptions(bool all_flags_set)
126     : m_callback(BreakpointOptions::NullCallback), m_callback_baton_sp(),
127       m_baton_is_command_baton(false), m_callback_is_synchronous(false),
128       m_enabled(true), m_one_shot(false), m_ignore_count(0), m_thread_spec_up(),
129       m_condition_text(), m_condition_text_hash(0), m_auto_continue(false),
130       m_set_flags(0) {
131   if (all_flags_set)
132     m_set_flags.Set(~((Flags::ValueType)0));
133 }
134 
135 BreakpointOptions::BreakpointOptions(const char *condition, bool enabled,
136                                      int32_t ignore, bool one_shot,
137                                      bool auto_continue)
138     : m_callback(nullptr), m_baton_is_command_baton(false),
139       m_callback_is_synchronous(false), m_enabled(enabled),
140       m_one_shot(one_shot), m_ignore_count(ignore),
141       m_condition_text_hash(0), m_auto_continue(auto_continue)
142 {
143     m_set_flags.Set(eEnabled | eIgnoreCount | eOneShot
144                    | eAutoContinue);
145     if (condition && *condition != '\0') {
146       SetCondition(condition);
147     }
148 }
149 
150 //----------------------------------------------------------------------
151 // BreakpointOptions copy constructor
152 //----------------------------------------------------------------------
153 BreakpointOptions::BreakpointOptions(const BreakpointOptions &rhs)
154     : m_callback(rhs.m_callback), m_callback_baton_sp(rhs.m_callback_baton_sp),
155       m_baton_is_command_baton(rhs.m_baton_is_command_baton),
156       m_callback_is_synchronous(rhs.m_callback_is_synchronous),
157       m_enabled(rhs.m_enabled), m_one_shot(rhs.m_one_shot),
158       m_ignore_count(rhs.m_ignore_count), m_thread_spec_up(),
159       m_auto_continue(rhs.m_auto_continue), m_set_flags(rhs.m_set_flags) {
160   if (rhs.m_thread_spec_up != nullptr)
161     m_thread_spec_up.reset(new ThreadSpec(*rhs.m_thread_spec_up));
162   m_condition_text = rhs.m_condition_text;
163   m_condition_text_hash = rhs.m_condition_text_hash;
164 }
165 
166 //----------------------------------------------------------------------
167 // BreakpointOptions assignment operator
168 //----------------------------------------------------------------------
169 const BreakpointOptions &BreakpointOptions::
170 operator=(const BreakpointOptions &rhs) {
171   m_callback = rhs.m_callback;
172   m_callback_baton_sp = rhs.m_callback_baton_sp;
173   m_baton_is_command_baton = rhs.m_baton_is_command_baton;
174   m_callback_is_synchronous = rhs.m_callback_is_synchronous;
175   m_enabled = rhs.m_enabled;
176   m_one_shot = rhs.m_one_shot;
177   m_ignore_count = rhs.m_ignore_count;
178   if (rhs.m_thread_spec_up != nullptr)
179     m_thread_spec_up.reset(new ThreadSpec(*rhs.m_thread_spec_up));
180   m_condition_text = rhs.m_condition_text;
181   m_condition_text_hash = rhs.m_condition_text_hash;
182   m_auto_continue = rhs.m_auto_continue;
183   m_set_flags = rhs.m_set_flags;
184   return *this;
185 }
186 
187 void BreakpointOptions::CopyOverSetOptions(const BreakpointOptions &incoming)
188 {
189   if (incoming.m_set_flags.Test(eEnabled))
190   {
191     m_enabled = incoming.m_enabled;
192     m_set_flags.Set(eEnabled);
193   }
194   if (incoming.m_set_flags.Test(eOneShot))
195   {
196     m_one_shot = incoming.m_one_shot;
197     m_set_flags.Set(eOneShot);
198   }
199   if (incoming.m_set_flags.Test(eCallback))
200   {
201     m_callback = incoming.m_callback;
202     m_callback_baton_sp = incoming.m_callback_baton_sp;
203     m_callback_is_synchronous = incoming.m_callback_is_synchronous;
204     m_baton_is_command_baton = incoming.m_baton_is_command_baton;
205     m_set_flags.Set(eCallback);
206   }
207   if (incoming.m_set_flags.Test(eIgnoreCount))
208   {
209     m_ignore_count = incoming.m_ignore_count;
210     m_set_flags.Set(eIgnoreCount);
211   }
212   if (incoming.m_set_flags.Test(eCondition))
213   {
214     // If we're copying over an empty condition, mark it as unset.
215     if (incoming.m_condition_text.empty()) {
216       m_condition_text.clear();
217       m_condition_text_hash = 0;
218       m_set_flags.Clear(eCondition);
219     } else {
220       m_condition_text = incoming.m_condition_text;
221       m_condition_text_hash = incoming.m_condition_text_hash;
222       m_set_flags.Set(eCondition);
223     }
224   }
225   if (incoming.m_set_flags.Test(eAutoContinue))
226   {
227     m_auto_continue = incoming.m_auto_continue;
228     m_set_flags.Set(eAutoContinue);
229   }
230   if (incoming.m_set_flags.Test(eThreadSpec) && incoming.m_thread_spec_up) {
231     if (!m_thread_spec_up)
232       m_thread_spec_up.reset(new ThreadSpec(*incoming.m_thread_spec_up));
233     else
234       *m_thread_spec_up = *incoming.m_thread_spec_up;
235     m_set_flags.Set(eThreadSpec);
236   }
237 }
238 
239 //----------------------------------------------------------------------
240 // Destructor
241 //----------------------------------------------------------------------
242 BreakpointOptions::~BreakpointOptions() = default;
243 
244 std::unique_ptr<BreakpointOptions> BreakpointOptions::CreateFromStructuredData(
245     Target &target, const StructuredData::Dictionary &options_dict,
246     Status &error) {
247   bool enabled = true;
248   bool one_shot = false;
249   bool auto_continue = false;
250   int32_t ignore_count = 0;
251   llvm::StringRef condition_ref("");
252   Flags set_options;
253 
254   const char *key = GetKey(OptionNames::EnabledState);
255   bool success;
256   if (key && options_dict.HasKey(key)) {
257     success = options_dict.GetValueForKeyAsBoolean(key, enabled);
258     if (!success) {
259       error.SetErrorStringWithFormat("%s key is not a boolean.", key);
260       return nullptr;
261     }
262     set_options.Set(eEnabled);
263   }
264 
265   key = GetKey(OptionNames::OneShotState);
266   if (key && options_dict.HasKey(key)) {
267     success = options_dict.GetValueForKeyAsBoolean(key, one_shot);
268     if (!success) {
269       error.SetErrorStringWithFormat("%s key is not a boolean.", key);
270       return nullptr;
271       }
272       set_options.Set(eOneShot);
273   }
274 
275   key = GetKey(OptionNames::AutoContinue);
276   if (key && options_dict.HasKey(key)) {
277     success = options_dict.GetValueForKeyAsBoolean(key, auto_continue);
278     if (!success) {
279       error.SetErrorStringWithFormat("%s key is not a boolean.", key);
280       return nullptr;
281       }
282       set_options.Set(eAutoContinue);
283   }
284 
285   key = GetKey(OptionNames::IgnoreCount);
286   if (key && options_dict.HasKey(key)) {
287     success = options_dict.GetValueForKeyAsInteger(key, ignore_count);
288     if (!success) {
289       error.SetErrorStringWithFormat("%s key is not an integer.", key);
290       return nullptr;
291     }
292     set_options.Set(eIgnoreCount);
293   }
294 
295   key = GetKey(OptionNames::ConditionText);
296   if (key && options_dict.HasKey(key)) {
297     success = options_dict.GetValueForKeyAsString(key, condition_ref);
298     if (!success) {
299       error.SetErrorStringWithFormat("%s key is not an string.", key);
300       return nullptr;
301     }
302     set_options.Set(eCondition);
303   }
304 
305   std::unique_ptr<CommandData> cmd_data_up;
306   StructuredData::Dictionary *cmds_dict;
307   success = options_dict.GetValueForKeyAsDictionary(
308       CommandData::GetSerializationKey(), cmds_dict);
309   if (success && cmds_dict) {
310     Status cmds_error;
311     cmd_data_up = CommandData::CreateFromStructuredData(*cmds_dict, cmds_error);
312     if (cmds_error.Fail()) {
313       error.SetErrorStringWithFormat(
314           "Failed to deserialize breakpoint command options: %s.",
315           cmds_error.AsCString());
316       return nullptr;
317     }
318   }
319 
320   auto bp_options = llvm::make_unique<BreakpointOptions>(
321       condition_ref.str().c_str(), enabled,
322       ignore_count, one_shot, auto_continue);
323   if (cmd_data_up) {
324     if (cmd_data_up->interpreter == eScriptLanguageNone)
325       bp_options->SetCommandDataCallback(cmd_data_up);
326     else {
327       ScriptInterpreter *interp =
328           target.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
329       if (!interp) {
330         error.SetErrorStringWithFormat(
331             "Can't set script commands - no script interpreter");
332         return nullptr;
333       }
334       if (interp->GetLanguage() != cmd_data_up->interpreter) {
335         error.SetErrorStringWithFormat(
336             "Current script language doesn't match breakpoint's language: %s",
337             ScriptInterpreter::LanguageToString(cmd_data_up->interpreter)
338                 .c_str());
339         return nullptr;
340       }
341       Status script_error;
342       script_error =
343           interp->SetBreakpointCommandCallback(bp_options.get(), cmd_data_up);
344       if (script_error.Fail()) {
345         error.SetErrorStringWithFormat("Error generating script callback: %s.",
346                                        error.AsCString());
347         return nullptr;
348       }
349     }
350   }
351 
352   StructuredData::Dictionary *thread_spec_dict;
353   success = options_dict.GetValueForKeyAsDictionary(
354       ThreadSpec::GetSerializationKey(), thread_spec_dict);
355   if (success) {
356     Status thread_spec_error;
357     std::unique_ptr<ThreadSpec> thread_spec_up =
358         ThreadSpec::CreateFromStructuredData(*thread_spec_dict,
359                                              thread_spec_error);
360     if (thread_spec_error.Fail()) {
361       error.SetErrorStringWithFormat(
362           "Failed to deserialize breakpoint thread spec options: %s.",
363           thread_spec_error.AsCString());
364       return nullptr;
365     }
366     bp_options->SetThreadSpec(thread_spec_up);
367   }
368   return bp_options;
369 }
370 
371 StructuredData::ObjectSP BreakpointOptions::SerializeToStructuredData() {
372   StructuredData::DictionarySP options_dict_sp(
373       new StructuredData::Dictionary());
374   if (m_set_flags.Test(eEnabled))
375     options_dict_sp->AddBooleanItem(GetKey(OptionNames::EnabledState),
376                                     m_enabled);
377   if (m_set_flags.Test(eOneShot))
378     options_dict_sp->AddBooleanItem(GetKey(OptionNames::OneShotState),
379                                m_one_shot);
380   if (m_set_flags.Test(eAutoContinue))
381     options_dict_sp->AddBooleanItem(GetKey(OptionNames::AutoContinue),
382                                m_auto_continue);
383   if (m_set_flags.Test(eIgnoreCount))
384     options_dict_sp->AddIntegerItem(GetKey(OptionNames::IgnoreCount),
385                                     m_ignore_count);
386   if (m_set_flags.Test(eCondition))
387     options_dict_sp->AddStringItem(GetKey(OptionNames::ConditionText),
388                                    m_condition_text);
389 
390   if (m_set_flags.Test(eCallback) && m_baton_is_command_baton) {
391     auto cmd_baton =
392         std::static_pointer_cast<CommandBaton>(m_callback_baton_sp);
393     StructuredData::ObjectSP commands_sp =
394         cmd_baton->getItem()->SerializeToStructuredData();
395     if (commands_sp) {
396       options_dict_sp->AddItem(
397           BreakpointOptions::CommandData::GetSerializationKey(), commands_sp);
398     }
399   }
400   if (m_set_flags.Test(eThreadSpec) && m_thread_spec_up) {
401     StructuredData::ObjectSP thread_spec_sp =
402         m_thread_spec_up->SerializeToStructuredData();
403     options_dict_sp->AddItem(ThreadSpec::GetSerializationKey(), thread_spec_sp);
404   }
405 
406   return options_dict_sp;
407 }
408 
409 //------------------------------------------------------------------
410 // Callbacks
411 //------------------------------------------------------------------
412 void BreakpointOptions::SetCallback(BreakpointHitCallback callback,
413                                     const lldb::BatonSP &callback_baton_sp,
414                                     bool callback_is_synchronous) {
415   // FIXME: This seems unsafe.  If BatonSP actually *is* a CommandBaton, but
416   // in a shared_ptr<Baton> instead of a shared_ptr<CommandBaton>, then we will
417   // set m_baton_is_command_baton to false, which is incorrect. One possible
418   // solution is to make the base Baton class provide a method such as:
419   //     virtual StringRef getBatonId() const { return ""; }
420   // and have CommandBaton override this to return something unique, and then
421   // check for it here.  Another option might be to make Baton using the llvm
422   // casting infrastructure, so that we could write something like:
423   //     if (llvm::isa<CommandBaton>(callback_baton_sp))
424   // at relevant callsites instead of storing a boolean.
425   m_callback_is_synchronous = callback_is_synchronous;
426   m_callback = callback;
427   m_callback_baton_sp = callback_baton_sp;
428   m_baton_is_command_baton = false;
429   m_set_flags.Set(eCallback);
430 }
431 
432 void BreakpointOptions::SetCallback(
433     BreakpointHitCallback callback,
434     const BreakpointOptions::CommandBatonSP &callback_baton_sp,
435     bool callback_is_synchronous) {
436   m_callback_is_synchronous = callback_is_synchronous;
437   m_callback = callback;
438   m_callback_baton_sp = callback_baton_sp;
439   m_baton_is_command_baton = true;
440   m_set_flags.Set(eCallback);
441 }
442 
443 void BreakpointOptions::ClearCallback() {
444   m_callback = BreakpointOptions::NullCallback;
445   m_callback_is_synchronous = false;
446   m_callback_baton_sp.reset();
447   m_baton_is_command_baton = false;
448   m_set_flags.Clear(eCallback);
449 }
450 
451 Baton *BreakpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
452 
453 const Baton *BreakpointOptions::GetBaton() const {
454   return m_callback_baton_sp.get();
455 }
456 
457 bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
458                                        lldb::user_id_t break_id,
459                                        lldb::user_id_t break_loc_id) {
460   if (m_callback) {
461     if (context->is_synchronous == IsCallbackSynchronous()) {
462         return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
463                                           : nullptr,
464                       context, break_id, break_loc_id);
465     } else if (IsCallbackSynchronous()) {
466       // If a synchronous callback is called at async time, it should not say
467       // to stop.
468       return false;
469     }
470   }
471   return true;
472 }
473 
474 bool BreakpointOptions::HasCallback() const {
475   return m_callback != BreakpointOptions::NullCallback;
476 }
477 
478 bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) {
479   if (!HasCallback())
480     return false;
481   if (!m_baton_is_command_baton)
482     return false;
483 
484   auto cmd_baton = std::static_pointer_cast<CommandBaton>(m_callback_baton_sp);
485   CommandData *data = cmd_baton->getItem();
486   if (!data)
487     return false;
488   command_list = data->user_source;
489   return true;
490 }
491 
492 void BreakpointOptions::SetCondition(const char *condition) {
493   if (!condition || condition[0] == '\0') {
494     condition = "";
495     m_set_flags.Clear(eCondition);
496   }
497   else
498     m_set_flags.Set(eCondition);
499 
500   m_condition_text.assign(condition);
501   std::hash<std::string> hasher;
502   m_condition_text_hash = hasher(m_condition_text);
503 }
504 
505 const char *BreakpointOptions::GetConditionText(size_t *hash) const {
506   if (!m_condition_text.empty()) {
507     if (hash)
508       *hash = m_condition_text_hash;
509 
510     return m_condition_text.c_str();
511   } else {
512     return nullptr;
513   }
514 }
515 
516 const ThreadSpec *BreakpointOptions::GetThreadSpecNoCreate() const {
517   return m_thread_spec_up.get();
518 }
519 
520 ThreadSpec *BreakpointOptions::GetThreadSpec() {
521   if (m_thread_spec_up == nullptr) {
522     m_set_flags.Set(eThreadSpec);
523     m_thread_spec_up.reset(new ThreadSpec());
524   }
525 
526   return m_thread_spec_up.get();
527 }
528 
529 void BreakpointOptions::SetThreadID(lldb::tid_t thread_id) {
530   GetThreadSpec()->SetTID(thread_id);
531   m_set_flags.Set(eThreadSpec);
532 }
533 
534 void BreakpointOptions::SetThreadSpec(
535     std::unique_ptr<ThreadSpec> &thread_spec_up) {
536   m_thread_spec_up = std::move(thread_spec_up);
537   m_set_flags.Set(eThreadSpec);
538 }
539 
540 void BreakpointOptions::GetDescription(Stream *s,
541                                        lldb::DescriptionLevel level) const {
542   // Figure out if there are any options not at their default value, and only
543   // print anything if there are:
544 
545   if (m_ignore_count != 0 || !m_enabled || m_one_shot || m_auto_continue ||
546       (GetThreadSpecNoCreate() != nullptr &&
547        GetThreadSpecNoCreate()->HasSpecification())) {
548     if (level == lldb::eDescriptionLevelVerbose) {
549       s->EOL();
550       s->IndentMore();
551       s->Indent();
552       s->PutCString("Breakpoint Options:\n");
553       s->IndentMore();
554       s->Indent();
555     } else
556       s->PutCString(" Options: ");
557 
558     if (m_ignore_count > 0)
559       s->Printf("ignore: %d ", m_ignore_count);
560     s->Printf("%sabled ", m_enabled ? "en" : "dis");
561 
562     if (m_one_shot)
563       s->Printf("one-shot ");
564 
565     if (m_auto_continue)
566       s->Printf("auto-continue ");
567 
568     if (m_thread_spec_up)
569       m_thread_spec_up->GetDescription(s, level);
570 
571     if (level == lldb::eDescriptionLevelFull) {
572       s->IndentLess();
573       s->IndentMore();
574     }
575   }
576 
577   if (m_callback_baton_sp.get()) {
578     if (level != eDescriptionLevelBrief) {
579       s->EOL();
580       m_callback_baton_sp->GetDescription(s, level);
581     }
582   }
583   if (!m_condition_text.empty()) {
584     if (level != eDescriptionLevelBrief) {
585       s->EOL();
586       s->Printf("Condition: %s\n", m_condition_text.c_str());
587     }
588   }
589 }
590 
591 void BreakpointOptions::CommandBaton::GetDescription(
592     Stream *s, lldb::DescriptionLevel level) const {
593   const CommandData *data = getItem();
594 
595   if (level == eDescriptionLevelBrief) {
596     s->Printf(", commands = %s",
597               (data && data->user_source.GetSize() > 0) ? "yes" : "no");
598     return;
599   }
600 
601   s->IndentMore();
602   s->Indent("Breakpoint commands");
603   if (data->interpreter != eScriptLanguageNone)
604     s->Printf(" (%s):\n",
605               ScriptInterpreter::LanguageToString(data->interpreter).c_str());
606   else
607     s->PutCString(":\n");
608 
609   s->IndentMore();
610   if (data && data->user_source.GetSize() > 0) {
611     const size_t num_strings = data->user_source.GetSize();
612     for (size_t i = 0; i < num_strings; ++i) {
613       s->Indent(data->user_source.GetStringAtIndex(i));
614       s->EOL();
615     }
616   } else {
617     s->PutCString("No commands.\n");
618   }
619   s->IndentLess();
620   s->IndentLess();
621 }
622 
623 void BreakpointOptions::SetCommandDataCallback(
624     std::unique_ptr<CommandData> &cmd_data) {
625   cmd_data->interpreter = eScriptLanguageNone;
626   auto baton_sp = std::make_shared<CommandBaton>(std::move(cmd_data));
627   SetCallback(BreakpointOptions::BreakpointOptionsCallbackFunction, baton_sp);
628   m_set_flags.Set(eCallback);
629 }
630 
631 bool BreakpointOptions::BreakpointOptionsCallbackFunction(
632     void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
633     lldb::user_id_t break_loc_id) {
634   bool ret_value = true;
635   if (baton == nullptr)
636     return true;
637 
638   CommandData *data = (CommandData *)baton;
639   StringList &commands = data->user_source;
640 
641   if (commands.GetSize() > 0) {
642     ExecutionContext exe_ctx(context->exe_ctx_ref);
643     Target *target = exe_ctx.GetTargetPtr();
644     if (target) {
645       CommandReturnObject result;
646       Debugger &debugger = target->GetDebugger();
647       // Rig up the results secondary output stream to the debugger's, so the
648       // output will come out synchronously if the debugger is set up that way.
649 
650       StreamSP output_stream(debugger.GetAsyncOutputStream());
651       StreamSP error_stream(debugger.GetAsyncErrorStream());
652       result.SetImmediateOutputStream(output_stream);
653       result.SetImmediateErrorStream(error_stream);
654 
655       CommandInterpreterRunOptions options;
656       options.SetStopOnContinue(true);
657       options.SetStopOnError(data->stop_on_error);
658       options.SetEchoCommands(true);
659       options.SetPrintResults(true);
660       options.SetAddToHistory(false);
661 
662       debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
663                                                       options, result);
664       result.GetImmediateOutputStream()->Flush();
665       result.GetImmediateErrorStream()->Flush();
666     }
667   }
668   return ret_value;
669 }
670 
671 void BreakpointOptions::Clear()
672 {
673   m_set_flags.Clear();
674   m_thread_spec_up.release();
675   m_one_shot = false;
676   m_ignore_count = 0;
677   m_auto_continue = false;
678   m_callback = nullptr;
679   m_callback_baton_sp.reset();
680   m_baton_is_command_baton = false;
681   m_callback_is_synchronous = false;
682   m_enabled = false;
683   m_condition_text.clear();
684 }
685