1cfca06d7SDimitry Andric //===-- Target.cpp --------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
994994d37SDimitry Andric #include "lldb/Target/Target.h"
1014f1b3e8SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
115f29bb8aSDimitry Andric #include "lldb/Breakpoint/BreakpointPrecondition.h"
12f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolver.h"
13f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverAddress.h"
14f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
15f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
16f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverName.h"
1794994d37SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverScripted.h"
18f034231aSEd Maste #include "lldb/Breakpoint/Watchpoint.h"
19f034231aSEd Maste #include "lldb/Core/Debugger.h"
20f034231aSEd Maste #include "lldb/Core/Module.h"
21f034231aSEd Maste #include "lldb/Core/ModuleSpec.h"
22ef5d0b5eSDimitry Andric #include "lldb/Core/PluginManager.h"
2394994d37SDimitry Andric #include "lldb/Core/SearchFilter.h"
24f034231aSEd Maste #include "lldb/Core/Section.h"
25f034231aSEd Maste #include "lldb/Core/SourceManager.h"
2694994d37SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
27f034231aSEd Maste #include "lldb/Core/ValueObject.h"
28e3b55780SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
29b60736ecSDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
30cfca06d7SDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
31e81d9d49SDimitry Andric #include "lldb/Expression/REPL.h"
32e81d9d49SDimitry Andric #include "lldb/Expression/UserExpression.h"
33b60736ecSDimitry Andric #include "lldb/Expression/UtilityFunction.h"
34f034231aSEd Maste #include "lldb/Host/Host.h"
35a884e649SDimitry Andric #include "lldb/Host/PosixApi.h"
36b1c73532SDimitry Andric #include "lldb/Host/StreamFile.h"
37f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
38f034231aSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
39f034231aSEd Maste #include "lldb/Interpreter/OptionGroupWatchpoint.h"
40f034231aSEd Maste #include "lldb/Interpreter/OptionValues.h"
41f034231aSEd Maste #include "lldb/Interpreter/Property.h"
42e81d9d49SDimitry Andric #include "lldb/Symbol/Function.h"
4314f1b3e8SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
44e81d9d49SDimitry Andric #include "lldb/Symbol/Symbol.h"
45344a3780SDimitry Andric #include "lldb/Target/ABI.h"
46ac9a064cSDimitry Andric #include "lldb/Target/ExecutionContext.h"
47e81d9d49SDimitry Andric #include "lldb/Target/Language.h"
485e95aa85SEd Maste #include "lldb/Target/LanguageRuntime.h"
49f034231aSEd Maste #include "lldb/Target/Process.h"
507fa27ce4SDimitry Andric #include "lldb/Target/RegisterTypeBuilder.h"
51866dcdacSEd Maste #include "lldb/Target/SectionLoadList.h"
52f034231aSEd Maste #include "lldb/Target/StackFrame.h"
53b60736ecSDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
54f21a844fSEd Maste #include "lldb/Target/SystemRuntime.h"
55f034231aSEd Maste #include "lldb/Target/Thread.h"
56f034231aSEd Maste #include "lldb/Target/ThreadSpec.h"
57145449b1SDimitry Andric #include "lldb/Target/UnixSignals.h"
5894994d37SDimitry Andric #include "lldb/Utility/Event.h"
5974a628f7SDimitry Andric #include "lldb/Utility/FileSpec.h"
60e81d9d49SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
61145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
6274a628f7SDimitry Andric #include "lldb/Utility/Log.h"
6394994d37SDimitry Andric #include "lldb/Utility/State.h"
6474a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
651b306c26SDimitry Andric #include "lldb/Utility/Timer.h"
665f29bb8aSDimitry Andric
675f29bb8aSDimitry Andric #include "llvm/ADT/ScopeExit.h"
68c0981da4SDimitry Andric #include "llvm/ADT/SetVector.h"
695f29bb8aSDimitry Andric
705f29bb8aSDimitry Andric #include <memory>
7194994d37SDimitry Andric #include <mutex>
72e3b55780SDimitry Andric #include <optional>
737fa27ce4SDimitry Andric #include <sstream>
74f034231aSEd Maste
75f034231aSEd Maste using namespace lldb;
76f034231aSEd Maste using namespace lldb_private;
77f034231aSEd Maste
7814f1b3e8SDimitry Andric constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
7914f1b3e8SDimitry Andric
Arch(const ArchSpec & spec)80ef5d0b5eSDimitry Andric Target::Arch::Arch(const ArchSpec &spec)
81ef5d0b5eSDimitry Andric : m_spec(spec),
82ef5d0b5eSDimitry Andric m_plugin_up(PluginManager::CreateArchitectureInstance(spec)) {}
83ef5d0b5eSDimitry Andric
operator =(const ArchSpec & spec)84ef5d0b5eSDimitry Andric const Target::Arch &Target::Arch::operator=(const ArchSpec &spec) {
85ef5d0b5eSDimitry Andric m_spec = spec;
86ef5d0b5eSDimitry Andric m_plugin_up = PluginManager::CreateArchitectureInstance(spec);
87ef5d0b5eSDimitry Andric return *this;
88ef5d0b5eSDimitry Andric }
89ef5d0b5eSDimitry Andric
GetStaticBroadcasterClass()90ac9a064cSDimitry Andric llvm::StringRef Target::GetStaticBroadcasterClass() {
91ac9a064cSDimitry Andric static constexpr llvm::StringLiteral class_name("lldb.target");
92f034231aSEd Maste return class_name;
93f034231aSEd Maste }
94f034231aSEd Maste
Target(Debugger & debugger,const ArchSpec & target_arch,const lldb::PlatformSP & platform_sp,bool is_dummy_target)9514f1b3e8SDimitry Andric Target::Target(Debugger &debugger, const ArchSpec &target_arch,
9614f1b3e8SDimitry Andric const lldb::PlatformSP &platform_sp, bool is_dummy_target)
97f3fbd1c0SDimitry Andric : TargetProperties(this),
9814f1b3e8SDimitry Andric Broadcaster(debugger.GetBroadcasterManager(),
99ac9a064cSDimitry Andric Target::GetStaticBroadcasterClass().str()),
10014f1b3e8SDimitry Andric ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp),
101f73363f1SDimitry Andric m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
102f73363f1SDimitry Andric m_breakpoint_list(false), m_internal_breakpoint_list(true),
103f73363f1SDimitry Andric m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
104cfca06d7SDimitry Andric m_image_search_paths(ImageSearchPathsChanged, this),
1055f29bb8aSDimitry Andric m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0),
106c0981da4SDimitry Andric m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false),
107f73363f1SDimitry Andric m_is_dummy_target(is_dummy_target),
108b60736ecSDimitry Andric m_frame_recognizer_manager_up(
109c0981da4SDimitry Andric std::make_unique<StackFrameRecognizerManager>()) {
110f034231aSEd Maste SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
111f034231aSEd Maste SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
112f034231aSEd Maste SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
113f034231aSEd Maste SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
114f034231aSEd Maste SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
115f034231aSEd Maste
116f034231aSEd Maste CheckInWithManager();
117f034231aSEd Maste
118145449b1SDimitry Andric LLDB_LOG(GetLog(LLDBLog::Object), "{0} Target::Target()",
119145449b1SDimitry Andric static_cast<void *>(this));
120ef5d0b5eSDimitry Andric if (target_arch.IsValid()) {
121145449b1SDimitry Andric LLDB_LOG(GetLog(LLDBLog::Target),
122ead24645SDimitry Andric "Target::Target created with architecture {0} ({1})",
123ef5d0b5eSDimitry Andric target_arch.GetArchitectureName(),
124ef5d0b5eSDimitry Andric target_arch.GetTriple().getTriple().c_str());
125f034231aSEd Maste }
126cfca06d7SDimitry Andric
127cfca06d7SDimitry Andric UpdateLaunchInfoFromProperties();
128f034231aSEd Maste }
129f034231aSEd Maste
~Target()13014f1b3e8SDimitry Andric Target::~Target() {
131145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Object);
132ead24645SDimitry Andric LLDB_LOG(log, "{0} Target::~Target()", static_cast<void *>(this));
133e81d9d49SDimitry Andric DeleteCurrentProcess();
134e81d9d49SDimitry Andric }
135e81d9d49SDimitry Andric
PrimeFromDummyTarget(Target & target)136b60736ecSDimitry Andric void Target::PrimeFromDummyTarget(Target &target) {
137b60736ecSDimitry Andric m_stop_hooks = target.m_stop_hooks;
138205afe67SEd Maste
139b60736ecSDimitry Andric for (const auto &breakpoint_sp : target.m_breakpoint_list.Breakpoints()) {
140205afe67SEd Maste if (breakpoint_sp->IsInternal())
141205afe67SEd Maste continue;
142205afe67SEd Maste
143cfca06d7SDimitry Andric BreakpointSP new_bp(
144cfca06d7SDimitry Andric Breakpoint::CopyFromBreakpoint(shared_from_this(), *breakpoint_sp));
145cfca06d7SDimitry Andric AddBreakpoint(std::move(new_bp), false);
146205afe67SEd Maste }
147ef5d0b5eSDimitry Andric
148e3b55780SDimitry Andric for (const auto &bp_name_entry : target.m_breakpoint_names) {
149e3b55780SDimitry Andric AddBreakpointName(std::make_unique<BreakpointName>(*bp_name_entry.second));
150ef5d0b5eSDimitry Andric }
151b60736ecSDimitry Andric
152b60736ecSDimitry Andric m_frame_recognizer_manager_up = std::make_unique<StackFrameRecognizerManager>(
153b60736ecSDimitry Andric *target.m_frame_recognizer_manager_up);
154145449b1SDimitry Andric
155145449b1SDimitry Andric m_dummy_signals = target.m_dummy_signals;
156205afe67SEd Maste }
157205afe67SEd Maste
Dump(Stream * s,lldb::DescriptionLevel description_level)15814f1b3e8SDimitry Andric void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {
159f034231aSEd Maste // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
16014f1b3e8SDimitry Andric if (description_level != lldb::eDescriptionLevelBrief) {
161f034231aSEd Maste s->Indent();
162f034231aSEd Maste s->PutCString("Target\n");
163f034231aSEd Maste s->IndentMore();
164f034231aSEd Maste m_images.Dump(s);
165f034231aSEd Maste m_breakpoint_list.Dump(s);
166f034231aSEd Maste m_internal_breakpoint_list.Dump(s);
167f034231aSEd Maste s->IndentLess();
16814f1b3e8SDimitry Andric } else {
169f034231aSEd Maste Module *exe_module = GetExecutableModulePointer();
170f034231aSEd Maste if (exe_module)
171f034231aSEd Maste s->PutCString(exe_module->GetFileSpec().GetFilename().GetCString());
172f034231aSEd Maste else
173f034231aSEd Maste s->PutCString("No executable module.");
174f034231aSEd Maste }
175f034231aSEd Maste }
176f034231aSEd Maste
CleanupProcess()17714f1b3e8SDimitry Andric void Target::CleanupProcess() {
178f034231aSEd Maste // Do any cleanup of the target we need to do between process instances.
179f034231aSEd Maste // NB It is better to do this before destroying the process in case the
180f034231aSEd Maste // clean up needs some help from the process.
181f034231aSEd Maste m_breakpoint_list.ClearAllBreakpointSites();
182f034231aSEd Maste m_internal_breakpoint_list.ClearAllBreakpointSites();
183e3b55780SDimitry Andric ResetBreakpointHitCounts();
184f034231aSEd Maste // Disable watchpoints just on the debugger side.
185f3fbd1c0SDimitry Andric std::unique_lock<std::recursive_mutex> lock;
186f3fbd1c0SDimitry Andric this->GetWatchpointList().GetListMutex(lock);
187f034231aSEd Maste DisableAllWatchpoints(false);
188f034231aSEd Maste ClearAllWatchpointHitCounts();
1895e95aa85SEd Maste ClearAllWatchpointHistoricValues();
190344a3780SDimitry Andric m_latest_stop_hook_id = 0;
191f034231aSEd Maste }
192f034231aSEd Maste
DeleteCurrentProcess()19314f1b3e8SDimitry Andric void Target::DeleteCurrentProcess() {
19414f1b3e8SDimitry Andric if (m_process_sp) {
195145449b1SDimitry Andric // We dispose any active tracing sessions on the current process
196145449b1SDimitry Andric m_trace_sp.reset();
197866dcdacSEd Maste m_section_load_history.Clear();
198f034231aSEd Maste if (m_process_sp->IsAlive())
1995e95aa85SEd Maste m_process_sp->Destroy(false);
200f034231aSEd Maste
201b1c73532SDimitry Andric m_process_sp->Finalize(false /* not destructing */);
202f034231aSEd Maste
203f034231aSEd Maste CleanupProcess();
204f034231aSEd Maste
205f034231aSEd Maste m_process_sp.reset();
206f034231aSEd Maste }
207f034231aSEd Maste }
208f034231aSEd Maste
CreateProcess(ListenerSP listener_sp,llvm::StringRef plugin_name,const FileSpec * crash_file,bool can_connect)20914f1b3e8SDimitry Andric const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp,
21014f1b3e8SDimitry Andric llvm::StringRef plugin_name,
211b60736ecSDimitry Andric const FileSpec *crash_file,
212b60736ecSDimitry Andric bool can_connect) {
21394994d37SDimitry Andric if (!listener_sp)
21494994d37SDimitry Andric listener_sp = GetDebugger().GetListener();
215f034231aSEd Maste DeleteCurrentProcess();
21614f1b3e8SDimitry Andric m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name,
217b60736ecSDimitry Andric listener_sp, crash_file, can_connect);
218f034231aSEd Maste return m_process_sp;
219f034231aSEd Maste }
220f034231aSEd Maste
GetProcessSP() const22114f1b3e8SDimitry Andric const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; }
222f034231aSEd Maste
GetREPL(Status & err,lldb::LanguageType language,const char * repl_options,bool can_create)223b76161e4SDimitry Andric lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language,
22414f1b3e8SDimitry Andric const char *repl_options, bool can_create) {
2256f8fc217SDimitry Andric if (language == eLanguageTypeUnknown)
2266f8fc217SDimitry Andric language = m_debugger.GetREPLLanguage();
2276f8fc217SDimitry Andric
22814f1b3e8SDimitry Andric if (language == eLanguageTypeUnknown) {
229ead24645SDimitry Andric LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
230e81d9d49SDimitry Andric
231ead24645SDimitry Andric if (auto single_lang = repl_languages.GetSingularLanguage()) {
232ead24645SDimitry Andric language = *single_lang;
233ead24645SDimitry Andric } else if (repl_languages.Empty()) {
2346f8fc217SDimitry Andric err.SetErrorString(
23514f1b3e8SDimitry Andric "LLDB isn't configured with REPL support for any languages.");
236e81d9d49SDimitry Andric return REPLSP();
23714f1b3e8SDimitry Andric } else {
2386f8fc217SDimitry Andric err.SetErrorString(
23914f1b3e8SDimitry Andric "Multiple possible REPL languages. Please specify a language.");
240e81d9d49SDimitry Andric return REPLSP();
241e81d9d49SDimitry Andric }
242e81d9d49SDimitry Andric }
243e81d9d49SDimitry Andric
244e81d9d49SDimitry Andric REPLMap::iterator pos = m_repl_map.find(language);
245e81d9d49SDimitry Andric
24614f1b3e8SDimitry Andric if (pos != m_repl_map.end()) {
247e81d9d49SDimitry Andric return pos->second;
248e81d9d49SDimitry Andric }
249e81d9d49SDimitry Andric
25014f1b3e8SDimitry Andric if (!can_create) {
25114f1b3e8SDimitry Andric err.SetErrorStringWithFormat(
25214f1b3e8SDimitry Andric "Couldn't find an existing REPL for %s, and can't create a new one",
25314f1b3e8SDimitry Andric Language::GetNameForLanguageType(language));
254e81d9d49SDimitry Andric return lldb::REPLSP();
255e81d9d49SDimitry Andric }
256e81d9d49SDimitry Andric
257e81d9d49SDimitry Andric Debugger *const debugger = nullptr;
258e81d9d49SDimitry Andric lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
259e81d9d49SDimitry Andric
26014f1b3e8SDimitry Andric if (ret) {
261e81d9d49SDimitry Andric m_repl_map[language] = ret;
262e81d9d49SDimitry Andric return m_repl_map[language];
263e81d9d49SDimitry Andric }
264e81d9d49SDimitry Andric
26514f1b3e8SDimitry Andric if (err.Success()) {
26614f1b3e8SDimitry Andric err.SetErrorStringWithFormat("Couldn't create a REPL for %s",
26714f1b3e8SDimitry Andric Language::GetNameForLanguageType(language));
268e81d9d49SDimitry Andric }
269e81d9d49SDimitry Andric
270e81d9d49SDimitry Andric return lldb::REPLSP();
271e81d9d49SDimitry Andric }
272e81d9d49SDimitry Andric
SetREPL(lldb::LanguageType language,lldb::REPLSP repl_sp)27314f1b3e8SDimitry Andric void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) {
274e81d9d49SDimitry Andric lldbassert(!m_repl_map.count(language));
275e81d9d49SDimitry Andric
276e81d9d49SDimitry Andric m_repl_map[language] = repl_sp;
277e81d9d49SDimitry Andric }
278e81d9d49SDimitry Andric
Destroy()27914f1b3e8SDimitry Andric void Target::Destroy() {
280f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
281f034231aSEd Maste m_valid = false;
282f034231aSEd Maste DeleteCurrentProcess();
283f034231aSEd Maste m_platform_sp.reset();
284ef5d0b5eSDimitry Andric m_arch = ArchSpec();
28586758c71SEd Maste ClearModules(true);
286866dcdacSEd Maste m_section_load_history.Clear();
287f034231aSEd Maste const bool notify = false;
288f034231aSEd Maste m_breakpoint_list.RemoveAll(notify);
289f034231aSEd Maste m_internal_breakpoint_list.RemoveAll(notify);
290f034231aSEd Maste m_last_created_breakpoint.reset();
291e3b55780SDimitry Andric m_watchpoint_list.RemoveAll(notify);
292f034231aSEd Maste m_last_created_watchpoint.reset();
293f034231aSEd Maste m_search_filter_sp.reset();
294f034231aSEd Maste m_image_search_paths.Clear(notify);
295f034231aSEd Maste m_stop_hooks.clear();
296f034231aSEd Maste m_stop_hook_next_id = 0;
297f034231aSEd Maste m_suppress_stop_hooks = false;
2987fa27ce4SDimitry Andric m_repl_map.clear();
299145449b1SDimitry Andric Args signal_args;
300145449b1SDimitry Andric ClearDummySignals(signal_args);
301145449b1SDimitry Andric }
302145449b1SDimitry Andric
GetABIName() const303145449b1SDimitry Andric llvm::StringRef Target::GetABIName() const {
304145449b1SDimitry Andric lldb::ABISP abi_sp;
305145449b1SDimitry Andric if (m_process_sp)
306145449b1SDimitry Andric abi_sp = m_process_sp->GetABI();
307145449b1SDimitry Andric if (!abi_sp)
308145449b1SDimitry Andric abi_sp = ABI::FindPlugin(ProcessSP(), GetArchitecture());
309145449b1SDimitry Andric if (abi_sp)
310145449b1SDimitry Andric return abi_sp->GetPluginName();
311145449b1SDimitry Andric return {};
312f034231aSEd Maste }
313f034231aSEd Maste
GetBreakpointList(bool internal)31414f1b3e8SDimitry Andric BreakpointList &Target::GetBreakpointList(bool internal) {
315f034231aSEd Maste if (internal)
316f034231aSEd Maste return m_internal_breakpoint_list;
317f034231aSEd Maste else
318f034231aSEd Maste return m_breakpoint_list;
319f034231aSEd Maste }
320f034231aSEd Maste
GetBreakpointList(bool internal) const32114f1b3e8SDimitry Andric const BreakpointList &Target::GetBreakpointList(bool internal) const {
322f034231aSEd Maste if (internal)
323f034231aSEd Maste return m_internal_breakpoint_list;
324f034231aSEd Maste else
325f034231aSEd Maste return m_breakpoint_list;
326f034231aSEd Maste }
327f034231aSEd Maste
GetBreakpointByID(break_id_t break_id)32814f1b3e8SDimitry Andric BreakpointSP Target::GetBreakpointByID(break_id_t break_id) {
329f034231aSEd Maste BreakpointSP bp_sp;
330f034231aSEd Maste
331f034231aSEd Maste if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
332f034231aSEd Maste bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
333f034231aSEd Maste else
334f034231aSEd Maste bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
335f034231aSEd Maste
336f034231aSEd Maste return bp_sp;
337f034231aSEd Maste }
338f034231aSEd Maste
339b1c73532SDimitry Andric lldb::BreakpointSP
CreateBreakpointAtUserEntry(Status & error)340b1c73532SDimitry Andric lldb_private::Target::CreateBreakpointAtUserEntry(Status &error) {
341b1c73532SDimitry Andric ModuleSP main_module_sp = GetExecutableModule();
342b1c73532SDimitry Andric FileSpecList shared_lib_filter;
343b1c73532SDimitry Andric shared_lib_filter.Append(main_module_sp->GetFileSpec());
344b1c73532SDimitry Andric llvm::SetVector<std::string, std::vector<std::string>,
345b1c73532SDimitry Andric std::unordered_set<std::string>>
346b1c73532SDimitry Andric entryPointNamesSet;
347b1c73532SDimitry Andric for (LanguageType lang_type : Language::GetSupportedLanguages()) {
348b1c73532SDimitry Andric Language *lang = Language::FindPlugin(lang_type);
349b1c73532SDimitry Andric if (!lang) {
350b1c73532SDimitry Andric error.SetErrorString("Language not found\n");
351b1c73532SDimitry Andric return lldb::BreakpointSP();
352b1c73532SDimitry Andric }
353b1c73532SDimitry Andric std::string entryPointName = lang->GetUserEntryPointName().str();
354b1c73532SDimitry Andric if (!entryPointName.empty())
355b1c73532SDimitry Andric entryPointNamesSet.insert(entryPointName);
356b1c73532SDimitry Andric }
357b1c73532SDimitry Andric if (entryPointNamesSet.empty()) {
358b1c73532SDimitry Andric error.SetErrorString("No entry point name found\n");
359b1c73532SDimitry Andric return lldb::BreakpointSP();
360b1c73532SDimitry Andric }
361b1c73532SDimitry Andric BreakpointSP bp_sp = CreateBreakpoint(
362b1c73532SDimitry Andric &shared_lib_filter,
363b1c73532SDimitry Andric /*containingSourceFiles=*/nullptr, entryPointNamesSet.takeVector(),
364b1c73532SDimitry Andric /*func_name_type_mask=*/eFunctionNameTypeFull,
365b1c73532SDimitry Andric /*language=*/eLanguageTypeUnknown,
366b1c73532SDimitry Andric /*offset=*/0,
367b1c73532SDimitry Andric /*skip_prologue=*/eLazyBoolNo,
368b1c73532SDimitry Andric /*internal=*/false,
369b1c73532SDimitry Andric /*hardware=*/false);
370b1c73532SDimitry Andric if (!bp_sp) {
371b1c73532SDimitry Andric error.SetErrorString("Breakpoint creation failed.\n");
372b1c73532SDimitry Andric return lldb::BreakpointSP();
373b1c73532SDimitry Andric }
374b1c73532SDimitry Andric bp_sp->SetOneShot(true);
375b1c73532SDimitry Andric return bp_sp;
376b1c73532SDimitry Andric }
377b1c73532SDimitry Andric
CreateSourceRegexBreakpoint(const FileSpecList * containingModules,const FileSpecList * source_file_spec_list,const std::unordered_set<std::string> & function_names,RegularExpression source_regex,bool internal,bool hardware,LazyBool move_to_nearest_code)37814f1b3e8SDimitry Andric BreakpointSP Target::CreateSourceRegexBreakpoint(
37914f1b3e8SDimitry Andric const FileSpecList *containingModules,
380f034231aSEd Maste const FileSpecList *source_file_spec_list,
381f3fbd1c0SDimitry Andric const std::unordered_set<std::string> &function_names,
382ead24645SDimitry Andric RegularExpression source_regex, bool internal, bool hardware,
38314f1b3e8SDimitry Andric LazyBool move_to_nearest_code) {
38414f1b3e8SDimitry Andric SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
38514f1b3e8SDimitry Andric containingModules, source_file_spec_list));
3865e95aa85SEd Maste if (move_to_nearest_code == eLazyBoolCalculate)
3875e95aa85SEd Maste move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
38814f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(
389ead24645SDimitry Andric nullptr, std::move(source_regex), function_names,
390f3fbd1c0SDimitry Andric !static_cast<bool>(move_to_nearest_code)));
391f3fbd1c0SDimitry Andric
392866dcdacSEd Maste return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
393f034231aSEd Maste }
394f034231aSEd Maste
CreateBreakpoint(const FileSpecList * containingModules,const FileSpec & file,uint32_t line_no,uint32_t column,lldb::addr_t offset,LazyBool check_inlines,LazyBool skip_prologue,bool internal,bool hardware,LazyBool move_to_nearest_code)39514f1b3e8SDimitry Andric BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules,
39614f1b3e8SDimitry Andric const FileSpec &file, uint32_t line_no,
39794994d37SDimitry Andric uint32_t column, lldb::addr_t offset,
398f034231aSEd Maste LazyBool check_inlines,
39914f1b3e8SDimitry Andric LazyBool skip_prologue, bool internal,
4005e95aa85SEd Maste bool hardware,
40114f1b3e8SDimitry Andric LazyBool move_to_nearest_code) {
402f3fbd1c0SDimitry Andric FileSpec remapped_file;
403e3b55780SDimitry Andric std::optional<llvm::StringRef> removed_prefix_opt =
404e3b55780SDimitry Andric GetSourcePathMap().ReverseRemapPath(file, remapped_file);
405e3b55780SDimitry Andric if (!removed_prefix_opt)
406f3fbd1c0SDimitry Andric remapped_file = file;
407f3fbd1c0SDimitry Andric
40814f1b3e8SDimitry Andric if (check_inlines == eLazyBoolCalculate) {
409f034231aSEd Maste const InlineStrategy inline_strategy = GetInlineStrategy();
41014f1b3e8SDimitry Andric switch (inline_strategy) {
411f034231aSEd Maste case eInlineBreakpointsNever:
412f034231aSEd Maste check_inlines = eLazyBoolNo;
413f034231aSEd Maste break;
414f034231aSEd Maste
415f034231aSEd Maste case eInlineBreakpointsHeaders:
416f3fbd1c0SDimitry Andric if (remapped_file.IsSourceImplementationFile())
417f034231aSEd Maste check_inlines = eLazyBoolNo;
418f034231aSEd Maste else
419f034231aSEd Maste check_inlines = eLazyBoolYes;
420f034231aSEd Maste break;
421f034231aSEd Maste
422f034231aSEd Maste case eInlineBreakpointsAlways:
423f034231aSEd Maste check_inlines = eLazyBoolYes;
424f034231aSEd Maste break;
425f034231aSEd Maste }
426f034231aSEd Maste }
427f034231aSEd Maste SearchFilterSP filter_sp;
42814f1b3e8SDimitry Andric if (check_inlines == eLazyBoolNo) {
429f034231aSEd Maste // Not checking for inlines, we are looking only for matching compile units
430f034231aSEd Maste FileSpecList compile_unit_list;
431f3fbd1c0SDimitry Andric compile_unit_list.Append(remapped_file);
43214f1b3e8SDimitry Andric filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
43314f1b3e8SDimitry Andric &compile_unit_list);
43414f1b3e8SDimitry Andric } else {
435f034231aSEd Maste filter_sp = GetSearchFilterForModuleList(containingModules);
436f034231aSEd Maste }
437f034231aSEd Maste if (skip_prologue == eLazyBoolCalculate)
438f034231aSEd Maste skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
4395e95aa85SEd Maste if (move_to_nearest_code == eLazyBoolCalculate)
4405e95aa85SEd Maste move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
441f034231aSEd Maste
442344a3780SDimitry Andric SourceLocationSpec location_spec(remapped_file, line_no, column,
443344a3780SDimitry Andric check_inlines,
444344a3780SDimitry Andric !static_cast<bool>(move_to_nearest_code));
445344a3780SDimitry Andric if (!location_spec)
446344a3780SDimitry Andric return nullptr;
447344a3780SDimitry Andric
44814f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(
449e3b55780SDimitry Andric nullptr, offset, skip_prologue, location_spec, removed_prefix_opt));
450866dcdacSEd Maste return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
451f034231aSEd Maste }
452f034231aSEd Maste
CreateBreakpoint(lldb::addr_t addr,bool internal,bool hardware)45314f1b3e8SDimitry Andric BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal,
45414f1b3e8SDimitry Andric bool hardware) {
455f034231aSEd Maste Address so_addr;
456e81d9d49SDimitry Andric
457e81d9d49SDimitry Andric // Check for any reason we want to move this breakpoint to other address.
458e81d9d49SDimitry Andric addr = GetBreakableLoadAddress(addr);
459e81d9d49SDimitry Andric
460f73363f1SDimitry Andric // Attempt to resolve our load address if possible, though it is ok if it
461f73363f1SDimitry Andric // doesn't resolve to section/offset.
462f034231aSEd Maste
463f034231aSEd Maste // Try and resolve as a load address if possible
464866dcdacSEd Maste GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
46514f1b3e8SDimitry Andric if (!so_addr.IsValid()) {
466f034231aSEd Maste // The address didn't resolve, so just set this as an absolute address
467f034231aSEd Maste so_addr.SetOffset(addr);
468f034231aSEd Maste }
469f21a844fSEd Maste BreakpointSP bp_sp(CreateBreakpoint(so_addr, internal, hardware));
470f034231aSEd Maste return bp_sp;
471f034231aSEd Maste }
472f034231aSEd Maste
CreateBreakpoint(const Address & addr,bool internal,bool hardware)47314f1b3e8SDimitry Andric BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
47414f1b3e8SDimitry Andric bool hardware) {
47514f1b3e8SDimitry Andric SearchFilterSP filter_sp(
47614f1b3e8SDimitry Andric new SearchFilterForUnconstrainedSearches(shared_from_this()));
47714f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(
47814f1b3e8SDimitry Andric new BreakpointResolverAddress(nullptr, addr));
479866dcdacSEd Maste return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, false);
480f034231aSEd Maste }
481f034231aSEd Maste
482e81d9d49SDimitry Andric lldb::BreakpointSP
CreateAddressInModuleBreakpoint(lldb::addr_t file_addr,bool internal,const FileSpec & file_spec,bool request_hardware)48314f1b3e8SDimitry Andric Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
484b1c73532SDimitry Andric const FileSpec &file_spec,
48514f1b3e8SDimitry Andric bool request_hardware) {
48614f1b3e8SDimitry Andric SearchFilterSP filter_sp(
48714f1b3e8SDimitry Andric new SearchFilterForUnconstrainedSearches(shared_from_this()));
488706b4fc4SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(
489b1c73532SDimitry Andric nullptr, file_addr, file_spec));
49014f1b3e8SDimitry Andric return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
49114f1b3e8SDimitry Andric false);
492e81d9d49SDimitry Andric }
493e81d9d49SDimitry Andric
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const char * func_name,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)49494994d37SDimitry Andric BreakpointSP Target::CreateBreakpoint(
49594994d37SDimitry Andric const FileSpecList *containingModules,
49694994d37SDimitry Andric const FileSpecList *containingSourceFiles, const char *func_name,
49794994d37SDimitry Andric FunctionNameType func_name_type_mask, LanguageType language,
49894994d37SDimitry Andric lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) {
499f034231aSEd Maste BreakpointSP bp_sp;
50014f1b3e8SDimitry Andric if (func_name) {
50114f1b3e8SDimitry Andric SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
50214f1b3e8SDimitry Andric containingModules, containingSourceFiles));
503f034231aSEd Maste
504f034231aSEd Maste if (skip_prologue == eLazyBoolCalculate)
505f034231aSEd Maste skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
506e81d9d49SDimitry Andric if (language == lldb::eLanguageTypeUnknown)
507ac9a064cSDimitry Andric language = GetLanguage().AsLanguageType();
508f034231aSEd Maste
50914f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverName(
51014f1b3e8SDimitry Andric nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact,
51114f1b3e8SDimitry Andric offset, skip_prologue));
512866dcdacSEd Maste bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
513f034231aSEd Maste }
514f034231aSEd Maste return bp_sp;
515f034231aSEd Maste }
516f034231aSEd Maste
517f034231aSEd Maste lldb::BreakpointSP
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const std::vector<std::string> & func_names,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)518f034231aSEd Maste Target::CreateBreakpoint(const FileSpecList *containingModules,
519f034231aSEd Maste const FileSpecList *containingSourceFiles,
520f034231aSEd Maste const std::vector<std::string> &func_names,
52194994d37SDimitry Andric FunctionNameType func_name_type_mask,
52294994d37SDimitry Andric LanguageType language, lldb::addr_t offset,
52394994d37SDimitry Andric LazyBool skip_prologue, bool internal, bool hardware) {
524f034231aSEd Maste BreakpointSP bp_sp;
525f034231aSEd Maste size_t num_names = func_names.size();
52614f1b3e8SDimitry Andric if (num_names > 0) {
52714f1b3e8SDimitry Andric SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
52814f1b3e8SDimitry Andric containingModules, containingSourceFiles));
529f034231aSEd Maste
530f034231aSEd Maste if (skip_prologue == eLazyBoolCalculate)
531f034231aSEd Maste skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
532e81d9d49SDimitry Andric if (language == lldb::eLanguageTypeUnknown)
533ac9a064cSDimitry Andric language = GetLanguage().AsLanguageType();
534f034231aSEd Maste
53514f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(
53614f1b3e8SDimitry Andric new BreakpointResolverName(nullptr, func_names, func_name_type_mask,
53714f1b3e8SDimitry Andric language, offset, skip_prologue));
538866dcdacSEd Maste bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
539f034231aSEd Maste }
540f034231aSEd Maste return bp_sp;
541f034231aSEd Maste }
542f034231aSEd Maste
54394994d37SDimitry Andric BreakpointSP
CreateBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,const char * func_names[],size_t num_names,FunctionNameType func_name_type_mask,LanguageType language,lldb::addr_t offset,LazyBool skip_prologue,bool internal,bool hardware)54494994d37SDimitry Andric Target::CreateBreakpoint(const FileSpecList *containingModules,
54594994d37SDimitry Andric const FileSpecList *containingSourceFiles,
54694994d37SDimitry Andric const char *func_names[], size_t num_names,
54794994d37SDimitry Andric FunctionNameType func_name_type_mask,
54894994d37SDimitry Andric LanguageType language, lldb::addr_t offset,
54994994d37SDimitry Andric LazyBool skip_prologue, bool internal, bool hardware) {
550f034231aSEd Maste BreakpointSP bp_sp;
55114f1b3e8SDimitry Andric if (num_names > 0) {
55214f1b3e8SDimitry Andric SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
55314f1b3e8SDimitry Andric containingModules, containingSourceFiles));
554f034231aSEd Maste
55514f1b3e8SDimitry Andric if (skip_prologue == eLazyBoolCalculate) {
556f3fbd1c0SDimitry Andric if (offset == 0)
557f034231aSEd Maste skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
558f3fbd1c0SDimitry Andric else
559f3fbd1c0SDimitry Andric skip_prologue = eLazyBoolNo;
560f3fbd1c0SDimitry Andric }
561e81d9d49SDimitry Andric if (language == lldb::eLanguageTypeUnknown)
562ac9a064cSDimitry Andric language = GetLanguage().AsLanguageType();
563f034231aSEd Maste
56414f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverName(
56514f1b3e8SDimitry Andric nullptr, func_names, num_names, func_name_type_mask, language, offset,
566f034231aSEd Maste skip_prologue));
567f3fbd1c0SDimitry Andric resolver_sp->SetOffset(offset);
568866dcdacSEd Maste bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
569f034231aSEd Maste }
570f034231aSEd Maste return bp_sp;
571f034231aSEd Maste }
572f034231aSEd Maste
573f034231aSEd Maste SearchFilterSP
GetSearchFilterForModule(const FileSpec * containingModule)57414f1b3e8SDimitry Andric Target::GetSearchFilterForModule(const FileSpec *containingModule) {
575f034231aSEd Maste SearchFilterSP filter_sp;
57614f1b3e8SDimitry Andric if (containingModule != nullptr) {
577f034231aSEd Maste // TODO: We should look into sharing module based search filters
578f034231aSEd Maste // across many breakpoints like we do for the simple target based one
5795f29bb8aSDimitry Andric filter_sp = std::make_shared<SearchFilterByModule>(shared_from_this(),
5805f29bb8aSDimitry Andric *containingModule);
58114f1b3e8SDimitry Andric } else {
582e81d9d49SDimitry Andric if (!m_search_filter_sp)
5835f29bb8aSDimitry Andric m_search_filter_sp =
5845f29bb8aSDimitry Andric std::make_shared<SearchFilterForUnconstrainedSearches>(
5855f29bb8aSDimitry Andric shared_from_this());
586f034231aSEd Maste filter_sp = m_search_filter_sp;
587f034231aSEd Maste }
588f034231aSEd Maste return filter_sp;
589f034231aSEd Maste }
590f034231aSEd Maste
591f034231aSEd Maste SearchFilterSP
GetSearchFilterForModuleList(const FileSpecList * containingModules)59214f1b3e8SDimitry Andric Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) {
593f034231aSEd Maste SearchFilterSP filter_sp;
59414f1b3e8SDimitry Andric if (containingModules && containingModules->GetSize() != 0) {
595f034231aSEd Maste // TODO: We should look into sharing module based search filters
596f034231aSEd Maste // across many breakpoints like we do for the simple target based one
5975f29bb8aSDimitry Andric filter_sp = std::make_shared<SearchFilterByModuleList>(shared_from_this(),
5985f29bb8aSDimitry Andric *containingModules);
59914f1b3e8SDimitry Andric } else {
600e81d9d49SDimitry Andric if (!m_search_filter_sp)
6015f29bb8aSDimitry Andric m_search_filter_sp =
6025f29bb8aSDimitry Andric std::make_shared<SearchFilterForUnconstrainedSearches>(
6035f29bb8aSDimitry Andric shared_from_this());
604f034231aSEd Maste filter_sp = m_search_filter_sp;
605f034231aSEd Maste }
606f034231aSEd Maste return filter_sp;
607f034231aSEd Maste }
608f034231aSEd Maste
GetSearchFilterForModuleAndCUList(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles)60914f1b3e8SDimitry Andric SearchFilterSP Target::GetSearchFilterForModuleAndCUList(
61014f1b3e8SDimitry Andric const FileSpecList *containingModules,
61114f1b3e8SDimitry Andric const FileSpecList *containingSourceFiles) {
612e81d9d49SDimitry Andric if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
613f034231aSEd Maste return GetSearchFilterForModuleList(containingModules);
614f034231aSEd Maste
615f034231aSEd Maste SearchFilterSP filter_sp;
61614f1b3e8SDimitry Andric if (containingModules == nullptr) {
61714f1b3e8SDimitry Andric // We could make a special "CU List only SearchFilter". Better yet was if
618f73363f1SDimitry Andric // these could be composable, but that will take a little reworking.
619f034231aSEd Maste
6205f29bb8aSDimitry Andric filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
6215f29bb8aSDimitry Andric shared_from_this(), FileSpecList(), *containingSourceFiles);
62214f1b3e8SDimitry Andric } else {
6235f29bb8aSDimitry Andric filter_sp = std::make_shared<SearchFilterByModuleListAndCU>(
6245f29bb8aSDimitry Andric shared_from_this(), *containingModules, *containingSourceFiles);
625f034231aSEd Maste }
626f034231aSEd Maste return filter_sp;
627f034231aSEd Maste }
628f034231aSEd Maste
CreateFuncRegexBreakpoint(const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,RegularExpression func_regex,lldb::LanguageType requested_language,LazyBool skip_prologue,bool internal,bool hardware)62914f1b3e8SDimitry Andric BreakpointSP Target::CreateFuncRegexBreakpoint(
63014f1b3e8SDimitry Andric const FileSpecList *containingModules,
631ead24645SDimitry Andric const FileSpecList *containingSourceFiles, RegularExpression func_regex,
63214f1b3e8SDimitry Andric lldb::LanguageType requested_language, LazyBool skip_prologue,
63314f1b3e8SDimitry Andric bool internal, bool hardware) {
63414f1b3e8SDimitry Andric SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
63514f1b3e8SDimitry Andric containingModules, containingSourceFiles));
63614f1b3e8SDimitry Andric bool skip = (skip_prologue == eLazyBoolCalculate)
63714f1b3e8SDimitry Andric ? GetSkipPrologue()
6380cac4ca3SEd Maste : static_cast<bool>(skip_prologue);
63914f1b3e8SDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverName(
640ead24645SDimitry Andric nullptr, std::move(func_regex), requested_language, 0, skip));
641f034231aSEd Maste
642866dcdacSEd Maste return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
643f034231aSEd Maste }
644f034231aSEd Maste
645f034231aSEd Maste lldb::BreakpointSP
CreateExceptionBreakpoint(enum lldb::LanguageType language,bool catch_bp,bool throw_bp,bool internal,Args * additional_args,Status * error)64614f1b3e8SDimitry Andric Target::CreateExceptionBreakpoint(enum lldb::LanguageType language,
64714f1b3e8SDimitry Andric bool catch_bp, bool throw_bp, bool internal,
648b76161e4SDimitry Andric Args *additional_args, Status *error) {
64914f1b3e8SDimitry Andric BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint(
65014f1b3e8SDimitry Andric *this, language, catch_bp, throw_bp, internal);
65114f1b3e8SDimitry Andric if (exc_bkpt_sp && additional_args) {
6525f29bb8aSDimitry Andric BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
65314f1b3e8SDimitry Andric if (precondition_sp && additional_args) {
6545e95aa85SEd Maste if (error)
6555e95aa85SEd Maste *error = precondition_sp->ConfigurePrecondition(*additional_args);
6565e95aa85SEd Maste else
6575e95aa85SEd Maste precondition_sp->ConfigurePrecondition(*additional_args);
6585e95aa85SEd Maste }
6595e95aa85SEd Maste }
6605e95aa85SEd Maste return exc_bkpt_sp;
661f034231aSEd Maste }
662f034231aSEd Maste
CreateScriptedBreakpoint(const llvm::StringRef class_name,const FileSpecList * containingModules,const FileSpecList * containingSourceFiles,bool internal,bool request_hardware,StructuredData::ObjectSP extra_args_sp,Status * creation_error)663ead24645SDimitry Andric lldb::BreakpointSP Target::CreateScriptedBreakpoint(
664ead24645SDimitry Andric const llvm::StringRef class_name, const FileSpecList *containingModules,
665ead24645SDimitry Andric const FileSpecList *containingSourceFiles, bool internal,
666ead24645SDimitry Andric bool request_hardware, StructuredData::ObjectSP extra_args_sp,
667ead24645SDimitry Andric Status *creation_error) {
66894994d37SDimitry Andric SearchFilterSP filter_sp;
66994994d37SDimitry Andric
67094994d37SDimitry Andric lldb::SearchDepth depth = lldb::eSearchDepthTarget;
671ead24645SDimitry Andric bool has_files =
672ead24645SDimitry Andric containingSourceFiles && containingSourceFiles->GetSize() > 0;
67394994d37SDimitry Andric bool has_modules = containingModules && containingModules->GetSize() > 0;
67494994d37SDimitry Andric
67594994d37SDimitry Andric if (has_files && has_modules) {
676ead24645SDimitry Andric filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
677ead24645SDimitry Andric containingSourceFiles);
67894994d37SDimitry Andric } else if (has_files) {
679ead24645SDimitry Andric filter_sp =
680ead24645SDimitry Andric GetSearchFilterForModuleAndCUList(nullptr, containingSourceFiles);
68194994d37SDimitry Andric } else if (has_modules) {
68294994d37SDimitry Andric filter_sp = GetSearchFilterForModuleList(containingModules);
68394994d37SDimitry Andric } else {
6845f29bb8aSDimitry Andric filter_sp = std::make_shared<SearchFilterForUnconstrainedSearches>(
6855f29bb8aSDimitry Andric shared_from_this());
68694994d37SDimitry Andric }
68794994d37SDimitry Andric
6885f29bb8aSDimitry Andric BreakpointResolverSP resolver_sp(new BreakpointResolverScripted(
68977fc4c14SDimitry Andric nullptr, class_name, depth, StructuredDataImpl(extra_args_sp)));
69094994d37SDimitry Andric return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
69194994d37SDimitry Andric }
69294994d37SDimitry Andric
CreateBreakpoint(SearchFilterSP & filter_sp,BreakpointResolverSP & resolver_sp,bool internal,bool request_hardware,bool resolve_indirect_symbols)69314f1b3e8SDimitry Andric BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp,
69414f1b3e8SDimitry Andric BreakpointResolverSP &resolver_sp,
69514f1b3e8SDimitry Andric bool internal, bool request_hardware,
69614f1b3e8SDimitry Andric bool resolve_indirect_symbols) {
697f034231aSEd Maste BreakpointSP bp_sp;
69814f1b3e8SDimitry Andric if (filter_sp && resolver_sp) {
69994994d37SDimitry Andric const bool hardware = request_hardware || GetRequireHardwareBreakpoints();
70094994d37SDimitry Andric bp_sp.reset(new Breakpoint(*this, filter_sp, resolver_sp, hardware,
70114f1b3e8SDimitry Andric resolve_indirect_symbols));
702cfca06d7SDimitry Andric resolver_sp->SetBreakpoint(bp_sp);
703205afe67SEd Maste AddBreakpoint(bp_sp, internal);
704205afe67SEd Maste }
705205afe67SEd Maste return bp_sp;
706205afe67SEd Maste }
707f034231aSEd Maste
AddBreakpoint(lldb::BreakpointSP bp_sp,bool internal)70814f1b3e8SDimitry Andric void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) {
709205afe67SEd Maste if (!bp_sp)
710205afe67SEd Maste return;
711f034231aSEd Maste if (internal)
712f034231aSEd Maste m_internal_breakpoint_list.Add(bp_sp, false);
713f034231aSEd Maste else
714f034231aSEd Maste m_breakpoint_list.Add(bp_sp, true);
715f034231aSEd Maste
716145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
71714f1b3e8SDimitry Andric if (log) {
718f034231aSEd Maste StreamString s;
719f034231aSEd Maste bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
720ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (internal = %s) => break_id = %s\n",
721ead24645SDimitry Andric __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
722f034231aSEd Maste }
723f034231aSEd Maste
724f034231aSEd Maste bp_sp->ResolveBreakpoint();
725f034231aSEd Maste
72614f1b3e8SDimitry Andric if (!internal) {
727f034231aSEd Maste m_last_created_breakpoint = bp_sp;
728f034231aSEd Maste }
729f034231aSEd Maste }
730f034231aSEd Maste
AddNameToBreakpoint(BreakpointID & id,llvm::StringRef name,Status & error)731b1c73532SDimitry Andric void Target::AddNameToBreakpoint(BreakpointID &id, llvm::StringRef name,
732ead24645SDimitry Andric Status &error) {
733ead24645SDimitry Andric BreakpointSP bp_sp =
734ead24645SDimitry Andric m_breakpoint_list.FindBreakpointByID(id.GetBreakpointID());
735ead24645SDimitry Andric if (!bp_sp) {
736ef5d0b5eSDimitry Andric StreamString s;
737ef5d0b5eSDimitry Andric id.GetDescription(&s, eDescriptionLevelBrief);
738ead24645SDimitry Andric error.SetErrorStringWithFormat("Could not find breakpoint %s", s.GetData());
739ef5d0b5eSDimitry Andric return;
740ef5d0b5eSDimitry Andric }
741ef5d0b5eSDimitry Andric AddNameToBreakpoint(bp_sp, name, error);
742ef5d0b5eSDimitry Andric }
743ef5d0b5eSDimitry Andric
AddNameToBreakpoint(BreakpointSP & bp_sp,llvm::StringRef name,Status & error)744b1c73532SDimitry Andric void Target::AddNameToBreakpoint(BreakpointSP &bp_sp, llvm::StringRef name,
745ead24645SDimitry Andric Status &error) {
746ef5d0b5eSDimitry Andric if (!bp_sp)
747ef5d0b5eSDimitry Andric return;
748ef5d0b5eSDimitry Andric
749ef5d0b5eSDimitry Andric BreakpointName *bp_name = FindBreakpointName(ConstString(name), true, error);
750ef5d0b5eSDimitry Andric if (!bp_name)
751ef5d0b5eSDimitry Andric return;
752ef5d0b5eSDimitry Andric
753ef5d0b5eSDimitry Andric bp_name->ConfigureBreakpoint(bp_sp);
754ef5d0b5eSDimitry Andric bp_sp->AddName(name);
755ef5d0b5eSDimitry Andric }
756ef5d0b5eSDimitry Andric
AddBreakpointName(std::unique_ptr<BreakpointName> bp_name)757e3b55780SDimitry Andric void Target::AddBreakpointName(std::unique_ptr<BreakpointName> bp_name) {
758e3b55780SDimitry Andric m_breakpoint_names.insert(
759e3b55780SDimitry Andric std::make_pair(bp_name->GetName(), std::move(bp_name)));
760ef5d0b5eSDimitry Andric }
761ef5d0b5eSDimitry Andric
FindBreakpointName(ConstString name,bool can_create,Status & error)762ead24645SDimitry Andric BreakpointName *Target::FindBreakpointName(ConstString name, bool can_create,
763ead24645SDimitry Andric Status &error) {
764ef5d0b5eSDimitry Andric BreakpointID::StringIsBreakpointName(name.GetStringRef(), error);
765ef5d0b5eSDimitry Andric if (!error.Success())
766ef5d0b5eSDimitry Andric return nullptr;
767ef5d0b5eSDimitry Andric
768ef5d0b5eSDimitry Andric BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
769e3b55780SDimitry Andric if (iter != m_breakpoint_names.end()) {
770e3b55780SDimitry Andric return iter->second.get();
771e3b55780SDimitry Andric }
772e3b55780SDimitry Andric
773ead24645SDimitry Andric if (!can_create) {
774ef5d0b5eSDimitry Andric error.SetErrorStringWithFormat("Breakpoint name \"%s\" doesn't exist and "
775ead24645SDimitry Andric "can_create is false.",
776ead24645SDimitry Andric name.AsCString());
777ef5d0b5eSDimitry Andric return nullptr;
778ef5d0b5eSDimitry Andric }
779ef5d0b5eSDimitry Andric
780e3b55780SDimitry Andric return m_breakpoint_names
781e3b55780SDimitry Andric .insert(std::make_pair(name, std::make_unique<BreakpointName>(name)))
782e3b55780SDimitry Andric .first->second.get();
783ef5d0b5eSDimitry Andric }
784ef5d0b5eSDimitry Andric
DeleteBreakpointName(ConstString name)785ead24645SDimitry Andric void Target::DeleteBreakpointName(ConstString name) {
786ef5d0b5eSDimitry Andric BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
787ef5d0b5eSDimitry Andric
788ef5d0b5eSDimitry Andric if (iter != m_breakpoint_names.end()) {
789ef5d0b5eSDimitry Andric const char *name_cstr = name.AsCString();
790ef5d0b5eSDimitry Andric m_breakpoint_names.erase(iter);
791ef5d0b5eSDimitry Andric for (auto bp_sp : m_breakpoint_list.Breakpoints())
792ef5d0b5eSDimitry Andric bp_sp->RemoveName(name_cstr);
793ef5d0b5eSDimitry Andric }
794ef5d0b5eSDimitry Andric }
795ef5d0b5eSDimitry Andric
RemoveNameFromBreakpoint(lldb::BreakpointSP & bp_sp,ConstString name)796ef5d0b5eSDimitry Andric void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp,
797ead24645SDimitry Andric ConstString name) {
798ef5d0b5eSDimitry Andric bp_sp->RemoveName(name.AsCString());
799ef5d0b5eSDimitry Andric }
800ef5d0b5eSDimitry Andric
ConfigureBreakpointName(BreakpointName & bp_name,const BreakpointOptions & new_options,const BreakpointName::Permissions & new_permissions)801ead24645SDimitry Andric void Target::ConfigureBreakpointName(
802ead24645SDimitry Andric BreakpointName &bp_name, const BreakpointOptions &new_options,
803ead24645SDimitry Andric const BreakpointName::Permissions &new_permissions) {
804ef5d0b5eSDimitry Andric bp_name.GetOptions().CopyOverSetOptions(new_options);
805ef5d0b5eSDimitry Andric bp_name.GetPermissions().MergeInto(new_permissions);
806ef5d0b5eSDimitry Andric ApplyNameToBreakpoints(bp_name);
807ef5d0b5eSDimitry Andric }
808ef5d0b5eSDimitry Andric
ApplyNameToBreakpoints(BreakpointName & bp_name)809ef5d0b5eSDimitry Andric void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) {
810706b4fc4SDimitry Andric llvm::Expected<std::vector<BreakpointSP>> expected_vector =
811706b4fc4SDimitry Andric m_breakpoint_list.FindBreakpointsByName(bp_name.GetName().AsCString());
812ef5d0b5eSDimitry Andric
813706b4fc4SDimitry Andric if (!expected_vector) {
814145449b1SDimitry Andric LLDB_LOG(GetLog(LLDBLog::Breakpoints), "invalid breakpoint name: {}",
815706b4fc4SDimitry Andric llvm::toString(expected_vector.takeError()));
816706b4fc4SDimitry Andric return;
817706b4fc4SDimitry Andric }
818706b4fc4SDimitry Andric
819706b4fc4SDimitry Andric for (auto bp_sp : *expected_vector)
820ef5d0b5eSDimitry Andric bp_name.ConfigureBreakpoint(bp_sp);
821ef5d0b5eSDimitry Andric }
822ef5d0b5eSDimitry Andric
GetBreakpointNames(std::vector<std::string> & names)823ead24645SDimitry Andric void Target::GetBreakpointNames(std::vector<std::string> &names) {
824ef5d0b5eSDimitry Andric names.clear();
825e3b55780SDimitry Andric for (const auto& bp_name_entry : m_breakpoint_names) {
826e3b55780SDimitry Andric names.push_back(bp_name_entry.first.AsCString());
827ef5d0b5eSDimitry Andric }
8284b4fe385SDimitry Andric llvm::sort(names);
829ef5d0b5eSDimitry Andric }
830ef5d0b5eSDimitry Andric
ProcessIsValid()83114f1b3e8SDimitry Andric bool Target::ProcessIsValid() {
832f034231aSEd Maste return (m_process_sp && m_process_sp->IsAlive());
833f034231aSEd Maste }
834f034231aSEd Maste
CheckIfWatchpointsSupported(Target * target,Status & error)83594994d37SDimitry Andric static bool CheckIfWatchpointsSupported(Target *target, Status &error) {
8367fa27ce4SDimitry Andric std::optional<uint32_t> num_supported_hardware_watchpoints =
8377fa27ce4SDimitry Andric target->GetProcessSP()->GetWatchpointSlotCount();
83894994d37SDimitry Andric
83994994d37SDimitry Andric // If unable to determine the # of watchpoints available,
84094994d37SDimitry Andric // assume they are supported.
8417fa27ce4SDimitry Andric if (!num_supported_hardware_watchpoints)
84294994d37SDimitry Andric return true;
84394994d37SDimitry Andric
844ac9a064cSDimitry Andric if (*num_supported_hardware_watchpoints == 0) {
84514f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
84614f1b3e8SDimitry Andric "Target supports (%u) hardware watchpoint slots.\n",
8477fa27ce4SDimitry Andric *num_supported_hardware_watchpoints);
848f034231aSEd Maste return false;
849f034231aSEd Maste }
850f3fbd1c0SDimitry Andric return true;
851f3fbd1c0SDimitry Andric }
852f034231aSEd Maste
853f73363f1SDimitry Andric // See also Watchpoint::SetWatchpointType(uint32_t type) and the
854f73363f1SDimitry Andric // OptionGroupWatchpoint::WatchType enum type.
CreateWatchpoint(lldb::addr_t addr,size_t size,const CompilerType * type,uint32_t kind,Status & error)85514f1b3e8SDimitry Andric WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size,
85614f1b3e8SDimitry Andric const CompilerType *type, uint32_t kind,
857b76161e4SDimitry Andric Status &error) {
858145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
859ead24645SDimitry Andric LLDB_LOGF(log,
860ead24645SDimitry Andric "Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64
86114f1b3e8SDimitry Andric " type = %u)\n",
862f034231aSEd Maste __FUNCTION__, addr, (uint64_t)size, kind);
863f034231aSEd Maste
864f034231aSEd Maste WatchpointSP wp_sp;
86514f1b3e8SDimitry Andric if (!ProcessIsValid()) {
866f034231aSEd Maste error.SetErrorString("process is not alive");
867f034231aSEd Maste return wp_sp;
868f034231aSEd Maste }
869f034231aSEd Maste
87014f1b3e8SDimitry Andric if (addr == LLDB_INVALID_ADDRESS || size == 0) {
871f034231aSEd Maste if (size == 0)
872f034231aSEd Maste error.SetErrorString("cannot set a watchpoint with watch_size of 0");
873f034231aSEd Maste else
874f034231aSEd Maste error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
875f034231aSEd Maste return wp_sp;
876f034231aSEd Maste }
877f034231aSEd Maste
87814f1b3e8SDimitry Andric if (!LLDB_WATCH_TYPE_IS_VALID(kind)) {
879f034231aSEd Maste error.SetErrorStringWithFormat("invalid watchpoint type: %d", kind);
880f034231aSEd Maste }
881f034231aSEd Maste
88294994d37SDimitry Andric if (!CheckIfWatchpointsSupported(this, error))
883f3fbd1c0SDimitry Andric return wp_sp;
884f3fbd1c0SDimitry Andric
885f73363f1SDimitry Andric // Currently we only support one watchpoint per address, with total number of
886f73363f1SDimitry Andric // watchpoints limited by the hardware which the inferior is running on.
887f034231aSEd Maste
888f034231aSEd Maste // Grab the list mutex while doing operations.
88914f1b3e8SDimitry Andric const bool notify = false; // Don't notify about all the state changes we do
89014f1b3e8SDimitry Andric // on creating the watchpoint.
891344a3780SDimitry Andric
892344a3780SDimitry Andric // Mask off ignored bits from watchpoint address.
893344a3780SDimitry Andric if (ABISP abi = m_process_sp->GetABI())
894344a3780SDimitry Andric addr = abi->FixDataAddress(addr);
895344a3780SDimitry Andric
896b1c73532SDimitry Andric // LWP_TODO this sequence is looking for an existing watchpoint
897b1c73532SDimitry Andric // at the exact same user-specified address, disables the new one
898b1c73532SDimitry Andric // if addr/size/type match. If type/size differ, disable old one.
899b1c73532SDimitry Andric // This isn't correct, we need both watchpoints to use a shared
900b1c73532SDimitry Andric // WatchpointResource in the target, and expand the WatchpointResource
901b1c73532SDimitry Andric // to handle the needs of both Watchpoints.
902b1c73532SDimitry Andric // Also, even if the addresses don't match, they may need to be
903b1c73532SDimitry Andric // supported by the same WatchpointResource, e.g. a watchpoint
904b1c73532SDimitry Andric // watching 1 byte at 0x102 and a watchpoint watching 1 byte at 0x103.
905b1c73532SDimitry Andric // They're in the same word and must be watched by a single hardware
906b1c73532SDimitry Andric // watchpoint register.
907b1c73532SDimitry Andric
908f3fbd1c0SDimitry Andric std::unique_lock<std::recursive_mutex> lock;
909f3fbd1c0SDimitry Andric this->GetWatchpointList().GetListMutex(lock);
910f034231aSEd Maste WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
91114f1b3e8SDimitry Andric if (matched_sp) {
912f034231aSEd Maste size_t old_size = matched_sp->GetByteSize();
913f034231aSEd Maste uint32_t old_type =
914f034231aSEd Maste (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
915b1c73532SDimitry Andric (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0) |
916b1c73532SDimitry Andric (matched_sp->WatchpointModify() ? LLDB_WATCH_TYPE_MODIFY : 0);
917f034231aSEd Maste // Return the existing watchpoint if both size and type match.
91814f1b3e8SDimitry Andric if (size == old_size && kind == old_type) {
919f034231aSEd Maste wp_sp = matched_sp;
920f034231aSEd Maste wp_sp->SetEnabled(false, notify);
92114f1b3e8SDimitry Andric } else {
922f034231aSEd Maste // Nil the matched watchpoint; we will be creating a new one.
923b1c73532SDimitry Andric m_process_sp->DisableWatchpoint(matched_sp, notify);
924f034231aSEd Maste m_watchpoint_list.Remove(matched_sp->GetID(), true);
925f034231aSEd Maste }
926f034231aSEd Maste }
927f034231aSEd Maste
92814f1b3e8SDimitry Andric if (!wp_sp) {
9295f29bb8aSDimitry Andric wp_sp = std::make_shared<Watchpoint>(*this, addr, size, type);
930f034231aSEd Maste wp_sp->SetWatchpointType(kind, notify);
931f034231aSEd Maste m_watchpoint_list.Add(wp_sp, true);
932f034231aSEd Maste }
933f034231aSEd Maste
934b1c73532SDimitry Andric error = m_process_sp->EnableWatchpoint(wp_sp, notify);
935ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (creation of watchpoint %s with id = %u)\n",
93614f1b3e8SDimitry Andric __FUNCTION__, error.Success() ? "succeeded" : "failed",
937f034231aSEd Maste wp_sp->GetID());
938f034231aSEd Maste
93914f1b3e8SDimitry Andric if (error.Fail()) {
940f73363f1SDimitry Andric // Enabling the watchpoint on the device side failed. Remove the said
941f73363f1SDimitry Andric // watchpoint from the list maintained by the target instance.
942f034231aSEd Maste m_watchpoint_list.Remove(wp_sp->GetID(), true);
943f034231aSEd Maste wp_sp.reset();
94414f1b3e8SDimitry Andric } else
945f034231aSEd Maste m_last_created_watchpoint = wp_sp;
946f034231aSEd Maste return wp_sp;
947f034231aSEd Maste }
948f034231aSEd Maste
RemoveAllowedBreakpoints()949ead24645SDimitry Andric void Target::RemoveAllowedBreakpoints() {
950145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
951ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s \n", __FUNCTION__);
952ef5d0b5eSDimitry Andric
953ef5d0b5eSDimitry Andric m_breakpoint_list.RemoveAllowed(true);
954ef5d0b5eSDimitry Andric
955ef5d0b5eSDimitry Andric m_last_created_breakpoint.reset();
956ef5d0b5eSDimitry Andric }
957ef5d0b5eSDimitry Andric
RemoveAllBreakpoints(bool internal_also)95814f1b3e8SDimitry Andric void Target::RemoveAllBreakpoints(bool internal_also) {
959145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
960ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
96114f1b3e8SDimitry Andric internal_also ? "yes" : "no");
962f034231aSEd Maste
963f034231aSEd Maste m_breakpoint_list.RemoveAll(true);
964f034231aSEd Maste if (internal_also)
965f034231aSEd Maste m_internal_breakpoint_list.RemoveAll(false);
966f034231aSEd Maste
967f034231aSEd Maste m_last_created_breakpoint.reset();
968f034231aSEd Maste }
969f034231aSEd Maste
DisableAllBreakpoints(bool internal_also)97014f1b3e8SDimitry Andric void Target::DisableAllBreakpoints(bool internal_also) {
971145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
972ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
97314f1b3e8SDimitry Andric internal_also ? "yes" : "no");
974f034231aSEd Maste
975f034231aSEd Maste m_breakpoint_list.SetEnabledAll(false);
976f034231aSEd Maste if (internal_also)
977f034231aSEd Maste m_internal_breakpoint_list.SetEnabledAll(false);
978f034231aSEd Maste }
979f034231aSEd Maste
DisableAllowedBreakpoints()980ef5d0b5eSDimitry Andric void Target::DisableAllowedBreakpoints() {
981145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
982ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s", __FUNCTION__);
983ef5d0b5eSDimitry Andric
984ef5d0b5eSDimitry Andric m_breakpoint_list.SetEnabledAllowed(false);
985ef5d0b5eSDimitry Andric }
986ef5d0b5eSDimitry Andric
EnableAllBreakpoints(bool internal_also)98714f1b3e8SDimitry Andric void Target::EnableAllBreakpoints(bool internal_also) {
988145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
989ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (internal_also = %s)\n", __FUNCTION__,
99014f1b3e8SDimitry Andric internal_also ? "yes" : "no");
991f034231aSEd Maste
992f034231aSEd Maste m_breakpoint_list.SetEnabledAll(true);
993f034231aSEd Maste if (internal_also)
994f034231aSEd Maste m_internal_breakpoint_list.SetEnabledAll(true);
995f034231aSEd Maste }
996f034231aSEd Maste
EnableAllowedBreakpoints()997ef5d0b5eSDimitry Andric void Target::EnableAllowedBreakpoints() {
998145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
999ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s", __FUNCTION__);
1000ef5d0b5eSDimitry Andric
1001ef5d0b5eSDimitry Andric m_breakpoint_list.SetEnabledAllowed(true);
1002ef5d0b5eSDimitry Andric }
1003ef5d0b5eSDimitry Andric
RemoveBreakpointByID(break_id_t break_id)100414f1b3e8SDimitry Andric bool Target::RemoveBreakpointByID(break_id_t break_id) {
1005145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
1006ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
100714f1b3e8SDimitry Andric break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
1008f034231aSEd Maste
100914f1b3e8SDimitry Andric if (DisableBreakpointByID(break_id)) {
1010f034231aSEd Maste if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
1011f034231aSEd Maste m_internal_breakpoint_list.Remove(break_id, false);
101214f1b3e8SDimitry Andric else {
101314f1b3e8SDimitry Andric if (m_last_created_breakpoint) {
1014f034231aSEd Maste if (m_last_created_breakpoint->GetID() == break_id)
1015f034231aSEd Maste m_last_created_breakpoint.reset();
1016f034231aSEd Maste }
1017f034231aSEd Maste m_breakpoint_list.Remove(break_id, true);
1018f034231aSEd Maste }
1019f034231aSEd Maste return true;
1020f034231aSEd Maste }
1021f034231aSEd Maste return false;
1022f034231aSEd Maste }
1023f034231aSEd Maste
DisableBreakpointByID(break_id_t break_id)102414f1b3e8SDimitry Andric bool Target::DisableBreakpointByID(break_id_t break_id) {
1025145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
1026ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
102714f1b3e8SDimitry Andric break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
1028f034231aSEd Maste
1029f034231aSEd Maste BreakpointSP bp_sp;
1030f034231aSEd Maste
1031f034231aSEd Maste if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
1032f034231aSEd Maste bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
1033f034231aSEd Maste else
1034f034231aSEd Maste bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
103514f1b3e8SDimitry Andric if (bp_sp) {
1036f034231aSEd Maste bp_sp->SetEnabled(false);
1037f034231aSEd Maste return true;
1038f034231aSEd Maste }
1039f034231aSEd Maste return false;
1040f034231aSEd Maste }
1041f034231aSEd Maste
EnableBreakpointByID(break_id_t break_id)104214f1b3e8SDimitry Andric bool Target::EnableBreakpointByID(break_id_t break_id) {
1043145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
1044ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
104514f1b3e8SDimitry Andric break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
1046f034231aSEd Maste
1047f034231aSEd Maste BreakpointSP bp_sp;
1048f034231aSEd Maste
1049f034231aSEd Maste if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
1050f034231aSEd Maste bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
1051f034231aSEd Maste else
1052f034231aSEd Maste bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
1053f034231aSEd Maste
105414f1b3e8SDimitry Andric if (bp_sp) {
1055f034231aSEd Maste bp_sp->SetEnabled(true);
1056f034231aSEd Maste return true;
1057f034231aSEd Maste }
1058f034231aSEd Maste return false;
1059f034231aSEd Maste }
1060f034231aSEd Maste
ResetBreakpointHitCounts()1061e3b55780SDimitry Andric void Target::ResetBreakpointHitCounts() {
1062e3b55780SDimitry Andric GetBreakpointList().ResetHitCounts();
1063e3b55780SDimitry Andric }
1064e3b55780SDimitry Andric
SerializeBreakpointsToFile(const FileSpec & file,const BreakpointIDList & bp_ids,bool append)1065b76161e4SDimitry Andric Status Target::SerializeBreakpointsToFile(const FileSpec &file,
106614f1b3e8SDimitry Andric const BreakpointIDList &bp_ids,
106714f1b3e8SDimitry Andric bool append) {
1068b76161e4SDimitry Andric Status error;
106914f1b3e8SDimitry Andric
107014f1b3e8SDimitry Andric if (!file) {
107114f1b3e8SDimitry Andric error.SetErrorString("Invalid FileSpec.");
107214f1b3e8SDimitry Andric return error;
107314f1b3e8SDimitry Andric }
107414f1b3e8SDimitry Andric
107514f1b3e8SDimitry Andric std::string path(file.GetPath());
107614f1b3e8SDimitry Andric StructuredData::ObjectSP input_data_sp;
107714f1b3e8SDimitry Andric
107814f1b3e8SDimitry Andric StructuredData::ArraySP break_store_sp;
107914f1b3e8SDimitry Andric StructuredData::Array *break_store_ptr = nullptr;
108014f1b3e8SDimitry Andric
108114f1b3e8SDimitry Andric if (append) {
108214f1b3e8SDimitry Andric input_data_sp = StructuredData::ParseJSONFromFile(file, error);
108314f1b3e8SDimitry Andric if (error.Success()) {
108414f1b3e8SDimitry Andric break_store_ptr = input_data_sp->GetAsArray();
108514f1b3e8SDimitry Andric if (!break_store_ptr) {
108614f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
108714f1b3e8SDimitry Andric "Tried to append to invalid input file %s", path.c_str());
108814f1b3e8SDimitry Andric return error;
108914f1b3e8SDimitry Andric }
109014f1b3e8SDimitry Andric }
109114f1b3e8SDimitry Andric }
109214f1b3e8SDimitry Andric
109314f1b3e8SDimitry Andric if (!break_store_ptr) {
10945f29bb8aSDimitry Andric break_store_sp = std::make_shared<StructuredData::Array>();
109514f1b3e8SDimitry Andric break_store_ptr = break_store_sp.get();
109614f1b3e8SDimitry Andric }
109714f1b3e8SDimitry Andric
109814f1b3e8SDimitry Andric StreamFile out_file(path.c_str(),
1099c0981da4SDimitry Andric File::eOpenOptionTruncate | File::eOpenOptionWriteOnly |
1100ead24645SDimitry Andric File::eOpenOptionCanCreate |
1101ead24645SDimitry Andric File::eOpenOptionCloseOnExec,
110214f1b3e8SDimitry Andric lldb::eFilePermissionsFileDefault);
110314f1b3e8SDimitry Andric if (!out_file.GetFile().IsValid()) {
110414f1b3e8SDimitry Andric error.SetErrorStringWithFormat("Unable to open output file: %s.",
110514f1b3e8SDimitry Andric path.c_str());
110614f1b3e8SDimitry Andric return error;
110714f1b3e8SDimitry Andric }
110814f1b3e8SDimitry Andric
110914f1b3e8SDimitry Andric std::unique_lock<std::recursive_mutex> lock;
111014f1b3e8SDimitry Andric GetBreakpointList().GetListMutex(lock);
111114f1b3e8SDimitry Andric
111214f1b3e8SDimitry Andric if (bp_ids.GetSize() == 0) {
111314f1b3e8SDimitry Andric const BreakpointList &breakpoints = GetBreakpointList();
111414f1b3e8SDimitry Andric
111514f1b3e8SDimitry Andric size_t num_breakpoints = breakpoints.GetSize();
111614f1b3e8SDimitry Andric for (size_t i = 0; i < num_breakpoints; i++) {
111714f1b3e8SDimitry Andric Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get();
111814f1b3e8SDimitry Andric StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
111914f1b3e8SDimitry Andric // If a breakpoint can't serialize it, just ignore it for now:
112014f1b3e8SDimitry Andric if (bkpt_save_sp)
112114f1b3e8SDimitry Andric break_store_ptr->AddItem(bkpt_save_sp);
112214f1b3e8SDimitry Andric }
112314f1b3e8SDimitry Andric } else {
112414f1b3e8SDimitry Andric
112514f1b3e8SDimitry Andric std::unordered_set<lldb::break_id_t> processed_bkpts;
112614f1b3e8SDimitry Andric const size_t count = bp_ids.GetSize();
112714f1b3e8SDimitry Andric for (size_t i = 0; i < count; ++i) {
112814f1b3e8SDimitry Andric BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(i);
112914f1b3e8SDimitry Andric lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID();
113014f1b3e8SDimitry Andric
113114f1b3e8SDimitry Andric if (bp_id != LLDB_INVALID_BREAK_ID) {
113214f1b3e8SDimitry Andric // Only do each breakpoint once:
113314f1b3e8SDimitry Andric std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool>
113414f1b3e8SDimitry Andric insert_result = processed_bkpts.insert(bp_id);
113514f1b3e8SDimitry Andric if (!insert_result.second)
113614f1b3e8SDimitry Andric continue;
113714f1b3e8SDimitry Andric
113814f1b3e8SDimitry Andric Breakpoint *bp = GetBreakpointByID(bp_id).get();
113914f1b3e8SDimitry Andric StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
114014f1b3e8SDimitry Andric // If the user explicitly asked to serialize a breakpoint, and we
1141f73363f1SDimitry Andric // can't, then raise an error:
114214f1b3e8SDimitry Andric if (!bkpt_save_sp) {
114314f1b3e8SDimitry Andric error.SetErrorStringWithFormat("Unable to serialize breakpoint %d",
114414f1b3e8SDimitry Andric bp_id);
114514f1b3e8SDimitry Andric return error;
114614f1b3e8SDimitry Andric }
114714f1b3e8SDimitry Andric break_store_ptr->AddItem(bkpt_save_sp);
114814f1b3e8SDimitry Andric }
114914f1b3e8SDimitry Andric }
115014f1b3e8SDimitry Andric }
115114f1b3e8SDimitry Andric
115214f1b3e8SDimitry Andric break_store_ptr->Dump(out_file, false);
115314f1b3e8SDimitry Andric out_file.PutChar('\n');
115414f1b3e8SDimitry Andric return error;
115514f1b3e8SDimitry Andric }
115614f1b3e8SDimitry Andric
CreateBreakpointsFromFile(const FileSpec & file,BreakpointIDList & new_bps)1157b76161e4SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
115814f1b3e8SDimitry Andric BreakpointIDList &new_bps) {
115914f1b3e8SDimitry Andric std::vector<std::string> no_names;
116014f1b3e8SDimitry Andric return CreateBreakpointsFromFile(file, no_names, new_bps);
116114f1b3e8SDimitry Andric }
116214f1b3e8SDimitry Andric
CreateBreakpointsFromFile(const FileSpec & file,std::vector<std::string> & names,BreakpointIDList & new_bps)1163b76161e4SDimitry Andric Status Target::CreateBreakpointsFromFile(const FileSpec &file,
116414f1b3e8SDimitry Andric std::vector<std::string> &names,
116514f1b3e8SDimitry Andric BreakpointIDList &new_bps) {
116614f1b3e8SDimitry Andric std::unique_lock<std::recursive_mutex> lock;
116714f1b3e8SDimitry Andric GetBreakpointList().GetListMutex(lock);
116814f1b3e8SDimitry Andric
1169b76161e4SDimitry Andric Status error;
117014f1b3e8SDimitry Andric StructuredData::ObjectSP input_data_sp =
117114f1b3e8SDimitry Andric StructuredData::ParseJSONFromFile(file, error);
117214f1b3e8SDimitry Andric if (!error.Success()) {
117314f1b3e8SDimitry Andric return error;
117414f1b3e8SDimitry Andric } else if (!input_data_sp || !input_data_sp->IsValid()) {
117514f1b3e8SDimitry Andric error.SetErrorStringWithFormat("Invalid JSON from input file: %s.",
117614f1b3e8SDimitry Andric file.GetPath().c_str());
117714f1b3e8SDimitry Andric return error;
117814f1b3e8SDimitry Andric }
117914f1b3e8SDimitry Andric
118014f1b3e8SDimitry Andric StructuredData::Array *bkpt_array = input_data_sp->GetAsArray();
118114f1b3e8SDimitry Andric if (!bkpt_array) {
118214f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
118314f1b3e8SDimitry Andric "Invalid breakpoint data from input file: %s.", file.GetPath().c_str());
118414f1b3e8SDimitry Andric return error;
118514f1b3e8SDimitry Andric }
118614f1b3e8SDimitry Andric
118714f1b3e8SDimitry Andric size_t num_bkpts = bkpt_array->GetSize();
118814f1b3e8SDimitry Andric size_t num_names = names.size();
118914f1b3e8SDimitry Andric
119014f1b3e8SDimitry Andric for (size_t i = 0; i < num_bkpts; i++) {
119114f1b3e8SDimitry Andric StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(i);
119214f1b3e8SDimitry Andric // Peel off the breakpoint key, and feed the rest to the Breakpoint:
119314f1b3e8SDimitry Andric StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary();
119414f1b3e8SDimitry Andric if (!bkpt_dict) {
119514f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
119614f1b3e8SDimitry Andric "Invalid breakpoint data for element %zu from input file: %s.", i,
119714f1b3e8SDimitry Andric file.GetPath().c_str());
119814f1b3e8SDimitry Andric return error;
119914f1b3e8SDimitry Andric }
120014f1b3e8SDimitry Andric StructuredData::ObjectSP bkpt_data_sp =
120114f1b3e8SDimitry Andric bkpt_dict->GetValueForKey(Breakpoint::GetSerializationKey());
120214f1b3e8SDimitry Andric if (num_names &&
120314f1b3e8SDimitry Andric !Breakpoint::SerializedBreakpointMatchesNames(bkpt_data_sp, names))
120414f1b3e8SDimitry Andric continue;
120514f1b3e8SDimitry Andric
1206cfca06d7SDimitry Andric BreakpointSP bkpt_sp = Breakpoint::CreateFromStructuredData(
1207cfca06d7SDimitry Andric shared_from_this(), bkpt_data_sp, error);
120814f1b3e8SDimitry Andric if (!error.Success()) {
120914f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
121014f1b3e8SDimitry Andric "Error restoring breakpoint %zu from %s: %s.", i,
121114f1b3e8SDimitry Andric file.GetPath().c_str(), error.AsCString());
121214f1b3e8SDimitry Andric return error;
121314f1b3e8SDimitry Andric }
121414f1b3e8SDimitry Andric new_bps.AddBreakpointID(BreakpointID(bkpt_sp->GetID()));
121514f1b3e8SDimitry Andric }
121614f1b3e8SDimitry Andric return error;
121714f1b3e8SDimitry Andric }
121814f1b3e8SDimitry Andric
1219f034231aSEd Maste // The flag 'end_to_end', default to true, signifies that the operation is
1220f034231aSEd Maste // performed end to end, for both the debugger and the debuggee.
1221f034231aSEd Maste
1222f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1223f034231aSEd Maste // to end operations.
RemoveAllWatchpoints(bool end_to_end)122414f1b3e8SDimitry Andric bool Target::RemoveAllWatchpoints(bool end_to_end) {
1225145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1226ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1227f034231aSEd Maste
1228f034231aSEd Maste if (!end_to_end) {
1229f034231aSEd Maste m_watchpoint_list.RemoveAll(true);
1230f034231aSEd Maste return true;
1231f034231aSEd Maste }
1232f034231aSEd Maste
1233f034231aSEd Maste // Otherwise, it's an end to end operation.
1234f034231aSEd Maste
1235f034231aSEd Maste if (!ProcessIsValid())
1236f034231aSEd Maste return false;
1237f034231aSEd Maste
1238344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
1239f034231aSEd Maste if (!wp_sp)
1240f034231aSEd Maste return false;
1241f034231aSEd Maste
1242b1c73532SDimitry Andric Status rc = m_process_sp->DisableWatchpoint(wp_sp);
1243f034231aSEd Maste if (rc.Fail())
1244f034231aSEd Maste return false;
1245f034231aSEd Maste }
1246f034231aSEd Maste m_watchpoint_list.RemoveAll(true);
1247f034231aSEd Maste m_last_created_watchpoint.reset();
1248f034231aSEd Maste return true; // Success!
1249f034231aSEd Maste }
1250f034231aSEd Maste
1251f73363f1SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1252f73363f1SDimitry Andric // to end operations.
DisableAllWatchpoints(bool end_to_end)125314f1b3e8SDimitry Andric bool Target::DisableAllWatchpoints(bool end_to_end) {
1254145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1255ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1256f034231aSEd Maste
1257f034231aSEd Maste if (!end_to_end) {
1258f034231aSEd Maste m_watchpoint_list.SetEnabledAll(false);
1259f034231aSEd Maste return true;
1260f034231aSEd Maste }
1261f034231aSEd Maste
1262f034231aSEd Maste // Otherwise, it's an end to end operation.
1263f034231aSEd Maste
1264f034231aSEd Maste if (!ProcessIsValid())
1265f034231aSEd Maste return false;
1266f034231aSEd Maste
1267344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
1268f034231aSEd Maste if (!wp_sp)
1269f034231aSEd Maste return false;
1270f034231aSEd Maste
1271b1c73532SDimitry Andric Status rc = m_process_sp->DisableWatchpoint(wp_sp);
1272f034231aSEd Maste if (rc.Fail())
1273f034231aSEd Maste return false;
1274f034231aSEd Maste }
1275f034231aSEd Maste return true; // Success!
1276f034231aSEd Maste }
1277f034231aSEd Maste
1278f73363f1SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1279f73363f1SDimitry Andric // to end operations.
EnableAllWatchpoints(bool end_to_end)128014f1b3e8SDimitry Andric bool Target::EnableAllWatchpoints(bool end_to_end) {
1281145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1282ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1283f034231aSEd Maste
1284f034231aSEd Maste if (!end_to_end) {
1285f034231aSEd Maste m_watchpoint_list.SetEnabledAll(true);
1286f034231aSEd Maste return true;
1287f034231aSEd Maste }
1288f034231aSEd Maste
1289f034231aSEd Maste // Otherwise, it's an end to end operation.
1290f034231aSEd Maste
1291f034231aSEd Maste if (!ProcessIsValid())
1292f034231aSEd Maste return false;
1293f034231aSEd Maste
1294344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
1295f034231aSEd Maste if (!wp_sp)
1296f034231aSEd Maste return false;
1297f034231aSEd Maste
1298b1c73532SDimitry Andric Status rc = m_process_sp->EnableWatchpoint(wp_sp);
1299f034231aSEd Maste if (rc.Fail())
1300f034231aSEd Maste return false;
1301f034231aSEd Maste }
1302f034231aSEd Maste return true; // Success!
1303f034231aSEd Maste }
1304f034231aSEd Maste
1305f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
ClearAllWatchpointHitCounts()130614f1b3e8SDimitry Andric bool Target::ClearAllWatchpointHitCounts() {
1307145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1308ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1309f034231aSEd Maste
1310344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
1311f034231aSEd Maste if (!wp_sp)
1312f034231aSEd Maste return false;
1313f034231aSEd Maste
1314f034231aSEd Maste wp_sp->ResetHitCount();
1315f034231aSEd Maste }
1316f034231aSEd Maste return true; // Success!
1317f034231aSEd Maste }
1318f034231aSEd Maste
13195e95aa85SEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
ClearAllWatchpointHistoricValues()132014f1b3e8SDimitry Andric bool Target::ClearAllWatchpointHistoricValues() {
1321145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1322ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
13235e95aa85SEd Maste
1324344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
13255e95aa85SEd Maste if (!wp_sp)
13265e95aa85SEd Maste return false;
13275e95aa85SEd Maste
13285e95aa85SEd Maste wp_sp->ResetHistoricValues();
13295e95aa85SEd Maste }
13305e95aa85SEd Maste return true; // Success!
13315e95aa85SEd Maste }
13325e95aa85SEd Maste
1333f73363f1SDimitry Andric // Assumption: Caller holds the list mutex lock for m_watchpoint_list during
1334f73363f1SDimitry Andric // these operations.
IgnoreAllWatchpoints(uint32_t ignore_count)133514f1b3e8SDimitry Andric bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) {
1336145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1337ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s\n", __FUNCTION__);
1338f034231aSEd Maste
1339f034231aSEd Maste if (!ProcessIsValid())
1340f034231aSEd Maste return false;
1341f034231aSEd Maste
1342344a3780SDimitry Andric for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) {
1343f034231aSEd Maste if (!wp_sp)
1344f034231aSEd Maste return false;
1345f034231aSEd Maste
1346f034231aSEd Maste wp_sp->SetIgnoreCount(ignore_count);
1347f034231aSEd Maste }
1348f034231aSEd Maste return true; // Success!
1349f034231aSEd Maste }
1350f034231aSEd Maste
1351f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
DisableWatchpointByID(lldb::watch_id_t watch_id)135214f1b3e8SDimitry Andric bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) {
1353145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1354ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1355f034231aSEd Maste
1356f034231aSEd Maste if (!ProcessIsValid())
1357f034231aSEd Maste return false;
1358f034231aSEd Maste
1359f034231aSEd Maste WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
136014f1b3e8SDimitry Andric if (wp_sp) {
1361b1c73532SDimitry Andric Status rc = m_process_sp->DisableWatchpoint(wp_sp);
1362f034231aSEd Maste if (rc.Success())
1363f034231aSEd Maste return true;
1364f034231aSEd Maste
1365f034231aSEd Maste // Else, fallthrough.
1366f034231aSEd Maste }
1367f034231aSEd Maste return false;
1368f034231aSEd Maste }
1369f034231aSEd Maste
1370f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
EnableWatchpointByID(lldb::watch_id_t watch_id)137114f1b3e8SDimitry Andric bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) {
1372145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1373ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1374f034231aSEd Maste
1375f034231aSEd Maste if (!ProcessIsValid())
1376f034231aSEd Maste return false;
1377f034231aSEd Maste
1378f034231aSEd Maste WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
137914f1b3e8SDimitry Andric if (wp_sp) {
1380b1c73532SDimitry Andric Status rc = m_process_sp->EnableWatchpoint(wp_sp);
1381f034231aSEd Maste if (rc.Success())
1382f034231aSEd Maste return true;
1383f034231aSEd Maste
1384f034231aSEd Maste // Else, fallthrough.
1385f034231aSEd Maste }
1386f034231aSEd Maste return false;
1387f034231aSEd Maste }
1388f034231aSEd Maste
1389f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
RemoveWatchpointByID(lldb::watch_id_t watch_id)139014f1b3e8SDimitry Andric bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) {
1391145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1392ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1393f034231aSEd Maste
1394f034231aSEd Maste WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1395f034231aSEd Maste if (watch_to_remove_sp == m_last_created_watchpoint)
1396f034231aSEd Maste m_last_created_watchpoint.reset();
1397f034231aSEd Maste
139814f1b3e8SDimitry Andric if (DisableWatchpointByID(watch_id)) {
1399f034231aSEd Maste m_watchpoint_list.Remove(watch_id, true);
1400f034231aSEd Maste return true;
1401f034231aSEd Maste }
1402f034231aSEd Maste return false;
1403f034231aSEd Maste }
1404f034231aSEd Maste
1405f034231aSEd Maste // Assumption: Caller holds the list mutex lock for m_watchpoint_list.
IgnoreWatchpointByID(lldb::watch_id_t watch_id,uint32_t ignore_count)140614f1b3e8SDimitry Andric bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id,
140714f1b3e8SDimitry Andric uint32_t ignore_count) {
1408145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints);
1409ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1410f034231aSEd Maste
1411f034231aSEd Maste if (!ProcessIsValid())
1412f034231aSEd Maste return false;
1413f034231aSEd Maste
1414f034231aSEd Maste WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
141514f1b3e8SDimitry Andric if (wp_sp) {
1416f034231aSEd Maste wp_sp->SetIgnoreCount(ignore_count);
1417f034231aSEd Maste return true;
1418f034231aSEd Maste }
1419f034231aSEd Maste return false;
1420f034231aSEd Maste }
1421f034231aSEd Maste
GetExecutableModule()142214f1b3e8SDimitry Andric ModuleSP Target::GetExecutableModule() {
14235e95aa85SEd Maste // search for the first executable in the module list
142414f1b3e8SDimitry Andric for (size_t i = 0; i < m_images.GetSize(); ++i) {
14255e95aa85SEd Maste ModuleSP module_sp = m_images.GetModuleAtIndex(i);
14265e95aa85SEd Maste lldb_private::ObjectFile *obj = module_sp->GetObjectFile();
14275e95aa85SEd Maste if (obj == nullptr)
14285e95aa85SEd Maste continue;
14295e95aa85SEd Maste if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
14305e95aa85SEd Maste return module_sp;
14315e95aa85SEd Maste }
14325e95aa85SEd Maste // as fall back return the first module loaded
1433f034231aSEd Maste return m_images.GetModuleAtIndex(0);
1434f034231aSEd Maste }
1435f034231aSEd Maste
GetExecutableModulePointer()143614f1b3e8SDimitry Andric Module *Target::GetExecutableModulePointer() {
14375e95aa85SEd Maste return GetExecutableModule().get();
1438f034231aSEd Maste }
1439f034231aSEd Maste
LoadScriptingResourceForModule(const ModuleSP & module_sp,Target * target)144014f1b3e8SDimitry Andric static void LoadScriptingResourceForModule(const ModuleSP &module_sp,
144114f1b3e8SDimitry Andric Target *target) {
1442b76161e4SDimitry Andric Status error;
1443f034231aSEd Maste StreamString feedback_stream;
14447fa27ce4SDimitry Andric if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error,
14457fa27ce4SDimitry Andric feedback_stream)) {
1446f034231aSEd Maste if (error.AsCString())
1447ead24645SDimitry Andric target->GetDebugger().GetErrorStream().Printf(
144814f1b3e8SDimitry Andric "unable to load scripting data for module %s - error reported was "
144914f1b3e8SDimitry Andric "%s\n",
1450f034231aSEd Maste module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1451f034231aSEd Maste error.AsCString());
14520cac4ca3SEd Maste }
1453f034231aSEd Maste if (feedback_stream.GetSize())
1454ead24645SDimitry Andric target->GetDebugger().GetErrorStream().Printf("%s\n",
1455f034231aSEd Maste feedback_stream.GetData());
1456f034231aSEd Maste }
1457f034231aSEd Maste
ClearModules(bool delete_locations)145814f1b3e8SDimitry Andric void Target::ClearModules(bool delete_locations) {
145986758c71SEd Maste ModulesDidUnload(m_images, delete_locations);
1460866dcdacSEd Maste m_section_load_history.Clear();
1461f034231aSEd Maste m_images.Clear();
1462e81d9d49SDimitry Andric m_scratch_type_system_map.Clear();
1463f21a844fSEd Maste }
1464f21a844fSEd Maste
DidExec()146514f1b3e8SDimitry Andric void Target::DidExec() {
146686758c71SEd Maste // When a process exec's we need to know about it so we can do some cleanup.
1467ef5d0b5eSDimitry Andric m_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
1468ef5d0b5eSDimitry Andric m_internal_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
146986758c71SEd Maste }
147086758c71SEd Maste
SetExecutableModule(ModuleSP & executable_sp,LoadDependentFiles load_dependent_files)147114f1b3e8SDimitry Andric void Target::SetExecutableModule(ModuleSP &executable_sp,
147294994d37SDimitry Andric LoadDependentFiles load_dependent_files) {
1473145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Target);
147486758c71SEd Maste ClearModules(false);
1475f034231aSEd Maste
147614f1b3e8SDimitry Andric if (executable_sp) {
1477c0981da4SDimitry Andric ElapsedTime elapsed(m_stats.GetCreateTime());
1478b60736ecSDimitry Andric LLDB_SCOPED_TIMERF("Target::SetExecutableModule (executable = '%s')",
1479f034231aSEd Maste executable_sp->GetFileSpec().GetPath().c_str());
1480f034231aSEd Maste
14815f29bb8aSDimitry Andric const bool notify = true;
1482ead24645SDimitry Andric m_images.Append(executable_sp,
1483ead24645SDimitry Andric notify); // The first image is our executable file
1484f034231aSEd Maste
148514f1b3e8SDimitry Andric // If we haven't set an architecture yet, reset our architecture based on
148614f1b3e8SDimitry Andric // what we found in the executable module.
1487ef5d0b5eSDimitry Andric if (!m_arch.GetSpec().IsValid()) {
1488f034231aSEd Maste m_arch = executable_sp->GetArchitecture();
1489ef5d0b5eSDimitry Andric LLDB_LOG(log,
1490e3b55780SDimitry Andric "Target::SetExecutableModule setting architecture to {0} ({1}) "
1491e3b55780SDimitry Andric "based on executable file",
1492ef5d0b5eSDimitry Andric m_arch.GetSpec().GetArchitectureName(),
1493ef5d0b5eSDimitry Andric m_arch.GetSpec().GetTriple().getTriple());
1494f034231aSEd Maste }
1495f034231aSEd Maste
1496f034231aSEd Maste FileSpecList dependent_files;
1497f034231aSEd Maste ObjectFile *executable_objfile = executable_sp->GetObjectFile();
149894994d37SDimitry Andric bool load_dependents = true;
149994994d37SDimitry Andric switch (load_dependent_files) {
150094994d37SDimitry Andric case eLoadDependentsDefault:
150194994d37SDimitry Andric load_dependents = executable_sp->IsExecutable();
150294994d37SDimitry Andric break;
150394994d37SDimitry Andric case eLoadDependentsYes:
150494994d37SDimitry Andric load_dependents = true;
150594994d37SDimitry Andric break;
150694994d37SDimitry Andric case eLoadDependentsNo:
150794994d37SDimitry Andric load_dependents = false;
150894994d37SDimitry Andric break;
150994994d37SDimitry Andric }
1510f034231aSEd Maste
151194994d37SDimitry Andric if (executable_objfile && load_dependents) {
15125f29bb8aSDimitry Andric ModuleList added_modules;
1513f034231aSEd Maste executable_objfile->GetDependentModules(dependent_files);
151414f1b3e8SDimitry Andric for (uint32_t i = 0; i < dependent_files.GetSize(); i++) {
1515706b4fc4SDimitry Andric FileSpec dependent_file_spec(dependent_files.GetFileSpecAtIndex(i));
1516f034231aSEd Maste FileSpec platform_dependent_file_spec;
1517f034231aSEd Maste if (m_platform_sp)
151814f1b3e8SDimitry Andric m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr,
151914f1b3e8SDimitry Andric platform_dependent_file_spec);
1520f034231aSEd Maste else
1521f034231aSEd Maste platform_dependent_file_spec = dependent_file_spec;
1522f034231aSEd Maste
1523ef5d0b5eSDimitry Andric ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec());
1524ead24645SDimitry Andric ModuleSP image_module_sp(
1525ead24645SDimitry Andric GetOrCreateModule(module_spec, false /* notify */));
152614f1b3e8SDimitry Andric if (image_module_sp) {
15275f29bb8aSDimitry Andric added_modules.AppendIfNeeded(image_module_sp, false);
1528f034231aSEd Maste ObjectFile *objfile = image_module_sp->GetObjectFile();
1529f034231aSEd Maste if (objfile)
1530f034231aSEd Maste objfile->GetDependentModules(dependent_files);
1531f034231aSEd Maste }
1532f034231aSEd Maste }
15335f29bb8aSDimitry Andric ModulesDidLoad(added_modules);
1534f034231aSEd Maste }
1535f034231aSEd Maste }
1536f034231aSEd Maste }
1537f034231aSEd Maste
SetArchitecture(const ArchSpec & arch_spec,bool set_platform,bool merge)1538e3b55780SDimitry Andric bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform,
1539e3b55780SDimitry Andric bool merge) {
1540145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Target);
1541ef5d0b5eSDimitry Andric bool missing_local_arch = !m_arch.GetSpec().IsValid();
1542e81d9d49SDimitry Andric bool replace_local_arch = true;
1543e81d9d49SDimitry Andric bool compatible_local_arch = false;
1544e81d9d49SDimitry Andric ArchSpec other(arch_spec);
1545e81d9d49SDimitry Andric
154694994d37SDimitry Andric // Changing the architecture might mean that the currently selected platform
154794994d37SDimitry Andric // isn't compatible. Set the platform correctly if we are asked to do so,
154894994d37SDimitry Andric // otherwise assume the user will set the platform manually.
154994994d37SDimitry Andric if (set_platform) {
155094994d37SDimitry Andric if (other.IsValid()) {
155194994d37SDimitry Andric auto platform_sp = GetPlatform();
1552e3b55780SDimitry Andric if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
1553e3b55780SDimitry Andric other, {}, ArchSpec::CompatibleMatch, nullptr)) {
155494994d37SDimitry Andric ArchSpec platform_arch;
1555145449b1SDimitry Andric if (PlatformSP arch_platform_sp =
1556145449b1SDimitry Andric GetDebugger().GetPlatformList().GetOrCreate(other, {},
1557145449b1SDimitry Andric &platform_arch)) {
155894994d37SDimitry Andric SetPlatform(arch_platform_sp);
155994994d37SDimitry Andric if (platform_arch.IsValid())
156094994d37SDimitry Andric other = platform_arch;
156194994d37SDimitry Andric }
156294994d37SDimitry Andric }
156394994d37SDimitry Andric }
156494994d37SDimitry Andric }
156594994d37SDimitry Andric
156614f1b3e8SDimitry Andric if (!missing_local_arch) {
1567e3b55780SDimitry Andric if (merge && m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
1568ef5d0b5eSDimitry Andric other.MergeFrom(m_arch.GetSpec());
1569e81d9d49SDimitry Andric
1570ef5d0b5eSDimitry Andric if (m_arch.GetSpec().IsCompatibleMatch(other)) {
1571e81d9d49SDimitry Andric compatible_local_arch = true;
1572e81d9d49SDimitry Andric
1573ac9a064cSDimitry Andric if (m_arch.GetSpec().GetTriple() == other.GetTriple())
1574e81d9d49SDimitry Andric replace_local_arch = false;
1575e81d9d49SDimitry Andric }
1576e81d9d49SDimitry Andric }
1577e81d9d49SDimitry Andric }
1578e81d9d49SDimitry Andric
157914f1b3e8SDimitry Andric if (compatible_local_arch || missing_local_arch) {
1580e81d9d49SDimitry Andric // If we haven't got a valid arch spec, or the architectures are compatible
1581f73363f1SDimitry Andric // update the architecture, unless the one we already have is more
1582f73363f1SDimitry Andric // specified
1583e81d9d49SDimitry Andric if (replace_local_arch)
1584e81d9d49SDimitry Andric m_arch = other;
1585e3b55780SDimitry Andric LLDB_LOG(log,
1586e3b55780SDimitry Andric "Target::SetArchitecture merging compatible arch; arch "
1587e3b55780SDimitry Andric "is now {0} ({1})",
1588ef5d0b5eSDimitry Andric m_arch.GetSpec().GetArchitectureName(),
1589ef5d0b5eSDimitry Andric m_arch.GetSpec().GetTriple().getTriple());
1590f034231aSEd Maste return true;
1591f034231aSEd Maste }
1592e81d9d49SDimitry Andric
159314f1b3e8SDimitry Andric // If we have an executable file, try to reset the executable to the desired
159414f1b3e8SDimitry Andric // architecture
1595e3b55780SDimitry Andric LLDB_LOGF(
1596e3b55780SDimitry Andric log,
1597e3b55780SDimitry Andric "Target::SetArchitecture changing architecture to %s (%s) from %s (%s)",
159814f1b3e8SDimitry Andric arch_spec.GetArchitectureName(),
1599e3b55780SDimitry Andric arch_spec.GetTriple().getTriple().c_str(),
1600e3b55780SDimitry Andric m_arch.GetSpec().GetArchitectureName(),
1601e3b55780SDimitry Andric m_arch.GetSpec().GetTriple().getTriple().c_str());
1602e81d9d49SDimitry Andric m_arch = other;
1603f034231aSEd Maste ModuleSP executable_sp = GetExecutableModule();
1604f21a844fSEd Maste
160586758c71SEd Maste ClearModules(true);
1606f034231aSEd Maste // Need to do something about unsetting breakpoints.
1607f034231aSEd Maste
160814f1b3e8SDimitry Andric if (executable_sp) {
1609ead24645SDimitry Andric LLDB_LOGF(log,
1610ead24645SDimitry Andric "Target::SetArchitecture Trying to select executable file "
161114f1b3e8SDimitry Andric "architecture %s (%s)",
161214f1b3e8SDimitry Andric arch_spec.GetArchitectureName(),
161314f1b3e8SDimitry Andric arch_spec.GetTriple().getTriple().c_str());
1614e81d9d49SDimitry Andric ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
16155f29bb8aSDimitry Andric FileSpecList search_paths = GetExecutableSearchPaths();
1616b76161e4SDimitry Andric Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
1617ead24645SDimitry Andric &search_paths, nullptr, nullptr);
1618f034231aSEd Maste
161914f1b3e8SDimitry Andric if (!error.Fail() && executable_sp) {
162094994d37SDimitry Andric SetExecutableModule(executable_sp, eLoadDependentsYes);
1621f034231aSEd Maste return true;
1622f034231aSEd Maste }
1623f034231aSEd Maste }
1624f034231aSEd Maste return false;
1625f034231aSEd Maste }
1626f034231aSEd Maste
MergeArchitecture(const ArchSpec & arch_spec)162714f1b3e8SDimitry Andric bool Target::MergeArchitecture(const ArchSpec &arch_spec) {
1628145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Target);
162914f1b3e8SDimitry Andric if (arch_spec.IsValid()) {
1630ef5d0b5eSDimitry Andric if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
1631f73363f1SDimitry Andric // The current target arch is compatible with "arch_spec", see if we can
1632f73363f1SDimitry Andric // improve our current architecture using bits from "arch_spec"
16335e95aa85SEd Maste
1634ead24645SDimitry Andric LLDB_LOGF(log,
1635ead24645SDimitry Andric "Target::MergeArchitecture target has arch %s, merging with "
163694994d37SDimitry Andric "arch %s",
163794994d37SDimitry Andric m_arch.GetSpec().GetTriple().getTriple().c_str(),
163894994d37SDimitry Andric arch_spec.GetTriple().getTriple().c_str());
163994994d37SDimitry Andric
16405e95aa85SEd Maste // Merge bits from arch_spec into "merged_arch" and set our architecture
1641ef5d0b5eSDimitry Andric ArchSpec merged_arch(m_arch.GetSpec());
16425e95aa85SEd Maste merged_arch.MergeFrom(arch_spec);
16435e95aa85SEd Maste return SetArchitecture(merged_arch);
164414f1b3e8SDimitry Andric } else {
16455e95aa85SEd Maste // The new architecture is different, we just need to replace it
16465e95aa85SEd Maste return SetArchitecture(arch_spec);
16475e95aa85SEd Maste }
16485e95aa85SEd Maste }
16495e95aa85SEd Maste return false;
16505e95aa85SEd Maste }
16515e95aa85SEd Maste
NotifyWillClearList(const ModuleList & module_list)16525f29bb8aSDimitry Andric void Target::NotifyWillClearList(const ModuleList &module_list) {}
1653f034231aSEd Maste
NotifyModuleAdded(const ModuleList & module_list,const ModuleSP & module_sp)16545f29bb8aSDimitry Andric void Target::NotifyModuleAdded(const ModuleList &module_list,
165514f1b3e8SDimitry Andric const ModuleSP &module_sp) {
1656f034231aSEd Maste // A module is being added to this target for the first time
165714f1b3e8SDimitry Andric if (m_valid) {
1658f034231aSEd Maste ModuleList my_module_list;
1659f034231aSEd Maste my_module_list.Append(module_sp);
1660f034231aSEd Maste ModulesDidLoad(my_module_list);
1661f034231aSEd Maste }
16620cac4ca3SEd Maste }
1663f034231aSEd Maste
NotifyModuleRemoved(const ModuleList & module_list,const ModuleSP & module_sp)16645f29bb8aSDimitry Andric void Target::NotifyModuleRemoved(const ModuleList &module_list,
166514f1b3e8SDimitry Andric const ModuleSP &module_sp) {
1666e81d9d49SDimitry Andric // A module is being removed from this target.
166714f1b3e8SDimitry Andric if (m_valid) {
1668f034231aSEd Maste ModuleList my_module_list;
1669f034231aSEd Maste my_module_list.Append(module_sp);
1670f21a844fSEd Maste ModulesDidUnload(my_module_list, false);
1671f034231aSEd Maste }
16720cac4ca3SEd Maste }
1673f034231aSEd Maste
NotifyModuleUpdated(const ModuleList & module_list,const ModuleSP & old_module_sp,const ModuleSP & new_module_sp)16745f29bb8aSDimitry Andric void Target::NotifyModuleUpdated(const ModuleList &module_list,
167514f1b3e8SDimitry Andric const ModuleSP &old_module_sp,
167614f1b3e8SDimitry Andric const ModuleSP &new_module_sp) {
1677f034231aSEd Maste // A module is replacing an already added module
167814f1b3e8SDimitry Andric if (m_valid) {
167914f1b3e8SDimitry Andric m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp,
168014f1b3e8SDimitry Andric new_module_sp);
168114f1b3e8SDimitry Andric m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(
168214f1b3e8SDimitry Andric old_module_sp, new_module_sp);
1683e81d9d49SDimitry Andric }
1684f034231aSEd Maste }
1685f034231aSEd Maste
NotifyModulesRemoved(lldb_private::ModuleList & module_list)16865f29bb8aSDimitry Andric void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) {
16875f29bb8aSDimitry Andric ModulesDidUnload(module_list, false);
16885f29bb8aSDimitry Andric }
16895f29bb8aSDimitry Andric
ModulesDidLoad(ModuleList & module_list)169014f1b3e8SDimitry Andric void Target::ModulesDidLoad(ModuleList &module_list) {
16915f29bb8aSDimitry Andric const size_t num_images = module_list.GetSize();
16925f29bb8aSDimitry Andric if (m_valid && num_images) {
16935f29bb8aSDimitry Andric for (size_t idx = 0; idx < num_images; ++idx) {
16945f29bb8aSDimitry Andric ModuleSP module_sp(module_list.GetModuleAtIndex(idx));
16955f29bb8aSDimitry Andric LoadScriptingResourceForModule(module_sp, this);
16965f29bb8aSDimitry Andric }
1697f21a844fSEd Maste m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1698e81d9d49SDimitry Andric m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
169914f1b3e8SDimitry Andric if (m_process_sp) {
17000cac4ca3SEd Maste m_process_sp->ModulesDidLoad(module_list);
1701f21a844fSEd Maste }
17024df029ccSDimitry Andric auto data_sp =
17034df029ccSDimitry Andric std::make_shared<TargetEventData>(shared_from_this(), module_list);
17044df029ccSDimitry Andric BroadcastEvent(eBroadcastBitModulesLoaded, data_sp);
1705f034231aSEd Maste }
1706f034231aSEd Maste }
1707f034231aSEd Maste
SymbolsDidLoad(ModuleList & module_list)170814f1b3e8SDimitry Andric void Target::SymbolsDidLoad(ModuleList &module_list) {
170914f1b3e8SDimitry Andric if (m_valid && module_list.GetSize()) {
171014f1b3e8SDimitry Andric if (m_process_sp) {
17115f29bb8aSDimitry Andric for (LanguageRuntime *runtime : m_process_sp->GetLanguageRuntimes()) {
17125f29bb8aSDimitry Andric runtime->SymbolsDidLoad(module_list);
1713f034231aSEd Maste }
1714f034231aSEd Maste }
1715f034231aSEd Maste
1716f21a844fSEd Maste m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1717e81d9d49SDimitry Andric m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
17184df029ccSDimitry Andric auto data_sp =
17194df029ccSDimitry Andric std::make_shared<TargetEventData>(shared_from_this(), module_list);
17204df029ccSDimitry Andric BroadcastEvent(eBroadcastBitSymbolsLoaded, data_sp);
1721f034231aSEd Maste }
1722f034231aSEd Maste }
1723f034231aSEd Maste
ModulesDidUnload(ModuleList & module_list,bool delete_locations)172414f1b3e8SDimitry Andric void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) {
172514f1b3e8SDimitry Andric if (m_valid && module_list.GetSize()) {
1726205afe67SEd Maste UnloadModuleSections(module_list);
17274df029ccSDimitry Andric auto data_sp =
17284df029ccSDimitry Andric std::make_shared<TargetEventData>(shared_from_this(), module_list);
17294df029ccSDimitry Andric BroadcastEvent(eBroadcastBitModulesUnloaded, data_sp);
1730f21a844fSEd Maste m_breakpoint_list.UpdateBreakpoints(module_list, false, delete_locations);
173114f1b3e8SDimitry Andric m_internal_breakpoint_list.UpdateBreakpoints(module_list, false,
173214f1b3e8SDimitry Andric delete_locations);
1733e3b55780SDimitry Andric
1734e3b55780SDimitry Andric // If a module was torn down it will have torn down the 'TypeSystemClang's
1735e3b55780SDimitry Andric // that we used as source 'ASTContext's for the persistent variables in
1736e3b55780SDimitry Andric // the current target. Those would now be unsafe to access because the
1737e3b55780SDimitry Andric // 'DeclOrigin' are now possibly stale. Thus clear all persistent
1738e3b55780SDimitry Andric // variables. We only want to flush 'TypeSystem's if the module being
1739e3b55780SDimitry Andric // unloaded was capable of describing a source type. JITted module unloads
1740e3b55780SDimitry Andric // happen frequently for Objective-C utility functions or the REPL and rely
1741e3b55780SDimitry Andric // on the persistent variables to stick around.
1742e3b55780SDimitry Andric const bool should_flush_type_systems =
1743e3b55780SDimitry Andric module_list.AnyOf([](lldb_private::Module &module) {
1744e3b55780SDimitry Andric auto *object_file = module.GetObjectFile();
1745e3b55780SDimitry Andric
1746e3b55780SDimitry Andric if (!object_file)
1747e3b55780SDimitry Andric return false;
1748e3b55780SDimitry Andric
1749e3b55780SDimitry Andric auto type = object_file->GetType();
1750e3b55780SDimitry Andric
1751e3b55780SDimitry Andric // eTypeExecutable: when debugged binary was rebuilt
1752e3b55780SDimitry Andric // eTypeSharedLibrary: if dylib was re-loaded
1753e3b55780SDimitry Andric return module.FileHasChanged() &&
1754e3b55780SDimitry Andric (type == ObjectFile::eTypeObjectFile ||
1755e3b55780SDimitry Andric type == ObjectFile::eTypeExecutable ||
1756e3b55780SDimitry Andric type == ObjectFile::eTypeSharedLibrary);
1757e3b55780SDimitry Andric });
1758e3b55780SDimitry Andric
1759e3b55780SDimitry Andric if (should_flush_type_systems)
1760e3b55780SDimitry Andric m_scratch_type_system_map.Clear();
1761f034231aSEd Maste }
1762f034231aSEd Maste }
1763f034231aSEd Maste
ModuleIsExcludedForUnconstrainedSearches(const FileSpec & module_file_spec)176414f1b3e8SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
176514f1b3e8SDimitry Andric const FileSpec &module_file_spec) {
176614f1b3e8SDimitry Andric if (GetBreakpointsConsultPlatformAvoidList()) {
1767f034231aSEd Maste ModuleList matchingModules;
1768f034231aSEd Maste ModuleSpec module_spec(module_file_spec);
1769ead24645SDimitry Andric GetImages().FindModules(module_spec, matchingModules);
1770ead24645SDimitry Andric size_t num_modules = matchingModules.GetSize();
1771f034231aSEd Maste
1772ead24645SDimitry Andric // If there is more than one module for this file spec, only
1773ead24645SDimitry Andric // return true if ALL the modules are on the black list.
177414f1b3e8SDimitry Andric if (num_modules > 0) {
177514f1b3e8SDimitry Andric for (size_t i = 0; i < num_modules; i++) {
177614f1b3e8SDimitry Andric if (!ModuleIsExcludedForUnconstrainedSearches(
177714f1b3e8SDimitry Andric matchingModules.GetModuleAtIndex(i)))
1778f034231aSEd Maste return false;
1779f034231aSEd Maste }
1780f034231aSEd Maste return true;
1781f034231aSEd Maste }
1782f034231aSEd Maste }
1783f034231aSEd Maste return false;
1784f034231aSEd Maste }
1785f034231aSEd Maste
ModuleIsExcludedForUnconstrainedSearches(const lldb::ModuleSP & module_sp)178614f1b3e8SDimitry Andric bool Target::ModuleIsExcludedForUnconstrainedSearches(
178714f1b3e8SDimitry Andric const lldb::ModuleSP &module_sp) {
178814f1b3e8SDimitry Andric if (GetBreakpointsConsultPlatformAvoidList()) {
1789f034231aSEd Maste if (m_platform_sp)
179014f1b3e8SDimitry Andric return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(*this,
179114f1b3e8SDimitry Andric module_sp);
1792f034231aSEd Maste }
1793f034231aSEd Maste return false;
1794f034231aSEd Maste }
1795f034231aSEd Maste
ReadMemoryFromFileCache(const Address & addr,void * dst,size_t dst_len,Status & error)179614f1b3e8SDimitry Andric size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst,
1797b76161e4SDimitry Andric size_t dst_len, Status &error) {
1798f034231aSEd Maste SectionSP section_sp(addr.GetSection());
179914f1b3e8SDimitry Andric if (section_sp) {
180014f1b3e8SDimitry Andric // If the contents of this section are encrypted, the on-disk file is
180114f1b3e8SDimitry Andric // unusable. Read only from live memory.
180214f1b3e8SDimitry Andric if (section_sp->IsEncrypted()) {
1803f034231aSEd Maste error.SetErrorString("section is encrypted");
1804f034231aSEd Maste return 0;
1805f034231aSEd Maste }
1806f034231aSEd Maste ModuleSP module_sp(section_sp->GetModule());
180714f1b3e8SDimitry Andric if (module_sp) {
1808f034231aSEd Maste ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
180914f1b3e8SDimitry Andric if (objfile) {
181014f1b3e8SDimitry Andric size_t bytes_read = objfile->ReadSectionData(
181114f1b3e8SDimitry Andric section_sp.get(), addr.GetOffset(), dst, dst_len);
1812f034231aSEd Maste if (bytes_read > 0)
1813f034231aSEd Maste return bytes_read;
1814f034231aSEd Maste else
181514f1b3e8SDimitry Andric error.SetErrorStringWithFormat("error reading data from section %s",
181614f1b3e8SDimitry Andric section_sp->GetName().GetCString());
181714f1b3e8SDimitry Andric } else
1818f034231aSEd Maste error.SetErrorString("address isn't from a object file");
181914f1b3e8SDimitry Andric } else
1820f034231aSEd Maste error.SetErrorString("address isn't in a module");
182114f1b3e8SDimitry Andric } else
182214f1b3e8SDimitry Andric error.SetErrorString("address doesn't contain a section that points to a "
182314f1b3e8SDimitry Andric "section in a object file");
1824f034231aSEd Maste
1825f034231aSEd Maste return 0;
1826f034231aSEd Maste }
1827f034231aSEd Maste
ReadMemory(const Address & addr,void * dst,size_t dst_len,Status & error,bool force_live_memory,lldb::addr_t * load_addr_ptr)1828344a3780SDimitry Andric size_t Target::ReadMemory(const Address &addr, void *dst, size_t dst_len,
1829344a3780SDimitry Andric Status &error, bool force_live_memory,
183014f1b3e8SDimitry Andric lldb::addr_t *load_addr_ptr) {
1831f034231aSEd Maste error.Clear();
1832f034231aSEd Maste
1833145449b1SDimitry Andric Address fixed_addr = addr;
1834145449b1SDimitry Andric if (ProcessIsValid())
1835145449b1SDimitry Andric if (const ABISP &abi = m_process_sp->GetABI())
1836145449b1SDimitry Andric fixed_addr.SetLoadAddress(abi->FixAnyAddress(addr.GetLoadAddress(this)),
1837145449b1SDimitry Andric this);
1838145449b1SDimitry Andric
1839f73363f1SDimitry Andric // if we end up reading this from process memory, we will fill this with the
1840f73363f1SDimitry Andric // actual load address
1841f034231aSEd Maste if (load_addr_ptr)
1842f034231aSEd Maste *load_addr_ptr = LLDB_INVALID_ADDRESS;
1843f034231aSEd Maste
1844f034231aSEd Maste size_t bytes_read = 0;
1845f034231aSEd Maste
1846f034231aSEd Maste addr_t load_addr = LLDB_INVALID_ADDRESS;
1847f034231aSEd Maste addr_t file_addr = LLDB_INVALID_ADDRESS;
1848f034231aSEd Maste Address resolved_addr;
1849145449b1SDimitry Andric if (!fixed_addr.IsSectionOffset()) {
1850866dcdacSEd Maste SectionLoadList §ion_load_list = GetSectionLoadList();
185114f1b3e8SDimitry Andric if (section_load_list.IsEmpty()) {
1852f73363f1SDimitry Andric // No sections are loaded, so we must assume we are not running yet and
1853f73363f1SDimitry Andric // anything we are given is a file address.
1854145449b1SDimitry Andric file_addr =
1855145449b1SDimitry Andric fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so
1856145449b1SDimitry Andric // its offset is the file address
1857f034231aSEd Maste m_images.ResolveFileAddress(file_addr, resolved_addr);
185814f1b3e8SDimitry Andric } else {
1859f73363f1SDimitry Andric // We have at least one section loaded. This can be because we have
1860f73363f1SDimitry Andric // manually loaded some sections with "target modules load ..." or
1861b1c73532SDimitry Andric // because we have a live process that has sections loaded through
1862f73363f1SDimitry Andric // the dynamic loader
1863145449b1SDimitry Andric load_addr =
1864145449b1SDimitry Andric fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so
1865145449b1SDimitry Andric // its offset is the load address
1866866dcdacSEd Maste section_load_list.ResolveLoadAddress(load_addr, resolved_addr);
1867f034231aSEd Maste }
1868f034231aSEd Maste }
1869f034231aSEd Maste if (!resolved_addr.IsValid())
1870145449b1SDimitry Andric resolved_addr = fixed_addr;
1871f034231aSEd Maste
1872344a3780SDimitry Andric // If we read from the file cache but can't get as many bytes as requested,
1873344a3780SDimitry Andric // we keep the result around in this buffer, in case this result is the
1874344a3780SDimitry Andric // best we can do.
1875344a3780SDimitry Andric std::unique_ptr<uint8_t[]> file_cache_read_buffer;
1876344a3780SDimitry Andric size_t file_cache_bytes_read = 0;
1877344a3780SDimitry Andric
1878344a3780SDimitry Andric // Read from file cache if read-only section.
1879344a3780SDimitry Andric if (!force_live_memory && resolved_addr.IsSectionOffset()) {
1880344a3780SDimitry Andric SectionSP section_sp(resolved_addr.GetSection());
1881344a3780SDimitry Andric if (section_sp) {
1882344a3780SDimitry Andric auto permissions = Flags(section_sp->GetPermissions());
1883344a3780SDimitry Andric bool is_readonly = !permissions.Test(ePermissionsWritable) &&
1884344a3780SDimitry Andric permissions.Test(ePermissionsReadable);
1885344a3780SDimitry Andric if (is_readonly) {
1886344a3780SDimitry Andric file_cache_bytes_read =
1887344a3780SDimitry Andric ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1888344a3780SDimitry Andric if (file_cache_bytes_read == dst_len)
1889344a3780SDimitry Andric return file_cache_bytes_read;
1890344a3780SDimitry Andric else if (file_cache_bytes_read > 0) {
1891344a3780SDimitry Andric file_cache_read_buffer =
1892344a3780SDimitry Andric std::make_unique<uint8_t[]>(file_cache_bytes_read);
1893344a3780SDimitry Andric std::memcpy(file_cache_read_buffer.get(), dst, file_cache_bytes_read);
1894344a3780SDimitry Andric }
1895344a3780SDimitry Andric }
1896344a3780SDimitry Andric }
1897f034231aSEd Maste }
1898f034231aSEd Maste
189914f1b3e8SDimitry Andric if (ProcessIsValid()) {
1900f034231aSEd Maste if (load_addr == LLDB_INVALID_ADDRESS)
1901f034231aSEd Maste load_addr = resolved_addr.GetLoadAddress(this);
1902f034231aSEd Maste
190314f1b3e8SDimitry Andric if (load_addr == LLDB_INVALID_ADDRESS) {
1904f034231aSEd Maste ModuleSP addr_module_sp(resolved_addr.GetModule());
1905f034231aSEd Maste if (addr_module_sp && addr_module_sp->GetFileSpec())
190614f1b3e8SDimitry Andric error.SetErrorStringWithFormatv(
190714f1b3e8SDimitry Andric "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded",
190814f1b3e8SDimitry Andric addr_module_sp->GetFileSpec(), resolved_addr.GetFileAddress());
1909f034231aSEd Maste else
191014f1b3e8SDimitry Andric error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved",
191114f1b3e8SDimitry Andric resolved_addr.GetFileAddress());
191214f1b3e8SDimitry Andric } else {
1913f034231aSEd Maste bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
191414f1b3e8SDimitry Andric if (bytes_read != dst_len) {
191514f1b3e8SDimitry Andric if (error.Success()) {
1916f034231aSEd Maste if (bytes_read == 0)
191714f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
191814f1b3e8SDimitry Andric "read memory from 0x%" PRIx64 " failed", load_addr);
1919f034231aSEd Maste else
192014f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
192114f1b3e8SDimitry Andric "only %" PRIu64 " of %" PRIu64
192214f1b3e8SDimitry Andric " bytes were read from memory at 0x%" PRIx64,
192314f1b3e8SDimitry Andric (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
1924f034231aSEd Maste }
1925f034231aSEd Maste }
192614f1b3e8SDimitry Andric if (bytes_read) {
1927f034231aSEd Maste if (load_addr_ptr)
1928f034231aSEd Maste *load_addr_ptr = load_addr;
1929f034231aSEd Maste return bytes_read;
1930f034231aSEd Maste }
1931f034231aSEd Maste }
1932f034231aSEd Maste }
1933f034231aSEd Maste
1934344a3780SDimitry Andric if (file_cache_read_buffer && file_cache_bytes_read > 0) {
1935344a3780SDimitry Andric // Reading from the process failed. If we've previously succeeded in reading
1936344a3780SDimitry Andric // something from the file cache, then copy that over and return that.
1937344a3780SDimitry Andric std::memcpy(dst, file_cache_read_buffer.get(), file_cache_bytes_read);
1938344a3780SDimitry Andric return file_cache_bytes_read;
1939344a3780SDimitry Andric }
1940344a3780SDimitry Andric
1941344a3780SDimitry Andric if (!file_cache_read_buffer && resolved_addr.IsSectionOffset()) {
1942f73363f1SDimitry Andric // If we didn't already try and read from the object file cache, then try
1943f73363f1SDimitry Andric // it after failing to read from the process.
1944f034231aSEd Maste return ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1945f034231aSEd Maste }
1946f034231aSEd Maste return 0;
1947f034231aSEd Maste }
1948f034231aSEd Maste
ReadCStringFromMemory(const Address & addr,std::string & out_str,Status & error,bool force_live_memory)194914f1b3e8SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str,
1950ecbca9f5SDimitry Andric Status &error, bool force_live_memory) {
1951f034231aSEd Maste char buf[256];
1952f034231aSEd Maste out_str.clear();
1953f034231aSEd Maste addr_t curr_addr = addr.GetLoadAddress(this);
1954f034231aSEd Maste Address address(addr);
19555f29bb8aSDimitry Andric while (true) {
1956ecbca9f5SDimitry Andric size_t length = ReadCStringFromMemory(address, buf, sizeof(buf), error,
1957ecbca9f5SDimitry Andric force_live_memory);
1958f034231aSEd Maste if (length == 0)
1959f034231aSEd Maste break;
1960f034231aSEd Maste out_str.append(buf, length);
1961f73363f1SDimitry Andric // If we got "length - 1" bytes, we didn't get the whole C string, we need
1962f73363f1SDimitry Andric // to read some more characters
1963f034231aSEd Maste if (length == sizeof(buf) - 1)
1964f034231aSEd Maste curr_addr += length;
1965f034231aSEd Maste else
1966f034231aSEd Maste break;
1967f034231aSEd Maste address = Address(curr_addr);
1968f034231aSEd Maste }
1969f034231aSEd Maste return out_str.size();
1970f034231aSEd Maste }
1971f034231aSEd Maste
ReadCStringFromMemory(const Address & addr,char * dst,size_t dst_max_len,Status & result_error,bool force_live_memory)197214f1b3e8SDimitry Andric size_t Target::ReadCStringFromMemory(const Address &addr, char *dst,
1973ecbca9f5SDimitry Andric size_t dst_max_len, Status &result_error,
1974ecbca9f5SDimitry Andric bool force_live_memory) {
1975f034231aSEd Maste size_t total_cstr_len = 0;
197614f1b3e8SDimitry Andric if (dst && dst_max_len) {
1977f034231aSEd Maste result_error.Clear();
1978f034231aSEd Maste // NULL out everything just to be safe
1979f034231aSEd Maste memset(dst, 0, dst_max_len);
1980b76161e4SDimitry Andric Status error;
1981f034231aSEd Maste addr_t curr_addr = addr.GetLoadAddress(this);
1982f034231aSEd Maste Address address(addr);
1983205afe67SEd Maste
1984f73363f1SDimitry Andric // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think
1985f73363f1SDimitry Andric // this really needs to be tied to the memory cache subsystem's cache line
1986f73363f1SDimitry Andric // size, so leave this as a fixed constant.
1987f034231aSEd Maste const size_t cache_line_size = 512;
1988205afe67SEd Maste
1989f034231aSEd Maste size_t bytes_left = dst_max_len - 1;
1990f034231aSEd Maste char *curr_dst = dst;
1991f034231aSEd Maste
199214f1b3e8SDimitry Andric while (bytes_left > 0) {
199314f1b3e8SDimitry Andric addr_t cache_line_bytes_left =
199414f1b3e8SDimitry Andric cache_line_size - (curr_addr % cache_line_size);
199514f1b3e8SDimitry Andric addr_t bytes_to_read =
199614f1b3e8SDimitry Andric std::min<addr_t>(bytes_left, cache_line_bytes_left);
1997ecbca9f5SDimitry Andric size_t bytes_read = ReadMemory(address, curr_dst, bytes_to_read, error,
1998ecbca9f5SDimitry Andric force_live_memory);
1999f034231aSEd Maste
200014f1b3e8SDimitry Andric if (bytes_read == 0) {
2001f034231aSEd Maste result_error = error;
2002f034231aSEd Maste dst[total_cstr_len] = '\0';
2003f034231aSEd Maste break;
2004f034231aSEd Maste }
2005f034231aSEd Maste const size_t len = strlen(curr_dst);
2006f034231aSEd Maste
2007f034231aSEd Maste total_cstr_len += len;
2008f034231aSEd Maste
2009f034231aSEd Maste if (len < bytes_to_read)
2010f034231aSEd Maste break;
2011f034231aSEd Maste
2012f034231aSEd Maste curr_dst += bytes_read;
2013f034231aSEd Maste curr_addr += bytes_read;
2014f034231aSEd Maste bytes_left -= bytes_read;
2015f034231aSEd Maste address = Address(curr_addr);
2016f034231aSEd Maste }
201714f1b3e8SDimitry Andric } else {
2018e81d9d49SDimitry Andric if (dst == nullptr)
2019f034231aSEd Maste result_error.SetErrorString("invalid arguments");
2020f034231aSEd Maste else
2021f034231aSEd Maste result_error.Clear();
2022f034231aSEd Maste }
2023f034231aSEd Maste return total_cstr_len;
2024f034231aSEd Maste }
2025f034231aSEd Maste
GetReasonableReadSize(const Address & addr)2026c0981da4SDimitry Andric addr_t Target::GetReasonableReadSize(const Address &addr) {
2027c0981da4SDimitry Andric addr_t load_addr = addr.GetLoadAddress(this);
2028c0981da4SDimitry Andric if (load_addr != LLDB_INVALID_ADDRESS && m_process_sp) {
2029c0981da4SDimitry Andric // Avoid crossing cache line boundaries.
2030c0981da4SDimitry Andric addr_t cache_line_size = m_process_sp->GetMemoryCacheLineSize();
2031c0981da4SDimitry Andric return cache_line_size - (load_addr % cache_line_size);
2032c0981da4SDimitry Andric }
2033c0981da4SDimitry Andric
2034c0981da4SDimitry Andric // The read is going to go to the file cache, so we can just pick a largish
2035c0981da4SDimitry Andric // value.
2036c0981da4SDimitry Andric return 0x1000;
2037c0981da4SDimitry Andric }
2038c0981da4SDimitry Andric
ReadStringFromMemory(const Address & addr,char * dst,size_t max_bytes,Status & error,size_t type_width,bool force_live_memory)2039c0981da4SDimitry Andric size_t Target::ReadStringFromMemory(const Address &addr, char *dst,
2040c0981da4SDimitry Andric size_t max_bytes, Status &error,
2041c0981da4SDimitry Andric size_t type_width, bool force_live_memory) {
2042c0981da4SDimitry Andric if (!dst || !max_bytes || !type_width || max_bytes < type_width)
2043c0981da4SDimitry Andric return 0;
2044c0981da4SDimitry Andric
2045c0981da4SDimitry Andric size_t total_bytes_read = 0;
2046c0981da4SDimitry Andric
2047c0981da4SDimitry Andric // Ensure a null terminator independent of the number of bytes that is
2048c0981da4SDimitry Andric // read.
2049c0981da4SDimitry Andric memset(dst, 0, max_bytes);
2050c0981da4SDimitry Andric size_t bytes_left = max_bytes - type_width;
2051c0981da4SDimitry Andric
2052c0981da4SDimitry Andric const char terminator[4] = {'\0', '\0', '\0', '\0'};
2053c0981da4SDimitry Andric assert(sizeof(terminator) >= type_width && "Attempting to validate a "
2054c0981da4SDimitry Andric "string with more than 4 bytes "
2055c0981da4SDimitry Andric "per character!");
2056c0981da4SDimitry Andric
2057c0981da4SDimitry Andric Address address = addr;
2058c0981da4SDimitry Andric char *curr_dst = dst;
2059c0981da4SDimitry Andric
2060c0981da4SDimitry Andric error.Clear();
2061c0981da4SDimitry Andric while (bytes_left > 0 && error.Success()) {
2062c0981da4SDimitry Andric addr_t bytes_to_read =
2063c0981da4SDimitry Andric std::min<addr_t>(bytes_left, GetReasonableReadSize(address));
2064c0981da4SDimitry Andric size_t bytes_read =
2065c0981da4SDimitry Andric ReadMemory(address, curr_dst, bytes_to_read, error, force_live_memory);
2066c0981da4SDimitry Andric
2067c0981da4SDimitry Andric if (bytes_read == 0)
2068c0981da4SDimitry Andric break;
2069c0981da4SDimitry Andric
2070c0981da4SDimitry Andric // Search for a null terminator of correct size and alignment in
2071c0981da4SDimitry Andric // bytes_read
2072c0981da4SDimitry Andric size_t aligned_start = total_bytes_read - total_bytes_read % type_width;
2073c0981da4SDimitry Andric for (size_t i = aligned_start;
2074c0981da4SDimitry Andric i + type_width <= total_bytes_read + bytes_read; i += type_width)
2075c0981da4SDimitry Andric if (::memcmp(&dst[i], terminator, type_width) == 0) {
2076c0981da4SDimitry Andric error.Clear();
2077c0981da4SDimitry Andric return i;
2078c0981da4SDimitry Andric }
2079c0981da4SDimitry Andric
2080c0981da4SDimitry Andric total_bytes_read += bytes_read;
2081c0981da4SDimitry Andric curr_dst += bytes_read;
2082c0981da4SDimitry Andric address.Slide(bytes_read);
2083c0981da4SDimitry Andric bytes_left -= bytes_read;
2084c0981da4SDimitry Andric }
2085c0981da4SDimitry Andric return total_bytes_read;
2086c0981da4SDimitry Andric }
2087c0981da4SDimitry Andric
ReadScalarIntegerFromMemory(const Address & addr,uint32_t byte_size,bool is_signed,Scalar & scalar,Status & error,bool force_live_memory)2088344a3780SDimitry Andric size_t Target::ReadScalarIntegerFromMemory(const Address &addr, uint32_t byte_size,
2089344a3780SDimitry Andric bool is_signed, Scalar &scalar,
2090344a3780SDimitry Andric Status &error,
2091344a3780SDimitry Andric bool force_live_memory) {
2092f034231aSEd Maste uint64_t uval;
2093f034231aSEd Maste
209414f1b3e8SDimitry Andric if (byte_size <= sizeof(uval)) {
209514f1b3e8SDimitry Andric size_t bytes_read =
2096344a3780SDimitry Andric ReadMemory(addr, &uval, byte_size, error, force_live_memory);
209714f1b3e8SDimitry Andric if (bytes_read == byte_size) {
2098ef5d0b5eSDimitry Andric DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(),
2099ef5d0b5eSDimitry Andric m_arch.GetSpec().GetAddressByteSize());
2100f034231aSEd Maste lldb::offset_t offset = 0;
2101f034231aSEd Maste if (byte_size <= 4)
2102f034231aSEd Maste scalar = data.GetMaxU32(&offset, byte_size);
2103f034231aSEd Maste else
2104f034231aSEd Maste scalar = data.GetMaxU64(&offset, byte_size);
2105f034231aSEd Maste
2106f034231aSEd Maste if (is_signed)
2107f034231aSEd Maste scalar.SignExtend(byte_size * 8);
2108f034231aSEd Maste return bytes_read;
2109f034231aSEd Maste }
211014f1b3e8SDimitry Andric } else {
211114f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
211214f1b3e8SDimitry Andric "byte size of %u is too large for integer scalar type", byte_size);
2113f034231aSEd Maste }
2114f034231aSEd Maste return 0;
2115f034231aSEd Maste }
2116f034231aSEd Maste
ReadUnsignedIntegerFromMemory(const Address & addr,size_t integer_byte_size,uint64_t fail_value,Status & error,bool force_live_memory)211714f1b3e8SDimitry Andric uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr,
2118f034231aSEd Maste size_t integer_byte_size,
2119344a3780SDimitry Andric uint64_t fail_value, Status &error,
2120344a3780SDimitry Andric bool force_live_memory) {
2121f034231aSEd Maste Scalar scalar;
2122344a3780SDimitry Andric if (ReadScalarIntegerFromMemory(addr, integer_byte_size, false, scalar, error,
2123344a3780SDimitry Andric force_live_memory))
2124f034231aSEd Maste return scalar.ULongLong(fail_value);
2125f034231aSEd Maste return fail_value;
2126f034231aSEd Maste }
2127f034231aSEd Maste
ReadPointerFromMemory(const Address & addr,Status & error,Address & pointer_addr,bool force_live_memory)2128344a3780SDimitry Andric bool Target::ReadPointerFromMemory(const Address &addr, Status &error,
2129344a3780SDimitry Andric Address &pointer_addr,
2130344a3780SDimitry Andric bool force_live_memory) {
2131f034231aSEd Maste Scalar scalar;
2132344a3780SDimitry Andric if (ReadScalarIntegerFromMemory(addr, m_arch.GetSpec().GetAddressByteSize(),
2133344a3780SDimitry Andric false, scalar, error, force_live_memory)) {
2134f034231aSEd Maste addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
213514f1b3e8SDimitry Andric if (pointer_vm_addr != LLDB_INVALID_ADDRESS) {
2136866dcdacSEd Maste SectionLoadList §ion_load_list = GetSectionLoadList();
213714f1b3e8SDimitry Andric if (section_load_list.IsEmpty()) {
2138f73363f1SDimitry Andric // No sections are loaded, so we must assume we are not running yet and
2139f73363f1SDimitry Andric // anything we are given is a file address.
2140f034231aSEd Maste m_images.ResolveFileAddress(pointer_vm_addr, pointer_addr);
214114f1b3e8SDimitry Andric } else {
2142f73363f1SDimitry Andric // We have at least one section loaded. This can be because we have
2143f73363f1SDimitry Andric // manually loaded some sections with "target modules load ..." or
2144b1c73532SDimitry Andric // because we have a live process that has sections loaded through
2145f73363f1SDimitry Andric // the dynamic loader
2146866dcdacSEd Maste section_load_list.ResolveLoadAddress(pointer_vm_addr, pointer_addr);
2147f034231aSEd Maste }
2148f73363f1SDimitry Andric // We weren't able to resolve the pointer value, so just return an
2149f73363f1SDimitry Andric // address with no section
2150f034231aSEd Maste if (!pointer_addr.IsValid())
2151f034231aSEd Maste pointer_addr.SetOffset(pointer_vm_addr);
2152f034231aSEd Maste return true;
2153f034231aSEd Maste }
2154f034231aSEd Maste }
2155f034231aSEd Maste return false;
2156f034231aSEd Maste }
2157f034231aSEd Maste
GetOrCreateModule(const ModuleSpec & module_spec,bool notify,Status * error_ptr)21585f29bb8aSDimitry Andric ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
2159b76161e4SDimitry Andric Status *error_ptr) {
2160f034231aSEd Maste ModuleSP module_sp;
2161f034231aSEd Maste
2162b76161e4SDimitry Andric Status error;
2163f034231aSEd Maste
216414f1b3e8SDimitry Andric // First see if we already have this module in our module list. If we do,
2165f73363f1SDimitry Andric // then we're done, we don't need to consult the shared modules list. But
2166f73363f1SDimitry Andric // only do this if we are passed a UUID.
2167f034231aSEd Maste
2168f034231aSEd Maste if (module_spec.GetUUID().IsValid())
2169f034231aSEd Maste module_sp = m_images.FindFirstModule(module_spec);
2170f034231aSEd Maste
217114f1b3e8SDimitry Andric if (!module_sp) {
2172b60736ecSDimitry Andric llvm::SmallVector<ModuleSP, 1>
2173b60736ecSDimitry Andric old_modules; // This will get filled in if we have a new version
217414f1b3e8SDimitry Andric // of the library
2175f034231aSEd Maste bool did_create_module = false;
21765f29bb8aSDimitry Andric FileSpecList search_paths = GetExecutableSearchPaths();
21777fa27ce4SDimitry Andric FileSpec symbol_file_spec;
21787fa27ce4SDimitry Andric
21797fa27ce4SDimitry Andric // Call locate module callback if set. This allows users to implement their
21807fa27ce4SDimitry Andric // own module cache system. For example, to leverage build system artifacts,
21817fa27ce4SDimitry Andric // to bypass pulling files from remote platform, or to search symbol files
21827fa27ce4SDimitry Andric // from symbol servers.
2183b1c73532SDimitry Andric if (m_platform_sp)
2184b1c73532SDimitry Andric m_platform_sp->CallLocateModuleCallbackIfSet(
2185b1c73532SDimitry Andric module_spec, module_sp, symbol_file_spec, &did_create_module);
21867fa27ce4SDimitry Andric
21877fa27ce4SDimitry Andric // The result of this CallLocateModuleCallbackIfSet is one of the following.
21887fa27ce4SDimitry Andric // 1. module_sp:loaded, symbol_file_spec:set
21897fa27ce4SDimitry Andric // The callback found a module file and a symbol file for the
21907fa27ce4SDimitry Andric // module_spec. We will call module_sp->SetSymbolFileFileSpec with
21917fa27ce4SDimitry Andric // the symbol_file_spec later.
21927fa27ce4SDimitry Andric // 2. module_sp:loaded, symbol_file_spec:empty
21937fa27ce4SDimitry Andric // The callback only found a module file for the module_spec.
21947fa27ce4SDimitry Andric // 3. module_sp:empty, symbol_file_spec:set
21957fa27ce4SDimitry Andric // The callback only found a symbol file for the module. We continue
21967fa27ce4SDimitry Andric // to find a module file for this module_spec and we will call
21977fa27ce4SDimitry Andric // module_sp->SetSymbolFileFileSpec with the symbol_file_spec later.
21987fa27ce4SDimitry Andric // 4. module_sp:empty, symbol_file_spec:empty
2199b1c73532SDimitry Andric // Platform does not exist, the callback is not set, the callback did
2200b1c73532SDimitry Andric // not find any module files nor any symbol files, the callback failed,
2201b1c73532SDimitry Andric // or something went wrong. We continue to find a module file for this
2202b1c73532SDimitry Andric // module_spec.
22037fa27ce4SDimitry Andric
22047fa27ce4SDimitry Andric if (!module_sp) {
22057fa27ce4SDimitry Andric // If there are image search path entries, try to use them to acquire a
22067fa27ce4SDimitry Andric // suitable image.
220714f1b3e8SDimitry Andric if (m_image_search_paths.GetSize()) {
2208f034231aSEd Maste ModuleSpec transformed_spec(module_spec);
2209e3b55780SDimitry Andric ConstString transformed_dir;
221014f1b3e8SDimitry Andric if (m_image_search_paths.RemapPath(
2211e3b55780SDimitry Andric module_spec.GetFileSpec().GetDirectory(), transformed_dir)) {
2212e3b55780SDimitry Andric transformed_spec.GetFileSpec().SetDirectory(transformed_dir);
2213e3b55780SDimitry Andric transformed_spec.GetFileSpec().SetFilename(
2214e3b55780SDimitry Andric module_spec.GetFileSpec().GetFilename());
221514f1b3e8SDimitry Andric error = ModuleList::GetSharedModule(transformed_spec, module_sp,
2216b60736ecSDimitry Andric &search_paths, &old_modules,
2217ead24645SDimitry Andric &did_create_module);
2218f034231aSEd Maste }
2219f034231aSEd Maste }
22207fa27ce4SDimitry Andric }
2221f034231aSEd Maste
222214f1b3e8SDimitry Andric if (!module_sp) {
2223f034231aSEd Maste // If we have a UUID, we can check our global shared module list in case
2224f034231aSEd Maste // we already have it. If we don't have a valid UUID, then we can't since
2225f034231aSEd Maste // the path in "module_spec" will be a platform path, and we will need to
2226f034231aSEd Maste // let the platform find that file. For example, we could be asking for
2227f034231aSEd Maste // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
2228f034231aSEd Maste // the local copy of "/usr/lib/dyld" since our platform could be a remote
2229f034231aSEd Maste // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
2230f034231aSEd Maste // cache.
223114f1b3e8SDimitry Andric if (module_spec.GetUUID().IsValid()) {
2232f034231aSEd Maste // We have a UUID, it is OK to check the global module list...
2233ead24645SDimitry Andric error =
2234ead24645SDimitry Andric ModuleList::GetSharedModule(module_spec, module_sp, &search_paths,
2235b60736ecSDimitry Andric &old_modules, &did_create_module);
2236f034231aSEd Maste }
2237f034231aSEd Maste
223814f1b3e8SDimitry Andric if (!module_sp) {
2239f034231aSEd Maste // The platform is responsible for finding and caching an appropriate
2240f034231aSEd Maste // module in the shared module cache.
224114f1b3e8SDimitry Andric if (m_platform_sp) {
224214f1b3e8SDimitry Andric error = m_platform_sp->GetSharedModule(
2243ead24645SDimitry Andric module_spec, m_process_sp.get(), module_sp, &search_paths,
2244b60736ecSDimitry Andric &old_modules, &did_create_module);
224514f1b3e8SDimitry Andric } else {
2246f034231aSEd Maste error.SetErrorString("no platform is currently set");
2247f034231aSEd Maste }
2248f034231aSEd Maste }
2249f034231aSEd Maste }
2250f034231aSEd Maste
225114f1b3e8SDimitry Andric // We found a module that wasn't in our target list. Let's make sure that
2252f73363f1SDimitry Andric // there wasn't an equivalent module in the list already, and if there was,
2253f73363f1SDimitry Andric // let's remove it.
225414f1b3e8SDimitry Andric if (module_sp) {
2255f034231aSEd Maste ObjectFile *objfile = module_sp->GetObjectFile();
225614f1b3e8SDimitry Andric if (objfile) {
225714f1b3e8SDimitry Andric switch (objfile->GetType()) {
225814f1b3e8SDimitry Andric case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of
225914f1b3e8SDimitry Andric /// a program's execution state
2260f034231aSEd Maste case ObjectFile::eTypeExecutable: /// A normal executable
226114f1b3e8SDimitry Andric case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker
226214f1b3e8SDimitry Andric /// executable
2263f034231aSEd Maste case ObjectFile::eTypeObjectFile: /// An intermediate object file
226414f1b3e8SDimitry Andric case ObjectFile::eTypeSharedLibrary: /// A shared library that can be
226514f1b3e8SDimitry Andric /// used during execution
2266f034231aSEd Maste break;
226714f1b3e8SDimitry Andric case ObjectFile::eTypeDebugInfo: /// An object file that contains only
226814f1b3e8SDimitry Andric /// debug information
2269f034231aSEd Maste if (error_ptr)
227014f1b3e8SDimitry Andric error_ptr->SetErrorString("debug info files aren't valid target "
227114f1b3e8SDimitry Andric "modules, please specify an executable");
2272f034231aSEd Maste return ModuleSP();
227314f1b3e8SDimitry Andric case ObjectFile::eTypeStubLibrary: /// A library that can be linked
227414f1b3e8SDimitry Andric /// against but not used for
227514f1b3e8SDimitry Andric /// execution
2276f034231aSEd Maste if (error_ptr)
227714f1b3e8SDimitry Andric error_ptr->SetErrorString("stub libraries aren't valid target "
227814f1b3e8SDimitry Andric "modules, please specify an executable");
2279f034231aSEd Maste return ModuleSP();
2280f034231aSEd Maste default:
2281f034231aSEd Maste if (error_ptr)
228214f1b3e8SDimitry Andric error_ptr->SetErrorString(
228314f1b3e8SDimitry Andric "unsupported file type, please specify an executable");
2284f034231aSEd Maste return ModuleSP();
2285f034231aSEd Maste }
228614f1b3e8SDimitry Andric // GetSharedModule is not guaranteed to find the old shared module, for
2287f73363f1SDimitry Andric // instance in the common case where you pass in the UUID, it is only
2288f73363f1SDimitry Andric // going to find the one module matching the UUID. In fact, it has no
2289f73363f1SDimitry Andric // good way to know what the "old module" relevant to this target is,
2290f73363f1SDimitry Andric // since there might be many copies of a module with this file spec in
2291f73363f1SDimitry Andric // various running debug sessions, but only one of them will belong to
2292f73363f1SDimitry Andric // this target. So let's remove the UUID from the module list, and look
2293f73363f1SDimitry Andric // in the target's module list. Only do this if there is SOMETHING else
2294f73363f1SDimitry Andric // in the module spec...
229514f1b3e8SDimitry Andric if (module_spec.GetUUID().IsValid() &&
229614f1b3e8SDimitry Andric !module_spec.GetFileSpec().GetFilename().IsEmpty() &&
229714f1b3e8SDimitry Andric !module_spec.GetFileSpec().GetDirectory().IsEmpty()) {
2298f034231aSEd Maste ModuleSpec module_spec_copy(module_spec.GetFileSpec());
2299f034231aSEd Maste module_spec_copy.GetUUID().Clear();
2300f034231aSEd Maste
2301f034231aSEd Maste ModuleList found_modules;
230214f1b3e8SDimitry Andric m_images.FindModules(module_spec_copy, found_modules);
2303b60736ecSDimitry Andric found_modules.ForEach([&](const ModuleSP &found_module) -> bool {
2304b60736ecSDimitry Andric old_modules.push_back(found_module);
2305b60736ecSDimitry Andric return true;
2306b60736ecSDimitry Andric });
2307f034231aSEd Maste }
2308f034231aSEd Maste
23097fa27ce4SDimitry Andric // If the locate module callback had found a symbol file, set it to the
23107fa27ce4SDimitry Andric // module_sp before preloading symbols.
23117fa27ce4SDimitry Andric if (symbol_file_spec)
23127fa27ce4SDimitry Andric module_sp->SetSymbolFileFileSpec(symbol_file_spec);
23137fa27ce4SDimitry Andric
2314773dd0e6SDimitry Andric // Preload symbols outside of any lock, so hopefully we can do this for
2315773dd0e6SDimitry Andric // each library in parallel.
2316773dd0e6SDimitry Andric if (GetPreloadSymbols())
2317773dd0e6SDimitry Andric module_sp->PreloadSymbols();
2318b60736ecSDimitry Andric llvm::SmallVector<ModuleSP, 1> replaced_modules;
2319b60736ecSDimitry Andric for (ModuleSP &old_module_sp : old_modules) {
2320b60736ecSDimitry Andric if (m_images.GetIndexForModule(old_module_sp.get()) !=
232114f1b3e8SDimitry Andric LLDB_INVALID_INDEX32) {
2322b60736ecSDimitry Andric if (replaced_modules.empty())
2323f034231aSEd Maste m_images.ReplaceModule(old_module_sp, module_sp);
2324b60736ecSDimitry Andric else
2325b60736ecSDimitry Andric m_images.Remove(old_module_sp);
2326b60736ecSDimitry Andric
2327b60736ecSDimitry Andric replaced_modules.push_back(std::move(old_module_sp));
2328b60736ecSDimitry Andric }
2329b60736ecSDimitry Andric }
2330b60736ecSDimitry Andric
2331b60736ecSDimitry Andric if (replaced_modules.size() > 1) {
2332b60736ecSDimitry Andric // The same new module replaced multiple old modules
2333b60736ecSDimitry Andric // simultaneously. It's not clear this should ever
2334b60736ecSDimitry Andric // happen (if we always replace old modules as we add
2335b60736ecSDimitry Andric // new ones, presumably we should never have more than
2336b60736ecSDimitry Andric // one old one). If there are legitimate cases where
2337b60736ecSDimitry Andric // this happens, then the ModuleList::Notifier interface
2338b60736ecSDimitry Andric // may need to be adjusted to allow reporting this.
2339b60736ecSDimitry Andric // In the meantime, just log that this has happened; just
2340b60736ecSDimitry Andric // above we called ReplaceModule on the first one, and Remove
2341b60736ecSDimitry Andric // on the rest.
2342145449b1SDimitry Andric if (Log *log = GetLog(LLDBLog::Target | LLDBLog::Modules)) {
2343b60736ecSDimitry Andric StreamString message;
2344b60736ecSDimitry Andric auto dump = [&message](Module &dump_module) -> void {
2345b60736ecSDimitry Andric UUID dump_uuid = dump_module.GetUUID();
2346b60736ecSDimitry Andric
2347b60736ecSDimitry Andric message << '[';
2348b60736ecSDimitry Andric dump_module.GetDescription(message.AsRawOstream());
2349b60736ecSDimitry Andric message << " (uuid ";
2350b60736ecSDimitry Andric
2351b60736ecSDimitry Andric if (dump_uuid.IsValid())
23527fa27ce4SDimitry Andric dump_uuid.Dump(message);
2353b60736ecSDimitry Andric else
2354b60736ecSDimitry Andric message << "not specified";
2355b60736ecSDimitry Andric
2356b60736ecSDimitry Andric message << ")]";
2357b60736ecSDimitry Andric };
2358b60736ecSDimitry Andric
2359b60736ecSDimitry Andric message << "New module ";
2360b60736ecSDimitry Andric dump(*module_sp);
2361b60736ecSDimitry Andric message.AsRawOstream()
2362b60736ecSDimitry Andric << llvm::formatv(" simultaneously replaced {0} old modules: ",
2363b60736ecSDimitry Andric replaced_modules.size());
2364b60736ecSDimitry Andric for (ModuleSP &replaced_module_sp : replaced_modules)
2365b60736ecSDimitry Andric dump(*replaced_module_sp);
2366b60736ecSDimitry Andric
2367b60736ecSDimitry Andric log->PutString(message.GetString());
2368b60736ecSDimitry Andric }
2369b60736ecSDimitry Andric }
2370b60736ecSDimitry Andric
2371b60736ecSDimitry Andric if (replaced_modules.empty())
2372b60736ecSDimitry Andric m_images.Append(module_sp, notify);
2373b60736ecSDimitry Andric
2374b60736ecSDimitry Andric for (ModuleSP &old_module_sp : replaced_modules) {
2375f034231aSEd Maste Module *old_module_ptr = old_module_sp.get();
2376f034231aSEd Maste old_module_sp.reset();
2377f034231aSEd Maste ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
23785f29bb8aSDimitry Andric }
237914f1b3e8SDimitry Andric } else
23800cac4ca3SEd Maste module_sp.reset();
2381f034231aSEd Maste }
2382f034231aSEd Maste }
2383f034231aSEd Maste if (error_ptr)
2384f034231aSEd Maste *error_ptr = error;
2385f034231aSEd Maste return module_sp;
2386f034231aSEd Maste }
2387f034231aSEd Maste
CalculateTarget()238814f1b3e8SDimitry Andric TargetSP Target::CalculateTarget() { return shared_from_this(); }
2389f034231aSEd Maste
CalculateProcess()239014f1b3e8SDimitry Andric ProcessSP Target::CalculateProcess() { return m_process_sp; }
2391f034231aSEd Maste
CalculateThread()239214f1b3e8SDimitry Andric ThreadSP Target::CalculateThread() { return ThreadSP(); }
2393f034231aSEd Maste
CalculateStackFrame()239414f1b3e8SDimitry Andric StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); }
2395f034231aSEd Maste
CalculateExecutionContext(ExecutionContext & exe_ctx)239614f1b3e8SDimitry Andric void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) {
2397f034231aSEd Maste exe_ctx.Clear();
2398f034231aSEd Maste exe_ctx.SetTargetPtr(this);
2399f034231aSEd Maste }
2400f034231aSEd Maste
GetImageSearchPathList()240114f1b3e8SDimitry Andric PathMappingList &Target::GetImageSearchPathList() {
2402f034231aSEd Maste return m_image_search_paths;
2403f034231aSEd Maste }
2404f034231aSEd Maste
ImageSearchPathsChanged(const PathMappingList & path_list,void * baton)240514f1b3e8SDimitry Andric void Target::ImageSearchPathsChanged(const PathMappingList &path_list,
240614f1b3e8SDimitry Andric void *baton) {
2407f034231aSEd Maste Target *target = (Target *)baton;
2408f034231aSEd Maste ModuleSP exe_module_sp(target->GetExecutableModule());
2409f034231aSEd Maste if (exe_module_sp)
241094994d37SDimitry Andric target->SetExecutableModule(exe_module_sp, eLoadDependentsYes);
2411f034231aSEd Maste }
2412f034231aSEd Maste
2413e3b55780SDimitry Andric llvm::Expected<lldb::TypeSystemSP>
GetScratchTypeSystemForLanguage(lldb::LanguageType language,bool create_on_demand)2414ead24645SDimitry Andric Target::GetScratchTypeSystemForLanguage(lldb::LanguageType language,
241514f1b3e8SDimitry Andric bool create_on_demand) {
2416e81d9d49SDimitry Andric if (!m_valid)
2417ac9a064cSDimitry Andric return llvm::createStringError("Invalid Target");
2418e81d9d49SDimitry Andric
241914f1b3e8SDimitry Andric if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all
242014f1b3e8SDimitry Andric // assembly code
242114f1b3e8SDimitry Andric || language == eLanguageTypeUnknown) {
2422ead24645SDimitry Andric LanguageSet languages_for_expressions =
2423ead24645SDimitry Andric Language::GetLanguagesSupportingTypeSystemsForExpressions();
2424e81d9d49SDimitry Andric
2425ead24645SDimitry Andric if (languages_for_expressions[eLanguageTypeC]) {
242614f1b3e8SDimitry Andric language = eLanguageTypeC; // LLDB's default. Override by setting the
242714f1b3e8SDimitry Andric // target language.
242814f1b3e8SDimitry Andric } else {
2429ead24645SDimitry Andric if (languages_for_expressions.Empty())
2430ac9a064cSDimitry Andric return llvm::createStringError(
2431ac9a064cSDimitry Andric "No expression support for any languages");
2432ead24645SDimitry Andric language = (LanguageType)languages_for_expressions.bitvector.find_first();
2433e81d9d49SDimitry Andric }
2434e81d9d49SDimitry Andric }
2435e81d9d49SDimitry Andric
243614f1b3e8SDimitry Andric return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this,
243714f1b3e8SDimitry Andric create_on_demand);
2438e81d9d49SDimitry Andric }
2439e81d9d49SDimitry Andric
GetRegisterType(const std::string & name,const lldb_private::RegisterFlags & flags,uint32_t byte_size)24407fa27ce4SDimitry Andric CompilerType Target::GetRegisterType(const std::string &name,
24417fa27ce4SDimitry Andric const lldb_private::RegisterFlags &flags,
24427fa27ce4SDimitry Andric uint32_t byte_size) {
24437fa27ce4SDimitry Andric RegisterTypeBuilderSP provider = PluginManager::GetRegisterTypeBuilder(*this);
24447fa27ce4SDimitry Andric assert(provider);
24457fa27ce4SDimitry Andric return provider->GetRegisterType(name, flags, byte_size);
24467fa27ce4SDimitry Andric }
24477fa27ce4SDimitry Andric
2448e3b55780SDimitry Andric std::vector<lldb::TypeSystemSP>
GetScratchTypeSystems(bool create_on_demand)2449e3b55780SDimitry Andric Target::GetScratchTypeSystems(bool create_on_demand) {
2450ead24645SDimitry Andric if (!m_valid)
2451ead24645SDimitry Andric return {};
2452ead24645SDimitry Andric
2453c0981da4SDimitry Andric // Some TypeSystem instances are associated with several LanguageTypes so
2454c0981da4SDimitry Andric // they will show up several times in the loop below. The SetVector filters
2455c0981da4SDimitry Andric // out all duplicates as they serve no use for the caller.
2456e3b55780SDimitry Andric std::vector<lldb::TypeSystemSP> scratch_type_systems;
2457ead24645SDimitry Andric
2458ead24645SDimitry Andric LanguageSet languages_for_expressions =
2459ead24645SDimitry Andric Language::GetLanguagesSupportingTypeSystemsForExpressions();
2460ead24645SDimitry Andric
2461ead24645SDimitry Andric for (auto bit : languages_for_expressions.bitvector.set_bits()) {
2462ead24645SDimitry Andric auto language = (LanguageType)bit;
2463ead24645SDimitry Andric auto type_system_or_err =
2464ead24645SDimitry Andric GetScratchTypeSystemForLanguage(language, create_on_demand);
2465ead24645SDimitry Andric if (!type_system_or_err)
24667fa27ce4SDimitry Andric LLDB_LOG_ERROR(
24677fa27ce4SDimitry Andric GetLog(LLDBLog::Target), type_system_or_err.takeError(),
24687fa27ce4SDimitry Andric "Language '{1}' has expression support but no scratch type "
24697fa27ce4SDimitry Andric "system available: {0}",
2470ead24645SDimitry Andric Language::GetNameForLanguageType(language));
2471ead24645SDimitry Andric else
2472e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
2473e3b55780SDimitry Andric scratch_type_systems.push_back(ts);
2474ead24645SDimitry Andric }
24757fa27ce4SDimitry Andric
24767fa27ce4SDimitry Andric std::sort(scratch_type_systems.begin(), scratch_type_systems.end());
2477e3b55780SDimitry Andric scratch_type_systems.erase(
2478e3b55780SDimitry Andric std::unique(scratch_type_systems.begin(), scratch_type_systems.end()),
2479e3b55780SDimitry Andric scratch_type_systems.end());
2480e3b55780SDimitry Andric return scratch_type_systems;
2481ead24645SDimitry Andric }
2482ead24645SDimitry Andric
2483e81d9d49SDimitry Andric PersistentExpressionState *
GetPersistentExpressionStateForLanguage(lldb::LanguageType language)248414f1b3e8SDimitry Andric Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) {
2485ead24645SDimitry Andric auto type_system_or_err = GetScratchTypeSystemForLanguage(language, true);
2486e81d9d49SDimitry Andric
2487ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
24887fa27ce4SDimitry Andric LLDB_LOG_ERROR(
24897fa27ce4SDimitry Andric GetLog(LLDBLog::Target), std::move(err),
24907fa27ce4SDimitry Andric "Unable to get persistent expression state for language {1}: {0}",
2491ead24645SDimitry Andric Language::GetNameForLanguageType(language));
2492e81d9d49SDimitry Andric return nullptr;
2493e81d9d49SDimitry Andric }
2494ead24645SDimitry Andric
2495e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
2496e3b55780SDimitry Andric return ts->GetPersistentExpressionState();
2497e3b55780SDimitry Andric
2498e3b55780SDimitry Andric LLDB_LOG(GetLog(LLDBLog::Target),
24997fa27ce4SDimitry Andric "Unable to get persistent expression state for language {1}: {0}",
2500e3b55780SDimitry Andric Language::GetNameForLanguageType(language));
2501e3b55780SDimitry Andric return nullptr;
2502e81d9d49SDimitry Andric }
2503e81d9d49SDimitry Andric
GetUserExpressionForLanguage(llvm::StringRef expr,llvm::StringRef prefix,SourceLanguage language,Expression::ResultType desired_type,const EvaluateExpressionOptions & options,ValueObject * ctx_obj,Status & error)250414f1b3e8SDimitry Andric UserExpression *Target::GetUserExpressionForLanguage(
2505ac9a064cSDimitry Andric llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language,
2506e81d9d49SDimitry Andric Expression::ResultType desired_type,
2507ead24645SDimitry Andric const EvaluateExpressionOptions &options, ValueObject *ctx_obj,
2508ead24645SDimitry Andric Status &error) {
2509ac9a064cSDimitry Andric auto type_system_or_err =
2510ac9a064cSDimitry Andric GetScratchTypeSystemForLanguage(language.AsLanguageType());
2511ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
251214f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
251314f1b3e8SDimitry Andric "Could not find type system for language %s: %s",
2514ac9a064cSDimitry Andric Language::GetNameForLanguageType(language.AsLanguageType()),
2515ead24645SDimitry Andric llvm::toString(std::move(err)).c_str());
2516e81d9d49SDimitry Andric return nullptr;
2517e81d9d49SDimitry Andric }
2518e81d9d49SDimitry Andric
2519e3b55780SDimitry Andric auto ts = *type_system_or_err;
2520e3b55780SDimitry Andric if (!ts) {
2521e3b55780SDimitry Andric error.SetErrorStringWithFormat(
2522e3b55780SDimitry Andric "Type system for language %s is no longer live",
2523ac9a064cSDimitry Andric language.GetDescription().data());
2524e3b55780SDimitry Andric return nullptr;
2525e3b55780SDimitry Andric }
2526e3b55780SDimitry Andric
2527e3b55780SDimitry Andric auto *user_expr = ts->GetUserExpression(expr, prefix, language, desired_type,
2528e3b55780SDimitry Andric options, ctx_obj);
2529e81d9d49SDimitry Andric if (!user_expr)
253014f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
253114f1b3e8SDimitry Andric "Could not create an expression for language %s",
2532ac9a064cSDimitry Andric language.GetDescription().data());
2533e81d9d49SDimitry Andric
2534e81d9d49SDimitry Andric return user_expr;
2535e81d9d49SDimitry Andric }
2536e81d9d49SDimitry Andric
GetFunctionCallerForLanguage(lldb::LanguageType language,const CompilerType & return_type,const Address & function_address,const ValueList & arg_value_list,const char * name,Status & error)253714f1b3e8SDimitry Andric FunctionCaller *Target::GetFunctionCallerForLanguage(
253814f1b3e8SDimitry Andric lldb::LanguageType language, const CompilerType &return_type,
253914f1b3e8SDimitry Andric const Address &function_address, const ValueList &arg_value_list,
2540b76161e4SDimitry Andric const char *name, Status &error) {
2541ead24645SDimitry Andric auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
2542ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
254314f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
254414f1b3e8SDimitry Andric "Could not find type system for language %s: %s",
254514f1b3e8SDimitry Andric Language::GetNameForLanguageType(language),
2546ead24645SDimitry Andric llvm::toString(std::move(err)).c_str());
2547ead24645SDimitry Andric return nullptr;
2548e81d9d49SDimitry Andric }
2549e3b55780SDimitry Andric auto ts = *type_system_or_err;
2550e3b55780SDimitry Andric if (!ts) {
2551e3b55780SDimitry Andric error.SetErrorStringWithFormat(
2552e3b55780SDimitry Andric "Type system for language %s is no longer live",
2553e3b55780SDimitry Andric Language::GetNameForLanguageType(language));
2554e3b55780SDimitry Andric return nullptr;
2555e3b55780SDimitry Andric }
2556e3b55780SDimitry Andric auto *persistent_fn = ts->GetFunctionCaller(return_type, function_address,
2557e3b55780SDimitry Andric arg_value_list, name);
2558e81d9d49SDimitry Andric if (!persistent_fn)
255914f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
256014f1b3e8SDimitry Andric "Could not create an expression for language %s",
256114f1b3e8SDimitry Andric Language::GetNameForLanguageType(language));
2562e81d9d49SDimitry Andric
2563e81d9d49SDimitry Andric return persistent_fn;
2564e81d9d49SDimitry Andric }
2565e81d9d49SDimitry Andric
2566b60736ecSDimitry Andric llvm::Expected<std::unique_ptr<UtilityFunction>>
CreateUtilityFunction(std::string expression,std::string name,lldb::LanguageType language,ExecutionContext & exe_ctx)2567b60736ecSDimitry Andric Target::CreateUtilityFunction(std::string expression, std::string name,
2568e81d9d49SDimitry Andric lldb::LanguageType language,
2569b60736ecSDimitry Andric ExecutionContext &exe_ctx) {
2570ead24645SDimitry Andric auto type_system_or_err = GetScratchTypeSystemForLanguage(language);
2571b60736ecSDimitry Andric if (!type_system_or_err)
2572b60736ecSDimitry Andric return type_system_or_err.takeError();
2573e3b55780SDimitry Andric auto ts = *type_system_or_err;
2574e3b55780SDimitry Andric if (!ts)
2575ac9a064cSDimitry Andric return llvm::createStringError(
2576e3b55780SDimitry Andric llvm::StringRef("Type system for language ") +
2577e3b55780SDimitry Andric Language::GetNameForLanguageType(language) +
2578ac9a064cSDimitry Andric llvm::StringRef(" is no longer live"));
2579b60736ecSDimitry Andric std::unique_ptr<UtilityFunction> utility_fn =
2580e3b55780SDimitry Andric ts->CreateUtilityFunction(std::move(expression), std::move(name));
2581e81d9d49SDimitry Andric if (!utility_fn)
2582ac9a064cSDimitry Andric return llvm::createStringError(
2583b60736ecSDimitry Andric llvm::StringRef("Could not create an expression for language") +
2584ac9a064cSDimitry Andric Language::GetNameForLanguageType(language));
2585e81d9d49SDimitry Andric
2586b60736ecSDimitry Andric DiagnosticManager diagnostics;
2587b60736ecSDimitry Andric if (!utility_fn->Install(diagnostics, exe_ctx))
2588ac9a064cSDimitry Andric return llvm::createStringError(diagnostics.GetString());
2589b60736ecSDimitry Andric
2590b60736ecSDimitry Andric return std::move(utility_fn);
2591e81d9d49SDimitry Andric }
2592e81d9d49SDimitry Andric
SettingsInitialize()259314f1b3e8SDimitry Andric void Target::SettingsInitialize() { Process::SettingsInitialize(); }
2594f034231aSEd Maste
SettingsTerminate()259514f1b3e8SDimitry Andric void Target::SettingsTerminate() { Process::SettingsTerminate(); }
2596f034231aSEd Maste
GetDefaultExecutableSearchPaths()259714f1b3e8SDimitry Andric FileSpecList Target::GetDefaultExecutableSearchPaths() {
2598c0981da4SDimitry Andric return Target::GetGlobalProperties().GetExecutableSearchPaths();
2599f034231aSEd Maste }
2600f034231aSEd Maste
GetDefaultDebugFileSearchPaths()260114f1b3e8SDimitry Andric FileSpecList Target::GetDefaultDebugFileSearchPaths() {
2602c0981da4SDimitry Andric return Target::GetGlobalProperties().GetDebugFileSearchPaths();
2603f034231aSEd Maste }
2604f034231aSEd Maste
GetDefaultArchitecture()260514f1b3e8SDimitry Andric ArchSpec Target::GetDefaultArchitecture() {
2606c0981da4SDimitry Andric return Target::GetGlobalProperties().GetDefaultArchitecture();
2607f034231aSEd Maste }
2608f034231aSEd Maste
SetDefaultArchitecture(const ArchSpec & arch)260914f1b3e8SDimitry Andric void Target::SetDefaultArchitecture(const ArchSpec &arch) {
2610145449b1SDimitry Andric LLDB_LOG(GetLog(LLDBLog::Target),
2611c0981da4SDimitry Andric "setting target's default architecture to {0} ({1})",
2612ead24645SDimitry Andric arch.GetArchitectureName(), arch.GetTriple().getTriple());
2613c0981da4SDimitry Andric Target::GetGlobalProperties().SetDefaultArchitecture(arch);
2614f034231aSEd Maste }
2615f034231aSEd Maste
SetLabel(llvm::StringRef label)26167fa27ce4SDimitry Andric llvm::Error Target::SetLabel(llvm::StringRef label) {
26177fa27ce4SDimitry Andric size_t n = LLDB_INVALID_INDEX32;
26187fa27ce4SDimitry Andric if (llvm::to_integer(label, n))
2619ac9a064cSDimitry Andric return llvm::createStringError("Cannot use integer as target label.");
26207fa27ce4SDimitry Andric TargetList &targets = GetDebugger().GetTargetList();
26217fa27ce4SDimitry Andric for (size_t i = 0; i < targets.GetNumTargets(); i++) {
26227fa27ce4SDimitry Andric TargetSP target_sp = targets.GetTargetAtIndex(i);
26237fa27ce4SDimitry Andric if (target_sp && target_sp->GetLabel() == label) {
26247fa27ce4SDimitry Andric return llvm::make_error<llvm::StringError>(
26257fa27ce4SDimitry Andric llvm::formatv(
26267fa27ce4SDimitry Andric "Cannot use label '{0}' since it's set in target #{1}.", label,
26277fa27ce4SDimitry Andric i),
26287fa27ce4SDimitry Andric llvm::inconvertibleErrorCode());
26297fa27ce4SDimitry Andric }
26307fa27ce4SDimitry Andric }
26317fa27ce4SDimitry Andric
26327fa27ce4SDimitry Andric m_label = label.str();
26337fa27ce4SDimitry Andric return llvm::Error::success();
26347fa27ce4SDimitry Andric }
26357fa27ce4SDimitry Andric
GetTargetFromContexts(const ExecutionContext * exe_ctx_ptr,const SymbolContext * sc_ptr)263614f1b3e8SDimitry Andric Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
263714f1b3e8SDimitry Andric const SymbolContext *sc_ptr) {
2638f034231aSEd Maste // The target can either exist in the "process" of ExecutionContext, or in
2639f034231aSEd Maste // the "target_sp" member of SymbolContext. This accessor helper function
2640f034231aSEd Maste // will get the target from one of these locations.
2641f034231aSEd Maste
2642e81d9d49SDimitry Andric Target *target = nullptr;
2643e81d9d49SDimitry Andric if (sc_ptr != nullptr)
2644f034231aSEd Maste target = sc_ptr->target_sp.get();
2645e81d9d49SDimitry Andric if (target == nullptr && exe_ctx_ptr)
2646f034231aSEd Maste target = exe_ctx_ptr->GetTargetPtr();
2647f034231aSEd Maste return target;
2648f034231aSEd Maste }
2649f034231aSEd Maste
EvaluateExpression(llvm::StringRef expr,ExecutionContextScope * exe_scope,lldb::ValueObjectSP & result_valobj_sp,const EvaluateExpressionOptions & options,std::string * fixed_expression,ValueObject * ctx_obj)265014f1b3e8SDimitry Andric ExpressionResults Target::EvaluateExpression(
265114f1b3e8SDimitry Andric llvm::StringRef expr, ExecutionContextScope *exe_scope,
2652f034231aSEd Maste lldb::ValueObjectSP &result_valobj_sp,
26535f29bb8aSDimitry Andric const EvaluateExpressionOptions &options, std::string *fixed_expression,
26545f29bb8aSDimitry Andric ValueObject *ctx_obj) {
2655f034231aSEd Maste result_valobj_sp.reset();
2656f034231aSEd Maste
26570cac4ca3SEd Maste ExpressionResults execution_results = eExpressionSetupError;
2658f034231aSEd Maste
2659c0981da4SDimitry Andric if (expr.empty()) {
2660c0981da4SDimitry Andric m_stats.GetExpressionStats().NotifyFailure();
2661f034231aSEd Maste return execution_results;
2662c0981da4SDimitry Andric }
2663f034231aSEd Maste
26645f29bb8aSDimitry Andric // We shouldn't run stop hooks in expressions.
2665f034231aSEd Maste bool old_suppress_value = m_suppress_stop_hooks;
2666f034231aSEd Maste m_suppress_stop_hooks = true;
26675f29bb8aSDimitry Andric auto on_exit = llvm::make_scope_exit([this, old_suppress_value]() {
2668ead24645SDimitry Andric m_suppress_stop_hooks = old_suppress_value;
2669ead24645SDimitry Andric });
2670f034231aSEd Maste
2671f034231aSEd Maste ExecutionContext exe_ctx;
2672f034231aSEd Maste
267314f1b3e8SDimitry Andric if (exe_scope) {
2674e81d9d49SDimitry Andric exe_scope->CalculateExecutionContext(exe_ctx);
267514f1b3e8SDimitry Andric } else if (m_process_sp) {
2676f034231aSEd Maste m_process_sp->CalculateExecutionContext(exe_ctx);
267714f1b3e8SDimitry Andric } else {
2678f034231aSEd Maste CalculateExecutionContext(exe_ctx);
2679f034231aSEd Maste }
2680f034231aSEd Maste
2681f73363f1SDimitry Andric // Make sure we aren't just trying to see the value of a persistent variable
2682f73363f1SDimitry Andric // (something like "$0")
2683f034231aSEd Maste // Only check for persistent variables the expression starts with a '$'
2684ead24645SDimitry Andric lldb::ExpressionVariableSP persistent_var_sp;
2685ead24645SDimitry Andric if (expr[0] == '$') {
2686ead24645SDimitry Andric auto type_system_or_err =
2687ead24645SDimitry Andric GetScratchTypeSystemForLanguage(eLanguageTypeC);
2688ead24645SDimitry Andric if (auto err = type_system_or_err.takeError()) {
2689145449b1SDimitry Andric LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err),
2690145449b1SDimitry Andric "Unable to get scratch type system");
2691ead24645SDimitry Andric } else {
2692e3b55780SDimitry Andric auto ts = *type_system_or_err;
2693e3b55780SDimitry Andric if (!ts)
2694e3b55780SDimitry Andric LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err),
26957fa27ce4SDimitry Andric "Scratch type system is no longer live: {0}");
2696e3b55780SDimitry Andric else
2697ead24645SDimitry Andric persistent_var_sp =
2698e3b55780SDimitry Andric ts->GetPersistentExpressionState()->GetVariable(expr);
2699ead24645SDimitry Andric }
2700ead24645SDimitry Andric }
270114f1b3e8SDimitry Andric if (persistent_var_sp) {
2702f034231aSEd Maste result_valobj_sp = persistent_var_sp->GetValueObject();
27030cac4ca3SEd Maste execution_results = eExpressionCompleted;
270414f1b3e8SDimitry Andric } else {
270523629167SDimitry Andric llvm::StringRef prefix = GetExpressionPrefixContents();
2706b76161e4SDimitry Andric Status error;
2707cfca06d7SDimitry Andric execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix,
2708cfca06d7SDimitry Andric result_valobj_sp, error,
2709cfca06d7SDimitry Andric fixed_expression, ctx_obj);
2710e3b55780SDimitry Andric // Pass up the error by wrapping it inside an error result.
2711e3b55780SDimitry Andric if (error.Fail() && !result_valobj_sp)
2712e3b55780SDimitry Andric result_valobj_sp = ValueObjectConstResult::Create(
2713e3b55780SDimitry Andric exe_ctx.GetBestExecutionContextScope(), error);
2714f034231aSEd Maste }
2715f034231aSEd Maste
2716c0981da4SDimitry Andric if (execution_results == eExpressionCompleted)
2717c0981da4SDimitry Andric m_stats.GetExpressionStats().NotifySuccess();
2718c0981da4SDimitry Andric else
2719c0981da4SDimitry Andric m_stats.GetExpressionStats().NotifyFailure();
2720f034231aSEd Maste return execution_results;
2721f034231aSEd Maste }
2722f034231aSEd Maste
GetPersistentVariable(ConstString name)2723ead24645SDimitry Andric lldb::ExpressionVariableSP Target::GetPersistentVariable(ConstString name) {
2724e81d9d49SDimitry Andric lldb::ExpressionVariableSP variable_sp;
272514f1b3e8SDimitry Andric m_scratch_type_system_map.ForEach(
2726e3b55780SDimitry Andric [name, &variable_sp](TypeSystemSP type_system) -> bool {
2727e3b55780SDimitry Andric auto ts = type_system.get();
2728e3b55780SDimitry Andric if (!ts)
2729e3b55780SDimitry Andric return true;
273014f1b3e8SDimitry Andric if (PersistentExpressionState *persistent_state =
2731e3b55780SDimitry Andric ts->GetPersistentExpressionState()) {
2732e81d9d49SDimitry Andric variable_sp = persistent_state->GetVariable(name);
2733e81d9d49SDimitry Andric
2734e81d9d49SDimitry Andric if (variable_sp)
2735e81d9d49SDimitry Andric return false; // Stop iterating the ForEach
2736e81d9d49SDimitry Andric }
2737e81d9d49SDimitry Andric return true; // Keep iterating the ForEach
2738e81d9d49SDimitry Andric });
2739e81d9d49SDimitry Andric return variable_sp;
2740e81d9d49SDimitry Andric }
2741e81d9d49SDimitry Andric
GetPersistentSymbol(ConstString name)27425f29bb8aSDimitry Andric lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
2743e81d9d49SDimitry Andric lldb::addr_t address = LLDB_INVALID_ADDRESS;
2744e81d9d49SDimitry Andric
274514f1b3e8SDimitry Andric m_scratch_type_system_map.ForEach(
2746e3b55780SDimitry Andric [name, &address](lldb::TypeSystemSP type_system) -> bool {
2747e3b55780SDimitry Andric auto ts = type_system.get();
2748e3b55780SDimitry Andric if (!ts)
2749e3b55780SDimitry Andric return true;
2750e3b55780SDimitry Andric
275114f1b3e8SDimitry Andric if (PersistentExpressionState *persistent_state =
2752e3b55780SDimitry Andric ts->GetPersistentExpressionState()) {
2753e81d9d49SDimitry Andric address = persistent_state->LookupSymbol(name);
2754e81d9d49SDimitry Andric if (address != LLDB_INVALID_ADDRESS)
2755e81d9d49SDimitry Andric return false; // Stop iterating the ForEach
2756e81d9d49SDimitry Andric }
2757e81d9d49SDimitry Andric return true; // Keep iterating the ForEach
2758e81d9d49SDimitry Andric });
2759e81d9d49SDimitry Andric return address;
27605e95aa85SEd Maste }
27615e95aa85SEd Maste
GetEntryPointAddress()2762ead24645SDimitry Andric llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
2763ead24645SDimitry Andric Module *exe_module = GetExecutableModulePointer();
2764ead24645SDimitry Andric
2765b60736ecSDimitry Andric // Try to find the entry point address in the primary executable.
2766b60736ecSDimitry Andric const bool has_primary_executable = exe_module && exe_module->GetObjectFile();
2767b60736ecSDimitry Andric if (has_primary_executable) {
2768ead24645SDimitry Andric Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
2769ead24645SDimitry Andric if (entry_addr.IsValid())
2770ead24645SDimitry Andric return entry_addr;
2771ead24645SDimitry Andric }
2772ead24645SDimitry Andric
2773ead24645SDimitry Andric const ModuleList &modules = GetImages();
2774ead24645SDimitry Andric const size_t num_images = modules.GetSize();
2775ead24645SDimitry Andric for (size_t idx = 0; idx < num_images; ++idx) {
2776ead24645SDimitry Andric ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2777ead24645SDimitry Andric if (!module_sp || !module_sp->GetObjectFile())
2778ead24645SDimitry Andric continue;
2779ead24645SDimitry Andric
2780ead24645SDimitry Andric Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
2781b60736ecSDimitry Andric if (entry_addr.IsValid())
2782ead24645SDimitry Andric return entry_addr;
2783ead24645SDimitry Andric }
2784ead24645SDimitry Andric
2785b60736ecSDimitry Andric // We haven't found the entry point address. Return an appropriate error.
2786b60736ecSDimitry Andric if (!has_primary_executable)
2787ac9a064cSDimitry Andric return llvm::createStringError(
2788b60736ecSDimitry Andric "No primary executable found and could not find entry point address in "
2789ac9a064cSDimitry Andric "any executable module");
2790b60736ecSDimitry Andric
2791ac9a064cSDimitry Andric return llvm::createStringError(
2792b60736ecSDimitry Andric "Could not find entry point address for primary executable module \"" +
2793ac9a064cSDimitry Andric exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"");
2794ead24645SDimitry Andric }
2795ead24645SDimitry Andric
GetCallableLoadAddress(lldb::addr_t load_addr,AddressClass addr_class) const279614f1b3e8SDimitry Andric lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
279714f1b3e8SDimitry Andric AddressClass addr_class) const {
279894994d37SDimitry Andric auto arch_plugin = GetArchitecturePlugin();
2799ead24645SDimitry Andric return arch_plugin
2800ead24645SDimitry Andric ? arch_plugin->GetCallableLoadAddress(load_addr, addr_class)
2801ead24645SDimitry Andric : load_addr;
2802f034231aSEd Maste }
2803f034231aSEd Maste
GetOpcodeLoadAddress(lldb::addr_t load_addr,AddressClass addr_class) const280414f1b3e8SDimitry Andric lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr,
280514f1b3e8SDimitry Andric AddressClass addr_class) const {
280694994d37SDimitry Andric auto arch_plugin = GetArchitecturePlugin();
2807ead24645SDimitry Andric return arch_plugin ? arch_plugin->GetOpcodeLoadAddress(load_addr, addr_class)
2808ead24645SDimitry Andric : load_addr;
2809f034231aSEd Maste }
2810f034231aSEd Maste
GetBreakableLoadAddress(lldb::addr_t addr)281114f1b3e8SDimitry Andric lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
281294994d37SDimitry Andric auto arch_plugin = GetArchitecturePlugin();
2813ead24645SDimitry Andric return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
2814e81d9d49SDimitry Andric }
2815e81d9d49SDimitry Andric
GetSourceManager()281614f1b3e8SDimitry Andric SourceManager &Target::GetSourceManager() {
28175f29bb8aSDimitry Andric if (!m_source_manager_up)
2818cfca06d7SDimitry Andric m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());
28195f29bb8aSDimitry Andric return *m_source_manager_up;
2820f034231aSEd Maste }
2821f034231aSEd Maste
CreateStopHook(StopHook::StopHookKind kind)2822b60736ecSDimitry Andric Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind) {
2823f034231aSEd Maste lldb::user_id_t new_uid = ++m_stop_hook_next_id;
2824b60736ecSDimitry Andric Target::StopHookSP stop_hook_sp;
2825b60736ecSDimitry Andric switch (kind) {
2826b60736ecSDimitry Andric case StopHook::StopHookKind::CommandBased:
2827b60736ecSDimitry Andric stop_hook_sp.reset(new StopHookCommandLine(shared_from_this(), new_uid));
2828b60736ecSDimitry Andric break;
2829b60736ecSDimitry Andric case StopHook::StopHookKind::ScriptBased:
2830b60736ecSDimitry Andric stop_hook_sp.reset(new StopHookScripted(shared_from_this(), new_uid));
2831b60736ecSDimitry Andric break;
2832b60736ecSDimitry Andric }
2833866dcdacSEd Maste m_stop_hooks[new_uid] = stop_hook_sp;
2834866dcdacSEd Maste return stop_hook_sp;
2835f034231aSEd Maste }
2836f034231aSEd Maste
UndoCreateStopHook(lldb::user_id_t user_id)2837b60736ecSDimitry Andric void Target::UndoCreateStopHook(lldb::user_id_t user_id) {
2838b60736ecSDimitry Andric if (!RemoveStopHookByID(user_id))
2839b60736ecSDimitry Andric return;
2840b60736ecSDimitry Andric if (user_id == m_stop_hook_next_id)
2841b60736ecSDimitry Andric m_stop_hook_next_id--;
2842b60736ecSDimitry Andric }
2843b60736ecSDimitry Andric
RemoveStopHookByID(lldb::user_id_t user_id)284414f1b3e8SDimitry Andric bool Target::RemoveStopHookByID(lldb::user_id_t user_id) {
2845e81d9d49SDimitry Andric size_t num_removed = m_stop_hooks.erase(user_id);
2846e81d9d49SDimitry Andric return (num_removed != 0);
2847f034231aSEd Maste }
2848f034231aSEd Maste
RemoveAllStopHooks()284914f1b3e8SDimitry Andric void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); }
2850f034231aSEd Maste
GetStopHookByID(lldb::user_id_t user_id)285114f1b3e8SDimitry Andric Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) {
2852f034231aSEd Maste StopHookSP found_hook;
2853f034231aSEd Maste
2854f034231aSEd Maste StopHookCollection::iterator specified_hook_iter;
2855f034231aSEd Maste specified_hook_iter = m_stop_hooks.find(user_id);
2856f034231aSEd Maste if (specified_hook_iter != m_stop_hooks.end())
2857f034231aSEd Maste found_hook = (*specified_hook_iter).second;
2858f034231aSEd Maste return found_hook;
2859f034231aSEd Maste }
2860f034231aSEd Maste
SetStopHookActiveStateByID(lldb::user_id_t user_id,bool active_state)286114f1b3e8SDimitry Andric bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id,
286214f1b3e8SDimitry Andric bool active_state) {
2863f034231aSEd Maste StopHookCollection::iterator specified_hook_iter;
2864f034231aSEd Maste specified_hook_iter = m_stop_hooks.find(user_id);
2865f034231aSEd Maste if (specified_hook_iter == m_stop_hooks.end())
2866f034231aSEd Maste return false;
2867f034231aSEd Maste
2868f034231aSEd Maste (*specified_hook_iter).second->SetIsActive(active_state);
2869f034231aSEd Maste return true;
2870f034231aSEd Maste }
2871f034231aSEd Maste
SetAllStopHooksActiveState(bool active_state)287214f1b3e8SDimitry Andric void Target::SetAllStopHooksActiveState(bool active_state) {
2873f034231aSEd Maste StopHookCollection::iterator pos, end = m_stop_hooks.end();
287414f1b3e8SDimitry Andric for (pos = m_stop_hooks.begin(); pos != end; pos++) {
2875f034231aSEd Maste (*pos).second->SetIsActive(active_state);
2876f034231aSEd Maste }
2877f034231aSEd Maste }
2878f034231aSEd Maste
RunStopHooks()2879b60736ecSDimitry Andric bool Target::RunStopHooks() {
2880f034231aSEd Maste if (m_suppress_stop_hooks)
2881b60736ecSDimitry Andric return false;
2882f034231aSEd Maste
2883f034231aSEd Maste if (!m_process_sp)
2884b60736ecSDimitry Andric return false;
2885f034231aSEd Maste
2886ef5d0b5eSDimitry Andric // Somebody might have restarted the process:
2887b60736ecSDimitry Andric // Still return false, the return value is about US restarting the target.
2888ef5d0b5eSDimitry Andric if (m_process_sp->GetState() != eStateStopped)
2889b60736ecSDimitry Andric return false;
2890ef5d0b5eSDimitry Andric
2891f034231aSEd Maste if (m_stop_hooks.empty())
2892b60736ecSDimitry Andric return false;
2893f034231aSEd Maste
28945f29bb8aSDimitry Andric // If there aren't any active stop hooks, don't bother either.
2895f034231aSEd Maste bool any_active_hooks = false;
28965f29bb8aSDimitry Andric for (auto hook : m_stop_hooks) {
28975f29bb8aSDimitry Andric if (hook.second->IsActive()) {
2898f034231aSEd Maste any_active_hooks = true;
2899b60736ecSDimitry Andric break;
2900f034231aSEd Maste }
2901f034231aSEd Maste }
2902f034231aSEd Maste if (!any_active_hooks)
2903b60736ecSDimitry Andric return false;
2904f034231aSEd Maste
2905b1c73532SDimitry Andric // Make sure we check that we are not stopped because of us running a user
2906b1c73532SDimitry Andric // expression since in that case we do not want to run the stop-hooks. Note,
2907b1c73532SDimitry Andric // you can't just check whether the last stop was for a User Expression,
2908b1c73532SDimitry Andric // because breakpoint commands get run before stop hooks, and one of them
2909b1c73532SDimitry Andric // might have run an expression. You have to ensure you run the stop hooks
2910b1c73532SDimitry Andric // once per natural stop.
2911344a3780SDimitry Andric uint32_t last_natural_stop = m_process_sp->GetModIDRef().GetLastNaturalStopID();
2912344a3780SDimitry Andric if (last_natural_stop != 0 && m_latest_stop_hook_id == last_natural_stop)
2913344a3780SDimitry Andric return false;
2914344a3780SDimitry Andric
2915344a3780SDimitry Andric m_latest_stop_hook_id = last_natural_stop;
2916344a3780SDimitry Andric
2917f034231aSEd Maste std::vector<ExecutionContext> exc_ctx_with_reasons;
2918f034231aSEd Maste
2919f034231aSEd Maste ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2920f034231aSEd Maste size_t num_threads = cur_threadlist.GetSize();
292114f1b3e8SDimitry Andric for (size_t i = 0; i < num_threads; i++) {
2922f034231aSEd Maste lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(i);
292314f1b3e8SDimitry Andric if (cur_thread_sp->ThreadStoppedForAReason()) {
2924f034231aSEd Maste lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2925b60736ecSDimitry Andric exc_ctx_with_reasons.emplace_back(m_process_sp.get(), cur_thread_sp.get(),
2926b60736ecSDimitry Andric cur_frame_sp.get());
2927f034231aSEd Maste }
2928f034231aSEd Maste }
2929f034231aSEd Maste
2930f034231aSEd Maste // If no threads stopped for a reason, don't run the stop-hooks.
2931f034231aSEd Maste size_t num_exe_ctx = exc_ctx_with_reasons.size();
2932f034231aSEd Maste if (num_exe_ctx == 0)
2933b60736ecSDimitry Andric return false;
2934f034231aSEd Maste
2935b60736ecSDimitry Andric StreamSP output_sp = m_debugger.GetAsyncOutputStream();
2936f034231aSEd Maste
2937b60736ecSDimitry Andric bool auto_continue = false;
2938f034231aSEd Maste bool hooks_ran = false;
2939e81d9d49SDimitry Andric bool print_hook_header = (m_stop_hooks.size() != 1);
2940e81d9d49SDimitry Andric bool print_thread_header = (num_exe_ctx != 1);
2941b60736ecSDimitry Andric bool should_stop = false;
2942b60736ecSDimitry Andric bool somebody_restarted = false;
2943f034231aSEd Maste
2944b60736ecSDimitry Andric for (auto stop_entry : m_stop_hooks) {
2945b60736ecSDimitry Andric StopHookSP cur_hook_sp = stop_entry.second;
2946f034231aSEd Maste if (!cur_hook_sp->IsActive())
2947f034231aSEd Maste continue;
2948f034231aSEd Maste
2949f034231aSEd Maste bool any_thread_matched = false;
2950b60736ecSDimitry Andric for (auto exc_ctx : exc_ctx_with_reasons) {
2951b60736ecSDimitry Andric // We detect somebody restarted in the stop-hook loop, and broke out of
2952b60736ecSDimitry Andric // that loop back to here. So break out of here too.
2953b60736ecSDimitry Andric if (somebody_restarted)
2954b60736ecSDimitry Andric break;
2955b60736ecSDimitry Andric
2956b60736ecSDimitry Andric if (!cur_hook_sp->ExecutionContextPasses(exc_ctx))
2957b60736ecSDimitry Andric continue;
2958b60736ecSDimitry Andric
2959b60736ecSDimitry Andric // We only consult the auto-continue for a stop hook if it matched the
2960b60736ecSDimitry Andric // specifier.
2961b60736ecSDimitry Andric auto_continue |= cur_hook_sp->GetAutoContinue();
2962b60736ecSDimitry Andric
2963b60736ecSDimitry Andric if (!hooks_ran)
2964f034231aSEd Maste hooks_ran = true;
2965b60736ecSDimitry Andric
296614f1b3e8SDimitry Andric if (print_hook_header && !any_thread_matched) {
2967b60736ecSDimitry Andric StreamString s;
29687fa27ce4SDimitry Andric cur_hook_sp->GetDescription(s, eDescriptionLevelBrief);
2969b60736ecSDimitry Andric if (s.GetSize() != 0)
2970b60736ecSDimitry Andric output_sp->Printf("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(),
2971b60736ecSDimitry Andric s.GetData());
2972f034231aSEd Maste else
2973b60736ecSDimitry Andric output_sp->Printf("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
2974f034231aSEd Maste any_thread_matched = true;
2975f034231aSEd Maste }
2976f034231aSEd Maste
2977f034231aSEd Maste if (print_thread_header)
2978b60736ecSDimitry Andric output_sp->Printf("-- Thread %d\n",
2979b60736ecSDimitry Andric exc_ctx.GetThreadPtr()->GetIndexID());
2980f034231aSEd Maste
2981b60736ecSDimitry Andric StopHook::StopHookResult this_result =
2982b60736ecSDimitry Andric cur_hook_sp->HandleStop(exc_ctx, output_sp);
2983b60736ecSDimitry Andric bool this_should_stop = true;
2984205afe67SEd Maste
2985b60736ecSDimitry Andric switch (this_result) {
2986b60736ecSDimitry Andric case StopHook::StopHookResult::KeepStopped:
2987b60736ecSDimitry Andric // If this hook is set to auto-continue that should override the
2988b60736ecSDimitry Andric // HandleStop result...
2989b60736ecSDimitry Andric if (cur_hook_sp->GetAutoContinue())
2990b60736ecSDimitry Andric this_should_stop = false;
2991b60736ecSDimitry Andric else
2992b60736ecSDimitry Andric this_should_stop = true;
2993b60736ecSDimitry Andric
2994b60736ecSDimitry Andric break;
2995b60736ecSDimitry Andric case StopHook::StopHookResult::RequestContinue:
2996b60736ecSDimitry Andric this_should_stop = false;
2997b60736ecSDimitry Andric break;
2998b60736ecSDimitry Andric case StopHook::StopHookResult::AlreadyContinued:
2999b60736ecSDimitry Andric // We don't have a good way to prohibit people from restarting the
3000b60736ecSDimitry Andric // target willy nilly in a stop hook. If the hook did so, give a
3001b60736ecSDimitry Andric // gentle suggestion here and bag out if the hook processing.
3002b60736ecSDimitry Andric output_sp->Printf("\nAborting stop hooks, hook %" PRIu64
30035f29bb8aSDimitry Andric " set the program running.\n"
30045f29bb8aSDimitry Andric " Consider using '-G true' to make "
30055f29bb8aSDimitry Andric "stop hooks auto-continue.\n",
300614f1b3e8SDimitry Andric cur_hook_sp->GetID());
3007b60736ecSDimitry Andric somebody_restarted = true;
3008b60736ecSDimitry Andric break;
3009f034231aSEd Maste }
3010b60736ecSDimitry Andric // If we're already restarted, stop processing stop hooks.
3011b60736ecSDimitry Andric // FIXME: if we are doing non-stop mode for real, we would have to
3012b60736ecSDimitry Andric // check that OUR thread was restarted, otherwise we should keep
3013b60736ecSDimitry Andric // processing stop hooks.
3014b60736ecSDimitry Andric if (somebody_restarted)
3015b60736ecSDimitry Andric break;
3016f034231aSEd Maste
3017b60736ecSDimitry Andric // If anybody wanted to stop, we should all stop.
3018b60736ecSDimitry Andric if (!should_stop)
3019b60736ecSDimitry Andric should_stop = this_should_stop;
3020b60736ecSDimitry Andric }
3021b60736ecSDimitry Andric }
3022b60736ecSDimitry Andric
3023b60736ecSDimitry Andric output_sp->Flush();
3024b60736ecSDimitry Andric
3025b60736ecSDimitry Andric // If one of the commands in the stop hook already restarted the target,
3026b60736ecSDimitry Andric // report that fact.
3027b60736ecSDimitry Andric if (somebody_restarted)
3028b60736ecSDimitry Andric return true;
3029b60736ecSDimitry Andric
3030b60736ecSDimitry Andric // Finally, if auto-continue was requested, do it now:
3031b60736ecSDimitry Andric // We only compute should_stop against the hook results if a hook got to run
3032b60736ecSDimitry Andric // which is why we have to do this conjoint test.
3033b60736ecSDimitry Andric if ((hooks_ran && !should_stop) || auto_continue) {
3034145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
3035b60736ecSDimitry Andric Status error = m_process_sp->PrivateResume();
3036b60736ecSDimitry Andric if (error.Success()) {
3037b60736ecSDimitry Andric LLDB_LOG(log, "Resuming from RunStopHooks");
3038b60736ecSDimitry Andric return true;
3039b60736ecSDimitry Andric } else {
3040b60736ecSDimitry Andric LLDB_LOG(log, "Resuming from RunStopHooks failed: {0}", error);
3041b60736ecSDimitry Andric return false;
3042b60736ecSDimitry Andric }
3043b60736ecSDimitry Andric }
3044b60736ecSDimitry Andric
3045b60736ecSDimitry Andric return false;
3046f034231aSEd Maste }
3047f034231aSEd Maste
GetGlobalProperties()3048c0981da4SDimitry Andric TargetProperties &Target::GetGlobalProperties() {
3049f3fbd1c0SDimitry Andric // NOTE: intentional leak so we don't crash if global destructor chain gets
3050f3fbd1c0SDimitry Andric // called as other threads still use the result of this function
3051c0981da4SDimitry Andric static TargetProperties *g_settings_ptr =
3052c0981da4SDimitry Andric new TargetProperties(nullptr);
3053c0981da4SDimitry Andric return *g_settings_ptr;
305486758c71SEd Maste }
305586758c71SEd Maste
Install(ProcessLaunchInfo * launch_info)3056b76161e4SDimitry Andric Status Target::Install(ProcessLaunchInfo *launch_info) {
3057b76161e4SDimitry Andric Status error;
305886758c71SEd Maste PlatformSP platform_sp(GetPlatform());
305914f1b3e8SDimitry Andric if (platform_sp) {
306014f1b3e8SDimitry Andric if (platform_sp->IsRemote()) {
306114f1b3e8SDimitry Andric if (platform_sp->IsConnected()) {
3062cfca06d7SDimitry Andric // Install all files that have an install path when connected to a
3063cfca06d7SDimitry Andric // remote platform. If target.auto-install-main-executable is set then
3064cfca06d7SDimitry Andric // also install the main executable even if it does not have an explicit
3065cfca06d7SDimitry Andric // install path specified.
306686758c71SEd Maste const ModuleList &modules = GetImages();
306786758c71SEd Maste const size_t num_images = modules.GetSize();
306814f1b3e8SDimitry Andric for (size_t idx = 0; idx < num_images; ++idx) {
306986758c71SEd Maste ModuleSP module_sp(modules.GetModuleAtIndex(idx));
307014f1b3e8SDimitry Andric if (module_sp) {
3071f3fbd1c0SDimitry Andric const bool is_main_executable = module_sp == GetExecutableModule();
307286758c71SEd Maste FileSpec local_file(module_sp->GetFileSpec());
307314f1b3e8SDimitry Andric if (local_file) {
307486758c71SEd Maste FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
307514f1b3e8SDimitry Andric if (!remote_file) {
3076cfca06d7SDimitry Andric if (is_main_executable && GetAutoInstallMainExecutable()) {
3077cfca06d7SDimitry Andric // Automatically install the main executable.
30785e95aa85SEd Maste remote_file = platform_sp->GetRemoteWorkingDirectory();
307914f1b3e8SDimitry Andric remote_file.AppendPathComponent(
308014f1b3e8SDimitry Andric module_sp->GetFileSpec().GetFilename().GetCString());
308186758c71SEd Maste }
308286758c71SEd Maste }
308314f1b3e8SDimitry Andric if (remote_file) {
308486758c71SEd Maste error = platform_sp->Install(local_file, remote_file);
308514f1b3e8SDimitry Andric if (error.Success()) {
308686758c71SEd Maste module_sp->SetPlatformFileSpec(remote_file);
308714f1b3e8SDimitry Andric if (is_main_executable) {
30885e95aa85SEd Maste platform_sp->SetFilePermissions(remote_file, 0700);
308986758c71SEd Maste if (launch_info)
309086758c71SEd Maste launch_info->SetExecutableFile(remote_file, false);
309186758c71SEd Maste }
309214f1b3e8SDimitry Andric } else
309386758c71SEd Maste break;
309486758c71SEd Maste }
309586758c71SEd Maste }
309686758c71SEd Maste }
309786758c71SEd Maste }
309886758c71SEd Maste }
309986758c71SEd Maste }
310086758c71SEd Maste }
310186758c71SEd Maste return error;
310286758c71SEd Maste }
3103f034231aSEd Maste
ResolveLoadAddress(addr_t load_addr,Address & so_addr,uint32_t stop_id)310414f1b3e8SDimitry Andric bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
310514f1b3e8SDimitry Andric uint32_t stop_id) {
3106866dcdacSEd Maste return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
3107866dcdacSEd Maste }
3108866dcdacSEd Maste
ResolveFileAddress(lldb::addr_t file_addr,Address & resolved_addr)310914f1b3e8SDimitry Andric bool Target::ResolveFileAddress(lldb::addr_t file_addr,
311014f1b3e8SDimitry Andric Address &resolved_addr) {
3111205afe67SEd Maste return m_images.ResolveFileAddress(file_addr, resolved_addr);
3112205afe67SEd Maste }
3113205afe67SEd Maste
SetSectionLoadAddress(const SectionSP & section_sp,addr_t new_section_load_addr,bool warn_multiple)311414f1b3e8SDimitry Andric bool Target::SetSectionLoadAddress(const SectionSP §ion_sp,
311514f1b3e8SDimitry Andric addr_t new_section_load_addr,
311614f1b3e8SDimitry Andric bool warn_multiple) {
311714f1b3e8SDimitry Andric const addr_t old_section_load_addr =
311814f1b3e8SDimitry Andric m_section_load_history.GetSectionLoadAddress(
311914f1b3e8SDimitry Andric SectionLoadHistory::eStopIDNow, section_sp);
312014f1b3e8SDimitry Andric if (old_section_load_addr != new_section_load_addr) {
3121866dcdacSEd Maste uint32_t stop_id = 0;
3122866dcdacSEd Maste ProcessSP process_sp(GetProcessSP());
3123866dcdacSEd Maste if (process_sp)
3124866dcdacSEd Maste stop_id = process_sp->GetStopID();
3125866dcdacSEd Maste else
3126866dcdacSEd Maste stop_id = m_section_load_history.GetLastStopID();
312714f1b3e8SDimitry Andric if (m_section_load_history.SetSectionLoadAddress(
312814f1b3e8SDimitry Andric stop_id, section_sp, new_section_load_addr, warn_multiple))
3129866dcdacSEd Maste return true; // Return true if the section load address was changed...
3130866dcdacSEd Maste }
3131866dcdacSEd Maste return false; // Return false to indicate nothing changed
3132866dcdacSEd Maste }
3133866dcdacSEd Maste
UnloadModuleSections(const ModuleList & module_list)313414f1b3e8SDimitry Andric size_t Target::UnloadModuleSections(const ModuleList &module_list) {
3135205afe67SEd Maste size_t section_unload_count = 0;
3136205afe67SEd Maste size_t num_modules = module_list.GetSize();
313714f1b3e8SDimitry Andric for (size_t i = 0; i < num_modules; ++i) {
313814f1b3e8SDimitry Andric section_unload_count +=
313914f1b3e8SDimitry Andric UnloadModuleSections(module_list.GetModuleAtIndex(i));
3140205afe67SEd Maste }
3141205afe67SEd Maste return section_unload_count;
3142205afe67SEd Maste }
3143205afe67SEd Maste
UnloadModuleSections(const lldb::ModuleSP & module_sp)314414f1b3e8SDimitry Andric size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) {
3145205afe67SEd Maste uint32_t stop_id = 0;
3146205afe67SEd Maste ProcessSP process_sp(GetProcessSP());
3147205afe67SEd Maste if (process_sp)
3148205afe67SEd Maste stop_id = process_sp->GetStopID();
3149205afe67SEd Maste else
3150205afe67SEd Maste stop_id = m_section_load_history.GetLastStopID();
3151205afe67SEd Maste SectionList *sections = module_sp->GetSectionList();
3152205afe67SEd Maste size_t section_unload_count = 0;
315314f1b3e8SDimitry Andric if (sections) {
3154205afe67SEd Maste const uint32_t num_sections = sections->GetNumSections(0);
315514f1b3e8SDimitry Andric for (uint32_t i = 0; i < num_sections; ++i) {
315614f1b3e8SDimitry Andric section_unload_count += m_section_load_history.SetSectionUnloaded(
315714f1b3e8SDimitry Andric stop_id, sections->GetSectionAtIndex(i));
3158205afe67SEd Maste }
3159205afe67SEd Maste }
3160205afe67SEd Maste return section_unload_count;
3161205afe67SEd Maste }
3162205afe67SEd Maste
SetSectionUnloaded(const lldb::SectionSP & section_sp)316314f1b3e8SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP §ion_sp) {
3164866dcdacSEd Maste uint32_t stop_id = 0;
3165866dcdacSEd Maste ProcessSP process_sp(GetProcessSP());
3166866dcdacSEd Maste if (process_sp)
3167866dcdacSEd Maste stop_id = process_sp->GetStopID();
3168866dcdacSEd Maste else
3169866dcdacSEd Maste stop_id = m_section_load_history.GetLastStopID();
3170866dcdacSEd Maste return m_section_load_history.SetSectionUnloaded(stop_id, section_sp);
3171866dcdacSEd Maste }
3172866dcdacSEd Maste
SetSectionUnloaded(const lldb::SectionSP & section_sp,addr_t load_addr)317314f1b3e8SDimitry Andric bool Target::SetSectionUnloaded(const lldb::SectionSP §ion_sp,
317414f1b3e8SDimitry Andric addr_t load_addr) {
3175866dcdacSEd Maste uint32_t stop_id = 0;
3176866dcdacSEd Maste ProcessSP process_sp(GetProcessSP());
3177866dcdacSEd Maste if (process_sp)
3178866dcdacSEd Maste stop_id = process_sp->GetStopID();
3179866dcdacSEd Maste else
3180866dcdacSEd Maste stop_id = m_section_load_history.GetLastStopID();
318114f1b3e8SDimitry Andric return m_section_load_history.SetSectionUnloaded(stop_id, section_sp,
318214f1b3e8SDimitry Andric load_addr);
3183866dcdacSEd Maste }
3184866dcdacSEd Maste
ClearAllLoadedSections()318514f1b3e8SDimitry Andric void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
3186866dcdacSEd Maste
SaveScriptedLaunchInfo(lldb_private::ProcessInfo & process_info)31877fa27ce4SDimitry Andric void Target::SaveScriptedLaunchInfo(lldb_private::ProcessInfo &process_info) {
31887fa27ce4SDimitry Andric if (process_info.IsScriptedProcess()) {
31897fa27ce4SDimitry Andric // Only copy scripted process launch options.
31907fa27ce4SDimitry Andric ProcessLaunchInfo &default_launch_info = const_cast<ProcessLaunchInfo &>(
31917fa27ce4SDimitry Andric GetGlobalProperties().GetProcessLaunchInfo());
31927fa27ce4SDimitry Andric default_launch_info.SetProcessPluginName("ScriptedProcess");
31937fa27ce4SDimitry Andric default_launch_info.SetScriptedMetadata(process_info.GetScriptedMetadata());
31947fa27ce4SDimitry Andric SetProcessLaunchInfo(default_launch_info);
31957fa27ce4SDimitry Andric }
31967fa27ce4SDimitry Andric }
31977fa27ce4SDimitry Andric
Launch(ProcessLaunchInfo & launch_info,Stream * stream)3198b76161e4SDimitry Andric Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
3199c0981da4SDimitry Andric m_stats.SetLaunchOrAttachTime();
3200b76161e4SDimitry Andric Status error;
3201145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Target);
3202205afe67SEd Maste
3203ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s() called for %s", __FUNCTION__,
320414f1b3e8SDimitry Andric launch_info.GetExecutableFile().GetPath().c_str());
3205866dcdacSEd Maste
3206866dcdacSEd Maste StateType state = eStateInvalid;
3207866dcdacSEd Maste
3208866dcdacSEd Maste // Scope to temporarily get the process state in case someone has manually
3209866dcdacSEd Maste // remotely connected already to a process and we can skip the platform
3210866dcdacSEd Maste // launching.
3211866dcdacSEd Maste {
3212866dcdacSEd Maste ProcessSP process_sp(GetProcessSP());
3213866dcdacSEd Maste
321414f1b3e8SDimitry Andric if (process_sp) {
3215866dcdacSEd Maste state = process_sp->GetState();
3216ead24645SDimitry Andric LLDB_LOGF(log,
321714f1b3e8SDimitry Andric "Target::%s the process exists, and its current state is %s",
321814f1b3e8SDimitry Andric __FUNCTION__, StateAsCString(state));
321914f1b3e8SDimitry Andric } else {
3220ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s the process instance doesn't currently exist.",
322114f1b3e8SDimitry Andric __FUNCTION__);
3222205afe67SEd Maste }
3223866dcdacSEd Maste }
3224866dcdacSEd Maste
3225866dcdacSEd Maste launch_info.GetFlags().Set(eLaunchFlagDebug);
3226866dcdacSEd Maste
32277fa27ce4SDimitry Andric SaveScriptedLaunchInfo(launch_info);
3228344a3780SDimitry Andric
322914f1b3e8SDimitry Andric // Get the value of synchronous execution here. If you wait till after you
3230f73363f1SDimitry Andric // have started to run, then you could have hit a breakpoint, whose command
3231f73363f1SDimitry Andric // might switch the value, and then you'll pick up that incorrect value.
3232866dcdacSEd Maste Debugger &debugger = GetDebugger();
323314f1b3e8SDimitry Andric const bool synchronous_execution =
323414f1b3e8SDimitry Andric debugger.GetCommandInterpreter().GetSynchronous();
3235866dcdacSEd Maste
3236866dcdacSEd Maste PlatformSP platform_sp(GetPlatform());
3237866dcdacSEd Maste
323894994d37SDimitry Andric FinalizeFileActions(launch_info);
3239866dcdacSEd Maste
324014f1b3e8SDimitry Andric if (state == eStateConnected) {
324114f1b3e8SDimitry Andric if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
324214f1b3e8SDimitry Andric error.SetErrorString(
324314f1b3e8SDimitry Andric "can't launch in tty when launching through a remote connection");
3244866dcdacSEd Maste return error;
3245866dcdacSEd Maste }
3246866dcdacSEd Maste }
3247866dcdacSEd Maste
3248866dcdacSEd Maste if (!launch_info.GetArchitecture().IsValid())
3249866dcdacSEd Maste launch_info.GetArchitecture() = GetArchitecture();
3250866dcdacSEd Maste
3251145449b1SDimitry Andric // Hijacking events of the process to be created to be sure that all events
3252145449b1SDimitry Andric // until the first stop are intercepted (in case if platform doesn't define
3253145449b1SDimitry Andric // its own hijacking listener or if the process is created by the target
3254145449b1SDimitry Andric // manually, without the platform).
3255145449b1SDimitry Andric if (!launch_info.GetHijackListener())
32567fa27ce4SDimitry Andric launch_info.SetHijackListener(Listener::MakeListener(
32577fa27ce4SDimitry Andric Process::LaunchSynchronousHijackListenerName.data()));
3258145449b1SDimitry Andric
325914f1b3e8SDimitry Andric // If we're not already connected to the process, and if we have a platform
326014f1b3e8SDimitry Andric // that can launch a process for debugging, go ahead and do that here.
326114f1b3e8SDimitry Andric if (state != eStateConnected && platform_sp &&
3262344a3780SDimitry Andric platform_sp->CanDebugProcess() && !launch_info.IsScriptedProcess()) {
3263ead24645SDimitry Andric LLDB_LOGF(log, "Target::%s asking the platform to debug the process",
326414f1b3e8SDimitry Andric __FUNCTION__);
3265205afe67SEd Maste
326694994d37SDimitry Andric // If there was a previous process, delete it before we make the new one.
326794994d37SDimitry Andric // One subtle point, we delete the process before we release the reference
326894994d37SDimitry Andric // to m_process_sp. That way even if we are the last owner, the process
326994994d37SDimitry Andric // will get Finalized before it gets destroyed.
327094994d37SDimitry Andric DeleteCurrentProcess();
327194994d37SDimitry Andric
327214f1b3e8SDimitry Andric m_process_sp =
3273c0981da4SDimitry Andric GetPlatform()->DebugProcess(launch_info, debugger, *this, error);
32745e95aa85SEd Maste
327514f1b3e8SDimitry Andric } else {
3276ead24645SDimitry Andric LLDB_LOGF(log,
3277ead24645SDimitry Andric "Target::%s the platform doesn't know how to debug a "
327814f1b3e8SDimitry Andric "process, getting a process plugin to do this for us.",
327914f1b3e8SDimitry Andric __FUNCTION__);
3280205afe67SEd Maste
328114f1b3e8SDimitry Andric if (state == eStateConnected) {
3282866dcdacSEd Maste assert(m_process_sp);
328314f1b3e8SDimitry Andric } else {
32840cac4ca3SEd Maste // Use a Process plugin to construct the process.
32857fa27ce4SDimitry Andric CreateProcess(launch_info.GetListener(),
32867fa27ce4SDimitry Andric launch_info.GetProcessPluginName(), nullptr, false);
3287866dcdacSEd Maste }
3288866dcdacSEd Maste
32890cac4ca3SEd Maste // Since we didn't have a platform launch the process, launch it here.
3290145449b1SDimitry Andric if (m_process_sp) {
3291145449b1SDimitry Andric m_process_sp->HijackProcessEvents(launch_info.GetHijackListener());
32927fa27ce4SDimitry Andric m_process_sp->SetShadowListener(launch_info.GetShadowListener());
3293866dcdacSEd Maste error = m_process_sp->Launch(launch_info);
3294866dcdacSEd Maste }
3295145449b1SDimitry Andric }
3296866dcdacSEd Maste
3297b60736ecSDimitry Andric if (!m_process_sp && error.Success())
3298866dcdacSEd Maste error.SetErrorString("failed to launch or debug process");
3299866dcdacSEd Maste
3300b60736ecSDimitry Andric if (!error.Success())
3301b60736ecSDimitry Andric return error;
3302b60736ecSDimitry Andric
3303145449b1SDimitry Andric bool rebroadcast_first_stop =
3304145449b1SDimitry Andric !synchronous_execution &&
3305145449b1SDimitry Andric launch_info.GetFlags().Test(eLaunchFlagStopAtEntry);
3306b60736ecSDimitry Andric
3307145449b1SDimitry Andric assert(launch_info.GetHijackListener());
3308145449b1SDimitry Andric
3309145449b1SDimitry Andric EventSP first_stop_event_sp;
3310e3b55780SDimitry Andric state = m_process_sp->WaitForProcessToStop(std::nullopt, &first_stop_event_sp,
3311145449b1SDimitry Andric rebroadcast_first_stop,
3312145449b1SDimitry Andric launch_info.GetHijackListener());
3313145449b1SDimitry Andric m_process_sp->RestoreProcessEvents();
3314145449b1SDimitry Andric
3315145449b1SDimitry Andric if (rebroadcast_first_stop) {
3316145449b1SDimitry Andric assert(first_stop_event_sp);
3317145449b1SDimitry Andric m_process_sp->BroadcastEvent(first_stop_event_sp);
3318b60736ecSDimitry Andric return error;
33195e95aa85SEd Maste }
3320866dcdacSEd Maste
3321145449b1SDimitry Andric switch (state) {
3322b60736ecSDimitry Andric case eStateStopped: {
3323b60736ecSDimitry Andric if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
3324b60736ecSDimitry Andric break;
3325145449b1SDimitry Andric if (synchronous_execution)
3326ead24645SDimitry Andric // Now we have handled the stop-from-attach, and we are just
3327ead24645SDimitry Andric // switching to a synchronous resume. So we should switch to the
3328ead24645SDimitry Andric // SyncResume hijacker.
33295f29bb8aSDimitry Andric m_process_sp->ResumeSynchronous(stream);
3330145449b1SDimitry Andric else
33317fa27ce4SDimitry Andric error = m_process_sp->Resume();
333214f1b3e8SDimitry Andric if (!error.Success()) {
3333b76161e4SDimitry Andric Status error2;
333414f1b3e8SDimitry Andric error2.SetErrorStringWithFormat(
333514f1b3e8SDimitry Andric "process resume at entry point failed: %s", error.AsCString());
3336866dcdacSEd Maste error = error2;
3337866dcdacSEd Maste }
3338b60736ecSDimitry Andric } break;
3339b60736ecSDimitry Andric case eStateExited: {
3340205afe67SEd Maste bool with_shell = !!launch_info.GetShell();
33410cac4ca3SEd Maste const int exit_status = m_process_sp->GetExitStatus();
33420cac4ca3SEd Maste const char *exit_desc = m_process_sp->GetExitDescription();
3343b60736ecSDimitry Andric std::string desc;
3344b60736ecSDimitry Andric if (exit_desc && exit_desc[0])
3345b60736ecSDimitry Andric desc = " (" + std::string(exit_desc) + ')';
33460cac4ca3SEd Maste if (with_shell)
334714f1b3e8SDimitry Andric error.SetErrorStringWithFormat(
3348b60736ecSDimitry Andric "process exited with status %i%s\n"
3349b60736ecSDimitry Andric "'r' and 'run' are aliases that default to launching through a "
3350b60736ecSDimitry Andric "shell.\n"
3351b60736ecSDimitry Andric "Try launching without going through a shell by using "
3352b60736ecSDimitry Andric "'process launch'.",
3353b60736ecSDimitry Andric exit_status, desc.c_str());
33540cac4ca3SEd Maste else
3355b60736ecSDimitry Andric error.SetErrorStringWithFormat("process exited with status %i%s",
3356b60736ecSDimitry Andric exit_status, desc.c_str());
3357b60736ecSDimitry Andric } break;
3358b60736ecSDimitry Andric default:
3359b60736ecSDimitry Andric error.SetErrorStringWithFormat("initial process state wasn't stopped: %s",
3360b60736ecSDimitry Andric StateAsCString(state));
3361b60736ecSDimitry Andric break;
3362866dcdacSEd Maste }
3363866dcdacSEd Maste return error;
3364866dcdacSEd Maste }
33655e95aa85SEd Maste
SetTrace(const TraceSP & trace_sp)3366b60736ecSDimitry Andric void Target::SetTrace(const TraceSP &trace_sp) { m_trace_sp = trace_sp; }
3367b60736ecSDimitry Andric
GetTrace()3368344a3780SDimitry Andric TraceSP Target::GetTrace() { return m_trace_sp; }
3369344a3780SDimitry Andric
CreateTrace()3370344a3780SDimitry Andric llvm::Expected<TraceSP> Target::CreateTrace() {
3371344a3780SDimitry Andric if (!m_process_sp)
3372344a3780SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(),
3373344a3780SDimitry Andric "A process is required for tracing");
3374344a3780SDimitry Andric if (m_trace_sp)
3375344a3780SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(),
3376344a3780SDimitry Andric "A trace already exists for the target");
3377344a3780SDimitry Andric
3378344a3780SDimitry Andric llvm::Expected<TraceSupportedResponse> trace_type =
3379344a3780SDimitry Andric m_process_sp->TraceSupported();
3380344a3780SDimitry Andric if (!trace_type)
3381344a3780SDimitry Andric return llvm::createStringError(
3382344a3780SDimitry Andric llvm::inconvertibleErrorCode(), "Tracing is not supported. %s",
3383344a3780SDimitry Andric llvm::toString(trace_type.takeError()).c_str());
3384344a3780SDimitry Andric if (llvm::Expected<TraceSP> trace_sp =
3385344a3780SDimitry Andric Trace::FindPluginForLiveProcess(trace_type->name, *m_process_sp))
3386344a3780SDimitry Andric m_trace_sp = *trace_sp;
3387344a3780SDimitry Andric else
3388344a3780SDimitry Andric return llvm::createStringError(
3389344a3780SDimitry Andric llvm::inconvertibleErrorCode(),
3390344a3780SDimitry Andric "Couldn't create a Trace object for the process. %s",
3391344a3780SDimitry Andric llvm::toString(trace_sp.takeError()).c_str());
3392344a3780SDimitry Andric return m_trace_sp;
3393344a3780SDimitry Andric }
3394344a3780SDimitry Andric
GetTraceOrCreate()3395344a3780SDimitry Andric llvm::Expected<TraceSP> Target::GetTraceOrCreate() {
3396344a3780SDimitry Andric if (m_trace_sp)
3397344a3780SDimitry Andric return m_trace_sp;
3398344a3780SDimitry Andric return CreateTrace();
3399344a3780SDimitry Andric }
3400b60736ecSDimitry Andric
Attach(ProcessAttachInfo & attach_info,Stream * stream)3401b76161e4SDimitry Andric Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
3402c0981da4SDimitry Andric m_stats.SetLaunchOrAttachTime();
34035e95aa85SEd Maste auto state = eStateInvalid;
34045e95aa85SEd Maste auto process_sp = GetProcessSP();
340514f1b3e8SDimitry Andric if (process_sp) {
34065e95aa85SEd Maste state = process_sp->GetState();
340714f1b3e8SDimitry Andric if (process_sp->IsAlive() && state != eStateConnected) {
34085e95aa85SEd Maste if (state == eStateAttaching)
3409b76161e4SDimitry Andric return Status("process attach is in progress");
3410b76161e4SDimitry Andric return Status("a process is already being debugged");
34115e95aa85SEd Maste }
34125e95aa85SEd Maste }
34135e95aa85SEd Maste
34145e95aa85SEd Maste const ModuleSP old_exec_module_sp = GetExecutableModule();
34155e95aa85SEd Maste
3416f73363f1SDimitry Andric // If no process info was specified, then use the target executable name as
3417f73363f1SDimitry Andric // the process to attach to by default
341814f1b3e8SDimitry Andric if (!attach_info.ProcessInfoSpecified()) {
34195e95aa85SEd Maste if (old_exec_module_sp)
3420e3b55780SDimitry Andric attach_info.GetExecutableFile().SetFilename(
3421e3b55780SDimitry Andric old_exec_module_sp->GetPlatformFileSpec().GetFilename());
34225e95aa85SEd Maste
342314f1b3e8SDimitry Andric if (!attach_info.ProcessInfoSpecified()) {
3424b76161e4SDimitry Andric return Status("no process specified, create a target with a file, or "
342514f1b3e8SDimitry Andric "specify the --pid or --name");
34265e95aa85SEd Maste }
34275e95aa85SEd Maste }
34285e95aa85SEd Maste
342914f1b3e8SDimitry Andric const auto platform_sp =
343014f1b3e8SDimitry Andric GetDebugger().GetPlatformList().GetSelectedPlatform();
3431e81d9d49SDimitry Andric ListenerSP hijack_listener_sp;
3432e81d9d49SDimitry Andric const bool async = attach_info.GetAsync();
343314f1b3e8SDimitry Andric if (!async) {
34347fa27ce4SDimitry Andric hijack_listener_sp = Listener::MakeListener(
34357fa27ce4SDimitry Andric Process::AttachSynchronousHijackListenerName.data());
3436e81d9d49SDimitry Andric attach_info.SetHijackListener(hijack_listener_sp);
3437e81d9d49SDimitry Andric }
34385e95aa85SEd Maste
3439b76161e4SDimitry Andric Status error;
344014f1b3e8SDimitry Andric if (state != eStateConnected && platform_sp != nullptr &&
34417fa27ce4SDimitry Andric platform_sp->CanDebugProcess() && !attach_info.IsScriptedProcess()) {
34425e95aa85SEd Maste SetPlatform(platform_sp);
34435e95aa85SEd Maste process_sp = platform_sp->Attach(attach_info, GetDebugger(), this, error);
344414f1b3e8SDimitry Andric } else {
344514f1b3e8SDimitry Andric if (state != eStateConnected) {
34467fa27ce4SDimitry Andric SaveScriptedLaunchInfo(attach_info);
34477fa27ce4SDimitry Andric llvm::StringRef plugin_name = attach_info.GetProcessPluginName();
344814f1b3e8SDimitry Andric process_sp =
344914f1b3e8SDimitry Andric CreateProcess(attach_info.GetListenerForProcess(GetDebugger()),
3450b60736ecSDimitry Andric plugin_name, nullptr, false);
34517fa27ce4SDimitry Andric if (!process_sp) {
34527fa27ce4SDimitry Andric error.SetErrorStringWithFormatv(
34537fa27ce4SDimitry Andric "failed to create process using plugin '{0}'",
34547fa27ce4SDimitry Andric plugin_name.empty() ? "<empty>" : plugin_name);
34555e95aa85SEd Maste return error;
34565e95aa85SEd Maste }
34575e95aa85SEd Maste }
3458e81d9d49SDimitry Andric if (hijack_listener_sp)
3459f3fbd1c0SDimitry Andric process_sp->HijackProcessEvents(hijack_listener_sp);
34605e95aa85SEd Maste error = process_sp->Attach(attach_info);
34615e95aa85SEd Maste }
34625e95aa85SEd Maste
346314f1b3e8SDimitry Andric if (error.Success() && process_sp) {
346414f1b3e8SDimitry Andric if (async) {
3465e81d9d49SDimitry Andric process_sp->RestoreProcessEvents();
346614f1b3e8SDimitry Andric } else {
34677fa27ce4SDimitry Andric // We are stopping all the way out to the user, so update selected frames.
34687fa27ce4SDimitry Andric state = process_sp->WaitForProcessToStop(
34697fa27ce4SDimitry Andric std::nullopt, nullptr, false, attach_info.GetHijackListener(), stream,
34707fa27ce4SDimitry Andric true, SelectMostRelevantFrame);
34715e95aa85SEd Maste process_sp->RestoreProcessEvents();
34725e95aa85SEd Maste
347314f1b3e8SDimitry Andric if (state != eStateStopped) {
34745e95aa85SEd Maste const char *exit_desc = process_sp->GetExitDescription();
34755e95aa85SEd Maste if (exit_desc)
3476e81d9d49SDimitry Andric error.SetErrorStringWithFormat("%s", exit_desc);
34775e95aa85SEd Maste else
347814f1b3e8SDimitry Andric error.SetErrorString(
347914f1b3e8SDimitry Andric "process did not stop (no such process or permission problem?)");
34805e95aa85SEd Maste process_sp->Destroy(false);
34815e95aa85SEd Maste }
34825e95aa85SEd Maste }
3483e81d9d49SDimitry Andric }
34845e95aa85SEd Maste return error;
34855e95aa85SEd Maste }
34865e95aa85SEd Maste
FinalizeFileActions(ProcessLaunchInfo & info)348794994d37SDimitry Andric void Target::FinalizeFileActions(ProcessLaunchInfo &info) {
3488145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
348994994d37SDimitry Andric
349094994d37SDimitry Andric // Finalize the file actions, and if none were given, default to opening up a
349194994d37SDimitry Andric // pseudo terminal
349294994d37SDimitry Andric PlatformSP platform_sp = GetPlatform();
349394994d37SDimitry Andric const bool default_to_use_pty =
349494994d37SDimitry Andric m_platform_sp ? m_platform_sp->IsHost() : false;
349594994d37SDimitry Andric LLDB_LOG(
349694994d37SDimitry Andric log,
349794994d37SDimitry Andric "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}",
349894994d37SDimitry Andric bool(platform_sp),
349994994d37SDimitry Andric platform_sp ? (platform_sp->IsHost() ? "true" : "false") : "n/a",
350094994d37SDimitry Andric default_to_use_pty);
350194994d37SDimitry Andric
350294994d37SDimitry Andric // If nothing for stdin or stdout or stderr was specified, then check the
350394994d37SDimitry Andric // process for any default settings that were set with "settings set"
350494994d37SDimitry Andric if (info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
350594994d37SDimitry Andric info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
350694994d37SDimitry Andric info.GetFileActionForFD(STDERR_FILENO) == nullptr) {
350794994d37SDimitry Andric LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating "
350894994d37SDimitry Andric "default handling");
350994994d37SDimitry Andric
351094994d37SDimitry Andric if (info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
351194994d37SDimitry Andric // Do nothing, if we are launching in a remote terminal no file actions
351294994d37SDimitry Andric // should be done at all.
351394994d37SDimitry Andric return;
351494994d37SDimitry Andric }
351594994d37SDimitry Andric
351694994d37SDimitry Andric if (info.GetFlags().Test(eLaunchFlagDisableSTDIO)) {
351794994d37SDimitry Andric LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action "
351894994d37SDimitry Andric "for stdin, stdout and stderr");
351994994d37SDimitry Andric info.AppendSuppressFileAction(STDIN_FILENO, true, false);
352094994d37SDimitry Andric info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
352194994d37SDimitry Andric info.AppendSuppressFileAction(STDERR_FILENO, false, true);
352294994d37SDimitry Andric } else {
352394994d37SDimitry Andric // Check for any values that might have gotten set with any of: (lldb)
352494994d37SDimitry Andric // settings set target.input-path (lldb) settings set target.output-path
352594994d37SDimitry Andric // (lldb) settings set target.error-path
352694994d37SDimitry Andric FileSpec in_file_spec;
352794994d37SDimitry Andric FileSpec out_file_spec;
352894994d37SDimitry Andric FileSpec err_file_spec;
352994994d37SDimitry Andric // Only override with the target settings if we don't already have an
353094994d37SDimitry Andric // action for in, out or error
353194994d37SDimitry Andric if (info.GetFileActionForFD(STDIN_FILENO) == nullptr)
353294994d37SDimitry Andric in_file_spec = GetStandardInputPath();
353394994d37SDimitry Andric if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr)
353494994d37SDimitry Andric out_file_spec = GetStandardOutputPath();
353594994d37SDimitry Andric if (info.GetFileActionForFD(STDERR_FILENO) == nullptr)
353694994d37SDimitry Andric err_file_spec = GetStandardErrorPath();
353794994d37SDimitry Andric
353894994d37SDimitry Andric LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{1}'",
353994994d37SDimitry Andric in_file_spec, out_file_spec, err_file_spec);
354094994d37SDimitry Andric
354194994d37SDimitry Andric if (in_file_spec) {
354294994d37SDimitry Andric info.AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
354394994d37SDimitry Andric LLDB_LOG(log, "appended stdin open file action for {0}", in_file_spec);
354494994d37SDimitry Andric }
354594994d37SDimitry Andric
354694994d37SDimitry Andric if (out_file_spec) {
354794994d37SDimitry Andric info.AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
354894994d37SDimitry Andric LLDB_LOG(log, "appended stdout open file action for {0}",
354994994d37SDimitry Andric out_file_spec);
355094994d37SDimitry Andric }
355194994d37SDimitry Andric
355294994d37SDimitry Andric if (err_file_spec) {
355394994d37SDimitry Andric info.AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
355494994d37SDimitry Andric LLDB_LOG(log, "appended stderr open file action for {0}",
355594994d37SDimitry Andric err_file_spec);
355694994d37SDimitry Andric }
355794994d37SDimitry Andric
355877fc4c14SDimitry Andric if (default_to_use_pty) {
355994994d37SDimitry Andric llvm::Error Err = info.SetUpPtyRedirection();
356094994d37SDimitry Andric LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
356194994d37SDimitry Andric }
356294994d37SDimitry Andric }
356394994d37SDimitry Andric }
356494994d37SDimitry Andric }
356594994d37SDimitry Andric
AddDummySignal(llvm::StringRef name,LazyBool pass,LazyBool notify,LazyBool stop)3566145449b1SDimitry Andric void Target::AddDummySignal(llvm::StringRef name, LazyBool pass, LazyBool notify,
3567145449b1SDimitry Andric LazyBool stop) {
3568145449b1SDimitry Andric if (name.empty())
3569145449b1SDimitry Andric return;
3570145449b1SDimitry Andric // Don't add a signal if all the actions are trivial:
3571145449b1SDimitry Andric if (pass == eLazyBoolCalculate && notify == eLazyBoolCalculate
3572145449b1SDimitry Andric && stop == eLazyBoolCalculate)
3573145449b1SDimitry Andric return;
3574145449b1SDimitry Andric
3575145449b1SDimitry Andric auto& elem = m_dummy_signals[name];
3576145449b1SDimitry Andric elem.pass = pass;
3577145449b1SDimitry Andric elem.notify = notify;
3578145449b1SDimitry Andric elem.stop = stop;
3579145449b1SDimitry Andric }
3580145449b1SDimitry Andric
UpdateSignalFromDummy(UnixSignalsSP signals_sp,const DummySignalElement & elem)3581145449b1SDimitry Andric bool Target::UpdateSignalFromDummy(UnixSignalsSP signals_sp,
3582145449b1SDimitry Andric const DummySignalElement &elem) {
3583145449b1SDimitry Andric if (!signals_sp)
3584145449b1SDimitry Andric return false;
3585145449b1SDimitry Andric
3586145449b1SDimitry Andric int32_t signo
3587145449b1SDimitry Andric = signals_sp->GetSignalNumberFromName(elem.first().str().c_str());
3588145449b1SDimitry Andric if (signo == LLDB_INVALID_SIGNAL_NUMBER)
3589145449b1SDimitry Andric return false;
3590145449b1SDimitry Andric
3591145449b1SDimitry Andric if (elem.second.pass == eLazyBoolYes)
3592145449b1SDimitry Andric signals_sp->SetShouldSuppress(signo, false);
3593145449b1SDimitry Andric else if (elem.second.pass == eLazyBoolNo)
3594145449b1SDimitry Andric signals_sp->SetShouldSuppress(signo, true);
3595145449b1SDimitry Andric
3596145449b1SDimitry Andric if (elem.second.notify == eLazyBoolYes)
3597145449b1SDimitry Andric signals_sp->SetShouldNotify(signo, true);
3598145449b1SDimitry Andric else if (elem.second.notify == eLazyBoolNo)
3599145449b1SDimitry Andric signals_sp->SetShouldNotify(signo, false);
3600145449b1SDimitry Andric
3601145449b1SDimitry Andric if (elem.second.stop == eLazyBoolYes)
3602145449b1SDimitry Andric signals_sp->SetShouldStop(signo, true);
3603145449b1SDimitry Andric else if (elem.second.stop == eLazyBoolNo)
3604145449b1SDimitry Andric signals_sp->SetShouldStop(signo, false);
3605145449b1SDimitry Andric return true;
3606145449b1SDimitry Andric }
3607145449b1SDimitry Andric
ResetSignalFromDummy(UnixSignalsSP signals_sp,const DummySignalElement & elem)3608145449b1SDimitry Andric bool Target::ResetSignalFromDummy(UnixSignalsSP signals_sp,
3609145449b1SDimitry Andric const DummySignalElement &elem) {
3610145449b1SDimitry Andric if (!signals_sp)
3611145449b1SDimitry Andric return false;
3612145449b1SDimitry Andric int32_t signo
3613145449b1SDimitry Andric = signals_sp->GetSignalNumberFromName(elem.first().str().c_str());
3614145449b1SDimitry Andric if (signo == LLDB_INVALID_SIGNAL_NUMBER)
3615145449b1SDimitry Andric return false;
3616145449b1SDimitry Andric bool do_pass = elem.second.pass != eLazyBoolCalculate;
3617145449b1SDimitry Andric bool do_stop = elem.second.stop != eLazyBoolCalculate;
3618145449b1SDimitry Andric bool do_notify = elem.second.notify != eLazyBoolCalculate;
3619145449b1SDimitry Andric signals_sp->ResetSignal(signo, do_stop, do_notify, do_pass);
3620145449b1SDimitry Andric return true;
3621145449b1SDimitry Andric }
3622145449b1SDimitry Andric
UpdateSignalsFromDummy(UnixSignalsSP signals_sp,StreamSP warning_stream_sp)3623145449b1SDimitry Andric void Target::UpdateSignalsFromDummy(UnixSignalsSP signals_sp,
3624145449b1SDimitry Andric StreamSP warning_stream_sp) {
3625145449b1SDimitry Andric if (!signals_sp)
3626145449b1SDimitry Andric return;
3627145449b1SDimitry Andric
3628145449b1SDimitry Andric for (const auto &elem : m_dummy_signals) {
3629145449b1SDimitry Andric if (!UpdateSignalFromDummy(signals_sp, elem))
3630145449b1SDimitry Andric warning_stream_sp->Printf("Target signal '%s' not found in process\n",
3631145449b1SDimitry Andric elem.first().str().c_str());
3632145449b1SDimitry Andric }
3633145449b1SDimitry Andric }
3634145449b1SDimitry Andric
ClearDummySignals(Args & signal_names)3635145449b1SDimitry Andric void Target::ClearDummySignals(Args &signal_names) {
3636145449b1SDimitry Andric ProcessSP process_sp = GetProcessSP();
3637145449b1SDimitry Andric // The simplest case, delete them all with no process to update.
3638145449b1SDimitry Andric if (signal_names.GetArgumentCount() == 0 && !process_sp) {
3639145449b1SDimitry Andric m_dummy_signals.clear();
3640145449b1SDimitry Andric return;
3641145449b1SDimitry Andric }
3642145449b1SDimitry Andric UnixSignalsSP signals_sp;
3643145449b1SDimitry Andric if (process_sp)
3644145449b1SDimitry Andric signals_sp = process_sp->GetUnixSignals();
3645145449b1SDimitry Andric
3646145449b1SDimitry Andric for (const Args::ArgEntry &entry : signal_names) {
3647145449b1SDimitry Andric const char *signal_name = entry.c_str();
3648145449b1SDimitry Andric auto elem = m_dummy_signals.find(signal_name);
3649145449b1SDimitry Andric // If we didn't find it go on.
3650145449b1SDimitry Andric // FIXME: Should I pipe error handling through here?
3651145449b1SDimitry Andric if (elem == m_dummy_signals.end()) {
3652145449b1SDimitry Andric continue;
3653145449b1SDimitry Andric }
3654145449b1SDimitry Andric if (signals_sp)
3655145449b1SDimitry Andric ResetSignalFromDummy(signals_sp, *elem);
3656145449b1SDimitry Andric m_dummy_signals.erase(elem);
3657145449b1SDimitry Andric }
3658145449b1SDimitry Andric }
3659145449b1SDimitry Andric
PrintDummySignals(Stream & strm,Args & signal_args)3660145449b1SDimitry Andric void Target::PrintDummySignals(Stream &strm, Args &signal_args) {
3661145449b1SDimitry Andric strm.Printf("NAME PASS STOP NOTIFY\n");
3662145449b1SDimitry Andric strm.Printf("=========== ======= ======= =======\n");
3663145449b1SDimitry Andric
3664145449b1SDimitry Andric auto str_for_lazy = [] (LazyBool lazy) -> const char * {
3665145449b1SDimitry Andric switch (lazy) {
3666145449b1SDimitry Andric case eLazyBoolCalculate: return "not set";
3667145449b1SDimitry Andric case eLazyBoolYes: return "true ";
3668145449b1SDimitry Andric case eLazyBoolNo: return "false ";
3669145449b1SDimitry Andric }
3670145449b1SDimitry Andric llvm_unreachable("Fully covered switch above!");
3671145449b1SDimitry Andric };
3672145449b1SDimitry Andric size_t num_args = signal_args.GetArgumentCount();
3673145449b1SDimitry Andric for (const auto &elem : m_dummy_signals) {
3674145449b1SDimitry Andric bool print_it = false;
3675145449b1SDimitry Andric for (size_t idx = 0; idx < num_args; idx++) {
3676145449b1SDimitry Andric if (elem.first() == signal_args.GetArgumentAtIndex(idx)) {
3677145449b1SDimitry Andric print_it = true;
3678145449b1SDimitry Andric break;
3679145449b1SDimitry Andric }
3680145449b1SDimitry Andric }
3681145449b1SDimitry Andric if (print_it) {
3682145449b1SDimitry Andric strm.Printf("%-11s ", elem.first().str().c_str());
3683145449b1SDimitry Andric strm.Printf("%s %s %s\n", str_for_lazy(elem.second.pass),
3684145449b1SDimitry Andric str_for_lazy(elem.second.stop),
3685145449b1SDimitry Andric str_for_lazy(elem.second.notify));
3686145449b1SDimitry Andric }
3687145449b1SDimitry Andric }
3688145449b1SDimitry Andric }
3689145449b1SDimitry Andric
369086758c71SEd Maste // Target::StopHook
StopHook(lldb::TargetSP target_sp,lldb::user_id_t uid)369114f1b3e8SDimitry Andric Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid)
3692b60736ecSDimitry Andric : UserID(uid), m_target_sp(target_sp), m_specifier_sp(),
36935f29bb8aSDimitry Andric m_thread_spec_up() {}
3694f034231aSEd Maste
StopHook(const StopHook & rhs)369514f1b3e8SDimitry Andric Target::StopHook::StopHook(const StopHook &rhs)
369614f1b3e8SDimitry Andric : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp),
3697b60736ecSDimitry Andric m_specifier_sp(rhs.m_specifier_sp), m_thread_spec_up(),
3698b60736ecSDimitry Andric m_active(rhs.m_active), m_auto_continue(rhs.m_auto_continue) {
36995f29bb8aSDimitry Andric if (rhs.m_thread_spec_up)
3700cfca06d7SDimitry Andric m_thread_spec_up = std::make_unique<ThreadSpec>(*rhs.m_thread_spec_up);
3701f034231aSEd Maste }
3702f034231aSEd Maste
SetSpecifier(SymbolContextSpecifier * specifier)370314f1b3e8SDimitry Andric void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) {
37045e95aa85SEd Maste m_specifier_sp.reset(specifier);
37055e95aa85SEd Maste }
37065e95aa85SEd Maste
SetThreadSpecifier(ThreadSpec * specifier)370714f1b3e8SDimitry Andric void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) {
37085f29bb8aSDimitry Andric m_thread_spec_up.reset(specifier);
3709f034231aSEd Maste }
3710f034231aSEd Maste
ExecutionContextPasses(const ExecutionContext & exc_ctx)3711b60736ecSDimitry Andric bool Target::StopHook::ExecutionContextPasses(const ExecutionContext &exc_ctx) {
3712b60736ecSDimitry Andric SymbolContextSpecifier *specifier = GetSpecifier();
3713b60736ecSDimitry Andric if (!specifier)
3714b60736ecSDimitry Andric return true;
3715b60736ecSDimitry Andric
3716b60736ecSDimitry Andric bool will_run = true;
3717b60736ecSDimitry Andric if (exc_ctx.GetFramePtr())
3718b60736ecSDimitry Andric will_run = GetSpecifier()->SymbolContextMatches(
3719b60736ecSDimitry Andric exc_ctx.GetFramePtr()->GetSymbolContext(eSymbolContextEverything));
3720b60736ecSDimitry Andric if (will_run && GetThreadSpecifier() != nullptr)
3721b60736ecSDimitry Andric will_run =
3722b60736ecSDimitry Andric GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx.GetThreadRef());
3723b60736ecSDimitry Andric
3724b60736ecSDimitry Andric return will_run;
3725b60736ecSDimitry Andric }
3726b60736ecSDimitry Andric
GetDescription(Stream & s,lldb::DescriptionLevel level) const37277fa27ce4SDimitry Andric void Target::StopHook::GetDescription(Stream &s,
372814f1b3e8SDimitry Andric lldb::DescriptionLevel level) const {
3729b60736ecSDimitry Andric
3730b60736ecSDimitry Andric // For brief descriptions, only print the subclass description:
3731b60736ecSDimitry Andric if (level == eDescriptionLevelBrief) {
3732b60736ecSDimitry Andric GetSubclassDescription(s, level);
3733b60736ecSDimitry Andric return;
3734b60736ecSDimitry Andric }
3735b60736ecSDimitry Andric
37367fa27ce4SDimitry Andric unsigned indent_level = s.GetIndentLevel();
3737f034231aSEd Maste
37387fa27ce4SDimitry Andric s.SetIndentLevel(indent_level + 2);
3739f034231aSEd Maste
37407fa27ce4SDimitry Andric s.Printf("Hook: %" PRIu64 "\n", GetID());
3741f034231aSEd Maste if (m_active)
37427fa27ce4SDimitry Andric s.Indent("State: enabled\n");
3743f034231aSEd Maste else
37447fa27ce4SDimitry Andric s.Indent("State: disabled\n");
3745f034231aSEd Maste
37465f29bb8aSDimitry Andric if (m_auto_continue)
37477fa27ce4SDimitry Andric s.Indent("AutoContinue on\n");
37485f29bb8aSDimitry Andric
374914f1b3e8SDimitry Andric if (m_specifier_sp) {
37507fa27ce4SDimitry Andric s.Indent();
37517fa27ce4SDimitry Andric s.PutCString("Specifier:\n");
37527fa27ce4SDimitry Andric s.SetIndentLevel(indent_level + 4);
37537fa27ce4SDimitry Andric m_specifier_sp->GetDescription(&s, level);
37547fa27ce4SDimitry Andric s.SetIndentLevel(indent_level + 2);
3755f034231aSEd Maste }
3756f034231aSEd Maste
37575f29bb8aSDimitry Andric if (m_thread_spec_up) {
3758f034231aSEd Maste StreamString tmp;
37597fa27ce4SDimitry Andric s.Indent("Thread:\n");
37605f29bb8aSDimitry Andric m_thread_spec_up->GetDescription(&tmp, level);
37617fa27ce4SDimitry Andric s.SetIndentLevel(indent_level + 4);
37627fa27ce4SDimitry Andric s.Indent(tmp.GetString());
37637fa27ce4SDimitry Andric s.PutCString("\n");
37647fa27ce4SDimitry Andric s.SetIndentLevel(indent_level + 2);
3765f034231aSEd Maste }
3766b60736ecSDimitry Andric GetSubclassDescription(s, level);
3767b60736ecSDimitry Andric }
3768f034231aSEd Maste
GetSubclassDescription(Stream & s,lldb::DescriptionLevel level) const3769b60736ecSDimitry Andric void Target::StopHookCommandLine::GetSubclassDescription(
37707fa27ce4SDimitry Andric Stream &s, lldb::DescriptionLevel level) const {
3771b60736ecSDimitry Andric // The brief description just prints the first command.
3772b60736ecSDimitry Andric if (level == eDescriptionLevelBrief) {
3773b60736ecSDimitry Andric if (m_commands.GetSize() == 1)
37747fa27ce4SDimitry Andric s.PutCString(m_commands.GetStringAtIndex(0));
3775b60736ecSDimitry Andric return;
3776b60736ecSDimitry Andric }
37777fa27ce4SDimitry Andric s.Indent("Commands: \n");
37787fa27ce4SDimitry Andric s.SetIndentLevel(s.GetIndentLevel() + 4);
3779f034231aSEd Maste uint32_t num_commands = m_commands.GetSize();
378014f1b3e8SDimitry Andric for (uint32_t i = 0; i < num_commands; i++) {
37817fa27ce4SDimitry Andric s.Indent(m_commands.GetStringAtIndex(i));
37827fa27ce4SDimitry Andric s.PutCString("\n");
3783f034231aSEd Maste }
37847fa27ce4SDimitry Andric s.SetIndentLevel(s.GetIndentLevel() - 4);
3785b60736ecSDimitry Andric }
3786b60736ecSDimitry Andric
3787b60736ecSDimitry Andric // Target::StopHookCommandLine
SetActionFromString(const std::string & string)3788b60736ecSDimitry Andric void Target::StopHookCommandLine::SetActionFromString(const std::string &string) {
3789b60736ecSDimitry Andric GetCommands().SplitIntoLines(string);
3790b60736ecSDimitry Andric }
3791b60736ecSDimitry Andric
SetActionFromStrings(const std::vector<std::string> & strings)3792b60736ecSDimitry Andric void Target::StopHookCommandLine::SetActionFromStrings(
3793b60736ecSDimitry Andric const std::vector<std::string> &strings) {
3794b60736ecSDimitry Andric for (auto string : strings)
3795b60736ecSDimitry Andric GetCommands().AppendString(string.c_str());
3796b60736ecSDimitry Andric }
3797b60736ecSDimitry Andric
3798b60736ecSDimitry Andric Target::StopHook::StopHookResult
HandleStop(ExecutionContext & exc_ctx,StreamSP output_sp)3799b60736ecSDimitry Andric Target::StopHookCommandLine::HandleStop(ExecutionContext &exc_ctx,
3800b60736ecSDimitry Andric StreamSP output_sp) {
3801b60736ecSDimitry Andric assert(exc_ctx.GetTargetPtr() && "Can't call PerformAction on a context "
3802b60736ecSDimitry Andric "with no target");
3803b60736ecSDimitry Andric
3804b60736ecSDimitry Andric if (!m_commands.GetSize())
3805b60736ecSDimitry Andric return StopHookResult::KeepStopped;
3806b60736ecSDimitry Andric
3807b60736ecSDimitry Andric CommandReturnObject result(false);
3808b60736ecSDimitry Andric result.SetImmediateOutputStream(output_sp);
3809b60736ecSDimitry Andric result.SetInteractive(false);
3810b60736ecSDimitry Andric Debugger &debugger = exc_ctx.GetTargetPtr()->GetDebugger();
3811b60736ecSDimitry Andric CommandInterpreterRunOptions options;
3812b60736ecSDimitry Andric options.SetStopOnContinue(true);
3813b60736ecSDimitry Andric options.SetStopOnError(true);
3814b60736ecSDimitry Andric options.SetEchoCommands(false);
3815b60736ecSDimitry Andric options.SetPrintResults(true);
3816b60736ecSDimitry Andric options.SetPrintErrors(true);
3817b60736ecSDimitry Andric options.SetAddToHistory(false);
3818b60736ecSDimitry Andric
3819b60736ecSDimitry Andric // Force Async:
3820b60736ecSDimitry Andric bool old_async = debugger.GetAsyncExecution();
3821b60736ecSDimitry Andric debugger.SetAsyncExecution(true);
3822344a3780SDimitry Andric debugger.GetCommandInterpreter().HandleCommands(GetCommands(), exc_ctx,
3823b60736ecSDimitry Andric options, result);
3824b60736ecSDimitry Andric debugger.SetAsyncExecution(old_async);
3825b60736ecSDimitry Andric lldb::ReturnStatus status = result.GetStatus();
3826b60736ecSDimitry Andric if (status == eReturnStatusSuccessContinuingNoResult ||
3827b60736ecSDimitry Andric status == eReturnStatusSuccessContinuingResult)
3828b60736ecSDimitry Andric return StopHookResult::AlreadyContinued;
3829b60736ecSDimitry Andric return StopHookResult::KeepStopped;
3830b60736ecSDimitry Andric }
3831b60736ecSDimitry Andric
3832b60736ecSDimitry Andric // Target::StopHookScripted
SetScriptCallback(std::string class_name,StructuredData::ObjectSP extra_args_sp)3833b60736ecSDimitry Andric Status Target::StopHookScripted::SetScriptCallback(
3834b60736ecSDimitry Andric std::string class_name, StructuredData::ObjectSP extra_args_sp) {
3835b60736ecSDimitry Andric Status error;
3836b60736ecSDimitry Andric
3837b60736ecSDimitry Andric ScriptInterpreter *script_interp =
3838b60736ecSDimitry Andric GetTarget()->GetDebugger().GetScriptInterpreter();
3839b60736ecSDimitry Andric if (!script_interp) {
3840b60736ecSDimitry Andric error.SetErrorString("No script interpreter installed.");
3841b60736ecSDimitry Andric return error;
3842b60736ecSDimitry Andric }
3843b60736ecSDimitry Andric
3844b60736ecSDimitry Andric m_class_name = class_name;
384577fc4c14SDimitry Andric m_extra_args.SetObjectSP(extra_args_sp);
3846b60736ecSDimitry Andric
3847b60736ecSDimitry Andric m_implementation_sp = script_interp->CreateScriptedStopHook(
3848b60736ecSDimitry Andric GetTarget(), m_class_name.c_str(), m_extra_args, error);
3849b60736ecSDimitry Andric
3850b60736ecSDimitry Andric return error;
3851b60736ecSDimitry Andric }
3852b60736ecSDimitry Andric
3853b60736ecSDimitry Andric Target::StopHook::StopHookResult
HandleStop(ExecutionContext & exc_ctx,StreamSP output_sp)3854b60736ecSDimitry Andric Target::StopHookScripted::HandleStop(ExecutionContext &exc_ctx,
3855b60736ecSDimitry Andric StreamSP output_sp) {
3856b60736ecSDimitry Andric assert(exc_ctx.GetTargetPtr() && "Can't call HandleStop on a context "
3857b60736ecSDimitry Andric "with no target");
3858b60736ecSDimitry Andric
3859b60736ecSDimitry Andric ScriptInterpreter *script_interp =
3860b60736ecSDimitry Andric GetTarget()->GetDebugger().GetScriptInterpreter();
3861b60736ecSDimitry Andric if (!script_interp)
3862b60736ecSDimitry Andric return StopHookResult::KeepStopped;
3863b60736ecSDimitry Andric
3864b60736ecSDimitry Andric bool should_stop = script_interp->ScriptedStopHookHandleStop(
3865b60736ecSDimitry Andric m_implementation_sp, exc_ctx, output_sp);
3866b60736ecSDimitry Andric
3867b60736ecSDimitry Andric return should_stop ? StopHookResult::KeepStopped
3868b60736ecSDimitry Andric : StopHookResult::RequestContinue;
3869b60736ecSDimitry Andric }
3870b60736ecSDimitry Andric
GetSubclassDescription(Stream & s,lldb::DescriptionLevel level) const3871b60736ecSDimitry Andric void Target::StopHookScripted::GetSubclassDescription(
38727fa27ce4SDimitry Andric Stream &s, lldb::DescriptionLevel level) const {
3873b60736ecSDimitry Andric if (level == eDescriptionLevelBrief) {
38747fa27ce4SDimitry Andric s.PutCString(m_class_name);
3875b60736ecSDimitry Andric return;
3876b60736ecSDimitry Andric }
38777fa27ce4SDimitry Andric s.Indent("Class:");
38787fa27ce4SDimitry Andric s.Printf("%s\n", m_class_name.c_str());
3879b60736ecSDimitry Andric
3880b60736ecSDimitry Andric // Now print the extra args:
3881b60736ecSDimitry Andric // FIXME: We should use StructuredData.GetDescription on the m_extra_args
3882b60736ecSDimitry Andric // but that seems to rely on some printing plugin that doesn't exist.
388377fc4c14SDimitry Andric if (!m_extra_args.IsValid())
3884b60736ecSDimitry Andric return;
388577fc4c14SDimitry Andric StructuredData::ObjectSP object_sp = m_extra_args.GetObjectSP();
3886b60736ecSDimitry Andric if (!object_sp || !object_sp->IsValid())
3887b60736ecSDimitry Andric return;
3888b60736ecSDimitry Andric
3889b60736ecSDimitry Andric StructuredData::Dictionary *as_dict = object_sp->GetAsDictionary();
3890b60736ecSDimitry Andric if (!as_dict || !as_dict->IsValid())
3891b60736ecSDimitry Andric return;
3892b60736ecSDimitry Andric
3893b60736ecSDimitry Andric uint32_t num_keys = as_dict->GetSize();
3894b60736ecSDimitry Andric if (num_keys == 0)
3895b60736ecSDimitry Andric return;
3896b60736ecSDimitry Andric
38977fa27ce4SDimitry Andric s.Indent("Args:\n");
38987fa27ce4SDimitry Andric s.SetIndentLevel(s.GetIndentLevel() + 4);
3899b60736ecSDimitry Andric
3900b1c73532SDimitry Andric auto print_one_element = [&s](llvm::StringRef key,
3901b60736ecSDimitry Andric StructuredData::Object *object) {
39027fa27ce4SDimitry Andric s.Indent();
3903b1c73532SDimitry Andric s.Format("{0} : {1}\n", key, object->GetStringValue());
3904b60736ecSDimitry Andric return true;
3905b60736ecSDimitry Andric };
3906b60736ecSDimitry Andric
3907b60736ecSDimitry Andric as_dict->ForEach(print_one_element);
3908b60736ecSDimitry Andric
39097fa27ce4SDimitry Andric s.SetIndentLevel(s.GetIndentLevel() - 4);
3910f034231aSEd Maste }
3911f034231aSEd Maste
391294994d37SDimitry Andric static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
3913ead24645SDimitry Andric {
3914ead24645SDimitry Andric eNoDynamicValues,
3915ead24645SDimitry Andric "no-dynamic-values",
3916ead24645SDimitry Andric "Don't calculate the dynamic type of values",
3917ead24645SDimitry Andric },
3918ead24645SDimitry Andric {
3919ead24645SDimitry Andric eDynamicCanRunTarget,
3920ead24645SDimitry Andric "run-target",
3921ead24645SDimitry Andric "Calculate the dynamic type of values "
3922ead24645SDimitry Andric "even if you have to run the target.",
3923ead24645SDimitry Andric },
3924ead24645SDimitry Andric {
3925ead24645SDimitry Andric eDynamicDontRunTarget,
3926ead24645SDimitry Andric "no-run-target",
3927ead24645SDimitry Andric "Calculate the dynamic type of values, but don't run the target.",
3928ead24645SDimitry Andric },
3929ead24645SDimitry Andric };
3930f034231aSEd Maste
GetDynamicValueTypes()393194994d37SDimitry Andric OptionEnumValues lldb_private::GetDynamicValueTypes() {
393294994d37SDimitry Andric return OptionEnumValues(g_dynamic_value_types);
393394994d37SDimitry Andric }
393494994d37SDimitry Andric
393594994d37SDimitry Andric static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = {
3936ead24645SDimitry Andric {
3937ead24645SDimitry Andric eInlineBreakpointsNever,
3938ead24645SDimitry Andric "never",
3939ead24645SDimitry Andric "Never look for inline breakpoint locations (fastest). This setting "
3940ead24645SDimitry Andric "should only be used if you know that no inlining occurs in your"
3941ead24645SDimitry Andric "programs.",
3942ead24645SDimitry Andric },
3943ead24645SDimitry Andric {
3944ead24645SDimitry Andric eInlineBreakpointsHeaders,
3945ead24645SDimitry Andric "headers",
3946ead24645SDimitry Andric "Only check for inline breakpoint locations when setting breakpoints "
3947ead24645SDimitry Andric "in header files, but not when setting breakpoint in implementation "
3948ead24645SDimitry Andric "source files (default).",
3949ead24645SDimitry Andric },
3950ead24645SDimitry Andric {
3951ead24645SDimitry Andric eInlineBreakpointsAlways,
3952ead24645SDimitry Andric "always",
3953ead24645SDimitry Andric "Always look for inline breakpoint locations when setting file and "
3954ead24645SDimitry Andric "line breakpoints (slower but most accurate).",
3955ead24645SDimitry Andric },
3956ead24645SDimitry Andric };
3957f034231aSEd Maste
39585f29bb8aSDimitry Andric enum x86DisassemblyFlavor {
3959f034231aSEd Maste eX86DisFlavorDefault,
3960f034231aSEd Maste eX86DisFlavorIntel,
3961f034231aSEd Maste eX86DisFlavorATT
39625f29bb8aSDimitry Andric };
3963f034231aSEd Maste
396494994d37SDimitry Andric static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = {
3965ead24645SDimitry Andric {
3966ead24645SDimitry Andric eX86DisFlavorDefault,
3967ead24645SDimitry Andric "default",
3968ead24645SDimitry Andric "Disassembler default (currently att).",
3969ead24645SDimitry Andric },
3970ead24645SDimitry Andric {
3971ead24645SDimitry Andric eX86DisFlavorIntel,
3972ead24645SDimitry Andric "intel",
3973ead24645SDimitry Andric "Intel disassembler flavor.",
3974ead24645SDimitry Andric },
3975ead24645SDimitry Andric {
3976ead24645SDimitry Andric eX86DisFlavorATT,
3977ead24645SDimitry Andric "att",
3978ead24645SDimitry Andric "AT&T disassembler flavor.",
3979ead24645SDimitry Andric },
3980ead24645SDimitry Andric };
3981f034231aSEd Maste
3982b60736ecSDimitry Andric static constexpr OptionEnumValueElement g_import_std_module_value_types[] = {
3983b60736ecSDimitry Andric {
3984b60736ecSDimitry Andric eImportStdModuleFalse,
3985b60736ecSDimitry Andric "false",
3986b60736ecSDimitry Andric "Never import the 'std' C++ module in the expression parser.",
3987b60736ecSDimitry Andric },
3988b60736ecSDimitry Andric {
3989b60736ecSDimitry Andric eImportStdModuleFallback,
3990b60736ecSDimitry Andric "fallback",
3991b60736ecSDimitry Andric "Retry evaluating expressions with an imported 'std' C++ module if they"
3992b60736ecSDimitry Andric " failed to parse without the module. This allows evaluating more "
3993b60736ecSDimitry Andric "complex expressions involving C++ standard library types."
3994b60736ecSDimitry Andric },
3995b60736ecSDimitry Andric {
3996b60736ecSDimitry Andric eImportStdModuleTrue,
3997b60736ecSDimitry Andric "true",
3998b60736ecSDimitry Andric "Always import the 'std' C++ module. This allows evaluating more "
3999b60736ecSDimitry Andric "complex expressions involving C++ standard library types. This feature"
4000b60736ecSDimitry Andric " is experimental."
4001b60736ecSDimitry Andric },
4002b60736ecSDimitry Andric };
4003b60736ecSDimitry Andric
4004145449b1SDimitry Andric static constexpr OptionEnumValueElement
4005145449b1SDimitry Andric g_dynamic_class_info_helper_value_types[] = {
4006145449b1SDimitry Andric {
4007145449b1SDimitry Andric eDynamicClassInfoHelperAuto,
4008145449b1SDimitry Andric "auto",
4009145449b1SDimitry Andric "Automatically determine the most appropriate method for the "
4010145449b1SDimitry Andric "target OS.",
4011145449b1SDimitry Andric },
4012145449b1SDimitry Andric {eDynamicClassInfoHelperRealizedClassesStruct, "RealizedClassesStruct",
4013145449b1SDimitry Andric "Prefer using the realized classes struct."},
4014145449b1SDimitry Andric {eDynamicClassInfoHelperCopyRealizedClassList, "CopyRealizedClassList",
4015145449b1SDimitry Andric "Prefer using the CopyRealizedClassList API."},
4016145449b1SDimitry Andric {eDynamicClassInfoHelperGetRealizedClassList, "GetRealizedClassList",
4017145449b1SDimitry Andric "Prefer using the GetRealizedClassList API."},
4018145449b1SDimitry Andric };
4019145449b1SDimitry Andric
402094994d37SDimitry Andric static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = {
4021ead24645SDimitry Andric {
4022ead24645SDimitry Andric Disassembler::eHexStyleC,
4023ead24645SDimitry Andric "c",
4024ead24645SDimitry Andric "C-style (0xffff).",
4025ead24645SDimitry Andric },
4026ead24645SDimitry Andric {
4027ead24645SDimitry Andric Disassembler::eHexStyleAsm,
4028ead24645SDimitry Andric "asm",
4029ead24645SDimitry Andric "Asm-style (0ffffh).",
4030ead24645SDimitry Andric },
4031ead24645SDimitry Andric };
4032f034231aSEd Maste
403394994d37SDimitry Andric static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = {
4034ead24645SDimitry Andric {
4035ead24645SDimitry Andric eLoadScriptFromSymFileTrue,
4036ead24645SDimitry Andric "true",
4037ead24645SDimitry Andric "Load debug scripts inside symbol files",
4038ead24645SDimitry Andric },
4039ead24645SDimitry Andric {
4040ead24645SDimitry Andric eLoadScriptFromSymFileFalse,
4041ead24645SDimitry Andric "false",
4042ead24645SDimitry Andric "Do not load debug scripts inside symbol files.",
4043ead24645SDimitry Andric },
4044ead24645SDimitry Andric {
4045ead24645SDimitry Andric eLoadScriptFromSymFileWarn,
4046ead24645SDimitry Andric "warn",
4047ead24645SDimitry Andric "Warn about debug scripts inside symbol files but do not load them.",
4048ead24645SDimitry Andric },
4049ead24645SDimitry Andric };
4050f034231aSEd Maste
4051ead24645SDimitry Andric static constexpr OptionEnumValueElement g_load_cwd_lldbinit_values[] = {
4052ead24645SDimitry Andric {
4053ead24645SDimitry Andric eLoadCWDlldbinitTrue,
4054ead24645SDimitry Andric "true",
4055ead24645SDimitry Andric "Load .lldbinit files from current directory",
4056ead24645SDimitry Andric },
4057ead24645SDimitry Andric {
4058ead24645SDimitry Andric eLoadCWDlldbinitFalse,
4059ead24645SDimitry Andric "false",
4060ead24645SDimitry Andric "Do not load .lldbinit files from current directory",
4061ead24645SDimitry Andric },
4062ead24645SDimitry Andric {
4063ead24645SDimitry Andric eLoadCWDlldbinitWarn,
4064ead24645SDimitry Andric "warn",
4065ead24645SDimitry Andric "Warn about loading .lldbinit files from current directory",
4066ead24645SDimitry Andric },
4067ead24645SDimitry Andric };
4068f3fbd1c0SDimitry Andric
406994994d37SDimitry Andric static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = {
4070ead24645SDimitry Andric {
4071ead24645SDimitry Andric eMemoryModuleLoadLevelMinimal,
4072ead24645SDimitry Andric "minimal",
407314f1b3e8SDimitry Andric "Load minimal information when loading modules from memory. Currently "
4074ead24645SDimitry Andric "this setting loads sections only.",
4075ead24645SDimitry Andric },
4076ead24645SDimitry Andric {
4077ead24645SDimitry Andric eMemoryModuleLoadLevelPartial,
4078ead24645SDimitry Andric "partial",
407914f1b3e8SDimitry Andric "Load partial information when loading modules from memory. Currently "
4080ead24645SDimitry Andric "this setting loads sections and function bounds.",
4081ead24645SDimitry Andric },
4082ead24645SDimitry Andric {
4083ead24645SDimitry Andric eMemoryModuleLoadLevelComplete,
4084ead24645SDimitry Andric "complete",
408514f1b3e8SDimitry Andric "Load complete information when loading modules from memory. Currently "
4086ead24645SDimitry Andric "this setting loads sections and all symbols.",
4087ead24645SDimitry Andric },
4088ead24645SDimitry Andric };
4089f034231aSEd Maste
4090ead24645SDimitry Andric #define LLDB_PROPERTIES_target
4091ead24645SDimitry Andric #include "TargetProperties.inc"
40925e95aa85SEd Maste
409314f1b3e8SDimitry Andric enum {
4094ead24645SDimitry Andric #define LLDB_PROPERTIES_target
4095ead24645SDimitry Andric #include "TargetPropertiesEnum.inc"
409694994d37SDimitry Andric ePropertyExperimental,
4097f034231aSEd Maste };
4098f034231aSEd Maste
4099344a3780SDimitry Andric class TargetOptionValueProperties
4100344a3780SDimitry Andric : public Cloneable<TargetOptionValueProperties, OptionValueProperties> {
4101f034231aSEd Maste public:
TargetOptionValueProperties(llvm::StringRef name)4102b1c73532SDimitry Andric TargetOptionValueProperties(llvm::StringRef name) : Cloneable(name) {}
4103f034231aSEd Maste
41047fa27ce4SDimitry Andric const Property *
GetPropertyAtIndex(size_t idx,const ExecutionContext * exe_ctx=nullptr) const41057fa27ce4SDimitry Andric GetPropertyAtIndex(size_t idx,
41067fa27ce4SDimitry Andric const ExecutionContext *exe_ctx = nullptr) const override {
41070cac4ca3SEd Maste // When getting the value for a key from the target options, we will always
4108f73363f1SDimitry Andric // try and grab the setting from the current target if there is one. Else
4109f73363f1SDimitry Andric // we just use the one from this instance.
411014f1b3e8SDimitry Andric if (exe_ctx) {
4111f034231aSEd Maste Target *target = exe_ctx->GetTargetPtr();
411214f1b3e8SDimitry Andric if (target) {
411314f1b3e8SDimitry Andric TargetOptionValueProperties *target_properties =
411414f1b3e8SDimitry Andric static_cast<TargetOptionValueProperties *>(
411514f1b3e8SDimitry Andric target->GetValueProperties().get());
4116f034231aSEd Maste if (this != target_properties)
4117f034231aSEd Maste return target_properties->ProtectedGetPropertyAtIndex(idx);
4118f034231aSEd Maste }
4119f034231aSEd Maste }
4120f034231aSEd Maste return ProtectedGetPropertyAtIndex(idx);
4121f034231aSEd Maste }
4122f034231aSEd Maste };
4123f034231aSEd Maste
412486758c71SEd Maste // TargetProperties
4125cfca06d7SDimitry Andric #define LLDB_PROPERTIES_target_experimental
4126ead24645SDimitry Andric #include "TargetProperties.inc"
4127f3fbd1c0SDimitry Andric
4128ead24645SDimitry Andric enum {
4129cfca06d7SDimitry Andric #define LLDB_PROPERTIES_target_experimental
4130ead24645SDimitry Andric #include "TargetPropertiesEnum.inc"
4131ead24645SDimitry Andric };
4132f3fbd1c0SDimitry Andric
4133344a3780SDimitry Andric class TargetExperimentalOptionValueProperties
4134344a3780SDimitry Andric : public Cloneable<TargetExperimentalOptionValueProperties,
4135344a3780SDimitry Andric OptionValueProperties> {
4136f3fbd1c0SDimitry Andric public:
TargetExperimentalOptionValueProperties()413714f1b3e8SDimitry Andric TargetExperimentalOptionValueProperties()
4138b1c73532SDimitry Andric : Cloneable(Properties::GetExperimentalSettingsName()) {}
4139f3fbd1c0SDimitry Andric };
4140f3fbd1c0SDimitry Andric
TargetExperimentalProperties()414114f1b3e8SDimitry Andric TargetExperimentalProperties::TargetExperimentalProperties()
414214f1b3e8SDimitry Andric : Properties(OptionValuePropertiesSP(
414314f1b3e8SDimitry Andric new TargetExperimentalOptionValueProperties())) {
4144cfca06d7SDimitry Andric m_collection_sp->Initialize(g_target_experimental_properties);
4145f3fbd1c0SDimitry Andric }
4146f3fbd1c0SDimitry Andric
4147f3fbd1c0SDimitry Andric // TargetProperties
TargetProperties(Target * target)414814f1b3e8SDimitry Andric TargetProperties::TargetProperties(Target *target)
4149cfca06d7SDimitry Andric : Properties(), m_launch_info(), m_target(target) {
415014f1b3e8SDimitry Andric if (target) {
4151344a3780SDimitry Andric m_collection_sp =
4152c0981da4SDimitry Andric OptionValueProperties::CreateLocalCopy(Target::GetGlobalProperties());
41535e95aa85SEd Maste
4154f73363f1SDimitry Andric // Set callbacks to update launch_info whenever "settins set" updated any
4155f73363f1SDimitry Andric // of these properties
415614f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4157706b4fc4SDimitry Andric ePropertyArg0, [this] { Arg0ValueChangedCallback(); });
415814f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4159706b4fc4SDimitry Andric ePropertyRunArgs, [this] { RunArgsValueChangedCallback(); });
416014f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4161706b4fc4SDimitry Andric ePropertyEnvVars, [this] { EnvVarsValueChangedCallback(); });
416214f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4163cfca06d7SDimitry Andric ePropertyUnsetEnvVars, [this] { EnvVarsValueChangedCallback(); });
4164cfca06d7SDimitry Andric m_collection_sp->SetValueChangedCallback(
4165cfca06d7SDimitry Andric ePropertyInheritEnv, [this] { EnvVarsValueChangedCallback(); });
4166cfca06d7SDimitry Andric m_collection_sp->SetValueChangedCallback(
4167706b4fc4SDimitry Andric ePropertyInputPath, [this] { InputPathValueChangedCallback(); });
416814f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4169706b4fc4SDimitry Andric ePropertyOutputPath, [this] { OutputPathValueChangedCallback(); });
417014f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4171706b4fc4SDimitry Andric ePropertyErrorPath, [this] { ErrorPathValueChangedCallback(); });
4172706b4fc4SDimitry Andric m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, [this] {
4173706b4fc4SDimitry Andric DetachOnErrorValueChangedCallback();
4174706b4fc4SDimitry Andric });
417514f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4176706b4fc4SDimitry Andric ePropertyDisableASLR, [this] { DisableASLRValueChangedCallback(); });
417714f1b3e8SDimitry Andric m_collection_sp->SetValueChangedCallback(
4178b60736ecSDimitry Andric ePropertyInheritTCC, [this] { InheritTCCValueChangedCallback(); });
4179b60736ecSDimitry Andric m_collection_sp->SetValueChangedCallback(
4180706b4fc4SDimitry Andric ePropertyDisableSTDIO, [this] { DisableSTDIOValueChangedCallback(); });
41815e95aa85SEd Maste
4182145449b1SDimitry Andric m_collection_sp->SetValueChangedCallback(
4183145449b1SDimitry Andric ePropertySaveObjectsDir, [this] { CheckJITObjectsDir(); });
4184cfca06d7SDimitry Andric m_experimental_properties_up =
4185cfca06d7SDimitry Andric std::make_unique<TargetExperimentalProperties>();
418614f1b3e8SDimitry Andric m_collection_sp->AppendProperty(
41877fa27ce4SDimitry Andric Properties::GetExperimentalSettingsName(),
41887fa27ce4SDimitry Andric "Experimental settings - setting these won't produce "
41897fa27ce4SDimitry Andric "errors if the setting is not present.",
419014f1b3e8SDimitry Andric true, m_experimental_properties_up->GetValueProperties());
419114f1b3e8SDimitry Andric } else {
4192b1c73532SDimitry Andric m_collection_sp = std::make_shared<TargetOptionValueProperties>("target");
4193ead24645SDimitry Andric m_collection_sp->Initialize(g_target_properties);
4194cfca06d7SDimitry Andric m_experimental_properties_up =
4195cfca06d7SDimitry Andric std::make_unique<TargetExperimentalProperties>();
419614f1b3e8SDimitry Andric m_collection_sp->AppendProperty(
41977fa27ce4SDimitry Andric Properties::GetExperimentalSettingsName(),
41987fa27ce4SDimitry Andric "Experimental settings - setting these won't produce "
41997fa27ce4SDimitry Andric "errors if the setting is not present.",
420014f1b3e8SDimitry Andric true, m_experimental_properties_up->GetValueProperties());
420114f1b3e8SDimitry Andric m_collection_sp->AppendProperty(
42027fa27ce4SDimitry Andric "process", "Settings specific to processes.", true,
42037fa27ce4SDimitry Andric Process::GetGlobalProperties().GetValueProperties());
4204145449b1SDimitry Andric m_collection_sp->SetValueChangedCallback(
4205145449b1SDimitry Andric ePropertySaveObjectsDir, [this] { CheckJITObjectsDir(); });
4206f034231aSEd Maste }
4207f034231aSEd Maste }
4208f034231aSEd Maste
4209e81d9d49SDimitry Andric TargetProperties::~TargetProperties() = default;
4210e81d9d49SDimitry Andric
UpdateLaunchInfoFromProperties()4211cfca06d7SDimitry Andric void TargetProperties::UpdateLaunchInfoFromProperties() {
4212cfca06d7SDimitry Andric Arg0ValueChangedCallback();
4213cfca06d7SDimitry Andric RunArgsValueChangedCallback();
4214cfca06d7SDimitry Andric EnvVarsValueChangedCallback();
4215cfca06d7SDimitry Andric InputPathValueChangedCallback();
4216cfca06d7SDimitry Andric OutputPathValueChangedCallback();
4217cfca06d7SDimitry Andric ErrorPathValueChangedCallback();
4218cfca06d7SDimitry Andric DetachOnErrorValueChangedCallback();
4219cfca06d7SDimitry Andric DisableASLRValueChangedCallback();
4220b60736ecSDimitry Andric InheritTCCValueChangedCallback();
4221cfca06d7SDimitry Andric DisableSTDIOValueChangedCallback();
4222cfca06d7SDimitry Andric }
4223cfca06d7SDimitry Andric
GetExperimentalPropertyValue(size_t prop_idx,ExecutionContext * exe_ctx) const4224ac9a064cSDimitry Andric std::optional<bool> TargetProperties::GetExperimentalPropertyValue(
4225ac9a064cSDimitry Andric size_t prop_idx, ExecutionContext *exe_ctx) const {
42267fa27ce4SDimitry Andric const Property *exp_property =
42277fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndex(ePropertyExperimental, exe_ctx);
422814f1b3e8SDimitry Andric OptionValueProperties *exp_values =
422914f1b3e8SDimitry Andric exp_property->GetValue()->GetAsProperties();
4230f3fbd1c0SDimitry Andric if (exp_values)
4231ac9a064cSDimitry Andric return exp_values->GetPropertyAtIndexAs<bool>(prop_idx, exe_ctx);
4232ac9a064cSDimitry Andric return std::nullopt;
4233f3fbd1c0SDimitry Andric }
4234f3fbd1c0SDimitry Andric
GetInjectLocalVariables(ExecutionContext * exe_ctx) const4235ac9a064cSDimitry Andric bool TargetProperties::GetInjectLocalVariables(
4236ac9a064cSDimitry Andric ExecutionContext *exe_ctx) const {
4237ac9a064cSDimitry Andric return GetExperimentalPropertyValue(ePropertyInjectLocalVars, exe_ctx)
4238ac9a064cSDimitry Andric .value_or(true);
4239f3fbd1c0SDimitry Andric }
4240f3fbd1c0SDimitry Andric
GetDefaultArchitecture() const424114f1b3e8SDimitry Andric ArchSpec TargetProperties::GetDefaultArchitecture() const {
42427fa27ce4SDimitry Andric const uint32_t idx = ePropertyDefaultArch;
42437fa27ce4SDimitry Andric return GetPropertyAtIndexAs<ArchSpec>(idx, {});
4244f034231aSEd Maste }
4245f034231aSEd Maste
SetDefaultArchitecture(const ArchSpec & arch)424614f1b3e8SDimitry Andric void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) {
42477fa27ce4SDimitry Andric const uint32_t idx = ePropertyDefaultArch;
42487fa27ce4SDimitry Andric SetPropertyAtIndex(idx, arch);
4249f034231aSEd Maste }
4250f034231aSEd Maste
GetMoveToNearestCode() const425114f1b3e8SDimitry Andric bool TargetProperties::GetMoveToNearestCode() const {
42525e95aa85SEd Maste const uint32_t idx = ePropertyMoveToNearestCode;
42537fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
42547fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
42555e95aa85SEd Maste }
42565e95aa85SEd Maste
GetPreferDynamicValue() const425714f1b3e8SDimitry Andric lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const {
4258f034231aSEd Maste const uint32_t idx = ePropertyPreferDynamic;
42597fa27ce4SDimitry Andric return GetPropertyAtIndexAs<lldb::DynamicValueType>(
42607fa27ce4SDimitry Andric idx, static_cast<lldb::DynamicValueType>(
42617fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4262f034231aSEd Maste }
4263f034231aSEd Maste
SetPreferDynamicValue(lldb::DynamicValueType d)426414f1b3e8SDimitry Andric bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
42655e95aa85SEd Maste const uint32_t idx = ePropertyPreferDynamic;
42667fa27ce4SDimitry Andric return SetPropertyAtIndex(idx, d);
42675e95aa85SEd Maste }
42685e95aa85SEd Maste
GetPreloadSymbols() const4269773dd0e6SDimitry Andric bool TargetProperties::GetPreloadSymbols() const {
42707fa27ce4SDimitry Andric if (INTERRUPT_REQUESTED(m_target->GetDebugger(),
42717fa27ce4SDimitry Andric "Interrupted checking preload symbols")) {
42727fa27ce4SDimitry Andric return false;
42737fa27ce4SDimitry Andric }
4274773dd0e6SDimitry Andric const uint32_t idx = ePropertyPreloadSymbols;
42757fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
42767fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4277773dd0e6SDimitry Andric }
4278773dd0e6SDimitry Andric
SetPreloadSymbols(bool b)4279773dd0e6SDimitry Andric void TargetProperties::SetPreloadSymbols(bool b) {
4280773dd0e6SDimitry Andric const uint32_t idx = ePropertyPreloadSymbols;
42817fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
4282773dd0e6SDimitry Andric }
4283773dd0e6SDimitry Andric
GetDisableASLR() const428414f1b3e8SDimitry Andric bool TargetProperties::GetDisableASLR() const {
4285f034231aSEd Maste const uint32_t idx = ePropertyDisableASLR;
42867fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
42877fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4288f034231aSEd Maste }
4289f034231aSEd Maste
SetDisableASLR(bool b)429014f1b3e8SDimitry Andric void TargetProperties::SetDisableASLR(bool b) {
4291f034231aSEd Maste const uint32_t idx = ePropertyDisableASLR;
42927fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
4293f034231aSEd Maste }
4294f034231aSEd Maste
GetInheritTCC() const4295b60736ecSDimitry Andric bool TargetProperties::GetInheritTCC() const {
4296b60736ecSDimitry Andric const uint32_t idx = ePropertyInheritTCC;
42977fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
42987fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4299b60736ecSDimitry Andric }
4300b60736ecSDimitry Andric
SetInheritTCC(bool b)4301b60736ecSDimitry Andric void TargetProperties::SetInheritTCC(bool b) {
4302b60736ecSDimitry Andric const uint32_t idx = ePropertyInheritTCC;
43037fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
4304b60736ecSDimitry Andric }
4305b60736ecSDimitry Andric
GetDetachOnError() const430614f1b3e8SDimitry Andric bool TargetProperties::GetDetachOnError() const {
43070cac4ca3SEd Maste const uint32_t idx = ePropertyDetachOnError;
43087fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
43097fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
43100cac4ca3SEd Maste }
43110cac4ca3SEd Maste
SetDetachOnError(bool b)431214f1b3e8SDimitry Andric void TargetProperties::SetDetachOnError(bool b) {
43130cac4ca3SEd Maste const uint32_t idx = ePropertyDetachOnError;
43147fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
43150cac4ca3SEd Maste }
43160cac4ca3SEd Maste
GetDisableSTDIO() const431714f1b3e8SDimitry Andric bool TargetProperties::GetDisableSTDIO() const {
4318f034231aSEd Maste const uint32_t idx = ePropertyDisableSTDIO;
43197fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
43207fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4321f034231aSEd Maste }
4322f034231aSEd Maste
SetDisableSTDIO(bool b)432314f1b3e8SDimitry Andric void TargetProperties::SetDisableSTDIO(bool b) {
4324f034231aSEd Maste const uint32_t idx = ePropertyDisableSTDIO;
43257fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
4326f034231aSEd Maste }
4327f034231aSEd Maste
GetDisassemblyFlavor() const432814f1b3e8SDimitry Andric const char *TargetProperties::GetDisassemblyFlavor() const {
4329f034231aSEd Maste const uint32_t idx = ePropertyDisassemblyFlavor;
4330f034231aSEd Maste const char *return_value;
4331f034231aSEd Maste
433214f1b3e8SDimitry Andric x86DisassemblyFlavor flavor_value =
43337fa27ce4SDimitry Andric GetPropertyAtIndexAs<x86DisassemblyFlavor>(
43347fa27ce4SDimitry Andric idx, static_cast<x86DisassemblyFlavor>(
43357fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
43367fa27ce4SDimitry Andric
4337f034231aSEd Maste return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
4338f034231aSEd Maste return return_value;
4339f034231aSEd Maste }
4340f034231aSEd Maste
GetInlineStrategy() const434114f1b3e8SDimitry Andric InlineStrategy TargetProperties::GetInlineStrategy() const {
4342f034231aSEd Maste const uint32_t idx = ePropertyInlineStrategy;
43437fa27ce4SDimitry Andric return GetPropertyAtIndexAs<InlineStrategy>(
43447fa27ce4SDimitry Andric idx,
43457fa27ce4SDimitry Andric static_cast<InlineStrategy>(g_target_properties[idx].default_uint_value));
4346f034231aSEd Maste }
4347f034231aSEd Maste
GetArg0() const434814f1b3e8SDimitry Andric llvm::StringRef TargetProperties::GetArg0() const {
4349f034231aSEd Maste const uint32_t idx = ePropertyArg0;
43507fa27ce4SDimitry Andric return GetPropertyAtIndexAs<llvm::StringRef>(
43517fa27ce4SDimitry Andric idx, g_target_properties[idx].default_cstr_value);
4352f034231aSEd Maste }
4353f034231aSEd Maste
SetArg0(llvm::StringRef arg)435414f1b3e8SDimitry Andric void TargetProperties::SetArg0(llvm::StringRef arg) {
4355f034231aSEd Maste const uint32_t idx = ePropertyArg0;
43567fa27ce4SDimitry Andric SetPropertyAtIndex(idx, arg);
43575e95aa85SEd Maste m_launch_info.SetArg0(arg);
4358f034231aSEd Maste }
4359f034231aSEd Maste
GetRunArguments(Args & args) const436014f1b3e8SDimitry Andric bool TargetProperties::GetRunArguments(Args &args) const {
4361f034231aSEd Maste const uint32_t idx = ePropertyRunArgs;
43627fa27ce4SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args);
4363f034231aSEd Maste }
4364f034231aSEd Maste
SetRunArguments(const Args & args)436514f1b3e8SDimitry Andric void TargetProperties::SetRunArguments(const Args &args) {
4366f034231aSEd Maste const uint32_t idx = ePropertyRunArgs;
43677fa27ce4SDimitry Andric m_collection_sp->SetPropertyAtIndexFromArgs(idx, args);
43685e95aa85SEd Maste m_launch_info.GetArguments() = args;
4369f034231aSEd Maste }
4370f034231aSEd Maste
ComputeEnvironment() const4371cfca06d7SDimitry Andric Environment TargetProperties::ComputeEnvironment() const {
4372cfca06d7SDimitry Andric Environment env;
4373cfca06d7SDimitry Andric
4374cfca06d7SDimitry Andric if (m_target &&
43757fa27ce4SDimitry Andric GetPropertyAtIndexAs<bool>(
43767fa27ce4SDimitry Andric ePropertyInheritEnv,
4377cfca06d7SDimitry Andric g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) {
4378cfca06d7SDimitry Andric if (auto platform_sp = m_target->GetPlatform()) {
4379cfca06d7SDimitry Andric Environment platform_env = platform_sp->GetEnvironment();
4380cfca06d7SDimitry Andric for (const auto &KV : platform_env)
4381cfca06d7SDimitry Andric env[KV.first()] = KV.second;
4382cfca06d7SDimitry Andric }
4383cfca06d7SDimitry Andric }
4384cfca06d7SDimitry Andric
4385cfca06d7SDimitry Andric Args property_unset_env;
43867fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars,
4387cfca06d7SDimitry Andric property_unset_env);
4388cfca06d7SDimitry Andric for (const auto &var : property_unset_env)
4389cfca06d7SDimitry Andric env.erase(var.ref());
4390cfca06d7SDimitry Andric
4391cfca06d7SDimitry Andric Args property_env;
43927fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars, property_env);
4393cfca06d7SDimitry Andric for (const auto &KV : Environment(property_env))
4394cfca06d7SDimitry Andric env[KV.first()] = KV.second;
4395cfca06d7SDimitry Andric
4396cfca06d7SDimitry Andric return env;
4397cfca06d7SDimitry Andric }
4398cfca06d7SDimitry Andric
GetEnvironment() const4399f73363f1SDimitry Andric Environment TargetProperties::GetEnvironment() const {
4400cfca06d7SDimitry Andric return ComputeEnvironment();
4401f034231aSEd Maste }
4402f034231aSEd Maste
GetInheritedEnvironment() const4403c0981da4SDimitry Andric Environment TargetProperties::GetInheritedEnvironment() const {
4404c0981da4SDimitry Andric Environment environment;
4405c0981da4SDimitry Andric
4406c0981da4SDimitry Andric if (m_target == nullptr)
4407c0981da4SDimitry Andric return environment;
4408c0981da4SDimitry Andric
44097fa27ce4SDimitry Andric if (!GetPropertyAtIndexAs<bool>(
44107fa27ce4SDimitry Andric ePropertyInheritEnv,
4411c0981da4SDimitry Andric g_target_properties[ePropertyInheritEnv].default_uint_value != 0))
4412c0981da4SDimitry Andric return environment;
4413c0981da4SDimitry Andric
4414c0981da4SDimitry Andric PlatformSP platform_sp = m_target->GetPlatform();
4415c0981da4SDimitry Andric if (platform_sp == nullptr)
4416c0981da4SDimitry Andric return environment;
4417c0981da4SDimitry Andric
4418c0981da4SDimitry Andric Environment platform_environment = platform_sp->GetEnvironment();
4419c0981da4SDimitry Andric for (const auto &KV : platform_environment)
4420c0981da4SDimitry Andric environment[KV.first()] = KV.second;
4421c0981da4SDimitry Andric
4422c0981da4SDimitry Andric Args property_unset_environment;
44237fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars,
4424c0981da4SDimitry Andric property_unset_environment);
4425c0981da4SDimitry Andric for (const auto &var : property_unset_environment)
4426c0981da4SDimitry Andric environment.erase(var.ref());
4427c0981da4SDimitry Andric
4428c0981da4SDimitry Andric return environment;
4429c0981da4SDimitry Andric }
4430c0981da4SDimitry Andric
GetTargetEnvironment() const4431c0981da4SDimitry Andric Environment TargetProperties::GetTargetEnvironment() const {
4432c0981da4SDimitry Andric Args property_environment;
44337fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars,
4434c0981da4SDimitry Andric property_environment);
4435c0981da4SDimitry Andric Environment environment;
4436c0981da4SDimitry Andric for (const auto &KV : Environment(property_environment))
4437c0981da4SDimitry Andric environment[KV.first()] = KV.second;
4438c0981da4SDimitry Andric
4439c0981da4SDimitry Andric return environment;
4440c0981da4SDimitry Andric }
4441c0981da4SDimitry Andric
SetEnvironment(Environment env)4442f73363f1SDimitry Andric void TargetProperties::SetEnvironment(Environment env) {
4443f73363f1SDimitry Andric // TODO: Get rid of the Args intermediate step
44445e95aa85SEd Maste const uint32_t idx = ePropertyEnvVars;
44457fa27ce4SDimitry Andric m_collection_sp->SetPropertyAtIndexFromArgs(idx, Args(env));
44465e95aa85SEd Maste }
44475e95aa85SEd Maste
GetSkipPrologue() const444814f1b3e8SDimitry Andric bool TargetProperties::GetSkipPrologue() const {
4449f034231aSEd Maste const uint32_t idx = ePropertySkipPrologue;
44507fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
44517fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4452f034231aSEd Maste }
4453f034231aSEd Maste
GetSourcePathMap() const445414f1b3e8SDimitry Andric PathMappingList &TargetProperties::GetSourcePathMap() const {
4455f034231aSEd Maste const uint32_t idx = ePropertySourceMap;
445614f1b3e8SDimitry Andric OptionValuePathMappings *option_value =
44577fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(idx);
4458f034231aSEd Maste assert(option_value);
4459f034231aSEd Maste return option_value->GetCurrentValue();
4460f034231aSEd Maste }
4461f034231aSEd Maste
GetAutoSourceMapRelative() const4462e3b55780SDimitry Andric bool TargetProperties::GetAutoSourceMapRelative() const {
4463e3b55780SDimitry Andric const uint32_t idx = ePropertyAutoSourceMapRelative;
44647fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
44657fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4466e3b55780SDimitry Andric }
4467e3b55780SDimitry Andric
AppendExecutableSearchPaths(const FileSpec & dir)44685f29bb8aSDimitry Andric void TargetProperties::AppendExecutableSearchPaths(const FileSpec &dir) {
4469f034231aSEd Maste const uint32_t idx = ePropertyExecutableSearchPaths;
447014f1b3e8SDimitry Andric OptionValueFileSpecList *option_value =
44717fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(idx);
4472f034231aSEd Maste assert(option_value);
44735f29bb8aSDimitry Andric option_value->AppendCurrentValue(dir);
4474f034231aSEd Maste }
4475f034231aSEd Maste
GetExecutableSearchPaths()44765f29bb8aSDimitry Andric FileSpecList TargetProperties::GetExecutableSearchPaths() {
44775f29bb8aSDimitry Andric const uint32_t idx = ePropertyExecutableSearchPaths;
44787fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpecList>(idx, {});
4479f034231aSEd Maste }
4480f034231aSEd Maste
GetDebugFileSearchPaths()44815f29bb8aSDimitry Andric FileSpecList TargetProperties::GetDebugFileSearchPaths() {
44825f29bb8aSDimitry Andric const uint32_t idx = ePropertyDebugFileSearchPaths;
44837fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpecList>(idx, {});
44845f29bb8aSDimitry Andric }
44855f29bb8aSDimitry Andric
GetClangModuleSearchPaths()44865f29bb8aSDimitry Andric FileSpecList TargetProperties::GetClangModuleSearchPaths() {
44875e95aa85SEd Maste const uint32_t idx = ePropertyClangModuleSearchPaths;
44887fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpecList>(idx, {});
44895e95aa85SEd Maste }
44905e95aa85SEd Maste
GetEnableAutoImportClangModules() const449114f1b3e8SDimitry Andric bool TargetProperties::GetEnableAutoImportClangModules() const {
44925e95aa85SEd Maste const uint32_t idx = ePropertyAutoImportClangModules;
44937fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
44947fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
44955e95aa85SEd Maste }
44965e95aa85SEd Maste
GetImportStdModule() const4497b60736ecSDimitry Andric ImportStdModule TargetProperties::GetImportStdModule() const {
44985f29bb8aSDimitry Andric const uint32_t idx = ePropertyImportStdModule;
44997fa27ce4SDimitry Andric return GetPropertyAtIndexAs<ImportStdModule>(
45007fa27ce4SDimitry Andric idx, static_cast<ImportStdModule>(
45017fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
45025f29bb8aSDimitry Andric }
45035f29bb8aSDimitry Andric
GetDynamicClassInfoHelper() const4504145449b1SDimitry Andric DynamicClassInfoHelper TargetProperties::GetDynamicClassInfoHelper() const {
4505145449b1SDimitry Andric const uint32_t idx = ePropertyDynamicClassInfoHelper;
45067fa27ce4SDimitry Andric return GetPropertyAtIndexAs<DynamicClassInfoHelper>(
45077fa27ce4SDimitry Andric idx, static_cast<DynamicClassInfoHelper>(
45087fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4509145449b1SDimitry Andric }
4510145449b1SDimitry Andric
GetEnableAutoApplyFixIts() const451114f1b3e8SDimitry Andric bool TargetProperties::GetEnableAutoApplyFixIts() const {
4512f3fbd1c0SDimitry Andric const uint32_t idx = ePropertyAutoApplyFixIts;
45137fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
45147fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4515f3fbd1c0SDimitry Andric }
4516f3fbd1c0SDimitry Andric
GetNumberOfRetriesWithFixits() const4517cfca06d7SDimitry Andric uint64_t TargetProperties::GetNumberOfRetriesWithFixits() const {
4518cfca06d7SDimitry Andric const uint32_t idx = ePropertyRetriesWithFixIts;
45197fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
45207fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4521cfca06d7SDimitry Andric }
4522cfca06d7SDimitry Andric
GetEnableNotifyAboutFixIts() const452314f1b3e8SDimitry Andric bool TargetProperties::GetEnableNotifyAboutFixIts() const {
4524f3fbd1c0SDimitry Andric const uint32_t idx = ePropertyNotifyAboutFixIts;
45257fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
45267fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4527f3fbd1c0SDimitry Andric }
4528f3fbd1c0SDimitry Andric
GetSaveJITObjectsDir() const4529145449b1SDimitry Andric FileSpec TargetProperties::GetSaveJITObjectsDir() const {
4530145449b1SDimitry Andric const uint32_t idx = ePropertySaveObjectsDir;
45317fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpec>(idx, {});
4532145449b1SDimitry Andric }
4533145449b1SDimitry Andric
CheckJITObjectsDir()4534145449b1SDimitry Andric void TargetProperties::CheckJITObjectsDir() {
4535145449b1SDimitry Andric FileSpec new_dir = GetSaveJITObjectsDir();
4536145449b1SDimitry Andric if (!new_dir)
4537145449b1SDimitry Andric return;
4538145449b1SDimitry Andric
4539145449b1SDimitry Andric const FileSystem &instance = FileSystem::Instance();
4540145449b1SDimitry Andric bool exists = instance.Exists(new_dir);
4541145449b1SDimitry Andric bool is_directory = instance.IsDirectory(new_dir);
4542145449b1SDimitry Andric std::string path = new_dir.GetPath(true);
4543145449b1SDimitry Andric bool writable = llvm::sys::fs::can_write(path);
4544145449b1SDimitry Andric if (exists && is_directory && writable)
4545145449b1SDimitry Andric return;
4546145449b1SDimitry Andric
45477fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndex(ePropertySaveObjectsDir)
4548145449b1SDimitry Andric ->GetValue()
4549145449b1SDimitry Andric ->Clear();
4550145449b1SDimitry Andric
4551145449b1SDimitry Andric std::string buffer;
4552145449b1SDimitry Andric llvm::raw_string_ostream os(buffer);
4553145449b1SDimitry Andric os << "JIT object dir '" << path << "' ";
4554145449b1SDimitry Andric if (!exists)
4555145449b1SDimitry Andric os << "does not exist";
4556145449b1SDimitry Andric else if (!is_directory)
4557145449b1SDimitry Andric os << "is not a directory";
4558145449b1SDimitry Andric else if (!writable)
4559145449b1SDimitry Andric os << "is not writable";
4560145449b1SDimitry Andric
4561e3b55780SDimitry Andric std::optional<lldb::user_id_t> debugger_id;
4562145449b1SDimitry Andric if (m_target)
4563145449b1SDimitry Andric debugger_id = m_target->GetDebugger().GetID();
4564145449b1SDimitry Andric Debugger::ReportError(os.str(), debugger_id);
456514f1b3e8SDimitry Andric }
456614f1b3e8SDimitry Andric
GetEnableSyntheticValue() const456714f1b3e8SDimitry Andric bool TargetProperties::GetEnableSyntheticValue() const {
4568f034231aSEd Maste const uint32_t idx = ePropertyEnableSynthetic;
45697fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
45707fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4571ead24645SDimitry Andric }
4572ead24645SDimitry Andric
ShowHexVariableValuesWithLeadingZeroes() const4573b1c73532SDimitry Andric bool TargetProperties::ShowHexVariableValuesWithLeadingZeroes() const {
4574b1c73532SDimitry Andric const uint32_t idx = ePropertyShowHexVariableValuesWithLeadingZeroes;
4575b1c73532SDimitry Andric return GetPropertyAtIndexAs<bool>(
4576b1c73532SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4577b1c73532SDimitry Andric }
4578b1c73532SDimitry Andric
GetMaxZeroPaddingInFloatFormat() const4579ead24645SDimitry Andric uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const {
4580ead24645SDimitry Andric const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat;
45817fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
45827fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4583f034231aSEd Maste }
4584f034231aSEd Maste
GetMaximumNumberOfChildrenToDisplay() const458514f1b3e8SDimitry Andric uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const {
4586f034231aSEd Maste const uint32_t idx = ePropertyMaxChildrenCount;
45877fa27ce4SDimitry Andric return GetPropertyAtIndexAs<int64_t>(
45887fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4589f034231aSEd Maste }
4590f034231aSEd Maste
4591145449b1SDimitry Andric std::pair<uint32_t, bool>
GetMaximumDepthOfChildrenToDisplay() const4592145449b1SDimitry Andric TargetProperties::GetMaximumDepthOfChildrenToDisplay() const {
4593145449b1SDimitry Andric const uint32_t idx = ePropertyMaxChildrenDepth;
4594145449b1SDimitry Andric auto *option_value =
45957fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(idx);
4596145449b1SDimitry Andric bool is_default = !option_value->OptionWasSet();
4597145449b1SDimitry Andric return {option_value->GetCurrentValue(), is_default};
4598145449b1SDimitry Andric }
4599145449b1SDimitry Andric
GetMaximumSizeOfStringSummary() const460014f1b3e8SDimitry Andric uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const {
4601f034231aSEd Maste const uint32_t idx = ePropertyMaxSummaryLength;
46027fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46037fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4604f034231aSEd Maste }
4605f034231aSEd Maste
GetMaximumMemReadSize() const460614f1b3e8SDimitry Andric uint32_t TargetProperties::GetMaximumMemReadSize() const {
4607f034231aSEd Maste const uint32_t idx = ePropertyMaxMemReadSize;
46087fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46097fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4610f034231aSEd Maste }
4611f034231aSEd Maste
GetStandardInputPath() const461214f1b3e8SDimitry Andric FileSpec TargetProperties::GetStandardInputPath() const {
4613f034231aSEd Maste const uint32_t idx = ePropertyInputPath;
46147fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpec>(idx, {});
4615f034231aSEd Maste }
4616f034231aSEd Maste
SetStandardInputPath(llvm::StringRef path)461714f1b3e8SDimitry Andric void TargetProperties::SetStandardInputPath(llvm::StringRef path) {
4618f034231aSEd Maste const uint32_t idx = ePropertyInputPath;
46197fa27ce4SDimitry Andric SetPropertyAtIndex(idx, path);
4620f034231aSEd Maste }
4621f034231aSEd Maste
GetStandardOutputPath() const462214f1b3e8SDimitry Andric FileSpec TargetProperties::GetStandardOutputPath() const {
4623f034231aSEd Maste const uint32_t idx = ePropertyOutputPath;
46247fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpec>(idx, {});
4625f034231aSEd Maste }
4626f034231aSEd Maste
SetStandardOutputPath(llvm::StringRef path)462714f1b3e8SDimitry Andric void TargetProperties::SetStandardOutputPath(llvm::StringRef path) {
4628f034231aSEd Maste const uint32_t idx = ePropertyOutputPath;
46297fa27ce4SDimitry Andric SetPropertyAtIndex(idx, path);
4630f034231aSEd Maste }
4631f034231aSEd Maste
GetStandardErrorPath() const463214f1b3e8SDimitry Andric FileSpec TargetProperties::GetStandardErrorPath() const {
4633f034231aSEd Maste const uint32_t idx = ePropertyErrorPath;
46347fa27ce4SDimitry Andric return GetPropertyAtIndexAs<FileSpec>(idx, {});
4635e81d9d49SDimitry Andric }
4636e81d9d49SDimitry Andric
SetStandardErrorPath(llvm::StringRef path)463714f1b3e8SDimitry Andric void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
463814f1b3e8SDimitry Andric const uint32_t idx = ePropertyErrorPath;
46397fa27ce4SDimitry Andric SetPropertyAtIndex(idx, path);
464014f1b3e8SDimitry Andric }
464114f1b3e8SDimitry Andric
GetLanguage() const4642ac9a064cSDimitry Andric SourceLanguage TargetProperties::GetLanguage() const {
46437fa27ce4SDimitry Andric const uint32_t idx = ePropertyLanguage;
4644ac9a064cSDimitry Andric return {GetPropertyAtIndexAs<LanguageType>(idx, {})};
4645f034231aSEd Maste }
4646f034231aSEd Maste
GetExpressionPrefixContents()464723629167SDimitry Andric llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
4648f034231aSEd Maste const uint32_t idx = ePropertyExprPrefix;
464914f1b3e8SDimitry Andric OptionValueFileSpec *file =
46507fa27ce4SDimitry Andric m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(idx);
465114f1b3e8SDimitry Andric if (file) {
465223629167SDimitry Andric DataBufferSP data_sp(file->GetFileContents());
4653f034231aSEd Maste if (data_sp)
465423629167SDimitry Andric return llvm::StringRef(
465523629167SDimitry Andric reinterpret_cast<const char *>(data_sp->GetBytes()),
465623629167SDimitry Andric data_sp->GetByteSize());
4657f034231aSEd Maste }
465823629167SDimitry Andric return "";
4659f034231aSEd Maste }
4660f034231aSEd Maste
GetExprErrorLimit() const4661b60736ecSDimitry Andric uint64_t TargetProperties::GetExprErrorLimit() const {
4662b60736ecSDimitry Andric const uint32_t idx = ePropertyExprErrorLimit;
46637fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46647fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
46657fa27ce4SDimitry Andric }
46667fa27ce4SDimitry Andric
GetExprAllocAddress() const46677fa27ce4SDimitry Andric uint64_t TargetProperties::GetExprAllocAddress() const {
46687fa27ce4SDimitry Andric const uint32_t idx = ePropertyExprAllocAddress;
46697fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46707fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
46717fa27ce4SDimitry Andric }
46727fa27ce4SDimitry Andric
GetExprAllocSize() const46737fa27ce4SDimitry Andric uint64_t TargetProperties::GetExprAllocSize() const {
46747fa27ce4SDimitry Andric const uint32_t idx = ePropertyExprAllocSize;
46757fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46767fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
46777fa27ce4SDimitry Andric }
46787fa27ce4SDimitry Andric
GetExprAllocAlign() const46797fa27ce4SDimitry Andric uint64_t TargetProperties::GetExprAllocAlign() const {
46807fa27ce4SDimitry Andric const uint32_t idx = ePropertyExprAllocAlign;
46817fa27ce4SDimitry Andric return GetPropertyAtIndexAs<uint64_t>(
46827fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value);
4683b60736ecSDimitry Andric }
4684b60736ecSDimitry Andric
GetBreakpointsConsultPlatformAvoidList()468514f1b3e8SDimitry Andric bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() {
4686f034231aSEd Maste const uint32_t idx = ePropertyBreakpointUseAvoidList;
46877fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
46887fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4689f034231aSEd Maste }
4690f034231aSEd Maste
GetUseHexImmediates() const469114f1b3e8SDimitry Andric bool TargetProperties::GetUseHexImmediates() const {
4692f034231aSEd Maste const uint32_t idx = ePropertyUseHexImmediates;
46937fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
46947fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4695f034231aSEd Maste }
4696f034231aSEd Maste
GetUseFastStepping() const469714f1b3e8SDimitry Andric bool TargetProperties::GetUseFastStepping() const {
4698f034231aSEd Maste const uint32_t idx = ePropertyUseFastStepping;
46997fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
47007fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4701f034231aSEd Maste }
4702f034231aSEd Maste
GetDisplayExpressionsInCrashlogs() const470314f1b3e8SDimitry Andric bool TargetProperties::GetDisplayExpressionsInCrashlogs() const {
4704866dcdacSEd Maste const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
47057fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
47067fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4707866dcdacSEd Maste }
4708866dcdacSEd Maste
GetLoadScriptFromSymbolFile() const470914f1b3e8SDimitry Andric LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const {
4710f034231aSEd Maste const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
47117fa27ce4SDimitry Andric return GetPropertyAtIndexAs<LoadScriptFromSymFile>(
47127fa27ce4SDimitry Andric idx, static_cast<LoadScriptFromSymFile>(
47137fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4714f034231aSEd Maste }
4715f034231aSEd Maste
GetLoadCWDlldbinitFile() const471614f1b3e8SDimitry Andric LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const {
4717f3fbd1c0SDimitry Andric const uint32_t idx = ePropertyLoadCWDlldbinitFile;
47187fa27ce4SDimitry Andric return GetPropertyAtIndexAs<LoadCWDlldbinitFile>(
47197fa27ce4SDimitry Andric idx, static_cast<LoadCWDlldbinitFile>(
47207fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4721f3fbd1c0SDimitry Andric }
4722f3fbd1c0SDimitry Andric
GetHexImmediateStyle() const472314f1b3e8SDimitry Andric Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const {
4724f034231aSEd Maste const uint32_t idx = ePropertyHexImmediateStyle;
47257fa27ce4SDimitry Andric return GetPropertyAtIndexAs<Disassembler::HexImmediateStyle>(
47267fa27ce4SDimitry Andric idx, static_cast<Disassembler::HexImmediateStyle>(
47277fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4728f034231aSEd Maste }
4729f034231aSEd Maste
GetMemoryModuleLoadLevel() const473014f1b3e8SDimitry Andric MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const {
4731f034231aSEd Maste const uint32_t idx = ePropertyMemoryModuleLoadLevel;
47327fa27ce4SDimitry Andric return GetPropertyAtIndexAs<MemoryModuleLoadLevel>(
47337fa27ce4SDimitry Andric idx, static_cast<MemoryModuleLoadLevel>(
47347fa27ce4SDimitry Andric g_target_properties[idx].default_uint_value));
4735f034231aSEd Maste }
4736f034231aSEd Maste
GetUserSpecifiedTrapHandlerNames(Args & args) const473714f1b3e8SDimitry Andric bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const {
4738866dcdacSEd Maste const uint32_t idx = ePropertyTrapHandlerNames;
47397fa27ce4SDimitry Andric return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args);
4740866dcdacSEd Maste }
4741f034231aSEd Maste
SetUserSpecifiedTrapHandlerNames(const Args & args)474214f1b3e8SDimitry Andric void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) {
4743866dcdacSEd Maste const uint32_t idx = ePropertyTrapHandlerNames;
47447fa27ce4SDimitry Andric m_collection_sp->SetPropertyAtIndexFromArgs(idx, args);
4745866dcdacSEd Maste }
4746f034231aSEd Maste
GetDisplayRuntimeSupportValues() const474714f1b3e8SDimitry Andric bool TargetProperties::GetDisplayRuntimeSupportValues() const {
47485e95aa85SEd Maste const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
47497fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
47507fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
47515e95aa85SEd Maste }
47525e95aa85SEd Maste
SetDisplayRuntimeSupportValues(bool b)475314f1b3e8SDimitry Andric void TargetProperties::SetDisplayRuntimeSupportValues(bool b) {
47545e95aa85SEd Maste const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
47557fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
47565e95aa85SEd Maste }
47575e95aa85SEd Maste
GetDisplayRecognizedArguments() const475894994d37SDimitry Andric bool TargetProperties::GetDisplayRecognizedArguments() const {
475994994d37SDimitry Andric const uint32_t idx = ePropertyDisplayRecognizedArguments;
47607fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
47617fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
476294994d37SDimitry Andric }
476394994d37SDimitry Andric
SetDisplayRecognizedArguments(bool b)476494994d37SDimitry Andric void TargetProperties::SetDisplayRecognizedArguments(bool b) {
476594994d37SDimitry Andric const uint32_t idx = ePropertyDisplayRecognizedArguments;
47667fa27ce4SDimitry Andric SetPropertyAtIndex(idx, b);
476794994d37SDimitry Andric }
476894994d37SDimitry Andric
GetProcessLaunchInfo() const4769344a3780SDimitry Andric const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() const {
47705e95aa85SEd Maste return m_launch_info;
47715e95aa85SEd Maste }
47725e95aa85SEd Maste
SetProcessLaunchInfo(const ProcessLaunchInfo & launch_info)477314f1b3e8SDimitry Andric void TargetProperties::SetProcessLaunchInfo(
477414f1b3e8SDimitry Andric const ProcessLaunchInfo &launch_info) {
47755e95aa85SEd Maste m_launch_info = launch_info;
47765e95aa85SEd Maste SetArg0(launch_info.GetArg0());
47775e95aa85SEd Maste SetRunArguments(launch_info.GetArguments());
4778f73363f1SDimitry Andric SetEnvironment(launch_info.GetEnvironment());
477914f1b3e8SDimitry Andric const FileAction *input_file_action =
478014f1b3e8SDimitry Andric launch_info.GetFileActionForFD(STDIN_FILENO);
478114f1b3e8SDimitry Andric if (input_file_action) {
478214f1b3e8SDimitry Andric SetStandardInputPath(input_file_action->GetPath());
47835e95aa85SEd Maste }
478414f1b3e8SDimitry Andric const FileAction *output_file_action =
478514f1b3e8SDimitry Andric launch_info.GetFileActionForFD(STDOUT_FILENO);
478614f1b3e8SDimitry Andric if (output_file_action) {
478714f1b3e8SDimitry Andric SetStandardOutputPath(output_file_action->GetPath());
47885e95aa85SEd Maste }
478914f1b3e8SDimitry Andric const FileAction *error_file_action =
479014f1b3e8SDimitry Andric launch_info.GetFileActionForFD(STDERR_FILENO);
479114f1b3e8SDimitry Andric if (error_file_action) {
479214f1b3e8SDimitry Andric SetStandardErrorPath(error_file_action->GetPath());
47935e95aa85SEd Maste }
47945e95aa85SEd Maste SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
47955e95aa85SEd Maste SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4796b60736ecSDimitry Andric SetInheritTCC(
4797b60736ecSDimitry Andric launch_info.GetFlags().Test(lldb::eLaunchFlagInheritTCCFromParent));
47985e95aa85SEd Maste SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
47995e95aa85SEd Maste }
48005e95aa85SEd Maste
GetRequireHardwareBreakpoints() const480194994d37SDimitry Andric bool TargetProperties::GetRequireHardwareBreakpoints() const {
480294994d37SDimitry Andric const uint32_t idx = ePropertyRequireHardwareBreakpoints;
48037fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
48047fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
480594994d37SDimitry Andric }
480694994d37SDimitry Andric
SetRequireHardwareBreakpoints(bool b)480794994d37SDimitry Andric void TargetProperties::SetRequireHardwareBreakpoints(bool b) {
480894994d37SDimitry Andric const uint32_t idx = ePropertyRequireHardwareBreakpoints;
48097fa27ce4SDimitry Andric m_collection_sp->SetPropertyAtIndex(idx, b);
481094994d37SDimitry Andric }
481194994d37SDimitry Andric
GetAutoInstallMainExecutable() const4812cfca06d7SDimitry Andric bool TargetProperties::GetAutoInstallMainExecutable() const {
4813cfca06d7SDimitry Andric const uint32_t idx = ePropertyAutoInstallMainExecutable;
48147fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
48157fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4816cfca06d7SDimitry Andric }
4817cfca06d7SDimitry Andric
Arg0ValueChangedCallback()4818706b4fc4SDimitry Andric void TargetProperties::Arg0ValueChangedCallback() {
4819706b4fc4SDimitry Andric m_launch_info.SetArg0(GetArg0());
48205e95aa85SEd Maste }
48215e95aa85SEd Maste
RunArgsValueChangedCallback()4822706b4fc4SDimitry Andric void TargetProperties::RunArgsValueChangedCallback() {
48235e95aa85SEd Maste Args args;
4824706b4fc4SDimitry Andric if (GetRunArguments(args))
4825706b4fc4SDimitry Andric m_launch_info.GetArguments() = args;
48265e95aa85SEd Maste }
48275e95aa85SEd Maste
EnvVarsValueChangedCallback()4828706b4fc4SDimitry Andric void TargetProperties::EnvVarsValueChangedCallback() {
4829cfca06d7SDimitry Andric m_launch_info.GetEnvironment() = ComputeEnvironment();
48305e95aa85SEd Maste }
48315e95aa85SEd Maste
InputPathValueChangedCallback()4832706b4fc4SDimitry Andric void TargetProperties::InputPathValueChangedCallback() {
4833706b4fc4SDimitry Andric m_launch_info.AppendOpenFileAction(STDIN_FILENO, GetStandardInputPath(), true,
4834706b4fc4SDimitry Andric false);
48355e95aa85SEd Maste }
48365e95aa85SEd Maste
OutputPathValueChangedCallback()4837706b4fc4SDimitry Andric void TargetProperties::OutputPathValueChangedCallback() {
4838706b4fc4SDimitry Andric m_launch_info.AppendOpenFileAction(STDOUT_FILENO, GetStandardOutputPath(),
4839706b4fc4SDimitry Andric false, true);
48405e95aa85SEd Maste }
48415e95aa85SEd Maste
ErrorPathValueChangedCallback()4842706b4fc4SDimitry Andric void TargetProperties::ErrorPathValueChangedCallback() {
4843706b4fc4SDimitry Andric m_launch_info.AppendOpenFileAction(STDERR_FILENO, GetStandardErrorPath(),
4844706b4fc4SDimitry Andric false, true);
48455e95aa85SEd Maste }
48465e95aa85SEd Maste
DetachOnErrorValueChangedCallback()4847706b4fc4SDimitry Andric void TargetProperties::DetachOnErrorValueChangedCallback() {
4848706b4fc4SDimitry Andric if (GetDetachOnError())
4849706b4fc4SDimitry Andric m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
48505e95aa85SEd Maste else
4851706b4fc4SDimitry Andric m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
48525e95aa85SEd Maste }
48535e95aa85SEd Maste
DisableASLRValueChangedCallback()4854706b4fc4SDimitry Andric void TargetProperties::DisableASLRValueChangedCallback() {
4855706b4fc4SDimitry Andric if (GetDisableASLR())
4856706b4fc4SDimitry Andric m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
48575e95aa85SEd Maste else
4858706b4fc4SDimitry Andric m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
48595e95aa85SEd Maste }
48605e95aa85SEd Maste
InheritTCCValueChangedCallback()4861b60736ecSDimitry Andric void TargetProperties::InheritTCCValueChangedCallback() {
4862b60736ecSDimitry Andric if (GetInheritTCC())
4863b60736ecSDimitry Andric m_launch_info.GetFlags().Set(lldb::eLaunchFlagInheritTCCFromParent);
4864b60736ecSDimitry Andric else
4865b60736ecSDimitry Andric m_launch_info.GetFlags().Clear(lldb::eLaunchFlagInheritTCCFromParent);
4866b60736ecSDimitry Andric }
4867b60736ecSDimitry Andric
DisableSTDIOValueChangedCallback()4868706b4fc4SDimitry Andric void TargetProperties::DisableSTDIOValueChangedCallback() {
4869706b4fc4SDimitry Andric if (GetDisableSTDIO())
4870706b4fc4SDimitry Andric m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
48715e95aa85SEd Maste else
4872706b4fc4SDimitry Andric m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
48735e95aa85SEd Maste }
48745e95aa85SEd Maste
GetDebugUtilityExpression() const4875344a3780SDimitry Andric bool TargetProperties::GetDebugUtilityExpression() const {
4876344a3780SDimitry Andric const uint32_t idx = ePropertyDebugUtilityExpression;
48777fa27ce4SDimitry Andric return GetPropertyAtIndexAs<bool>(
48787fa27ce4SDimitry Andric idx, g_target_properties[idx].default_uint_value != 0);
4879344a3780SDimitry Andric }
4880344a3780SDimitry Andric
SetDebugUtilityExpression(bool debug)4881344a3780SDimitry Andric void TargetProperties::SetDebugUtilityExpression(bool debug) {
4882344a3780SDimitry Andric const uint32_t idx = ePropertyDebugUtilityExpression;
48837fa27ce4SDimitry Andric SetPropertyAtIndex(idx, debug);
4884344a3780SDimitry Andric }
4885344a3780SDimitry Andric
488686758c71SEd Maste // Target::TargetEventData
48875e95aa85SEd Maste
TargetEventData(const lldb::TargetSP & target_sp)488814f1b3e8SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
488914f1b3e8SDimitry Andric : EventData(), m_target_sp(target_sp), m_module_list() {}
48905e95aa85SEd Maste
TargetEventData(const lldb::TargetSP & target_sp,const ModuleList & module_list)489114f1b3e8SDimitry Andric Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
489214f1b3e8SDimitry Andric const ModuleList &module_list)
489314f1b3e8SDimitry Andric : EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
48945e95aa85SEd Maste
4895e81d9d49SDimitry Andric Target::TargetEventData::~TargetEventData() = default;
48965e95aa85SEd Maste
GetFlavorString()48977fa27ce4SDimitry Andric llvm::StringRef Target::TargetEventData::GetFlavorString() {
48987fa27ce4SDimitry Andric return "Target::TargetEventData";
4899f034231aSEd Maste }
4900f034231aSEd Maste
Dump(Stream * s) const490114f1b3e8SDimitry Andric void Target::TargetEventData::Dump(Stream *s) const {
490214f1b3e8SDimitry Andric for (size_t i = 0; i < m_module_list.GetSize(); ++i) {
4903f3fbd1c0SDimitry Andric if (i != 0)
4904f3fbd1c0SDimitry Andric *s << ", ";
490514f1b3e8SDimitry Andric m_module_list.GetModuleAtIndex(i)->GetDescription(
4906706b4fc4SDimitry Andric s->AsRawOstream(), lldb::eDescriptionLevelBrief);
4907f3fbd1c0SDimitry Andric }
4908f034231aSEd Maste }
4909f034231aSEd Maste
4910f034231aSEd Maste const Target::TargetEventData *
GetEventDataFromEvent(const Event * event_ptr)491114f1b3e8SDimitry Andric Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) {
491214f1b3e8SDimitry Andric if (event_ptr) {
4913f034231aSEd Maste const EventData *event_data = event_ptr->GetData();
491414f1b3e8SDimitry Andric if (event_data &&
491514f1b3e8SDimitry Andric event_data->GetFlavor() == TargetEventData::GetFlavorString())
4916f034231aSEd Maste return static_cast<const TargetEventData *>(event_ptr->GetData());
4917f034231aSEd Maste }
4918e81d9d49SDimitry Andric return nullptr;
4919f034231aSEd Maste }
4920f034231aSEd Maste
GetTargetFromEvent(const Event * event_ptr)492114f1b3e8SDimitry Andric TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
49225e95aa85SEd Maste TargetSP target_sp;
49235e95aa85SEd Maste const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
49245e95aa85SEd Maste if (event_data)
49255e95aa85SEd Maste target_sp = event_data->m_target_sp;
49265e95aa85SEd Maste return target_sp;
49275e95aa85SEd Maste }
49285e95aa85SEd Maste
49295e95aa85SEd Maste ModuleList
GetModuleListFromEvent(const Event * event_ptr)493014f1b3e8SDimitry Andric Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
49315e95aa85SEd Maste ModuleList module_list;
49325e95aa85SEd Maste const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
49335e95aa85SEd Maste if (event_data)
49345e95aa85SEd Maste module_list = event_data->m_module_list;
49355e95aa85SEd Maste return module_list;
49365e95aa85SEd Maste }
4937ead24645SDimitry Andric
GetAPIMutex()4938ead24645SDimitry Andric std::recursive_mutex &Target::GetAPIMutex() {
4939ead24645SDimitry Andric if (GetProcessSP() && GetProcessSP()->CurrentThreadIsPrivateStateThread())
4940ead24645SDimitry Andric return m_private_mutex;
4941ead24645SDimitry Andric else
4942ead24645SDimitry Andric return m_mutex;
4943ead24645SDimitry Andric }
4944c0981da4SDimitry Andric
4945c0981da4SDimitry Andric /// Get metrics associated with this target in JSON format.
4946ac9a064cSDimitry Andric llvm::json::Value
ReportStatistics(const lldb_private::StatisticsOptions & options)4947ac9a064cSDimitry Andric Target::ReportStatistics(const lldb_private::StatisticsOptions &options) {
4948ac9a064cSDimitry Andric return m_stats.ToJSON(*this, options);
4949ac9a064cSDimitry Andric }
4950