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
429 // will set m_baton_is_command_baton to false, which is incorrect.
430 // One possible solution is to make the base Baton class provide a method
432 // virtual StringRef getBatonId() const { return ""; }
433 // and have CommandBaton override this to return something unique, and then
434 // check for it here. Another option might be to make Baton using the llvm
435 // casting infrastructure, so that we could write something like:
436 // if (llvm::isa<CommandBaton>(callback_baton_sp))
437 // at relevant callsites instead of storing a boolean.
438 m_callback_is_synchronous = callback_is_synchronous;
439 m_callback = callback;
440 m_callback_baton_sp = callback_baton_sp;
441 m_baton_is_command_baton = false;
442 m_set_flags.Set(eCallback);
445 void BreakpointOptions::SetCallback(
446 BreakpointHitCallback callback,
447 const BreakpointOptions::CommandBatonSP &callback_baton_sp,
448 bool callback_is_synchronous) {
449 m_callback_is_synchronous = callback_is_synchronous;
450 m_callback = callback;
451 m_callback_baton_sp = callback_baton_sp;
452 m_baton_is_command_baton = true;
453 m_set_flags.Set(eCallback);
456 void BreakpointOptions::ClearCallback() {
457 m_callback = BreakpointOptions::NullCallback;
458 m_callback_is_synchronous = false;
459 m_callback_baton_sp.reset();
460 m_baton_is_command_baton = false;
461 m_set_flags.Clear(eCallback);
464 Baton *BreakpointOptions::GetBaton() { return m_callback_baton_sp.get(); }
466 const Baton *BreakpointOptions::GetBaton() const {
467 return m_callback_baton_sp.get();
470 bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
471 lldb::user_id_t break_id,
472 lldb::user_id_t break_loc_id) {
473 if (m_callback && context->is_synchronous == IsCallbackSynchronous()) {
474 return m_callback(m_callback_baton_sp ? m_callback_baton_sp->data()
476 context, break_id, break_loc_id);
481 bool BreakpointOptions::HasCallback() const {
482 return m_callback != BreakpointOptions::NullCallback;
485 bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) {
488 if (!m_baton_is_command_baton)
491 auto cmd_baton = std::static_pointer_cast<CommandBaton>(m_callback_baton_sp);
492 CommandData *data = cmd_baton->getItem();
495 command_list = data->user_source;
499 void BreakpointOptions::SetCondition(const char *condition) {
500 if (!condition || condition[0] == '\0') {
502 m_set_flags.Clear(eCondition);
505 m_set_flags.Set(eCondition);
507 m_condition_text.assign(condition);
508 std::hash<std::string> hasher;
509 m_condition_text_hash = hasher(m_condition_text);
512 const char *BreakpointOptions::GetConditionText(size_t *hash) const {
513 if (!m_condition_text.empty()) {
515 *hash = m_condition_text_hash;
517 return m_condition_text.c_str();
523 const ThreadSpec *BreakpointOptions::GetThreadSpecNoCreate() const {
524 return m_thread_spec_ap.get();
527 ThreadSpec *BreakpointOptions::GetThreadSpec() {
528 if (m_thread_spec_ap.get() == nullptr)
529 m_thread_spec_ap.reset(new ThreadSpec());
531 return m_thread_spec_ap.get();
534 void BreakpointOptions::SetThreadID(lldb::tid_t thread_id) {
535 GetThreadSpec()->SetTID(thread_id);
536 m_set_flags.Set(eThreadSpec);
539 void BreakpointOptions::SetThreadSpec(
540 std::unique_ptr<ThreadSpec> &thread_spec_up) {
541 m_thread_spec_ap = std::move(thread_spec_up);
542 m_set_flags.Set(eThreadSpec);
545 void BreakpointOptions::GetDescription(Stream *s,
546 lldb::DescriptionLevel level) const {
547 // Figure out if there are any options not at their default value, and only
549 // anything if there are:
551 if (m_ignore_count != 0 || !m_enabled || m_one_shot || m_auto_continue ||
552 (GetThreadSpecNoCreate() != nullptr &&
553 GetThreadSpecNoCreate()->HasSpecification())) {
554 if (level == lldb::eDescriptionLevelVerbose) {
558 s->PutCString("Breakpoint Options:\n");
562 s->PutCString(" Options: ");
564 if (m_ignore_count > 0)
565 s->Printf("ignore: %d ", m_ignore_count);
566 s->Printf("%sabled ", m_enabled ? "en" : "dis");
569 s->Printf("one-shot ");
572 s->Printf("auto-continue ");
574 if (m_thread_spec_ap.get())
575 m_thread_spec_ap->GetDescription(s, level);
577 if (level == lldb::eDescriptionLevelFull) {
583 if (m_callback_baton_sp.get()) {
584 if (level != eDescriptionLevelBrief) {
586 m_callback_baton_sp->GetDescription(s, level);
589 if (!m_condition_text.empty()) {
590 if (level != eDescriptionLevelBrief) {
592 s->Printf("Condition: %s\n", m_condition_text.c_str());
597 void BreakpointOptions::CommandBaton::GetDescription(
598 Stream *s, lldb::DescriptionLevel level) const {
599 const CommandData *data = getItem();
601 if (level == eDescriptionLevelBrief) {
602 s->Printf(", commands = %s",
603 (data && data->user_source.GetSize() > 0) ? "yes" : "no");
608 s->Indent("Breakpoint commands");
609 if (data->interpreter != eScriptLanguageNone)
610 s->Printf(" (%s):\n",
611 ScriptInterpreter::LanguageToString(data->interpreter).c_str());
613 s->PutCString(":\n");
616 if (data && data->user_source.GetSize() > 0) {
617 const size_t num_strings = data->user_source.GetSize();
618 for (size_t i = 0; i < num_strings; ++i) {
619 s->Indent(data->user_source.GetStringAtIndex(i));
623 s->PutCString("No commands.\n");
629 void BreakpointOptions::SetCommandDataCallback(
630 std::unique_ptr<CommandData> &cmd_data) {
631 cmd_data->interpreter = eScriptLanguageNone;
632 auto baton_sp = std::make_shared<CommandBaton>(std::move(cmd_data));
633 SetCallback(BreakpointOptions::BreakpointOptionsCallbackFunction, baton_sp);
634 m_set_flags.Set(eCallback);
637 bool BreakpointOptions::BreakpointOptionsCallbackFunction(
638 void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
639 lldb::user_id_t break_loc_id) {
640 bool ret_value = true;
641 if (baton == nullptr)
644 CommandData *data = (CommandData *)baton;
645 StringList &commands = data->user_source;
647 if (commands.GetSize() > 0) {
648 ExecutionContext exe_ctx(context->exe_ctx_ref);
649 Target *target = exe_ctx.GetTargetPtr();
651 CommandReturnObject result;
652 Debugger &debugger = target->GetDebugger();
653 // Rig up the results secondary output stream to the debugger's, so the
654 // output will come out synchronously
655 // if the debugger is set up that way.
657 StreamSP output_stream(debugger.GetAsyncOutputStream());
658 StreamSP error_stream(debugger.GetAsyncErrorStream());
659 result.SetImmediateOutputStream(output_stream);
660 result.SetImmediateErrorStream(error_stream);
662 CommandInterpreterRunOptions options;
663 options.SetStopOnContinue(true);
664 options.SetStopOnError(data->stop_on_error);
665 options.SetEchoCommands(true);
666 options.SetPrintResults(true);
667 options.SetAddToHistory(false);
669 debugger.GetCommandInterpreter().HandleCommands(commands, &exe_ctx,
671 result.GetImmediateOutputStream()->Flush();
672 result.GetImmediateErrorStream()->Flush();
678 void BreakpointOptions::Clear()
681 m_thread_spec_ap.release();
684 m_auto_continue = false;
685 m_callback = nullptr;
686 m_callback_baton_sp.reset();
687 m_baton_is_command_baton = false;
688 m_callback_is_synchronous = false;
690 m_condition_text.clear();