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