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