1 //===-- PlatformWindows.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 10 #include "PlatformWindows.h" 11 12 // C Includes 13 #include <stdio.h> 14 #if defined (_WIN32) 15 #include "lldb/Host/windows/windows.h" 16 #include <winsock2.h> 17 #endif 18 19 // C++ Includes 20 // Other libraries and framework includes 21 // Project includes 22 #include "lldb/Core/Error.h" 23 #include "lldb/Core/Debugger.h" 24 #include "lldb/Core/PluginManager.h" 25 #include "lldb/Host/HostInfo.h" 26 #include "lldb/Core/ModuleSpec.h" 27 #include "lldb/Core/Module.h" 28 #include "lldb/Breakpoint/BreakpointLocation.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 static uint32_t g_initialize_count = 0; 34 35 namespace 36 { 37 class SupportedArchList 38 { 39 public: 40 SupportedArchList() 41 { 42 AddArch(ArchSpec("i686-pc-windows")); 43 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKindDefault)); 44 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind32)); 45 AddArch(HostInfo::GetArchitecture(HostInfo::eArchKind64)); 46 AddArch(ArchSpec("i386-pc-windows")); 47 } 48 49 size_t Count() const { return m_archs.size(); } 50 51 const ArchSpec& operator[](int idx) { return m_archs[idx]; } 52 53 private: 54 void AddArch(const ArchSpec& spec) 55 { 56 auto iter = std::find_if( 57 m_archs.begin(), m_archs.end(), 58 [spec](const ArchSpec& rhs) { return spec.IsExactMatch(rhs); }); 59 if (iter != m_archs.end()) 60 return; 61 if (spec.IsValid()) 62 m_archs.push_back(spec); 63 } 64 65 std::vector<ArchSpec> m_archs; 66 }; 67 } 68 69 PlatformSP 70 PlatformWindows::CreateInstance (bool force, const lldb_private::ArchSpec *arch) 71 { 72 // The only time we create an instance is when we are creating a remote 73 // windows platform 74 const bool is_host = false; 75 76 bool create = force; 77 if (create == false && arch && arch->IsValid()) 78 { 79 const llvm::Triple &triple = arch->GetTriple(); 80 switch (triple.getVendor()) 81 { 82 case llvm::Triple::PC: 83 create = true; 84 break; 85 86 case llvm::Triple::UnknownArch: 87 create = !arch->TripleVendorWasSpecified(); 88 break; 89 90 default: 91 break; 92 } 93 94 if (create) 95 { 96 switch (triple.getOS()) 97 { 98 case llvm::Triple::Win32: 99 break; 100 101 case llvm::Triple::UnknownOS: 102 create = arch->TripleOSWasSpecified(); 103 break; 104 105 default: 106 create = false; 107 break; 108 } 109 } 110 } 111 if (create) 112 return PlatformSP(new PlatformWindows (is_host)); 113 return PlatformSP(); 114 115 } 116 117 lldb_private::ConstString 118 PlatformWindows::GetPluginNameStatic(bool is_host) 119 { 120 if (is_host) 121 { 122 static ConstString g_host_name(Platform::GetHostPlatformName ()); 123 return g_host_name; 124 } 125 else 126 { 127 static ConstString g_remote_name("remote-windows"); 128 return g_remote_name; 129 } 130 } 131 132 const char * 133 PlatformWindows::GetPluginDescriptionStatic(bool is_host) 134 { 135 return is_host ? 136 "Local Windows user platform plug-in." : 137 "Remote Windows user platform plug-in."; 138 } 139 140 lldb_private::ConstString 141 PlatformWindows::GetPluginName(void) 142 { 143 return GetPluginNameStatic(IsHost()); 144 } 145 146 void 147 PlatformWindows::Initialize(void) 148 { 149 Platform::Initialize (); 150 151 if (g_initialize_count++ == 0) 152 { 153 #if defined (_WIN32) 154 WSADATA dummy; 155 WSAStartup(MAKEWORD(2,2), &dummy); 156 // Force a host flag to true for the default platform object. 157 PlatformSP default_platform_sp (new PlatformWindows(true)); 158 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 159 Platform::SetHostPlatform (default_platform_sp); 160 #endif 161 PluginManager::RegisterPlugin(PlatformWindows::GetPluginNameStatic(false), 162 PlatformWindows::GetPluginDescriptionStatic(false), 163 PlatformWindows::CreateInstance); 164 } 165 } 166 167 void 168 PlatformWindows::Terminate( void ) 169 { 170 if (g_initialize_count > 0) 171 { 172 if (--g_initialize_count == 0) 173 { 174 #ifdef _WIN32 175 WSACleanup(); 176 #endif 177 PluginManager::UnregisterPlugin (PlatformWindows::CreateInstance); 178 } 179 } 180 181 Platform::Terminate (); 182 } 183 184 //------------------------------------------------------------------ 185 /// Default Constructor 186 //------------------------------------------------------------------ 187 PlatformWindows::PlatformWindows (bool is_host) : 188 Platform(is_host) 189 { 190 } 191 192 //------------------------------------------------------------------ 193 /// Destructor. 194 /// 195 /// The destructor is virtual since this class is designed to be 196 /// inherited from by the plug-in instance. 197 //------------------------------------------------------------------ 198 PlatformWindows::~PlatformWindows() 199 { 200 } 201 202 Error 203 PlatformWindows::ResolveExecutable (const ModuleSpec &ms, 204 lldb::ModuleSP &exe_module_sp, 205 const FileSpecList *module_search_paths_ptr) 206 { 207 Error error; 208 // Nothing special to do here, just use the actual file and architecture 209 210 char exe_path[PATH_MAX]; 211 ModuleSpec resolved_module_spec(ms); 212 213 if (IsHost()) 214 { 215 // if we cant resolve the executable loation based on the current path variables 216 if (!resolved_module_spec.GetFileSpec().Exists()) 217 { 218 resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 219 resolved_module_spec.GetFileSpec().SetFile(exe_path, true); 220 } 221 222 if (!resolved_module_spec.GetFileSpec().Exists()) 223 resolved_module_spec.GetFileSpec().ResolveExecutableLocation (); 224 225 if (resolved_module_spec.GetFileSpec().Exists()) 226 error.Clear(); 227 else 228 { 229 ms.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 230 error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 231 } 232 } 233 else 234 { 235 if (m_remote_platform_sp) 236 { 237 error = m_remote_platform_sp->ResolveExecutable (ms, 238 exe_module_sp, 239 NULL); 240 } 241 else 242 { 243 // We may connect to a process and use the provided executable (Don't use local $PATH). 244 if (resolved_module_spec.GetFileSpec().Exists()) 245 error.Clear(); 246 else 247 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 248 } 249 } 250 251 if (error.Success()) 252 { 253 if (resolved_module_spec.GetArchitecture().IsValid()) 254 { 255 error = ModuleList::GetSharedModule (resolved_module_spec, 256 exe_module_sp, 257 NULL, 258 NULL, 259 NULL); 260 261 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 262 { 263 exe_module_sp.reset(); 264 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 265 resolved_module_spec.GetFileSpec().GetPath().c_str(), 266 resolved_module_spec.GetArchitecture().GetArchitectureName()); 267 } 268 } 269 else 270 { 271 // No valid architecture was specified, ask the platform for 272 // the architectures that we should be using (in the correct order) 273 // and see if we can find a match that way 274 StreamString arch_names; 275 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx) 276 { 277 error = ModuleList::GetSharedModule (resolved_module_spec, 278 exe_module_sp, 279 NULL, 280 NULL, 281 NULL); 282 // Did we find an executable using one of the 283 if (error.Success()) 284 { 285 if (exe_module_sp && exe_module_sp->GetObjectFile()) 286 break; 287 else 288 error.SetErrorToGenericError(); 289 } 290 291 if (idx > 0) 292 arch_names.PutCString (", "); 293 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName()); 294 } 295 296 if (error.Fail() || !exe_module_sp) 297 { 298 if (resolved_module_spec.GetFileSpec().Readable()) 299 { 300 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 301 resolved_module_spec.GetFileSpec().GetPath().c_str(), 302 GetPluginName().GetCString(), 303 arch_names.GetString().c_str()); 304 } 305 else 306 { 307 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str()); 308 } 309 } 310 } 311 } 312 313 return error; 314 } 315 316 size_t 317 PlatformWindows::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site) 318 { 319 ArchSpec arch = target.GetArchitecture(); 320 const uint8_t *trap_opcode = NULL; 321 size_t trap_opcode_size = 0; 322 323 switch (arch.GetMachine()) 324 { 325 case llvm::Triple::x86: 326 case llvm::Triple::x86_64: 327 { 328 static const uint8_t g_i386_opcode[] = { 0xCC }; 329 trap_opcode = g_i386_opcode; 330 trap_opcode_size = sizeof(g_i386_opcode); 331 } 332 break; 333 334 case llvm::Triple::hexagon: 335 { 336 static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 337 trap_opcode = g_hex_opcode; 338 trap_opcode_size = sizeof(g_hex_opcode); 339 } 340 break; 341 default: 342 llvm_unreachable("Unhandled architecture in PlatformWindows::GetSoftwareBreakpointTrapOpcode()"); 343 break; 344 } 345 346 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 347 return trap_opcode_size; 348 349 return 0; 350 } 351 352 bool 353 PlatformWindows::GetRemoteOSVersion () 354 { 355 if (m_remote_platform_sp) 356 return m_remote_platform_sp->GetOSVersion (m_major_os_version, 357 m_minor_os_version, 358 m_update_os_version); 359 return false; 360 } 361 362 bool 363 PlatformWindows::GetRemoteOSBuildString (std::string &s) 364 { 365 if (m_remote_platform_sp) 366 return m_remote_platform_sp->GetRemoteOSBuildString (s); 367 s.clear(); 368 return false; 369 } 370 371 bool 372 PlatformWindows::GetRemoteOSKernelDescription (std::string &s) 373 { 374 if (m_remote_platform_sp) 375 return m_remote_platform_sp->GetRemoteOSKernelDescription (s); 376 s.clear(); 377 return false; 378 } 379 380 // Remote Platform subclasses need to override this function 381 ArchSpec 382 PlatformWindows::GetRemoteSystemArchitecture () 383 { 384 if (m_remote_platform_sp) 385 return m_remote_platform_sp->GetRemoteSystemArchitecture (); 386 return ArchSpec(); 387 } 388 389 const char * 390 PlatformWindows::GetHostname () 391 { 392 if (IsHost()) 393 return Platform::GetHostname(); 394 395 if (m_remote_platform_sp) 396 return m_remote_platform_sp->GetHostname (); 397 return NULL; 398 } 399 400 bool 401 PlatformWindows::IsConnected () const 402 { 403 if (IsHost()) 404 return true; 405 else if (m_remote_platform_sp) 406 return m_remote_platform_sp->IsConnected(); 407 return false; 408 } 409 410 Error 411 PlatformWindows::ConnectRemote (Args& args) 412 { 413 Error error; 414 if (IsHost()) 415 { 416 error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetPluginName().AsCString() ); 417 } 418 else 419 { 420 if (!m_remote_platform_sp) 421 m_remote_platform_sp = Platform::Create (ConstString("remote-gdb-server"), error); 422 423 if (m_remote_platform_sp) 424 { 425 if (error.Success()) 426 { 427 if (m_remote_platform_sp) 428 { 429 error = m_remote_platform_sp->ConnectRemote (args); 430 } 431 else 432 { 433 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>"); 434 } 435 } 436 } 437 else 438 error.SetErrorString ("failed to create a 'remote-gdb-server' platform"); 439 440 if (error.Fail()) 441 m_remote_platform_sp.reset(); 442 } 443 444 return error; 445 } 446 447 Error 448 PlatformWindows::DisconnectRemote () 449 { 450 Error error; 451 452 if (IsHost()) 453 { 454 error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetPluginName().AsCString() ); 455 } 456 else 457 { 458 if (m_remote_platform_sp) 459 error = m_remote_platform_sp->DisconnectRemote (); 460 else 461 error.SetErrorString ("the platform is not currently connected"); 462 } 463 return error; 464 } 465 466 bool 467 PlatformWindows::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 468 { 469 bool success = false; 470 if (IsHost()) 471 { 472 success = Platform::GetProcessInfo (pid, process_info); 473 } 474 else if (m_remote_platform_sp) 475 { 476 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 477 } 478 return success; 479 } 480 481 uint32_t 482 PlatformWindows::FindProcesses (const ProcessInstanceInfoMatch &match_info, 483 ProcessInstanceInfoList &process_infos) 484 { 485 uint32_t match_count = 0; 486 if (IsHost()) 487 { 488 // Let the base class figure out the host details 489 match_count = Platform::FindProcesses (match_info, process_infos); 490 } 491 else 492 { 493 // If we are remote, we can only return results if we are connected 494 if (m_remote_platform_sp) 495 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos); 496 } 497 return match_count; 498 } 499 500 Error 501 PlatformWindows::LaunchProcess (ProcessLaunchInfo &launch_info) 502 { 503 Error error; 504 if (IsHost()) 505 { 506 error = Platform::LaunchProcess (launch_info); 507 } 508 else 509 { 510 if (m_remote_platform_sp) 511 error = m_remote_platform_sp->LaunchProcess (launch_info); 512 else 513 error.SetErrorString ("the platform is not currently connected"); 514 } 515 return error; 516 } 517 518 lldb::ProcessSP 519 PlatformWindows::Attach(ProcessAttachInfo &attach_info, 520 Debugger &debugger, 521 Target *target, 522 Error &error) 523 { 524 lldb::ProcessSP process_sp; 525 if (IsHost()) 526 { 527 if (target == NULL) 528 { 529 TargetSP new_target_sp; 530 FileSpec emptyFileSpec; 531 ArchSpec emptyArchSpec; 532 533 error = debugger.GetTargetList().CreateTarget (debugger, 534 NULL, 535 NULL, 536 false, 537 NULL, 538 new_target_sp); 539 target = new_target_sp.get(); 540 } 541 else 542 error.Clear(); 543 544 if (target && error.Success()) 545 { 546 debugger.GetTargetList().SetSelectedTarget(target); 547 // The Windows platform always currently uses the GDB remote debugger plug-in 548 // so even when debugging locally we are debugging remotely! 549 // Just like the darwin plugin. 550 process_sp = target->CreateProcess (attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL); 551 552 if (process_sp) 553 error = process_sp->Attach (attach_info); 554 } 555 } 556 else 557 { 558 if (m_remote_platform_sp) 559 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, error); 560 else 561 error.SetErrorString ("the platform is not currently connected"); 562 } 563 return process_sp; 564 } 565 566 const char * 567 PlatformWindows::GetUserName (uint32_t uid) 568 { 569 // Check the cache in Platform in case we have already looked this uid up 570 const char *user_name = Platform::GetUserName(uid); 571 if (user_name) 572 return user_name; 573 574 if (IsRemote() && m_remote_platform_sp) 575 return m_remote_platform_sp->GetUserName(uid); 576 return NULL; 577 } 578 579 const char * 580 PlatformWindows::GetGroupName (uint32_t gid) 581 { 582 const char *group_name = Platform::GetGroupName(gid); 583 if (group_name) 584 return group_name; 585 586 if (IsRemote() && m_remote_platform_sp) 587 return m_remote_platform_sp->GetGroupName(gid); 588 return NULL; 589 } 590 591 Error 592 PlatformWindows::GetFileWithUUID (const FileSpec &platform_file, 593 const UUID *uuid_ptr, 594 FileSpec &local_file) 595 { 596 if (IsRemote()) 597 { 598 if (m_remote_platform_sp) 599 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 600 } 601 602 // Default to the local case 603 local_file = platform_file; 604 return Error(); 605 } 606 607 Error 608 PlatformWindows::GetSharedModule (const ModuleSpec &module_spec, 609 ModuleSP &module_sp, 610 const FileSpecList *module_search_paths_ptr, 611 ModuleSP *old_module_sp_ptr, 612 bool *did_create_ptr) 613 { 614 Error error; 615 module_sp.reset(); 616 617 if (IsRemote()) 618 { 619 // If we have a remote platform always, let it try and locate 620 // the shared module first. 621 if (m_remote_platform_sp) 622 { 623 error = m_remote_platform_sp->GetSharedModule (module_spec, 624 module_sp, 625 module_search_paths_ptr, 626 old_module_sp_ptr, 627 did_create_ptr); 628 } 629 } 630 631 if (!module_sp) 632 { 633 // Fall back to the local platform and find the file locally 634 error = Platform::GetSharedModule (module_spec, 635 module_sp, 636 module_search_paths_ptr, 637 old_module_sp_ptr, 638 did_create_ptr); 639 } 640 if (module_sp) 641 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 642 return error; 643 } 644 645 bool 646 PlatformWindows::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 647 { 648 static SupportedArchList architectures; 649 650 if (idx >= architectures.Count()) 651 return false; 652 arch = architectures[idx]; 653 return true; 654 } 655 656 void 657 PlatformWindows::GetStatus (Stream &strm) 658 { 659 Platform::GetStatus(strm); 660 661 #ifdef _WIN32 662 uint32_t major; 663 uint32_t minor; 664 uint32_t update; 665 if (!HostInfo::GetOSVersion(major, minor, update)) 666 { 667 strm << "Windows"; 668 return; 669 } 670 671 strm << "Host: Windows " << major 672 << '.' << minor 673 << " Build: " << update << '\n'; 674 #endif 675 } 676