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