26#include "llvm/ADT/SmallString.h"
27#include "llvm/Support/FileSystem.h"
33 static constexpr llvm::StringLiteral class_name(
"lldb.targetList");
46 llvm::StringRef user_exe_path,
47 llvm::StringRef triple_str,
53 debugger, user_exe_path, triple_str, load_dependent_files,
54 platform_options, target_sp);
56 if (target_sp && result.Success())
62 llvm::StringRef user_exe_path,
68 debugger, user_exe_path, specified_arch, load_dependent_files,
69 platform_sp, target_sp);
71 if (target_sp && result.Success())
77 Debugger &debugger, llvm::StringRef user_exe_path,
89 const ArchSpec arch(triple_str);
90 if (!triple_str.empty() && !arch.IsValid()) {
92 triple_str.str().c_str());
96 ArchSpec platform_arch(arch);
102 const bool select_platform =
true;
110 bool prefer_platform_arch =
false;
111 auto update_platform_arch = [&](
const ArchSpec &module_arch) {
114 if (!platform_arch.TripleOSWasSpecified() ||
115 !platform_arch.TripleVendorWasSpecified()) {
116 prefer_platform_arch =
true;
117 platform_arch = module_arch;
121 if (!user_exe_path.empty()) {
122 ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native));
127 if (platform_sp->IsHost() &&
130 module_spec.GetFileSpec());
139 module_spec.GetFileSpec(), file_offset, file_size);
141 if (module_specs.
GetSize() > 0) {
142 ModuleSpec matching_module_spec;
144 if (module_specs.
GetSize() == 1) {
146 if (platform_arch.IsValid()) {
147 if (platform_arch.IsCompatibleMatch(
154 StreamString platform_arch_strm;
155 StreamString module_arch_strm;
157 platform_arch.DumpTriple(platform_arch_strm.
AsRawOstream());
161 "the specified architecture '%s' is not compatible with '%s' "
164 module_spec.GetFileSpec().GetPath().c_str());
169 prefer_platform_arch =
true;
173 }
else if (arch.IsValid()) {
175 module_spec.GetArchitecture() = arch;
177 matching_module_spec))
182 std::vector<PlatformSP> candidates;
183 std::vector<ArchSpec> archs;
184 for (
const ModuleSpec &spec : module_specs.
ModuleSpecs())
185 archs.push_back(spec.GetArchitecture());
187 platform_list.
GetOrCreate(archs, {}, candidates)) {
188 platform_sp = platform_for_archs_sp;
189 }
else if (candidates.empty()) {
191 "no matching platforms found for this file");
195 StreamString error_strm;
196 std::set<llvm::StringRef> platform_set;
198 "more than one platform supports this executable (");
199 for (
const auto &candidate : candidates) {
200 llvm::StringRef platform_name = candidate->GetName();
201 if (platform_set.count(platform_name))
203 if (!platform_set.empty())
206 platform_set.insert(platform_name);
208 error_strm.
Printf(
"), specify an architecture to disambiguate");
218 if (!prefer_platform_arch && arch.IsValid()) {
219 if (!platform_sp->IsCompatibleArchitecture(
221 platform_sp = platform_list.
GetOrCreate(arch, {}, &platform_arch);
225 }
else if (platform_arch.IsValid()) {
228 ArchSpec fixed_platform_arch;
229 if (!platform_sp->IsCompatibleArchitecture(
232 platform_list.
GetOrCreate(platform_arch, {}, &fixed_platform_arch);
238 if (!platform_arch.IsValid())
239 platform_arch = arch;
242 platform_arch, load_dependent_files,
243 platform_sp, target_sp);
247 llvm::StringRef user_exe_path,
253 user_exe_path.str().c_str(),
256 const bool is_dummy_target =
false;
258 ArchSpec arch(specified_arch);
260 if (arch.IsValid()) {
261 if (!platform_sp || !platform_sp->IsCompatibleArchitecture(
271 arch = specified_arch;
273 FileSpec file(user_exe_path);
277 llvm::SmallString<64> unglobbed_path;
278 StandardTildeExpressionResolver Resolver;
281 if (unglobbed_path.empty())
282 file = FileSpec(user_exe_path);
284 file = FileSpec(unglobbed_path.c_str());
287 bool user_exe_path_is_bundle =
false;
288 char resolved_bundle_exe_path[
PATH_MAX];
289 resolved_bundle_exe_path[0] =
'\0';
292 user_exe_path_is_bundle =
true;
294 if (file.IsRelative() && !user_exe_path.empty()) {
295 llvm::SmallString<64> cwd;
296 if (! llvm::sys::fs::current_path(cwd)) {
297 FileSpec cwd_file(cwd.c_str());
298 cwd_file.AppendPathComponent(file);
306 ModuleSpec module_spec(file, arch);
307 module_spec.SetTarget(target_sp);
311 module_spec.SetPlatform(platform_sp);
312 error = platform_sp->ResolveExecutable(module_spec, exe_module_sp);
315 if (
error.Success() && exe_module_sp) {
316 if (exe_module_sp->GetObjectFile() ==
nullptr) {
317 if (arch.IsValid()) {
319 "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(),
320 arch.GetArchitectureName());
323 "unsupported file type \"%s\"", file.GetPath().c_str());
327 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
329 target_sp->SetExecutableModule(exe_module_sp, load_dependent_files);
330 if (user_exe_path_is_bundle)
331 exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path,
332 sizeof(resolved_bundle_exe_path));
333 if (target_sp->GetPreloadSymbols())
334 exe_module_sp->PreloadSymbols();
339 target_sp.reset(
new Target(debugger, arch, platform_sp, is_dummy_target));
349 if (!user_exe_path.empty()) {
352 if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) {
353 target_sp->SetArg0(resolved_bundle_exe_path);
356 target_sp->SetArg0(file.GetPath().c_str());
359 if (file.GetDirectory()) {
362 target_sp->AppendExecutableSearchPaths(file_dir);
384 auto it = llvm::find_if(
386 Module *exe_module = item->GetExecutableModulePointer();
391 return !exe_arch_ptr ||
404 auto *process_ptr = item->GetProcessSP().get();
405 return process_ptr && (process_ptr->GetID() == pid);
421 return item->GetProcessSP().get() == process;
433 return item->GetGloballyUniqueID() ==
id;
449 return item.get() == target;
458 uint32_t num_async_interrupts_sent = 0;
463 Process *process = target_sp->GetProcessSP().get();
466 ++num_async_interrupts_sent;
475 return num_async_interrupts_sent;
479 uint32_t num_signals_sent = 0;
485 process = target_sp->GetProcessSP().get();
486 if (process && process->
IsAlive()) {
495 process = target_sp->GetProcessSP().get();
496 if (process && process->
IsAlive()) {
502 return num_signals_sent;
529 "target already exists it the list");
549 if (target_sp && target_sp->IsValid()) {
566 if (target_sp->GetImages().FindModule(&module))
570 if (target_sp->GetImages().FindModule(&module))
578 [[maybe_unused]]
bool was_added;
579 std::tie(std::ignore, was_added) =
581 assert(was_added &&
"Target pointer was left in the in-process map");
586 [[maybe_unused]]
bool was_present =
588 assert(was_present &&
"Target pointer being removed was not registered");
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_SCOPED_TIMERF(...)
An architecture specification class.
void DumpTriple(llvm::raw_ostream &s) const
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Broadcaster(lldb::BroadcasterManagerSP manager_sp, std::string name)
Construct with a broadcaster with a name.
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
void CheckInWithManager()
A class to manage flag bits.
CommandInterpreter & GetCommandInterpreter()
TargetList & GetTargetList()
Get accessor for the target list.
PlatformList & GetPlatformList()
Target & GetDummyTarget()
void SetDirectory(ConstString directory)
Directory string set accessor.
static bool Match(const FileSpec &pattern, const FileSpec &file)
Match FileSpec pattern against FileSpec file.
bool ResolveExecutableLocation(FileSpec &file_spec)
Call into the Host to see if it can help find the file.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
static bool ResolveExecutableInBundle(FileSpec &file)
When executable files may live within a directory, where the directory represents an executable bundl...
ModuleSpecIterable ModuleSpecs()
bool GetModuleSpecAtIndex(size_t i, ModuleSpec &module_spec) const
bool FindMatchingModuleSpec(const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
ArchSpec & GetArchitecture()
A class that describes an executable image and its associated object and symbol files.
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP=lldb::DataExtractorSP())
A plug-in interface definition class for debugging a process.
void SendAsyncInterrupt(Thread *thread=nullptr)
Send an async interrupt request.
Status Signal(int signal)
Sends a process a UNIX signal signal.
virtual bool IsAlive()
Check if a process is still alive.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
const char * GetData() const
llvm::StringRef GetString() const
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
void SetSelectedTargetInternal(uint32_t index)
static llvm::StringRef GetStaticBroadcasterClass()
lldb::TargetSP FindTargetWithProcessID(lldb::pid_t pid) const
Find the target that contains a process with process ID pid.
bool IsTargetInProcess(lldb::TargetSP target_sp)
lldb::TargetSP GetTargetAtIndex(uint32_t index) const
Status CreateTarget(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles get_dependent_modules, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
Create a new Target.
TargetList(Debugger &debugger)
Constructor.
std::recursive_mutex m_target_list_mutex
uint32_t GetIndexOfTarget(lldb::TargetSP target_sp) const
lldb::TargetSP GetTargetSP(Target *target) const
void UnregisterInProcessTarget(lldb::TargetSP target_sp)
void SetSelectedTarget(uint32_t index)
uint32_t SendAsyncInterrupt(lldb::pid_t pid=LLDB_INVALID_PROCESS_ID)
Send an async interrupt to one or all processes.
bool DeleteTarget(lldb::TargetSP &target_sp)
Delete a Target object from the list.
lldb::TargetSP FindTargetWithProcess(lldb_private::Process *process) const
void RegisterInProcessTarget(lldb::TargetSP target_sp)
static Status CreateTargetInternal(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles load_dependent_files, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
lldb::TargetSP GetSelectedTarget()
void AddTargetInternal(lldb::TargetSP target_sp, bool do_select)
lldb::TargetSP FindTargetByGloballyUniqueID(lldb::user_id_t id) const
Find the target that has a globally unique ID that matches ID id.
bool AnyTargetContainsModule(Module &module)
Returns whether any module, including ones in the process of being added, contains this module.
size_t GetNumTargets() const
uint32_t SignalIfRunning(lldb::pid_t pid, int signo)
lldb::TargetSP FindTargetWithExecutableAndArchitecture(const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr=nullptr) const
Find the target that contains has an executable whose path matches exe_file_spec, and whose architect...
std::unordered_set< lldb::TargetSP > m_in_process_target_list
uint32_t m_selected_target_idx
bool ResolveFullPath(llvm::StringRef Expr, llvm::SmallVectorImpl< char > &Output)
Resolve an entire path that begins with a tilde expression, replacing the username portion with the m...
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP