15ffd83dbSDimitry Andric //===-- SBTarget.cpp ------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/API/SBTarget.h"
1004eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h"
1181ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
120b57cec5SDimitry Andric #include "lldb/lldb-public.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "lldb/API/SBBreakpoint.h"
150b57cec5SDimitry Andric #include "lldb/API/SBDebugger.h"
165ffd83dbSDimitry Andric #include "lldb/API/SBEnvironment.h"
170b57cec5SDimitry Andric #include "lldb/API/SBEvent.h"
180b57cec5SDimitry Andric #include "lldb/API/SBExpressionOptions.h"
190b57cec5SDimitry Andric #include "lldb/API/SBFileSpec.h"
200b57cec5SDimitry Andric #include "lldb/API/SBListener.h"
210b57cec5SDimitry Andric #include "lldb/API/SBModule.h"
220b57cec5SDimitry Andric #include "lldb/API/SBModuleSpec.h"
230b57cec5SDimitry Andric #include "lldb/API/SBProcess.h"
240b57cec5SDimitry Andric #include "lldb/API/SBSourceManager.h"
250b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
260b57cec5SDimitry Andric #include "lldb/API/SBStringList.h"
270b57cec5SDimitry Andric #include "lldb/API/SBStructuredData.h"
280b57cec5SDimitry Andric #include "lldb/API/SBSymbolContextList.h"
29fe6060f1SDimitry Andric #include "lldb/API/SBTrace.h"
300b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointID.h"
310b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
320b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointList.h"
330b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
340b57cec5SDimitry Andric #include "lldb/Core/Address.h"
350b57cec5SDimitry Andric #include "lldb/Core/AddressResolver.h"
360b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
370b57cec5SDimitry Andric #include "lldb/Core/Disassembler.h"
380b57cec5SDimitry Andric #include "lldb/Core/Module.h"
390b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
40*c9157d92SDimitry Andric #include "lldb/Core/PluginManager.h"
410b57cec5SDimitry Andric #include "lldb/Core/SearchFilter.h"
420b57cec5SDimitry Andric #include "lldb/Core/Section.h"
430b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
440b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
450b57cec5SDimitry Andric #include "lldb/Core/ValueObjectList.h"
460b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
470b57cec5SDimitry Andric #include "lldb/Host/Host.h"
480b57cec5SDimitry Andric #include "lldb/Symbol/DeclVendor.h"
490b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
500b57cec5SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
510b57cec5SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
529dba64beSDimitry Andric #include "lldb/Symbol/TypeSystem.h"
530b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
540b57cec5SDimitry Andric #include "lldb/Target/ABI.h"
550b57cec5SDimitry Andric #include "lldb/Target/Language.h"
560b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
570b57cec5SDimitry Andric #include "lldb/Target/Process.h"
580b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
590b57cec5SDimitry Andric #include "lldb/Target/Target.h"
600b57cec5SDimitry Andric #include "lldb/Target/TargetList.h"
610b57cec5SDimitry Andric #include "lldb/Utility/ArchSpec.h"
620b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
630b57cec5SDimitry Andric #include "lldb/Utility/FileSpec.h"
640b57cec5SDimitry Andric #include "lldb/Utility/ProcessInfo.h"
650b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric #include "Commands/CommandObjectBreakpoint.h"
680b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
690b57cec5SDimitry Andric #include "llvm/Support/PrettyStackTrace.h"
700b57cec5SDimitry Andric #include "llvm/Support/Regex.h"
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric using namespace lldb;
730b57cec5SDimitry Andric using namespace lldb_private;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric #define DEFAULT_DISASM_BYTE_SIZE 32
760b57cec5SDimitry Andric 
AttachToProcess(ProcessAttachInfo & attach_info,Target & target)77349cc55cSDimitry Andric static Status AttachToProcess(ProcessAttachInfo &attach_info, Target &target) {
780b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   auto process_sp = target.GetProcessSP();
810b57cec5SDimitry Andric   if (process_sp) {
820b57cec5SDimitry Andric     const auto state = process_sp->GetState();
830b57cec5SDimitry Andric     if (process_sp->IsAlive() && state == eStateConnected) {
840b57cec5SDimitry Andric       // If we are already connected, then we have already specified the
850b57cec5SDimitry Andric       // listener, so if a valid listener is supplied, we need to error out to
860b57cec5SDimitry Andric       // let the client know.
870b57cec5SDimitry Andric       if (attach_info.GetListener())
880b57cec5SDimitry Andric         return Status("process is connected and already has a listener, pass "
890b57cec5SDimitry Andric                       "empty listener");
900b57cec5SDimitry Andric     }
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   return target.Attach(attach_info, nullptr);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric // SBTarget constructor
SBTarget()9704eeddc0SDimitry Andric SBTarget::SBTarget() { LLDB_INSTRUMENT_VA(this); }
980b57cec5SDimitry Andric 
SBTarget(const SBTarget & rhs)990b57cec5SDimitry Andric SBTarget::SBTarget(const SBTarget &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
10004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
SBTarget(const TargetSP & target_sp)1030b57cec5SDimitry Andric SBTarget::SBTarget(const TargetSP &target_sp) : m_opaque_sp(target_sp) {
10404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, target_sp);
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
operator =(const SBTarget & rhs)1070b57cec5SDimitry Andric const SBTarget &SBTarget::operator=(const SBTarget &rhs) {
10804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   if (this != &rhs)
1110b57cec5SDimitry Andric     m_opaque_sp = rhs.m_opaque_sp;
11204eeddc0SDimitry Andric   return *this;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric // Destructor
1165ffd83dbSDimitry Andric SBTarget::~SBTarget() = default;
1170b57cec5SDimitry Andric 
EventIsTargetEvent(const SBEvent & event)1180b57cec5SDimitry Andric bool SBTarget::EventIsTargetEvent(const SBEvent &event) {
11904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(event);
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   return Target::TargetEventData::GetEventDataFromEvent(event.get()) != nullptr;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
GetTargetFromEvent(const SBEvent & event)1240b57cec5SDimitry Andric SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
12504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(event);
1260b57cec5SDimitry Andric 
12704eeddc0SDimitry Andric   return Target::TargetEventData::GetTargetFromEvent(event.get());
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
GetNumModulesFromEvent(const SBEvent & event)1300b57cec5SDimitry Andric uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
13104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(event);
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   const ModuleList module_list =
1340b57cec5SDimitry Andric       Target::TargetEventData::GetModuleListFromEvent(event.get());
1350b57cec5SDimitry Andric   return module_list.GetSize();
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric 
GetModuleAtIndexFromEvent(const uint32_t idx,const SBEvent & event)1380b57cec5SDimitry Andric SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
1390b57cec5SDimitry Andric                                              const SBEvent &event) {
14004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(idx, event);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   const ModuleList module_list =
1430b57cec5SDimitry Andric       Target::TargetEventData::GetModuleListFromEvent(event.get());
14404eeddc0SDimitry Andric   return SBModule(module_list.GetModuleAtIndex(idx));
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
GetBroadcasterClassName()1470b57cec5SDimitry Andric const char *SBTarget::GetBroadcasterClassName() {
14804eeddc0SDimitry Andric   LLDB_INSTRUMENT();
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   return Target::GetStaticBroadcasterClass().AsCString();
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
IsValid() const1530b57cec5SDimitry Andric bool SBTarget::IsValid() const {
15404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1550b57cec5SDimitry Andric   return this->operator bool();
1560b57cec5SDimitry Andric }
operator bool() const1570b57cec5SDimitry Andric SBTarget::operator bool() const {
15804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   return m_opaque_sp.get() != nullptr && m_opaque_sp->IsValid();
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
GetProcess()1630b57cec5SDimitry Andric SBProcess SBTarget::GetProcess() {
16404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   SBProcess sb_process;
1670b57cec5SDimitry Andric   ProcessSP process_sp;
1680b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1690b57cec5SDimitry Andric   if (target_sp) {
1700b57cec5SDimitry Andric     process_sp = target_sp->GetProcessSP();
1710b57cec5SDimitry Andric     sb_process.SetSP(process_sp);
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric 
17404eeddc0SDimitry Andric   return sb_process;
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
GetPlatform()1770b57cec5SDimitry Andric SBPlatform SBTarget::GetPlatform() {
17804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1810b57cec5SDimitry Andric   if (!target_sp)
18204eeddc0SDimitry Andric     return SBPlatform();
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   SBPlatform platform;
1850b57cec5SDimitry Andric   platform.m_opaque_sp = target_sp->GetPlatform();
1860b57cec5SDimitry Andric 
18704eeddc0SDimitry Andric   return platform;
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric 
GetDebugger() const1900b57cec5SDimitry Andric SBDebugger SBTarget::GetDebugger() const {
19104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   SBDebugger debugger;
1940b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1950b57cec5SDimitry Andric   if (target_sp)
1960b57cec5SDimitry Andric     debugger.reset(target_sp->GetDebugger().shared_from_this());
19704eeddc0SDimitry Andric   return debugger;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
GetStatistics()2000b57cec5SDimitry Andric SBStructuredData SBTarget::GetStatistics() {
20104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   SBStructuredData data;
2040b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
2050b57cec5SDimitry Andric   if (!target_sp)
20604eeddc0SDimitry Andric     return data;
207349cc55cSDimitry Andric   std::string json_str =
208349cc55cSDimitry Andric       llvm::formatv("{0:2}",
209349cc55cSDimitry Andric           DebuggerStats::ReportStatistics(target_sp->GetDebugger(),
210349cc55cSDimitry Andric                                           target_sp.get())).str();
211349cc55cSDimitry Andric   data.m_impl_up->SetObjectSP(StructuredData::ParseJSON(json_str));
21204eeddc0SDimitry Andric   return data;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
SetCollectingStats(bool v)2150b57cec5SDimitry Andric void SBTarget::SetCollectingStats(bool v) {
21604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, v);
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
2190b57cec5SDimitry Andric   if (!target_sp)
2200b57cec5SDimitry Andric     return;
221349cc55cSDimitry Andric   return DebuggerStats::SetCollectingStats(v);
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric 
GetCollectingStats()2240b57cec5SDimitry Andric bool SBTarget::GetCollectingStats() {
22504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
2280b57cec5SDimitry Andric   if (!target_sp)
2290b57cec5SDimitry Andric     return false;
230349cc55cSDimitry Andric   return DebuggerStats::GetCollectingStats();
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric 
LoadCore(const char * core_file)2330b57cec5SDimitry Andric SBProcess SBTarget::LoadCore(const char *core_file) {
23404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, core_file);
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   lldb::SBError error; // Ignored
23704eeddc0SDimitry Andric   return LoadCore(core_file, error);
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
LoadCore(const char * core_file,lldb::SBError & error)2400b57cec5SDimitry Andric SBProcess SBTarget::LoadCore(const char *core_file, lldb::SBError &error) {
24104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, core_file, error);
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   SBProcess sb_process;
2440b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
2450b57cec5SDimitry Andric   if (target_sp) {
2460b57cec5SDimitry Andric     FileSpec filespec(core_file);
2470b57cec5SDimitry Andric     FileSystem::Instance().Resolve(filespec);
2480b57cec5SDimitry Andric     ProcessSP process_sp(target_sp->CreateProcess(
249e8d8bef9SDimitry Andric         target_sp->GetDebugger().GetListener(), "", &filespec, false));
2500b57cec5SDimitry Andric     if (process_sp) {
2510b57cec5SDimitry Andric       error.SetError(process_sp->LoadCore());
2520b57cec5SDimitry Andric       if (error.Success())
2530b57cec5SDimitry Andric         sb_process.SetSP(process_sp);
2540b57cec5SDimitry Andric     } else {
2550b57cec5SDimitry Andric       error.SetErrorString("Failed to create the process");
2560b57cec5SDimitry Andric     }
2570b57cec5SDimitry Andric   } else {
2580b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
2590b57cec5SDimitry Andric   }
26004eeddc0SDimitry Andric   return sb_process;
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric 
LaunchSimple(char const ** argv,char const ** envp,const char * working_directory)2630b57cec5SDimitry Andric SBProcess SBTarget::LaunchSimple(char const **argv, char const **envp,
2640b57cec5SDimitry Andric                                  const char *working_directory) {
26504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, argv, envp, working_directory);
2660b57cec5SDimitry Andric 
267e8d8bef9SDimitry Andric   TargetSP target_sp = GetSP();
268e8d8bef9SDimitry Andric   if (!target_sp)
26904eeddc0SDimitry Andric     return SBProcess();
270e8d8bef9SDimitry Andric 
271e8d8bef9SDimitry Andric   SBLaunchInfo launch_info = GetLaunchInfo();
272e8d8bef9SDimitry Andric 
273e8d8bef9SDimitry Andric   if (Module *exe_module = target_sp->GetExecutableModulePointer())
274e8d8bef9SDimitry Andric     launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(),
275e8d8bef9SDimitry Andric                                   /*add_as_first_arg*/ true);
276e8d8bef9SDimitry Andric   if (argv)
277e8d8bef9SDimitry Andric     launch_info.SetArguments(argv, /*append*/ true);
278e8d8bef9SDimitry Andric   if (envp)
279e8d8bef9SDimitry Andric     launch_info.SetEnvironmentEntries(envp, /*append*/ false);
280e8d8bef9SDimitry Andric   if (working_directory)
281e8d8bef9SDimitry Andric     launch_info.SetWorkingDirectory(working_directory);
282e8d8bef9SDimitry Andric 
2830b57cec5SDimitry Andric   SBError error;
28404eeddc0SDimitry Andric   return Launch(launch_info, error);
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
Install()2870b57cec5SDimitry Andric SBError SBTarget::Install() {
28804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   SBError sb_error;
2910b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
2920b57cec5SDimitry Andric   if (target_sp) {
2930b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
2940b57cec5SDimitry Andric     sb_error.ref() = target_sp->Install(nullptr);
2950b57cec5SDimitry Andric   }
29604eeddc0SDimitry Andric   return sb_error;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
Launch(SBListener & listener,char const ** argv,char const ** envp,const char * stdin_path,const char * stdout_path,const char * stderr_path,const char * working_directory,uint32_t launch_flags,bool stop_at_entry,lldb::SBError & error)2990b57cec5SDimitry Andric SBProcess SBTarget::Launch(SBListener &listener, char const **argv,
3000b57cec5SDimitry Andric                            char const **envp, const char *stdin_path,
3010b57cec5SDimitry Andric                            const char *stdout_path, const char *stderr_path,
3020b57cec5SDimitry Andric                            const char *working_directory,
3030b57cec5SDimitry Andric                            uint32_t launch_flags, // See LaunchFlags
3040b57cec5SDimitry Andric                            bool stop_at_entry, lldb::SBError &error) {
30504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, listener, argv, envp, stdin_path, stdout_path,
30604eeddc0SDimitry Andric                      stderr_path, working_directory, launch_flags,
30704eeddc0SDimitry Andric                      stop_at_entry, error);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   SBProcess sb_process;
3100b57cec5SDimitry Andric   ProcessSP process_sp;
3110b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   if (target_sp) {
3140b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric     if (stop_at_entry)
3170b57cec5SDimitry Andric       launch_flags |= eLaunchFlagStopAtEntry;
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric     if (getenv("LLDB_LAUNCH_FLAG_DISABLE_ASLR"))
3200b57cec5SDimitry Andric       launch_flags |= eLaunchFlagDisableASLR;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric     StateType state = eStateInvalid;
3230b57cec5SDimitry Andric     process_sp = target_sp->GetProcessSP();
3240b57cec5SDimitry Andric     if (process_sp) {
3250b57cec5SDimitry Andric       state = process_sp->GetState();
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric       if (process_sp->IsAlive() && state != eStateConnected) {
3280b57cec5SDimitry Andric         if (state == eStateAttaching)
3290b57cec5SDimitry Andric           error.SetErrorString("process attach is in progress");
3300b57cec5SDimitry Andric         else
3310b57cec5SDimitry Andric           error.SetErrorString("a process is already being debugged");
33204eeddc0SDimitry Andric         return sb_process;
3330b57cec5SDimitry Andric       }
3340b57cec5SDimitry Andric     }
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric     if (state == eStateConnected) {
3370b57cec5SDimitry Andric       // If we are already connected, then we have already specified the
3380b57cec5SDimitry Andric       // listener, so if a valid listener is supplied, we need to error out to
3390b57cec5SDimitry Andric       // let the client know.
3400b57cec5SDimitry Andric       if (listener.IsValid()) {
3410b57cec5SDimitry Andric         error.SetErrorString("process is connected and already has a listener, "
3420b57cec5SDimitry Andric                              "pass empty listener");
34304eeddc0SDimitry Andric         return sb_process;
3440b57cec5SDimitry Andric       }
3450b57cec5SDimitry Andric     }
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     if (getenv("LLDB_LAUNCH_FLAG_DISABLE_STDIO"))
3480b57cec5SDimitry Andric       launch_flags |= eLaunchFlagDisableSTDIO;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     ProcessLaunchInfo launch_info(FileSpec(stdin_path), FileSpec(stdout_path),
3510b57cec5SDimitry Andric                                   FileSpec(stderr_path),
3520b57cec5SDimitry Andric                                   FileSpec(working_directory), launch_flags);
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric     Module *exe_module = target_sp->GetExecutableModulePointer();
3550b57cec5SDimitry Andric     if (exe_module)
3560b57cec5SDimitry Andric       launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
3575ffd83dbSDimitry Andric     if (argv) {
3580b57cec5SDimitry Andric       launch_info.GetArguments().AppendArguments(argv);
3595ffd83dbSDimitry Andric     } else {
3605ffd83dbSDimitry Andric       auto default_launch_info = target_sp->GetProcessLaunchInfo();
3615ffd83dbSDimitry Andric       launch_info.GetArguments().AppendArguments(
3625ffd83dbSDimitry Andric           default_launch_info.GetArguments());
3635ffd83dbSDimitry Andric     }
3645ffd83dbSDimitry Andric     if (envp) {
3650b57cec5SDimitry Andric       launch_info.GetEnvironment() = Environment(envp);
3665ffd83dbSDimitry Andric     } else {
3675ffd83dbSDimitry Andric       auto default_launch_info = target_sp->GetProcessLaunchInfo();
3685ffd83dbSDimitry Andric       launch_info.GetEnvironment() = default_launch_info.GetEnvironment();
3695ffd83dbSDimitry Andric     }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric     if (listener.IsValid())
3720b57cec5SDimitry Andric       launch_info.SetListener(listener.GetSP());
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric     error.SetError(target_sp->Launch(launch_info, nullptr));
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric     sb_process.SetSP(target_sp->GetProcessSP());
3770b57cec5SDimitry Andric   } else {
3780b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
3790b57cec5SDimitry Andric   }
3800b57cec5SDimitry Andric 
38104eeddc0SDimitry Andric   return sb_process;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
Launch(SBLaunchInfo & sb_launch_info,SBError & error)3840b57cec5SDimitry Andric SBProcess SBTarget::Launch(SBLaunchInfo &sb_launch_info, SBError &error) {
38504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_launch_info, error);
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   SBProcess sb_process;
3880b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   if (target_sp) {
3910b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
3920b57cec5SDimitry Andric     StateType state = eStateInvalid;
3930b57cec5SDimitry Andric     {
3940b57cec5SDimitry Andric       ProcessSP process_sp = target_sp->GetProcessSP();
3950b57cec5SDimitry Andric       if (process_sp) {
3960b57cec5SDimitry Andric         state = process_sp->GetState();
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric         if (process_sp->IsAlive() && state != eStateConnected) {
3990b57cec5SDimitry Andric           if (state == eStateAttaching)
4000b57cec5SDimitry Andric             error.SetErrorString("process attach is in progress");
4010b57cec5SDimitry Andric           else
4020b57cec5SDimitry Andric             error.SetErrorString("a process is already being debugged");
40304eeddc0SDimitry Andric           return sb_process;
4040b57cec5SDimitry Andric         }
4050b57cec5SDimitry Andric       }
4060b57cec5SDimitry Andric     }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric     lldb_private::ProcessLaunchInfo launch_info = sb_launch_info.ref();
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric     if (!launch_info.GetExecutableFile()) {
4110b57cec5SDimitry Andric       Module *exe_module = target_sp->GetExecutableModulePointer();
4120b57cec5SDimitry Andric       if (exe_module)
4130b57cec5SDimitry Andric         launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
4140b57cec5SDimitry Andric     }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric     const ArchSpec &arch_spec = target_sp->GetArchitecture();
4170b57cec5SDimitry Andric     if (arch_spec.IsValid())
4180b57cec5SDimitry Andric       launch_info.GetArchitecture() = arch_spec;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric     error.SetError(target_sp->Launch(launch_info, nullptr));
4210b57cec5SDimitry Andric     sb_launch_info.set_ref(launch_info);
4220b57cec5SDimitry Andric     sb_process.SetSP(target_sp->GetProcessSP());
4230b57cec5SDimitry Andric   } else {
4240b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric 
42704eeddc0SDimitry Andric   return sb_process;
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric 
Attach(SBAttachInfo & sb_attach_info,SBError & error)4300b57cec5SDimitry Andric lldb::SBProcess SBTarget::Attach(SBAttachInfo &sb_attach_info, SBError &error) {
43104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_attach_info, error);
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   SBProcess sb_process;
4340b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric   if (target_sp) {
4370b57cec5SDimitry Andric     ProcessAttachInfo &attach_info = sb_attach_info.ref();
438fe013be4SDimitry Andric     if (attach_info.ProcessIDIsValid() && !attach_info.UserIDIsValid() &&
439fe013be4SDimitry Andric         !attach_info.IsScriptedProcess()) {
4400b57cec5SDimitry Andric       PlatformSP platform_sp = target_sp->GetPlatform();
4410b57cec5SDimitry Andric       // See if we can pre-verify if a process exists or not
4420b57cec5SDimitry Andric       if (platform_sp && platform_sp->IsConnected()) {
4430b57cec5SDimitry Andric         lldb::pid_t attach_pid = attach_info.GetProcessID();
4440b57cec5SDimitry Andric         ProcessInstanceInfo instance_info;
4450b57cec5SDimitry Andric         if (platform_sp->GetProcessInfo(attach_pid, instance_info)) {
4460b57cec5SDimitry Andric           attach_info.SetUserID(instance_info.GetEffectiveUserID());
4470b57cec5SDimitry Andric         } else {
4480b57cec5SDimitry Andric           error.ref().SetErrorStringWithFormat(
4490b57cec5SDimitry Andric               "no process found with process ID %" PRIu64, attach_pid);
45004eeddc0SDimitry Andric           return sb_process;
4510b57cec5SDimitry Andric         }
4520b57cec5SDimitry Andric       }
4530b57cec5SDimitry Andric     }
4540b57cec5SDimitry Andric     error.SetError(AttachToProcess(attach_info, *target_sp));
4550b57cec5SDimitry Andric     if (error.Success())
4560b57cec5SDimitry Andric       sb_process.SetSP(target_sp->GetProcessSP());
4570b57cec5SDimitry Andric   } else {
4580b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric 
46104eeddc0SDimitry Andric   return sb_process;
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
AttachToProcessWithID(SBListener & listener,lldb::pid_t pid,SBError & error)4640b57cec5SDimitry Andric lldb::SBProcess SBTarget::AttachToProcessWithID(
4650b57cec5SDimitry Andric     SBListener &listener,
4660b57cec5SDimitry Andric     lldb::pid_t pid, // The process ID to attach to
4670b57cec5SDimitry Andric     SBError &error   // An error explaining what went wrong if attach fails
4680b57cec5SDimitry Andric ) {
46904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, listener, pid, error);
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric   SBProcess sb_process;
4720b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   if (target_sp) {
4750b57cec5SDimitry Andric     ProcessAttachInfo attach_info;
4760b57cec5SDimitry Andric     attach_info.SetProcessID(pid);
4770b57cec5SDimitry Andric     if (listener.IsValid())
4780b57cec5SDimitry Andric       attach_info.SetListener(listener.GetSP());
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     ProcessInstanceInfo instance_info;
4810b57cec5SDimitry Andric     if (target_sp->GetPlatform()->GetProcessInfo(pid, instance_info))
4820b57cec5SDimitry Andric       attach_info.SetUserID(instance_info.GetEffectiveUserID());
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric     error.SetError(AttachToProcess(attach_info, *target_sp));
4850b57cec5SDimitry Andric     if (error.Success())
4860b57cec5SDimitry Andric       sb_process.SetSP(target_sp->GetProcessSP());
4870b57cec5SDimitry Andric   } else
4880b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
4890b57cec5SDimitry Andric 
49004eeddc0SDimitry Andric   return sb_process;
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
AttachToProcessWithName(SBListener & listener,const char * name,bool wait_for,SBError & error)4930b57cec5SDimitry Andric lldb::SBProcess SBTarget::AttachToProcessWithName(
4940b57cec5SDimitry Andric     SBListener &listener,
4950b57cec5SDimitry Andric     const char *name, // basename of process to attach to
4960b57cec5SDimitry Andric     bool wait_for, // if true wait for a new instance of "name" to be launched
4970b57cec5SDimitry Andric     SBError &error // An error explaining what went wrong if attach fails
4980b57cec5SDimitry Andric ) {
49904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, listener, name, wait_for, error);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   SBProcess sb_process;
5020b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   if (name && target_sp) {
5050b57cec5SDimitry Andric     ProcessAttachInfo attach_info;
5060b57cec5SDimitry Andric     attach_info.GetExecutableFile().SetFile(name, FileSpec::Style::native);
5070b57cec5SDimitry Andric     attach_info.SetWaitForLaunch(wait_for);
5080b57cec5SDimitry Andric     if (listener.IsValid())
5090b57cec5SDimitry Andric       attach_info.SetListener(listener.GetSP());
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric     error.SetError(AttachToProcess(attach_info, *target_sp));
5120b57cec5SDimitry Andric     if (error.Success())
5130b57cec5SDimitry Andric       sb_process.SetSP(target_sp->GetProcessSP());
5140b57cec5SDimitry Andric   } else
5150b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
5160b57cec5SDimitry Andric 
51704eeddc0SDimitry Andric   return sb_process;
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric 
ConnectRemote(SBListener & listener,const char * url,const char * plugin_name,SBError & error)5200b57cec5SDimitry Andric lldb::SBProcess SBTarget::ConnectRemote(SBListener &listener, const char *url,
5210b57cec5SDimitry Andric                                         const char *plugin_name,
5220b57cec5SDimitry Andric                                         SBError &error) {
52304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, listener, url, plugin_name, error);
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   SBProcess sb_process;
5260b57cec5SDimitry Andric   ProcessSP process_sp;
5270b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   if (target_sp) {
5300b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
5310b57cec5SDimitry Andric     if (listener.IsValid())
5320b57cec5SDimitry Andric       process_sp =
533e8d8bef9SDimitry Andric           target_sp->CreateProcess(listener.m_opaque_sp, plugin_name, nullptr,
534e8d8bef9SDimitry Andric                                    true);
5350b57cec5SDimitry Andric     else
5360b57cec5SDimitry Andric       process_sp = target_sp->CreateProcess(
537e8d8bef9SDimitry Andric           target_sp->GetDebugger().GetListener(), plugin_name, nullptr, true);
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric     if (process_sp) {
5400b57cec5SDimitry Andric       sb_process.SetSP(process_sp);
5415ffd83dbSDimitry Andric       error.SetError(process_sp->ConnectRemote(url));
5420b57cec5SDimitry Andric     } else {
5430b57cec5SDimitry Andric       error.SetErrorString("unable to create lldb_private::Process");
5440b57cec5SDimitry Andric     }
5450b57cec5SDimitry Andric   } else {
5460b57cec5SDimitry Andric     error.SetErrorString("SBTarget is invalid");
5470b57cec5SDimitry Andric   }
5480b57cec5SDimitry Andric 
54904eeddc0SDimitry Andric   return sb_process;
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric 
GetExecutable()5520b57cec5SDimitry Andric SBFileSpec SBTarget::GetExecutable() {
55304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric   SBFileSpec exe_file_spec;
5560b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
5570b57cec5SDimitry Andric   if (target_sp) {
5580b57cec5SDimitry Andric     Module *exe_module = target_sp->GetExecutableModulePointer();
5590b57cec5SDimitry Andric     if (exe_module)
5600b57cec5SDimitry Andric       exe_file_spec.SetFileSpec(exe_module->GetFileSpec());
5610b57cec5SDimitry Andric   }
5620b57cec5SDimitry Andric 
56304eeddc0SDimitry Andric   return exe_file_spec;
5640b57cec5SDimitry Andric }
5650b57cec5SDimitry Andric 
operator ==(const SBTarget & rhs) const5660b57cec5SDimitry Andric bool SBTarget::operator==(const SBTarget &rhs) const {
56704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   return m_opaque_sp.get() == rhs.m_opaque_sp.get();
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
operator !=(const SBTarget & rhs) const5720b57cec5SDimitry Andric bool SBTarget::operator!=(const SBTarget &rhs) const {
57304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   return m_opaque_sp.get() != rhs.m_opaque_sp.get();
5760b57cec5SDimitry Andric }
5770b57cec5SDimitry Andric 
GetSP() const5780b57cec5SDimitry Andric lldb::TargetSP SBTarget::GetSP() const { return m_opaque_sp; }
5790b57cec5SDimitry Andric 
SetSP(const lldb::TargetSP & target_sp)5800b57cec5SDimitry Andric void SBTarget::SetSP(const lldb::TargetSP &target_sp) {
5810b57cec5SDimitry Andric   m_opaque_sp = target_sp;
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric 
ResolveLoadAddress(lldb::addr_t vm_addr)5840b57cec5SDimitry Andric lldb::SBAddress SBTarget::ResolveLoadAddress(lldb::addr_t vm_addr) {
58504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, vm_addr);
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   lldb::SBAddress sb_addr;
5880b57cec5SDimitry Andric   Address &addr = sb_addr.ref();
5890b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
5900b57cec5SDimitry Andric   if (target_sp) {
5910b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
5920b57cec5SDimitry Andric     if (target_sp->ResolveLoadAddress(vm_addr, addr))
59304eeddc0SDimitry Andric       return sb_addr;
5940b57cec5SDimitry Andric   }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   // We have a load address that isn't in a section, just return an address
5970b57cec5SDimitry Andric   // with the offset filled in (the address) and the section set to NULL
5980b57cec5SDimitry Andric   addr.SetRawAddress(vm_addr);
59904eeddc0SDimitry Andric   return sb_addr;
6000b57cec5SDimitry Andric }
6010b57cec5SDimitry Andric 
ResolveFileAddress(lldb::addr_t file_addr)6020b57cec5SDimitry Andric lldb::SBAddress SBTarget::ResolveFileAddress(lldb::addr_t file_addr) {
60304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, file_addr);
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   lldb::SBAddress sb_addr;
6060b57cec5SDimitry Andric   Address &addr = sb_addr.ref();
6070b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
6080b57cec5SDimitry Andric   if (target_sp) {
6090b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
6100b57cec5SDimitry Andric     if (target_sp->ResolveFileAddress(file_addr, addr))
61104eeddc0SDimitry Andric       return sb_addr;
6120b57cec5SDimitry Andric   }
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   addr.SetRawAddress(file_addr);
61504eeddc0SDimitry Andric   return sb_addr;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
ResolvePastLoadAddress(uint32_t stop_id,lldb::addr_t vm_addr)6180b57cec5SDimitry Andric lldb::SBAddress SBTarget::ResolvePastLoadAddress(uint32_t stop_id,
6190b57cec5SDimitry Andric                                                  lldb::addr_t vm_addr) {
62004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, stop_id, vm_addr);
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   lldb::SBAddress sb_addr;
6230b57cec5SDimitry Andric   Address &addr = sb_addr.ref();
6240b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
6250b57cec5SDimitry Andric   if (target_sp) {
6260b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
6270b57cec5SDimitry Andric     if (target_sp->ResolveLoadAddress(vm_addr, addr))
62804eeddc0SDimitry Andric       return sb_addr;
6290b57cec5SDimitry Andric   }
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   // We have a load address that isn't in a section, just return an address
6320b57cec5SDimitry Andric   // with the offset filled in (the address) and the section set to NULL
6330b57cec5SDimitry Andric   addr.SetRawAddress(vm_addr);
63404eeddc0SDimitry Andric   return sb_addr;
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric SBSymbolContext
ResolveSymbolContextForAddress(const SBAddress & addr,uint32_t resolve_scope)6380b57cec5SDimitry Andric SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
6390b57cec5SDimitry Andric                                          uint32_t resolve_scope) {
64004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, addr, resolve_scope);
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric   SBSymbolContext sc;
6430b57cec5SDimitry Andric   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
6440b57cec5SDimitry Andric   if (addr.IsValid()) {
6450b57cec5SDimitry Andric     TargetSP target_sp(GetSP());
6460b57cec5SDimitry Andric     if (target_sp)
6470b57cec5SDimitry Andric       target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
6480b57cec5SDimitry Andric                                                             sc.ref());
6490b57cec5SDimitry Andric   }
65004eeddc0SDimitry Andric   return sc;
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
ReadMemory(const SBAddress addr,void * buf,size_t size,lldb::SBError & error)6530b57cec5SDimitry Andric size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,
6540b57cec5SDimitry Andric                             lldb::SBError &error) {
65504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, addr, buf, size, error);
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   SBError sb_error;
6580b57cec5SDimitry Andric   size_t bytes_read = 0;
6590b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
6600b57cec5SDimitry Andric   if (target_sp) {
6610b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
6620b57cec5SDimitry Andric     bytes_read =
663fe6060f1SDimitry Andric         target_sp->ReadMemory(addr.ref(), buf, size, sb_error.ref(), true);
6640b57cec5SDimitry Andric   } else {
6650b57cec5SDimitry Andric     sb_error.SetErrorString("invalid target");
6660b57cec5SDimitry Andric   }
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   return bytes_read;
6690b57cec5SDimitry Andric }
6700b57cec5SDimitry Andric 
BreakpointCreateByLocation(const char * file,uint32_t line)6710b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByLocation(const char *file,
6720b57cec5SDimitry Andric                                                   uint32_t line) {
67304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, file, line);
6740b57cec5SDimitry Andric 
67504eeddc0SDimitry Andric   return SBBreakpoint(
67604eeddc0SDimitry Andric       BreakpointCreateByLocation(SBFileSpec(file, false), line));
6770b57cec5SDimitry Andric }
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric SBBreakpoint
BreakpointCreateByLocation(const SBFileSpec & sb_file_spec,uint32_t line)6800b57cec5SDimitry Andric SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
6810b57cec5SDimitry Andric                                      uint32_t line) {
68204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec, line);
6830b57cec5SDimitry Andric 
68404eeddc0SDimitry Andric   return BreakpointCreateByLocation(sb_file_spec, line, 0);
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric SBBreakpoint
BreakpointCreateByLocation(const SBFileSpec & sb_file_spec,uint32_t line,lldb::addr_t offset)6880b57cec5SDimitry Andric SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
6890b57cec5SDimitry Andric                                      uint32_t line, lldb::addr_t offset) {
69004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec, line, offset);
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric   SBFileSpecList empty_list;
69304eeddc0SDimitry Andric   return BreakpointCreateByLocation(sb_file_spec, line, offset, empty_list);
6940b57cec5SDimitry Andric }
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric SBBreakpoint
BreakpointCreateByLocation(const SBFileSpec & sb_file_spec,uint32_t line,lldb::addr_t offset,SBFileSpecList & sb_module_list)6970b57cec5SDimitry Andric SBTarget::BreakpointCreateByLocation(const SBFileSpec &sb_file_spec,
6980b57cec5SDimitry Andric                                      uint32_t line, lldb::addr_t offset,
6990b57cec5SDimitry Andric                                      SBFileSpecList &sb_module_list) {
70004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec, line, offset, sb_module_list);
7010b57cec5SDimitry Andric 
70204eeddc0SDimitry Andric   return BreakpointCreateByLocation(sb_file_spec, line, 0, offset,
70304eeddc0SDimitry Andric                                     sb_module_list);
7040b57cec5SDimitry Andric }
7050b57cec5SDimitry Andric 
BreakpointCreateByLocation(const SBFileSpec & sb_file_spec,uint32_t line,uint32_t column,lldb::addr_t offset,SBFileSpecList & sb_module_list)7060b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByLocation(
7070b57cec5SDimitry Andric     const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
7080b57cec5SDimitry Andric     lldb::addr_t offset, SBFileSpecList &sb_module_list) {
70904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec, line, column, offset, sb_module_list);
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric   SBBreakpoint sb_bp;
7120b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
7130b57cec5SDimitry Andric   if (target_sp && line != 0) {
7140b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric     const LazyBool check_inlines = eLazyBoolCalculate;
7170b57cec5SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
7180b57cec5SDimitry Andric     const bool internal = false;
7190b57cec5SDimitry Andric     const bool hardware = false;
7200b57cec5SDimitry Andric     const LazyBool move_to_nearest_code = eLazyBoolCalculate;
7210b57cec5SDimitry Andric     const FileSpecList *module_list = nullptr;
7220b57cec5SDimitry Andric     if (sb_module_list.GetSize() > 0) {
7230b57cec5SDimitry Andric       module_list = sb_module_list.get();
7240b57cec5SDimitry Andric     }
7250b57cec5SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(
7260b57cec5SDimitry Andric         module_list, *sb_file_spec, line, column, offset, check_inlines,
7270b57cec5SDimitry Andric         skip_prologue, internal, hardware, move_to_nearest_code);
7280b57cec5SDimitry Andric   }
7290b57cec5SDimitry Andric 
73004eeddc0SDimitry Andric   return sb_bp;
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric 
BreakpointCreateByLocation(const SBFileSpec & sb_file_spec,uint32_t line,uint32_t column,lldb::addr_t offset,SBFileSpecList & sb_module_list,bool move_to_nearest_code)733e8d8bef9SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByLocation(
734e8d8bef9SDimitry Andric     const SBFileSpec &sb_file_spec, uint32_t line, uint32_t column,
735e8d8bef9SDimitry Andric     lldb::addr_t offset, SBFileSpecList &sb_module_list,
736e8d8bef9SDimitry Andric     bool move_to_nearest_code) {
73704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec, line, column, offset, sb_module_list,
738e8d8bef9SDimitry Andric                      move_to_nearest_code);
739e8d8bef9SDimitry Andric 
740e8d8bef9SDimitry Andric   SBBreakpoint sb_bp;
741e8d8bef9SDimitry Andric   TargetSP target_sp(GetSP());
742e8d8bef9SDimitry Andric   if (target_sp && line != 0) {
743e8d8bef9SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
744e8d8bef9SDimitry Andric 
745e8d8bef9SDimitry Andric     const LazyBool check_inlines = eLazyBoolCalculate;
746e8d8bef9SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
747e8d8bef9SDimitry Andric     const bool internal = false;
748e8d8bef9SDimitry Andric     const bool hardware = false;
749e8d8bef9SDimitry Andric     const FileSpecList *module_list = nullptr;
750e8d8bef9SDimitry Andric     if (sb_module_list.GetSize() > 0) {
751e8d8bef9SDimitry Andric       module_list = sb_module_list.get();
752e8d8bef9SDimitry Andric     }
753e8d8bef9SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(
754e8d8bef9SDimitry Andric         module_list, *sb_file_spec, line, column, offset, check_inlines,
755e8d8bef9SDimitry Andric         skip_prologue, internal, hardware,
756e8d8bef9SDimitry Andric         move_to_nearest_code ? eLazyBoolYes : eLazyBoolNo);
757e8d8bef9SDimitry Andric   }
758e8d8bef9SDimitry Andric 
75904eeddc0SDimitry Andric   return sb_bp;
760e8d8bef9SDimitry Andric }
761e8d8bef9SDimitry Andric 
BreakpointCreateByName(const char * symbol_name,const char * module_name)7620b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByName(const char *symbol_name,
7630b57cec5SDimitry Andric                                               const char *module_name) {
76404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name, module_name);
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric   SBBreakpoint sb_bp;
7670b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
7680b57cec5SDimitry Andric   if (target_sp.get()) {
7690b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric     const bool internal = false;
7720b57cec5SDimitry Andric     const bool hardware = false;
7730b57cec5SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
7740b57cec5SDimitry Andric     const lldb::addr_t offset = 0;
7750b57cec5SDimitry Andric     if (module_name && module_name[0]) {
7760b57cec5SDimitry Andric       FileSpecList module_spec_list;
7770b57cec5SDimitry Andric       module_spec_list.Append(FileSpec(module_name));
7780b57cec5SDimitry Andric       sb_bp = target_sp->CreateBreakpoint(
7790b57cec5SDimitry Andric           &module_spec_list, nullptr, symbol_name, eFunctionNameTypeAuto,
7800b57cec5SDimitry Andric           eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
7810b57cec5SDimitry Andric     } else {
7820b57cec5SDimitry Andric       sb_bp = target_sp->CreateBreakpoint(
7830b57cec5SDimitry Andric           nullptr, nullptr, symbol_name, eFunctionNameTypeAuto,
7840b57cec5SDimitry Andric           eLanguageTypeUnknown, offset, skip_prologue, internal, hardware);
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric   }
7870b57cec5SDimitry Andric 
78804eeddc0SDimitry Andric   return sb_bp;
7890b57cec5SDimitry Andric }
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric lldb::SBBreakpoint
BreakpointCreateByName(const char * symbol_name,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)7920b57cec5SDimitry Andric SBTarget::BreakpointCreateByName(const char *symbol_name,
7930b57cec5SDimitry Andric                                  const SBFileSpecList &module_list,
7940b57cec5SDimitry Andric                                  const SBFileSpecList &comp_unit_list) {
79504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name, module_list, comp_unit_list);
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   lldb::FunctionNameType name_type_mask = eFunctionNameTypeAuto;
79804eeddc0SDimitry Andric   return BreakpointCreateByName(symbol_name, name_type_mask,
79904eeddc0SDimitry Andric                                 eLanguageTypeUnknown, module_list,
80004eeddc0SDimitry Andric                                 comp_unit_list);
8010b57cec5SDimitry Andric }
8020b57cec5SDimitry Andric 
BreakpointCreateByName(const char * symbol_name,uint32_t name_type_mask,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8030b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
8040b57cec5SDimitry Andric     const char *symbol_name, uint32_t name_type_mask,
8050b57cec5SDimitry Andric     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
80604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name, name_type_mask, module_list,
80704eeddc0SDimitry Andric                      comp_unit_list);
8080b57cec5SDimitry Andric 
80904eeddc0SDimitry Andric   return BreakpointCreateByName(symbol_name, name_type_mask,
81004eeddc0SDimitry Andric                                 eLanguageTypeUnknown, module_list,
81104eeddc0SDimitry Andric                                 comp_unit_list);
8120b57cec5SDimitry Andric }
8130b57cec5SDimitry Andric 
BreakpointCreateByName(const char * symbol_name,uint32_t name_type_mask,LanguageType symbol_language,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8140b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByName(
8150b57cec5SDimitry Andric     const char *symbol_name, uint32_t name_type_mask,
8160b57cec5SDimitry Andric     LanguageType symbol_language, const SBFileSpecList &module_list,
8170b57cec5SDimitry Andric     const SBFileSpecList &comp_unit_list) {
81804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name, name_type_mask, symbol_language,
81904eeddc0SDimitry Andric                      module_list, comp_unit_list);
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric   SBBreakpoint sb_bp;
8220b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
8230b57cec5SDimitry Andric   if (target_sp && symbol_name && symbol_name[0]) {
8240b57cec5SDimitry Andric     const bool internal = false;
8250b57cec5SDimitry Andric     const bool hardware = false;
8260b57cec5SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
8270b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
8280b57cec5SDimitry Andric     FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
8290b57cec5SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(module_list.get(), comp_unit_list.get(),
8300b57cec5SDimitry Andric                                         symbol_name, mask, symbol_language, 0,
8310b57cec5SDimitry Andric                                         skip_prologue, internal, hardware);
8320b57cec5SDimitry Andric   }
8330b57cec5SDimitry Andric 
83404eeddc0SDimitry Andric   return sb_bp;
8350b57cec5SDimitry Andric }
8360b57cec5SDimitry Andric 
BreakpointCreateByNames(const char * symbol_names[],uint32_t num_names,uint32_t name_type_mask,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8370b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
8380b57cec5SDimitry Andric     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
8390b57cec5SDimitry Andric     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
84004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask, module_list,
84104eeddc0SDimitry Andric                      comp_unit_list);
8420b57cec5SDimitry Andric 
84304eeddc0SDimitry Andric   return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
84404eeddc0SDimitry Andric                                  eLanguageTypeUnknown, module_list,
84504eeddc0SDimitry Andric                                  comp_unit_list);
8460b57cec5SDimitry Andric }
8470b57cec5SDimitry Andric 
BreakpointCreateByNames(const char * symbol_names[],uint32_t num_names,uint32_t name_type_mask,LanguageType symbol_language,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8480b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
8490b57cec5SDimitry Andric     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
8500b57cec5SDimitry Andric     LanguageType symbol_language, const SBFileSpecList &module_list,
8510b57cec5SDimitry Andric     const SBFileSpecList &comp_unit_list) {
85204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask,
85304eeddc0SDimitry Andric                      symbol_language, module_list, comp_unit_list);
8540b57cec5SDimitry Andric 
85504eeddc0SDimitry Andric   return BreakpointCreateByNames(symbol_names, num_names, name_type_mask,
85604eeddc0SDimitry Andric                                  eLanguageTypeUnknown, 0, module_list,
85704eeddc0SDimitry Andric                                  comp_unit_list);
8580b57cec5SDimitry Andric }
8590b57cec5SDimitry Andric 
BreakpointCreateByNames(const char * symbol_names[],uint32_t num_names,uint32_t name_type_mask,LanguageType symbol_language,lldb::addr_t offset,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8600b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByNames(
8610b57cec5SDimitry Andric     const char *symbol_names[], uint32_t num_names, uint32_t name_type_mask,
8620b57cec5SDimitry Andric     LanguageType symbol_language, lldb::addr_t offset,
8630b57cec5SDimitry Andric     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
86404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_names, num_names, name_type_mask,
86504eeddc0SDimitry Andric                      symbol_language, offset, module_list, comp_unit_list);
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   SBBreakpoint sb_bp;
8680b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
8690b57cec5SDimitry Andric   if (target_sp && num_names > 0) {
8700b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
8710b57cec5SDimitry Andric     const bool internal = false;
8720b57cec5SDimitry Andric     const bool hardware = false;
8730b57cec5SDimitry Andric     FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
8740b57cec5SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
8750b57cec5SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(
8760b57cec5SDimitry Andric         module_list.get(), comp_unit_list.get(), symbol_names, num_names, mask,
8770b57cec5SDimitry Andric         symbol_language, offset, skip_prologue, internal, hardware);
8780b57cec5SDimitry Andric   }
8790b57cec5SDimitry Andric 
88004eeddc0SDimitry Andric   return sb_bp;
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric 
BreakpointCreateByRegex(const char * symbol_name_regex,const char * module_name)8830b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
8840b57cec5SDimitry Andric                                                const char *module_name) {
88504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name_regex, module_name);
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   SBFileSpecList module_spec_list;
8880b57cec5SDimitry Andric   SBFileSpecList comp_unit_list;
8890b57cec5SDimitry Andric   if (module_name && module_name[0]) {
8900b57cec5SDimitry Andric     module_spec_list.Append(FileSpec(module_name));
8910b57cec5SDimitry Andric   }
89204eeddc0SDimitry Andric   return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
89304eeddc0SDimitry Andric                                  module_spec_list, comp_unit_list);
8940b57cec5SDimitry Andric }
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric lldb::SBBreakpoint
BreakpointCreateByRegex(const char * symbol_name_regex,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)8970b57cec5SDimitry Andric SBTarget::BreakpointCreateByRegex(const char *symbol_name_regex,
8980b57cec5SDimitry Andric                                   const SBFileSpecList &module_list,
8990b57cec5SDimitry Andric                                   const SBFileSpecList &comp_unit_list) {
90004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name_regex, module_list, comp_unit_list);
9010b57cec5SDimitry Andric 
90204eeddc0SDimitry Andric   return BreakpointCreateByRegex(symbol_name_regex, eLanguageTypeUnknown,
90304eeddc0SDimitry Andric                                  module_list, comp_unit_list);
9040b57cec5SDimitry Andric }
9050b57cec5SDimitry Andric 
BreakpointCreateByRegex(const char * symbol_name_regex,LanguageType symbol_language,const SBFileSpecList & module_list,const SBFileSpecList & comp_unit_list)9060b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateByRegex(
9070b57cec5SDimitry Andric     const char *symbol_name_regex, LanguageType symbol_language,
9080b57cec5SDimitry Andric     const SBFileSpecList &module_list, const SBFileSpecList &comp_unit_list) {
90904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, symbol_name_regex, symbol_language, module_list,
91004eeddc0SDimitry Andric                      comp_unit_list);
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   SBBreakpoint sb_bp;
9130b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
9140b57cec5SDimitry Andric   if (target_sp && symbol_name_regex && symbol_name_regex[0]) {
9150b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
9160b57cec5SDimitry Andric     RegularExpression regexp((llvm::StringRef(symbol_name_regex)));
9170b57cec5SDimitry Andric     const bool internal = false;
9180b57cec5SDimitry Andric     const bool hardware = false;
9190b57cec5SDimitry Andric     const LazyBool skip_prologue = eLazyBoolCalculate;
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric     sb_bp = target_sp->CreateFuncRegexBreakpoint(
9229dba64beSDimitry Andric         module_list.get(), comp_unit_list.get(), std::move(regexp),
9239dba64beSDimitry Andric         symbol_language, skip_prologue, internal, hardware);
9240b57cec5SDimitry Andric   }
9250b57cec5SDimitry Andric 
92604eeddc0SDimitry Andric   return sb_bp;
9270b57cec5SDimitry Andric }
9280b57cec5SDimitry Andric 
BreakpointCreateByAddress(addr_t address)9290b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateByAddress(addr_t address) {
93004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, address);
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   SBBreakpoint sb_bp;
9330b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
9340b57cec5SDimitry Andric   if (target_sp) {
9350b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
9360b57cec5SDimitry Andric     const bool hardware = false;
9370b57cec5SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(address, false, hardware);
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
94004eeddc0SDimitry Andric   return sb_bp;
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric 
BreakpointCreateBySBAddress(SBAddress & sb_address)9430b57cec5SDimitry Andric SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
94404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_address);
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric   SBBreakpoint sb_bp;
9470b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
9480b57cec5SDimitry Andric   if (!sb_address.IsValid()) {
94904eeddc0SDimitry Andric     return sb_bp;
9500b57cec5SDimitry Andric   }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   if (target_sp) {
9530b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
9540b57cec5SDimitry Andric     const bool hardware = false;
9550b57cec5SDimitry Andric     sb_bp = target_sp->CreateBreakpoint(sb_address.ref(), false, hardware);
9560b57cec5SDimitry Andric   }
9570b57cec5SDimitry Andric 
95804eeddc0SDimitry Andric   return sb_bp;
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric lldb::SBBreakpoint
BreakpointCreateBySourceRegex(const char * source_regex,const lldb::SBFileSpec & source_file,const char * module_name)9620b57cec5SDimitry Andric SBTarget::BreakpointCreateBySourceRegex(const char *source_regex,
9630b57cec5SDimitry Andric                                         const lldb::SBFileSpec &source_file,
9640b57cec5SDimitry Andric                                         const char *module_name) {
96504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, source_regex, source_file, module_name);
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   SBFileSpecList module_spec_list;
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric   if (module_name && module_name[0]) {
9700b57cec5SDimitry Andric     module_spec_list.Append(FileSpec(module_name));
9710b57cec5SDimitry Andric   }
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric   SBFileSpecList source_file_list;
9740b57cec5SDimitry Andric   if (source_file.IsValid()) {
9750b57cec5SDimitry Andric     source_file_list.Append(source_file);
9760b57cec5SDimitry Andric   }
9770b57cec5SDimitry Andric 
97804eeddc0SDimitry Andric   return BreakpointCreateBySourceRegex(source_regex, module_spec_list,
97904eeddc0SDimitry Andric                                        source_file_list);
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric 
BreakpointCreateBySourceRegex(const char * source_regex,const SBFileSpecList & module_list,const lldb::SBFileSpecList & source_file_list)9820b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
9830b57cec5SDimitry Andric     const char *source_regex, const SBFileSpecList &module_list,
9840b57cec5SDimitry Andric     const lldb::SBFileSpecList &source_file_list) {
98504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, source_regex, module_list, source_file_list);
9860b57cec5SDimitry Andric 
98704eeddc0SDimitry Andric   return BreakpointCreateBySourceRegex(source_regex, module_list,
98804eeddc0SDimitry Andric                                        source_file_list, SBStringList());
9890b57cec5SDimitry Andric }
9900b57cec5SDimitry Andric 
BreakpointCreateBySourceRegex(const char * source_regex,const SBFileSpecList & module_list,const lldb::SBFileSpecList & source_file_list,const SBStringList & func_names)9910b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateBySourceRegex(
9920b57cec5SDimitry Andric     const char *source_regex, const SBFileSpecList &module_list,
9930b57cec5SDimitry Andric     const lldb::SBFileSpecList &source_file_list,
9940b57cec5SDimitry Andric     const SBStringList &func_names) {
99504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, source_regex, module_list, source_file_list,
99604eeddc0SDimitry Andric                      func_names);
9970b57cec5SDimitry Andric 
9980b57cec5SDimitry Andric   SBBreakpoint sb_bp;
9990b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10000b57cec5SDimitry Andric   if (target_sp && source_regex && source_regex[0]) {
10010b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
10020b57cec5SDimitry Andric     const bool hardware = false;
10030b57cec5SDimitry Andric     const LazyBool move_to_nearest_code = eLazyBoolCalculate;
10040b57cec5SDimitry Andric     RegularExpression regexp((llvm::StringRef(source_regex)));
10050b57cec5SDimitry Andric     std::unordered_set<std::string> func_names_set;
10060b57cec5SDimitry Andric     for (size_t i = 0; i < func_names.GetSize(); i++) {
10070b57cec5SDimitry Andric       func_names_set.insert(func_names.GetStringAtIndex(i));
10080b57cec5SDimitry Andric     }
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric     sb_bp = target_sp->CreateSourceRegexBreakpoint(
10119dba64beSDimitry Andric         module_list.get(), source_file_list.get(), func_names_set,
10129dba64beSDimitry Andric         std::move(regexp), false, hardware, move_to_nearest_code);
10130b57cec5SDimitry Andric   }
10140b57cec5SDimitry Andric 
101504eeddc0SDimitry Andric   return sb_bp;
10160b57cec5SDimitry Andric }
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric lldb::SBBreakpoint
BreakpointCreateForException(lldb::LanguageType language,bool catch_bp,bool throw_bp)10190b57cec5SDimitry Andric SBTarget::BreakpointCreateForException(lldb::LanguageType language,
10200b57cec5SDimitry Andric                                        bool catch_bp, bool throw_bp) {
102104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, language, catch_bp, throw_bp);
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   SBBreakpoint sb_bp;
10240b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10250b57cec5SDimitry Andric   if (target_sp) {
10260b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
10270b57cec5SDimitry Andric     const bool hardware = false;
10280b57cec5SDimitry Andric     sb_bp = target_sp->CreateExceptionBreakpoint(language, catch_bp, throw_bp,
10290b57cec5SDimitry Andric                                                   hardware);
10300b57cec5SDimitry Andric   }
10310b57cec5SDimitry Andric 
103204eeddc0SDimitry Andric   return sb_bp;
10330b57cec5SDimitry Andric }
10340b57cec5SDimitry Andric 
BreakpointCreateFromScript(const char * class_name,SBStructuredData & extra_args,const SBFileSpecList & module_list,const SBFileSpecList & file_list,bool request_hardware)10350b57cec5SDimitry Andric lldb::SBBreakpoint SBTarget::BreakpointCreateFromScript(
10360b57cec5SDimitry Andric     const char *class_name, SBStructuredData &extra_args,
10370b57cec5SDimitry Andric     const SBFileSpecList &module_list, const SBFileSpecList &file_list,
10380b57cec5SDimitry Andric     bool request_hardware) {
103904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, class_name, extra_args, module_list, file_list,
104004eeddc0SDimitry Andric                      request_hardware);
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   SBBreakpoint sb_bp;
10430b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10440b57cec5SDimitry Andric   if (target_sp) {
10450b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
10460b57cec5SDimitry Andric     Status error;
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric     StructuredData::ObjectSP obj_sp = extra_args.m_impl_up->GetObjectSP();
10490b57cec5SDimitry Andric     sb_bp =
10500b57cec5SDimitry Andric         target_sp->CreateScriptedBreakpoint(class_name,
10510b57cec5SDimitry Andric                                             module_list.get(),
10520b57cec5SDimitry Andric                                             file_list.get(),
10530b57cec5SDimitry Andric                                             false, /* internal */
10540b57cec5SDimitry Andric                                             request_hardware,
10550b57cec5SDimitry Andric                                             obj_sp,
10560b57cec5SDimitry Andric                                             &error);
10570b57cec5SDimitry Andric   }
10580b57cec5SDimitry Andric 
105904eeddc0SDimitry Andric   return sb_bp;
10600b57cec5SDimitry Andric }
10610b57cec5SDimitry Andric 
GetNumBreakpoints() const10620b57cec5SDimitry Andric uint32_t SBTarget::GetNumBreakpoints() const {
106304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10660b57cec5SDimitry Andric   if (target_sp) {
10670b57cec5SDimitry Andric     // The breakpoint list is thread safe, no need to lock
10680b57cec5SDimitry Andric     return target_sp->GetBreakpointList().GetSize();
10690b57cec5SDimitry Andric   }
10700b57cec5SDimitry Andric   return 0;
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric 
GetBreakpointAtIndex(uint32_t idx) const10730b57cec5SDimitry Andric SBBreakpoint SBTarget::GetBreakpointAtIndex(uint32_t idx) const {
107404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric   SBBreakpoint sb_breakpoint;
10770b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10780b57cec5SDimitry Andric   if (target_sp) {
10790b57cec5SDimitry Andric     // The breakpoint list is thread safe, no need to lock
10800b57cec5SDimitry Andric     sb_breakpoint = target_sp->GetBreakpointList().GetBreakpointAtIndex(idx);
10810b57cec5SDimitry Andric   }
108204eeddc0SDimitry Andric   return sb_breakpoint;
10830b57cec5SDimitry Andric }
10840b57cec5SDimitry Andric 
BreakpointDelete(break_id_t bp_id)10850b57cec5SDimitry Andric bool SBTarget::BreakpointDelete(break_id_t bp_id) {
108604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, bp_id);
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   bool result = false;
10890b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
10900b57cec5SDimitry Andric   if (target_sp) {
10910b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
10920b57cec5SDimitry Andric     result = target_sp->RemoveBreakpointByID(bp_id);
10930b57cec5SDimitry Andric   }
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric   return result;
10960b57cec5SDimitry Andric }
10970b57cec5SDimitry Andric 
FindBreakpointByID(break_id_t bp_id)10980b57cec5SDimitry Andric SBBreakpoint SBTarget::FindBreakpointByID(break_id_t bp_id) {
109904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, bp_id);
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   SBBreakpoint sb_breakpoint;
11020b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11030b57cec5SDimitry Andric   if (target_sp && bp_id != LLDB_INVALID_BREAK_ID) {
11040b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11050b57cec5SDimitry Andric     sb_breakpoint = target_sp->GetBreakpointByID(bp_id);
11060b57cec5SDimitry Andric   }
11070b57cec5SDimitry Andric 
110804eeddc0SDimitry Andric   return sb_breakpoint;
11090b57cec5SDimitry Andric }
11100b57cec5SDimitry Andric 
FindBreakpointsByName(const char * name,SBBreakpointList & bkpts)11110b57cec5SDimitry Andric bool SBTarget::FindBreakpointsByName(const char *name,
11120b57cec5SDimitry Andric                                      SBBreakpointList &bkpts) {
111304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, bkpts);
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11160b57cec5SDimitry Andric   if (target_sp) {
11170b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
1118480093f4SDimitry Andric     llvm::Expected<std::vector<BreakpointSP>> expected_vector =
1119480093f4SDimitry Andric         target_sp->GetBreakpointList().FindBreakpointsByName(name);
1120480093f4SDimitry Andric     if (!expected_vector) {
1121fe013be4SDimitry Andric       LLDB_LOG_ERROR(GetLog(LLDBLog::Breakpoints), expected_vector.takeError(),
1122fe013be4SDimitry Andric                      "invalid breakpoint name: {0}");
11230b57cec5SDimitry Andric       return false;
1124480093f4SDimitry Andric     }
1125480093f4SDimitry Andric     for (BreakpointSP bkpt_sp : *expected_vector) {
11260b57cec5SDimitry Andric       bkpts.AppendByID(bkpt_sp->GetID());
11270b57cec5SDimitry Andric     }
11280b57cec5SDimitry Andric   }
11290b57cec5SDimitry Andric   return true;
11300b57cec5SDimitry Andric }
11310b57cec5SDimitry Andric 
GetBreakpointNames(SBStringList & names)11320b57cec5SDimitry Andric void SBTarget::GetBreakpointNames(SBStringList &names) {
113304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, names);
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric   names.Clear();
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11380b57cec5SDimitry Andric   if (target_sp) {
11390b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric     std::vector<std::string> name_vec;
11420b57cec5SDimitry Andric     target_sp->GetBreakpointNames(name_vec);
11430b57cec5SDimitry Andric     for (auto name : name_vec)
11440b57cec5SDimitry Andric       names.AppendString(name.c_str());
11450b57cec5SDimitry Andric   }
11460b57cec5SDimitry Andric }
11470b57cec5SDimitry Andric 
DeleteBreakpointName(const char * name)11480b57cec5SDimitry Andric void SBTarget::DeleteBreakpointName(const char *name) {
114904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name);
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11520b57cec5SDimitry Andric   if (target_sp) {
11530b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11540b57cec5SDimitry Andric     target_sp->DeleteBreakpointName(ConstString(name));
11550b57cec5SDimitry Andric   }
11560b57cec5SDimitry Andric }
11570b57cec5SDimitry Andric 
EnableAllBreakpoints()11580b57cec5SDimitry Andric bool SBTarget::EnableAllBreakpoints() {
115904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11620b57cec5SDimitry Andric   if (target_sp) {
11630b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11640b57cec5SDimitry Andric     target_sp->EnableAllowedBreakpoints();
11650b57cec5SDimitry Andric     return true;
11660b57cec5SDimitry Andric   }
11670b57cec5SDimitry Andric   return false;
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric 
DisableAllBreakpoints()11700b57cec5SDimitry Andric bool SBTarget::DisableAllBreakpoints() {
117104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11740b57cec5SDimitry Andric   if (target_sp) {
11750b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11760b57cec5SDimitry Andric     target_sp->DisableAllowedBreakpoints();
11770b57cec5SDimitry Andric     return true;
11780b57cec5SDimitry Andric   }
11790b57cec5SDimitry Andric   return false;
11800b57cec5SDimitry Andric }
11810b57cec5SDimitry Andric 
DeleteAllBreakpoints()11820b57cec5SDimitry Andric bool SBTarget::DeleteAllBreakpoints() {
118304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
11860b57cec5SDimitry Andric   if (target_sp) {
11870b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
11880b57cec5SDimitry Andric     target_sp->RemoveAllowedBreakpoints();
11890b57cec5SDimitry Andric     return true;
11900b57cec5SDimitry Andric   }
11910b57cec5SDimitry Andric   return false;
11920b57cec5SDimitry Andric }
11930b57cec5SDimitry Andric 
BreakpointsCreateFromFile(SBFileSpec & source_file,SBBreakpointList & new_bps)11940b57cec5SDimitry Andric lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
11950b57cec5SDimitry Andric                                                   SBBreakpointList &new_bps) {
119604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, source_file, new_bps);
11970b57cec5SDimitry Andric 
11980b57cec5SDimitry Andric   SBStringList empty_name_list;
119904eeddc0SDimitry Andric   return BreakpointsCreateFromFile(source_file, empty_name_list, new_bps);
12000b57cec5SDimitry Andric }
12010b57cec5SDimitry Andric 
BreakpointsCreateFromFile(SBFileSpec & source_file,SBStringList & matching_names,SBBreakpointList & new_bps)12020b57cec5SDimitry Andric lldb::SBError SBTarget::BreakpointsCreateFromFile(SBFileSpec &source_file,
12030b57cec5SDimitry Andric                                                   SBStringList &matching_names,
12040b57cec5SDimitry Andric                                                   SBBreakpointList &new_bps) {
120504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, source_file, matching_names, new_bps);
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric   SBError sberr;
12080b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12090b57cec5SDimitry Andric   if (!target_sp) {
12100b57cec5SDimitry Andric     sberr.SetErrorString(
12110b57cec5SDimitry Andric         "BreakpointCreateFromFile called with invalid target.");
121204eeddc0SDimitry Andric     return sberr;
12130b57cec5SDimitry Andric   }
12140b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
12150b57cec5SDimitry Andric 
12160b57cec5SDimitry Andric   BreakpointIDList bp_ids;
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric   std::vector<std::string> name_vector;
12190b57cec5SDimitry Andric   size_t num_names = matching_names.GetSize();
12200b57cec5SDimitry Andric   for (size_t i = 0; i < num_names; i++)
12210b57cec5SDimitry Andric     name_vector.push_back(matching_names.GetStringAtIndex(i));
12220b57cec5SDimitry Andric 
12230b57cec5SDimitry Andric   sberr.ref() = target_sp->CreateBreakpointsFromFile(source_file.ref(),
12240b57cec5SDimitry Andric                                                      name_vector, bp_ids);
12250b57cec5SDimitry Andric   if (sberr.Fail())
122604eeddc0SDimitry Andric     return sberr;
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric   size_t num_bkpts = bp_ids.GetSize();
12290b57cec5SDimitry Andric   for (size_t i = 0; i < num_bkpts; i++) {
12300b57cec5SDimitry Andric     BreakpointID bp_id = bp_ids.GetBreakpointIDAtIndex(i);
12310b57cec5SDimitry Andric     new_bps.AppendByID(bp_id.GetBreakpointID());
12320b57cec5SDimitry Andric   }
123304eeddc0SDimitry Andric   return sberr;
12340b57cec5SDimitry Andric }
12350b57cec5SDimitry Andric 
BreakpointsWriteToFile(SBFileSpec & dest_file)12360b57cec5SDimitry Andric lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file) {
123704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, dest_file);
12380b57cec5SDimitry Andric 
12390b57cec5SDimitry Andric   SBError sberr;
12400b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12410b57cec5SDimitry Andric   if (!target_sp) {
12420b57cec5SDimitry Andric     sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
124304eeddc0SDimitry Andric     return sberr;
12440b57cec5SDimitry Andric   }
12450b57cec5SDimitry Andric   SBBreakpointList bkpt_list(*this);
124604eeddc0SDimitry Andric   return BreakpointsWriteToFile(dest_file, bkpt_list);
12470b57cec5SDimitry Andric }
12480b57cec5SDimitry Andric 
BreakpointsWriteToFile(SBFileSpec & dest_file,SBBreakpointList & bkpt_list,bool append)12490b57cec5SDimitry Andric lldb::SBError SBTarget::BreakpointsWriteToFile(SBFileSpec &dest_file,
12500b57cec5SDimitry Andric                                                SBBreakpointList &bkpt_list,
12510b57cec5SDimitry Andric                                                bool append) {
125204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, dest_file, bkpt_list, append);
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric   SBError sberr;
12550b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12560b57cec5SDimitry Andric   if (!target_sp) {
12570b57cec5SDimitry Andric     sberr.SetErrorString("BreakpointWriteToFile called with invalid target.");
125804eeddc0SDimitry Andric     return sberr;
12590b57cec5SDimitry Andric   }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
12620b57cec5SDimitry Andric   BreakpointIDList bp_id_list;
12630b57cec5SDimitry Andric   bkpt_list.CopyToBreakpointIDList(bp_id_list);
12640b57cec5SDimitry Andric   sberr.ref() = target_sp->SerializeBreakpointsToFile(dest_file.ref(),
12650b57cec5SDimitry Andric                                                       bp_id_list, append);
126604eeddc0SDimitry Andric   return sberr;
12670b57cec5SDimitry Andric }
12680b57cec5SDimitry Andric 
GetNumWatchpoints() const12690b57cec5SDimitry Andric uint32_t SBTarget::GetNumWatchpoints() const {
127004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12730b57cec5SDimitry Andric   if (target_sp) {
12740b57cec5SDimitry Andric     // The watchpoint list is thread safe, no need to lock
12750b57cec5SDimitry Andric     return target_sp->GetWatchpointList().GetSize();
12760b57cec5SDimitry Andric   }
12770b57cec5SDimitry Andric   return 0;
12780b57cec5SDimitry Andric }
12790b57cec5SDimitry Andric 
GetWatchpointAtIndex(uint32_t idx) const12800b57cec5SDimitry Andric SBWatchpoint SBTarget::GetWatchpointAtIndex(uint32_t idx) const {
128104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric   SBWatchpoint sb_watchpoint;
12840b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12850b57cec5SDimitry Andric   if (target_sp) {
12860b57cec5SDimitry Andric     // The watchpoint list is thread safe, no need to lock
12870b57cec5SDimitry Andric     sb_watchpoint.SetSP(target_sp->GetWatchpointList().GetByIndex(idx));
12880b57cec5SDimitry Andric   }
128904eeddc0SDimitry Andric   return sb_watchpoint;
12900b57cec5SDimitry Andric }
12910b57cec5SDimitry Andric 
DeleteWatchpoint(watch_id_t wp_id)12920b57cec5SDimitry Andric bool SBTarget::DeleteWatchpoint(watch_id_t wp_id) {
129304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, wp_id);
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric   bool result = false;
12960b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
12970b57cec5SDimitry Andric   if (target_sp) {
12980b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
12990b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
13000b57cec5SDimitry Andric     target_sp->GetWatchpointList().GetListMutex(lock);
13010b57cec5SDimitry Andric     result = target_sp->RemoveWatchpointByID(wp_id);
13020b57cec5SDimitry Andric   }
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric   return result;
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric 
FindWatchpointByID(lldb::watch_id_t wp_id)13070b57cec5SDimitry Andric SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
130804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, wp_id);
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric   SBWatchpoint sb_watchpoint;
13110b57cec5SDimitry Andric   lldb::WatchpointSP watchpoint_sp;
13120b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
13130b57cec5SDimitry Andric   if (target_sp && wp_id != LLDB_INVALID_WATCH_ID) {
13140b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
13150b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
13160b57cec5SDimitry Andric     target_sp->GetWatchpointList().GetListMutex(lock);
13170b57cec5SDimitry Andric     watchpoint_sp = target_sp->GetWatchpointList().FindByID(wp_id);
13180b57cec5SDimitry Andric     sb_watchpoint.SetSP(watchpoint_sp);
13190b57cec5SDimitry Andric   }
13200b57cec5SDimitry Andric 
132104eeddc0SDimitry Andric   return sb_watchpoint;
13220b57cec5SDimitry Andric }
13230b57cec5SDimitry Andric 
WatchAddress(lldb::addr_t addr,size_t size,bool read,bool modify,SBError & error)13240b57cec5SDimitry Andric lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
1325*c9157d92SDimitry Andric                                           bool read, bool modify,
13260b57cec5SDimitry Andric                                           SBError &error) {
132704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, addr, size, read, write, error);
13280b57cec5SDimitry Andric 
1329*c9157d92SDimitry Andric   SBWatchpointOptions options;
1330*c9157d92SDimitry Andric   options.SetWatchpointTypeRead(read);
1331*c9157d92SDimitry Andric   options.SetWatchpointTypeWrite(eWatchpointWriteTypeOnModify);
1332*c9157d92SDimitry Andric   return WatchpointCreateByAddress(addr, size, options, error);
1333*c9157d92SDimitry Andric }
1334*c9157d92SDimitry Andric 
1335*c9157d92SDimitry Andric lldb::SBWatchpoint
WatchpointCreateByAddress(lldb::addr_t addr,size_t size,SBWatchpointOptions options,SBError & error)1336*c9157d92SDimitry Andric SBTarget::WatchpointCreateByAddress(lldb::addr_t addr, size_t size,
1337*c9157d92SDimitry Andric                                     SBWatchpointOptions options,
1338*c9157d92SDimitry Andric                                     SBError &error) {
1339*c9157d92SDimitry Andric   LLDB_INSTRUMENT_VA(this, addr, size, options, error);
1340*c9157d92SDimitry Andric 
13410b57cec5SDimitry Andric   SBWatchpoint sb_watchpoint;
13420b57cec5SDimitry Andric   lldb::WatchpointSP watchpoint_sp;
13430b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
13440b57cec5SDimitry Andric   uint32_t watch_type = 0;
1345*c9157d92SDimitry Andric   if (options.GetWatchpointTypeRead())
13460b57cec5SDimitry Andric     watch_type |= LLDB_WATCH_TYPE_READ;
1347*c9157d92SDimitry Andric   if (options.GetWatchpointTypeWrite() == eWatchpointWriteTypeAlways)
13480b57cec5SDimitry Andric     watch_type |= LLDB_WATCH_TYPE_WRITE;
1349*c9157d92SDimitry Andric   if (options.GetWatchpointTypeWrite() == eWatchpointWriteTypeOnModify)
1350*c9157d92SDimitry Andric     watch_type |= LLDB_WATCH_TYPE_MODIFY;
13510b57cec5SDimitry Andric   if (watch_type == 0) {
1352*c9157d92SDimitry Andric     error.SetErrorString("Can't create a watchpoint that is neither read nor "
1353*c9157d92SDimitry Andric                          "write nor modify.");
135404eeddc0SDimitry Andric     return sb_watchpoint;
13550b57cec5SDimitry Andric   }
1356*c9157d92SDimitry Andric   if (target_sp && addr != LLDB_INVALID_ADDRESS && size > 0) {
1357*c9157d92SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
13580b57cec5SDimitry Andric     // Target::CreateWatchpoint() is thread safe.
13590b57cec5SDimitry Andric     Status cw_error;
13600b57cec5SDimitry Andric     // This API doesn't take in a type, so we can't figure out what it is.
13610b57cec5SDimitry Andric     CompilerType *type = nullptr;
13620b57cec5SDimitry Andric     watchpoint_sp =
13630b57cec5SDimitry Andric         target_sp->CreateWatchpoint(addr, size, type, watch_type, cw_error);
13640b57cec5SDimitry Andric     error.SetError(cw_error);
13650b57cec5SDimitry Andric     sb_watchpoint.SetSP(watchpoint_sp);
13660b57cec5SDimitry Andric   }
13670b57cec5SDimitry Andric 
136804eeddc0SDimitry Andric   return sb_watchpoint;
13690b57cec5SDimitry Andric }
13700b57cec5SDimitry Andric 
EnableAllWatchpoints()13710b57cec5SDimitry Andric bool SBTarget::EnableAllWatchpoints() {
137204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
13750b57cec5SDimitry Andric   if (target_sp) {
13760b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
13770b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
13780b57cec5SDimitry Andric     target_sp->GetWatchpointList().GetListMutex(lock);
13790b57cec5SDimitry Andric     target_sp->EnableAllWatchpoints();
13800b57cec5SDimitry Andric     return true;
13810b57cec5SDimitry Andric   }
13820b57cec5SDimitry Andric   return false;
13830b57cec5SDimitry Andric }
13840b57cec5SDimitry Andric 
DisableAllWatchpoints()13850b57cec5SDimitry Andric bool SBTarget::DisableAllWatchpoints() {
138604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
13890b57cec5SDimitry Andric   if (target_sp) {
13900b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
13910b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
13920b57cec5SDimitry Andric     target_sp->GetWatchpointList().GetListMutex(lock);
13930b57cec5SDimitry Andric     target_sp->DisableAllWatchpoints();
13940b57cec5SDimitry Andric     return true;
13950b57cec5SDimitry Andric   }
13960b57cec5SDimitry Andric   return false;
13970b57cec5SDimitry Andric }
13980b57cec5SDimitry Andric 
CreateValueFromAddress(const char * name,SBAddress addr,SBType type)13990b57cec5SDimitry Andric SBValue SBTarget::CreateValueFromAddress(const char *name, SBAddress addr,
14000b57cec5SDimitry Andric                                          SBType type) {
140104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, addr, type);
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric   SBValue sb_value;
14040b57cec5SDimitry Andric   lldb::ValueObjectSP new_value_sp;
14050b57cec5SDimitry Andric   if (IsValid() && name && *name && addr.IsValid() && type.IsValid()) {
14060b57cec5SDimitry Andric     lldb::addr_t load_addr(addr.GetLoadAddress(*this));
14070b57cec5SDimitry Andric     ExecutionContext exe_ctx(
14080b57cec5SDimitry Andric         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
14090b57cec5SDimitry Andric     CompilerType ast_type(type.GetSP()->GetCompilerType(true));
14100b57cec5SDimitry Andric     new_value_sp = ValueObject::CreateValueObjectFromAddress(name, load_addr,
14110b57cec5SDimitry Andric                                                              exe_ctx, ast_type);
14120b57cec5SDimitry Andric   }
14130b57cec5SDimitry Andric   sb_value.SetSP(new_value_sp);
141404eeddc0SDimitry Andric   return sb_value;
14150b57cec5SDimitry Andric }
14160b57cec5SDimitry Andric 
CreateValueFromData(const char * name,lldb::SBData data,lldb::SBType type)14170b57cec5SDimitry Andric lldb::SBValue SBTarget::CreateValueFromData(const char *name, lldb::SBData data,
14180b57cec5SDimitry Andric                                             lldb::SBType type) {
141904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, data, type);
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   SBValue sb_value;
14220b57cec5SDimitry Andric   lldb::ValueObjectSP new_value_sp;
14230b57cec5SDimitry Andric   if (IsValid() && name && *name && data.IsValid() && type.IsValid()) {
14240b57cec5SDimitry Andric     DataExtractorSP extractor(*data);
14250b57cec5SDimitry Andric     ExecutionContext exe_ctx(
14260b57cec5SDimitry Andric         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
14270b57cec5SDimitry Andric     CompilerType ast_type(type.GetSP()->GetCompilerType(true));
14280b57cec5SDimitry Andric     new_value_sp = ValueObject::CreateValueObjectFromData(name, *extractor,
14290b57cec5SDimitry Andric                                                           exe_ctx, ast_type);
14300b57cec5SDimitry Andric   }
14310b57cec5SDimitry Andric   sb_value.SetSP(new_value_sp);
143204eeddc0SDimitry Andric   return sb_value;
14330b57cec5SDimitry Andric }
14340b57cec5SDimitry Andric 
CreateValueFromExpression(const char * name,const char * expr)14350b57cec5SDimitry Andric lldb::SBValue SBTarget::CreateValueFromExpression(const char *name,
14360b57cec5SDimitry Andric                                                   const char *expr) {
143704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, expr);
14380b57cec5SDimitry Andric 
14390b57cec5SDimitry Andric   SBValue sb_value;
14400b57cec5SDimitry Andric   lldb::ValueObjectSP new_value_sp;
14410b57cec5SDimitry Andric   if (IsValid() && name && *name && expr && *expr) {
14420b57cec5SDimitry Andric     ExecutionContext exe_ctx(
14430b57cec5SDimitry Andric         ExecutionContextRef(ExecutionContext(m_opaque_sp.get(), false)));
14440b57cec5SDimitry Andric     new_value_sp =
14450b57cec5SDimitry Andric         ValueObject::CreateValueObjectFromExpression(name, expr, exe_ctx);
14460b57cec5SDimitry Andric   }
14470b57cec5SDimitry Andric   sb_value.SetSP(new_value_sp);
144804eeddc0SDimitry Andric   return sb_value;
14490b57cec5SDimitry Andric }
14500b57cec5SDimitry Andric 
DeleteAllWatchpoints()14510b57cec5SDimitry Andric bool SBTarget::DeleteAllWatchpoints() {
145204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
14550b57cec5SDimitry Andric   if (target_sp) {
14560b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
14570b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> lock;
14580b57cec5SDimitry Andric     target_sp->GetWatchpointList().GetListMutex(lock);
14590b57cec5SDimitry Andric     target_sp->RemoveAllWatchpoints();
14600b57cec5SDimitry Andric     return true;
14610b57cec5SDimitry Andric   }
14620b57cec5SDimitry Andric   return false;
14630b57cec5SDimitry Andric }
14640b57cec5SDimitry Andric 
AppendImageSearchPath(const char * from,const char * to,lldb::SBError & error)14650b57cec5SDimitry Andric void SBTarget::AppendImageSearchPath(const char *from, const char *to,
14660b57cec5SDimitry Andric                                      lldb::SBError &error) {
146704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, from, to, error);
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
14700b57cec5SDimitry Andric   if (!target_sp)
14710b57cec5SDimitry Andric     return error.SetErrorString("invalid target");
14720b57cec5SDimitry Andric 
1473349cc55cSDimitry Andric   llvm::StringRef srFrom = from, srTo = to;
1474349cc55cSDimitry Andric   if (srFrom.empty())
14750b57cec5SDimitry Andric     return error.SetErrorString("<from> path can't be empty");
1476349cc55cSDimitry Andric   if (srTo.empty())
14770b57cec5SDimitry Andric     return error.SetErrorString("<to> path can't be empty");
14780b57cec5SDimitry Andric 
1479349cc55cSDimitry Andric   target_sp->GetImageSearchPathList().Append(srFrom, srTo, true);
14800b57cec5SDimitry Andric }
14810b57cec5SDimitry Andric 
AddModule(const char * path,const char * triple,const char * uuid_cstr)14820b57cec5SDimitry Andric lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
14830b57cec5SDimitry Andric                                    const char *uuid_cstr) {
148404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, path, triple, uuid_cstr);
14850b57cec5SDimitry Andric 
148604eeddc0SDimitry Andric   return AddModule(path, triple, uuid_cstr, nullptr);
14870b57cec5SDimitry Andric }
14880b57cec5SDimitry Andric 
AddModule(const char * path,const char * triple,const char * uuid_cstr,const char * symfile)14890b57cec5SDimitry Andric lldb::SBModule SBTarget::AddModule(const char *path, const char *triple,
14900b57cec5SDimitry Andric                                    const char *uuid_cstr, const char *symfile) {
149104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, path, triple, uuid_cstr, symfile);
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1494*c9157d92SDimitry Andric   if (!target_sp)
1495*c9157d92SDimitry Andric     return {};
1496*c9157d92SDimitry Andric 
14970b57cec5SDimitry Andric   ModuleSpec module_spec;
14980b57cec5SDimitry Andric   if (path)
14990b57cec5SDimitry Andric     module_spec.GetFileSpec().SetFile(path, FileSpec::Style::native);
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric   if (uuid_cstr)
15020b57cec5SDimitry Andric     module_spec.GetUUID().SetFromStringRef(uuid_cstr);
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   if (triple)
1505*c9157d92SDimitry Andric     module_spec.GetArchitecture() =
1506*c9157d92SDimitry Andric         Platform::GetAugmentedArchSpec(target_sp->GetPlatform().get(), triple);
15070b57cec5SDimitry Andric   else
15080b57cec5SDimitry Andric     module_spec.GetArchitecture() = target_sp->GetArchitecture();
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric   if (symfile)
15110b57cec5SDimitry Andric     module_spec.GetSymbolFileSpec().SetFile(symfile, FileSpec::Style::native);
15120b57cec5SDimitry Andric 
1513*c9157d92SDimitry Andric   SBModuleSpec sb_modulespec(module_spec);
1514*c9157d92SDimitry Andric 
1515*c9157d92SDimitry Andric   return AddModule(sb_modulespec);
15160b57cec5SDimitry Andric }
15170b57cec5SDimitry Andric 
AddModule(const SBModuleSpec & module_spec)15180b57cec5SDimitry Andric lldb::SBModule SBTarget::AddModule(const SBModuleSpec &module_spec) {
151904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module_spec);
15200b57cec5SDimitry Andric 
15210b57cec5SDimitry Andric   lldb::SBModule sb_module;
15220b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1523*c9157d92SDimitry Andric   if (target_sp) {
15240b57cec5SDimitry Andric     sb_module.SetSP(target_sp->GetOrCreateModule(*module_spec.m_opaque_up,
15250b57cec5SDimitry Andric                                                  true /* notify */));
1526*c9157d92SDimitry Andric     if (!sb_module.IsValid() && module_spec.m_opaque_up->GetUUID().IsValid()) {
1527*c9157d92SDimitry Andric       Status error;
1528*c9157d92SDimitry Andric       if (PluginManager::DownloadObjectAndSymbolFile(*module_spec.m_opaque_up,
1529*c9157d92SDimitry Andric                                                      error,
1530*c9157d92SDimitry Andric                                                      /* force_lookup */ true)) {
1531*c9157d92SDimitry Andric         if (FileSystem::Instance().Exists(
1532*c9157d92SDimitry Andric                 module_spec.m_opaque_up->GetFileSpec())) {
1533*c9157d92SDimitry Andric           sb_module.SetSP(target_sp->GetOrCreateModule(*module_spec.m_opaque_up,
1534*c9157d92SDimitry Andric                                                        true /* notify */));
1535*c9157d92SDimitry Andric         }
1536*c9157d92SDimitry Andric       }
1537*c9157d92SDimitry Andric     }
1538*c9157d92SDimitry Andric   }
1539*c9157d92SDimitry Andric   // If the target hasn't initialized any architecture yet, use the
1540*c9157d92SDimitry Andric   // binary's architecture.
1541*c9157d92SDimitry Andric   if (sb_module.IsValid() && !target_sp->GetArchitecture().IsValid() &&
1542*c9157d92SDimitry Andric       sb_module.GetSP()->GetArchitecture().IsValid())
1543*c9157d92SDimitry Andric     target_sp->SetArchitecture(sb_module.GetSP()->GetArchitecture());
154404eeddc0SDimitry Andric   return sb_module;
15450b57cec5SDimitry Andric }
15460b57cec5SDimitry Andric 
AddModule(lldb::SBModule & module)15470b57cec5SDimitry Andric bool SBTarget::AddModule(lldb::SBModule &module) {
154804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module);
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
15510b57cec5SDimitry Andric   if (target_sp) {
15520b57cec5SDimitry Andric     target_sp->GetImages().AppendIfNeeded(module.GetSP());
15530b57cec5SDimitry Andric     return true;
15540b57cec5SDimitry Andric   }
15550b57cec5SDimitry Andric   return false;
15560b57cec5SDimitry Andric }
15570b57cec5SDimitry Andric 
GetNumModules() const15580b57cec5SDimitry Andric uint32_t SBTarget::GetNumModules() const {
155904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
15600b57cec5SDimitry Andric 
15610b57cec5SDimitry Andric   uint32_t num = 0;
15620b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
15630b57cec5SDimitry Andric   if (target_sp) {
15640b57cec5SDimitry Andric     // The module list is thread safe, no need to lock
15650b57cec5SDimitry Andric     num = target_sp->GetImages().GetSize();
15660b57cec5SDimitry Andric   }
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   return num;
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric 
Clear()15710b57cec5SDimitry Andric void SBTarget::Clear() {
157204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric   m_opaque_sp.reset();
15750b57cec5SDimitry Andric }
15760b57cec5SDimitry Andric 
FindModule(const SBFileSpec & sb_file_spec)15770b57cec5SDimitry Andric SBModule SBTarget::FindModule(const SBFileSpec &sb_file_spec) {
157804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec);
15790b57cec5SDimitry Andric 
15800b57cec5SDimitry Andric   SBModule sb_module;
15810b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
15820b57cec5SDimitry Andric   if (target_sp && sb_file_spec.IsValid()) {
15830b57cec5SDimitry Andric     ModuleSpec module_spec(*sb_file_spec);
15840b57cec5SDimitry Andric     // The module list is thread safe, no need to lock
15850b57cec5SDimitry Andric     sb_module.SetSP(target_sp->GetImages().FindFirstModule(module_spec));
15860b57cec5SDimitry Andric   }
158704eeddc0SDimitry Andric   return sb_module;
15880b57cec5SDimitry Andric }
15890b57cec5SDimitry Andric 
FindCompileUnits(const SBFileSpec & sb_file_spec)15900b57cec5SDimitry Andric SBSymbolContextList SBTarget::FindCompileUnits(const SBFileSpec &sb_file_spec) {
159104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_file_spec);
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric   SBSymbolContextList sb_sc_list;
15940b57cec5SDimitry Andric   const TargetSP target_sp(GetSP());
15959dba64beSDimitry Andric   if (target_sp && sb_file_spec.IsValid())
15969dba64beSDimitry Andric     target_sp->GetImages().FindCompileUnits(*sb_file_spec, *sb_sc_list);
159704eeddc0SDimitry Andric   return sb_sc_list;
15980b57cec5SDimitry Andric }
15990b57cec5SDimitry Andric 
GetByteOrder()16000b57cec5SDimitry Andric lldb::ByteOrder SBTarget::GetByteOrder() {
160104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
16040b57cec5SDimitry Andric   if (target_sp)
16050b57cec5SDimitry Andric     return target_sp->GetArchitecture().GetByteOrder();
16060b57cec5SDimitry Andric   return eByteOrderInvalid;
16070b57cec5SDimitry Andric }
16080b57cec5SDimitry Andric 
GetTriple()16090b57cec5SDimitry Andric const char *SBTarget::GetTriple() {
161004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
1613fe013be4SDimitry Andric   if (!target_sp)
1614fe013be4SDimitry Andric     return nullptr;
1615fe013be4SDimitry Andric 
16160b57cec5SDimitry Andric   std::string triple(target_sp->GetArchitecture().GetTriple().str());
16170b57cec5SDimitry Andric   // Unique the string so we don't run into ownership issues since the const
16180b57cec5SDimitry Andric   // strings put the string into the string pool once and the strings never
16190b57cec5SDimitry Andric   // comes out
16200b57cec5SDimitry Andric   ConstString const_triple(triple.c_str());
16210b57cec5SDimitry Andric   return const_triple.GetCString();
16220b57cec5SDimitry Andric }
16230b57cec5SDimitry Andric 
GetABIName()162481ad6265SDimitry Andric const char *SBTarget::GetABIName() {
162581ad6265SDimitry Andric   LLDB_INSTRUMENT_VA(this);
162681ad6265SDimitry Andric 
162781ad6265SDimitry Andric   TargetSP target_sp(GetSP());
1628fe013be4SDimitry Andric   if (!target_sp)
1629fe013be4SDimitry Andric     return nullptr;
1630fe013be4SDimitry Andric 
163181ad6265SDimitry Andric   std::string abi_name(target_sp->GetABIName().str());
163281ad6265SDimitry Andric   ConstString const_name(abi_name.c_str());
163381ad6265SDimitry Andric   return const_name.GetCString();
163481ad6265SDimitry Andric }
1635fe013be4SDimitry Andric 
GetLabel() const1636fe013be4SDimitry Andric const char *SBTarget::GetLabel() const {
1637fe013be4SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1638fe013be4SDimitry Andric 
1639fe013be4SDimitry Andric   TargetSP target_sp(GetSP());
1640fe013be4SDimitry Andric   if (!target_sp)
164181ad6265SDimitry Andric     return nullptr;
1642fe013be4SDimitry Andric 
1643fe013be4SDimitry Andric   return ConstString(target_sp->GetLabel().data()).AsCString();
1644fe013be4SDimitry Andric }
1645fe013be4SDimitry Andric 
SetLabel(const char * label)1646fe013be4SDimitry Andric SBError SBTarget::SetLabel(const char *label) {
1647fe013be4SDimitry Andric   LLDB_INSTRUMENT_VA(this, label);
1648fe013be4SDimitry Andric 
1649fe013be4SDimitry Andric   TargetSP target_sp(GetSP());
1650fe013be4SDimitry Andric   if (!target_sp)
1651fe013be4SDimitry Andric     return Status("Couldn't get internal target object.");
1652fe013be4SDimitry Andric 
1653fe013be4SDimitry Andric   return Status(target_sp->SetLabel(label));
165481ad6265SDimitry Andric }
165581ad6265SDimitry Andric 
GetDataByteSize()16560b57cec5SDimitry Andric uint32_t SBTarget::GetDataByteSize() {
165704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
16600b57cec5SDimitry Andric   if (target_sp) {
16610b57cec5SDimitry Andric     return target_sp->GetArchitecture().GetDataByteSize();
16620b57cec5SDimitry Andric   }
16630b57cec5SDimitry Andric   return 0;
16640b57cec5SDimitry Andric }
16650b57cec5SDimitry Andric 
GetCodeByteSize()16660b57cec5SDimitry Andric uint32_t SBTarget::GetCodeByteSize() {
166704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16680b57cec5SDimitry Andric 
16690b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
16700b57cec5SDimitry Andric   if (target_sp) {
16710b57cec5SDimitry Andric     return target_sp->GetArchitecture().GetCodeByteSize();
16720b57cec5SDimitry Andric   }
16730b57cec5SDimitry Andric   return 0;
16740b57cec5SDimitry Andric }
16750b57cec5SDimitry Andric 
GetMaximumNumberOfChildrenToDisplay() const16764824e7fdSDimitry Andric uint32_t SBTarget::GetMaximumNumberOfChildrenToDisplay() const {
167704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16784824e7fdSDimitry Andric 
16794824e7fdSDimitry Andric   TargetSP target_sp(GetSP());
16804824e7fdSDimitry Andric   if(target_sp){
16814824e7fdSDimitry Andric      return target_sp->GetMaximumNumberOfChildrenToDisplay();
16824824e7fdSDimitry Andric   }
16834824e7fdSDimitry Andric   return 0;
16844824e7fdSDimitry Andric }
16854824e7fdSDimitry Andric 
GetAddressByteSize()16860b57cec5SDimitry Andric uint32_t SBTarget::GetAddressByteSize() {
168704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
16880b57cec5SDimitry Andric 
16890b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
16900b57cec5SDimitry Andric   if (target_sp)
16910b57cec5SDimitry Andric     return target_sp->GetArchitecture().GetAddressByteSize();
16920b57cec5SDimitry Andric   return sizeof(void *);
16930b57cec5SDimitry Andric }
16940b57cec5SDimitry Andric 
GetModuleAtIndex(uint32_t idx)16950b57cec5SDimitry Andric SBModule SBTarget::GetModuleAtIndex(uint32_t idx) {
169604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   SBModule sb_module;
16990b57cec5SDimitry Andric   ModuleSP module_sp;
17000b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
17010b57cec5SDimitry Andric   if (target_sp) {
17020b57cec5SDimitry Andric     // The module list is thread safe, no need to lock
17030b57cec5SDimitry Andric     module_sp = target_sp->GetImages().GetModuleAtIndex(idx);
17040b57cec5SDimitry Andric     sb_module.SetSP(module_sp);
17050b57cec5SDimitry Andric   }
17060b57cec5SDimitry Andric 
170704eeddc0SDimitry Andric   return sb_module;
17080b57cec5SDimitry Andric }
17090b57cec5SDimitry Andric 
RemoveModule(lldb::SBModule module)17100b57cec5SDimitry Andric bool SBTarget::RemoveModule(lldb::SBModule module) {
171104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module);
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
17140b57cec5SDimitry Andric   if (target_sp)
17150b57cec5SDimitry Andric     return target_sp->GetImages().Remove(module.GetSP());
17160b57cec5SDimitry Andric   return false;
17170b57cec5SDimitry Andric }
17180b57cec5SDimitry Andric 
GetBroadcaster() const17190b57cec5SDimitry Andric SBBroadcaster SBTarget::GetBroadcaster() const {
172004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
17230b57cec5SDimitry Andric   SBBroadcaster broadcaster(target_sp.get(), false);
17240b57cec5SDimitry Andric 
172504eeddc0SDimitry Andric   return broadcaster;
17260b57cec5SDimitry Andric }
17270b57cec5SDimitry Andric 
GetDescription(SBStream & description,lldb::DescriptionLevel description_level)17280b57cec5SDimitry Andric bool SBTarget::GetDescription(SBStream &description,
17290b57cec5SDimitry Andric                               lldb::DescriptionLevel description_level) {
173004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, description, description_level);
17310b57cec5SDimitry Andric 
17320b57cec5SDimitry Andric   Stream &strm = description.ref();
17330b57cec5SDimitry Andric 
17340b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
17350b57cec5SDimitry Andric   if (target_sp) {
17360b57cec5SDimitry Andric     target_sp->Dump(&strm, description_level);
17370b57cec5SDimitry Andric   } else
17380b57cec5SDimitry Andric     strm.PutCString("No value");
17390b57cec5SDimitry Andric 
17400b57cec5SDimitry Andric   return true;
17410b57cec5SDimitry Andric }
17420b57cec5SDimitry Andric 
FindFunctions(const char * name,uint32_t name_type_mask)17430b57cec5SDimitry Andric lldb::SBSymbolContextList SBTarget::FindFunctions(const char *name,
17440b57cec5SDimitry Andric                                                   uint32_t name_type_mask) {
174504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, name_type_mask);
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   lldb::SBSymbolContextList sb_sc_list;
1748e8d8bef9SDimitry Andric   if (!name || !name[0])
174904eeddc0SDimitry Andric     return sb_sc_list;
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
17520b57cec5SDimitry Andric   if (!target_sp)
175304eeddc0SDimitry Andric     return sb_sc_list;
17540b57cec5SDimitry Andric 
1755349cc55cSDimitry Andric   ModuleFunctionSearchOptions function_options;
1756349cc55cSDimitry Andric   function_options.include_symbols = true;
1757349cc55cSDimitry Andric   function_options.include_inlines = true;
1758349cc55cSDimitry Andric 
17590b57cec5SDimitry Andric   FunctionNameType mask = static_cast<FunctionNameType>(name_type_mask);
1760349cc55cSDimitry Andric   target_sp->GetImages().FindFunctions(ConstString(name), mask,
1761349cc55cSDimitry Andric                                        function_options, *sb_sc_list);
176204eeddc0SDimitry Andric   return sb_sc_list;
17630b57cec5SDimitry Andric }
17640b57cec5SDimitry Andric 
FindGlobalFunctions(const char * name,uint32_t max_matches,MatchType matchtype)17650b57cec5SDimitry Andric lldb::SBSymbolContextList SBTarget::FindGlobalFunctions(const char *name,
17660b57cec5SDimitry Andric                                                         uint32_t max_matches,
17670b57cec5SDimitry Andric                                                         MatchType matchtype) {
176804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, max_matches, matchtype);
17690b57cec5SDimitry Andric 
17700b57cec5SDimitry Andric   lldb::SBSymbolContextList sb_sc_list;
17710b57cec5SDimitry Andric   if (name && name[0]) {
17720b57cec5SDimitry Andric     llvm::StringRef name_ref(name);
17730b57cec5SDimitry Andric     TargetSP target_sp(GetSP());
17740b57cec5SDimitry Andric     if (target_sp) {
1775349cc55cSDimitry Andric       ModuleFunctionSearchOptions function_options;
1776349cc55cSDimitry Andric       function_options.include_symbols = true;
1777349cc55cSDimitry Andric       function_options.include_inlines = true;
1778349cc55cSDimitry Andric 
17790b57cec5SDimitry Andric       std::string regexstr;
17800b57cec5SDimitry Andric       switch (matchtype) {
17810b57cec5SDimitry Andric       case eMatchTypeRegex:
1782349cc55cSDimitry Andric         target_sp->GetImages().FindFunctions(RegularExpression(name_ref),
1783349cc55cSDimitry Andric                                              function_options, *sb_sc_list);
17840b57cec5SDimitry Andric         break;
17850b57cec5SDimitry Andric       case eMatchTypeStartsWith:
17860b57cec5SDimitry Andric         regexstr = llvm::Regex::escape(name) + ".*";
1787349cc55cSDimitry Andric         target_sp->GetImages().FindFunctions(RegularExpression(regexstr),
1788349cc55cSDimitry Andric                                              function_options, *sb_sc_list);
17890b57cec5SDimitry Andric         break;
17900b57cec5SDimitry Andric       default:
1791349cc55cSDimitry Andric         target_sp->GetImages().FindFunctions(ConstString(name),
1792349cc55cSDimitry Andric                                              eFunctionNameTypeAny,
1793349cc55cSDimitry Andric                                              function_options, *sb_sc_list);
17940b57cec5SDimitry Andric         break;
17950b57cec5SDimitry Andric       }
17960b57cec5SDimitry Andric     }
17970b57cec5SDimitry Andric   }
179804eeddc0SDimitry Andric   return sb_sc_list;
17990b57cec5SDimitry Andric }
18000b57cec5SDimitry Andric 
FindFirstType(const char * typename_cstr)18010b57cec5SDimitry Andric lldb::SBType SBTarget::FindFirstType(const char *typename_cstr) {
180204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, typename_cstr);
18030b57cec5SDimitry Andric 
18040b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
18050b57cec5SDimitry Andric   if (typename_cstr && typename_cstr[0] && target_sp) {
18060b57cec5SDimitry Andric     ConstString const_typename(typename_cstr);
1807*c9157d92SDimitry Andric     TypeQuery query(const_typename.GetStringRef(),
1808*c9157d92SDimitry Andric                     TypeQueryOptions::e_find_one);
1809*c9157d92SDimitry Andric     TypeResults results;
1810*c9157d92SDimitry Andric     target_sp->GetImages().FindTypes(/*search_first=*/nullptr, query, results);
1811*c9157d92SDimitry Andric     TypeSP type_sp = results.GetFirstType();
18120b57cec5SDimitry Andric     if (type_sp)
181304eeddc0SDimitry Andric       return SBType(type_sp);
1814bdd1243dSDimitry Andric     // Didn't find the type in the symbols; Try the loaded language runtimes.
18150b57cec5SDimitry Andric     if (auto process_sp = target_sp->GetProcessSP()) {
18160b57cec5SDimitry Andric       for (auto *runtime : process_sp->GetLanguageRuntimes()) {
18170b57cec5SDimitry Andric         if (auto vendor = runtime->GetDeclVendor()) {
18180b57cec5SDimitry Andric           auto types = vendor->FindTypes(const_typename, /*max_matches*/ 1);
18190b57cec5SDimitry Andric           if (!types.empty())
182004eeddc0SDimitry Andric             return SBType(types.front());
18210b57cec5SDimitry Andric         }
18220b57cec5SDimitry Andric       }
18230b57cec5SDimitry Andric     }
18240b57cec5SDimitry Andric 
1825bdd1243dSDimitry Andric     // No matches, search for basic typename matches.
1826bdd1243dSDimitry Andric     for (auto type_system_sp : target_sp->GetScratchTypeSystems())
1827bdd1243dSDimitry Andric       if (auto type = type_system_sp->GetBuiltinTypeByName(const_typename))
182804eeddc0SDimitry Andric         return SBType(type);
18290b57cec5SDimitry Andric   }
18309dba64beSDimitry Andric 
183104eeddc0SDimitry Andric   return SBType();
18320b57cec5SDimitry Andric }
18330b57cec5SDimitry Andric 
GetBasicType(lldb::BasicType type)18340b57cec5SDimitry Andric SBType SBTarget::GetBasicType(lldb::BasicType type) {
183504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, type);
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
18380b57cec5SDimitry Andric   if (target_sp) {
1839bdd1243dSDimitry Andric     for (auto type_system_sp : target_sp->GetScratchTypeSystems())
1840bdd1243dSDimitry Andric       if (auto compiler_type = type_system_sp->GetBasicTypeFromAST(type))
184104eeddc0SDimitry Andric         return SBType(compiler_type);
18420b57cec5SDimitry Andric   }
184304eeddc0SDimitry Andric   return SBType();
18440b57cec5SDimitry Andric }
18450b57cec5SDimitry Andric 
FindTypes(const char * typename_cstr)18460b57cec5SDimitry Andric lldb::SBTypeList SBTarget::FindTypes(const char *typename_cstr) {
184704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, typename_cstr);
18480b57cec5SDimitry Andric 
18490b57cec5SDimitry Andric   SBTypeList sb_type_list;
18500b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
18510b57cec5SDimitry Andric   if (typename_cstr && typename_cstr[0] && target_sp) {
18520b57cec5SDimitry Andric     ModuleList &images = target_sp->GetImages();
18530b57cec5SDimitry Andric     ConstString const_typename(typename_cstr);
1854*c9157d92SDimitry Andric     TypeQuery query(typename_cstr);
1855*c9157d92SDimitry Andric     TypeResults results;
1856*c9157d92SDimitry Andric     images.FindTypes(nullptr, query, results);
1857*c9157d92SDimitry Andric     for (const TypeSP &type_sp : results.GetTypeMap().Types())
18580b57cec5SDimitry Andric       sb_type_list.Append(SBType(type_sp));
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric     // Try the loaded language runtimes
18610b57cec5SDimitry Andric     if (auto process_sp = target_sp->GetProcessSP()) {
18620b57cec5SDimitry Andric       for (auto *runtime : process_sp->GetLanguageRuntimes()) {
18630b57cec5SDimitry Andric         if (auto *vendor = runtime->GetDeclVendor()) {
18640b57cec5SDimitry Andric           auto types =
18650b57cec5SDimitry Andric               vendor->FindTypes(const_typename, /*max_matches*/ UINT32_MAX);
18660b57cec5SDimitry Andric           for (auto type : types)
18670b57cec5SDimitry Andric             sb_type_list.Append(SBType(type));
18680b57cec5SDimitry Andric         }
18690b57cec5SDimitry Andric       }
18700b57cec5SDimitry Andric     }
18710b57cec5SDimitry Andric 
18720b57cec5SDimitry Andric     if (sb_type_list.GetSize() == 0) {
18730b57cec5SDimitry Andric       // No matches, search for basic typename matches
1874bdd1243dSDimitry Andric       for (auto type_system_sp : target_sp->GetScratchTypeSystems())
18759dba64beSDimitry Andric         if (auto compiler_type =
1876bdd1243dSDimitry Andric                 type_system_sp->GetBuiltinTypeByName(const_typename))
18779dba64beSDimitry Andric           sb_type_list.Append(SBType(compiler_type));
18780b57cec5SDimitry Andric     }
18790b57cec5SDimitry Andric   }
188004eeddc0SDimitry Andric   return sb_type_list;
18810b57cec5SDimitry Andric }
18820b57cec5SDimitry Andric 
FindGlobalVariables(const char * name,uint32_t max_matches)18830b57cec5SDimitry Andric SBValueList SBTarget::FindGlobalVariables(const char *name,
18840b57cec5SDimitry Andric                                           uint32_t max_matches) {
188504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, max_matches);
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric   SBValueList sb_value_list;
18880b57cec5SDimitry Andric 
18890b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
18900b57cec5SDimitry Andric   if (name && target_sp) {
18910b57cec5SDimitry Andric     VariableList variable_list;
18929dba64beSDimitry Andric     target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches,
18939dba64beSDimitry Andric                                                variable_list);
1894480093f4SDimitry Andric     if (!variable_list.Empty()) {
18950b57cec5SDimitry Andric       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
18960b57cec5SDimitry Andric       if (exe_scope == nullptr)
18970b57cec5SDimitry Andric         exe_scope = target_sp.get();
1898480093f4SDimitry Andric       for (const VariableSP &var_sp : variable_list) {
1899480093f4SDimitry Andric         lldb::ValueObjectSP valobj_sp(
1900480093f4SDimitry Andric             ValueObjectVariable::Create(exe_scope, var_sp));
19010b57cec5SDimitry Andric         if (valobj_sp)
19020b57cec5SDimitry Andric           sb_value_list.Append(SBValue(valobj_sp));
19030b57cec5SDimitry Andric       }
19040b57cec5SDimitry Andric     }
19050b57cec5SDimitry Andric   }
19060b57cec5SDimitry Andric 
190704eeddc0SDimitry Andric   return sb_value_list;
19080b57cec5SDimitry Andric }
19090b57cec5SDimitry Andric 
FindGlobalVariables(const char * name,uint32_t max_matches,MatchType matchtype)19100b57cec5SDimitry Andric SBValueList SBTarget::FindGlobalVariables(const char *name,
19110b57cec5SDimitry Andric                                           uint32_t max_matches,
19120b57cec5SDimitry Andric                                           MatchType matchtype) {
191304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, max_matches, matchtype);
19140b57cec5SDimitry Andric 
19150b57cec5SDimitry Andric   SBValueList sb_value_list;
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
19180b57cec5SDimitry Andric   if (name && target_sp) {
19190b57cec5SDimitry Andric     llvm::StringRef name_ref(name);
19200b57cec5SDimitry Andric     VariableList variable_list;
19210b57cec5SDimitry Andric 
19220b57cec5SDimitry Andric     std::string regexstr;
19230b57cec5SDimitry Andric     switch (matchtype) {
19240b57cec5SDimitry Andric     case eMatchTypeNormal:
19259dba64beSDimitry Andric       target_sp->GetImages().FindGlobalVariables(ConstString(name), max_matches,
19269dba64beSDimitry Andric                                                  variable_list);
19270b57cec5SDimitry Andric       break;
19280b57cec5SDimitry Andric     case eMatchTypeRegex:
19299dba64beSDimitry Andric       target_sp->GetImages().FindGlobalVariables(RegularExpression(name_ref),
19309dba64beSDimitry Andric                                                  max_matches, variable_list);
19310b57cec5SDimitry Andric       break;
19320b57cec5SDimitry Andric     case eMatchTypeStartsWith:
1933fe013be4SDimitry Andric       regexstr = "^" + llvm::Regex::escape(name) + ".*";
19349dba64beSDimitry Andric       target_sp->GetImages().FindGlobalVariables(RegularExpression(regexstr),
19359dba64beSDimitry Andric                                                  max_matches, variable_list);
19360b57cec5SDimitry Andric       break;
19370b57cec5SDimitry Andric     }
1938480093f4SDimitry Andric     if (!variable_list.Empty()) {
19390b57cec5SDimitry Andric       ExecutionContextScope *exe_scope = target_sp->GetProcessSP().get();
19400b57cec5SDimitry Andric       if (exe_scope == nullptr)
19410b57cec5SDimitry Andric         exe_scope = target_sp.get();
1942480093f4SDimitry Andric       for (const VariableSP &var_sp : variable_list) {
1943480093f4SDimitry Andric         lldb::ValueObjectSP valobj_sp(
1944480093f4SDimitry Andric             ValueObjectVariable::Create(exe_scope, var_sp));
19450b57cec5SDimitry Andric         if (valobj_sp)
19460b57cec5SDimitry Andric           sb_value_list.Append(SBValue(valobj_sp));
19470b57cec5SDimitry Andric       }
19480b57cec5SDimitry Andric     }
19490b57cec5SDimitry Andric   }
19500b57cec5SDimitry Andric 
195104eeddc0SDimitry Andric   return sb_value_list;
19520b57cec5SDimitry Andric }
19530b57cec5SDimitry Andric 
FindFirstGlobalVariable(const char * name)19540b57cec5SDimitry Andric lldb::SBValue SBTarget::FindFirstGlobalVariable(const char *name) {
195504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name);
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   SBValueList sb_value_list(FindGlobalVariables(name, 1));
19580b57cec5SDimitry Andric   if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
195904eeddc0SDimitry Andric     return sb_value_list.GetValueAtIndex(0);
196004eeddc0SDimitry Andric   return SBValue();
19610b57cec5SDimitry Andric }
19620b57cec5SDimitry Andric 
GetSourceManager()19630b57cec5SDimitry Andric SBSourceManager SBTarget::GetSourceManager() {
196404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
19650b57cec5SDimitry Andric 
19660b57cec5SDimitry Andric   SBSourceManager source_manager(*this);
196704eeddc0SDimitry Andric   return source_manager;
19680b57cec5SDimitry Andric }
19690b57cec5SDimitry Andric 
ReadInstructions(lldb::SBAddress base_addr,uint32_t count)19700b57cec5SDimitry Andric lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
19710b57cec5SDimitry Andric                                                    uint32_t count) {
197204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, count);
19730b57cec5SDimitry Andric 
197404eeddc0SDimitry Andric   return ReadInstructions(base_addr, count, nullptr);
19750b57cec5SDimitry Andric }
19760b57cec5SDimitry Andric 
ReadInstructions(lldb::SBAddress base_addr,uint32_t count,const char * flavor_string)19770b57cec5SDimitry Andric lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
19780b57cec5SDimitry Andric                                                    uint32_t count,
19790b57cec5SDimitry Andric                                                    const char *flavor_string) {
198004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, count, flavor_string);
19810b57cec5SDimitry Andric 
19820b57cec5SDimitry Andric   SBInstructionList sb_instructions;
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
19850b57cec5SDimitry Andric   if (target_sp) {
19860b57cec5SDimitry Andric     Address *addr_ptr = base_addr.get();
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric     if (addr_ptr) {
19890b57cec5SDimitry Andric       DataBufferHeap data(
19900b57cec5SDimitry Andric           target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1991fe6060f1SDimitry Andric       bool force_live_memory = true;
19920b57cec5SDimitry Andric       lldb_private::Status error;
19930b57cec5SDimitry Andric       lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
19940b57cec5SDimitry Andric       const size_t bytes_read =
1995fe6060f1SDimitry Andric           target_sp->ReadMemory(*addr_ptr, data.GetBytes(), data.GetByteSize(),
1996fe6060f1SDimitry Andric                                 error, force_live_memory, &load_addr);
19970b57cec5SDimitry Andric       const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
19980b57cec5SDimitry Andric       sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
19990b57cec5SDimitry Andric           target_sp->GetArchitecture(), nullptr, flavor_string, *addr_ptr,
20000b57cec5SDimitry Andric           data.GetBytes(), bytes_read, count, data_from_file));
20010b57cec5SDimitry Andric     }
20020b57cec5SDimitry Andric   }
20030b57cec5SDimitry Andric 
200404eeddc0SDimitry Andric   return sb_instructions;
20050b57cec5SDimitry Andric }
20060b57cec5SDimitry Andric 
GetInstructions(lldb::SBAddress base_addr,const void * buf,size_t size)20070b57cec5SDimitry Andric lldb::SBInstructionList SBTarget::GetInstructions(lldb::SBAddress base_addr,
20080b57cec5SDimitry Andric                                                   const void *buf,
20090b57cec5SDimitry Andric                                                   size_t size) {
201004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, buf, size);
20110b57cec5SDimitry Andric 
201204eeddc0SDimitry Andric   return GetInstructionsWithFlavor(base_addr, nullptr, buf, size);
20130b57cec5SDimitry Andric }
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric lldb::SBInstructionList
GetInstructionsWithFlavor(lldb::SBAddress base_addr,const char * flavor_string,const void * buf,size_t size)20160b57cec5SDimitry Andric SBTarget::GetInstructionsWithFlavor(lldb::SBAddress base_addr,
20170b57cec5SDimitry Andric                                     const char *flavor_string, const void *buf,
20180b57cec5SDimitry Andric                                     size_t size) {
201904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, flavor_string, buf, size);
20200b57cec5SDimitry Andric 
20210b57cec5SDimitry Andric   SBInstructionList sb_instructions;
20220b57cec5SDimitry Andric 
20230b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
20240b57cec5SDimitry Andric   if (target_sp) {
20250b57cec5SDimitry Andric     Address addr;
20260b57cec5SDimitry Andric 
20270b57cec5SDimitry Andric     if (base_addr.get())
20280b57cec5SDimitry Andric       addr = *base_addr.get();
20290b57cec5SDimitry Andric 
20300b57cec5SDimitry Andric     const bool data_from_file = true;
20310b57cec5SDimitry Andric 
20320b57cec5SDimitry Andric     sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
20330b57cec5SDimitry Andric         target_sp->GetArchitecture(), nullptr, flavor_string, addr, buf, size,
20340b57cec5SDimitry Andric         UINT32_MAX, data_from_file));
20350b57cec5SDimitry Andric   }
20360b57cec5SDimitry Andric 
203704eeddc0SDimitry Andric   return sb_instructions;
20380b57cec5SDimitry Andric }
20390b57cec5SDimitry Andric 
GetInstructions(lldb::addr_t base_addr,const void * buf,size_t size)20400b57cec5SDimitry Andric lldb::SBInstructionList SBTarget::GetInstructions(lldb::addr_t base_addr,
20410b57cec5SDimitry Andric                                                   const void *buf,
20420b57cec5SDimitry Andric                                                   size_t size) {
204304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, buf, size);
20445ffd83dbSDimitry Andric 
204504eeddc0SDimitry Andric   return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), nullptr, buf,
204604eeddc0SDimitry Andric                                    size);
20470b57cec5SDimitry Andric }
20480b57cec5SDimitry Andric 
20490b57cec5SDimitry Andric lldb::SBInstructionList
GetInstructionsWithFlavor(lldb::addr_t base_addr,const char * flavor_string,const void * buf,size_t size)20500b57cec5SDimitry Andric SBTarget::GetInstructionsWithFlavor(lldb::addr_t base_addr,
20510b57cec5SDimitry Andric                                     const char *flavor_string, const void *buf,
20520b57cec5SDimitry Andric                                     size_t size) {
205304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, base_addr, flavor_string, buf, size);
20540b57cec5SDimitry Andric 
205504eeddc0SDimitry Andric   return GetInstructionsWithFlavor(ResolveLoadAddress(base_addr), flavor_string,
205604eeddc0SDimitry Andric                                    buf, size);
20570b57cec5SDimitry Andric }
20580b57cec5SDimitry Andric 
SetSectionLoadAddress(lldb::SBSection section,lldb::addr_t section_base_addr)20590b57cec5SDimitry Andric SBError SBTarget::SetSectionLoadAddress(lldb::SBSection section,
20600b57cec5SDimitry Andric                                         lldb::addr_t section_base_addr) {
206104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, section, section_base_addr);
20620b57cec5SDimitry Andric 
20630b57cec5SDimitry Andric   SBError sb_error;
20640b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
20650b57cec5SDimitry Andric   if (target_sp) {
20660b57cec5SDimitry Andric     if (!section.IsValid()) {
20670b57cec5SDimitry Andric       sb_error.SetErrorStringWithFormat("invalid section");
20680b57cec5SDimitry Andric     } else {
20690b57cec5SDimitry Andric       SectionSP section_sp(section.GetSP());
20700b57cec5SDimitry Andric       if (section_sp) {
20710b57cec5SDimitry Andric         if (section_sp->IsThreadSpecific()) {
20720b57cec5SDimitry Andric           sb_error.SetErrorString(
20730b57cec5SDimitry Andric               "thread specific sections are not yet supported");
20740b57cec5SDimitry Andric         } else {
20750b57cec5SDimitry Andric           ProcessSP process_sp(target_sp->GetProcessSP());
20760b57cec5SDimitry Andric           if (target_sp->SetSectionLoadAddress(section_sp, section_base_addr)) {
20770b57cec5SDimitry Andric             ModuleSP module_sp(section_sp->GetModule());
20780b57cec5SDimitry Andric             if (module_sp) {
20790b57cec5SDimitry Andric               ModuleList module_list;
20800b57cec5SDimitry Andric               module_list.Append(module_sp);
20810b57cec5SDimitry Andric               target_sp->ModulesDidLoad(module_list);
20820b57cec5SDimitry Andric             }
20830b57cec5SDimitry Andric             // Flush info in the process (stack frames, etc)
20840b57cec5SDimitry Andric             if (process_sp)
20850b57cec5SDimitry Andric               process_sp->Flush();
20860b57cec5SDimitry Andric           }
20870b57cec5SDimitry Andric         }
20880b57cec5SDimitry Andric       }
20890b57cec5SDimitry Andric     }
20900b57cec5SDimitry Andric   } else {
20910b57cec5SDimitry Andric     sb_error.SetErrorString("invalid target");
20920b57cec5SDimitry Andric   }
209304eeddc0SDimitry Andric   return sb_error;
20940b57cec5SDimitry Andric }
20950b57cec5SDimitry Andric 
ClearSectionLoadAddress(lldb::SBSection section)20960b57cec5SDimitry Andric SBError SBTarget::ClearSectionLoadAddress(lldb::SBSection section) {
209704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, section);
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric   SBError sb_error;
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
21020b57cec5SDimitry Andric   if (target_sp) {
21030b57cec5SDimitry Andric     if (!section.IsValid()) {
21040b57cec5SDimitry Andric       sb_error.SetErrorStringWithFormat("invalid section");
21050b57cec5SDimitry Andric     } else {
21060b57cec5SDimitry Andric       SectionSP section_sp(section.GetSP());
21070b57cec5SDimitry Andric       if (section_sp) {
21080b57cec5SDimitry Andric         ProcessSP process_sp(target_sp->GetProcessSP());
21090b57cec5SDimitry Andric         if (target_sp->SetSectionUnloaded(section_sp)) {
21100b57cec5SDimitry Andric           ModuleSP module_sp(section_sp->GetModule());
21110b57cec5SDimitry Andric           if (module_sp) {
21120b57cec5SDimitry Andric             ModuleList module_list;
21130b57cec5SDimitry Andric             module_list.Append(module_sp);
21140b57cec5SDimitry Andric             target_sp->ModulesDidUnload(module_list, false);
21150b57cec5SDimitry Andric           }
21160b57cec5SDimitry Andric           // Flush info in the process (stack frames, etc)
21170b57cec5SDimitry Andric           if (process_sp)
21180b57cec5SDimitry Andric             process_sp->Flush();
21190b57cec5SDimitry Andric         }
21200b57cec5SDimitry Andric       } else {
21210b57cec5SDimitry Andric         sb_error.SetErrorStringWithFormat("invalid section");
21220b57cec5SDimitry Andric       }
21230b57cec5SDimitry Andric     }
21240b57cec5SDimitry Andric   } else {
21250b57cec5SDimitry Andric     sb_error.SetErrorStringWithFormat("invalid target");
21260b57cec5SDimitry Andric   }
212704eeddc0SDimitry Andric   return sb_error;
21280b57cec5SDimitry Andric }
21290b57cec5SDimitry Andric 
SetModuleLoadAddress(lldb::SBModule module,int64_t slide_offset)21300b57cec5SDimitry Andric SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
21310b57cec5SDimitry Andric                                        int64_t slide_offset) {
213204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module, slide_offset);
21330b57cec5SDimitry Andric 
2134fe013be4SDimitry Andric   if (slide_offset < 0) {
2135fe013be4SDimitry Andric     SBError sb_error;
2136fe013be4SDimitry Andric     sb_error.SetErrorStringWithFormat("slide must be positive");
2137fe013be4SDimitry Andric     return sb_error;
2138fe013be4SDimitry Andric   }
2139fe013be4SDimitry Andric 
2140fe013be4SDimitry Andric   return SetModuleLoadAddress(module, static_cast<uint64_t>(slide_offset));
2141fe013be4SDimitry Andric }
2142fe013be4SDimitry Andric 
SetModuleLoadAddress(lldb::SBModule module,uint64_t slide_offset)2143fe013be4SDimitry Andric SBError SBTarget::SetModuleLoadAddress(lldb::SBModule module,
2144fe013be4SDimitry Andric                                                uint64_t slide_offset) {
2145fe013be4SDimitry Andric 
21460b57cec5SDimitry Andric   SBError sb_error;
21470b57cec5SDimitry Andric 
21480b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
21490b57cec5SDimitry Andric   if (target_sp) {
21500b57cec5SDimitry Andric     ModuleSP module_sp(module.GetSP());
21510b57cec5SDimitry Andric     if (module_sp) {
21520b57cec5SDimitry Andric       bool changed = false;
21530b57cec5SDimitry Andric       if (module_sp->SetLoadAddress(*target_sp, slide_offset, true, changed)) {
21540b57cec5SDimitry Andric         // The load was successful, make sure that at least some sections
21550b57cec5SDimitry Andric         // changed before we notify that our module was loaded.
21560b57cec5SDimitry Andric         if (changed) {
21570b57cec5SDimitry Andric           ModuleList module_list;
21580b57cec5SDimitry Andric           module_list.Append(module_sp);
21590b57cec5SDimitry Andric           target_sp->ModulesDidLoad(module_list);
21600b57cec5SDimitry Andric           // Flush info in the process (stack frames, etc)
21610b57cec5SDimitry Andric           ProcessSP process_sp(target_sp->GetProcessSP());
21620b57cec5SDimitry Andric           if (process_sp)
21630b57cec5SDimitry Andric             process_sp->Flush();
21640b57cec5SDimitry Andric         }
21650b57cec5SDimitry Andric       }
21660b57cec5SDimitry Andric     } else {
21670b57cec5SDimitry Andric       sb_error.SetErrorStringWithFormat("invalid module");
21680b57cec5SDimitry Andric     }
21690b57cec5SDimitry Andric 
21700b57cec5SDimitry Andric   } else {
21710b57cec5SDimitry Andric     sb_error.SetErrorStringWithFormat("invalid target");
21720b57cec5SDimitry Andric   }
217304eeddc0SDimitry Andric   return sb_error;
21740b57cec5SDimitry Andric }
21750b57cec5SDimitry Andric 
ClearModuleLoadAddress(lldb::SBModule module)21760b57cec5SDimitry Andric SBError SBTarget::ClearModuleLoadAddress(lldb::SBModule module) {
217704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module);
21780b57cec5SDimitry Andric 
21790b57cec5SDimitry Andric   SBError sb_error;
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric   char path[PATH_MAX];
21820b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
21830b57cec5SDimitry Andric   if (target_sp) {
21840b57cec5SDimitry Andric     ModuleSP module_sp(module.GetSP());
21850b57cec5SDimitry Andric     if (module_sp) {
21860b57cec5SDimitry Andric       ObjectFile *objfile = module_sp->GetObjectFile();
21870b57cec5SDimitry Andric       if (objfile) {
21880b57cec5SDimitry Andric         SectionList *section_list = objfile->GetSectionList();
21890b57cec5SDimitry Andric         if (section_list) {
21900b57cec5SDimitry Andric           ProcessSP process_sp(target_sp->GetProcessSP());
21910b57cec5SDimitry Andric 
21920b57cec5SDimitry Andric           bool changed = false;
21930b57cec5SDimitry Andric           const size_t num_sections = section_list->GetSize();
21940b57cec5SDimitry Andric           for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
21950b57cec5SDimitry Andric             SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
21960b57cec5SDimitry Andric             if (section_sp)
21970b57cec5SDimitry Andric               changed |= target_sp->SetSectionUnloaded(section_sp);
21980b57cec5SDimitry Andric           }
21990b57cec5SDimitry Andric           if (changed) {
22000b57cec5SDimitry Andric             ModuleList module_list;
22010b57cec5SDimitry Andric             module_list.Append(module_sp);
22020b57cec5SDimitry Andric             target_sp->ModulesDidUnload(module_list, false);
22030b57cec5SDimitry Andric             // Flush info in the process (stack frames, etc)
22040b57cec5SDimitry Andric             ProcessSP process_sp(target_sp->GetProcessSP());
22050b57cec5SDimitry Andric             if (process_sp)
22060b57cec5SDimitry Andric               process_sp->Flush();
22070b57cec5SDimitry Andric           }
22080b57cec5SDimitry Andric         } else {
22090b57cec5SDimitry Andric           module_sp->GetFileSpec().GetPath(path, sizeof(path));
22100b57cec5SDimitry Andric           sb_error.SetErrorStringWithFormat("no sections in object file '%s'",
22110b57cec5SDimitry Andric                                             path);
22120b57cec5SDimitry Andric         }
22130b57cec5SDimitry Andric       } else {
22140b57cec5SDimitry Andric         module_sp->GetFileSpec().GetPath(path, sizeof(path));
22150b57cec5SDimitry Andric         sb_error.SetErrorStringWithFormat("no object file for module '%s'",
22160b57cec5SDimitry Andric                                           path);
22170b57cec5SDimitry Andric       }
22180b57cec5SDimitry Andric     } else {
22190b57cec5SDimitry Andric       sb_error.SetErrorStringWithFormat("invalid module");
22200b57cec5SDimitry Andric     }
22210b57cec5SDimitry Andric   } else {
22220b57cec5SDimitry Andric     sb_error.SetErrorStringWithFormat("invalid target");
22230b57cec5SDimitry Andric   }
222404eeddc0SDimitry Andric   return sb_error;
22250b57cec5SDimitry Andric }
22260b57cec5SDimitry Andric 
FindSymbols(const char * name,lldb::SymbolType symbol_type)22270b57cec5SDimitry Andric lldb::SBSymbolContextList SBTarget::FindSymbols(const char *name,
22280b57cec5SDimitry Andric                                                 lldb::SymbolType symbol_type) {
222904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, name, symbol_type);
22300b57cec5SDimitry Andric 
22310b57cec5SDimitry Andric   SBSymbolContextList sb_sc_list;
22320b57cec5SDimitry Andric   if (name && name[0]) {
22330b57cec5SDimitry Andric     TargetSP target_sp(GetSP());
22349dba64beSDimitry Andric     if (target_sp)
22350b57cec5SDimitry Andric       target_sp->GetImages().FindSymbolsWithNameAndType(
22369dba64beSDimitry Andric           ConstString(name), symbol_type, *sb_sc_list);
22370b57cec5SDimitry Andric   }
223804eeddc0SDimitry Andric   return sb_sc_list;
22390b57cec5SDimitry Andric }
22400b57cec5SDimitry Andric 
EvaluateExpression(const char * expr)22410b57cec5SDimitry Andric lldb::SBValue SBTarget::EvaluateExpression(const char *expr) {
224204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, expr);
22430b57cec5SDimitry Andric 
22440b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
22450b57cec5SDimitry Andric   if (!target_sp)
224604eeddc0SDimitry Andric     return SBValue();
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric   SBExpressionOptions options;
22490b57cec5SDimitry Andric   lldb::DynamicValueType fetch_dynamic_value =
22500b57cec5SDimitry Andric       target_sp->GetPreferDynamicValue();
22510b57cec5SDimitry Andric   options.SetFetchDynamicValue(fetch_dynamic_value);
22520b57cec5SDimitry Andric   options.SetUnwindOnError(true);
225304eeddc0SDimitry Andric   return EvaluateExpression(expr, options);
22540b57cec5SDimitry Andric }
22550b57cec5SDimitry Andric 
EvaluateExpression(const char * expr,const SBExpressionOptions & options)22560b57cec5SDimitry Andric lldb::SBValue SBTarget::EvaluateExpression(const char *expr,
22570b57cec5SDimitry Andric                                            const SBExpressionOptions &options) {
225804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, expr, options);
22590b57cec5SDimitry Andric 
226081ad6265SDimitry Andric   Log *expr_log = GetLog(LLDBLog::Expressions);
22610b57cec5SDimitry Andric   SBValue expr_result;
22620b57cec5SDimitry Andric   ValueObjectSP expr_value_sp;
22630b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
22640b57cec5SDimitry Andric   StackFrame *frame = nullptr;
22650b57cec5SDimitry Andric   if (target_sp) {
22660b57cec5SDimitry Andric     if (expr == nullptr || expr[0] == '\0') {
226704eeddc0SDimitry Andric       return expr_result;
22680b57cec5SDimitry Andric     }
22690b57cec5SDimitry Andric 
22700b57cec5SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
22710b57cec5SDimitry Andric     ExecutionContext exe_ctx(m_opaque_sp.get());
22720b57cec5SDimitry Andric 
22730b57cec5SDimitry Andric     frame = exe_ctx.GetFramePtr();
22740b57cec5SDimitry Andric     Target *target = exe_ctx.GetTargetPtr();
2275fe013be4SDimitry Andric     Process *process = exe_ctx.GetProcessPtr();
22760b57cec5SDimitry Andric 
22770b57cec5SDimitry Andric     if (target) {
2278fe013be4SDimitry Andric       // If we have a process, make sure to lock the runlock:
2279fe013be4SDimitry Andric       if (process) {
2280fe013be4SDimitry Andric         Process::StopLocker stop_locker;
2281fe013be4SDimitry Andric         if (stop_locker.TryLock(&process->GetRunLock())) {
22820b57cec5SDimitry Andric           target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2283fe013be4SDimitry Andric         } else {
2284fe013be4SDimitry Andric           Status error;
2285fe013be4SDimitry Andric           error.SetErrorString("can't evaluate expressions when the "
2286fe013be4SDimitry Andric                                "process is running.");
2287fe013be4SDimitry Andric           expr_value_sp = ValueObjectConstResult::Create(nullptr, error);
2288fe013be4SDimitry Andric         }
2289fe013be4SDimitry Andric       } else {
2290fe013be4SDimitry Andric         target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
2291fe013be4SDimitry Andric       }
22920b57cec5SDimitry Andric 
22930b57cec5SDimitry Andric       expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
22940b57cec5SDimitry Andric     }
22950b57cec5SDimitry Andric   }
22969dba64beSDimitry Andric   LLDB_LOGF(expr_log,
22979dba64beSDimitry Andric             "** [SBTarget::EvaluateExpression] Expression result is "
22980b57cec5SDimitry Andric             "%s, summary %s **",
22990b57cec5SDimitry Andric             expr_result.GetValue(), expr_result.GetSummary());
230004eeddc0SDimitry Andric   return expr_result;
23010b57cec5SDimitry Andric }
23020b57cec5SDimitry Andric 
GetStackRedZoneSize()23030b57cec5SDimitry Andric lldb::addr_t SBTarget::GetStackRedZoneSize() {
230404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
23070b57cec5SDimitry Andric   if (target_sp) {
23080b57cec5SDimitry Andric     ABISP abi_sp;
23090b57cec5SDimitry Andric     ProcessSP process_sp(target_sp->GetProcessSP());
23100b57cec5SDimitry Andric     if (process_sp)
23110b57cec5SDimitry Andric       abi_sp = process_sp->GetABI();
23120b57cec5SDimitry Andric     else
23130b57cec5SDimitry Andric       abi_sp = ABI::FindPlugin(ProcessSP(), target_sp->GetArchitecture());
23140b57cec5SDimitry Andric     if (abi_sp)
23150b57cec5SDimitry Andric       return abi_sp->GetRedZoneSize();
23160b57cec5SDimitry Andric   }
23170b57cec5SDimitry Andric   return 0;
23180b57cec5SDimitry Andric }
23190b57cec5SDimitry Andric 
IsLoaded(const SBModule & module) const2320fe6060f1SDimitry Andric bool SBTarget::IsLoaded(const SBModule &module) const {
232104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, module);
2322fe6060f1SDimitry Andric 
2323fe6060f1SDimitry Andric   TargetSP target_sp(GetSP());
2324fe6060f1SDimitry Andric   if (!target_sp)
2325fe6060f1SDimitry Andric     return false;
2326fe6060f1SDimitry Andric 
2327fe6060f1SDimitry Andric   ModuleSP module_sp(module.GetSP());
2328fe6060f1SDimitry Andric   if (!module_sp)
2329fe6060f1SDimitry Andric     return false;
2330fe6060f1SDimitry Andric 
2331fe6060f1SDimitry Andric   return module_sp->IsLoadedInTarget(target_sp.get());
2332fe6060f1SDimitry Andric }
2333fe6060f1SDimitry Andric 
GetLaunchInfo() const23340b57cec5SDimitry Andric lldb::SBLaunchInfo SBTarget::GetLaunchInfo() const {
233504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric   lldb::SBLaunchInfo launch_info(nullptr);
23380b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
23390b57cec5SDimitry Andric   if (target_sp)
23400b57cec5SDimitry Andric     launch_info.set_ref(m_opaque_sp->GetProcessLaunchInfo());
234104eeddc0SDimitry Andric   return launch_info;
23420b57cec5SDimitry Andric }
23430b57cec5SDimitry Andric 
SetLaunchInfo(const lldb::SBLaunchInfo & launch_info)23440b57cec5SDimitry Andric void SBTarget::SetLaunchInfo(const lldb::SBLaunchInfo &launch_info) {
234504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, launch_info);
23460b57cec5SDimitry Andric 
23470b57cec5SDimitry Andric   TargetSP target_sp(GetSP());
23480b57cec5SDimitry Andric   if (target_sp)
23490b57cec5SDimitry Andric     m_opaque_sp->SetProcessLaunchInfo(launch_info.ref());
23500b57cec5SDimitry Andric }
23510b57cec5SDimitry Andric 
GetEnvironment()23525ffd83dbSDimitry Andric SBEnvironment SBTarget::GetEnvironment() {
235304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
23545ffd83dbSDimitry Andric   TargetSP target_sp(GetSP());
23555ffd83dbSDimitry Andric 
23565ffd83dbSDimitry Andric   if (target_sp) {
235704eeddc0SDimitry Andric     return SBEnvironment(target_sp->GetEnvironment());
23585ffd83dbSDimitry Andric   }
23595ffd83dbSDimitry Andric 
236004eeddc0SDimitry Andric   return SBEnvironment();
23615ffd83dbSDimitry Andric }
23625ffd83dbSDimitry Andric 
GetTrace()2363fe6060f1SDimitry Andric lldb::SBTrace SBTarget::GetTrace() {
236404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2365fe6060f1SDimitry Andric   TargetSP target_sp(GetSP());
2366fe6060f1SDimitry Andric 
2367fe6060f1SDimitry Andric   if (target_sp)
236804eeddc0SDimitry Andric     return SBTrace(target_sp->GetTrace());
2369fe6060f1SDimitry Andric 
237004eeddc0SDimitry Andric   return SBTrace();
2371fe6060f1SDimitry Andric }
2372fe6060f1SDimitry Andric 
CreateTrace(lldb::SBError & error)2373fe6060f1SDimitry Andric lldb::SBTrace SBTarget::CreateTrace(lldb::SBError &error) {
237404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, error);
2375fe6060f1SDimitry Andric   TargetSP target_sp(GetSP());
2376fe6060f1SDimitry Andric   error.Clear();
2377fe6060f1SDimitry Andric 
2378fe6060f1SDimitry Andric   if (target_sp) {
2379fe6060f1SDimitry Andric     if (llvm::Expected<lldb::TraceSP> trace_sp = target_sp->CreateTrace()) {
238004eeddc0SDimitry Andric       return SBTrace(*trace_sp);
2381fe6060f1SDimitry Andric     } else {
2382fe6060f1SDimitry Andric       error.SetErrorString(llvm::toString(trace_sp.takeError()).c_str());
2383fe6060f1SDimitry Andric     }
2384fe6060f1SDimitry Andric   } else {
2385fe6060f1SDimitry Andric     error.SetErrorString("missing target");
2386fe6060f1SDimitry Andric   }
238704eeddc0SDimitry Andric   return SBTrace();
23880b57cec5SDimitry Andric }
2389