Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- TargetList.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Broadcaster.h" |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Event.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/State.h" |
| 22 | #include "lldb/Core/Timer.h" |
| 23 | #include "lldb/Host/Host.h" |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 24 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/OptionGroupPlatform.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/TargetList.h" |
| 31 | |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" |
| 33 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 37 | ConstString & |
| 38 | TargetList::GetStaticBroadcasterClass () |
| 39 | { |
| 40 | static ConstString class_name ("lldb.targetList"); |
| 41 | return class_name; |
| 42 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
| 44 | //---------------------------------------------------------------------- |
| 45 | // TargetList constructor |
| 46 | //---------------------------------------------------------------------- |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 47 | TargetList::TargetList(Debugger &debugger) : |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 48 | Broadcaster(&debugger, TargetList::GetStaticBroadcasterClass().AsCString()), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | m_target_list(), |
| 50 | m_target_list_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 51 | m_selected_target_idx (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 53 | CheckInWithManager(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | //---------------------------------------------------------------------- |
| 57 | // Destructor |
| 58 | //---------------------------------------------------------------------- |
| 59 | TargetList::~TargetList() |
| 60 | { |
| 61 | Mutex::Locker locker(m_target_list_mutex); |
| 62 | m_target_list.clear(); |
| 63 | } |
| 64 | |
| 65 | Error |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 66 | TargetList::CreateTarget (Debugger &debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 67 | const char *user_exe_path, |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 68 | const char *triple_cstr, |
| 69 | bool get_dependent_files, |
| 70 | const OptionGroupPlatform *platform_options, |
| 71 | TargetSP &target_sp) |
| 72 | { |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 73 | return CreateTargetInternal (debugger, |
| 74 | user_exe_path, |
| 75 | triple_cstr, |
| 76 | get_dependent_files, |
| 77 | platform_options, |
| 78 | target_sp, |
| 79 | false); |
| 80 | } |
| 81 | |
| 82 | Error |
| 83 | TargetList::CreateTarget (Debugger &debugger, |
| 84 | const char *user_exe_path, |
| 85 | const ArchSpec& specified_arch, |
| 86 | bool get_dependent_files, |
| 87 | PlatformSP &platform_sp, |
| 88 | TargetSP &target_sp) |
| 89 | { |
| 90 | return CreateTargetInternal (debugger, |
| 91 | user_exe_path, |
| 92 | specified_arch, |
| 93 | get_dependent_files, |
| 94 | platform_sp, |
| 95 | target_sp, |
| 96 | false); |
| 97 | } |
| 98 | |
| 99 | Error |
| 100 | TargetList::CreateTargetInternal (Debugger &debugger, |
| 101 | const char *user_exe_path, |
| 102 | const char *triple_cstr, |
| 103 | bool get_dependent_files, |
| 104 | const OptionGroupPlatform *platform_options, |
| 105 | TargetSP &target_sp, |
| 106 | bool is_dummy_target) |
| 107 | { |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 108 | Error error; |
| 109 | PlatformSP platform_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 110 | |
Johnny Chen | cdc21d4 | 2012-01-05 01:26:01 +0000 | [diff] [blame] | 111 | // This is purposely left empty unless it is specified by triple_cstr. |
| 112 | // If not initialized via triple_cstr, then the currently selected platform |
| 113 | // will set the architecture correctly. |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 114 | const ArchSpec arch(triple_cstr); |
| 115 | if (triple_cstr && triple_cstr[0]) |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 116 | { |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 117 | if (!arch.IsValid()) |
| 118 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 119 | error.SetErrorStringWithFormat("invalid triple '%s'", triple_cstr); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 120 | return error; |
| 121 | } |
| 122 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 123 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 124 | ArchSpec platform_arch(arch); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 125 | |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 126 | bool prefer_platform_arch = false; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 127 | |
Greg Clayton | b0cc53c | 2014-08-20 18:13:03 +0000 | [diff] [blame] | 128 | CommandInterpreter &interpreter = debugger.GetCommandInterpreter(); |
| 129 | if (platform_options && platform_options->PlatformWasSpecified ()) |
| 130 | { |
| 131 | const bool select_platform = true; |
| 132 | platform_sp = platform_options->CreatePlatformWithOptions (interpreter, |
| 133 | arch, |
| 134 | select_platform, |
| 135 | error, |
| 136 | platform_arch); |
| 137 | if (!platform_sp) |
| 138 | return error; |
| 139 | } |
| 140 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 141 | if (user_exe_path && user_exe_path[0]) |
| 142 | { |
| 143 | ModuleSpecList module_specs; |
| 144 | ModuleSpec module_spec; |
| 145 | module_spec.GetFileSpec().SetFile(user_exe_path, true); |
Greg Clayton | c76fa8a | 2014-07-29 21:27:21 +0000 | [diff] [blame] | 146 | |
| 147 | // Resolve the executable in case we are given a path to a application bundle |
| 148 | // like a .app bundle on MacOSX |
| 149 | Host::ResolveExecutableInBundle (module_spec.GetFileSpec()); |
| 150 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 151 | lldb::offset_t file_offset = 0; |
Greg Clayton | 2540a8a | 2013-07-12 22:07:46 +0000 | [diff] [blame] | 152 | lldb::offset_t file_size = 0; |
| 153 | const size_t num_specs = ObjectFile::GetModuleSpecifications (module_spec.GetFileSpec(), file_offset, file_size, module_specs); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 154 | if (num_specs > 0) |
| 155 | { |
| 156 | ModuleSpec matching_module_spec; |
| 157 | |
| 158 | if (num_specs == 1) |
| 159 | { |
| 160 | if (module_specs.GetModuleSpecAtIndex(0, matching_module_spec)) |
| 161 | { |
| 162 | if (platform_arch.IsValid()) |
| 163 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 164 | if (platform_arch.IsCompatibleMatch(matching_module_spec.GetArchitecture())) |
| 165 | { |
| 166 | // If the OS or vendor weren't specified, then adopt the module's |
| 167 | // architecture so that the platform matching can be more accurate |
| 168 | if (!platform_arch.TripleOSWasSpecified() || !platform_arch.TripleVendorWasSpecified()) |
| 169 | { |
| 170 | prefer_platform_arch = true; |
| 171 | platform_arch = matching_module_spec.GetArchitecture(); |
| 172 | } |
| 173 | } |
| 174 | else |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 175 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 176 | error.SetErrorStringWithFormat("the specified architecture '%s' is not compatible with '%s' in '%s'", |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 177 | platform_arch.GetTriple().str().c_str(), |
| 178 | matching_module_spec.GetArchitecture().GetTriple().str().c_str(), |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 179 | module_spec.GetFileSpec().GetPath().c_str()); |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 180 | return error; |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | // Only one arch and none was specified |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 186 | prefer_platform_arch = true; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 187 | platform_arch = matching_module_spec.GetArchitecture(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if (arch.IsValid()) |
| 194 | { |
| 195 | module_spec.GetArchitecture() = arch; |
| 196 | if (module_specs.FindMatchingModuleSpec(module_spec, matching_module_spec)) |
| 197 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 198 | prefer_platform_arch = true; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 199 | platform_arch = matching_module_spec.GetArchitecture(); |
| 200 | } |
| 201 | } |
Greg Clayton | b0cc53c | 2014-08-20 18:13:03 +0000 | [diff] [blame] | 202 | else |
| 203 | { |
| 204 | // No architecture specified, check if there is only one platform for |
| 205 | // all of the architectures. |
| 206 | |
| 207 | typedef std::vector<PlatformSP> PlatformList; |
| 208 | PlatformList platforms; |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 209 | PlatformSP host_platform_sp = Platform::GetHostPlatform(); |
Greg Clayton | b0cc53c | 2014-08-20 18:13:03 +0000 | [diff] [blame] | 210 | for (size_t i=0; i<num_specs; ++i) |
| 211 | { |
| 212 | ModuleSpec module_spec; |
| 213 | if (module_specs.GetModuleSpecAtIndex(i, module_spec)) |
| 214 | { |
| 215 | // See if there was a selected platform and check that first |
| 216 | // since the user may have specified it. |
| 217 | if (platform_sp) |
| 218 | { |
| 219 | if (platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL)) |
| 220 | { |
| 221 | platforms.push_back(platform_sp); |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Next check the host platform it if wasn't already checked above |
| 227 | if (host_platform_sp && (!platform_sp || host_platform_sp->GetName() != platform_sp->GetName())) |
| 228 | { |
| 229 | if (host_platform_sp->IsCompatibleArchitecture(module_spec.GetArchitecture(), false, NULL)) |
| 230 | { |
| 231 | platforms.push_back(host_platform_sp); |
| 232 | continue; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Just find a platform that matches the architecture in the executable file |
| 237 | platforms.push_back(Platform::GetPlatformForArchitecture(module_spec.GetArchitecture(), nullptr)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | Platform *platform_ptr = NULL; |
| 242 | for (const auto &the_platform_sp : platforms) |
| 243 | { |
| 244 | if (platform_ptr) |
| 245 | { |
| 246 | if (platform_ptr->GetName() != the_platform_sp->GetName()) |
| 247 | { |
| 248 | platform_ptr = NULL; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | platform_ptr = the_platform_sp.get(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (platform_ptr) |
| 259 | { |
| 260 | // All platforms for all modules in the exectuable match, so we can select this platform |
| 261 | platform_sp = platforms.front(); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | // More than one platform claims to support this file, so the --platform option must be specified |
| 266 | StreamString error_strm; |
| 267 | std::set<Platform *> platform_set; |
| 268 | error_strm.Printf ("more than one platform supports this executable ("); |
| 269 | for (const auto &the_platform_sp : platforms) |
| 270 | { |
| 271 | if (platform_set.find(the_platform_sp.get()) == platform_set.end()) |
| 272 | { |
| 273 | if (!platform_set.empty()) |
| 274 | error_strm.PutCString(", "); |
| 275 | error_strm.PutCString(the_platform_sp->GetName().GetCString()); |
| 276 | platform_set.insert(the_platform_sp.get()); |
| 277 | } |
| 278 | } |
| 279 | error_strm.Printf("), use the --platform option to specify a platform"); |
| 280 | error.SetErrorString(error_strm.GetString().c_str()); |
| 281 | return error; |
| 282 | } |
| 283 | } |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 288 | if (!platform_sp) |
| 289 | { |
| 290 | // Get the current platform and make sure it is compatible with the |
| 291 | // current architecture if we have a valid architecture. |
| 292 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform (); |
| 293 | |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 294 | if (!prefer_platform_arch && arch.IsValid()) |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 295 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 296 | if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) |
Greg Clayton | 95bbdf6 | 2014-09-17 16:42:50 +0000 | [diff] [blame] | 297 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 298 | platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch); |
Greg Clayton | 95bbdf6 | 2014-09-17 16:42:50 +0000 | [diff] [blame] | 299 | if (platform_sp) |
| 300 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 301 | } |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 302 | } |
| 303 | else if (platform_arch.IsValid()) |
| 304 | { |
| 305 | // if "arch" isn't valid, yet "platform_arch" is, it means we have an executable file with |
| 306 | // a single architecture which should be used |
| 307 | ArchSpec fixed_platform_arch; |
| 308 | if (!platform_sp->IsCompatibleArchitecture(platform_arch, false, &fixed_platform_arch)) |
Greg Clayton | 95bbdf6 | 2014-09-17 16:42:50 +0000 | [diff] [blame] | 309 | { |
Greg Clayton | 3f19ada | 2014-07-10 23:33:37 +0000 | [diff] [blame] | 310 | platform_sp = Platform::GetPlatformForArchitecture(platform_arch, &fixed_platform_arch); |
Greg Clayton | 95bbdf6 | 2014-09-17 16:42:50 +0000 | [diff] [blame] | 311 | if (platform_sp) |
| 312 | debugger.GetPlatformList().SetSelectedPlatform(platform_sp); |
| 313 | } |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 316 | |
| 317 | if (!platform_arch.IsValid()) |
| 318 | platform_arch = arch; |
Greg Clayton | b3a40ba | 2012-03-20 18:34:04 +0000 | [diff] [blame] | 319 | |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 320 | error = TargetList::CreateTargetInternal (debugger, |
| 321 | user_exe_path, |
| 322 | platform_arch, |
| 323 | get_dependent_files, |
| 324 | platform_sp, |
| 325 | target_sp, |
| 326 | is_dummy_target); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 327 | return error; |
| 328 | } |
| 329 | |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 330 | lldb::TargetSP |
| 331 | TargetList::GetDummyTarget (lldb_private::Debugger &debugger) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 332 | { |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 333 | // FIXME: Maybe the dummy target should be per-Debugger |
| 334 | if (!m_dummy_target_sp || !m_dummy_target_sp->IsValid()) |
| 335 | { |
| 336 | ArchSpec arch(Target::GetDefaultArchitecture()); |
| 337 | if (!arch.IsValid()) |
| 338 | arch = HostInfo::GetArchitecture(); |
| 339 | Error err = CreateDummyTarget(debugger, |
| 340 | arch.GetTriple().getTriple().c_str(), |
| 341 | m_dummy_target_sp); |
| 342 | } |
| 343 | |
| 344 | return m_dummy_target_sp; |
| 345 | } |
| 346 | |
| 347 | Error |
| 348 | TargetList::CreateDummyTarget (Debugger &debugger, |
| 349 | const char *specified_arch_name, |
| 350 | lldb::TargetSP &target_sp) |
| 351 | { |
| 352 | PlatformSP host_platform_sp(Platform::GetHostPlatform()); |
| 353 | return CreateTargetInternal (debugger, |
| 354 | (const char *) nullptr, |
| 355 | specified_arch_name, |
| 356 | false, |
| 357 | (const OptionGroupPlatform *) nullptr, |
| 358 | target_sp, |
| 359 | true); |
| 360 | } |
| 361 | |
| 362 | Error |
| 363 | TargetList::CreateTargetInternal (Debugger &debugger, |
| 364 | const char *user_exe_path, |
| 365 | const ArchSpec& specified_arch, |
| 366 | bool get_dependent_files, |
| 367 | lldb::PlatformSP &platform_sp, |
| 368 | lldb::TargetSP &target_sp, |
| 369 | bool is_dummy_target) |
| 370 | { |
| 371 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | Timer scoped_timer (__PRETTY_FUNCTION__, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 373 | "TargetList::CreateTarget (file = '%s', arch = '%s')", |
| 374 | user_exe_path, |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 375 | specified_arch.GetArchitectureName()); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 376 | Error error; |
Greg Clayton | ded470d | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 377 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 378 | ArchSpec arch(specified_arch); |
| 379 | |
Greg Clayton | b0cc53c | 2014-08-20 18:13:03 +0000 | [diff] [blame] | 380 | if (arch.IsValid()) |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 381 | { |
Greg Clayton | b0cc53c | 2014-08-20 18:13:03 +0000 | [diff] [blame] | 382 | if (!platform_sp || !platform_sp->IsCompatibleArchitecture(arch, false, NULL)) |
| 383 | platform_sp = Platform::GetPlatformForArchitecture(specified_arch, &arch); |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 384 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 385 | |
Greg Clayton | 7051231 | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 386 | if (!platform_sp) |
| 387 | platform_sp = debugger.GetPlatformList().GetSelectedPlatform(); |
| 388 | |
Greg Clayton | 8ae50eb | 2012-06-05 21:17:09 +0000 | [diff] [blame] | 389 | if (!arch.IsValid()) |
| 390 | arch = specified_arch; |
Greg Clayton | 8ae50eb | 2012-06-05 21:17:09 +0000 | [diff] [blame] | 391 | |
Jason Molenda | d26206b5 | 2013-04-19 22:38:50 +0000 | [diff] [blame] | 392 | FileSpec file (user_exe_path, false); |
| 393 | if (!file.Exists() && user_exe_path && user_exe_path[0] == '~') |
| 394 | { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 395 | // we want to expand the tilde but we don't want to resolve any symbolic links |
| 396 | // so we can't use the FileSpec constructor's resolve flag |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 397 | llvm::SmallString<64> unglobbed_path(user_exe_path); |
| 398 | FileSpec::ResolveUsername(unglobbed_path); |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 399 | |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 400 | if (unglobbed_path.empty()) |
| 401 | file = FileSpec(user_exe_path, false); |
| 402 | else |
| 403 | file = FileSpec(unglobbed_path.c_str(), false); |
Jason Molenda | d26206b5 | 2013-04-19 22:38:50 +0000 | [diff] [blame] | 404 | } |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 405 | |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 406 | bool user_exe_path_is_bundle = false; |
| 407 | char resolved_bundle_exe_path[PATH_MAX]; |
| 408 | resolved_bundle_exe_path[0] = '\0'; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 409 | if (file) |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 410 | { |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 411 | if (file.GetFileType() == FileSpec::eFileTypeDirectory) |
| 412 | user_exe_path_is_bundle = true; |
| 413 | |
Jason Molenda | 1446881 | 2014-10-16 01:42:11 +0000 | [diff] [blame] | 414 | if (file.IsRelativeToCurrentWorkingDirectory() && user_exe_path) |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 415 | { |
| 416 | // Ignore paths that start with "./" and "../" |
| 417 | if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') || |
| 418 | (user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/'))) |
| 419 | { |
| 420 | char cwd[PATH_MAX]; |
| 421 | if (getcwd (cwd, sizeof(cwd))) |
| 422 | { |
| 423 | std::string cwd_user_exe_path (cwd); |
| 424 | cwd_user_exe_path += '/'; |
| 425 | cwd_user_exe_path += user_exe_path; |
Greg Clayton | 9ff5aae | 2013-04-04 00:15:09 +0000 | [diff] [blame] | 426 | FileSpec cwd_file (cwd_user_exe_path.c_str(), false); |
| 427 | if (cwd_file.Exists()) |
| 428 | file = cwd_file; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 433 | ModuleSP exe_module_sp; |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 434 | if (platform_sp) |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 435 | { |
| 436 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 437 | ModuleSpec module_spec(file, arch); |
| 438 | error = platform_sp->ResolveExecutable (module_spec, |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 439 | exe_module_sp, |
| 440 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 441 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 443 | if (error.Success() && exe_module_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 444 | { |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 445 | if (exe_module_sp->GetObjectFile() == NULL) |
| 446 | { |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 447 | if (arch.IsValid()) |
| 448 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 449 | error.SetErrorStringWithFormat("\"%s\" doesn't contain architecture %s", |
| 450 | file.GetPath().c_str(), |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 451 | arch.GetArchitectureName()); |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 452 | } |
| 453 | else |
| 454 | { |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 455 | error.SetErrorStringWithFormat("unsupported file type \"%s\"", |
| 456 | file.GetPath().c_str()); |
Greg Clayton | bc5cad6 | 2010-12-08 04:55:11 +0000 | [diff] [blame] | 457 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 458 | return error; |
| 459 | } |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 460 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 461 | target_sp->SetExecutableModule (exe_module_sp, get_dependent_files); |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 462 | if (user_exe_path_is_bundle) |
| 463 | exe_module_sp->GetFileSpec().GetPath(resolved_bundle_exe_path, sizeof(resolved_bundle_exe_path)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 465 | } |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 466 | else |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 467 | { |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 468 | // No file was specified, just create an empty target with any arch |
| 469 | // if a valid arch was specified |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 470 | target_sp.reset(new Target(debugger, arch, platform_sp, is_dummy_target)); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | if (target_sp) |
| 474 | { |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 475 | // Set argv0 with what the user typed, unless the user specified a |
| 476 | // directory. If the user specified a directory, then it is probably a |
| 477 | // bundle that was resolved and we need to use the resolved bundle path |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 478 | if (user_exe_path) |
| 479 | { |
| 480 | // Use exactly what the user typed as the first argument when we exec or posix_spawn |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 481 | if (user_exe_path_is_bundle && resolved_bundle_exe_path[0]) |
| 482 | { |
| 483 | target_sp->SetArg0 (resolved_bundle_exe_path); |
| 484 | } |
| 485 | else |
| 486 | { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 487 | // Use resolved path |
| 488 | target_sp->SetArg0 (file.GetPath().c_str()); |
Greg Clayton | 82d7929 | 2012-10-25 22:45:35 +0000 | [diff] [blame] | 489 | } |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 490 | } |
| 491 | if (file.GetDirectory()) |
| 492 | { |
| 493 | FileSpec file_dir; |
| 494 | file_dir.GetDirectory() = file.GetDirectory(); |
| 495 | target_sp->GetExecutableSearchPaths ().Append (file_dir); |
| 496 | } |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame^] | 497 | |
| 498 | // Don't put the dummy target in the target list, it's held separately. |
| 499 | if (!is_dummy_target) |
| 500 | { |
| 501 | Mutex::Locker locker(m_target_list_mutex); |
| 502 | m_selected_target_idx = m_target_list.size(); |
| 503 | m_target_list.push_back(target_sp); |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | m_dummy_target_sp = target_sp; |
| 508 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 509 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | return error; |
| 512 | } |
| 513 | |
| 514 | bool |
| 515 | TargetList::DeleteTarget (TargetSP &target_sp) |
| 516 | { |
| 517 | Mutex::Locker locker(m_target_list_mutex); |
| 518 | collection::iterator pos, end = m_target_list.end(); |
| 519 | |
| 520 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 521 | { |
| 522 | if (pos->get() == target_sp.get()) |
| 523 | { |
| 524 | m_target_list.erase(pos); |
| 525 | return true; |
| 526 | } |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | TargetSP |
| 533 | TargetList::FindTargetWithExecutableAndArchitecture |
| 534 | ( |
| 535 | const FileSpec &exe_file_spec, |
| 536 | const ArchSpec *exe_arch_ptr |
| 537 | ) const |
| 538 | { |
| 539 | Mutex::Locker locker (m_target_list_mutex); |
| 540 | TargetSP target_sp; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 541 | bool full_match = (bool)exe_file_spec.GetDirectory(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | |
| 543 | collection::const_iterator pos, end = m_target_list.end(); |
| 544 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 545 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 546 | Module *exe_module = (*pos)->GetExecutableModulePointer(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 548 | if (exe_module) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | { |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 550 | if (FileSpec::Equal (exe_file_spec, exe_module->GetFileSpec(), full_match)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 551 | { |
| 552 | if (exe_arch_ptr) |
| 553 | { |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 554 | if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | continue; |
| 556 | } |
| 557 | target_sp = *pos; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | return target_sp; |
| 563 | } |
| 564 | |
| 565 | TargetSP |
| 566 | TargetList::FindTargetWithProcessID (lldb::pid_t pid) const |
| 567 | { |
| 568 | Mutex::Locker locker(m_target_list_mutex); |
| 569 | TargetSP target_sp; |
| 570 | collection::const_iterator pos, end = m_target_list.end(); |
| 571 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 572 | { |
| 573 | Process* process = (*pos)->GetProcessSP().get(); |
| 574 | if (process && process->GetID() == pid) |
| 575 | { |
| 576 | target_sp = *pos; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | return target_sp; |
| 581 | } |
| 582 | |
| 583 | |
| 584 | TargetSP |
| 585 | TargetList::FindTargetWithProcess (Process *process) const |
| 586 | { |
| 587 | TargetSP target_sp; |
| 588 | if (process) |
| 589 | { |
| 590 | Mutex::Locker locker(m_target_list_mutex); |
| 591 | collection::const_iterator pos, end = m_target_list.end(); |
| 592 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 593 | { |
| 594 | if (process == (*pos)->GetProcessSP().get()) |
| 595 | { |
| 596 | target_sp = *pos; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | return target_sp; |
| 602 | } |
| 603 | |
| 604 | TargetSP |
| 605 | TargetList::GetTargetSP (Target *target) const |
| 606 | { |
| 607 | TargetSP target_sp; |
| 608 | if (target) |
| 609 | { |
| 610 | Mutex::Locker locker(m_target_list_mutex); |
| 611 | collection::const_iterator pos, end = m_target_list.end(); |
| 612 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 613 | { |
| 614 | if (target == (*pos).get()) |
| 615 | { |
| 616 | target_sp = *pos; |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | return target_sp; |
| 622 | } |
| 623 | |
| 624 | uint32_t |
| 625 | TargetList::SendAsyncInterrupt (lldb::pid_t pid) |
| 626 | { |
| 627 | uint32_t num_async_interrupts_sent = 0; |
| 628 | |
| 629 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 630 | { |
| 631 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 632 | if (target_sp.get()) |
| 633 | { |
| 634 | Process* process = target_sp->GetProcessSP().get(); |
| 635 | if (process) |
| 636 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 637 | process->SendAsyncInterrupt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 638 | ++num_async_interrupts_sent; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | // We don't have a valid pid to broadcast to, so broadcast to the target |
| 645 | // list's async broadcaster... |
| 646 | BroadcastEvent (Process::eBroadcastBitInterrupt, NULL); |
| 647 | } |
| 648 | |
| 649 | return num_async_interrupts_sent; |
| 650 | } |
| 651 | |
| 652 | uint32_t |
| 653 | TargetList::SignalIfRunning (lldb::pid_t pid, int signo) |
| 654 | { |
| 655 | uint32_t num_signals_sent = 0; |
| 656 | Process *process = NULL; |
| 657 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 658 | { |
| 659 | // Signal all processes with signal |
| 660 | Mutex::Locker locker(m_target_list_mutex); |
| 661 | collection::iterator pos, end = m_target_list.end(); |
| 662 | for (pos = m_target_list.begin(); pos != end; ++pos) |
| 663 | { |
| 664 | process = (*pos)->GetProcessSP().get(); |
| 665 | if (process) |
| 666 | { |
| 667 | if (process->IsAlive()) |
| 668 | { |
| 669 | ++num_signals_sent; |
| 670 | process->Signal (signo); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | // Signal a specific process with signal |
| 678 | TargetSP target_sp(FindTargetWithProcessID (pid)); |
| 679 | if (target_sp.get()) |
| 680 | { |
| 681 | process = target_sp->GetProcessSP().get(); |
| 682 | if (process) |
| 683 | { |
| 684 | if (process->IsAlive()) |
| 685 | { |
| 686 | ++num_signals_sent; |
| 687 | process->Signal (signo); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | return num_signals_sent; |
| 693 | } |
| 694 | |
| 695 | int |
| 696 | TargetList::GetNumTargets () const |
| 697 | { |
| 698 | Mutex::Locker locker (m_target_list_mutex); |
| 699 | return m_target_list.size(); |
| 700 | } |
| 701 | |
| 702 | lldb::TargetSP |
| 703 | TargetList::GetTargetAtIndex (uint32_t idx) const |
| 704 | { |
| 705 | TargetSP target_sp; |
| 706 | Mutex::Locker locker (m_target_list_mutex); |
| 707 | if (idx < m_target_list.size()) |
| 708 | target_sp = m_target_list[idx]; |
| 709 | return target_sp; |
| 710 | } |
| 711 | |
| 712 | uint32_t |
Jim Ingham | 8499e1a | 2012-05-08 23:06:07 +0000 | [diff] [blame] | 713 | TargetList::GetIndexOfTarget (lldb::TargetSP target_sp) const |
| 714 | { |
| 715 | Mutex::Locker locker (m_target_list_mutex); |
| 716 | size_t num_targets = m_target_list.size(); |
| 717 | for (size_t idx = 0; idx < num_targets; idx++) |
| 718 | { |
| 719 | if (target_sp == m_target_list[idx]) |
| 720 | return idx; |
| 721 | } |
| 722 | return UINT32_MAX; |
| 723 | } |
| 724 | |
| 725 | uint32_t |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 726 | TargetList::SetSelectedTarget (Target* target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 727 | { |
| 728 | Mutex::Locker locker (m_target_list_mutex); |
| 729 | collection::const_iterator pos, |
| 730 | begin = m_target_list.begin(), |
| 731 | end = m_target_list.end(); |
| 732 | for (pos = begin; pos != end; ++pos) |
| 733 | { |
| 734 | if (pos->get() == target) |
| 735 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 736 | m_selected_target_idx = std::distance (begin, pos); |
| 737 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 740 | m_selected_target_idx = 0; |
| 741 | return m_selected_target_idx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | lldb::TargetSP |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 745 | TargetList::GetSelectedTarget () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | { |
| 747 | Mutex::Locker locker (m_target_list_mutex); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 748 | if (m_selected_target_idx >= m_target_list.size()) |
| 749 | m_selected_target_idx = 0; |
| 750 | return GetTargetAtIndex (m_selected_target_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | } |