15ffd83dbSDimitry Andric //===-- Target.cpp --------------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
10*0b57cec5SDimitry Andric #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
11*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
12*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointPrecondition.h"
13*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolver.h"
14*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverAddress.h"
15*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
16*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
17*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverName.h"
18*0b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
19*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Watchpoint.h"
20*0b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
21*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
22*0b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
23*0b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
24*0b57cec5SDimitry Andric #include "lldb/Core/SearchFilter.h"
25*0b57cec5SDimitry Andric #include "lldb/Core/Section.h"
26*0b57cec5SDimitry Andric #include "lldb/Core/SourceManager.h"
27*0b57cec5SDimitry Andric #include "lldb/Core/StreamFile.h"
28*0b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
29*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
305ffd83dbSDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
31*0b57cec5SDimitry Andric #include "lldb/Expression/REPL.h"
32*0b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h"
33*0b57cec5SDimitry Andric #include "lldb/Host/Host.h"
34*0b57cec5SDimitry Andric #include "lldb/Host/PosixApi.h"
35*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
36*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
37*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupWatchpoint.h"
38*0b57cec5SDimitry Andric #include "lldb/Interpreter/OptionValues.h"
39*0b57cec5SDimitry Andric #include "lldb/Interpreter/Property.h"
40*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
41*0b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
42*0b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
43*0b57cec5SDimitry Andric #include "lldb/Target/Language.h"
44*0b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
45*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
46*0b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
47*0b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
48*0b57cec5SDimitry Andric #include "lldb/Target/SystemRuntime.h"
49*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
50*0b57cec5SDimitry Andric #include "lldb/Target/ThreadSpec.h"
51*0b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
52*0b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
53*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
54*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
55*0b57cec5SDimitry Andric #include "lldb/Utility/State.h"
56*0b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
57*0b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric #include <memory>
62*0b57cec5SDimitry Andric #include <mutex>
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric using namespace lldb;
65*0b57cec5SDimitry Andric using namespace lldb_private;
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric Target::Arch::Arch(const ArchSpec &spec)
70*0b57cec5SDimitry Andric     : m_spec(spec),
71*0b57cec5SDimitry Andric       m_plugin_up(PluginManager::CreateArchitectureInstance(spec)) {}
72*0b57cec5SDimitry Andric 
73*0b57cec5SDimitry Andric const Target::Arch &Target::Arch::operator=(const ArchSpec &spec) {
74*0b57cec5SDimitry Andric   m_spec = spec;
75*0b57cec5SDimitry Andric   m_plugin_up = PluginManager::CreateArchitectureInstance(spec);
76*0b57cec5SDimitry Andric   return *this;
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric ConstString &Target::GetStaticBroadcasterClass() {
80*0b57cec5SDimitry Andric   static ConstString class_name("lldb.target");
81*0b57cec5SDimitry Andric   return class_name;
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric Target::Target(Debugger &debugger, const ArchSpec &target_arch,
85*0b57cec5SDimitry Andric                const lldb::PlatformSP &platform_sp, bool is_dummy_target)
86*0b57cec5SDimitry Andric     : TargetProperties(this),
87*0b57cec5SDimitry Andric       Broadcaster(debugger.GetBroadcasterManager(),
88*0b57cec5SDimitry Andric                   Target::GetStaticBroadcasterClass().AsCString()),
89*0b57cec5SDimitry Andric       ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp),
90*0b57cec5SDimitry Andric       m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
91*0b57cec5SDimitry Andric       m_breakpoint_list(false), m_internal_breakpoint_list(true),
92*0b57cec5SDimitry Andric       m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
935ffd83dbSDimitry Andric       m_image_search_paths(ImageSearchPathsChanged, this),
94*0b57cec5SDimitry Andric       m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0),
95*0b57cec5SDimitry Andric       m_valid(true), m_suppress_stop_hooks(false),
96*0b57cec5SDimitry Andric       m_is_dummy_target(is_dummy_target),
97*0b57cec5SDimitry Andric       m_stats_storage(static_cast<int>(StatisticKind::StatisticMax))
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric {
100*0b57cec5SDimitry Andric   SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
101*0b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
102*0b57cec5SDimitry Andric   SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
103*0b57cec5SDimitry Andric   SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
104*0b57cec5SDimitry Andric   SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric   CheckInWithManager();
107*0b57cec5SDimitry Andric 
1089dba64beSDimitry Andric   LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT),
1099dba64beSDimitry Andric            "{0} Target::Target()", static_cast<void *>(this));
110*0b57cec5SDimitry Andric   if (target_arch.IsValid()) {
1119dba64beSDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET),
1129dba64beSDimitry Andric              "Target::Target created with architecture {0} ({1})",
113*0b57cec5SDimitry Andric              target_arch.GetArchitectureName(),
114*0b57cec5SDimitry Andric              target_arch.GetTriple().getTriple().c_str());
115*0b57cec5SDimitry Andric   }
1165ffd83dbSDimitry Andric 
1175ffd83dbSDimitry Andric   UpdateLaunchInfoFromProperties();
118*0b57cec5SDimitry Andric }
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric Target::~Target() {
121*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
1229dba64beSDimitry Andric   LLDB_LOG(log, "{0} Target::~Target()", static_cast<void *>(this));
123*0b57cec5SDimitry Andric   DeleteCurrentProcess();
124*0b57cec5SDimitry Andric }
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric void Target::PrimeFromDummyTarget(Target *target) {
127*0b57cec5SDimitry Andric   if (!target)
128*0b57cec5SDimitry Andric     return;
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric   m_stop_hooks = target->m_stop_hooks;
131*0b57cec5SDimitry Andric 
1325ffd83dbSDimitry Andric   for (const auto &breakpoint_sp : target->m_breakpoint_list.Breakpoints()) {
133*0b57cec5SDimitry Andric     if (breakpoint_sp->IsInternal())
134*0b57cec5SDimitry Andric       continue;
135*0b57cec5SDimitry Andric 
1365ffd83dbSDimitry Andric     BreakpointSP new_bp(
1375ffd83dbSDimitry Andric         Breakpoint::CopyFromBreakpoint(shared_from_this(), *breakpoint_sp));
1385ffd83dbSDimitry Andric     AddBreakpoint(std::move(new_bp), false);
139*0b57cec5SDimitry Andric   }
140*0b57cec5SDimitry Andric 
1419dba64beSDimitry Andric   for (auto bp_name_entry : target->m_breakpoint_names) {
142*0b57cec5SDimitry Andric 
143*0b57cec5SDimitry Andric     BreakpointName *new_bp_name = new BreakpointName(*bp_name_entry.second);
144*0b57cec5SDimitry Andric     AddBreakpointName(new_bp_name);
145*0b57cec5SDimitry Andric   }
146*0b57cec5SDimitry Andric }
147*0b57cec5SDimitry Andric 
148*0b57cec5SDimitry Andric void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {
149*0b57cec5SDimitry Andric   //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
150*0b57cec5SDimitry Andric   if (description_level != lldb::eDescriptionLevelBrief) {
151*0b57cec5SDimitry Andric     s->Indent();
152*0b57cec5SDimitry Andric     s->PutCString("Target\n");
153*0b57cec5SDimitry Andric     s->IndentMore();
154*0b57cec5SDimitry Andric     m_images.Dump(s);
155*0b57cec5SDimitry Andric     m_breakpoint_list.Dump(s);
156*0b57cec5SDimitry Andric     m_internal_breakpoint_list.Dump(s);
157*0b57cec5SDimitry Andric     s->IndentLess();
158*0b57cec5SDimitry Andric   } else {
159*0b57cec5SDimitry Andric     Module *exe_module = GetExecutableModulePointer();
160*0b57cec5SDimitry Andric     if (exe_module)
161*0b57cec5SDimitry Andric       s->PutCString(exe_module->GetFileSpec().GetFilename().GetCString());
162*0b57cec5SDimitry Andric     else
163*0b57cec5SDimitry Andric       s->PutCString("No executable module.");
164*0b57cec5SDimitry Andric   }
165*0b57cec5SDimitry Andric }
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric void Target::CleanupProcess() {
168*0b57cec5SDimitry Andric   // Do any cleanup of the target we need to do between process instances.
169*0b57cec5SDimitry Andric   // NB It is better to do this before destroying the process in case the
170*0b57cec5SDimitry Andric   // clean up needs some help from the process.
171*0b57cec5SDimitry Andric   m_breakpoint_list.ClearAllBreakpointSites();
172*0b57cec5SDimitry Andric   m_internal_breakpoint_list.ClearAllBreakpointSites();
173*0b57cec5SDimitry Andric   // Disable watchpoints just on the debugger side.
174*0b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
175*0b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
176*0b57cec5SDimitry Andric   DisableAllWatchpoints(false);
177*0b57cec5SDimitry Andric   ClearAllWatchpointHitCounts();
178*0b57cec5SDimitry Andric   ClearAllWatchpointHistoricValues();
179*0b57cec5SDimitry Andric }
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric void Target::DeleteCurrentProcess() {
182*0b57cec5SDimitry Andric   if (m_process_sp) {
183*0b57cec5SDimitry Andric     m_section_load_history.Clear();
184*0b57cec5SDimitry Andric     if (m_process_sp->IsAlive())
185*0b57cec5SDimitry Andric       m_process_sp->Destroy(false);
186*0b57cec5SDimitry Andric 
187*0b57cec5SDimitry Andric     m_process_sp->Finalize();
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric     CleanupProcess();
190*0b57cec5SDimitry Andric 
191*0b57cec5SDimitry Andric     m_process_sp.reset();
192*0b57cec5SDimitry Andric   }
193*0b57cec5SDimitry Andric }
194*0b57cec5SDimitry Andric 
195*0b57cec5SDimitry Andric const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp,
196*0b57cec5SDimitry Andric                                              llvm::StringRef plugin_name,
197*0b57cec5SDimitry Andric                                              const FileSpec *crash_file) {
198*0b57cec5SDimitry Andric   if (!listener_sp)
199*0b57cec5SDimitry Andric     listener_sp = GetDebugger().GetListener();
200*0b57cec5SDimitry Andric   DeleteCurrentProcess();
201*0b57cec5SDimitry Andric   m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name,
202*0b57cec5SDimitry Andric                                      listener_sp, crash_file);
203*0b57cec5SDimitry Andric   return m_process_sp;
204*0b57cec5SDimitry Andric }
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; }
207*0b57cec5SDimitry Andric 
208*0b57cec5SDimitry Andric lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language,
209*0b57cec5SDimitry Andric                              const char *repl_options, bool can_create) {
210*0b57cec5SDimitry Andric   if (language == eLanguageTypeUnknown) {
2119dba64beSDimitry Andric     LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
212*0b57cec5SDimitry Andric 
2139dba64beSDimitry Andric     if (auto single_lang = repl_languages.GetSingularLanguage()) {
2149dba64beSDimitry Andric       language = *single_lang;
2159dba64beSDimitry Andric     } else if (repl_languages.Empty()) {
216*0b57cec5SDimitry Andric       err.SetErrorStringWithFormat(
217*0b57cec5SDimitry Andric           "LLDB isn't configured with REPL support for any languages.");
218*0b57cec5SDimitry Andric       return REPLSP();
219*0b57cec5SDimitry Andric     } else {
220*0b57cec5SDimitry Andric       err.SetErrorStringWithFormat(
221*0b57cec5SDimitry Andric           "Multiple possible REPL languages.  Please specify a language.");
222*0b57cec5SDimitry Andric       return REPLSP();
223*0b57cec5SDimitry Andric     }
224*0b57cec5SDimitry Andric   }
225*0b57cec5SDimitry Andric 
226*0b57cec5SDimitry Andric   REPLMap::iterator pos = m_repl_map.find(language);
227*0b57cec5SDimitry Andric 
228*0b57cec5SDimitry Andric   if (pos != m_repl_map.end()) {
229*0b57cec5SDimitry Andric     return pos->second;
230*0b57cec5SDimitry Andric   }
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric   if (!can_create) {
233*0b57cec5SDimitry Andric     err.SetErrorStringWithFormat(
234*0b57cec5SDimitry Andric         "Couldn't find an existing REPL for %s, and can't create a new one",
235*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
236*0b57cec5SDimitry Andric     return lldb::REPLSP();
237*0b57cec5SDimitry Andric   }
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric   Debugger *const debugger = nullptr;
240*0b57cec5SDimitry Andric   lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
241*0b57cec5SDimitry Andric 
242*0b57cec5SDimitry Andric   if (ret) {
243*0b57cec5SDimitry Andric     m_repl_map[language] = ret;
244*0b57cec5SDimitry Andric     return m_repl_map[language];
245*0b57cec5SDimitry Andric   }
246*0b57cec5SDimitry Andric 
247*0b57cec5SDimitry Andric   if (err.Success()) {
248*0b57cec5SDimitry Andric     err.SetErrorStringWithFormat("Couldn't create a REPL for %s",
249*0b57cec5SDimitry Andric                                  Language::GetNameForLanguageType(language));
250*0b57cec5SDimitry Andric   }
251*0b57cec5SDimitry Andric 
252*0b57cec5SDimitry Andric   return lldb::REPLSP();
253*0b57cec5SDimitry Andric }
254*0b57cec5SDimitry Andric 
255*0b57cec5SDimitry Andric void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) {
256*0b57cec5SDimitry Andric   lldbassert(!m_repl_map.count(language));
257*0b57cec5SDimitry Andric 
258*0b57cec5SDimitry Andric   m_repl_map[language] = repl_sp;
259*0b57cec5SDimitry Andric }
260*0b57cec5SDimitry Andric 
261*0b57cec5SDimitry Andric void Target::Destroy() {
262*0b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
263*0b57cec5SDimitry Andric   m_valid = false;
264*0b57cec5SDimitry Andric   DeleteCurrentProcess();
265*0b57cec5SDimitry Andric   m_platform_sp.reset();
266*0b57cec5SDimitry Andric   m_arch = ArchSpec();
267*0b57cec5SDimitry Andric   ClearModules(true);
268*0b57cec5SDimitry Andric   m_section_load_history.Clear();
269*0b57cec5SDimitry Andric   const bool notify = false;
270*0b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(notify);
271*0b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveAll(notify);
272*0b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
273*0b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
274*0b57cec5SDimitry Andric   m_search_filter_sp.reset();
275*0b57cec5SDimitry Andric   m_image_search_paths.Clear(notify);
276*0b57cec5SDimitry Andric   m_stop_hooks.clear();
277*0b57cec5SDimitry Andric   m_stop_hook_next_id = 0;
278*0b57cec5SDimitry Andric   m_suppress_stop_hooks = false;
279*0b57cec5SDimitry Andric }
280*0b57cec5SDimitry Andric 
281*0b57cec5SDimitry Andric BreakpointList &Target::GetBreakpointList(bool internal) {
282*0b57cec5SDimitry Andric   if (internal)
283*0b57cec5SDimitry Andric     return m_internal_breakpoint_list;
284*0b57cec5SDimitry Andric   else
285*0b57cec5SDimitry Andric     return m_breakpoint_list;
286*0b57cec5SDimitry Andric }
287*0b57cec5SDimitry Andric 
288*0b57cec5SDimitry Andric const BreakpointList &Target::GetBreakpointList(bool internal) const {
289*0b57cec5SDimitry Andric   if (internal)
290*0b57cec5SDimitry Andric     return m_internal_breakpoint_list;
291*0b57cec5SDimitry Andric   else
292*0b57cec5SDimitry Andric     return m_breakpoint_list;
293*0b57cec5SDimitry Andric }
294*0b57cec5SDimitry Andric 
295*0b57cec5SDimitry Andric BreakpointSP Target::GetBreakpointByID(break_id_t break_id) {
296*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
297*0b57cec5SDimitry Andric 
298*0b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
299*0b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
300*0b57cec5SDimitry Andric   else
301*0b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric   return bp_sp;
304*0b57cec5SDimitry Andric }
305*0b57cec5SDimitry Andric 
306*0b57cec5SDimitry Andric BreakpointSP Target::CreateSourceRegexBreakpoint(
307*0b57cec5SDimitry Andric     const FileSpecList *containingModules,
308*0b57cec5SDimitry Andric     const FileSpecList *source_file_spec_list,
309*0b57cec5SDimitry Andric     const std::unordered_set<std::string> &function_names,
3109dba64beSDimitry Andric     RegularExpression source_regex, bool internal, bool hardware,
311*0b57cec5SDimitry Andric     LazyBool move_to_nearest_code) {
312*0b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
313*0b57cec5SDimitry Andric       containingModules, source_file_spec_list));
314*0b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
315*0b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
316*0b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(
3179dba64beSDimitry Andric       nullptr, std::move(source_regex), function_names,
318*0b57cec5SDimitry Andric       !static_cast<bool>(move_to_nearest_code)));
319*0b57cec5SDimitry Andric 
320*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
321*0b57cec5SDimitry Andric }
322*0b57cec5SDimitry Andric 
323*0b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules,
324*0b57cec5SDimitry Andric                                       const FileSpec &file, uint32_t line_no,
325*0b57cec5SDimitry Andric                                       uint32_t column, lldb::addr_t offset,
326*0b57cec5SDimitry Andric                                       LazyBool check_inlines,
327*0b57cec5SDimitry Andric                                       LazyBool skip_prologue, bool internal,
328*0b57cec5SDimitry Andric                                       bool hardware,
329*0b57cec5SDimitry Andric                                       LazyBool move_to_nearest_code) {
330*0b57cec5SDimitry Andric   FileSpec remapped_file;
331*0b57cec5SDimitry Andric   if (!GetSourcePathMap().ReverseRemapPath(file, remapped_file))
332*0b57cec5SDimitry Andric     remapped_file = file;
333*0b57cec5SDimitry Andric 
334*0b57cec5SDimitry Andric   if (check_inlines == eLazyBoolCalculate) {
335*0b57cec5SDimitry Andric     const InlineStrategy inline_strategy = GetInlineStrategy();
336*0b57cec5SDimitry Andric     switch (inline_strategy) {
337*0b57cec5SDimitry Andric     case eInlineBreakpointsNever:
338*0b57cec5SDimitry Andric       check_inlines = eLazyBoolNo;
339*0b57cec5SDimitry Andric       break;
340*0b57cec5SDimitry Andric 
341*0b57cec5SDimitry Andric     case eInlineBreakpointsHeaders:
342*0b57cec5SDimitry Andric       if (remapped_file.IsSourceImplementationFile())
343*0b57cec5SDimitry Andric         check_inlines = eLazyBoolNo;
344*0b57cec5SDimitry Andric       else
345*0b57cec5SDimitry Andric         check_inlines = eLazyBoolYes;
346*0b57cec5SDimitry Andric       break;
347*0b57cec5SDimitry Andric 
348*0b57cec5SDimitry Andric     case eInlineBreakpointsAlways:
349*0b57cec5SDimitry Andric       check_inlines = eLazyBoolYes;
350*0b57cec5SDimitry Andric       break;
351*0b57cec5SDimitry Andric     }
352*0b57cec5SDimitry Andric   }
353*0b57cec5SDimitry Andric   SearchFilterSP filter_sp;
354*0b57cec5SDimitry Andric   if (check_inlines == eLazyBoolNo) {
355*0b57cec5SDimitry Andric     // Not checking for inlines, we are looking only for matching compile units
356*0b57cec5SDimitry Andric     FileSpecList compile_unit_list;
357*0b57cec5SDimitry Andric     compile_unit_list.Append(remapped_file);
358*0b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
359*0b57cec5SDimitry Andric                                                   &compile_unit_list);
360*0b57cec5SDimitry Andric   } else {
361*0b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
362*0b57cec5SDimitry Andric   }
363*0b57cec5SDimitry Andric   if (skip_prologue == eLazyBoolCalculate)
364*0b57cec5SDimitry Andric     skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
365*0b57cec5SDimitry Andric   if (move_to_nearest_code == eLazyBoolCalculate)
366*0b57cec5SDimitry Andric     move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
367*0b57cec5SDimitry Andric 
368*0b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(
369*0b57cec5SDimitry Andric       nullptr, remapped_file, line_no, column, offset, check_inlines,
370*0b57cec5SDimitry Andric       skip_prologue, !static_cast<bool>(move_to_nearest_code)));
371*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
372*0b57cec5SDimitry Andric }
373*0b57cec5SDimitry Andric 
374*0b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal,
375*0b57cec5SDimitry Andric                                       bool hardware) {
376*0b57cec5SDimitry Andric   Address so_addr;
377*0b57cec5SDimitry Andric 
378*0b57cec5SDimitry Andric   // Check for any reason we want to move this breakpoint to other address.
379*0b57cec5SDimitry Andric   addr = GetBreakableLoadAddress(addr);
380*0b57cec5SDimitry Andric 
381*0b57cec5SDimitry Andric   // Attempt to resolve our load address if possible, though it is ok if it
382*0b57cec5SDimitry Andric   // doesn't resolve to section/offset.
383*0b57cec5SDimitry Andric 
384*0b57cec5SDimitry Andric   // Try and resolve as a load address if possible
385*0b57cec5SDimitry Andric   GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
386*0b57cec5SDimitry Andric   if (!so_addr.IsValid()) {
387*0b57cec5SDimitry Andric     // The address didn't resolve, so just set this as an absolute address
388*0b57cec5SDimitry Andric     so_addr.SetOffset(addr);
389*0b57cec5SDimitry Andric   }
390*0b57cec5SDimitry Andric   BreakpointSP bp_sp(CreateBreakpoint(so_addr, internal, hardware));
391*0b57cec5SDimitry Andric   return bp_sp;
392*0b57cec5SDimitry Andric }
393*0b57cec5SDimitry Andric 
394*0b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
395*0b57cec5SDimitry Andric                                       bool hardware) {
396*0b57cec5SDimitry Andric   SearchFilterSP filter_sp(
397*0b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
398*0b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(
399*0b57cec5SDimitry Andric       new BreakpointResolverAddress(nullptr, addr));
400*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, false);
401*0b57cec5SDimitry Andric }
402*0b57cec5SDimitry Andric 
403*0b57cec5SDimitry Andric lldb::BreakpointSP
404*0b57cec5SDimitry Andric Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
405*0b57cec5SDimitry Andric                                         const FileSpec *file_spec,
406*0b57cec5SDimitry Andric                                         bool request_hardware) {
407*0b57cec5SDimitry Andric   SearchFilterSP filter_sp(
408*0b57cec5SDimitry Andric       new SearchFilterForUnconstrainedSearches(shared_from_this()));
409480093f4SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(
410480093f4SDimitry Andric       nullptr, file_addr, file_spec ? *file_spec : FileSpec()));
411*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
412*0b57cec5SDimitry Andric                           false);
413*0b57cec5SDimitry Andric }
414*0b57cec5SDimitry Andric 
415*0b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(
416*0b57cec5SDimitry Andric     const FileSpecList *containingModules,
417*0b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles, const char *func_name,
418*0b57cec5SDimitry Andric     FunctionNameType func_name_type_mask, LanguageType language,
419*0b57cec5SDimitry Andric     lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) {
420*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
421*0b57cec5SDimitry Andric   if (func_name) {
422*0b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
423*0b57cec5SDimitry Andric         containingModules, containingSourceFiles));
424*0b57cec5SDimitry Andric 
425*0b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
426*0b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
427*0b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
428*0b57cec5SDimitry Andric       language = GetLanguage();
429*0b57cec5SDimitry Andric 
430*0b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
431*0b57cec5SDimitry Andric         nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact,
432*0b57cec5SDimitry Andric         offset, skip_prologue));
433*0b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
434*0b57cec5SDimitry Andric   }
435*0b57cec5SDimitry Andric   return bp_sp;
436*0b57cec5SDimitry Andric }
437*0b57cec5SDimitry Andric 
438*0b57cec5SDimitry Andric lldb::BreakpointSP
439*0b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
440*0b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
441*0b57cec5SDimitry Andric                          const std::vector<std::string> &func_names,
442*0b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
443*0b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
444*0b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
445*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
446*0b57cec5SDimitry Andric   size_t num_names = func_names.size();
447*0b57cec5SDimitry Andric   if (num_names > 0) {
448*0b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
449*0b57cec5SDimitry Andric         containingModules, containingSourceFiles));
450*0b57cec5SDimitry Andric 
451*0b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate)
452*0b57cec5SDimitry Andric       skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
453*0b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
454*0b57cec5SDimitry Andric       language = GetLanguage();
455*0b57cec5SDimitry Andric 
456*0b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(
457*0b57cec5SDimitry Andric         new BreakpointResolverName(nullptr, func_names, func_name_type_mask,
458*0b57cec5SDimitry Andric                                    language, offset, skip_prologue));
459*0b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
460*0b57cec5SDimitry Andric   }
461*0b57cec5SDimitry Andric   return bp_sp;
462*0b57cec5SDimitry Andric }
463*0b57cec5SDimitry Andric 
464*0b57cec5SDimitry Andric BreakpointSP
465*0b57cec5SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
466*0b57cec5SDimitry Andric                          const FileSpecList *containingSourceFiles,
467*0b57cec5SDimitry Andric                          const char *func_names[], size_t num_names,
468*0b57cec5SDimitry Andric                          FunctionNameType func_name_type_mask,
469*0b57cec5SDimitry Andric                          LanguageType language, lldb::addr_t offset,
470*0b57cec5SDimitry Andric                          LazyBool skip_prologue, bool internal, bool hardware) {
471*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
472*0b57cec5SDimitry Andric   if (num_names > 0) {
473*0b57cec5SDimitry Andric     SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
474*0b57cec5SDimitry Andric         containingModules, containingSourceFiles));
475*0b57cec5SDimitry Andric 
476*0b57cec5SDimitry Andric     if (skip_prologue == eLazyBoolCalculate) {
477*0b57cec5SDimitry Andric       if (offset == 0)
478*0b57cec5SDimitry Andric         skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
479*0b57cec5SDimitry Andric       else
480*0b57cec5SDimitry Andric         skip_prologue = eLazyBoolNo;
481*0b57cec5SDimitry Andric     }
482*0b57cec5SDimitry Andric     if (language == lldb::eLanguageTypeUnknown)
483*0b57cec5SDimitry Andric       language = GetLanguage();
484*0b57cec5SDimitry Andric 
485*0b57cec5SDimitry Andric     BreakpointResolverSP resolver_sp(new BreakpointResolverName(
486*0b57cec5SDimitry Andric         nullptr, func_names, num_names, func_name_type_mask, language, offset,
487*0b57cec5SDimitry Andric         skip_prologue));
488*0b57cec5SDimitry Andric     resolver_sp->SetOffset(offset);
489*0b57cec5SDimitry Andric     bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
490*0b57cec5SDimitry Andric   }
491*0b57cec5SDimitry Andric   return bp_sp;
492*0b57cec5SDimitry Andric }
493*0b57cec5SDimitry Andric 
494*0b57cec5SDimitry Andric SearchFilterSP
495*0b57cec5SDimitry Andric Target::GetSearchFilterForModule(const FileSpec *containingModule) {
496*0b57cec5SDimitry Andric   SearchFilterSP filter_sp;
497*0b57cec5SDimitry Andric   if (containingModule != nullptr) {
498*0b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
499*0b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
500*0b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModule>(shared_from_this(),
501*0b57cec5SDimitry Andric                                                        *containingModule);
502*0b57cec5SDimitry Andric   } else {
503*0b57cec5SDimitry Andric     if (!m_search_filter_sp)
504*0b57cec5SDimitry Andric       m_search_filter_sp =
505*0b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
506*0b57cec5SDimitry Andric               shared_from_this());
507*0b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
508*0b57cec5SDimitry Andric   }
509*0b57cec5SDimitry Andric   return filter_sp;
510*0b57cec5SDimitry Andric }
511*0b57cec5SDimitry Andric 
512*0b57cec5SDimitry Andric SearchFilterSP
513*0b57cec5SDimitry Andric Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) {
514*0b57cec5SDimitry Andric   SearchFilterSP filter_sp;
515*0b57cec5SDimitry Andric   if (containingModules && containingModules->GetSize() != 0) {
516*0b57cec5SDimitry Andric     // TODO: We should look into sharing module based search filters
517*0b57cec5SDimitry Andric     // across many breakpoints like we do for the simple target based one
518*0b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleList>(shared_from_this(),
519*0b57cec5SDimitry Andric                                                            *containingModules);
520*0b57cec5SDimitry Andric   } else {
521*0b57cec5SDimitry Andric     if (!m_search_filter_sp)
522*0b57cec5SDimitry Andric       m_search_filter_sp =
523*0b57cec5SDimitry Andric           std::make_shared<SearchFilterForUnconstrainedSearches>(
524*0b57cec5SDimitry Andric               shared_from_this());
525*0b57cec5SDimitry Andric     filter_sp = m_search_filter_sp;
526*0b57cec5SDimitry Andric   }
527*0b57cec5SDimitry Andric   return filter_sp;
528*0b57cec5SDimitry Andric }
529*0b57cec5SDimitry Andric 
530*0b57cec5SDimitry Andric SearchFilterSP Target::GetSearchFilterForModuleAndCUList(
531*0b57cec5SDimitry Andric     const FileSpecList *containingModules,
532*0b57cec5SDimitry Andric     const FileSpecList *containingSourceFiles) {
533*0b57cec5SDimitry Andric   if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
534*0b57cec5SDimitry Andric     return GetSearchFilterForModuleList(containingModules);
535*0b57cec5SDimitry Andric 
536*0b57cec5SDimitry Andric   SearchFilterSP filter_sp;
537*0b57cec5SDimitry Andric   if (containingModules == nullptr) {
538*0b57cec5SDimitry Andric     // We could make a special "CU List only SearchFilter".  Better yet was if
539*0b57cec5SDimitry Andric     // these could be composable, but that will take a little reworking.
540*0b57cec5SDimitry Andric 
541*0b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
542*0b57cec5SDimitry Andric         shared_from_this(), FileSpecList(), *containingSourceFiles);
543*0b57cec5SDimitry Andric   } else {
544*0b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
545*0b57cec5SDimitry Andric         shared_from_this(), *containingModules, *containingSourceFiles);
546*0b57cec5SDimitry Andric   }
547*0b57cec5SDimitry Andric   return filter_sp;
548*0b57cec5SDimitry Andric }
549*0b57cec5SDimitry Andric 
550*0b57cec5SDimitry Andric BreakpointSP Target::CreateFuncRegexBreakpoint(
551*0b57cec5SDimitry Andric     const FileSpecList *containingModules,
5529dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, RegularExpression func_regex,
553*0b57cec5SDimitry Andric     lldb::LanguageType requested_language, LazyBool skip_prologue,
554*0b57cec5SDimitry Andric     bool internal, bool hardware) {
555*0b57cec5SDimitry Andric   SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
556*0b57cec5SDimitry Andric       containingModules, containingSourceFiles));
557*0b57cec5SDimitry Andric   bool skip = (skip_prologue == eLazyBoolCalculate)
558*0b57cec5SDimitry Andric                   ? GetSkipPrologue()
559*0b57cec5SDimitry Andric                   : static_cast<bool>(skip_prologue);
560*0b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverName(
5619dba64beSDimitry Andric       nullptr, std::move(func_regex), requested_language, 0, skip));
562*0b57cec5SDimitry Andric 
563*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
564*0b57cec5SDimitry Andric }
565*0b57cec5SDimitry Andric 
566*0b57cec5SDimitry Andric lldb::BreakpointSP
567*0b57cec5SDimitry Andric Target::CreateExceptionBreakpoint(enum lldb::LanguageType language,
568*0b57cec5SDimitry Andric                                   bool catch_bp, bool throw_bp, bool internal,
569*0b57cec5SDimitry Andric                                   Args *additional_args, Status *error) {
570*0b57cec5SDimitry Andric   BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint(
571*0b57cec5SDimitry Andric       *this, language, catch_bp, throw_bp, internal);
572*0b57cec5SDimitry Andric   if (exc_bkpt_sp && additional_args) {
573*0b57cec5SDimitry Andric     BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
574*0b57cec5SDimitry Andric     if (precondition_sp && additional_args) {
575*0b57cec5SDimitry Andric       if (error)
576*0b57cec5SDimitry Andric         *error = precondition_sp->ConfigurePrecondition(*additional_args);
577*0b57cec5SDimitry Andric       else
578*0b57cec5SDimitry Andric         precondition_sp->ConfigurePrecondition(*additional_args);
579*0b57cec5SDimitry Andric     }
580*0b57cec5SDimitry Andric   }
581*0b57cec5SDimitry Andric   return exc_bkpt_sp;
582*0b57cec5SDimitry Andric }
583*0b57cec5SDimitry Andric 
5849dba64beSDimitry Andric lldb::BreakpointSP Target::CreateScriptedBreakpoint(
5859dba64beSDimitry Andric     const llvm::StringRef class_name, const FileSpecList *containingModules,
5869dba64beSDimitry Andric     const FileSpecList *containingSourceFiles, bool internal,
5879dba64beSDimitry Andric     bool request_hardware, StructuredData::ObjectSP extra_args_sp,
5889dba64beSDimitry Andric     Status *creation_error) {
589*0b57cec5SDimitry Andric   SearchFilterSP filter_sp;
590*0b57cec5SDimitry Andric 
591*0b57cec5SDimitry Andric   lldb::SearchDepth depth = lldb::eSearchDepthTarget;
5929dba64beSDimitry Andric   bool has_files =
5939dba64beSDimitry Andric       containingSourceFiles && containingSourceFiles->GetSize() > 0;
594*0b57cec5SDimitry Andric   bool has_modules = containingModules && containingModules->GetSize() > 0;
595*0b57cec5SDimitry Andric 
596*0b57cec5SDimitry Andric   if (has_files && has_modules) {
5979dba64beSDimitry Andric     filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
5989dba64beSDimitry Andric                                                   containingSourceFiles);
599*0b57cec5SDimitry Andric   } else if (has_files) {
6009dba64beSDimitry Andric     filter_sp =
6019dba64beSDimitry Andric         GetSearchFilterForModuleAndCUList(nullptr, containingSourceFiles);
602*0b57cec5SDimitry Andric   } else if (has_modules) {
603*0b57cec5SDimitry Andric     filter_sp = GetSearchFilterForModuleList(containingModules);
604*0b57cec5SDimitry Andric   } else {
605*0b57cec5SDimitry Andric     filter_sp = std::make_shared<SearchFilterForUnconstrainedSearches>(
606*0b57cec5SDimitry Andric         shared_from_this());
607*0b57cec5SDimitry Andric   }
608*0b57cec5SDimitry Andric 
609*0b57cec5SDimitry Andric   StructuredDataImpl *extra_args_impl = new StructuredDataImpl();
610*0b57cec5SDimitry Andric   if (extra_args_sp)
611*0b57cec5SDimitry Andric     extra_args_impl->SetObjectSP(extra_args_sp);
612*0b57cec5SDimitry Andric 
613*0b57cec5SDimitry Andric   BreakpointResolverSP resolver_sp(new BreakpointResolverScripted(
6149dba64beSDimitry Andric       nullptr, class_name, depth, extra_args_impl));
615*0b57cec5SDimitry Andric   return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
616*0b57cec5SDimitry Andric }
617*0b57cec5SDimitry Andric 
618*0b57cec5SDimitry Andric BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp,
619*0b57cec5SDimitry Andric                                       BreakpointResolverSP &resolver_sp,
620*0b57cec5SDimitry Andric                                       bool internal, bool request_hardware,
621*0b57cec5SDimitry Andric                                       bool resolve_indirect_symbols) {
622*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
623*0b57cec5SDimitry Andric   if (filter_sp && resolver_sp) {
624*0b57cec5SDimitry Andric     const bool hardware = request_hardware || GetRequireHardwareBreakpoints();
625*0b57cec5SDimitry Andric     bp_sp.reset(new Breakpoint(*this, filter_sp, resolver_sp, hardware,
626*0b57cec5SDimitry Andric                                resolve_indirect_symbols));
6275ffd83dbSDimitry Andric     resolver_sp->SetBreakpoint(bp_sp);
628*0b57cec5SDimitry Andric     AddBreakpoint(bp_sp, internal);
629*0b57cec5SDimitry Andric   }
630*0b57cec5SDimitry Andric   return bp_sp;
631*0b57cec5SDimitry Andric }
632*0b57cec5SDimitry Andric 
633*0b57cec5SDimitry Andric void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) {
634*0b57cec5SDimitry Andric   if (!bp_sp)
635*0b57cec5SDimitry Andric     return;
636*0b57cec5SDimitry Andric   if (internal)
637*0b57cec5SDimitry Andric     m_internal_breakpoint_list.Add(bp_sp, false);
638*0b57cec5SDimitry Andric   else
639*0b57cec5SDimitry Andric     m_breakpoint_list.Add(bp_sp, true);
640*0b57cec5SDimitry Andric 
641*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
642*0b57cec5SDimitry Andric   if (log) {
643*0b57cec5SDimitry Andric     StreamString s;
644*0b57cec5SDimitry Andric     bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
6459dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s (internal = %s) => break_id = %s\n",
6469dba64beSDimitry Andric               __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
647*0b57cec5SDimitry Andric   }
648*0b57cec5SDimitry Andric 
649*0b57cec5SDimitry Andric   bp_sp->ResolveBreakpoint();
650*0b57cec5SDimitry Andric 
651*0b57cec5SDimitry Andric   if (!internal) {
652*0b57cec5SDimitry Andric     m_last_created_breakpoint = bp_sp;
653*0b57cec5SDimitry Andric   }
654*0b57cec5SDimitry Andric }
655*0b57cec5SDimitry Andric 
6569dba64beSDimitry Andric void Target::AddNameToBreakpoint(BreakpointID &id, const char *name,
6579dba64beSDimitry Andric                                  Status &error) {
6589dba64beSDimitry Andric   BreakpointSP bp_sp =
6599dba64beSDimitry Andric       m_breakpoint_list.FindBreakpointByID(id.GetBreakpointID());
6609dba64beSDimitry Andric   if (!bp_sp) {
661*0b57cec5SDimitry Andric     StreamString s;
662*0b57cec5SDimitry Andric     id.GetDescription(&s, eDescriptionLevelBrief);
6639dba64beSDimitry Andric     error.SetErrorStringWithFormat("Could not find breakpoint %s", s.GetData());
664*0b57cec5SDimitry Andric     return;
665*0b57cec5SDimitry Andric   }
666*0b57cec5SDimitry Andric   AddNameToBreakpoint(bp_sp, name, error);
667*0b57cec5SDimitry Andric }
668*0b57cec5SDimitry Andric 
6699dba64beSDimitry Andric void Target::AddNameToBreakpoint(BreakpointSP &bp_sp, const char *name,
6709dba64beSDimitry Andric                                  Status &error) {
671*0b57cec5SDimitry Andric   if (!bp_sp)
672*0b57cec5SDimitry Andric     return;
673*0b57cec5SDimitry Andric 
674*0b57cec5SDimitry Andric   BreakpointName *bp_name = FindBreakpointName(ConstString(name), true, error);
675*0b57cec5SDimitry Andric   if (!bp_name)
676*0b57cec5SDimitry Andric     return;
677*0b57cec5SDimitry Andric 
678*0b57cec5SDimitry Andric   bp_name->ConfigureBreakpoint(bp_sp);
679*0b57cec5SDimitry Andric   bp_sp->AddName(name);
680*0b57cec5SDimitry Andric }
681*0b57cec5SDimitry Andric 
682*0b57cec5SDimitry Andric void Target::AddBreakpointName(BreakpointName *bp_name) {
683*0b57cec5SDimitry Andric   m_breakpoint_names.insert(std::make_pair(bp_name->GetName(), bp_name));
684*0b57cec5SDimitry Andric }
685*0b57cec5SDimitry Andric 
6869dba64beSDimitry Andric BreakpointName *Target::FindBreakpointName(ConstString name, bool can_create,
6879dba64beSDimitry Andric                                            Status &error) {
688*0b57cec5SDimitry Andric   BreakpointID::StringIsBreakpointName(name.GetStringRef(), error);
689*0b57cec5SDimitry Andric   if (!error.Success())
690*0b57cec5SDimitry Andric     return nullptr;
691*0b57cec5SDimitry Andric 
692*0b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
693*0b57cec5SDimitry Andric   if (iter == m_breakpoint_names.end()) {
6949dba64beSDimitry Andric     if (!can_create) {
695*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat("Breakpoint name \"%s\" doesn't exist and "
6969dba64beSDimitry Andric                                      "can_create is false.",
6979dba64beSDimitry Andric                                      name.AsCString());
698*0b57cec5SDimitry Andric       return nullptr;
699*0b57cec5SDimitry Andric     }
700*0b57cec5SDimitry Andric 
7019dba64beSDimitry Andric     iter = m_breakpoint_names
7029dba64beSDimitry Andric                .insert(std::make_pair(name, new BreakpointName(name)))
703*0b57cec5SDimitry Andric                .first;
704*0b57cec5SDimitry Andric   }
705*0b57cec5SDimitry Andric   return (iter->second);
706*0b57cec5SDimitry Andric }
707*0b57cec5SDimitry Andric 
7089dba64beSDimitry Andric void Target::DeleteBreakpointName(ConstString name) {
709*0b57cec5SDimitry Andric   BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
710*0b57cec5SDimitry Andric 
711*0b57cec5SDimitry Andric   if (iter != m_breakpoint_names.end()) {
712*0b57cec5SDimitry Andric     const char *name_cstr = name.AsCString();
713*0b57cec5SDimitry Andric     m_breakpoint_names.erase(iter);
714*0b57cec5SDimitry Andric     for (auto bp_sp : m_breakpoint_list.Breakpoints())
715*0b57cec5SDimitry Andric       bp_sp->RemoveName(name_cstr);
716*0b57cec5SDimitry Andric   }
717*0b57cec5SDimitry Andric }
718*0b57cec5SDimitry Andric 
719*0b57cec5SDimitry Andric void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp,
7209dba64beSDimitry Andric                                       ConstString name) {
721*0b57cec5SDimitry Andric   bp_sp->RemoveName(name.AsCString());
722*0b57cec5SDimitry Andric }
723*0b57cec5SDimitry Andric 
7249dba64beSDimitry Andric void Target::ConfigureBreakpointName(
7259dba64beSDimitry Andric     BreakpointName &bp_name, const BreakpointOptions &new_options,
7269dba64beSDimitry Andric     const BreakpointName::Permissions &new_permissions) {
727*0b57cec5SDimitry Andric   bp_name.GetOptions().CopyOverSetOptions(new_options);
728*0b57cec5SDimitry Andric   bp_name.GetPermissions().MergeInto(new_permissions);
729*0b57cec5SDimitry Andric   ApplyNameToBreakpoints(bp_name);
730*0b57cec5SDimitry Andric }
731*0b57cec5SDimitry Andric 
732*0b57cec5SDimitry Andric void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) {
733480093f4SDimitry Andric   llvm::Expected<std::vector<BreakpointSP>> expected_vector =
734480093f4SDimitry Andric       m_breakpoint_list.FindBreakpointsByName(bp_name.GetName().AsCString());
735*0b57cec5SDimitry Andric 
736480093f4SDimitry Andric   if (!expected_vector) {
737480093f4SDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS),
738480093f4SDimitry Andric              "invalid breakpoint name: {}",
739480093f4SDimitry Andric              llvm::toString(expected_vector.takeError()));
740480093f4SDimitry Andric     return;
741480093f4SDimitry Andric   }
742480093f4SDimitry Andric 
743480093f4SDimitry Andric   for (auto bp_sp : *expected_vector)
744*0b57cec5SDimitry Andric     bp_name.ConfigureBreakpoint(bp_sp);
745*0b57cec5SDimitry Andric }
746*0b57cec5SDimitry Andric 
7479dba64beSDimitry Andric void Target::GetBreakpointNames(std::vector<std::string> &names) {
748*0b57cec5SDimitry Andric   names.clear();
749*0b57cec5SDimitry Andric   for (auto bp_name : m_breakpoint_names) {
750*0b57cec5SDimitry Andric     names.push_back(bp_name.first.AsCString());
751*0b57cec5SDimitry Andric   }
752*0b57cec5SDimitry Andric   llvm::sort(names.begin(), names.end());
753*0b57cec5SDimitry Andric }
754*0b57cec5SDimitry Andric 
755*0b57cec5SDimitry Andric bool Target::ProcessIsValid() {
756*0b57cec5SDimitry Andric   return (m_process_sp && m_process_sp->IsAlive());
757*0b57cec5SDimitry Andric }
758*0b57cec5SDimitry Andric 
759*0b57cec5SDimitry Andric static bool CheckIfWatchpointsSupported(Target *target, Status &error) {
760*0b57cec5SDimitry Andric   uint32_t num_supported_hardware_watchpoints;
761*0b57cec5SDimitry Andric   Status rc = target->GetProcessSP()->GetWatchpointSupportInfo(
762*0b57cec5SDimitry Andric       num_supported_hardware_watchpoints);
763*0b57cec5SDimitry Andric 
764*0b57cec5SDimitry Andric   // If unable to determine the # of watchpoints available,
765*0b57cec5SDimitry Andric   // assume they are supported.
766*0b57cec5SDimitry Andric   if (rc.Fail())
767*0b57cec5SDimitry Andric     return true;
768*0b57cec5SDimitry Andric 
769*0b57cec5SDimitry Andric   if (num_supported_hardware_watchpoints == 0) {
770*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
771*0b57cec5SDimitry Andric         "Target supports (%u) hardware watchpoint slots.\n",
772*0b57cec5SDimitry Andric         num_supported_hardware_watchpoints);
773*0b57cec5SDimitry Andric     return false;
774*0b57cec5SDimitry Andric   }
775*0b57cec5SDimitry Andric   return true;
776*0b57cec5SDimitry Andric }
777*0b57cec5SDimitry Andric 
778*0b57cec5SDimitry Andric // See also Watchpoint::SetWatchpointType(uint32_t type) and the
779*0b57cec5SDimitry Andric // OptionGroupWatchpoint::WatchType enum type.
780*0b57cec5SDimitry Andric WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size,
781*0b57cec5SDimitry Andric                                       const CompilerType *type, uint32_t kind,
782*0b57cec5SDimitry Andric                                       Status &error) {
783*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
7849dba64beSDimitry Andric   LLDB_LOGF(log,
7859dba64beSDimitry Andric             "Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64
786*0b57cec5SDimitry Andric             " type = %u)\n",
787*0b57cec5SDimitry Andric             __FUNCTION__, addr, (uint64_t)size, kind);
788*0b57cec5SDimitry Andric 
789*0b57cec5SDimitry Andric   WatchpointSP wp_sp;
790*0b57cec5SDimitry Andric   if (!ProcessIsValid()) {
791*0b57cec5SDimitry Andric     error.SetErrorString("process is not alive");
792*0b57cec5SDimitry Andric     return wp_sp;
793*0b57cec5SDimitry Andric   }
794*0b57cec5SDimitry Andric 
795*0b57cec5SDimitry Andric   if (addr == LLDB_INVALID_ADDRESS || size == 0) {
796*0b57cec5SDimitry Andric     if (size == 0)
797*0b57cec5SDimitry Andric       error.SetErrorString("cannot set a watchpoint with watch_size of 0");
798*0b57cec5SDimitry Andric     else
799*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
800*0b57cec5SDimitry Andric     return wp_sp;
801*0b57cec5SDimitry Andric   }
802*0b57cec5SDimitry Andric 
803*0b57cec5SDimitry Andric   if (!LLDB_WATCH_TYPE_IS_VALID(kind)) {
804*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat("invalid watchpoint type: %d", kind);
805*0b57cec5SDimitry Andric   }
806*0b57cec5SDimitry Andric 
807*0b57cec5SDimitry Andric   if (!CheckIfWatchpointsSupported(this, error))
808*0b57cec5SDimitry Andric     return wp_sp;
809*0b57cec5SDimitry Andric 
810*0b57cec5SDimitry Andric   // Currently we only support one watchpoint per address, with total number of
811*0b57cec5SDimitry Andric   // watchpoints limited by the hardware which the inferior is running on.
812*0b57cec5SDimitry Andric 
813*0b57cec5SDimitry Andric   // Grab the list mutex while doing operations.
814*0b57cec5SDimitry Andric   const bool notify = false; // Don't notify about all the state changes we do
815*0b57cec5SDimitry Andric                              // on creating the watchpoint.
816*0b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
817*0b57cec5SDimitry Andric   this->GetWatchpointList().GetListMutex(lock);
818*0b57cec5SDimitry Andric   WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
819*0b57cec5SDimitry Andric   if (matched_sp) {
820*0b57cec5SDimitry Andric     size_t old_size = matched_sp->GetByteSize();
821*0b57cec5SDimitry Andric     uint32_t old_type =
822*0b57cec5SDimitry Andric         (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
823*0b57cec5SDimitry Andric         (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
824*0b57cec5SDimitry Andric     // Return the existing watchpoint if both size and type match.
825*0b57cec5SDimitry Andric     if (size == old_size && kind == old_type) {
826*0b57cec5SDimitry Andric       wp_sp = matched_sp;
827*0b57cec5SDimitry Andric       wp_sp->SetEnabled(false, notify);
828*0b57cec5SDimitry Andric     } else {
829*0b57cec5SDimitry Andric       // Nil the matched watchpoint; we will be creating a new one.
830*0b57cec5SDimitry Andric       m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
831*0b57cec5SDimitry Andric       m_watchpoint_list.Remove(matched_sp->GetID(), true);
832*0b57cec5SDimitry Andric     }
833*0b57cec5SDimitry Andric   }
834*0b57cec5SDimitry Andric 
835*0b57cec5SDimitry Andric   if (!wp_sp) {
836*0b57cec5SDimitry Andric     wp_sp = std::make_shared<Watchpoint>(*this, addr, size, type);
837*0b57cec5SDimitry Andric     wp_sp->SetWatchpointType(kind, notify);
838*0b57cec5SDimitry Andric     m_watchpoint_list.Add(wp_sp, true);
839*0b57cec5SDimitry Andric   }
840*0b57cec5SDimitry Andric 
841*0b57cec5SDimitry Andric   error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
8429dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (creation of watchpoint %s with id = %u)\n",
843*0b57cec5SDimitry Andric             __FUNCTION__, error.Success() ? "succeeded" : "failed",
844*0b57cec5SDimitry Andric             wp_sp->GetID());
845*0b57cec5SDimitry Andric 
846*0b57cec5SDimitry Andric   if (error.Fail()) {
847*0b57cec5SDimitry Andric     // Enabling the watchpoint on the device side failed. Remove the said
848*0b57cec5SDimitry Andric     // watchpoint from the list maintained by the target instance.
849*0b57cec5SDimitry Andric     m_watchpoint_list.Remove(wp_sp->GetID(), true);
850*0b57cec5SDimitry Andric     // See if we could provide more helpful error message.
851*0b57cec5SDimitry Andric     if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
852*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
853*0b57cec5SDimitry Andric           "watch size of %" PRIu64 " is not supported", (uint64_t)size);
854*0b57cec5SDimitry Andric 
855*0b57cec5SDimitry Andric     wp_sp.reset();
856*0b57cec5SDimitry Andric   } else
857*0b57cec5SDimitry Andric     m_last_created_watchpoint = wp_sp;
858*0b57cec5SDimitry Andric   return wp_sp;
859*0b57cec5SDimitry Andric }
860*0b57cec5SDimitry Andric 
8619dba64beSDimitry Andric void Target::RemoveAllowedBreakpoints() {
862*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8639dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s \n", __FUNCTION__);
864*0b57cec5SDimitry Andric 
865*0b57cec5SDimitry Andric   m_breakpoint_list.RemoveAllowed(true);
866*0b57cec5SDimitry Andric 
867*0b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
868*0b57cec5SDimitry Andric }
869*0b57cec5SDimitry Andric 
870*0b57cec5SDimitry Andric void Target::RemoveAllBreakpoints(bool internal_also) {
871*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8729dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
873*0b57cec5SDimitry Andric             internal_also ? "yes" : "no");
874*0b57cec5SDimitry Andric 
875*0b57cec5SDimitry Andric   m_breakpoint_list.RemoveAll(true);
876*0b57cec5SDimitry Andric   if (internal_also)
877*0b57cec5SDimitry Andric     m_internal_breakpoint_list.RemoveAll(false);
878*0b57cec5SDimitry Andric 
879*0b57cec5SDimitry Andric   m_last_created_breakpoint.reset();
880*0b57cec5SDimitry Andric }
881*0b57cec5SDimitry Andric 
882*0b57cec5SDimitry Andric void Target::DisableAllBreakpoints(bool internal_also) {
883*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8849dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
885*0b57cec5SDimitry Andric             internal_also ? "yes" : "no");
886*0b57cec5SDimitry Andric 
887*0b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(false);
888*0b57cec5SDimitry Andric   if (internal_also)
889*0b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(false);
890*0b57cec5SDimitry Andric }
891*0b57cec5SDimitry Andric 
892*0b57cec5SDimitry Andric void Target::DisableAllowedBreakpoints() {
893*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
8949dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
895*0b57cec5SDimitry Andric 
896*0b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(false);
897*0b57cec5SDimitry Andric }
898*0b57cec5SDimitry Andric 
899*0b57cec5SDimitry Andric void Target::EnableAllBreakpoints(bool internal_also) {
900*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9019dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
902*0b57cec5SDimitry Andric             internal_also ? "yes" : "no");
903*0b57cec5SDimitry Andric 
904*0b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAll(true);
905*0b57cec5SDimitry Andric   if (internal_also)
906*0b57cec5SDimitry Andric     m_internal_breakpoint_list.SetEnabledAll(true);
907*0b57cec5SDimitry Andric }
908*0b57cec5SDimitry Andric 
909*0b57cec5SDimitry Andric void Target::EnableAllowedBreakpoints() {
910*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9119dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s", __FUNCTION__);
912*0b57cec5SDimitry Andric 
913*0b57cec5SDimitry Andric   m_breakpoint_list.SetEnabledAllowed(true);
914*0b57cec5SDimitry Andric }
915*0b57cec5SDimitry Andric 
916*0b57cec5SDimitry Andric bool Target::RemoveBreakpointByID(break_id_t break_id) {
917*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9189dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
919*0b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
920*0b57cec5SDimitry Andric 
921*0b57cec5SDimitry Andric   if (DisableBreakpointByID(break_id)) {
922*0b57cec5SDimitry Andric     if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
923*0b57cec5SDimitry Andric       m_internal_breakpoint_list.Remove(break_id, false);
924*0b57cec5SDimitry Andric     else {
925*0b57cec5SDimitry Andric       if (m_last_created_breakpoint) {
926*0b57cec5SDimitry Andric         if (m_last_created_breakpoint->GetID() == break_id)
927*0b57cec5SDimitry Andric           m_last_created_breakpoint.reset();
928*0b57cec5SDimitry Andric       }
929*0b57cec5SDimitry Andric       m_breakpoint_list.Remove(break_id, true);
930*0b57cec5SDimitry Andric     }
931*0b57cec5SDimitry Andric     return true;
932*0b57cec5SDimitry Andric   }
933*0b57cec5SDimitry Andric   return false;
934*0b57cec5SDimitry Andric }
935*0b57cec5SDimitry Andric 
936*0b57cec5SDimitry Andric bool Target::DisableBreakpointByID(break_id_t break_id) {
937*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9389dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
939*0b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
940*0b57cec5SDimitry Andric 
941*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
942*0b57cec5SDimitry Andric 
943*0b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
944*0b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
945*0b57cec5SDimitry Andric   else
946*0b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
947*0b57cec5SDimitry Andric   if (bp_sp) {
948*0b57cec5SDimitry Andric     bp_sp->SetEnabled(false);
949*0b57cec5SDimitry Andric     return true;
950*0b57cec5SDimitry Andric   }
951*0b57cec5SDimitry Andric   return false;
952*0b57cec5SDimitry Andric }
953*0b57cec5SDimitry Andric 
954*0b57cec5SDimitry Andric bool Target::EnableBreakpointByID(break_id_t break_id) {
955*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
9569dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
957*0b57cec5SDimitry Andric             break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
958*0b57cec5SDimitry Andric 
959*0b57cec5SDimitry Andric   BreakpointSP bp_sp;
960*0b57cec5SDimitry Andric 
961*0b57cec5SDimitry Andric   if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
962*0b57cec5SDimitry Andric     bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
963*0b57cec5SDimitry Andric   else
964*0b57cec5SDimitry Andric     bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
965*0b57cec5SDimitry Andric 
966*0b57cec5SDimitry Andric   if (bp_sp) {
967*0b57cec5SDimitry Andric     bp_sp->SetEnabled(true);
968*0b57cec5SDimitry Andric     return true;
969*0b57cec5SDimitry Andric   }
970*0b57cec5SDimitry Andric   return false;
971*0b57cec5SDimitry Andric }
972*0b57cec5SDimitry Andric 
973*0b57cec5SDimitry Andric Status Target::SerializeBreakpointsToFile(const FileSpec &file,
974*0b57cec5SDimitry Andric                                           const BreakpointIDList &bp_ids,
975*0b57cec5SDimitry Andric                                           bool append) {
976*0b57cec5SDimitry Andric   Status error;
977*0b57cec5SDimitry Andric 
978*0b57cec5SDimitry Andric   if (!file) {
979*0b57cec5SDimitry Andric     error.SetErrorString("Invalid FileSpec.");
980*0b57cec5SDimitry Andric     return error;
981*0b57cec5SDimitry Andric   }
982*0b57cec5SDimitry Andric 
983*0b57cec5SDimitry Andric   std::string path(file.GetPath());
984*0b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp;
985*0b57cec5SDimitry Andric 
986*0b57cec5SDimitry Andric   StructuredData::ArraySP break_store_sp;
987*0b57cec5SDimitry Andric   StructuredData::Array *break_store_ptr = nullptr;
988*0b57cec5SDimitry Andric 
989*0b57cec5SDimitry Andric   if (append) {
990*0b57cec5SDimitry Andric     input_data_sp = StructuredData::ParseJSONFromFile(file, error);
991*0b57cec5SDimitry Andric     if (error.Success()) {
992*0b57cec5SDimitry Andric       break_store_ptr = input_data_sp->GetAsArray();
993*0b57cec5SDimitry Andric       if (!break_store_ptr) {
994*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
995*0b57cec5SDimitry Andric             "Tried to append to invalid input file %s", path.c_str());
996*0b57cec5SDimitry Andric         return error;
997*0b57cec5SDimitry Andric       }
998*0b57cec5SDimitry Andric     }
999*0b57cec5SDimitry Andric   }
1000*0b57cec5SDimitry Andric 
1001*0b57cec5SDimitry Andric   if (!break_store_ptr) {
1002*0b57cec5SDimitry Andric     break_store_sp = std::make_shared<StructuredData::Array>();
1003*0b57cec5SDimitry Andric     break_store_ptr = break_store_sp.get();
1004*0b57cec5SDimitry Andric   }
1005*0b57cec5SDimitry Andric 
1006*0b57cec5SDimitry Andric   StreamFile out_file(path.c_str(),
10079dba64beSDimitry Andric                       File::eOpenOptionTruncate | File::eOpenOptionWrite |
10089dba64beSDimitry Andric                           File::eOpenOptionCanCreate |
10099dba64beSDimitry Andric                           File::eOpenOptionCloseOnExec,
1010*0b57cec5SDimitry Andric                       lldb::eFilePermissionsFileDefault);
1011*0b57cec5SDimitry Andric   if (!out_file.GetFile().IsValid()) {
1012*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Unable to open output file: %s.",
1013*0b57cec5SDimitry Andric                                    path.c_str());
1014*0b57cec5SDimitry Andric     return error;
1015*0b57cec5SDimitry Andric   }
1016*0b57cec5SDimitry Andric 
1017*0b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
1018*0b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
1019*0b57cec5SDimitry Andric 
1020*0b57cec5SDimitry Andric   if (bp_ids.GetSize() == 0) {
1021*0b57cec5SDimitry Andric     const BreakpointList &breakpoints = GetBreakpointList();
1022*0b57cec5SDimitry Andric 
1023*0b57cec5SDimitry Andric     size_t num_breakpoints = breakpoints.GetSize();
1024*0b57cec5SDimitry Andric     for (size_t i = 0; i < num_breakpoints; i++) {
1025*0b57cec5SDimitry Andric       Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get();
1026*0b57cec5SDimitry Andric       StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
1027*0b57cec5SDimitry Andric       // If a breakpoint can't serialize it, just ignore it for now:
1028*0b57cec5SDimitry Andric       if (bkpt_save_sp)
1029*0b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
1030*0b57cec5SDimitry Andric     }
1031*0b57cec5SDimitry Andric   } else {
1032*0b57cec5SDimitry Andric 
1033*0b57cec5SDimitry Andric     std::unordered_set<lldb::break_id_t> processed_bkpts;
1034*0b57cec5SDimitry Andric     const size_t count = bp_ids.GetSize();
1035*0b57cec5SDimitry Andric     for (size_t i = 0; i < count; ++i) {
1036*0b57cec5SDimitry Andric       BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1037*0b57cec5SDimitry Andric       lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID();
1038*0b57cec5SDimitry Andric 
1039*0b57cec5SDimitry Andric       if (bp_id != LLDB_INVALID_BREAK_ID) {
1040*0b57cec5SDimitry Andric         // Only do each breakpoint once:
1041*0b57cec5SDimitry Andric         std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool>
1042*0b57cec5SDimitry Andric             insert_result = processed_bkpts.insert(bp_id);
1043*0b57cec5SDimitry Andric         if (!insert_result.second)
1044*0b57cec5SDimitry Andric           continue;
1045*0b57cec5SDimitry Andric 
1046*0b57cec5SDimitry Andric         Breakpoint *bp = GetBreakpointByID(bp_id).get();
1047*0b57cec5SDimitry Andric         StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
1048*0b57cec5SDimitry Andric         // If the user explicitly asked to serialize a breakpoint, and we
1049*0b57cec5SDimitry Andric         // can't, then raise an error:
1050*0b57cec5SDimitry Andric         if (!bkpt_save_sp) {
1051*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("Unable to serialize breakpoint %d",
1052*0b57cec5SDimitry Andric                                          bp_id);
1053*0b57cec5SDimitry Andric           return error;
1054*0b57cec5SDimitry Andric         }
1055*0b57cec5SDimitry Andric         break_store_ptr->AddItem(bkpt_save_sp);
1056*0b57cec5SDimitry Andric       }
1057*0b57cec5SDimitry Andric     }
1058*0b57cec5SDimitry Andric   }
1059*0b57cec5SDimitry Andric 
1060*0b57cec5SDimitry Andric   break_store_ptr->Dump(out_file, false);
1061*0b57cec5SDimitry Andric   out_file.PutChar('\n');
1062*0b57cec5SDimitry Andric   return error;
1063*0b57cec5SDimitry Andric }
1064*0b57cec5SDimitry Andric 
1065*0b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
1066*0b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
1067*0b57cec5SDimitry Andric   std::vector<std::string> no_names;
1068*0b57cec5SDimitry Andric   return CreateBreakpointsFromFile(file, no_names, new_bps);
1069*0b57cec5SDimitry Andric }
1070*0b57cec5SDimitry Andric 
1071*0b57cec5SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
1072*0b57cec5SDimitry Andric                                          std::vector<std::string> &names,
1073*0b57cec5SDimitry Andric                                          BreakpointIDList &new_bps) {
1074*0b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> lock;
1075*0b57cec5SDimitry Andric   GetBreakpointList().GetListMutex(lock);
1076*0b57cec5SDimitry Andric 
1077*0b57cec5SDimitry Andric   Status error;
1078*0b57cec5SDimitry Andric   StructuredData::ObjectSP input_data_sp =
1079*0b57cec5SDimitry Andric       StructuredData::ParseJSONFromFile(file, error);
1080*0b57cec5SDimitry Andric   if (!error.Success()) {
1081*0b57cec5SDimitry Andric     return error;
1082*0b57cec5SDimitry Andric   } else if (!input_data_sp || !input_data_sp->IsValid()) {
1083*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Invalid JSON from input file: %s.",
1084*0b57cec5SDimitry Andric                                    file.GetPath().c_str());
1085*0b57cec5SDimitry Andric     return error;
1086*0b57cec5SDimitry Andric   }
1087*0b57cec5SDimitry Andric 
1088*0b57cec5SDimitry Andric   StructuredData::Array *bkpt_array = input_data_sp->GetAsArray();
1089*0b57cec5SDimitry Andric   if (!bkpt_array) {
1090*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
1091*0b57cec5SDimitry Andric         "Invalid breakpoint data from input file: %s.", file.GetPath().c_str());
1092*0b57cec5SDimitry Andric     return error;
1093*0b57cec5SDimitry Andric   }
1094*0b57cec5SDimitry Andric 
1095*0b57cec5SDimitry Andric   size_t num_bkpts = bkpt_array->GetSize();
1096*0b57cec5SDimitry Andric   size_t num_names = names.size();
1097*0b57cec5SDimitry Andric 
1098*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_bkpts; i++) {
1099*0b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(i);
1100*0b57cec5SDimitry Andric     // Peel off the breakpoint key, and feed the rest to the Breakpoint:
1101*0b57cec5SDimitry Andric     StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary();
1102*0b57cec5SDimitry Andric     if (!bkpt_dict) {
1103*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1104*0b57cec5SDimitry Andric           "Invalid breakpoint data for element %zu from input file: %s.", i,
1105*0b57cec5SDimitry Andric           file.GetPath().c_str());
1106*0b57cec5SDimitry Andric       return error;
1107*0b57cec5SDimitry Andric     }
1108*0b57cec5SDimitry Andric     StructuredData::ObjectSP bkpt_data_sp =
1109*0b57cec5SDimitry Andric         bkpt_dict->GetValueForKey(Breakpoint::GetSerializationKey());
1110*0b57cec5SDimitry Andric     if (num_names &&
1111*0b57cec5SDimitry Andric         !Breakpoint::SerializedBreakpointMatchesNames(bkpt_data_sp, names))
1112*0b57cec5SDimitry Andric       continue;
1113*0b57cec5SDimitry Andric 
11145ffd83dbSDimitry Andric     BreakpointSP bkpt_sp = Breakpoint::CreateFromStructuredData(
11155ffd83dbSDimitry Andric         shared_from_this(), bkpt_data_sp, error);
1116*0b57cec5SDimitry Andric     if (!error.Success()) {
1117*0b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1118*0b57cec5SDimitry Andric           "Error restoring breakpoint %zu from %s: %s.", i,
1119*0b57cec5SDimitry Andric           file.GetPath().c_str(), error.AsCString());
1120*0b57cec5SDimitry Andric       return error;
1121*0b57cec5SDimitry Andric     }
1122*0b57cec5SDimitry Andric     new_bps.AddBreakpointID(BreakpointID(bkpt_sp->GetID()));
1123*0b57cec5SDimitry Andric   }
1124*0b57cec5SDimitry Andric   return error;
1125*0b57cec5SDimitry Andric }
1126*0b57cec5SDimitry Andric 
1127*0b57cec5SDimitry Andric // The flag 'end_to_end', default to true, signifies that the operation is
1128*0b57cec5SDimitry Andric // performed end to end, for both the debugger and the debuggee.
1129*0b57cec5SDimitry Andric 
1130*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1131*0b57cec5SDimitry Andric // to end operations.
1132*0b57cec5SDimitry Andric bool Target::RemoveAllWatchpoints(bool end_to_end) {
1133*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11349dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1135*0b57cec5SDimitry Andric 
1136*0b57cec5SDimitry Andric   if (!end_to_end) {
1137*0b57cec5SDimitry Andric     m_watchpoint_list.RemoveAll(true);
1138*0b57cec5SDimitry Andric     return true;
1139*0b57cec5SDimitry Andric   }
1140*0b57cec5SDimitry Andric 
1141*0b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
1142*0b57cec5SDimitry Andric 
1143*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1144*0b57cec5SDimitry Andric     return false;
1145*0b57cec5SDimitry Andric 
1146*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1147*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1148*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1149*0b57cec5SDimitry Andric     if (!wp_sp)
1150*0b57cec5SDimitry Andric       return false;
1151*0b57cec5SDimitry Andric 
1152*0b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
1153*0b57cec5SDimitry Andric     if (rc.Fail())
1154*0b57cec5SDimitry Andric       return false;
1155*0b57cec5SDimitry Andric   }
1156*0b57cec5SDimitry Andric   m_watchpoint_list.RemoveAll(true);
1157*0b57cec5SDimitry Andric   m_last_created_watchpoint.reset();
1158*0b57cec5SDimitry Andric   return true; // Success!
1159*0b57cec5SDimitry Andric }
1160*0b57cec5SDimitry Andric 
1161*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1162*0b57cec5SDimitry Andric // to end operations.
1163*0b57cec5SDimitry Andric bool Target::DisableAllWatchpoints(bool end_to_end) {
1164*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11659dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1166*0b57cec5SDimitry Andric 
1167*0b57cec5SDimitry Andric   if (!end_to_end) {
1168*0b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(false);
1169*0b57cec5SDimitry Andric     return true;
1170*0b57cec5SDimitry Andric   }
1171*0b57cec5SDimitry Andric 
1172*0b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
1173*0b57cec5SDimitry Andric 
1174*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1175*0b57cec5SDimitry Andric     return false;
1176*0b57cec5SDimitry Andric 
1177*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1178*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1179*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1180*0b57cec5SDimitry Andric     if (!wp_sp)
1181*0b57cec5SDimitry Andric       return false;
1182*0b57cec5SDimitry Andric 
1183*0b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
1184*0b57cec5SDimitry Andric     if (rc.Fail())
1185*0b57cec5SDimitry Andric       return false;
1186*0b57cec5SDimitry Andric   }
1187*0b57cec5SDimitry Andric   return true; // Success!
1188*0b57cec5SDimitry Andric }
1189*0b57cec5SDimitry Andric 
1190*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1191*0b57cec5SDimitry Andric // to end operations.
1192*0b57cec5SDimitry Andric bool Target::EnableAllWatchpoints(bool end_to_end) {
1193*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
11949dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1195*0b57cec5SDimitry Andric 
1196*0b57cec5SDimitry Andric   if (!end_to_end) {
1197*0b57cec5SDimitry Andric     m_watchpoint_list.SetEnabledAll(true);
1198*0b57cec5SDimitry Andric     return true;
1199*0b57cec5SDimitry Andric   }
1200*0b57cec5SDimitry Andric 
1201*0b57cec5SDimitry Andric   // Otherwise, it's an end to end operation.
1202*0b57cec5SDimitry Andric 
1203*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1204*0b57cec5SDimitry Andric     return false;
1205*0b57cec5SDimitry Andric 
1206*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1207*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1208*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1209*0b57cec5SDimitry Andric     if (!wp_sp)
1210*0b57cec5SDimitry Andric       return false;
1211*0b57cec5SDimitry Andric 
1212*0b57cec5SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
1213*0b57cec5SDimitry Andric     if (rc.Fail())
1214*0b57cec5SDimitry Andric       return false;
1215*0b57cec5SDimitry Andric   }
1216*0b57cec5SDimitry Andric   return true; // Success!
1217*0b57cec5SDimitry Andric }
1218*0b57cec5SDimitry Andric 
1219*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1220*0b57cec5SDimitry Andric bool Target::ClearAllWatchpointHitCounts() {
1221*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12229dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1223*0b57cec5SDimitry Andric 
1224*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1225*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1226*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1227*0b57cec5SDimitry Andric     if (!wp_sp)
1228*0b57cec5SDimitry Andric       return false;
1229*0b57cec5SDimitry Andric 
1230*0b57cec5SDimitry Andric     wp_sp->ResetHitCount();
1231*0b57cec5SDimitry Andric   }
1232*0b57cec5SDimitry Andric   return true; // Success!
1233*0b57cec5SDimitry Andric }
1234*0b57cec5SDimitry Andric 
1235*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1236*0b57cec5SDimitry Andric bool Target::ClearAllWatchpointHistoricValues() {
1237*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12389dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1239*0b57cec5SDimitry Andric 
1240*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1241*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1242*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1243*0b57cec5SDimitry Andric     if (!wp_sp)
1244*0b57cec5SDimitry Andric       return false;
1245*0b57cec5SDimitry Andric 
1246*0b57cec5SDimitry Andric     wp_sp->ResetHistoricValues();
1247*0b57cec5SDimitry Andric   }
1248*0b57cec5SDimitry Andric   return true; // Success!
1249*0b57cec5SDimitry Andric }
1250*0b57cec5SDimitry Andric 
1251*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list during
1252*0b57cec5SDimitry Andric // these operations.
1253*0b57cec5SDimitry Andric bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) {
1254*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12559dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1256*0b57cec5SDimitry Andric 
1257*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1258*0b57cec5SDimitry Andric     return false;
1259*0b57cec5SDimitry Andric 
1260*0b57cec5SDimitry Andric   size_t num_watchpoints = m_watchpoint_list.GetSize();
1261*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_watchpoints; ++i) {
1262*0b57cec5SDimitry Andric     WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1263*0b57cec5SDimitry Andric     if (!wp_sp)
1264*0b57cec5SDimitry Andric       return false;
1265*0b57cec5SDimitry Andric 
1266*0b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
1267*0b57cec5SDimitry Andric   }
1268*0b57cec5SDimitry Andric   return true; // Success!
1269*0b57cec5SDimitry Andric }
1270*0b57cec5SDimitry Andric 
1271*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1272*0b57cec5SDimitry Andric bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) {
1273*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12749dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1275*0b57cec5SDimitry Andric 
1276*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1277*0b57cec5SDimitry Andric     return false;
1278*0b57cec5SDimitry Andric 
1279*0b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1280*0b57cec5SDimitry Andric   if (wp_sp) {
1281*0b57cec5SDimitry Andric     Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
1282*0b57cec5SDimitry Andric     if (rc.Success())
1283*0b57cec5SDimitry Andric       return true;
1284*0b57cec5SDimitry Andric 
1285*0b57cec5SDimitry Andric     // Else, fallthrough.
1286*0b57cec5SDimitry Andric   }
1287*0b57cec5SDimitry Andric   return false;
1288*0b57cec5SDimitry Andric }
1289*0b57cec5SDimitry Andric 
1290*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1291*0b57cec5SDimitry Andric bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) {
1292*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
12939dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1294*0b57cec5SDimitry Andric 
1295*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1296*0b57cec5SDimitry Andric     return false;
1297*0b57cec5SDimitry Andric 
1298*0b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1299*0b57cec5SDimitry Andric   if (wp_sp) {
1300*0b57cec5SDimitry Andric     Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
1301*0b57cec5SDimitry Andric     if (rc.Success())
1302*0b57cec5SDimitry Andric       return true;
1303*0b57cec5SDimitry Andric 
1304*0b57cec5SDimitry Andric     // Else, fallthrough.
1305*0b57cec5SDimitry Andric   }
1306*0b57cec5SDimitry Andric   return false;
1307*0b57cec5SDimitry Andric }
1308*0b57cec5SDimitry Andric 
1309*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1310*0b57cec5SDimitry Andric bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) {
1311*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
13129dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1313*0b57cec5SDimitry Andric 
1314*0b57cec5SDimitry Andric   WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1315*0b57cec5SDimitry Andric   if (watch_to_remove_sp == m_last_created_watchpoint)
1316*0b57cec5SDimitry Andric     m_last_created_watchpoint.reset();
1317*0b57cec5SDimitry Andric 
1318*0b57cec5SDimitry Andric   if (DisableWatchpointByID(watch_id)) {
1319*0b57cec5SDimitry Andric     m_watchpoint_list.Remove(watch_id, true);
1320*0b57cec5SDimitry Andric     return true;
1321*0b57cec5SDimitry Andric   }
1322*0b57cec5SDimitry Andric   return false;
1323*0b57cec5SDimitry Andric }
1324*0b57cec5SDimitry Andric 
1325*0b57cec5SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1326*0b57cec5SDimitry Andric bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id,
1327*0b57cec5SDimitry Andric                                   uint32_t ignore_count) {
1328*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
13299dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1330*0b57cec5SDimitry Andric 
1331*0b57cec5SDimitry Andric   if (!ProcessIsValid())
1332*0b57cec5SDimitry Andric     return false;
1333*0b57cec5SDimitry Andric 
1334*0b57cec5SDimitry Andric   WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1335*0b57cec5SDimitry Andric   if (wp_sp) {
1336*0b57cec5SDimitry Andric     wp_sp->SetIgnoreCount(ignore_count);
1337*0b57cec5SDimitry Andric     return true;
1338*0b57cec5SDimitry Andric   }
1339*0b57cec5SDimitry Andric   return false;
1340*0b57cec5SDimitry Andric }
1341*0b57cec5SDimitry Andric 
1342*0b57cec5SDimitry Andric ModuleSP Target::GetExecutableModule() {
1343*0b57cec5SDimitry Andric   // search for the first executable in the module list
1344*0b57cec5SDimitry Andric   for (size_t i = 0; i < m_images.GetSize(); ++i) {
1345*0b57cec5SDimitry Andric     ModuleSP module_sp = m_images.GetModuleAtIndex(i);
1346*0b57cec5SDimitry Andric     lldb_private::ObjectFile *obj = module_sp->GetObjectFile();
1347*0b57cec5SDimitry Andric     if (obj == nullptr)
1348*0b57cec5SDimitry Andric       continue;
1349*0b57cec5SDimitry Andric     if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1350*0b57cec5SDimitry Andric       return module_sp;
1351*0b57cec5SDimitry Andric   }
1352*0b57cec5SDimitry Andric   // as fall back return the first module loaded
1353*0b57cec5SDimitry Andric   return m_images.GetModuleAtIndex(0);
1354*0b57cec5SDimitry Andric }
1355*0b57cec5SDimitry Andric 
1356*0b57cec5SDimitry Andric Module *Target::GetExecutableModulePointer() {
1357*0b57cec5SDimitry Andric   return GetExecutableModule().get();
1358*0b57cec5SDimitry Andric }
1359*0b57cec5SDimitry Andric 
1360*0b57cec5SDimitry Andric static void LoadScriptingResourceForModule(const ModuleSP &module_sp,
1361*0b57cec5SDimitry Andric                                            Target *target) {
1362*0b57cec5SDimitry Andric   Status error;
1363*0b57cec5SDimitry Andric   StreamString feedback_stream;
13649dba64beSDimitry Andric   if (module_sp && !module_sp->LoadScriptingResourceInTarget(
13659dba64beSDimitry Andric                        target, error, &feedback_stream)) {
1366*0b57cec5SDimitry Andric     if (error.AsCString())
13679dba64beSDimitry Andric       target->GetDebugger().GetErrorStream().Printf(
1368*0b57cec5SDimitry Andric           "unable to load scripting data for module %s - error reported was "
1369*0b57cec5SDimitry Andric           "%s\n",
1370*0b57cec5SDimitry Andric           module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1371*0b57cec5SDimitry Andric           error.AsCString());
1372*0b57cec5SDimitry Andric   }
1373*0b57cec5SDimitry Andric   if (feedback_stream.GetSize())
13749dba64beSDimitry Andric     target->GetDebugger().GetErrorStream().Printf("%s\n",
1375*0b57cec5SDimitry Andric                                                   feedback_stream.GetData());
1376*0b57cec5SDimitry Andric }
1377*0b57cec5SDimitry Andric 
1378*0b57cec5SDimitry Andric void Target::ClearModules(bool delete_locations) {
1379*0b57cec5SDimitry Andric   ModulesDidUnload(m_images, delete_locations);
1380*0b57cec5SDimitry Andric   m_section_load_history.Clear();
1381*0b57cec5SDimitry Andric   m_images.Clear();
1382*0b57cec5SDimitry Andric   m_scratch_type_system_map.Clear();
1383*0b57cec5SDimitry Andric }
1384*0b57cec5SDimitry Andric 
1385*0b57cec5SDimitry Andric void Target::DidExec() {
1386*0b57cec5SDimitry Andric   // When a process exec's we need to know about it so we can do some cleanup.
1387*0b57cec5SDimitry Andric   m_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
1388*0b57cec5SDimitry Andric   m_internal_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
1389*0b57cec5SDimitry Andric }
1390*0b57cec5SDimitry Andric 
1391*0b57cec5SDimitry Andric void Target::SetExecutableModule(ModuleSP &executable_sp,
1392*0b57cec5SDimitry Andric                                  LoadDependentFiles load_dependent_files) {
1393*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
1394*0b57cec5SDimitry Andric   ClearModules(false);
1395*0b57cec5SDimitry Andric 
1396*0b57cec5SDimitry Andric   if (executable_sp) {
1397*0b57cec5SDimitry Andric     static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1398*0b57cec5SDimitry Andric     Timer scoped_timer(func_cat,
1399*0b57cec5SDimitry Andric                        "Target::SetExecutableModule (executable = '%s')",
1400*0b57cec5SDimitry Andric                        executable_sp->GetFileSpec().GetPath().c_str());
1401*0b57cec5SDimitry Andric 
1402*0b57cec5SDimitry Andric     const bool notify = true;
14039dba64beSDimitry Andric     m_images.Append(executable_sp,
14049dba64beSDimitry Andric                     notify); // The first image is our executable file
1405*0b57cec5SDimitry Andric 
1406*0b57cec5SDimitry Andric     // If we haven't set an architecture yet, reset our architecture based on
1407*0b57cec5SDimitry Andric     // what we found in the executable module.
1408*0b57cec5SDimitry Andric     if (!m_arch.GetSpec().IsValid()) {
1409*0b57cec5SDimitry Andric       m_arch = executable_sp->GetArchitecture();
1410*0b57cec5SDimitry Andric       LLDB_LOG(log,
1411*0b57cec5SDimitry Andric                "setting architecture to {0} ({1}) based on executable file",
1412*0b57cec5SDimitry Andric                m_arch.GetSpec().GetArchitectureName(),
1413*0b57cec5SDimitry Andric                m_arch.GetSpec().GetTriple().getTriple());
1414*0b57cec5SDimitry Andric     }
1415*0b57cec5SDimitry Andric 
1416*0b57cec5SDimitry Andric     FileSpecList dependent_files;
1417*0b57cec5SDimitry Andric     ObjectFile *executable_objfile = executable_sp->GetObjectFile();
1418*0b57cec5SDimitry Andric     bool load_dependents = true;
1419*0b57cec5SDimitry Andric     switch (load_dependent_files) {
1420*0b57cec5SDimitry Andric     case eLoadDependentsDefault:
1421*0b57cec5SDimitry Andric       load_dependents = executable_sp->IsExecutable();
1422*0b57cec5SDimitry Andric       break;
1423*0b57cec5SDimitry Andric     case eLoadDependentsYes:
1424*0b57cec5SDimitry Andric       load_dependents = true;
1425*0b57cec5SDimitry Andric       break;
1426*0b57cec5SDimitry Andric     case eLoadDependentsNo:
1427*0b57cec5SDimitry Andric       load_dependents = false;
1428*0b57cec5SDimitry Andric       break;
1429*0b57cec5SDimitry Andric     }
1430*0b57cec5SDimitry Andric 
1431*0b57cec5SDimitry Andric     if (executable_objfile && load_dependents) {
1432*0b57cec5SDimitry Andric       ModuleList added_modules;
1433*0b57cec5SDimitry Andric       executable_objfile->GetDependentModules(dependent_files);
1434*0b57cec5SDimitry Andric       for (uint32_t i = 0; i < dependent_files.GetSize(); i++) {
1435480093f4SDimitry Andric         FileSpec dependent_file_spec(dependent_files.GetFileSpecAtIndex(i));
1436*0b57cec5SDimitry Andric         FileSpec platform_dependent_file_spec;
1437*0b57cec5SDimitry Andric         if (m_platform_sp)
1438*0b57cec5SDimitry Andric           m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr,
1439*0b57cec5SDimitry Andric                                          platform_dependent_file_spec);
1440*0b57cec5SDimitry Andric         else
1441*0b57cec5SDimitry Andric           platform_dependent_file_spec = dependent_file_spec;
1442*0b57cec5SDimitry Andric 
1443*0b57cec5SDimitry Andric         ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec());
14449dba64beSDimitry Andric         ModuleSP image_module_sp(
14459dba64beSDimitry Andric             GetOrCreateModule(module_spec, false /* notify */));
1446*0b57cec5SDimitry Andric         if (image_module_sp) {
1447*0b57cec5SDimitry Andric           added_modules.AppendIfNeeded(image_module_sp, false);
1448*0b57cec5SDimitry Andric           ObjectFile *objfile = image_module_sp->GetObjectFile();
1449*0b57cec5SDimitry Andric           if (objfile)
1450*0b57cec5SDimitry Andric             objfile->GetDependentModules(dependent_files);
1451*0b57cec5SDimitry Andric         }
1452*0b57cec5SDimitry Andric       }
1453*0b57cec5SDimitry Andric       ModulesDidLoad(added_modules);
1454*0b57cec5SDimitry Andric     }
1455*0b57cec5SDimitry Andric   }
1456*0b57cec5SDimitry Andric }
1457*0b57cec5SDimitry Andric 
1458*0b57cec5SDimitry Andric bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform) {
1459*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
1460*0b57cec5SDimitry Andric   bool missing_local_arch = !m_arch.GetSpec().IsValid();
1461*0b57cec5SDimitry Andric   bool replace_local_arch = true;
1462*0b57cec5SDimitry Andric   bool compatible_local_arch = false;
1463*0b57cec5SDimitry Andric   ArchSpec other(arch_spec);
1464*0b57cec5SDimitry Andric 
1465*0b57cec5SDimitry Andric   // Changing the architecture might mean that the currently selected platform
1466*0b57cec5SDimitry Andric   // isn't compatible. Set the platform correctly if we are asked to do so,
1467*0b57cec5SDimitry Andric   // otherwise assume the user will set the platform manually.
1468*0b57cec5SDimitry Andric   if (set_platform) {
1469*0b57cec5SDimitry Andric     if (other.IsValid()) {
1470*0b57cec5SDimitry Andric       auto platform_sp = GetPlatform();
1471*0b57cec5SDimitry Andric       if (!platform_sp ||
1472*0b57cec5SDimitry Andric           !platform_sp->IsCompatibleArchitecture(other, false, nullptr)) {
1473*0b57cec5SDimitry Andric         ArchSpec platform_arch;
1474*0b57cec5SDimitry Andric         auto arch_platform_sp =
1475*0b57cec5SDimitry Andric             Platform::GetPlatformForArchitecture(other, &platform_arch);
1476*0b57cec5SDimitry Andric         if (arch_platform_sp) {
1477*0b57cec5SDimitry Andric           SetPlatform(arch_platform_sp);
1478*0b57cec5SDimitry Andric           if (platform_arch.IsValid())
1479*0b57cec5SDimitry Andric             other = platform_arch;
1480*0b57cec5SDimitry Andric         }
1481*0b57cec5SDimitry Andric       }
1482*0b57cec5SDimitry Andric     }
1483*0b57cec5SDimitry Andric   }
1484*0b57cec5SDimitry Andric 
1485*0b57cec5SDimitry Andric   if (!missing_local_arch) {
1486*0b57cec5SDimitry Andric     if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
1487*0b57cec5SDimitry Andric       other.MergeFrom(m_arch.GetSpec());
1488*0b57cec5SDimitry Andric 
1489*0b57cec5SDimitry Andric       if (m_arch.GetSpec().IsCompatibleMatch(other)) {
1490*0b57cec5SDimitry Andric         compatible_local_arch = true;
1491*0b57cec5SDimitry Andric         bool arch_changed, vendor_changed, os_changed, os_ver_changed,
1492*0b57cec5SDimitry Andric             env_changed;
1493*0b57cec5SDimitry Andric 
14949dba64beSDimitry Andric         m_arch.GetSpec().PiecewiseTripleCompare(other, arch_changed,
14959dba64beSDimitry Andric                                                 vendor_changed, os_changed,
14969dba64beSDimitry Andric                                                 os_ver_changed, env_changed);
1497*0b57cec5SDimitry Andric 
1498*0b57cec5SDimitry Andric         if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
1499*0b57cec5SDimitry Andric           replace_local_arch = false;
1500*0b57cec5SDimitry Andric       }
1501*0b57cec5SDimitry Andric     }
1502*0b57cec5SDimitry Andric   }
1503*0b57cec5SDimitry Andric 
1504*0b57cec5SDimitry Andric   if (compatible_local_arch || missing_local_arch) {
1505*0b57cec5SDimitry Andric     // If we haven't got a valid arch spec, or the architectures are compatible
1506*0b57cec5SDimitry Andric     // update the architecture, unless the one we already have is more
1507*0b57cec5SDimitry Andric     // specified
1508*0b57cec5SDimitry Andric     if (replace_local_arch)
1509*0b57cec5SDimitry Andric       m_arch = other;
1510*0b57cec5SDimitry Andric     LLDB_LOG(log, "set architecture to {0} ({1})",
1511*0b57cec5SDimitry Andric              m_arch.GetSpec().GetArchitectureName(),
1512*0b57cec5SDimitry Andric              m_arch.GetSpec().GetTriple().getTriple());
1513*0b57cec5SDimitry Andric     return true;
1514*0b57cec5SDimitry Andric   }
1515*0b57cec5SDimitry Andric 
1516*0b57cec5SDimitry Andric   // If we have an executable file, try to reset the executable to the desired
1517*0b57cec5SDimitry Andric   // architecture
15189dba64beSDimitry Andric   LLDB_LOGF(log, "Target::SetArchitecture changing architecture to %s (%s)",
1519*0b57cec5SDimitry Andric             arch_spec.GetArchitectureName(),
1520*0b57cec5SDimitry Andric             arch_spec.GetTriple().getTriple().c_str());
1521*0b57cec5SDimitry Andric   m_arch = other;
1522*0b57cec5SDimitry Andric   ModuleSP executable_sp = GetExecutableModule();
1523*0b57cec5SDimitry Andric 
1524*0b57cec5SDimitry Andric   ClearModules(true);
1525*0b57cec5SDimitry Andric   // Need to do something about unsetting breakpoints.
1526*0b57cec5SDimitry Andric 
1527*0b57cec5SDimitry Andric   if (executable_sp) {
15289dba64beSDimitry Andric     LLDB_LOGF(log,
15299dba64beSDimitry Andric               "Target::SetArchitecture Trying to select executable file "
1530*0b57cec5SDimitry Andric               "architecture %s (%s)",
1531*0b57cec5SDimitry Andric               arch_spec.GetArchitectureName(),
1532*0b57cec5SDimitry Andric               arch_spec.GetTriple().getTriple().c_str());
1533*0b57cec5SDimitry Andric     ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
1534*0b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
1535*0b57cec5SDimitry Andric     Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
15369dba64beSDimitry Andric                                                &search_paths, nullptr, nullptr);
1537*0b57cec5SDimitry Andric 
1538*0b57cec5SDimitry Andric     if (!error.Fail() && executable_sp) {
1539*0b57cec5SDimitry Andric       SetExecutableModule(executable_sp, eLoadDependentsYes);
1540*0b57cec5SDimitry Andric       return true;
1541*0b57cec5SDimitry Andric     }
1542*0b57cec5SDimitry Andric   }
1543*0b57cec5SDimitry Andric   return false;
1544*0b57cec5SDimitry Andric }
1545*0b57cec5SDimitry Andric 
1546*0b57cec5SDimitry Andric bool Target::MergeArchitecture(const ArchSpec &arch_spec) {
1547*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
1548*0b57cec5SDimitry Andric   if (arch_spec.IsValid()) {
1549*0b57cec5SDimitry Andric     if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
1550*0b57cec5SDimitry Andric       // The current target arch is compatible with "arch_spec", see if we can
1551*0b57cec5SDimitry Andric       // improve our current architecture using bits from "arch_spec"
1552*0b57cec5SDimitry Andric 
15539dba64beSDimitry Andric       LLDB_LOGF(log,
15549dba64beSDimitry Andric                 "Target::MergeArchitecture target has arch %s, merging with "
1555*0b57cec5SDimitry Andric                 "arch %s",
1556*0b57cec5SDimitry Andric                 m_arch.GetSpec().GetTriple().getTriple().c_str(),
1557*0b57cec5SDimitry Andric                 arch_spec.GetTriple().getTriple().c_str());
1558*0b57cec5SDimitry Andric 
1559*0b57cec5SDimitry Andric       // Merge bits from arch_spec into "merged_arch" and set our architecture
1560*0b57cec5SDimitry Andric       ArchSpec merged_arch(m_arch.GetSpec());
1561*0b57cec5SDimitry Andric       merged_arch.MergeFrom(arch_spec);
1562*0b57cec5SDimitry Andric       return SetArchitecture(merged_arch);
1563*0b57cec5SDimitry Andric     } else {
1564*0b57cec5SDimitry Andric       // The new architecture is different, we just need to replace it
1565*0b57cec5SDimitry Andric       return SetArchitecture(arch_spec);
1566*0b57cec5SDimitry Andric     }
1567*0b57cec5SDimitry Andric   }
1568*0b57cec5SDimitry Andric   return false;
1569*0b57cec5SDimitry Andric }
1570*0b57cec5SDimitry Andric 
1571*0b57cec5SDimitry Andric void Target::NotifyWillClearList(const ModuleList &module_list) {}
1572*0b57cec5SDimitry Andric 
1573*0b57cec5SDimitry Andric void Target::NotifyModuleAdded(const ModuleList &module_list,
1574*0b57cec5SDimitry Andric                                const ModuleSP &module_sp) {
1575*0b57cec5SDimitry Andric   // A module is being added to this target for the first time
1576*0b57cec5SDimitry Andric   if (m_valid) {
1577*0b57cec5SDimitry Andric     ModuleList my_module_list;
1578*0b57cec5SDimitry Andric     my_module_list.Append(module_sp);
1579*0b57cec5SDimitry Andric     ModulesDidLoad(my_module_list);
1580*0b57cec5SDimitry Andric   }
1581*0b57cec5SDimitry Andric }
1582*0b57cec5SDimitry Andric 
1583*0b57cec5SDimitry Andric void Target::NotifyModuleRemoved(const ModuleList &module_list,
1584*0b57cec5SDimitry Andric                                  const ModuleSP &module_sp) {
1585*0b57cec5SDimitry Andric   // A module is being removed from this target.
1586*0b57cec5SDimitry Andric   if (m_valid) {
1587*0b57cec5SDimitry Andric     ModuleList my_module_list;
1588*0b57cec5SDimitry Andric     my_module_list.Append(module_sp);
1589*0b57cec5SDimitry Andric     ModulesDidUnload(my_module_list, false);
1590*0b57cec5SDimitry Andric   }
1591*0b57cec5SDimitry Andric }
1592*0b57cec5SDimitry Andric 
1593*0b57cec5SDimitry Andric void Target::NotifyModuleUpdated(const ModuleList &module_list,
1594*0b57cec5SDimitry Andric                                  const ModuleSP &old_module_sp,
1595*0b57cec5SDimitry Andric                                  const ModuleSP &new_module_sp) {
1596*0b57cec5SDimitry Andric   // A module is replacing an already added module
1597*0b57cec5SDimitry Andric   if (m_valid) {
1598*0b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp,
1599*0b57cec5SDimitry Andric                                                             new_module_sp);
1600*0b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(
1601*0b57cec5SDimitry Andric         old_module_sp, new_module_sp);
1602*0b57cec5SDimitry Andric   }
1603*0b57cec5SDimitry Andric }
1604*0b57cec5SDimitry Andric 
1605*0b57cec5SDimitry Andric void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) {
1606*0b57cec5SDimitry Andric   ModulesDidUnload(module_list, false);
1607*0b57cec5SDimitry Andric }
1608*0b57cec5SDimitry Andric 
1609*0b57cec5SDimitry Andric void Target::ModulesDidLoad(ModuleList &module_list) {
1610*0b57cec5SDimitry Andric   const size_t num_images = module_list.GetSize();
1611*0b57cec5SDimitry Andric   if (m_valid && num_images) {
1612*0b57cec5SDimitry Andric     for (size_t idx = 0; idx < num_images; ++idx) {
1613*0b57cec5SDimitry Andric       ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
1614*0b57cec5SDimitry Andric       LoadScriptingResourceForModule(module_sp, this);
1615*0b57cec5SDimitry Andric     }
1616*0b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1617*0b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1618*0b57cec5SDimitry Andric     if (m_process_sp) {
1619*0b57cec5SDimitry Andric       m_process_sp->ModulesDidLoad(module_list);
1620*0b57cec5SDimitry Andric     }
1621*0b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitModulesLoaded,
1622*0b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
1623*0b57cec5SDimitry Andric   }
1624*0b57cec5SDimitry Andric }
1625*0b57cec5SDimitry Andric 
1626*0b57cec5SDimitry Andric void Target::SymbolsDidLoad(ModuleList &module_list) {
1627*0b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
1628*0b57cec5SDimitry Andric     if (m_process_sp) {
1629*0b57cec5SDimitry Andric       for (LanguageRuntime *runtime : m_process_sp->GetLanguageRuntimes()) {
1630*0b57cec5SDimitry Andric         runtime->SymbolsDidLoad(module_list);
1631*0b57cec5SDimitry Andric       }
1632*0b57cec5SDimitry Andric     }
1633*0b57cec5SDimitry Andric 
1634*0b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1635*0b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1636*0b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitSymbolsLoaded,
1637*0b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
1638*0b57cec5SDimitry Andric   }
1639*0b57cec5SDimitry Andric }
1640*0b57cec5SDimitry Andric 
1641*0b57cec5SDimitry Andric void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) {
1642*0b57cec5SDimitry Andric   if (m_valid && module_list.GetSize()) {
1643*0b57cec5SDimitry Andric     UnloadModuleSections(module_list);
1644*0b57cec5SDimitry Andric     m_breakpoint_list.UpdateBreakpoints(module_list, false, delete_locations);
1645*0b57cec5SDimitry Andric     m_internal_breakpoint_list.UpdateBreakpoints(module_list, false,
1646*0b57cec5SDimitry Andric                                                  delete_locations);
1647*0b57cec5SDimitry Andric     BroadcastEvent(eBroadcastBitModulesUnloaded,
1648*0b57cec5SDimitry Andric                    new TargetEventData(this->shared_from_this(), module_list));
1649*0b57cec5SDimitry Andric   }
1650*0b57cec5SDimitry Andric }
1651*0b57cec5SDimitry Andric 
1652*0b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
1653*0b57cec5SDimitry Andric     const FileSpec &module_file_spec) {
1654*0b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
1655*0b57cec5SDimitry Andric     ModuleList matchingModules;
1656*0b57cec5SDimitry Andric     ModuleSpec module_spec(module_file_spec);
16579dba64beSDimitry Andric     GetImages().FindModules(module_spec, matchingModules);
16589dba64beSDimitry Andric     size_t num_modules = matchingModules.GetSize();
1659*0b57cec5SDimitry Andric 
16609dba64beSDimitry Andric     // If there is more than one module for this file spec, only
16619dba64beSDimitry Andric     // return true if ALL the modules are on the black list.
1662*0b57cec5SDimitry Andric     if (num_modules > 0) {
1663*0b57cec5SDimitry Andric       for (size_t i = 0; i < num_modules; i++) {
1664*0b57cec5SDimitry Andric         if (!ModuleIsExcludedForUnconstrainedSearches(
1665*0b57cec5SDimitry Andric                 matchingModules.GetModuleAtIndex(i)))
1666*0b57cec5SDimitry Andric           return false;
1667*0b57cec5SDimitry Andric       }
1668*0b57cec5SDimitry Andric       return true;
1669*0b57cec5SDimitry Andric     }
1670*0b57cec5SDimitry Andric   }
1671*0b57cec5SDimitry Andric   return false;
1672*0b57cec5SDimitry Andric }
1673*0b57cec5SDimitry Andric 
1674*0b57cec5SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
1675*0b57cec5SDimitry Andric     const lldb::ModuleSP &module_sp) {
1676*0b57cec5SDimitry Andric   if (GetBreakpointsConsultPlatformAvoidList()) {
1677*0b57cec5SDimitry Andric     if (m_platform_sp)
1678*0b57cec5SDimitry Andric       return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(*this,
1679*0b57cec5SDimitry Andric                                                                      module_sp);
1680*0b57cec5SDimitry Andric   }
1681*0b57cec5SDimitry Andric   return false;
1682*0b57cec5SDimitry Andric }
1683*0b57cec5SDimitry Andric 
1684*0b57cec5SDimitry Andric size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst,
1685*0b57cec5SDimitry Andric                                        size_t dst_len, Status &error) {
1686*0b57cec5SDimitry Andric   SectionSP section_sp(addr.GetSection());
1687*0b57cec5SDimitry Andric   if (section_sp) {
1688*0b57cec5SDimitry Andric     // If the contents of this section are encrypted, the on-disk file is
1689*0b57cec5SDimitry Andric     // unusable.  Read only from live memory.
1690*0b57cec5SDimitry Andric     if (section_sp->IsEncrypted()) {
1691*0b57cec5SDimitry Andric       error.SetErrorString("section is encrypted");
1692*0b57cec5SDimitry Andric       return 0;
1693*0b57cec5SDimitry Andric     }
1694*0b57cec5SDimitry Andric     ModuleSP module_sp(section_sp->GetModule());
1695*0b57cec5SDimitry Andric     if (module_sp) {
1696*0b57cec5SDimitry Andric       ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1697*0b57cec5SDimitry Andric       if (objfile) {
1698*0b57cec5SDimitry Andric         size_t bytes_read = objfile->ReadSectionData(
1699*0b57cec5SDimitry Andric             section_sp.get(), addr.GetOffset(), dst, dst_len);
1700*0b57cec5SDimitry Andric         if (bytes_read > 0)
1701*0b57cec5SDimitry Andric           return bytes_read;
1702*0b57cec5SDimitry Andric         else
1703*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("error reading data from section %s",
1704*0b57cec5SDimitry Andric                                          section_sp->GetName().GetCString());
1705*0b57cec5SDimitry Andric       } else
1706*0b57cec5SDimitry Andric         error.SetErrorString("address isn't from a object file");
1707*0b57cec5SDimitry Andric     } else
1708*0b57cec5SDimitry Andric       error.SetErrorString("address isn't in a module");
1709*0b57cec5SDimitry Andric   } else
1710*0b57cec5SDimitry Andric     error.SetErrorString("address doesn't contain a section that points to a "
1711*0b57cec5SDimitry Andric                          "section in a object file");
1712*0b57cec5SDimitry Andric 
1713*0b57cec5SDimitry Andric   return 0;
1714*0b57cec5SDimitry Andric }
1715*0b57cec5SDimitry Andric 
1716*0b57cec5SDimitry Andric size_t Target::ReadMemory(const Address &addr, bool prefer_file_cache,
1717*0b57cec5SDimitry Andric                           void *dst, size_t dst_len, Status &error,
1718*0b57cec5SDimitry Andric                           lldb::addr_t *load_addr_ptr) {
1719*0b57cec5SDimitry Andric   error.Clear();
1720*0b57cec5SDimitry Andric 
1721*0b57cec5SDimitry Andric   // if we end up reading this from process memory, we will fill this with the
1722*0b57cec5SDimitry Andric   // actual load address
1723*0b57cec5SDimitry Andric   if (load_addr_ptr)
1724*0b57cec5SDimitry Andric     *load_addr_ptr = LLDB_INVALID_ADDRESS;
1725*0b57cec5SDimitry Andric 
1726*0b57cec5SDimitry Andric   size_t bytes_read = 0;
1727*0b57cec5SDimitry Andric 
1728*0b57cec5SDimitry Andric   addr_t load_addr = LLDB_INVALID_ADDRESS;
1729*0b57cec5SDimitry Andric   addr_t file_addr = LLDB_INVALID_ADDRESS;
1730*0b57cec5SDimitry Andric   Address resolved_addr;
1731*0b57cec5SDimitry Andric   if (!addr.IsSectionOffset()) {
1732*0b57cec5SDimitry Andric     SectionLoadList &section_load_list = GetSectionLoadList();
1733*0b57cec5SDimitry Andric     if (section_load_list.IsEmpty()) {
1734*0b57cec5SDimitry Andric       // No sections are loaded, so we must assume we are not running yet and
1735*0b57cec5SDimitry Andric       // anything we are given is a file address.
1736*0b57cec5SDimitry Andric       file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
1737*0b57cec5SDimitry Andric                                     // offset is the file address
1738*0b57cec5SDimitry Andric       m_images.ResolveFileAddress(file_addr, resolved_addr);
1739*0b57cec5SDimitry Andric     } else {
1740*0b57cec5SDimitry Andric       // We have at least one section loaded. This can be because we have
1741*0b57cec5SDimitry Andric       // manually loaded some sections with "target modules load ..." or
1742*0b57cec5SDimitry Andric       // because we have have a live process that has sections loaded through
1743*0b57cec5SDimitry Andric       // the dynamic loader
1744*0b57cec5SDimitry Andric       load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
1745*0b57cec5SDimitry Andric                                     // offset is the load address
1746*0b57cec5SDimitry Andric       section_load_list.ResolveLoadAddress(load_addr, resolved_addr);
1747*0b57cec5SDimitry Andric     }
1748*0b57cec5SDimitry Andric   }
1749*0b57cec5SDimitry Andric   if (!resolved_addr.IsValid())
1750*0b57cec5SDimitry Andric     resolved_addr = addr;
1751*0b57cec5SDimitry Andric 
1752*0b57cec5SDimitry Andric   if (prefer_file_cache) {
1753*0b57cec5SDimitry Andric     bytes_read = ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1754*0b57cec5SDimitry Andric     if (bytes_read > 0)
1755*0b57cec5SDimitry Andric       return bytes_read;
1756*0b57cec5SDimitry Andric   }
1757*0b57cec5SDimitry Andric 
1758*0b57cec5SDimitry Andric   if (ProcessIsValid()) {
1759*0b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS)
1760*0b57cec5SDimitry Andric       load_addr = resolved_addr.GetLoadAddress(this);
1761*0b57cec5SDimitry Andric 
1762*0b57cec5SDimitry Andric     if (load_addr == LLDB_INVALID_ADDRESS) {
1763*0b57cec5SDimitry Andric       ModuleSP addr_module_sp(resolved_addr.GetModule());
1764*0b57cec5SDimitry Andric       if (addr_module_sp && addr_module_sp->GetFileSpec())
1765*0b57cec5SDimitry Andric         error.SetErrorStringWithFormatv(
1766*0b57cec5SDimitry Andric             "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded",
1767*0b57cec5SDimitry Andric             addr_module_sp->GetFileSpec(), resolved_addr.GetFileAddress());
1768*0b57cec5SDimitry Andric       else
1769*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved",
1770*0b57cec5SDimitry Andric                                        resolved_addr.GetFileAddress());
1771*0b57cec5SDimitry Andric     } else {
1772*0b57cec5SDimitry Andric       bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
1773*0b57cec5SDimitry Andric       if (bytes_read != dst_len) {
1774*0b57cec5SDimitry Andric         if (error.Success()) {
1775*0b57cec5SDimitry Andric           if (bytes_read == 0)
1776*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
1777*0b57cec5SDimitry Andric                 "read memory from 0x%" PRIx64 " failed", load_addr);
1778*0b57cec5SDimitry Andric           else
1779*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
1780*0b57cec5SDimitry Andric                 "only %" PRIu64 " of %" PRIu64
1781*0b57cec5SDimitry Andric                 " bytes were read from memory at 0x%" PRIx64,
1782*0b57cec5SDimitry Andric                 (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
1783*0b57cec5SDimitry Andric         }
1784*0b57cec5SDimitry Andric       }
1785*0b57cec5SDimitry Andric       if (bytes_read) {
1786*0b57cec5SDimitry Andric         if (load_addr_ptr)
1787*0b57cec5SDimitry Andric           *load_addr_ptr = load_addr;
1788*0b57cec5SDimitry Andric         return bytes_read;
1789*0b57cec5SDimitry Andric       }
1790*0b57cec5SDimitry Andric       // If the address is not section offset we have an address that doesn't
1791*0b57cec5SDimitry Andric       // resolve to any address in any currently loaded shared libraries and we
1792*0b57cec5SDimitry Andric       // failed to read memory so there isn't anything more we can do. If it is
1793*0b57cec5SDimitry Andric       // section offset, we might be able to read cached memory from the object
1794*0b57cec5SDimitry Andric       // file.
1795*0b57cec5SDimitry Andric       if (!resolved_addr.IsSectionOffset())
1796*0b57cec5SDimitry Andric         return 0;
1797*0b57cec5SDimitry Andric     }
1798*0b57cec5SDimitry Andric   }
1799*0b57cec5SDimitry Andric 
1800*0b57cec5SDimitry Andric   if (!prefer_file_cache && resolved_addr.IsSectionOffset()) {
1801*0b57cec5SDimitry Andric     // If we didn't already try and read from the object file cache, then try
1802*0b57cec5SDimitry Andric     // it after failing to read from the process.
1803*0b57cec5SDimitry Andric     return ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1804*0b57cec5SDimitry Andric   }
1805*0b57cec5SDimitry Andric   return 0;
1806*0b57cec5SDimitry Andric }
1807*0b57cec5SDimitry Andric 
1808*0b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str,
1809*0b57cec5SDimitry Andric                                      Status &error) {
1810*0b57cec5SDimitry Andric   char buf[256];
1811*0b57cec5SDimitry Andric   out_str.clear();
1812*0b57cec5SDimitry Andric   addr_t curr_addr = addr.GetLoadAddress(this);
1813*0b57cec5SDimitry Andric   Address address(addr);
1814*0b57cec5SDimitry Andric   while (true) {
1815*0b57cec5SDimitry Andric     size_t length = ReadCStringFromMemory(address, buf, sizeof(buf), error);
1816*0b57cec5SDimitry Andric     if (length == 0)
1817*0b57cec5SDimitry Andric       break;
1818*0b57cec5SDimitry Andric     out_str.append(buf, length);
1819*0b57cec5SDimitry Andric     // If we got "length - 1" bytes, we didn't get the whole C string, we need
1820*0b57cec5SDimitry Andric     // to read some more characters
1821*0b57cec5SDimitry Andric     if (length == sizeof(buf) - 1)
1822*0b57cec5SDimitry Andric       curr_addr += length;
1823*0b57cec5SDimitry Andric     else
1824*0b57cec5SDimitry Andric       break;
1825*0b57cec5SDimitry Andric     address = Address(curr_addr);
1826*0b57cec5SDimitry Andric   }
1827*0b57cec5SDimitry Andric   return out_str.size();
1828*0b57cec5SDimitry Andric }
1829*0b57cec5SDimitry Andric 
1830*0b57cec5SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, char *dst,
1831*0b57cec5SDimitry Andric                                      size_t dst_max_len, Status &result_error) {
1832*0b57cec5SDimitry Andric   size_t total_cstr_len = 0;
1833*0b57cec5SDimitry Andric   if (dst && dst_max_len) {
1834*0b57cec5SDimitry Andric     result_error.Clear();
1835*0b57cec5SDimitry Andric     // NULL out everything just to be safe
1836*0b57cec5SDimitry Andric     memset(dst, 0, dst_max_len);
1837*0b57cec5SDimitry Andric     Status error;
1838*0b57cec5SDimitry Andric     addr_t curr_addr = addr.GetLoadAddress(this);
1839*0b57cec5SDimitry Andric     Address address(addr);
1840*0b57cec5SDimitry Andric 
1841*0b57cec5SDimitry Andric     // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think
1842*0b57cec5SDimitry Andric     // this really needs to be tied to the memory cache subsystem's cache line
1843*0b57cec5SDimitry Andric     // size, so leave this as a fixed constant.
1844*0b57cec5SDimitry Andric     const size_t cache_line_size = 512;
1845*0b57cec5SDimitry Andric 
1846*0b57cec5SDimitry Andric     size_t bytes_left = dst_max_len - 1;
1847*0b57cec5SDimitry Andric     char *curr_dst = dst;
1848*0b57cec5SDimitry Andric 
1849*0b57cec5SDimitry Andric     while (bytes_left > 0) {
1850*0b57cec5SDimitry Andric       addr_t cache_line_bytes_left =
1851*0b57cec5SDimitry Andric           cache_line_size - (curr_addr % cache_line_size);
1852*0b57cec5SDimitry Andric       addr_t bytes_to_read =
1853*0b57cec5SDimitry Andric           std::min<addr_t>(bytes_left, cache_line_bytes_left);
1854*0b57cec5SDimitry Andric       size_t bytes_read =
1855*0b57cec5SDimitry Andric           ReadMemory(address, false, curr_dst, bytes_to_read, error);
1856*0b57cec5SDimitry Andric 
1857*0b57cec5SDimitry Andric       if (bytes_read == 0) {
1858*0b57cec5SDimitry Andric         result_error = error;
1859*0b57cec5SDimitry Andric         dst[total_cstr_len] = '\0';
1860*0b57cec5SDimitry Andric         break;
1861*0b57cec5SDimitry Andric       }
1862*0b57cec5SDimitry Andric       const size_t len = strlen(curr_dst);
1863*0b57cec5SDimitry Andric 
1864*0b57cec5SDimitry Andric       total_cstr_len += len;
1865*0b57cec5SDimitry Andric 
1866*0b57cec5SDimitry Andric       if (len < bytes_to_read)
1867*0b57cec5SDimitry Andric         break;
1868*0b57cec5SDimitry Andric 
1869*0b57cec5SDimitry Andric       curr_dst += bytes_read;
1870*0b57cec5SDimitry Andric       curr_addr += bytes_read;
1871*0b57cec5SDimitry Andric       bytes_left -= bytes_read;
1872*0b57cec5SDimitry Andric       address = Address(curr_addr);
1873*0b57cec5SDimitry Andric     }
1874*0b57cec5SDimitry Andric   } else {
1875*0b57cec5SDimitry Andric     if (dst == nullptr)
1876*0b57cec5SDimitry Andric       result_error.SetErrorString("invalid arguments");
1877*0b57cec5SDimitry Andric     else
1878*0b57cec5SDimitry Andric       result_error.Clear();
1879*0b57cec5SDimitry Andric   }
1880*0b57cec5SDimitry Andric   return total_cstr_len;
1881*0b57cec5SDimitry Andric }
1882*0b57cec5SDimitry Andric 
1883*0b57cec5SDimitry Andric size_t Target::ReadScalarIntegerFromMemory(const Address &addr,
1884*0b57cec5SDimitry Andric                                            bool prefer_file_cache,
1885*0b57cec5SDimitry Andric                                            uint32_t byte_size, bool is_signed,
1886*0b57cec5SDimitry Andric                                            Scalar &scalar, Status &error) {
1887*0b57cec5SDimitry Andric   uint64_t uval;
1888*0b57cec5SDimitry Andric 
1889*0b57cec5SDimitry Andric   if (byte_size <= sizeof(uval)) {
1890*0b57cec5SDimitry Andric     size_t bytes_read =
1891*0b57cec5SDimitry Andric         ReadMemory(addr, prefer_file_cache, &uval, byte_size, error);
1892*0b57cec5SDimitry Andric     if (bytes_read == byte_size) {
1893*0b57cec5SDimitry Andric       DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(),
1894*0b57cec5SDimitry Andric                          m_arch.GetSpec().GetAddressByteSize());
1895*0b57cec5SDimitry Andric       lldb::offset_t offset = 0;
1896*0b57cec5SDimitry Andric       if (byte_size <= 4)
1897*0b57cec5SDimitry Andric         scalar = data.GetMaxU32(&offset, byte_size);
1898*0b57cec5SDimitry Andric       else
1899*0b57cec5SDimitry Andric         scalar = data.GetMaxU64(&offset, byte_size);
1900*0b57cec5SDimitry Andric 
1901*0b57cec5SDimitry Andric       if (is_signed)
1902*0b57cec5SDimitry Andric         scalar.SignExtend(byte_size * 8);
1903*0b57cec5SDimitry Andric       return bytes_read;
1904*0b57cec5SDimitry Andric     }
1905*0b57cec5SDimitry Andric   } else {
1906*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
1907*0b57cec5SDimitry Andric         "byte size of %u is too large for integer scalar type", byte_size);
1908*0b57cec5SDimitry Andric   }
1909*0b57cec5SDimitry Andric   return 0;
1910*0b57cec5SDimitry Andric }
1911*0b57cec5SDimitry Andric 
1912*0b57cec5SDimitry Andric uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr,
1913*0b57cec5SDimitry Andric                                                bool prefer_file_cache,
1914*0b57cec5SDimitry Andric                                                size_t integer_byte_size,
1915*0b57cec5SDimitry Andric                                                uint64_t fail_value,
1916*0b57cec5SDimitry Andric                                                Status &error) {
1917*0b57cec5SDimitry Andric   Scalar scalar;
1918*0b57cec5SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, prefer_file_cache, integer_byte_size,
1919*0b57cec5SDimitry Andric                                   false, scalar, error))
1920*0b57cec5SDimitry Andric     return scalar.ULongLong(fail_value);
1921*0b57cec5SDimitry Andric   return fail_value;
1922*0b57cec5SDimitry Andric }
1923*0b57cec5SDimitry Andric 
1924*0b57cec5SDimitry Andric bool Target::ReadPointerFromMemory(const Address &addr, bool prefer_file_cache,
1925*0b57cec5SDimitry Andric                                    Status &error, Address &pointer_addr) {
1926*0b57cec5SDimitry Andric   Scalar scalar;
1927*0b57cec5SDimitry Andric   if (ReadScalarIntegerFromMemory(addr, prefer_file_cache,
19289dba64beSDimitry Andric                                   m_arch.GetSpec().GetAddressByteSize(), false,
19299dba64beSDimitry Andric                                   scalar, error)) {
1930*0b57cec5SDimitry Andric     addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1931*0b57cec5SDimitry Andric     if (pointer_vm_addr != LLDB_INVALID_ADDRESS) {
1932*0b57cec5SDimitry Andric       SectionLoadList &section_load_list = GetSectionLoadList();
1933*0b57cec5SDimitry Andric       if (section_load_list.IsEmpty()) {
1934*0b57cec5SDimitry Andric         // No sections are loaded, so we must assume we are not running yet and
1935*0b57cec5SDimitry Andric         // anything we are given is a file address.
1936*0b57cec5SDimitry Andric         m_images.ResolveFileAddress(pointer_vm_addr, pointer_addr);
1937*0b57cec5SDimitry Andric       } else {
1938*0b57cec5SDimitry Andric         // We have at least one section loaded. This can be because we have
1939*0b57cec5SDimitry Andric         // manually loaded some sections with "target modules load ..." or
1940*0b57cec5SDimitry Andric         // because we have have a live process that has sections loaded through
1941*0b57cec5SDimitry Andric         // the dynamic loader
1942*0b57cec5SDimitry Andric         section_load_list.ResolveLoadAddress(pointer_vm_addr, pointer_addr);
1943*0b57cec5SDimitry Andric       }
1944*0b57cec5SDimitry Andric       // We weren't able to resolve the pointer value, so just return an
1945*0b57cec5SDimitry Andric       // address with no section
1946*0b57cec5SDimitry Andric       if (!pointer_addr.IsValid())
1947*0b57cec5SDimitry Andric         pointer_addr.SetOffset(pointer_vm_addr);
1948*0b57cec5SDimitry Andric       return true;
1949*0b57cec5SDimitry Andric     }
1950*0b57cec5SDimitry Andric   }
1951*0b57cec5SDimitry Andric   return false;
1952*0b57cec5SDimitry Andric }
1953*0b57cec5SDimitry Andric 
1954*0b57cec5SDimitry Andric ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
1955*0b57cec5SDimitry Andric                                    Status *error_ptr) {
1956*0b57cec5SDimitry Andric   ModuleSP module_sp;
1957*0b57cec5SDimitry Andric 
1958*0b57cec5SDimitry Andric   Status error;
1959*0b57cec5SDimitry Andric 
1960*0b57cec5SDimitry Andric   // First see if we already have this module in our module list.  If we do,
1961*0b57cec5SDimitry Andric   // then we're done, we don't need to consult the shared modules list.  But
1962*0b57cec5SDimitry Andric   // only do this if we are passed a UUID.
1963*0b57cec5SDimitry Andric 
1964*0b57cec5SDimitry Andric   if (module_spec.GetUUID().IsValid())
1965*0b57cec5SDimitry Andric     module_sp = m_images.FindFirstModule(module_spec);
1966*0b57cec5SDimitry Andric 
1967*0b57cec5SDimitry Andric   if (!module_sp) {
1968*0b57cec5SDimitry Andric     ModuleSP old_module_sp; // This will get filled in if we have a new version
1969*0b57cec5SDimitry Andric                             // of the library
1970*0b57cec5SDimitry Andric     bool did_create_module = false;
1971*0b57cec5SDimitry Andric     FileSpecList search_paths = GetExecutableSearchPaths();
1972*0b57cec5SDimitry Andric     // If there are image search path entries, try to use them first to acquire
1973*0b57cec5SDimitry Andric     // a suitable image.
1974*0b57cec5SDimitry Andric     if (m_image_search_paths.GetSize()) {
1975*0b57cec5SDimitry Andric       ModuleSpec transformed_spec(module_spec);
1976*0b57cec5SDimitry Andric       if (m_image_search_paths.RemapPath(
1977*0b57cec5SDimitry Andric               module_spec.GetFileSpec().GetDirectory(),
1978*0b57cec5SDimitry Andric               transformed_spec.GetFileSpec().GetDirectory())) {
1979*0b57cec5SDimitry Andric         transformed_spec.GetFileSpec().GetFilename() =
1980*0b57cec5SDimitry Andric             module_spec.GetFileSpec().GetFilename();
1981*0b57cec5SDimitry Andric         error = ModuleList::GetSharedModule(transformed_spec, module_sp,
19829dba64beSDimitry Andric                                             &search_paths, &old_module_sp,
19839dba64beSDimitry Andric                                             &did_create_module);
1984*0b57cec5SDimitry Andric       }
1985*0b57cec5SDimitry Andric     }
1986*0b57cec5SDimitry Andric 
1987*0b57cec5SDimitry Andric     if (!module_sp) {
1988*0b57cec5SDimitry Andric       // If we have a UUID, we can check our global shared module list in case
1989*0b57cec5SDimitry Andric       // we already have it. If we don't have a valid UUID, then we can't since
1990*0b57cec5SDimitry Andric       // the path in "module_spec" will be a platform path, and we will need to
1991*0b57cec5SDimitry Andric       // let the platform find that file. For example, we could be asking for
1992*0b57cec5SDimitry Andric       // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1993*0b57cec5SDimitry Andric       // the local copy of "/usr/lib/dyld" since our platform could be a remote
1994*0b57cec5SDimitry Andric       // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1995*0b57cec5SDimitry Andric       // cache.
1996*0b57cec5SDimitry Andric       if (module_spec.GetUUID().IsValid()) {
1997*0b57cec5SDimitry Andric         // We have a UUID, it is OK to check the global module list...
19989dba64beSDimitry Andric         error =
19999dba64beSDimitry Andric             ModuleList::GetSharedModule(module_spec, module_sp, &search_paths,
2000*0b57cec5SDimitry Andric                                         &old_module_sp, &did_create_module);
2001*0b57cec5SDimitry Andric       }
2002*0b57cec5SDimitry Andric 
2003*0b57cec5SDimitry Andric       if (!module_sp) {
2004*0b57cec5SDimitry Andric         // The platform is responsible for finding and caching an appropriate
2005*0b57cec5SDimitry Andric         // module in the shared module cache.
2006*0b57cec5SDimitry Andric         if (m_platform_sp) {
2007*0b57cec5SDimitry Andric           error = m_platform_sp->GetSharedModule(
20089dba64beSDimitry Andric               module_spec, m_process_sp.get(), module_sp, &search_paths,
20099dba64beSDimitry Andric               &old_module_sp, &did_create_module);
2010*0b57cec5SDimitry Andric         } else {
2011*0b57cec5SDimitry Andric           error.SetErrorString("no platform is currently set");
2012*0b57cec5SDimitry Andric         }
2013*0b57cec5SDimitry Andric       }
2014*0b57cec5SDimitry Andric     }
2015*0b57cec5SDimitry Andric 
2016*0b57cec5SDimitry Andric     // We found a module that wasn't in our target list.  Let's make sure that
2017*0b57cec5SDimitry Andric     // there wasn't an equivalent module in the list already, and if there was,
2018*0b57cec5SDimitry Andric     // let's remove it.
2019*0b57cec5SDimitry Andric     if (module_sp) {
2020*0b57cec5SDimitry Andric       ObjectFile *objfile = module_sp->GetObjectFile();
2021*0b57cec5SDimitry Andric       if (objfile) {
2022*0b57cec5SDimitry Andric         switch (objfile->GetType()) {
2023*0b57cec5SDimitry Andric         case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of
2024*0b57cec5SDimitry Andric                                         /// a program's execution state
2025*0b57cec5SDimitry Andric         case ObjectFile::eTypeExecutable:    /// A normal executable
2026*0b57cec5SDimitry Andric         case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker
2027*0b57cec5SDimitry Andric                                              /// executable
2028*0b57cec5SDimitry Andric         case ObjectFile::eTypeObjectFile:    /// An intermediate object file
2029*0b57cec5SDimitry Andric         case ObjectFile::eTypeSharedLibrary: /// A shared library that can be
2030*0b57cec5SDimitry Andric                                              /// used during execution
2031*0b57cec5SDimitry Andric           break;
2032*0b57cec5SDimitry Andric         case ObjectFile::eTypeDebugInfo: /// An object file that contains only
2033*0b57cec5SDimitry Andric                                          /// debug information
2034*0b57cec5SDimitry Andric           if (error_ptr)
2035*0b57cec5SDimitry Andric             error_ptr->SetErrorString("debug info files aren't valid target "
2036*0b57cec5SDimitry Andric                                       "modules, please specify an executable");
2037*0b57cec5SDimitry Andric           return ModuleSP();
2038*0b57cec5SDimitry Andric         case ObjectFile::eTypeStubLibrary: /// A library that can be linked
2039*0b57cec5SDimitry Andric                                            /// against but not used for
2040*0b57cec5SDimitry Andric                                            /// execution
2041*0b57cec5SDimitry Andric           if (error_ptr)
2042*0b57cec5SDimitry Andric             error_ptr->SetErrorString("stub libraries aren't valid target "
2043*0b57cec5SDimitry Andric                                       "modules, please specify an executable");
2044*0b57cec5SDimitry Andric           return ModuleSP();
2045*0b57cec5SDimitry Andric         default:
2046*0b57cec5SDimitry Andric           if (error_ptr)
2047*0b57cec5SDimitry Andric             error_ptr->SetErrorString(
2048*0b57cec5SDimitry Andric                 "unsupported file type, please specify an executable");
2049*0b57cec5SDimitry Andric           return ModuleSP();
2050*0b57cec5SDimitry Andric         }
2051*0b57cec5SDimitry Andric         // GetSharedModule is not guaranteed to find the old shared module, for
2052*0b57cec5SDimitry Andric         // instance in the common case where you pass in the UUID, it is only
2053*0b57cec5SDimitry Andric         // going to find the one module matching the UUID.  In fact, it has no
2054*0b57cec5SDimitry Andric         // good way to know what the "old module" relevant to this target is,
2055*0b57cec5SDimitry Andric         // since there might be many copies of a module with this file spec in
2056*0b57cec5SDimitry Andric         // various running debug sessions, but only one of them will belong to
2057*0b57cec5SDimitry Andric         // this target. So let's remove the UUID from the module list, and look
2058*0b57cec5SDimitry Andric         // in the target's module list. Only do this if there is SOMETHING else
2059*0b57cec5SDimitry Andric         // in the module spec...
2060*0b57cec5SDimitry Andric         if (!old_module_sp) {
2061*0b57cec5SDimitry Andric           if (module_spec.GetUUID().IsValid() &&
2062*0b57cec5SDimitry Andric               !module_spec.GetFileSpec().GetFilename().IsEmpty() &&
2063*0b57cec5SDimitry Andric               !module_spec.GetFileSpec().GetDirectory().IsEmpty()) {
2064*0b57cec5SDimitry Andric             ModuleSpec module_spec_copy(module_spec.GetFileSpec());
2065*0b57cec5SDimitry Andric             module_spec_copy.GetUUID().Clear();
2066*0b57cec5SDimitry Andric 
2067*0b57cec5SDimitry Andric             ModuleList found_modules;
2068*0b57cec5SDimitry Andric             m_images.FindModules(module_spec_copy, found_modules);
20699dba64beSDimitry Andric             if (found_modules.GetSize() == 1)
2070*0b57cec5SDimitry Andric               old_module_sp = found_modules.GetModuleAtIndex(0);
2071*0b57cec5SDimitry Andric           }
2072*0b57cec5SDimitry Andric         }
2073*0b57cec5SDimitry Andric 
2074*0b57cec5SDimitry Andric         // Preload symbols outside of any lock, so hopefully we can do this for
2075*0b57cec5SDimitry Andric         // each library in parallel.
2076*0b57cec5SDimitry Andric         if (GetPreloadSymbols())
2077*0b57cec5SDimitry Andric           module_sp->PreloadSymbols();
2078*0b57cec5SDimitry Andric 
20799dba64beSDimitry Andric         if (old_module_sp && m_images.GetIndexForModule(old_module_sp.get()) !=
2080*0b57cec5SDimitry Andric                                  LLDB_INVALID_INDEX32) {
2081*0b57cec5SDimitry Andric           m_images.ReplaceModule(old_module_sp, module_sp);
2082*0b57cec5SDimitry Andric           Module *old_module_ptr = old_module_sp.get();
2083*0b57cec5SDimitry Andric           old_module_sp.reset();
2084*0b57cec5SDimitry Andric           ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
2085*0b57cec5SDimitry Andric         } else {
2086*0b57cec5SDimitry Andric           m_images.Append(module_sp, notify);
2087*0b57cec5SDimitry Andric         }
2088*0b57cec5SDimitry Andric       } else
2089*0b57cec5SDimitry Andric         module_sp.reset();
2090*0b57cec5SDimitry Andric     }
2091*0b57cec5SDimitry Andric   }
2092*0b57cec5SDimitry Andric   if (error_ptr)
2093*0b57cec5SDimitry Andric     *error_ptr = error;
2094*0b57cec5SDimitry Andric   return module_sp;
2095*0b57cec5SDimitry Andric }
2096*0b57cec5SDimitry Andric 
2097*0b57cec5SDimitry Andric TargetSP Target::CalculateTarget() { return shared_from_this(); }
2098*0b57cec5SDimitry Andric 
2099*0b57cec5SDimitry Andric ProcessSP Target::CalculateProcess() { return m_process_sp; }
2100*0b57cec5SDimitry Andric 
2101*0b57cec5SDimitry Andric ThreadSP Target::CalculateThread() { return ThreadSP(); }
2102*0b57cec5SDimitry Andric 
2103*0b57cec5SDimitry Andric StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); }
2104*0b57cec5SDimitry Andric 
2105*0b57cec5SDimitry Andric void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) {
2106*0b57cec5SDimitry Andric   exe_ctx.Clear();
2107*0b57cec5SDimitry Andric   exe_ctx.SetTargetPtr(this);
2108*0b57cec5SDimitry Andric }
2109*0b57cec5SDimitry Andric 
2110*0b57cec5SDimitry Andric PathMappingList &Target::GetImageSearchPathList() {
2111*0b57cec5SDimitry Andric   return m_image_search_paths;
2112*0b57cec5SDimitry Andric }
2113*0b57cec5SDimitry Andric 
2114*0b57cec5SDimitry Andric void Target::ImageSearchPathsChanged(const PathMappingList &path_list,
2115*0b57cec5SDimitry Andric                                      void *baton) {
2116*0b57cec5SDimitry Andric   Target *target = (Target *)baton;
2117*0b57cec5SDimitry Andric   ModuleSP exe_module_sp(target->GetExecutableModule());
2118*0b57cec5SDimitry Andric   if (exe_module_sp)
2119*0b57cec5SDimitry Andric     target->SetExecutableModule(exe_module_sp, eLoadDependentsYes);
2120*0b57cec5SDimitry Andric }
2121*0b57cec5SDimitry Andric 
21229dba64beSDimitry Andric llvm::Expected<TypeSystem &>
21239dba64beSDimitry Andric Target::GetScratchTypeSystemForLanguage(lldb::LanguageType language,
2124*0b57cec5SDimitry Andric                                         bool create_on_demand) {
2125*0b57cec5SDimitry Andric   if (!m_valid)
21269dba64beSDimitry Andric     return llvm::make_error<llvm::StringError>("Invalid Target",
21279dba64beSDimitry Andric                                                llvm::inconvertibleErrorCode());
2128*0b57cec5SDimitry Andric 
2129*0b57cec5SDimitry Andric   if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all
2130*0b57cec5SDimitry Andric                                              // assembly code
2131*0b57cec5SDimitry Andric       || language == eLanguageTypeUnknown) {
21329dba64beSDimitry Andric     LanguageSet languages_for_expressions =
21339dba64beSDimitry Andric         Language::GetLanguagesSupportingTypeSystemsForExpressions();
2134*0b57cec5SDimitry Andric 
21359dba64beSDimitry Andric     if (languages_for_expressions[eLanguageTypeC]) {
2136*0b57cec5SDimitry Andric       language = eLanguageTypeC; // LLDB's default.  Override by setting the
2137*0b57cec5SDimitry Andric                                  // target language.
2138*0b57cec5SDimitry Andric     } else {
21399dba64beSDimitry Andric       if (languages_for_expressions.Empty())
21409dba64beSDimitry Andric         return llvm::make_error<llvm::StringError>(
21419dba64beSDimitry Andric             "No expression support for any languages",
21429dba64beSDimitry Andric             llvm::inconvertibleErrorCode());
21439dba64beSDimitry Andric       language = (LanguageType)languages_for_expressions.bitvector.find_first();
2144*0b57cec5SDimitry Andric     }
2145*0b57cec5SDimitry Andric   }
2146*0b57cec5SDimitry Andric 
2147*0b57cec5SDimitry Andric   return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this,
2148*0b57cec5SDimitry Andric                                                             create_on_demand);
2149*0b57cec5SDimitry Andric }
2150*0b57cec5SDimitry Andric 
21519dba64beSDimitry Andric std::vector<TypeSystem *> Target::GetScratchTypeSystems(bool create_on_demand) {
21529dba64beSDimitry Andric   if (!m_valid)
21539dba64beSDimitry Andric     return {};
21549dba64beSDimitry Andric 
21559dba64beSDimitry Andric   std::vector<TypeSystem *> scratch_type_systems;
21569dba64beSDimitry Andric 
21579dba64beSDimitry Andric   LanguageSet languages_for_expressions =
21589dba64beSDimitry Andric       Language::GetLanguagesSupportingTypeSystemsForExpressions();
21599dba64beSDimitry Andric 
21609dba64beSDimitry Andric   for (auto bit : languages_for_expressions.bitvector.set_bits()) {
21619dba64beSDimitry Andric     auto language = (LanguageType)bit;
21629dba64beSDimitry Andric     auto type_system_or_err =
21639dba64beSDimitry Andric         GetScratchTypeSystemForLanguage(language, create_on_demand);
21649dba64beSDimitry Andric     if (!type_system_or_err)
21659dba64beSDimitry Andric       LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
21669dba64beSDimitry Andric                      type_system_or_err.takeError(),
21679dba64beSDimitry Andric                      "Language '{}' has expression support but no scratch type "
21689dba64beSDimitry Andric                      "system available",
21699dba64beSDimitry Andric                      Language::GetNameForLanguageType(language));
21709dba64beSDimitry Andric     else
21719dba64beSDimitry Andric       scratch_type_systems.emplace_back(&type_system_or_err.get());
21729dba64beSDimitry Andric   }
21739dba64beSDimitry Andric 
21749dba64beSDimitry Andric   return scratch_type_systems;
21759dba64beSDimitry Andric }
21769dba64beSDimitry Andric 
2177*0b57cec5SDimitry Andric PersistentExpressionState *
2178*0b57cec5SDimitry Andric Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) {
21799dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language, true);
2180*0b57cec5SDimitry Andric 
21819dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
21829dba64beSDimitry Andric     LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
21839dba64beSDimitry Andric                    std::move(err),
21849dba64beSDimitry Andric                    "Unable to get persistent expression state for language {}",
21859dba64beSDimitry Andric                    Language::GetNameForLanguageType(language));
2186*0b57cec5SDimitry Andric     return nullptr;
2187*0b57cec5SDimitry Andric   }
21889dba64beSDimitry Andric 
21899dba64beSDimitry Andric   return type_system_or_err->GetPersistentExpressionState();
2190*0b57cec5SDimitry Andric }
2191*0b57cec5SDimitry Andric 
2192*0b57cec5SDimitry Andric UserExpression *Target::GetUserExpressionForLanguage(
2193*0b57cec5SDimitry Andric     llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
2194*0b57cec5SDimitry Andric     Expression::ResultType desired_type,
21959dba64beSDimitry Andric     const EvaluateExpressionOptions &options, ValueObject *ctx_obj,
21969dba64beSDimitry Andric     Status &error) {
21979dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
21989dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
2199*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2200*0b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
2201*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22029dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
2203*0b57cec5SDimitry Andric     return nullptr;
2204*0b57cec5SDimitry Andric   }
2205*0b57cec5SDimitry Andric 
22069dba64beSDimitry Andric   auto *user_expr = type_system_or_err->GetUserExpression(
22079dba64beSDimitry Andric       expr, prefix, language, desired_type, options, ctx_obj);
2208*0b57cec5SDimitry Andric   if (!user_expr)
2209*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2210*0b57cec5SDimitry Andric         "Could not create an expression for language %s",
2211*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
2212*0b57cec5SDimitry Andric 
2213*0b57cec5SDimitry Andric   return user_expr;
2214*0b57cec5SDimitry Andric }
2215*0b57cec5SDimitry Andric 
2216*0b57cec5SDimitry Andric FunctionCaller *Target::GetFunctionCallerForLanguage(
2217*0b57cec5SDimitry Andric     lldb::LanguageType language, const CompilerType &return_type,
2218*0b57cec5SDimitry Andric     const Address &function_address, const ValueList &arg_value_list,
2219*0b57cec5SDimitry Andric     const char *name, Status &error) {
22209dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
22219dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
2222*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2223*0b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
2224*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22259dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
22269dba64beSDimitry Andric     return nullptr;
2227*0b57cec5SDimitry Andric   }
2228*0b57cec5SDimitry Andric 
22299dba64beSDimitry Andric   auto *persistent_fn = type_system_or_err->GetFunctionCaller(
22309dba64beSDimitry Andric       return_type, function_address, arg_value_list, name);
2231*0b57cec5SDimitry Andric   if (!persistent_fn)
2232*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2233*0b57cec5SDimitry Andric         "Could not create an expression for language %s",
2234*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
2235*0b57cec5SDimitry Andric 
2236*0b57cec5SDimitry Andric   return persistent_fn;
2237*0b57cec5SDimitry Andric }
2238*0b57cec5SDimitry Andric 
2239*0b57cec5SDimitry Andric UtilityFunction *
2240*0b57cec5SDimitry Andric Target::GetUtilityFunctionForLanguage(const char *text,
2241*0b57cec5SDimitry Andric                                       lldb::LanguageType language,
2242*0b57cec5SDimitry Andric                                       const char *name, Status &error) {
22439dba64beSDimitry Andric   auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
2244*0b57cec5SDimitry Andric 
22459dba64beSDimitry Andric   if (auto err = type_system_or_err.takeError()) {
2246*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2247*0b57cec5SDimitry Andric         "Could not find type system for language %s: %s",
2248*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language),
22499dba64beSDimitry Andric         llvm::toString(std::move(err)).c_str());
22509dba64beSDimitry Andric     return nullptr;
2251*0b57cec5SDimitry Andric   }
2252*0b57cec5SDimitry Andric 
22539dba64beSDimitry Andric   auto *utility_fn = type_system_or_err->GetUtilityFunction(text, name);
2254*0b57cec5SDimitry Andric   if (!utility_fn)
2255*0b57cec5SDimitry Andric     error.SetErrorStringWithFormat(
2256*0b57cec5SDimitry Andric         "Could not create an expression for language %s",
2257*0b57cec5SDimitry Andric         Language::GetNameForLanguageType(language));
2258*0b57cec5SDimitry Andric 
2259*0b57cec5SDimitry Andric   return utility_fn;
2260*0b57cec5SDimitry Andric }
2261*0b57cec5SDimitry Andric 
2262*0b57cec5SDimitry Andric void Target::SettingsInitialize() { Process::SettingsInitialize(); }
2263*0b57cec5SDimitry Andric 
2264*0b57cec5SDimitry Andric void Target::SettingsTerminate() { Process::SettingsTerminate(); }
2265*0b57cec5SDimitry Andric 
2266*0b57cec5SDimitry Andric FileSpecList Target::GetDefaultExecutableSearchPaths() {
2267*0b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2268*0b57cec5SDimitry Andric   if (properties_sp)
2269*0b57cec5SDimitry Andric     return properties_sp->GetExecutableSearchPaths();
2270*0b57cec5SDimitry Andric   return FileSpecList();
2271*0b57cec5SDimitry Andric }
2272*0b57cec5SDimitry Andric 
2273*0b57cec5SDimitry Andric FileSpecList Target::GetDefaultDebugFileSearchPaths() {
2274*0b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2275*0b57cec5SDimitry Andric   if (properties_sp)
2276*0b57cec5SDimitry Andric     return properties_sp->GetDebugFileSearchPaths();
2277*0b57cec5SDimitry Andric   return FileSpecList();
2278*0b57cec5SDimitry Andric }
2279*0b57cec5SDimitry Andric 
2280*0b57cec5SDimitry Andric ArchSpec Target::GetDefaultArchitecture() {
2281*0b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2282*0b57cec5SDimitry Andric   if (properties_sp)
2283*0b57cec5SDimitry Andric     return properties_sp->GetDefaultArchitecture();
2284*0b57cec5SDimitry Andric   return ArchSpec();
2285*0b57cec5SDimitry Andric }
2286*0b57cec5SDimitry Andric 
2287*0b57cec5SDimitry Andric void Target::SetDefaultArchitecture(const ArchSpec &arch) {
2288*0b57cec5SDimitry Andric   TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2289*0b57cec5SDimitry Andric   if (properties_sp) {
22909dba64beSDimitry Andric     LLDB_LOG(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET),
22919dba64beSDimitry Andric              "Target::SetDefaultArchitecture setting target's "
22929dba64beSDimitry Andric              "default architecture to  {0} ({1})",
22939dba64beSDimitry Andric              arch.GetArchitectureName(), arch.GetTriple().getTriple());
2294*0b57cec5SDimitry Andric     return properties_sp->SetDefaultArchitecture(arch);
2295*0b57cec5SDimitry Andric   }
2296*0b57cec5SDimitry Andric }
2297*0b57cec5SDimitry Andric 
2298*0b57cec5SDimitry Andric Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
2299*0b57cec5SDimitry Andric                                       const SymbolContext *sc_ptr) {
2300*0b57cec5SDimitry Andric   // The target can either exist in the "process" of ExecutionContext, or in
2301*0b57cec5SDimitry Andric   // the "target_sp" member of SymbolContext. This accessor helper function
2302*0b57cec5SDimitry Andric   // will get the target from one of these locations.
2303*0b57cec5SDimitry Andric 
2304*0b57cec5SDimitry Andric   Target *target = nullptr;
2305*0b57cec5SDimitry Andric   if (sc_ptr != nullptr)
2306*0b57cec5SDimitry Andric     target = sc_ptr->target_sp.get();
2307*0b57cec5SDimitry Andric   if (target == nullptr && exe_ctx_ptr)
2308*0b57cec5SDimitry Andric     target = exe_ctx_ptr->GetTargetPtr();
2309*0b57cec5SDimitry Andric   return target;
2310*0b57cec5SDimitry Andric }
2311*0b57cec5SDimitry Andric 
2312*0b57cec5SDimitry Andric ExpressionResults Target::EvaluateExpression(
2313*0b57cec5SDimitry Andric     llvm::StringRef expr, ExecutionContextScope *exe_scope,
2314*0b57cec5SDimitry Andric     lldb::ValueObjectSP &result_valobj_sp,
2315*0b57cec5SDimitry Andric     const EvaluateExpressionOptions &options, std::string *fixed_expression,
2316*0b57cec5SDimitry Andric     ValueObject *ctx_obj) {
2317*0b57cec5SDimitry Andric   result_valobj_sp.reset();
2318*0b57cec5SDimitry Andric 
2319*0b57cec5SDimitry Andric   ExpressionResults execution_results = eExpressionSetupError;
2320*0b57cec5SDimitry Andric 
2321*0b57cec5SDimitry Andric   if (expr.empty())
2322*0b57cec5SDimitry Andric     return execution_results;
2323*0b57cec5SDimitry Andric 
2324*0b57cec5SDimitry Andric   // We shouldn't run stop hooks in expressions.
2325*0b57cec5SDimitry Andric   bool old_suppress_value = m_suppress_stop_hooks;
2326*0b57cec5SDimitry Andric   m_suppress_stop_hooks = true;
2327*0b57cec5SDimitry Andric   auto on_exit = llvm::make_scope_exit([this, old_suppress_value]() {
23289dba64beSDimitry Andric     m_suppress_stop_hooks = old_suppress_value;
23299dba64beSDimitry Andric   });
2330*0b57cec5SDimitry Andric 
2331*0b57cec5SDimitry Andric   ExecutionContext exe_ctx;
2332*0b57cec5SDimitry Andric 
2333*0b57cec5SDimitry Andric   if (exe_scope) {
2334*0b57cec5SDimitry Andric     exe_scope->CalculateExecutionContext(exe_ctx);
2335*0b57cec5SDimitry Andric   } else if (m_process_sp) {
2336*0b57cec5SDimitry Andric     m_process_sp->CalculateExecutionContext(exe_ctx);
2337*0b57cec5SDimitry Andric   } else {
2338*0b57cec5SDimitry Andric     CalculateExecutionContext(exe_ctx);
2339*0b57cec5SDimitry Andric   }
2340*0b57cec5SDimitry Andric 
2341*0b57cec5SDimitry Andric   // Make sure we aren't just trying to see the value of a persistent variable
2342*0b57cec5SDimitry Andric   // (something like "$0")
2343*0b57cec5SDimitry Andric   // Only check for persistent variables the expression starts with a '$'
23449dba64beSDimitry Andric   lldb::ExpressionVariableSP persistent_var_sp;
23459dba64beSDimitry Andric   if (expr[0] == '$') {
23469dba64beSDimitry Andric     auto type_system_or_err =
23479dba64beSDimitry Andric             GetScratchTypeSystemForLanguage(eLanguageTypeC);
23489dba64beSDimitry Andric     if (auto err = type_system_or_err.takeError()) {
23499dba64beSDimitry Andric       LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
23509dba64beSDimitry Andric                      std::move(err), "Unable to get scratch type system");
23519dba64beSDimitry Andric     } else {
23529dba64beSDimitry Andric       persistent_var_sp =
23539dba64beSDimitry Andric           type_system_or_err->GetPersistentExpressionState()->GetVariable(expr);
23549dba64beSDimitry Andric     }
23559dba64beSDimitry Andric   }
2356*0b57cec5SDimitry Andric   if (persistent_var_sp) {
2357*0b57cec5SDimitry Andric     result_valobj_sp = persistent_var_sp->GetValueObject();
2358*0b57cec5SDimitry Andric     execution_results = eExpressionCompleted;
2359*0b57cec5SDimitry Andric   } else {
2360*0b57cec5SDimitry Andric     llvm::StringRef prefix = GetExpressionPrefixContents();
2361*0b57cec5SDimitry Andric     Status error;
23625ffd83dbSDimitry Andric     execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix,
23635ffd83dbSDimitry Andric                                                  result_valobj_sp, error,
23645ffd83dbSDimitry Andric                                                  fixed_expression, ctx_obj);
2365*0b57cec5SDimitry Andric   }
2366*0b57cec5SDimitry Andric 
2367*0b57cec5SDimitry Andric   return execution_results;
2368*0b57cec5SDimitry Andric }
2369*0b57cec5SDimitry Andric 
23709dba64beSDimitry Andric lldb::ExpressionVariableSP Target::GetPersistentVariable(ConstString name) {
2371*0b57cec5SDimitry Andric   lldb::ExpressionVariableSP variable_sp;
2372*0b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
2373*0b57cec5SDimitry Andric       [name, &variable_sp](TypeSystem *type_system) -> bool {
2374*0b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
2375*0b57cec5SDimitry Andric                 type_system->GetPersistentExpressionState()) {
2376*0b57cec5SDimitry Andric           variable_sp = persistent_state->GetVariable(name);
2377*0b57cec5SDimitry Andric 
2378*0b57cec5SDimitry Andric           if (variable_sp)
2379*0b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
2380*0b57cec5SDimitry Andric         }
2381*0b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
2382*0b57cec5SDimitry Andric       });
2383*0b57cec5SDimitry Andric   return variable_sp;
2384*0b57cec5SDimitry Andric }
2385*0b57cec5SDimitry Andric 
2386*0b57cec5SDimitry Andric lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
2387*0b57cec5SDimitry Andric   lldb::addr_t address = LLDB_INVALID_ADDRESS;
2388*0b57cec5SDimitry Andric 
2389*0b57cec5SDimitry Andric   m_scratch_type_system_map.ForEach(
2390*0b57cec5SDimitry Andric       [name, &address](TypeSystem *type_system) -> bool {
2391*0b57cec5SDimitry Andric         if (PersistentExpressionState *persistent_state =
2392*0b57cec5SDimitry Andric                 type_system->GetPersistentExpressionState()) {
2393*0b57cec5SDimitry Andric           address = persistent_state->LookupSymbol(name);
2394*0b57cec5SDimitry Andric           if (address != LLDB_INVALID_ADDRESS)
2395*0b57cec5SDimitry Andric             return false; // Stop iterating the ForEach
2396*0b57cec5SDimitry Andric         }
2397*0b57cec5SDimitry Andric         return true; // Keep iterating the ForEach
2398*0b57cec5SDimitry Andric       });
2399*0b57cec5SDimitry Andric   return address;
2400*0b57cec5SDimitry Andric }
2401*0b57cec5SDimitry Andric 
24029dba64beSDimitry Andric llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
24039dba64beSDimitry Andric   Module *exe_module = GetExecutableModulePointer();
24049dba64beSDimitry Andric   llvm::Error error = llvm::Error::success();
24059dba64beSDimitry Andric   assert(!error); // Check the success value when assertions are enabled.
24069dba64beSDimitry Andric 
24079dba64beSDimitry Andric   if (!exe_module || !exe_module->GetObjectFile()) {
24089dba64beSDimitry Andric     error = llvm::make_error<llvm::StringError>("No primary executable found",
24099dba64beSDimitry Andric                                                 llvm::inconvertibleErrorCode());
24109dba64beSDimitry Andric   } else {
24119dba64beSDimitry Andric     Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
24129dba64beSDimitry Andric     if (entry_addr.IsValid())
24139dba64beSDimitry Andric       return entry_addr;
24149dba64beSDimitry Andric 
24159dba64beSDimitry Andric     error = llvm::make_error<llvm::StringError>(
24169dba64beSDimitry Andric         "Could not find entry point address for executable module \"" +
24179dba64beSDimitry Andric             exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"",
24189dba64beSDimitry Andric         llvm::inconvertibleErrorCode());
24199dba64beSDimitry Andric   }
24209dba64beSDimitry Andric 
24219dba64beSDimitry Andric   const ModuleList &modules = GetImages();
24229dba64beSDimitry Andric   const size_t num_images = modules.GetSize();
24239dba64beSDimitry Andric   for (size_t idx = 0; idx < num_images; ++idx) {
24249dba64beSDimitry Andric     ModuleSP module_sp(modules.GetModuleAtIndex(idx));
24259dba64beSDimitry Andric     if (!module_sp || !module_sp->GetObjectFile())
24269dba64beSDimitry Andric       continue;
24279dba64beSDimitry Andric 
24289dba64beSDimitry Andric     Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
24299dba64beSDimitry Andric     if (entry_addr.IsValid()) {
24309dba64beSDimitry Andric       // Discard the error.
24319dba64beSDimitry Andric       llvm::consumeError(std::move(error));
24329dba64beSDimitry Andric       return entry_addr;
24339dba64beSDimitry Andric     }
24349dba64beSDimitry Andric   }
24359dba64beSDimitry Andric 
24369dba64beSDimitry Andric   return std::move(error);
24379dba64beSDimitry Andric }
24389dba64beSDimitry Andric 
2439*0b57cec5SDimitry Andric lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
2440*0b57cec5SDimitry Andric                                             AddressClass addr_class) const {
2441*0b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24429dba64beSDimitry Andric   return arch_plugin
24439dba64beSDimitry Andric              ? arch_plugin->GetCallableLoadAddress(load_addr, addr_class)
24449dba64beSDimitry Andric              : load_addr;
2445*0b57cec5SDimitry Andric }
2446*0b57cec5SDimitry Andric 
2447*0b57cec5SDimitry Andric lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr,
2448*0b57cec5SDimitry Andric                                           AddressClass addr_class) const {
2449*0b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24509dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetOpcodeLoadAddress(load_addr, addr_class)
24519dba64beSDimitry Andric                      : load_addr;
2452*0b57cec5SDimitry Andric }
2453*0b57cec5SDimitry Andric 
2454*0b57cec5SDimitry Andric lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
2455*0b57cec5SDimitry Andric   auto arch_plugin = GetArchitecturePlugin();
24569dba64beSDimitry Andric   return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
2457*0b57cec5SDimitry Andric }
2458*0b57cec5SDimitry Andric 
2459*0b57cec5SDimitry Andric SourceManager &Target::GetSourceManager() {
2460*0b57cec5SDimitry Andric   if (!m_source_manager_up)
24615ffd83dbSDimitry Andric     m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
2462*0b57cec5SDimitry Andric   return *m_source_manager_up;
2463*0b57cec5SDimitry Andric }
2464*0b57cec5SDimitry Andric 
2465*0b57cec5SDimitry Andric ClangModulesDeclVendor *Target::GetClangModulesDeclVendor() {
2466*0b57cec5SDimitry Andric   static std::mutex s_clang_modules_decl_vendor_mutex; // If this is contended
2467*0b57cec5SDimitry Andric                                                        // we can make it
2468*0b57cec5SDimitry Andric                                                        // per-target
2469*0b57cec5SDimitry Andric 
2470*0b57cec5SDimitry Andric   {
2471*0b57cec5SDimitry Andric     std::lock_guard<std::mutex> guard(s_clang_modules_decl_vendor_mutex);
2472*0b57cec5SDimitry Andric 
2473*0b57cec5SDimitry Andric     if (!m_clang_modules_decl_vendor_up) {
2474*0b57cec5SDimitry Andric       m_clang_modules_decl_vendor_up.reset(
2475*0b57cec5SDimitry Andric           ClangModulesDeclVendor::Create(*this));
2476*0b57cec5SDimitry Andric     }
2477*0b57cec5SDimitry Andric   }
2478*0b57cec5SDimitry Andric 
2479*0b57cec5SDimitry Andric   return m_clang_modules_decl_vendor_up.get();
2480*0b57cec5SDimitry Andric }
2481*0b57cec5SDimitry Andric 
2482*0b57cec5SDimitry Andric Target::StopHookSP Target::CreateStopHook() {
2483*0b57cec5SDimitry Andric   lldb::user_id_t new_uid = ++m_stop_hook_next_id;
2484*0b57cec5SDimitry Andric   Target::StopHookSP stop_hook_sp(new StopHook(shared_from_this(), new_uid));
2485*0b57cec5SDimitry Andric   m_stop_hooks[new_uid] = stop_hook_sp;
2486*0b57cec5SDimitry Andric   return stop_hook_sp;
2487*0b57cec5SDimitry Andric }
2488*0b57cec5SDimitry Andric 
2489*0b57cec5SDimitry Andric bool Target::RemoveStopHookByID(lldb::user_id_t user_id) {
2490*0b57cec5SDimitry Andric   size_t num_removed = m_stop_hooks.erase(user_id);
2491*0b57cec5SDimitry Andric   return (num_removed != 0);
2492*0b57cec5SDimitry Andric }
2493*0b57cec5SDimitry Andric 
2494*0b57cec5SDimitry Andric void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); }
2495*0b57cec5SDimitry Andric 
2496*0b57cec5SDimitry Andric Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) {
2497*0b57cec5SDimitry Andric   StopHookSP found_hook;
2498*0b57cec5SDimitry Andric 
2499*0b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
2500*0b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
2501*0b57cec5SDimitry Andric   if (specified_hook_iter != m_stop_hooks.end())
2502*0b57cec5SDimitry Andric     found_hook = (*specified_hook_iter).second;
2503*0b57cec5SDimitry Andric   return found_hook;
2504*0b57cec5SDimitry Andric }
2505*0b57cec5SDimitry Andric 
2506*0b57cec5SDimitry Andric bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id,
2507*0b57cec5SDimitry Andric                                         bool active_state) {
2508*0b57cec5SDimitry Andric   StopHookCollection::iterator specified_hook_iter;
2509*0b57cec5SDimitry Andric   specified_hook_iter = m_stop_hooks.find(user_id);
2510*0b57cec5SDimitry Andric   if (specified_hook_iter == m_stop_hooks.end())
2511*0b57cec5SDimitry Andric     return false;
2512*0b57cec5SDimitry Andric 
2513*0b57cec5SDimitry Andric   (*specified_hook_iter).second->SetIsActive(active_state);
2514*0b57cec5SDimitry Andric   return true;
2515*0b57cec5SDimitry Andric }
2516*0b57cec5SDimitry Andric 
2517*0b57cec5SDimitry Andric void Target::SetAllStopHooksActiveState(bool active_state) {
2518*0b57cec5SDimitry Andric   StopHookCollection::iterator pos, end = m_stop_hooks.end();
2519*0b57cec5SDimitry Andric   for (pos = m_stop_hooks.begin(); pos != end; pos++) {
2520*0b57cec5SDimitry Andric     (*pos).second->SetIsActive(active_state);
2521*0b57cec5SDimitry Andric   }
2522*0b57cec5SDimitry Andric }
2523*0b57cec5SDimitry Andric 
2524*0b57cec5SDimitry Andric void Target::RunStopHooks() {
2525*0b57cec5SDimitry Andric   if (m_suppress_stop_hooks)
2526*0b57cec5SDimitry Andric     return;
2527*0b57cec5SDimitry Andric 
2528*0b57cec5SDimitry Andric   if (!m_process_sp)
2529*0b57cec5SDimitry Andric     return;
2530*0b57cec5SDimitry Andric 
2531*0b57cec5SDimitry Andric   // Somebody might have restarted the process:
2532*0b57cec5SDimitry Andric   if (m_process_sp->GetState() != eStateStopped)
2533*0b57cec5SDimitry Andric     return;
2534*0b57cec5SDimitry Andric 
2535*0b57cec5SDimitry Andric   // <rdar://problem/12027563> make sure we check that we are not stopped
2536*0b57cec5SDimitry Andric   // because of us running a user expression since in that case we do not want
2537*0b57cec5SDimitry Andric   // to run the stop-hooks
2538*0b57cec5SDimitry Andric   if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2539*0b57cec5SDimitry Andric     return;
2540*0b57cec5SDimitry Andric 
2541*0b57cec5SDimitry Andric   if (m_stop_hooks.empty())
2542*0b57cec5SDimitry Andric     return;
2543*0b57cec5SDimitry Andric 
2544*0b57cec5SDimitry Andric   StopHookCollection::iterator pos, end = m_stop_hooks.end();
2545*0b57cec5SDimitry Andric 
2546*0b57cec5SDimitry Andric   // If there aren't any active stop hooks, don't bother either.
2547*0b57cec5SDimitry Andric   // Also see if any of the active hooks want to auto-continue.
2548*0b57cec5SDimitry Andric   bool any_active_hooks = false;
2549*0b57cec5SDimitry Andric   bool auto_continue = false;
2550*0b57cec5SDimitry Andric   for (auto hook : m_stop_hooks) {
2551*0b57cec5SDimitry Andric     if (hook.second->IsActive()) {
2552*0b57cec5SDimitry Andric       any_active_hooks = true;
2553*0b57cec5SDimitry Andric       auto_continue |= hook.second->GetAutoContinue();
2554*0b57cec5SDimitry Andric     }
2555*0b57cec5SDimitry Andric   }
2556*0b57cec5SDimitry Andric   if (!any_active_hooks)
2557*0b57cec5SDimitry Andric     return;
2558*0b57cec5SDimitry Andric 
25595ffd83dbSDimitry Andric   CommandReturnObject result(m_debugger.GetUseColor());
2560*0b57cec5SDimitry Andric 
2561*0b57cec5SDimitry Andric   std::vector<ExecutionContext> exc_ctx_with_reasons;
2562*0b57cec5SDimitry Andric   std::vector<SymbolContext> sym_ctx_with_reasons;
2563*0b57cec5SDimitry Andric 
2564*0b57cec5SDimitry Andric   ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2565*0b57cec5SDimitry Andric   size_t num_threads = cur_threadlist.GetSize();
2566*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_threads; i++) {
2567*0b57cec5SDimitry Andric     lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(i);
2568*0b57cec5SDimitry Andric     if (cur_thread_sp->ThreadStoppedForAReason()) {
2569*0b57cec5SDimitry Andric       lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2570*0b57cec5SDimitry Andric       exc_ctx_with_reasons.push_back(ExecutionContext(
2571*0b57cec5SDimitry Andric           m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2572*0b57cec5SDimitry Andric       sym_ctx_with_reasons.push_back(
2573*0b57cec5SDimitry Andric           cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2574*0b57cec5SDimitry Andric     }
2575*0b57cec5SDimitry Andric   }
2576*0b57cec5SDimitry Andric 
2577*0b57cec5SDimitry Andric   // If no threads stopped for a reason, don't run the stop-hooks.
2578*0b57cec5SDimitry Andric   size_t num_exe_ctx = exc_ctx_with_reasons.size();
2579*0b57cec5SDimitry Andric   if (num_exe_ctx == 0)
2580*0b57cec5SDimitry Andric     return;
2581*0b57cec5SDimitry Andric 
2582*0b57cec5SDimitry Andric   result.SetImmediateOutputStream(m_debugger.GetAsyncOutputStream());
2583*0b57cec5SDimitry Andric   result.SetImmediateErrorStream(m_debugger.GetAsyncErrorStream());
2584*0b57cec5SDimitry Andric 
2585*0b57cec5SDimitry Andric   bool keep_going = true;
2586*0b57cec5SDimitry Andric   bool hooks_ran = false;
2587*0b57cec5SDimitry Andric   bool print_hook_header = (m_stop_hooks.size() != 1);
2588*0b57cec5SDimitry Andric   bool print_thread_header = (num_exe_ctx != 1);
2589*0b57cec5SDimitry Andric   bool did_restart = false;
2590*0b57cec5SDimitry Andric 
2591*0b57cec5SDimitry Andric   for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) {
2592*0b57cec5SDimitry Andric     // result.Clear();
2593*0b57cec5SDimitry Andric     StopHookSP cur_hook_sp = (*pos).second;
2594*0b57cec5SDimitry Andric     if (!cur_hook_sp->IsActive())
2595*0b57cec5SDimitry Andric       continue;
2596*0b57cec5SDimitry Andric 
2597*0b57cec5SDimitry Andric     bool any_thread_matched = false;
2598*0b57cec5SDimitry Andric     for (size_t i = 0; keep_going && i < num_exe_ctx; i++) {
2599*0b57cec5SDimitry Andric       if ((cur_hook_sp->GetSpecifier() == nullptr ||
2600*0b57cec5SDimitry Andric            cur_hook_sp->GetSpecifier()->SymbolContextMatches(
2601*0b57cec5SDimitry Andric                sym_ctx_with_reasons[i])) &&
2602*0b57cec5SDimitry Andric           (cur_hook_sp->GetThreadSpecifier() == nullptr ||
2603*0b57cec5SDimitry Andric            cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(
2604*0b57cec5SDimitry Andric                exc_ctx_with_reasons[i].GetThreadRef()))) {
2605*0b57cec5SDimitry Andric         if (!hooks_ran) {
2606*0b57cec5SDimitry Andric           hooks_ran = true;
2607*0b57cec5SDimitry Andric         }
2608*0b57cec5SDimitry Andric         if (print_hook_header && !any_thread_matched) {
2609*0b57cec5SDimitry Andric           const char *cmd =
2610*0b57cec5SDimitry Andric               (cur_hook_sp->GetCommands().GetSize() == 1
2611*0b57cec5SDimitry Andric                    ? cur_hook_sp->GetCommands().GetStringAtIndex(0)
2612*0b57cec5SDimitry Andric                    : nullptr);
2613*0b57cec5SDimitry Andric           if (cmd)
2614*0b57cec5SDimitry Andric             result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n",
2615*0b57cec5SDimitry Andric                                            cur_hook_sp->GetID(), cmd);
2616*0b57cec5SDimitry Andric           else
2617*0b57cec5SDimitry Andric             result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n",
2618*0b57cec5SDimitry Andric                                            cur_hook_sp->GetID());
2619*0b57cec5SDimitry Andric           any_thread_matched = true;
2620*0b57cec5SDimitry Andric         }
2621*0b57cec5SDimitry Andric 
2622*0b57cec5SDimitry Andric         if (print_thread_header)
2623*0b57cec5SDimitry Andric           result.AppendMessageWithFormat(
2624*0b57cec5SDimitry Andric               "-- Thread %d\n",
2625*0b57cec5SDimitry Andric               exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
2626*0b57cec5SDimitry Andric 
2627*0b57cec5SDimitry Andric         CommandInterpreterRunOptions options;
2628*0b57cec5SDimitry Andric         options.SetStopOnContinue(true);
2629*0b57cec5SDimitry Andric         options.SetStopOnError(true);
2630*0b57cec5SDimitry Andric         options.SetEchoCommands(false);
2631*0b57cec5SDimitry Andric         options.SetPrintResults(true);
2632*0b57cec5SDimitry Andric         options.SetPrintErrors(true);
2633*0b57cec5SDimitry Andric         options.SetAddToHistory(false);
2634*0b57cec5SDimitry Andric 
2635*0b57cec5SDimitry Andric         // Force Async:
2636*0b57cec5SDimitry Andric         bool old_async = GetDebugger().GetAsyncExecution();
2637*0b57cec5SDimitry Andric         GetDebugger().SetAsyncExecution(true);
2638*0b57cec5SDimitry Andric         GetDebugger().GetCommandInterpreter().HandleCommands(
2639*0b57cec5SDimitry Andric             cur_hook_sp->GetCommands(), &exc_ctx_with_reasons[i], options,
2640*0b57cec5SDimitry Andric             result);
2641*0b57cec5SDimitry Andric         GetDebugger().SetAsyncExecution(old_async);
2642*0b57cec5SDimitry Andric         // If the command started the target going again, we should bag out of
2643*0b57cec5SDimitry Andric         // running the stop hooks.
2644*0b57cec5SDimitry Andric         if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2645*0b57cec5SDimitry Andric             (result.GetStatus() == eReturnStatusSuccessContinuingResult)) {
2646*0b57cec5SDimitry Andric           // But only complain if there were more stop hooks to do:
2647*0b57cec5SDimitry Andric           StopHookCollection::iterator tmp = pos;
2648*0b57cec5SDimitry Andric           if (++tmp != end)
26499dba64beSDimitry Andric             result.AppendMessageWithFormat(
26509dba64beSDimitry Andric                 "\nAborting stop hooks, hook %" PRIu64
2651*0b57cec5SDimitry Andric                 " set the program running.\n"
2652*0b57cec5SDimitry Andric                 "  Consider using '-G true' to make "
2653*0b57cec5SDimitry Andric                 "stop hooks auto-continue.\n",
2654*0b57cec5SDimitry Andric                 cur_hook_sp->GetID());
2655*0b57cec5SDimitry Andric           keep_going = false;
2656*0b57cec5SDimitry Andric           did_restart = true;
2657*0b57cec5SDimitry Andric         }
2658*0b57cec5SDimitry Andric       }
2659*0b57cec5SDimitry Andric     }
2660*0b57cec5SDimitry Andric   }
2661*0b57cec5SDimitry Andric   // Finally, if auto-continue was requested, do it now:
2662*0b57cec5SDimitry Andric   if (!did_restart && auto_continue)
2663*0b57cec5SDimitry Andric     m_process_sp->PrivateResume();
2664*0b57cec5SDimitry Andric 
2665*0b57cec5SDimitry Andric   result.GetImmediateOutputStream()->Flush();
2666*0b57cec5SDimitry Andric   result.GetImmediateErrorStream()->Flush();
2667*0b57cec5SDimitry Andric }
2668*0b57cec5SDimitry Andric 
2669*0b57cec5SDimitry Andric const TargetPropertiesSP &Target::GetGlobalProperties() {
2670*0b57cec5SDimitry Andric   // NOTE: intentional leak so we don't crash if global destructor chain gets
2671*0b57cec5SDimitry Andric   // called as other threads still use the result of this function
2672*0b57cec5SDimitry Andric   static TargetPropertiesSP *g_settings_sp_ptr =
2673*0b57cec5SDimitry Andric       new TargetPropertiesSP(new TargetProperties(nullptr));
2674*0b57cec5SDimitry Andric   return *g_settings_sp_ptr;
2675*0b57cec5SDimitry Andric }
2676*0b57cec5SDimitry Andric 
2677*0b57cec5SDimitry Andric Status Target::Install(ProcessLaunchInfo *launch_info) {
2678*0b57cec5SDimitry Andric   Status error;
2679*0b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
2680*0b57cec5SDimitry Andric   if (platform_sp) {
2681*0b57cec5SDimitry Andric     if (platform_sp->IsRemote()) {
2682*0b57cec5SDimitry Andric       if (platform_sp->IsConnected()) {
26835ffd83dbSDimitry Andric         // Install all files that have an install path when connected to a
26845ffd83dbSDimitry Andric         // remote platform. If target.auto-install-main-executable is set then
26855ffd83dbSDimitry Andric         // also install the main executable even if it does not have an explicit
26865ffd83dbSDimitry Andric         // install path specified.
2687*0b57cec5SDimitry Andric         const ModuleList &modules = GetImages();
2688*0b57cec5SDimitry Andric         const size_t num_images = modules.GetSize();
2689*0b57cec5SDimitry Andric         for (size_t idx = 0; idx < num_images; ++idx) {
2690*0b57cec5SDimitry Andric           ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2691*0b57cec5SDimitry Andric           if (module_sp) {
2692*0b57cec5SDimitry Andric             const bool is_main_executable = module_sp == GetExecutableModule();
2693*0b57cec5SDimitry Andric             FileSpec local_file(module_sp->GetFileSpec());
2694*0b57cec5SDimitry Andric             if (local_file) {
2695*0b57cec5SDimitry Andric               FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
2696*0b57cec5SDimitry Andric               if (!remote_file) {
26975ffd83dbSDimitry Andric                 if (is_main_executable && GetAutoInstallMainExecutable()) {
26985ffd83dbSDimitry Andric                   // Automatically install the main executable.
2699*0b57cec5SDimitry Andric                   remote_file = platform_sp->GetRemoteWorkingDirectory();
2700*0b57cec5SDimitry Andric                   remote_file.AppendPathComponent(
2701*0b57cec5SDimitry Andric                       module_sp->GetFileSpec().GetFilename().GetCString());
2702*0b57cec5SDimitry Andric                 }
2703*0b57cec5SDimitry Andric               }
2704*0b57cec5SDimitry Andric               if (remote_file) {
2705*0b57cec5SDimitry Andric                 error = platform_sp->Install(local_file, remote_file);
2706*0b57cec5SDimitry Andric                 if (error.Success()) {
2707*0b57cec5SDimitry Andric                   module_sp->SetPlatformFileSpec(remote_file);
2708*0b57cec5SDimitry Andric                   if (is_main_executable) {
2709*0b57cec5SDimitry Andric                     platform_sp->SetFilePermissions(remote_file, 0700);
2710*0b57cec5SDimitry Andric                     if (launch_info)
2711*0b57cec5SDimitry Andric                       launch_info->SetExecutableFile(remote_file, false);
2712*0b57cec5SDimitry Andric                   }
2713*0b57cec5SDimitry Andric                 } else
2714*0b57cec5SDimitry Andric                   break;
2715*0b57cec5SDimitry Andric               }
2716*0b57cec5SDimitry Andric             }
2717*0b57cec5SDimitry Andric           }
2718*0b57cec5SDimitry Andric         }
2719*0b57cec5SDimitry Andric       }
2720*0b57cec5SDimitry Andric     }
2721*0b57cec5SDimitry Andric   }
2722*0b57cec5SDimitry Andric   return error;
2723*0b57cec5SDimitry Andric }
2724*0b57cec5SDimitry Andric 
2725*0b57cec5SDimitry Andric bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
2726*0b57cec5SDimitry Andric                                 uint32_t stop_id) {
2727*0b57cec5SDimitry Andric   return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2728*0b57cec5SDimitry Andric }
2729*0b57cec5SDimitry Andric 
2730*0b57cec5SDimitry Andric bool Target::ResolveFileAddress(lldb::addr_t file_addr,
2731*0b57cec5SDimitry Andric                                 Address &resolved_addr) {
2732*0b57cec5SDimitry Andric   return m_images.ResolveFileAddress(file_addr, resolved_addr);
2733*0b57cec5SDimitry Andric }
2734*0b57cec5SDimitry Andric 
2735*0b57cec5SDimitry Andric bool Target::SetSectionLoadAddress(const SectionSP &section_sp,
2736*0b57cec5SDimitry Andric                                    addr_t new_section_load_addr,
2737*0b57cec5SDimitry Andric                                    bool warn_multiple) {
2738*0b57cec5SDimitry Andric   const addr_t old_section_load_addr =
2739*0b57cec5SDimitry Andric       m_section_load_history.GetSectionLoadAddress(
2740*0b57cec5SDimitry Andric           SectionLoadHistory::eStopIDNow, section_sp);
2741*0b57cec5SDimitry Andric   if (old_section_load_addr != new_section_load_addr) {
2742*0b57cec5SDimitry Andric     uint32_t stop_id = 0;
2743*0b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
2744*0b57cec5SDimitry Andric     if (process_sp)
2745*0b57cec5SDimitry Andric       stop_id = process_sp->GetStopID();
2746*0b57cec5SDimitry Andric     else
2747*0b57cec5SDimitry Andric       stop_id = m_section_load_history.GetLastStopID();
2748*0b57cec5SDimitry Andric     if (m_section_load_history.SetSectionLoadAddress(
2749*0b57cec5SDimitry Andric             stop_id, section_sp, new_section_load_addr, warn_multiple))
2750*0b57cec5SDimitry Andric       return true; // Return true if the section load address was changed...
2751*0b57cec5SDimitry Andric   }
2752*0b57cec5SDimitry Andric   return false; // Return false to indicate nothing changed
2753*0b57cec5SDimitry Andric }
2754*0b57cec5SDimitry Andric 
2755*0b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const ModuleList &module_list) {
2756*0b57cec5SDimitry Andric   size_t section_unload_count = 0;
2757*0b57cec5SDimitry Andric   size_t num_modules = module_list.GetSize();
2758*0b57cec5SDimitry Andric   for (size_t i = 0; i < num_modules; ++i) {
2759*0b57cec5SDimitry Andric     section_unload_count +=
2760*0b57cec5SDimitry Andric         UnloadModuleSections(module_list.GetModuleAtIndex(i));
2761*0b57cec5SDimitry Andric   }
2762*0b57cec5SDimitry Andric   return section_unload_count;
2763*0b57cec5SDimitry Andric }
2764*0b57cec5SDimitry Andric 
2765*0b57cec5SDimitry Andric size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) {
2766*0b57cec5SDimitry Andric   uint32_t stop_id = 0;
2767*0b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
2768*0b57cec5SDimitry Andric   if (process_sp)
2769*0b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
2770*0b57cec5SDimitry Andric   else
2771*0b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
2772*0b57cec5SDimitry Andric   SectionList *sections = module_sp->GetSectionList();
2773*0b57cec5SDimitry Andric   size_t section_unload_count = 0;
2774*0b57cec5SDimitry Andric   if (sections) {
2775*0b57cec5SDimitry Andric     const uint32_t num_sections = sections->GetNumSections(0);
2776*0b57cec5SDimitry Andric     for (uint32_t i = 0; i < num_sections; ++i) {
2777*0b57cec5SDimitry Andric       section_unload_count += m_section_load_history.SetSectionUnloaded(
2778*0b57cec5SDimitry Andric           stop_id, sections->GetSectionAtIndex(i));
2779*0b57cec5SDimitry Andric     }
2780*0b57cec5SDimitry Andric   }
2781*0b57cec5SDimitry Andric   return section_unload_count;
2782*0b57cec5SDimitry Andric }
2783*0b57cec5SDimitry Andric 
2784*0b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
2785*0b57cec5SDimitry Andric   uint32_t stop_id = 0;
2786*0b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
2787*0b57cec5SDimitry Andric   if (process_sp)
2788*0b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
2789*0b57cec5SDimitry Andric   else
2790*0b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
2791*0b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp);
2792*0b57cec5SDimitry Andric }
2793*0b57cec5SDimitry Andric 
2794*0b57cec5SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp,
2795*0b57cec5SDimitry Andric                                 addr_t load_addr) {
2796*0b57cec5SDimitry Andric   uint32_t stop_id = 0;
2797*0b57cec5SDimitry Andric   ProcessSP process_sp(GetProcessSP());
2798*0b57cec5SDimitry Andric   if (process_sp)
2799*0b57cec5SDimitry Andric     stop_id = process_sp->GetStopID();
2800*0b57cec5SDimitry Andric   else
2801*0b57cec5SDimitry Andric     stop_id = m_section_load_history.GetLastStopID();
2802*0b57cec5SDimitry Andric   return m_section_load_history.SetSectionUnloaded(stop_id, section_sp,
2803*0b57cec5SDimitry Andric                                                    load_addr);
2804*0b57cec5SDimitry Andric }
2805*0b57cec5SDimitry Andric 
2806*0b57cec5SDimitry Andric void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
2807*0b57cec5SDimitry Andric 
2808*0b57cec5SDimitry Andric Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
2809*0b57cec5SDimitry Andric   Status error;
2810*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
2811*0b57cec5SDimitry Andric 
28129dba64beSDimitry Andric   LLDB_LOGF(log, "Target::%s() called for %s", __FUNCTION__,
2813*0b57cec5SDimitry Andric             launch_info.GetExecutableFile().GetPath().c_str());
2814*0b57cec5SDimitry Andric 
2815*0b57cec5SDimitry Andric   StateType state = eStateInvalid;
2816*0b57cec5SDimitry Andric 
2817*0b57cec5SDimitry Andric   // Scope to temporarily get the process state in case someone has manually
2818*0b57cec5SDimitry Andric   // remotely connected already to a process and we can skip the platform
2819*0b57cec5SDimitry Andric   // launching.
2820*0b57cec5SDimitry Andric   {
2821*0b57cec5SDimitry Andric     ProcessSP process_sp(GetProcessSP());
2822*0b57cec5SDimitry Andric 
2823*0b57cec5SDimitry Andric     if (process_sp) {
2824*0b57cec5SDimitry Andric       state = process_sp->GetState();
28259dba64beSDimitry Andric       LLDB_LOGF(log,
2826*0b57cec5SDimitry Andric                 "Target::%s the process exists, and its current state is %s",
2827*0b57cec5SDimitry Andric                 __FUNCTION__, StateAsCString(state));
2828*0b57cec5SDimitry Andric     } else {
28299dba64beSDimitry Andric       LLDB_LOGF(log, "Target::%s the process instance doesn't currently exist.",
2830*0b57cec5SDimitry Andric                 __FUNCTION__);
2831*0b57cec5SDimitry Andric     }
2832*0b57cec5SDimitry Andric   }
2833*0b57cec5SDimitry Andric 
2834*0b57cec5SDimitry Andric   launch_info.GetFlags().Set(eLaunchFlagDebug);
2835*0b57cec5SDimitry Andric 
2836*0b57cec5SDimitry Andric   // Get the value of synchronous execution here.  If you wait till after you
2837*0b57cec5SDimitry Andric   // have started to run, then you could have hit a breakpoint, whose command
2838*0b57cec5SDimitry Andric   // might switch the value, and then you'll pick up that incorrect value.
2839*0b57cec5SDimitry Andric   Debugger &debugger = GetDebugger();
2840*0b57cec5SDimitry Andric   const bool synchronous_execution =
2841*0b57cec5SDimitry Andric       debugger.GetCommandInterpreter().GetSynchronous();
2842*0b57cec5SDimitry Andric 
2843*0b57cec5SDimitry Andric   PlatformSP platform_sp(GetPlatform());
2844*0b57cec5SDimitry Andric 
2845*0b57cec5SDimitry Andric   FinalizeFileActions(launch_info);
2846*0b57cec5SDimitry Andric 
2847*0b57cec5SDimitry Andric   if (state == eStateConnected) {
2848*0b57cec5SDimitry Andric     if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
2849*0b57cec5SDimitry Andric       error.SetErrorString(
2850*0b57cec5SDimitry Andric           "can't launch in tty when launching through a remote connection");
2851*0b57cec5SDimitry Andric       return error;
2852*0b57cec5SDimitry Andric     }
2853*0b57cec5SDimitry Andric   }
2854*0b57cec5SDimitry Andric 
2855*0b57cec5SDimitry Andric   if (!launch_info.GetArchitecture().IsValid())
2856*0b57cec5SDimitry Andric     launch_info.GetArchitecture() = GetArchitecture();
2857*0b57cec5SDimitry Andric 
2858*0b57cec5SDimitry Andric   // If we're not already connected to the process, and if we have a platform
2859*0b57cec5SDimitry Andric   // that can launch a process for debugging, go ahead and do that here.
2860*0b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp &&
2861*0b57cec5SDimitry Andric       platform_sp->CanDebugProcess()) {
28629dba64beSDimitry Andric     LLDB_LOGF(log, "Target::%s asking the platform to debug the process",
2863*0b57cec5SDimitry Andric               __FUNCTION__);
2864*0b57cec5SDimitry Andric 
2865*0b57cec5SDimitry Andric     // If there was a previous process, delete it before we make the new one.
2866*0b57cec5SDimitry Andric     // One subtle point, we delete the process before we release the reference
2867*0b57cec5SDimitry Andric     // to m_process_sp.  That way even if we are the last owner, the process
2868*0b57cec5SDimitry Andric     // will get Finalized before it gets destroyed.
2869*0b57cec5SDimitry Andric     DeleteCurrentProcess();
2870*0b57cec5SDimitry Andric 
2871*0b57cec5SDimitry Andric     m_process_sp =
2872*0b57cec5SDimitry Andric         GetPlatform()->DebugProcess(launch_info, debugger, this, error);
2873*0b57cec5SDimitry Andric 
2874*0b57cec5SDimitry Andric   } else {
28759dba64beSDimitry Andric     LLDB_LOGF(log,
28769dba64beSDimitry Andric               "Target::%s the platform doesn't know how to debug a "
2877*0b57cec5SDimitry Andric               "process, getting a process plugin to do this for us.",
2878*0b57cec5SDimitry Andric               __FUNCTION__);
2879*0b57cec5SDimitry Andric 
2880*0b57cec5SDimitry Andric     if (state == eStateConnected) {
2881*0b57cec5SDimitry Andric       assert(m_process_sp);
2882*0b57cec5SDimitry Andric     } else {
2883*0b57cec5SDimitry Andric       // Use a Process plugin to construct the process.
2884*0b57cec5SDimitry Andric       const char *plugin_name = launch_info.GetProcessPluginName();
2885*0b57cec5SDimitry Andric       CreateProcess(launch_info.GetListener(), plugin_name, nullptr);
2886*0b57cec5SDimitry Andric     }
2887*0b57cec5SDimitry Andric 
2888*0b57cec5SDimitry Andric     // Since we didn't have a platform launch the process, launch it here.
2889*0b57cec5SDimitry Andric     if (m_process_sp)
2890*0b57cec5SDimitry Andric       error = m_process_sp->Launch(launch_info);
2891*0b57cec5SDimitry Andric   }
2892*0b57cec5SDimitry Andric 
2893*0b57cec5SDimitry Andric   if (!m_process_sp) {
2894*0b57cec5SDimitry Andric     if (error.Success())
2895*0b57cec5SDimitry Andric       error.SetErrorString("failed to launch or debug process");
2896*0b57cec5SDimitry Andric     return error;
2897*0b57cec5SDimitry Andric   }
2898*0b57cec5SDimitry Andric 
2899*0b57cec5SDimitry Andric   if (error.Success()) {
2900*0b57cec5SDimitry Andric     if (synchronous_execution ||
2901*0b57cec5SDimitry Andric         !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
2902*0b57cec5SDimitry Andric       ListenerSP hijack_listener_sp(launch_info.GetHijackListener());
2903*0b57cec5SDimitry Andric       if (!hijack_listener_sp) {
2904*0b57cec5SDimitry Andric         hijack_listener_sp =
2905*0b57cec5SDimitry Andric             Listener::MakeListener("lldb.Target.Launch.hijack");
2906*0b57cec5SDimitry Andric         launch_info.SetHijackListener(hijack_listener_sp);
2907*0b57cec5SDimitry Andric         m_process_sp->HijackProcessEvents(hijack_listener_sp);
2908*0b57cec5SDimitry Andric       }
2909*0b57cec5SDimitry Andric 
2910*0b57cec5SDimitry Andric       StateType state = m_process_sp->WaitForProcessToStop(
2911*0b57cec5SDimitry Andric           llvm::None, nullptr, false, hijack_listener_sp, nullptr);
2912*0b57cec5SDimitry Andric 
2913*0b57cec5SDimitry Andric       if (state == eStateStopped) {
2914*0b57cec5SDimitry Andric         if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
2915*0b57cec5SDimitry Andric           if (synchronous_execution) {
29169dba64beSDimitry Andric             // Now we have handled the stop-from-attach, and we are just
29179dba64beSDimitry Andric             // switching to a synchronous resume.  So we should switch to the
29189dba64beSDimitry Andric             // SyncResume hijacker.
2919*0b57cec5SDimitry Andric             m_process_sp->RestoreProcessEvents();
2920*0b57cec5SDimitry Andric             m_process_sp->ResumeSynchronous(stream);
2921*0b57cec5SDimitry Andric           } else {
2922*0b57cec5SDimitry Andric             m_process_sp->RestoreProcessEvents();
2923*0b57cec5SDimitry Andric             error = m_process_sp->PrivateResume();
2924*0b57cec5SDimitry Andric           }
2925*0b57cec5SDimitry Andric           if (!error.Success()) {
2926*0b57cec5SDimitry Andric             Status error2;
2927*0b57cec5SDimitry Andric             error2.SetErrorStringWithFormat(
2928*0b57cec5SDimitry Andric                 "process resume at entry point failed: %s", error.AsCString());
2929*0b57cec5SDimitry Andric             error = error2;
2930*0b57cec5SDimitry Andric           }
2931*0b57cec5SDimitry Andric         }
2932*0b57cec5SDimitry Andric       } else if (state == eStateExited) {
2933*0b57cec5SDimitry Andric         bool with_shell = !!launch_info.GetShell();
2934*0b57cec5SDimitry Andric         const int exit_status = m_process_sp->GetExitStatus();
2935*0b57cec5SDimitry Andric         const char *exit_desc = m_process_sp->GetExitDescription();
2936*0b57cec5SDimitry Andric #define LAUNCH_SHELL_MESSAGE                                                   \
2937*0b57cec5SDimitry Andric   "\n'r' and 'run' are aliases that default to launching through a "           \
2938*0b57cec5SDimitry Andric   "shell.\nTry launching without going through a shell by using 'process "     \
2939*0b57cec5SDimitry Andric   "launch'."
2940*0b57cec5SDimitry Andric         if (exit_desc && exit_desc[0]) {
2941*0b57cec5SDimitry Andric           if (with_shell)
2942*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
2943*0b57cec5SDimitry Andric                 "process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE,
2944*0b57cec5SDimitry Andric                 exit_status, exit_desc);
2945*0b57cec5SDimitry Andric           else
2946*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat("process exited with status %i (%s)",
2947*0b57cec5SDimitry Andric                                            exit_status, exit_desc);
2948*0b57cec5SDimitry Andric         } else {
2949*0b57cec5SDimitry Andric           if (with_shell)
2950*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat(
2951*0b57cec5SDimitry Andric                 "process exited with status %i" LAUNCH_SHELL_MESSAGE,
2952*0b57cec5SDimitry Andric                 exit_status);
2953*0b57cec5SDimitry Andric           else
2954*0b57cec5SDimitry Andric             error.SetErrorStringWithFormat("process exited with status %i",
2955*0b57cec5SDimitry Andric                                            exit_status);
2956*0b57cec5SDimitry Andric         }
2957*0b57cec5SDimitry Andric       } else {
2958*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
2959*0b57cec5SDimitry Andric             "initial process state wasn't stopped: %s", StateAsCString(state));
2960*0b57cec5SDimitry Andric       }
2961*0b57cec5SDimitry Andric     }
2962*0b57cec5SDimitry Andric     m_process_sp->RestoreProcessEvents();
2963*0b57cec5SDimitry Andric   }
2964*0b57cec5SDimitry Andric   return error;
2965*0b57cec5SDimitry Andric }
2966*0b57cec5SDimitry Andric 
2967*0b57cec5SDimitry Andric Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
2968*0b57cec5SDimitry Andric   auto state = eStateInvalid;
2969*0b57cec5SDimitry Andric   auto process_sp = GetProcessSP();
2970*0b57cec5SDimitry Andric   if (process_sp) {
2971*0b57cec5SDimitry Andric     state = process_sp->GetState();
2972*0b57cec5SDimitry Andric     if (process_sp->IsAlive() && state != eStateConnected) {
2973*0b57cec5SDimitry Andric       if (state == eStateAttaching)
2974*0b57cec5SDimitry Andric         return Status("process attach is in progress");
2975*0b57cec5SDimitry Andric       return Status("a process is already being debugged");
2976*0b57cec5SDimitry Andric     }
2977*0b57cec5SDimitry Andric   }
2978*0b57cec5SDimitry Andric 
2979*0b57cec5SDimitry Andric   const ModuleSP old_exec_module_sp = GetExecutableModule();
2980*0b57cec5SDimitry Andric 
2981*0b57cec5SDimitry Andric   // If no process info was specified, then use the target executable name as
2982*0b57cec5SDimitry Andric   // the process to attach to by default
2983*0b57cec5SDimitry Andric   if (!attach_info.ProcessInfoSpecified()) {
2984*0b57cec5SDimitry Andric     if (old_exec_module_sp)
2985*0b57cec5SDimitry Andric       attach_info.GetExecutableFile().GetFilename() =
2986*0b57cec5SDimitry Andric           old_exec_module_sp->GetPlatformFileSpec().GetFilename();
2987*0b57cec5SDimitry Andric 
2988*0b57cec5SDimitry Andric     if (!attach_info.ProcessInfoSpecified()) {
2989*0b57cec5SDimitry Andric       return Status("no process specified, create a target with a file, or "
2990*0b57cec5SDimitry Andric                     "specify the --pid or --name");
2991*0b57cec5SDimitry Andric     }
2992*0b57cec5SDimitry Andric   }
2993*0b57cec5SDimitry Andric 
2994*0b57cec5SDimitry Andric   const auto platform_sp =
2995*0b57cec5SDimitry Andric       GetDebugger().GetPlatformList().GetSelectedPlatform();
2996*0b57cec5SDimitry Andric   ListenerSP hijack_listener_sp;
2997*0b57cec5SDimitry Andric   const bool async = attach_info.GetAsync();
2998*0b57cec5SDimitry Andric   if (!async) {
2999*0b57cec5SDimitry Andric     hijack_listener_sp =
3000*0b57cec5SDimitry Andric         Listener::MakeListener("lldb.Target.Attach.attach.hijack");
3001*0b57cec5SDimitry Andric     attach_info.SetHijackListener(hijack_listener_sp);
3002*0b57cec5SDimitry Andric   }
3003*0b57cec5SDimitry Andric 
3004*0b57cec5SDimitry Andric   Status error;
3005*0b57cec5SDimitry Andric   if (state != eStateConnected && platform_sp != nullptr &&
3006*0b57cec5SDimitry Andric       platform_sp->CanDebugProcess()) {
3007*0b57cec5SDimitry Andric     SetPlatform(platform_sp);
3008*0b57cec5SDimitry Andric     process_sp = platform_sp->Attach(attach_info, GetDebugger(), this, error);
3009*0b57cec5SDimitry Andric   } else {
3010*0b57cec5SDimitry Andric     if (state != eStateConnected) {
3011*0b57cec5SDimitry Andric       const char *plugin_name = attach_info.GetProcessPluginName();
3012*0b57cec5SDimitry Andric       process_sp =
3013*0b57cec5SDimitry Andric           CreateProcess(attach_info.GetListenerForProcess(GetDebugger()),
3014*0b57cec5SDimitry Andric                         plugin_name, nullptr);
3015*0b57cec5SDimitry Andric       if (process_sp == nullptr) {
3016*0b57cec5SDimitry Andric         error.SetErrorStringWithFormat(
3017*0b57cec5SDimitry Andric             "failed to create process using plugin %s",
3018*0b57cec5SDimitry Andric             (plugin_name) ? plugin_name : "null");
3019*0b57cec5SDimitry Andric         return error;
3020*0b57cec5SDimitry Andric       }
3021*0b57cec5SDimitry Andric     }
3022*0b57cec5SDimitry Andric     if (hijack_listener_sp)
3023*0b57cec5SDimitry Andric       process_sp->HijackProcessEvents(hijack_listener_sp);
3024*0b57cec5SDimitry Andric     error = process_sp->Attach(attach_info);
3025*0b57cec5SDimitry Andric   }
3026*0b57cec5SDimitry Andric 
3027*0b57cec5SDimitry Andric   if (error.Success() && process_sp) {
3028*0b57cec5SDimitry Andric     if (async) {
3029*0b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
3030*0b57cec5SDimitry Andric     } else {
3031*0b57cec5SDimitry Andric       state = process_sp->WaitForProcessToStop(
3032*0b57cec5SDimitry Andric           llvm::None, nullptr, false, attach_info.GetHijackListener(), stream);
3033*0b57cec5SDimitry Andric       process_sp->RestoreProcessEvents();
3034*0b57cec5SDimitry Andric 
3035*0b57cec5SDimitry Andric       if (state != eStateStopped) {
3036*0b57cec5SDimitry Andric         const char *exit_desc = process_sp->GetExitDescription();
3037*0b57cec5SDimitry Andric         if (exit_desc)
3038*0b57cec5SDimitry Andric           error.SetErrorStringWithFormat("%s", exit_desc);
3039*0b57cec5SDimitry Andric         else
3040*0b57cec5SDimitry Andric           error.SetErrorString(
3041*0b57cec5SDimitry Andric               "process did not stop (no such process or permission problem?)");
3042*0b57cec5SDimitry Andric         process_sp->Destroy(false);
3043*0b57cec5SDimitry Andric       }
3044*0b57cec5SDimitry Andric     }
3045*0b57cec5SDimitry Andric   }
3046*0b57cec5SDimitry Andric   return error;
3047*0b57cec5SDimitry Andric }
3048*0b57cec5SDimitry Andric 
3049*0b57cec5SDimitry Andric void Target::FinalizeFileActions(ProcessLaunchInfo &info) {
3050*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
3051*0b57cec5SDimitry Andric 
3052*0b57cec5SDimitry Andric   // Finalize the file actions, and if none were given, default to opening up a
3053*0b57cec5SDimitry Andric   // pseudo terminal
3054*0b57cec5SDimitry Andric   PlatformSP platform_sp = GetPlatform();
3055*0b57cec5SDimitry Andric   const bool default_to_use_pty =
3056*0b57cec5SDimitry Andric       m_platform_sp ? m_platform_sp->IsHost() : false;
3057*0b57cec5SDimitry Andric   LLDB_LOG(
3058*0b57cec5SDimitry Andric       log,
3059*0b57cec5SDimitry Andric       "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}",
3060*0b57cec5SDimitry Andric       bool(platform_sp),
3061*0b57cec5SDimitry Andric       platform_sp ? (platform_sp->IsHost() ? "true" : "false") : "n/a",
3062*0b57cec5SDimitry Andric       default_to_use_pty);
3063*0b57cec5SDimitry Andric 
3064*0b57cec5SDimitry Andric   // If nothing for stdin or stdout or stderr was specified, then check the
3065*0b57cec5SDimitry Andric   // process for any default settings that were set with "settings set"
3066*0b57cec5SDimitry Andric   if (info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
3067*0b57cec5SDimitry Andric       info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
3068*0b57cec5SDimitry Andric       info.GetFileActionForFD(STDERR_FILENO) == nullptr) {
3069*0b57cec5SDimitry Andric     LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating "
3070*0b57cec5SDimitry Andric                   "default handling");
3071*0b57cec5SDimitry Andric 
3072*0b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
3073*0b57cec5SDimitry Andric       // Do nothing, if we are launching in a remote terminal no file actions
3074*0b57cec5SDimitry Andric       // should be done at all.
3075*0b57cec5SDimitry Andric       return;
3076*0b57cec5SDimitry Andric     }
3077*0b57cec5SDimitry Andric 
3078*0b57cec5SDimitry Andric     if (info.GetFlags().Test(eLaunchFlagDisableSTDIO)) {
3079*0b57cec5SDimitry Andric       LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action "
3080*0b57cec5SDimitry Andric                     "for stdin, stdout and stderr");
3081*0b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDIN_FILENO, true, false);
3082*0b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
3083*0b57cec5SDimitry Andric       info.AppendSuppressFileAction(STDERR_FILENO, false, true);
3084*0b57cec5SDimitry Andric     } else {
3085*0b57cec5SDimitry Andric       // Check for any values that might have gotten set with any of: (lldb)
3086*0b57cec5SDimitry Andric       // settings set target.input-path (lldb) settings set target.output-path
3087*0b57cec5SDimitry Andric       // (lldb) settings set target.error-path
3088*0b57cec5SDimitry Andric       FileSpec in_file_spec;
3089*0b57cec5SDimitry Andric       FileSpec out_file_spec;
3090*0b57cec5SDimitry Andric       FileSpec err_file_spec;
3091*0b57cec5SDimitry Andric       // Only override with the target settings if we don't already have an
3092*0b57cec5SDimitry Andric       // action for in, out or error
3093*0b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDIN_FILENO) == nullptr)
3094*0b57cec5SDimitry Andric         in_file_spec = GetStandardInputPath();
3095*0b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr)
3096*0b57cec5SDimitry Andric         out_file_spec = GetStandardOutputPath();
3097*0b57cec5SDimitry Andric       if (info.GetFileActionForFD(STDERR_FILENO) == nullptr)
3098*0b57cec5SDimitry Andric         err_file_spec = GetStandardErrorPath();
3099*0b57cec5SDimitry Andric 
3100*0b57cec5SDimitry Andric       LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{1}'",
3101*0b57cec5SDimitry Andric                in_file_spec, out_file_spec, err_file_spec);
3102*0b57cec5SDimitry Andric 
3103*0b57cec5SDimitry Andric       if (in_file_spec) {
3104*0b57cec5SDimitry Andric         info.AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
3105*0b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdin open file action for {0}", in_file_spec);
3106*0b57cec5SDimitry Andric       }
3107*0b57cec5SDimitry Andric 
3108*0b57cec5SDimitry Andric       if (out_file_spec) {
3109*0b57cec5SDimitry Andric         info.AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
3110*0b57cec5SDimitry Andric         LLDB_LOG(log, "appended stdout open file action for {0}",
3111*0b57cec5SDimitry Andric                  out_file_spec);
3112*0b57cec5SDimitry Andric       }
3113*0b57cec5SDimitry Andric 
3114*0b57cec5SDimitry Andric       if (err_file_spec) {
3115*0b57cec5SDimitry Andric         info.AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
3116*0b57cec5SDimitry Andric         LLDB_LOG(log, "appended stderr open file action for {0}",
3117*0b57cec5SDimitry Andric                  err_file_spec);
3118*0b57cec5SDimitry Andric       }
3119*0b57cec5SDimitry Andric 
3120*0b57cec5SDimitry Andric       if (default_to_use_pty &&
3121*0b57cec5SDimitry Andric           (!in_file_spec || !out_file_spec || !err_file_spec)) {
3122*0b57cec5SDimitry Andric         llvm::Error Err = info.SetUpPtyRedirection();
3123*0b57cec5SDimitry Andric         LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
3124*0b57cec5SDimitry Andric       }
3125*0b57cec5SDimitry Andric     }
3126*0b57cec5SDimitry Andric   }
3127*0b57cec5SDimitry Andric }
3128*0b57cec5SDimitry Andric 
3129*0b57cec5SDimitry Andric // Target::StopHook
3130*0b57cec5SDimitry Andric Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid)
3131*0b57cec5SDimitry Andric     : UserID(uid), m_target_sp(target_sp), m_commands(), m_specifier_sp(),
3132*0b57cec5SDimitry Andric       m_thread_spec_up() {}
3133*0b57cec5SDimitry Andric 
3134*0b57cec5SDimitry Andric Target::StopHook::StopHook(const StopHook &rhs)
3135*0b57cec5SDimitry Andric     : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp),
3136*0b57cec5SDimitry Andric       m_commands(rhs.m_commands), m_specifier_sp(rhs.m_specifier_sp),
3137*0b57cec5SDimitry Andric       m_thread_spec_up(), m_active(rhs.m_active),
3138*0b57cec5SDimitry Andric       m_auto_continue(rhs.m_auto_continue) {
3139*0b57cec5SDimitry Andric   if (rhs.m_thread_spec_up)
31405ffd83dbSDimitry Andric     m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
3141*0b57cec5SDimitry Andric }
3142*0b57cec5SDimitry Andric 
3143*0b57cec5SDimitry Andric Target::StopHook::~StopHook() = default;
3144*0b57cec5SDimitry Andric 
3145*0b57cec5SDimitry Andric void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) {
3146*0b57cec5SDimitry Andric   m_specifier_sp.reset(specifier);
3147*0b57cec5SDimitry Andric }
3148*0b57cec5SDimitry Andric 
3149*0b57cec5SDimitry Andric void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) {
3150*0b57cec5SDimitry Andric   m_thread_spec_up.reset(specifier);
3151*0b57cec5SDimitry Andric }
3152*0b57cec5SDimitry Andric 
3153*0b57cec5SDimitry Andric void Target::StopHook::GetDescription(Stream *s,
3154*0b57cec5SDimitry Andric                                       lldb::DescriptionLevel level) const {
3155480093f4SDimitry Andric   unsigned indent_level = s->GetIndentLevel();
3156*0b57cec5SDimitry Andric 
3157*0b57cec5SDimitry Andric   s->SetIndentLevel(indent_level + 2);
3158*0b57cec5SDimitry Andric 
3159*0b57cec5SDimitry Andric   s->Printf("Hook: %" PRIu64 "\n", GetID());
3160*0b57cec5SDimitry Andric   if (m_active)
3161*0b57cec5SDimitry Andric     s->Indent("State: enabled\n");
3162*0b57cec5SDimitry Andric   else
3163*0b57cec5SDimitry Andric     s->Indent("State: disabled\n");
3164*0b57cec5SDimitry Andric 
3165*0b57cec5SDimitry Andric   if (m_auto_continue)
3166*0b57cec5SDimitry Andric     s->Indent("AutoContinue on\n");
3167*0b57cec5SDimitry Andric 
3168*0b57cec5SDimitry Andric   if (m_specifier_sp) {
3169*0b57cec5SDimitry Andric     s->Indent();
3170*0b57cec5SDimitry Andric     s->PutCString("Specifier:\n");
3171*0b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 4);
3172*0b57cec5SDimitry Andric     m_specifier_sp->GetDescription(s, level);
3173*0b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 2);
3174*0b57cec5SDimitry Andric   }
3175*0b57cec5SDimitry Andric 
3176*0b57cec5SDimitry Andric   if (m_thread_spec_up) {
3177*0b57cec5SDimitry Andric     StreamString tmp;
3178*0b57cec5SDimitry Andric     s->Indent("Thread:\n");
3179*0b57cec5SDimitry Andric     m_thread_spec_up->GetDescription(&tmp, level);
3180*0b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 4);
3181*0b57cec5SDimitry Andric     s->Indent(tmp.GetString());
3182*0b57cec5SDimitry Andric     s->PutCString("\n");
3183*0b57cec5SDimitry Andric     s->SetIndentLevel(indent_level + 2);
3184*0b57cec5SDimitry Andric   }
3185*0b57cec5SDimitry Andric 
3186*0b57cec5SDimitry Andric   s->Indent("Commands: \n");
3187*0b57cec5SDimitry Andric   s->SetIndentLevel(indent_level + 4);
3188*0b57cec5SDimitry Andric   uint32_t num_commands = m_commands.GetSize();
3189*0b57cec5SDimitry Andric   for (uint32_t i = 0; i < num_commands; i++) {
3190*0b57cec5SDimitry Andric     s->Indent(m_commands.GetStringAtIndex(i));
3191*0b57cec5SDimitry Andric     s->PutCString("\n");
3192*0b57cec5SDimitry Andric   }
3193*0b57cec5SDimitry Andric   s->SetIndentLevel(indent_level);
3194*0b57cec5SDimitry Andric }
3195*0b57cec5SDimitry Andric 
3196*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
31979dba64beSDimitry Andric     {
31989dba64beSDimitry Andric         eNoDynamicValues,
31999dba64beSDimitry Andric         "no-dynamic-values",
32009dba64beSDimitry Andric         "Don't calculate the dynamic type of values",
32019dba64beSDimitry Andric     },
32029dba64beSDimitry Andric     {
32039dba64beSDimitry Andric         eDynamicCanRunTarget,
32049dba64beSDimitry Andric         "run-target",
32059dba64beSDimitry Andric         "Calculate the dynamic type of values "
32069dba64beSDimitry Andric         "even if you have to run the target.",
32079dba64beSDimitry Andric     },
32089dba64beSDimitry Andric     {
32099dba64beSDimitry Andric         eDynamicDontRunTarget,
32109dba64beSDimitry Andric         "no-run-target",
32119dba64beSDimitry Andric         "Calculate the dynamic type of values, but don't run the target.",
32129dba64beSDimitry Andric     },
32139dba64beSDimitry Andric };
3214*0b57cec5SDimitry Andric 
3215*0b57cec5SDimitry Andric OptionEnumValues lldb_private::GetDynamicValueTypes() {
3216*0b57cec5SDimitry Andric   return OptionEnumValues(g_dynamic_value_types);
3217*0b57cec5SDimitry Andric }
3218*0b57cec5SDimitry Andric 
3219*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = {
32209dba64beSDimitry Andric     {
32219dba64beSDimitry Andric         eInlineBreakpointsNever,
32229dba64beSDimitry Andric         "never",
32239dba64beSDimitry Andric         "Never look for inline breakpoint locations (fastest). This setting "
32249dba64beSDimitry Andric         "should only be used if you know that no inlining occurs in your"
32259dba64beSDimitry Andric         "programs.",
32269dba64beSDimitry Andric     },
32279dba64beSDimitry Andric     {
32289dba64beSDimitry Andric         eInlineBreakpointsHeaders,
32299dba64beSDimitry Andric         "headers",
32309dba64beSDimitry Andric         "Only check for inline breakpoint locations when setting breakpoints "
32319dba64beSDimitry Andric         "in header files, but not when setting breakpoint in implementation "
32329dba64beSDimitry Andric         "source files (default).",
32339dba64beSDimitry Andric     },
32349dba64beSDimitry Andric     {
32359dba64beSDimitry Andric         eInlineBreakpointsAlways,
32369dba64beSDimitry Andric         "always",
32379dba64beSDimitry Andric         "Always look for inline breakpoint locations when setting file and "
32389dba64beSDimitry Andric         "line breakpoints (slower but most accurate).",
32399dba64beSDimitry Andric     },
32409dba64beSDimitry Andric };
3241*0b57cec5SDimitry Andric 
3242*0b57cec5SDimitry Andric enum x86DisassemblyFlavor {
3243*0b57cec5SDimitry Andric   eX86DisFlavorDefault,
3244*0b57cec5SDimitry Andric   eX86DisFlavorIntel,
3245*0b57cec5SDimitry Andric   eX86DisFlavorATT
3246*0b57cec5SDimitry Andric };
3247*0b57cec5SDimitry Andric 
3248*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = {
32499dba64beSDimitry Andric     {
32509dba64beSDimitry Andric         eX86DisFlavorDefault,
32519dba64beSDimitry Andric         "default",
32529dba64beSDimitry Andric         "Disassembler default (currently att).",
32539dba64beSDimitry Andric     },
32549dba64beSDimitry Andric     {
32559dba64beSDimitry Andric         eX86DisFlavorIntel,
32569dba64beSDimitry Andric         "intel",
32579dba64beSDimitry Andric         "Intel disassembler flavor.",
32589dba64beSDimitry Andric     },
32599dba64beSDimitry Andric     {
32609dba64beSDimitry Andric         eX86DisFlavorATT,
32619dba64beSDimitry Andric         "att",
32629dba64beSDimitry Andric         "AT&T disassembler flavor.",
32639dba64beSDimitry Andric     },
32649dba64beSDimitry Andric };
3265*0b57cec5SDimitry Andric 
3266*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = {
32679dba64beSDimitry Andric     {
32689dba64beSDimitry Andric         Disassembler::eHexStyleC,
32699dba64beSDimitry Andric         "c",
32709dba64beSDimitry Andric         "C-style (0xffff).",
32719dba64beSDimitry Andric     },
32729dba64beSDimitry Andric     {
32739dba64beSDimitry Andric         Disassembler::eHexStyleAsm,
32749dba64beSDimitry Andric         "asm",
32759dba64beSDimitry Andric         "Asm-style (0ffffh).",
32769dba64beSDimitry Andric     },
32779dba64beSDimitry Andric };
3278*0b57cec5SDimitry Andric 
3279*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = {
32809dba64beSDimitry Andric     {
32819dba64beSDimitry Andric         eLoadScriptFromSymFileTrue,
32829dba64beSDimitry Andric         "true",
32839dba64beSDimitry Andric         "Load debug scripts inside symbol files",
32849dba64beSDimitry Andric     },
32859dba64beSDimitry Andric     {
32869dba64beSDimitry Andric         eLoadScriptFromSymFileFalse,
32879dba64beSDimitry Andric         "false",
32889dba64beSDimitry Andric         "Do not load debug scripts inside symbol files.",
32899dba64beSDimitry Andric     },
32909dba64beSDimitry Andric     {
32919dba64beSDimitry Andric         eLoadScriptFromSymFileWarn,
32929dba64beSDimitry Andric         "warn",
32939dba64beSDimitry Andric         "Warn about debug scripts inside symbol files but do not load them.",
32949dba64beSDimitry Andric     },
32959dba64beSDimitry Andric };
3296*0b57cec5SDimitry Andric 
32979dba64beSDimitry Andric static constexpr OptionEnumValueElement g_load_cwd_lldbinit_values[] = {
32989dba64beSDimitry Andric     {
32999dba64beSDimitry Andric         eLoadCWDlldbinitTrue,
33009dba64beSDimitry Andric         "true",
33019dba64beSDimitry Andric         "Load .lldbinit files from current directory",
33029dba64beSDimitry Andric     },
33039dba64beSDimitry Andric     {
33049dba64beSDimitry Andric         eLoadCWDlldbinitFalse,
33059dba64beSDimitry Andric         "false",
33069dba64beSDimitry Andric         "Do not load .lldbinit files from current directory",
33079dba64beSDimitry Andric     },
33089dba64beSDimitry Andric     {
33099dba64beSDimitry Andric         eLoadCWDlldbinitWarn,
33109dba64beSDimitry Andric         "warn",
33119dba64beSDimitry Andric         "Warn about loading .lldbinit files from current directory",
33129dba64beSDimitry Andric     },
33139dba64beSDimitry Andric };
3314*0b57cec5SDimitry Andric 
3315*0b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = {
33169dba64beSDimitry Andric     {
33179dba64beSDimitry Andric         eMemoryModuleLoadLevelMinimal,
33189dba64beSDimitry Andric         "minimal",
3319*0b57cec5SDimitry Andric         "Load minimal information when loading modules from memory. Currently "
33209dba64beSDimitry Andric         "this setting loads sections only.",
33219dba64beSDimitry Andric     },
33229dba64beSDimitry Andric     {
33239dba64beSDimitry Andric         eMemoryModuleLoadLevelPartial,
33249dba64beSDimitry Andric         "partial",
3325*0b57cec5SDimitry Andric         "Load partial information when loading modules from memory. Currently "
33269dba64beSDimitry Andric         "this setting loads sections and function bounds.",
33279dba64beSDimitry Andric     },
33289dba64beSDimitry Andric     {
33299dba64beSDimitry Andric         eMemoryModuleLoadLevelComplete,
33309dba64beSDimitry Andric         "complete",
3331*0b57cec5SDimitry Andric         "Load complete information when loading modules from memory. Currently "
33329dba64beSDimitry Andric         "this setting loads sections and all symbols.",
33339dba64beSDimitry Andric     },
33349dba64beSDimitry Andric };
3335*0b57cec5SDimitry Andric 
33369dba64beSDimitry Andric #define LLDB_PROPERTIES_target
33379dba64beSDimitry Andric #include "TargetProperties.inc"
3338*0b57cec5SDimitry Andric 
3339*0b57cec5SDimitry Andric enum {
33409dba64beSDimitry Andric #define LLDB_PROPERTIES_target
33419dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
3342*0b57cec5SDimitry Andric   ePropertyExperimental,
3343*0b57cec5SDimitry Andric };
3344*0b57cec5SDimitry Andric 
3345*0b57cec5SDimitry Andric class TargetOptionValueProperties : public OptionValueProperties {
3346*0b57cec5SDimitry Andric public:
33475ffd83dbSDimitry Andric   TargetOptionValueProperties(ConstString name) : OptionValueProperties(name) {}
3348*0b57cec5SDimitry Andric 
3349*0b57cec5SDimitry Andric   // This constructor is used when creating TargetOptionValueProperties when it
3350*0b57cec5SDimitry Andric   // is part of a new lldb_private::Target instance. It will copy all current
3351*0b57cec5SDimitry Andric   // global property values as needed
33525ffd83dbSDimitry Andric   TargetOptionValueProperties(const TargetPropertiesSP &target_properties_sp)
33535ffd83dbSDimitry Andric       : OptionValueProperties(*target_properties_sp->GetValueProperties()) {}
3354*0b57cec5SDimitry Andric 
3355*0b57cec5SDimitry Andric   const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
3356*0b57cec5SDimitry Andric                                      bool will_modify,
3357*0b57cec5SDimitry Andric                                      uint32_t idx) const override {
3358*0b57cec5SDimitry Andric     // When getting the value for a key from the target options, we will always
3359*0b57cec5SDimitry Andric     // try and grab the setting from the current target if there is one. Else
3360*0b57cec5SDimitry Andric     // we just use the one from this instance.
3361*0b57cec5SDimitry Andric     if (exe_ctx) {
3362*0b57cec5SDimitry Andric       Target *target = exe_ctx->GetTargetPtr();
3363*0b57cec5SDimitry Andric       if (target) {
3364*0b57cec5SDimitry Andric         TargetOptionValueProperties *target_properties =
3365*0b57cec5SDimitry Andric             static_cast<TargetOptionValueProperties *>(
3366*0b57cec5SDimitry Andric                 target->GetValueProperties().get());
3367*0b57cec5SDimitry Andric         if (this != target_properties)
3368*0b57cec5SDimitry Andric           return target_properties->ProtectedGetPropertyAtIndex(idx);
3369*0b57cec5SDimitry Andric       }
3370*0b57cec5SDimitry Andric     }
3371*0b57cec5SDimitry Andric     return ProtectedGetPropertyAtIndex(idx);
3372*0b57cec5SDimitry Andric   }
3373*0b57cec5SDimitry Andric };
3374*0b57cec5SDimitry Andric 
3375*0b57cec5SDimitry Andric // TargetProperties
33765ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
33779dba64beSDimitry Andric #include "TargetProperties.inc"
3378*0b57cec5SDimitry Andric 
33799dba64beSDimitry Andric enum {
33805ffd83dbSDimitry Andric #define LLDB_PROPERTIES_target_experimental
33819dba64beSDimitry Andric #include "TargetPropertiesEnum.inc"
33829dba64beSDimitry Andric };
3383*0b57cec5SDimitry Andric 
3384*0b57cec5SDimitry Andric class TargetExperimentalOptionValueProperties : public OptionValueProperties {
3385*0b57cec5SDimitry Andric public:
3386*0b57cec5SDimitry Andric   TargetExperimentalOptionValueProperties()
3387*0b57cec5SDimitry Andric       : OptionValueProperties(
3388*0b57cec5SDimitry Andric             ConstString(Properties::GetExperimentalSettingsName())) {}
3389*0b57cec5SDimitry Andric };
3390*0b57cec5SDimitry Andric 
3391*0b57cec5SDimitry Andric TargetExperimentalProperties::TargetExperimentalProperties()
3392*0b57cec5SDimitry Andric     : Properties(OptionValuePropertiesSP(
3393*0b57cec5SDimitry Andric           new TargetExperimentalOptionValueProperties())) {
33945ffd83dbSDimitry Andric   m_collection_sp->Initialize(g_target_experimental_properties);
3395*0b57cec5SDimitry Andric }
3396*0b57cec5SDimitry Andric 
3397*0b57cec5SDimitry Andric // TargetProperties
3398*0b57cec5SDimitry Andric TargetProperties::TargetProperties(Target *target)
33995ffd83dbSDimitry Andric     : Properties(), m_launch_info(), m_target(target) {
3400*0b57cec5SDimitry Andric   if (target) {
3401*0b57cec5SDimitry Andric     m_collection_sp = std::make_shared<TargetOptionValueProperties>(
34025ffd83dbSDimitry Andric         Target::GetGlobalProperties());
3403*0b57cec5SDimitry Andric 
3404*0b57cec5SDimitry Andric     // Set callbacks to update launch_info whenever "settins set" updated any
3405*0b57cec5SDimitry Andric     // of these properties
3406*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3407480093f4SDimitry Andric         ePropertyArg0, [this] { Arg0ValueChangedCallback(); });
3408*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3409480093f4SDimitry Andric         ePropertyRunArgs, [this] { RunArgsValueChangedCallback(); });
3410*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3411480093f4SDimitry Andric         ePropertyEnvVars, [this] { EnvVarsValueChangedCallback(); });
3412*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
34135ffd83dbSDimitry Andric         ePropertyUnsetEnvVars, [this] { EnvVarsValueChangedCallback(); });
34145ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
34155ffd83dbSDimitry Andric         ePropertyInheritEnv, [this] { EnvVarsValueChangedCallback(); });
34165ffd83dbSDimitry Andric     m_collection_sp->SetValueChangedCallback(
3417480093f4SDimitry Andric         ePropertyInputPath, [this] { InputPathValueChangedCallback(); });
3418*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3419480093f4SDimitry Andric         ePropertyOutputPath, [this] { OutputPathValueChangedCallback(); });
3420*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3421480093f4SDimitry Andric         ePropertyErrorPath, [this] { ErrorPathValueChangedCallback(); });
3422480093f4SDimitry Andric     m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, [this] {
3423480093f4SDimitry Andric       DetachOnErrorValueChangedCallback();
3424480093f4SDimitry Andric     });
3425*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3426480093f4SDimitry Andric         ePropertyDisableASLR, [this] { DisableASLRValueChangedCallback(); });
3427*0b57cec5SDimitry Andric     m_collection_sp->SetValueChangedCallback(
3428480093f4SDimitry Andric         ePropertyDisableSTDIO, [this] { DisableSTDIOValueChangedCallback(); });
3429*0b57cec5SDimitry Andric 
34305ffd83dbSDimitry Andric     m_experimental_properties_up =
34315ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
3432*0b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
3433*0b57cec5SDimitry Andric         ConstString(Properties::GetExperimentalSettingsName()),
3434*0b57cec5SDimitry Andric         ConstString("Experimental settings - setting these won't produce "
3435*0b57cec5SDimitry Andric                     "errors if the setting is not present."),
3436*0b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
3437*0b57cec5SDimitry Andric   } else {
3438*0b57cec5SDimitry Andric     m_collection_sp =
3439*0b57cec5SDimitry Andric         std::make_shared<TargetOptionValueProperties>(ConstString("target"));
34409dba64beSDimitry Andric     m_collection_sp->Initialize(g_target_properties);
34415ffd83dbSDimitry Andric     m_experimental_properties_up =
34425ffd83dbSDimitry Andric         std::make_unique<TargetExperimentalProperties>();
3443*0b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
3444*0b57cec5SDimitry Andric         ConstString(Properties::GetExperimentalSettingsName()),
3445*0b57cec5SDimitry Andric         ConstString("Experimental settings - setting these won't produce "
3446*0b57cec5SDimitry Andric                     "errors if the setting is not present."),
3447*0b57cec5SDimitry Andric         true, m_experimental_properties_up->GetValueProperties());
3448*0b57cec5SDimitry Andric     m_collection_sp->AppendProperty(
3449*0b57cec5SDimitry Andric         ConstString("process"), ConstString("Settings specific to processes."),
3450*0b57cec5SDimitry Andric         true, Process::GetGlobalProperties()->GetValueProperties());
3451*0b57cec5SDimitry Andric   }
3452*0b57cec5SDimitry Andric }
3453*0b57cec5SDimitry Andric 
3454*0b57cec5SDimitry Andric TargetProperties::~TargetProperties() = default;
3455*0b57cec5SDimitry Andric 
34565ffd83dbSDimitry Andric void TargetProperties::UpdateLaunchInfoFromProperties() {
34575ffd83dbSDimitry Andric   Arg0ValueChangedCallback();
34585ffd83dbSDimitry Andric   RunArgsValueChangedCallback();
34595ffd83dbSDimitry Andric   EnvVarsValueChangedCallback();
34605ffd83dbSDimitry Andric   InputPathValueChangedCallback();
34615ffd83dbSDimitry Andric   OutputPathValueChangedCallback();
34625ffd83dbSDimitry Andric   ErrorPathValueChangedCallback();
34635ffd83dbSDimitry Andric   DetachOnErrorValueChangedCallback();
34645ffd83dbSDimitry Andric   DisableASLRValueChangedCallback();
34655ffd83dbSDimitry Andric   DisableSTDIOValueChangedCallback();
34665ffd83dbSDimitry Andric }
34675ffd83dbSDimitry Andric 
3468*0b57cec5SDimitry Andric bool TargetProperties::GetInjectLocalVariables(
3469*0b57cec5SDimitry Andric     ExecutionContext *exe_ctx) const {
3470*0b57cec5SDimitry Andric   const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
3471*0b57cec5SDimitry Andric       exe_ctx, false, ePropertyExperimental);
3472*0b57cec5SDimitry Andric   OptionValueProperties *exp_values =
3473*0b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
3474*0b57cec5SDimitry Andric   if (exp_values)
3475*0b57cec5SDimitry Andric     return exp_values->GetPropertyAtIndexAsBoolean(
3476*0b57cec5SDimitry Andric         exe_ctx, ePropertyInjectLocalVars, true);
3477*0b57cec5SDimitry Andric   else
3478*0b57cec5SDimitry Andric     return true;
3479*0b57cec5SDimitry Andric }
3480*0b57cec5SDimitry Andric 
3481*0b57cec5SDimitry Andric void TargetProperties::SetInjectLocalVariables(ExecutionContext *exe_ctx,
3482*0b57cec5SDimitry Andric                                                bool b) {
3483*0b57cec5SDimitry Andric   const Property *exp_property =
3484*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndex(exe_ctx, true, ePropertyExperimental);
3485*0b57cec5SDimitry Andric   OptionValueProperties *exp_values =
3486*0b57cec5SDimitry Andric       exp_property->GetValue()->GetAsProperties();
3487*0b57cec5SDimitry Andric   if (exp_values)
3488*0b57cec5SDimitry Andric     exp_values->SetPropertyAtIndexAsBoolean(exe_ctx, ePropertyInjectLocalVars,
3489*0b57cec5SDimitry Andric                                             true);
3490*0b57cec5SDimitry Andric }
3491*0b57cec5SDimitry Andric 
3492*0b57cec5SDimitry Andric ArchSpec TargetProperties::GetDefaultArchitecture() const {
3493*0b57cec5SDimitry Andric   OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
3494*0b57cec5SDimitry Andric       nullptr, ePropertyDefaultArch);
3495*0b57cec5SDimitry Andric   if (value)
3496*0b57cec5SDimitry Andric     return value->GetCurrentValue();
3497*0b57cec5SDimitry Andric   return ArchSpec();
3498*0b57cec5SDimitry Andric }
3499*0b57cec5SDimitry Andric 
3500*0b57cec5SDimitry Andric void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) {
3501*0b57cec5SDimitry Andric   OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
3502*0b57cec5SDimitry Andric       nullptr, ePropertyDefaultArch);
3503*0b57cec5SDimitry Andric   if (value)
3504*0b57cec5SDimitry Andric     return value->SetCurrentValue(arch, true);
3505*0b57cec5SDimitry Andric }
3506*0b57cec5SDimitry Andric 
3507*0b57cec5SDimitry Andric bool TargetProperties::GetMoveToNearestCode() const {
3508*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyMoveToNearestCode;
3509*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35109dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3511*0b57cec5SDimitry Andric }
3512*0b57cec5SDimitry Andric 
3513*0b57cec5SDimitry Andric lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const {
3514*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
3515*0b57cec5SDimitry Andric   return (lldb::DynamicValueType)
3516*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
35179dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
3518*0b57cec5SDimitry Andric }
3519*0b57cec5SDimitry Andric 
3520*0b57cec5SDimitry Andric bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
3521*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreferDynamic;
3522*0b57cec5SDimitry Andric   return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
3523*0b57cec5SDimitry Andric }
3524*0b57cec5SDimitry Andric 
3525*0b57cec5SDimitry Andric bool TargetProperties::GetPreloadSymbols() const {
3526*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
3527*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35289dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3529*0b57cec5SDimitry Andric }
3530*0b57cec5SDimitry Andric 
3531*0b57cec5SDimitry Andric void TargetProperties::SetPreloadSymbols(bool b) {
3532*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyPreloadSymbols;
3533*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3534*0b57cec5SDimitry Andric }
3535*0b57cec5SDimitry Andric 
3536*0b57cec5SDimitry Andric bool TargetProperties::GetDisableASLR() const {
3537*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
3538*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35399dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3540*0b57cec5SDimitry Andric }
3541*0b57cec5SDimitry Andric 
3542*0b57cec5SDimitry Andric void TargetProperties::SetDisableASLR(bool b) {
3543*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableASLR;
3544*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3545*0b57cec5SDimitry Andric }
3546*0b57cec5SDimitry Andric 
3547*0b57cec5SDimitry Andric bool TargetProperties::GetDetachOnError() const {
3548*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
3549*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35509dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3551*0b57cec5SDimitry Andric }
3552*0b57cec5SDimitry Andric 
3553*0b57cec5SDimitry Andric void TargetProperties::SetDetachOnError(bool b) {
3554*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDetachOnError;
3555*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3556*0b57cec5SDimitry Andric }
3557*0b57cec5SDimitry Andric 
3558*0b57cec5SDimitry Andric bool TargetProperties::GetDisableSTDIO() const {
3559*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
3560*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
35619dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3562*0b57cec5SDimitry Andric }
3563*0b57cec5SDimitry Andric 
3564*0b57cec5SDimitry Andric void TargetProperties::SetDisableSTDIO(bool b) {
3565*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisableSTDIO;
3566*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3567*0b57cec5SDimitry Andric }
3568*0b57cec5SDimitry Andric 
3569*0b57cec5SDimitry Andric const char *TargetProperties::GetDisassemblyFlavor() const {
3570*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisassemblyFlavor;
3571*0b57cec5SDimitry Andric   const char *return_value;
3572*0b57cec5SDimitry Andric 
3573*0b57cec5SDimitry Andric   x86DisassemblyFlavor flavor_value =
3574*0b57cec5SDimitry Andric       (x86DisassemblyFlavor)m_collection_sp->GetPropertyAtIndexAsEnumeration(
35759dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
3576*0b57cec5SDimitry Andric   return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3577*0b57cec5SDimitry Andric   return return_value;
3578*0b57cec5SDimitry Andric }
3579*0b57cec5SDimitry Andric 
3580*0b57cec5SDimitry Andric InlineStrategy TargetProperties::GetInlineStrategy() const {
3581*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyInlineStrategy;
3582*0b57cec5SDimitry Andric   return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(
35839dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3584*0b57cec5SDimitry Andric }
3585*0b57cec5SDimitry Andric 
3586*0b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetArg0() const {
3587*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
35889dba64beSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx,
35899dba64beSDimitry Andric                                                      llvm::StringRef());
3590*0b57cec5SDimitry Andric }
3591*0b57cec5SDimitry Andric 
3592*0b57cec5SDimitry Andric void TargetProperties::SetArg0(llvm::StringRef arg) {
3593*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyArg0;
35949dba64beSDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
3595*0b57cec5SDimitry Andric   m_launch_info.SetArg0(arg);
3596*0b57cec5SDimitry Andric }
3597*0b57cec5SDimitry Andric 
3598*0b57cec5SDimitry Andric bool TargetProperties::GetRunArguments(Args &args) const {
3599*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
3600*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
3601*0b57cec5SDimitry Andric }
3602*0b57cec5SDimitry Andric 
3603*0b57cec5SDimitry Andric void TargetProperties::SetRunArguments(const Args &args) {
3604*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyRunArgs;
3605*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
3606*0b57cec5SDimitry Andric   m_launch_info.GetArguments() = args;
3607*0b57cec5SDimitry Andric }
3608*0b57cec5SDimitry Andric 
36095ffd83dbSDimitry Andric Environment TargetProperties::ComputeEnvironment() const {
36105ffd83dbSDimitry Andric   Environment env;
36115ffd83dbSDimitry Andric 
36125ffd83dbSDimitry Andric   if (m_target &&
36135ffd83dbSDimitry Andric       m_collection_sp->GetPropertyAtIndexAsBoolean(
36145ffd83dbSDimitry Andric           nullptr, ePropertyInheritEnv,
36155ffd83dbSDimitry Andric           g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) {
36165ffd83dbSDimitry Andric     if (auto platform_sp = m_target->GetPlatform()) {
36175ffd83dbSDimitry Andric       Environment platform_env = platform_sp->GetEnvironment();
36185ffd83dbSDimitry Andric       for (const auto &KV : platform_env)
36195ffd83dbSDimitry Andric         env[KV.first()] = KV.second;
36205ffd83dbSDimitry Andric     }
36215ffd83dbSDimitry Andric   }
36225ffd83dbSDimitry Andric 
36235ffd83dbSDimitry Andric   Args property_unset_env;
36245ffd83dbSDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, ePropertyUnsetEnvVars,
36255ffd83dbSDimitry Andric                                             property_unset_env);
36265ffd83dbSDimitry Andric   for (const auto &var : property_unset_env)
36275ffd83dbSDimitry Andric     env.erase(var.ref());
36285ffd83dbSDimitry Andric 
36295ffd83dbSDimitry Andric   Args property_env;
36305ffd83dbSDimitry Andric   m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, ePropertyEnvVars,
36315ffd83dbSDimitry Andric                                             property_env);
36325ffd83dbSDimitry Andric   for (const auto &KV : Environment(property_env))
36335ffd83dbSDimitry Andric     env[KV.first()] = KV.second;
36345ffd83dbSDimitry Andric 
36355ffd83dbSDimitry Andric   return env;
36365ffd83dbSDimitry Andric }
36375ffd83dbSDimitry Andric 
3638*0b57cec5SDimitry Andric Environment TargetProperties::GetEnvironment() const {
36395ffd83dbSDimitry Andric   return ComputeEnvironment();
3640*0b57cec5SDimitry Andric }
3641*0b57cec5SDimitry Andric 
3642*0b57cec5SDimitry Andric void TargetProperties::SetEnvironment(Environment env) {
3643*0b57cec5SDimitry Andric   // TODO: Get rid of the Args intermediate step
3644*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnvVars;
3645*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, Args(env));
3646*0b57cec5SDimitry Andric }
3647*0b57cec5SDimitry Andric 
3648*0b57cec5SDimitry Andric bool TargetProperties::GetSkipPrologue() const {
3649*0b57cec5SDimitry Andric   const uint32_t idx = ePropertySkipPrologue;
3650*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
36519dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3652*0b57cec5SDimitry Andric }
3653*0b57cec5SDimitry Andric 
3654*0b57cec5SDimitry Andric PathMappingList &TargetProperties::GetSourcePathMap() const {
3655*0b57cec5SDimitry Andric   const uint32_t idx = ePropertySourceMap;
3656*0b57cec5SDimitry Andric   OptionValuePathMappings *option_value =
3657*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr,
3658*0b57cec5SDimitry Andric                                                                    false, idx);
3659*0b57cec5SDimitry Andric   assert(option_value);
3660*0b57cec5SDimitry Andric   return option_value->GetCurrentValue();
3661*0b57cec5SDimitry Andric }
3662*0b57cec5SDimitry Andric 
3663*0b57cec5SDimitry Andric void TargetProperties::AppendExecutableSearchPaths(const FileSpec &dir) {
3664*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
3665*0b57cec5SDimitry Andric   OptionValueFileSpecList *option_value =
3666*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3667*0b57cec5SDimitry Andric                                                                    false, idx);
3668*0b57cec5SDimitry Andric   assert(option_value);
3669*0b57cec5SDimitry Andric   option_value->AppendCurrentValue(dir);
3670*0b57cec5SDimitry Andric }
3671*0b57cec5SDimitry Andric 
3672*0b57cec5SDimitry Andric FileSpecList TargetProperties::GetExecutableSearchPaths() {
3673*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyExecutableSearchPaths;
3674*0b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
3675*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3676*0b57cec5SDimitry Andric                                                                    false, idx);
3677*0b57cec5SDimitry Andric   assert(option_value);
3678*0b57cec5SDimitry Andric   return option_value->GetCurrentValue();
3679*0b57cec5SDimitry Andric }
3680*0b57cec5SDimitry Andric 
3681*0b57cec5SDimitry Andric FileSpecList TargetProperties::GetDebugFileSearchPaths() {
3682*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDebugFileSearchPaths;
3683*0b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
3684*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3685*0b57cec5SDimitry Andric                                                                    false, idx);
3686*0b57cec5SDimitry Andric   assert(option_value);
3687*0b57cec5SDimitry Andric   return option_value->GetCurrentValue();
3688*0b57cec5SDimitry Andric }
3689*0b57cec5SDimitry Andric 
3690*0b57cec5SDimitry Andric FileSpecList TargetProperties::GetClangModuleSearchPaths() {
3691*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyClangModuleSearchPaths;
3692*0b57cec5SDimitry Andric   const OptionValueFileSpecList *option_value =
3693*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3694*0b57cec5SDimitry Andric                                                                    false, idx);
3695*0b57cec5SDimitry Andric   assert(option_value);
3696*0b57cec5SDimitry Andric   return option_value->GetCurrentValue();
3697*0b57cec5SDimitry Andric }
3698*0b57cec5SDimitry Andric 
3699*0b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoImportClangModules() const {
3700*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoImportClangModules;
3701*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37029dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3703*0b57cec5SDimitry Andric }
3704*0b57cec5SDimitry Andric 
3705*0b57cec5SDimitry Andric bool TargetProperties::GetEnableImportStdModule() const {
3706*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyImportStdModule;
3707*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37089dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3709*0b57cec5SDimitry Andric }
3710*0b57cec5SDimitry Andric 
3711*0b57cec5SDimitry Andric bool TargetProperties::GetEnableAutoApplyFixIts() const {
3712*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyAutoApplyFixIts;
3713*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37149dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3715*0b57cec5SDimitry Andric }
3716*0b57cec5SDimitry Andric 
37175ffd83dbSDimitry Andric uint64_t TargetProperties::GetNumberOfRetriesWithFixits() const {
37185ffd83dbSDimitry Andric   const uint32_t idx = ePropertyRetriesWithFixIts;
37195ffd83dbSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsUInt64(
37205ffd83dbSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
37215ffd83dbSDimitry Andric }
37225ffd83dbSDimitry Andric 
3723*0b57cec5SDimitry Andric bool TargetProperties::GetEnableNotifyAboutFixIts() const {
3724*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyNotifyAboutFixIts;
3725*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37269dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3727*0b57cec5SDimitry Andric }
3728*0b57cec5SDimitry Andric 
3729*0b57cec5SDimitry Andric bool TargetProperties::GetEnableSaveObjects() const {
3730*0b57cec5SDimitry Andric   const uint32_t idx = ePropertySaveObjects;
3731*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37329dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3733*0b57cec5SDimitry Andric }
3734*0b57cec5SDimitry Andric 
3735*0b57cec5SDimitry Andric bool TargetProperties::GetEnableSyntheticValue() const {
3736*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyEnableSynthetic;
3737*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
37389dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
37399dba64beSDimitry Andric }
37409dba64beSDimitry Andric 
37419dba64beSDimitry Andric uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
37429dba64beSDimitry Andric   const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
37439dba64beSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsUInt64(
37449dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3745*0b57cec5SDimitry Andric }
3746*0b57cec5SDimitry Andric 
3747*0b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const {
3748*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxChildrenCount;
3749*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37509dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3751*0b57cec5SDimitry Andric }
3752*0b57cec5SDimitry Andric 
3753*0b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const {
3754*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxSummaryLength;
3755*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37569dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3757*0b57cec5SDimitry Andric }
3758*0b57cec5SDimitry Andric 
3759*0b57cec5SDimitry Andric uint32_t TargetProperties::GetMaximumMemReadSize() const {
3760*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyMaxMemReadSize;
3761*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsSInt64(
37629dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3763*0b57cec5SDimitry Andric }
3764*0b57cec5SDimitry Andric 
3765*0b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardInputPath() const {
3766*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
3767*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
3768*0b57cec5SDimitry Andric }
3769*0b57cec5SDimitry Andric 
3770*0b57cec5SDimitry Andric void TargetProperties::SetStandardInputPath(llvm::StringRef path) {
3771*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyInputPath;
3772*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
3773*0b57cec5SDimitry Andric }
3774*0b57cec5SDimitry Andric 
3775*0b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardOutputPath() const {
3776*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
3777*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
3778*0b57cec5SDimitry Andric }
3779*0b57cec5SDimitry Andric 
3780*0b57cec5SDimitry Andric void TargetProperties::SetStandardOutputPath(llvm::StringRef path) {
3781*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyOutputPath;
3782*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
3783*0b57cec5SDimitry Andric }
3784*0b57cec5SDimitry Andric 
3785*0b57cec5SDimitry Andric FileSpec TargetProperties::GetStandardErrorPath() const {
3786*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
3787*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
3788*0b57cec5SDimitry Andric }
3789*0b57cec5SDimitry Andric 
3790*0b57cec5SDimitry Andric void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
3791*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyErrorPath;
3792*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
3793*0b57cec5SDimitry Andric }
3794*0b57cec5SDimitry Andric 
3795*0b57cec5SDimitry Andric LanguageType TargetProperties::GetLanguage() const {
3796*0b57cec5SDimitry Andric   OptionValueLanguage *value =
3797*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(
3798*0b57cec5SDimitry Andric           nullptr, ePropertyLanguage);
3799*0b57cec5SDimitry Andric   if (value)
3800*0b57cec5SDimitry Andric     return value->GetCurrentValue();
3801*0b57cec5SDimitry Andric   return LanguageType();
3802*0b57cec5SDimitry Andric }
3803*0b57cec5SDimitry Andric 
3804*0b57cec5SDimitry Andric llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
3805*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyExprPrefix;
3806*0b57cec5SDimitry Andric   OptionValueFileSpec *file =
3807*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
3808*0b57cec5SDimitry Andric                                                                idx);
3809*0b57cec5SDimitry Andric   if (file) {
3810*0b57cec5SDimitry Andric     DataBufferSP data_sp(file->GetFileContents());
3811*0b57cec5SDimitry Andric     if (data_sp)
3812*0b57cec5SDimitry Andric       return llvm::StringRef(
3813*0b57cec5SDimitry Andric           reinterpret_cast<const char *>(data_sp->GetBytes()),
3814*0b57cec5SDimitry Andric           data_sp->GetByteSize());
3815*0b57cec5SDimitry Andric   }
3816*0b57cec5SDimitry Andric   return "";
3817*0b57cec5SDimitry Andric }
3818*0b57cec5SDimitry Andric 
3819*0b57cec5SDimitry Andric bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() {
3820*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyBreakpointUseAvoidList;
3821*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38229dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3823*0b57cec5SDimitry Andric }
3824*0b57cec5SDimitry Andric 
3825*0b57cec5SDimitry Andric bool TargetProperties::GetUseHexImmediates() const {
3826*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseHexImmediates;
3827*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38289dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3829*0b57cec5SDimitry Andric }
3830*0b57cec5SDimitry Andric 
3831*0b57cec5SDimitry Andric bool TargetProperties::GetUseFastStepping() const {
3832*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyUseFastStepping;
3833*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38349dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3835*0b57cec5SDimitry Andric }
3836*0b57cec5SDimitry Andric 
3837*0b57cec5SDimitry Andric bool TargetProperties::GetDisplayExpressionsInCrashlogs() const {
3838*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3839*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
38409dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3841*0b57cec5SDimitry Andric }
3842*0b57cec5SDimitry Andric 
3843*0b57cec5SDimitry Andric LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const {
3844*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
3845*0b57cec5SDimitry Andric   return (LoadScriptFromSymFile)
3846*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38479dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
3848*0b57cec5SDimitry Andric }
3849*0b57cec5SDimitry Andric 
3850*0b57cec5SDimitry Andric LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const {
3851*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyLoadCWDlldbinitFile;
3852*0b57cec5SDimitry Andric   return (LoadCWDlldbinitFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(
38539dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value);
3854*0b57cec5SDimitry Andric }
3855*0b57cec5SDimitry Andric 
3856*0b57cec5SDimitry Andric Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const {
3857*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyHexImmediateStyle;
3858*0b57cec5SDimitry Andric   return (Disassembler::HexImmediateStyle)
3859*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38609dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
3861*0b57cec5SDimitry Andric }
3862*0b57cec5SDimitry Andric 
3863*0b57cec5SDimitry Andric MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const {
3864*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3865*0b57cec5SDimitry Andric   return (MemoryModuleLoadLevel)
3866*0b57cec5SDimitry Andric       m_collection_sp->GetPropertyAtIndexAsEnumeration(
38679dba64beSDimitry Andric           nullptr, idx, g_target_properties[idx].default_uint_value);
3868*0b57cec5SDimitry Andric }
3869*0b57cec5SDimitry Andric 
3870*0b57cec5SDimitry Andric bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const {
3871*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
3872*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
3873*0b57cec5SDimitry Andric }
3874*0b57cec5SDimitry Andric 
3875*0b57cec5SDimitry Andric void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) {
3876*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyTrapHandlerNames;
3877*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
3878*0b57cec5SDimitry Andric }
3879*0b57cec5SDimitry Andric 
3880*0b57cec5SDimitry Andric bool TargetProperties::GetDisplayRuntimeSupportValues() const {
3881*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3882*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
3883*0b57cec5SDimitry Andric }
3884*0b57cec5SDimitry Andric 
3885*0b57cec5SDimitry Andric void TargetProperties::SetDisplayRuntimeSupportValues(bool b) {
3886*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3887*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3888*0b57cec5SDimitry Andric }
3889*0b57cec5SDimitry Andric 
3890*0b57cec5SDimitry Andric bool TargetProperties::GetDisplayRecognizedArguments() const {
3891*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
3892*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
3893*0b57cec5SDimitry Andric }
3894*0b57cec5SDimitry Andric 
3895*0b57cec5SDimitry Andric void TargetProperties::SetDisplayRecognizedArguments(bool b) {
3896*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyDisplayRecognizedArguments;
3897*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3898*0b57cec5SDimitry Andric }
3899*0b57cec5SDimitry Andric 
3900*0b57cec5SDimitry Andric bool TargetProperties::GetNonStopModeEnabled() const {
3901*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyNonStopModeEnabled;
3902*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
3903*0b57cec5SDimitry Andric }
3904*0b57cec5SDimitry Andric 
3905*0b57cec5SDimitry Andric void TargetProperties::SetNonStopModeEnabled(bool b) {
3906*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyNonStopModeEnabled;
3907*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3908*0b57cec5SDimitry Andric }
3909*0b57cec5SDimitry Andric 
3910*0b57cec5SDimitry Andric const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() {
3911*0b57cec5SDimitry Andric   m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
3912*0b57cec5SDimitry Andric   return m_launch_info;
3913*0b57cec5SDimitry Andric }
3914*0b57cec5SDimitry Andric 
3915*0b57cec5SDimitry Andric void TargetProperties::SetProcessLaunchInfo(
3916*0b57cec5SDimitry Andric     const ProcessLaunchInfo &launch_info) {
3917*0b57cec5SDimitry Andric   m_launch_info = launch_info;
3918*0b57cec5SDimitry Andric   SetArg0(launch_info.GetArg0());
3919*0b57cec5SDimitry Andric   SetRunArguments(launch_info.GetArguments());
3920*0b57cec5SDimitry Andric   SetEnvironment(launch_info.GetEnvironment());
3921*0b57cec5SDimitry Andric   const FileAction *input_file_action =
3922*0b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDIN_FILENO);
3923*0b57cec5SDimitry Andric   if (input_file_action) {
3924*0b57cec5SDimitry Andric     SetStandardInputPath(input_file_action->GetPath());
3925*0b57cec5SDimitry Andric   }
3926*0b57cec5SDimitry Andric   const FileAction *output_file_action =
3927*0b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDOUT_FILENO);
3928*0b57cec5SDimitry Andric   if (output_file_action) {
3929*0b57cec5SDimitry Andric     SetStandardOutputPath(output_file_action->GetPath());
3930*0b57cec5SDimitry Andric   }
3931*0b57cec5SDimitry Andric   const FileAction *error_file_action =
3932*0b57cec5SDimitry Andric       launch_info.GetFileActionForFD(STDERR_FILENO);
3933*0b57cec5SDimitry Andric   if (error_file_action) {
3934*0b57cec5SDimitry Andric     SetStandardErrorPath(error_file_action->GetPath());
3935*0b57cec5SDimitry Andric   }
3936*0b57cec5SDimitry Andric   SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3937*0b57cec5SDimitry Andric   SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3938*0b57cec5SDimitry Andric   SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3939*0b57cec5SDimitry Andric }
3940*0b57cec5SDimitry Andric 
3941*0b57cec5SDimitry Andric bool TargetProperties::GetRequireHardwareBreakpoints() const {
3942*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
3943*0b57cec5SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
39449dba64beSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
3945*0b57cec5SDimitry Andric }
3946*0b57cec5SDimitry Andric 
3947*0b57cec5SDimitry Andric void TargetProperties::SetRequireHardwareBreakpoints(bool b) {
3948*0b57cec5SDimitry Andric   const uint32_t idx = ePropertyRequireHardwareBreakpoints;
3949*0b57cec5SDimitry Andric   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3950*0b57cec5SDimitry Andric }
3951*0b57cec5SDimitry Andric 
39525ffd83dbSDimitry Andric bool TargetProperties::GetAutoInstallMainExecutable() const {
39535ffd83dbSDimitry Andric   const uint32_t idx = ePropertyAutoInstallMainExecutable;
39545ffd83dbSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
39555ffd83dbSDimitry Andric       nullptr, idx, g_target_properties[idx].default_uint_value != 0);
39565ffd83dbSDimitry Andric }
39575ffd83dbSDimitry Andric 
3958480093f4SDimitry Andric void TargetProperties::Arg0ValueChangedCallback() {
3959480093f4SDimitry Andric   m_launch_info.SetArg0(GetArg0());
3960*0b57cec5SDimitry Andric }
3961*0b57cec5SDimitry Andric 
3962480093f4SDimitry Andric void TargetProperties::RunArgsValueChangedCallback() {
3963*0b57cec5SDimitry Andric   Args args;
3964480093f4SDimitry Andric   if (GetRunArguments(args))
3965480093f4SDimitry Andric     m_launch_info.GetArguments() = args;
3966*0b57cec5SDimitry Andric }
3967*0b57cec5SDimitry Andric 
3968480093f4SDimitry Andric void TargetProperties::EnvVarsValueChangedCallback() {
39695ffd83dbSDimitry Andric   m_launch_info.GetEnvironment() = ComputeEnvironment();
3970*0b57cec5SDimitry Andric }
3971*0b57cec5SDimitry Andric 
3972480093f4SDimitry Andric void TargetProperties::InputPathValueChangedCallback() {
3973480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDIN_FILENO, GetStandardInputPath(), true,
3974480093f4SDimitry Andric                                      false);
3975*0b57cec5SDimitry Andric }
3976*0b57cec5SDimitry Andric 
3977480093f4SDimitry Andric void TargetProperties::OutputPathValueChangedCallback() {
3978480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDOUT_FILENO, GetStandardOutputPath(),
3979480093f4SDimitry Andric                                      false, true);
3980*0b57cec5SDimitry Andric }
3981*0b57cec5SDimitry Andric 
3982480093f4SDimitry Andric void TargetProperties::ErrorPathValueChangedCallback() {
3983480093f4SDimitry Andric   m_launch_info.AppendOpenFileAction(STDERR_FILENO, GetStandardErrorPath(),
3984480093f4SDimitry Andric                                      false, true);
3985*0b57cec5SDimitry Andric }
3986*0b57cec5SDimitry Andric 
3987480093f4SDimitry Andric void TargetProperties::DetachOnErrorValueChangedCallback() {
3988480093f4SDimitry Andric   if (GetDetachOnError())
3989480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3990*0b57cec5SDimitry Andric   else
3991480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3992*0b57cec5SDimitry Andric }
3993*0b57cec5SDimitry Andric 
3994480093f4SDimitry Andric void TargetProperties::DisableASLRValueChangedCallback() {
3995480093f4SDimitry Andric   if (GetDisableASLR())
3996480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3997*0b57cec5SDimitry Andric   else
3998480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3999*0b57cec5SDimitry Andric }
4000*0b57cec5SDimitry Andric 
4001480093f4SDimitry Andric void TargetProperties::DisableSTDIOValueChangedCallback() {
4002480093f4SDimitry Andric   if (GetDisableSTDIO())
4003480093f4SDimitry Andric     m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4004*0b57cec5SDimitry Andric   else
4005480093f4SDimitry Andric     m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4006*0b57cec5SDimitry Andric }
4007*0b57cec5SDimitry Andric 
4008*0b57cec5SDimitry Andric // Target::TargetEventData
4009*0b57cec5SDimitry Andric 
4010*0b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
4011*0b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list() {}
4012*0b57cec5SDimitry Andric 
4013*0b57cec5SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
4014*0b57cec5SDimitry Andric                                          const ModuleList &module_list)
4015*0b57cec5SDimitry Andric     : EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
4016*0b57cec5SDimitry Andric 
4017*0b57cec5SDimitry Andric Target::TargetEventData::~TargetEventData() = default;
4018*0b57cec5SDimitry Andric 
4019*0b57cec5SDimitry Andric ConstString Target::TargetEventData::GetFlavorString() {
4020*0b57cec5SDimitry Andric   static ConstString g_flavor("Target::TargetEventData");
4021*0b57cec5SDimitry Andric   return g_flavor;
4022*0b57cec5SDimitry Andric }
4023*0b57cec5SDimitry Andric 
4024*0b57cec5SDimitry Andric void Target::TargetEventData::Dump(Stream *s) const {
4025*0b57cec5SDimitry Andric   for (size_t i = 0; i < m_module_list.GetSize(); ++i) {
4026*0b57cec5SDimitry Andric     if (i != 0)
4027*0b57cec5SDimitry Andric       *s << ", ";
4028*0b57cec5SDimitry Andric     m_module_list.GetModuleAtIndex(i)->GetDescription(
4029480093f4SDimitry Andric         s->AsRawOstream(), lldb::eDescriptionLevelBrief);
4030*0b57cec5SDimitry Andric   }
4031*0b57cec5SDimitry Andric }
4032*0b57cec5SDimitry Andric 
4033*0b57cec5SDimitry Andric const Target::TargetEventData *
4034*0b57cec5SDimitry Andric Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) {
4035*0b57cec5SDimitry Andric   if (event_ptr) {
4036*0b57cec5SDimitry Andric     const EventData *event_data = event_ptr->GetData();
4037*0b57cec5SDimitry Andric     if (event_data &&
4038*0b57cec5SDimitry Andric         event_data->GetFlavor() == TargetEventData::GetFlavorString())
4039*0b57cec5SDimitry Andric       return static_cast<const TargetEventData *>(event_ptr->GetData());
4040*0b57cec5SDimitry Andric   }
4041*0b57cec5SDimitry Andric   return nullptr;
4042*0b57cec5SDimitry Andric }
4043*0b57cec5SDimitry Andric 
4044*0b57cec5SDimitry Andric TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
4045*0b57cec5SDimitry Andric   TargetSP target_sp;
4046*0b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
4047*0b57cec5SDimitry Andric   if (event_data)
4048*0b57cec5SDimitry Andric     target_sp = event_data->m_target_sp;
4049*0b57cec5SDimitry Andric   return target_sp;
4050*0b57cec5SDimitry Andric }
4051*0b57cec5SDimitry Andric 
4052*0b57cec5SDimitry Andric ModuleList
4053*0b57cec5SDimitry Andric Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
4054*0b57cec5SDimitry Andric   ModuleList module_list;
4055*0b57cec5SDimitry Andric   const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
4056*0b57cec5SDimitry Andric   if (event_data)
4057*0b57cec5SDimitry Andric     module_list = event_data->m_module_list;
4058*0b57cec5SDimitry Andric   return module_list;
4059*0b57cec5SDimitry Andric }
40609dba64beSDimitry Andric 
40619dba64beSDimitry Andric std::recursive_mutex &Target::GetAPIMutex() {
40629dba64beSDimitry Andric   if (GetProcessSP() && GetProcessSP()->CurrentThreadIsPrivateStateThread())
40639dba64beSDimitry Andric     return m_private_mutex;
40649dba64beSDimitry Andric   else
40659dba64beSDimitry Andric     return m_mutex;
40669dba64beSDimitry Andric }
4067