1 //===-- PlatformWindows.cpp -------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "PlatformWindows.h" 10 11 #include <stdio.h> 12 #if defined(_WIN32) 13 #include "lldb/Host/windows/windows.h" 14 #include <winsock2.h> 15 #endif 16 17 #include "lldb/Breakpoint/BreakpointLocation.h" 18 #include "lldb/Breakpoint/BreakpointSite.h" 19 #include "lldb/Core/Debugger.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/ModuleSpec.h" 22 #include "lldb/Core/PluginManager.h" 23 #include "lldb/Host/HostInfo.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Utility/Status.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 static uint32_t g_initialize_count = 0; 31 32 namespace { 33 class SupportedArchList { 34 public: 35 SupportedArchList() { 36 AddArch(ArchSpec("i686-pc-windows")); 37 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKindDefault)); 38 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind32)); 39 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind64)); 40 AddArch(ArchSpec("i386-pc-windows")); 41 } 42 43 size_t Count() const { return m_archs.size(); } 44 45 const ArchSpec &operator[](int idx) { return m_archs[idx]; } 46 47 private: 48 void AddArch(const ArchSpec &spec) { 49 auto iter = std::find_if( 50 m_archs.begin(), m_archs.end(), 51 [spec](const ArchSpec &rhs) { return spec.IsExactMatch(rhs); }); 52 if (iter != m_archs.end()) 53 return; 54 if (spec.IsValid()) 55 m_archs.push_back(spec); 56 } 57 58 std::vector<ArchSpec> m_archs; 59 }; 60 } // anonymous namespace 61 62 PlatformSP PlatformWindows::CreateInstance(bool force, 63 const lldb_private::ArchSpec *arch) { 64 // The only time we create an instance is when we are creating a remote 65 // windows platform 66 const bool is_host = false; 67 68 bool create = force; 69 if (!create && arch && arch->IsValid()) { 70 const llvm::Triple &triple = arch->GetTriple(); 71 switch (triple.getVendor()) { 72 case llvm::Triple::PC: 73 create = true; 74 break; 75 76 case llvm::Triple::UnknownVendor: 77 create = !arch->TripleVendorWasSpecified(); 78 break; 79 80 default: 81 break; 82 } 83 84 if (create) { 85 switch (triple.getOS()) { 86 case llvm::Triple::Win32: 87 break; 88 89 case llvm::Triple::UnknownOS: 90 create = arch->TripleOSWasSpecified(); 91 break; 92 93 default: 94 create = false; 95 break; 96 } 97 } 98 } 99 if (create) 100 return PlatformSP(new PlatformWindows(is_host)); 101 return PlatformSP(); 102 } 103 104 lldb_private::ConstString PlatformWindows::GetPluginNameStatic(bool is_host) { 105 if (is_host) { 106 static ConstString g_host_name(Platform::GetHostPlatformName()); 107 return g_host_name; 108 } else { 109 static ConstString g_remote_name("remote-windows"); 110 return g_remote_name; 111 } 112 } 113 114 const char *PlatformWindows::GetPluginDescriptionStatic(bool is_host) { 115 return is_host ? "Local Windows user platform plug-in." 116 : "Remote Windows user platform plug-in."; 117 } 118 119 lldb_private::ConstString PlatformWindows::GetPluginName() { 120 return GetPluginNameStatic(IsHost()); 121 } 122 123 void PlatformWindows::Initialize() { 124 Platform::Initialize(); 125 126 if (g_initialize_count++ == 0) { 127 #if defined(_WIN32) 128 // Force a host flag to true for the default platform object. 129 PlatformSP default_platform_sp(new PlatformWindows(true)); 130 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 131 Platform::SetHostPlatform(default_platform_sp); 132 #endif 133 PluginManager::RegisterPlugin( 134 PlatformWindows::GetPluginNameStatic(false), 135 PlatformWindows::GetPluginDescriptionStatic(false), 136 PlatformWindows::CreateInstance); 137 } 138 } 139 140 void PlatformWindows::Terminate() { 141 if (g_initialize_count > 0) { 142 if (--g_initialize_count == 0) { 143 PluginManager::UnregisterPlugin(PlatformWindows::CreateInstance); 144 } 145 } 146 147 Platform::Terminate(); 148 } 149 150 //------------------------------------------------------------------ 151 /// Default Constructor 152 //------------------------------------------------------------------ 153 PlatformWindows::PlatformWindows(bool is_host) : RemoteAwarePlatform(is_host) {} 154 155 //------------------------------------------------------------------ 156 /// Destructor. 157 /// 158 /// The destructor is virtual since this class is designed to be 159 /// inherited from by the plug-in instance. 160 //------------------------------------------------------------------ 161 PlatformWindows::~PlatformWindows() = default; 162 163 Status PlatformWindows::ResolveExecutable( 164 const ModuleSpec &ms, lldb::ModuleSP &exe_module_sp, 165 const FileSpecList *module_search_paths_ptr) { 166 Status error; 167 // Nothing special to do here, just use the actual file and architecture 168 169 char exe_path[PATH_MAX]; 170 ModuleSpec resolved_module_spec(ms); 171 172 if (IsHost()) { 173 // if we cant resolve the executable loation based on the current path 174 // variables 175 if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) { 176 resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 177 resolved_module_spec.GetFileSpec().SetFile(exe_path, 178 FileSpec::Style::native); 179 FileSystem::Instance().Resolve(resolved_module_spec.GetFileSpec()); 180 } 181 182 if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) 183 FileSystem::Instance().ResolveExecutableLocation( 184 resolved_module_spec.GetFileSpec()); 185 186 if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) 187 error.Clear(); 188 else { 189 ms.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 190 error.SetErrorStringWithFormat("unable to find executable for '%s'", 191 exe_path); 192 } 193 } else { 194 if (m_remote_platform_sp) { 195 error = 196 GetCachedExecutable(resolved_module_spec, exe_module_sp, 197 module_search_paths_ptr, *m_remote_platform_sp); 198 } else { 199 // We may connect to a process and use the provided executable (Don't use 200 // local $PATH). 201 if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) 202 error.Clear(); 203 else 204 error.SetErrorStringWithFormat("the platform is not currently " 205 "connected, and '%s' doesn't exist in " 206 "the system root.", 207 exe_path); 208 } 209 } 210 211 if (error.Success()) { 212 if (resolved_module_spec.GetArchitecture().IsValid()) { 213 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, 214 nullptr, nullptr, nullptr); 215 216 if (!exe_module_sp || exe_module_sp->GetObjectFile() == nullptr) { 217 exe_module_sp.reset(); 218 error.SetErrorStringWithFormat( 219 "'%s' doesn't contain the architecture %s", 220 resolved_module_spec.GetFileSpec().GetPath().c_str(), 221 resolved_module_spec.GetArchitecture().GetArchitectureName()); 222 } 223 } else { 224 // No valid architecture was specified, ask the platform for the 225 // architectures that we should be using (in the correct order) and see 226 // if we can find a match that way 227 StreamString arch_names; 228 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex( 229 idx, resolved_module_spec.GetArchitecture()); 230 ++idx) { 231 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, 232 module_search_paths_ptr, nullptr, 233 nullptr); 234 // Did we find an executable using one of the 235 if (error.Success()) { 236 if (exe_module_sp && exe_module_sp->GetObjectFile()) 237 break; 238 else 239 error.SetErrorToGenericError(); 240 } 241 242 if (idx > 0) 243 arch_names.PutCString(", "); 244 arch_names.PutCString( 245 resolved_module_spec.GetArchitecture().GetArchitectureName()); 246 } 247 248 if (error.Fail() || !exe_module_sp) { 249 if (FileSystem::Instance().Readable( 250 resolved_module_spec.GetFileSpec())) { 251 error.SetErrorStringWithFormat( 252 "'%s' doesn't contain any '%s' platform architectures: %s", 253 resolved_module_spec.GetFileSpec().GetPath().c_str(), 254 GetPluginName().GetCString(), arch_names.GetData()); 255 } else { 256 error.SetErrorStringWithFormat( 257 "'%s' is not readable", 258 resolved_module_spec.GetFileSpec().GetPath().c_str()); 259 } 260 } 261 } 262 } 263 264 return error; 265 } 266 267 Status PlatformWindows::ConnectRemote(Args &args) { 268 Status error; 269 if (IsHost()) { 270 error.SetErrorStringWithFormat( 271 "can't connect to the host platform '%s', always connected", 272 GetPluginName().AsCString()); 273 } else { 274 if (!m_remote_platform_sp) 275 m_remote_platform_sp = 276 Platform::Create(ConstString("remote-gdb-server"), error); 277 278 if (m_remote_platform_sp) { 279 if (error.Success()) { 280 if (m_remote_platform_sp) { 281 error = m_remote_platform_sp->ConnectRemote(args); 282 } else { 283 error.SetErrorString( 284 "\"platform connect\" takes a single argument: <connect-url>"); 285 } 286 } 287 } else 288 error.SetErrorString("failed to create a 'remote-gdb-server' platform"); 289 290 if (error.Fail()) 291 m_remote_platform_sp.reset(); 292 } 293 294 return error; 295 } 296 297 Status PlatformWindows::DisconnectRemote() { 298 Status error; 299 300 if (IsHost()) { 301 error.SetErrorStringWithFormat( 302 "can't disconnect from the host platform '%s', always connected", 303 GetPluginName().AsCString()); 304 } else { 305 if (m_remote_platform_sp) 306 error = m_remote_platform_sp->DisconnectRemote(); 307 else 308 error.SetErrorString("the platform is not currently connected"); 309 } 310 return error; 311 } 312 313 ProcessSP PlatformWindows::DebugProcess(ProcessLaunchInfo &launch_info, 314 Debugger &debugger, Target *target, 315 Status &error) { 316 // Windows has special considerations that must be followed when launching or 317 // attaching to a process. The key requirement is that when launching or 318 // attaching to a process, you must do it from the same the thread that will 319 // go into a permanent loop which will then receive debug events from the 320 // process. In particular, this means we can't use any of LLDB's generic 321 // mechanisms to do it for us, because it doesn't have the special knowledge 322 // required for setting up the background thread or passing the right flags. 323 // 324 // Another problem is that that LLDB's standard model for debugging a process 325 // is to first launch it, have it stop at the entry point, and then attach to 326 // it. In Windows this doesn't quite work, you have to specify as an 327 // argument to CreateProcess() that you're going to debug the process. So we 328 // override DebugProcess here to handle this. Launch operations go directly 329 // to the process plugin, and attach operations almost go directly to the 330 // process plugin (but we hijack the events first). In essence, we 331 // encapsulate all the logic of Launching and Attaching in the process 332 // plugin, and PlatformWindows::DebugProcess is just a pass-through to get to 333 // the process plugin. 334 335 if (IsRemote()) { 336 if (m_remote_platform_sp) 337 return m_remote_platform_sp->DebugProcess(launch_info, debugger, target, 338 error); 339 else 340 error.SetErrorString("the platform is not currently connected"); 341 } 342 343 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) { 344 // This is a process attach. Don't need to launch anything. 345 ProcessAttachInfo attach_info(launch_info); 346 return Attach(attach_info, debugger, target, error); 347 } else { 348 ProcessSP process_sp = target->CreateProcess( 349 launch_info.GetListener(), launch_info.GetProcessPluginName(), nullptr); 350 351 // We need to launch and attach to the process. 352 launch_info.GetFlags().Set(eLaunchFlagDebug); 353 if (process_sp) 354 error = process_sp->Launch(launch_info); 355 356 return process_sp; 357 } 358 } 359 360 lldb::ProcessSP PlatformWindows::Attach(ProcessAttachInfo &attach_info, 361 Debugger &debugger, Target *target, 362 Status &error) { 363 error.Clear(); 364 lldb::ProcessSP process_sp; 365 if (!IsHost()) { 366 if (m_remote_platform_sp) 367 process_sp = 368 m_remote_platform_sp->Attach(attach_info, debugger, target, error); 369 else 370 error.SetErrorString("the platform is not currently connected"); 371 return process_sp; 372 } 373 374 if (target == nullptr) { 375 TargetSP new_target_sp; 376 FileSpec emptyFileSpec; 377 ArchSpec emptyArchSpec; 378 379 error = debugger.GetTargetList().CreateTarget( 380 debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp); 381 target = new_target_sp.get(); 382 } 383 384 if (!target || error.Fail()) 385 return process_sp; 386 387 debugger.GetTargetList().SetSelectedTarget(target); 388 389 const char *plugin_name = attach_info.GetProcessPluginName(); 390 process_sp = target->CreateProcess( 391 attach_info.GetListenerForProcess(debugger), plugin_name, nullptr); 392 393 process_sp->HijackProcessEvents(attach_info.GetHijackListener()); 394 if (process_sp) 395 error = process_sp->Attach(attach_info); 396 397 return process_sp; 398 } 399 400 bool PlatformWindows::GetSupportedArchitectureAtIndex(uint32_t idx, 401 ArchSpec &arch) { 402 static SupportedArchList architectures; 403 404 if (idx >= architectures.Count()) 405 return false; 406 arch = architectures[idx]; 407 return true; 408 } 409 410 void PlatformWindows::GetStatus(Stream &strm) { 411 Platform::GetStatus(strm); 412 413 #ifdef _WIN32 414 llvm::VersionTuple version = HostInfo::GetOSVersion(); 415 strm << " Host: Windows " << version.getAsString() << '\n'; 416 #endif 417 } 418 419 bool PlatformWindows::CanDebugProcess() { return true; } 420 421 ConstString PlatformWindows::GetFullNameForDylib(ConstString basename) { 422 if (basename.IsEmpty()) 423 return basename; 424 425 StreamString stream; 426 stream.Printf("%s.dll", basename.GetCString()); 427 return ConstString(stream.GetString()); 428 } 429