15ffd83dbSDimitry Andric //===-- Target.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Target/Target.h"
100b57cec5SDimitry Andric #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
110b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
120b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointPrecondition.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolver.h"
140b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverAddress.h"
150b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
160b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
170b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverName.h"
180b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
190b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h"
200b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
210b57cec5SDimitry Andric #include "lldb/Core/Module.h"
220b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
230b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
240b57cec5SDimitry Andric #include "lldb/Core/SearchFilter.h"
250b57cec5SDimitry Andric #include "lldb/Core/Section.h"
260b57cec5SDimitry Andric #include "lldb/Core/SourceManager.h"
270b57cec5SDimitry Andric #include "lldb/Core/StreamFile.h"
280b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
290b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
305ffd83dbSDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
310b57cec5SDimitry Andric #include "lldb/Expression/REPL.h"
320b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h"
330b57cec5SDimitry Andric #include "lldb/Host/Host.h"
340b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h"
350b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
360b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
370b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupWatchpoint.h"
380b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValues.h"
390b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
400b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
410b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
420b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
430b57cec5SDimitry Andric #include "lldb/Target/Language.h"
440b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
450b57cec5SDimitry Andric #include "lldb/Target/Process.h"
460b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
470b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
480b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h"
490b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
500b57cec5SDimitry Andric #include "lldb/Target/ThreadSpec.h"
510b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
520b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
530b57cec5SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
540b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
550b57cec5SDimitry Andric #include "lldb/Utility/State.h"
560b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
570b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric #include <memory>
620b57cec5SDimitry Andric #include <mutex>
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric using namespace lldb;
650b57cec5SDimitry Andric using namespace lldb_private;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric Target::Arch::Arch(const ArchSpec &spec)
700b57cec5SDimitry Andric     : m_spec(spec),
710b57cec5SDimitry Andric       m_plugin_up(PluginManager::CreateArchitectureInstance(spec)) {}
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric const Target::Arch &Target::Arch::operator=(const ArchSpec &spec) {
740b57cec5SDimitry Andric   m_spec = spec;
750b57cec5SDimitry Andric   m_plugin_up = PluginManager::CreateArchitectureInstance(spec);
760b57cec5SDimitry Andric   return *this;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric ConstString &Target::GetStaticBroadcasterClass() {
800b57cec5SDimitry Andric   static ConstString class_name("lldb.target");
810b57cec5SDimitry Andric   return class_name;
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric Target::Target(Debugger &debugger, const ArchSpec &target_arch,
850b57cec5SDimitry Andric                const lldb::PlatformSP &platform_sp, bool is_dummy_target)
860b57cec5SDimitry Andric     : TargetProperties(this),
870b57cec5SDimitry Andric       Broadcaster(debugger.GetBroadcasterManager(),
880b57cec5SDimitry Andric                   Target::GetStaticBroadcasterClass().AsCString()),
890b57cec5SDimitry Andric       ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp),
900b57cec5SDimitry Andric       m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
910b57cec5SDimitry Andric       m_breakpoint_list(false), m_internal_breakpoint_list(true),
920b57cec5SDimitry Andric       m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
935ffd83dbSDimitry Andric       m_image_search_paths(ImageSearchPathsChanged, this),
940b57cec5SDimitry Andric       m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0),
950b57cec5SDimitry Andric       m_valid(true), m_suppress_stop_hooks(false),
960b57cec5SDimitry Andric       m_is_dummy_target(is_dummy_target),
970b57cec5SDimitry Andric       m_stats_storage(static_cast<int>(StatisticKind::StatisticMax))
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric {
1000b57cec5SDimitry Andric   SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
1010b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
1020b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
1030b57cec5SDimitry Andric   SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
1040b57cec5SDimitry Andric   SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   CheckInWithManager();
1070b57cec5SDimitry Andric 
1089dba64beSDimitry Andric   LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT),
1099dba64beSDimitry Andric            "{0} Target::Target()", static_cast<void *>(this));
1100b57cec5SDimitry Andric   if (target_arch.IsValid()) {
1119dba64beSDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET),
1129dba64beSDimitry Andric              "Target::Target created with architecture {0} ({1})",
1130b57cec5SDimitry Andric              target_arch.GetArchitectureName(),
1140b57cec5SDimitry Andric              target_arch.GetTriple().getTriple().c_str());
1150b57cec5SDimitry Andric   }
1165ffd83dbSDimitry Andric 
1175ffd83dbSDimitry Andric   UpdateLaunchInfoFromProperties();
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric Target::~Target() {
1210b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
1229dba64beSDimitry Andric   LLDB_LOG(log, "{0} Target::~Target()", static_cast<void *>(this));
1230b57cec5SDimitry Andric   DeleteCurrentProcess();
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric void Target::PrimeFromDummyTarget(Target *target) {
1270b57cec5SDimitry Andric   if (!target)
1280b57cec5SDimitry Andric     return;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   m_stop_hooks = target->m_stop_hooks;
1310b57cec5SDimitry Andric 
1325ffd83dbSDimitry Andric   for (const auto &breakpoint_sp : target->m_breakpoint_list.Breakpoints()) {
1330b57cec5SDimitry Andric     if (breakpoint_sp->IsInternal())
1340b57cec5SDimitry Andric       continue;
1350b57cec5SDimitry Andric 
1365ffd83dbSDimitry Andric     BreakpointSP new_bp(
1375ffd83dbSDimitry Andric         Breakpoint::CopyFromBreakpoint(shared_from_this(), *breakpoint_sp));
1385ffd83dbSDimitry Andric     AddBreakpoint(std::move(new_bp), false);
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1419dba64beSDimitry Andric   for (auto bp_name_entry : target->m_breakpoint_names) {
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric     BreakpointName *new_bp_name = new BreakpointName(*bp_name_entry.second);
1440b57cec5SDimitry Andric     AddBreakpointName(new_bp_name);
1450b57cec5SDimitry Andric   }
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {
1490b57cec5SDimitry Andric   //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1500b57cec5SDimitry Andric   if (description_level != lldb::eDescriptionLevelBrief) {
1510b57cec5SDimitry Andric     s->Indent();
1520b57cec5SDimitry Andric     s->PutCString("Target\n");
1530b57cec5SDimitry Andric     s->IndentMore();
1540b57cec5SDimitry Andric     m_images.Dump(s);
1550b57cec5SDimitry Andric     m_breakpoint_list.Dump(s);
1560b57cec5SDimitry Andric     m_internal_breakpoint_list.Dump(s);
1570b57cec5SDimitry Andric     s->IndentLess();
1580b57cec5SDimitry Andric   } else {
1590b57cec5SDimitry Andric     Module *exe_module = GetExecutableModulePointer();
1600b57cec5SDimitry Andric     if (exe_module)
1610b57cec5SDimitry Andric       s->PutCString(exe_module->GetFileSpec().GetFilename().GetCString());
1620b57cec5SDimitry Andric     else
1630b57cec5SDimitry Andric       s->PutCString("No executable module.");
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric void Target::CleanupProcess() {
1680b57cec5SDimitry Andric   // Do any cleanup of the target we need to do between process instances.
1690b57cec5SDimitry Andric   // NB It is better to do this before destroying the process in case the
1700b57cec5SDimitry Andric   // clean up needs some help from the process.
1710b57cec5SDimitry Andric   m_breakpoint_list.ClearAllBreakpointSites();
1720b57cec5SDimitry Andric   m_internal_breakpoint_list.ClearAllBreakpointSites();
1730b57cec5SDimitry Andric   // Disable watchpoints just on the debugger side.
1740b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
1750b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
1760b57cec5SDimitry Andric   DisableAllWatchpoints(false);
1770b57cec5SDimitry Andric   ClearAllWatchpointHitCounts();
1780b57cec5SDimitry Andric   ClearAllWatchpointHistoricValues();
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric void Target::DeleteCurrentProcess() {
1820b57cec5SDimitry Andric   if (m_process_sp) {
1830b57cec5SDimitry Andric     m_section_load_history.Clear();
1840b57cec5SDimitry Andric     if (m_process_sp->IsAlive())
1850b57cec5SDimitry Andric       m_process_sp->Destroy(false);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric     m_process_sp->Finalize();
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric     CleanupProcess();
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric     m_process_sp.reset();
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp,
1960b57cec5SDimitry Andric                                              llvm::StringRef plugin_name,
1970b57cec5SDimitry Andric                                              const FileSpec *crash_file) {
1980b57cec5SDimitry Andric   if (!listener_sp)
1990b57cec5SDimitry Andric     listener_sp = GetDebugger().GetListener();
2000b57cec5SDimitry Andric   DeleteCurrentProcess();
2010b57cec5SDimitry Andric   m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name,
2020b57cec5SDimitry Andric                                      listener_sp, crash_file);
2030b57cec5SDimitry Andric   return m_process_sp;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language,
2090b57cec5SDimitry Andric                              const char *repl_options, bool can_create) {
2100b57cec5SDimitry Andric   if (language == eLanguageTypeUnknown) {
2119dba64beSDimitry Andric     LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
2120b57cec5SDimitry Andric 
2139dba64beSDimitry Andric     if (auto single_lang = repl_languages.GetSingularLanguage()) {
2149dba64beSDimitry Andric       language = *single_lang;
2159dba64beSDimitry Andric     } else if (repl_languages.Empty()) {
2160b57cec5SDimitry Andric       err.SetErrorStringWithFormat(
2170b57cec5SDimitry Andric           "LLDB isn't configured with REPL support for any languages.");
2180b57cec5SDimitry Andric       return REPLSP();
2190b57cec5SDimitry Andric     } else {
2200b57cec5SDimitry Andric       err.SetErrorStringWithFormat(
2210b57cec5SDimitry Andric           "Multiple possible REPL languages.  Please specify a language.");
2220b57cec5SDimitry Andric       return REPLSP();
2230b57cec5SDimitry Andric     }
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   REPLMap::iterator pos = m_repl_map.find(language);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   if (pos != m_repl_map.end()) {
2290b57cec5SDimitry Andric     return pos->second;
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   if (!can_create) {
2330b57cec5SDimitry Andric     err.SetErrorStringWithFormat(
2340b57cec5SDimitry Andric         "Couldn't find an existing REPL for %s, and can't create a new one",
2350b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
2360b57cec5SDimitry Andric     return lldb::REPLSP();
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   Debugger *const debugger = nullptr;
2400b57cec5SDimitry Andric   lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   if (ret) {
2430b57cec5SDimitry Andric     m_repl_map[language] = ret;
2440b57cec5SDimitry Andric     return m_repl_map[language];
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   if (err.Success()) {
2480b57cec5SDimitry Andric     err.SetErrorStringWithFormat("Couldn't create a REPL for %s",
2490b57cec5SDimitry Andric                                  Language::GetNameForLanguageType(language));
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   return lldb::REPLSP();
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) {
2560b57cec5SDimitry Andric   lldbassert(!m_repl_map.count(language));
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   m_repl_map[language] = repl_sp;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric void Target::Destroy() {
2620b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2630b57cec5SDimitry Andric   m_valid = false;
2640b57cec5SDimitry Andric   DeleteCurrentProcess();
2650b57cec5SDimitry Andric   m_platform_sp.reset();
2660b57cec5SDimitry Andric   m_arch = ArchSpec();
2670b57cec5SDimitry Andric   ClearModules(true);
2680b57cec5SDimitry Andric   m_section_load_history.Clear();
2690b57cec5SDimitry Andric   const bool notify = false;
2700b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(notify);
2710b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveAll(notify);
2720b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
2730b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
2740b57cec5SDimitry Andric   m_search_filter_sp.reset();
2750b57cec5SDimitry Andric   m_image_search_paths.Clear(notify);
2760b57cec5SDimitry Andric   m_stop_hooks.clear();
2770b57cec5SDimitry Andric   m_stop_hook_next_id = 0;
2780b57cec5SDimitry Andric   m_suppress_stop_hooks = false;
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric BreakpointList &Target::GetBreakpointList(bool internal) {
2820b57cec5SDimitry Andric   if (internal)
2830b57cec5SDimitry Andric     return m_internal_breakpoint_list;
2840b57cec5SDimitry Andric   else
2850b57cec5SDimitry Andric     return m_breakpoint_list;
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric const BreakpointList &Target::GetBreakpointList(bool internal) const {
2890b57cec5SDimitry Andric   if (internal)
2900b57cec5SDimitry Andric     return m_internal_breakpoint_list;
2910b57cec5SDimitry Andric   else
2920b57cec5SDimitry Andric     return m_breakpoint_list;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric BreakpointSP Target::GetBreakpointByID(break_id_t break_id) {
2960b57cec5SDimitry Andric   BreakpointSP bp_sp;
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
2990b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
3000b57cec5SDimitry Andric   else
3010b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   return bp_sp;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric BreakpointSP Target::CreateSourceRegexBreakpoint(
3070b57cec5SDimitry Andric     const FileSpecList *containingModules,
3080b57cec5SDimitry Andric     const FileSpecList *source_file_spec_list,
3090b57cec5SDimitry Andric     const std::unordered_set<std::string> &function_names,
3109dba64beSDimitry Andric     RegularExpression source_regex, bool internal, bool hardware,
3110b57cec5SDimitry Andric     LazyBool move_to_nearest_code) {
3120b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
3130b57cec5SDimitry Andric       containingModules, source_file_spec_list));
3140b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
3150b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
3160b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(
3179dba64beSDimitry Andric       nullptr, std::move(source_regex), function_names,
3180b57cec5SDimitry Andric       !static_cast<bool>(move_to_nearest_code)));
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules,
3240b57cec5SDimitry Andric                                       const FileSpec &file, uint32_t line_no,
3250b57cec5SDimitry Andric                                       uint32_t column, lldb::addr_t offset,
3260b57cec5SDimitry Andric                                       LazyBool check_inlines,
3270b57cec5SDimitry Andric                                       LazyBool skip_prologue, bool internal,
3280b57cec5SDimitry Andric                                       bool hardware,
3290b57cec5SDimitry Andric                                       LazyBool move_to_nearest_code) {
3300b57cec5SDimitry Andric   FileSpec remapped_file;
3310b57cec5SDimitry Andric   if (!GetSourcePathMap().ReverseRemapPath(file, remapped_file))
3320b57cec5SDimitry Andric     remapped_file = file;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   if (check_inlines == eLazyBoolCalculate) {
3350b57cec5SDimitry Andric     const InlineStrategy inline_strategy = GetInlineStrategy();
3360b57cec5SDimitry Andric     switch (inline_strategy) {
3370b57cec5SDimitry Andric     case eInlineBreakpointsNever:
3380b57cec5SDimitry Andric       check_inlines = eLazyBoolNo;
3390b57cec5SDimitry Andric       break;
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric     case eInlineBreakpointsHeaders:
3420b57cec5SDimitry Andric       if (remapped_file.IsSourceImplementationFile())
3430b57cec5SDimitry Andric         check_inlines = eLazyBoolNo;
3440b57cec5SDimitry Andric       else
3450b57cec5SDimitry Andric         check_inlines = eLazyBoolYes;
3460b57cec5SDimitry Andric       break;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric     case eInlineBreakpointsAlways:
3490b57cec5SDimitry Andric       check_inlines = eLazyBoolYes;
3500b57cec5SDimitry Andric       break;
3510b57cec5SDimitry Andric     }
3520b57cec5SDimitry Andric   }
3530b57cec5SDimitry Andric   SearchFilterSP filter_sp;
3540b57cec5SDimitry Andric   if (check_inlines == eLazyBoolNo) {
3550b57cec5SDimitry Andric     // Not checking for inlines, we are looking only for matching compile units
3560b57cec5SDimitry Andric     FileSpecList compile_unit_list;
3570b57cec5SDimitry Andric     compile_unit_list.Append(remapped_file);
3580b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
3590b57cec5SDimitry Andric                                                   &compile_unit_list);
3600b57cec5SDimitry Andric   } else {
3610b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric   if (skip_prologue == eLazyBoolCalculate)
3640b57cec5SDimitry Andric     skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
3650b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
3660b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(
3690b57cec5SDimitry Andric       nullptr, remapped_file, line_no, column, offset, check_inlines,
3700b57cec5SDimitry Andric       skip_prologue, !static_cast<bool>(move_to_nearest_code)));
3710b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal,
3750b57cec5SDimitry Andric                                       bool hardware) {
3760b57cec5SDimitry Andric   Address so_addr;
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   // Check for any reason we want to move this breakpoint to other address.
3790b57cec5SDimitry Andric   addr = GetBreakableLoadAddress(addr);
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   // Attempt to resolve our load address if possible, though it is ok if it
3820b57cec5SDimitry Andric   // doesn't resolve to section/offset.
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   // Try and resolve as a load address if possible
3850b57cec5SDimitry Andric   GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
3860b57cec5SDimitry Andric   if (!so_addr.IsValid()) {
3870b57cec5SDimitry Andric     // The address didn't resolve, so just set this as an absolute address
3880b57cec5SDimitry Andric     so_addr.SetOffset(addr);
3890b57cec5SDimitry Andric   }
3900b57cec5SDimitry Andric   BreakpointSP bp_sp(CreateBreakpoint(so_addr, internal, hardware));
3910b57cec5SDimitry Andric   return bp_sp;
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
3950b57cec5SDimitry Andric                                       bool hardware) {
3960b57cec5SDimitry Andric   SearchFilterSP filter_sp(
3970b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
3980b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(
3990b57cec5SDimitry Andric       new BreakpointResolverAddress(nullptr, addr));
4000b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, false);
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric lldb::BreakpointSP
4040b57cec5SDimitry Andric Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
4050b57cec5SDimitry Andric                                         const FileSpec *file_spec,
4060b57cec5SDimitry Andric                                         bool request_hardware) {
4070b57cec5SDimitry Andric   SearchFilterSP filter_sp(
4080b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
409480093f4SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(
410480093f4SDimitry Andric       nullptr, file_addr, file_spec ? *file_spec : FileSpec()));
4110b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
4120b57cec5SDimitry Andric                           false);
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(
4160b57cec5SDimitry Andric     const FileSpecList *containingModules,
4170b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles, const char *func_name,
4180b57cec5SDimitry Andric     FunctionNameType func_name_type_mask, LanguageType language,
4190b57cec5SDimitry Andric     lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) {
4200b57cec5SDimitry Andric   BreakpointSP bp_sp;
4210b57cec5SDimitry Andric   if (func_name) {
4220b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
4230b57cec5SDimitry Andric         containingModules, containingSourceFiles));
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
4260b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
4270b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
4280b57cec5SDimitry Andric       language = GetLanguage();
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
4310b57cec5SDimitry Andric         nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact,
4320b57cec5SDimitry Andric         offset, skip_prologue));
4330b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
4340b57cec5SDimitry Andric   }
4350b57cec5SDimitry Andric   return bp_sp;
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric lldb::BreakpointSP
4390b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
4400b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
4410b57cec5SDimitry Andric                          const std::vector<std::string> &func_names,
4420b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
4430b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
4440b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
4450b57cec5SDimitry Andric   BreakpointSP bp_sp;
4460b57cec5SDimitry Andric   size_t num_names = func_names.size();
4470b57cec5SDimitry Andric   if (num_names > 0) {
4480b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
4490b57cec5SDimitry Andric         containingModules, containingSourceFiles));
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
4520b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
4530b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
4540b57cec5SDimitry Andric       language = GetLanguage();
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(
4570b57cec5SDimitry Andric         new BreakpointResolverName(nullptr, func_names, func_name_type_mask,
4580b57cec5SDimitry Andric                                    language, offset, skip_prologue));
4590b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric   return bp_sp;
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric BreakpointSP
4650b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
4660b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
4670b57cec5SDimitry Andric                          const char *func_names[], size_t num_names,
4680b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
4690b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
4700b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
4710b57cec5SDimitry Andric   BreakpointSP bp_sp;
4720b57cec5SDimitry Andric   if (num_names > 0) {
4730b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
4740b57cec5SDimitry Andric         containingModules, containingSourceFiles));
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate) {
4770b57cec5SDimitry Andric       if (offset == 0)
4780b57cec5SDimitry Andric         skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
4790b57cec5SDimitry Andric       else
4800b57cec5SDimitry Andric         skip_prologue = eLazyBoolNo;
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
4830b57cec5SDimitry Andric       language = GetLanguage();
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
4860b57cec5SDimitry Andric         nullptr, func_names, num_names, func_name_type_mask, language, offset,
4870b57cec5SDimitry Andric         skip_prologue));
4880b57cec5SDimitry Andric     resolver_sp->SetOffset(offset);
4890b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
4900b57cec5SDimitry Andric   }
4910b57cec5SDimitry Andric   return bp_sp;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric SearchFilterSP
4950b57cec5SDimitry Andric Target::GetSearchFilterForModule(const FileSpec *containingModule) {
4960b57cec5SDimitry Andric   SearchFilterSP filter_sp;
4970b57cec5SDimitry Andric   if (containingModule != nullptr) {
4980b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
4990b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
5000b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModule>(shared_from_this(),
5010b57cec5SDimitry Andric                                                        *containingModule);
5020b57cec5SDimitry Andric   } else {
5030b57cec5SDimitry Andric     if (!m_search_filter_sp)
5040b57cec5SDimitry Andric       m_search_filter_sp =
5050b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
5060b57cec5SDimitry Andric               shared_from_this());
5070b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
5080b57cec5SDimitry Andric   }
5090b57cec5SDimitry Andric   return filter_sp;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric SearchFilterSP
5130b57cec5SDimitry Andric Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) {
5140b57cec5SDimitry Andric   SearchFilterSP filter_sp;
5150b57cec5SDimitry Andric   if (containingModules && containingModules->GetSize() != 0) {
5160b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
5170b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
5180b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleList>(shared_from_this(),
5190b57cec5SDimitry Andric                                                            *containingModules);
5200b57cec5SDimitry Andric   } else {
5210b57cec5SDimitry Andric     if (!m_search_filter_sp)
5220b57cec5SDimitry Andric       m_search_filter_sp =
5230b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
5240b57cec5SDimitry Andric               shared_from_this());
5250b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
5260b57cec5SDimitry Andric   }
5270b57cec5SDimitry Andric   return filter_sp;
5280b57cec5SDimitry Andric }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric SearchFilterSP Target::GetSearchFilterForModuleAndCUList(
5310b57cec5SDimitry Andric     const FileSpecList *containingModules,
5320b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles) {
5330b57cec5SDimitry Andric   if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
5340b57cec5SDimitry Andric     return GetSearchFilterForModuleList(containingModules);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   SearchFilterSP filter_sp;
5370b57cec5SDimitry Andric   if (containingModules == nullptr) {
5380b57cec5SDimitry Andric     // We could make a special "CU List only SearchFilter".  Better yet was if
5390b57cec5SDimitry Andric     // these could be composable, but that will take a little reworking.
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
5420b57cec5SDimitry Andric         shared_from_this(), FileSpecList(), *containingSourceFiles);
5430b57cec5SDimitry Andric   } else {
5440b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
5450b57cec5SDimitry Andric         shared_from_this(), *containingModules, *containingSourceFiles);
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric   return filter_sp;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric BreakpointSP Target::CreateFuncRegexBreakpoint(
5510b57cec5SDimitry Andric     const FileSpecList *containingModules,
5529dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, RegularExpression func_regex,
5530b57cec5SDimitry Andric     lldb::LanguageType requested_language, LazyBool skip_prologue,
5540b57cec5SDimitry Andric     bool internal, bool hardware) {
5550b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
5560b57cec5SDimitry Andric       containingModules, containingSourceFiles));
5570b57cec5SDimitry Andric   bool skip = (skip_prologue == eLazyBoolCalculate)
5580b57cec5SDimitry Andric                   ? GetSkipPrologue()
5590b57cec5SDimitry Andric                   : static_cast<bool>(skip_prologue);
5600b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverName(
5619dba64beSDimitry Andric       nullptr, std::move(func_regex), requested_language, 0, skip));
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
5640b57cec5SDimitry Andric }
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric lldb::BreakpointSP
5670b57cec5SDimitry Andric Target::CreateExceptionBreakpoint(enum lldb::LanguageType language,
5680b57cec5SDimitry Andric                                   bool catch_bp, bool throw_bp, bool internal,
5690b57cec5SDimitry Andric                                   Args *additional_args, Status *error) {
5700b57cec5SDimitry Andric   BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint(
5710b57cec5SDimitry Andric       *this, language, catch_bp, throw_bp, internal);
5720b57cec5SDimitry Andric   if (exc_bkpt_sp && additional_args) {
5730b57cec5SDimitry Andric     BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
5740b57cec5SDimitry Andric     if (precondition_sp && additional_args) {
5750b57cec5SDimitry Andric       if (error)
5760b57cec5SDimitry Andric         *error = precondition_sp->ConfigurePrecondition(*additional_args);
5770b57cec5SDimitry Andric       else
5780b57cec5SDimitry Andric         precondition_sp->ConfigurePrecondition(*additional_args);
5790b57cec5SDimitry Andric     }
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric   return exc_bkpt_sp;
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric 
5849dba64beSDimitry Andric lldb::BreakpointSP Target::CreateScriptedBreakpoint(
5859dba64beSDimitry Andric     const llvm::StringRef class_name, const FileSpecList *containingModules,
5869dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, bool internal,
5879dba64beSDimitry Andric     bool request_hardware, StructuredData::ObjectSP extra_args_sp,
5889dba64beSDimitry Andric     Status *creation_error) {
5890b57cec5SDimitry Andric   SearchFilterSP filter_sp;
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthTarget;
5929dba64beSDimitry Andric   bool has_files =
5939dba64beSDimitry Andric       containingSourceFiles && containingSourceFiles->GetSize() > 0;
5940b57cec5SDimitry Andric   bool has_modules = containingModules && containingModules->GetSize() > 0;
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   if (has_files && has_modules) {
5979dba64beSDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
5989dba64beSDimitry Andric                                                   containingSourceFiles);
5990b57cec5SDimitry Andric   } else if (has_files) {
6009dba64beSDimitry Andric     filter_sp =
6019dba64beSDimitry Andric         GetSearchFilterForModuleAndCUList(nullptr, containingSourceFiles);
6020b57cec5SDimitry Andric   } else if (has_modules) {
6030b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
6040b57cec5SDimitry Andric   } else {
6050b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterForUnconstrainedSearches>(
6060b57cec5SDimitry Andric         shared_from_this());
6070b57cec5SDimitry Andric   }
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   StructuredDataImpl *extra_args_impl = new StructuredDataImpl();
6100b57cec5SDimitry Andric   if (extra_args_sp)
6110b57cec5SDimitry Andric     extra_args_impl->SetObjectSP(extra_args_sp);
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverScripted(
6149dba64beSDimitry Andric       nullptr, class_name, depth, extra_args_impl));
6150b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp,
6190b57cec5SDimitry Andric                                       BreakpointResolverSP &resolver_sp,
6200b57cec5SDimitry Andric                                       bool internal, bool request_hardware,
6210b57cec5SDimitry Andric                                       bool resolve_indirect_symbols) {
6220b57cec5SDimitry Andric   BreakpointSP bp_sp;
6230b57cec5SDimitry Andric   if (filter_sp && resolver_sp) {
6240b57cec5SDimitry Andric     const bool hardware = request_hardware || GetRequireHardwareBreakpoints();
6250b57cec5SDimitry Andric     bp_sp.reset(new Breakpoint(*this, filter_sp, resolver_sp, hardware,
6260b57cec5SDimitry Andric                                resolve_indirect_symbols));
6275ffd83dbSDimitry Andric     resolver_sp->SetBreakpoint(bp_sp);
6280b57cec5SDimitry Andric     AddBreakpoint(bp_sp, internal);
6290b57cec5SDimitry Andric   }
6300b57cec5SDimitry Andric   return bp_sp;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) {
6340b57cec5SDimitry Andric   if (!bp_sp)
6350b57cec5SDimitry Andric     return;
6360b57cec5SDimitry Andric   if (internal)
6370b57cec5SDimitry Andric     m_internal_breakpoint_list.Add(bp_sp, false);
6380b57cec5SDimitry Andric   else
6390b57cec5SDimitry Andric     m_breakpoint_list.Add(bp_sp, true);
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
6420b57cec5SDimitry Andric   if (log) {
6430b57cec5SDimitry Andric     StreamString s;
6440b57cec5SDimitry Andric     bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
6459dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s (internal = %s) => break_id = %s\n",
6469dba64beSDimitry Andric               __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
6470b57cec5SDimitry Andric   }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric   bp_sp->ResolveBreakpoint();
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   if (!internal) {
6520b57cec5SDimitry Andric     m_last_created_breakpoint = bp_sp;
6530b57cec5SDimitry Andric   }
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric 
6569dba64beSDimitry Andric void Target::AddNameToBreakpoint(BreakpointID &id, const char *name,
6579dba64beSDimitry Andric                                  Status &error) {
6589dba64beSDimitry Andric   BreakpointSP bp_sp =
6599dba64beSDimitry Andric       m_breakpoint_list.FindBreakpointByID(id.GetBreakpointID());
6609dba64beSDimitry Andric   if (!bp_sp) {
6610b57cec5SDimitry Andric     StreamString s;
6620b57cec5SDimitry Andric     id.GetDescription(&s, eDescriptionLevelBrief);
6639dba64beSDimitry Andric     error.SetErrorStringWithFormat("Could not find breakpoint %s", s.GetData());
6640b57cec5SDimitry Andric     return;
6650b57cec5SDimitry Andric   }
6660b57cec5SDimitry Andric   AddNameToBreakpoint(bp_sp, name, error);
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
6699dba64beSDimitry Andric void Target::AddNameToBreakpoint(BreakpointSP &bp_sp, const char *name,
6709dba64beSDimitry Andric                                  Status &error) {
6710b57cec5SDimitry Andric   if (!bp_sp)
6720b57cec5SDimitry Andric     return;
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   BreakpointName *bp_name = FindBreakpointName(ConstString(name), true, error);
6750b57cec5SDimitry Andric   if (!bp_name)
6760b57cec5SDimitry Andric     return;
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   bp_name->ConfigureBreakpoint(bp_sp);
6790b57cec5SDimitry Andric   bp_sp->AddName(name);
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric void Target::AddBreakpointName(BreakpointName *bp_name) {
6830b57cec5SDimitry Andric   m_breakpoint_names.insert(std::make_pair(bp_name->GetName(), bp_name));
6840b57cec5SDimitry Andric }
6850b57cec5SDimitry Andric 
6869dba64beSDimitry Andric BreakpointName *Target::FindBreakpointName(ConstString name, bool can_create,
6879dba64beSDimitry Andric                                            Status &error) {
6880b57cec5SDimitry Andric   BreakpointID::StringIsBreakpointName(name.GetStringRef(), error);
6890b57cec5SDimitry Andric   if (!error.Success())
6900b57cec5SDimitry Andric     return nullptr;
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
6930b57cec5SDimitry Andric   if (iter == m_breakpoint_names.end()) {
6949dba64beSDimitry Andric     if (!can_create) {
6950b57cec5SDimitry Andric       error.SetErrorStringWithFormat("Breakpoint name \"%s\" doesn't exist and "
6969dba64beSDimitry Andric                                      "can_create is false.",
6979dba64beSDimitry Andric                                      name.AsCString());
6980b57cec5SDimitry Andric       return nullptr;
6990b57cec5SDimitry Andric     }
7000b57cec5SDimitry Andric 
7019dba64beSDimitry Andric     iter = m_breakpoint_names
7029dba64beSDimitry Andric                .insert(std::make_pair(name, new BreakpointName(name)))
7030b57cec5SDimitry Andric                .first;
7040b57cec5SDimitry Andric   }
7050b57cec5SDimitry Andric   return (iter->second);
7060b57cec5SDimitry Andric }
7070b57cec5SDimitry Andric 
7089dba64beSDimitry Andric void Target::DeleteBreakpointName(ConstString name) {
7090b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric   if (iter != m_breakpoint_names.end()) {
7120b57cec5SDimitry Andric     const char *name_cstr = name.AsCString();
7130b57cec5SDimitry Andric     m_breakpoint_names.erase(iter);
7140b57cec5SDimitry Andric     for (auto bp_sp : m_breakpoint_list.Breakpoints())
7150b57cec5SDimitry Andric       bp_sp->RemoveName(name_cstr);
7160b57cec5SDimitry Andric   }
7170b57cec5SDimitry Andric }
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp,
7209dba64beSDimitry Andric                                       ConstString name) {
7210b57cec5SDimitry Andric   bp_sp->RemoveName(name.AsCString());
7220b57cec5SDimitry Andric }
7230b57cec5SDimitry Andric 
7249dba64beSDimitry Andric void Target::ConfigureBreakpointName(
7259dba64beSDimitry Andric     BreakpointName &bp_name, const BreakpointOptions &new_options,
7269dba64beSDimitry Andric     const BreakpointName::Permissions &new_permissions) {
7270b57cec5SDimitry Andric   bp_name.GetOptions().CopyOverSetOptions(new_options);
7280b57cec5SDimitry Andric   bp_name.GetPermissions().MergeInto(new_permissions);
7290b57cec5SDimitry Andric   ApplyNameToBreakpoints(bp_name);
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) {
733480093f4SDimitry Andric   llvm::Expected<std::vector<BreakpointSP>> expected_vector =
734480093f4SDimitry Andric       m_breakpoint_list.FindBreakpointsByName(bp_name.GetName().AsCString());
7350b57cec5SDimitry Andric 
736480093f4SDimitry Andric   if (!expected_vector) {
737480093f4SDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS),
738480093f4SDimitry Andric              "invalid breakpoint name: {}",
739480093f4SDimitry Andric              llvm::toString(expected_vector.takeError()));
740480093f4SDimitry Andric     return;
741480093f4SDimitry Andric   }
742480093f4SDimitry Andric 
743480093f4SDimitry Andric   for (auto bp_sp : *expected_vector)
7440b57cec5SDimitry Andric     bp_name.ConfigureBreakpoint(bp_sp);
7450b57cec5SDimitry Andric }
7460b57cec5SDimitry Andric 
7479dba64beSDimitry Andric void Target::GetBreakpointNames(std::vector<std::string> &names) {
7480b57cec5SDimitry Andric   names.clear();
7490b57cec5SDimitry Andric   for (auto bp_name : m_breakpoint_names) {
7500b57cec5SDimitry Andric     names.push_back(bp_name.first.AsCString());
7510b57cec5SDimitry Andric   }
7520b57cec5SDimitry Andric   llvm::sort(names.begin(), names.end());
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric bool Target::ProcessIsValid() {
7560b57cec5SDimitry Andric   return (m_process_sp && m_process_sp->IsAlive());
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric static bool CheckIfWatchpointsSupported(Target *target, Status &error) {
7600b57cec5SDimitry Andric   uint32_t num_supported_hardware_watchpoints;
7610b57cec5SDimitry Andric   Status rc = target->GetProcessSP()->GetWatchpointSupportInfo(
7620b57cec5SDimitry Andric       num_supported_hardware_watchpoints);
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   // If unable to determine the # of watchpoints available,
7650b57cec5SDimitry Andric   // assume they are supported.
7660b57cec5SDimitry Andric   if (rc.Fail())
7670b57cec5SDimitry Andric     return true;
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric   if (num_supported_hardware_watchpoints == 0) {
7700b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
7710b57cec5SDimitry Andric         "Target supports (%u) hardware watchpoint slots.\n",
7720b57cec5SDimitry Andric         num_supported_hardware_watchpoints);
7730b57cec5SDimitry Andric     return false;
7740b57cec5SDimitry Andric   }
7750b57cec5SDimitry Andric   return true;
7760b57cec5SDimitry Andric }
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric // See also Watchpoint::SetWatchpointType(uint32_t type) and the
7790b57cec5SDimitry Andric // OptionGroupWatchpoint::WatchType enum type.
7800b57cec5SDimitry Andric WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size,
7810b57cec5SDimitry Andric                                       const CompilerType *type, uint32_t kind,
7820b57cec5SDimitry Andric                                       Status &error) {
7830b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
7849dba64beSDimitry Andric   LLDB_LOGF(log,
7859dba64beSDimitry Andric             "Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64
7860b57cec5SDimitry Andric             " type = %u)\n",
7870b57cec5SDimitry Andric             __FUNCTION__, addr, (uint64_t)size, kind);
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric   WatchpointSP wp_sp;
7900b57cec5SDimitry Andric   if (!ProcessIsValid()) {
7910b57cec5SDimitry Andric     error.SetErrorString("process is not alive");
7920b57cec5SDimitry Andric     return wp_sp;
7930b57cec5SDimitry Andric   }
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   if (addr == LLDB_INVALID_ADDRESS || size == 0) {
7960b57cec5SDimitry Andric     if (size == 0)
7970b57cec5SDimitry Andric       error.SetErrorString("cannot set a watchpoint with watch_size of 0");
7980b57cec5SDimitry Andric     else
7990b57cec5SDimitry Andric       error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
8000b57cec5SDimitry Andric     return wp_sp;
8010b57cec5SDimitry Andric   }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric   if (!LLDB_WATCH_TYPE_IS_VALID(kind)) {
8040b57cec5SDimitry Andric     error.SetErrorStringWithFormat("invalid watchpoint type: %d", kind);
8050b57cec5SDimitry Andric   }
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   if (!CheckIfWatchpointsSupported(this, error))
8080b57cec5SDimitry Andric     return wp_sp;
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric   // Currently we only support one watchpoint per address, with total number of
8110b57cec5SDimitry Andric   // watchpoints limited by the hardware which the inferior is running on.
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric   // Grab the list mutex while doing operations.
8140b57cec5SDimitry Andric   const bool notify = false; // Don't notify about all the state changes we do
8150b57cec5SDimitry Andric                              // on creating the watchpoint.
8160b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
8170b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
8180b57cec5SDimitry Andric   WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
8190b57cec5SDimitry Andric   if (matched_sp) {
8200b57cec5SDimitry Andric     size_t old_size = matched_sp->GetByteSize();
8210b57cec5SDimitry Andric     uint32_t old_type =
8220b57cec5SDimitry Andric         (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
8230b57cec5SDimitry Andric         (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
8240b57cec5SDimitry Andric     // Return the existing watchpoint if both size and type match.
8250b57cec5SDimitry Andric     if (size == old_size && kind == old_type) {
8260b57cec5SDimitry Andric       wp_sp = matched_sp;
8270b57cec5SDimitry Andric       wp_sp->SetEnabled(false, notify);
8280b57cec5SDimitry Andric     } else {
8290b57cec5SDimitry Andric       // Nil the matched watchpoint; we will be creating a new one.
8300b57cec5SDimitry Andric       m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
8310b57cec5SDimitry Andric       m_watchpoint_list.Remove(matched_sp->GetID(), true);
8320b57cec5SDimitry Andric     }
8330b57cec5SDimitry Andric   }
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   if (!wp_sp) {
8360b57cec5SDimitry Andric     wp_sp = std::make_shared<Watchpoint>(*this, addr, size, type);
8370b57cec5SDimitry Andric     wp_sp->SetWatchpointType(kind, notify);
8380b57cec5SDimitry Andric     m_watchpoint_list.Add(wp_sp, true);
8390b57cec5SDimitry Andric   }
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric   error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
8429dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (creation of watchpoint %s with id = %u)\n",
8430b57cec5SDimitry Andric             __FUNCTION__, error.Success() ? "succeeded" : "failed",
8440b57cec5SDimitry Andric             wp_sp->GetID());
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric   if (error.Fail()) {
8470b57cec5SDimitry Andric     // Enabling the watchpoint on the device side failed. Remove the said
8480b57cec5SDimitry Andric     // watchpoint from the list maintained by the target instance.
8490b57cec5SDimitry Andric     m_watchpoint_list.Remove(wp_sp->GetID(), true);
8500b57cec5SDimitry Andric     // See if we could provide more helpful error message.
8510b57cec5SDimitry Andric     if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
8520b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
8530b57cec5SDimitry Andric           "watch size of %" PRIu64 " is not supported", (uint64_t)size);
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric     wp_sp.reset();
8560b57cec5SDimitry Andric   } else
8570b57cec5SDimitry Andric     m_last_created_watchpoint = wp_sp;
8580b57cec5SDimitry Andric   return wp_sp;
8590b57cec5SDimitry Andric }
8600b57cec5SDimitry Andric 
8619dba64beSDimitry Andric void Target::RemoveAllowedBreakpoints() {
8620b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8639dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s \n", __FUNCTION__);
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric   m_breakpoint_list.RemoveAllowed(true);
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
8680b57cec5SDimitry Andric }
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric void Target::RemoveAllBreakpoints(bool internal_also) {
8710b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8729dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
8730b57cec5SDimitry Andric             internal_also ? "yes" : "no");
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(true);
8760b57cec5SDimitry Andric   if (internal_also)
8770b57cec5SDimitry Andric     m_internal_breakpoint_list.RemoveAll(false);
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric void Target::DisableAllBreakpoints(bool internal_also) {
8830b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8849dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
8850b57cec5SDimitry Andric             internal_also ? "yes" : "no");
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(false);
8880b57cec5SDimitry Andric   if (internal_also)
8890b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(false);
8900b57cec5SDimitry Andric }
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric void Target::DisableAllowedBreakpoints() {
8930b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8949dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(false);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric void Target::EnableAllBreakpoints(bool internal_also) {
9000b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9019dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
9020b57cec5SDimitry Andric             internal_also ? "yes" : "no");
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(true);
9050b57cec5SDimitry Andric   if (internal_also)
9060b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(true);
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric void Target::EnableAllowedBreakpoints() {
9100b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9119dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(true);
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric bool Target::RemoveBreakpointByID(break_id_t break_id) {
9170b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9189dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
9190b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   if (DisableBreakpointByID(break_id)) {
9220b57cec5SDimitry Andric     if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
9230b57cec5SDimitry Andric       m_internal_breakpoint_list.Remove(break_id, false);
9240b57cec5SDimitry Andric     else {
9250b57cec5SDimitry Andric       if (m_last_created_breakpoint) {
9260b57cec5SDimitry Andric         if (m_last_created_breakpoint->GetID() == break_id)
9270b57cec5SDimitry Andric           m_last_created_breakpoint.reset();
9280b57cec5SDimitry Andric       }
9290b57cec5SDimitry Andric       m_breakpoint_list.Remove(break_id, true);
9300b57cec5SDimitry Andric     }
9310b57cec5SDimitry Andric     return true;
9320b57cec5SDimitry Andric   }
9330b57cec5SDimitry Andric   return false;
9340b57cec5SDimitry Andric }
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric bool Target::DisableBreakpointByID(break_id_t break_id) {
9370b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9389dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
9390b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
9400b57cec5SDimitry Andric 
9410b57cec5SDimitry Andric   BreakpointSP bp_sp;
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
9440b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
9450b57cec5SDimitry Andric   else
9460b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
9470b57cec5SDimitry Andric   if (bp_sp) {
9480b57cec5SDimitry Andric     bp_sp->SetEnabled(false);
9490b57cec5SDimitry Andric     return true;
9500b57cec5SDimitry Andric   }
9510b57cec5SDimitry Andric   return false;
9520b57cec5SDimitry Andric }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric bool Target::EnableBreakpointByID(break_id_t break_id) {
9550b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9569dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
9570b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
9580b57cec5SDimitry Andric 
9590b57cec5SDimitry Andric   BreakpointSP bp_sp;
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
9620b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
9630b57cec5SDimitry Andric   else
9640b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric   if (bp_sp) {
9670b57cec5SDimitry Andric     bp_sp->SetEnabled(true);
9680b57cec5SDimitry Andric     return true;
9690b57cec5SDimitry Andric   }
9700b57cec5SDimitry Andric   return false;
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric Status Target::SerializeBreakpointsToFile(const FileSpec &file,
9740b57cec5SDimitry Andric                                           const BreakpointIDList &bp_ids,
9750b57cec5SDimitry Andric                                           bool append) {
9760b57cec5SDimitry Andric   Status error;
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric   if (!file) {
9790b57cec5SDimitry Andric     error.SetErrorString("Invalid FileSpec.");
9800b57cec5SDimitry Andric     return error;
9810b57cec5SDimitry Andric   }
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   std::string path(file.GetPath());
9840b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp;
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   StructuredData::ArraySP break_store_sp;
9870b57cec5SDimitry Andric   StructuredData::Array *break_store_ptr = nullptr;
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   if (append) {
9900b57cec5SDimitry Andric     input_data_sp = StructuredData::ParseJSONFromFile(file, error);
9910b57cec5SDimitry Andric     if (error.Success()) {
9920b57cec5SDimitry Andric       break_store_ptr = input_data_sp->GetAsArray();
9930b57cec5SDimitry Andric       if (!break_store_ptr) {
9940b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
9950b57cec5SDimitry Andric             "Tried to append to invalid input file %s", path.c_str());
9960b57cec5SDimitry Andric         return error;
9970b57cec5SDimitry Andric       }
9980b57cec5SDimitry Andric     }
9990b57cec5SDimitry Andric   }
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   if (!break_store_ptr) {
10020b57cec5SDimitry Andric     break_store_sp = std::make_shared<StructuredData::Array>();
10030b57cec5SDimitry Andric     break_store_ptr = break_store_sp.get();
10040b57cec5SDimitry Andric   }
10050b57cec5SDimitry Andric 
10060b57cec5SDimitry Andric   StreamFile out_file(path.c_str(),
10079dba64beSDimitry Andric                       File::eOpenOptionTruncate | File::eOpenOptionWrite |
10089dba64beSDimitry Andric                           File::eOpenOptionCanCreate |
10099dba64beSDimitry Andric                           File::eOpenOptionCloseOnExec,
10100b57cec5SDimitry Andric                       lldb::eFilePermissionsFileDefault);
10110b57cec5SDimitry Andric   if (!out_file.GetFile().IsValid()) {
10120b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Unable to open output file: %s.",
10130b57cec5SDimitry Andric                                    path.c_str());
10140b57cec5SDimitry Andric     return error;
10150b57cec5SDimitry Andric   }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
10180b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   if (bp_ids.GetSize() == 0) {
10210b57cec5SDimitry Andric     const BreakpointList &breakpoints = GetBreakpointList();
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric     size_t num_breakpoints = breakpoints.GetSize();
10240b57cec5SDimitry Andric     for (size_t i = 0; i < num_breakpoints; i++) {
10250b57cec5SDimitry Andric       Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get();
10260b57cec5SDimitry Andric       StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
10270b57cec5SDimitry Andric       // If a breakpoint can't serialize it, just ignore it for now:
10280b57cec5SDimitry Andric       if (bkpt_save_sp)
10290b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
10300b57cec5SDimitry Andric     }
10310b57cec5SDimitry Andric   } else {
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric     std::unordered_set<lldb::break_id_t> processed_bkpts;
10340b57cec5SDimitry Andric     const size_t count = bp_ids.GetSize();
10350b57cec5SDimitry Andric     for (size_t i = 0; i < count; ++i) {
10360b57cec5SDimitry Andric       BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(i);
10370b57cec5SDimitry Andric       lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID();
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric       if (bp_id != LLDB_INVALID_BREAK_ID) {
10400b57cec5SDimitry Andric         // Only do each breakpoint once:
10410b57cec5SDimitry Andric         std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool>
10420b57cec5SDimitry Andric             insert_result = processed_bkpts.insert(bp_id);
10430b57cec5SDimitry Andric         if (!insert_result.second)
10440b57cec5SDimitry Andric           continue;
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric         Breakpoint *bp = GetBreakpointByID(bp_id).get();
10470b57cec5SDimitry Andric         StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
10480b57cec5SDimitry Andric         // If the user explicitly asked to serialize a breakpoint, and we
10490b57cec5SDimitry Andric         // can't, then raise an error:
10500b57cec5SDimitry Andric         if (!bkpt_save_sp) {
10510b57cec5SDimitry Andric           error.SetErrorStringWithFormat("Unable to serialize breakpoint %d",
10520b57cec5SDimitry Andric                                          bp_id);
10530b57cec5SDimitry Andric           return error;
10540b57cec5SDimitry Andric         }
10550b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
10560b57cec5SDimitry Andric       }
10570b57cec5SDimitry Andric     }
10580b57cec5SDimitry Andric   }
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   break_store_ptr->Dump(out_file, false);
10610b57cec5SDimitry Andric   out_file.PutChar('\n');
10620b57cec5SDimitry Andric   return error;
10630b57cec5SDimitry Andric }
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
10660b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
10670b57cec5SDimitry Andric   std::vector<std::string> no_names;
10680b57cec5SDimitry Andric   return CreateBreakpointsFromFile(file, no_names, new_bps);
10690b57cec5SDimitry Andric }
10700b57cec5SDimitry Andric 
10710b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
10720b57cec5SDimitry Andric                                          std::vector<std::string> &names,
10730b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
10740b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
10750b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric   Status error;
10780b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp =
10790b57cec5SDimitry Andric       StructuredData::ParseJSONFromFile(file, error);
10800b57cec5SDimitry Andric   if (!error.Success()) {
10810b57cec5SDimitry Andric     return error;
10820b57cec5SDimitry Andric   } else if (!input_data_sp || !input_data_sp->IsValid()) {
10830b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Invalid JSON from input file: %s.",
10840b57cec5SDimitry Andric                                    file.GetPath().c_str());
10850b57cec5SDimitry Andric     return error;
10860b57cec5SDimitry Andric   }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   StructuredData::Array *bkpt_array = input_data_sp->GetAsArray();
10890b57cec5SDimitry Andric   if (!bkpt_array) {
10900b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
10910b57cec5SDimitry Andric         "Invalid breakpoint data from input file: %s.", file.GetPath().c_str());
10920b57cec5SDimitry Andric     return error;
10930b57cec5SDimitry Andric   }
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric   size_t num_bkpts = bkpt_array->GetSize();
10960b57cec5SDimitry Andric   size_t num_names = names.size();
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric   for (size_t i = 0; i < num_bkpts; i++) {
10990b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(i);
11000b57cec5SDimitry Andric     // Peel off the breakpoint key, and feed the rest to the Breakpoint:
11010b57cec5SDimitry Andric     StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary();
11020b57cec5SDimitry Andric     if (!bkpt_dict) {
11030b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
11040b57cec5SDimitry Andric           "Invalid breakpoint data for element %zu from input file: %s.", i,
11050b57cec5SDimitry Andric           file.GetPath().c_str());
11060b57cec5SDimitry Andric       return error;
11070b57cec5SDimitry Andric     }
11080b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_data_sp =
11090b57cec5SDimitry Andric         bkpt_dict->GetValueForKey(Breakpoint::GetSerializationKey());
11100b57cec5SDimitry Andric     if (num_names &&
11110b57cec5SDimitry Andric         !Breakpoint::SerializedBreakpointMatchesNames(bkpt_data_sp, names))
11120b57cec5SDimitry Andric       continue;
11130b57cec5SDimitry Andric 
11145ffd83dbSDimitry Andric     BreakpointSP bkpt_sp = Breakpoint::CreateFromStructuredData(
11155ffd83dbSDimitry Andric         shared_from_this(), bkpt_data_sp, error);
11160b57cec5SDimitry Andric     if (!error.Success()) {
11170b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
11180b57cec5SDimitry Andric           "Error restoring breakpoint %zu from %s: %s.", i,
11190b57cec5SDimitry Andric           file.GetPath().c_str(), error.AsCString());
11200b57cec5SDimitry Andric       return error;
11210b57cec5SDimitry Andric     }
11220b57cec5SDimitry Andric     new_bps.AddBreakpointID(BreakpointID(bkpt_sp->GetID()));
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric   return error;
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric // The flag 'end_to_end', default to true, signifies that the operation is
11280b57cec5SDimitry Andric // performed end to end, for both the debugger and the debuggee.
11290b57cec5SDimitry Andric 
11300b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
11310b57cec5SDimitry Andric // to end operations.
11320b57cec5SDimitry Andric bool Target::RemoveAllWatchpoints(bool end_to_end) {
11330b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11349dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric   if (!end_to_end) {
11370b57cec5SDimitry Andric     m_watchpoint_list.RemoveAll(true);
11380b57cec5SDimitry Andric     return true;
11390b57cec5SDimitry Andric   }
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric   if (!ProcessIsValid())
11440b57cec5SDimitry Andric     return false;
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
11470b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
11480b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
11490b57cec5SDimitry Andric     if (!wp_sp)
11500b57cec5SDimitry Andric       return false;
11510b57cec5SDimitry Andric 
11520b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
11530b57cec5SDimitry Andric     if (rc.Fail())
11540b57cec5SDimitry Andric       return false;
11550b57cec5SDimitry Andric   }
11560b57cec5SDimitry Andric   m_watchpoint_list.RemoveAll(true);
11570b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
11580b57cec5SDimitry Andric   return true; // Success!
11590b57cec5SDimitry Andric }
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
11620b57cec5SDimitry Andric // to end operations.
11630b57cec5SDimitry Andric bool Target::DisableAllWatchpoints(bool end_to_end) {
11640b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11659dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   if (!end_to_end) {
11680b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(false);
11690b57cec5SDimitry Andric     return true;
11700b57cec5SDimitry Andric   }
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric   if (!ProcessIsValid())
11750b57cec5SDimitry Andric     return false;
11760b57cec5SDimitry Andric 
11770b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
11780b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
11790b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
11800b57cec5SDimitry Andric     if (!wp_sp)
11810b57cec5SDimitry Andric       return false;
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
11840b57cec5SDimitry Andric     if (rc.Fail())
11850b57cec5SDimitry Andric       return false;
11860b57cec5SDimitry Andric   }
11870b57cec5SDimitry Andric   return true; // Success!
11880b57cec5SDimitry Andric }
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
11910b57cec5SDimitry Andric // to end operations.
11920b57cec5SDimitry Andric bool Target::EnableAllWatchpoints(bool end_to_end) {
11930b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11949dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric   if (!end_to_end) {
11970b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(true);
11980b57cec5SDimitry Andric     return true;
11990b57cec5SDimitry Andric   }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
12020b57cec5SDimitry Andric 
12030b57cec5SDimitry Andric   if (!ProcessIsValid())
12040b57cec5SDimitry Andric     return false;
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
12070b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
12080b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
12090b57cec5SDimitry Andric     if (!wp_sp)
12100b57cec5SDimitry Andric       return false;
12110b57cec5SDimitry Andric 
12120b57cec5SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
12130b57cec5SDimitry Andric     if (rc.Fail())
12140b57cec5SDimitry Andric       return false;
12150b57cec5SDimitry Andric   }
12160b57cec5SDimitry Andric   return true; // Success!
12170b57cec5SDimitry Andric }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
12200b57cec5SDimitry Andric bool Target::ClearAllWatchpointHitCounts() {
12210b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12229dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
12250b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
12260b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
12270b57cec5SDimitry Andric     if (!wp_sp)
12280b57cec5SDimitry Andric       return false;
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric     wp_sp->ResetHitCount();
12310b57cec5SDimitry Andric   }
12320b57cec5SDimitry Andric   return true; // Success!
12330b57cec5SDimitry Andric }
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
12360b57cec5SDimitry Andric bool Target::ClearAllWatchpointHistoricValues() {
12370b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12389dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
12410b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
12420b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
12430b57cec5SDimitry Andric     if (!wp_sp)
12440b57cec5SDimitry Andric       return false;
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric     wp_sp->ResetHistoricValues();
12470b57cec5SDimitry Andric   }
12480b57cec5SDimitry Andric   return true; // Success!
12490b57cec5SDimitry Andric }
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list during
12520b57cec5SDimitry Andric // these operations.
12530b57cec5SDimitry Andric bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) {
12540b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12559dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12560b57cec5SDimitry Andric 
12570b57cec5SDimitry Andric   if (!ProcessIsValid())
12580b57cec5SDimitry Andric     return false;
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
12610b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
12620b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
12630b57cec5SDimitry Andric     if (!wp_sp)
12640b57cec5SDimitry Andric       return false;
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
12670b57cec5SDimitry Andric   }
12680b57cec5SDimitry Andric   return true; // Success!
12690b57cec5SDimitry Andric }
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
12720b57cec5SDimitry Andric bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) {
12730b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12749dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
12750b57cec5SDimitry Andric 
12760b57cec5SDimitry Andric   if (!ProcessIsValid())
12770b57cec5SDimitry Andric     return false;
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
12800b57cec5SDimitry Andric   if (wp_sp) {
12810b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
12820b57cec5SDimitry Andric     if (rc.Success())
12830b57cec5SDimitry Andric       return true;
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric     // Else, fallthrough.
12860b57cec5SDimitry Andric   }
12870b57cec5SDimitry Andric   return false;
12880b57cec5SDimitry Andric }
12890b57cec5SDimitry Andric 
12900b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
12910b57cec5SDimitry Andric bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) {
12920b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12939dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric   if (!ProcessIsValid())
12960b57cec5SDimitry Andric     return false;
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
12990b57cec5SDimitry Andric   if (wp_sp) {
13000b57cec5SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
13010b57cec5SDimitry Andric     if (rc.Success())
13020b57cec5SDimitry Andric       return true;
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric     // Else, fallthrough.
13050b57cec5SDimitry Andric   }
13060b57cec5SDimitry Andric   return false;
13070b57cec5SDimitry Andric }
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
13100b57cec5SDimitry Andric bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) {
13110b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
13129dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric   WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
13150b57cec5SDimitry Andric   if (watch_to_remove_sp == m_last_created_watchpoint)
13160b57cec5SDimitry Andric     m_last_created_watchpoint.reset();
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric   if (DisableWatchpointByID(watch_id)) {
13190b57cec5SDimitry Andric     m_watchpoint_list.Remove(watch_id, true);
13200b57cec5SDimitry Andric     return true;
13210b57cec5SDimitry Andric   }
13220b57cec5SDimitry Andric   return false;
13230b57cec5SDimitry Andric }
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
13260b57cec5SDimitry Andric bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id,
13270b57cec5SDimitry Andric                                   uint32_t ignore_count) {
13280b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
13299dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric   if (!ProcessIsValid())
13320b57cec5SDimitry Andric     return false;
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
13350b57cec5SDimitry Andric   if (wp_sp) {
13360b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
13370b57cec5SDimitry Andric     return true;
13380b57cec5SDimitry Andric   }
13390b57cec5SDimitry Andric   return false;
13400b57cec5SDimitry Andric }
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric ModuleSP Target::GetExecutableModule() {
13430b57cec5SDimitry Andric   // search for the first executable in the module list
13440b57cec5SDimitry Andric   for (size_t i = 0; i < m_images.GetSize(); ++i) {
13450b57cec5SDimitry Andric     ModuleSP module_sp = m_images.GetModuleAtIndex(i);
13460b57cec5SDimitry Andric     lldb_private::ObjectFile *obj = module_sp->GetObjectFile();
13470b57cec5SDimitry Andric     if (obj == nullptr)
13480b57cec5SDimitry Andric       continue;
13490b57cec5SDimitry Andric     if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
13500b57cec5SDimitry Andric       return module_sp;
13510b57cec5SDimitry Andric   }
13520b57cec5SDimitry Andric   // as fall back return the first module loaded
13530b57cec5SDimitry Andric   return m_images.GetModuleAtIndex(0);
13540b57cec5SDimitry Andric }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric Module *Target::GetExecutableModulePointer() {
13570b57cec5SDimitry Andric   return GetExecutableModule().get();
13580b57cec5SDimitry Andric }
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric static void LoadScriptingResourceForModule(const ModuleSP &module_sp,
13610b57cec5SDimitry Andric                                            Target *target) {
13620b57cec5SDimitry Andric   Status error;
13630b57cec5SDimitry Andric   StreamString feedback_stream;
13649dba64beSDimitry Andric   if (module_sp && !module_sp->LoadScriptingResourceInTarget(
13659dba64beSDimitry Andric                        target, error, &feedback_stream)) {
13660b57cec5SDimitry Andric     if (error.AsCString())
13679dba64beSDimitry Andric       target->GetDebugger().GetErrorStream().Printf(
13680b57cec5SDimitry Andric           "unable to load scripting data for module %s - error reported was "
13690b57cec5SDimitry Andric           "%s\n",
13700b57cec5SDimitry Andric           module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
13710b57cec5SDimitry Andric           error.AsCString());
13720b57cec5SDimitry Andric   }
13730b57cec5SDimitry Andric   if (feedback_stream.GetSize())
13749dba64beSDimitry Andric     target->GetDebugger().GetErrorStream().Printf("%s\n",
13750b57cec5SDimitry Andric                                                   feedback_stream.GetData());
13760b57cec5SDimitry Andric }
13770b57cec5SDimitry Andric 
13780b57cec5SDimitry Andric void Target::ClearModules(bool delete_locations) {
13790b57cec5SDimitry Andric   ModulesDidUnload(m_images, delete_locations);
13800b57cec5SDimitry Andric   m_section_load_history.Clear();
13810b57cec5SDimitry Andric   m_images.Clear();
13820b57cec5SDimitry Andric   m_scratch_type_system_map.Clear();
13830b57cec5SDimitry Andric }
13840b57cec5SDimitry Andric 
13850b57cec5SDimitry Andric void Target::DidExec() {
13860b57cec5SDimitry Andric   // When a process exec's we need to know about it so we can do some cleanup.
13870b57cec5SDimitry Andric   m_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
13880b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
13890b57cec5SDimitry Andric }
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric void Target::SetExecutableModule(ModuleSP &executable_sp,
13920b57cec5SDimitry Andric                                  LoadDependentFiles load_dependent_files) {
13930b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
13940b57cec5SDimitry Andric   ClearModules(false);
13950b57cec5SDimitry Andric 
13960b57cec5SDimitry Andric   if (executable_sp) {
13970b57cec5SDimitry Andric     static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
13980b57cec5SDimitry Andric     Timer scoped_timer(func_cat,
13990b57cec5SDimitry Andric                        "Target::SetExecutableModule (executable = '%s')",
14000b57cec5SDimitry Andric                        executable_sp->GetFileSpec().GetPath().c_str());
14010b57cec5SDimitry Andric 
14020b57cec5SDimitry Andric     const bool notify = true;
14039dba64beSDimitry Andric     m_images.Append(executable_sp,
14049dba64beSDimitry Andric                     notify); // The first image is our executable file
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric     // If we haven't set an architecture yet, reset our architecture based on
14070b57cec5SDimitry Andric     // what we found in the executable module.
14080b57cec5SDimitry Andric     if (!m_arch.GetSpec().IsValid()) {
14090b57cec5SDimitry Andric       m_arch = executable_sp->GetArchitecture();
14100b57cec5SDimitry Andric       LLDB_LOG(log,
14110b57cec5SDimitry Andric                "setting architecture to {0} ({1}) based on executable file",
14120b57cec5SDimitry Andric                m_arch.GetSpec().GetArchitectureName(),
14130b57cec5SDimitry Andric                m_arch.GetSpec().GetTriple().getTriple());
14140b57cec5SDimitry Andric     }
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric     FileSpecList dependent_files;
14170b57cec5SDimitry Andric     ObjectFile *executable_objfile = executable_sp->GetObjectFile();
14180b57cec5SDimitry Andric     bool load_dependents = true;
14190b57cec5SDimitry Andric     switch (load_dependent_files) {
14200b57cec5SDimitry Andric     case eLoadDependentsDefault:
14210b57cec5SDimitry Andric       load_dependents = executable_sp->IsExecutable();
14220b57cec5SDimitry Andric       break;
14230b57cec5SDimitry Andric     case eLoadDependentsYes:
14240b57cec5SDimitry Andric       load_dependents = true;
14250b57cec5SDimitry Andric       break;
14260b57cec5SDimitry Andric     case eLoadDependentsNo:
14270b57cec5SDimitry Andric       load_dependents = false;
14280b57cec5SDimitry Andric       break;
14290b57cec5SDimitry Andric     }
14300b57cec5SDimitry Andric 
14310b57cec5SDimitry Andric     if (executable_objfile && load_dependents) {
14320b57cec5SDimitry Andric       ModuleList added_modules;
14330b57cec5SDimitry Andric       executable_objfile->GetDependentModules(dependent_files);
14340b57cec5SDimitry Andric       for (uint32_t i = 0; i < dependent_files.GetSize(); i++) {
1435480093f4SDimitry Andric         FileSpec dependent_file_spec(dependent_files.GetFileSpecAtIndex(i));
14360b57cec5SDimitry Andric         FileSpec platform_dependent_file_spec;
14370b57cec5SDimitry Andric         if (m_platform_sp)
14380b57cec5SDimitry Andric           m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr,
14390b57cec5SDimitry Andric                                          platform_dependent_file_spec);
14400b57cec5SDimitry Andric         else
14410b57cec5SDimitry Andric           platform_dependent_file_spec = dependent_file_spec;
14420b57cec5SDimitry Andric 
14430b57cec5SDimitry Andric         ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec());
14449dba64beSDimitry Andric         ModuleSP image_module_sp(
14459dba64beSDimitry Andric             GetOrCreateModule(module_spec, false /* notify */));
14460b57cec5SDimitry Andric         if (image_module_sp) {
14470b57cec5SDimitry Andric           added_modules.AppendIfNeeded(image_module_sp, false);
14480b57cec5SDimitry Andric           ObjectFile *objfile = image_module_sp->GetObjectFile();
14490b57cec5SDimitry Andric           if (objfile)
14500b57cec5SDimitry Andric             objfile->GetDependentModules(dependent_files);
14510b57cec5SDimitry Andric         }
14520b57cec5SDimitry Andric       }
14530b57cec5SDimitry Andric       ModulesDidLoad(added_modules);
14540b57cec5SDimitry Andric     }
14550b57cec5SDimitry Andric   }
14560b57cec5SDimitry Andric }
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform) {
14590b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
14600b57cec5SDimitry Andric   bool missing_local_arch = !m_arch.GetSpec().IsValid();
14610b57cec5SDimitry Andric   bool replace_local_arch = true;
14620b57cec5SDimitry Andric   bool compatible_local_arch = false;
14630b57cec5SDimitry Andric   ArchSpec other(arch_spec);
14640b57cec5SDimitry Andric 
14650b57cec5SDimitry Andric   // Changing the architecture might mean that the currently selected platform
14660b57cec5SDimitry Andric   // isn't compatible. Set the platform correctly if we are asked to do so,
14670b57cec5SDimitry Andric   // otherwise assume the user will set the platform manually.
14680b57cec5SDimitry Andric   if (set_platform) {
14690b57cec5SDimitry Andric     if (other.IsValid()) {
14700b57cec5SDimitry Andric       auto platform_sp = GetPlatform();
14710b57cec5SDimitry Andric       if (!platform_sp ||
14720b57cec5SDimitry Andric           !platform_sp->IsCompatibleArchitecture(other, false, nullptr)) {
14730b57cec5SDimitry Andric         ArchSpec platform_arch;
14740b57cec5SDimitry Andric         auto arch_platform_sp =
14750b57cec5SDimitry Andric             Platform::GetPlatformForArchitecture(other, &platform_arch);
14760b57cec5SDimitry Andric         if (arch_platform_sp) {
14770b57cec5SDimitry Andric           SetPlatform(arch_platform_sp);
14780b57cec5SDimitry Andric           if (platform_arch.IsValid())
14790b57cec5SDimitry Andric             other = platform_arch;
14800b57cec5SDimitry Andric         }
14810b57cec5SDimitry Andric       }
14820b57cec5SDimitry Andric     }
14830b57cec5SDimitry Andric   }
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric   if (!missing_local_arch) {
14860b57cec5SDimitry Andric     if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
14870b57cec5SDimitry Andric       other.MergeFrom(m_arch.GetSpec());
14880b57cec5SDimitry Andric 
14890b57cec5SDimitry Andric       if (m_arch.GetSpec().IsCompatibleMatch(other)) {
14900b57cec5SDimitry Andric         compatible_local_arch = true;
14910b57cec5SDimitry Andric         bool arch_changed, vendor_changed, os_changed, os_ver_changed,
14920b57cec5SDimitry Andric             env_changed;
14930b57cec5SDimitry Andric 
14949dba64beSDimitry Andric         m_arch.GetSpec().PiecewiseTripleCompare(other, arch_changed,
14959dba64beSDimitry Andric                                                 vendor_changed, os_changed,
14969dba64beSDimitry Andric                                                 os_ver_changed, env_changed);
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric         if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
14990b57cec5SDimitry Andric           replace_local_arch = false;
15000b57cec5SDimitry Andric       }
15010b57cec5SDimitry Andric     }
15020b57cec5SDimitry Andric   }
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   if (compatible_local_arch || missing_local_arch) {
15050b57cec5SDimitry Andric     // If we haven't got a valid arch spec, or the architectures are compatible
15060b57cec5SDimitry Andric     // update the architecture, unless the one we already have is more
15070b57cec5SDimitry Andric     // specified
15080b57cec5SDimitry Andric     if (replace_local_arch)
15090b57cec5SDimitry Andric       m_arch = other;
15100b57cec5SDimitry Andric     LLDB_LOG(log, "set architecture to {0} ({1})",
15110b57cec5SDimitry Andric              m_arch.GetSpec().GetArchitectureName(),
15120b57cec5SDimitry Andric              m_arch.GetSpec().GetTriple().getTriple());
15130b57cec5SDimitry Andric     return true;
15140b57cec5SDimitry Andric   }
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric   // If we have an executable file, try to reset the executable to the desired
15170b57cec5SDimitry Andric   // architecture
15189dba64beSDimitry Andric   LLDB_LOGF(log, "Target::SetArchitecture changing architecture to %s (%s)",
15190b57cec5SDimitry Andric             arch_spec.GetArchitectureName(),
15200b57cec5SDimitry Andric             arch_spec.GetTriple().getTriple().c_str());
15210b57cec5SDimitry Andric   m_arch = other;
15220b57cec5SDimitry Andric   ModuleSP executable_sp = GetExecutableModule();
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric   ClearModules(true);
15250b57cec5SDimitry Andric   // Need to do something about unsetting breakpoints.
15260b57cec5SDimitry Andric 
15270b57cec5SDimitry Andric   if (executable_sp) {
15289dba64beSDimitry Andric     LLDB_LOGF(log,
15299dba64beSDimitry Andric               "Target::SetArchitecture Trying to select executable file "
15300b57cec5SDimitry Andric               "architecture %s (%s)",
15310b57cec5SDimitry Andric               arch_spec.GetArchitectureName(),
15320b57cec5SDimitry Andric               arch_spec.GetTriple().getTriple().c_str());
15330b57cec5SDimitry Andric     ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
15340b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
15350b57cec5SDimitry Andric     Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
15369dba64beSDimitry Andric                                                &search_paths, nullptr, nullptr);
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric     if (!error.Fail() && executable_sp) {
15390b57cec5SDimitry Andric       SetExecutableModule(executable_sp, eLoadDependentsYes);
15400b57cec5SDimitry Andric       return true;
15410b57cec5SDimitry Andric     }
15420b57cec5SDimitry Andric   }
15430b57cec5SDimitry Andric   return false;
15440b57cec5SDimitry Andric }
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric bool Target::MergeArchitecture(const ArchSpec &arch_spec) {
15470b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
15480b57cec5SDimitry Andric   if (arch_spec.IsValid()) {
15490b57cec5SDimitry Andric     if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
15500b57cec5SDimitry Andric       // The current target arch is compatible with "arch_spec", see if we can
15510b57cec5SDimitry Andric       // improve our current architecture using bits from "arch_spec"
15520b57cec5SDimitry Andric 
15539dba64beSDimitry Andric       LLDB_LOGF(log,
15549dba64beSDimitry Andric                 "Target::MergeArchitecture target has arch %s, merging with "
15550b57cec5SDimitry Andric                 "arch %s",
15560b57cec5SDimitry Andric                 m_arch.GetSpec().GetTriple().getTriple().c_str(),
15570b57cec5SDimitry Andric                 arch_spec.GetTriple().getTriple().c_str());
15580b57cec5SDimitry Andric 
15590b57cec5SDimitry Andric       // Merge bits from arch_spec into "merged_arch" and set our architecture
15600b57cec5SDimitry Andric       ArchSpec merged_arch(m_arch.GetSpec());
15610b57cec5SDimitry Andric       merged_arch.MergeFrom(arch_spec);
15620b57cec5SDimitry Andric       return SetArchitecture(merged_arch);
15630b57cec5SDimitry Andric     } else {
15640b57cec5SDimitry Andric       // The new architecture is different, we just need to replace it
15650b57cec5SDimitry Andric       return SetArchitecture(arch_spec);
15660b57cec5SDimitry Andric     }
15670b57cec5SDimitry Andric   }
15680b57cec5SDimitry Andric   return false;
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric void Target::NotifyWillClearList(const ModuleList &module_list) {}
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric void Target::NotifyModuleAdded(const ModuleList &module_list,
15740b57cec5SDimitry Andric                                const ModuleSP &module_sp) {
15750b57cec5SDimitry Andric   // A module is being added to this target for the first time
15760b57cec5SDimitry Andric   if (m_valid) {
15770b57cec5SDimitry Andric     ModuleList my_module_list;
15780b57cec5SDimitry Andric     my_module_list.Append(module_sp);
15790b57cec5SDimitry Andric     ModulesDidLoad(my_module_list);
15800b57cec5SDimitry Andric   }
15810b57cec5SDimitry Andric }
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric void Target::NotifyModuleRemoved(const ModuleList &module_list,
15840b57cec5SDimitry Andric                                  const ModuleSP &module_sp) {
15850b57cec5SDimitry Andric   // A module is being removed from this target.
15860b57cec5SDimitry Andric   if (m_valid) {
15870b57cec5SDimitry Andric     ModuleList my_module_list;
15880b57cec5SDimitry Andric     my_module_list.Append(module_sp);
15890b57cec5SDimitry Andric     ModulesDidUnload(my_module_list, false);
15900b57cec5SDimitry Andric   }
15910b57cec5SDimitry Andric }
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric void Target::NotifyModuleUpdated(const ModuleList &module_list,
15940b57cec5SDimitry Andric                                  const ModuleSP &old_module_sp,
15950b57cec5SDimitry Andric                                  const ModuleSP &new_module_sp) {
15960b57cec5SDimitry Andric   // A module is replacing an already added module
15970b57cec5SDimitry Andric   if (m_valid) {
15980b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp,
15990b57cec5SDimitry Andric                                                             new_module_sp);
16000b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(
16010b57cec5SDimitry Andric         old_module_sp, new_module_sp);
16020b57cec5SDimitry Andric   }
16030b57cec5SDimitry Andric }
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) {
16060b57cec5SDimitry Andric   ModulesDidUnload(module_list, false);
16070b57cec5SDimitry Andric }
16080b57cec5SDimitry Andric 
16090b57cec5SDimitry Andric void Target::ModulesDidLoad(ModuleList &module_list) {
16100b57cec5SDimitry Andric   const size_t num_images = module_list.GetSize();
16110b57cec5SDimitry Andric   if (m_valid && num_images) {
16120b57cec5SDimitry Andric     for (size_t idx = 0; idx < num_images; ++idx) {
16130b57cec5SDimitry Andric       ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
16140b57cec5SDimitry Andric       LoadScriptingResourceForModule(module_sp, this);
16150b57cec5SDimitry Andric     }
16160b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
16170b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
16180b57cec5SDimitry Andric     if (m_process_sp) {
16190b57cec5SDimitry Andric       m_process_sp->ModulesDidLoad(module_list);
16200b57cec5SDimitry Andric     }
16210b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitModulesLoaded,
16220b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
16230b57cec5SDimitry Andric   }
16240b57cec5SDimitry Andric }
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric void Target::SymbolsDidLoad(ModuleList &module_list) {
16270b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
16280b57cec5SDimitry Andric     if (m_process_sp) {
16290b57cec5SDimitry Andric       for (LanguageRuntime *runtime : m_process_sp->GetLanguageRuntimes()) {
16300b57cec5SDimitry Andric         runtime->SymbolsDidLoad(module_list);
16310b57cec5SDimitry Andric       }
16320b57cec5SDimitry Andric     }
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
16350b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
16360b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitSymbolsLoaded,
16370b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
16380b57cec5SDimitry Andric   }
16390b57cec5SDimitry Andric }
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) {
16420b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
16430b57cec5SDimitry Andric     UnloadModuleSections(module_list);
16440b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, false, delete_locations);
16450b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, false,
16460b57cec5SDimitry Andric                                                  delete_locations);
16470b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitModulesUnloaded,
16480b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
16490b57cec5SDimitry Andric   }
16500b57cec5SDimitry Andric }
16510b57cec5SDimitry Andric 
16520b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
16530b57cec5SDimitry Andric     const FileSpec &module_file_spec) {
16540b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
16550b57cec5SDimitry Andric     ModuleList matchingModules;
16560b57cec5SDimitry Andric     ModuleSpec module_spec(module_file_spec);
16579dba64beSDimitry Andric     GetImages().FindModules(module_spec, matchingModules);
16589dba64beSDimitry Andric     size_t num_modules = matchingModules.GetSize();
16590b57cec5SDimitry Andric 
16609dba64beSDimitry Andric     // If there is more than one module for this file spec, only
16619dba64beSDimitry Andric     // return true if ALL the modules are on the black list.
16620b57cec5SDimitry Andric     if (num_modules > 0) {
16630b57cec5SDimitry Andric       for (size_t i = 0; i < num_modules; i++) {
16640b57cec5SDimitry Andric         if (!ModuleIsExcludedForUnconstrainedSearches(
16650b57cec5SDimitry Andric                 matchingModules.GetModuleAtIndex(i)))
16660b57cec5SDimitry Andric           return false;
16670b57cec5SDimitry Andric       }
16680b57cec5SDimitry Andric       return true;
16690b57cec5SDimitry Andric     }
16700b57cec5SDimitry Andric   }
16710b57cec5SDimitry Andric   return false;
16720b57cec5SDimitry Andric }
16730b57cec5SDimitry Andric 
16740b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
16750b57cec5SDimitry Andric     const lldb::ModuleSP &module_sp) {
16760b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
16770b57cec5SDimitry Andric     if (m_platform_sp)
16780b57cec5SDimitry Andric       return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(*this,
16790b57cec5SDimitry Andric                                                                      module_sp);
16800b57cec5SDimitry Andric   }
16810b57cec5SDimitry Andric   return false;
16820b57cec5SDimitry Andric }
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst,
16850b57cec5SDimitry Andric                                        size_t dst_len, Status &error) {
16860b57cec5SDimitry Andric   SectionSP section_sp(addr.GetSection());
16870b57cec5SDimitry Andric   if (section_sp) {
16880b57cec5SDimitry Andric     // If the contents of this section are encrypted, the on-disk file is
16890b57cec5SDimitry Andric     // unusable.  Read only from live memory.
16900b57cec5SDimitry Andric     if (section_sp->IsEncrypted()) {
16910b57cec5SDimitry Andric       error.SetErrorString("section is encrypted");
16920b57cec5SDimitry Andric       return 0;
16930b57cec5SDimitry Andric     }
16940b57cec5SDimitry Andric     ModuleSP module_sp(section_sp->GetModule());
16950b57cec5SDimitry Andric     if (module_sp) {
16960b57cec5SDimitry Andric       ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
16970b57cec5SDimitry Andric       if (objfile) {
16980b57cec5SDimitry Andric         size_t bytes_read = objfile->ReadSectionData(
16990b57cec5SDimitry Andric             section_sp.get(), addr.GetOffset(), dst, dst_len);
17000b57cec5SDimitry Andric         if (bytes_read > 0)
17010b57cec5SDimitry Andric           return bytes_read;
17020b57cec5SDimitry Andric         else
17030b57cec5SDimitry Andric           error.SetErrorStringWithFormat("error reading data from section %s",
17040b57cec5SDimitry Andric                                          section_sp->GetName().GetCString());
17050b57cec5SDimitry Andric       } else
17060b57cec5SDimitry Andric         error.SetErrorString("address isn't from a object file");
17070b57cec5SDimitry Andric     } else
17080b57cec5SDimitry Andric       error.SetErrorString("address isn't in a module");
17090b57cec5SDimitry Andric   } else
17100b57cec5SDimitry Andric     error.SetErrorString("address doesn't contain a section that points to a "
17110b57cec5SDimitry Andric                          "section in a object file");
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric   return 0;
17140b57cec5SDimitry Andric }
17150b57cec5SDimitry Andric 
17160b57cec5SDimitry Andric size_t Target::ReadMemory(const Address &addr, bool prefer_file_cache,
17170b57cec5SDimitry Andric                           void *dst, size_t dst_len, Status &error,
17180b57cec5SDimitry Andric                           lldb::addr_t *load_addr_ptr) {
17190b57cec5SDimitry Andric   error.Clear();
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   // if we end up reading this from process memory, we will fill this with the
17220b57cec5SDimitry Andric   // actual load address
17230b57cec5SDimitry Andric   if (load_addr_ptr)
17240b57cec5SDimitry Andric     *load_addr_ptr = LLDB_INVALID_ADDRESS;
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric   size_t bytes_read = 0;
17270b57cec5SDimitry Andric 
17280b57cec5SDimitry Andric   addr_t load_addr = LLDB_INVALID_ADDRESS;
17290b57cec5SDimitry Andric   addr_t file_addr = LLDB_INVALID_ADDRESS;
17300b57cec5SDimitry Andric   Address resolved_addr;
17310b57cec5SDimitry Andric   if (!addr.IsSectionOffset()) {
17320b57cec5SDimitry Andric     SectionLoadList &section_load_list = GetSectionLoadList();
17330b57cec5SDimitry Andric     if (section_load_list.IsEmpty()) {
17340b57cec5SDimitry Andric       // No sections are loaded, so we must assume we are not running yet and
17350b57cec5SDimitry Andric       // anything we are given is a file address.
17360b57cec5SDimitry Andric       file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
17370b57cec5SDimitry Andric                                     // offset is the file address
17380b57cec5SDimitry Andric       m_images.ResolveFileAddress(file_addr, resolved_addr);
17390b57cec5SDimitry Andric     } else {
17400b57cec5SDimitry Andric       // We have at least one section loaded. This can be because we have
17410b57cec5SDimitry Andric       // manually loaded some sections with "target modules load ..." or
17420b57cec5SDimitry Andric       // because we have have a live process that has sections loaded through
17430b57cec5SDimitry Andric       // the dynamic loader
17440b57cec5SDimitry Andric       load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
17450b57cec5SDimitry Andric                                     // offset is the load address
17460b57cec5SDimitry Andric       section_load_list.ResolveLoadAddress(load_addr, resolved_addr);
17470b57cec5SDimitry Andric     }
17480b57cec5SDimitry Andric   }
17490b57cec5SDimitry Andric   if (!resolved_addr.IsValid())
17500b57cec5SDimitry Andric     resolved_addr = addr;
17510b57cec5SDimitry Andric 
17520b57cec5SDimitry Andric   if (prefer_file_cache) {
17530b57cec5SDimitry Andric     bytes_read = ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
17540b57cec5SDimitry Andric     if (bytes_read > 0)
17550b57cec5SDimitry Andric       return bytes_read;
17560b57cec5SDimitry Andric   }
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   if (ProcessIsValid()) {
17590b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS)
17600b57cec5SDimitry Andric       load_addr = resolved_addr.GetLoadAddress(this);
17610b57cec5SDimitry Andric 
17620b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS) {
17630b57cec5SDimitry Andric       ModuleSP addr_module_sp(resolved_addr.GetModule());
17640b57cec5SDimitry Andric       if (addr_module_sp && addr_module_sp->GetFileSpec())
17650b57cec5SDimitry Andric         error.SetErrorStringWithFormatv(
17660b57cec5SDimitry Andric             "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded",
17670b57cec5SDimitry Andric             addr_module_sp->GetFileSpec(), resolved_addr.GetFileAddress());
17680b57cec5SDimitry Andric       else
17690b57cec5SDimitry Andric         error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved",
17700b57cec5SDimitry Andric                                        resolved_addr.GetFileAddress());
17710b57cec5SDimitry Andric     } else {
17720b57cec5SDimitry Andric       bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
17730b57cec5SDimitry Andric       if (bytes_read != dst_len) {
17740b57cec5SDimitry Andric         if (error.Success()) {
17750b57cec5SDimitry Andric           if (bytes_read == 0)
17760b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
17770b57cec5SDimitry Andric                 "read memory from 0x%" PRIx64 " failed", load_addr);
17780b57cec5SDimitry Andric           else
17790b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
17800b57cec5SDimitry Andric                 "only %" PRIu64 " of %" PRIu64
17810b57cec5SDimitry Andric                 " bytes were read from memory at 0x%" PRIx64,
17820b57cec5SDimitry Andric                 (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
17830b57cec5SDimitry Andric         }
17840b57cec5SDimitry Andric       }
17850b57cec5SDimitry Andric       if (bytes_read) {
17860b57cec5SDimitry Andric         if (load_addr_ptr)
17870b57cec5SDimitry Andric           *load_addr_ptr = load_addr;
17880b57cec5SDimitry Andric         return bytes_read;
17890b57cec5SDimitry Andric       }
17900b57cec5SDimitry Andric       // If the address is not section offset we have an address that doesn't
17910b57cec5SDimitry Andric       // resolve to any address in any currently loaded shared libraries and we
17920b57cec5SDimitry Andric       // failed to read memory so there isn't anything more we can do. If it is
17930b57cec5SDimitry Andric       // section offset, we might be able to read cached memory from the object
17940b57cec5SDimitry Andric       // file.
17950b57cec5SDimitry Andric       if (!resolved_addr.IsSectionOffset())
17960b57cec5SDimitry Andric         return 0;
17970b57cec5SDimitry Andric     }
17980b57cec5SDimitry Andric   }
17990b57cec5SDimitry Andric 
18000b57cec5SDimitry Andric   if (!prefer_file_cache && resolved_addr.IsSectionOffset()) {
18010b57cec5SDimitry Andric     // If we didn't already try and read from the object file cache, then try
18020b57cec5SDimitry Andric     // it after failing to read from the process.
18030b57cec5SDimitry Andric     return ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
18040b57cec5SDimitry Andric   }
18050b57cec5SDimitry Andric   return 0;
18060b57cec5SDimitry Andric }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str,
18090b57cec5SDimitry Andric                                      Status &error) {
18100b57cec5SDimitry Andric   char buf[256];
18110b57cec5SDimitry Andric   out_str.clear();
18120b57cec5SDimitry Andric   addr_t curr_addr = addr.GetLoadAddress(this);
18130b57cec5SDimitry Andric   Address address(addr);
18140b57cec5SDimitry Andric   while (true) {
18150b57cec5SDimitry Andric     size_t length = ReadCStringFromMemory(address, buf, sizeof(buf), error);
18160b57cec5SDimitry Andric     if (length == 0)
18170b57cec5SDimitry Andric       break;
18180b57cec5SDimitry Andric     out_str.append(buf, length);
18190b57cec5SDimitry Andric     // If we got "length - 1" bytes, we didn't get the whole C string, we need
18200b57cec5SDimitry Andric     // to read some more characters
18210b57cec5SDimitry Andric     if (length == sizeof(buf) - 1)
18220b57cec5SDimitry Andric       curr_addr += length;
18230b57cec5SDimitry Andric     else
18240b57cec5SDimitry Andric       break;
18250b57cec5SDimitry Andric     address = Address(curr_addr);
18260b57cec5SDimitry Andric   }
18270b57cec5SDimitry Andric   return out_str.size();
18280b57cec5SDimitry Andric }
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, char *dst,
18310b57cec5SDimitry Andric                                      size_t dst_max_len, Status &result_error) {
18320b57cec5SDimitry Andric   size_t total_cstr_len = 0;
18330b57cec5SDimitry Andric   if (dst && dst_max_len) {
18340b57cec5SDimitry Andric     result_error.Clear();
18350b57cec5SDimitry Andric     // NULL out everything just to be safe
18360b57cec5SDimitry Andric     memset(dst, 0, dst_max_len);
18370b57cec5SDimitry Andric     Status error;
18380b57cec5SDimitry Andric     addr_t curr_addr = addr.GetLoadAddress(this);
18390b57cec5SDimitry Andric     Address address(addr);
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric     // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think
18420b57cec5SDimitry Andric     // this really needs to be tied to the memory cache subsystem's cache line
18430b57cec5SDimitry Andric     // size, so leave this as a fixed constant.
18440b57cec5SDimitry Andric     const size_t cache_line_size = 512;
18450b57cec5SDimitry Andric 
18460b57cec5SDimitry Andric     size_t bytes_left = dst_max_len - 1;
18470b57cec5SDimitry Andric     char *curr_dst = dst;
18480b57cec5SDimitry Andric 
18490b57cec5SDimitry Andric     while (bytes_left > 0) {
18500b57cec5SDimitry Andric       addr_t cache_line_bytes_left =
18510b57cec5SDimitry Andric           cache_line_size - (curr_addr % cache_line_size);
18520b57cec5SDimitry Andric       addr_t bytes_to_read =
18530b57cec5SDimitry Andric           std::min<addr_t>(bytes_left, cache_line_bytes_left);
18540b57cec5SDimitry Andric       size_t bytes_read =
18550b57cec5SDimitry Andric           ReadMemory(address, false, curr_dst, bytes_to_read, error);
18560b57cec5SDimitry Andric 
18570b57cec5SDimitry Andric       if (bytes_read == 0) {
18580b57cec5SDimitry Andric         result_error = error;
18590b57cec5SDimitry Andric         dst[total_cstr_len] = '\0';
18600b57cec5SDimitry Andric         break;
18610b57cec5SDimitry Andric       }
18620b57cec5SDimitry Andric       const size_t len = strlen(curr_dst);
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric       total_cstr_len += len;
18650b57cec5SDimitry Andric 
18660b57cec5SDimitry Andric       if (len < bytes_to_read)
18670b57cec5SDimitry Andric         break;
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric       curr_dst += bytes_read;
18700b57cec5SDimitry Andric       curr_addr += bytes_read;
18710b57cec5SDimitry Andric       bytes_left -= bytes_read;
18720b57cec5SDimitry Andric       address = Address(curr_addr);
18730b57cec5SDimitry Andric     }
18740b57cec5SDimitry Andric   } else {
18750b57cec5SDimitry Andric     if (dst == nullptr)
18760b57cec5SDimitry Andric       result_error.SetErrorString("invalid arguments");
18770b57cec5SDimitry Andric     else
18780b57cec5SDimitry Andric       result_error.Clear();
18790b57cec5SDimitry Andric   }
18800b57cec5SDimitry Andric   return total_cstr_len;
18810b57cec5SDimitry Andric }
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric size_t Target::ReadScalarIntegerFromMemory(const Address &addr,
18840b57cec5SDimitry Andric                                            bool prefer_file_cache,
18850b57cec5SDimitry Andric                                            uint32_t byte_size, bool is_signed,
18860b57cec5SDimitry Andric                                            Scalar &scalar, Status &error) {
18870b57cec5SDimitry Andric   uint64_t uval;
18880b57cec5SDimitry Andric 
18890b57cec5SDimitry Andric   if (byte_size <= sizeof(uval)) {
18900b57cec5SDimitry Andric     size_t bytes_read =
18910b57cec5SDimitry Andric         ReadMemory(addr, prefer_file_cache, &uval, byte_size, error);
18920b57cec5SDimitry Andric     if (bytes_read == byte_size) {
18930b57cec5SDimitry Andric       DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(),
18940b57cec5SDimitry Andric                          m_arch.GetSpec().GetAddressByteSize());
18950b57cec5SDimitry Andric       lldb::offset_t offset = 0;
18960b57cec5SDimitry Andric       if (byte_size <= 4)
18970b57cec5SDimitry Andric         scalar = data.GetMaxU32(&offset, byte_size);
18980b57cec5SDimitry Andric       else
18990b57cec5SDimitry Andric         scalar = data.GetMaxU64(&offset, byte_size);
19000b57cec5SDimitry Andric 
19010b57cec5SDimitry Andric       if (is_signed)
19020b57cec5SDimitry Andric         scalar.SignExtend(byte_size * 8);
19030b57cec5SDimitry Andric       return bytes_read;
19040b57cec5SDimitry Andric     }
19050b57cec5SDimitry Andric   } else {
19060b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
19070b57cec5SDimitry Andric         "byte size of %u is too large for integer scalar type", byte_size);
19080b57cec5SDimitry Andric   }
19090b57cec5SDimitry Andric   return 0;
19100b57cec5SDimitry Andric }
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr,
19130b57cec5SDimitry Andric                                                bool prefer_file_cache,
19140b57cec5SDimitry Andric                                                size_t integer_byte_size,
19150b57cec5SDimitry Andric                                                uint64_t fail_value,
19160b57cec5SDimitry Andric                                                Status &error) {
19170b57cec5SDimitry Andric   Scalar scalar;
19180b57cec5SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, prefer_file_cache, integer_byte_size,
19190b57cec5SDimitry Andric                                   false, scalar, error))
19200b57cec5SDimitry Andric     return scalar.ULongLong(fail_value);
19210b57cec5SDimitry Andric   return fail_value;
19220b57cec5SDimitry Andric }
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric bool Target::ReadPointerFromMemory(const Address &addr, bool prefer_file_cache,
19250b57cec5SDimitry Andric                                    Status &error, Address &pointer_addr) {
19260b57cec5SDimitry Andric   Scalar scalar;
19270b57cec5SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, prefer_file_cache,
19289dba64beSDimitry Andric                                   m_arch.GetSpec().GetAddressByteSize(), false,
19299dba64beSDimitry Andric                                   scalar, error)) {
19300b57cec5SDimitry Andric     addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
19310b57cec5SDimitry Andric     if (pointer_vm_addr != LLDB_INVALID_ADDRESS) {
19320b57cec5SDimitry Andric       SectionLoadList &section_load_list = GetSectionLoadList();
19330b57cec5SDimitry Andric       if (section_load_list.IsEmpty()) {
19340b57cec5SDimitry Andric         // No sections are loaded, so we must assume we are not running yet and
19350b57cec5SDimitry Andric         // anything we are given is a file address.
19360b57cec5SDimitry Andric         m_images.ResolveFileAddress(pointer_vm_addr, pointer_addr);
19370b57cec5SDimitry Andric       } else {
19380b57cec5SDimitry Andric         // We have at least one section loaded. This can be because we have
19390b57cec5SDimitry Andric         // manually loaded some sections with "target modules load ..." or
19400b57cec5SDimitry Andric         // because we have have a live process that has sections loaded through
19410b57cec5SDimitry Andric         // the dynamic loader
19420b57cec5SDimitry Andric         section_load_list.ResolveLoadAddress(pointer_vm_addr, pointer_addr);
19430b57cec5SDimitry Andric       }
19440b57cec5SDimitry Andric       // We weren't able to resolve the pointer value, so just return an
19450b57cec5SDimitry Andric       // address with no section
19460b57cec5SDimitry Andric       if (!pointer_addr.IsValid())
19470b57cec5SDimitry Andric         pointer_addr.SetOffset(pointer_vm_addr);
19480b57cec5SDimitry Andric       return true;
19490b57cec5SDimitry Andric     }
19500b57cec5SDimitry Andric   }
19510b57cec5SDimitry Andric   return false;
19520b57cec5SDimitry Andric }
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
19550b57cec5SDimitry Andric                                    Status *error_ptr) {
19560b57cec5SDimitry Andric   ModuleSP module_sp;
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric   Status error;
19590b57cec5SDimitry Andric 
19600b57cec5SDimitry Andric   // First see if we already have this module in our module list.  If we do,
19610b57cec5SDimitry Andric   // then we're done, we don't need to consult the shared modules list.  But
19620b57cec5SDimitry Andric   // only do this if we are passed a UUID.
19630b57cec5SDimitry Andric 
19640b57cec5SDimitry Andric   if (module_spec.GetUUID().IsValid())
19650b57cec5SDimitry Andric     module_sp = m_images.FindFirstModule(module_spec);
19660b57cec5SDimitry Andric 
19670b57cec5SDimitry Andric   if (!module_sp) {
19680b57cec5SDimitry Andric     ModuleSP old_module_sp; // This will get filled in if we have a new version
19690b57cec5SDimitry Andric                             // of the library
19700b57cec5SDimitry Andric     bool did_create_module = false;
19710b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
19720b57cec5SDimitry Andric     // If there are image search path entries, try to use them first to acquire
19730b57cec5SDimitry Andric     // a suitable image.
19740b57cec5SDimitry Andric     if (m_image_search_paths.GetSize()) {
19750b57cec5SDimitry Andric       ModuleSpec transformed_spec(module_spec);
19760b57cec5SDimitry Andric       if (m_image_search_paths.RemapPath(
19770b57cec5SDimitry Andric               module_spec.GetFileSpec().GetDirectory(),
19780b57cec5SDimitry Andric               transformed_spec.GetFileSpec().GetDirectory())) {
19790b57cec5SDimitry Andric         transformed_spec.GetFileSpec().GetFilename() =
19800b57cec5SDimitry Andric             module_spec.GetFileSpec().GetFilename();
19810b57cec5SDimitry Andric         error = ModuleList::GetSharedModule(transformed_spec, module_sp,
19829dba64beSDimitry Andric                                             &search_paths, &old_module_sp,
19839dba64beSDimitry Andric                                             &did_create_module);
19840b57cec5SDimitry Andric       }
19850b57cec5SDimitry Andric     }
19860b57cec5SDimitry Andric 
19870b57cec5SDimitry Andric     if (!module_sp) {
19880b57cec5SDimitry Andric       // If we have a UUID, we can check our global shared module list in case
19890b57cec5SDimitry Andric       // we already have it. If we don't have a valid UUID, then we can't since
19900b57cec5SDimitry Andric       // the path in "module_spec" will be a platform path, and we will need to
19910b57cec5SDimitry Andric       // let the platform find that file. For example, we could be asking for
19920b57cec5SDimitry Andric       // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
19930b57cec5SDimitry Andric       // the local copy of "/usr/lib/dyld" since our platform could be a remote
19940b57cec5SDimitry Andric       // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
19950b57cec5SDimitry Andric       // cache.
19960b57cec5SDimitry Andric       if (module_spec.GetUUID().IsValid()) {
19970b57cec5SDimitry Andric         // We have a UUID, it is OK to check the global module list...
19989dba64beSDimitry Andric         error =
19999dba64beSDimitry Andric             ModuleList::GetSharedModule(module_spec, module_sp, &search_paths,
20000b57cec5SDimitry Andric                                         &old_module_sp, &did_create_module);
20010b57cec5SDimitry Andric       }
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric       if (!module_sp) {
20040b57cec5SDimitry Andric         // The platform is responsible for finding and caching an appropriate
20050b57cec5SDimitry Andric         // module in the shared module cache.
20060b57cec5SDimitry Andric         if (m_platform_sp) {
20070b57cec5SDimitry Andric           error = m_platform_sp->GetSharedModule(
20089dba64beSDimitry Andric               module_spec, m_process_sp.get(), module_sp, &search_paths,
20099dba64beSDimitry Andric               &old_module_sp, &did_create_module);
20100b57cec5SDimitry Andric         } else {
20110b57cec5SDimitry Andric           error.SetErrorString("no platform is currently set");
20120b57cec5SDimitry Andric         }
20130b57cec5SDimitry Andric       }
20140b57cec5SDimitry Andric     }
20150b57cec5SDimitry Andric 
20160b57cec5SDimitry Andric     // We found a module that wasn't in our target list.  Let's make sure that
20170b57cec5SDimitry Andric     // there wasn't an equivalent module in the list already, and if there was,
20180b57cec5SDimitry Andric     // let's remove it.
20190b57cec5SDimitry Andric     if (module_sp) {
20200b57cec5SDimitry Andric       ObjectFile *objfile = module_sp->GetObjectFile();
20210b57cec5SDimitry Andric       if (objfile) {
20220b57cec5SDimitry Andric         switch (objfile->GetType()) {
20230b57cec5SDimitry Andric         case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of
20240b57cec5SDimitry Andric                                         /// a program's execution state
20250b57cec5SDimitry Andric         case ObjectFile::eTypeExecutable:    /// A normal executable
20260b57cec5SDimitry Andric         case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker
20270b57cec5SDimitry Andric                                              /// executable
20280b57cec5SDimitry Andric         case ObjectFile::eTypeObjectFile:    /// An intermediate object file
20290b57cec5SDimitry Andric         case ObjectFile::eTypeSharedLibrary: /// A shared library that can be
20300b57cec5SDimitry Andric                                              /// used during execution
20310b57cec5SDimitry Andric           break;
20320b57cec5SDimitry Andric         case ObjectFile::eTypeDebugInfo: /// An object file that contains only
20330b57cec5SDimitry Andric                                          /// debug information
20340b57cec5SDimitry Andric           if (error_ptr)
20350b57cec5SDimitry Andric             error_ptr->SetErrorString("debug info files aren't valid target "
20360b57cec5SDimitry Andric                                       "modules, please specify an executable");
20370b57cec5SDimitry Andric           return ModuleSP();
20380b57cec5SDimitry Andric         case ObjectFile::eTypeStubLibrary: /// A library that can be linked
20390b57cec5SDimitry Andric                                            /// against but not used for
20400b57cec5SDimitry Andric                                            /// execution
20410b57cec5SDimitry Andric           if (error_ptr)
20420b57cec5SDimitry Andric             error_ptr->SetErrorString("stub libraries aren't valid target "
20430b57cec5SDimitry Andric                                       "modules, please specify an executable");
20440b57cec5SDimitry Andric           return ModuleSP();
20450b57cec5SDimitry Andric         default:
20460b57cec5SDimitry Andric           if (error_ptr)
20470b57cec5SDimitry Andric             error_ptr->SetErrorString(
20480b57cec5SDimitry Andric                 "unsupported file type, please specify an executable");
20490b57cec5SDimitry Andric           return ModuleSP();
20500b57cec5SDimitry Andric         }
20510b57cec5SDimitry Andric         // GetSharedModule is not guaranteed to find the old shared module, for
20520b57cec5SDimitry Andric         // instance in the common case where you pass in the UUID, it is only
20530b57cec5SDimitry Andric         // going to find the one module matching the UUID.  In fact, it has no
20540b57cec5SDimitry Andric         // good way to know what the "old module" relevant to this target is,
20550b57cec5SDimitry Andric         // since there might be many copies of a module with this file spec in
20560b57cec5SDimitry Andric         // various running debug sessions, but only one of them will belong to
20570b57cec5SDimitry Andric         // this target. So let's remove the UUID from the module list, and look
20580b57cec5SDimitry Andric         // in the target's module list. Only do this if there is SOMETHING else
20590b57cec5SDimitry Andric         // in the module spec...
20600b57cec5SDimitry Andric         if (!old_module_sp) {
20610b57cec5SDimitry Andric           if (module_spec.GetUUID().IsValid() &&
20620b57cec5SDimitry Andric               !module_spec.GetFileSpec().GetFilename().IsEmpty() &&
20630b57cec5SDimitry Andric               !module_spec.GetFileSpec().GetDirectory().IsEmpty()) {
20640b57cec5SDimitry Andric             ModuleSpec module_spec_copy(module_spec.GetFileSpec());
20650b57cec5SDimitry Andric             module_spec_copy.GetUUID().Clear();
20660b57cec5SDimitry Andric 
20670b57cec5SDimitry Andric             ModuleList found_modules;
20680b57cec5SDimitry Andric             m_images.FindModules(module_spec_copy, found_modules);
20699dba64beSDimitry Andric             if (found_modules.GetSize() == 1)
20700b57cec5SDimitry Andric               old_module_sp = found_modules.GetModuleAtIndex(0);
20710b57cec5SDimitry Andric           }
20720b57cec5SDimitry Andric         }
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric         // Preload symbols outside of any lock, so hopefully we can do this for
20750b57cec5SDimitry Andric         // each library in parallel.
20760b57cec5SDimitry Andric         if (GetPreloadSymbols())
20770b57cec5SDimitry Andric           module_sp->PreloadSymbols();
20780b57cec5SDimitry Andric 
20799dba64beSDimitry Andric         if (old_module_sp && m_images.GetIndexForModule(old_module_sp.get()) !=
20800b57cec5SDimitry Andric                                  LLDB_INVALID_INDEX32) {
20810b57cec5SDimitry Andric           m_images.ReplaceModule(old_module_sp, module_sp);
20820b57cec5SDimitry Andric           Module *old_module_ptr = old_module_sp.get();
20830b57cec5SDimitry Andric           old_module_sp.reset();
20840b57cec5SDimitry Andric           ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
20850b57cec5SDimitry Andric         } else {
20860b57cec5SDimitry Andric           m_images.Append(module_sp, notify);
20870b57cec5SDimitry Andric         }
20880b57cec5SDimitry Andric       } else
20890b57cec5SDimitry Andric         module_sp.reset();
20900b57cec5SDimitry Andric     }
20910b57cec5SDimitry Andric   }
20920b57cec5SDimitry Andric   if (error_ptr)
20930b57cec5SDimitry Andric     *error_ptr = error;
20940b57cec5SDimitry Andric   return module_sp;
20950b57cec5SDimitry Andric }
20960b57cec5SDimitry Andric 
20970b57cec5SDimitry Andric TargetSP Target::CalculateTarget() { return shared_from_this(); }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric ProcessSP Target::CalculateProcess() { return m_process_sp; }
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric ThreadSP Target::CalculateThread() { return ThreadSP(); }
21020b57cec5SDimitry Andric 
21030b57cec5SDimitry Andric StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); }
21040b57cec5SDimitry Andric 
21050b57cec5SDimitry Andric void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) {
21060b57cec5SDimitry Andric   exe_ctx.Clear();
21070b57cec5SDimitry Andric   exe_ctx.SetTargetPtr(this);
21080b57cec5SDimitry Andric }
21090b57cec5SDimitry Andric 
21100b57cec5SDimitry Andric PathMappingList &Target::GetImageSearchPathList() {
21110b57cec5SDimitry Andric   return m_image_search_paths;
21120b57cec5SDimitry Andric }
21130b57cec5SDimitry Andric 
21140b57cec5SDimitry Andric void Target::ImageSearchPathsChanged(const PathMappingList &path_list,
21150b57cec5SDimitry Andric                                      void *baton) {
21160b57cec5SDimitry Andric   Target *target = (Target *)baton;
21170b57cec5SDimitry Andric   ModuleSP exe_module_sp(target->GetExecutableModule());
21180b57cec5SDimitry Andric   if (exe_module_sp)
21190b57cec5SDimitry Andric     target->SetExecutableModule(exe_module_sp, eLoadDependentsYes);
21200b57cec5SDimitry Andric }
21210b57cec5SDimitry Andric 
21229dba64beSDimitry Andric llvm::Expected<TypeSystem &>
21239dba64beSDimitry Andric Target::GetScratchTypeSystemForLanguage(lldb::LanguageType language,
21240b57cec5SDimitry Andric                                         bool create_on_demand) {
21250b57cec5SDimitry Andric   if (!m_valid)
21269dba64beSDimitry Andric     return llvm::make_error<llvm::StringError>("Invalid Target",
21279dba64beSDimitry Andric                                                llvm::inconvertibleErrorCode());
21280b57cec5SDimitry Andric 
21290b57cec5SDimitry Andric   if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all
21300b57cec5SDimitry Andric                                              // assembly code
21310b57cec5SDimitry Andric       || language == eLanguageTypeUnknown) {
21329dba64beSDimitry Andric     LanguageSet languages_for_expressions =
21339dba64beSDimitry Andric         Language::GetLanguagesSupportingTypeSystemsForExpressions();
21340b57cec5SDimitry Andric 
21359dba64beSDimitry Andric     if (languages_for_expressions[eLanguageTypeC]) {
21360b57cec5SDimitry Andric       language = eLanguageTypeC; // LLDB's default.  Override by setting the
21370b57cec5SDimitry Andric                                  // target language.
21380b57cec5SDimitry Andric     } else {
21399dba64beSDimitry Andric       if (languages_for_expressions.Empty())
21409dba64beSDimitry Andric         return llvm::make_error<llvm::StringError>(
21419dba64beSDimitry Andric             "No expression support for any languages",
21429dba64beSDimitry Andric             llvm::inconvertibleErrorCode());
21439dba64beSDimitry Andric       language = (LanguageType)languages_for_expressions.bitvector.find_first();
21440b57cec5SDimitry Andric     }
21450b57cec5SDimitry Andric   }
21460b57cec5SDimitry Andric 
21470b57cec5SDimitry Andric   return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this,
21480b57cec5SDimitry Andric                                                             create_on_demand);
21490b57cec5SDimitry Andric }
21500b57cec5SDimitry Andric 
21519dba64beSDimitry Andric std::vector<TypeSystem *> Target::GetScratchTypeSystems(bool create_on_demand) {
21529dba64beSDimitry Andric   if (!m_valid)
21539dba64beSDimitry Andric     return {};
21549dba64beSDimitry Andric 
21559dba64beSDimitry Andric   std::vector<TypeSystem *> scratch_type_systems;
21569dba64beSDimitry Andric 
21579dba64beSDimitry Andric   LanguageSet languages_for_expressions =
21589dba64beSDimitry Andric       Language::GetLanguagesSupportingTypeSystemsForExpressions();
21599dba64beSDimitry Andric 
21609dba64beSDimitry Andric   for (auto bit : languages_for_expressions.bitvector.set_bits()) {
21619dba64beSDimitry Andric     auto language = (LanguageType)bit;
21629dba64beSDimitry Andric     auto type_system_or_err =
21639dba64beSDimitry Andric         GetScratchTypeSystemForLanguage(language, create_on_demand);
21649dba64beSDimitry Andric     if (!type_system_or_err)
21659dba64beSDimitry Andric       LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
21669dba64beSDimitry Andric                      type_system_or_err.takeError(),
21679dba64beSDimitry Andric                      "Language '{}' has expression support but no scratch type "
21689dba64beSDimitry Andric                      "system available",
21699dba64beSDimitry Andric                      Language::GetNameForLanguageType(language));
21709dba64beSDimitry Andric     else
21719dba64beSDimitry Andric       scratch_type_systems.emplace_back(&type_system_or_err.get());
21729dba64beSDimitry Andric   }
21739dba64beSDimitry Andric 
21749dba64beSDimitry Andric   return scratch_type_systems;
21759dba64beSDimitry Andric }
21769dba64beSDimitry Andric 
21770b57cec5SDimitry Andric PersistentExpressionState *
21780b57cec5SDimitry Andric Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) {
21799dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language, true);
21800b57cec5SDimitry Andric 
21819dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
21829dba64beSDimitry Andric     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
21839dba64beSDimitry Andric                    std::move(err),
21849dba64beSDimitry Andric                    "Unable to get persistent expression state for language {}",
21859dba64beSDimitry Andric                    Language::GetNameForLanguageType(language));
21860b57cec5SDimitry Andric     return nullptr;
21870b57cec5SDimitry Andric   }
21889dba64beSDimitry Andric 
21899dba64beSDimitry Andric   return type_system_or_err->GetPersistentExpressionState();
21900b57cec5SDimitry Andric }
21910b57cec5SDimitry Andric 
21920b57cec5SDimitry Andric UserExpression *Target::GetUserExpressionForLanguage(
21930b57cec5SDimitry Andric     llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
21940b57cec5SDimitry Andric     Expression::ResultType desired_type,
21959dba64beSDimitry Andric     const EvaluateExpressionOptions &options, ValueObject *ctx_obj,
21969dba64beSDimitry Andric     Status &error) {
21979dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
21989dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
21990b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22000b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
22010b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22029dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
22030b57cec5SDimitry Andric     return nullptr;
22040b57cec5SDimitry Andric   }
22050b57cec5SDimitry Andric 
22069dba64beSDimitry Andric   auto *user_expr = type_system_or_err->GetUserExpression(
22079dba64beSDimitry Andric       expr, prefix, language, desired_type, options, ctx_obj);
22080b57cec5SDimitry Andric   if (!user_expr)
22090b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22100b57cec5SDimitry Andric         "Could not create an expression for language %s",
22110b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
22120b57cec5SDimitry Andric 
22130b57cec5SDimitry Andric   return user_expr;
22140b57cec5SDimitry Andric }
22150b57cec5SDimitry Andric 
22160b57cec5SDimitry Andric FunctionCaller *Target::GetFunctionCallerForLanguage(
22170b57cec5SDimitry Andric     lldb::LanguageType language, const CompilerType &return_type,
22180b57cec5SDimitry Andric     const Address &function_address, const ValueList &arg_value_list,
22190b57cec5SDimitry Andric     const char *name, Status &error) {
22209dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
22219dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
22220b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22230b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
22240b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22259dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
22269dba64beSDimitry Andric     return nullptr;
22270b57cec5SDimitry Andric   }
22280b57cec5SDimitry Andric 
22299dba64beSDimitry Andric   auto *persistent_fn = type_system_or_err->GetFunctionCaller(
22309dba64beSDimitry Andric       return_type, function_address, arg_value_list, name);
22310b57cec5SDimitry Andric   if (!persistent_fn)
22320b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22330b57cec5SDimitry Andric         "Could not create an expression for language %s",
22340b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
22350b57cec5SDimitry Andric 
22360b57cec5SDimitry Andric   return persistent_fn;
22370b57cec5SDimitry Andric }
22380b57cec5SDimitry Andric 
22390b57cec5SDimitry Andric UtilityFunction *
22400b57cec5SDimitry Andric Target::GetUtilityFunctionForLanguage(const char *text,
22410b57cec5SDimitry Andric                                       lldb::LanguageType language,
22420b57cec5SDimitry Andric                                       const char *name, Status &error) {
22439dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
22440b57cec5SDimitry Andric 
22459dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
22460b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22470b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
22480b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22499dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
22509dba64beSDimitry Andric     return nullptr;
22510b57cec5SDimitry Andric   }
22520b57cec5SDimitry Andric 
22539dba64beSDimitry Andric   auto *utility_fn = type_system_or_err->GetUtilityFunction(text, name);
22540b57cec5SDimitry Andric   if (!utility_fn)
22550b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
22560b57cec5SDimitry Andric         "Could not create an expression for language %s",
22570b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
22580b57cec5SDimitry Andric 
22590b57cec5SDimitry Andric   return utility_fn;
22600b57cec5SDimitry Andric }
22610b57cec5SDimitry Andric 
22620b57cec5SDimitry Andric void Target::SettingsInitialize() { Process::SettingsInitialize(); }
22630b57cec5SDimitry Andric 
22640b57cec5SDimitry Andric void Target::SettingsTerminate() { Process::SettingsTerminate(); }
22650b57cec5SDimitry Andric 
22660b57cec5SDimitry Andric FileSpecList Target::GetDefaultExecutableSearchPaths() {
22670b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
22680b57cec5SDimitry Andric   if (properties_sp)
22690b57cec5SDimitry Andric     return properties_sp->GetExecutableSearchPaths();
22700b57cec5SDimitry Andric   return FileSpecList();
22710b57cec5SDimitry Andric }
22720b57cec5SDimitry Andric 
22730b57cec5SDimitry Andric FileSpecList Target::GetDefaultDebugFileSearchPaths() {
22740b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
22750b57cec5SDimitry Andric   if (properties_sp)
22760b57cec5SDimitry Andric     return properties_sp->GetDebugFileSearchPaths();
22770b57cec5SDimitry Andric   return FileSpecList();
22780b57cec5SDimitry Andric }
22790b57cec5SDimitry Andric 
22800b57cec5SDimitry Andric ArchSpec Target::GetDefaultArchitecture() {
22810b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
22820b57cec5SDimitry Andric   if (properties_sp)
22830b57cec5SDimitry Andric     return properties_sp->GetDefaultArchitecture();
22840b57cec5SDimitry Andric   return ArchSpec();
22850b57cec5SDimitry Andric }
22860b57cec5SDimitry Andric 
22870b57cec5SDimitry Andric void Target::SetDefaultArchitecture(const ArchSpec &arch) {
22880b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
22890b57cec5SDimitry Andric   if (properties_sp) {
22909dba64beSDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET),
22919dba64beSDimitry Andric              "Target::SetDefaultArchitecture setting target's "
22929dba64beSDimitry Andric              "default architecture to  {0} ({1})",
22939dba64beSDimitry Andric              arch.GetArchitectureName(), arch.GetTriple().getTriple());
22940b57cec5SDimitry Andric     return properties_sp->SetDefaultArchitecture(arch);
22950b57cec5SDimitry Andric   }
22960b57cec5SDimitry Andric }
22970b57cec5SDimitry Andric 
22980b57cec5SDimitry Andric Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
22990b57cec5SDimitry Andric                                       const SymbolContext *sc_ptr) {
23000b57cec5SDimitry Andric   // The target can either exist in the "process" of ExecutionContext, or in
23010b57cec5SDimitry Andric   // the "target_sp" member of SymbolContext. This accessor helper function
23020b57cec5SDimitry Andric   // will get the target from one of these locations.
23030b57cec5SDimitry Andric 
23040b57cec5SDimitry Andric   Target *target = nullptr;
23050b57cec5SDimitry Andric   if (sc_ptr != nullptr)
23060b57cec5SDimitry Andric     target = sc_ptr->target_sp.get();
23070b57cec5SDimitry Andric   if (target == nullptr && exe_ctx_ptr)
23080b57cec5SDimitry Andric     target = exe_ctx_ptr->GetTargetPtr();
23090b57cec5SDimitry Andric   return target;
23100b57cec5SDimitry Andric }
23110b57cec5SDimitry Andric 
23120b57cec5SDimitry Andric ExpressionResults Target::EvaluateExpression(
23130b57cec5SDimitry Andric     llvm::StringRef expr, ExecutionContextScope *exe_scope,
23140b57cec5SDimitry Andric     lldb::ValueObjectSP &result_valobj_sp,
23150b57cec5SDimitry Andric     const EvaluateExpressionOptions &options, std::string *fixed_expression,
23160b57cec5SDimitry Andric     ValueObject *ctx_obj) {
23170b57cec5SDimitry Andric   result_valobj_sp.reset();
23180b57cec5SDimitry Andric 
23190b57cec5SDimitry Andric   ExpressionResults execution_results = eExpressionSetupError;
23200b57cec5SDimitry Andric 
23210b57cec5SDimitry Andric   if (expr.empty())
23220b57cec5SDimitry Andric     return execution_results;
23230b57cec5SDimitry Andric 
23240b57cec5SDimitry Andric   // We shouldn't run stop hooks in expressions.
23250b57cec5SDimitry Andric   bool old_suppress_value = m_suppress_stop_hooks;
23260b57cec5SDimitry Andric   m_suppress_stop_hooks = true;
23270b57cec5SDimitry Andric   auto on_exit = llvm::make_scope_exit([this, old_suppress_value]() {
23289dba64beSDimitry Andric     m_suppress_stop_hooks = old_suppress_value;
23299dba64beSDimitry Andric   });
23300b57cec5SDimitry Andric 
23310b57cec5SDimitry Andric   ExecutionContext exe_ctx;
23320b57cec5SDimitry Andric 
23330b57cec5SDimitry Andric   if (exe_scope) {
23340b57cec5SDimitry Andric     exe_scope->CalculateExecutionContext(exe_ctx);
23350b57cec5SDimitry Andric   } else if (m_process_sp) {
23360b57cec5SDimitry Andric     m_process_sp->CalculateExecutionContext(exe_ctx);
23370b57cec5SDimitry Andric   } else {
23380b57cec5SDimitry Andric     CalculateExecutionContext(exe_ctx);
23390b57cec5SDimitry Andric   }
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   // Make sure we aren't just trying to see the value of a persistent variable
23420b57cec5SDimitry Andric   // (something like "$0")
23430b57cec5SDimitry Andric   // Only check for persistent variables the expression starts with a '$'
23449dba64beSDimitry Andric   lldb::ExpressionVariableSP persistent_var_sp;
23459dba64beSDimitry Andric   if (expr[0] == '$') {
23469dba64beSDimitry Andric     auto type_system_or_err =
23479dba64beSDimitry Andric             GetScratchTypeSystemForLanguage(eLanguageTypeC);
23489dba64beSDimitry Andric     if (auto err = type_system_or_err.takeError()) {
23499dba64beSDimitry Andric       LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
23509dba64beSDimitry Andric                      std::move(err), "Unable to get scratch type system");
23519dba64beSDimitry Andric     } else {
23529dba64beSDimitry Andric       persistent_var_sp =
23539dba64beSDimitry Andric           type_system_or_err->GetPersistentExpressionState()->GetVariable(expr);
23549dba64beSDimitry Andric     }
23559dba64beSDimitry Andric   }
23560b57cec5SDimitry Andric   if (persistent_var_sp) {
23570b57cec5SDimitry Andric     result_valobj_sp = persistent_var_sp->GetValueObject();
23580b57cec5SDimitry Andric     execution_results = eExpressionCompleted;
23590b57cec5SDimitry Andric   } else {
23600b57cec5SDimitry Andric     llvm::StringRef prefix = GetExpressionPrefixContents();
23610b57cec5SDimitry Andric     Status error;
23625ffd83dbSDimitry Andric     execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix,
23635ffd83dbSDimitry Andric                                                  result_valobj_sp, error,
23645ffd83dbSDimitry Andric                                                  fixed_expression, ctx_obj);
23650b57cec5SDimitry Andric   }
23660b57cec5SDimitry Andric 
23670b57cec5SDimitry Andric   return execution_results;
23680b57cec5SDimitry Andric }
23690b57cec5SDimitry Andric 
23709dba64beSDimitry Andric lldb::ExpressionVariableSP Target::GetPersistentVariable(ConstString name) {
23710b57cec5SDimitry Andric   lldb::ExpressionVariableSP variable_sp;
23720b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
23730b57cec5SDimitry Andric       [name, &variable_sp](TypeSystem *type_system) -> bool {
23740b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
23750b57cec5SDimitry Andric                 type_system->GetPersistentExpressionState()) {
23760b57cec5SDimitry Andric           variable_sp = persistent_state->GetVariable(name);
23770b57cec5SDimitry Andric 
23780b57cec5SDimitry Andric           if (variable_sp)
23790b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
23800b57cec5SDimitry Andric         }
23810b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
23820b57cec5SDimitry Andric       });
23830b57cec5SDimitry Andric   return variable_sp;
23840b57cec5SDimitry Andric }
23850b57cec5SDimitry Andric 
23860b57cec5SDimitry Andric lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
23870b57cec5SDimitry Andric   lldb::addr_t address = LLDB_INVALID_ADDRESS;
23880b57cec5SDimitry Andric 
23890b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
23900b57cec5SDimitry Andric       [name, &address](TypeSystem *type_system) -> bool {
23910b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
23920b57cec5SDimitry Andric                 type_system->GetPersistentExpressionState()) {
23930b57cec5SDimitry Andric           address = persistent_state->LookupSymbol(name);
23940b57cec5SDimitry Andric           if (address != LLDB_INVALID_ADDRESS)
23950b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
23960b57cec5SDimitry Andric         }
23970b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
23980b57cec5SDimitry Andric       });
23990b57cec5SDimitry Andric   return address;
24000b57cec5SDimitry Andric }
24010b57cec5SDimitry Andric 
24029dba64beSDimitry Andric llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
24039dba64beSDimitry Andric   Module *exe_module = GetExecutableModulePointer();
24049dba64beSDimitry Andric 
2405*1e991466SDimitry Andric   // Try to find the entry point address in the primary executable.
2406*1e991466SDimitry Andric   const bool has_primary_executable = exe_module && exe_module->GetObjectFile();
2407*1e991466SDimitry Andric   if (has_primary_executable) {
24089dba64beSDimitry Andric     Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
24099dba64beSDimitry Andric     if (entry_addr.IsValid())
24109dba64beSDimitry Andric       return entry_addr;
24119dba64beSDimitry Andric   }
24129dba64beSDimitry Andric 
24139dba64beSDimitry Andric   const ModuleList &modules = GetImages();
24149dba64beSDimitry Andric   const size_t num_images = modules.GetSize();
24159dba64beSDimitry Andric   for (size_t idx = 0; idx < num_images; ++idx) {
24169dba64beSDimitry Andric     ModuleSP module_sp(modules.GetModuleAtIndex(idx));
24179dba64beSDimitry Andric     if (!module_sp || !module_sp->GetObjectFile())
24189dba64beSDimitry Andric       continue;
24199dba64beSDimitry Andric 
24209dba64beSDimitry Andric     Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
2421*1e991466SDimitry Andric     if (entry_addr.IsValid())
24229dba64beSDimitry Andric       return entry_addr;
24239dba64beSDimitry Andric   }
24249dba64beSDimitry Andric 
2425*1e991466SDimitry Andric   // We haven't found the entry point address. Return an appropriate error.
2426*1e991466SDimitry Andric   if (!has_primary_executable)
2427*1e991466SDimitry Andric     return llvm::make_error<llvm::StringError>(
2428*1e991466SDimitry Andric         "No primary executable found and could not find entry point address in "
2429*1e991466SDimitry Andric         "any executable module",
2430*1e991466SDimitry Andric         llvm::inconvertibleErrorCode());
2431*1e991466SDimitry Andric 
2432*1e991466SDimitry Andric   return llvm::make_error<llvm::StringError>(
2433*1e991466SDimitry Andric       "Could not find entry point address for primary executable module \"" +
2434*1e991466SDimitry Andric           exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"",
2435*1e991466SDimitry Andric       llvm::inconvertibleErrorCode());
24369dba64beSDimitry Andric }
24379dba64beSDimitry Andric 
24380b57cec5SDimitry Andric lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
24390b57cec5SDimitry Andric                                             AddressClass addr_class) const {
24400b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24419dba64beSDimitry Andric   return arch_plugin
24429dba64beSDimitry Andric              ? arch_plugin->GetCallableLoadAddress(load_addr, addr_class)
24439dba64beSDimitry Andric              : load_addr;
24440b57cec5SDimitry Andric }
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr,
24470b57cec5SDimitry Andric                                           AddressClass addr_class) const {
24480b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24499dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetOpcodeLoadAddress(load_addr, addr_class)
24509dba64beSDimitry Andric                      : load_addr;
24510b57cec5SDimitry Andric }
24520b57cec5SDimitry Andric 
24530b57cec5SDimitry Andric lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
24540b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24559dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
24560b57cec5SDimitry Andric }
24570b57cec5SDimitry Andric 
24580b57cec5SDimitry Andric SourceManager &Target::GetSourceManager() {
24590b57cec5SDimitry Andric   if (!m_source_manager_up)
24605ffd83dbSDimitry Andric     m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
24610b57cec5SDimitry Andric   return *m_source_manager_up;
24620b57cec5SDimitry Andric }
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric ClangModulesDeclVendor *Target::GetClangModulesDeclVendor() {
24650b57cec5SDimitry Andric   static std::mutex s_clang_modules_decl_vendor_mutex; // If this is contended
24660b57cec5SDimitry Andric                                                        // we can make it
24670b57cec5SDimitry Andric                                                        // per-target
24680b57cec5SDimitry Andric 
24690b57cec5SDimitry Andric   {
24700b57cec5SDimitry Andric     std::lock_guard<std::mutex> guard(s_clang_modules_decl_vendor_mutex);
24710b57cec5SDimitry Andric 
24720b57cec5SDimitry Andric     if (!m_clang_modules_decl_vendor_up) {
24730b57cec5SDimitry Andric       m_clang_modules_decl_vendor_up.reset(
24740b57cec5SDimitry Andric           ClangModulesDeclVendor::Create(*this));
24750b57cec5SDimitry Andric     }
24760b57cec5SDimitry Andric   }
24770b57cec5SDimitry Andric 
24780b57cec5SDimitry Andric   return m_clang_modules_decl_vendor_up.get();
24790b57cec5SDimitry Andric }
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric Target::StopHookSP Target::CreateStopHook() {
24820b57cec5SDimitry Andric   lldb::user_id_t new_uid = ++m_stop_hook_next_id;
24830b57cec5SDimitry Andric   Target::StopHookSP stop_hook_sp(new StopHook(shared_from_this(), new_uid));
24840b57cec5SDimitry Andric   m_stop_hooks[new_uid] = stop_hook_sp;
24850b57cec5SDimitry Andric   return stop_hook_sp;
24860b57cec5SDimitry Andric }
24870b57cec5SDimitry Andric 
24880b57cec5SDimitry Andric bool Target::RemoveStopHookByID(lldb::user_id_t user_id) {
24890b57cec5SDimitry Andric   size_t num_removed = m_stop_hooks.erase(user_id);
24900b57cec5SDimitry Andric   return (num_removed != 0);
24910b57cec5SDimitry Andric }
24920b57cec5SDimitry Andric 
24930b57cec5SDimitry Andric void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); }
24940b57cec5SDimitry Andric 
24950b57cec5SDimitry Andric Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) {
24960b57cec5SDimitry Andric   StopHookSP found_hook;
24970b57cec5SDimitry Andric 
24980b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
24990b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
25000b57cec5SDimitry Andric   if (specified_hook_iter != m_stop_hooks.end())
25010b57cec5SDimitry Andric     found_hook = (*specified_hook_iter).second;
25020b57cec5SDimitry Andric   return found_hook;
25030b57cec5SDimitry Andric }
25040b57cec5SDimitry Andric 
25050b57cec5SDimitry Andric bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id,
25060b57cec5SDimitry Andric                                         bool active_state) {
25070b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
25080b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
25090b57cec5SDimitry Andric   if (specified_hook_iter == m_stop_hooks.end())
25100b57cec5SDimitry Andric     return false;
25110b57cec5SDimitry Andric 
25120b57cec5SDimitry Andric   (*specified_hook_iter).second->SetIsActive(active_state);
25130b57cec5SDimitry Andric   return true;
25140b57cec5SDimitry Andric }
25150b57cec5SDimitry Andric 
25160b57cec5SDimitry Andric void Target::SetAllStopHooksActiveState(bool active_state) {
25170b57cec5SDimitry Andric   StopHookCollection::iterator pos, end = m_stop_hooks.end();
25180b57cec5SDimitry Andric   for (pos = m_stop_hooks.begin(); pos != end; pos++) {
25190b57cec5SDimitry Andric     (*pos).second->SetIsActive(active_state);
25200b57cec5SDimitry Andric   }
25210b57cec5SDimitry Andric }
25220b57cec5SDimitry Andric 
25230b57cec5SDimitry Andric void Target::RunStopHooks() {
25240b57cec5SDimitry Andric   if (m_suppress_stop_hooks)
25250b57cec5SDimitry Andric     return;
25260b57cec5SDimitry Andric 
25270b57cec5SDimitry Andric   if (!m_process_sp)
25280b57cec5SDimitry Andric     return;
25290b57cec5SDimitry Andric 
25300b57cec5SDimitry Andric   // Somebody might have restarted the process:
25310b57cec5SDimitry Andric   if (m_process_sp->GetState() != eStateStopped)
25320b57cec5SDimitry Andric     return;
25330b57cec5SDimitry Andric 
25340b57cec5SDimitry Andric   // <rdar://problem/12027563> make sure we check that we are not stopped
25350b57cec5SDimitry Andric   // because of us running a user expression since in that case we do not want
25360b57cec5SDimitry Andric   // to run the stop-hooks
25370b57cec5SDimitry Andric   if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
25380b57cec5SDimitry Andric     return;
25390b57cec5SDimitry Andric 
25400b57cec5SDimitry Andric   if (m_stop_hooks.empty())
25410b57cec5SDimitry Andric     return;
25420b57cec5SDimitry Andric 
25430b57cec5SDimitry Andric   StopHookCollection::iterator pos, end = m_stop_hooks.end();
25440b57cec5SDimitry Andric 
25450b57cec5SDimitry Andric   // If there aren't any active stop hooks, don't bother either.
25460b57cec5SDimitry Andric   // Also see if any of the active hooks want to auto-continue.
25470b57cec5SDimitry Andric   bool any_active_hooks = false;
25480b57cec5SDimitry Andric   bool auto_continue = false;
25490b57cec5SDimitry Andric   for (auto hook : m_stop_hooks) {
25500b57cec5SDimitry Andric     if (hook.second->IsActive()) {
25510b57cec5SDimitry Andric       any_active_hooks = true;
25520b57cec5SDimitry Andric       auto_continue |= hook.second->GetAutoContinue();
25530b57cec5SDimitry Andric     }
25540b57cec5SDimitry Andric   }
25550b57cec5SDimitry Andric   if (!any_active_hooks)
25560b57cec5SDimitry Andric     return;
25570b57cec5SDimitry Andric 
25585ffd83dbSDimitry Andric   CommandReturnObject result(m_debugger.GetUseColor());
25590b57cec5SDimitry Andric 
25600b57cec5SDimitry Andric   std::vector<ExecutionContext> exc_ctx_with_reasons;
25610b57cec5SDimitry Andric   std::vector<SymbolContext> sym_ctx_with_reasons;
25620b57cec5SDimitry Andric 
25630b57cec5SDimitry Andric   ThreadList &cur_threadlist = m_process_sp->GetThreadList();
25640b57cec5SDimitry Andric   size_t num_threads = cur_threadlist.GetSize();
25650b57cec5SDimitry Andric   for (size_t i = 0; i < num_threads; i++) {
25660b57cec5SDimitry Andric     lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(i);
25670b57cec5SDimitry Andric     if (cur_thread_sp->ThreadStoppedForAReason()) {
25680b57cec5SDimitry Andric       lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
25690b57cec5SDimitry Andric       exc_ctx_with_reasons.push_back(ExecutionContext(
25700b57cec5SDimitry Andric           m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
25710b57cec5SDimitry Andric       sym_ctx_with_reasons.push_back(
25720b57cec5SDimitry Andric           cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
25730b57cec5SDimitry Andric     }
25740b57cec5SDimitry Andric   }
25750b57cec5SDimitry Andric 
25760b57cec5SDimitry Andric   // If no threads stopped for a reason, don't run the stop-hooks.
25770b57cec5SDimitry Andric   size_t num_exe_ctx = exc_ctx_with_reasons.size();
25780b57cec5SDimitry Andric   if (num_exe_ctx == 0)
25790b57cec5SDimitry Andric     return;
25800b57cec5SDimitry Andric 
25810b57cec5SDimitry Andric   result.SetImmediateOutputStream(m_debugger.GetAsyncOutputStream());
25820b57cec5SDimitry Andric   result.SetImmediateErrorStream(m_debugger.GetAsyncErrorStream());
25830b57cec5SDimitry Andric 
25840b57cec5SDimitry Andric   bool keep_going = true;
25850b57cec5SDimitry Andric   bool hooks_ran = false;
25860b57cec5SDimitry Andric   bool print_hook_header = (m_stop_hooks.size() != 1);
25870b57cec5SDimitry Andric   bool print_thread_header = (num_exe_ctx != 1);
25880b57cec5SDimitry Andric   bool did_restart = false;
25890b57cec5SDimitry Andric 
25900b57cec5SDimitry Andric   for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) {
25910b57cec5SDimitry Andric     // result.Clear();
25920b57cec5SDimitry Andric     StopHookSP cur_hook_sp = (*pos).second;
25930b57cec5SDimitry Andric     if (!cur_hook_sp->IsActive())
25940b57cec5SDimitry Andric       continue;
25950b57cec5SDimitry Andric 
25960b57cec5SDimitry Andric     bool any_thread_matched = false;
25970b57cec5SDimitry Andric     for (size_t i = 0; keep_going && i < num_exe_ctx; i++) {
25980b57cec5SDimitry Andric       if ((cur_hook_sp->GetSpecifier() == nullptr ||
25990b57cec5SDimitry Andric            cur_hook_sp->GetSpecifier()->SymbolContextMatches(
26000b57cec5SDimitry Andric                sym_ctx_with_reasons[i])) &&
26010b57cec5SDimitry Andric           (cur_hook_sp->GetThreadSpecifier() == nullptr ||
26020b57cec5SDimitry Andric            cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(
26030b57cec5SDimitry Andric                exc_ctx_with_reasons[i].GetThreadRef()))) {
26040b57cec5SDimitry Andric         if (!hooks_ran) {
26050b57cec5SDimitry Andric           hooks_ran = true;
26060b57cec5SDimitry Andric         }
26070b57cec5SDimitry Andric         if (print_hook_header && !any_thread_matched) {
26080b57cec5SDimitry Andric           const char *cmd =
26090b57cec5SDimitry Andric               (cur_hook_sp->GetCommands().GetSize() == 1
26100b57cec5SDimitry Andric                    ? cur_hook_sp->GetCommands().GetStringAtIndex(0)
26110b57cec5SDimitry Andric                    : nullptr);
26120b57cec5SDimitry Andric           if (cmd)
26130b57cec5SDimitry Andric             result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n",
26140b57cec5SDimitry Andric                                            cur_hook_sp->GetID(), cmd);
26150b57cec5SDimitry Andric           else
26160b57cec5SDimitry Andric             result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n",
26170b57cec5SDimitry Andric                                            cur_hook_sp->GetID());
26180b57cec5SDimitry Andric           any_thread_matched = true;
26190b57cec5SDimitry Andric         }
26200b57cec5SDimitry Andric 
26210b57cec5SDimitry Andric         if (print_thread_header)
26220b57cec5SDimitry Andric           result.AppendMessageWithFormat(
26230b57cec5SDimitry Andric               "-- Thread %d\n",
26240b57cec5SDimitry Andric               exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
26250b57cec5SDimitry Andric 
26260b57cec5SDimitry Andric         CommandInterpreterRunOptions options;
26270b57cec5SDimitry Andric         options.SetStopOnContinue(true);
26280b57cec5SDimitry Andric         options.SetStopOnError(true);
26290b57cec5SDimitry Andric         options.SetEchoCommands(false);
26300b57cec5SDimitry Andric         options.SetPrintResults(true);
26310b57cec5SDimitry Andric         options.SetPrintErrors(true);
26320b57cec5SDimitry Andric         options.SetAddToHistory(false);
26330b57cec5SDimitry Andric 
26340b57cec5SDimitry Andric         // Force Async:
26350b57cec5SDimitry Andric         bool old_async = GetDebugger().GetAsyncExecution();
26360b57cec5SDimitry Andric         GetDebugger().SetAsyncExecution(true);
26370b57cec5SDimitry Andric         GetDebugger().GetCommandInterpreter().HandleCommands(
26380b57cec5SDimitry Andric             cur_hook_sp->GetCommands(), &exc_ctx_with_reasons[i], options,
26390b57cec5SDimitry Andric             result);
26400b57cec5SDimitry Andric         GetDebugger().SetAsyncExecution(old_async);
26410b57cec5SDimitry Andric         // If the command started the target going again, we should bag out of
26420b57cec5SDimitry Andric         // running the stop hooks.
26430b57cec5SDimitry Andric         if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
26440b57cec5SDimitry Andric             (result.GetStatus() == eReturnStatusSuccessContinuingResult)) {
26450b57cec5SDimitry Andric           // But only complain if there were more stop hooks to do:
26460b57cec5SDimitry Andric           StopHookCollection::iterator tmp = pos;
26470b57cec5SDimitry Andric           if (++tmp != end)
26489dba64beSDimitry Andric             result.AppendMessageWithFormat(
26499dba64beSDimitry Andric                 "\nAborting stop hooks, hook %" PRIu64
26500b57cec5SDimitry Andric                 " set the program running.\n"
26510b57cec5SDimitry Andric                 "  Consider using '-G true' to make "
26520b57cec5SDimitry Andric                 "stop hooks auto-continue.\n",
26530b57cec5SDimitry Andric                 cur_hook_sp->GetID());
26540b57cec5SDimitry Andric           keep_going = false;
26550b57cec5SDimitry Andric           did_restart = true;
26560b57cec5SDimitry Andric         }
26570b57cec5SDimitry Andric       }
26580b57cec5SDimitry Andric     }
26590b57cec5SDimitry Andric   }
26600b57cec5SDimitry Andric   // Finally, if auto-continue was requested, do it now:
26610b57cec5SDimitry Andric   if (!did_restart && auto_continue)
26620b57cec5SDimitry Andric     m_process_sp->PrivateResume();
26630b57cec5SDimitry Andric 
26640b57cec5SDimitry Andric   result.GetImmediateOutputStream()->Flush();
26650b57cec5SDimitry Andric   result.GetImmediateErrorStream()->Flush();
26660b57cec5SDimitry Andric }
26670b57cec5SDimitry Andric 
26680b57cec5SDimitry Andric const TargetPropertiesSP &Target::GetGlobalProperties() {
26690b57cec5SDimitry Andric   // NOTE: intentional leak so we don't crash if global destructor chain gets
26700b57cec5SDimitry Andric   // called as other threads still use the result of this function
26710b57cec5SDimitry Andric   static TargetPropertiesSP *g_settings_sp_ptr =
26720b57cec5SDimitry Andric       new TargetPropertiesSP(new TargetProperties(nullptr));
26730b57cec5SDimitry Andric   return *g_settings_sp_ptr;
26740b57cec5SDimitry Andric }
26750b57cec5SDimitry Andric 
26760b57cec5SDimitry Andric Status Target::Install(ProcessLaunchInfo *launch_info) {
26770b57cec5SDimitry Andric   Status error;
26780b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
26790b57cec5SDimitry Andric   if (platform_sp) {
26800b57cec5SDimitry Andric     if (platform_sp->IsRemote()) {
26810b57cec5SDimitry Andric       if (platform_sp->IsConnected()) {
26825ffd83dbSDimitry Andric         // Install all files that have an install path when connected to a
26835ffd83dbSDimitry Andric         // remote platform. If target.auto-install-main-executable is set then
26845ffd83dbSDimitry Andric         // also install the main executable even if it does not have an explicit
26855ffd83dbSDimitry Andric         // install path specified.
26860b57cec5SDimitry Andric         const ModuleList &modules = GetImages();
26870b57cec5SDimitry Andric         const size_t num_images = modules.GetSize();
26880b57cec5SDimitry Andric         for (size_t idx = 0; idx < num_images; ++idx) {
26890b57cec5SDimitry Andric           ModuleSP module_sp(modules.GetModuleAtIndex(idx));
26900b57cec5SDimitry Andric           if (module_sp) {
26910b57cec5SDimitry Andric             const bool is_main_executable = module_sp == GetExecutableModule();
26920b57cec5SDimitry Andric             FileSpec local_file(module_sp->GetFileSpec());
26930b57cec5SDimitry Andric             if (local_file) {
26940b57cec5SDimitry Andric               FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
26950b57cec5SDimitry Andric               if (!remote_file) {
26965ffd83dbSDimitry Andric                 if (is_main_executable && GetAutoInstallMainExecutable()) {
26975ffd83dbSDimitry Andric                   // Automatically install the main executable.
26980b57cec5SDimitry Andric                   remote_file = platform_sp->GetRemoteWorkingDirectory();
26990b57cec5SDimitry Andric                   remote_file.AppendPathComponent(
27000b57cec5SDimitry Andric                       module_sp->GetFileSpec().GetFilename().GetCString());
27010b57cec5SDimitry Andric                 }
27020b57cec5SDimitry Andric               }
27030b57cec5SDimitry Andric               if (remote_file) {
27040b57cec5SDimitry Andric                 error = platform_sp->Install(local_file, remote_file);
27050b57cec5SDimitry Andric                 if (error.Success()) {
27060b57cec5SDimitry Andric                   module_sp->SetPlatformFileSpec(remote_file);
27070b57cec5SDimitry Andric                   if (is_main_executable) {
27080b57cec5SDimitry Andric                     platform_sp->SetFilePermissions(remote_file, 0700);
27090b57cec5SDimitry Andric                     if (launch_info)
27100b57cec5SDimitry Andric                       launch_info->SetExecutableFile(remote_file, false);
27110b57cec5SDimitry Andric                   }
27120b57cec5SDimitry Andric                 } else
27130b57cec5SDimitry Andric                   break;
27140b57cec5SDimitry Andric               }
27150b57cec5SDimitry Andric             }
27160b57cec5SDimitry Andric           }
27170b57cec5SDimitry Andric         }
27180b57cec5SDimitry Andric       }
27190b57cec5SDimitry Andric     }
27200b57cec5SDimitry Andric   }
27210b57cec5SDimitry Andric   return error;
27220b57cec5SDimitry Andric }
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
27250b57cec5SDimitry Andric                                 uint32_t stop_id) {
27260b57cec5SDimitry Andric   return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
27270b57cec5SDimitry Andric }
27280b57cec5SDimitry Andric 
27290b57cec5SDimitry Andric bool Target::ResolveFileAddress(lldb::addr_t file_addr,
27300b57cec5SDimitry Andric                                 Address &resolved_addr) {
27310b57cec5SDimitry Andric   return m_images.ResolveFileAddress(file_addr, resolved_addr);
27320b57cec5SDimitry Andric }
27330b57cec5SDimitry Andric 
27340b57cec5SDimitry Andric bool Target::SetSectionLoadAddress(const SectionSP &section_sp,
27350b57cec5SDimitry Andric                                    addr_t new_section_load_addr,
27360b57cec5SDimitry Andric                                    bool warn_multiple) {
27370b57cec5SDimitry Andric   const addr_t old_section_load_addr =
27380b57cec5SDimitry Andric       m_section_load_history.GetSectionLoadAddress(
27390b57cec5SDimitry Andric           SectionLoadHistory::eStopIDNow, section_sp);
27400b57cec5SDimitry Andric   if (old_section_load_addr != new_section_load_addr) {
27410b57cec5SDimitry Andric     uint32_t stop_id = 0;
27420b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
27430b57cec5SDimitry Andric     if (process_sp)
27440b57cec5SDimitry Andric       stop_id = process_sp->GetStopID();
27450b57cec5SDimitry Andric     else
27460b57cec5SDimitry Andric       stop_id = m_section_load_history.GetLastStopID();
27470b57cec5SDimitry Andric     if (m_section_load_history.SetSectionLoadAddress(
27480b57cec5SDimitry Andric             stop_id, section_sp, new_section_load_addr, warn_multiple))
27490b57cec5SDimitry Andric       return true; // Return true if the section load address was changed...
27500b57cec5SDimitry Andric   }
27510b57cec5SDimitry Andric   return false; // Return false to indicate nothing changed
27520b57cec5SDimitry Andric }
27530b57cec5SDimitry Andric 
27540b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const ModuleList &module_list) {
27550b57cec5SDimitry Andric   size_t section_unload_count = 0;
27560b57cec5SDimitry Andric   size_t num_modules = module_list.GetSize();
27570b57cec5SDimitry Andric   for (size_t i = 0; i < num_modules; ++i) {
27580b57cec5SDimitry Andric     section_unload_count +=
27590b57cec5SDimitry Andric         UnloadModuleSections(module_list.GetModuleAtIndex(i));
27600b57cec5SDimitry Andric   }
27610b57cec5SDimitry Andric   return section_unload_count;
27620b57cec5SDimitry Andric }
27630b57cec5SDimitry Andric 
27640b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) {
27650b57cec5SDimitry Andric   uint32_t stop_id = 0;
27660b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
27670b57cec5SDimitry Andric   if (process_sp)
27680b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
27690b57cec5SDimitry Andric   else
27700b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
27710b57cec5SDimitry Andric   SectionList *sections = module_sp->GetSectionList();
27720b57cec5SDimitry Andric   size_t section_unload_count = 0;
27730b57cec5SDimitry Andric   if (sections) {
27740b57cec5SDimitry Andric     const uint32_t num_sections = sections->GetNumSections(0);
27750b57cec5SDimitry Andric     for (uint32_t i = 0; i < num_sections; ++i) {
27760b57cec5SDimitry Andric       section_unload_count += m_section_load_history.SetSectionUnloaded(
27770b57cec5SDimitry Andric           stop_id, sections->GetSectionAtIndex(i));
27780b57cec5SDimitry Andric     }
27790b57cec5SDimitry Andric   }
27800b57cec5SDimitry Andric   return section_unload_count;
27810b57cec5SDimitry Andric }
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
27840b57cec5SDimitry Andric   uint32_t stop_id = 0;
27850b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
27860b57cec5SDimitry Andric   if (process_sp)
27870b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
27880b57cec5SDimitry Andric   else
27890b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
27900b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp);
27910b57cec5SDimitry Andric }
27920b57cec5SDimitry Andric 
27930b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp,
27940b57cec5SDimitry Andric                                 addr_t load_addr) {
27950b57cec5SDimitry Andric   uint32_t stop_id = 0;
27960b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
27970b57cec5SDimitry Andric   if (process_sp)
27980b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
27990b57cec5SDimitry Andric   else
28000b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
28010b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp,
28020b57cec5SDimitry Andric                                                    load_addr);
28030b57cec5SDimitry Andric }
28040b57cec5SDimitry Andric 
28050b57cec5SDimitry Andric void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
28060b57cec5SDimitry Andric 
28070b57cec5SDimitry Andric Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
28080b57cec5SDimitry Andric   Status error;
28090b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
28100b57cec5SDimitry Andric 
28119dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s() called for %s", __FUNCTION__,
28120b57cec5SDimitry Andric             launch_info.GetExecutableFile().GetPath().c_str());
28130b57cec5SDimitry Andric 
28140b57cec5SDimitry Andric   StateType state = eStateInvalid;
28150b57cec5SDimitry Andric 
28160b57cec5SDimitry Andric   // Scope to temporarily get the process state in case someone has manually
28170b57cec5SDimitry Andric   // remotely connected already to a process and we can skip the platform
28180b57cec5SDimitry Andric   // launching.
28190b57cec5SDimitry Andric   {
28200b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric     if (process_sp) {
28230b57cec5SDimitry Andric       state = process_sp->GetState();
28249dba64beSDimitry Andric       LLDB_LOGF(log,
28250b57cec5SDimitry Andric                 "Target::%s the process exists, and its current state is %s",
28260b57cec5SDimitry Andric                 __FUNCTION__, StateAsCString(state));
28270b57cec5SDimitry Andric     } else {
28289dba64beSDimitry Andric       LLDB_LOGF(log, "Target::%s the process instance doesn't currently exist.",
28290b57cec5SDimitry Andric                 __FUNCTION__);
28300b57cec5SDimitry Andric     }
28310b57cec5SDimitry Andric   }
28320b57cec5SDimitry Andric 
28330b57cec5SDimitry Andric   launch_info.GetFlags().Set(eLaunchFlagDebug);
28340b57cec5SDimitry Andric 
28350b57cec5SDimitry Andric   // Get the value of synchronous execution here.  If you wait till after you
28360b57cec5SDimitry Andric   // have started to run, then you could have hit a breakpoint, whose command
28370b57cec5SDimitry Andric   // might switch the value, and then you'll pick up that incorrect value.
28380b57cec5SDimitry Andric   Debugger &debugger = GetDebugger();
28390b57cec5SDimitry Andric   const bool synchronous_execution =
28400b57cec5SDimitry Andric       debugger.GetCommandInterpreter().GetSynchronous();
28410b57cec5SDimitry Andric 
28420b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
28430b57cec5SDimitry Andric 
28440b57cec5SDimitry Andric   FinalizeFileActions(launch_info);
28450b57cec5SDimitry Andric 
28460b57cec5SDimitry Andric   if (state == eStateConnected) {
28470b57cec5SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
28480b57cec5SDimitry Andric       error.SetErrorString(
28490b57cec5SDimitry Andric           "can't launch in tty when launching through a remote connection");
28500b57cec5SDimitry Andric       return error;
28510b57cec5SDimitry Andric     }
28520b57cec5SDimitry Andric   }
28530b57cec5SDimitry Andric 
28540b57cec5SDimitry Andric   if (!launch_info.GetArchitecture().IsValid())
28550b57cec5SDimitry Andric     launch_info.GetArchitecture() = GetArchitecture();
28560b57cec5SDimitry Andric 
28570b57cec5SDimitry Andric   // If we're not already connected to the process, and if we have a platform
28580b57cec5SDimitry Andric   // that can launch a process for debugging, go ahead and do that here.
28590b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp &&
28600b57cec5SDimitry Andric       platform_sp->CanDebugProcess()) {
28619dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s asking the platform to debug the process",
28620b57cec5SDimitry Andric               __FUNCTION__);
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric     // If there was a previous process, delete it before we make the new one.
28650b57cec5SDimitry Andric     // One subtle point, we delete the process before we release the reference
28660b57cec5SDimitry Andric     // to m_process_sp.  That way even if we are the last owner, the process
28670b57cec5SDimitry Andric     // will get Finalized before it gets destroyed.
28680b57cec5SDimitry Andric     DeleteCurrentProcess();
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric     m_process_sp =
28710b57cec5SDimitry Andric         GetPlatform()->DebugProcess(launch_info, debugger, this, error);
28720b57cec5SDimitry Andric 
28730b57cec5SDimitry Andric   } else {
28749dba64beSDimitry Andric     LLDB_LOGF(log,
28759dba64beSDimitry Andric               "Target::%s the platform doesn't know how to debug a "
28760b57cec5SDimitry Andric               "process, getting a process plugin to do this for us.",
28770b57cec5SDimitry Andric               __FUNCTION__);
28780b57cec5SDimitry Andric 
28790b57cec5SDimitry Andric     if (state == eStateConnected) {
28800b57cec5SDimitry Andric       assert(m_process_sp);
28810b57cec5SDimitry Andric     } else {
28820b57cec5SDimitry Andric       // Use a Process plugin to construct the process.
28830b57cec5SDimitry Andric       const char *plugin_name = launch_info.GetProcessPluginName();
28840b57cec5SDimitry Andric       CreateProcess(launch_info.GetListener(), plugin_name, nullptr);
28850b57cec5SDimitry Andric     }
28860b57cec5SDimitry Andric 
28870b57cec5SDimitry Andric     // Since we didn't have a platform launch the process, launch it here.
28880b57cec5SDimitry Andric     if (m_process_sp)
28890b57cec5SDimitry Andric       error = m_process_sp->Launch(launch_info);
28900b57cec5SDimitry Andric   }
28910b57cec5SDimitry Andric 
28920b57cec5SDimitry Andric   if (!m_process_sp) {
28930b57cec5SDimitry Andric     if (error.Success())
28940b57cec5SDimitry Andric       error.SetErrorString("failed to launch or debug process");
28950b57cec5SDimitry Andric     return error;
28960b57cec5SDimitry Andric   }
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   if (error.Success()) {
28990b57cec5SDimitry Andric     if (synchronous_execution ||
29000b57cec5SDimitry Andric         !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
29010b57cec5SDimitry Andric       ListenerSP hijack_listener_sp(launch_info.GetHijackListener());
29020b57cec5SDimitry Andric       if (!hijack_listener_sp) {
29030b57cec5SDimitry Andric         hijack_listener_sp =
29040b57cec5SDimitry Andric             Listener::MakeListener("lldb.Target.Launch.hijack");
29050b57cec5SDimitry Andric         launch_info.SetHijackListener(hijack_listener_sp);
29060b57cec5SDimitry Andric         m_process_sp->HijackProcessEvents(hijack_listener_sp);
29070b57cec5SDimitry Andric       }
29080b57cec5SDimitry Andric 
29090b57cec5SDimitry Andric       StateType state = m_process_sp->WaitForProcessToStop(
29100b57cec5SDimitry Andric           llvm::None, nullptr, false, hijack_listener_sp, nullptr);
29110b57cec5SDimitry Andric 
29120b57cec5SDimitry Andric       if (state == eStateStopped) {
29130b57cec5SDimitry Andric         if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
29140b57cec5SDimitry Andric           if (synchronous_execution) {
29159dba64beSDimitry Andric             // Now we have handled the stop-from-attach, and we are just
29169dba64beSDimitry Andric             // switching to a synchronous resume.  So we should switch to the
29179dba64beSDimitry Andric             // SyncResume hijacker.
29180b57cec5SDimitry Andric             m_process_sp->RestoreProcessEvents();
29190b57cec5SDimitry Andric             m_process_sp->ResumeSynchronous(stream);
29200b57cec5SDimitry Andric           } else {
29210b57cec5SDimitry Andric             m_process_sp->RestoreProcessEvents();
29220b57cec5SDimitry Andric             error = m_process_sp->PrivateResume();
29230b57cec5SDimitry Andric           }
29240b57cec5SDimitry Andric           if (!error.Success()) {
29250b57cec5SDimitry Andric             Status error2;
29260b57cec5SDimitry Andric             error2.SetErrorStringWithFormat(
29270b57cec5SDimitry Andric                 "process resume at entry point failed: %s", error.AsCString());
29280b57cec5SDimitry Andric             error = error2;
29290b57cec5SDimitry Andric           }
29300b57cec5SDimitry Andric         }
29310b57cec5SDimitry Andric       } else if (state == eStateExited) {
29320b57cec5SDimitry Andric         bool with_shell = !!launch_info.GetShell();
29330b57cec5SDimitry Andric         const int exit_status = m_process_sp->GetExitStatus();
29340b57cec5SDimitry Andric         const char *exit_desc = m_process_sp->GetExitDescription();
29350b57cec5SDimitry Andric #define LAUNCH_SHELL_MESSAGE                                                   \
29360b57cec5SDimitry Andric   "\n'r' and 'run' are aliases that default to launching through a "           \
29370b57cec5SDimitry Andric   "shell.\nTry launching without going through a shell by using 'process "     \
29380b57cec5SDimitry Andric   "launch'."
29390b57cec5SDimitry Andric         if (exit_desc && exit_desc[0]) {
29400b57cec5SDimitry Andric           if (with_shell)
29410b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
29420b57cec5SDimitry Andric                 "process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE,
29430b57cec5SDimitry Andric                 exit_status, exit_desc);
29440b57cec5SDimitry Andric           else
29450b57cec5SDimitry Andric             error.SetErrorStringWithFormat("process exited with status %i (%s)",
29460b57cec5SDimitry Andric                                            exit_status, exit_desc);
29470b57cec5SDimitry Andric         } else {
29480b57cec5SDimitry Andric           if (with_shell)
29490b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
29500b57cec5SDimitry Andric                 "process exited with status %i" LAUNCH_SHELL_MESSAGE,
29510b57cec5SDimitry Andric                 exit_status);
29520b57cec5SDimitry Andric           else
29530b57cec5SDimitry Andric             error.SetErrorStringWithFormat("process exited with status %i",
29540b57cec5SDimitry Andric                                            exit_status);
29550b57cec5SDimitry Andric         }
29560b57cec5SDimitry Andric       } else {
29570b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
29580b57cec5SDimitry Andric             "initial process state wasn't stopped: %s", StateAsCString(state));
29590b57cec5SDimitry Andric       }
29600b57cec5SDimitry Andric     }
29610b57cec5SDimitry Andric     m_process_sp->RestoreProcessEvents();
29620b57cec5SDimitry Andric   }
29630b57cec5SDimitry Andric   return error;
29640b57cec5SDimitry Andric }
29650b57cec5SDimitry Andric 
29660b57cec5SDimitry Andric Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
29670b57cec5SDimitry Andric   auto state = eStateInvalid;
29680b57cec5SDimitry Andric   auto process_sp = GetProcessSP();
29690b57cec5SDimitry Andric   if (process_sp) {
29700b57cec5SDimitry Andric     state = process_sp->GetState();
29710b57cec5SDimitry Andric     if (process_sp->IsAlive() && state != eStateConnected) {
29720b57cec5SDimitry Andric       if (state == eStateAttaching)
29730b57cec5SDimitry Andric         return Status("process attach is in progress");
29740b57cec5SDimitry Andric       return Status("a process is already being debugged");
29750b57cec5SDimitry Andric     }
29760b57cec5SDimitry Andric   }
29770b57cec5SDimitry Andric 
29780b57cec5SDimitry Andric   const ModuleSP old_exec_module_sp = GetExecutableModule();
29790b57cec5SDimitry Andric 
29800b57cec5SDimitry Andric   // If no process info was specified, then use the target executable name as
29810b57cec5SDimitry Andric   // the process to attach to by default
29820b57cec5SDimitry Andric   if (!attach_info.ProcessInfoSpecified()) {
29830b57cec5SDimitry Andric     if (old_exec_module_sp)
29840b57cec5SDimitry Andric       attach_info.GetExecutableFile().GetFilename() =
29850b57cec5SDimitry Andric           old_exec_module_sp->GetPlatformFileSpec().GetFilename();
29860b57cec5SDimitry Andric 
29870b57cec5SDimitry Andric     if (!attach_info.ProcessInfoSpecified()) {
29880b57cec5SDimitry Andric       return Status("no process specified, create a target with a file, or "
29890b57cec5SDimitry Andric                     "specify the --pid or --name");
29900b57cec5SDimitry Andric     }
29910b57cec5SDimitry Andric   }
29920b57cec5SDimitry Andric 
29930b57cec5SDimitry Andric   const auto platform_sp =
29940b57cec5SDimitry Andric       GetDebugger().GetPlatformList().GetSelectedPlatform();
29950b57cec5SDimitry Andric   ListenerSP hijack_listener_sp;
29960b57cec5SDimitry Andric   const bool async = attach_info.GetAsync();
29970b57cec5SDimitry Andric   if (!async) {
29980b57cec5SDimitry Andric     hijack_listener_sp =
29990b57cec5SDimitry Andric         Listener::MakeListener("lldb.Target.Attach.attach.hijack");
30000b57cec5SDimitry Andric     attach_info.SetHijackListener(hijack_listener_sp);
30010b57cec5SDimitry Andric   }
30020b57cec5SDimitry Andric 
30030b57cec5SDimitry Andric   Status error;
30040b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp != nullptr &&
30050b57cec5SDimitry Andric       platform_sp->CanDebugProcess()) {
30060b57cec5SDimitry Andric     SetPlatform(platform_sp);
30070b57cec5SDimitry Andric     process_sp = platform_sp->Attach(attach_info, GetDebugger(), this, error);
30080b57cec5SDimitry Andric   } else {
30090b57cec5SDimitry Andric     if (state != eStateConnected) {
30100b57cec5SDimitry Andric       const char *plugin_name = attach_info.GetProcessPluginName();
30110b57cec5SDimitry Andric       process_sp =
30120b57cec5SDimitry Andric           CreateProcess(attach_info.GetListenerForProcess(GetDebugger()),
30130b57cec5SDimitry Andric                         plugin_name, nullptr);
30140b57cec5SDimitry Andric       if (process_sp == nullptr) {
30150b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
30160b57cec5SDimitry Andric             "failed to create process using plugin %s",
30170b57cec5SDimitry Andric             (plugin_name) ? plugin_name : "null");
30180b57cec5SDimitry Andric         return error;
30190b57cec5SDimitry Andric       }
30200b57cec5SDimitry Andric     }
30210b57cec5SDimitry Andric     if (hijack_listener_sp)
30220b57cec5SDimitry Andric       process_sp->HijackProcessEvents(hijack_listener_sp);
30230b57cec5SDimitry Andric     error = process_sp->Attach(attach_info);
30240b57cec5SDimitry Andric   }
30250b57cec5SDimitry Andric 
30260b57cec5SDimitry Andric   if (error.Success() && process_sp) {
30270b57cec5SDimitry Andric     if (async) {
30280b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
30290b57cec5SDimitry Andric     } else {
30300b57cec5SDimitry Andric       state = process_sp->WaitForProcessToStop(
30310b57cec5SDimitry Andric           llvm::None, nullptr, false, attach_info.GetHijackListener(), stream);
30320b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
30330b57cec5SDimitry Andric 
30340b57cec5SDimitry Andric       if (state != eStateStopped) {
30350b57cec5SDimitry Andric         const char *exit_desc = process_sp->GetExitDescription();
30360b57cec5SDimitry Andric         if (exit_desc)
30370b57cec5SDimitry Andric           error.SetErrorStringWithFormat("%s", exit_desc);
30380b57cec5SDimitry Andric         else
30390b57cec5SDimitry Andric           error.SetErrorString(
30400b57cec5SDimitry Andric               "process did not stop (no such process or permission problem?)");
30410b57cec5SDimitry Andric         process_sp->Destroy(false);
30420b57cec5SDimitry Andric       }
30430b57cec5SDimitry Andric     }
30440b57cec5SDimitry Andric   }
30450b57cec5SDimitry Andric   return error;
30460b57cec5SDimitry Andric }
30470b57cec5SDimitry Andric 
30480b57cec5SDimitry Andric void Target::FinalizeFileActions(ProcessLaunchInfo &info) {
30490b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
30500b57cec5SDimitry Andric 
30510b57cec5SDimitry Andric   // Finalize the file actions, and if none were given, default to opening up a
30520b57cec5SDimitry Andric   // pseudo terminal
30530b57cec5SDimitry Andric   PlatformSP platform_sp = GetPlatform();
30540b57cec5SDimitry Andric   const bool default_to_use_pty =
30550b57cec5SDimitry Andric       m_platform_sp ? m_platform_sp->IsHost() : false;
30560b57cec5SDimitry Andric   LLDB_LOG(
30570b57cec5SDimitry Andric       log,
30580b57cec5SDimitry Andric       "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}",
30590b57cec5SDimitry Andric       bool(platform_sp),
30600b57cec5SDimitry Andric       platform_sp ? (platform_sp->IsHost() ? "true" : "false") : "n/a",
30610b57cec5SDimitry Andric       default_to_use_pty);
30620b57cec5SDimitry Andric 
30630b57cec5SDimitry Andric   // If nothing for stdin or stdout or stderr was specified, then check the
30640b57cec5SDimitry Andric   // process for any default settings that were set with "settings set"
30650b57cec5SDimitry Andric   if (info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
30660b57cec5SDimitry Andric       info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
30670b57cec5SDimitry Andric       info.GetFileActionForFD(STDERR_FILENO) == nullptr) {
30680b57cec5SDimitry Andric     LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating "
30690b57cec5SDimitry Andric                   "default handling");
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
30720b57cec5SDimitry Andric       // Do nothing, if we are launching in a remote terminal no file actions
30730b57cec5SDimitry Andric       // should be done at all.
30740b57cec5SDimitry Andric       return;
30750b57cec5SDimitry Andric     }
30760b57cec5SDimitry Andric 
30770b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagDisableSTDIO)) {
30780b57cec5SDimitry Andric       LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action "
30790b57cec5SDimitry Andric                     "for stdin, stdout and stderr");
30800b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDIN_FILENO, true, false);
30810b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
30820b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDERR_FILENO, false, true);
30830b57cec5SDimitry Andric     } else {
30840b57cec5SDimitry Andric       // Check for any values that might have gotten set with any of: (lldb)
30850b57cec5SDimitry Andric       // settings set target.input-path (lldb) settings set target.output-path
30860b57cec5SDimitry Andric       // (lldb) settings set target.error-path
30870b57cec5SDimitry Andric       FileSpec in_file_spec;
30880b57cec5SDimitry Andric       FileSpec out_file_spec;
30890b57cec5SDimitry Andric       FileSpec err_file_spec;
30900b57cec5SDimitry Andric       // Only override with the target settings if we don't already have an
30910b57cec5SDimitry Andric       // action for in, out or error
30920b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDIN_FILENO) == nullptr)
30930b57cec5SDimitry Andric         in_file_spec = GetStandardInputPath();
30940b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr)
30950b57cec5SDimitry Andric         out_file_spec = GetStandardOutputPath();
30960b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDERR_FILENO) == nullptr)
30970b57cec5SDimitry Andric         err_file_spec = GetStandardErrorPath();
30980b57cec5SDimitry Andric 
30990b57cec5SDimitry Andric       LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{1}'",
31000b57cec5SDimitry Andric                in_file_spec, out_file_spec, err_file_spec);
31010b57cec5SDimitry Andric 
31020b57cec5SDimitry Andric       if (in_file_spec) {
31030b57cec5SDimitry Andric         info.AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
31040b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdin open file action for {0}", in_file_spec);
31050b57cec5SDimitry Andric       }
31060b57cec5SDimitry Andric 
31070b57cec5SDimitry Andric       if (out_file_spec) {
31080b57cec5SDimitry Andric         info.AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
31090b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdout open file action for {0}",
31100b57cec5SDimitry Andric                  out_file_spec);
31110b57cec5SDimitry Andric       }
31120b57cec5SDimitry Andric 
31130b57cec5SDimitry Andric       if (err_file_spec) {
31140b57cec5SDimitry Andric         info.AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
31150b57cec5SDimitry Andric         LLDB_LOG(log, "appended stderr open file action for {0}",
31160b57cec5SDimitry Andric                  err_file_spec);
31170b57cec5SDimitry Andric       }
31180b57cec5SDimitry Andric 
31190b57cec5SDimitry Andric       if (default_to_use_pty &&
31200b57cec5SDimitry Andric           (!in_file_spec || !out_file_spec || !err_file_spec)) {
31210b57cec5SDimitry Andric         llvm::Error Err = info.SetUpPtyRedirection();
31220b57cec5SDimitry Andric         LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
31230b57cec5SDimitry Andric       }
31240b57cec5SDimitry Andric     }
31250b57cec5SDimitry Andric   }
31260b57cec5SDimitry Andric }
31270b57cec5SDimitry Andric 
31280b57cec5SDimitry Andric // Target::StopHook
31290b57cec5SDimitry Andric Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid)
31300b57cec5SDimitry Andric     : UserID(uid), m_target_sp(target_sp), m_commands(), m_specifier_sp(),
31310b57cec5SDimitry Andric       m_thread_spec_up() {}
31320b57cec5SDimitry Andric 
31330b57cec5SDimitry Andric Target::StopHook::StopHook(const StopHook &rhs)
31340b57cec5SDimitry Andric     : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp),
31350b57cec5SDimitry Andric       m_commands(rhs.m_commands), m_specifier_sp(rhs.m_specifier_sp),
31360b57cec5SDimitry Andric       m_thread_spec_up(), m_active(rhs.m_active),
31370b57cec5SDimitry Andric       m_auto_continue(rhs.m_auto_continue) {
31380b57cec5SDimitry Andric   if (rhs.m_thread_spec_up)
31395ffd83dbSDimitry Andric     m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
31400b57cec5SDimitry Andric }
31410b57cec5SDimitry Andric 
31420b57cec5SDimitry Andric Target::StopHook::~StopHook() = default;
31430b57cec5SDimitry Andric 
31440b57cec5SDimitry Andric void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) {
31450b57cec5SDimitry Andric   m_specifier_sp.reset(specifier);
31460b57cec5SDimitry Andric }
31470b57cec5SDimitry Andric 
31480b57cec5SDimitry Andric void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) {
31490b57cec5SDimitry Andric   m_thread_spec_up.reset(specifier);
31500b57cec5SDimitry Andric }
31510b57cec5SDimitry Andric 
31520b57cec5SDimitry Andric void Target::StopHook::GetDescription(Stream *s,
31530b57cec5SDimitry Andric                                       lldb::DescriptionLevel level) const {
3154480093f4SDimitry Andric   unsigned indent_level = s->GetIndentLevel();
31550b57cec5SDimitry Andric 
31560b57cec5SDimitry Andric   s->SetIndentLevel(indent_level + 2);
31570b57cec5SDimitry Andric 
31580b57cec5SDimitry Andric   s->Printf("Hook: %" PRIu64 "\n", GetID());
31590b57cec5SDimitry Andric   if (m_active)
31600b57cec5SDimitry Andric     s->Indent("State: enabled\n");
31610b57cec5SDimitry Andric   else
31620b57cec5SDimitry Andric     s->Indent("State: disabled\n");
31630b57cec5SDimitry Andric 
31640b57cec5SDimitry Andric   if (m_auto_continue)
31650b57cec5SDimitry Andric     s->Indent("AutoContinue on\n");
31660b57cec5SDimitry Andric 
31670b57cec5SDimitry Andric   if (m_specifier_sp) {
31680b57cec5SDimitry Andric     s->Indent();
31690b57cec5SDimitry Andric     s->PutCString("Specifier:\n");
31700b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 4);
31710b57cec5SDimitry Andric     m_specifier_sp->GetDescription(s, level);
31720b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 2);
31730b57cec5SDimitry Andric   }
31740b57cec5SDimitry Andric 
31750b57cec5SDimitry Andric   if (m_thread_spec_up) {
31760b57cec5SDimitry Andric     StreamString tmp;
31770b57cec5SDimitry Andric     s->Indent("Thread:\n");
31780b57cec5SDimitry Andric     m_thread_spec_up->GetDescription(&tmp, level);
31790b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 4);
31800b57cec5SDimitry Andric     s->Indent(tmp.GetString());
31810b57cec5SDimitry Andric     s->PutCString("\n");
31820b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 2);
31830b57cec5SDimitry Andric   }
31840b57cec5SDimitry Andric 
31850b57cec5SDimitry Andric   s->Indent("Commands: \n");
31860b57cec5SDimitry Andric   s->SetIndentLevel(indent_level + 4);
31870b57cec5SDimitry Andric   uint32_t num_commands = m_commands.GetSize();
31880b57cec5SDimitry Andric   for (uint32_t i = 0; i < num_commands; i++) {
31890b57cec5SDimitry Andric     s->Indent(m_commands.GetStringAtIndex(i));
31900b57cec5SDimitry Andric     s->PutCString("\n");
31910b57cec5SDimitry Andric   }
31920b57cec5SDimitry Andric   s->SetIndentLevel(indent_level);
31930b57cec5SDimitry Andric }
31940b57cec5SDimitry Andric 
31950b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
31969dba64beSDimitry Andric     {
31979dba64beSDimitry Andric         eNoDynamicValues,
31989dba64beSDimitry Andric         "no-dynamic-values",
31999dba64beSDimitry Andric         "Don't calculate the dynamic type of values",
32009dba64beSDimitry Andric     },
32019dba64beSDimitry Andric     {
32029dba64beSDimitry Andric         eDynamicCanRunTarget,
32039dba64beSDimitry Andric         "run-target",
32049dba64beSDimitry Andric         "Calculate the dynamic type of values "
32059dba64beSDimitry Andric         "even if you have to run the target.",
32069dba64beSDimitry Andric     },
32079dba64beSDimitry Andric     {
32089dba64beSDimitry Andric         eDynamicDontRunTarget,
32099dba64beSDimitry Andric         "no-run-target",
32109dba64beSDimitry Andric         "Calculate the dynamic type of values, but don't run the target.",
32119dba64beSDimitry Andric     },
32129dba64beSDimitry Andric };
32130b57cec5SDimitry Andric 
32140b57cec5SDimitry Andric OptionEnumValues lldb_private::GetDynamicValueTypes() {
32150b57cec5SDimitry Andric   return OptionEnumValues(g_dynamic_value_types);
32160b57cec5SDimitry Andric }
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = {
32199dba64beSDimitry Andric     {
32209dba64beSDimitry Andric         eInlineBreakpointsNever,
32219dba64beSDimitry Andric         "never",
32229dba64beSDimitry Andric         "Never look for inline breakpoint locations (fastest). This setting "
32239dba64beSDimitry Andric         "should only be used if you know that no inlining occurs in your"
32249dba64beSDimitry Andric         "programs.",
32259dba64beSDimitry Andric     },
32269dba64beSDimitry Andric     {
32279dba64beSDimitry Andric         eInlineBreakpointsHeaders,
32289dba64beSDimitry Andric         "headers",
32299dba64beSDimitry Andric         "Only check for inline breakpoint locations when setting breakpoints "
32309dba64beSDimitry Andric         "in header files, but not when setting breakpoint in implementation "
32319dba64beSDimitry Andric         "source files (default).",
32329dba64beSDimitry Andric     },
32339dba64beSDimitry Andric     {
32349dba64beSDimitry Andric         eInlineBreakpointsAlways,
32359dba64beSDimitry Andric         "always",
32369dba64beSDimitry Andric         "Always look for inline breakpoint locations when setting file and "
32379dba64beSDimitry Andric         "line breakpoints (slower but most accurate).",
32389dba64beSDimitry Andric     },
32399dba64beSDimitry Andric };
32400b57cec5SDimitry Andric 
32410b57cec5SDimitry Andric enum x86DisassemblyFlavor {
32420b57cec5SDimitry Andric   eX86DisFlavorDefault,
32430b57cec5SDimitry Andric   eX86DisFlavorIntel,
32440b57cec5SDimitry Andric   eX86DisFlavorATT
32450b57cec5SDimitry Andric };
32460b57cec5SDimitry Andric 
32470b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = {
32489dba64beSDimitry Andric     {
32499dba64beSDimitry Andric         eX86DisFlavorDefault,
32509dba64beSDimitry Andric         "default",
32519dba64beSDimitry Andric         "Disassembler default (currently att).",
32529dba64beSDimitry Andric     },
32539dba64beSDimitry Andric     {
32549dba64beSDimitry Andric         eX86DisFlavorIntel,
32559dba64beSDimitry Andric         "intel",
32569dba64beSDimitry Andric         "Intel disassembler flavor.",
32579dba64beSDimitry Andric     },
32589dba64beSDimitry Andric     {
32599dba64beSDimitry Andric         eX86DisFlavorATT,
32609dba64beSDimitry Andric         "att",
32619dba64beSDimitry Andric         "AT&T disassembler flavor.",
32629dba64beSDimitry Andric     },
32639dba64beSDimitry Andric };
32640b57cec5SDimitry Andric 
32650b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = {
32669dba64beSDimitry Andric     {
32679dba64beSDimitry Andric         Disassembler::eHexStyleC,
32689dba64beSDimitry Andric         "c",
32699dba64beSDimitry Andric         "C-style (0xffff).",
32709dba64beSDimitry Andric     },
32719dba64beSDimitry Andric     {
32729dba64beSDimitry Andric         Disassembler::eHexStyleAsm,
32739dba64beSDimitry Andric         "asm",
32749dba64beSDimitry Andric         "Asm-style (0ffffh).",
32759dba64beSDimitry Andric     },
32769dba64beSDimitry Andric };
32770b57cec5SDimitry Andric 
32780b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = {
32799dba64beSDimitry Andric     {
32809dba64beSDimitry Andric         eLoadScriptFromSymFileTrue,
32819dba64beSDimitry Andric         "true",
32829dba64beSDimitry Andric         "Load debug scripts inside symbol files",
32839dba64beSDimitry Andric     },
32849dba64beSDimitry Andric     {
32859dba64beSDimitry Andric         eLoadScriptFromSymFileFalse,
32869dba64beSDimitry Andric         "false",
32879dba64beSDimitry Andric         "Do not load debug scripts inside symbol files.",
32889dba64beSDimitry Andric     },
32899dba64beSDimitry Andric     {
32909dba64beSDimitry Andric         eLoadScriptFromSymFileWarn,
32919dba64beSDimitry Andric         "warn",
32929dba64beSDimitry Andric         "Warn about debug scripts inside symbol files but do not load them.",
32939dba64beSDimitry Andric     },
32949dba64beSDimitry Andric };
32950b57cec5SDimitry Andric 
32969dba64beSDimitry Andric static constexpr OptionEnumValueElement g_load_cwd_lldbinit_values[] = {
32979dba64beSDimitry Andric     {
32989dba64beSDimitry Andric         eLoadCWDlldbinitTrue,
32999dba64beSDimitry Andric         "true",
33009dba64beSDimitry Andric         "Load .lldbinit files from current directory",
33019dba64beSDimitry Andric     },
33029dba64beSDimitry Andric     {
33039dba64beSDimitry Andric         eLoadCWDlldbinitFalse,
33049dba64beSDimitry Andric         "false",
33059dba64beSDimitry Andric         "Do not load .lldbinit files from current directory",
33069dba64beSDimitry Andric     },
33079dba64beSDimitry Andric     {
33089dba64beSDimitry Andric         eLoadCWDlldbinitWarn,
33099dba64beSDimitry Andric         "warn",
33109dba64beSDimitry Andric         "Warn about loading .lldbinit files from current directory",
33119dba64beSDimitry Andric     },
33129dba64beSDimitry Andric };
33130b57cec5SDimitry Andric 
33140b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = {
33159dba64beSDimitry Andric     {
33169dba64beSDimitry Andric         eMemoryModuleLoadLevelMinimal,
33179dba64beSDimitry Andric         "minimal",
33180b57cec5SDimitry Andric         "Load minimal information when loading modules from memory. Currently "
33199dba64beSDimitry Andric         "this setting loads sections only.",
33209dba64beSDimitry Andric     },
33219dba64beSDimitry Andric     {
33229dba64beSDimitry Andric         eMemoryModuleLoadLevelPartial,
33239dba64beSDimitry Andric         "partial",
33240b57cec5SDimitry Andric         "Load partial information when loading modules from memory. Currently "
33259dba64beSDimitry Andric         "this setting loads sections and function bounds.",
33269dba64beSDimitry Andric     },
33279dba64beSDimitry Andric     {
33289dba64beSDimitry Andric         eMemoryModuleLoadLevelComplete,
33299dba64beSDimitry Andric         "complete",
33300b57cec5SDimitry Andric         "Load complete information when loading modules from memory. Currently "
33319dba64beSDimitry Andric         "this setting loads sections and all symbols.",
33329dba64beSDimitry Andric     },
33339dba64beSDimitry Andric };
33340b57cec5SDimitry Andric 
33359dba64beSDimitry Andric #define LLDB_PROPERTIES_target
33369dba64beSDimitry Andric #include "TargetProperties.inc"
33370b57cec5SDimitry Andric 
33380b57cec5SDimitry Andric enum {
33399dba64beSDimitry Andric #define LLDB_PROPERTIES_target
33409dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
33410b57cec5SDimitry Andric   ePropertyExperimental,
33420b57cec5SDimitry Andric };
33430b57cec5SDimitry Andric 
33440b57cec5SDimitry Andric class TargetOptionValueProperties : public OptionValueProperties {
33450b57cec5SDimitry Andric public:
33465ffd83dbSDimitry Andric   TargetOptionValueProperties(ConstString name) : OptionValueProperties(name) {}
33470b57cec5SDimitry Andric 
33480b57cec5SDimitry Andric   // This constructor is used when creating TargetOptionValueProperties when it
33490b57cec5SDimitry Andric   // is part of a new lldb_private::Target instance. It will copy all current
33500b57cec5SDimitry Andric   // global property values as needed
33515ffd83dbSDimitry Andric   TargetOptionValueProperties(const TargetPropertiesSP &target_properties_sp)
33525ffd83dbSDimitry Andric       : OptionValueProperties(*target_properties_sp->GetValueProperties()) {}
33530b57cec5SDimitry Andric 
33540b57cec5SDimitry Andric   const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
33550b57cec5SDimitry Andric                                      bool will_modify,
33560b57cec5SDimitry Andric                                      uint32_t idx) const override {
33570b57cec5SDimitry Andric     // When getting the value for a key from the target options, we will always
33580b57cec5SDimitry Andric     // try and grab the setting from the current target if there is one. Else
33590b57cec5SDimitry Andric     // we just use the one from this instance.
33600b57cec5SDimitry Andric     if (exe_ctx) {
33610b57cec5SDimitry Andric       Target *target = exe_ctx->GetTargetPtr();
33620b57cec5SDimitry Andric       if (target) {
33630b57cec5SDimitry Andric         TargetOptionValueProperties *target_properties =
33640b57cec5SDimitry Andric             static_cast<TargetOptionValueProperties *>(
33650b57cec5SDimitry Andric                 target->GetValueProperties().get());
33660b57cec5SDimitry Andric         if (this != target_properties)
33670b57cec5SDimitry Andric           return target_properties->ProtectedGetPropertyAtIndex(idx);
33680b57cec5SDimitry Andric       }
33690b57cec5SDimitry Andric     }
33700b57cec5SDimitry Andric     return ProtectedGetPropertyAtIndex(idx);
33710b57cec5SDimitry Andric   }
33720b57cec5SDimitry Andric };
33730b57cec5SDimitry Andric 
33740b57cec5SDimitry Andric // TargetProperties
33755ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
33769dba64beSDimitry Andric #include "TargetProperties.inc"
33770b57cec5SDimitry Andric 
33789dba64beSDimitry Andric enum {
33795ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
33809dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
33819dba64beSDimitry Andric };
33820b57cec5SDimitry Andric 
33830b57cec5SDimitry Andric class TargetExperimentalOptionValueProperties : public OptionValueProperties {
33840b57cec5SDimitry Andric public:
33850b57cec5SDimitry Andric   TargetExperimentalOptionValueProperties()
33860b57cec5SDimitry Andric       : OptionValueProperties(
33870b57cec5SDimitry Andric             ConstString(Properties::GetExperimentalSettingsName())) {}
33880b57cec5SDimitry Andric };
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric TargetExperimentalProperties::TargetExperimentalProperties()
33910b57cec5SDimitry Andric     : Properties(OptionValuePropertiesSP(
33920b57cec5SDimitry Andric           new TargetExperimentalOptionValueProperties())) {
33935ffd83dbSDimitry Andric   m_collection_sp->Initialize(g_target_experimental_properties);
33940b57cec5SDimitry Andric }
33950b57cec5SDimitry Andric 
33960b57cec5SDimitry Andric // TargetProperties
33970b57cec5SDimitry Andric TargetProperties::TargetProperties(Target *target)
33985ffd83dbSDimitry Andric     : Properties(), m_launch_info(), m_target(target) {
33990b57cec5SDimitry Andric   if (target) {
34000b57cec5SDimitry Andric     m_collection_sp = std::make_shared<TargetOptionValueProperties>(
34015ffd83dbSDimitry Andric         Target::GetGlobalProperties());
34020b57cec5SDimitry Andric 
34030b57cec5SDimitry Andric     // Set callbacks to update launch_info whenever "settins set" updated any
34040b57cec5SDimitry Andric     // of these properties
34050b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3406480093f4SDimitry Andric         ePropertyArg0, [this] { Arg0ValueChangedCallback(); });
34070b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3408480093f4SDimitry Andric         ePropertyRunArgs, [this] { RunArgsValueChangedCallback(); });
34090b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3410480093f4SDimitry Andric         ePropertyEnvVars, [this] { EnvVarsValueChangedCallback(); });
34110b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
34125ffd83dbSDimitry Andric         ePropertyUnsetEnvVars, [this] { EnvVarsValueChangedCallback(); });
34135ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
34145ffd83dbSDimitry Andric         ePropertyInheritEnv, [this] { EnvVarsValueChangedCallback(); });
34155ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
3416480093f4SDimitry Andric         ePropertyInputPath, [this] { InputPathValueChangedCallback(); });
34170b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3418480093f4SDimitry Andric         ePropertyOutputPath, [this] { OutputPathValueChangedCallback(); });
34190b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3420480093f4SDimitry Andric         ePropertyErrorPath, [this] { ErrorPathValueChangedCallback(); });
3421480093f4SDimitry Andric     m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, [this] {
3422480093f4SDimitry Andric       DetachOnErrorValueChangedCallback();
3423480093f4SDimitry Andric     });
34240b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3425480093f4SDimitry Andric         ePropertyDisableASLR, [this] { DisableASLRValueChangedCallback(); });
34260b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3427480093f4SDimitry Andric         ePropertyDisableSTDIO, [this] { DisableSTDIOValueChangedCallback(); });
34280b57cec5SDimitry Andric 
34295ffd83dbSDimitry Andric     m_experimental_properties_up =
34305ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
34310b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
34320b57cec5SDimitry Andric         ConstString(Properties::GetExperimentalSettingsName()),
34330b57cec5SDimitry Andric         ConstString("Experimental settings - setting these won't produce "
34340b57cec5SDimitry Andric                     "errors if the setting is not present."),
34350b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
34360b57cec5SDimitry Andric   } else {
34370b57cec5SDimitry Andric     m_collection_sp =
34380b57cec5SDimitry Andric         std::make_shared<TargetOptionValueProperties>(ConstString("target"));
34399dba64beSDimitry Andric     m_collection_sp->Initialize(g_target_properties);
34405ffd83dbSDimitry Andric     m_experimental_properties_up =
34415ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
34420b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
34430b57cec5SDimitry Andric         ConstString(Properties::GetExperimentalSettingsName()),
34440b57cec5SDimitry Andric         ConstString("Experimental settings - setting these won't produce "
34450b57cec5SDimitry Andric                     "errors if the setting is not present."),
34460b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
34470b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
34480b57cec5SDimitry Andric         ConstString("process"), ConstString("Settings specific to processes."),
34490b57cec5SDimitry Andric         true, Process::GetGlobalProperties()->GetValueProperties());
34500b57cec5SDimitry Andric   }
34510b57cec5SDimitry Andric }
34520b57cec5SDimitry Andric 
34530b57cec5SDimitry Andric TargetProperties::~TargetProperties() = default;
34540b57cec5SDimitry Andric 
34555ffd83dbSDimitry Andric void TargetProperties::UpdateLaunchInfoFromProperties() {
34565ffd83dbSDimitry Andric   Arg0ValueChangedCallback();
34575ffd83dbSDimitry Andric   RunArgsValueChangedCallback();
34585ffd83dbSDimitry Andric   EnvVarsValueChangedCallback();
34595ffd83dbSDimitry Andric   InputPathValueChangedCallback();
34605ffd83dbSDimitry Andric   OutputPathValueChangedCallback();
34615ffd83dbSDimitry Andric   ErrorPathValueChangedCallback();
34625ffd83dbSDimitry Andric   DetachOnErrorValueChangedCallback();
34635ffd83dbSDimitry Andric   DisableASLRValueChangedCallback();
34645ffd83dbSDimitry Andric   DisableSTDIOValueChangedCallback();
34655ffd83dbSDimitry Andric }
34665ffd83dbSDimitry Andric 
34670b57cec5SDimitry Andric bool TargetProperties::GetInjectLocalVariables(
34680b57cec5SDimitry Andric     ExecutionContext *exe_ctx) const {
34690b57cec5SDimitry Andric   const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
34700b57cec5SDimitry Andric       exe_ctx, false, ePropertyExperimental);
34710b57cec5SDimitry Andric   OptionValueProperties *exp_values =
34720b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
34730b57cec5SDimitry Andric   if (exp_values)
34740b57cec5SDimitry Andric     return exp_values->GetPropertyAtIndexAsBoolean(
34750b57cec5SDimitry Andric         exe_ctx, ePropertyInjectLocalVars, true);
34760b57cec5SDimitry Andric   else
34770b57cec5SDimitry Andric     return true;
34780b57cec5SDimitry Andric }
34790b57cec5SDimitry Andric 
34800b57cec5SDimitry Andric void TargetProperties::SetInjectLocalVariables(ExecutionContext *exe_ctx,
34810b57cec5SDimitry Andric                                                bool b) {
34820b57cec5SDimitry Andric   const Property *exp_property =
34830b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndex(exe_ctx, true, ePropertyExperimental);
34840b57cec5SDimitry Andric   OptionValueProperties *exp_values =
34850b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
34860b57cec5SDimitry Andric   if (exp_values)
34870b57cec5SDimitry Andric     exp_values->SetPropertyAtIndexAsBoolean(exe_ctx, ePropertyInjectLocalVars,
34880b57cec5SDimitry Andric                                             true);
34890b57cec5SDimitry Andric }
34900b57cec5SDimitry Andric 
34910b57cec5SDimitry Andric ArchSpec TargetProperties::GetDefaultArchitecture() const {
34920b57cec5SDimitry Andric   OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
34930b57cec5SDimitry Andric       nullptr, ePropertyDefaultArch);
34940b57cec5SDimitry Andric   if (value)
34950b57cec5SDimitry Andric     return value->GetCurrentValue();
34960b57cec5SDimitry Andric   return ArchSpec();
34970b57cec5SDimitry Andric }
34980b57cec5SDimitry Andric 
34990b57cec5SDimitry Andric void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) {
35000b57cec5SDimitry Andric   OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
35010b57cec5SDimitry Andric       nullptr, ePropertyDefaultArch);
35020b57cec5SDimitry Andric   if (value)
35030b57cec5SDimitry Andric     return value->SetCurrentValue(arch, true);
35040b57cec5SDimitry Andric }
35050b57cec5SDimitry Andric 
35060b57cec5SDimitry Andric bool TargetProperties::GetMoveToNearestCode() const {
35070b57cec5SDimitry Andric   const uint32_t idx = ePropertyMoveToNearestCode;
35080b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35099dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
35100b57cec5SDimitry Andric }
35110b57cec5SDimitry Andric 
35120b57cec5SDimitry Andric lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const {
35130b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
35140b57cec5SDimitry Andric   return (lldb::DynamicValueType)
35150b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
35169dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
35170b57cec5SDimitry Andric }
35180b57cec5SDimitry Andric 
35190b57cec5SDimitry Andric bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
35200b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
35210b57cec5SDimitry Andric   return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
35220b57cec5SDimitry Andric }
35230b57cec5SDimitry Andric 
35240b57cec5SDimitry Andric bool TargetProperties::GetPreloadSymbols() const {
35250b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
35260b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35279dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
35280b57cec5SDimitry Andric }
35290b57cec5SDimitry Andric 
35300b57cec5SDimitry Andric void TargetProperties::SetPreloadSymbols(bool b) {
35310b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
35320b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
35330b57cec5SDimitry Andric }
35340b57cec5SDimitry Andric 
35350b57cec5SDimitry Andric bool TargetProperties::GetDisableASLR() const {
35360b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
35370b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35389dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
35390b57cec5SDimitry Andric }
35400b57cec5SDimitry Andric 
35410b57cec5SDimitry Andric void TargetProperties::SetDisableASLR(bool b) {
35420b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
35430b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
35440b57cec5SDimitry Andric }
35450b57cec5SDimitry Andric 
35460b57cec5SDimitry Andric bool TargetProperties::GetDetachOnError() const {
35470b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
35480b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35499dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
35500b57cec5SDimitry Andric }
35510b57cec5SDimitry Andric 
35520b57cec5SDimitry Andric void TargetProperties::SetDetachOnError(bool b) {
35530b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
35540b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
35550b57cec5SDimitry Andric }
35560b57cec5SDimitry Andric 
35570b57cec5SDimitry Andric bool TargetProperties::GetDisableSTDIO() const {
35580b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
35590b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35609dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
35610b57cec5SDimitry Andric }
35620b57cec5SDimitry Andric 
35630b57cec5SDimitry Andric void TargetProperties::SetDisableSTDIO(bool b) {
35640b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
35650b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
35660b57cec5SDimitry Andric }
35670b57cec5SDimitry Andric 
35680b57cec5SDimitry Andric const char *TargetProperties::GetDisassemblyFlavor() const {
35690b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisassemblyFlavor;
35700b57cec5SDimitry Andric   const char *return_value;
35710b57cec5SDimitry Andric 
35720b57cec5SDimitry Andric   x86DisassemblyFlavor flavor_value =
35730b57cec5SDimitry Andric       (x86DisassemblyFlavor)m_collection_sp->GetPropertyAtIndexAsEnumeration(
35749dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
35750b57cec5SDimitry Andric   return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
35760b57cec5SDimitry Andric   return return_value;
35770b57cec5SDimitry Andric }
35780b57cec5SDimitry Andric 
35790b57cec5SDimitry Andric InlineStrategy TargetProperties::GetInlineStrategy() const {
35800b57cec5SDimitry Andric   const uint32_t idx = ePropertyInlineStrategy;
35810b57cec5SDimitry Andric   return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(
35829dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
35830b57cec5SDimitry Andric }
35840b57cec5SDimitry Andric 
35850b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetArg0() const {
35860b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
35879dba64beSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx,
35889dba64beSDimitry Andric                                                      llvm::StringRef());
35890b57cec5SDimitry Andric }
35900b57cec5SDimitry Andric 
35910b57cec5SDimitry Andric void TargetProperties::SetArg0(llvm::StringRef arg) {
35920b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
35939dba64beSDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
35940b57cec5SDimitry Andric   m_launch_info.SetArg0(arg);
35950b57cec5SDimitry Andric }
35960b57cec5SDimitry Andric 
35970b57cec5SDimitry Andric bool TargetProperties::GetRunArguments(Args &args) const {
35980b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
35990b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
36000b57cec5SDimitry Andric }
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric void TargetProperties::SetRunArguments(const Args &args) {
36030b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
36040b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
36050b57cec5SDimitry Andric   m_launch_info.GetArguments() = args;
36060b57cec5SDimitry Andric }
36070b57cec5SDimitry Andric 
36085ffd83dbSDimitry Andric Environment TargetProperties::ComputeEnvironment() const {
36095ffd83dbSDimitry Andric   Environment env;
36105ffd83dbSDimitry Andric 
36115ffd83dbSDimitry Andric   if (m_target &&
36125ffd83dbSDimitry Andric       m_collection_sp->GetPropertyAtIndexAsBoolean(
36135ffd83dbSDimitry Andric           nullptr, ePropertyInheritEnv,
36145ffd83dbSDimitry Andric           g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) {
36155ffd83dbSDimitry Andric     if (auto platform_sp = m_target->GetPlatform()) {
36165ffd83dbSDimitry Andric       Environment platform_env = platform_sp->GetEnvironment();
36175ffd83dbSDimitry Andric       for (const auto &KV : platform_env)
36185ffd83dbSDimitry Andric         env[KV.first()] = KV.second;
36195ffd83dbSDimitry Andric     }
36205ffd83dbSDimitry Andric   }
36215ffd83dbSDimitry Andric 
36225ffd83dbSDimitry Andric   Args property_unset_env;
36235ffd83dbSDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, ePropertyUnsetEnvVars,
36245ffd83dbSDimitry Andric                                             property_unset_env);
36255ffd83dbSDimitry Andric   for (const auto &var : property_unset_env)
36265ffd83dbSDimitry Andric     env.erase(var.ref());
36275ffd83dbSDimitry Andric 
36285ffd83dbSDimitry Andric   Args property_env;
36295ffd83dbSDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, ePropertyEnvVars,
36305ffd83dbSDimitry Andric                                             property_env);
36315ffd83dbSDimitry Andric   for (const auto &KV : Environment(property_env))
36325ffd83dbSDimitry Andric     env[KV.first()] = KV.second;
36335ffd83dbSDimitry Andric 
36345ffd83dbSDimitry Andric   return env;
36355ffd83dbSDimitry Andric }
36365ffd83dbSDimitry Andric 
36370b57cec5SDimitry Andric Environment TargetProperties::GetEnvironment() const {
36385ffd83dbSDimitry Andric   return ComputeEnvironment();
36390b57cec5SDimitry Andric }
36400b57cec5SDimitry Andric 
36410b57cec5SDimitry Andric void TargetProperties::SetEnvironment(Environment env) {
36420b57cec5SDimitry Andric   // TODO: Get rid of the Args intermediate step
36430b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnvVars;
36440b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, Args(env));
36450b57cec5SDimitry Andric }
36460b57cec5SDimitry Andric 
36470b57cec5SDimitry Andric bool TargetProperties::GetSkipPrologue() const {
36480b57cec5SDimitry Andric   const uint32_t idx = ePropertySkipPrologue;
36490b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
36509dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
36510b57cec5SDimitry Andric }
36520b57cec5SDimitry Andric 
36530b57cec5SDimitry Andric PathMappingList &TargetProperties::GetSourcePathMap() const {
36540b57cec5SDimitry Andric   const uint32_t idx = ePropertySourceMap;
36550b57cec5SDimitry Andric   OptionValuePathMappings *option_value =
36560b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr,
36570b57cec5SDimitry Andric                                                                    false, idx);
36580b57cec5SDimitry Andric   assert(option_value);
36590b57cec5SDimitry Andric   return option_value->GetCurrentValue();
36600b57cec5SDimitry Andric }
36610b57cec5SDimitry Andric 
36620b57cec5SDimitry Andric void TargetProperties::AppendExecutableSearchPaths(const FileSpec &dir) {
36630b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
36640b57cec5SDimitry Andric   OptionValueFileSpecList *option_value =
36650b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
36660b57cec5SDimitry Andric                                                                    false, idx);
36670b57cec5SDimitry Andric   assert(option_value);
36680b57cec5SDimitry Andric   option_value->AppendCurrentValue(dir);
36690b57cec5SDimitry Andric }
36700b57cec5SDimitry Andric 
36710b57cec5SDimitry Andric FileSpecList TargetProperties::GetExecutableSearchPaths() {
36720b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
36730b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
36740b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
36750b57cec5SDimitry Andric                                                                    false, idx);
36760b57cec5SDimitry Andric   assert(option_value);
36770b57cec5SDimitry Andric   return option_value->GetCurrentValue();
36780b57cec5SDimitry Andric }
36790b57cec5SDimitry Andric 
36800b57cec5SDimitry Andric FileSpecList TargetProperties::GetDebugFileSearchPaths() {
36810b57cec5SDimitry Andric   const uint32_t idx = ePropertyDebugFileSearchPaths;
36820b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
36830b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
36840b57cec5SDimitry Andric                                                                    false, idx);
36850b57cec5SDimitry Andric   assert(option_value);
36860b57cec5SDimitry Andric   return option_value->GetCurrentValue();
36870b57cec5SDimitry Andric }
36880b57cec5SDimitry Andric 
36890b57cec5SDimitry Andric FileSpecList TargetProperties::GetClangModuleSearchPaths() {
36900b57cec5SDimitry Andric   const uint32_t idx = ePropertyClangModuleSearchPaths;
36910b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
36920b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
36930b57cec5SDimitry Andric                                                                    false, idx);
36940b57cec5SDimitry Andric   assert(option_value);
36950b57cec5SDimitry Andric   return option_value->GetCurrentValue();
36960b57cec5SDimitry Andric }
36970b57cec5SDimitry Andric 
36980b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoImportClangModules() const {
36990b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoImportClangModules;
37000b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37019dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37020b57cec5SDimitry Andric }
37030b57cec5SDimitry Andric 
37040b57cec5SDimitry Andric bool TargetProperties::GetEnableImportStdModule() const {
37050b57cec5SDimitry Andric   const uint32_t idx = ePropertyImportStdModule;
37060b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37079dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37080b57cec5SDimitry Andric }
37090b57cec5SDimitry Andric 
37100b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoApplyFixIts() const {
37110b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoApplyFixIts;
37120b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37139dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37140b57cec5SDimitry Andric }
37150b57cec5SDimitry Andric 
37165ffd83dbSDimitry Andric uint64_t TargetProperties::GetNumberOfRetriesWithFixits() const {
37175ffd83dbSDimitry Andric   const uint32_t idx = ePropertyRetriesWithFixIts;
37185ffd83dbSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsUInt64(
37195ffd83dbSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37205ffd83dbSDimitry Andric }
37215ffd83dbSDimitry Andric 
37220b57cec5SDimitry Andric bool TargetProperties::GetEnableNotifyAboutFixIts() const {
37230b57cec5SDimitry Andric   const uint32_t idx = ePropertyNotifyAboutFixIts;
37240b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37259dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37260b57cec5SDimitry Andric }
37270b57cec5SDimitry Andric 
37280b57cec5SDimitry Andric bool TargetProperties::GetEnableSaveObjects() const {
37290b57cec5SDimitry Andric   const uint32_t idx = ePropertySaveObjects;
37300b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37319dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37320b57cec5SDimitry Andric }
37330b57cec5SDimitry Andric 
37340b57cec5SDimitry Andric bool TargetProperties::GetEnableSyntheticValue() const {
37350b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnableSynthetic;
37360b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37379dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37389dba64beSDimitry Andric }
37399dba64beSDimitry Andric 
37409dba64beSDimitry Andric uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
37419dba64beSDimitry Andric   const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
37429dba64beSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsUInt64(
37439dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37440b57cec5SDimitry Andric }
37450b57cec5SDimitry Andric 
37460b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const {
37470b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxChildrenCount;
37480b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37499dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37500b57cec5SDimitry Andric }
37510b57cec5SDimitry Andric 
37520b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const {
37530b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxSummaryLength;
37540b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37559dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37560b57cec5SDimitry Andric }
37570b57cec5SDimitry Andric 
37580b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumMemReadSize() const {
37590b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxMemReadSize;
37600b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37619dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37620b57cec5SDimitry Andric }
37630b57cec5SDimitry Andric 
37640b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardInputPath() const {
37650b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
37660b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
37670b57cec5SDimitry Andric }
37680b57cec5SDimitry Andric 
37690b57cec5SDimitry Andric void TargetProperties::SetStandardInputPath(llvm::StringRef path) {
37700b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
37710b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
37720b57cec5SDimitry Andric }
37730b57cec5SDimitry Andric 
37740b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardOutputPath() const {
37750b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
37760b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
37770b57cec5SDimitry Andric }
37780b57cec5SDimitry Andric 
37790b57cec5SDimitry Andric void TargetProperties::SetStandardOutputPath(llvm::StringRef path) {
37800b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
37810b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
37820b57cec5SDimitry Andric }
37830b57cec5SDimitry Andric 
37840b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardErrorPath() const {
37850b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
37860b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
37870b57cec5SDimitry Andric }
37880b57cec5SDimitry Andric 
37890b57cec5SDimitry Andric void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
37900b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
37910b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
37920b57cec5SDimitry Andric }
37930b57cec5SDimitry Andric 
37940b57cec5SDimitry Andric LanguageType TargetProperties::GetLanguage() const {
37950b57cec5SDimitry Andric   OptionValueLanguage *value =
37960b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(
37970b57cec5SDimitry Andric           nullptr, ePropertyLanguage);
37980b57cec5SDimitry Andric   if (value)
37990b57cec5SDimitry Andric     return value->GetCurrentValue();
38000b57cec5SDimitry Andric   return LanguageType();
38010b57cec5SDimitry Andric }
38020b57cec5SDimitry Andric 
38030b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
38040b57cec5SDimitry Andric   const uint32_t idx = ePropertyExprPrefix;
38050b57cec5SDimitry Andric   OptionValueFileSpec *file =
38060b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
38070b57cec5SDimitry Andric                                                                idx);
38080b57cec5SDimitry Andric   if (file) {
38090b57cec5SDimitry Andric     DataBufferSP data_sp(file->GetFileContents());
38100b57cec5SDimitry Andric     if (data_sp)
38110b57cec5SDimitry Andric       return llvm::StringRef(
38120b57cec5SDimitry Andric           reinterpret_cast<const char *>(data_sp->GetBytes()),
38130b57cec5SDimitry Andric           data_sp->GetByteSize());
38140b57cec5SDimitry Andric   }
38150b57cec5SDimitry Andric   return "";
38160b57cec5SDimitry Andric }
38170b57cec5SDimitry Andric 
38180b57cec5SDimitry Andric bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() {
38190b57cec5SDimitry Andric   const uint32_t idx = ePropertyBreakpointUseAvoidList;
38200b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38219dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
38220b57cec5SDimitry Andric }
38230b57cec5SDimitry Andric 
38240b57cec5SDimitry Andric bool TargetProperties::GetUseHexImmediates() const {
38250b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseHexImmediates;
38260b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38279dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
38280b57cec5SDimitry Andric }
38290b57cec5SDimitry Andric 
38300b57cec5SDimitry Andric bool TargetProperties::GetUseFastStepping() const {
38310b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseFastStepping;
38320b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38339dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
38340b57cec5SDimitry Andric }
38350b57cec5SDimitry Andric 
38360b57cec5SDimitry Andric bool TargetProperties::GetDisplayExpressionsInCrashlogs() const {
38370b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
38380b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38399dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
38400b57cec5SDimitry Andric }
38410b57cec5SDimitry Andric 
38420b57cec5SDimitry Andric LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const {
38430b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
38440b57cec5SDimitry Andric   return (LoadScriptFromSymFile)
38450b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38469dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
38470b57cec5SDimitry Andric }
38480b57cec5SDimitry Andric 
38490b57cec5SDimitry Andric LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const {
38500b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadCWDlldbinitFile;
38510b57cec5SDimitry Andric   return (LoadCWDlldbinitFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(
38529dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
38530b57cec5SDimitry Andric }
38540b57cec5SDimitry Andric 
38550b57cec5SDimitry Andric Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const {
38560b57cec5SDimitry Andric   const uint32_t idx = ePropertyHexImmediateStyle;
38570b57cec5SDimitry Andric   return (Disassembler::HexImmediateStyle)
38580b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38599dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
38600b57cec5SDimitry Andric }
38610b57cec5SDimitry Andric 
38620b57cec5SDimitry Andric MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const {
38630b57cec5SDimitry Andric   const uint32_t idx = ePropertyMemoryModuleLoadLevel;
38640b57cec5SDimitry Andric   return (MemoryModuleLoadLevel)
38650b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38669dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
38670b57cec5SDimitry Andric }
38680b57cec5SDimitry Andric 
38690b57cec5SDimitry Andric bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const {
38700b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
38710b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
38720b57cec5SDimitry Andric }
38730b57cec5SDimitry Andric 
38740b57cec5SDimitry Andric void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) {
38750b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
38760b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
38770b57cec5SDimitry Andric }
38780b57cec5SDimitry Andric 
38790b57cec5SDimitry Andric bool TargetProperties::GetDisplayRuntimeSupportValues() const {
38800b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
38810b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
38820b57cec5SDimitry Andric }
38830b57cec5SDimitry Andric 
38840b57cec5SDimitry Andric void TargetProperties::SetDisplayRuntimeSupportValues(bool b) {
38850b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
38860b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
38870b57cec5SDimitry Andric }
38880b57cec5SDimitry Andric 
38890b57cec5SDimitry Andric bool TargetProperties::GetDisplayRecognizedArguments() const {
38900b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
38910b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
38920b57cec5SDimitry Andric }
38930b57cec5SDimitry Andric 
38940b57cec5SDimitry Andric void TargetProperties::SetDisplayRecognizedArguments(bool b) {
38950b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
38960b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
38970b57cec5SDimitry Andric }
38980b57cec5SDimitry Andric 
38990b57cec5SDimitry Andric bool TargetProperties::GetNonStopModeEnabled() const {
39000b57cec5SDimitry Andric   const uint32_t idx = ePropertyNonStopModeEnabled;
39010b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
39020b57cec5SDimitry Andric }
39030b57cec5SDimitry Andric 
39040b57cec5SDimitry Andric void TargetProperties::SetNonStopModeEnabled(bool b) {
39050b57cec5SDimitry Andric   const uint32_t idx = ePropertyNonStopModeEnabled;
39060b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
39070b57cec5SDimitry Andric }
39080b57cec5SDimitry Andric 
39090b57cec5SDimitry Andric const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() {
39100b57cec5SDimitry Andric   m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
39110b57cec5SDimitry Andric   return m_launch_info;
39120b57cec5SDimitry Andric }
39130b57cec5SDimitry Andric 
39140b57cec5SDimitry Andric void TargetProperties::SetProcessLaunchInfo(
39150b57cec5SDimitry Andric     const ProcessLaunchInfo &launch_info) {
39160b57cec5SDimitry Andric   m_launch_info = launch_info;
39170b57cec5SDimitry Andric   SetArg0(launch_info.GetArg0());
39180b57cec5SDimitry Andric   SetRunArguments(launch_info.GetArguments());
39190b57cec5SDimitry Andric   SetEnvironment(launch_info.GetEnvironment());
39200b57cec5SDimitry Andric   const FileAction *input_file_action =
39210b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDIN_FILENO);
39220b57cec5SDimitry Andric   if (input_file_action) {
39230b57cec5SDimitry Andric     SetStandardInputPath(input_file_action->GetPath());
39240b57cec5SDimitry Andric   }
39250b57cec5SDimitry Andric   const FileAction *output_file_action =
39260b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDOUT_FILENO);
39270b57cec5SDimitry Andric   if (output_file_action) {
39280b57cec5SDimitry Andric     SetStandardOutputPath(output_file_action->GetPath());
39290b57cec5SDimitry Andric   }
39300b57cec5SDimitry Andric   const FileAction *error_file_action =
39310b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDERR_FILENO);
39320b57cec5SDimitry Andric   if (error_file_action) {
39330b57cec5SDimitry Andric     SetStandardErrorPath(error_file_action->GetPath());
39340b57cec5SDimitry Andric   }
39350b57cec5SDimitry Andric   SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
39360b57cec5SDimitry Andric   SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
39370b57cec5SDimitry Andric   SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
39380b57cec5SDimitry Andric }
39390b57cec5SDimitry Andric 
39400b57cec5SDimitry Andric bool TargetProperties::GetRequireHardwareBreakpoints() const {
39410b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
39420b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
39439dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
39440b57cec5SDimitry Andric }
39450b57cec5SDimitry Andric 
39460b57cec5SDimitry Andric void TargetProperties::SetRequireHardwareBreakpoints(bool b) {
39470b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
39480b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
39490b57cec5SDimitry Andric }
39500b57cec5SDimitry Andric 
39515ffd83dbSDimitry Andric bool TargetProperties::GetAutoInstallMainExecutable() const {
39525ffd83dbSDimitry Andric   const uint32_t idx = ePropertyAutoInstallMainExecutable;
39535ffd83dbSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
39545ffd83dbSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
39555ffd83dbSDimitry Andric }
39565ffd83dbSDimitry Andric 
3957480093f4SDimitry Andric void TargetProperties::Arg0ValueChangedCallback() {
3958480093f4SDimitry Andric   m_launch_info.SetArg0(GetArg0());
39590b57cec5SDimitry Andric }
39600b57cec5SDimitry Andric 
3961480093f4SDimitry Andric void TargetProperties::RunArgsValueChangedCallback() {
39620b57cec5SDimitry Andric   Args args;
3963480093f4SDimitry Andric   if (GetRunArguments(args))
3964480093f4SDimitry Andric     m_launch_info.GetArguments() = args;
39650b57cec5SDimitry Andric }
39660b57cec5SDimitry Andric 
3967480093f4SDimitry Andric void TargetProperties::EnvVarsValueChangedCallback() {
39685ffd83dbSDimitry Andric   m_launch_info.GetEnvironment() = ComputeEnvironment();
39690b57cec5SDimitry Andric }
39700b57cec5SDimitry Andric 
3971480093f4SDimitry Andric void TargetProperties::InputPathValueChangedCallback() {
3972480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDIN_FILENO, GetStandardInputPath(), true,
3973480093f4SDimitry Andric                                      false);
39740b57cec5SDimitry Andric }
39750b57cec5SDimitry Andric 
3976480093f4SDimitry Andric void TargetProperties::OutputPathValueChangedCallback() {
3977480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDOUT_FILENO, GetStandardOutputPath(),
3978480093f4SDimitry Andric                                      false, true);
39790b57cec5SDimitry Andric }
39800b57cec5SDimitry Andric 
3981480093f4SDimitry Andric void TargetProperties::ErrorPathValueChangedCallback() {
3982480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDERR_FILENO, GetStandardErrorPath(),
3983480093f4SDimitry Andric                                      false, true);
39840b57cec5SDimitry Andric }
39850b57cec5SDimitry Andric 
3986480093f4SDimitry Andric void TargetProperties::DetachOnErrorValueChangedCallback() {
3987480093f4SDimitry Andric   if (GetDetachOnError())
3988480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
39890b57cec5SDimitry Andric   else
3990480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
39910b57cec5SDimitry Andric }
39920b57cec5SDimitry Andric 
3993480093f4SDimitry Andric void TargetProperties::DisableASLRValueChangedCallback() {
3994480093f4SDimitry Andric   if (GetDisableASLR())
3995480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
39960b57cec5SDimitry Andric   else
3997480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
39980b57cec5SDimitry Andric }
39990b57cec5SDimitry Andric 
4000480093f4SDimitry Andric void TargetProperties::DisableSTDIOValueChangedCallback() {
4001480093f4SDimitry Andric   if (GetDisableSTDIO())
4002480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
40030b57cec5SDimitry Andric   else
4004480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
40050b57cec5SDimitry Andric }
40060b57cec5SDimitry Andric 
40070b57cec5SDimitry Andric // Target::TargetEventData
40080b57cec5SDimitry Andric 
40090b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
40100b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list() {}
40110b57cec5SDimitry Andric 
40120b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
40130b57cec5SDimitry Andric                                          const ModuleList &module_list)
40140b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
40150b57cec5SDimitry Andric 
40160b57cec5SDimitry Andric Target::TargetEventData::~TargetEventData() = default;
40170b57cec5SDimitry Andric 
40180b57cec5SDimitry Andric ConstString Target::TargetEventData::GetFlavorString() {
40190b57cec5SDimitry Andric   static ConstString g_flavor("Target::TargetEventData");
40200b57cec5SDimitry Andric   return g_flavor;
40210b57cec5SDimitry Andric }
40220b57cec5SDimitry Andric 
40230b57cec5SDimitry Andric void Target::TargetEventData::Dump(Stream *s) const {
40240b57cec5SDimitry Andric   for (size_t i = 0; i < m_module_list.GetSize(); ++i) {
40250b57cec5SDimitry Andric     if (i != 0)
40260b57cec5SDimitry Andric       *s << ", ";
40270b57cec5SDimitry Andric     m_module_list.GetModuleAtIndex(i)->GetDescription(
4028480093f4SDimitry Andric         s->AsRawOstream(), lldb::eDescriptionLevelBrief);
40290b57cec5SDimitry Andric   }
40300b57cec5SDimitry Andric }
40310b57cec5SDimitry Andric 
40320b57cec5SDimitry Andric const Target::TargetEventData *
40330b57cec5SDimitry Andric Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) {
40340b57cec5SDimitry Andric   if (event_ptr) {
40350b57cec5SDimitry Andric     const EventData *event_data = event_ptr->GetData();
40360b57cec5SDimitry Andric     if (event_data &&
40370b57cec5SDimitry Andric         event_data->GetFlavor() == TargetEventData::GetFlavorString())
40380b57cec5SDimitry Andric       return static_cast<const TargetEventData *>(event_ptr->GetData());
40390b57cec5SDimitry Andric   }
40400b57cec5SDimitry Andric   return nullptr;
40410b57cec5SDimitry Andric }
40420b57cec5SDimitry Andric 
40430b57cec5SDimitry Andric TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
40440b57cec5SDimitry Andric   TargetSP target_sp;
40450b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
40460b57cec5SDimitry Andric   if (event_data)
40470b57cec5SDimitry Andric     target_sp = event_data->m_target_sp;
40480b57cec5SDimitry Andric   return target_sp;
40490b57cec5SDimitry Andric }
40500b57cec5SDimitry Andric 
40510b57cec5SDimitry Andric ModuleList
40520b57cec5SDimitry Andric Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
40530b57cec5SDimitry Andric   ModuleList module_list;
40540b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
40550b57cec5SDimitry Andric   if (event_data)
40560b57cec5SDimitry Andric     module_list = event_data->m_module_list;
40570b57cec5SDimitry Andric   return module_list;
40580b57cec5SDimitry Andric }
40599dba64beSDimitry Andric 
40609dba64beSDimitry Andric std::recursive_mutex &Target::GetAPIMutex() {
40619dba64beSDimitry Andric   if (GetProcessSP() && GetProcessSP()->CurrentThreadIsPrivateStateThread())
40629dba64beSDimitry Andric     return m_private_mutex;
40639dba64beSDimitry Andric   else
40649dba64beSDimitry Andric     return m_mutex;
40659dba64beSDimitry Andric }
4066