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 "lldb/Breakpoint/BreakpointIDList.h"
110b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointPrecondition.h"
120b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolver.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverAddress.h"
140b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
150b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
160b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverName.h"
170b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
180b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h"
190b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
200b57cec5SDimitry Andric #include "lldb/Core/Module.h"
210b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
220b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
230b57cec5SDimitry Andric #include "lldb/Core/SearchFilter.h"
240b57cec5SDimitry Andric #include "lldb/Core/Section.h"
250b57cec5SDimitry Andric #include "lldb/Core/SourceManager.h"
260b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
270b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
28bdd1243dSDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
29e8d8bef9SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
305ffd83dbSDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
310b57cec5SDimitry Andric #include "lldb/Expression/REPL.h"
320b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h"
33e8d8bef9SDimitry Andric #include "lldb/Expression/UtilityFunction.h"
340b57cec5SDimitry Andric #include "lldb/Host/Host.h"
350b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h"
36c9157d92SDimitry Andric #include "lldb/Host/StreamFile.h"
370b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
380b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
390b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupWatchpoint.h"
400b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValues.h"
410b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
420b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
430b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
440b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
45fe6060f1SDimitry Andric #include "lldb/Target/ABI.h"
460b57cec5SDimitry Andric #include "lldb/Target/Language.h"
470b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
480b57cec5SDimitry Andric #include "lldb/Target/Process.h"
49fe013be4SDimitry Andric #include "lldb/Target/RegisterTypeBuilder.h"
500b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
510b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
52e8d8bef9SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
530b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h"
540b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
550b57cec5SDimitry Andric #include "lldb/Target/ThreadSpec.h"
5681ad6265SDimitry Andric #include "lldb/Target/UnixSignals.h"
570b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
580b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
590b57cec5SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
6081ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
610b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
620b57cec5SDimitry Andric #include "lldb/Utility/State.h"
630b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
640b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
67349cc55cSDimitry Andric #include "llvm/ADT/SetVector.h"
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric #include <memory>
700b57cec5SDimitry Andric #include <mutex>
71bdd1243dSDimitry Andric #include <optional>
72fe013be4SDimitry Andric #include <sstream>
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric using namespace lldb;
750b57cec5SDimitry Andric using namespace lldb_private;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
780b57cec5SDimitry Andric 
Arch(const ArchSpec & spec)790b57cec5SDimitry Andric Target::Arch::Arch(const ArchSpec &spec)
800b57cec5SDimitry Andric     : m_spec(spec),
810b57cec5SDimitry Andric       m_plugin_up(PluginManager::CreateArchitectureInstance(spec)) {}
820b57cec5SDimitry Andric 
operator =(const ArchSpec & spec)830b57cec5SDimitry Andric const Target::Arch &Target::Arch::operator=(const ArchSpec &spec) {
840b57cec5SDimitry Andric   m_spec = spec;
850b57cec5SDimitry Andric   m_plugin_up = PluginManager::CreateArchitectureInstance(spec);
860b57cec5SDimitry Andric   return *this;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
GetStaticBroadcasterClass()890b57cec5SDimitry Andric ConstString &Target::GetStaticBroadcasterClass() {
900b57cec5SDimitry Andric   static ConstString class_name("lldb.target");
910b57cec5SDimitry Andric   return class_name;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
Target(Debugger & debugger,const ArchSpec & target_arch,const lldb::PlatformSP & platform_sp,bool is_dummy_target)940b57cec5SDimitry Andric Target::Target(Debugger &debugger, const ArchSpec &target_arch,
950b57cec5SDimitry Andric                const lldb::PlatformSP &platform_sp, bool is_dummy_target)
960b57cec5SDimitry Andric     : TargetProperties(this),
970b57cec5SDimitry Andric       Broadcaster(debugger.GetBroadcasterManager(),
980b57cec5SDimitry Andric                   Target::GetStaticBroadcasterClass().AsCString()),
990b57cec5SDimitry Andric       ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp),
1000b57cec5SDimitry Andric       m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
1010b57cec5SDimitry Andric       m_breakpoint_list(false), m_internal_breakpoint_list(true),
1020b57cec5SDimitry Andric       m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
1035ffd83dbSDimitry Andric       m_image_search_paths(ImageSearchPathsChanged, this),
1040b57cec5SDimitry Andric       m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0),
105349cc55cSDimitry Andric       m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false),
1060b57cec5SDimitry Andric       m_is_dummy_target(is_dummy_target),
107e8d8bef9SDimitry Andric       m_frame_recognizer_manager_up(
108349cc55cSDimitry Andric           std::make_unique<StackFrameRecognizerManager>()) {
1090b57cec5SDimitry Andric   SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
1100b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
1110b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
1120b57cec5SDimitry Andric   SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
1130b57cec5SDimitry Andric   SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   CheckInWithManager();
1160b57cec5SDimitry Andric 
11781ad6265SDimitry Andric   LLDB_LOG(GetLog(LLDBLog::Object), "{0} Target::Target()",
11881ad6265SDimitry Andric            static_cast<void *>(this));
1190b57cec5SDimitry Andric   if (target_arch.IsValid()) {
12081ad6265SDimitry Andric     LLDB_LOG(GetLog(LLDBLog::Target),
1219dba64beSDimitry Andric              "Target::Target created with architecture {0} ({1})",
1220b57cec5SDimitry Andric              target_arch.GetArchitectureName(),
1230b57cec5SDimitry Andric              target_arch.GetTriple().getTriple().c_str());
1240b57cec5SDimitry Andric   }
1255ffd83dbSDimitry Andric 
1265ffd83dbSDimitry Andric   UpdateLaunchInfoFromProperties();
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
~Target()1290b57cec5SDimitry Andric Target::~Target() {
13081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
1319dba64beSDimitry Andric   LLDB_LOG(log, "{0} Target::~Target()", static_cast<void *>(this));
1320b57cec5SDimitry Andric   DeleteCurrentProcess();
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
PrimeFromDummyTarget(Target & target)135e8d8bef9SDimitry Andric void Target::PrimeFromDummyTarget(Target &target) {
136e8d8bef9SDimitry Andric   m_stop_hooks = target.m_stop_hooks;
1370b57cec5SDimitry Andric 
138e8d8bef9SDimitry Andric   for (const auto &breakpoint_sp : target.m_breakpoint_list.Breakpoints()) {
1390b57cec5SDimitry Andric     if (breakpoint_sp->IsInternal())
1400b57cec5SDimitry Andric       continue;
1410b57cec5SDimitry Andric 
1425ffd83dbSDimitry Andric     BreakpointSP new_bp(
1435ffd83dbSDimitry Andric         Breakpoint::CopyFromBreakpoint(shared_from_this(), *breakpoint_sp));
1445ffd83dbSDimitry Andric     AddBreakpoint(std::move(new_bp), false);
1450b57cec5SDimitry Andric   }
1460b57cec5SDimitry Andric 
147bdd1243dSDimitry Andric   for (const auto &bp_name_entry : target.m_breakpoint_names) {
148bdd1243dSDimitry Andric     AddBreakpointName(std::make_unique<BreakpointName>(*bp_name_entry.second));
1490b57cec5SDimitry Andric   }
150e8d8bef9SDimitry Andric 
151e8d8bef9SDimitry Andric   m_frame_recognizer_manager_up = std::make_unique<StackFrameRecognizerManager>(
152e8d8bef9SDimitry Andric       *target.m_frame_recognizer_manager_up);
15381ad6265SDimitry Andric 
15481ad6265SDimitry Andric   m_dummy_signals = target.m_dummy_signals;
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
Dump(Stream * s,lldb::DescriptionLevel description_level)1570b57cec5SDimitry Andric void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {
1580b57cec5SDimitry Andric   //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
1590b57cec5SDimitry Andric   if (description_level != lldb::eDescriptionLevelBrief) {
1600b57cec5SDimitry Andric     s->Indent();
1610b57cec5SDimitry Andric     s->PutCString("Target\n");
1620b57cec5SDimitry Andric     s->IndentMore();
1630b57cec5SDimitry Andric     m_images.Dump(s);
1640b57cec5SDimitry Andric     m_breakpoint_list.Dump(s);
1650b57cec5SDimitry Andric     m_internal_breakpoint_list.Dump(s);
1660b57cec5SDimitry Andric     s->IndentLess();
1670b57cec5SDimitry Andric   } else {
1680b57cec5SDimitry Andric     Module *exe_module = GetExecutableModulePointer();
1690b57cec5SDimitry Andric     if (exe_module)
1700b57cec5SDimitry Andric       s->PutCString(exe_module->GetFileSpec().GetFilename().GetCString());
1710b57cec5SDimitry Andric     else
1720b57cec5SDimitry Andric       s->PutCString("No executable module.");
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
CleanupProcess()1760b57cec5SDimitry Andric void Target::CleanupProcess() {
1770b57cec5SDimitry Andric   // Do any cleanup of the target we need to do between process instances.
1780b57cec5SDimitry Andric   // NB It is better to do this before destroying the process in case the
1790b57cec5SDimitry Andric   // clean up needs some help from the process.
1800b57cec5SDimitry Andric   m_breakpoint_list.ClearAllBreakpointSites();
1810b57cec5SDimitry Andric   m_internal_breakpoint_list.ClearAllBreakpointSites();
182bdd1243dSDimitry Andric   ResetBreakpointHitCounts();
1830b57cec5SDimitry Andric   // Disable watchpoints just on the debugger side.
1840b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
1850b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
1860b57cec5SDimitry Andric   DisableAllWatchpoints(false);
1870b57cec5SDimitry Andric   ClearAllWatchpointHitCounts();
1880b57cec5SDimitry Andric   ClearAllWatchpointHistoricValues();
189fe6060f1SDimitry Andric   m_latest_stop_hook_id = 0;
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric 
DeleteCurrentProcess()1920b57cec5SDimitry Andric void Target::DeleteCurrentProcess() {
1930b57cec5SDimitry Andric   if (m_process_sp) {
19481ad6265SDimitry Andric     // We dispose any active tracing sessions on the current process
19581ad6265SDimitry Andric     m_trace_sp.reset();
1960b57cec5SDimitry Andric     m_section_load_history.Clear();
1970b57cec5SDimitry Andric     if (m_process_sp->IsAlive())
1980b57cec5SDimitry Andric       m_process_sp->Destroy(false);
1990b57cec5SDimitry Andric 
200c9157d92SDimitry Andric     m_process_sp->Finalize(false /* not destructing */);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric     CleanupProcess();
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     m_process_sp.reset();
2050b57cec5SDimitry Andric   }
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric 
CreateProcess(ListenerSP listener_sp,llvm::StringRef plugin_name,const FileSpec * crash_file,bool can_connect)2080b57cec5SDimitry Andric const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp,
2090b57cec5SDimitry Andric                                              llvm::StringRef plugin_name,
210e8d8bef9SDimitry Andric                                              const FileSpec *crash_file,
211e8d8bef9SDimitry Andric                                              bool can_connect) {
2120b57cec5SDimitry Andric   if (!listener_sp)
2130b57cec5SDimitry Andric     listener_sp = GetDebugger().GetListener();
2140b57cec5SDimitry Andric   DeleteCurrentProcess();
2150b57cec5SDimitry Andric   m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name,
216e8d8bef9SDimitry Andric                                      listener_sp, crash_file, can_connect);
2170b57cec5SDimitry Andric   return m_process_sp;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
GetProcessSP() const2200b57cec5SDimitry Andric const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; }
2210b57cec5SDimitry Andric 
GetREPL(Status & err,lldb::LanguageType language,const char * repl_options,bool can_create)2220b57cec5SDimitry Andric lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language,
2230b57cec5SDimitry Andric                              const char *repl_options, bool can_create) {
22404eeddc0SDimitry Andric   if (language == eLanguageTypeUnknown)
22504eeddc0SDimitry Andric     language = m_debugger.GetREPLLanguage();
22604eeddc0SDimitry Andric 
2270b57cec5SDimitry Andric   if (language == eLanguageTypeUnknown) {
2289dba64beSDimitry Andric     LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
2290b57cec5SDimitry Andric 
2309dba64beSDimitry Andric     if (auto single_lang = repl_languages.GetSingularLanguage()) {
2319dba64beSDimitry Andric       language = *single_lang;
2329dba64beSDimitry Andric     } else if (repl_languages.Empty()) {
23304eeddc0SDimitry Andric       err.SetErrorString(
2340b57cec5SDimitry Andric           "LLDB isn't configured with REPL support for any languages.");
2350b57cec5SDimitry Andric       return REPLSP();
2360b57cec5SDimitry Andric     } else {
23704eeddc0SDimitry Andric       err.SetErrorString(
2380b57cec5SDimitry Andric           "Multiple possible REPL languages.  Please specify a language.");
2390b57cec5SDimitry Andric       return REPLSP();
2400b57cec5SDimitry Andric     }
2410b57cec5SDimitry Andric   }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   REPLMap::iterator pos = m_repl_map.find(language);
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   if (pos != m_repl_map.end()) {
2460b57cec5SDimitry Andric     return pos->second;
2470b57cec5SDimitry Andric   }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric   if (!can_create) {
2500b57cec5SDimitry Andric     err.SetErrorStringWithFormat(
2510b57cec5SDimitry Andric         "Couldn't find an existing REPL for %s, and can't create a new one",
2520b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
2530b57cec5SDimitry Andric     return lldb::REPLSP();
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   Debugger *const debugger = nullptr;
2570b57cec5SDimitry Andric   lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   if (ret) {
2600b57cec5SDimitry Andric     m_repl_map[language] = ret;
2610b57cec5SDimitry Andric     return m_repl_map[language];
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   if (err.Success()) {
2650b57cec5SDimitry Andric     err.SetErrorStringWithFormat("Couldn't create a REPL for %s",
2660b57cec5SDimitry Andric                                  Language::GetNameForLanguageType(language));
2670b57cec5SDimitry Andric   }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   return lldb::REPLSP();
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric 
SetREPL(lldb::LanguageType language,lldb::REPLSP repl_sp)2720b57cec5SDimitry Andric void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) {
2730b57cec5SDimitry Andric   lldbassert(!m_repl_map.count(language));
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   m_repl_map[language] = repl_sp;
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
Destroy()2780b57cec5SDimitry Andric void Target::Destroy() {
2790b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2800b57cec5SDimitry Andric   m_valid = false;
2810b57cec5SDimitry Andric   DeleteCurrentProcess();
2820b57cec5SDimitry Andric   m_platform_sp.reset();
2830b57cec5SDimitry Andric   m_arch = ArchSpec();
2840b57cec5SDimitry Andric   ClearModules(true);
2850b57cec5SDimitry Andric   m_section_load_history.Clear();
2860b57cec5SDimitry Andric   const bool notify = false;
2870b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(notify);
2880b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveAll(notify);
2890b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
290bdd1243dSDimitry Andric   m_watchpoint_list.RemoveAll(notify);
2910b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
2920b57cec5SDimitry Andric   m_search_filter_sp.reset();
2930b57cec5SDimitry Andric   m_image_search_paths.Clear(notify);
2940b57cec5SDimitry Andric   m_stop_hooks.clear();
2950b57cec5SDimitry Andric   m_stop_hook_next_id = 0;
2960b57cec5SDimitry Andric   m_suppress_stop_hooks = false;
297fe013be4SDimitry Andric   m_repl_map.clear();
29881ad6265SDimitry Andric   Args signal_args;
29981ad6265SDimitry Andric   ClearDummySignals(signal_args);
30081ad6265SDimitry Andric }
30181ad6265SDimitry Andric 
GetABIName() const30281ad6265SDimitry Andric llvm::StringRef Target::GetABIName() const {
30381ad6265SDimitry Andric   lldb::ABISP abi_sp;
30481ad6265SDimitry Andric   if (m_process_sp)
30581ad6265SDimitry Andric     abi_sp = m_process_sp->GetABI();
30681ad6265SDimitry Andric   if (!abi_sp)
30781ad6265SDimitry Andric     abi_sp = ABI::FindPlugin(ProcessSP(), GetArchitecture());
30881ad6265SDimitry Andric   if (abi_sp)
30981ad6265SDimitry Andric       return abi_sp->GetPluginName();
31081ad6265SDimitry Andric   return {};
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric 
GetBreakpointList(bool internal)3130b57cec5SDimitry Andric BreakpointList &Target::GetBreakpointList(bool internal) {
3140b57cec5SDimitry Andric   if (internal)
3150b57cec5SDimitry Andric     return m_internal_breakpoint_list;
3160b57cec5SDimitry Andric   else
3170b57cec5SDimitry Andric     return m_breakpoint_list;
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric 
GetBreakpointList(bool internal) const3200b57cec5SDimitry Andric const BreakpointList &Target::GetBreakpointList(bool internal) const {
3210b57cec5SDimitry Andric   if (internal)
3220b57cec5SDimitry Andric     return m_internal_breakpoint_list;
3230b57cec5SDimitry Andric   else
3240b57cec5SDimitry Andric     return m_breakpoint_list;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
GetBreakpointByID(break_id_t break_id)3270b57cec5SDimitry Andric BreakpointSP Target::GetBreakpointByID(break_id_t break_id) {
3280b57cec5SDimitry Andric   BreakpointSP bp_sp;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
3310b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
3320b57cec5SDimitry Andric   else
3330b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   return bp_sp;
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric 
338c9157d92SDimitry Andric lldb::BreakpointSP
CreateBreakpointAtUserEntry(Status & error)339c9157d92SDimitry Andric lldb_private::Target::CreateBreakpointAtUserEntry(Status &error) {
340c9157d92SDimitry Andric   ModuleSP main_module_sp = GetExecutableModule();
341c9157d92SDimitry Andric   FileSpecList shared_lib_filter;
342c9157d92SDimitry Andric   shared_lib_filter.Append(main_module_sp->GetFileSpec());
343c9157d92SDimitry Andric   llvm::SetVector<std::string, std::vector<std::string>,
344c9157d92SDimitry Andric                   std::unordered_set<std::string>>
345c9157d92SDimitry Andric       entryPointNamesSet;
346c9157d92SDimitry Andric   for (LanguageType lang_type : Language::GetSupportedLanguages()) {
347c9157d92SDimitry Andric     Language *lang = Language::FindPlugin(lang_type);
348c9157d92SDimitry Andric     if (!lang) {
349c9157d92SDimitry Andric       error.SetErrorString("Language not found\n");
350c9157d92SDimitry Andric       return lldb::BreakpointSP();
351c9157d92SDimitry Andric     }
352c9157d92SDimitry Andric     std::string entryPointName = lang->GetUserEntryPointName().str();
353c9157d92SDimitry Andric     if (!entryPointName.empty())
354c9157d92SDimitry Andric       entryPointNamesSet.insert(entryPointName);
355c9157d92SDimitry Andric   }
356c9157d92SDimitry Andric   if (entryPointNamesSet.empty()) {
357c9157d92SDimitry Andric     error.SetErrorString("No entry point name found\n");
358c9157d92SDimitry Andric     return lldb::BreakpointSP();
359c9157d92SDimitry Andric   }
360c9157d92SDimitry Andric   BreakpointSP bp_sp = CreateBreakpoint(
361c9157d92SDimitry Andric       &shared_lib_filter,
362c9157d92SDimitry Andric       /*containingSourceFiles=*/nullptr, entryPointNamesSet.takeVector(),
363c9157d92SDimitry Andric       /*func_name_type_mask=*/eFunctionNameTypeFull,
364c9157d92SDimitry Andric       /*language=*/eLanguageTypeUnknown,
365c9157d92SDimitry Andric       /*offset=*/0,
366c9157d92SDimitry Andric       /*skip_prologue=*/eLazyBoolNo,
367c9157d92SDimitry Andric       /*internal=*/false,
368c9157d92SDimitry Andric       /*hardware=*/false);
369c9157d92SDimitry Andric   if (!bp_sp) {
370c9157d92SDimitry Andric     error.SetErrorString("Breakpoint creation failed.\n");
371c9157d92SDimitry Andric     return lldb::BreakpointSP();
372c9157d92SDimitry Andric   }
373c9157d92SDimitry Andric   bp_sp->SetOneShot(true);
374c9157d92SDimitry Andric   return bp_sp;
375c9157d92SDimitry Andric }
376c9157d92SDimitry Andric 
CreateSourceRegexBreakpoint(const FileSpecList * containingModules,const FileSpecList * source_file_spec_list,const std::unordered_set<std::string> & function_names,RegularExpression source_regex,bool internal,bool hardware,LazyBool move_to_nearest_code)3770b57cec5SDimitry Andric BreakpointSP Target::CreateSourceRegexBreakpoint(
3780b57cec5SDimitry Andric     const FileSpecList *containingModules,
3790b57cec5SDimitry Andric     const FileSpecList *source_file_spec_list,
3800b57cec5SDimitry Andric     const std::unordered_set<std::string> &function_names,
3819dba64beSDimitry Andric     RegularExpression source_regex, bool internal, bool hardware,
3820b57cec5SDimitry Andric     LazyBool move_to_nearest_code) {
3830b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
3840b57cec5SDimitry Andric       containingModules, source_file_spec_list));
3850b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
3860b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
3870b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(
3889dba64beSDimitry Andric       nullptr, std::move(source_regex), function_names,
3890b57cec5SDimitry Andric       !static_cast<bool>(move_to_nearest_code)));
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
CreateBreakpoint(const FileSpecList * containingModules,const FileSpec & file,uint32_t line_no,uint32_t column,lldb::addr_t offset,LazyBool check_inlines,LazyBool skip_prologue,bool internal,bool hardware,LazyBool move_to_nearest_code)3940b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules,
3950b57cec5SDimitry Andric                                       const FileSpec &file, uint32_t line_no,
3960b57cec5SDimitry Andric                                       uint32_t column, lldb::addr_t offset,
3970b57cec5SDimitry Andric                                       LazyBool check_inlines,
3980b57cec5SDimitry Andric                                       LazyBool skip_prologue, bool internal,
3990b57cec5SDimitry Andric                                       bool hardware,
4000b57cec5SDimitry Andric                                       LazyBool move_to_nearest_code) {
4010b57cec5SDimitry Andric   FileSpec remapped_file;
402bdd1243dSDimitry Andric   std::optional<llvm::StringRef> removed_prefix_opt =
403bdd1243dSDimitry Andric       GetSourcePathMap().ReverseRemapPath(file, remapped_file);
404bdd1243dSDimitry Andric   if (!removed_prefix_opt)
4050b57cec5SDimitry Andric     remapped_file = file;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   if (check_inlines == eLazyBoolCalculate) {
4080b57cec5SDimitry Andric     const InlineStrategy inline_strategy = GetInlineStrategy();
4090b57cec5SDimitry Andric     switch (inline_strategy) {
4100b57cec5SDimitry Andric     case eInlineBreakpointsNever:
4110b57cec5SDimitry Andric       check_inlines = eLazyBoolNo;
4120b57cec5SDimitry Andric       break;
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric     case eInlineBreakpointsHeaders:
4150b57cec5SDimitry Andric       if (remapped_file.IsSourceImplementationFile())
4160b57cec5SDimitry Andric         check_inlines = eLazyBoolNo;
4170b57cec5SDimitry Andric       else
4180b57cec5SDimitry Andric         check_inlines = eLazyBoolYes;
4190b57cec5SDimitry Andric       break;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric     case eInlineBreakpointsAlways:
4220b57cec5SDimitry Andric       check_inlines = eLazyBoolYes;
4230b57cec5SDimitry Andric       break;
4240b57cec5SDimitry Andric     }
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric   SearchFilterSP filter_sp;
4270b57cec5SDimitry Andric   if (check_inlines == eLazyBoolNo) {
4280b57cec5SDimitry Andric     // Not checking for inlines, we are looking only for matching compile units
4290b57cec5SDimitry Andric     FileSpecList compile_unit_list;
4300b57cec5SDimitry Andric     compile_unit_list.Append(remapped_file);
4310b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
4320b57cec5SDimitry Andric                                                   &compile_unit_list);
4330b57cec5SDimitry Andric   } else {
4340b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
4350b57cec5SDimitry Andric   }
4360b57cec5SDimitry Andric   if (skip_prologue == eLazyBoolCalculate)
4370b57cec5SDimitry Andric     skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
4380b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
4390b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
4400b57cec5SDimitry Andric 
441fe6060f1SDimitry Andric   SourceLocationSpec location_spec(remapped_file, line_no, column,
442fe6060f1SDimitry Andric                                    check_inlines,
443fe6060f1SDimitry Andric                                    !static_cast<bool>(move_to_nearest_code));
444fe6060f1SDimitry Andric   if (!location_spec)
445fe6060f1SDimitry Andric     return nullptr;
446fe6060f1SDimitry Andric 
4470b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(
448bdd1243dSDimitry Andric       nullptr, offset, skip_prologue, location_spec, removed_prefix_opt));
4490b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric 
CreateBreakpoint(lldb::addr_t addr,bool internal,bool hardware)4520b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal,
4530b57cec5SDimitry Andric                                       bool hardware) {
4540b57cec5SDimitry Andric   Address so_addr;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   // Check for any reason we want to move this breakpoint to other address.
4570b57cec5SDimitry Andric   addr = GetBreakableLoadAddress(addr);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   // Attempt to resolve our load address if possible, though it is ok if it
4600b57cec5SDimitry Andric   // doesn't resolve to section/offset.
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric   // Try and resolve as a load address if possible
4630b57cec5SDimitry Andric   GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
4640b57cec5SDimitry Andric   if (!so_addr.IsValid()) {
4650b57cec5SDimitry Andric     // The address didn't resolve, so just set this as an absolute address
4660b57cec5SDimitry Andric     so_addr.SetOffset(addr);
4670b57cec5SDimitry Andric   }
4680b57cec5SDimitry Andric   BreakpointSP bp_sp(CreateBreakpoint(so_addr, internal, hardware));
4690b57cec5SDimitry Andric   return bp_sp;
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric 
CreateBreakpoint(const Address & addr,bool internal,bool hardware)4720b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
4730b57cec5SDimitry Andric                                       bool hardware) {
4740b57cec5SDimitry Andric   SearchFilterSP filter_sp(
4750b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
4760b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(
4770b57cec5SDimitry Andric       new BreakpointResolverAddress(nullptr, addr));
4780b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, false);
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric lldb::BreakpointSP
CreateAddressInModuleBreakpoint(lldb::addr_t file_addr,bool internal,const FileSpec & file_spec,bool request_hardware)4820b57cec5SDimitry Andric Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
483c9157d92SDimitry Andric                                         const FileSpec &file_spec,
4840b57cec5SDimitry Andric                                         bool request_hardware) {
4850b57cec5SDimitry Andric   SearchFilterSP filter_sp(
4860b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
487480093f4SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(
488c9157d92SDimitry Andric       nullptr, file_addr, file_spec));
4890b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
4900b57cec5SDimitry Andric                           false);
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const char * func_name,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)4930b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(
4940b57cec5SDimitry Andric     const FileSpecList *containingModules,
4950b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles, const char *func_name,
4960b57cec5SDimitry Andric     FunctionNameType func_name_type_mask, LanguageType language,
4970b57cec5SDimitry Andric     lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) {
4980b57cec5SDimitry Andric   BreakpointSP bp_sp;
4990b57cec5SDimitry Andric   if (func_name) {
5000b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
5010b57cec5SDimitry Andric         containingModules, containingSourceFiles));
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
5040b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
5050b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
5060b57cec5SDimitry Andric       language = GetLanguage();
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
5090b57cec5SDimitry Andric         nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact,
5100b57cec5SDimitry Andric         offset, skip_prologue));
5110b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
5120b57cec5SDimitry Andric   }
5130b57cec5SDimitry Andric   return bp_sp;
5140b57cec5SDimitry Andric }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric lldb::BreakpointSP
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const std::vector<std::string> & func_names,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)5170b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
5180b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
5190b57cec5SDimitry Andric                          const std::vector<std::string> &func_names,
5200b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
5210b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
5220b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
5230b57cec5SDimitry Andric   BreakpointSP bp_sp;
5240b57cec5SDimitry Andric   size_t num_names = func_names.size();
5250b57cec5SDimitry Andric   if (num_names > 0) {
5260b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
5270b57cec5SDimitry Andric         containingModules, containingSourceFiles));
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
5300b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
5310b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
5320b57cec5SDimitry Andric       language = GetLanguage();
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(
5350b57cec5SDimitry Andric         new BreakpointResolverName(nullptr, func_names, func_name_type_mask,
5360b57cec5SDimitry Andric                                    language, offset, skip_prologue));
5370b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
5380b57cec5SDimitry Andric   }
5390b57cec5SDimitry Andric   return bp_sp;
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric BreakpointSP
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const char * func_names[],size_t num_names,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)5430b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
5440b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
5450b57cec5SDimitry Andric                          const char *func_names[], size_t num_names,
5460b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
5470b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
5480b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
5490b57cec5SDimitry Andric   BreakpointSP bp_sp;
5500b57cec5SDimitry Andric   if (num_names > 0) {
5510b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
5520b57cec5SDimitry Andric         containingModules, containingSourceFiles));
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate) {
5550b57cec5SDimitry Andric       if (offset == 0)
5560b57cec5SDimitry Andric         skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
5570b57cec5SDimitry Andric       else
5580b57cec5SDimitry Andric         skip_prologue = eLazyBoolNo;
5590b57cec5SDimitry Andric     }
5600b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
5610b57cec5SDimitry Andric       language = GetLanguage();
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
5640b57cec5SDimitry Andric         nullptr, func_names, num_names, func_name_type_mask, language, offset,
5650b57cec5SDimitry Andric         skip_prologue));
5660b57cec5SDimitry Andric     resolver_sp->SetOffset(offset);
5670b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
5680b57cec5SDimitry Andric   }
5690b57cec5SDimitry Andric   return bp_sp;
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric SearchFilterSP
GetSearchFilterForModule(const FileSpec * containingModule)5730b57cec5SDimitry Andric Target::GetSearchFilterForModule(const FileSpec *containingModule) {
5740b57cec5SDimitry Andric   SearchFilterSP filter_sp;
5750b57cec5SDimitry Andric   if (containingModule != nullptr) {
5760b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
5770b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
5780b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModule>(shared_from_this(),
5790b57cec5SDimitry Andric                                                        *containingModule);
5800b57cec5SDimitry Andric   } else {
5810b57cec5SDimitry Andric     if (!m_search_filter_sp)
5820b57cec5SDimitry Andric       m_search_filter_sp =
5830b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
5840b57cec5SDimitry Andric               shared_from_this());
5850b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
5860b57cec5SDimitry Andric   }
5870b57cec5SDimitry Andric   return filter_sp;
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric SearchFilterSP
GetSearchFilterForModuleList(const FileSpecList * containingModules)5910b57cec5SDimitry Andric Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) {
5920b57cec5SDimitry Andric   SearchFilterSP filter_sp;
5930b57cec5SDimitry Andric   if (containingModules && containingModules->GetSize() != 0) {
5940b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
5950b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
5960b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleList>(shared_from_this(),
5970b57cec5SDimitry Andric                                                            *containingModules);
5980b57cec5SDimitry Andric   } else {
5990b57cec5SDimitry Andric     if (!m_search_filter_sp)
6000b57cec5SDimitry Andric       m_search_filter_sp =
6010b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
6020b57cec5SDimitry Andric               shared_from_this());
6030b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
6040b57cec5SDimitry Andric   }
6050b57cec5SDimitry Andric   return filter_sp;
6060b57cec5SDimitry Andric }
6070b57cec5SDimitry Andric 
GetSearchFilterForModuleAndCUList(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles)6080b57cec5SDimitry Andric SearchFilterSP Target::GetSearchFilterForModuleAndCUList(
6090b57cec5SDimitry Andric     const FileSpecList *containingModules,
6100b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles) {
6110b57cec5SDimitry Andric   if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
6120b57cec5SDimitry Andric     return GetSearchFilterForModuleList(containingModules);
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   SearchFilterSP filter_sp;
6150b57cec5SDimitry Andric   if (containingModules == nullptr) {
6160b57cec5SDimitry Andric     // We could make a special "CU List only SearchFilter".  Better yet was if
6170b57cec5SDimitry Andric     // these could be composable, but that will take a little reworking.
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
6200b57cec5SDimitry Andric         shared_from_this(), FileSpecList(), *containingSourceFiles);
6210b57cec5SDimitry Andric   } else {
6220b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
6230b57cec5SDimitry Andric         shared_from_this(), *containingModules, *containingSourceFiles);
6240b57cec5SDimitry Andric   }
6250b57cec5SDimitry Andric   return filter_sp;
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric 
CreateFuncRegexBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,RegularExpression func_regex,lldb::LanguageType requested_language,LazyBool skip_prologue,bool internal,bool hardware)6280b57cec5SDimitry Andric BreakpointSP Target::CreateFuncRegexBreakpoint(
6290b57cec5SDimitry Andric     const FileSpecList *containingModules,
6309dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, RegularExpression func_regex,
6310b57cec5SDimitry Andric     lldb::LanguageType requested_language, LazyBool skip_prologue,
6320b57cec5SDimitry Andric     bool internal, bool hardware) {
6330b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
6340b57cec5SDimitry Andric       containingModules, containingSourceFiles));
6350b57cec5SDimitry Andric   bool skip = (skip_prologue == eLazyBoolCalculate)
6360b57cec5SDimitry Andric                   ? GetSkipPrologue()
6370b57cec5SDimitry Andric                   : static_cast<bool>(skip_prologue);
6380b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverName(
6399dba64beSDimitry Andric       nullptr, std::move(func_regex), requested_language, 0, skip));
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric lldb::BreakpointSP
CreateExceptionBreakpoint(enum lldb::LanguageType language,bool catch_bp,bool throw_bp,bool internal,Args * additional_args,Status * error)6450b57cec5SDimitry Andric Target::CreateExceptionBreakpoint(enum lldb::LanguageType language,
6460b57cec5SDimitry Andric                                   bool catch_bp, bool throw_bp, bool internal,
6470b57cec5SDimitry Andric                                   Args *additional_args, Status *error) {
6480b57cec5SDimitry Andric   BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint(
6490b57cec5SDimitry Andric       *this, language, catch_bp, throw_bp, internal);
6500b57cec5SDimitry Andric   if (exc_bkpt_sp && additional_args) {
6510b57cec5SDimitry Andric     BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
6520b57cec5SDimitry Andric     if (precondition_sp && additional_args) {
6530b57cec5SDimitry Andric       if (error)
6540b57cec5SDimitry Andric         *error = precondition_sp->ConfigurePrecondition(*additional_args);
6550b57cec5SDimitry Andric       else
6560b57cec5SDimitry Andric         precondition_sp->ConfigurePrecondition(*additional_args);
6570b57cec5SDimitry Andric     }
6580b57cec5SDimitry Andric   }
6590b57cec5SDimitry Andric   return exc_bkpt_sp;
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric 
CreateScriptedBreakpoint(const llvm::StringRef class_name,const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,bool internal,bool request_hardware,StructuredData::ObjectSP extra_args_sp,Status * creation_error)6629dba64beSDimitry Andric lldb::BreakpointSP Target::CreateScriptedBreakpoint(
6639dba64beSDimitry Andric     const llvm::StringRef class_name, const FileSpecList *containingModules,
6649dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, bool internal,
6659dba64beSDimitry Andric     bool request_hardware, StructuredData::ObjectSP extra_args_sp,
6669dba64beSDimitry Andric     Status *creation_error) {
6670b57cec5SDimitry Andric   SearchFilterSP filter_sp;
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthTarget;
6709dba64beSDimitry Andric   bool has_files =
6719dba64beSDimitry Andric       containingSourceFiles && containingSourceFiles->GetSize() > 0;
6720b57cec5SDimitry Andric   bool has_modules = containingModules && containingModules->GetSize() > 0;
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   if (has_files && has_modules) {
6759dba64beSDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
6769dba64beSDimitry Andric                                                   containingSourceFiles);
6770b57cec5SDimitry Andric   } else if (has_files) {
6789dba64beSDimitry Andric     filter_sp =
6799dba64beSDimitry Andric         GetSearchFilterForModuleAndCUList(nullptr, containingSourceFiles);
6800b57cec5SDimitry Andric   } else if (has_modules) {
6810b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
6820b57cec5SDimitry Andric   } else {
6830b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterForUnconstrainedSearches>(
6840b57cec5SDimitry Andric         shared_from_this());
6850b57cec5SDimitry Andric   }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverScripted(
6880eae32dcSDimitry Andric       nullptr, class_name, depth, StructuredDataImpl(extra_args_sp)));
6890b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
6900b57cec5SDimitry Andric }
6910b57cec5SDimitry Andric 
CreateBreakpoint(SearchFilterSP & filter_sp,BreakpointResolverSP & resolver_sp,bool internal,bool request_hardware,bool resolve_indirect_symbols)6920b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp,
6930b57cec5SDimitry Andric                                       BreakpointResolverSP &resolver_sp,
6940b57cec5SDimitry Andric                                       bool internal, bool request_hardware,
6950b57cec5SDimitry Andric                                       bool resolve_indirect_symbols) {
6960b57cec5SDimitry Andric   BreakpointSP bp_sp;
6970b57cec5SDimitry Andric   if (filter_sp && resolver_sp) {
6980b57cec5SDimitry Andric     const bool hardware = request_hardware || GetRequireHardwareBreakpoints();
6990b57cec5SDimitry Andric     bp_sp.reset(new Breakpoint(*this, filter_sp, resolver_sp, hardware,
7000b57cec5SDimitry Andric                                resolve_indirect_symbols));
7015ffd83dbSDimitry Andric     resolver_sp->SetBreakpoint(bp_sp);
7020b57cec5SDimitry Andric     AddBreakpoint(bp_sp, internal);
7030b57cec5SDimitry Andric   }
7040b57cec5SDimitry Andric   return bp_sp;
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric 
AddBreakpoint(lldb::BreakpointSP bp_sp,bool internal)7070b57cec5SDimitry Andric void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) {
7080b57cec5SDimitry Andric   if (!bp_sp)
7090b57cec5SDimitry Andric     return;
7100b57cec5SDimitry Andric   if (internal)
7110b57cec5SDimitry Andric     m_internal_breakpoint_list.Add(bp_sp, false);
7120b57cec5SDimitry Andric   else
7130b57cec5SDimitry Andric     m_breakpoint_list.Add(bp_sp, true);
7140b57cec5SDimitry Andric 
71581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
7160b57cec5SDimitry Andric   if (log) {
7170b57cec5SDimitry Andric     StreamString s;
7180b57cec5SDimitry Andric     bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
7199dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s (internal = %s) => break_id = %s\n",
7209dba64beSDimitry Andric               __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
7210b57cec5SDimitry Andric   }
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   bp_sp->ResolveBreakpoint();
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric   if (!internal) {
7260b57cec5SDimitry Andric     m_last_created_breakpoint = bp_sp;
7270b57cec5SDimitry Andric   }
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric 
AddNameToBreakpoint(BreakpointID & id,llvm::StringRef name,Status & error)730c9157d92SDimitry Andric void Target::AddNameToBreakpoint(BreakpointID &id, llvm::StringRef name,
7319dba64beSDimitry Andric                                  Status &error) {
7329dba64beSDimitry Andric   BreakpointSP bp_sp =
7339dba64beSDimitry Andric       m_breakpoint_list.FindBreakpointByID(id.GetBreakpointID());
7349dba64beSDimitry Andric   if (!bp_sp) {
7350b57cec5SDimitry Andric     StreamString s;
7360b57cec5SDimitry Andric     id.GetDescription(&s, eDescriptionLevelBrief);
7379dba64beSDimitry Andric     error.SetErrorStringWithFormat("Could not find breakpoint %s", s.GetData());
7380b57cec5SDimitry Andric     return;
7390b57cec5SDimitry Andric   }
7400b57cec5SDimitry Andric   AddNameToBreakpoint(bp_sp, name, error);
7410b57cec5SDimitry Andric }
7420b57cec5SDimitry Andric 
AddNameToBreakpoint(BreakpointSP & bp_sp,llvm::StringRef name,Status & error)743c9157d92SDimitry Andric void Target::AddNameToBreakpoint(BreakpointSP &bp_sp, llvm::StringRef name,
7449dba64beSDimitry Andric                                  Status &error) {
7450b57cec5SDimitry Andric   if (!bp_sp)
7460b57cec5SDimitry Andric     return;
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric   BreakpointName *bp_name = FindBreakpointName(ConstString(name), true, error);
7490b57cec5SDimitry Andric   if (!bp_name)
7500b57cec5SDimitry Andric     return;
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric   bp_name->ConfigureBreakpoint(bp_sp);
7530b57cec5SDimitry Andric   bp_sp->AddName(name);
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
AddBreakpointName(std::unique_ptr<BreakpointName> bp_name)756bdd1243dSDimitry Andric void Target::AddBreakpointName(std::unique_ptr<BreakpointName> bp_name) {
757bdd1243dSDimitry Andric   m_breakpoint_names.insert(
758bdd1243dSDimitry Andric       std::make_pair(bp_name->GetName(), std::move(bp_name)));
7590b57cec5SDimitry Andric }
7600b57cec5SDimitry Andric 
FindBreakpointName(ConstString name,bool can_create,Status & error)7619dba64beSDimitry Andric BreakpointName *Target::FindBreakpointName(ConstString name, bool can_create,
7629dba64beSDimitry Andric                                            Status &error) {
7630b57cec5SDimitry Andric   BreakpointID::StringIsBreakpointName(name.GetStringRef(), error);
7640b57cec5SDimitry Andric   if (!error.Success())
7650b57cec5SDimitry Andric     return nullptr;
7660b57cec5SDimitry Andric 
7670b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
768bdd1243dSDimitry Andric   if (iter != m_breakpoint_names.end()) {
769bdd1243dSDimitry Andric     return iter->second.get();
770bdd1243dSDimitry Andric   }
771bdd1243dSDimitry Andric 
7729dba64beSDimitry Andric   if (!can_create) {
7730b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Breakpoint name \"%s\" doesn't exist and "
7749dba64beSDimitry Andric                                    "can_create is false.",
7759dba64beSDimitry Andric                                    name.AsCString());
7760b57cec5SDimitry Andric     return nullptr;
7770b57cec5SDimitry Andric   }
7780b57cec5SDimitry Andric 
779bdd1243dSDimitry Andric   return m_breakpoint_names
780bdd1243dSDimitry Andric       .insert(std::make_pair(name, std::make_unique<BreakpointName>(name)))
781bdd1243dSDimitry Andric       .first->second.get();
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric 
DeleteBreakpointName(ConstString name)7849dba64beSDimitry Andric void Target::DeleteBreakpointName(ConstString name) {
7850b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric   if (iter != m_breakpoint_names.end()) {
7880b57cec5SDimitry Andric     const char *name_cstr = name.AsCString();
7890b57cec5SDimitry Andric     m_breakpoint_names.erase(iter);
7900b57cec5SDimitry Andric     for (auto bp_sp : m_breakpoint_list.Breakpoints())
7910b57cec5SDimitry Andric       bp_sp->RemoveName(name_cstr);
7920b57cec5SDimitry Andric   }
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric 
RemoveNameFromBreakpoint(lldb::BreakpointSP & bp_sp,ConstString name)7950b57cec5SDimitry Andric void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp,
7969dba64beSDimitry Andric                                       ConstString name) {
7970b57cec5SDimitry Andric   bp_sp->RemoveName(name.AsCString());
7980b57cec5SDimitry Andric }
7990b57cec5SDimitry Andric 
ConfigureBreakpointName(BreakpointName & bp_name,const BreakpointOptions & new_options,const BreakpointName::Permissions & new_permissions)8009dba64beSDimitry Andric void Target::ConfigureBreakpointName(
8019dba64beSDimitry Andric     BreakpointName &bp_name, const BreakpointOptions &new_options,
8029dba64beSDimitry Andric     const BreakpointName::Permissions &new_permissions) {
8030b57cec5SDimitry Andric   bp_name.GetOptions().CopyOverSetOptions(new_options);
8040b57cec5SDimitry Andric   bp_name.GetPermissions().MergeInto(new_permissions);
8050b57cec5SDimitry Andric   ApplyNameToBreakpoints(bp_name);
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric 
ApplyNameToBreakpoints(BreakpointName & bp_name)8080b57cec5SDimitry Andric void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) {
809480093f4SDimitry Andric   llvm::Expected<std::vector<BreakpointSP>> expected_vector =
810480093f4SDimitry Andric       m_breakpoint_list.FindBreakpointsByName(bp_name.GetName().AsCString());
8110b57cec5SDimitry Andric 
812480093f4SDimitry Andric   if (!expected_vector) {
81381ad6265SDimitry Andric     LLDB_LOG(GetLog(LLDBLog::Breakpoints), "invalid breakpoint name: {}",
814480093f4SDimitry Andric              llvm::toString(expected_vector.takeError()));
815480093f4SDimitry Andric     return;
816480093f4SDimitry Andric   }
817480093f4SDimitry Andric 
818480093f4SDimitry Andric   for (auto bp_sp : *expected_vector)
8190b57cec5SDimitry Andric     bp_name.ConfigureBreakpoint(bp_sp);
8200b57cec5SDimitry Andric }
8210b57cec5SDimitry Andric 
GetBreakpointNames(std::vector<std::string> & names)8229dba64beSDimitry Andric void Target::GetBreakpointNames(std::vector<std::string> &names) {
8230b57cec5SDimitry Andric   names.clear();
824bdd1243dSDimitry Andric   for (const auto& bp_name_entry : m_breakpoint_names) {
825bdd1243dSDimitry Andric     names.push_back(bp_name_entry.first.AsCString());
8260b57cec5SDimitry Andric   }
827fcaf7f86SDimitry Andric   llvm::sort(names);
8280b57cec5SDimitry Andric }
8290b57cec5SDimitry Andric 
ProcessIsValid()8300b57cec5SDimitry Andric bool Target::ProcessIsValid() {
8310b57cec5SDimitry Andric   return (m_process_sp && m_process_sp->IsAlive());
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric 
CheckIfWatchpointsSupported(Target * target,Status & error)8340b57cec5SDimitry Andric static bool CheckIfWatchpointsSupported(Target *target, Status &error) {
835fe013be4SDimitry Andric   std::optional<uint32_t> num_supported_hardware_watchpoints =
836fe013be4SDimitry Andric       target->GetProcessSP()->GetWatchpointSlotCount();
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric   // If unable to determine the # of watchpoints available,
8390b57cec5SDimitry Andric   // assume they are supported.
840fe013be4SDimitry Andric   if (!num_supported_hardware_watchpoints)
8410b57cec5SDimitry Andric     return true;
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric   if (num_supported_hardware_watchpoints == 0) {
8440b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
8450b57cec5SDimitry Andric         "Target supports (%u) hardware watchpoint slots.\n",
846fe013be4SDimitry Andric         *num_supported_hardware_watchpoints);
8470b57cec5SDimitry Andric     return false;
8480b57cec5SDimitry Andric   }
8490b57cec5SDimitry Andric   return true;
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric // See also Watchpoint::SetWatchpointType(uint32_t type) and the
8530b57cec5SDimitry Andric // OptionGroupWatchpoint::WatchType enum type.
CreateWatchpoint(lldb::addr_t addr,size_t size,const CompilerType * type,uint32_t kind,Status & error)8540b57cec5SDimitry Andric WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size,
8550b57cec5SDimitry Andric                                       const CompilerType *type, uint32_t kind,
8560b57cec5SDimitry Andric                                       Status &error) {
85781ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
8589dba64beSDimitry Andric   LLDB_LOGF(log,
8599dba64beSDimitry Andric             "Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64
8600b57cec5SDimitry Andric             " type = %u)\n",
8610b57cec5SDimitry Andric             __FUNCTION__, addr, (uint64_t)size, kind);
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   WatchpointSP wp_sp;
8640b57cec5SDimitry Andric   if (!ProcessIsValid()) {
8650b57cec5SDimitry Andric     error.SetErrorString("process is not alive");
8660b57cec5SDimitry Andric     return wp_sp;
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   if (addr == LLDB_INVALID_ADDRESS || size == 0) {
8700b57cec5SDimitry Andric     if (size == 0)
8710b57cec5SDimitry Andric       error.SetErrorString("cannot set a watchpoint with watch_size of 0");
8720b57cec5SDimitry Andric     else
8730b57cec5SDimitry Andric       error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
8740b57cec5SDimitry Andric     return wp_sp;
8750b57cec5SDimitry Andric   }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   if (!LLDB_WATCH_TYPE_IS_VALID(kind)) {
8780b57cec5SDimitry Andric     error.SetErrorStringWithFormat("invalid watchpoint type: %d", kind);
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   if (!CheckIfWatchpointsSupported(this, error))
8820b57cec5SDimitry Andric     return wp_sp;
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   // Currently we only support one watchpoint per address, with total number of
8850b57cec5SDimitry Andric   // watchpoints limited by the hardware which the inferior is running on.
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   // Grab the list mutex while doing operations.
8880b57cec5SDimitry Andric   const bool notify = false; // Don't notify about all the state changes we do
8890b57cec5SDimitry Andric                              // on creating the watchpoint.
890fe6060f1SDimitry Andric 
891fe6060f1SDimitry Andric   // Mask off ignored bits from watchpoint address.
892fe6060f1SDimitry Andric   if (ABISP abi = m_process_sp->GetABI())
893fe6060f1SDimitry Andric     addr = abi->FixDataAddress(addr);
894fe6060f1SDimitry Andric 
895c9157d92SDimitry Andric   // LWP_TODO this sequence is looking for an existing watchpoint
896c9157d92SDimitry Andric   // at the exact same user-specified address, disables the new one
897c9157d92SDimitry Andric   // if addr/size/type match.  If type/size differ, disable old one.
898c9157d92SDimitry Andric   // This isn't correct, we need both watchpoints to use a shared
899c9157d92SDimitry Andric   // WatchpointResource in the target, and expand the WatchpointResource
900c9157d92SDimitry Andric   // to handle the needs of both Watchpoints.
901c9157d92SDimitry Andric   // Also, even if the addresses don't match, they may need to be
902c9157d92SDimitry Andric   // supported by the same WatchpointResource, e.g. a watchpoint
903c9157d92SDimitry Andric   // watching 1 byte at 0x102 and a watchpoint watching 1 byte at 0x103.
904c9157d92SDimitry Andric   // They're in the same word and must be watched by a single hardware
905c9157d92SDimitry Andric   // watchpoint register.
906c9157d92SDimitry Andric 
9070b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
9080b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
9090b57cec5SDimitry Andric   WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
9100b57cec5SDimitry Andric   if (matched_sp) {
9110b57cec5SDimitry Andric     size_t old_size = matched_sp->GetByteSize();
9120b57cec5SDimitry Andric     uint32_t old_type =
9130b57cec5SDimitry Andric         (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
914c9157d92SDimitry Andric         (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0) |
915c9157d92SDimitry Andric         (matched_sp->WatchpointModify() ? LLDB_WATCH_TYPE_MODIFY : 0);
9160b57cec5SDimitry Andric     // Return the existing watchpoint if both size and type match.
9170b57cec5SDimitry Andric     if (size == old_size && kind == old_type) {
9180b57cec5SDimitry Andric       wp_sp = matched_sp;
9190b57cec5SDimitry Andric       wp_sp->SetEnabled(false, notify);
9200b57cec5SDimitry Andric     } else {
9210b57cec5SDimitry Andric       // Nil the matched watchpoint; we will be creating a new one.
922c9157d92SDimitry Andric       m_process_sp->DisableWatchpoint(matched_sp, notify);
9230b57cec5SDimitry Andric       m_watchpoint_list.Remove(matched_sp->GetID(), true);
9240b57cec5SDimitry Andric     }
9250b57cec5SDimitry Andric   }
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   if (!wp_sp) {
9280b57cec5SDimitry Andric     wp_sp = std::make_shared<Watchpoint>(*this, addr, size, type);
9290b57cec5SDimitry Andric     wp_sp->SetWatchpointType(kind, notify);
9300b57cec5SDimitry Andric     m_watchpoint_list.Add(wp_sp, true);
9310b57cec5SDimitry Andric   }
9320b57cec5SDimitry Andric 
933c9157d92SDimitry Andric   error = m_process_sp->EnableWatchpoint(wp_sp, notify);
9349dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (creation of watchpoint %s with id = %u)\n",
9350b57cec5SDimitry Andric             __FUNCTION__, error.Success() ? "succeeded" : "failed",
9360b57cec5SDimitry Andric             wp_sp->GetID());
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   if (error.Fail()) {
9390b57cec5SDimitry Andric     // Enabling the watchpoint on the device side failed. Remove the said
9400b57cec5SDimitry Andric     // watchpoint from the list maintained by the target instance.
9410b57cec5SDimitry Andric     m_watchpoint_list.Remove(wp_sp->GetID(), true);
9420b57cec5SDimitry Andric     wp_sp.reset();
9430b57cec5SDimitry Andric   } else
9440b57cec5SDimitry Andric     m_last_created_watchpoint = wp_sp;
9450b57cec5SDimitry Andric   return wp_sp;
9460b57cec5SDimitry Andric }
9470b57cec5SDimitry Andric 
RemoveAllowedBreakpoints()9489dba64beSDimitry Andric void Target::RemoveAllowedBreakpoints() {
94981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9509dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s \n", __FUNCTION__);
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   m_breakpoint_list.RemoveAllowed(true);
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric 
RemoveAllBreakpoints(bool internal_also)9570b57cec5SDimitry Andric void Target::RemoveAllBreakpoints(bool internal_also) {
95881ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9599dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
9600b57cec5SDimitry Andric             internal_also ? "yes" : "no");
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(true);
9630b57cec5SDimitry Andric   if (internal_also)
9640b57cec5SDimitry Andric     m_internal_breakpoint_list.RemoveAll(false);
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
9670b57cec5SDimitry Andric }
9680b57cec5SDimitry Andric 
DisableAllBreakpoints(bool internal_also)9690b57cec5SDimitry Andric void Target::DisableAllBreakpoints(bool internal_also) {
97081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9719dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
9720b57cec5SDimitry Andric             internal_also ? "yes" : "no");
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(false);
9750b57cec5SDimitry Andric   if (internal_also)
9760b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(false);
9770b57cec5SDimitry Andric }
9780b57cec5SDimitry Andric 
DisableAllowedBreakpoints()9790b57cec5SDimitry Andric void Target::DisableAllowedBreakpoints() {
98081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9819dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(false);
9840b57cec5SDimitry Andric }
9850b57cec5SDimitry Andric 
EnableAllBreakpoints(bool internal_also)9860b57cec5SDimitry Andric void Target::EnableAllBreakpoints(bool internal_also) {
98781ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9889dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
9890b57cec5SDimitry Andric             internal_also ? "yes" : "no");
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(true);
9920b57cec5SDimitry Andric   if (internal_also)
9930b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(true);
9940b57cec5SDimitry Andric }
9950b57cec5SDimitry Andric 
EnableAllowedBreakpoints()9960b57cec5SDimitry Andric void Target::EnableAllowedBreakpoints() {
99781ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
9989dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(true);
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric 
RemoveBreakpointByID(break_id_t break_id)10030b57cec5SDimitry Andric bool Target::RemoveBreakpointByID(break_id_t break_id) {
100481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
10059dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
10060b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   if (DisableBreakpointByID(break_id)) {
10090b57cec5SDimitry Andric     if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
10100b57cec5SDimitry Andric       m_internal_breakpoint_list.Remove(break_id, false);
10110b57cec5SDimitry Andric     else {
10120b57cec5SDimitry Andric       if (m_last_created_breakpoint) {
10130b57cec5SDimitry Andric         if (m_last_created_breakpoint->GetID() == break_id)
10140b57cec5SDimitry Andric           m_last_created_breakpoint.reset();
10150b57cec5SDimitry Andric       }
10160b57cec5SDimitry Andric       m_breakpoint_list.Remove(break_id, true);
10170b57cec5SDimitry Andric     }
10180b57cec5SDimitry Andric     return true;
10190b57cec5SDimitry Andric   }
10200b57cec5SDimitry Andric   return false;
10210b57cec5SDimitry Andric }
10220b57cec5SDimitry Andric 
DisableBreakpointByID(break_id_t break_id)10230b57cec5SDimitry Andric bool Target::DisableBreakpointByID(break_id_t break_id) {
102481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
10259dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
10260b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric   BreakpointSP bp_sp;
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
10310b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
10320b57cec5SDimitry Andric   else
10330b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
10340b57cec5SDimitry Andric   if (bp_sp) {
10350b57cec5SDimitry Andric     bp_sp->SetEnabled(false);
10360b57cec5SDimitry Andric     return true;
10370b57cec5SDimitry Andric   }
10380b57cec5SDimitry Andric   return false;
10390b57cec5SDimitry Andric }
10400b57cec5SDimitry Andric 
EnableBreakpointByID(break_id_t break_id)10410b57cec5SDimitry Andric bool Target::EnableBreakpointByID(break_id_t break_id) {
104281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Breakpoints);
10439dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
10440b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric   BreakpointSP bp_sp;
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
10490b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
10500b57cec5SDimitry Andric   else
10510b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric   if (bp_sp) {
10540b57cec5SDimitry Andric     bp_sp->SetEnabled(true);
10550b57cec5SDimitry Andric     return true;
10560b57cec5SDimitry Andric   }
10570b57cec5SDimitry Andric   return false;
10580b57cec5SDimitry Andric }
10590b57cec5SDimitry Andric 
ResetBreakpointHitCounts()1060bdd1243dSDimitry Andric void Target::ResetBreakpointHitCounts() {
1061bdd1243dSDimitry Andric   GetBreakpointList().ResetHitCounts();
1062bdd1243dSDimitry Andric }
1063bdd1243dSDimitry Andric 
SerializeBreakpointsToFile(const FileSpec & file,const BreakpointIDList & bp_ids,bool append)10640b57cec5SDimitry Andric Status Target::SerializeBreakpointsToFile(const FileSpec &file,
10650b57cec5SDimitry Andric                                           const BreakpointIDList &bp_ids,
10660b57cec5SDimitry Andric                                           bool append) {
10670b57cec5SDimitry Andric   Status error;
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   if (!file) {
10700b57cec5SDimitry Andric     error.SetErrorString("Invalid FileSpec.");
10710b57cec5SDimitry Andric     return error;
10720b57cec5SDimitry Andric   }
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric   std::string path(file.GetPath());
10750b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp;
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric   StructuredData::ArraySP break_store_sp;
10780b57cec5SDimitry Andric   StructuredData::Array *break_store_ptr = nullptr;
10790b57cec5SDimitry Andric 
10800b57cec5SDimitry Andric   if (append) {
10810b57cec5SDimitry Andric     input_data_sp = StructuredData::ParseJSONFromFile(file, error);
10820b57cec5SDimitry Andric     if (error.Success()) {
10830b57cec5SDimitry Andric       break_store_ptr = input_data_sp->GetAsArray();
10840b57cec5SDimitry Andric       if (!break_store_ptr) {
10850b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
10860b57cec5SDimitry Andric             "Tried to append to invalid input file %s", path.c_str());
10870b57cec5SDimitry Andric         return error;
10880b57cec5SDimitry Andric       }
10890b57cec5SDimitry Andric     }
10900b57cec5SDimitry Andric   }
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric   if (!break_store_ptr) {
10930b57cec5SDimitry Andric     break_store_sp = std::make_shared<StructuredData::Array>();
10940b57cec5SDimitry Andric     break_store_ptr = break_store_sp.get();
10950b57cec5SDimitry Andric   }
10960b57cec5SDimitry Andric 
10970b57cec5SDimitry Andric   StreamFile out_file(path.c_str(),
1098349cc55cSDimitry Andric                       File::eOpenOptionTruncate | File::eOpenOptionWriteOnly |
10999dba64beSDimitry Andric                           File::eOpenOptionCanCreate |
11009dba64beSDimitry Andric                           File::eOpenOptionCloseOnExec,
11010b57cec5SDimitry Andric                       lldb::eFilePermissionsFileDefault);
11020b57cec5SDimitry Andric   if (!out_file.GetFile().IsValid()) {
11030b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Unable to open output file: %s.",
11040b57cec5SDimitry Andric                                    path.c_str());
11050b57cec5SDimitry Andric     return error;
11060b57cec5SDimitry Andric   }
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
11090b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric   if (bp_ids.GetSize() == 0) {
11120b57cec5SDimitry Andric     const BreakpointList &breakpoints = GetBreakpointList();
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric     size_t num_breakpoints = breakpoints.GetSize();
11150b57cec5SDimitry Andric     for (size_t i = 0; i < num_breakpoints; i++) {
11160b57cec5SDimitry Andric       Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get();
11170b57cec5SDimitry Andric       StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
11180b57cec5SDimitry Andric       // If a breakpoint can't serialize it, just ignore it for now:
11190b57cec5SDimitry Andric       if (bkpt_save_sp)
11200b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
11210b57cec5SDimitry Andric     }
11220b57cec5SDimitry Andric   } else {
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric     std::unordered_set<lldb::break_id_t> processed_bkpts;
11250b57cec5SDimitry Andric     const size_t count = bp_ids.GetSize();
11260b57cec5SDimitry Andric     for (size_t i = 0; i < count; ++i) {
11270b57cec5SDimitry Andric       BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(i);
11280b57cec5SDimitry Andric       lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID();
11290b57cec5SDimitry Andric 
11300b57cec5SDimitry Andric       if (bp_id != LLDB_INVALID_BREAK_ID) {
11310b57cec5SDimitry Andric         // Only do each breakpoint once:
11320b57cec5SDimitry Andric         std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool>
11330b57cec5SDimitry Andric             insert_result = processed_bkpts.insert(bp_id);
11340b57cec5SDimitry Andric         if (!insert_result.second)
11350b57cec5SDimitry Andric           continue;
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric         Breakpoint *bp = GetBreakpointByID(bp_id).get();
11380b57cec5SDimitry Andric         StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
11390b57cec5SDimitry Andric         // If the user explicitly asked to serialize a breakpoint, and we
11400b57cec5SDimitry Andric         // can't, then raise an error:
11410b57cec5SDimitry Andric         if (!bkpt_save_sp) {
11420b57cec5SDimitry Andric           error.SetErrorStringWithFormat("Unable to serialize breakpoint %d",
11430b57cec5SDimitry Andric                                          bp_id);
11440b57cec5SDimitry Andric           return error;
11450b57cec5SDimitry Andric         }
11460b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
11470b57cec5SDimitry Andric       }
11480b57cec5SDimitry Andric     }
11490b57cec5SDimitry Andric   }
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric   break_store_ptr->Dump(out_file, false);
11520b57cec5SDimitry Andric   out_file.PutChar('\n');
11530b57cec5SDimitry Andric   return error;
11540b57cec5SDimitry Andric }
11550b57cec5SDimitry Andric 
CreateBreakpointsFromFile(const FileSpec & file,BreakpointIDList & new_bps)11560b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
11570b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
11580b57cec5SDimitry Andric   std::vector<std::string> no_names;
11590b57cec5SDimitry Andric   return CreateBreakpointsFromFile(file, no_names, new_bps);
11600b57cec5SDimitry Andric }
11610b57cec5SDimitry Andric 
CreateBreakpointsFromFile(const FileSpec & file,std::vector<std::string> & names,BreakpointIDList & new_bps)11620b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
11630b57cec5SDimitry Andric                                          std::vector<std::string> &names,
11640b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
11650b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
11660b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
11670b57cec5SDimitry Andric 
11680b57cec5SDimitry Andric   Status error;
11690b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp =
11700b57cec5SDimitry Andric       StructuredData::ParseJSONFromFile(file, error);
11710b57cec5SDimitry Andric   if (!error.Success()) {
11720b57cec5SDimitry Andric     return error;
11730b57cec5SDimitry Andric   } else if (!input_data_sp || !input_data_sp->IsValid()) {
11740b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Invalid JSON from input file: %s.",
11750b57cec5SDimitry Andric                                    file.GetPath().c_str());
11760b57cec5SDimitry Andric     return error;
11770b57cec5SDimitry Andric   }
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric   StructuredData::Array *bkpt_array = input_data_sp->GetAsArray();
11800b57cec5SDimitry Andric   if (!bkpt_array) {
11810b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
11820b57cec5SDimitry Andric         "Invalid breakpoint data from input file: %s.", file.GetPath().c_str());
11830b57cec5SDimitry Andric     return error;
11840b57cec5SDimitry Andric   }
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric   size_t num_bkpts = bkpt_array->GetSize();
11870b57cec5SDimitry Andric   size_t num_names = names.size();
11880b57cec5SDimitry Andric 
11890b57cec5SDimitry Andric   for (size_t i = 0; i < num_bkpts; i++) {
11900b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(i);
11910b57cec5SDimitry Andric     // Peel off the breakpoint key, and feed the rest to the Breakpoint:
11920b57cec5SDimitry Andric     StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary();
11930b57cec5SDimitry Andric     if (!bkpt_dict) {
11940b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
11950b57cec5SDimitry Andric           "Invalid breakpoint data for element %zu from input file: %s.", i,
11960b57cec5SDimitry Andric           file.GetPath().c_str());
11970b57cec5SDimitry Andric       return error;
11980b57cec5SDimitry Andric     }
11990b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_data_sp =
12000b57cec5SDimitry Andric         bkpt_dict->GetValueForKey(Breakpoint::GetSerializationKey());
12010b57cec5SDimitry Andric     if (num_names &&
12020b57cec5SDimitry Andric         !Breakpoint::SerializedBreakpointMatchesNames(bkpt_data_sp, names))
12030b57cec5SDimitry Andric       continue;
12040b57cec5SDimitry Andric 
12055ffd83dbSDimitry Andric     BreakpointSP bkpt_sp = Breakpoint::CreateFromStructuredData(
12065ffd83dbSDimitry Andric         shared_from_this(), bkpt_data_sp, error);
12070b57cec5SDimitry Andric     if (!error.Success()) {
12080b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
12090b57cec5SDimitry Andric           "Error restoring breakpoint %zu from %s: %s.", i,
12100b57cec5SDimitry Andric           file.GetPath().c_str(), error.AsCString());
12110b57cec5SDimitry Andric       return error;
12120b57cec5SDimitry Andric     }
12130b57cec5SDimitry Andric     new_bps.AddBreakpointID(BreakpointID(bkpt_sp->GetID()));
12140b57cec5SDimitry Andric   }
12150b57cec5SDimitry Andric   return error;
12160b57cec5SDimitry Andric }
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric // The flag 'end_to_end', default to true, signifies that the operation is
12190b57cec5SDimitry Andric // performed end to end, for both the debugger and the debuggee.
12200b57cec5SDimitry Andric 
12210b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
12220b57cec5SDimitry Andric // to end operations.
RemoveAllWatchpoints(bool end_to_end)12230b57cec5SDimitry Andric bool Target::RemoveAllWatchpoints(bool end_to_end) {
122481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
12259dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12260b57cec5SDimitry Andric 
12270b57cec5SDimitry Andric   if (!end_to_end) {
12280b57cec5SDimitry Andric     m_watchpoint_list.RemoveAll(true);
12290b57cec5SDimitry Andric     return true;
12300b57cec5SDimitry Andric   }
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric   if (!ProcessIsValid())
12350b57cec5SDimitry Andric     return false;
12360b57cec5SDimitry Andric 
1237fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
12380b57cec5SDimitry Andric     if (!wp_sp)
12390b57cec5SDimitry Andric       return false;
12400b57cec5SDimitry Andric 
1241c9157d92SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp);
12420b57cec5SDimitry Andric     if (rc.Fail())
12430b57cec5SDimitry Andric       return false;
12440b57cec5SDimitry Andric   }
12450b57cec5SDimitry Andric   m_watchpoint_list.RemoveAll(true);
12460b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
12470b57cec5SDimitry Andric   return true; // Success!
12480b57cec5SDimitry Andric }
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
12510b57cec5SDimitry Andric // to end operations.
DisableAllWatchpoints(bool end_to_end)12520b57cec5SDimitry Andric bool Target::DisableAllWatchpoints(bool end_to_end) {
125381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
12549dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric   if (!end_to_end) {
12570b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(false);
12580b57cec5SDimitry Andric     return true;
12590b57cec5SDimitry Andric   }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry Andric   if (!ProcessIsValid())
12640b57cec5SDimitry Andric     return false;
12650b57cec5SDimitry Andric 
1266fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
12670b57cec5SDimitry Andric     if (!wp_sp)
12680b57cec5SDimitry Andric       return false;
12690b57cec5SDimitry Andric 
1270c9157d92SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp);
12710b57cec5SDimitry Andric     if (rc.Fail())
12720b57cec5SDimitry Andric       return false;
12730b57cec5SDimitry Andric   }
12740b57cec5SDimitry Andric   return true; // Success!
12750b57cec5SDimitry Andric }
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
12780b57cec5SDimitry Andric // to end operations.
EnableAllWatchpoints(bool end_to_end)12790b57cec5SDimitry Andric bool Target::EnableAllWatchpoints(bool end_to_end) {
128081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
12819dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric   if (!end_to_end) {
12840b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(true);
12850b57cec5SDimitry Andric     return true;
12860b57cec5SDimitry Andric   }
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
12890b57cec5SDimitry Andric 
12900b57cec5SDimitry Andric   if (!ProcessIsValid())
12910b57cec5SDimitry Andric     return false;
12920b57cec5SDimitry Andric 
1293fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
12940b57cec5SDimitry Andric     if (!wp_sp)
12950b57cec5SDimitry Andric       return false;
12960b57cec5SDimitry Andric 
1297c9157d92SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp);
12980b57cec5SDimitry Andric     if (rc.Fail())
12990b57cec5SDimitry Andric       return false;
13000b57cec5SDimitry Andric   }
13010b57cec5SDimitry Andric   return true; // Success!
13020b57cec5SDimitry Andric }
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
ClearAllWatchpointHitCounts()13050b57cec5SDimitry Andric bool Target::ClearAllWatchpointHitCounts() {
130681ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13079dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
13080b57cec5SDimitry Andric 
1309fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
13100b57cec5SDimitry Andric     if (!wp_sp)
13110b57cec5SDimitry Andric       return false;
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric     wp_sp->ResetHitCount();
13140b57cec5SDimitry Andric   }
13150b57cec5SDimitry Andric   return true; // Success!
13160b57cec5SDimitry Andric }
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
ClearAllWatchpointHistoricValues()13190b57cec5SDimitry Andric bool Target::ClearAllWatchpointHistoricValues() {
132081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13219dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
13220b57cec5SDimitry Andric 
1323fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
13240b57cec5SDimitry Andric     if (!wp_sp)
13250b57cec5SDimitry Andric       return false;
13260b57cec5SDimitry Andric 
13270b57cec5SDimitry Andric     wp_sp->ResetHistoricValues();
13280b57cec5SDimitry Andric   }
13290b57cec5SDimitry Andric   return true; // Success!
13300b57cec5SDimitry Andric }
13310b57cec5SDimitry Andric 
13320b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list during
13330b57cec5SDimitry Andric // these operations.
IgnoreAllWatchpoints(uint32_t ignore_count)13340b57cec5SDimitry Andric bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) {
133581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13369dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
13370b57cec5SDimitry Andric 
13380b57cec5SDimitry Andric   if (!ProcessIsValid())
13390b57cec5SDimitry Andric     return false;
13400b57cec5SDimitry Andric 
1341fe6060f1SDimitry Andric   for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
13420b57cec5SDimitry Andric     if (!wp_sp)
13430b57cec5SDimitry Andric       return false;
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
13460b57cec5SDimitry Andric   }
13470b57cec5SDimitry Andric   return true; // Success!
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
DisableWatchpointByID(lldb::watch_id_t watch_id)13510b57cec5SDimitry Andric bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) {
135281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13539dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric   if (!ProcessIsValid())
13560b57cec5SDimitry Andric     return false;
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
13590b57cec5SDimitry Andric   if (wp_sp) {
1360c9157d92SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp);
13610b57cec5SDimitry Andric     if (rc.Success())
13620b57cec5SDimitry Andric       return true;
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric     // Else, fallthrough.
13650b57cec5SDimitry Andric   }
13660b57cec5SDimitry Andric   return false;
13670b57cec5SDimitry Andric }
13680b57cec5SDimitry Andric 
13690b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
EnableWatchpointByID(lldb::watch_id_t watch_id)13700b57cec5SDimitry Andric bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) {
137181ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13729dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric   if (!ProcessIsValid())
13750b57cec5SDimitry Andric     return false;
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
13780b57cec5SDimitry Andric   if (wp_sp) {
1379c9157d92SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp);
13800b57cec5SDimitry Andric     if (rc.Success())
13810b57cec5SDimitry Andric       return true;
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric     // Else, fallthrough.
13840b57cec5SDimitry Andric   }
13850b57cec5SDimitry Andric   return false;
13860b57cec5SDimitry Andric }
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
RemoveWatchpointByID(lldb::watch_id_t watch_id)13890b57cec5SDimitry Andric bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) {
139081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
13919dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric   WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
13940b57cec5SDimitry Andric   if (watch_to_remove_sp == m_last_created_watchpoint)
13950b57cec5SDimitry Andric     m_last_created_watchpoint.reset();
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   if (DisableWatchpointByID(watch_id)) {
13980b57cec5SDimitry Andric     m_watchpoint_list.Remove(watch_id, true);
13990b57cec5SDimitry Andric     return true;
14000b57cec5SDimitry Andric   }
14010b57cec5SDimitry Andric   return false;
14020b57cec5SDimitry Andric }
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
IgnoreWatchpointByID(lldb::watch_id_t watch_id,uint32_t ignore_count)14050b57cec5SDimitry Andric bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id,
14060b57cec5SDimitry Andric                                   uint32_t ignore_count) {
140781ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Watchpoints);
14089dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
14090b57cec5SDimitry Andric 
14100b57cec5SDimitry Andric   if (!ProcessIsValid())
14110b57cec5SDimitry Andric     return false;
14120b57cec5SDimitry Andric 
14130b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
14140b57cec5SDimitry Andric   if (wp_sp) {
14150b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
14160b57cec5SDimitry Andric     return true;
14170b57cec5SDimitry Andric   }
14180b57cec5SDimitry Andric   return false;
14190b57cec5SDimitry Andric }
14200b57cec5SDimitry Andric 
GetExecutableModule()14210b57cec5SDimitry Andric ModuleSP Target::GetExecutableModule() {
14220b57cec5SDimitry Andric   // search for the first executable in the module list
14230b57cec5SDimitry Andric   for (size_t i = 0; i < m_images.GetSize(); ++i) {
14240b57cec5SDimitry Andric     ModuleSP module_sp = m_images.GetModuleAtIndex(i);
14250b57cec5SDimitry Andric     lldb_private::ObjectFile *obj = module_sp->GetObjectFile();
14260b57cec5SDimitry Andric     if (obj == nullptr)
14270b57cec5SDimitry Andric       continue;
14280b57cec5SDimitry Andric     if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
14290b57cec5SDimitry Andric       return module_sp;
14300b57cec5SDimitry Andric   }
14310b57cec5SDimitry Andric   // as fall back return the first module loaded
14320b57cec5SDimitry Andric   return m_images.GetModuleAtIndex(0);
14330b57cec5SDimitry Andric }
14340b57cec5SDimitry Andric 
GetExecutableModulePointer()14350b57cec5SDimitry Andric Module *Target::GetExecutableModulePointer() {
14360b57cec5SDimitry Andric   return GetExecutableModule().get();
14370b57cec5SDimitry Andric }
14380b57cec5SDimitry Andric 
LoadScriptingResourceForModule(const ModuleSP & module_sp,Target * target)14390b57cec5SDimitry Andric static void LoadScriptingResourceForModule(const ModuleSP &module_sp,
14400b57cec5SDimitry Andric                                            Target *target) {
14410b57cec5SDimitry Andric   Status error;
14420b57cec5SDimitry Andric   StreamString feedback_stream;
1443fe013be4SDimitry Andric   if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error,
1444fe013be4SDimitry Andric                                                              feedback_stream)) {
14450b57cec5SDimitry Andric     if (error.AsCString())
14469dba64beSDimitry Andric       target->GetDebugger().GetErrorStream().Printf(
14470b57cec5SDimitry Andric           "unable to load scripting data for module %s - error reported was "
14480b57cec5SDimitry Andric           "%s\n",
14490b57cec5SDimitry Andric           module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
14500b57cec5SDimitry Andric           error.AsCString());
14510b57cec5SDimitry Andric   }
14520b57cec5SDimitry Andric   if (feedback_stream.GetSize())
14539dba64beSDimitry Andric     target->GetDebugger().GetErrorStream().Printf("%s\n",
14540b57cec5SDimitry Andric                                                   feedback_stream.GetData());
14550b57cec5SDimitry Andric }
14560b57cec5SDimitry Andric 
ClearModules(bool delete_locations)14570b57cec5SDimitry Andric void Target::ClearModules(bool delete_locations) {
14580b57cec5SDimitry Andric   ModulesDidUnload(m_images, delete_locations);
14590b57cec5SDimitry Andric   m_section_load_history.Clear();
14600b57cec5SDimitry Andric   m_images.Clear();
14610b57cec5SDimitry Andric   m_scratch_type_system_map.Clear();
14620b57cec5SDimitry Andric }
14630b57cec5SDimitry Andric 
DidExec()14640b57cec5SDimitry Andric void Target::DidExec() {
14650b57cec5SDimitry Andric   // When a process exec's we need to know about it so we can do some cleanup.
14660b57cec5SDimitry Andric   m_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
14670b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
14680b57cec5SDimitry Andric }
14690b57cec5SDimitry Andric 
SetExecutableModule(ModuleSP & executable_sp,LoadDependentFiles load_dependent_files)14700b57cec5SDimitry Andric void Target::SetExecutableModule(ModuleSP &executable_sp,
14710b57cec5SDimitry Andric                                  LoadDependentFiles load_dependent_files) {
147281ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Target);
14730b57cec5SDimitry Andric   ClearModules(false);
14740b57cec5SDimitry Andric 
14750b57cec5SDimitry Andric   if (executable_sp) {
1476349cc55cSDimitry Andric     ElapsedTime elapsed(m_stats.GetCreateTime());
1477e8d8bef9SDimitry Andric     LLDB_SCOPED_TIMERF("Target::SetExecutableModule (executable = '%s')",
14780b57cec5SDimitry Andric                        executable_sp->GetFileSpec().GetPath().c_str());
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric     const bool notify = true;
14819dba64beSDimitry Andric     m_images.Append(executable_sp,
14829dba64beSDimitry Andric                     notify); // The first image is our executable file
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric     // If we haven't set an architecture yet, reset our architecture based on
14850b57cec5SDimitry Andric     // what we found in the executable module.
14860b57cec5SDimitry Andric     if (!m_arch.GetSpec().IsValid()) {
14870b57cec5SDimitry Andric       m_arch = executable_sp->GetArchitecture();
14880b57cec5SDimitry Andric       LLDB_LOG(log,
1489bdd1243dSDimitry Andric                "Target::SetExecutableModule setting architecture to {0} ({1}) "
1490bdd1243dSDimitry Andric                "based on executable file",
14910b57cec5SDimitry Andric                m_arch.GetSpec().GetArchitectureName(),
14920b57cec5SDimitry Andric                m_arch.GetSpec().GetTriple().getTriple());
14930b57cec5SDimitry Andric     }
14940b57cec5SDimitry Andric 
14950b57cec5SDimitry Andric     FileSpecList dependent_files;
14960b57cec5SDimitry Andric     ObjectFile *executable_objfile = executable_sp->GetObjectFile();
14970b57cec5SDimitry Andric     bool load_dependents = true;
14980b57cec5SDimitry Andric     switch (load_dependent_files) {
14990b57cec5SDimitry Andric     case eLoadDependentsDefault:
15000b57cec5SDimitry Andric       load_dependents = executable_sp->IsExecutable();
15010b57cec5SDimitry Andric       break;
15020b57cec5SDimitry Andric     case eLoadDependentsYes:
15030b57cec5SDimitry Andric       load_dependents = true;
15040b57cec5SDimitry Andric       break;
15050b57cec5SDimitry Andric     case eLoadDependentsNo:
15060b57cec5SDimitry Andric       load_dependents = false;
15070b57cec5SDimitry Andric       break;
15080b57cec5SDimitry Andric     }
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric     if (executable_objfile && load_dependents) {
15110b57cec5SDimitry Andric       ModuleList added_modules;
15120b57cec5SDimitry Andric       executable_objfile->GetDependentModules(dependent_files);
15130b57cec5SDimitry Andric       for (uint32_t i = 0; i < dependent_files.GetSize(); i++) {
1514480093f4SDimitry Andric         FileSpec dependent_file_spec(dependent_files.GetFileSpecAtIndex(i));
15150b57cec5SDimitry Andric         FileSpec platform_dependent_file_spec;
15160b57cec5SDimitry Andric         if (m_platform_sp)
15170b57cec5SDimitry Andric           m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr,
15180b57cec5SDimitry Andric                                          platform_dependent_file_spec);
15190b57cec5SDimitry Andric         else
15200b57cec5SDimitry Andric           platform_dependent_file_spec = dependent_file_spec;
15210b57cec5SDimitry Andric 
15220b57cec5SDimitry Andric         ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec());
15239dba64beSDimitry Andric         ModuleSP image_module_sp(
15249dba64beSDimitry Andric             GetOrCreateModule(module_spec, false /* notify */));
15250b57cec5SDimitry Andric         if (image_module_sp) {
15260b57cec5SDimitry Andric           added_modules.AppendIfNeeded(image_module_sp, false);
15270b57cec5SDimitry Andric           ObjectFile *objfile = image_module_sp->GetObjectFile();
15280b57cec5SDimitry Andric           if (objfile)
15290b57cec5SDimitry Andric             objfile->GetDependentModules(dependent_files);
15300b57cec5SDimitry Andric         }
15310b57cec5SDimitry Andric       }
15320b57cec5SDimitry Andric       ModulesDidLoad(added_modules);
15330b57cec5SDimitry Andric     }
15340b57cec5SDimitry Andric   }
15350b57cec5SDimitry Andric }
15360b57cec5SDimitry Andric 
SetArchitecture(const ArchSpec & arch_spec,bool set_platform,bool merge)1537bdd1243dSDimitry Andric bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform,
1538bdd1243dSDimitry Andric                              bool merge) {
153981ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Target);
15400b57cec5SDimitry Andric   bool missing_local_arch = !m_arch.GetSpec().IsValid();
15410b57cec5SDimitry Andric   bool replace_local_arch = true;
15420b57cec5SDimitry Andric   bool compatible_local_arch = false;
15430b57cec5SDimitry Andric   ArchSpec other(arch_spec);
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric   // Changing the architecture might mean that the currently selected platform
15460b57cec5SDimitry Andric   // isn't compatible. Set the platform correctly if we are asked to do so,
15470b57cec5SDimitry Andric   // otherwise assume the user will set the platform manually.
15480b57cec5SDimitry Andric   if (set_platform) {
15490b57cec5SDimitry Andric     if (other.IsValid()) {
15500b57cec5SDimitry Andric       auto platform_sp = GetPlatform();
1551bdd1243dSDimitry Andric       if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
1552bdd1243dSDimitry Andric                               other, {}, ArchSpec::CompatibleMatch, nullptr)) {
15530b57cec5SDimitry Andric         ArchSpec platform_arch;
155481ad6265SDimitry Andric         if (PlatformSP arch_platform_sp =
155581ad6265SDimitry Andric                 GetDebugger().GetPlatformList().GetOrCreate(other, {},
155681ad6265SDimitry Andric                                                             &platform_arch)) {
15570b57cec5SDimitry Andric           SetPlatform(arch_platform_sp);
15580b57cec5SDimitry Andric           if (platform_arch.IsValid())
15590b57cec5SDimitry Andric             other = platform_arch;
15600b57cec5SDimitry Andric         }
15610b57cec5SDimitry Andric       }
15620b57cec5SDimitry Andric     }
15630b57cec5SDimitry Andric   }
15640b57cec5SDimitry Andric 
15650b57cec5SDimitry Andric   if (!missing_local_arch) {
1566bdd1243dSDimitry Andric     if (merge && m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
15670b57cec5SDimitry Andric       other.MergeFrom(m_arch.GetSpec());
15680b57cec5SDimitry Andric 
15690b57cec5SDimitry Andric       if (m_arch.GetSpec().IsCompatibleMatch(other)) {
15700b57cec5SDimitry Andric         compatible_local_arch = true;
15710b57cec5SDimitry Andric         bool arch_changed, vendor_changed, os_changed, os_ver_changed,
15720b57cec5SDimitry Andric             env_changed;
15730b57cec5SDimitry Andric 
15749dba64beSDimitry Andric         m_arch.GetSpec().PiecewiseTripleCompare(other, arch_changed,
15759dba64beSDimitry Andric                                                 vendor_changed, os_changed,
15769dba64beSDimitry Andric                                                 os_ver_changed, env_changed);
15770b57cec5SDimitry Andric 
15780b57cec5SDimitry Andric         if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
15790b57cec5SDimitry Andric           replace_local_arch = false;
15800b57cec5SDimitry Andric       }
15810b57cec5SDimitry Andric     }
15820b57cec5SDimitry Andric   }
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric   if (compatible_local_arch || missing_local_arch) {
15850b57cec5SDimitry Andric     // If we haven't got a valid arch spec, or the architectures are compatible
15860b57cec5SDimitry Andric     // update the architecture, unless the one we already have is more
15870b57cec5SDimitry Andric     // specified
15880b57cec5SDimitry Andric     if (replace_local_arch)
15890b57cec5SDimitry Andric       m_arch = other;
1590bdd1243dSDimitry Andric     LLDB_LOG(log,
1591bdd1243dSDimitry Andric              "Target::SetArchitecture merging compatible arch; arch "
1592bdd1243dSDimitry Andric              "is now {0} ({1})",
15930b57cec5SDimitry Andric              m_arch.GetSpec().GetArchitectureName(),
15940b57cec5SDimitry Andric              m_arch.GetSpec().GetTriple().getTriple());
15950b57cec5SDimitry Andric     return true;
15960b57cec5SDimitry Andric   }
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric   // If we have an executable file, try to reset the executable to the desired
15990b57cec5SDimitry Andric   // architecture
1600bdd1243dSDimitry Andric   LLDB_LOGF(
1601bdd1243dSDimitry Andric       log,
1602bdd1243dSDimitry Andric       "Target::SetArchitecture changing architecture to %s (%s) from %s (%s)",
16030b57cec5SDimitry Andric       arch_spec.GetArchitectureName(),
1604bdd1243dSDimitry Andric       arch_spec.GetTriple().getTriple().c_str(),
1605bdd1243dSDimitry Andric       m_arch.GetSpec().GetArchitectureName(),
1606bdd1243dSDimitry Andric       m_arch.GetSpec().GetTriple().getTriple().c_str());
16070b57cec5SDimitry Andric   m_arch = other;
16080b57cec5SDimitry Andric   ModuleSP executable_sp = GetExecutableModule();
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric   ClearModules(true);
16110b57cec5SDimitry Andric   // Need to do something about unsetting breakpoints.
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   if (executable_sp) {
16149dba64beSDimitry Andric     LLDB_LOGF(log,
16159dba64beSDimitry Andric               "Target::SetArchitecture Trying to select executable file "
16160b57cec5SDimitry Andric               "architecture %s (%s)",
16170b57cec5SDimitry Andric               arch_spec.GetArchitectureName(),
16180b57cec5SDimitry Andric               arch_spec.GetTriple().getTriple().c_str());
16190b57cec5SDimitry Andric     ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
16200b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
16210b57cec5SDimitry Andric     Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
16229dba64beSDimitry Andric                                                &search_paths, nullptr, nullptr);
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric     if (!error.Fail() && executable_sp) {
16250b57cec5SDimitry Andric       SetExecutableModule(executable_sp, eLoadDependentsYes);
16260b57cec5SDimitry Andric       return true;
16270b57cec5SDimitry Andric     }
16280b57cec5SDimitry Andric   }
16290b57cec5SDimitry Andric   return false;
16300b57cec5SDimitry Andric }
16310b57cec5SDimitry Andric 
MergeArchitecture(const ArchSpec & arch_spec)16320b57cec5SDimitry Andric bool Target::MergeArchitecture(const ArchSpec &arch_spec) {
163381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Target);
16340b57cec5SDimitry Andric   if (arch_spec.IsValid()) {
16350b57cec5SDimitry Andric     if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
16360b57cec5SDimitry Andric       // The current target arch is compatible with "arch_spec", see if we can
16370b57cec5SDimitry Andric       // improve our current architecture using bits from "arch_spec"
16380b57cec5SDimitry Andric 
16399dba64beSDimitry Andric       LLDB_LOGF(log,
16409dba64beSDimitry Andric                 "Target::MergeArchitecture target has arch %s, merging with "
16410b57cec5SDimitry Andric                 "arch %s",
16420b57cec5SDimitry Andric                 m_arch.GetSpec().GetTriple().getTriple().c_str(),
16430b57cec5SDimitry Andric                 arch_spec.GetTriple().getTriple().c_str());
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric       // Merge bits from arch_spec into "merged_arch" and set our architecture
16460b57cec5SDimitry Andric       ArchSpec merged_arch(m_arch.GetSpec());
16470b57cec5SDimitry Andric       merged_arch.MergeFrom(arch_spec);
16480b57cec5SDimitry Andric       return SetArchitecture(merged_arch);
16490b57cec5SDimitry Andric     } else {
16500b57cec5SDimitry Andric       // The new architecture is different, we just need to replace it
16510b57cec5SDimitry Andric       return SetArchitecture(arch_spec);
16520b57cec5SDimitry Andric     }
16530b57cec5SDimitry Andric   }
16540b57cec5SDimitry Andric   return false;
16550b57cec5SDimitry Andric }
16560b57cec5SDimitry Andric 
NotifyWillClearList(const ModuleList & module_list)16570b57cec5SDimitry Andric void Target::NotifyWillClearList(const ModuleList &module_list) {}
16580b57cec5SDimitry Andric 
NotifyModuleAdded(const ModuleList & module_list,const ModuleSP & module_sp)16590b57cec5SDimitry Andric void Target::NotifyModuleAdded(const ModuleList &module_list,
16600b57cec5SDimitry Andric                                const ModuleSP &module_sp) {
16610b57cec5SDimitry Andric   // A module is being added to this target for the first time
16620b57cec5SDimitry Andric   if (m_valid) {
16630b57cec5SDimitry Andric     ModuleList my_module_list;
16640b57cec5SDimitry Andric     my_module_list.Append(module_sp);
16650b57cec5SDimitry Andric     ModulesDidLoad(my_module_list);
16660b57cec5SDimitry Andric   }
16670b57cec5SDimitry Andric }
16680b57cec5SDimitry Andric 
NotifyModuleRemoved(const ModuleList & module_list,const ModuleSP & module_sp)16690b57cec5SDimitry Andric void Target::NotifyModuleRemoved(const ModuleList &module_list,
16700b57cec5SDimitry Andric                                  const ModuleSP &module_sp) {
16710b57cec5SDimitry Andric   // A module is being removed from this target.
16720b57cec5SDimitry Andric   if (m_valid) {
16730b57cec5SDimitry Andric     ModuleList my_module_list;
16740b57cec5SDimitry Andric     my_module_list.Append(module_sp);
16750b57cec5SDimitry Andric     ModulesDidUnload(my_module_list, false);
16760b57cec5SDimitry Andric   }
16770b57cec5SDimitry Andric }
16780b57cec5SDimitry Andric 
NotifyModuleUpdated(const ModuleList & module_list,const ModuleSP & old_module_sp,const ModuleSP & new_module_sp)16790b57cec5SDimitry Andric void Target::NotifyModuleUpdated(const ModuleList &module_list,
16800b57cec5SDimitry Andric                                  const ModuleSP &old_module_sp,
16810b57cec5SDimitry Andric                                  const ModuleSP &new_module_sp) {
16820b57cec5SDimitry Andric   // A module is replacing an already added module
16830b57cec5SDimitry Andric   if (m_valid) {
16840b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp,
16850b57cec5SDimitry Andric                                                             new_module_sp);
16860b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(
16870b57cec5SDimitry Andric         old_module_sp, new_module_sp);
16880b57cec5SDimitry Andric   }
16890b57cec5SDimitry Andric }
16900b57cec5SDimitry Andric 
NotifyModulesRemoved(lldb_private::ModuleList & module_list)16910b57cec5SDimitry Andric void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) {
16920b57cec5SDimitry Andric   ModulesDidUnload(module_list, false);
16930b57cec5SDimitry Andric }
16940b57cec5SDimitry Andric 
ModulesDidLoad(ModuleList & module_list)16950b57cec5SDimitry Andric void Target::ModulesDidLoad(ModuleList &module_list) {
16960b57cec5SDimitry Andric   const size_t num_images = module_list.GetSize();
16970b57cec5SDimitry Andric   if (m_valid && num_images) {
16980b57cec5SDimitry Andric     for (size_t idx = 0; idx < num_images; ++idx) {
16990b57cec5SDimitry Andric       ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
17000b57cec5SDimitry Andric       LoadScriptingResourceForModule(module_sp, this);
17010b57cec5SDimitry Andric     }
17020b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
17030b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
17040b57cec5SDimitry Andric     if (m_process_sp) {
17050b57cec5SDimitry Andric       m_process_sp->ModulesDidLoad(module_list);
17060b57cec5SDimitry Andric     }
1707*a58f00eaSDimitry Andric     auto data_sp =
1708*a58f00eaSDimitry Andric         std::make_shared<TargetEventData>(shared_from_this(), module_list);
1709*a58f00eaSDimitry Andric     BroadcastEvent(eBroadcastBitModulesLoaded, data_sp);
17100b57cec5SDimitry Andric   }
17110b57cec5SDimitry Andric }
17120b57cec5SDimitry Andric 
SymbolsDidLoad(ModuleList & module_list)17130b57cec5SDimitry Andric void Target::SymbolsDidLoad(ModuleList &module_list) {
17140b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
17150b57cec5SDimitry Andric     if (m_process_sp) {
17160b57cec5SDimitry Andric       for (LanguageRuntime *runtime : m_process_sp->GetLanguageRuntimes()) {
17170b57cec5SDimitry Andric         runtime->SymbolsDidLoad(module_list);
17180b57cec5SDimitry Andric       }
17190b57cec5SDimitry Andric     }
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
17220b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1723*a58f00eaSDimitry Andric     auto data_sp =
1724*a58f00eaSDimitry Andric         std::make_shared<TargetEventData>(shared_from_this(), module_list);
1725*a58f00eaSDimitry Andric     BroadcastEvent(eBroadcastBitSymbolsLoaded, data_sp);
17260b57cec5SDimitry Andric   }
17270b57cec5SDimitry Andric }
17280b57cec5SDimitry Andric 
ModulesDidUnload(ModuleList & module_list,bool delete_locations)17290b57cec5SDimitry Andric void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) {
17300b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
17310b57cec5SDimitry Andric     UnloadModuleSections(module_list);
1732*a58f00eaSDimitry Andric     auto data_sp =
1733*a58f00eaSDimitry Andric         std::make_shared<TargetEventData>(shared_from_this(), module_list);
1734*a58f00eaSDimitry Andric     BroadcastEvent(eBroadcastBitModulesUnloaded, data_sp);
17350b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, false, delete_locations);
17360b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, false,
17370b57cec5SDimitry Andric                                                  delete_locations);
1738bdd1243dSDimitry Andric 
1739bdd1243dSDimitry Andric     // If a module was torn down it will have torn down the 'TypeSystemClang's
1740bdd1243dSDimitry Andric     // that we used as source 'ASTContext's for the persistent variables in
1741bdd1243dSDimitry Andric     // the current target. Those would now be unsafe to access because the
1742bdd1243dSDimitry Andric     // 'DeclOrigin' are now possibly stale. Thus clear all persistent
1743bdd1243dSDimitry Andric     // variables. We only want to flush 'TypeSystem's if the module being
1744bdd1243dSDimitry Andric     // unloaded was capable of describing a source type. JITted module unloads
1745bdd1243dSDimitry Andric     // happen frequently for Objective-C utility functions or the REPL and rely
1746bdd1243dSDimitry Andric     // on the persistent variables to stick around.
1747bdd1243dSDimitry Andric     const bool should_flush_type_systems =
1748bdd1243dSDimitry Andric         module_list.AnyOf([](lldb_private::Module &module) {
1749bdd1243dSDimitry Andric           auto *object_file = module.GetObjectFile();
1750bdd1243dSDimitry Andric 
1751bdd1243dSDimitry Andric           if (!object_file)
1752bdd1243dSDimitry Andric             return false;
1753bdd1243dSDimitry Andric 
1754bdd1243dSDimitry Andric           auto type = object_file->GetType();
1755bdd1243dSDimitry Andric 
1756bdd1243dSDimitry Andric           // eTypeExecutable: when debugged binary was rebuilt
1757bdd1243dSDimitry Andric           // eTypeSharedLibrary: if dylib was re-loaded
1758bdd1243dSDimitry Andric           return module.FileHasChanged() &&
1759bdd1243dSDimitry Andric                  (type == ObjectFile::eTypeObjectFile ||
1760bdd1243dSDimitry Andric                   type == ObjectFile::eTypeExecutable ||
1761bdd1243dSDimitry Andric                   type == ObjectFile::eTypeSharedLibrary);
1762bdd1243dSDimitry Andric         });
1763bdd1243dSDimitry Andric 
1764bdd1243dSDimitry Andric     if (should_flush_type_systems)
1765bdd1243dSDimitry Andric       m_scratch_type_system_map.Clear();
17660b57cec5SDimitry Andric   }
17670b57cec5SDimitry Andric }
17680b57cec5SDimitry Andric 
ModuleIsExcludedForUnconstrainedSearches(const FileSpec & module_file_spec)17690b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
17700b57cec5SDimitry Andric     const FileSpec &module_file_spec) {
17710b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
17720b57cec5SDimitry Andric     ModuleList matchingModules;
17730b57cec5SDimitry Andric     ModuleSpec module_spec(module_file_spec);
17749dba64beSDimitry Andric     GetImages().FindModules(module_spec, matchingModules);
17759dba64beSDimitry Andric     size_t num_modules = matchingModules.GetSize();
17760b57cec5SDimitry Andric 
17779dba64beSDimitry Andric     // If there is more than one module for this file spec, only
17789dba64beSDimitry Andric     // return true if ALL the modules are on the black list.
17790b57cec5SDimitry Andric     if (num_modules > 0) {
17800b57cec5SDimitry Andric       for (size_t i = 0; i < num_modules; i++) {
17810b57cec5SDimitry Andric         if (!ModuleIsExcludedForUnconstrainedSearches(
17820b57cec5SDimitry Andric                 matchingModules.GetModuleAtIndex(i)))
17830b57cec5SDimitry Andric           return false;
17840b57cec5SDimitry Andric       }
17850b57cec5SDimitry Andric       return true;
17860b57cec5SDimitry Andric     }
17870b57cec5SDimitry Andric   }
17880b57cec5SDimitry Andric   return false;
17890b57cec5SDimitry Andric }
17900b57cec5SDimitry Andric 
ModuleIsExcludedForUnconstrainedSearches(const lldb::ModuleSP & module_sp)17910b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
17920b57cec5SDimitry Andric     const lldb::ModuleSP &module_sp) {
17930b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
17940b57cec5SDimitry Andric     if (m_platform_sp)
17950b57cec5SDimitry Andric       return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(*this,
17960b57cec5SDimitry Andric                                                                      module_sp);
17970b57cec5SDimitry Andric   }
17980b57cec5SDimitry Andric   return false;
17990b57cec5SDimitry Andric }
18000b57cec5SDimitry Andric 
ReadMemoryFromFileCache(const Address & addr,void * dst,size_t dst_len,Status & error)18010b57cec5SDimitry Andric size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst,
18020b57cec5SDimitry Andric                                        size_t dst_len, Status &error) {
18030b57cec5SDimitry Andric   SectionSP section_sp(addr.GetSection());
18040b57cec5SDimitry Andric   if (section_sp) {
18050b57cec5SDimitry Andric     // If the contents of this section are encrypted, the on-disk file is
18060b57cec5SDimitry Andric     // unusable.  Read only from live memory.
18070b57cec5SDimitry Andric     if (section_sp->IsEncrypted()) {
18080b57cec5SDimitry Andric       error.SetErrorString("section is encrypted");
18090b57cec5SDimitry Andric       return 0;
18100b57cec5SDimitry Andric     }
18110b57cec5SDimitry Andric     ModuleSP module_sp(section_sp->GetModule());
18120b57cec5SDimitry Andric     if (module_sp) {
18130b57cec5SDimitry Andric       ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
18140b57cec5SDimitry Andric       if (objfile) {
18150b57cec5SDimitry Andric         size_t bytes_read = objfile->ReadSectionData(
18160b57cec5SDimitry Andric             section_sp.get(), addr.GetOffset(), dst, dst_len);
18170b57cec5SDimitry Andric         if (bytes_read > 0)
18180b57cec5SDimitry Andric           return bytes_read;
18190b57cec5SDimitry Andric         else
18200b57cec5SDimitry Andric           error.SetErrorStringWithFormat("error reading data from section %s",
18210b57cec5SDimitry Andric                                          section_sp->GetName().GetCString());
18220b57cec5SDimitry Andric       } else
18230b57cec5SDimitry Andric         error.SetErrorString("address isn't from a object file");
18240b57cec5SDimitry Andric     } else
18250b57cec5SDimitry Andric       error.SetErrorString("address isn't in a module");
18260b57cec5SDimitry Andric   } else
18270b57cec5SDimitry Andric     error.SetErrorString("address doesn't contain a section that points to a "
18280b57cec5SDimitry Andric                          "section in a object file");
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric   return 0;
18310b57cec5SDimitry Andric }
18320b57cec5SDimitry Andric 
ReadMemory(const Address & addr,void * dst,size_t dst_len,Status & error,bool force_live_memory,lldb::addr_t * load_addr_ptr)1833fe6060f1SDimitry Andric size_t Target::ReadMemory(const Address &addr, void *dst, size_t dst_len,
1834fe6060f1SDimitry Andric                           Status &error, bool force_live_memory,
18350b57cec5SDimitry Andric                           lldb::addr_t *load_addr_ptr) {
18360b57cec5SDimitry Andric   error.Clear();
18370b57cec5SDimitry Andric 
183881ad6265SDimitry Andric   Address fixed_addr = addr;
183981ad6265SDimitry Andric   if (ProcessIsValid())
184081ad6265SDimitry Andric     if (const ABISP &abi = m_process_sp->GetABI())
184181ad6265SDimitry Andric       fixed_addr.SetLoadAddress(abi->FixAnyAddress(addr.GetLoadAddress(this)),
184281ad6265SDimitry Andric                                 this);
184381ad6265SDimitry Andric 
18440b57cec5SDimitry Andric   // if we end up reading this from process memory, we will fill this with the
18450b57cec5SDimitry Andric   // actual load address
18460b57cec5SDimitry Andric   if (load_addr_ptr)
18470b57cec5SDimitry Andric     *load_addr_ptr = LLDB_INVALID_ADDRESS;
18480b57cec5SDimitry Andric 
18490b57cec5SDimitry Andric   size_t bytes_read = 0;
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric   addr_t load_addr = LLDB_INVALID_ADDRESS;
18520b57cec5SDimitry Andric   addr_t file_addr = LLDB_INVALID_ADDRESS;
18530b57cec5SDimitry Andric   Address resolved_addr;
185481ad6265SDimitry Andric   if (!fixed_addr.IsSectionOffset()) {
18550b57cec5SDimitry Andric     SectionLoadList &section_load_list = GetSectionLoadList();
18560b57cec5SDimitry Andric     if (section_load_list.IsEmpty()) {
18570b57cec5SDimitry Andric       // No sections are loaded, so we must assume we are not running yet and
18580b57cec5SDimitry Andric       // anything we are given is a file address.
185981ad6265SDimitry Andric       file_addr =
186081ad6265SDimitry Andric           fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so
186181ad6265SDimitry Andric                                   // its offset is the file address
18620b57cec5SDimitry Andric       m_images.ResolveFileAddress(file_addr, resolved_addr);
18630b57cec5SDimitry Andric     } else {
18640b57cec5SDimitry Andric       // We have at least one section loaded. This can be because we have
18650b57cec5SDimitry Andric       // manually loaded some sections with "target modules load ..." or
1866c9157d92SDimitry Andric       // because we have a live process that has sections loaded through
18670b57cec5SDimitry Andric       // the dynamic loader
186881ad6265SDimitry Andric       load_addr =
186981ad6265SDimitry Andric           fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so
187081ad6265SDimitry Andric                                   // its offset is the load address
18710b57cec5SDimitry Andric       section_load_list.ResolveLoadAddress(load_addr, resolved_addr);
18720b57cec5SDimitry Andric     }
18730b57cec5SDimitry Andric   }
18740b57cec5SDimitry Andric   if (!resolved_addr.IsValid())
187581ad6265SDimitry Andric     resolved_addr = fixed_addr;
18760b57cec5SDimitry Andric 
1877fe6060f1SDimitry Andric   // If we read from the file cache but can't get as many bytes as requested,
1878fe6060f1SDimitry Andric   // we keep the result around in this buffer, in case this result is the
1879fe6060f1SDimitry Andric   // best we can do.
1880fe6060f1SDimitry Andric   std::unique_ptr<uint8_t[]> file_cache_read_buffer;
1881fe6060f1SDimitry Andric   size_t file_cache_bytes_read = 0;
1882fe6060f1SDimitry Andric 
1883fe6060f1SDimitry Andric   // Read from file cache if read-only section.
1884fe6060f1SDimitry Andric   if (!force_live_memory && resolved_addr.IsSectionOffset()) {
1885fe6060f1SDimitry Andric     SectionSP section_sp(resolved_addr.GetSection());
1886fe6060f1SDimitry Andric     if (section_sp) {
1887fe6060f1SDimitry Andric       auto permissions = Flags(section_sp->GetPermissions());
1888fe6060f1SDimitry Andric       bool is_readonly = !permissions.Test(ePermissionsWritable) &&
1889fe6060f1SDimitry Andric                          permissions.Test(ePermissionsReadable);
1890fe6060f1SDimitry Andric       if (is_readonly) {
1891fe6060f1SDimitry Andric         file_cache_bytes_read =
1892fe6060f1SDimitry Andric             ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1893fe6060f1SDimitry Andric         if (file_cache_bytes_read == dst_len)
1894fe6060f1SDimitry Andric           return file_cache_bytes_read;
1895fe6060f1SDimitry Andric         else if (file_cache_bytes_read > 0) {
1896fe6060f1SDimitry Andric           file_cache_read_buffer =
1897fe6060f1SDimitry Andric               std::make_unique<uint8_t[]>(file_cache_bytes_read);
1898fe6060f1SDimitry Andric           std::memcpy(file_cache_read_buffer.get(), dst, file_cache_bytes_read);
1899fe6060f1SDimitry Andric         }
1900fe6060f1SDimitry Andric       }
1901fe6060f1SDimitry Andric     }
19020b57cec5SDimitry Andric   }
19030b57cec5SDimitry Andric 
19040b57cec5SDimitry Andric   if (ProcessIsValid()) {
19050b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS)
19060b57cec5SDimitry Andric       load_addr = resolved_addr.GetLoadAddress(this);
19070b57cec5SDimitry Andric 
19080b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS) {
19090b57cec5SDimitry Andric       ModuleSP addr_module_sp(resolved_addr.GetModule());
19100b57cec5SDimitry Andric       if (addr_module_sp && addr_module_sp->GetFileSpec())
19110b57cec5SDimitry Andric         error.SetErrorStringWithFormatv(
19120b57cec5SDimitry Andric             "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded",
19130b57cec5SDimitry Andric             addr_module_sp->GetFileSpec(), resolved_addr.GetFileAddress());
19140b57cec5SDimitry Andric       else
19150b57cec5SDimitry Andric         error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved",
19160b57cec5SDimitry Andric                                        resolved_addr.GetFileAddress());
19170b57cec5SDimitry Andric     } else {
19180b57cec5SDimitry Andric       bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
19190b57cec5SDimitry Andric       if (bytes_read != dst_len) {
19200b57cec5SDimitry Andric         if (error.Success()) {
19210b57cec5SDimitry Andric           if (bytes_read == 0)
19220b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
19230b57cec5SDimitry Andric                 "read memory from 0x%" PRIx64 " failed", load_addr);
19240b57cec5SDimitry Andric           else
19250b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
19260b57cec5SDimitry Andric                 "only %" PRIu64 " of %" PRIu64
19270b57cec5SDimitry Andric                 " bytes were read from memory at 0x%" PRIx64,
19280b57cec5SDimitry Andric                 (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
19290b57cec5SDimitry Andric         }
19300b57cec5SDimitry Andric       }
19310b57cec5SDimitry Andric       if (bytes_read) {
19320b57cec5SDimitry Andric         if (load_addr_ptr)
19330b57cec5SDimitry Andric           *load_addr_ptr = load_addr;
19340b57cec5SDimitry Andric         return bytes_read;
19350b57cec5SDimitry Andric       }
19360b57cec5SDimitry Andric     }
19370b57cec5SDimitry Andric   }
19380b57cec5SDimitry Andric 
1939fe6060f1SDimitry Andric   if (file_cache_read_buffer && file_cache_bytes_read > 0) {
1940fe6060f1SDimitry Andric     // Reading from the process failed. If we've previously succeeded in reading
1941fe6060f1SDimitry Andric     // something from the file cache, then copy that over and return that.
1942fe6060f1SDimitry Andric     std::memcpy(dst, file_cache_read_buffer.get(), file_cache_bytes_read);
1943fe6060f1SDimitry Andric     return file_cache_bytes_read;
1944fe6060f1SDimitry Andric   }
1945fe6060f1SDimitry Andric 
1946fe6060f1SDimitry Andric   if (!file_cache_read_buffer && resolved_addr.IsSectionOffset()) {
19470b57cec5SDimitry Andric     // If we didn't already try and read from the object file cache, then try
19480b57cec5SDimitry Andric     // it after failing to read from the process.
19490b57cec5SDimitry Andric     return ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
19500b57cec5SDimitry Andric   }
19510b57cec5SDimitry Andric   return 0;
19520b57cec5SDimitry Andric }
19530b57cec5SDimitry Andric 
ReadCStringFromMemory(const Address & addr,std::string & out_str,Status & error,bool force_live_memory)19540b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str,
19551fd87a68SDimitry Andric                                      Status &error, bool force_live_memory) {
19560b57cec5SDimitry Andric   char buf[256];
19570b57cec5SDimitry Andric   out_str.clear();
19580b57cec5SDimitry Andric   addr_t curr_addr = addr.GetLoadAddress(this);
19590b57cec5SDimitry Andric   Address address(addr);
19600b57cec5SDimitry Andric   while (true) {
19611fd87a68SDimitry Andric     size_t length = ReadCStringFromMemory(address, buf, sizeof(buf), error,
19621fd87a68SDimitry Andric                                           force_live_memory);
19630b57cec5SDimitry Andric     if (length == 0)
19640b57cec5SDimitry Andric       break;
19650b57cec5SDimitry Andric     out_str.append(buf, length);
19660b57cec5SDimitry Andric     // If we got "length - 1" bytes, we didn't get the whole C string, we need
19670b57cec5SDimitry Andric     // to read some more characters
19680b57cec5SDimitry Andric     if (length == sizeof(buf) - 1)
19690b57cec5SDimitry Andric       curr_addr += length;
19700b57cec5SDimitry Andric     else
19710b57cec5SDimitry Andric       break;
19720b57cec5SDimitry Andric     address = Address(curr_addr);
19730b57cec5SDimitry Andric   }
19740b57cec5SDimitry Andric   return out_str.size();
19750b57cec5SDimitry Andric }
19760b57cec5SDimitry Andric 
ReadCStringFromMemory(const Address & addr,char * dst,size_t dst_max_len,Status & result_error,bool force_live_memory)19770b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, char *dst,
19781fd87a68SDimitry Andric                                      size_t dst_max_len, Status &result_error,
19791fd87a68SDimitry Andric                                      bool force_live_memory) {
19800b57cec5SDimitry Andric   size_t total_cstr_len = 0;
19810b57cec5SDimitry Andric   if (dst && dst_max_len) {
19820b57cec5SDimitry Andric     result_error.Clear();
19830b57cec5SDimitry Andric     // NULL out everything just to be safe
19840b57cec5SDimitry Andric     memset(dst, 0, dst_max_len);
19850b57cec5SDimitry Andric     Status error;
19860b57cec5SDimitry Andric     addr_t curr_addr = addr.GetLoadAddress(this);
19870b57cec5SDimitry Andric     Address address(addr);
19880b57cec5SDimitry Andric 
19890b57cec5SDimitry Andric     // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think
19900b57cec5SDimitry Andric     // this really needs to be tied to the memory cache subsystem's cache line
19910b57cec5SDimitry Andric     // size, so leave this as a fixed constant.
19920b57cec5SDimitry Andric     const size_t cache_line_size = 512;
19930b57cec5SDimitry Andric 
19940b57cec5SDimitry Andric     size_t bytes_left = dst_max_len - 1;
19950b57cec5SDimitry Andric     char *curr_dst = dst;
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric     while (bytes_left > 0) {
19980b57cec5SDimitry Andric       addr_t cache_line_bytes_left =
19990b57cec5SDimitry Andric           cache_line_size - (curr_addr % cache_line_size);
20000b57cec5SDimitry Andric       addr_t bytes_to_read =
20010b57cec5SDimitry Andric           std::min<addr_t>(bytes_left, cache_line_bytes_left);
20021fd87a68SDimitry Andric       size_t bytes_read = ReadMemory(address, curr_dst, bytes_to_read, error,
20031fd87a68SDimitry Andric                                      force_live_memory);
20040b57cec5SDimitry Andric 
20050b57cec5SDimitry Andric       if (bytes_read == 0) {
20060b57cec5SDimitry Andric         result_error = error;
20070b57cec5SDimitry Andric         dst[total_cstr_len] = '\0';
20080b57cec5SDimitry Andric         break;
20090b57cec5SDimitry Andric       }
20100b57cec5SDimitry Andric       const size_t len = strlen(curr_dst);
20110b57cec5SDimitry Andric 
20120b57cec5SDimitry Andric       total_cstr_len += len;
20130b57cec5SDimitry Andric 
20140b57cec5SDimitry Andric       if (len < bytes_to_read)
20150b57cec5SDimitry Andric         break;
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric       curr_dst += bytes_read;
20180b57cec5SDimitry Andric       curr_addr += bytes_read;
20190b57cec5SDimitry Andric       bytes_left -= bytes_read;
20200b57cec5SDimitry Andric       address = Address(curr_addr);
20210b57cec5SDimitry Andric     }
20220b57cec5SDimitry Andric   } else {
20230b57cec5SDimitry Andric     if (dst == nullptr)
20240b57cec5SDimitry Andric       result_error.SetErrorString("invalid arguments");
20250b57cec5SDimitry Andric     else
20260b57cec5SDimitry Andric       result_error.Clear();
20270b57cec5SDimitry Andric   }
20280b57cec5SDimitry Andric   return total_cstr_len;
20290b57cec5SDimitry Andric }
20300b57cec5SDimitry Andric 
GetReasonableReadSize(const Address & addr)2031349cc55cSDimitry Andric addr_t Target::GetReasonableReadSize(const Address &addr) {
2032349cc55cSDimitry Andric   addr_t load_addr = addr.GetLoadAddress(this);
2033349cc55cSDimitry Andric   if (load_addr != LLDB_INVALID_ADDRESS && m_process_sp) {
2034349cc55cSDimitry Andric     // Avoid crossing cache line boundaries.
2035349cc55cSDimitry Andric     addr_t cache_line_size = m_process_sp->GetMemoryCacheLineSize();
2036349cc55cSDimitry Andric     return cache_line_size - (load_addr % cache_line_size);
2037349cc55cSDimitry Andric   }
2038349cc55cSDimitry Andric 
2039349cc55cSDimitry Andric   // The read is going to go to the file cache, so we can just pick a largish
2040349cc55cSDimitry Andric   // value.
2041349cc55cSDimitry Andric   return 0x1000;
2042349cc55cSDimitry Andric }
2043349cc55cSDimitry Andric 
ReadStringFromMemory(const Address & addr,char * dst,size_t max_bytes,Status & error,size_t type_width,bool force_live_memory)2044349cc55cSDimitry Andric size_t Target::ReadStringFromMemory(const Address &addr, char *dst,
2045349cc55cSDimitry Andric                                     size_t max_bytes, Status &error,
2046349cc55cSDimitry Andric                                     size_t type_width, bool force_live_memory) {
2047349cc55cSDimitry Andric   if (!dst || !max_bytes || !type_width || max_bytes < type_width)
2048349cc55cSDimitry Andric     return 0;
2049349cc55cSDimitry Andric 
2050349cc55cSDimitry Andric   size_t total_bytes_read = 0;
2051349cc55cSDimitry Andric 
2052349cc55cSDimitry Andric   // Ensure a null terminator independent of the number of bytes that is
2053349cc55cSDimitry Andric   // read.
2054349cc55cSDimitry Andric   memset(dst, 0, max_bytes);
2055349cc55cSDimitry Andric   size_t bytes_left = max_bytes - type_width;
2056349cc55cSDimitry Andric 
2057349cc55cSDimitry Andric   const char terminator[4] = {'\0', '\0', '\0', '\0'};
2058349cc55cSDimitry Andric   assert(sizeof(terminator) >= type_width && "Attempting to validate a "
2059349cc55cSDimitry Andric                                              "string with more than 4 bytes "
2060349cc55cSDimitry Andric                                              "per character!");
2061349cc55cSDimitry Andric 
2062349cc55cSDimitry Andric   Address address = addr;
2063349cc55cSDimitry Andric   char *curr_dst = dst;
2064349cc55cSDimitry Andric 
2065349cc55cSDimitry Andric   error.Clear();
2066349cc55cSDimitry Andric   while (bytes_left > 0 && error.Success()) {
2067349cc55cSDimitry Andric     addr_t bytes_to_read =
2068349cc55cSDimitry Andric         std::min<addr_t>(bytes_left, GetReasonableReadSize(address));
2069349cc55cSDimitry Andric     size_t bytes_read =
2070349cc55cSDimitry Andric         ReadMemory(address, curr_dst, bytes_to_read, error, force_live_memory);
2071349cc55cSDimitry Andric 
2072349cc55cSDimitry Andric     if (bytes_read == 0)
2073349cc55cSDimitry Andric       break;
2074349cc55cSDimitry Andric 
2075349cc55cSDimitry Andric     // Search for a null terminator of correct size and alignment in
2076349cc55cSDimitry Andric     // bytes_read
2077349cc55cSDimitry Andric     size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2078349cc55cSDimitry Andric     for (size_t i = aligned_start;
2079349cc55cSDimitry Andric          i + type_width <= total_bytes_read + bytes_read; i += type_width)
2080349cc55cSDimitry Andric       if (::memcmp(&dst[i], terminator, type_width) == 0) {
2081349cc55cSDimitry Andric         error.Clear();
2082349cc55cSDimitry Andric         return i;
2083349cc55cSDimitry Andric       }
2084349cc55cSDimitry Andric 
2085349cc55cSDimitry Andric     total_bytes_read += bytes_read;
2086349cc55cSDimitry Andric     curr_dst += bytes_read;
2087349cc55cSDimitry Andric     address.Slide(bytes_read);
2088349cc55cSDimitry Andric     bytes_left -= bytes_read;
2089349cc55cSDimitry Andric   }
2090349cc55cSDimitry Andric   return total_bytes_read;
2091349cc55cSDimitry Andric }
2092349cc55cSDimitry Andric 
ReadScalarIntegerFromMemory(const Address & addr,uint32_t byte_size,bool is_signed,Scalar & scalar,Status & error,bool force_live_memory)2093fe6060f1SDimitry Andric size_t Target::ReadScalarIntegerFromMemory(const Address &addr, uint32_t byte_size,
2094fe6060f1SDimitry Andric                                            bool is_signed, Scalar &scalar,
2095fe6060f1SDimitry Andric                                            Status &error,
2096fe6060f1SDimitry Andric                                            bool force_live_memory) {
20970b57cec5SDimitry Andric   uint64_t uval;
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric   if (byte_size <= sizeof(uval)) {
21000b57cec5SDimitry Andric     size_t bytes_read =
2101fe6060f1SDimitry Andric         ReadMemory(addr, &uval, byte_size, error, force_live_memory);
21020b57cec5SDimitry Andric     if (bytes_read == byte_size) {
21030b57cec5SDimitry Andric       DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(),
21040b57cec5SDimitry Andric                          m_arch.GetSpec().GetAddressByteSize());
21050b57cec5SDimitry Andric       lldb::offset_t offset = 0;
21060b57cec5SDimitry Andric       if (byte_size <= 4)
21070b57cec5SDimitry Andric         scalar = data.GetMaxU32(&offset, byte_size);
21080b57cec5SDimitry Andric       else
21090b57cec5SDimitry Andric         scalar = data.GetMaxU64(&offset, byte_size);
21100b57cec5SDimitry Andric 
21110b57cec5SDimitry Andric       if (is_signed)
21120b57cec5SDimitry Andric         scalar.SignExtend(byte_size * 8);
21130b57cec5SDimitry Andric       return bytes_read;
21140b57cec5SDimitry Andric     }
21150b57cec5SDimitry Andric   } else {
21160b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
21170b57cec5SDimitry Andric         "byte size of %u is too large for integer scalar type", byte_size);
21180b57cec5SDimitry Andric   }
21190b57cec5SDimitry Andric   return 0;
21200b57cec5SDimitry Andric }
21210b57cec5SDimitry Andric 
ReadUnsignedIntegerFromMemory(const Address & addr,size_t integer_byte_size,uint64_t fail_value,Status & error,bool force_live_memory)21220b57cec5SDimitry Andric uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr,
21230b57cec5SDimitry Andric                                                size_t integer_byte_size,
2124fe6060f1SDimitry Andric                                                uint64_t fail_value, Status &error,
2125fe6060f1SDimitry Andric                                                bool force_live_memory) {
21260b57cec5SDimitry Andric   Scalar scalar;
2127fe6060f1SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, integer_byte_size, false, scalar, error,
2128fe6060f1SDimitry Andric                                   force_live_memory))
21290b57cec5SDimitry Andric     return scalar.ULongLong(fail_value);
21300b57cec5SDimitry Andric   return fail_value;
21310b57cec5SDimitry Andric }
21320b57cec5SDimitry Andric 
ReadPointerFromMemory(const Address & addr,Status & error,Address & pointer_addr,bool force_live_memory)2133fe6060f1SDimitry Andric bool Target::ReadPointerFromMemory(const Address &addr, Status &error,
2134fe6060f1SDimitry Andric                                    Address &pointer_addr,
2135fe6060f1SDimitry Andric                                    bool force_live_memory) {
21360b57cec5SDimitry Andric   Scalar scalar;
2137fe6060f1SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, m_arch.GetSpec().GetAddressByteSize(),
2138fe6060f1SDimitry Andric                                   false, scalar, error, force_live_memory)) {
21390b57cec5SDimitry Andric     addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
21400b57cec5SDimitry Andric     if (pointer_vm_addr != LLDB_INVALID_ADDRESS) {
21410b57cec5SDimitry Andric       SectionLoadList &section_load_list = GetSectionLoadList();
21420b57cec5SDimitry Andric       if (section_load_list.IsEmpty()) {
21430b57cec5SDimitry Andric         // No sections are loaded, so we must assume we are not running yet and
21440b57cec5SDimitry Andric         // anything we are given is a file address.
21450b57cec5SDimitry Andric         m_images.ResolveFileAddress(pointer_vm_addr, pointer_addr);
21460b57cec5SDimitry Andric       } else {
21470b57cec5SDimitry Andric         // We have at least one section loaded. This can be because we have
21480b57cec5SDimitry Andric         // manually loaded some sections with "target modules load ..." or
2149c9157d92SDimitry Andric         // because we have a live process that has sections loaded through
21500b57cec5SDimitry Andric         // the dynamic loader
21510b57cec5SDimitry Andric         section_load_list.ResolveLoadAddress(pointer_vm_addr, pointer_addr);
21520b57cec5SDimitry Andric       }
21530b57cec5SDimitry Andric       // We weren't able to resolve the pointer value, so just return an
21540b57cec5SDimitry Andric       // address with no section
21550b57cec5SDimitry Andric       if (!pointer_addr.IsValid())
21560b57cec5SDimitry Andric         pointer_addr.SetOffset(pointer_vm_addr);
21570b57cec5SDimitry Andric       return true;
21580b57cec5SDimitry Andric     }
21590b57cec5SDimitry Andric   }
21600b57cec5SDimitry Andric   return false;
21610b57cec5SDimitry Andric }
21620b57cec5SDimitry Andric 
GetOrCreateModule(const ModuleSpec & module_spec,bool notify,Status * error_ptr)21630b57cec5SDimitry Andric ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
21640b57cec5SDimitry Andric                                    Status *error_ptr) {
21650b57cec5SDimitry Andric   ModuleSP module_sp;
21660b57cec5SDimitry Andric 
21670b57cec5SDimitry Andric   Status error;
21680b57cec5SDimitry Andric 
21690b57cec5SDimitry Andric   // First see if we already have this module in our module list.  If we do,
21700b57cec5SDimitry Andric   // then we're done, we don't need to consult the shared modules list.  But
21710b57cec5SDimitry Andric   // only do this if we are passed a UUID.
21720b57cec5SDimitry Andric 
21730b57cec5SDimitry Andric   if (module_spec.GetUUID().IsValid())
21740b57cec5SDimitry Andric     module_sp = m_images.FindFirstModule(module_spec);
21750b57cec5SDimitry Andric 
21760b57cec5SDimitry Andric   if (!module_sp) {
2177eaeb601bSDimitry Andric     llvm::SmallVector<ModuleSP, 1>
2178eaeb601bSDimitry Andric         old_modules; // This will get filled in if we have a new version
21790b57cec5SDimitry Andric                      // of the library
21800b57cec5SDimitry Andric     bool did_create_module = false;
21810b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
2182fe013be4SDimitry Andric     FileSpec symbol_file_spec;
2183fe013be4SDimitry Andric 
2184fe013be4SDimitry Andric     // Call locate module callback if set. This allows users to implement their
2185fe013be4SDimitry Andric     // own module cache system. For example, to leverage build system artifacts,
2186fe013be4SDimitry Andric     // to bypass pulling files from remote platform, or to search symbol files
2187fe013be4SDimitry Andric     // from symbol servers.
2188c9157d92SDimitry Andric     if (m_platform_sp)
2189c9157d92SDimitry Andric       m_platform_sp->CallLocateModuleCallbackIfSet(
2190c9157d92SDimitry Andric           module_spec, module_sp, symbol_file_spec, &did_create_module);
2191fe013be4SDimitry Andric 
2192fe013be4SDimitry Andric     // The result of this CallLocateModuleCallbackIfSet is one of the following.
2193fe013be4SDimitry Andric     // 1. module_sp:loaded, symbol_file_spec:set
2194fe013be4SDimitry Andric     //      The callback found a module file and a symbol file for the
2195fe013be4SDimitry Andric     //      module_spec. We will call module_sp->SetSymbolFileFileSpec with
2196fe013be4SDimitry Andric     //      the symbol_file_spec later.
2197fe013be4SDimitry Andric     // 2. module_sp:loaded, symbol_file_spec:empty
2198fe013be4SDimitry Andric     //      The callback only found a module file for the module_spec.
2199fe013be4SDimitry Andric     // 3. module_sp:empty, symbol_file_spec:set
2200fe013be4SDimitry Andric     //      The callback only found a symbol file for the module. We continue
2201fe013be4SDimitry Andric     //      to find a module file for this module_spec and we will call
2202fe013be4SDimitry Andric     //      module_sp->SetSymbolFileFileSpec with the symbol_file_spec later.
2203fe013be4SDimitry Andric     // 4. module_sp:empty, symbol_file_spec:empty
2204c9157d92SDimitry Andric     //      Platform does not exist, the callback is not set, the callback did
2205c9157d92SDimitry Andric     //      not find any module files nor any symbol files, the callback failed,
2206c9157d92SDimitry Andric     //      or something went wrong. We continue to find a module file for this
2207c9157d92SDimitry Andric     //      module_spec.
2208fe013be4SDimitry Andric 
2209fe013be4SDimitry Andric     if (!module_sp) {
2210fe013be4SDimitry Andric       // If there are image search path entries, try to use them to acquire a
2211fe013be4SDimitry Andric       // suitable image.
22120b57cec5SDimitry Andric       if (m_image_search_paths.GetSize()) {
22130b57cec5SDimitry Andric         ModuleSpec transformed_spec(module_spec);
2214bdd1243dSDimitry Andric         ConstString transformed_dir;
22150b57cec5SDimitry Andric         if (m_image_search_paths.RemapPath(
2216bdd1243dSDimitry Andric                 module_spec.GetFileSpec().GetDirectory(), transformed_dir)) {
2217bdd1243dSDimitry Andric           transformed_spec.GetFileSpec().SetDirectory(transformed_dir);
2218bdd1243dSDimitry Andric           transformed_spec.GetFileSpec().SetFilename(
2219bdd1243dSDimitry Andric                 module_spec.GetFileSpec().GetFilename());
22200b57cec5SDimitry Andric           error = ModuleList::GetSharedModule(transformed_spec, module_sp,
2221eaeb601bSDimitry Andric                                               &search_paths, &old_modules,
22229dba64beSDimitry Andric                                               &did_create_module);
22230b57cec5SDimitry Andric         }
22240b57cec5SDimitry Andric       }
2225fe013be4SDimitry Andric     }
22260b57cec5SDimitry Andric 
22270b57cec5SDimitry Andric     if (!module_sp) {
22280b57cec5SDimitry Andric       // If we have a UUID, we can check our global shared module list in case
22290b57cec5SDimitry Andric       // we already have it. If we don't have a valid UUID, then we can't since
22300b57cec5SDimitry Andric       // the path in "module_spec" will be a platform path, and we will need to
22310b57cec5SDimitry Andric       // let the platform find that file. For example, we could be asking for
22320b57cec5SDimitry Andric       // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
22330b57cec5SDimitry Andric       // the local copy of "/usr/lib/dyld" since our platform could be a remote
22340b57cec5SDimitry Andric       // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
22350b57cec5SDimitry Andric       // cache.
22360b57cec5SDimitry Andric       if (module_spec.GetUUID().IsValid()) {
22370b57cec5SDimitry Andric         // We have a UUID, it is OK to check the global module list...
22389dba64beSDimitry Andric         error =
22399dba64beSDimitry Andric             ModuleList::GetSharedModule(module_spec, module_sp, &search_paths,
2240eaeb601bSDimitry Andric                                         &old_modules, &did_create_module);
22410b57cec5SDimitry Andric       }
22420b57cec5SDimitry Andric 
22430b57cec5SDimitry Andric       if (!module_sp) {
22440b57cec5SDimitry Andric         // The platform is responsible for finding and caching an appropriate
22450b57cec5SDimitry Andric         // module in the shared module cache.
22460b57cec5SDimitry Andric         if (m_platform_sp) {
22470b57cec5SDimitry Andric           error = m_platform_sp->GetSharedModule(
22489dba64beSDimitry Andric               module_spec, m_process_sp.get(), module_sp, &search_paths,
2249eaeb601bSDimitry Andric               &old_modules, &did_create_module);
22500b57cec5SDimitry Andric         } else {
22510b57cec5SDimitry Andric           error.SetErrorString("no platform is currently set");
22520b57cec5SDimitry Andric         }
22530b57cec5SDimitry Andric       }
22540b57cec5SDimitry Andric     }
22550b57cec5SDimitry Andric 
22560b57cec5SDimitry Andric     // We found a module that wasn't in our target list.  Let's make sure that
22570b57cec5SDimitry Andric     // there wasn't an equivalent module in the list already, and if there was,
22580b57cec5SDimitry Andric     // let's remove it.
22590b57cec5SDimitry Andric     if (module_sp) {
22600b57cec5SDimitry Andric       ObjectFile *objfile = module_sp->GetObjectFile();
22610b57cec5SDimitry Andric       if (objfile) {
22620b57cec5SDimitry Andric         switch (objfile->GetType()) {
22630b57cec5SDimitry Andric         case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of
22640b57cec5SDimitry Andric                                         /// a program's execution state
22650b57cec5SDimitry Andric         case ObjectFile::eTypeExecutable:    /// A normal executable
22660b57cec5SDimitry Andric         case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker
22670b57cec5SDimitry Andric                                              /// executable
22680b57cec5SDimitry Andric         case ObjectFile::eTypeObjectFile:    /// An intermediate object file
22690b57cec5SDimitry Andric         case ObjectFile::eTypeSharedLibrary: /// A shared library that can be
22700b57cec5SDimitry Andric                                              /// used during execution
22710b57cec5SDimitry Andric           break;
22720b57cec5SDimitry Andric         case ObjectFile::eTypeDebugInfo: /// An object file that contains only
22730b57cec5SDimitry Andric                                          /// debug information
22740b57cec5SDimitry Andric           if (error_ptr)
22750b57cec5SDimitry Andric             error_ptr->SetErrorString("debug info files aren't valid target "
22760b57cec5SDimitry Andric                                       "modules, please specify an executable");
22770b57cec5SDimitry Andric           return ModuleSP();
22780b57cec5SDimitry Andric         case ObjectFile::eTypeStubLibrary: /// A library that can be linked
22790b57cec5SDimitry Andric                                            /// against but not used for
22800b57cec5SDimitry Andric                                            /// execution
22810b57cec5SDimitry Andric           if (error_ptr)
22820b57cec5SDimitry Andric             error_ptr->SetErrorString("stub libraries aren't valid target "
22830b57cec5SDimitry Andric                                       "modules, please specify an executable");
22840b57cec5SDimitry Andric           return ModuleSP();
22850b57cec5SDimitry Andric         default:
22860b57cec5SDimitry Andric           if (error_ptr)
22870b57cec5SDimitry Andric             error_ptr->SetErrorString(
22880b57cec5SDimitry Andric                 "unsupported file type, please specify an executable");
22890b57cec5SDimitry Andric           return ModuleSP();
22900b57cec5SDimitry Andric         }
22910b57cec5SDimitry Andric         // GetSharedModule is not guaranteed to find the old shared module, for
22920b57cec5SDimitry Andric         // instance in the common case where you pass in the UUID, it is only
22930b57cec5SDimitry Andric         // going to find the one module matching the UUID.  In fact, it has no
22940b57cec5SDimitry Andric         // good way to know what the "old module" relevant to this target is,
22950b57cec5SDimitry Andric         // since there might be many copies of a module with this file spec in
22960b57cec5SDimitry Andric         // various running debug sessions, but only one of them will belong to
22970b57cec5SDimitry Andric         // this target. So let's remove the UUID from the module list, and look
22980b57cec5SDimitry Andric         // in the target's module list. Only do this if there is SOMETHING else
22990b57cec5SDimitry Andric         // in the module spec...
23000b57cec5SDimitry Andric         if (module_spec.GetUUID().IsValid() &&
23010b57cec5SDimitry Andric             !module_spec.GetFileSpec().GetFilename().IsEmpty() &&
23020b57cec5SDimitry Andric             !module_spec.GetFileSpec().GetDirectory().IsEmpty()) {
23030b57cec5SDimitry Andric           ModuleSpec module_spec_copy(module_spec.GetFileSpec());
23040b57cec5SDimitry Andric           module_spec_copy.GetUUID().Clear();
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric           ModuleList found_modules;
23070b57cec5SDimitry Andric           m_images.FindModules(module_spec_copy, found_modules);
2308eaeb601bSDimitry Andric           found_modules.ForEach([&](const ModuleSP &found_module) -> bool {
2309eaeb601bSDimitry Andric             old_modules.push_back(found_module);
2310eaeb601bSDimitry Andric             return true;
2311eaeb601bSDimitry Andric           });
23120b57cec5SDimitry Andric         }
23130b57cec5SDimitry Andric 
2314fe013be4SDimitry Andric         // If the locate module callback had found a symbol file, set it to the
2315fe013be4SDimitry Andric         // module_sp before preloading symbols.
2316fe013be4SDimitry Andric         if (symbol_file_spec)
2317fe013be4SDimitry Andric           module_sp->SetSymbolFileFileSpec(symbol_file_spec);
2318fe013be4SDimitry Andric 
23190b57cec5SDimitry Andric         // Preload symbols outside of any lock, so hopefully we can do this for
23200b57cec5SDimitry Andric         // each library in parallel.
23210b57cec5SDimitry Andric         if (GetPreloadSymbols())
23220b57cec5SDimitry Andric           module_sp->PreloadSymbols();
2323eaeb601bSDimitry Andric         llvm::SmallVector<ModuleSP, 1> replaced_modules;
2324eaeb601bSDimitry Andric         for (ModuleSP &old_module_sp : old_modules) {
2325eaeb601bSDimitry Andric           if (m_images.GetIndexForModule(old_module_sp.get()) !=
23260b57cec5SDimitry Andric               LLDB_INVALID_INDEX32) {
2327eaeb601bSDimitry Andric             if (replaced_modules.empty())
23280b57cec5SDimitry Andric               m_images.ReplaceModule(old_module_sp, module_sp);
2329eaeb601bSDimitry Andric             else
2330eaeb601bSDimitry Andric               m_images.Remove(old_module_sp);
2331eaeb601bSDimitry Andric 
2332eaeb601bSDimitry Andric             replaced_modules.push_back(std::move(old_module_sp));
2333eaeb601bSDimitry Andric           }
2334eaeb601bSDimitry Andric         }
2335eaeb601bSDimitry Andric 
2336eaeb601bSDimitry Andric         if (replaced_modules.size() > 1) {
2337eaeb601bSDimitry Andric           // The same new module replaced multiple old modules
2338eaeb601bSDimitry Andric           // simultaneously.  It's not clear this should ever
2339eaeb601bSDimitry Andric           // happen (if we always replace old modules as we add
2340eaeb601bSDimitry Andric           // new ones, presumably we should never have more than
2341eaeb601bSDimitry Andric           // one old one).  If there are legitimate cases where
2342eaeb601bSDimitry Andric           // this happens, then the ModuleList::Notifier interface
2343eaeb601bSDimitry Andric           // may need to be adjusted to allow reporting this.
2344eaeb601bSDimitry Andric           // In the meantime, just log that this has happened; just
2345eaeb601bSDimitry Andric           // above we called ReplaceModule on the first one, and Remove
2346eaeb601bSDimitry Andric           // on the rest.
234781ad6265SDimitry Andric           if (Log *log = GetLog(LLDBLog::Target | LLDBLog::Modules)) {
2348eaeb601bSDimitry Andric             StreamString message;
2349eaeb601bSDimitry Andric             auto dump = [&message](Module &dump_module) -> void {
2350eaeb601bSDimitry Andric               UUID dump_uuid = dump_module.GetUUID();
2351eaeb601bSDimitry Andric 
2352eaeb601bSDimitry Andric               message << '[';
2353eaeb601bSDimitry Andric               dump_module.GetDescription(message.AsRawOstream());
2354eaeb601bSDimitry Andric               message << " (uuid ";
2355eaeb601bSDimitry Andric 
2356eaeb601bSDimitry Andric               if (dump_uuid.IsValid())
2357fe013be4SDimitry Andric                 dump_uuid.Dump(message);
2358eaeb601bSDimitry Andric               else
2359eaeb601bSDimitry Andric                 message << "not specified";
2360eaeb601bSDimitry Andric 
2361eaeb601bSDimitry Andric               message << ")]";
2362eaeb601bSDimitry Andric             };
2363eaeb601bSDimitry Andric 
2364eaeb601bSDimitry Andric             message << "New module ";
2365eaeb601bSDimitry Andric             dump(*module_sp);
2366eaeb601bSDimitry Andric             message.AsRawOstream()
2367eaeb601bSDimitry Andric                 << llvm::formatv(" simultaneously replaced {0} old modules: ",
2368eaeb601bSDimitry Andric                                  replaced_modules.size());
2369eaeb601bSDimitry Andric             for (ModuleSP &replaced_module_sp : replaced_modules)
2370eaeb601bSDimitry Andric               dump(*replaced_module_sp);
2371eaeb601bSDimitry Andric 
2372eaeb601bSDimitry Andric             log->PutString(message.GetString());
2373eaeb601bSDimitry Andric           }
2374eaeb601bSDimitry Andric         }
2375eaeb601bSDimitry Andric 
2376eaeb601bSDimitry Andric         if (replaced_modules.empty())
2377eaeb601bSDimitry Andric           m_images.Append(module_sp, notify);
2378eaeb601bSDimitry Andric 
2379eaeb601bSDimitry Andric         for (ModuleSP &old_module_sp : replaced_modules) {
23800b57cec5SDimitry Andric           Module *old_module_ptr = old_module_sp.get();
23810b57cec5SDimitry Andric           old_module_sp.reset();
23820b57cec5SDimitry Andric           ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
23830b57cec5SDimitry Andric         }
23840b57cec5SDimitry Andric       } else
23850b57cec5SDimitry Andric         module_sp.reset();
23860b57cec5SDimitry Andric     }
23870b57cec5SDimitry Andric   }
23880b57cec5SDimitry Andric   if (error_ptr)
23890b57cec5SDimitry Andric     *error_ptr = error;
23900b57cec5SDimitry Andric   return module_sp;
23910b57cec5SDimitry Andric }
23920b57cec5SDimitry Andric 
CalculateTarget()23930b57cec5SDimitry Andric TargetSP Target::CalculateTarget() { return shared_from_this(); }
23940b57cec5SDimitry Andric 
CalculateProcess()23950b57cec5SDimitry Andric ProcessSP Target::CalculateProcess() { return m_process_sp; }
23960b57cec5SDimitry Andric 
CalculateThread()23970b57cec5SDimitry Andric ThreadSP Target::CalculateThread() { return ThreadSP(); }
23980b57cec5SDimitry Andric 
CalculateStackFrame()23990b57cec5SDimitry Andric StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); }
24000b57cec5SDimitry Andric 
CalculateExecutionContext(ExecutionContext & exe_ctx)24010b57cec5SDimitry Andric void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) {
24020b57cec5SDimitry Andric   exe_ctx.Clear();
24030b57cec5SDimitry Andric   exe_ctx.SetTargetPtr(this);
24040b57cec5SDimitry Andric }
24050b57cec5SDimitry Andric 
GetImageSearchPathList()24060b57cec5SDimitry Andric PathMappingList &Target::GetImageSearchPathList() {
24070b57cec5SDimitry Andric   return m_image_search_paths;
24080b57cec5SDimitry Andric }
24090b57cec5SDimitry Andric 
ImageSearchPathsChanged(const PathMappingList & path_list,void * baton)24100b57cec5SDimitry Andric void Target::ImageSearchPathsChanged(const PathMappingList &path_list,
24110b57cec5SDimitry Andric                                      void *baton) {
24120b57cec5SDimitry Andric   Target *target = (Target *)baton;
24130b57cec5SDimitry Andric   ModuleSP exe_module_sp(target->GetExecutableModule());
24140b57cec5SDimitry Andric   if (exe_module_sp)
24150b57cec5SDimitry Andric     target->SetExecutableModule(exe_module_sp, eLoadDependentsYes);
24160b57cec5SDimitry Andric }
24170b57cec5SDimitry Andric 
2418bdd1243dSDimitry Andric llvm::Expected<lldb::TypeSystemSP>
GetScratchTypeSystemForLanguage(lldb::LanguageType language,bool create_on_demand)24199dba64beSDimitry Andric Target::GetScratchTypeSystemForLanguage(lldb::LanguageType language,
24200b57cec5SDimitry Andric                                         bool create_on_demand) {
24210b57cec5SDimitry Andric   if (!m_valid)
24229dba64beSDimitry Andric     return llvm::make_error<llvm::StringError>("Invalid Target",
24239dba64beSDimitry Andric                                                llvm::inconvertibleErrorCode());
24240b57cec5SDimitry Andric 
24250b57cec5SDimitry Andric   if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all
24260b57cec5SDimitry Andric                                              // assembly code
24270b57cec5SDimitry Andric       || language == eLanguageTypeUnknown) {
24289dba64beSDimitry Andric     LanguageSet languages_for_expressions =
24299dba64beSDimitry Andric         Language::GetLanguagesSupportingTypeSystemsForExpressions();
24300b57cec5SDimitry Andric 
24319dba64beSDimitry Andric     if (languages_for_expressions[eLanguageTypeC]) {
24320b57cec5SDimitry Andric       language = eLanguageTypeC; // LLDB's default.  Override by setting the
24330b57cec5SDimitry Andric                                  // target language.
24340b57cec5SDimitry Andric     } else {
24359dba64beSDimitry Andric       if (languages_for_expressions.Empty())
24369dba64beSDimitry Andric         return llvm::make_error<llvm::StringError>(
24379dba64beSDimitry Andric             "No expression support for any languages",
24389dba64beSDimitry Andric             llvm::inconvertibleErrorCode());
24399dba64beSDimitry Andric       language = (LanguageType)languages_for_expressions.bitvector.find_first();
24400b57cec5SDimitry Andric     }
24410b57cec5SDimitry Andric   }
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric   return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this,
24440b57cec5SDimitry Andric                                                             create_on_demand);
24450b57cec5SDimitry Andric }
24460b57cec5SDimitry Andric 
GetRegisterType(const std::string & name,const lldb_private::RegisterFlags & flags,uint32_t byte_size)2447fe013be4SDimitry Andric CompilerType Target::GetRegisterType(const std::string &name,
2448fe013be4SDimitry Andric                                      const lldb_private::RegisterFlags &flags,
2449fe013be4SDimitry Andric                                      uint32_t byte_size) {
2450fe013be4SDimitry Andric   RegisterTypeBuilderSP provider = PluginManager::GetRegisterTypeBuilder(*this);
2451fe013be4SDimitry Andric   assert(provider);
2452fe013be4SDimitry Andric   return provider->GetRegisterType(name, flags, byte_size);
2453fe013be4SDimitry Andric }
2454fe013be4SDimitry Andric 
2455bdd1243dSDimitry Andric std::vector<lldb::TypeSystemSP>
GetScratchTypeSystems(bool create_on_demand)2456bdd1243dSDimitry Andric Target::GetScratchTypeSystems(bool create_on_demand) {
24579dba64beSDimitry Andric   if (!m_valid)
24589dba64beSDimitry Andric     return {};
24599dba64beSDimitry Andric 
2460349cc55cSDimitry Andric   // Some TypeSystem instances are associated with several LanguageTypes so
2461349cc55cSDimitry Andric   // they will show up several times in the loop below. The SetVector filters
2462349cc55cSDimitry Andric   // out all duplicates as they serve no use for the caller.
2463bdd1243dSDimitry Andric   std::vector<lldb::TypeSystemSP> scratch_type_systems;
24649dba64beSDimitry Andric 
24659dba64beSDimitry Andric   LanguageSet languages_for_expressions =
24669dba64beSDimitry Andric       Language::GetLanguagesSupportingTypeSystemsForExpressions();
24679dba64beSDimitry Andric 
24689dba64beSDimitry Andric   for (auto bit : languages_for_expressions.bitvector.set_bits()) {
24699dba64beSDimitry Andric     auto language = (LanguageType)bit;
24709dba64beSDimitry Andric     auto type_system_or_err =
24719dba64beSDimitry Andric         GetScratchTypeSystemForLanguage(language, create_on_demand);
24729dba64beSDimitry Andric     if (!type_system_or_err)
2473fe013be4SDimitry Andric       LLDB_LOG_ERROR(
2474fe013be4SDimitry Andric           GetLog(LLDBLog::Target), type_system_or_err.takeError(),
2475fe013be4SDimitry Andric           "Language '{1}' has expression support but no scratch type "
2476fe013be4SDimitry Andric           "system available: {0}",
24779dba64beSDimitry Andric           Language::GetNameForLanguageType(language));
24789dba64beSDimitry Andric     else
2479bdd1243dSDimitry Andric       if (auto ts = *type_system_or_err)
2480bdd1243dSDimitry Andric         scratch_type_systems.push_back(ts);
24819dba64beSDimitry Andric   }
2482fe013be4SDimitry Andric 
2483fe013be4SDimitry Andric   std::sort(scratch_type_systems.begin(), scratch_type_systems.end());
2484bdd1243dSDimitry Andric   scratch_type_systems.erase(
2485bdd1243dSDimitry Andric       std::unique(scratch_type_systems.begin(), scratch_type_systems.end()),
2486bdd1243dSDimitry Andric       scratch_type_systems.end());
2487bdd1243dSDimitry Andric   return scratch_type_systems;
24889dba64beSDimitry Andric }
24899dba64beSDimitry Andric 
24900b57cec5SDimitry Andric PersistentExpressionState *
GetPersistentExpressionStateForLanguage(lldb::LanguageType language)24910b57cec5SDimitry Andric Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) {
24929dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language, true);
24930b57cec5SDimitry Andric 
24949dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
2495fe013be4SDimitry Andric     LLDB_LOG_ERROR(
2496fe013be4SDimitry Andric         GetLog(LLDBLog::Target), std::move(err),
2497fe013be4SDimitry Andric         "Unable to get persistent expression state for language {1}: {0}",
24989dba64beSDimitry Andric         Language::GetNameForLanguageType(language));
24990b57cec5SDimitry Andric     return nullptr;
25000b57cec5SDimitry Andric   }
25019dba64beSDimitry Andric 
2502bdd1243dSDimitry Andric   if (auto ts = *type_system_or_err)
2503bdd1243dSDimitry Andric     return ts->GetPersistentExpressionState();
2504bdd1243dSDimitry Andric 
2505bdd1243dSDimitry Andric   LLDB_LOG(GetLog(LLDBLog::Target),
2506fe013be4SDimitry Andric            "Unable to get persistent expression state for language {1}: {0}",
2507bdd1243dSDimitry Andric            Language::GetNameForLanguageType(language));
2508bdd1243dSDimitry Andric   return nullptr;
25090b57cec5SDimitry Andric }
25100b57cec5SDimitry Andric 
GetUserExpressionForLanguage(llvm::StringRef expr,llvm::StringRef prefix,lldb::LanguageType language,Expression::ResultType desired_type,const EvaluateExpressionOptions & options,ValueObject * ctx_obj,Status & error)25110b57cec5SDimitry Andric UserExpression *Target::GetUserExpressionForLanguage(
25120b57cec5SDimitry Andric     llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
25130b57cec5SDimitry Andric     Expression::ResultType desired_type,
25149dba64beSDimitry Andric     const EvaluateExpressionOptions &options, ValueObject *ctx_obj,
25159dba64beSDimitry Andric     Status &error) {
25169dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
25179dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
25180b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
25190b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
25200b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
25219dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
25220b57cec5SDimitry Andric     return nullptr;
25230b57cec5SDimitry Andric   }
25240b57cec5SDimitry Andric 
2525bdd1243dSDimitry Andric   auto ts = *type_system_or_err;
2526bdd1243dSDimitry Andric   if (!ts) {
2527bdd1243dSDimitry Andric     error.SetErrorStringWithFormat(
2528bdd1243dSDimitry Andric         "Type system for language %s is no longer live",
2529bdd1243dSDimitry Andric         Language::GetNameForLanguageType(language));
2530bdd1243dSDimitry Andric     return nullptr;
2531bdd1243dSDimitry Andric   }
2532bdd1243dSDimitry Andric 
2533bdd1243dSDimitry Andric   auto *user_expr = ts->GetUserExpression(expr, prefix, language, desired_type,
2534bdd1243dSDimitry Andric                                           options, ctx_obj);
25350b57cec5SDimitry Andric   if (!user_expr)
25360b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
25370b57cec5SDimitry Andric         "Could not create an expression for language %s",
25380b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
25390b57cec5SDimitry Andric 
25400b57cec5SDimitry Andric   return user_expr;
25410b57cec5SDimitry Andric }
25420b57cec5SDimitry Andric 
GetFunctionCallerForLanguage(lldb::LanguageType language,const CompilerType & return_type,const Address & function_address,const ValueList & arg_value_list,const char * name,Status & error)25430b57cec5SDimitry Andric FunctionCaller *Target::GetFunctionCallerForLanguage(
25440b57cec5SDimitry Andric     lldb::LanguageType language, const CompilerType &return_type,
25450b57cec5SDimitry Andric     const Address &function_address, const ValueList &arg_value_list,
25460b57cec5SDimitry Andric     const char *name, Status &error) {
25479dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
25489dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
25490b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
25500b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
25510b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
25529dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
25539dba64beSDimitry Andric     return nullptr;
25540b57cec5SDimitry Andric   }
2555bdd1243dSDimitry Andric   auto ts = *type_system_or_err;
2556bdd1243dSDimitry Andric   if (!ts) {
2557bdd1243dSDimitry Andric     error.SetErrorStringWithFormat(
2558bdd1243dSDimitry Andric         "Type system for language %s is no longer live",
2559bdd1243dSDimitry Andric         Language::GetNameForLanguageType(language));
2560bdd1243dSDimitry Andric     return nullptr;
2561bdd1243dSDimitry Andric   }
2562bdd1243dSDimitry Andric   auto *persistent_fn = ts->GetFunctionCaller(return_type, function_address,
2563bdd1243dSDimitry Andric                                               arg_value_list, name);
25640b57cec5SDimitry Andric   if (!persistent_fn)
25650b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
25660b57cec5SDimitry Andric         "Could not create an expression for language %s",
25670b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
25680b57cec5SDimitry Andric 
25690b57cec5SDimitry Andric   return persistent_fn;
25700b57cec5SDimitry Andric }
25710b57cec5SDimitry Andric 
2572e8d8bef9SDimitry Andric llvm::Expected<std::unique_ptr<UtilityFunction>>
CreateUtilityFunction(std::string expression,std::string name,lldb::LanguageType language,ExecutionContext & exe_ctx)2573e8d8bef9SDimitry Andric Target::CreateUtilityFunction(std::string expression, std::string name,
25740b57cec5SDimitry Andric                               lldb::LanguageType language,
2575e8d8bef9SDimitry Andric                               ExecutionContext &exe_ctx) {
25769dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
2577e8d8bef9SDimitry Andric   if (!type_system_or_err)
2578e8d8bef9SDimitry Andric     return type_system_or_err.takeError();
2579bdd1243dSDimitry Andric   auto ts = *type_system_or_err;
2580bdd1243dSDimitry Andric   if (!ts)
2581bdd1243dSDimitry Andric     return llvm::make_error<llvm::StringError>(
2582bdd1243dSDimitry Andric         llvm::StringRef("Type system for language ") +
2583bdd1243dSDimitry Andric             Language::GetNameForLanguageType(language) +
2584bdd1243dSDimitry Andric             llvm::StringRef(" is no longer live"),
2585bdd1243dSDimitry Andric         llvm::inconvertibleErrorCode());
2586e8d8bef9SDimitry Andric   std::unique_ptr<UtilityFunction> utility_fn =
2587bdd1243dSDimitry Andric       ts->CreateUtilityFunction(std::move(expression), std::move(name));
25880b57cec5SDimitry Andric   if (!utility_fn)
2589e8d8bef9SDimitry Andric     return llvm::make_error<llvm::StringError>(
2590e8d8bef9SDimitry Andric         llvm::StringRef("Could not create an expression for language") +
2591e8d8bef9SDimitry Andric             Language::GetNameForLanguageType(language),
2592e8d8bef9SDimitry Andric         llvm::inconvertibleErrorCode());
25930b57cec5SDimitry Andric 
2594e8d8bef9SDimitry Andric   DiagnosticManager diagnostics;
2595e8d8bef9SDimitry Andric   if (!utility_fn->Install(diagnostics, exe_ctx))
2596e8d8bef9SDimitry Andric     return llvm::make_error<llvm::StringError>(diagnostics.GetString(),
2597e8d8bef9SDimitry Andric                                                llvm::inconvertibleErrorCode());
2598e8d8bef9SDimitry Andric 
2599e8d8bef9SDimitry Andric   return std::move(utility_fn);
26000b57cec5SDimitry Andric }
26010b57cec5SDimitry Andric 
SettingsInitialize()26020b57cec5SDimitry Andric void Target::SettingsInitialize() { Process::SettingsInitialize(); }
26030b57cec5SDimitry Andric 
SettingsTerminate()26040b57cec5SDimitry Andric void Target::SettingsTerminate() { Process::SettingsTerminate(); }
26050b57cec5SDimitry Andric 
GetDefaultExecutableSearchPaths()26060b57cec5SDimitry Andric FileSpecList Target::GetDefaultExecutableSearchPaths() {
2607349cc55cSDimitry Andric   return Target::GetGlobalProperties().GetExecutableSearchPaths();
26080b57cec5SDimitry Andric }
26090b57cec5SDimitry Andric 
GetDefaultDebugFileSearchPaths()26100b57cec5SDimitry Andric FileSpecList Target::GetDefaultDebugFileSearchPaths() {
2611349cc55cSDimitry Andric   return Target::GetGlobalProperties().GetDebugFileSearchPaths();
26120b57cec5SDimitry Andric }
26130b57cec5SDimitry Andric 
GetDefaultArchitecture()26140b57cec5SDimitry Andric ArchSpec Target::GetDefaultArchitecture() {
2615349cc55cSDimitry Andric   return Target::GetGlobalProperties().GetDefaultArchitecture();
26160b57cec5SDimitry Andric }
26170b57cec5SDimitry Andric 
SetDefaultArchitecture(const ArchSpec & arch)26180b57cec5SDimitry Andric void Target::SetDefaultArchitecture(const ArchSpec &arch) {
261981ad6265SDimitry Andric   LLDB_LOG(GetLog(LLDBLog::Target),
2620349cc55cSDimitry Andric            "setting target's default architecture to  {0} ({1})",
26219dba64beSDimitry Andric            arch.GetArchitectureName(), arch.GetTriple().getTriple());
2622349cc55cSDimitry Andric   Target::GetGlobalProperties().SetDefaultArchitecture(arch);
26230b57cec5SDimitry Andric }
26240b57cec5SDimitry Andric 
SetLabel(llvm::StringRef label)2625fe013be4SDimitry Andric llvm::Error Target::SetLabel(llvm::StringRef label) {
2626fe013be4SDimitry Andric   size_t n = LLDB_INVALID_INDEX32;
2627fe013be4SDimitry Andric   if (llvm::to_integer(label, n))
2628fe013be4SDimitry Andric     return llvm::make_error<llvm::StringError>(
2629fe013be4SDimitry Andric         "Cannot use integer as target label.", llvm::inconvertibleErrorCode());
2630fe013be4SDimitry Andric   TargetList &targets = GetDebugger().GetTargetList();
2631fe013be4SDimitry Andric   for (size_t i = 0; i < targets.GetNumTargets(); i++) {
2632fe013be4SDimitry Andric     TargetSP target_sp = targets.GetTargetAtIndex(i);
2633fe013be4SDimitry Andric     if (target_sp && target_sp->GetLabel() == label) {
2634fe013be4SDimitry Andric         return llvm::make_error<llvm::StringError>(
2635fe013be4SDimitry Andric             llvm::formatv(
2636fe013be4SDimitry Andric                 "Cannot use label '{0}' since it's set in target #{1}.", label,
2637fe013be4SDimitry Andric                 i),
2638fe013be4SDimitry Andric             llvm::inconvertibleErrorCode());
2639fe013be4SDimitry Andric     }
2640fe013be4SDimitry Andric   }
2641fe013be4SDimitry Andric 
2642fe013be4SDimitry Andric   m_label = label.str();
2643fe013be4SDimitry Andric   return llvm::Error::success();
2644fe013be4SDimitry Andric }
2645fe013be4SDimitry Andric 
GetTargetFromContexts(const ExecutionContext * exe_ctx_ptr,const SymbolContext * sc_ptr)26460b57cec5SDimitry Andric Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
26470b57cec5SDimitry Andric                                       const SymbolContext *sc_ptr) {
26480b57cec5SDimitry Andric   // The target can either exist in the "process" of ExecutionContext, or in
26490b57cec5SDimitry Andric   // the "target_sp" member of SymbolContext. This accessor helper function
26500b57cec5SDimitry Andric   // will get the target from one of these locations.
26510b57cec5SDimitry Andric 
26520b57cec5SDimitry Andric   Target *target = nullptr;
26530b57cec5SDimitry Andric   if (sc_ptr != nullptr)
26540b57cec5SDimitry Andric     target = sc_ptr->target_sp.get();
26550b57cec5SDimitry Andric   if (target == nullptr && exe_ctx_ptr)
26560b57cec5SDimitry Andric     target = exe_ctx_ptr->GetTargetPtr();
26570b57cec5SDimitry Andric   return target;
26580b57cec5SDimitry Andric }
26590b57cec5SDimitry Andric 
EvaluateExpression(llvm::StringRef expr,ExecutionContextScope * exe_scope,lldb::ValueObjectSP & result_valobj_sp,const EvaluateExpressionOptions & options,std::string * fixed_expression,ValueObject * ctx_obj)26600b57cec5SDimitry Andric ExpressionResults Target::EvaluateExpression(
26610b57cec5SDimitry Andric     llvm::StringRef expr, ExecutionContextScope *exe_scope,
26620b57cec5SDimitry Andric     lldb::ValueObjectSP &result_valobj_sp,
26630b57cec5SDimitry Andric     const EvaluateExpressionOptions &options, std::string *fixed_expression,
26640b57cec5SDimitry Andric     ValueObject *ctx_obj) {
26650b57cec5SDimitry Andric   result_valobj_sp.reset();
26660b57cec5SDimitry Andric 
26670b57cec5SDimitry Andric   ExpressionResults execution_results = eExpressionSetupError;
26680b57cec5SDimitry Andric 
2669349cc55cSDimitry Andric   if (expr.empty()) {
2670349cc55cSDimitry Andric     m_stats.GetExpressionStats().NotifyFailure();
26710b57cec5SDimitry Andric     return execution_results;
2672349cc55cSDimitry Andric   }
26730b57cec5SDimitry Andric 
26740b57cec5SDimitry Andric   // We shouldn't run stop hooks in expressions.
26750b57cec5SDimitry Andric   bool old_suppress_value = m_suppress_stop_hooks;
26760b57cec5SDimitry Andric   m_suppress_stop_hooks = true;
26770b57cec5SDimitry Andric   auto on_exit = llvm::make_scope_exit([this, old_suppress_value]() {
26789dba64beSDimitry Andric     m_suppress_stop_hooks = old_suppress_value;
26799dba64beSDimitry Andric   });
26800b57cec5SDimitry Andric 
26810b57cec5SDimitry Andric   ExecutionContext exe_ctx;
26820b57cec5SDimitry Andric 
26830b57cec5SDimitry Andric   if (exe_scope) {
26840b57cec5SDimitry Andric     exe_scope->CalculateExecutionContext(exe_ctx);
26850b57cec5SDimitry Andric   } else if (m_process_sp) {
26860b57cec5SDimitry Andric     m_process_sp->CalculateExecutionContext(exe_ctx);
26870b57cec5SDimitry Andric   } else {
26880b57cec5SDimitry Andric     CalculateExecutionContext(exe_ctx);
26890b57cec5SDimitry Andric   }
26900b57cec5SDimitry Andric 
26910b57cec5SDimitry Andric   // Make sure we aren't just trying to see the value of a persistent variable
26920b57cec5SDimitry Andric   // (something like "$0")
26930b57cec5SDimitry Andric   // Only check for persistent variables the expression starts with a '$'
26949dba64beSDimitry Andric   lldb::ExpressionVariableSP persistent_var_sp;
26959dba64beSDimitry Andric   if (expr[0] == '$') {
26969dba64beSDimitry Andric     auto type_system_or_err =
26979dba64beSDimitry Andric             GetScratchTypeSystemForLanguage(eLanguageTypeC);
26989dba64beSDimitry Andric     if (auto err = type_system_or_err.takeError()) {
269981ad6265SDimitry Andric       LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err),
270081ad6265SDimitry Andric                      "Unable to get scratch type system");
27019dba64beSDimitry Andric     } else {
2702bdd1243dSDimitry Andric       auto ts = *type_system_or_err;
2703bdd1243dSDimitry Andric       if (!ts)
2704bdd1243dSDimitry Andric         LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err),
2705fe013be4SDimitry Andric                        "Scratch type system is no longer live: {0}");
2706bdd1243dSDimitry Andric       else
27079dba64beSDimitry Andric         persistent_var_sp =
2708bdd1243dSDimitry Andric             ts->GetPersistentExpressionState()->GetVariable(expr);
27099dba64beSDimitry Andric     }
27109dba64beSDimitry Andric   }
27110b57cec5SDimitry Andric   if (persistent_var_sp) {
27120b57cec5SDimitry Andric     result_valobj_sp = persistent_var_sp->GetValueObject();
27130b57cec5SDimitry Andric     execution_results = eExpressionCompleted;
27140b57cec5SDimitry Andric   } else {
27150b57cec5SDimitry Andric     llvm::StringRef prefix = GetExpressionPrefixContents();
27160b57cec5SDimitry Andric     Status error;
27175ffd83dbSDimitry Andric     execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix,
27185ffd83dbSDimitry Andric                                                  result_valobj_sp, error,
27195ffd83dbSDimitry Andric                                                  fixed_expression, ctx_obj);
2720bdd1243dSDimitry Andric     // Pass up the error by wrapping it inside an error result.
2721bdd1243dSDimitry Andric     if (error.Fail() && !result_valobj_sp)
2722bdd1243dSDimitry Andric       result_valobj_sp = ValueObjectConstResult::Create(
2723bdd1243dSDimitry Andric           exe_ctx.GetBestExecutionContextScope(), error);
27240b57cec5SDimitry Andric   }
27250b57cec5SDimitry Andric 
2726349cc55cSDimitry Andric   if (execution_results == eExpressionCompleted)
2727349cc55cSDimitry Andric     m_stats.GetExpressionStats().NotifySuccess();
2728349cc55cSDimitry Andric   else
2729349cc55cSDimitry Andric     m_stats.GetExpressionStats().NotifyFailure();
27300b57cec5SDimitry Andric   return execution_results;
27310b57cec5SDimitry Andric }
27320b57cec5SDimitry Andric 
GetPersistentVariable(ConstString name)27339dba64beSDimitry Andric lldb::ExpressionVariableSP Target::GetPersistentVariable(ConstString name) {
27340b57cec5SDimitry Andric   lldb::ExpressionVariableSP variable_sp;
27350b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
2736bdd1243dSDimitry Andric       [name, &variable_sp](TypeSystemSP type_system) -> bool {
2737bdd1243dSDimitry Andric         auto ts = type_system.get();
2738bdd1243dSDimitry Andric         if (!ts)
2739bdd1243dSDimitry Andric           return true;
27400b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
2741bdd1243dSDimitry Andric                 ts->GetPersistentExpressionState()) {
27420b57cec5SDimitry Andric           variable_sp = persistent_state->GetVariable(name);
27430b57cec5SDimitry Andric 
27440b57cec5SDimitry Andric           if (variable_sp)
27450b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
27460b57cec5SDimitry Andric         }
27470b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
27480b57cec5SDimitry Andric       });
27490b57cec5SDimitry Andric   return variable_sp;
27500b57cec5SDimitry Andric }
27510b57cec5SDimitry Andric 
GetPersistentSymbol(ConstString name)27520b57cec5SDimitry Andric lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
27530b57cec5SDimitry Andric   lldb::addr_t address = LLDB_INVALID_ADDRESS;
27540b57cec5SDimitry Andric 
27550b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
2756bdd1243dSDimitry Andric       [name, &address](lldb::TypeSystemSP type_system) -> bool {
2757bdd1243dSDimitry Andric         auto ts = type_system.get();
2758bdd1243dSDimitry Andric         if (!ts)
2759bdd1243dSDimitry Andric           return true;
2760bdd1243dSDimitry Andric 
27610b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
2762bdd1243dSDimitry Andric                 ts->GetPersistentExpressionState()) {
27630b57cec5SDimitry Andric           address = persistent_state->LookupSymbol(name);
27640b57cec5SDimitry Andric           if (address != LLDB_INVALID_ADDRESS)
27650b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
27660b57cec5SDimitry Andric         }
27670b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
27680b57cec5SDimitry Andric       });
27690b57cec5SDimitry Andric   return address;
27700b57cec5SDimitry Andric }
27710b57cec5SDimitry Andric 
GetEntryPointAddress()27729dba64beSDimitry Andric llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
27739dba64beSDimitry Andric   Module *exe_module = GetExecutableModulePointer();
27749dba64beSDimitry Andric 
27751e991466SDimitry Andric   // Try to find the entry point address in the primary executable.
27761e991466SDimitry Andric   const bool has_primary_executable = exe_module && exe_module->GetObjectFile();
27771e991466SDimitry Andric   if (has_primary_executable) {
27789dba64beSDimitry Andric     Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
27799dba64beSDimitry Andric     if (entry_addr.IsValid())
27809dba64beSDimitry Andric       return entry_addr;
27819dba64beSDimitry Andric   }
27829dba64beSDimitry Andric 
27839dba64beSDimitry Andric   const ModuleList &modules = GetImages();
27849dba64beSDimitry Andric   const size_t num_images = modules.GetSize();
27859dba64beSDimitry Andric   for (size_t idx = 0; idx < num_images; ++idx) {
27869dba64beSDimitry Andric     ModuleSP module_sp(modules.GetModuleAtIndex(idx));
27879dba64beSDimitry Andric     if (!module_sp || !module_sp->GetObjectFile())
27889dba64beSDimitry Andric       continue;
27899dba64beSDimitry Andric 
27909dba64beSDimitry Andric     Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
27911e991466SDimitry Andric     if (entry_addr.IsValid())
27929dba64beSDimitry Andric       return entry_addr;
27939dba64beSDimitry Andric   }
27949dba64beSDimitry Andric 
27951e991466SDimitry Andric   // We haven't found the entry point address. Return an appropriate error.
27961e991466SDimitry Andric   if (!has_primary_executable)
27971e991466SDimitry Andric     return llvm::make_error<llvm::StringError>(
27981e991466SDimitry Andric         "No primary executable found and could not find entry point address in "
27991e991466SDimitry Andric         "any executable module",
28001e991466SDimitry Andric         llvm::inconvertibleErrorCode());
28011e991466SDimitry Andric 
28021e991466SDimitry Andric   return llvm::make_error<llvm::StringError>(
28031e991466SDimitry Andric       "Could not find entry point address for primary executable module \"" +
28041e991466SDimitry Andric           exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"",
28051e991466SDimitry Andric       llvm::inconvertibleErrorCode());
28069dba64beSDimitry Andric }
28079dba64beSDimitry Andric 
GetCallableLoadAddress(lldb::addr_t load_addr,AddressClass addr_class) const28080b57cec5SDimitry Andric lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
28090b57cec5SDimitry Andric                                             AddressClass addr_class) const {
28100b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
28119dba64beSDimitry Andric   return arch_plugin
28129dba64beSDimitry Andric              ? arch_plugin->GetCallableLoadAddress(load_addr, addr_class)
28139dba64beSDimitry Andric              : load_addr;
28140b57cec5SDimitry Andric }
28150b57cec5SDimitry Andric 
GetOpcodeLoadAddress(lldb::addr_t load_addr,AddressClass addr_class) const28160b57cec5SDimitry Andric lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr,
28170b57cec5SDimitry Andric                                           AddressClass addr_class) const {
28180b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
28199dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetOpcodeLoadAddress(load_addr, addr_class)
28209dba64beSDimitry Andric                      : load_addr;
28210b57cec5SDimitry Andric }
28220b57cec5SDimitry Andric 
GetBreakableLoadAddress(lldb::addr_t addr)28230b57cec5SDimitry Andric lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
28240b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
28259dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
28260b57cec5SDimitry Andric }
28270b57cec5SDimitry Andric 
GetSourceManager()28280b57cec5SDimitry Andric SourceManager &Target::GetSourceManager() {
28290b57cec5SDimitry Andric   if (!m_source_manager_up)
28305ffd83dbSDimitry Andric     m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
28310b57cec5SDimitry Andric   return *m_source_manager_up;
28320b57cec5SDimitry Andric }
28330b57cec5SDimitry Andric 
CreateStopHook(StopHook::StopHookKind kind)2834e8d8bef9SDimitry Andric Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind) {
28350b57cec5SDimitry Andric   lldb::user_id_t new_uid = ++m_stop_hook_next_id;
2836e8d8bef9SDimitry Andric   Target::StopHookSP stop_hook_sp;
2837e8d8bef9SDimitry Andric   switch (kind) {
2838e8d8bef9SDimitry Andric   case StopHook::StopHookKind::CommandBased:
2839e8d8bef9SDimitry Andric     stop_hook_sp.reset(new StopHookCommandLine(shared_from_this(), new_uid));
2840e8d8bef9SDimitry Andric     break;
2841e8d8bef9SDimitry Andric   case StopHook::StopHookKind::ScriptBased:
2842e8d8bef9SDimitry Andric     stop_hook_sp.reset(new StopHookScripted(shared_from_this(), new_uid));
2843e8d8bef9SDimitry Andric     break;
2844e8d8bef9SDimitry Andric   }
28450b57cec5SDimitry Andric   m_stop_hooks[new_uid] = stop_hook_sp;
28460b57cec5SDimitry Andric   return stop_hook_sp;
28470b57cec5SDimitry Andric }
28480b57cec5SDimitry Andric 
UndoCreateStopHook(lldb::user_id_t user_id)2849e8d8bef9SDimitry Andric void Target::UndoCreateStopHook(lldb::user_id_t user_id) {
2850e8d8bef9SDimitry Andric   if (!RemoveStopHookByID(user_id))
2851e8d8bef9SDimitry Andric     return;
2852e8d8bef9SDimitry Andric   if (user_id == m_stop_hook_next_id)
2853e8d8bef9SDimitry Andric     m_stop_hook_next_id--;
2854e8d8bef9SDimitry Andric }
2855e8d8bef9SDimitry Andric 
RemoveStopHookByID(lldb::user_id_t user_id)28560b57cec5SDimitry Andric bool Target::RemoveStopHookByID(lldb::user_id_t user_id) {
28570b57cec5SDimitry Andric   size_t num_removed = m_stop_hooks.erase(user_id);
28580b57cec5SDimitry Andric   return (num_removed != 0);
28590b57cec5SDimitry Andric }
28600b57cec5SDimitry Andric 
RemoveAllStopHooks()28610b57cec5SDimitry Andric void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); }
28620b57cec5SDimitry Andric 
GetStopHookByID(lldb::user_id_t user_id)28630b57cec5SDimitry Andric Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) {
28640b57cec5SDimitry Andric   StopHookSP found_hook;
28650b57cec5SDimitry Andric 
28660b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
28670b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
28680b57cec5SDimitry Andric   if (specified_hook_iter != m_stop_hooks.end())
28690b57cec5SDimitry Andric     found_hook = (*specified_hook_iter).second;
28700b57cec5SDimitry Andric   return found_hook;
28710b57cec5SDimitry Andric }
28720b57cec5SDimitry Andric 
SetStopHookActiveStateByID(lldb::user_id_t user_id,bool active_state)28730b57cec5SDimitry Andric bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id,
28740b57cec5SDimitry Andric                                         bool active_state) {
28750b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
28760b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
28770b57cec5SDimitry Andric   if (specified_hook_iter == m_stop_hooks.end())
28780b57cec5SDimitry Andric     return false;
28790b57cec5SDimitry Andric 
28800b57cec5SDimitry Andric   (*specified_hook_iter).second->SetIsActive(active_state);
28810b57cec5SDimitry Andric   return true;
28820b57cec5SDimitry Andric }
28830b57cec5SDimitry Andric 
SetAllStopHooksActiveState(bool active_state)28840b57cec5SDimitry Andric void Target::SetAllStopHooksActiveState(bool active_state) {
28850b57cec5SDimitry Andric   StopHookCollection::iterator pos, end = m_stop_hooks.end();
28860b57cec5SDimitry Andric   for (pos = m_stop_hooks.begin(); pos != end; pos++) {
28870b57cec5SDimitry Andric     (*pos).second->SetIsActive(active_state);
28880b57cec5SDimitry Andric   }
28890b57cec5SDimitry Andric }
28900b57cec5SDimitry Andric 
RunStopHooks()2891e8d8bef9SDimitry Andric bool Target::RunStopHooks() {
28920b57cec5SDimitry Andric   if (m_suppress_stop_hooks)
2893e8d8bef9SDimitry Andric     return false;
28940b57cec5SDimitry Andric 
28950b57cec5SDimitry Andric   if (!m_process_sp)
2896e8d8bef9SDimitry Andric     return false;
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   // Somebody might have restarted the process:
2899e8d8bef9SDimitry Andric   // Still return false, the return value is about US restarting the target.
29000b57cec5SDimitry Andric   if (m_process_sp->GetState() != eStateStopped)
2901e8d8bef9SDimitry Andric     return false;
29020b57cec5SDimitry Andric 
29030b57cec5SDimitry Andric   if (m_stop_hooks.empty())
2904e8d8bef9SDimitry Andric     return false;
29050b57cec5SDimitry Andric 
29060b57cec5SDimitry Andric   // If there aren't any active stop hooks, don't bother either.
29070b57cec5SDimitry Andric   bool any_active_hooks = false;
29080b57cec5SDimitry Andric   for (auto hook : m_stop_hooks) {
29090b57cec5SDimitry Andric     if (hook.second->IsActive()) {
29100b57cec5SDimitry Andric       any_active_hooks = true;
2911e8d8bef9SDimitry Andric       break;
29120b57cec5SDimitry Andric     }
29130b57cec5SDimitry Andric   }
29140b57cec5SDimitry Andric   if (!any_active_hooks)
2915e8d8bef9SDimitry Andric     return false;
29160b57cec5SDimitry Andric 
2917c9157d92SDimitry Andric   // Make sure we check that we are not stopped because of us running a user
2918c9157d92SDimitry Andric   // expression since in that case we do not want to run the stop-hooks. Note,
2919c9157d92SDimitry Andric   // you can't just check whether the last stop was for a User Expression,
2920c9157d92SDimitry Andric   // because breakpoint commands get run before stop hooks, and one of them
2921c9157d92SDimitry Andric   // might have run an expression. You have to ensure you run the stop hooks
2922c9157d92SDimitry Andric   // once per natural stop.
2923fe6060f1SDimitry Andric   uint32_t last_natural_stop = m_process_sp->GetModIDRef().GetLastNaturalStopID();
2924fe6060f1SDimitry Andric   if (last_natural_stop != 0 && m_latest_stop_hook_id == last_natural_stop)
2925fe6060f1SDimitry Andric     return false;
2926fe6060f1SDimitry Andric 
2927fe6060f1SDimitry Andric   m_latest_stop_hook_id = last_natural_stop;
2928fe6060f1SDimitry Andric 
29290b57cec5SDimitry Andric   std::vector<ExecutionContext> exc_ctx_with_reasons;
29300b57cec5SDimitry Andric 
29310b57cec5SDimitry Andric   ThreadList &cur_threadlist = m_process_sp->GetThreadList();
29320b57cec5SDimitry Andric   size_t num_threads = cur_threadlist.GetSize();
29330b57cec5SDimitry Andric   for (size_t i = 0; i < num_threads; i++) {
29340b57cec5SDimitry Andric     lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(i);
29350b57cec5SDimitry Andric     if (cur_thread_sp->ThreadStoppedForAReason()) {
29360b57cec5SDimitry Andric       lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2937e8d8bef9SDimitry Andric       exc_ctx_with_reasons.emplace_back(m_process_sp.get(), cur_thread_sp.get(),
2938e8d8bef9SDimitry Andric                                         cur_frame_sp.get());
29390b57cec5SDimitry Andric     }
29400b57cec5SDimitry Andric   }
29410b57cec5SDimitry Andric 
29420b57cec5SDimitry Andric   // If no threads stopped for a reason, don't run the stop-hooks.
29430b57cec5SDimitry Andric   size_t num_exe_ctx = exc_ctx_with_reasons.size();
29440b57cec5SDimitry Andric   if (num_exe_ctx == 0)
2945e8d8bef9SDimitry Andric     return false;
29460b57cec5SDimitry Andric 
2947e8d8bef9SDimitry Andric   StreamSP output_sp = m_debugger.GetAsyncOutputStream();
29480b57cec5SDimitry Andric 
2949e8d8bef9SDimitry Andric   bool auto_continue = false;
29500b57cec5SDimitry Andric   bool hooks_ran = false;
29510b57cec5SDimitry Andric   bool print_hook_header = (m_stop_hooks.size() != 1);
29520b57cec5SDimitry Andric   bool print_thread_header = (num_exe_ctx != 1);
2953e8d8bef9SDimitry Andric   bool should_stop = false;
2954e8d8bef9SDimitry Andric   bool somebody_restarted = false;
29550b57cec5SDimitry Andric 
2956e8d8bef9SDimitry Andric   for (auto stop_entry : m_stop_hooks) {
2957e8d8bef9SDimitry Andric     StopHookSP cur_hook_sp = stop_entry.second;
29580b57cec5SDimitry Andric     if (!cur_hook_sp->IsActive())
29590b57cec5SDimitry Andric       continue;
29600b57cec5SDimitry Andric 
29610b57cec5SDimitry Andric     bool any_thread_matched = false;
2962e8d8bef9SDimitry Andric     for (auto exc_ctx : exc_ctx_with_reasons) {
2963e8d8bef9SDimitry Andric       // We detect somebody restarted in the stop-hook loop, and broke out of
2964e8d8bef9SDimitry Andric       // that loop back to here.  So break out of here too.
2965e8d8bef9SDimitry Andric       if (somebody_restarted)
2966e8d8bef9SDimitry Andric         break;
2967e8d8bef9SDimitry Andric 
2968e8d8bef9SDimitry Andric       if (!cur_hook_sp->ExecutionContextPasses(exc_ctx))
2969e8d8bef9SDimitry Andric         continue;
2970e8d8bef9SDimitry Andric 
2971e8d8bef9SDimitry Andric       // We only consult the auto-continue for a stop hook if it matched the
2972e8d8bef9SDimitry Andric       // specifier.
2973e8d8bef9SDimitry Andric       auto_continue |= cur_hook_sp->GetAutoContinue();
2974e8d8bef9SDimitry Andric 
2975e8d8bef9SDimitry Andric       if (!hooks_ran)
29760b57cec5SDimitry Andric         hooks_ran = true;
2977e8d8bef9SDimitry Andric 
29780b57cec5SDimitry Andric       if (print_hook_header && !any_thread_matched) {
2979e8d8bef9SDimitry Andric         StreamString s;
2980fe013be4SDimitry Andric         cur_hook_sp->GetDescription(s, eDescriptionLevelBrief);
2981e8d8bef9SDimitry Andric         if (s.GetSize() != 0)
2982e8d8bef9SDimitry Andric           output_sp->Printf("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(),
2983e8d8bef9SDimitry Andric                             s.GetData());
29840b57cec5SDimitry Andric         else
2985e8d8bef9SDimitry Andric           output_sp->Printf("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
29860b57cec5SDimitry Andric         any_thread_matched = true;
29870b57cec5SDimitry Andric       }
29880b57cec5SDimitry Andric 
29890b57cec5SDimitry Andric       if (print_thread_header)
2990e8d8bef9SDimitry Andric         output_sp->Printf("-- Thread %d\n",
2991e8d8bef9SDimitry Andric                           exc_ctx.GetThreadPtr()->GetIndexID());
29920b57cec5SDimitry Andric 
2993e8d8bef9SDimitry Andric       StopHook::StopHookResult this_result =
2994e8d8bef9SDimitry Andric           cur_hook_sp->HandleStop(exc_ctx, output_sp);
2995e8d8bef9SDimitry Andric       bool this_should_stop = true;
29960b57cec5SDimitry Andric 
2997e8d8bef9SDimitry Andric       switch (this_result) {
2998e8d8bef9SDimitry Andric       case StopHook::StopHookResult::KeepStopped:
2999e8d8bef9SDimitry Andric         // If this hook is set to auto-continue that should override the
3000e8d8bef9SDimitry Andric         // HandleStop result...
3001e8d8bef9SDimitry Andric         if (cur_hook_sp->GetAutoContinue())
3002e8d8bef9SDimitry Andric           this_should_stop = false;
3003e8d8bef9SDimitry Andric         else
3004e8d8bef9SDimitry Andric           this_should_stop = true;
3005e8d8bef9SDimitry Andric 
3006e8d8bef9SDimitry Andric         break;
3007e8d8bef9SDimitry Andric       case StopHook::StopHookResult::RequestContinue:
3008e8d8bef9SDimitry Andric         this_should_stop = false;
3009e8d8bef9SDimitry Andric         break;
3010e8d8bef9SDimitry Andric       case StopHook::StopHookResult::AlreadyContinued:
3011e8d8bef9SDimitry Andric         // We don't have a good way to prohibit people from restarting the
3012e8d8bef9SDimitry Andric         // target willy nilly in a stop hook.  If the hook did so, give a
3013e8d8bef9SDimitry Andric         // gentle suggestion here and bag out if the hook processing.
3014e8d8bef9SDimitry Andric         output_sp->Printf("\nAborting stop hooks, hook %" PRIu64
30150b57cec5SDimitry Andric                           " set the program running.\n"
30160b57cec5SDimitry Andric                           "  Consider using '-G true' to make "
30170b57cec5SDimitry Andric                           "stop hooks auto-continue.\n",
30180b57cec5SDimitry Andric                           cur_hook_sp->GetID());
3019e8d8bef9SDimitry Andric         somebody_restarted = true;
3020e8d8bef9SDimitry Andric         break;
30210b57cec5SDimitry Andric       }
3022e8d8bef9SDimitry Andric       // If we're already restarted, stop processing stop hooks.
3023e8d8bef9SDimitry Andric       // FIXME: if we are doing non-stop mode for real, we would have to
3024e8d8bef9SDimitry Andric       // check that OUR thread was restarted, otherwise we should keep
3025e8d8bef9SDimitry Andric       // processing stop hooks.
3026e8d8bef9SDimitry Andric       if (somebody_restarted)
3027e8d8bef9SDimitry Andric         break;
30280b57cec5SDimitry Andric 
3029e8d8bef9SDimitry Andric       // If anybody wanted to stop, we should all stop.
3030e8d8bef9SDimitry Andric       if (!should_stop)
3031e8d8bef9SDimitry Andric         should_stop = this_should_stop;
3032e8d8bef9SDimitry Andric     }
3033e8d8bef9SDimitry Andric   }
3034e8d8bef9SDimitry Andric 
3035e8d8bef9SDimitry Andric   output_sp->Flush();
3036e8d8bef9SDimitry Andric 
3037e8d8bef9SDimitry Andric   // If one of the commands in the stop hook already restarted the target,
3038e8d8bef9SDimitry Andric   // report that fact.
3039e8d8bef9SDimitry Andric   if (somebody_restarted)
3040e8d8bef9SDimitry Andric     return true;
3041e8d8bef9SDimitry Andric 
3042e8d8bef9SDimitry Andric   // Finally, if auto-continue was requested, do it now:
3043e8d8bef9SDimitry Andric   // We only compute should_stop against the hook results if a hook got to run
3044e8d8bef9SDimitry Andric   // which is why we have to do this conjoint test.
3045e8d8bef9SDimitry Andric   if ((hooks_ran && !should_stop) || auto_continue) {
304681ad6265SDimitry Andric     Log *log = GetLog(LLDBLog::Process);
3047e8d8bef9SDimitry Andric     Status error = m_process_sp->PrivateResume();
3048e8d8bef9SDimitry Andric     if (error.Success()) {
3049e8d8bef9SDimitry Andric       LLDB_LOG(log, "Resuming from RunStopHooks");
3050e8d8bef9SDimitry Andric       return true;
3051e8d8bef9SDimitry Andric     } else {
3052e8d8bef9SDimitry Andric       LLDB_LOG(log, "Resuming from RunStopHooks failed: {0}", error);
3053e8d8bef9SDimitry Andric       return false;
3054e8d8bef9SDimitry Andric     }
3055e8d8bef9SDimitry Andric   }
3056e8d8bef9SDimitry Andric 
3057e8d8bef9SDimitry Andric   return false;
30580b57cec5SDimitry Andric }
30590b57cec5SDimitry Andric 
GetGlobalProperties()3060349cc55cSDimitry Andric TargetProperties &Target::GetGlobalProperties() {
30610b57cec5SDimitry Andric   // NOTE: intentional leak so we don't crash if global destructor chain gets
30620b57cec5SDimitry Andric   // called as other threads still use the result of this function
3063349cc55cSDimitry Andric   static TargetProperties *g_settings_ptr =
3064349cc55cSDimitry Andric       new TargetProperties(nullptr);
3065349cc55cSDimitry Andric   return *g_settings_ptr;
30660b57cec5SDimitry Andric }
30670b57cec5SDimitry Andric 
Install(ProcessLaunchInfo * launch_info)30680b57cec5SDimitry Andric Status Target::Install(ProcessLaunchInfo *launch_info) {
30690b57cec5SDimitry Andric   Status error;
30700b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
30710b57cec5SDimitry Andric   if (platform_sp) {
30720b57cec5SDimitry Andric     if (platform_sp->IsRemote()) {
30730b57cec5SDimitry Andric       if (platform_sp->IsConnected()) {
30745ffd83dbSDimitry Andric         // Install all files that have an install path when connected to a
30755ffd83dbSDimitry Andric         // remote platform. If target.auto-install-main-executable is set then
30765ffd83dbSDimitry Andric         // also install the main executable even if it does not have an explicit
30775ffd83dbSDimitry Andric         // install path specified.
30780b57cec5SDimitry Andric         const ModuleList &modules = GetImages();
30790b57cec5SDimitry Andric         const size_t num_images = modules.GetSize();
30800b57cec5SDimitry Andric         for (size_t idx = 0; idx < num_images; ++idx) {
30810b57cec5SDimitry Andric           ModuleSP module_sp(modules.GetModuleAtIndex(idx));
30820b57cec5SDimitry Andric           if (module_sp) {
30830b57cec5SDimitry Andric             const bool is_main_executable = module_sp == GetExecutableModule();
30840b57cec5SDimitry Andric             FileSpec local_file(module_sp->GetFileSpec());
30850b57cec5SDimitry Andric             if (local_file) {
30860b57cec5SDimitry Andric               FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
30870b57cec5SDimitry Andric               if (!remote_file) {
30885ffd83dbSDimitry Andric                 if (is_main_executable && GetAutoInstallMainExecutable()) {
30895ffd83dbSDimitry Andric                   // Automatically install the main executable.
30900b57cec5SDimitry Andric                   remote_file = platform_sp->GetRemoteWorkingDirectory();
30910b57cec5SDimitry Andric                   remote_file.AppendPathComponent(
30920b57cec5SDimitry Andric                       module_sp->GetFileSpec().GetFilename().GetCString());
30930b57cec5SDimitry Andric                 }
30940b57cec5SDimitry Andric               }
30950b57cec5SDimitry Andric               if (remote_file) {
30960b57cec5SDimitry Andric                 error = platform_sp->Install(local_file, remote_file);
30970b57cec5SDimitry Andric                 if (error.Success()) {
30980b57cec5SDimitry Andric                   module_sp->SetPlatformFileSpec(remote_file);
30990b57cec5SDimitry Andric                   if (is_main_executable) {
31000b57cec5SDimitry Andric                     platform_sp->SetFilePermissions(remote_file, 0700);
31010b57cec5SDimitry Andric                     if (launch_info)
31020b57cec5SDimitry Andric                       launch_info->SetExecutableFile(remote_file, false);
31030b57cec5SDimitry Andric                   }
31040b57cec5SDimitry Andric                 } else
31050b57cec5SDimitry Andric                   break;
31060b57cec5SDimitry Andric               }
31070b57cec5SDimitry Andric             }
31080b57cec5SDimitry Andric           }
31090b57cec5SDimitry Andric         }
31100b57cec5SDimitry Andric       }
31110b57cec5SDimitry Andric     }
31120b57cec5SDimitry Andric   }
31130b57cec5SDimitry Andric   return error;
31140b57cec5SDimitry Andric }
31150b57cec5SDimitry Andric 
ResolveLoadAddress(addr_t load_addr,Address & so_addr,uint32_t stop_id)31160b57cec5SDimitry Andric bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
31170b57cec5SDimitry Andric                                 uint32_t stop_id) {
31180b57cec5SDimitry Andric   return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
31190b57cec5SDimitry Andric }
31200b57cec5SDimitry Andric 
ResolveFileAddress(lldb::addr_t file_addr,Address & resolved_addr)31210b57cec5SDimitry Andric bool Target::ResolveFileAddress(lldb::addr_t file_addr,
31220b57cec5SDimitry Andric                                 Address &resolved_addr) {
31230b57cec5SDimitry Andric   return m_images.ResolveFileAddress(file_addr, resolved_addr);
31240b57cec5SDimitry Andric }
31250b57cec5SDimitry Andric 
SetSectionLoadAddress(const SectionSP & section_sp,addr_t new_section_load_addr,bool warn_multiple)31260b57cec5SDimitry Andric bool Target::SetSectionLoadAddress(const SectionSP &section_sp,
31270b57cec5SDimitry Andric                                    addr_t new_section_load_addr,
31280b57cec5SDimitry Andric                                    bool warn_multiple) {
31290b57cec5SDimitry Andric   const addr_t old_section_load_addr =
31300b57cec5SDimitry Andric       m_section_load_history.GetSectionLoadAddress(
31310b57cec5SDimitry Andric           SectionLoadHistory::eStopIDNow, section_sp);
31320b57cec5SDimitry Andric   if (old_section_load_addr != new_section_load_addr) {
31330b57cec5SDimitry Andric     uint32_t stop_id = 0;
31340b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
31350b57cec5SDimitry Andric     if (process_sp)
31360b57cec5SDimitry Andric       stop_id = process_sp->GetStopID();
31370b57cec5SDimitry Andric     else
31380b57cec5SDimitry Andric       stop_id = m_section_load_history.GetLastStopID();
31390b57cec5SDimitry Andric     if (m_section_load_history.SetSectionLoadAddress(
31400b57cec5SDimitry Andric             stop_id, section_sp, new_section_load_addr, warn_multiple))
31410b57cec5SDimitry Andric       return true; // Return true if the section load address was changed...
31420b57cec5SDimitry Andric   }
31430b57cec5SDimitry Andric   return false; // Return false to indicate nothing changed
31440b57cec5SDimitry Andric }
31450b57cec5SDimitry Andric 
UnloadModuleSections(const ModuleList & module_list)31460b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const ModuleList &module_list) {
31470b57cec5SDimitry Andric   size_t section_unload_count = 0;
31480b57cec5SDimitry Andric   size_t num_modules = module_list.GetSize();
31490b57cec5SDimitry Andric   for (size_t i = 0; i < num_modules; ++i) {
31500b57cec5SDimitry Andric     section_unload_count +=
31510b57cec5SDimitry Andric         UnloadModuleSections(module_list.GetModuleAtIndex(i));
31520b57cec5SDimitry Andric   }
31530b57cec5SDimitry Andric   return section_unload_count;
31540b57cec5SDimitry Andric }
31550b57cec5SDimitry Andric 
UnloadModuleSections(const lldb::ModuleSP & module_sp)31560b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) {
31570b57cec5SDimitry Andric   uint32_t stop_id = 0;
31580b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
31590b57cec5SDimitry Andric   if (process_sp)
31600b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
31610b57cec5SDimitry Andric   else
31620b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
31630b57cec5SDimitry Andric   SectionList *sections = module_sp->GetSectionList();
31640b57cec5SDimitry Andric   size_t section_unload_count = 0;
31650b57cec5SDimitry Andric   if (sections) {
31660b57cec5SDimitry Andric     const uint32_t num_sections = sections->GetNumSections(0);
31670b57cec5SDimitry Andric     for (uint32_t i = 0; i < num_sections; ++i) {
31680b57cec5SDimitry Andric       section_unload_count += m_section_load_history.SetSectionUnloaded(
31690b57cec5SDimitry Andric           stop_id, sections->GetSectionAtIndex(i));
31700b57cec5SDimitry Andric     }
31710b57cec5SDimitry Andric   }
31720b57cec5SDimitry Andric   return section_unload_count;
31730b57cec5SDimitry Andric }
31740b57cec5SDimitry Andric 
SetSectionUnloaded(const lldb::SectionSP & section_sp)31750b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
31760b57cec5SDimitry Andric   uint32_t stop_id = 0;
31770b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
31780b57cec5SDimitry Andric   if (process_sp)
31790b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
31800b57cec5SDimitry Andric   else
31810b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
31820b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp);
31830b57cec5SDimitry Andric }
31840b57cec5SDimitry Andric 
SetSectionUnloaded(const lldb::SectionSP & section_sp,addr_t load_addr)31850b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp,
31860b57cec5SDimitry Andric                                 addr_t load_addr) {
31870b57cec5SDimitry Andric   uint32_t stop_id = 0;
31880b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
31890b57cec5SDimitry Andric   if (process_sp)
31900b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
31910b57cec5SDimitry Andric   else
31920b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
31930b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp,
31940b57cec5SDimitry Andric                                                    load_addr);
31950b57cec5SDimitry Andric }
31960b57cec5SDimitry Andric 
ClearAllLoadedSections()31970b57cec5SDimitry Andric void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
31980b57cec5SDimitry Andric 
SaveScriptedLaunchInfo(lldb_private::ProcessInfo & process_info)3199fe013be4SDimitry Andric void Target::SaveScriptedLaunchInfo(lldb_private::ProcessInfo &process_info) {
3200fe013be4SDimitry Andric   if (process_info.IsScriptedProcess()) {
3201fe013be4SDimitry Andric     // Only copy scripted process launch options.
3202fe013be4SDimitry Andric     ProcessLaunchInfo &default_launch_info = const_cast<ProcessLaunchInfo &>(
3203fe013be4SDimitry Andric         GetGlobalProperties().GetProcessLaunchInfo());
3204fe013be4SDimitry Andric     default_launch_info.SetProcessPluginName("ScriptedProcess");
3205fe013be4SDimitry Andric     default_launch_info.SetScriptedMetadata(process_info.GetScriptedMetadata());
3206fe013be4SDimitry Andric     SetProcessLaunchInfo(default_launch_info);
3207fe013be4SDimitry Andric   }
3208fe013be4SDimitry Andric }
3209fe013be4SDimitry Andric 
Launch(ProcessLaunchInfo & launch_info,Stream * stream)32100b57cec5SDimitry Andric Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
3211349cc55cSDimitry Andric   m_stats.SetLaunchOrAttachTime();
32120b57cec5SDimitry Andric   Status error;
321381ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Target);
32140b57cec5SDimitry Andric 
32159dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s() called for %s", __FUNCTION__,
32160b57cec5SDimitry Andric             launch_info.GetExecutableFile().GetPath().c_str());
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric   StateType state = eStateInvalid;
32190b57cec5SDimitry Andric 
32200b57cec5SDimitry Andric   // Scope to temporarily get the process state in case someone has manually
32210b57cec5SDimitry Andric   // remotely connected already to a process and we can skip the platform
32220b57cec5SDimitry Andric   // launching.
32230b57cec5SDimitry Andric   {
32240b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
32250b57cec5SDimitry Andric 
32260b57cec5SDimitry Andric     if (process_sp) {
32270b57cec5SDimitry Andric       state = process_sp->GetState();
32289dba64beSDimitry Andric       LLDB_LOGF(log,
32290b57cec5SDimitry Andric                 "Target::%s the process exists, and its current state is %s",
32300b57cec5SDimitry Andric                 __FUNCTION__, StateAsCString(state));
32310b57cec5SDimitry Andric     } else {
32329dba64beSDimitry Andric       LLDB_LOGF(log, "Target::%s the process instance doesn't currently exist.",
32330b57cec5SDimitry Andric                 __FUNCTION__);
32340b57cec5SDimitry Andric     }
32350b57cec5SDimitry Andric   }
32360b57cec5SDimitry Andric 
32370b57cec5SDimitry Andric   launch_info.GetFlags().Set(eLaunchFlagDebug);
32380b57cec5SDimitry Andric 
3239fe013be4SDimitry Andric   SaveScriptedLaunchInfo(launch_info);
3240fe6060f1SDimitry Andric 
32410b57cec5SDimitry Andric   // Get the value of synchronous execution here.  If you wait till after you
32420b57cec5SDimitry Andric   // have started to run, then you could have hit a breakpoint, whose command
32430b57cec5SDimitry Andric   // might switch the value, and then you'll pick up that incorrect value.
32440b57cec5SDimitry Andric   Debugger &debugger = GetDebugger();
32450b57cec5SDimitry Andric   const bool synchronous_execution =
32460b57cec5SDimitry Andric       debugger.GetCommandInterpreter().GetSynchronous();
32470b57cec5SDimitry Andric 
32480b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
32490b57cec5SDimitry Andric 
32500b57cec5SDimitry Andric   FinalizeFileActions(launch_info);
32510b57cec5SDimitry Andric 
32520b57cec5SDimitry Andric   if (state == eStateConnected) {
32530b57cec5SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
32540b57cec5SDimitry Andric       error.SetErrorString(
32550b57cec5SDimitry Andric           "can't launch in tty when launching through a remote connection");
32560b57cec5SDimitry Andric       return error;
32570b57cec5SDimitry Andric     }
32580b57cec5SDimitry Andric   }
32590b57cec5SDimitry Andric 
32600b57cec5SDimitry Andric   if (!launch_info.GetArchitecture().IsValid())
32610b57cec5SDimitry Andric     launch_info.GetArchitecture() = GetArchitecture();
32620b57cec5SDimitry Andric 
326381ad6265SDimitry Andric   // Hijacking events of the process to be created to be sure that all events
326481ad6265SDimitry Andric   // until the first stop are intercepted (in case if platform doesn't define
326581ad6265SDimitry Andric   // its own hijacking listener or if the process is created by the target
326681ad6265SDimitry Andric   // manually, without the platform).
326781ad6265SDimitry Andric   if (!launch_info.GetHijackListener())
3268fe013be4SDimitry Andric     launch_info.SetHijackListener(Listener::MakeListener(
3269fe013be4SDimitry Andric         Process::LaunchSynchronousHijackListenerName.data()));
327081ad6265SDimitry Andric 
32710b57cec5SDimitry Andric   // If we're not already connected to the process, and if we have a platform
32720b57cec5SDimitry Andric   // that can launch a process for debugging, go ahead and do that here.
32730b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp &&
3274fe6060f1SDimitry Andric       platform_sp->CanDebugProcess() && !launch_info.IsScriptedProcess()) {
32759dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s asking the platform to debug the process",
32760b57cec5SDimitry Andric               __FUNCTION__);
32770b57cec5SDimitry Andric 
32780b57cec5SDimitry Andric     // If there was a previous process, delete it before we make the new one.
32790b57cec5SDimitry Andric     // One subtle point, we delete the process before we release the reference
32800b57cec5SDimitry Andric     // to m_process_sp.  That way even if we are the last owner, the process
32810b57cec5SDimitry Andric     // will get Finalized before it gets destroyed.
32820b57cec5SDimitry Andric     DeleteCurrentProcess();
32830b57cec5SDimitry Andric 
32840b57cec5SDimitry Andric     m_process_sp =
3285349cc55cSDimitry Andric         GetPlatform()->DebugProcess(launch_info, debugger, *this, error);
32860b57cec5SDimitry Andric 
32870b57cec5SDimitry Andric   } else {
32889dba64beSDimitry Andric     LLDB_LOGF(log,
32899dba64beSDimitry Andric               "Target::%s the platform doesn't know how to debug a "
32900b57cec5SDimitry Andric               "process, getting a process plugin to do this for us.",
32910b57cec5SDimitry Andric               __FUNCTION__);
32920b57cec5SDimitry Andric 
32930b57cec5SDimitry Andric     if (state == eStateConnected) {
32940b57cec5SDimitry Andric       assert(m_process_sp);
32950b57cec5SDimitry Andric     } else {
32960b57cec5SDimitry Andric       // Use a Process plugin to construct the process.
3297fe013be4SDimitry Andric       CreateProcess(launch_info.GetListener(),
3298fe013be4SDimitry Andric                     launch_info.GetProcessPluginName(), nullptr, false);
32990b57cec5SDimitry Andric     }
33000b57cec5SDimitry Andric 
33010b57cec5SDimitry Andric     // Since we didn't have a platform launch the process, launch it here.
330281ad6265SDimitry Andric     if (m_process_sp) {
330381ad6265SDimitry Andric       m_process_sp->HijackProcessEvents(launch_info.GetHijackListener());
3304fe013be4SDimitry Andric       m_process_sp->SetShadowListener(launch_info.GetShadowListener());
33050b57cec5SDimitry Andric       error = m_process_sp->Launch(launch_info);
33060b57cec5SDimitry Andric     }
330781ad6265SDimitry Andric   }
33080b57cec5SDimitry Andric 
3309e8d8bef9SDimitry Andric   if (!m_process_sp && error.Success())
33100b57cec5SDimitry Andric     error.SetErrorString("failed to launch or debug process");
33110b57cec5SDimitry Andric 
3312e8d8bef9SDimitry Andric   if (!error.Success())
3313e8d8bef9SDimitry Andric     return error;
3314e8d8bef9SDimitry Andric 
331581ad6265SDimitry Andric   bool rebroadcast_first_stop =
331681ad6265SDimitry Andric       !synchronous_execution &&
331781ad6265SDimitry Andric       launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
3318e8d8bef9SDimitry Andric 
331981ad6265SDimitry Andric   assert(launch_info.GetHijackListener());
332081ad6265SDimitry Andric 
332181ad6265SDimitry Andric   EventSP first_stop_event_sp;
3322bdd1243dSDimitry Andric   state = m_process_sp->WaitForProcessToStop(std::nullopt, &first_stop_event_sp,
332381ad6265SDimitry Andric                                              rebroadcast_first_stop,
332481ad6265SDimitry Andric                                              launch_info.GetHijackListener());
332581ad6265SDimitry Andric   m_process_sp->RestoreProcessEvents();
332681ad6265SDimitry Andric 
332781ad6265SDimitry Andric   if (rebroadcast_first_stop) {
332881ad6265SDimitry Andric     assert(first_stop_event_sp);
332981ad6265SDimitry Andric     m_process_sp->BroadcastEvent(first_stop_event_sp);
3330e8d8bef9SDimitry Andric     return error;
33310b57cec5SDimitry Andric   }
33320b57cec5SDimitry Andric 
333381ad6265SDimitry Andric   switch (state) {
3334e8d8bef9SDimitry Andric   case eStateStopped: {
3335e8d8bef9SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
3336e8d8bef9SDimitry Andric       break;
333781ad6265SDimitry Andric     if (synchronous_execution)
33389dba64beSDimitry Andric       // Now we have handled the stop-from-attach, and we are just
33399dba64beSDimitry Andric       // switching to a synchronous resume.  So we should switch to the
33409dba64beSDimitry Andric       // SyncResume hijacker.
33410b57cec5SDimitry Andric       m_process_sp->ResumeSynchronous(stream);
334281ad6265SDimitry Andric     else
3343fe013be4SDimitry Andric       error = m_process_sp->Resume();
33440b57cec5SDimitry Andric     if (!error.Success()) {
33450b57cec5SDimitry Andric       Status error2;
33460b57cec5SDimitry Andric       error2.SetErrorStringWithFormat(
33470b57cec5SDimitry Andric           "process resume at entry point failed: %s", error.AsCString());
33480b57cec5SDimitry Andric       error = error2;
33490b57cec5SDimitry Andric     }
3350e8d8bef9SDimitry Andric   } break;
3351e8d8bef9SDimitry Andric   case eStateExited: {
33520b57cec5SDimitry Andric     bool with_shell = !!launch_info.GetShell();
33530b57cec5SDimitry Andric     const int exit_status = m_process_sp->GetExitStatus();
33540b57cec5SDimitry Andric     const char *exit_desc = m_process_sp->GetExitDescription();
3355e8d8bef9SDimitry Andric     std::string desc;
3356e8d8bef9SDimitry Andric     if (exit_desc && exit_desc[0])
3357e8d8bef9SDimitry Andric       desc = " (" + std::string(exit_desc) + ')';
33580b57cec5SDimitry Andric     if (with_shell)
33590b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
3360e8d8bef9SDimitry Andric           "process exited with status %i%s\n"
3361e8d8bef9SDimitry Andric           "'r' and 'run' are aliases that default to launching through a "
3362e8d8bef9SDimitry Andric           "shell.\n"
3363e8d8bef9SDimitry Andric           "Try launching without going through a shell by using "
3364e8d8bef9SDimitry Andric           "'process launch'.",
3365e8d8bef9SDimitry Andric           exit_status, desc.c_str());
33660b57cec5SDimitry Andric     else
3367e8d8bef9SDimitry Andric       error.SetErrorStringWithFormat("process exited with status %i%s",
3368e8d8bef9SDimitry Andric                                      exit_status, desc.c_str());
3369e8d8bef9SDimitry Andric   } break;
3370e8d8bef9SDimitry Andric   default:
3371e8d8bef9SDimitry Andric     error.SetErrorStringWithFormat("initial process state wasn't stopped: %s",
3372e8d8bef9SDimitry Andric                                    StateAsCString(state));
3373e8d8bef9SDimitry Andric     break;
33740b57cec5SDimitry Andric   }
33750b57cec5SDimitry Andric   return error;
33760b57cec5SDimitry Andric }
33770b57cec5SDimitry Andric 
SetTrace(const TraceSP & trace_sp)3378e8d8bef9SDimitry Andric void Target::SetTrace(const TraceSP &trace_sp) { m_trace_sp = trace_sp; }
3379e8d8bef9SDimitry Andric 
GetTrace()3380fe6060f1SDimitry Andric TraceSP Target::GetTrace() { return m_trace_sp; }
3381fe6060f1SDimitry Andric 
CreateTrace()3382fe6060f1SDimitry Andric llvm::Expected<TraceSP> Target::CreateTrace() {
3383fe6060f1SDimitry Andric   if (!m_process_sp)
3384fe6060f1SDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3385fe6060f1SDimitry Andric                                    "A process is required for tracing");
3386fe6060f1SDimitry Andric   if (m_trace_sp)
3387fe6060f1SDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3388fe6060f1SDimitry Andric                                    "A trace already exists for the target");
3389fe6060f1SDimitry Andric 
3390fe6060f1SDimitry Andric   llvm::Expected<TraceSupportedResponse> trace_type =
3391fe6060f1SDimitry Andric       m_process_sp->TraceSupported();
3392fe6060f1SDimitry Andric   if (!trace_type)
3393fe6060f1SDimitry Andric     return llvm::createStringError(
3394fe6060f1SDimitry Andric         llvm::inconvertibleErrorCode(), "Tracing is not supported. %s",
3395fe6060f1SDimitry Andric         llvm::toString(trace_type.takeError()).c_str());
3396fe6060f1SDimitry Andric   if (llvm::Expected<TraceSP> trace_sp =
3397fe6060f1SDimitry Andric           Trace::FindPluginForLiveProcess(trace_type->name, *m_process_sp))
3398fe6060f1SDimitry Andric     m_trace_sp = *trace_sp;
3399fe6060f1SDimitry Andric   else
3400fe6060f1SDimitry Andric     return llvm::createStringError(
3401fe6060f1SDimitry Andric         llvm::inconvertibleErrorCode(),
3402fe6060f1SDimitry Andric         "Couldn't create a Trace object for the process. %s",
3403fe6060f1SDimitry Andric         llvm::toString(trace_sp.takeError()).c_str());
3404fe6060f1SDimitry Andric   return m_trace_sp;
3405fe6060f1SDimitry Andric }
3406fe6060f1SDimitry Andric 
GetTraceOrCreate()3407fe6060f1SDimitry Andric llvm::Expected<TraceSP> Target::GetTraceOrCreate() {
3408fe6060f1SDimitry Andric   if (m_trace_sp)
3409fe6060f1SDimitry Andric     return m_trace_sp;
3410fe6060f1SDimitry Andric   return CreateTrace();
3411fe6060f1SDimitry Andric }
3412e8d8bef9SDimitry Andric 
Attach(ProcessAttachInfo & attach_info,Stream * stream)34130b57cec5SDimitry Andric Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
3414349cc55cSDimitry Andric   m_stats.SetLaunchOrAttachTime();
34150b57cec5SDimitry Andric   auto state = eStateInvalid;
34160b57cec5SDimitry Andric   auto process_sp = GetProcessSP();
34170b57cec5SDimitry Andric   if (process_sp) {
34180b57cec5SDimitry Andric     state = process_sp->GetState();
34190b57cec5SDimitry Andric     if (process_sp->IsAlive() && state != eStateConnected) {
34200b57cec5SDimitry Andric       if (state == eStateAttaching)
34210b57cec5SDimitry Andric         return Status("process attach is in progress");
34220b57cec5SDimitry Andric       return Status("a process is already being debugged");
34230b57cec5SDimitry Andric     }
34240b57cec5SDimitry Andric   }
34250b57cec5SDimitry Andric 
34260b57cec5SDimitry Andric   const ModuleSP old_exec_module_sp = GetExecutableModule();
34270b57cec5SDimitry Andric 
34280b57cec5SDimitry Andric   // If no process info was specified, then use the target executable name as
34290b57cec5SDimitry Andric   // the process to attach to by default
34300b57cec5SDimitry Andric   if (!attach_info.ProcessInfoSpecified()) {
34310b57cec5SDimitry Andric     if (old_exec_module_sp)
3432bdd1243dSDimitry Andric       attach_info.GetExecutableFile().SetFilename(
3433bdd1243dSDimitry Andric             old_exec_module_sp->GetPlatformFileSpec().GetFilename());
34340b57cec5SDimitry Andric 
34350b57cec5SDimitry Andric     if (!attach_info.ProcessInfoSpecified()) {
34360b57cec5SDimitry Andric       return Status("no process specified, create a target with a file, or "
34370b57cec5SDimitry Andric                     "specify the --pid or --name");
34380b57cec5SDimitry Andric     }
34390b57cec5SDimitry Andric   }
34400b57cec5SDimitry Andric 
34410b57cec5SDimitry Andric   const auto platform_sp =
34420b57cec5SDimitry Andric       GetDebugger().GetPlatformList().GetSelectedPlatform();
34430b57cec5SDimitry Andric   ListenerSP hijack_listener_sp;
34440b57cec5SDimitry Andric   const bool async = attach_info.GetAsync();
34450b57cec5SDimitry Andric   if (!async) {
3446fe013be4SDimitry Andric     hijack_listener_sp = Listener::MakeListener(
3447fe013be4SDimitry Andric         Process::AttachSynchronousHijackListenerName.data());
34480b57cec5SDimitry Andric     attach_info.SetHijackListener(hijack_listener_sp);
34490b57cec5SDimitry Andric   }
34500b57cec5SDimitry Andric 
34510b57cec5SDimitry Andric   Status error;
34520b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp != nullptr &&
3453fe013be4SDimitry Andric       platform_sp->CanDebugProcess() && !attach_info.IsScriptedProcess()) {
34540b57cec5SDimitry Andric     SetPlatform(platform_sp);
34550b57cec5SDimitry Andric     process_sp = platform_sp->Attach(attach_info, GetDebugger(), this, error);
34560b57cec5SDimitry Andric   } else {
34570b57cec5SDimitry Andric     if (state != eStateConnected) {
3458fe013be4SDimitry Andric       SaveScriptedLaunchInfo(attach_info);
3459fe013be4SDimitry Andric       llvm::StringRef plugin_name = attach_info.GetProcessPluginName();
34600b57cec5SDimitry Andric       process_sp =
34610b57cec5SDimitry Andric           CreateProcess(attach_info.GetListenerForProcess(GetDebugger()),
3462e8d8bef9SDimitry Andric                         plugin_name, nullptr, false);
3463fe013be4SDimitry Andric       if (!process_sp) {
3464fe013be4SDimitry Andric         error.SetErrorStringWithFormatv(
3465fe013be4SDimitry Andric             "failed to create process using plugin '{0}'",
3466fe013be4SDimitry Andric             plugin_name.empty() ? "<empty>" : plugin_name);
34670b57cec5SDimitry Andric         return error;
34680b57cec5SDimitry Andric       }
34690b57cec5SDimitry Andric     }
34700b57cec5SDimitry Andric     if (hijack_listener_sp)
34710b57cec5SDimitry Andric       process_sp->HijackProcessEvents(hijack_listener_sp);
34720b57cec5SDimitry Andric     error = process_sp->Attach(attach_info);
34730b57cec5SDimitry Andric   }
34740b57cec5SDimitry Andric 
34750b57cec5SDimitry Andric   if (error.Success() && process_sp) {
34760b57cec5SDimitry Andric     if (async) {
34770b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
34780b57cec5SDimitry Andric     } else {
3479fe013be4SDimitry Andric       // We are stopping all the way out to the user, so update selected frames.
3480fe013be4SDimitry Andric       state = process_sp->WaitForProcessToStop(
3481fe013be4SDimitry Andric           std::nullopt, nullptr, false, attach_info.GetHijackListener(), stream,
3482fe013be4SDimitry Andric           true, SelectMostRelevantFrame);
34830b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
34840b57cec5SDimitry Andric 
34850b57cec5SDimitry Andric       if (state != eStateStopped) {
34860b57cec5SDimitry Andric         const char *exit_desc = process_sp->GetExitDescription();
34870b57cec5SDimitry Andric         if (exit_desc)
34880b57cec5SDimitry Andric           error.SetErrorStringWithFormat("%s", exit_desc);
34890b57cec5SDimitry Andric         else
34900b57cec5SDimitry Andric           error.SetErrorString(
34910b57cec5SDimitry Andric               "process did not stop (no such process or permission problem?)");
34920b57cec5SDimitry Andric         process_sp->Destroy(false);
34930b57cec5SDimitry Andric       }
34940b57cec5SDimitry Andric     }
34950b57cec5SDimitry Andric   }
34960b57cec5SDimitry Andric   return error;
34970b57cec5SDimitry Andric }
34980b57cec5SDimitry Andric 
FinalizeFileActions(ProcessLaunchInfo & info)34990b57cec5SDimitry Andric void Target::FinalizeFileActions(ProcessLaunchInfo &info) {
350081ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Process);
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric   // Finalize the file actions, and if none were given, default to opening up a
35030b57cec5SDimitry Andric   // pseudo terminal
35040b57cec5SDimitry Andric   PlatformSP platform_sp = GetPlatform();
35050b57cec5SDimitry Andric   const bool default_to_use_pty =
35060b57cec5SDimitry Andric       m_platform_sp ? m_platform_sp->IsHost() : false;
35070b57cec5SDimitry Andric   LLDB_LOG(
35080b57cec5SDimitry Andric       log,
35090b57cec5SDimitry Andric       "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}",
35100b57cec5SDimitry Andric       bool(platform_sp),
35110b57cec5SDimitry Andric       platform_sp ? (platform_sp->IsHost() ? "true" : "false") : "n/a",
35120b57cec5SDimitry Andric       default_to_use_pty);
35130b57cec5SDimitry Andric 
35140b57cec5SDimitry Andric   // If nothing for stdin or stdout or stderr was specified, then check the
35150b57cec5SDimitry Andric   // process for any default settings that were set with "settings set"
35160b57cec5SDimitry Andric   if (info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
35170b57cec5SDimitry Andric       info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
35180b57cec5SDimitry Andric       info.GetFileActionForFD(STDERR_FILENO) == nullptr) {
35190b57cec5SDimitry Andric     LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating "
35200b57cec5SDimitry Andric                   "default handling");
35210b57cec5SDimitry Andric 
35220b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
35230b57cec5SDimitry Andric       // Do nothing, if we are launching in a remote terminal no file actions
35240b57cec5SDimitry Andric       // should be done at all.
35250b57cec5SDimitry Andric       return;
35260b57cec5SDimitry Andric     }
35270b57cec5SDimitry Andric 
35280b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagDisableSTDIO)) {
35290b57cec5SDimitry Andric       LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action "
35300b57cec5SDimitry Andric                     "for stdin, stdout and stderr");
35310b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDIN_FILENO, true, false);
35320b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
35330b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDERR_FILENO, false, true);
35340b57cec5SDimitry Andric     } else {
35350b57cec5SDimitry Andric       // Check for any values that might have gotten set with any of: (lldb)
35360b57cec5SDimitry Andric       // settings set target.input-path (lldb) settings set target.output-path
35370b57cec5SDimitry Andric       // (lldb) settings set target.error-path
35380b57cec5SDimitry Andric       FileSpec in_file_spec;
35390b57cec5SDimitry Andric       FileSpec out_file_spec;
35400b57cec5SDimitry Andric       FileSpec err_file_spec;
35410b57cec5SDimitry Andric       // Only override with the target settings if we don't already have an
35420b57cec5SDimitry Andric       // action for in, out or error
35430b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDIN_FILENO) == nullptr)
35440b57cec5SDimitry Andric         in_file_spec = GetStandardInputPath();
35450b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr)
35460b57cec5SDimitry Andric         out_file_spec = GetStandardOutputPath();
35470b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDERR_FILENO) == nullptr)
35480b57cec5SDimitry Andric         err_file_spec = GetStandardErrorPath();
35490b57cec5SDimitry Andric 
35500b57cec5SDimitry Andric       LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{1}'",
35510b57cec5SDimitry Andric                in_file_spec, out_file_spec, err_file_spec);
35520b57cec5SDimitry Andric 
35530b57cec5SDimitry Andric       if (in_file_spec) {
35540b57cec5SDimitry Andric         info.AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
35550b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdin open file action for {0}", in_file_spec);
35560b57cec5SDimitry Andric       }
35570b57cec5SDimitry Andric 
35580b57cec5SDimitry Andric       if (out_file_spec) {
35590b57cec5SDimitry Andric         info.AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
35600b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdout open file action for {0}",
35610b57cec5SDimitry Andric                  out_file_spec);
35620b57cec5SDimitry Andric       }
35630b57cec5SDimitry Andric 
35640b57cec5SDimitry Andric       if (err_file_spec) {
35650b57cec5SDimitry Andric         info.AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
35660b57cec5SDimitry Andric         LLDB_LOG(log, "appended stderr open file action for {0}",
35670b57cec5SDimitry Andric                  err_file_spec);
35680b57cec5SDimitry Andric       }
35690b57cec5SDimitry Andric 
35700eae32dcSDimitry Andric       if (default_to_use_pty) {
35710b57cec5SDimitry Andric         llvm::Error Err = info.SetUpPtyRedirection();
35720b57cec5SDimitry Andric         LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
35730b57cec5SDimitry Andric       }
35740b57cec5SDimitry Andric     }
35750b57cec5SDimitry Andric   }
35760b57cec5SDimitry Andric }
35770b57cec5SDimitry Andric 
AddDummySignal(llvm::StringRef name,LazyBool pass,LazyBool notify,LazyBool stop)357881ad6265SDimitry Andric void Target::AddDummySignal(llvm::StringRef name, LazyBool pass, LazyBool notify,
357981ad6265SDimitry Andric                             LazyBool stop) {
358081ad6265SDimitry Andric     if (name.empty())
358181ad6265SDimitry Andric       return;
358281ad6265SDimitry Andric     // Don't add a signal if all the actions are trivial:
358381ad6265SDimitry Andric     if (pass == eLazyBoolCalculate && notify == eLazyBoolCalculate
358481ad6265SDimitry Andric         && stop == eLazyBoolCalculate)
358581ad6265SDimitry Andric       return;
358681ad6265SDimitry Andric 
358781ad6265SDimitry Andric     auto& elem = m_dummy_signals[name];
358881ad6265SDimitry Andric     elem.pass = pass;
358981ad6265SDimitry Andric     elem.notify = notify;
359081ad6265SDimitry Andric     elem.stop = stop;
359181ad6265SDimitry Andric }
359281ad6265SDimitry Andric 
UpdateSignalFromDummy(UnixSignalsSP signals_sp,const DummySignalElement & elem)359381ad6265SDimitry Andric bool Target::UpdateSignalFromDummy(UnixSignalsSP signals_sp,
359481ad6265SDimitry Andric                                           const DummySignalElement &elem) {
359581ad6265SDimitry Andric   if (!signals_sp)
359681ad6265SDimitry Andric     return false;
359781ad6265SDimitry Andric 
359881ad6265SDimitry Andric   int32_t signo
359981ad6265SDimitry Andric       = signals_sp->GetSignalNumberFromName(elem.first().str().c_str());
360081ad6265SDimitry Andric   if (signo == LLDB_INVALID_SIGNAL_NUMBER)
360181ad6265SDimitry Andric     return false;
360281ad6265SDimitry Andric 
360381ad6265SDimitry Andric   if (elem.second.pass == eLazyBoolYes)
360481ad6265SDimitry Andric     signals_sp->SetShouldSuppress(signo, false);
360581ad6265SDimitry Andric   else if (elem.second.pass == eLazyBoolNo)
360681ad6265SDimitry Andric     signals_sp->SetShouldSuppress(signo, true);
360781ad6265SDimitry Andric 
360881ad6265SDimitry Andric   if (elem.second.notify == eLazyBoolYes)
360981ad6265SDimitry Andric     signals_sp->SetShouldNotify(signo, true);
361081ad6265SDimitry Andric   else if (elem.second.notify == eLazyBoolNo)
361181ad6265SDimitry Andric     signals_sp->SetShouldNotify(signo, false);
361281ad6265SDimitry Andric 
361381ad6265SDimitry Andric   if (elem.second.stop == eLazyBoolYes)
361481ad6265SDimitry Andric     signals_sp->SetShouldStop(signo, true);
361581ad6265SDimitry Andric   else if (elem.second.stop == eLazyBoolNo)
361681ad6265SDimitry Andric     signals_sp->SetShouldStop(signo, false);
361781ad6265SDimitry Andric   return true;
361881ad6265SDimitry Andric }
361981ad6265SDimitry Andric 
ResetSignalFromDummy(UnixSignalsSP signals_sp,const DummySignalElement & elem)362081ad6265SDimitry Andric bool Target::ResetSignalFromDummy(UnixSignalsSP signals_sp,
362181ad6265SDimitry Andric                                           const DummySignalElement &elem) {
362281ad6265SDimitry Andric   if (!signals_sp)
362381ad6265SDimitry Andric     return false;
362481ad6265SDimitry Andric   int32_t signo
362581ad6265SDimitry Andric       = signals_sp->GetSignalNumberFromName(elem.first().str().c_str());
362681ad6265SDimitry Andric   if (signo == LLDB_INVALID_SIGNAL_NUMBER)
362781ad6265SDimitry Andric     return false;
362881ad6265SDimitry Andric   bool do_pass = elem.second.pass != eLazyBoolCalculate;
362981ad6265SDimitry Andric   bool do_stop = elem.second.stop != eLazyBoolCalculate;
363081ad6265SDimitry Andric   bool do_notify = elem.second.notify != eLazyBoolCalculate;
363181ad6265SDimitry Andric   signals_sp->ResetSignal(signo, do_stop, do_notify, do_pass);
363281ad6265SDimitry Andric   return true;
363381ad6265SDimitry Andric }
363481ad6265SDimitry Andric 
UpdateSignalsFromDummy(UnixSignalsSP signals_sp,StreamSP warning_stream_sp)363581ad6265SDimitry Andric void Target::UpdateSignalsFromDummy(UnixSignalsSP signals_sp,
363681ad6265SDimitry Andric                                     StreamSP warning_stream_sp) {
363781ad6265SDimitry Andric   if (!signals_sp)
363881ad6265SDimitry Andric     return;
363981ad6265SDimitry Andric 
364081ad6265SDimitry Andric   for (const auto &elem : m_dummy_signals) {
364181ad6265SDimitry Andric     if (!UpdateSignalFromDummy(signals_sp, elem))
364281ad6265SDimitry Andric       warning_stream_sp->Printf("Target signal '%s' not found in process\n",
364381ad6265SDimitry Andric           elem.first().str().c_str());
364481ad6265SDimitry Andric   }
364581ad6265SDimitry Andric }
364681ad6265SDimitry Andric 
ClearDummySignals(Args & signal_names)364781ad6265SDimitry Andric void Target::ClearDummySignals(Args &signal_names) {
364881ad6265SDimitry Andric   ProcessSP process_sp = GetProcessSP();
364981ad6265SDimitry Andric   // The simplest case, delete them all with no process to update.
365081ad6265SDimitry Andric   if (signal_names.GetArgumentCount() == 0 && !process_sp) {
365181ad6265SDimitry Andric     m_dummy_signals.clear();
365281ad6265SDimitry Andric     return;
365381ad6265SDimitry Andric   }
365481ad6265SDimitry Andric   UnixSignalsSP signals_sp;
365581ad6265SDimitry Andric   if (process_sp)
365681ad6265SDimitry Andric     signals_sp = process_sp->GetUnixSignals();
365781ad6265SDimitry Andric 
365881ad6265SDimitry Andric   for (const Args::ArgEntry &entry : signal_names) {
365981ad6265SDimitry Andric     const char *signal_name = entry.c_str();
366081ad6265SDimitry Andric     auto elem = m_dummy_signals.find(signal_name);
366181ad6265SDimitry Andric     // If we didn't find it go on.
366281ad6265SDimitry Andric     // FIXME: Should I pipe error handling through here?
366381ad6265SDimitry Andric     if (elem == m_dummy_signals.end()) {
366481ad6265SDimitry Andric       continue;
366581ad6265SDimitry Andric     }
366681ad6265SDimitry Andric     if (signals_sp)
366781ad6265SDimitry Andric       ResetSignalFromDummy(signals_sp, *elem);
366881ad6265SDimitry Andric     m_dummy_signals.erase(elem);
366981ad6265SDimitry Andric   }
367081ad6265SDimitry Andric }
367181ad6265SDimitry Andric 
PrintDummySignals(Stream & strm,Args & signal_args)367281ad6265SDimitry Andric void Target::PrintDummySignals(Stream &strm, Args &signal_args) {
367381ad6265SDimitry Andric   strm.Printf("NAME         PASS     STOP     NOTIFY\n");
367481ad6265SDimitry Andric   strm.Printf("===========  =======  =======  =======\n");
367581ad6265SDimitry Andric 
367681ad6265SDimitry Andric   auto str_for_lazy = [] (LazyBool lazy) -> const char * {
367781ad6265SDimitry Andric     switch (lazy) {
367881ad6265SDimitry Andric       case eLazyBoolCalculate: return "not set";
367981ad6265SDimitry Andric       case eLazyBoolYes: return "true   ";
368081ad6265SDimitry Andric       case eLazyBoolNo: return "false  ";
368181ad6265SDimitry Andric     }
368281ad6265SDimitry Andric     llvm_unreachable("Fully covered switch above!");
368381ad6265SDimitry Andric   };
368481ad6265SDimitry Andric   size_t num_args = signal_args.GetArgumentCount();
368581ad6265SDimitry Andric   for (const auto &elem : m_dummy_signals) {
368681ad6265SDimitry Andric     bool print_it = false;
368781ad6265SDimitry Andric     for (size_t idx = 0; idx < num_args; idx++) {
368881ad6265SDimitry Andric       if (elem.first() == signal_args.GetArgumentAtIndex(idx)) {
368981ad6265SDimitry Andric         print_it = true;
369081ad6265SDimitry Andric         break;
369181ad6265SDimitry Andric       }
369281ad6265SDimitry Andric     }
369381ad6265SDimitry Andric     if (print_it) {
369481ad6265SDimitry Andric       strm.Printf("%-11s  ", elem.first().str().c_str());
369581ad6265SDimitry Andric       strm.Printf("%s  %s  %s\n", str_for_lazy(elem.second.pass),
369681ad6265SDimitry Andric                   str_for_lazy(elem.second.stop),
369781ad6265SDimitry Andric                   str_for_lazy(elem.second.notify));
369881ad6265SDimitry Andric     }
369981ad6265SDimitry Andric   }
370081ad6265SDimitry Andric }
370181ad6265SDimitry Andric 
37020b57cec5SDimitry Andric // Target::StopHook
StopHook(lldb::TargetSP target_sp,lldb::user_id_t uid)37030b57cec5SDimitry Andric Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid)
3704e8d8bef9SDimitry Andric     : UserID(uid), m_target_sp(target_sp), m_specifier_sp(),
37050b57cec5SDimitry Andric       m_thread_spec_up() {}
37060b57cec5SDimitry Andric 
StopHook(const StopHook & rhs)37070b57cec5SDimitry Andric Target::StopHook::StopHook(const StopHook &rhs)
37080b57cec5SDimitry Andric     : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp),
3709e8d8bef9SDimitry Andric       m_specifier_sp(rhs.m_specifier_sp), m_thread_spec_up(),
3710e8d8bef9SDimitry Andric       m_active(rhs.m_active), m_auto_continue(rhs.m_auto_continue) {
37110b57cec5SDimitry Andric   if (rhs.m_thread_spec_up)
37125ffd83dbSDimitry Andric     m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
37130b57cec5SDimitry Andric }
37140b57cec5SDimitry Andric 
SetSpecifier(SymbolContextSpecifier * specifier)37150b57cec5SDimitry Andric void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) {
37160b57cec5SDimitry Andric   m_specifier_sp.reset(specifier);
37170b57cec5SDimitry Andric }
37180b57cec5SDimitry Andric 
SetThreadSpecifier(ThreadSpec * specifier)37190b57cec5SDimitry Andric void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) {
37200b57cec5SDimitry Andric   m_thread_spec_up.reset(specifier);
37210b57cec5SDimitry Andric }
37220b57cec5SDimitry Andric 
ExecutionContextPasses(const ExecutionContext & exc_ctx)3723e8d8bef9SDimitry Andric bool Target::StopHook::ExecutionContextPasses(const ExecutionContext &exc_ctx) {
3724e8d8bef9SDimitry Andric   SymbolContextSpecifier *specifier = GetSpecifier();
3725e8d8bef9SDimitry Andric   if (!specifier)
3726e8d8bef9SDimitry Andric     return true;
3727e8d8bef9SDimitry Andric 
3728e8d8bef9SDimitry Andric   bool will_run = true;
3729e8d8bef9SDimitry Andric   if (exc_ctx.GetFramePtr())
3730e8d8bef9SDimitry Andric     will_run = GetSpecifier()->SymbolContextMatches(
3731e8d8bef9SDimitry Andric         exc_ctx.GetFramePtr()->GetSymbolContext(eSymbolContextEverything));
3732e8d8bef9SDimitry Andric   if (will_run && GetThreadSpecifier() != nullptr)
3733e8d8bef9SDimitry Andric     will_run =
3734e8d8bef9SDimitry Andric         GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx.GetThreadRef());
3735e8d8bef9SDimitry Andric 
3736e8d8bef9SDimitry Andric   return will_run;
3737e8d8bef9SDimitry Andric }
3738e8d8bef9SDimitry Andric 
GetDescription(Stream & s,lldb::DescriptionLevel level) const3739fe013be4SDimitry Andric void Target::StopHook::GetDescription(Stream &s,
37400b57cec5SDimitry Andric                                       lldb::DescriptionLevel level) const {
3741e8d8bef9SDimitry Andric 
3742e8d8bef9SDimitry Andric   // For brief descriptions, only print the subclass description:
3743e8d8bef9SDimitry Andric   if (level == eDescriptionLevelBrief) {
3744e8d8bef9SDimitry Andric     GetSubclassDescription(s, level);
3745e8d8bef9SDimitry Andric     return;
3746e8d8bef9SDimitry Andric   }
3747e8d8bef9SDimitry Andric 
3748fe013be4SDimitry Andric   unsigned indent_level = s.GetIndentLevel();
37490b57cec5SDimitry Andric 
3750fe013be4SDimitry Andric   s.SetIndentLevel(indent_level + 2);
37510b57cec5SDimitry Andric 
3752fe013be4SDimitry Andric   s.Printf("Hook: %" PRIu64 "\n", GetID());
37530b57cec5SDimitry Andric   if (m_active)
3754fe013be4SDimitry Andric     s.Indent("State: enabled\n");
37550b57cec5SDimitry Andric   else
3756fe013be4SDimitry Andric     s.Indent("State: disabled\n");
37570b57cec5SDimitry Andric 
37580b57cec5SDimitry Andric   if (m_auto_continue)
3759fe013be4SDimitry Andric     s.Indent("AutoContinue on\n");
37600b57cec5SDimitry Andric 
37610b57cec5SDimitry Andric   if (m_specifier_sp) {
3762fe013be4SDimitry Andric     s.Indent();
3763fe013be4SDimitry Andric     s.PutCString("Specifier:\n");
3764fe013be4SDimitry Andric     s.SetIndentLevel(indent_level + 4);
3765fe013be4SDimitry Andric     m_specifier_sp->GetDescription(&s, level);
3766fe013be4SDimitry Andric     s.SetIndentLevel(indent_level + 2);
37670b57cec5SDimitry Andric   }
37680b57cec5SDimitry Andric 
37690b57cec5SDimitry Andric   if (m_thread_spec_up) {
37700b57cec5SDimitry Andric     StreamString tmp;
3771fe013be4SDimitry Andric     s.Indent("Thread:\n");
37720b57cec5SDimitry Andric     m_thread_spec_up->GetDescription(&tmp, level);
3773fe013be4SDimitry Andric     s.SetIndentLevel(indent_level + 4);
3774fe013be4SDimitry Andric     s.Indent(tmp.GetString());
3775fe013be4SDimitry Andric     s.PutCString("\n");
3776fe013be4SDimitry Andric     s.SetIndentLevel(indent_level + 2);
37770b57cec5SDimitry Andric   }
3778e8d8bef9SDimitry Andric   GetSubclassDescription(s, level);
3779e8d8bef9SDimitry Andric }
37800b57cec5SDimitry Andric 
GetSubclassDescription(Stream & s,lldb::DescriptionLevel level) const3781e8d8bef9SDimitry Andric void Target::StopHookCommandLine::GetSubclassDescription(
3782fe013be4SDimitry Andric     Stream &s, lldb::DescriptionLevel level) const {
3783e8d8bef9SDimitry Andric   // The brief description just prints the first command.
3784e8d8bef9SDimitry Andric   if (level == eDescriptionLevelBrief) {
3785e8d8bef9SDimitry Andric     if (m_commands.GetSize() == 1)
3786fe013be4SDimitry Andric       s.PutCString(m_commands.GetStringAtIndex(0));
3787e8d8bef9SDimitry Andric     return;
3788e8d8bef9SDimitry Andric   }
3789fe013be4SDimitry Andric   s.Indent("Commands: \n");
3790fe013be4SDimitry Andric   s.SetIndentLevel(s.GetIndentLevel() + 4);
37910b57cec5SDimitry Andric   uint32_t num_commands = m_commands.GetSize();
37920b57cec5SDimitry Andric   for (uint32_t i = 0; i < num_commands; i++) {
3793fe013be4SDimitry Andric     s.Indent(m_commands.GetStringAtIndex(i));
3794fe013be4SDimitry Andric     s.PutCString("\n");
37950b57cec5SDimitry Andric   }
3796fe013be4SDimitry Andric   s.SetIndentLevel(s.GetIndentLevel() - 4);
3797e8d8bef9SDimitry Andric }
3798e8d8bef9SDimitry Andric 
3799e8d8bef9SDimitry Andric // Target::StopHookCommandLine
SetActionFromString(const std::string & string)3800e8d8bef9SDimitry Andric void Target::StopHookCommandLine::SetActionFromString(const std::string &string) {
3801e8d8bef9SDimitry Andric   GetCommands().SplitIntoLines(string);
3802e8d8bef9SDimitry Andric }
3803e8d8bef9SDimitry Andric 
SetActionFromStrings(const std::vector<std::string> & strings)3804e8d8bef9SDimitry Andric void Target::StopHookCommandLine::SetActionFromStrings(
3805e8d8bef9SDimitry Andric     const std::vector<std::string> &strings) {
3806e8d8bef9SDimitry Andric   for (auto string : strings)
3807e8d8bef9SDimitry Andric     GetCommands().AppendString(string.c_str());
3808e8d8bef9SDimitry Andric }
3809e8d8bef9SDimitry Andric 
3810e8d8bef9SDimitry Andric Target::StopHook::StopHookResult
HandleStop(ExecutionContext & exc_ctx,StreamSP output_sp)3811e8d8bef9SDimitry Andric Target::StopHookCommandLine::HandleStop(ExecutionContext &exc_ctx,
3812e8d8bef9SDimitry Andric                                         StreamSP output_sp) {
3813e8d8bef9SDimitry Andric   assert(exc_ctx.GetTargetPtr() && "Can't call PerformAction on a context "
3814e8d8bef9SDimitry Andric                                    "with no target");
3815e8d8bef9SDimitry Andric 
3816e8d8bef9SDimitry Andric   if (!m_commands.GetSize())
3817e8d8bef9SDimitry Andric     return StopHookResult::KeepStopped;
3818e8d8bef9SDimitry Andric 
3819e8d8bef9SDimitry Andric   CommandReturnObject result(false);
3820e8d8bef9SDimitry Andric   result.SetImmediateOutputStream(output_sp);
3821e8d8bef9SDimitry Andric   result.SetInteractive(false);
3822e8d8bef9SDimitry Andric   Debugger &debugger = exc_ctx.GetTargetPtr()->GetDebugger();
3823e8d8bef9SDimitry Andric   CommandInterpreterRunOptions options;
3824e8d8bef9SDimitry Andric   options.SetStopOnContinue(true);
3825e8d8bef9SDimitry Andric   options.SetStopOnError(true);
3826e8d8bef9SDimitry Andric   options.SetEchoCommands(false);
3827e8d8bef9SDimitry Andric   options.SetPrintResults(true);
3828e8d8bef9SDimitry Andric   options.SetPrintErrors(true);
3829e8d8bef9SDimitry Andric   options.SetAddToHistory(false);
3830e8d8bef9SDimitry Andric 
3831e8d8bef9SDimitry Andric   // Force Async:
3832e8d8bef9SDimitry Andric   bool old_async = debugger.GetAsyncExecution();
3833e8d8bef9SDimitry Andric   debugger.SetAsyncExecution(true);
3834fe6060f1SDimitry Andric   debugger.GetCommandInterpreter().HandleCommands(GetCommands(), exc_ctx,
3835e8d8bef9SDimitry Andric                                                   options, result);
3836e8d8bef9SDimitry Andric   debugger.SetAsyncExecution(old_async);
3837e8d8bef9SDimitry Andric   lldb::ReturnStatus status = result.GetStatus();
3838e8d8bef9SDimitry Andric   if (status == eReturnStatusSuccessContinuingNoResult ||
3839e8d8bef9SDimitry Andric       status == eReturnStatusSuccessContinuingResult)
3840e8d8bef9SDimitry Andric     return StopHookResult::AlreadyContinued;
3841e8d8bef9SDimitry Andric   return StopHookResult::KeepStopped;
3842e8d8bef9SDimitry Andric }
3843e8d8bef9SDimitry Andric 
3844e8d8bef9SDimitry Andric // Target::StopHookScripted
SetScriptCallback(std::string class_name,StructuredData::ObjectSP extra_args_sp)3845e8d8bef9SDimitry Andric Status Target::StopHookScripted::SetScriptCallback(
3846e8d8bef9SDimitry Andric     std::string class_name, StructuredData::ObjectSP extra_args_sp) {
3847e8d8bef9SDimitry Andric   Status error;
3848e8d8bef9SDimitry Andric 
3849e8d8bef9SDimitry Andric   ScriptInterpreter *script_interp =
3850e8d8bef9SDimitry Andric       GetTarget()->GetDebugger().GetScriptInterpreter();
3851e8d8bef9SDimitry Andric   if (!script_interp) {
3852e8d8bef9SDimitry Andric     error.SetErrorString("No script interpreter installed.");
3853e8d8bef9SDimitry Andric     return error;
3854e8d8bef9SDimitry Andric   }
3855e8d8bef9SDimitry Andric 
3856e8d8bef9SDimitry Andric   m_class_name = class_name;
38570eae32dcSDimitry Andric   m_extra_args.SetObjectSP(extra_args_sp);
3858e8d8bef9SDimitry Andric 
3859e8d8bef9SDimitry Andric   m_implementation_sp = script_interp->CreateScriptedStopHook(
3860e8d8bef9SDimitry Andric       GetTarget(), m_class_name.c_str(), m_extra_args, error);
3861e8d8bef9SDimitry Andric 
3862e8d8bef9SDimitry Andric   return error;
3863e8d8bef9SDimitry Andric }
3864e8d8bef9SDimitry Andric 
3865e8d8bef9SDimitry Andric Target::StopHook::StopHookResult
HandleStop(ExecutionContext & exc_ctx,StreamSP output_sp)3866e8d8bef9SDimitry Andric Target::StopHookScripted::HandleStop(ExecutionContext &exc_ctx,
3867e8d8bef9SDimitry Andric                                      StreamSP output_sp) {
3868e8d8bef9SDimitry Andric   assert(exc_ctx.GetTargetPtr() && "Can't call HandleStop on a context "
3869e8d8bef9SDimitry Andric                                    "with no target");
3870e8d8bef9SDimitry Andric 
3871e8d8bef9SDimitry Andric   ScriptInterpreter *script_interp =
3872e8d8bef9SDimitry Andric       GetTarget()->GetDebugger().GetScriptInterpreter();
3873e8d8bef9SDimitry Andric   if (!script_interp)
3874e8d8bef9SDimitry Andric     return StopHookResult::KeepStopped;
3875e8d8bef9SDimitry Andric 
3876e8d8bef9SDimitry Andric   bool should_stop = script_interp->ScriptedStopHookHandleStop(
3877e8d8bef9SDimitry Andric       m_implementation_sp, exc_ctx, output_sp);
3878e8d8bef9SDimitry Andric 
3879e8d8bef9SDimitry Andric   return should_stop ? StopHookResult::KeepStopped
3880e8d8bef9SDimitry Andric                      : StopHookResult::RequestContinue;
3881e8d8bef9SDimitry Andric }
3882e8d8bef9SDimitry Andric 
GetSubclassDescription(Stream & s,lldb::DescriptionLevel level) const3883e8d8bef9SDimitry Andric void Target::StopHookScripted::GetSubclassDescription(
3884fe013be4SDimitry Andric     Stream &s, lldb::DescriptionLevel level) const {
3885e8d8bef9SDimitry Andric   if (level == eDescriptionLevelBrief) {
3886fe013be4SDimitry Andric     s.PutCString(m_class_name);
3887e8d8bef9SDimitry Andric     return;
3888e8d8bef9SDimitry Andric   }
3889fe013be4SDimitry Andric   s.Indent("Class:");
3890fe013be4SDimitry Andric   s.Printf("%s\n", m_class_name.c_str());
3891e8d8bef9SDimitry Andric 
3892e8d8bef9SDimitry Andric   // Now print the extra args:
3893e8d8bef9SDimitry Andric   // FIXME: We should use StructuredData.GetDescription on the m_extra_args
3894e8d8bef9SDimitry Andric   // but that seems to rely on some printing plugin that doesn't exist.
38950eae32dcSDimitry Andric   if (!m_extra_args.IsValid())
3896e8d8bef9SDimitry Andric     return;
38970eae32dcSDimitry Andric   StructuredData::ObjectSP object_sp = m_extra_args.GetObjectSP();
3898e8d8bef9SDimitry Andric   if (!object_sp || !object_sp->IsValid())
3899e8d8bef9SDimitry Andric     return;
3900e8d8bef9SDimitry Andric 
3901e8d8bef9SDimitry Andric   StructuredData::Dictionary *as_dict = object_sp->GetAsDictionary();
3902e8d8bef9SDimitry Andric   if (!as_dict || !as_dict->IsValid())
3903e8d8bef9SDimitry Andric     return;
3904e8d8bef9SDimitry Andric 
3905e8d8bef9SDimitry Andric   uint32_t num_keys = as_dict->GetSize();
3906e8d8bef9SDimitry Andric   if (num_keys == 0)
3907e8d8bef9SDimitry Andric     return;
3908e8d8bef9SDimitry Andric 
3909fe013be4SDimitry Andric   s.Indent("Args:\n");
3910fe013be4SDimitry Andric   s.SetIndentLevel(s.GetIndentLevel() + 4);
3911e8d8bef9SDimitry Andric 
3912c9157d92SDimitry Andric   auto print_one_element = [&s](llvm::StringRef key,
3913e8d8bef9SDimitry Andric                                 StructuredData::Object *object) {
3914fe013be4SDimitry Andric     s.Indent();
3915c9157d92SDimitry Andric     s.Format("{0} : {1}\n", key, object->GetStringValue());
3916e8d8bef9SDimitry Andric     return true;
3917e8d8bef9SDimitry Andric   };
3918e8d8bef9SDimitry Andric 
3919e8d8bef9SDimitry Andric   as_dict->ForEach(print_one_element);
3920e8d8bef9SDimitry Andric 
3921fe013be4SDimitry Andric   s.SetIndentLevel(s.GetIndentLevel() - 4);
39220b57cec5SDimitry Andric }
39230b57cec5SDimitry Andric 
39240b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
39259dba64beSDimitry Andric     {
39269dba64beSDimitry Andric         eNoDynamicValues,
39279dba64beSDimitry Andric         "no-dynamic-values",
39289dba64beSDimitry Andric         "Don't calculate the dynamic type of values",
39299dba64beSDimitry Andric     },
39309dba64beSDimitry Andric     {
39319dba64beSDimitry Andric         eDynamicCanRunTarget,
39329dba64beSDimitry Andric         "run-target",
39339dba64beSDimitry Andric         "Calculate the dynamic type of values "
39349dba64beSDimitry Andric         "even if you have to run the target.",
39359dba64beSDimitry Andric     },
39369dba64beSDimitry Andric     {
39379dba64beSDimitry Andric         eDynamicDontRunTarget,
39389dba64beSDimitry Andric         "no-run-target",
39399dba64beSDimitry Andric         "Calculate the dynamic type of values, but don't run the target.",
39409dba64beSDimitry Andric     },
39419dba64beSDimitry Andric };
39420b57cec5SDimitry Andric 
GetDynamicValueTypes()39430b57cec5SDimitry Andric OptionEnumValues lldb_private::GetDynamicValueTypes() {
39440b57cec5SDimitry Andric   return OptionEnumValues(g_dynamic_value_types);
39450b57cec5SDimitry Andric }
39460b57cec5SDimitry Andric 
39470b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = {
39489dba64beSDimitry Andric     {
39499dba64beSDimitry Andric         eInlineBreakpointsNever,
39509dba64beSDimitry Andric         "never",
39519dba64beSDimitry Andric         "Never look for inline breakpoint locations (fastest). This setting "
39529dba64beSDimitry Andric         "should only be used if you know that no inlining occurs in your"
39539dba64beSDimitry Andric         "programs.",
39549dba64beSDimitry Andric     },
39559dba64beSDimitry Andric     {
39569dba64beSDimitry Andric         eInlineBreakpointsHeaders,
39579dba64beSDimitry Andric         "headers",
39589dba64beSDimitry Andric         "Only check for inline breakpoint locations when setting breakpoints "
39599dba64beSDimitry Andric         "in header files, but not when setting breakpoint in implementation "
39609dba64beSDimitry Andric         "source files (default).",
39619dba64beSDimitry Andric     },
39629dba64beSDimitry Andric     {
39639dba64beSDimitry Andric         eInlineBreakpointsAlways,
39649dba64beSDimitry Andric         "always",
39659dba64beSDimitry Andric         "Always look for inline breakpoint locations when setting file and "
39669dba64beSDimitry Andric         "line breakpoints (slower but most accurate).",
39679dba64beSDimitry Andric     },
39689dba64beSDimitry Andric };
39690b57cec5SDimitry Andric 
39700b57cec5SDimitry Andric enum x86DisassemblyFlavor {
39710b57cec5SDimitry Andric   eX86DisFlavorDefault,
39720b57cec5SDimitry Andric   eX86DisFlavorIntel,
39730b57cec5SDimitry Andric   eX86DisFlavorATT
39740b57cec5SDimitry Andric };
39750b57cec5SDimitry Andric 
39760b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = {
39779dba64beSDimitry Andric     {
39789dba64beSDimitry Andric         eX86DisFlavorDefault,
39799dba64beSDimitry Andric         "default",
39809dba64beSDimitry Andric         "Disassembler default (currently att).",
39819dba64beSDimitry Andric     },
39829dba64beSDimitry Andric     {
39839dba64beSDimitry Andric         eX86DisFlavorIntel,
39849dba64beSDimitry Andric         "intel",
39859dba64beSDimitry Andric         "Intel disassembler flavor.",
39869dba64beSDimitry Andric     },
39879dba64beSDimitry Andric     {
39889dba64beSDimitry Andric         eX86DisFlavorATT,
39899dba64beSDimitry Andric         "att",
39909dba64beSDimitry Andric         "AT&T disassembler flavor.",
39919dba64beSDimitry Andric     },
39929dba64beSDimitry Andric };
39930b57cec5SDimitry Andric 
3994e8d8bef9SDimitry Andric static constexpr OptionEnumValueElement g_import_std_module_value_types[] = {
3995e8d8bef9SDimitry Andric     {
3996e8d8bef9SDimitry Andric         eImportStdModuleFalse,
3997e8d8bef9SDimitry Andric         "false",
3998e8d8bef9SDimitry Andric         "Never import the 'std' C++ module in the expression parser.",
3999e8d8bef9SDimitry Andric     },
4000e8d8bef9SDimitry Andric     {
4001e8d8bef9SDimitry Andric         eImportStdModuleFallback,
4002e8d8bef9SDimitry Andric         "fallback",
4003e8d8bef9SDimitry Andric         "Retry evaluating expressions with an imported 'std' C++ module if they"
4004e8d8bef9SDimitry Andric         " failed to parse without the module. This allows evaluating more "
4005e8d8bef9SDimitry Andric         "complex expressions involving C++ standard library types."
4006e8d8bef9SDimitry Andric     },
4007e8d8bef9SDimitry Andric     {
4008e8d8bef9SDimitry Andric         eImportStdModuleTrue,
4009e8d8bef9SDimitry Andric         "true",
4010e8d8bef9SDimitry Andric         "Always import the 'std' C++ module. This allows evaluating more "
4011e8d8bef9SDimitry Andric         "complex expressions involving C++ standard library types. This feature"
4012e8d8bef9SDimitry Andric         " is experimental."
4013e8d8bef9SDimitry Andric     },
4014e8d8bef9SDimitry Andric };
4015e8d8bef9SDimitry Andric 
401681ad6265SDimitry Andric static constexpr OptionEnumValueElement
401781ad6265SDimitry Andric     g_dynamic_class_info_helper_value_types[] = {
401881ad6265SDimitry Andric         {
401981ad6265SDimitry Andric             eDynamicClassInfoHelperAuto,
402081ad6265SDimitry Andric             "auto",
402181ad6265SDimitry Andric             "Automatically determine the most appropriate method for the "
402281ad6265SDimitry Andric             "target OS.",
402381ad6265SDimitry Andric         },
402481ad6265SDimitry Andric         {eDynamicClassInfoHelperRealizedClassesStruct, "RealizedClassesStruct",
402581ad6265SDimitry Andric          "Prefer using the realized classes struct."},
402681ad6265SDimitry Andric         {eDynamicClassInfoHelperCopyRealizedClassList, "CopyRealizedClassList",
402781ad6265SDimitry Andric          "Prefer using the CopyRealizedClassList API."},
402881ad6265SDimitry Andric         {eDynamicClassInfoHelperGetRealizedClassList, "GetRealizedClassList",
402981ad6265SDimitry Andric          "Prefer using the GetRealizedClassList API."},
403081ad6265SDimitry Andric };
403181ad6265SDimitry Andric 
40320b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = {
40339dba64beSDimitry Andric     {
40349dba64beSDimitry Andric         Disassembler::eHexStyleC,
40359dba64beSDimitry Andric         "c",
40369dba64beSDimitry Andric         "C-style (0xffff).",
40379dba64beSDimitry Andric     },
40389dba64beSDimitry Andric     {
40399dba64beSDimitry Andric         Disassembler::eHexStyleAsm,
40409dba64beSDimitry Andric         "asm",
40419dba64beSDimitry Andric         "Asm-style (0ffffh).",
40429dba64beSDimitry Andric     },
40439dba64beSDimitry Andric };
40440b57cec5SDimitry Andric 
40450b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = {
40469dba64beSDimitry Andric     {
40479dba64beSDimitry Andric         eLoadScriptFromSymFileTrue,
40489dba64beSDimitry Andric         "true",
40499dba64beSDimitry Andric         "Load debug scripts inside symbol files",
40509dba64beSDimitry Andric     },
40519dba64beSDimitry Andric     {
40529dba64beSDimitry Andric         eLoadScriptFromSymFileFalse,
40539dba64beSDimitry Andric         "false",
40549dba64beSDimitry Andric         "Do not load debug scripts inside symbol files.",
40559dba64beSDimitry Andric     },
40569dba64beSDimitry Andric     {
40579dba64beSDimitry Andric         eLoadScriptFromSymFileWarn,
40589dba64beSDimitry Andric         "warn",
40599dba64beSDimitry Andric         "Warn about debug scripts inside symbol files but do not load them.",
40609dba64beSDimitry Andric     },
40619dba64beSDimitry Andric };
40620b57cec5SDimitry Andric 
40639dba64beSDimitry Andric static constexpr OptionEnumValueElement g_load_cwd_lldbinit_values[] = {
40649dba64beSDimitry Andric     {
40659dba64beSDimitry Andric         eLoadCWDlldbinitTrue,
40669dba64beSDimitry Andric         "true",
40679dba64beSDimitry Andric         "Load .lldbinit files from current directory",
40689dba64beSDimitry Andric     },
40699dba64beSDimitry Andric     {
40709dba64beSDimitry Andric         eLoadCWDlldbinitFalse,
40719dba64beSDimitry Andric         "false",
40729dba64beSDimitry Andric         "Do not load .lldbinit files from current directory",
40739dba64beSDimitry Andric     },
40749dba64beSDimitry Andric     {
40759dba64beSDimitry Andric         eLoadCWDlldbinitWarn,
40769dba64beSDimitry Andric         "warn",
40779dba64beSDimitry Andric         "Warn about loading .lldbinit files from current directory",
40789dba64beSDimitry Andric     },
40799dba64beSDimitry Andric };
40800b57cec5SDimitry Andric 
40810b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = {
40829dba64beSDimitry Andric     {
40839dba64beSDimitry Andric         eMemoryModuleLoadLevelMinimal,
40849dba64beSDimitry Andric         "minimal",
40850b57cec5SDimitry Andric         "Load minimal information when loading modules from memory. Currently "
40869dba64beSDimitry Andric         "this setting loads sections only.",
40879dba64beSDimitry Andric     },
40889dba64beSDimitry Andric     {
40899dba64beSDimitry Andric         eMemoryModuleLoadLevelPartial,
40909dba64beSDimitry Andric         "partial",
40910b57cec5SDimitry Andric         "Load partial information when loading modules from memory. Currently "
40929dba64beSDimitry Andric         "this setting loads sections and function bounds.",
40939dba64beSDimitry Andric     },
40949dba64beSDimitry Andric     {
40959dba64beSDimitry Andric         eMemoryModuleLoadLevelComplete,
40969dba64beSDimitry Andric         "complete",
40970b57cec5SDimitry Andric         "Load complete information when loading modules from memory. Currently "
40989dba64beSDimitry Andric         "this setting loads sections and all symbols.",
40999dba64beSDimitry Andric     },
41009dba64beSDimitry Andric };
41010b57cec5SDimitry Andric 
41029dba64beSDimitry Andric #define LLDB_PROPERTIES_target
41039dba64beSDimitry Andric #include "TargetProperties.inc"
41040b57cec5SDimitry Andric 
41050b57cec5SDimitry Andric enum {
41069dba64beSDimitry Andric #define LLDB_PROPERTIES_target
41079dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
41080b57cec5SDimitry Andric   ePropertyExperimental,
41090b57cec5SDimitry Andric };
41100b57cec5SDimitry Andric 
4111fe6060f1SDimitry Andric class TargetOptionValueProperties
4112fe6060f1SDimitry Andric     : public Cloneable<TargetOptionValueProperties, OptionValueProperties> {
41130b57cec5SDimitry Andric public:
TargetOptionValueProperties(llvm::StringRef name)4114c9157d92SDimitry Andric   TargetOptionValueProperties(llvm::StringRef name) : Cloneable(name) {}
41150b57cec5SDimitry Andric 
4116fe013be4SDimitry Andric   const Property *
GetPropertyAtIndex(size_t idx,const ExecutionContext * exe_ctx=nullptr) const4117fe013be4SDimitry Andric   GetPropertyAtIndex(size_t idx,
4118fe013be4SDimitry Andric                      const ExecutionContext *exe_ctx = nullptr) const override {
41190b57cec5SDimitry Andric     // When getting the value for a key from the target options, we will always
41200b57cec5SDimitry Andric     // try and grab the setting from the current target if there is one. Else
41210b57cec5SDimitry Andric     // we just use the one from this instance.
41220b57cec5SDimitry Andric     if (exe_ctx) {
41230b57cec5SDimitry Andric       Target *target = exe_ctx->GetTargetPtr();
41240b57cec5SDimitry Andric       if (target) {
41250b57cec5SDimitry Andric         TargetOptionValueProperties *target_properties =
41260b57cec5SDimitry Andric             static_cast<TargetOptionValueProperties *>(
41270b57cec5SDimitry Andric                 target->GetValueProperties().get());
41280b57cec5SDimitry Andric         if (this != target_properties)
41290b57cec5SDimitry Andric           return target_properties->ProtectedGetPropertyAtIndex(idx);
41300b57cec5SDimitry Andric       }
41310b57cec5SDimitry Andric     }
41320b57cec5SDimitry Andric     return ProtectedGetPropertyAtIndex(idx);
41330b57cec5SDimitry Andric   }
41340b57cec5SDimitry Andric };
41350b57cec5SDimitry Andric 
41360b57cec5SDimitry Andric // TargetProperties
41375ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
41389dba64beSDimitry Andric #include "TargetProperties.inc"
41390b57cec5SDimitry Andric 
41409dba64beSDimitry Andric enum {
41415ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
41429dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
41439dba64beSDimitry Andric };
41440b57cec5SDimitry Andric 
4145fe6060f1SDimitry Andric class TargetExperimentalOptionValueProperties
4146fe6060f1SDimitry Andric     : public Cloneable<TargetExperimentalOptionValueProperties,
4147fe6060f1SDimitry Andric                        OptionValueProperties> {
41480b57cec5SDimitry Andric public:
TargetExperimentalOptionValueProperties()41490b57cec5SDimitry Andric   TargetExperimentalOptionValueProperties()
4150c9157d92SDimitry Andric       : Cloneable(Properties::GetExperimentalSettingsName()) {}
41510b57cec5SDimitry Andric };
41520b57cec5SDimitry Andric 
TargetExperimentalProperties()41530b57cec5SDimitry Andric TargetExperimentalProperties::TargetExperimentalProperties()
41540b57cec5SDimitry Andric     : Properties(OptionValuePropertiesSP(
41550b57cec5SDimitry Andric           new TargetExperimentalOptionValueProperties())) {
41565ffd83dbSDimitry Andric   m_collection_sp->Initialize(g_target_experimental_properties);
41570b57cec5SDimitry Andric }
41580b57cec5SDimitry Andric 
41590b57cec5SDimitry Andric // TargetProperties
TargetProperties(Target * target)41600b57cec5SDimitry Andric TargetProperties::TargetProperties(Target *target)
41615ffd83dbSDimitry Andric     : Properties(), m_launch_info(), m_target(target) {
41620b57cec5SDimitry Andric   if (target) {
4163fe6060f1SDimitry Andric     m_collection_sp =
4164349cc55cSDimitry Andric         OptionValueProperties::CreateLocalCopy(Target::GetGlobalProperties());
41650b57cec5SDimitry Andric 
41660b57cec5SDimitry Andric     // Set callbacks to update launch_info whenever "settins set" updated any
41670b57cec5SDimitry Andric     // of these properties
41680b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4169480093f4SDimitry Andric         ePropertyArg0, [this] { Arg0ValueChangedCallback(); });
41700b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4171480093f4SDimitry Andric         ePropertyRunArgs, [this] { RunArgsValueChangedCallback(); });
41720b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4173480093f4SDimitry Andric         ePropertyEnvVars, [this] { EnvVarsValueChangedCallback(); });
41740b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
41755ffd83dbSDimitry Andric         ePropertyUnsetEnvVars, [this] { EnvVarsValueChangedCallback(); });
41765ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
41775ffd83dbSDimitry Andric         ePropertyInheritEnv, [this] { EnvVarsValueChangedCallback(); });
41785ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
4179480093f4SDimitry Andric         ePropertyInputPath, [this] { InputPathValueChangedCallback(); });
41800b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4181480093f4SDimitry Andric         ePropertyOutputPath, [this] { OutputPathValueChangedCallback(); });
41820b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4183480093f4SDimitry Andric         ePropertyErrorPath, [this] { ErrorPathValueChangedCallback(); });
4184480093f4SDimitry Andric     m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, [this] {
4185480093f4SDimitry Andric       DetachOnErrorValueChangedCallback();
4186480093f4SDimitry Andric     });
41870b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4188480093f4SDimitry Andric         ePropertyDisableASLR, [this] { DisableASLRValueChangedCallback(); });
41890b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4190e8d8bef9SDimitry Andric         ePropertyInheritTCC, [this] { InheritTCCValueChangedCallback(); });
4191e8d8bef9SDimitry Andric     m_collection_sp->SetValueChangedCallback(
4192480093f4SDimitry Andric         ePropertyDisableSTDIO, [this] { DisableSTDIOValueChangedCallback(); });
41930b57cec5SDimitry Andric 
419481ad6265SDimitry Andric     m_collection_sp->SetValueChangedCallback(
419581ad6265SDimitry Andric         ePropertySaveObjectsDir, [this] { CheckJITObjectsDir(); });
41965ffd83dbSDimitry Andric     m_experimental_properties_up =
41975ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
41980b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
4199fe013be4SDimitry Andric         Properties::GetExperimentalSettingsName(),
4200fe013be4SDimitry Andric         "Experimental settings - setting these won't produce "
4201fe013be4SDimitry Andric         "errors if the setting is not present.",
42020b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
42030b57cec5SDimitry Andric   } else {
4204c9157d92SDimitry Andric     m_collection_sp = std::make_shared<TargetOptionValueProperties>("target");
42059dba64beSDimitry Andric     m_collection_sp->Initialize(g_target_properties);
42065ffd83dbSDimitry Andric     m_experimental_properties_up =
42075ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
42080b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
4209fe013be4SDimitry Andric         Properties::GetExperimentalSettingsName(),
4210fe013be4SDimitry Andric         "Experimental settings - setting these won't produce "
4211fe013be4SDimitry Andric         "errors if the setting is not present.",
42120b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
42130b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
4214fe013be4SDimitry Andric         "process", "Settings specific to processes.", true,
4215fe013be4SDimitry Andric         Process::GetGlobalProperties().GetValueProperties());
421681ad6265SDimitry Andric     m_collection_sp->SetValueChangedCallback(
421781ad6265SDimitry Andric         ePropertySaveObjectsDir, [this] { CheckJITObjectsDir(); });
42180b57cec5SDimitry Andric   }
42190b57cec5SDimitry Andric }
42200b57cec5SDimitry Andric 
42210b57cec5SDimitry Andric TargetProperties::~TargetProperties() = default;
42220b57cec5SDimitry Andric 
UpdateLaunchInfoFromProperties()42235ffd83dbSDimitry Andric void TargetProperties::UpdateLaunchInfoFromProperties() {
42245ffd83dbSDimitry Andric   Arg0ValueChangedCallback();
42255ffd83dbSDimitry Andric   RunArgsValueChangedCallback();
42265ffd83dbSDimitry Andric   EnvVarsValueChangedCallback();
42275ffd83dbSDimitry Andric   InputPathValueChangedCallback();
42285ffd83dbSDimitry Andric   OutputPathValueChangedCallback();
42295ffd83dbSDimitry Andric   ErrorPathValueChangedCallback();
42305ffd83dbSDimitry Andric   DetachOnErrorValueChangedCallback();
42315ffd83dbSDimitry Andric   DisableASLRValueChangedCallback();
4232e8d8bef9SDimitry Andric   InheritTCCValueChangedCallback();
42335ffd83dbSDimitry Andric   DisableSTDIOValueChangedCallback();
42345ffd83dbSDimitry Andric }
42355ffd83dbSDimitry Andric 
GetInjectLocalVariables(ExecutionContext * exe_ctx) const42360b57cec5SDimitry Andric bool TargetProperties::GetInjectLocalVariables(
42370b57cec5SDimitry Andric     ExecutionContext *exe_ctx) const {
4238fe013be4SDimitry Andric   const Property *exp_property =
4239fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndex(ePropertyExperimental, exe_ctx);
42400b57cec5SDimitry Andric   OptionValueProperties *exp_values =
42410b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
42420b57cec5SDimitry Andric   if (exp_values)
4243fe013be4SDimitry Andric     return exp_values
4244fe013be4SDimitry Andric         ->GetPropertyAtIndexAs<bool>(ePropertyInjectLocalVars, exe_ctx)
4245fe013be4SDimitry Andric         .value_or(true);
42460b57cec5SDimitry Andric   else
42470b57cec5SDimitry Andric     return true;
42480b57cec5SDimitry Andric }
42490b57cec5SDimitry Andric 
SetInjectLocalVariables(ExecutionContext * exe_ctx,bool b)42500b57cec5SDimitry Andric void TargetProperties::SetInjectLocalVariables(ExecutionContext *exe_ctx,
42510b57cec5SDimitry Andric                                                bool b) {
42520b57cec5SDimitry Andric   const Property *exp_property =
4253fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndex(ePropertyExperimental, exe_ctx);
42540b57cec5SDimitry Andric   OptionValueProperties *exp_values =
42550b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
42560b57cec5SDimitry Andric   if (exp_values)
4257fe013be4SDimitry Andric     exp_values->SetPropertyAtIndex(ePropertyInjectLocalVars, true, exe_ctx);
42580b57cec5SDimitry Andric }
42590b57cec5SDimitry Andric 
GetDefaultArchitecture() const42600b57cec5SDimitry Andric ArchSpec TargetProperties::GetDefaultArchitecture() const {
4261fe013be4SDimitry Andric   const uint32_t idx = ePropertyDefaultArch;
4262fe013be4SDimitry Andric   return GetPropertyAtIndexAs<ArchSpec>(idx, {});
42630b57cec5SDimitry Andric }
42640b57cec5SDimitry Andric 
SetDefaultArchitecture(const ArchSpec & arch)42650b57cec5SDimitry Andric void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) {
4266fe013be4SDimitry Andric   const uint32_t idx = ePropertyDefaultArch;
4267fe013be4SDimitry Andric   SetPropertyAtIndex(idx, arch);
42680b57cec5SDimitry Andric }
42690b57cec5SDimitry Andric 
GetMoveToNearestCode() const42700b57cec5SDimitry Andric bool TargetProperties::GetMoveToNearestCode() const {
42710b57cec5SDimitry Andric   const uint32_t idx = ePropertyMoveToNearestCode;
4272fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4273fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
42740b57cec5SDimitry Andric }
42750b57cec5SDimitry Andric 
GetPreferDynamicValue() const42760b57cec5SDimitry Andric lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const {
42770b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
4278fe013be4SDimitry Andric   return GetPropertyAtIndexAs<lldb::DynamicValueType>(
4279fe013be4SDimitry Andric       idx, static_cast<lldb::DynamicValueType>(
4280fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
42810b57cec5SDimitry Andric }
42820b57cec5SDimitry Andric 
SetPreferDynamicValue(lldb::DynamicValueType d)42830b57cec5SDimitry Andric bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
42840b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
4285fe013be4SDimitry Andric   return SetPropertyAtIndex(idx, d);
42860b57cec5SDimitry Andric }
42870b57cec5SDimitry Andric 
GetPreloadSymbols() const42880b57cec5SDimitry Andric bool TargetProperties::GetPreloadSymbols() const {
4289fe013be4SDimitry Andric   if (INTERRUPT_REQUESTED(m_target->GetDebugger(),
4290fe013be4SDimitry Andric                           "Interrupted checking preload symbols")) {
4291fe013be4SDimitry Andric     return false;
4292fe013be4SDimitry Andric   }
42930b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
4294fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4295fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
42960b57cec5SDimitry Andric }
42970b57cec5SDimitry Andric 
SetPreloadSymbols(bool b)42980b57cec5SDimitry Andric void TargetProperties::SetPreloadSymbols(bool b) {
42990b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
4300fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
43010b57cec5SDimitry Andric }
43020b57cec5SDimitry Andric 
GetDisableASLR() const43030b57cec5SDimitry Andric bool TargetProperties::GetDisableASLR() const {
43040b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
4305fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4306fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
43070b57cec5SDimitry Andric }
43080b57cec5SDimitry Andric 
SetDisableASLR(bool b)43090b57cec5SDimitry Andric void TargetProperties::SetDisableASLR(bool b) {
43100b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
4311fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
43120b57cec5SDimitry Andric }
43130b57cec5SDimitry Andric 
GetInheritTCC() const4314e8d8bef9SDimitry Andric bool TargetProperties::GetInheritTCC() const {
4315e8d8bef9SDimitry Andric   const uint32_t idx = ePropertyInheritTCC;
4316fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4317fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
4318e8d8bef9SDimitry Andric }
4319e8d8bef9SDimitry Andric 
SetInheritTCC(bool b)4320e8d8bef9SDimitry Andric void TargetProperties::SetInheritTCC(bool b) {
4321e8d8bef9SDimitry Andric   const uint32_t idx = ePropertyInheritTCC;
4322fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
4323e8d8bef9SDimitry Andric }
4324e8d8bef9SDimitry Andric 
GetDetachOnError() const43250b57cec5SDimitry Andric bool TargetProperties::GetDetachOnError() const {
43260b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
4327fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4328fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
43290b57cec5SDimitry Andric }
43300b57cec5SDimitry Andric 
SetDetachOnError(bool b)43310b57cec5SDimitry Andric void TargetProperties::SetDetachOnError(bool b) {
43320b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
4333fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
43340b57cec5SDimitry Andric }
43350b57cec5SDimitry Andric 
GetDisableSTDIO() const43360b57cec5SDimitry Andric bool TargetProperties::GetDisableSTDIO() const {
43370b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
4338fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4339fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
43400b57cec5SDimitry Andric }
43410b57cec5SDimitry Andric 
SetDisableSTDIO(bool b)43420b57cec5SDimitry Andric void TargetProperties::SetDisableSTDIO(bool b) {
43430b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
4344fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
43450b57cec5SDimitry Andric }
43460b57cec5SDimitry Andric 
GetDisassemblyFlavor() const43470b57cec5SDimitry Andric const char *TargetProperties::GetDisassemblyFlavor() const {
43480b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisassemblyFlavor;
43490b57cec5SDimitry Andric   const char *return_value;
43500b57cec5SDimitry Andric 
43510b57cec5SDimitry Andric   x86DisassemblyFlavor flavor_value =
4352fe013be4SDimitry Andric       GetPropertyAtIndexAs<x86DisassemblyFlavor>(
4353fe013be4SDimitry Andric           idx, static_cast<x86DisassemblyFlavor>(
4354fe013be4SDimitry Andric                    g_target_properties[idx].default_uint_value));
4355fe013be4SDimitry Andric 
43560b57cec5SDimitry Andric   return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
43570b57cec5SDimitry Andric   return return_value;
43580b57cec5SDimitry Andric }
43590b57cec5SDimitry Andric 
GetInlineStrategy() const43600b57cec5SDimitry Andric InlineStrategy TargetProperties::GetInlineStrategy() const {
43610b57cec5SDimitry Andric   const uint32_t idx = ePropertyInlineStrategy;
4362fe013be4SDimitry Andric   return GetPropertyAtIndexAs<InlineStrategy>(
4363fe013be4SDimitry Andric       idx,
4364fe013be4SDimitry Andric       static_cast<InlineStrategy>(g_target_properties[idx].default_uint_value));
43650b57cec5SDimitry Andric }
43660b57cec5SDimitry Andric 
GetArg0() const43670b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetArg0() const {
43680b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
4369fe013be4SDimitry Andric   return GetPropertyAtIndexAs<llvm::StringRef>(
4370fe013be4SDimitry Andric       idx, g_target_properties[idx].default_cstr_value);
43710b57cec5SDimitry Andric }
43720b57cec5SDimitry Andric 
SetArg0(llvm::StringRef arg)43730b57cec5SDimitry Andric void TargetProperties::SetArg0(llvm::StringRef arg) {
43740b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
4375fe013be4SDimitry Andric   SetPropertyAtIndex(idx, arg);
43760b57cec5SDimitry Andric   m_launch_info.SetArg0(arg);
43770b57cec5SDimitry Andric }
43780b57cec5SDimitry Andric 
GetRunArguments(Args & args) const43790b57cec5SDimitry Andric bool TargetProperties::GetRunArguments(Args &args) const {
43800b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
4381fe013be4SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args);
43820b57cec5SDimitry Andric }
43830b57cec5SDimitry Andric 
SetRunArguments(const Args & args)43840b57cec5SDimitry Andric void TargetProperties::SetRunArguments(const Args &args) {
43850b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
4386fe013be4SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(idx, args);
43870b57cec5SDimitry Andric   m_launch_info.GetArguments() = args;
43880b57cec5SDimitry Andric }
43890b57cec5SDimitry Andric 
ComputeEnvironment() const43905ffd83dbSDimitry Andric Environment TargetProperties::ComputeEnvironment() const {
43915ffd83dbSDimitry Andric   Environment env;
43925ffd83dbSDimitry Andric 
43935ffd83dbSDimitry Andric   if (m_target &&
4394fe013be4SDimitry Andric       GetPropertyAtIndexAs<bool>(
4395fe013be4SDimitry Andric           ePropertyInheritEnv,
43965ffd83dbSDimitry Andric           g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) {
43975ffd83dbSDimitry Andric     if (auto platform_sp = m_target->GetPlatform()) {
43985ffd83dbSDimitry Andric       Environment platform_env = platform_sp->GetEnvironment();
43995ffd83dbSDimitry Andric       for (const auto &KV : platform_env)
44005ffd83dbSDimitry Andric         env[KV.first()] = KV.second;
44015ffd83dbSDimitry Andric     }
44025ffd83dbSDimitry Andric   }
44035ffd83dbSDimitry Andric 
44045ffd83dbSDimitry Andric   Args property_unset_env;
4405fe013be4SDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars,
44065ffd83dbSDimitry Andric                                             property_unset_env);
44075ffd83dbSDimitry Andric   for (const auto &var : property_unset_env)
44085ffd83dbSDimitry Andric     env.erase(var.ref());
44095ffd83dbSDimitry Andric 
44105ffd83dbSDimitry Andric   Args property_env;
4411fe013be4SDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars, property_env);
44125ffd83dbSDimitry Andric   for (const auto &KV : Environment(property_env))
44135ffd83dbSDimitry Andric     env[KV.first()] = KV.second;
44145ffd83dbSDimitry Andric 
44155ffd83dbSDimitry Andric   return env;
44165ffd83dbSDimitry Andric }
44175ffd83dbSDimitry Andric 
GetEnvironment() const44180b57cec5SDimitry Andric Environment TargetProperties::GetEnvironment() const {
44195ffd83dbSDimitry Andric   return ComputeEnvironment();
44200b57cec5SDimitry Andric }
44210b57cec5SDimitry Andric 
GetInheritedEnvironment() const4422349cc55cSDimitry Andric Environment TargetProperties::GetInheritedEnvironment() const {
4423349cc55cSDimitry Andric   Environment environment;
4424349cc55cSDimitry Andric 
4425349cc55cSDimitry Andric   if (m_target == nullptr)
4426349cc55cSDimitry Andric     return environment;
4427349cc55cSDimitry Andric 
4428fe013be4SDimitry Andric   if (!GetPropertyAtIndexAs<bool>(
4429fe013be4SDimitry Andric           ePropertyInheritEnv,
4430349cc55cSDimitry Andric           g_target_properties[ePropertyInheritEnv].default_uint_value != 0))
4431349cc55cSDimitry Andric     return environment;
4432349cc55cSDimitry Andric 
4433349cc55cSDimitry Andric   PlatformSP platform_sp = m_target->GetPlatform();
4434349cc55cSDimitry Andric   if (platform_sp == nullptr)
4435349cc55cSDimitry Andric     return environment;
4436349cc55cSDimitry Andric 
4437349cc55cSDimitry Andric   Environment platform_environment = platform_sp->GetEnvironment();
4438349cc55cSDimitry Andric   for (const auto &KV : platform_environment)
4439349cc55cSDimitry Andric     environment[KV.first()] = KV.second;
4440349cc55cSDimitry Andric 
4441349cc55cSDimitry Andric   Args property_unset_environment;
4442fe013be4SDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars,
4443349cc55cSDimitry Andric                                             property_unset_environment);
4444349cc55cSDimitry Andric   for (const auto &var : property_unset_environment)
4445349cc55cSDimitry Andric     environment.erase(var.ref());
4446349cc55cSDimitry Andric 
4447349cc55cSDimitry Andric   return environment;
4448349cc55cSDimitry Andric }
4449349cc55cSDimitry Andric 
GetTargetEnvironment() const4450349cc55cSDimitry Andric Environment TargetProperties::GetTargetEnvironment() const {
4451349cc55cSDimitry Andric   Args property_environment;
4452fe013be4SDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars,
4453349cc55cSDimitry Andric                                             property_environment);
4454349cc55cSDimitry Andric   Environment environment;
4455349cc55cSDimitry Andric   for (const auto &KV : Environment(property_environment))
4456349cc55cSDimitry Andric     environment[KV.first()] = KV.second;
4457349cc55cSDimitry Andric 
4458349cc55cSDimitry Andric   return environment;
4459349cc55cSDimitry Andric }
4460349cc55cSDimitry Andric 
SetEnvironment(Environment env)44610b57cec5SDimitry Andric void TargetProperties::SetEnvironment(Environment env) {
44620b57cec5SDimitry Andric   // TODO: Get rid of the Args intermediate step
44630b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnvVars;
4464fe013be4SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(idx, Args(env));
44650b57cec5SDimitry Andric }
44660b57cec5SDimitry Andric 
GetSkipPrologue() const44670b57cec5SDimitry Andric bool TargetProperties::GetSkipPrologue() const {
44680b57cec5SDimitry Andric   const uint32_t idx = ePropertySkipPrologue;
4469fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4470fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
44710b57cec5SDimitry Andric }
44720b57cec5SDimitry Andric 
GetSourcePathMap() const44730b57cec5SDimitry Andric PathMappingList &TargetProperties::GetSourcePathMap() const {
44740b57cec5SDimitry Andric   const uint32_t idx = ePropertySourceMap;
44750b57cec5SDimitry Andric   OptionValuePathMappings *option_value =
4476fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(idx);
44770b57cec5SDimitry Andric   assert(option_value);
44780b57cec5SDimitry Andric   return option_value->GetCurrentValue();
44790b57cec5SDimitry Andric }
44800b57cec5SDimitry Andric 
GetAutoSourceMapRelative() const4481bdd1243dSDimitry Andric bool TargetProperties::GetAutoSourceMapRelative() const {
4482bdd1243dSDimitry Andric   const uint32_t idx = ePropertyAutoSourceMapRelative;
4483fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4484fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
4485bdd1243dSDimitry Andric }
4486bdd1243dSDimitry Andric 
AppendExecutableSearchPaths(const FileSpec & dir)44870b57cec5SDimitry Andric void TargetProperties::AppendExecutableSearchPaths(const FileSpec &dir) {
44880b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
44890b57cec5SDimitry Andric   OptionValueFileSpecList *option_value =
4490fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(idx);
44910b57cec5SDimitry Andric   assert(option_value);
44920b57cec5SDimitry Andric   option_value->AppendCurrentValue(dir);
44930b57cec5SDimitry Andric }
44940b57cec5SDimitry Andric 
GetExecutableSearchPaths()44950b57cec5SDimitry Andric FileSpecList TargetProperties::GetExecutableSearchPaths() {
44960b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
4497fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpecList>(idx, {});
44980b57cec5SDimitry Andric }
44990b57cec5SDimitry Andric 
GetDebugFileSearchPaths()45000b57cec5SDimitry Andric FileSpecList TargetProperties::GetDebugFileSearchPaths() {
45010b57cec5SDimitry Andric   const uint32_t idx = ePropertyDebugFileSearchPaths;
4502fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpecList>(idx, {});
45030b57cec5SDimitry Andric }
45040b57cec5SDimitry Andric 
GetClangModuleSearchPaths()45050b57cec5SDimitry Andric FileSpecList TargetProperties::GetClangModuleSearchPaths() {
45060b57cec5SDimitry Andric   const uint32_t idx = ePropertyClangModuleSearchPaths;
4507fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpecList>(idx, {});
45080b57cec5SDimitry Andric }
45090b57cec5SDimitry Andric 
GetEnableAutoImportClangModules() const45100b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoImportClangModules() const {
45110b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoImportClangModules;
4512fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4513fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
45140b57cec5SDimitry Andric }
45150b57cec5SDimitry Andric 
GetImportStdModule() const4516e8d8bef9SDimitry Andric ImportStdModule TargetProperties::GetImportStdModule() const {
45170b57cec5SDimitry Andric   const uint32_t idx = ePropertyImportStdModule;
4518fe013be4SDimitry Andric   return GetPropertyAtIndexAs<ImportStdModule>(
4519fe013be4SDimitry Andric       idx, static_cast<ImportStdModule>(
4520fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
45210b57cec5SDimitry Andric }
45220b57cec5SDimitry Andric 
GetDynamicClassInfoHelper() const452381ad6265SDimitry Andric DynamicClassInfoHelper TargetProperties::GetDynamicClassInfoHelper() const {
452481ad6265SDimitry Andric   const uint32_t idx = ePropertyDynamicClassInfoHelper;
4525fe013be4SDimitry Andric   return GetPropertyAtIndexAs<DynamicClassInfoHelper>(
4526fe013be4SDimitry Andric       idx, static_cast<DynamicClassInfoHelper>(
4527fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
452881ad6265SDimitry Andric }
452981ad6265SDimitry Andric 
GetEnableAutoApplyFixIts() const45300b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoApplyFixIts() const {
45310b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoApplyFixIts;
4532fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4533fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
45340b57cec5SDimitry Andric }
45350b57cec5SDimitry Andric 
GetNumberOfRetriesWithFixits() const45365ffd83dbSDimitry Andric uint64_t TargetProperties::GetNumberOfRetriesWithFixits() const {
45375ffd83dbSDimitry Andric   const uint32_t idx = ePropertyRetriesWithFixIts;
4538fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4539fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
45405ffd83dbSDimitry Andric }
45415ffd83dbSDimitry Andric 
GetEnableNotifyAboutFixIts() const45420b57cec5SDimitry Andric bool TargetProperties::GetEnableNotifyAboutFixIts() const {
45430b57cec5SDimitry Andric   const uint32_t idx = ePropertyNotifyAboutFixIts;
4544fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4545fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
45460b57cec5SDimitry Andric }
45470b57cec5SDimitry Andric 
GetSaveJITObjectsDir() const454881ad6265SDimitry Andric FileSpec TargetProperties::GetSaveJITObjectsDir() const {
454981ad6265SDimitry Andric   const uint32_t idx = ePropertySaveObjectsDir;
4550fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(idx, {});
455181ad6265SDimitry Andric }
455281ad6265SDimitry Andric 
CheckJITObjectsDir()455381ad6265SDimitry Andric void TargetProperties::CheckJITObjectsDir() {
455481ad6265SDimitry Andric   FileSpec new_dir = GetSaveJITObjectsDir();
455581ad6265SDimitry Andric   if (!new_dir)
455681ad6265SDimitry Andric     return;
455781ad6265SDimitry Andric 
455881ad6265SDimitry Andric   const FileSystem &instance = FileSystem::Instance();
455981ad6265SDimitry Andric   bool exists = instance.Exists(new_dir);
456081ad6265SDimitry Andric   bool is_directory = instance.IsDirectory(new_dir);
456181ad6265SDimitry Andric   std::string path = new_dir.GetPath(true);
456281ad6265SDimitry Andric   bool writable = llvm::sys::fs::can_write(path);
456381ad6265SDimitry Andric   if (exists && is_directory && writable)
456481ad6265SDimitry Andric     return;
456581ad6265SDimitry Andric 
4566fe013be4SDimitry Andric   m_collection_sp->GetPropertyAtIndex(ePropertySaveObjectsDir)
456781ad6265SDimitry Andric       ->GetValue()
456881ad6265SDimitry Andric       ->Clear();
456981ad6265SDimitry Andric 
457081ad6265SDimitry Andric   std::string buffer;
457181ad6265SDimitry Andric   llvm::raw_string_ostream os(buffer);
457281ad6265SDimitry Andric   os << "JIT object dir '" << path << "' ";
457381ad6265SDimitry Andric   if (!exists)
457481ad6265SDimitry Andric     os << "does not exist";
457581ad6265SDimitry Andric   else if (!is_directory)
457681ad6265SDimitry Andric     os << "is not a directory";
457781ad6265SDimitry Andric   else if (!writable)
457881ad6265SDimitry Andric     os << "is not writable";
457981ad6265SDimitry Andric 
4580bdd1243dSDimitry Andric   std::optional<lldb::user_id_t> debugger_id;
458181ad6265SDimitry Andric   if (m_target)
458281ad6265SDimitry Andric     debugger_id = m_target->GetDebugger().GetID();
458381ad6265SDimitry Andric   Debugger::ReportError(os.str(), debugger_id);
45840b57cec5SDimitry Andric }
45850b57cec5SDimitry Andric 
GetEnableSyntheticValue() const45860b57cec5SDimitry Andric bool TargetProperties::GetEnableSyntheticValue() const {
45870b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnableSynthetic;
4588fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4589fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
45909dba64beSDimitry Andric }
45919dba64beSDimitry Andric 
ShowHexVariableValuesWithLeadingZeroes() const4592c9157d92SDimitry Andric bool TargetProperties::ShowHexVariableValuesWithLeadingZeroes() const {
4593c9157d92SDimitry Andric   const uint32_t idx = ePropertyShowHexVariableValuesWithLeadingZeroes;
4594c9157d92SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4595c9157d92SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
4596c9157d92SDimitry Andric }
4597c9157d92SDimitry Andric 
GetMaxZeroPaddingInFloatFormat() const45989dba64beSDimitry Andric uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
45999dba64beSDimitry Andric   const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
4600fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4601fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
46020b57cec5SDimitry Andric }
46030b57cec5SDimitry Andric 
GetMaximumNumberOfChildrenToDisplay() const46040b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const {
46050b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxChildrenCount;
4606fe013be4SDimitry Andric   return GetPropertyAtIndexAs<int64_t>(
4607fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
46080b57cec5SDimitry Andric }
46090b57cec5SDimitry Andric 
461081ad6265SDimitry Andric std::pair<uint32_t, bool>
GetMaximumDepthOfChildrenToDisplay() const461181ad6265SDimitry Andric TargetProperties::GetMaximumDepthOfChildrenToDisplay() const {
461281ad6265SDimitry Andric   const uint32_t idx = ePropertyMaxChildrenDepth;
461381ad6265SDimitry Andric   auto *option_value =
4614fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(idx);
461581ad6265SDimitry Andric   bool is_default = !option_value->OptionWasSet();
461681ad6265SDimitry Andric   return {option_value->GetCurrentValue(), is_default};
461781ad6265SDimitry Andric }
461881ad6265SDimitry Andric 
GetMaximumSizeOfStringSummary() const46190b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const {
46200b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxSummaryLength;
4621fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4622fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
46230b57cec5SDimitry Andric }
46240b57cec5SDimitry Andric 
GetMaximumMemReadSize() const46250b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumMemReadSize() const {
46260b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxMemReadSize;
4627fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4628fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
46290b57cec5SDimitry Andric }
46300b57cec5SDimitry Andric 
GetStandardInputPath() const46310b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardInputPath() const {
46320b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
4633fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(idx, {});
46340b57cec5SDimitry Andric }
46350b57cec5SDimitry Andric 
SetStandardInputPath(llvm::StringRef path)46360b57cec5SDimitry Andric void TargetProperties::SetStandardInputPath(llvm::StringRef path) {
46370b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
4638fe013be4SDimitry Andric   SetPropertyAtIndex(idx, path);
46390b57cec5SDimitry Andric }
46400b57cec5SDimitry Andric 
GetStandardOutputPath() const46410b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardOutputPath() const {
46420b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
4643fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(idx, {});
46440b57cec5SDimitry Andric }
46450b57cec5SDimitry Andric 
SetStandardOutputPath(llvm::StringRef path)46460b57cec5SDimitry Andric void TargetProperties::SetStandardOutputPath(llvm::StringRef path) {
46470b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
4648fe013be4SDimitry Andric   SetPropertyAtIndex(idx, path);
46490b57cec5SDimitry Andric }
46500b57cec5SDimitry Andric 
GetStandardErrorPath() const46510b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardErrorPath() const {
46520b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
4653fe013be4SDimitry Andric   return GetPropertyAtIndexAs<FileSpec>(idx, {});
46540b57cec5SDimitry Andric }
46550b57cec5SDimitry Andric 
SetStandardErrorPath(llvm::StringRef path)46560b57cec5SDimitry Andric void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
46570b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
4658fe013be4SDimitry Andric   SetPropertyAtIndex(idx, path);
46590b57cec5SDimitry Andric }
46600b57cec5SDimitry Andric 
GetLanguage() const46610b57cec5SDimitry Andric LanguageType TargetProperties::GetLanguage() const {
4662fe013be4SDimitry Andric   const uint32_t idx = ePropertyLanguage;
4663fe013be4SDimitry Andric   return GetPropertyAtIndexAs<LanguageType>(idx, {});
46640b57cec5SDimitry Andric }
46650b57cec5SDimitry Andric 
GetExpressionPrefixContents()46660b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
46670b57cec5SDimitry Andric   const uint32_t idx = ePropertyExprPrefix;
46680b57cec5SDimitry Andric   OptionValueFileSpec *file =
4669fe013be4SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(idx);
46700b57cec5SDimitry Andric   if (file) {
46710b57cec5SDimitry Andric     DataBufferSP data_sp(file->GetFileContents());
46720b57cec5SDimitry Andric     if (data_sp)
46730b57cec5SDimitry Andric       return llvm::StringRef(
46740b57cec5SDimitry Andric           reinterpret_cast<const char *>(data_sp->GetBytes()),
46750b57cec5SDimitry Andric           data_sp->GetByteSize());
46760b57cec5SDimitry Andric   }
46770b57cec5SDimitry Andric   return "";
46780b57cec5SDimitry Andric }
46790b57cec5SDimitry Andric 
GetExprErrorLimit() const4680e8d8bef9SDimitry Andric uint64_t TargetProperties::GetExprErrorLimit() const {
4681e8d8bef9SDimitry Andric   const uint32_t idx = ePropertyExprErrorLimit;
4682fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4683fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
4684fe013be4SDimitry Andric }
4685fe013be4SDimitry Andric 
GetExprAllocAddress() const4686fe013be4SDimitry Andric uint64_t TargetProperties::GetExprAllocAddress() const {
4687fe013be4SDimitry Andric   const uint32_t idx = ePropertyExprAllocAddress;
4688fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4689fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
4690fe013be4SDimitry Andric }
4691fe013be4SDimitry Andric 
GetExprAllocSize() const4692fe013be4SDimitry Andric uint64_t TargetProperties::GetExprAllocSize() const {
4693fe013be4SDimitry Andric   const uint32_t idx = ePropertyExprAllocSize;
4694fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4695fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
4696fe013be4SDimitry Andric }
4697fe013be4SDimitry Andric 
GetExprAllocAlign() const4698fe013be4SDimitry Andric uint64_t TargetProperties::GetExprAllocAlign() const {
4699fe013be4SDimitry Andric   const uint32_t idx = ePropertyExprAllocAlign;
4700fe013be4SDimitry Andric   return GetPropertyAtIndexAs<uint64_t>(
4701fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value);
4702e8d8bef9SDimitry Andric }
4703e8d8bef9SDimitry Andric 
GetBreakpointsConsultPlatformAvoidList()47040b57cec5SDimitry Andric bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() {
47050b57cec5SDimitry Andric   const uint32_t idx = ePropertyBreakpointUseAvoidList;
4706fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4707fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47080b57cec5SDimitry Andric }
47090b57cec5SDimitry Andric 
GetUseHexImmediates() const47100b57cec5SDimitry Andric bool TargetProperties::GetUseHexImmediates() const {
47110b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseHexImmediates;
4712fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4713fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47140b57cec5SDimitry Andric }
47150b57cec5SDimitry Andric 
GetUseFastStepping() const47160b57cec5SDimitry Andric bool TargetProperties::GetUseFastStepping() const {
47170b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseFastStepping;
4718fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4719fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47200b57cec5SDimitry Andric }
47210b57cec5SDimitry Andric 
GetDisplayExpressionsInCrashlogs() const47220b57cec5SDimitry Andric bool TargetProperties::GetDisplayExpressionsInCrashlogs() const {
47230b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
4724fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4725fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47260b57cec5SDimitry Andric }
47270b57cec5SDimitry Andric 
GetLoadScriptFromSymbolFile() const47280b57cec5SDimitry Andric LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const {
47290b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
4730fe013be4SDimitry Andric   return GetPropertyAtIndexAs<LoadScriptFromSymFile>(
4731fe013be4SDimitry Andric       idx, static_cast<LoadScriptFromSymFile>(
4732fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
47330b57cec5SDimitry Andric }
47340b57cec5SDimitry Andric 
GetLoadCWDlldbinitFile() const47350b57cec5SDimitry Andric LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const {
47360b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadCWDlldbinitFile;
4737fe013be4SDimitry Andric   return GetPropertyAtIndexAs<LoadCWDlldbinitFile>(
4738fe013be4SDimitry Andric       idx, static_cast<LoadCWDlldbinitFile>(
4739fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
47400b57cec5SDimitry Andric }
47410b57cec5SDimitry Andric 
GetHexImmediateStyle() const47420b57cec5SDimitry Andric Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const {
47430b57cec5SDimitry Andric   const uint32_t idx = ePropertyHexImmediateStyle;
4744fe013be4SDimitry Andric   return GetPropertyAtIndexAs<Disassembler::HexImmediateStyle>(
4745fe013be4SDimitry Andric       idx, static_cast<Disassembler::HexImmediateStyle>(
4746fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
47470b57cec5SDimitry Andric }
47480b57cec5SDimitry Andric 
GetMemoryModuleLoadLevel() const47490b57cec5SDimitry Andric MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const {
47500b57cec5SDimitry Andric   const uint32_t idx = ePropertyMemoryModuleLoadLevel;
4751fe013be4SDimitry Andric   return GetPropertyAtIndexAs<MemoryModuleLoadLevel>(
4752fe013be4SDimitry Andric       idx, static_cast<MemoryModuleLoadLevel>(
4753fe013be4SDimitry Andric                g_target_properties[idx].default_uint_value));
47540b57cec5SDimitry Andric }
47550b57cec5SDimitry Andric 
GetUserSpecifiedTrapHandlerNames(Args & args) const47560b57cec5SDimitry Andric bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const {
47570b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
4758fe013be4SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args);
47590b57cec5SDimitry Andric }
47600b57cec5SDimitry Andric 
SetUserSpecifiedTrapHandlerNames(const Args & args)47610b57cec5SDimitry Andric void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) {
47620b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
4763fe013be4SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(idx, args);
47640b57cec5SDimitry Andric }
47650b57cec5SDimitry Andric 
GetDisplayRuntimeSupportValues() const47660b57cec5SDimitry Andric bool TargetProperties::GetDisplayRuntimeSupportValues() const {
47670b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
4768fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4769fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47700b57cec5SDimitry Andric }
47710b57cec5SDimitry Andric 
SetDisplayRuntimeSupportValues(bool b)47720b57cec5SDimitry Andric void TargetProperties::SetDisplayRuntimeSupportValues(bool b) {
47730b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
4774fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
47750b57cec5SDimitry Andric }
47760b57cec5SDimitry Andric 
GetDisplayRecognizedArguments() const47770b57cec5SDimitry Andric bool TargetProperties::GetDisplayRecognizedArguments() const {
47780b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
4779fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4780fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
47810b57cec5SDimitry Andric }
47820b57cec5SDimitry Andric 
SetDisplayRecognizedArguments(bool b)47830b57cec5SDimitry Andric void TargetProperties::SetDisplayRecognizedArguments(bool b) {
47840b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
4785fe013be4SDimitry Andric   SetPropertyAtIndex(idx, b);
47860b57cec5SDimitry Andric }
47870b57cec5SDimitry Andric 
GetProcessLaunchInfo() const4788fe6060f1SDimitry Andric const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() const {
47890b57cec5SDimitry Andric   return m_launch_info;
47900b57cec5SDimitry Andric }
47910b57cec5SDimitry Andric 
SetProcessLaunchInfo(const ProcessLaunchInfo & launch_info)47920b57cec5SDimitry Andric void TargetProperties::SetProcessLaunchInfo(
47930b57cec5SDimitry Andric     const ProcessLaunchInfo &launch_info) {
47940b57cec5SDimitry Andric   m_launch_info = launch_info;
47950b57cec5SDimitry Andric   SetArg0(launch_info.GetArg0());
47960b57cec5SDimitry Andric   SetRunArguments(launch_info.GetArguments());
47970b57cec5SDimitry Andric   SetEnvironment(launch_info.GetEnvironment());
47980b57cec5SDimitry Andric   const FileAction *input_file_action =
47990b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDIN_FILENO);
48000b57cec5SDimitry Andric   if (input_file_action) {
48010b57cec5SDimitry Andric     SetStandardInputPath(input_file_action->GetPath());
48020b57cec5SDimitry Andric   }
48030b57cec5SDimitry Andric   const FileAction *output_file_action =
48040b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDOUT_FILENO);
48050b57cec5SDimitry Andric   if (output_file_action) {
48060b57cec5SDimitry Andric     SetStandardOutputPath(output_file_action->GetPath());
48070b57cec5SDimitry Andric   }
48080b57cec5SDimitry Andric   const FileAction *error_file_action =
48090b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDERR_FILENO);
48100b57cec5SDimitry Andric   if (error_file_action) {
48110b57cec5SDimitry Andric     SetStandardErrorPath(error_file_action->GetPath());
48120b57cec5SDimitry Andric   }
48130b57cec5SDimitry Andric   SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
48140b57cec5SDimitry Andric   SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4815e8d8bef9SDimitry Andric   SetInheritTCC(
4816e8d8bef9SDimitry Andric       launch_info.GetFlags().Test(lldb::eLaunchFlagInheritTCCFromParent));
48170b57cec5SDimitry Andric   SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
48180b57cec5SDimitry Andric }
48190b57cec5SDimitry Andric 
GetRequireHardwareBreakpoints() const48200b57cec5SDimitry Andric bool TargetProperties::GetRequireHardwareBreakpoints() const {
48210b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
4822fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4823fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
48240b57cec5SDimitry Andric }
48250b57cec5SDimitry Andric 
SetRequireHardwareBreakpoints(bool b)48260b57cec5SDimitry Andric void TargetProperties::SetRequireHardwareBreakpoints(bool b) {
48270b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
4828fe013be4SDimitry Andric   m_collection_sp->SetPropertyAtIndex(idx, b);
48290b57cec5SDimitry Andric }
48300b57cec5SDimitry Andric 
GetAutoInstallMainExecutable() const48315ffd83dbSDimitry Andric bool TargetProperties::GetAutoInstallMainExecutable() const {
48325ffd83dbSDimitry Andric   const uint32_t idx = ePropertyAutoInstallMainExecutable;
4833fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4834fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
48355ffd83dbSDimitry Andric }
48365ffd83dbSDimitry Andric 
Arg0ValueChangedCallback()4837480093f4SDimitry Andric void TargetProperties::Arg0ValueChangedCallback() {
4838480093f4SDimitry Andric   m_launch_info.SetArg0(GetArg0());
48390b57cec5SDimitry Andric }
48400b57cec5SDimitry Andric 
RunArgsValueChangedCallback()4841480093f4SDimitry Andric void TargetProperties::RunArgsValueChangedCallback() {
48420b57cec5SDimitry Andric   Args args;
4843480093f4SDimitry Andric   if (GetRunArguments(args))
4844480093f4SDimitry Andric     m_launch_info.GetArguments() = args;
48450b57cec5SDimitry Andric }
48460b57cec5SDimitry Andric 
EnvVarsValueChangedCallback()4847480093f4SDimitry Andric void TargetProperties::EnvVarsValueChangedCallback() {
48485ffd83dbSDimitry Andric   m_launch_info.GetEnvironment() = ComputeEnvironment();
48490b57cec5SDimitry Andric }
48500b57cec5SDimitry Andric 
InputPathValueChangedCallback()4851480093f4SDimitry Andric void TargetProperties::InputPathValueChangedCallback() {
4852480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDIN_FILENO, GetStandardInputPath(), true,
4853480093f4SDimitry Andric                                      false);
48540b57cec5SDimitry Andric }
48550b57cec5SDimitry Andric 
OutputPathValueChangedCallback()4856480093f4SDimitry Andric void TargetProperties::OutputPathValueChangedCallback() {
4857480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDOUT_FILENO, GetStandardOutputPath(),
4858480093f4SDimitry Andric                                      false, true);
48590b57cec5SDimitry Andric }
48600b57cec5SDimitry Andric 
ErrorPathValueChangedCallback()4861480093f4SDimitry Andric void TargetProperties::ErrorPathValueChangedCallback() {
4862480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDERR_FILENO, GetStandardErrorPath(),
4863480093f4SDimitry Andric                                      false, true);
48640b57cec5SDimitry Andric }
48650b57cec5SDimitry Andric 
DetachOnErrorValueChangedCallback()4866480093f4SDimitry Andric void TargetProperties::DetachOnErrorValueChangedCallback() {
4867480093f4SDimitry Andric   if (GetDetachOnError())
4868480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
48690b57cec5SDimitry Andric   else
4870480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
48710b57cec5SDimitry Andric }
48720b57cec5SDimitry Andric 
DisableASLRValueChangedCallback()4873480093f4SDimitry Andric void TargetProperties::DisableASLRValueChangedCallback() {
4874480093f4SDimitry Andric   if (GetDisableASLR())
4875480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
48760b57cec5SDimitry Andric   else
4877480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
48780b57cec5SDimitry Andric }
48790b57cec5SDimitry Andric 
InheritTCCValueChangedCallback()4880e8d8bef9SDimitry Andric void TargetProperties::InheritTCCValueChangedCallback() {
4881e8d8bef9SDimitry Andric   if (GetInheritTCC())
4882e8d8bef9SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagInheritTCCFromParent);
4883e8d8bef9SDimitry Andric   else
4884e8d8bef9SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagInheritTCCFromParent);
4885e8d8bef9SDimitry Andric }
4886e8d8bef9SDimitry Andric 
DisableSTDIOValueChangedCallback()4887480093f4SDimitry Andric void TargetProperties::DisableSTDIOValueChangedCallback() {
4888480093f4SDimitry Andric   if (GetDisableSTDIO())
4889480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
48900b57cec5SDimitry Andric   else
4891480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
48920b57cec5SDimitry Andric }
48930b57cec5SDimitry Andric 
GetDebugUtilityExpression() const4894fe6060f1SDimitry Andric bool TargetProperties::GetDebugUtilityExpression() const {
4895fe6060f1SDimitry Andric   const uint32_t idx = ePropertyDebugUtilityExpression;
4896fe013be4SDimitry Andric   return GetPropertyAtIndexAs<bool>(
4897fe013be4SDimitry Andric       idx, g_target_properties[idx].default_uint_value != 0);
4898fe6060f1SDimitry Andric }
4899fe6060f1SDimitry Andric 
SetDebugUtilityExpression(bool debug)4900fe6060f1SDimitry Andric void TargetProperties::SetDebugUtilityExpression(bool debug) {
4901fe6060f1SDimitry Andric   const uint32_t idx = ePropertyDebugUtilityExpression;
4902fe013be4SDimitry Andric   SetPropertyAtIndex(idx, debug);
4903fe6060f1SDimitry Andric }
4904fe6060f1SDimitry Andric 
49050b57cec5SDimitry Andric // Target::TargetEventData
49060b57cec5SDimitry Andric 
TargetEventData(const lldb::TargetSP & target_sp)49070b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
49080b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list() {}
49090b57cec5SDimitry Andric 
TargetEventData(const lldb::TargetSP & target_sp,const ModuleList & module_list)49100b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
49110b57cec5SDimitry Andric                                          const ModuleList &module_list)
49120b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
49130b57cec5SDimitry Andric 
49140b57cec5SDimitry Andric Target::TargetEventData::~TargetEventData() = default;
49150b57cec5SDimitry Andric 
GetFlavorString()4916fe013be4SDimitry Andric llvm::StringRef Target::TargetEventData::GetFlavorString() {
4917fe013be4SDimitry Andric   return "Target::TargetEventData";
49180b57cec5SDimitry Andric }
49190b57cec5SDimitry Andric 
Dump(Stream * s) const49200b57cec5SDimitry Andric void Target::TargetEventData::Dump(Stream *s) const {
49210b57cec5SDimitry Andric   for (size_t i = 0; i < m_module_list.GetSize(); ++i) {
49220b57cec5SDimitry Andric     if (i != 0)
49230b57cec5SDimitry Andric       *s << ", ";
49240b57cec5SDimitry Andric     m_module_list.GetModuleAtIndex(i)->GetDescription(
4925480093f4SDimitry Andric         s->AsRawOstream(), lldb::eDescriptionLevelBrief);
49260b57cec5SDimitry Andric   }
49270b57cec5SDimitry Andric }
49280b57cec5SDimitry Andric 
49290b57cec5SDimitry Andric const Target::TargetEventData *
GetEventDataFromEvent(const Event * event_ptr)49300b57cec5SDimitry Andric Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) {
49310b57cec5SDimitry Andric   if (event_ptr) {
49320b57cec5SDimitry Andric     const EventData *event_data = event_ptr->GetData();
49330b57cec5SDimitry Andric     if (event_data &&
49340b57cec5SDimitry Andric         event_data->GetFlavor() == TargetEventData::GetFlavorString())
49350b57cec5SDimitry Andric       return static_cast<const TargetEventData *>(event_ptr->GetData());
49360b57cec5SDimitry Andric   }
49370b57cec5SDimitry Andric   return nullptr;
49380b57cec5SDimitry Andric }
49390b57cec5SDimitry Andric 
GetTargetFromEvent(const Event * event_ptr)49400b57cec5SDimitry Andric TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
49410b57cec5SDimitry Andric   TargetSP target_sp;
49420b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
49430b57cec5SDimitry Andric   if (event_data)
49440b57cec5SDimitry Andric     target_sp = event_data->m_target_sp;
49450b57cec5SDimitry Andric   return target_sp;
49460b57cec5SDimitry Andric }
49470b57cec5SDimitry Andric 
49480b57cec5SDimitry Andric ModuleList
GetModuleListFromEvent(const Event * event_ptr)49490b57cec5SDimitry Andric Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
49500b57cec5SDimitry Andric   ModuleList module_list;
49510b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
49520b57cec5SDimitry Andric   if (event_data)
49530b57cec5SDimitry Andric     module_list = event_data->m_module_list;
49540b57cec5SDimitry Andric   return module_list;
49550b57cec5SDimitry Andric }
49569dba64beSDimitry Andric 
GetAPIMutex()49579dba64beSDimitry Andric std::recursive_mutex &Target::GetAPIMutex() {
49589dba64beSDimitry Andric   if (GetProcessSP() && GetProcessSP()->CurrentThreadIsPrivateStateThread())
49599dba64beSDimitry Andric     return m_private_mutex;
49609dba64beSDimitry Andric   else
49619dba64beSDimitry Andric     return m_mutex;
49629dba64beSDimitry Andric }
4963349cc55cSDimitry Andric 
4964349cc55cSDimitry Andric /// Get metrics associated with this target in JSON format.
ReportStatistics()4965349cc55cSDimitry Andric llvm::json::Value Target::ReportStatistics() { return m_stats.ToJSON(*this); }
4966