1 //===-- PlatformWindows.cpp -----------------------------------------------===// 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 <cstdio> 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/PluginManager.h" 22 #include "lldb/Host/HostInfo.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Utility/Status.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 LLDB_PLUGIN_DEFINE(PlatformWindows) 30 31 static uint32_t g_initialize_count = 0; 32 33 namespace { 34 class SupportedArchList { 35 public: 36 SupportedArchList() { 37 AddArch(ArchSpec("i686-pc-windows")); 38 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKindDefault)); 39 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind32)); 40 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind64)); 41 AddArch(ArchSpec("i386-pc-windows")); 42 } 43 44 size_t Count() const { return m_archs.size(); } 45 46 const ArchSpec &operator[](int idx) { return m_archs[idx]; } 47 48 private: 49 void AddArch(const ArchSpec &spec) { 50 if (llvm::any_of(m_archs, [spec](const ArchSpec &rhs) { 51 return spec.IsExactMatch(rhs); 52 })) 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 llvm::StringRef PlatformWindows::GetPluginDescriptionStatic(bool is_host) { 105 return is_host ? "Local Windows user platform plug-in." 106 : "Remote Windows user platform plug-in."; 107 } 108 109 void PlatformWindows::Initialize() { 110 Platform::Initialize(); 111 112 if (g_initialize_count++ == 0) { 113 #if defined(_WIN32) 114 // Force a host flag to true for the default platform object. 115 PlatformSP default_platform_sp(new PlatformWindows(true)); 116 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 117 Platform::SetHostPlatform(default_platform_sp); 118 #endif 119 PluginManager::RegisterPlugin( 120 PlatformWindows::GetPluginNameStatic(false), 121 PlatformWindows::GetPluginDescriptionStatic(false), 122 PlatformWindows::CreateInstance); 123 } 124 } 125 126 void PlatformWindows::Terminate() { 127 if (g_initialize_count > 0) { 128 if (--g_initialize_count == 0) { 129 PluginManager::UnregisterPlugin(PlatformWindows::CreateInstance); 130 } 131 } 132 133 Platform::Terminate(); 134 } 135 136 /// Default Constructor 137 PlatformWindows::PlatformWindows(bool is_host) : RemoteAwarePlatform(is_host) {} 138 139 Status PlatformWindows::ConnectRemote(Args &args) { 140 Status error; 141 if (IsHost()) { 142 error.SetErrorStringWithFormatv( 143 "can't connect to the host platform '{0}', always connected", 144 GetPluginName()); 145 } else { 146 if (!m_remote_platform_sp) 147 m_remote_platform_sp = 148 Platform::Create(ConstString("remote-gdb-server"), error); 149 150 if (m_remote_platform_sp) { 151 if (error.Success()) { 152 if (m_remote_platform_sp) { 153 error = m_remote_platform_sp->ConnectRemote(args); 154 } else { 155 error.SetErrorString( 156 "\"platform connect\" takes a single argument: <connect-url>"); 157 } 158 } 159 } else 160 error.SetErrorString("failed to create a 'remote-gdb-server' platform"); 161 162 if (error.Fail()) 163 m_remote_platform_sp.reset(); 164 } 165 166 return error; 167 } 168 169 Status PlatformWindows::DisconnectRemote() { 170 Status error; 171 172 if (IsHost()) { 173 error.SetErrorStringWithFormatv( 174 "can't disconnect from the host platform '{0}', always connected", 175 GetPluginName()); 176 } else { 177 if (m_remote_platform_sp) 178 error = m_remote_platform_sp->DisconnectRemote(); 179 else 180 error.SetErrorString("the platform is not currently connected"); 181 } 182 return error; 183 } 184 185 ProcessSP PlatformWindows::DebugProcess(ProcessLaunchInfo &launch_info, 186 Debugger &debugger, Target &target, 187 Status &error) { 188 // Windows has special considerations that must be followed when launching or 189 // attaching to a process. The key requirement is that when launching or 190 // attaching to a process, you must do it from the same the thread that will 191 // go into a permanent loop which will then receive debug events from the 192 // process. In particular, this means we can't use any of LLDB's generic 193 // mechanisms to do it for us, because it doesn't have the special knowledge 194 // required for setting up the background thread or passing the right flags. 195 // 196 // Another problem is that that LLDB's standard model for debugging a process 197 // is to first launch it, have it stop at the entry point, and then attach to 198 // it. In Windows this doesn't quite work, you have to specify as an 199 // argument to CreateProcess() that you're going to debug the process. So we 200 // override DebugProcess here to handle this. Launch operations go directly 201 // to the process plugin, and attach operations almost go directly to the 202 // process plugin (but we hijack the events first). In essence, we 203 // encapsulate all the logic of Launching and Attaching in the process 204 // plugin, and PlatformWindows::DebugProcess is just a pass-through to get to 205 // the process plugin. 206 207 if (IsRemote()) { 208 if (m_remote_platform_sp) 209 return m_remote_platform_sp->DebugProcess(launch_info, debugger, target, 210 error); 211 else 212 error.SetErrorString("the platform is not currently connected"); 213 } 214 215 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) { 216 // This is a process attach. Don't need to launch anything. 217 ProcessAttachInfo attach_info(launch_info); 218 return Attach(attach_info, debugger, &target, error); 219 } else { 220 ProcessSP process_sp = target.CreateProcess( 221 launch_info.GetListener(), launch_info.GetProcessPluginName(), nullptr, 222 false); 223 224 // We need to launch and attach to the process. 225 launch_info.GetFlags().Set(eLaunchFlagDebug); 226 if (process_sp) 227 error = process_sp->Launch(launch_info); 228 229 return process_sp; 230 } 231 } 232 233 lldb::ProcessSP PlatformWindows::Attach(ProcessAttachInfo &attach_info, 234 Debugger &debugger, Target *target, 235 Status &error) { 236 error.Clear(); 237 lldb::ProcessSP process_sp; 238 if (!IsHost()) { 239 if (m_remote_platform_sp) 240 process_sp = 241 m_remote_platform_sp->Attach(attach_info, debugger, target, error); 242 else 243 error.SetErrorString("the platform is not currently connected"); 244 return process_sp; 245 } 246 247 if (target == nullptr) { 248 TargetSP new_target_sp; 249 FileSpec emptyFileSpec; 250 ArchSpec emptyArchSpec; 251 252 error = debugger.GetTargetList().CreateTarget( 253 debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp); 254 target = new_target_sp.get(); 255 } 256 257 if (!target || error.Fail()) 258 return process_sp; 259 260 const char *plugin_name = attach_info.GetProcessPluginName(); 261 process_sp = target->CreateProcess( 262 attach_info.GetListenerForProcess(debugger), plugin_name, nullptr, false); 263 264 process_sp->HijackProcessEvents(attach_info.GetHijackListener()); 265 if (process_sp) 266 error = process_sp->Attach(attach_info); 267 268 return process_sp; 269 } 270 271 bool PlatformWindows::GetSupportedArchitectureAtIndex(uint32_t idx, 272 ArchSpec &arch) { 273 static SupportedArchList architectures; 274 275 if (idx >= architectures.Count()) 276 return false; 277 arch = architectures[idx]; 278 return true; 279 } 280 281 void PlatformWindows::GetStatus(Stream &strm) { 282 Platform::GetStatus(strm); 283 284 #ifdef _WIN32 285 llvm::VersionTuple version = HostInfo::GetOSVersion(); 286 strm << " Host: Windows " << version.getAsString() << '\n'; 287 #endif 288 } 289 290 bool PlatformWindows::CanDebugProcess() { return true; } 291 292 ConstString PlatformWindows::GetFullNameForDylib(ConstString basename) { 293 if (basename.IsEmpty()) 294 return basename; 295 296 StreamString stream; 297 stream.Printf("%s.dll", basename.GetCString()); 298 return ConstString(stream.GetString()); 299 } 300 301 size_t 302 PlatformWindows::GetSoftwareBreakpointTrapOpcode(Target &target, 303 BreakpointSite *bp_site) { 304 ArchSpec arch = target.GetArchitecture(); 305 assert(arch.IsValid()); 306 const uint8_t *trap_opcode = nullptr; 307 size_t trap_opcode_size = 0; 308 309 switch (arch.GetMachine()) { 310 case llvm::Triple::aarch64: { 311 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x3e, 0xd4}; // brk #0xf000 312 trap_opcode = g_aarch64_opcode; 313 trap_opcode_size = sizeof(g_aarch64_opcode); 314 315 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 316 return trap_opcode_size; 317 return 0; 318 } break; 319 320 case llvm::Triple::arm: 321 case llvm::Triple::thumb: { 322 static const uint8_t g_thumb_opcode[] = {0xfe, 0xde}; // udf #0xfe 323 trap_opcode = g_thumb_opcode; 324 trap_opcode_size = sizeof(g_thumb_opcode); 325 326 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 327 return trap_opcode_size; 328 return 0; 329 } break; 330 331 default: 332 return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site); 333 } 334 } 335