1 //===-- BreakpointOptions.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Breakpoint/BreakpointOptions.h"
16 #include "lldb/Breakpoint/StoppointCallbackContext.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Interpreter/CommandReturnObject.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/ThreadSpec.h"
23 #include "lldb/Utility/Stream.h"
24 #include "lldb/Utility/StringList.h"
26 #include "llvm/ADT/STLExtras.h"
29 using namespace lldb_private;
32 *BreakpointOptions::CommandData::g_option_names[static_cast<uint32_t>(
33 BreakpointOptions::CommandData::OptionNames::LastOptionName)]{
34 "UserSource", "ScriptSource", "StopOnError"};
36 StructuredData::ObjectSP
37 BreakpointOptions::CommandData::SerializeToStructuredData() {
38 size_t num_strings = user_source.GetSize();
39 if (num_strings == 0 && script_source.empty()) {
40 // We shouldn't serialize commands if there aren't any, return an empty sp
42 return StructuredData::ObjectSP();
45 StructuredData::DictionarySP options_dict_sp(
46 new StructuredData::Dictionary());
47 options_dict_sp->AddBooleanItem(GetKey(OptionNames::StopOnError),
50 StructuredData::ArraySP user_source_sp(new StructuredData::Array());
51 for (size_t i = 0; i < num_strings; i++) {
52 StructuredData::StringSP item_sp(
53 new StructuredData::String(user_source[i]));
54 user_source_sp->AddItem(item_sp);
55 options_dict_sp->AddItem(GetKey(OptionNames::UserSource), user_source_sp);
58 options_dict_sp->AddStringItem(
59 GetKey(OptionNames::Interpreter),
60 ScriptInterpreter::LanguageToString(interpreter));
61 return options_dict_sp;
64 std::unique_ptr<BreakpointOptions::CommandData>
65 BreakpointOptions::CommandData::CreateFromStructuredData(
66 const StructuredData::Dictionary &options_dict, Status &error) {
67 std::unique_ptr<CommandData> data_up(new CommandData());
68 bool found_something = false;
70 bool success = options_dict.GetValueForKeyAsBoolean(
71 GetKey(OptionNames::StopOnError), data_up->stop_on_error);
74 found_something = true;
76 llvm::StringRef interpreter_str;
77 ScriptLanguage interp_language;
78 success = options_dict.GetValueForKeyAsString(
79 GetKey(OptionNames::Interpreter), interpreter_str);
82 error.SetErrorString("Missing command language value.");
86 found_something = true;
87 interp_language = ScriptInterpreter::StringToLanguage(interpreter_str);
88 if (interp_language == eScriptLanguageUnknown) {
89 error.SetErrorStringWithFormatv("Unknown breakpoint command language: {0}.",
93 data_up->interpreter = interp_language;
95 StructuredData::Array *user_source;
96 success = options_dict.GetValueForKeyAsArray(GetKey(OptionNames::UserSource),
99 found_something = true;
100 size_t num_elems = user_source->GetSize();
101 for (size_t i = 0; i < num_elems; i++) {
102 llvm::StringRef elem_string;
103 success = user_source->GetItemAtIndexAsString(i, elem_string);
105 data_up->user_source.AppendString(elem_string);
112 return std::unique_ptr<BreakpointOptions::CommandData>();
115 const char *BreakpointOptions::g_option_names[(
116 size_t)BreakpointOptions::OptionNames::LastOptionName]{
117 "ConditionText", "IgnoreCount",
118 "EnabledState", "OneShotState", "AutoContinue"};
120 bool BreakpointOptions::NullCallback(void *baton,
121 StoppointCallbackContext *context,
122 lldb::user_id_t break_id,
123 lldb::user_id_t break_loc_id) {
127 //----------------------------------------------------------------------
128 // BreakpointOptions constructor
129 //----------------------------------------------------------------------
130 BreakpointOptions::BreakpointOptions(bool all_flags_set)
131 : m_callback(BreakpointOptions::NullCallback), m_callback_baton_sp(),
132 m_baton_is_command_baton(false), m_callback_is_synchronous(false),
133 m_enabled(true), m_one_shot(false), m_ignore_count(0), m_thread_spec_ap(),
134 m_condition_text(), m_condition_text_hash(0), m_auto_continue(false),
137 m_set_flags.Set(~((Flags::ValueType) 0));
140 BreakpointOptions::BreakpointOptions(const char *condition, bool enabled,
141 int32_t ignore, bool one_shot,
143 : m_callback(nullptr), m_baton_is_command_baton(false),
144 m_callback_is_synchronous(false), m_enabled(enabled),
145 m_one_shot(one_shot), m_ignore_count(ignore),
146 m_condition_text_hash(0), m_auto_continue(auto_continue)
148 m_set_flags.Set(eEnabled | eIgnoreCount | eOneShot
150 if (condition && *condition != '\0') {
151 SetCondition(condition);
155 //----------------------------------------------------------------------
156 // BreakpointOptions copy constructor
157 //----------------------------------------------------------------------
158 BreakpointOptions::BreakpointOptions(const BreakpointOptions &rhs)
159 : m_callback(rhs.m_callback), m_callback_baton_sp(rhs.m_callback_baton_sp),
160 m_baton_is_command_baton(rhs.m_baton_is_command_baton),
161 m_callback_is_synchronous(rhs.m_callback_is_synchronous),
162 m_enabled(rhs.m_enabled), m_one_shot(rhs.m_one_shot),
163 m_ignore_count(rhs.m_ignore_count), m_thread_spec_ap(),
164 m_auto_continue(rhs.m_auto_continue),
165 m_set_flags(rhs.m_set_flags) {
166 if (rhs.m_thread_spec_ap.get() != nullptr)
167 m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
168 m_condition_text = rhs.m_condition_text;
169 m_condition_text_hash = rhs.m_condition_text_hash;
172 //----------------------------------------------------------------------
173 // BreakpointOptions assignment operator
174 //----------------------------------------------------------------------
175 const BreakpointOptions &BreakpointOptions::
176 operator=(const BreakpointOptions &rhs) {
177 m_callback = rhs.m_callback;
178 m_callback_baton_sp = rhs.m_callback_baton_sp;
179 m_baton_is_command_baton = rhs.m_baton_is_command_baton;
180 m_callback_is_synchronous = rhs.m_callback_is_synchronous;
181 m_enabled = rhs.m_enabled;
182 m_one_shot = rhs.m_one_shot;
183 m_ignore_count = rhs.m_ignore_count;
184 if (rhs.m_thread_spec_ap.get() != nullptr)
185 m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
186 m_condition_text = rhs.m_condition_text;
187 m_condition_text_hash = rhs.m_condition_text_hash;
188 m_auto_continue = rhs.m_auto_continue;
189 m_set_flags = rhs.m_set_flags;
193 void BreakpointOptions::CopyOverSetOptions(const BreakpointOptions &incoming)
195 if (incoming.m_set_flags.Test(eEnabled))
197 m_enabled = incoming.m_enabled;
198 m_set_flags.Set(eEnabled);
200 if (incoming.m_set_flags.Test(eOneShot))
202 m_one_shot = incoming.m_one_shot;
203 m_set_flags.Set(eOneShot);
205 if (incoming.m_set_flags.Test(eCallback))
207 m_callback = incoming.m_callback;
208 m_callback_baton_sp = incoming.m_callback_baton_sp;
209 m_callback_is_synchronous = incoming.m_callback_is_synchronous;
210 m_baton_is_command_baton = incoming.m_baton_is_command_baton;
211 m_set_flags.Set(eCallback);
213 if (incoming.m_set_flags.Test(eIgnoreCount))
215 m_ignore_count = incoming.m_ignore_count;
216 m_set_flags.Set(eIgnoreCount);
218 if (incoming.m_set_flags.Test(eCondition))
220 // If we're copying over an empty condition, mark it as unset.
221 if (incoming.m_condition_text.empty()) {
222 m_condition_text.clear();
223 m_condition_text_hash = 0;
224 m_set_flags.Clear(eCondition);
226 m_condition_text = incoming.m_condition_text;
227 m_condition_text_hash = incoming.m_condition_text_hash;
228 m_set_flags.Set(eCondition);
231 if (incoming.m_set_flags.Test(eAutoContinue))
233 m_auto_continue = incoming.m_auto_continue;
234 m_set_flags.Set(eAutoContinue);
236 if (incoming.m_set_flags.Test(eThreadSpec) && incoming.m_thread_spec_ap)
238 if (!m_thread_spec_ap)
239 m_thread_spec_ap.reset(new ThreadSpec(*incoming.m_thread_spec_ap.get()));
241 *m_thread_spec_ap.get() = *incoming.m_thread_spec_ap.get();
242 m_set_flags.Set(eThreadSpec);
246 //----------------------------------------------------------------------
248 //----------------------------------------------------------------------
249 BreakpointOptions::~BreakpointOptions() = default;
251 std::unique_ptr<BreakpointOptions> BreakpointOptions::CreateFromStructuredData(
252 Target &target, const StructuredData::Dictionary &options_dict,
255 bool one_shot = false;
256 bool auto_continue = false;
257 int32_t ignore_count = 0;
258 llvm::StringRef condition_ref("");
261 const char *key = GetKey(OptionNames::EnabledState);
264 success = options_dict.GetValueForKeyAsBoolean(key, enabled);
266 error.SetErrorStringWithFormat("%s key is not a boolean.",
267 GetKey(OptionNames::EnabledState));
270 set_options.Set(eEnabled);
273 key = GetKey(OptionNames::OneShotState);
275 success = options_dict.GetValueForKeyAsBoolean(key, one_shot);
277 error.SetErrorStringWithFormat("%s key is not a boolean.",
278 GetKey(OptionNames::OneShotState));
281 set_options.Set(eOneShot);
284 key = GetKey(OptionNames::AutoContinue);
286 success = options_dict.GetValueForKeyAsBoolean(key, auto_continue);
288 error.SetErrorStringWithFormat("%s key is not a boolean.",
289 GetKey(OptionNames::AutoContinue));
292 set_options.Set(eAutoContinue);
295 key = GetKey(OptionNames::IgnoreCount);
297 success = options_dict.GetValueForKeyAsInteger(key, ignore_count);
299 error.SetErrorStringWithFormat("%s key is not an integer.",
300 GetKey(OptionNames::IgnoreCount));
303 set_options.Set(eIgnoreCount);
306 key = GetKey(OptionNames::ConditionText);
308 success = options_dict.GetValueForKeyAsString(key, condition_ref);
310 error.SetErrorStringWithFormat("%s key is not an string.",
311 GetKey(OptionNames::ConditionText));
314 set_options.Set(eCondition);
317 std::unique_ptr<CommandData> cmd_data_up;
318 StructuredData::Dictionary *cmds_dict;
319 success = options_dict.GetValueForKeyAsDictionary(
320 CommandData::GetSerializationKey(), cmds_dict);
321 if (success && cmds_dict) {
323 cmd_data_up = CommandData::CreateFromStructuredData(*cmds_dict, cmds_error);
324 if (cmds_error.Fail()) {
325 error.SetErrorStringWithFormat(
326 "Failed to deserialize breakpoint command options: %s.",
327 cmds_error.AsCString());
332 auto bp_options = llvm::make_unique<BreakpointOptions>(
333 condition_ref.str().c_str(), enabled,
334 ignore_count, one_shot, auto_continue);
335 if (cmd_data_up.get()) {
336 if (cmd_data_up->interpreter == eScriptLanguageNone)
337 bp_options->SetCommandDataCallback(cmd_data_up);
339 ScriptInterpreter *interp =
340 target.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
342 error.SetErrorStringWithFormat(
343 "Can't set script commands - no script interpreter");
346 if (interp->GetLanguage() != cmd_data_up->interpreter) {
347 error.SetErrorStringWithFormat(
348 "Current script language doesn't match breakpoint's language: %s",
349 ScriptInterpreter::LanguageToString(cmd_data_up->interpreter)
355 interp->SetBreakpointCommandCallback(bp_options.get(), cmd_data_up);
356 if (script_error.Fail()) {
357 error.SetErrorStringWithFormat("Error generating script callback: %s.",
364 StructuredData::Dictionary *thread_spec_dict;
365 success = options_dict.GetValueForKeyAsDictionary(
366 ThreadSpec::GetSerializationKey(), thread_spec_dict);
368 Status thread_spec_error;
369 std::unique_ptr<ThreadSpec> thread_spec_up =
370 ThreadSpec::CreateFromStructuredData(*thread_spec_dict,
372 if (thread_spec_error.Fail()) {
373 error.SetErrorStringWithFormat(
374 "Failed to deserialize breakpoint thread spec options: %s.",
375 thread_spec_error.AsCString());
378 bp_options->SetThreadSpec(thread_spec_up);
383 StructuredData::ObjectSP BreakpointOptions::SerializeToStructuredData() {
384 StructuredData::DictionarySP options_dict_sp(
385 new StructuredData::Dictionary());
386 if (m_set_flags.Test(eEnabled))
387 options_dict_sp->AddBooleanItem(GetKey(OptionNames::EnabledState),
389 if (m_set_flags.Test(eOneShot))
390 options_dict_sp->AddBooleanItem(GetKey(OptionNames::OneShotState),
392 if (m_set_flags.Test(eAutoContinue))
393 options_dict_sp->AddBooleanItem(GetKey(OptionNames::AutoContinue),
395 if (m_set_flags.Test(eIgnoreCount))
396 options_dict_sp->AddIntegerItem(GetKey(OptionNames::IgnoreCount),
398 if (m_set_flags.Test(eCondition))
399 options_dict_sp->AddStringItem(GetKey(OptionNames::ConditionText),
402 if (m_set_flags.Test(eCallback) && m_baton_is_command_baton) {
404 std::static_pointer_cast<CommandBaton>(m_callback_baton_sp);
405 StructuredData::ObjectSP commands_sp =
406 cmd_baton->getItem()->SerializeToStructuredData();
408 options_dict_sp->AddItem(
409 BreakpointOptions::CommandData::GetSerializationKey(), commands_sp);
412 if (m_set_flags.Test(eThreadSpec) && m_thread_spec_ap) {
413 StructuredData::ObjectSP thread_spec_sp =
414 m_thread_spec_ap->SerializeToStructuredData();
415 options_dict_sp->AddItem(ThreadSpec::GetSerializationKey(), thread_spec_sp);
418 return options_dict_sp;
421 //------------------------------------------------------------------
423 //------------------------------------------------------------------
424 void BreakpointOptions::SetCallback(BreakpointHitCallback callback,
425 const lldb::BatonSP &callback_baton_sp,
426 bool callback_is_synchronous) {
427 // FIXME: This seems unsafe. If BatonSP actually *is* a CommandBaton, but
428 // in a shared_ptr<Baton> instead of a shared_ptr<CommandBaton>, then we will
429 // set m_baton_is_command_baton to false, which is incorrect. One possible
430 // solution is to make the base Baton class provide a method such as:
431 // virtual StringRef getBatonId() const { return ""; }
432 // and have CommandBaton override this to return something unique, and then
433 // check for it here. Another option might be to make Baton using the llvm
434 // casting infrastructure, so that we could write something like:
435 // if (llvm::isa<CommandBaton>(callback_baton_sp))
436 // at relevant callsites instead of storing a boolean.
437 m_callback_is_synchronous = callback_is_synchronous;
438 m_callback = callback;
439 m_callback_baton_sp = callback_baton_sp;
440 m_baton_is_command_baton = false;
441 m_set_flags.Set(eCallback);
444 void BreakpointOptions::SetCallback(
445 BreakpointHitCallback callback,
446 const BreakpointOptions::CommandBatonSP &callback_baton_sp,
447 bool callback_is_synchronous) {
448 m_callback_is_synchronous = callback_is_synchronous;
449 m_callback = callback;
450 m_callback_baton_sp = callback_baton_sp;
451 m_baton_is_command_baton = true;
452 m_set_flags.Set(eCallback);
455 void BreakpointOptions::ClearCallback() {
456 m_callback = BreakpointOptions::NullCallback;
457 m_callback_is_synchronous = false;
458 m_callback_baton_sp.reset();
459 m_baton_is_command_baton = false;
460 m_set_flags.Clear(eCallback);
463 Baton *BreakpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
465 const Baton *BreakpointOptions::GetBaton() const {
466 return m_callback_baton_sp.get();
469 bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
470 lldb::user_id_t break_id,
471 lldb::user_id_t break_loc_id) {
473 if (context->is_synchronous == IsCallbackSynchronous()) {
474 return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
476 context, break_id, break_loc_id);
477 } else if (IsCallbackSynchronous()) {
478 // If a synchronous callback is called at async time, it should not say
486 bool BreakpointOptions::HasCallback() const {
487 return m_callback != BreakpointOptions::NullCallback;
490 bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) {
493 if (!m_baton_is_command_baton)
496 auto cmd_baton = std::static_pointer_cast<CommandBaton>(m_callback_baton_sp);
497 CommandData *data = cmd_baton->getItem();
500 command_list = data->user_source;
504 void BreakpointOptions::SetCondition(const char *condition) {
505 if (!condition || condition[0] == '\0') {
507 m_set_flags.Clear(eCondition);
510 m_set_flags.Set(eCondition);
512 m_condition_text.assign(condition);
513 std::hash<std::string> hasher;
514 m_condition_text_hash = hasher(m_condition_text);
517 const char *BreakpointOptions::GetConditionText(size_t *hash) const {
518 if (!m_condition_text.empty()) {
520 *hash = m_condition_text_hash;
522 return m_condition_text.c_str();
528 const ThreadSpec *BreakpointOptions::GetThreadSpecNoCreate() const {
529 return m_thread_spec_ap.get();
532 ThreadSpec *BreakpointOptions::GetThreadSpec() {
533 if (m_thread_spec_ap.get() == nullptr)
535 m_set_flags.Set(eThreadSpec);
536 m_thread_spec_ap.reset(new ThreadSpec());
539 return m_thread_spec_ap.get();
542 void BreakpointOptions::SetThreadID(lldb::tid_t thread_id) {
543 GetThreadSpec()->SetTID(thread_id);
544 m_set_flags.Set(eThreadSpec);
547 void BreakpointOptions::SetThreadSpec(
548 std::unique_ptr<ThreadSpec> &thread_spec_up) {
549 m_thread_spec_ap = std::move(thread_spec_up);
550 m_set_flags.Set(eThreadSpec);
553 void BreakpointOptions::GetDescription(Stream *s,
554 lldb::DescriptionLevel level) const {
555 // Figure out if there are any options not at their default value, and only
556 // print anything if there are:
558 if (m_ignore_count != 0 || !m_enabled || m_one_shot || m_auto_continue ||
559 (GetThreadSpecNoCreate() != nullptr &&
560 GetThreadSpecNoCreate()->HasSpecification())) {
561 if (level == lldb::eDescriptionLevelVerbose) {
565 s->PutCString("Breakpoint Options:\n");
569 s->PutCString(" Options: ");
571 if (m_ignore_count > 0)
572 s->Printf("ignore: %d ", m_ignore_count);
573 s->Printf("%sabled ", m_enabled ? "en" : "dis");
576 s->Printf("one-shot ");
579 s->Printf("auto-continue ");
581 if (m_thread_spec_ap.get())
582 m_thread_spec_ap->GetDescription(s, level);
584 if (level == lldb::eDescriptionLevelFull) {
590 if (m_callback_baton_sp.get()) {
591 if (level != eDescriptionLevelBrief) {
593 m_callback_baton_sp->GetDescription(s, level);
596 if (!m_condition_text.empty()) {
597 if (level != eDescriptionLevelBrief) {
599 s->Printf("Condition: %s\n", m_condition_text.c_str());
604 void BreakpointOptions::CommandBaton::GetDescription(
605 Stream *s, lldb::DescriptionLevel level) const {
606 const CommandData *data = getItem();
608 if (level == eDescriptionLevelBrief) {
609 s->Printf(", commands = %s",
610 (data && data->user_source.GetSize() > 0) ? "yes" : "no");
615 s->Indent("Breakpoint commands");
616 if (data->interpreter != eScriptLanguageNone)
617 s->Printf(" (%s):\n",
618 ScriptInterpreter::LanguageToString(data->interpreter).c_str());
620 s->PutCString(":\n");
623 if (data && data->user_source.GetSize() > 0) {
624 const size_t num_strings = data->user_source.GetSize();
625 for (size_t i = 0; i < num_strings; ++i) {
626 s->Indent(data->user_source.GetStringAtIndex(i));
630 s->PutCString("No commands.\n");
636 void BreakpointOptions::SetCommandDataCallback(
637 std::unique_ptr<CommandData> &cmd_data) {
638 cmd_data->interpreter = eScriptLanguageNone;
639 auto baton_sp = std::make_shared<CommandBaton>(std::move(cmd_data));
640 SetCallback(BreakpointOptions::BreakpointOptionsCallbackFunction, baton_sp);
641 m_set_flags.Set(eCallback);
644 bool BreakpointOptions::BreakpointOptionsCallbackFunction(
645 void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
646 lldb::user_id_t break_loc_id) {
647 bool ret_value = true;
648 if (baton == nullptr)
651 CommandData *data = (CommandData *)baton;
652 StringList &commands = data->user_source;
654 if (commands.GetSize() > 0) {
655 ExecutionContext exe_ctx(context->exe_ctx_ref);
656 Target *target = exe_ctx.GetTargetPtr();
658 CommandReturnObject result;
659 Debugger &debugger = target->GetDebugger();
660 // Rig up the results secondary output stream to the debugger's, so the
661 // output will come out synchronously if the debugger is set up that way.
663 StreamSP output_stream(debugger.GetAsyncOutputStream());
664 StreamSP error_stream(debugger.GetAsyncErrorStream());
665 result.SetImmediateOutputStream(output_stream);
666 result.SetImmediateErrorStream(error_stream);
668 CommandInterpreterRunOptions options;
669 options.SetStopOnContinue(true);
670 options.SetStopOnError(data->stop_on_error);
671 options.SetEchoCommands(true);
672 options.SetPrintResults(true);
673 options.SetAddToHistory(false);
675 debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
677 result.GetImmediateOutputStream()->Flush();
678 result.GetImmediateErrorStream()->Flush();
684 void BreakpointOptions::Clear()
687 m_thread_spec_ap.release();
690 m_auto_continue = false;
691 m_callback = nullptr;
692 m_callback_baton_sp.reset();
693 m_baton_is_command_baton = false;
694 m_callback_is_synchronous = false;
696 m_condition_text.clear();