Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- TargetList.cpp ----------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | fb1a0a0 | 2017-03-06 18:34:25 +0000 | [diff] [blame] | 9 | #include "lldb/Target/TargetList.h" |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 10 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 12 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Host/Host.h" |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 14 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 15 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/OptionGroupPlatform.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Target/Process.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/Broadcaster.h" |
| 21 | #include "lldb/Utility/Event.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/State.h" |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/TildeExpressionResolver.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/Timer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
| 27 | #include "llvm/Support/FileSystem.h" |
| 28 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | ConstString &TargetList::GetStaticBroadcasterClass() { |
| 33 | static ConstString class_name("lldb.targetList"); |
| 34 | return class_name; |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | // TargetList constructor |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 38 | TargetList::TargetList(Debugger &debugger) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | : Broadcaster(debugger.GetBroadcasterManager(), |
| 40 | TargetList::GetStaticBroadcasterClass().AsCString()), |
| 41 | m_target_list(), m_target_list_mutex(), m_selected_target_idx(0) { |
| 42 | CheckInWithManager(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 45 | Status TargetList::CreateTarget(Debugger &debugger, |
| 46 | llvm::StringRef user_exe_path, |
| 47 | llvm::StringRef triple_str, |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 48 | LoadDependentFiles load_dependent_files, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 49 | const OptionGroupPlatform *platform_options, |
| 50 | TargetSP &target_sp) { |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 51 | return CreateTargetInternal(debugger, user_exe_path, triple_str, |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 52 | load_dependent_files, platform_options, |
| 53 | target_sp); |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 56 | Status TargetList::CreateTarget(Debugger &debugger, |
| 57 | llvm::StringRef user_exe_path, |
| 58 | const ArchSpec &specified_arch, |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 59 | LoadDependentFiles load_dependent_files, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 60 | PlatformSP &platform_sp, TargetSP &target_sp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | return CreateTargetInternal(debugger, user_exe_path, specified_arch, |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 62 | load_dependent_files, platform_sp, target_sp); |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 65 | Status TargetList::CreateTargetInternal( |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 66 | Debugger &debugger, llvm::StringRef user_exe_path, |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 67 | llvm::StringRef triple_str, LoadDependentFiles load_dependent_files, |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 68 | const OptionGroupPlatform *platform_options, TargetSP &target_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 69 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 71 | // Let's start by looking at the selected platform. |
| 72 | PlatformSP platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 73 | |
| 74 | // This variable corresponds to the architecture specified by the triple |
| 75 | // string. If that string was empty the currently selected platform will |
| 76 | // determine the architecture. |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 77 | const ArchSpec arch(triple_str); |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 78 | if (!triple_str.empty() && !arch.IsValid()) { |
| 79 | error.SetErrorStringWithFormat("invalid triple '%s'", |
| 80 | triple_str.str().c_str()); |
| 81 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | ArchSpec platform_arch(arch); |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 85 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 86 | // Create a new platform if a platform was specified in the platform options |
| 87 | // and doesn't match the selected platform. |
| 88 | if (platform_options && platform_options->PlatformWasSpecified() && |
| 89 | !platform_options->PlatformMatches(platform_sp)) { |
| 90 | const bool select_platform = true; |
| 91 | platform_sp = platform_options->CreatePlatformWithOptions( |
| 92 | debugger.GetCommandInterpreter(), arch, select_platform, error, |
| 93 | platform_arch); |
| 94 | if (!platform_sp) |
| 95 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 98 | bool prefer_platform_arch = false; |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 99 | auto update_platform_arch = [&](const ArchSpec &module_arch) { |
| 100 | // If the OS or vendor weren't specified, then adopt the module's |
| 101 | // architecture so that the platform matching can be more accurate. |
| 102 | if (!platform_arch.TripleOSWasSpecified() || |
| 103 | !platform_arch.TripleVendorWasSpecified()) { |
| 104 | prefer_platform_arch = true; |
| 105 | platform_arch = module_arch; |
| 106 | } |
| 107 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 109 | if (!user_exe_path.empty()) { |
| 110 | ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native)); |
| 111 | FileSystem::Instance().Resolve(module_spec.GetFileSpec()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | // Resolve the executable in case we are given a path to a application |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 113 | // bundle like a .app bundle on MacOSX. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | Host::ResolveExecutableInBundle(module_spec.GetFileSpec()); |
| 115 | |
| 116 | lldb::offset_t file_offset = 0; |
| 117 | lldb::offset_t file_size = 0; |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 118 | ModuleSpecList module_specs; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | const size_t num_specs = ObjectFile::GetModuleSpecifications( |
| 120 | module_spec.GetFileSpec(), file_offset, file_size, module_specs); |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | if (num_specs > 0) { |
| 123 | ModuleSpec matching_module_spec; |
| 124 | |
| 125 | if (num_specs == 1) { |
| 126 | if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) { |
| 127 | if (platform_arch.IsValid()) { |
| 128 | if (platform_arch.IsCompatibleMatch( |
| 129 | matching_module_spec.GetArchitecture())) { |
| 130 | // If the OS or vendor weren't specified, then adopt the module's |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 131 | // architecture so that the platform matching can be more |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 132 | // accurate. |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 133 | update_platform_arch(matching_module_spec.GetArchitecture()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | } else { |
| 135 | StreamString platform_arch_strm; |
| 136 | StreamString module_arch_strm; |
| 137 | |
Raphael Isemann | 2f1e7b3 | 2019-12-04 08:27:43 +0100 | [diff] [blame] | 138 | platform_arch.DumpTriple(platform_arch_strm.AsRawOstream()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | matching_module_spec.GetArchitecture().DumpTriple( |
Raphael Isemann | 2f1e7b3 | 2019-12-04 08:27:43 +0100 | [diff] [blame] | 140 | module_arch_strm.AsRawOstream()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | error.SetErrorStringWithFormat( |
| 142 | "the specified architecture '%s' is not compatible with '%s' " |
| 143 | "in '%s'", |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 144 | platform_arch_strm.GetData(), module_arch_strm.GetData(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | module_spec.GetFileSpec().GetPath().c_str()); |
| 146 | return error; |
| 147 | } |
| 148 | } else { |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 149 | // Only one arch and none was specified. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | prefer_platform_arch = true; |
| 151 | platform_arch = matching_module_spec.GetArchitecture(); |
| 152 | } |
| 153 | } |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 154 | } else if (arch.IsValid()) { |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 155 | // Fat binary. A (valid) architecture was specified. |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 156 | module_spec.GetArchitecture() = arch; |
| 157 | if (module_specs.FindMatchingModuleSpec(module_spec, |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 158 | matching_module_spec)) |
| 159 | update_platform_arch(matching_module_spec.GetArchitecture()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | } else { |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 161 | // Fat binary. No architecture specified, check if there is |
| 162 | // only one platform for all of the architectures. |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 163 | PlatformSP host_platform_sp = Platform::GetHostPlatform(); |
| 164 | std::vector<PlatformSP> platforms; |
| 165 | for (size_t i = 0; i < num_specs; ++i) { |
| 166 | ModuleSpec module_spec; |
| 167 | if (module_specs.GetModuleSpecAtIndex(i, module_spec)) { |
| 168 | // First consider the platform specified by the user, if any, and |
| 169 | // the selected platform otherwise. |
| 170 | if (platform_sp) { |
| 171 | if (platform_sp->IsCompatibleArchitecture( |
| 172 | module_spec.GetArchitecture(), false, nullptr)) { |
| 173 | platforms.push_back(platform_sp); |
| 174 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 178 | // Now consider the host platform if it is different from the |
| 179 | // specified/selected platform. |
| 180 | if (host_platform_sp && |
| 181 | (!platform_sp || |
| 182 | host_platform_sp->GetName() != platform_sp->GetName())) { |
| 183 | if (host_platform_sp->IsCompatibleArchitecture( |
| 184 | module_spec.GetArchitecture(), false, nullptr)) { |
| 185 | platforms.push_back(host_platform_sp); |
| 186 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | } |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Finally find a platform that matches the architecture in the |
| 191 | // executable file. |
| 192 | PlatformSP fallback_platform_sp( |
| 193 | Platform::GetPlatformForArchitecture( |
| 194 | module_spec.GetArchitecture(), nullptr)); |
| 195 | if (fallback_platform_sp) { |
| 196 | platforms.push_back(fallback_platform_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 199 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 201 | Platform *platform_ptr = nullptr; |
| 202 | bool more_than_one_platforms = false; |
| 203 | for (const auto &the_platform_sp : platforms) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | if (platform_ptr) { |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 205 | if (platform_ptr->GetName() != the_platform_sp->GetName()) { |
| 206 | more_than_one_platforms = true; |
| 207 | platform_ptr = nullptr; |
| 208 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | } |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 210 | } else { |
| 211 | platform_ptr = the_platform_sp.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 214 | |
| 215 | if (platform_ptr) { |
| 216 | // All platforms for all modules in the executable match, so we can |
| 217 | // select this platform. |
| 218 | platform_sp = platforms.front(); |
| 219 | } else if (!more_than_one_platforms) { |
| 220 | // No platforms claim to support this file. |
| 221 | error.SetErrorString("no matching platforms found for this file"); |
| 222 | return error; |
| 223 | } else { |
| 224 | // More than one platform claims to support this file. |
| 225 | StreamString error_strm; |
| 226 | std::set<Platform *> platform_set; |
| 227 | error_strm.Printf( |
| 228 | "more than one platform supports this executable ("); |
| 229 | for (const auto &the_platform_sp : platforms) { |
| 230 | if (platform_set.find(the_platform_sp.get()) == |
| 231 | platform_set.end()) { |
| 232 | if (!platform_set.empty()) |
| 233 | error_strm.PutCString(", "); |
| 234 | error_strm.PutCString(the_platform_sp->GetName().GetCString()); |
| 235 | platform_set.insert(the_platform_sp.get()); |
| 236 | } |
| 237 | } |
| 238 | error_strm.Printf("), specify an architecture to disambiguate"); |
| 239 | error.SetErrorString(error_strm.GetString()); |
| 240 | return error; |
| 241 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // If we have a valid architecture, make sure the current platform is |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 247 | // compatible with that architecture. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | if (!prefer_platform_arch && arch.IsValid()) { |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 249 | if (!platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 251 | if (platform_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 252 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 253 | } |
| 254 | } else if (platform_arch.IsValid()) { |
Jonas Devlieghere | 4add853 | 2020-07-29 08:59:57 -0700 | [diff] [blame] | 255 | // If "arch" isn't valid, yet "platform_arch" is, it means we have an |
| 256 | // executable file with a single architecture which should be used. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | ArchSpec fixed_platform_arch; |
Adrian Prantl | 0fa520a | 2020-08-06 10:52:16 -0700 | [diff] [blame] | 258 | if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, nullptr)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | platform_sp = Platform::GetPlatformForArchitecture(platform_arch, |
| 260 | &fixed_platform_arch); |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 261 | if (platform_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (!platform_arch.IsValid()) |
| 267 | platform_arch = arch; |
| 268 | |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 269 | return TargetList::CreateTargetInternal(debugger, user_exe_path, |
| 270 | platform_arch, load_dependent_files, |
| 271 | platform_sp, target_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 274 | Status TargetList::CreateTargetInternal(Debugger &debugger, |
| 275 | llvm::StringRef user_exe_path, |
| 276 | const ArchSpec &specified_arch, |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 277 | LoadDependentFiles load_dependent_files, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 278 | lldb::PlatformSP &platform_sp, |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 279 | lldb::TargetSP &target_sp) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 280 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
| 281 | Timer scoped_timer( |
| 282 | func_cat, "TargetList::CreateTarget (file = '%s', arch = '%s')", |
| 283 | user_exe_path.str().c_str(), specified_arch.GetArchitectureName()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 284 | Status error; |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 285 | const bool is_dummy_target = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 286 | |
| 287 | ArchSpec arch(specified_arch); |
| 288 | |
| 289 | if (arch.IsValid()) { |
| 290 | if (!platform_sp || |
| 291 | !platform_sp->IsCompatibleArchitecture(arch, false, nullptr)) |
| 292 | platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); |
| 293 | } |
| 294 | |
| 295 | if (!platform_sp) |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 296 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 297 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | if (!arch.IsValid()) |
| 299 | arch = specified_arch; |
Greg Clayton | c76fa8a | 2014-07-29 21:27:21 +0000 | [diff] [blame] | 300 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 301 | FileSpec file(user_exe_path); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 302 | if (!FileSystem::Instance().Exists(file) && user_exe_path.startswith("~")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | // we want to expand the tilde but we don't want to resolve any symbolic |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 304 | // links so we can't use the FileSpec constructor's resolve flag |
| 305 | llvm::SmallString<64> unglobbed_path; |
Jonas Devlieghere | 72787ac | 2018-11-09 01:59:28 +0000 | [diff] [blame] | 306 | StandardTildeExpressionResolver Resolver; |
| 307 | Resolver.ResolveFullPath(user_exe_path, unglobbed_path); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | if (unglobbed_path.empty()) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 310 | file = FileSpec(user_exe_path); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 311 | else |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 312 | file = FileSpec(unglobbed_path.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | bool user_exe_path_is_bundle = false; |
| 316 | char resolved_bundle_exe_path[PATH_MAX]; |
| 317 | resolved_bundle_exe_path[0] = '\0'; |
| 318 | if (file) { |
Jonas Devlieghere | 3a58d89 | 2018-11-08 00:14:50 +0000 | [diff] [blame] | 319 | if (FileSystem::Instance().IsDirectory(file)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | user_exe_path_is_bundle = true; |
| 321 | |
Zachary Turner | a47464b | 2016-11-18 20:44:46 +0000 | [diff] [blame] | 322 | if (file.IsRelative() && !user_exe_path.empty()) { |
Greg Clayton | d68dbd5 | 2018-05-07 14:21:04 +0000 | [diff] [blame] | 323 | llvm::SmallString<64> cwd; |
| 324 | if (! llvm::sys::fs::current_path(cwd)) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 325 | FileSpec cwd_file(cwd.c_str()); |
Greg Clayton | d68dbd5 | 2018-05-07 14:21:04 +0000 | [diff] [blame] | 326 | cwd_file.AppendPathComponent(file); |
Jonas Devlieghere | dbd7fab | 2018-11-01 17:09:25 +0000 | [diff] [blame] | 327 | if (FileSystem::Instance().Exists(cwd_file)) |
Greg Clayton | d68dbd5 | 2018-05-07 14:21:04 +0000 | [diff] [blame] | 328 | file = cwd_file; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 332 | ModuleSP exe_module_sp; |
| 333 | if (platform_sp) { |
| 334 | FileSpecList executable_search_paths( |
| 335 | Target::GetDefaultExecutableSearchPaths()); |
| 336 | ModuleSpec module_spec(file, arch); |
| 337 | error = platform_sp->ResolveExecutable(module_spec, exe_module_sp, |
| 338 | executable_search_paths.GetSize() |
| 339 | ? &executable_search_paths |
| 340 | : nullptr); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 341 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 342 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | if (error.Success() && exe_module_sp) { |
| 344 | if (exe_module_sp->GetObjectFile() == nullptr) { |
| 345 | if (arch.IsValid()) { |
| 346 | error.SetErrorStringWithFormat( |
| 347 | "\"%s\" doesn't contain architecture %s", file.GetPath().c_str(), |
| 348 | arch.GetArchitectureName()); |
| 349 | } else { |
| 350 | error.SetErrorStringWithFormat("unsupported file type \"%s\"", |
| 351 | file.GetPath().c_str()); |
| 352 | } |
| 353 | return error; |
| 354 | } |
| 355 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 356 | target_sp->SetExecutableModule(exe_module_sp, load_dependent_files); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | if (user_exe_path_is_bundle) |
| 358 | exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, |
| 359 | sizeof(resolved_bundle_exe_path)); |
Levon Ter-Grigoryan | 631048e | 2020-05-14 16:05:40 +0200 | [diff] [blame] | 360 | if (target_sp->GetPreloadSymbols()) |
| 361 | exe_module_sp->PreloadSymbols(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | } |
| 363 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 364 | // No file was specified, just create an empty target with any arch if a |
| 365 | // valid arch was specified |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
| 367 | } |
| 368 | |
Adrian Prantl | 5110fd0 | 2020-07-31 13:30:59 -0700 | [diff] [blame] | 369 | if (!target_sp) |
| 370 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | |
Adrian Prantl | 5110fd0 | 2020-07-31 13:30:59 -0700 | [diff] [blame] | 372 | // Set argv0 with what the user typed, unless the user specified a |
| 373 | // directory. If the user specified a directory, then it is probably a |
| 374 | // bundle that was resolved and we need to use the resolved bundle path |
| 375 | if (!user_exe_path.empty()) { |
| 376 | // Use exactly what the user typed as the first argument when we exec or |
| 377 | // posix_spawn |
| 378 | if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) { |
| 379 | target_sp->SetArg0(resolved_bundle_exe_path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 380 | } else { |
Adrian Prantl | 5110fd0 | 2020-07-31 13:30:59 -0700 | [diff] [blame] | 381 | // Use resolved path |
| 382 | target_sp->SetArg0(file.GetPath().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
Adrian Prantl | 5110fd0 | 2020-07-31 13:30:59 -0700 | [diff] [blame] | 385 | if (file.GetDirectory()) { |
| 386 | FileSpec file_dir; |
| 387 | file_dir.GetDirectory() = file.GetDirectory(); |
| 388 | target_sp->AppendExecutableSearchPaths(file_dir); |
| 389 | } |
| 390 | |
Vedant Kumar | 16e5a34 | 2020-11-05 11:38:50 -0800 | [diff] [blame] | 391 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 392 | m_selected_target_idx = m_target_list.size(); |
| 393 | m_target_list.push_back(target_sp); |
| 394 | // Now prime this from the dummy target: |
| 395 | target_sp->PrimeFromDummyTarget(debugger.GetDummyTarget()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | |
| 397 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | bool TargetList::DeleteTarget(TargetSP &target_sp) { |
| 401 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 402 | collection::iterator pos, end = m_target_list.end(); |
| 403 | |
| 404 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 405 | if (pos->get() == target_sp.get()) { |
| 406 | m_target_list.erase(pos); |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | TargetSP TargetList::FindTargetWithExecutableAndArchitecture( |
| 414 | const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const { |
| 415 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 416 | TargetSP target_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 417 | collection::const_iterator pos, end = m_target_list.end(); |
| 418 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 419 | Module *exe_module = (*pos)->GetExecutableModulePointer(); |
| 420 | |
| 421 | if (exe_module) { |
Pavel Labath | 532290e | 2019-11-29 11:31:00 +0100 | [diff] [blame] | 422 | if (FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | if (exe_arch_ptr) { |
| 424 | if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) |
| 425 | continue; |
| 426 | } |
| 427 | target_sp = *pos; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | return target_sp; |
| 433 | } |
| 434 | |
| 435 | TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const { |
| 436 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 437 | TargetSP target_sp; |
| 438 | collection::const_iterator pos, end = m_target_list.end(); |
| 439 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 440 | Process *process = (*pos)->GetProcessSP().get(); |
| 441 | if (process && process->GetID() == pid) { |
| 442 | target_sp = *pos; |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | return target_sp; |
| 447 | } |
| 448 | |
| 449 | TargetSP TargetList::FindTargetWithProcess(Process *process) const { |
| 450 | TargetSP target_sp; |
| 451 | if (process) { |
| 452 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 453 | collection::const_iterator pos, end = m_target_list.end(); |
| 454 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 455 | if (process == (*pos)->GetProcessSP().get()) { |
| 456 | target_sp = *pos; |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | return target_sp; |
| 462 | } |
| 463 | |
| 464 | TargetSP TargetList::GetTargetSP(Target *target) const { |
| 465 | TargetSP target_sp; |
| 466 | if (target) { |
| 467 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 468 | collection::const_iterator pos, end = m_target_list.end(); |
| 469 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 470 | if (target == (*pos).get()) { |
| 471 | target_sp = *pos; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | return target_sp; |
| 477 | } |
| 478 | |
| 479 | uint32_t TargetList::SendAsyncInterrupt(lldb::pid_t pid) { |
| 480 | uint32_t num_async_interrupts_sent = 0; |
| 481 | |
| 482 | if (pid != LLDB_INVALID_PROCESS_ID) { |
| 483 | TargetSP target_sp(FindTargetWithProcessID(pid)); |
| 484 | if (target_sp) { |
| 485 | Process *process = target_sp->GetProcessSP().get(); |
| 486 | if (process) { |
| 487 | process->SendAsyncInterrupt(); |
| 488 | ++num_async_interrupts_sent; |
| 489 | } |
| 490 | } |
| 491 | } else { |
| 492 | // We don't have a valid pid to broadcast to, so broadcast to the target |
| 493 | // list's async broadcaster... |
| 494 | BroadcastEvent(Process::eBroadcastBitInterrupt, nullptr); |
| 495 | } |
| 496 | |
| 497 | return num_async_interrupts_sent; |
| 498 | } |
| 499 | |
| 500 | uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) { |
| 501 | uint32_t num_signals_sent = 0; |
| 502 | Process *process = nullptr; |
| 503 | if (pid == LLDB_INVALID_PROCESS_ID) { |
| 504 | // Signal all processes with signal |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 505 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 506 | collection::iterator pos, end = m_target_list.end(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 507 | for (pos = m_target_list.begin(); pos != end; ++pos) { |
| 508 | process = (*pos)->GetProcessSP().get(); |
| 509 | if (process) { |
| 510 | if (process->IsAlive()) { |
| 511 | ++num_signals_sent; |
| 512 | process->Signal(signo); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | } else { |
| 517 | // Signal a specific process with signal |
| 518 | TargetSP target_sp(FindTargetWithProcessID(pid)); |
| 519 | if (target_sp) { |
| 520 | process = target_sp->GetProcessSP().get(); |
| 521 | if (process) { |
| 522 | if (process->IsAlive()) { |
| 523 | ++num_signals_sent; |
| 524 | process->Signal(signo); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 525 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | } |
| 529 | return num_signals_sent; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | int TargetList::GetNumTargets() const { |
| 533 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 534 | return m_target_list.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | lldb::TargetSP TargetList::GetTargetAtIndex(uint32_t idx) const { |
| 538 | TargetSP target_sp; |
| 539 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 540 | if (idx < m_target_list.size()) |
| 541 | target_sp = m_target_list[idx]; |
| 542 | return target_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | uint32_t TargetList::GetIndexOfTarget(lldb::TargetSP target_sp) const { |
| 546 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 547 | size_t num_targets = m_target_list.size(); |
| 548 | for (size_t idx = 0; idx < num_targets; idx++) { |
| 549 | if (target_sp == m_target_list[idx]) |
| 550 | return idx; |
| 551 | } |
| 552 | return UINT32_MAX; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | uint32_t TargetList::SetSelectedTarget(Target *target) { |
| 556 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 557 | collection::const_iterator pos, begin = m_target_list.begin(), |
| 558 | end = m_target_list.end(); |
| 559 | for (pos = begin; pos != end; ++pos) { |
| 560 | if (pos->get() == target) { |
| 561 | m_selected_target_idx = std::distance(begin, pos); |
| 562 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | } |
| 565 | m_selected_target_idx = 0; |
| 566 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 569 | lldb::TargetSP TargetList::GetSelectedTarget() { |
| 570 | std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); |
| 571 | if (m_selected_target_idx >= m_target_list.size()) |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 572 | m_selected_target_idx = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | return GetTargetAtIndex(m_selected_target_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 574 | } |